OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [uart.c] - Diff between revs 138 and 152

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 138 Rev 152
Line 11... Line 11...
 *--------------------------------------------------------------------*/
 *--------------------------------------------------------------------*/
#define NO_ELLIPSIS2
#define NO_ELLIPSIS2
#include "plasma.h"
#include "plasma.h"
#include "rtos.h"
#include "rtos.h"
 
 
 
#define SUPPORT_DATA_PACKETS
 
 
#define BUFFER_WRITE_SIZE 128
#define BUFFER_WRITE_SIZE 128
#define BUFFER_READ_SIZE 128
#define BUFFER_READ_SIZE 128
#define BUFFER_PRINTF_SIZE 128
#define BUFFER_PRINTF_SIZE 1024
#undef UartPrintf
#undef UartPrintf
 
 
typedef struct Buffer_s {
typedef struct Buffer_s {
   uint8 *data;
   uint8 *data;
   int size;
   int size;
Line 28... Line 30...
 
 
static Buffer_t *WriteBuffer, *ReadBuffer;
static Buffer_t *WriteBuffer, *ReadBuffer;
static OS_Semaphore_t *SemaphoreUart;
static OS_Semaphore_t *SemaphoreUart;
static char PrintfString[BUFFER_PRINTF_SIZE];  //Used in UartPrintf
static char PrintfString[BUFFER_PRINTF_SIZE];  //Used in UartPrintf
 
 
 
#ifdef SUPPORT_DATA_PACKETS
 
//For packet processing [0xff lengthMSB lengthLSB checksum data]
 
static PacketGetFunc_t UartPacketGet;
 
static uint8 *PacketCurrent;
 
static uint32 UartPacketSize;
 
static uint32 UartPacketChecksum, Checksum;
 
static OS_MQueue_t *UartPacketMQueue;
 
static uint32 PacketBytes, PacketLength;
 
static uint32 UartPacketOutLength, UartPacketOutByte;
 
int CountOk, CountError;
 
#endif
 
static uint8 *UartPacketOut;
 
 
 
 
/******************************************/
/******************************************/
Buffer_t *BufferCreate(int size)
Buffer_t *BufferCreate(int size)
{
{
   Buffer_t *buffer;
   Buffer_t *buffer;
Line 101... Line 116...
   return value;
   return value;
}
}
 
 
 
 
/******************************************/
/******************************************/
 
#ifdef SUPPORT_DATA_PACKETS
 
static void UartPacketRead(uint32 value)
 
{
 
   uint32 message[4];
 
   if(PacketBytes == 0 && value == 0xff)
 
   {
 
      ++PacketBytes;
 
   }
 
   else if(PacketBytes == 1)
 
   {
 
      ++PacketBytes;
 
      PacketLength = value << 8;
 
   }
 
   else if(PacketBytes == 2)
 
   {
 
      ++PacketBytes;
 
      PacketLength |= value;
 
      if(PacketLength <= UartPacketSize)
 
         PacketCurrent = UartPacketGet();
 
      else
 
      {
 
         PacketCurrent = NULL;
 
         PacketBytes = 0;
 
      }
 
   }
 
   else if(PacketBytes == 3)
 
   {
 
      ++PacketBytes;
 
      UartPacketChecksum = value;
 
      Checksum = 0;
 
   }
 
   else if(PacketBytes >= 4)
 
   {
 
      if(PacketCurrent)
 
         PacketCurrent[PacketBytes - 4] = (uint8)value;
 
      Checksum += value;
 
      ++PacketBytes;
 
      if(PacketBytes - 4 >= PacketLength)
 
      {
 
         if((uint8)Checksum == UartPacketChecksum)
 
         {
 
            //Notify thread that a packet have been received
 
            ++CountOk;
 
            message[0] = 0;
 
            message[1] = (uint32)PacketCurrent;
 
            message[2] = PacketLength;
 
            if(PacketCurrent)
 
               OS_MQueueSend(UartPacketMQueue, message);
 
         }
 
         else
 
         {
 
            ++CountError;
 
            //printf("E");
 
         }
 
         PacketBytes = 0;
 
      }
 
   }
 
}
 
 
 
 
 
static int UartPacketWrite(void)
 
{
 
   int value=0, i;
 
   uint32 message[4];
 
   if(UartPacketOut)
 
   {
 
      if(UartPacketOutByte == 0)
 
      {
 
         value = 0xff;
 
         ++UartPacketOutByte;
 
      }
 
      else if(UartPacketOutByte == 1)
 
      {
 
         value = UartPacketOutLength >> 8;
 
         ++UartPacketOutByte;
 
      }
 
      else if(UartPacketOutByte == 2)
 
      {
 
         value = (uint8)UartPacketOutLength;
 
         ++UartPacketOutByte;
 
      }
 
      else if(UartPacketOutByte == 3)
 
      {
 
         value = 0;
 
         for(i = 0; i < (int)UartPacketOutLength; ++i)
 
            value += UartPacketOut[i];
 
         value = (uint8)value;
 
         ++UartPacketOutByte;
 
      }
 
      else
 
      {
 
         value = UartPacketOut[UartPacketOutByte - 4];
 
         ++UartPacketOutByte;
 
         if(UartPacketOutByte - 4 >= UartPacketOutLength)
 
         {
 
            //Notify thread that a packet has been sent
 
            message[0] = 1;
 
            message[1] = (uint32)UartPacketOut;
 
            UartPacketOut = 0;
 
            OS_MQueueSend(UartPacketMQueue, message);
 
         }
 
      }
 
   }
 
   return value;
 
}
 
#endif
 
 
 
 
static void UartInterrupt(void *Arg)
static void UartInterrupt(void *Arg)
{
{
   uint32 status, value;
   uint32 status, value, count=0;
   (void)Arg;
   (void)Arg;
 
 
   status = OS_InterruptStatus();
   status = OS_InterruptStatus();
   if(status & IRQ_UART_READ_AVAILABLE)
   while(status & IRQ_UART_READ_AVAILABLE)
   {
   {
      value = MemoryRead(UART_READ);
      value = MemoryRead(UART_READ);
 
#ifdef SUPPORT_DATA_PACKETS
 
      if(UartPacketGet && (value == 0xff || PacketBytes))
 
         UartPacketRead(value);
 
      else
 
#endif
      BufferWrite(ReadBuffer, value, 0);
      BufferWrite(ReadBuffer, value, 0);
 
      status = OS_InterruptStatus();
 
      if(++count >= 16)
 
         break;
   }
   }
   if(status & IRQ_UART_WRITE_AVAILABLE)
   while(status & IRQ_UART_WRITE_AVAILABLE)
   {
   {
 
#ifdef SUPPORT_DATA_PACKETS
 
      if(UartPacketOut)
 
      {
 
         value = UartPacketWrite();
 
         MemoryWrite(UART_WRITE, value);
 
      } else
 
#endif
      if(WriteBuffer->read != WriteBuffer->write)
      if(WriteBuffer->read != WriteBuffer->write)
      {
      {
         value = BufferRead(WriteBuffer, 0);
         value = BufferRead(WriteBuffer, 0);
         MemoryWrite(UART_WRITE, value);
         MemoryWrite(UART_WRITE, value);
      }
      }
      else
      else
      {
      {
         OS_InterruptMaskClear(IRQ_UART_WRITE_AVAILABLE);
         OS_InterruptMaskClear(IRQ_UART_WRITE_AVAILABLE);
 
         break;
      }
      }
 
      status = OS_InterruptStatus();
   }
   }
}
}
 
 
 
 
void UartInit(void)
void UartInit(void)
Line 185... Line 325...
   ptr = (uint8*)PrintfString;
   ptr = (uint8*)PrintfString;
   while(*ptr)
   while(*ptr)
   {
   {
      if(*ptr == '\n')
      if(*ptr == '\n')
         UartWrite('\r');
         UartWrite('\r');
 
#if 1
 
      if(*ptr == 0xff)
 
         *ptr = '@';
 
#endif
      UartWrite(*ptr++);
      UartWrite(*ptr++);
   }
   }
   OS_SemaphorePost(SemaphoreUart);
   OS_SemaphorePost(SemaphoreUart);
}
}
 
 
Line 196... Line 340...
void UartPrintfPoll(const char *format,
void UartPrintfPoll(const char *format,
                    int arg0, int arg1, int arg2, int arg3,
                    int arg0, int arg1, int arg2, int arg3,
                    int arg4, int arg5, int arg6, int arg7)
                    int arg4, int arg5, int arg6, int arg7)
{
{
   uint8 *ptr;
   uint8 *ptr;
 
   uint32 state;
 
 
   if(SemaphoreUart)
   if(SemaphoreUart)
      OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
      OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   sprintf(PrintfString, format, arg0, arg1, arg2, arg3,
   sprintf(PrintfString, format, arg0, arg1, arg2, arg3,
           arg4, arg5, arg6, arg7);
           arg4, arg5, arg6, arg7);
   ptr = (uint8*)PrintfString;
   ptr = (uint8*)PrintfString;
   while(*ptr)
   while(*ptr)
   {
   {
      while((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) == 0)
      while((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) == 0)
         ;
         ;
 
      state = OS_CriticalBegin();
 
      if((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) &&
 
         UartPacketOut == NULL)
 
      {
      MemoryWrite(UART_WRITE, *ptr++);
      MemoryWrite(UART_WRITE, *ptr++);
   }
   }
 
      OS_CriticalEnd(state);
 
   }
   if(SemaphoreUart)
   if(SemaphoreUart)
      OS_SemaphorePost(SemaphoreUart);
      OS_SemaphorePost(SemaphoreUart);
}
}
 
 
 
 
Line 229... Line 380...
   while(*ptr)
   while(*ptr)
   {
   {
      while((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) == 0)
      while((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) == 0)
         ;
         ;
      MemoryWrite(UART_WRITE, *ptr++);
      MemoryWrite(UART_WRITE, *ptr++);
 
#ifdef SUPPORT_DATA_PACKETS
 
      if(UartPacketOut && UartPacketOutByte - 4 < UartPacketOutLength)
 
      {
 
         ++UartPacketOutByte;
 
         --ptr;
 
      }
 
#endif
   }
   }
   memset(PrintfString, 0, sizeof(PrintfString));
   memset(PrintfString, 0, sizeof(PrintfString));
   OS_CriticalEnd(state);
   OS_CriticalEnd(state);
}
}
 
 
Line 268... Line 426...
          arg4, arg5, arg6, arg7);
          arg4, arg5, arg6, arg7);
   OS_SemaphorePost(SemaphoreUart);
   OS_SemaphorePost(SemaphoreUart);
}
}
 
 
 
 
 
#ifdef SUPPORT_DATA_PACKETS
 
void UartPacketConfig(PacketGetFunc_t PacketGetFunc,
 
                      int PacketSize,
 
                      OS_MQueue_t *mQueue)
 
{
 
   UartPacketGet = PacketGetFunc;
 
   UartPacketSize = PacketSize;
 
   UartPacketMQueue = mQueue;
 
}
 
 
 
 
 
void UartPacketSend(uint8 *Data, int Bytes)
 
{
 
   UartPacketOutByte = 0;
 
   UartPacketOutLength = Bytes;
 
   UartPacketOut = Data;
 
   OS_InterruptMaskSet(IRQ_UART_WRITE_AVAILABLE);
 
}
 
#endif
 
 
 
 
 
void Led(int value)
 
{
 
   value |= 0xffffff00;
 
   MemoryWrite(GPIO0_OUT, value);  //Change LEDs
 
}
 
 
 
 
/******************************************/
/******************************************/
#ifndef WIN32
#ifndef WIN32
int puts(const char *string)
int puts(const char *string)
{
{
   uint8 *ptr;
   uint8 *ptr;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.