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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [uart.c] - Diff between revs 416 and 422

Only display areas with differences | Details | Blame | View Log

Rev 416 Rev 422
/*--------------------------------------------------------------------
/*--------------------------------------------------------------------
 * TITLE: Plasma Uart Driver
 * TITLE: Plasma Uart Driver
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
 * DATE CREATED: 12/31/05
 * DATE CREATED: 12/31/05
 * FILENAME: uart.c
 * FILENAME: uart.c
 * PROJECT: Plasma CPU core
 * PROJECT: Plasma CPU core
 * COPYRIGHT: Software placed into the public domain by the author.
 * COPYRIGHT: Software placed into the public domain by the author.
 *    Software 'as is' without warranty.  Author liable for nothing.
 *    Software 'as is' without warranty.  Author liable for nothing.
 * DESCRIPTION:
 * DESCRIPTION:
 *    Plasma Uart Driver
 *    Plasma Uart Driver
 *    UART_PACKETS permits "Ethernet" packets to be sent and received.
 *    UART_PACKETS permits "Ethernet" packets to be sent and received.
 *--------------------------------------------------------------------*/
 *--------------------------------------------------------------------*/
#define NO_ELLIPSIS2
#define NO_ELLIPSIS2
#include "plasma.h"
#include "plasma.h"
#include "rtos.h"
#include "rtos.h"
 
 
#define BUFFER_WRITE_SIZE 128
#define BUFFER_WRITE_SIZE 128
#define BUFFER_READ_SIZE 128
#define BUFFER_READ_SIZE 128
#define BUFFER_PRINTF_SIZE 1024
#define BUFFER_PRINTF_SIZE 1024
#undef UartPrintf
#undef UartPrintf
 
 
void UartPrintfCritical(const char *format,
void UartPrintfCritical(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);
 
 
typedef struct Buffer_s {
typedef struct Buffer_s {
   uint8 *data;
   uint8 *data;
   int size;
   int size;
   volatile int read, write;
   volatile int read, write;
   volatile int pendingRead, pendingWrite;
   volatile int pendingRead, pendingWrite;
   OS_Semaphore_t *semaphoreRead, *semaphoreWrite;
   OS_Semaphore_t *semaphoreRead, *semaphoreWrite;
} Buffer_t;
} Buffer_t;
 
 
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 UART_PACKETS
#ifdef UART_PACKETS
//For packet processing [0xff lengthMSB lengthLSB checksum data]
//For packet processing [0xff lengthMSB lengthLSB checksum data]
static PacketGetFunc_t UartPacketGet;
static PacketGetFunc_t UartPacketGet;
static uint8 *PacketCurrent;
static uint8 *PacketCurrent;
static uint32 UartPacketSize;
static uint32 UartPacketSize;
static uint32 UartPacketChecksum, Checksum;
static uint32 UartPacketChecksum, Checksum;
static OS_MQueue_t *UartPacketMQueue;
static OS_MQueue_t *UartPacketMQueue;
static uint32 PacketBytes, PacketLength;
static uint32 PacketBytes, PacketLength;
static uint32 UartPacketOutLength, UartPacketOutByte;
static uint32 UartPacketOutLength, UartPacketOutByte;
int CountOk, CountError;
int CountOk, CountError;
static uint8 *UartPacketOut;
static uint8 *UartPacketOut;
#endif //UART_PACKETS
#endif //UART_PACKETS
 
 
 
 
/******************************************/
/******************************************/
Buffer_t *BufferCreate(int size)
Buffer_t *BufferCreate(int size)
{
{
   Buffer_t *buffer;
   Buffer_t *buffer;
   buffer = (Buffer_t*)OS_HeapMalloc(NULL, sizeof(Buffer_t) + size);
   buffer = (Buffer_t*)OS_HeapMalloc(NULL, sizeof(Buffer_t) + size);
   if(buffer == NULL)
   if(buffer == NULL)
      return NULL;
      return NULL;
   buffer->data = (uint8*)(buffer + 1);
   buffer->data = (uint8*)(buffer + 1);
   buffer->read = 0;
   buffer->read = 0;
   buffer->write = 0;
   buffer->write = 0;
   buffer->size = size;
   buffer->size = size;
   buffer->pendingRead = 0;
   buffer->pendingRead = 0;
   buffer->pendingWrite = 0;
   buffer->pendingWrite = 0;
   buffer->semaphoreRead = OS_SemaphoreCreate("BufferRead", 0);
   buffer->semaphoreRead = OS_SemaphoreCreate("BufferRead", 0);
   buffer->semaphoreWrite = OS_SemaphoreCreate("BufferWrite", 0);
   buffer->semaphoreWrite = OS_SemaphoreCreate("BufferWrite", 0);
   return buffer;
   return buffer;
}
}
 
 
 
 
void BufferWrite(Buffer_t *buffer, int value, int pend)
void BufferWrite(Buffer_t *buffer, int value, int pend)
{
{
   int writeNext;
   int writeNext;
 
 
   writeNext = buffer->write + 1;
   writeNext = buffer->write + 1;
   if(writeNext >= buffer->size)
   if(writeNext >= buffer->size)
      writeNext = 0;
      writeNext = 0;
 
 
   //Check if room for value
   //Check if room for value
   if(writeNext == buffer->read)
   if(writeNext == buffer->read)
   {
   {
      if(pend == 0)
      if(pend == 0)
         return;
         return;
      ++buffer->pendingWrite;
      ++buffer->pendingWrite;
      OS_SemaphorePend(buffer->semaphoreWrite, OS_WAIT_FOREVER);
      OS_SemaphorePend(buffer->semaphoreWrite, OS_WAIT_FOREVER);
   }
   }
 
 
   buffer->data[buffer->write] = (uint8)value;
   buffer->data[buffer->write] = (uint8)value;
   buffer->write = writeNext;
   buffer->write = writeNext;
   if(buffer->pendingRead)
   if(buffer->pendingRead)
   {
   {
      --buffer->pendingRead;
      --buffer->pendingRead;
      OS_SemaphorePost(buffer->semaphoreRead);
      OS_SemaphorePost(buffer->semaphoreRead);
   }
   }
}
}
 
 
 
 
int BufferRead(Buffer_t *buffer, int pend)
int BufferRead(Buffer_t *buffer, int pend)
{
{
   int value;
   int value;
 
 
   //Check if empty buffer
   //Check if empty buffer
   if(buffer->read == buffer->write)
   if(buffer->read == buffer->write)
   {
   {
      if(pend == 0)
      if(pend == 0)
         return 0;
         return 0;
      ++buffer->pendingRead;
      ++buffer->pendingRead;
      OS_SemaphorePend(buffer->semaphoreRead, OS_WAIT_FOREVER);
      OS_SemaphorePend(buffer->semaphoreRead, OS_WAIT_FOREVER);
   }
   }
 
 
   value = buffer->data[buffer->read];
   value = buffer->data[buffer->read];
   if(++buffer->read >= buffer->size)
   if(++buffer->read >= buffer->size)
      buffer->read = 0;
      buffer->read = 0;
   if(buffer->pendingWrite)
   if(buffer->pendingWrite)
   {
   {
      --buffer->pendingWrite;
      --buffer->pendingWrite;
      OS_SemaphorePost(buffer->semaphoreWrite);
      OS_SemaphorePost(buffer->semaphoreWrite);
   }
   }
   return value;
   return value;
}
}
 
 
 
 
/******************************************/
/******************************************/
#ifdef UART_PACKETS
#ifdef UART_PACKETS
static void UartPacketRead(uint32 value)
static void UartPacketRead(uint32 value)
{
{
   uint32 message[4];
   uint32 message[4];
   if(PacketBytes == 0 && value == 0xff)
   if(PacketBytes == 0 && value == 0xff)
   {
   {
      ++PacketBytes;
      ++PacketBytes;
   }
   }
   else if(PacketBytes == 1)
   else if(PacketBytes == 1)
   {
   {
      ++PacketBytes;
      ++PacketBytes;
      PacketLength = value << 8;
      PacketLength = value << 8;
   }
   }
   else if(PacketBytes == 2)
   else if(PacketBytes == 2)
   {
   {
      ++PacketBytes;
      ++PacketBytes;
      PacketLength |= value;
      PacketLength |= value;
      if(PacketLength <= UartPacketSize)
      if(PacketLength <= UartPacketSize)
      {
      {
         if(PacketCurrent == NULL)
         if(PacketCurrent == NULL)
            PacketCurrent = UartPacketGet();
            PacketCurrent = UartPacketGet();
      }
      }
      else
      else
      {
      {
         PacketBytes = 0;
         PacketBytes = 0;
      }
      }
   }
   }
   else if(PacketBytes == 3)
   else if(PacketBytes == 3)
   {
   {
      ++PacketBytes;
      ++PacketBytes;
      UartPacketChecksum = value;
      UartPacketChecksum = value;
      Checksum = 0;
      Checksum = 0;
   }
   }
   else if(PacketBytes >= 4)
   else if(PacketBytes >= 4)
   {
   {
      if(PacketCurrent)
      if(PacketCurrent)
         PacketCurrent[PacketBytes - 4] = (uint8)value;
         PacketCurrent[PacketBytes - 4] = (uint8)value;
      Checksum += value;
      Checksum += value;
      ++PacketBytes;
      ++PacketBytes;
      if(PacketBytes - 4 >= PacketLength)
      if(PacketBytes - 4 >= PacketLength)
      {
      {
         if((uint8)Checksum == UartPacketChecksum)
         if((uint8)Checksum == UartPacketChecksum)
         {
         {
            //Notify thread that a packet has been received
            //Notify thread that a packet has been received
            ++CountOk;
            ++CountOk;
            message[0] = 0;
            message[0] = 0;
            message[1] = (uint32)PacketCurrent;
            message[1] = (uint32)PacketCurrent;
            message[2] = PacketLength;
            message[2] = PacketLength;
            if(PacketCurrent)
            if(PacketCurrent)
               OS_MQueueSend(UartPacketMQueue, message);
               OS_MQueueSend(UartPacketMQueue, message);
            PacketCurrent = NULL;
            PacketCurrent = NULL;
         }
         }
         else
         else
         {
         {
            ++CountError;
            ++CountError;
            //printf("E");
            //printf("E");
         }
         }
         PacketBytes = 0;
         PacketBytes = 0;
      }
      }
   }
   }
}
}
 
 
 
 
static int UartPacketWrite(void)
static int UartPacketWrite(void)
{
{
   int value=0, i;
   int value=0, i;
   uint32 message[4];
   uint32 message[4];
   if(UartPacketOut)
   if(UartPacketOut)
   {
   {
      if(UartPacketOutByte == 0)
      if(UartPacketOutByte == 0)
      {
      {
         value = 0xff;
         value = 0xff;
         ++UartPacketOutByte;
         ++UartPacketOutByte;
      }
      }
      else if(UartPacketOutByte == 1)
      else if(UartPacketOutByte == 1)
      {
      {
         value = UartPacketOutLength >> 8;
         value = UartPacketOutLength >> 8;
         ++UartPacketOutByte;
         ++UartPacketOutByte;
      }
      }
      else if(UartPacketOutByte == 2)
      else if(UartPacketOutByte == 2)
      {
      {
         value = (uint8)UartPacketOutLength;
         value = (uint8)UartPacketOutLength;
         ++UartPacketOutByte;
         ++UartPacketOutByte;
      }
      }
      else if(UartPacketOutByte == 3)
      else if(UartPacketOutByte == 3)
      {
      {
         value = 0;
         value = 0;
         for(i = 0; i < (int)UartPacketOutLength; ++i)
         for(i = 0; i < (int)UartPacketOutLength; ++i)
            value += UartPacketOut[i];
            value += UartPacketOut[i];
         value = (uint8)value;
         value = (uint8)value;
         ++UartPacketOutByte;
         ++UartPacketOutByte;
      }
      }
      else
      else
      {
      {
         value = UartPacketOut[UartPacketOutByte - 4];
         value = UartPacketOut[UartPacketOutByte - 4];
         ++UartPacketOutByte;
         ++UartPacketOutByte;
         if(UartPacketOutByte - 4 >= UartPacketOutLength)
         if(UartPacketOutByte - 4 >= UartPacketOutLength)
         {
         {
            //Notify thread that a packet has been sent
            //Notify thread that a packet has been sent
            message[0] = 1;
            message[0] = 1;
            message[1] = (uint32)UartPacketOut;
            message[1] = (uint32)UartPacketOut;
            UartPacketOut = 0;
            UartPacketOut = 0;
            OS_MQueueSend(UartPacketMQueue, message);
            OS_MQueueSend(UartPacketMQueue, message);
         }
         }
      }
      }
   }
   }
   return value;
   return value;
}
}
#endif  //UART_PACKETS
#endif  //UART_PACKETS
 
 
 
 
static void UartInterrupt(void *arg)
static void UartInterrupt(void *arg)
{
{
   uint32 status, value, count=0;
   uint32 status, value, count=0;
   (void)arg;
   (void)arg;
 
 
   status = OS_InterruptStatus();
   status = OS_InterruptStatus();
   while(status & IRQ_UART_READ_AVAILABLE)
   while(status & IRQ_UART_READ_AVAILABLE)
   {
   {
      value = MemoryRead(UART_READ);
      value = MemoryRead(UART_READ);
#ifdef UART_PACKETS
#ifdef UART_PACKETS
      if(UartPacketGet && (value == 0xff || PacketBytes))
      if(UartPacketGet && (value == 0xff || PacketBytes))
         UartPacketRead(value);
         UartPacketRead(value);
      else
      else
#endif
#endif
      BufferWrite(ReadBuffer, value, 0);
      BufferWrite(ReadBuffer, value, 0);
      status = OS_InterruptStatus();
      status = OS_InterruptStatus();
      if(++count >= 16)
      if(++count >= 16)
         break;
         break;
   }
   }
   while(status & IRQ_UART_WRITE_AVAILABLE)
   while(status & IRQ_UART_WRITE_AVAILABLE)
   {
   {
#ifdef UART_PACKETS
#ifdef UART_PACKETS
      if(UartPacketOut)
      if(UartPacketOut)
      {
      {
         value = UartPacketWrite();
         value = UartPacketWrite();
         MemoryWrite(UART_WRITE, value);
         MemoryWrite(UART_WRITE, value);
      } else
      } else
#endif
#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;
         break;
      }
      }
      status = OS_InterruptStatus();
      status = OS_InterruptStatus();
   }
   }
}
}
 
 
 
 
void UartInit(void)
void UartInit(void)
{
{
   uint32 mask;
   uint32 mask;
 
 
   SemaphoreUart = OS_SemaphoreCreate("Uart", 1);
   SemaphoreUart = OS_SemaphoreCreate("Uart", 1);
   WriteBuffer = BufferCreate(BUFFER_WRITE_SIZE);
   WriteBuffer = BufferCreate(BUFFER_WRITE_SIZE);
   ReadBuffer = BufferCreate(BUFFER_READ_SIZE);
   ReadBuffer = BufferCreate(BUFFER_READ_SIZE);
 
 
   mask = IRQ_UART_READ_AVAILABLE | IRQ_UART_WRITE_AVAILABLE;
   mask = IRQ_UART_READ_AVAILABLE | IRQ_UART_WRITE_AVAILABLE;
   OS_InterruptRegister(mask, UartInterrupt);
   OS_InterruptRegister(mask, UartInterrupt);
   OS_InterruptMaskSet(IRQ_UART_READ_AVAILABLE);
   OS_InterruptMaskSet(IRQ_UART_READ_AVAILABLE);
}
}
 
 
 
 
void UartWrite(int ch)
void UartWrite(int ch)
{
{
   BufferWrite(WriteBuffer, ch, 1);
   BufferWrite(WriteBuffer, ch, 1);
   OS_InterruptMaskSet(IRQ_UART_WRITE_AVAILABLE);
   OS_InterruptMaskSet(IRQ_UART_WRITE_AVAILABLE);
}
}
 
 
 
 
uint8 UartRead(void)
uint8 UartRead(void)
{
{
   return (uint8)BufferRead(ReadBuffer, 1);
   return (uint8)BufferRead(ReadBuffer, 1);
}
}
 
 
 
 
void UartWriteData(uint8 *data, int length)
void UartWriteData(uint8 *data, int length)
{
{
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   while(length--)
   while(length--)
      UartWrite(*data++);
      UartWrite(*data++);
   OS_SemaphorePost(SemaphoreUart);
   OS_SemaphorePost(SemaphoreUart);
}
}
 
 
 
 
void UartReadData(uint8 *data, int length)
void UartReadData(uint8 *data, int length)
{
{
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   while(length--)
   while(length--)
      *data++ = UartRead();
      *data++ = UartRead();
   OS_SemaphorePost(SemaphoreUart);
   OS_SemaphorePost(SemaphoreUart);
}
}
 
 
 
 
void UartPrintf(const char *format,
void UartPrintf(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;
#if 0
#if 0
   //Check for string "!m#~" to mask print statement
   //Check for string "!m#~" to mask print statement
   static char moduleLevel[26];
   static char moduleLevel[26];
   if(format[0] == '!' && format[3] == '~')
   if(format[0] == '!' && format[3] == '~')
   {
   {
      int level = format[2] - '5';
      int level = format[2] - '5';
      if('a' <= format[1] && format[1] <= 'z')
      if('a' <= format[1] && format[1] <= 'z')
      {
      {
         if(level < moduleLevel[format[1] - 'a'])
         if(level < moduleLevel[format[1] - 'a'])
            return;
            return;
      }
      }
      else if('A' <= format[1] && format[1] <= 'Z')
      else if('A' <= format[1] && format[1] <= 'Z')
         moduleLevel[format[1] - 'A'] = (char)level;
         moduleLevel[format[1] - 'A'] = (char)level;
      format += 4;
      format += 4;
   }
   }
#endif
#endif
   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)
   {
   {
      if(*ptr == '\n')
      if(*ptr == '\n')
         UartWrite('\r');
         UartWrite('\r');
#ifdef UART_PACKETS
#ifdef UART_PACKETS
      if(*ptr == 0xff)
      if(*ptr == 0xff)
         *ptr = '@';
         *ptr = '@';
#endif
#endif
      UartWrite(*ptr++);
      UartWrite(*ptr++);
   }
   }
   OS_SemaphorePost(SemaphoreUart);
   OS_SemaphorePost(SemaphoreUart);
}
}
 
 
 
 
#if 0
#if 0
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;
   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();
      state = OS_CriticalBegin();
      if((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) &&
      if((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) &&
         UartPacketOut == NULL)
         UartPacketOut == NULL)
      {
      {
         MemoryWrite(UART_WRITE, *ptr++);
         MemoryWrite(UART_WRITE, *ptr++);
      }
      }
      OS_CriticalEnd(state);
      OS_CriticalEnd(state);
   }
   }
   if(SemaphoreUart)
   if(SemaphoreUart)
      OS_SemaphorePost(SemaphoreUart);
      OS_SemaphorePost(SemaphoreUart);
}
}
#endif
#endif
 
 
 
 
void UartPrintfCritical(const char *format,
void UartPrintfCritical(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)
{
{
   char buffer[128];
   char buffer[128];
   uint8 *ptr;
   uint8 *ptr;
   uint32 state;
   uint32 state;
 
 
   state = OS_CriticalBegin();
   state = OS_CriticalBegin();
   sprintf(buffer, format, arg0, arg1, arg2, arg3,
   sprintf(buffer, format, arg0, arg1, arg2, arg3,
           arg4, arg5, arg6, arg7);
           arg4, arg5, arg6, arg7);
   ptr = (uint8*)buffer;
   ptr = (uint8*)buffer;
   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 UART_PACKETS
#ifdef UART_PACKETS
      if(UartPacketOut && UartPacketOutByte - 4 < UartPacketOutLength)
      if(UartPacketOut && UartPacketOutByte - 4 < UartPacketOutLength)
      {
      {
         ++UartPacketOutByte;
         ++UartPacketOutByte;
         --ptr;
         --ptr;
      }
      }
#endif
#endif
   }
   }
   OS_CriticalEnd(state);
   OS_CriticalEnd(state);
}
}
 
 
 
 
void UartScanf(const char *format,
void UartScanf(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)
{
{
   int index = 0, ch;
   int index = 0, ch;
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   for(;;)
   for(;;)
   {
   {
      ch = UartRead();
      ch = UartRead();
      if(ch != '\b' || index)
      if(ch != '\b' || index)
         UartWrite(ch);
         UartWrite(ch);
      if(ch == '\n' || ch == '\r')
      if(ch == '\n' || ch == '\r')
         break;
         break;
      else if(ch == '\b')
      else if(ch == '\b')
      {
      {
         if(index)
         if(index)
         {
         {
            UartWrite(' ');
            UartWrite(' ');
            UartWrite(ch);
            UartWrite(ch);
            --index;
            --index;
         }
         }
      }
      }
      else if(index < sizeof(PrintfString))
      else if(index < sizeof(PrintfString))
         PrintfString[index++] = (uint8)ch;
         PrintfString[index++] = (uint8)ch;
   }
   }
   UartWrite('\n');
   UartWrite('\n');
   PrintfString[index] = 0;
   PrintfString[index] = 0;
   sscanf(PrintfString, format, arg0, arg1, arg2, arg3,
   sscanf(PrintfString, format, arg0, arg1, arg2, arg3,
          arg4, arg5, arg6, arg7);
          arg4, arg5, arg6, arg7);
   OS_SemaphorePost(SemaphoreUart);
   OS_SemaphorePost(SemaphoreUart);
}
}
 
 
 
 
#ifdef UART_PACKETS
#ifdef UART_PACKETS
void UartPacketConfig(PacketGetFunc_t PacketGetFunc,
void UartPacketConfig(PacketGetFunc_t PacketGetFunc,
                      int PacketSize,
                      int PacketSize,
                      OS_MQueue_t *mQueue)
                      OS_MQueue_t *mQueue)
{
{
   UartPacketGet = PacketGetFunc;
   UartPacketGet = PacketGetFunc;
   UartPacketSize = PacketSize;
   UartPacketSize = PacketSize;
   UartPacketMQueue = mQueue;
   UartPacketMQueue = mQueue;
}
}
 
 
 
 
void UartPacketSend(uint8 *data, int bytes)
void UartPacketSend(uint8 *data, int bytes)
{
{
   UartPacketOutByte = 0;
   UartPacketOutByte = 0;
   UartPacketOutLength = bytes;
   UartPacketOutLength = bytes;
   UartPacketOut = data;
   UartPacketOut = data;
   OS_InterruptMaskSet(IRQ_UART_WRITE_AVAILABLE);
   OS_InterruptMaskSet(IRQ_UART_WRITE_AVAILABLE);
}
}
#else  //UART_PACKETS
#else  //UART_PACKETS
void UartPacketConfig(PacketGetFunc_t PacketGetFunc,
void UartPacketConfig(PacketGetFunc_t PacketGetFunc,
                      int PacketSize,
                      int PacketSize,
                      OS_MQueue_t *mQueue)
                      OS_MQueue_t *mQueue)
{ (void)PacketGetFunc; (void)PacketSize; (void)mQueue; }
{ (void)PacketGetFunc; (void)PacketSize; (void)mQueue; }
 
 
 
 
void UartPacketSend(uint8 *data, int bytes)
void UartPacketSend(uint8 *data, int bytes)
{ (void)data; (void)bytes; }
{ (void)data; (void)bytes; }
#endif
#endif
 
 
 
 
void Led(int mask, int value)
void Led(int mask, int value)
{
{
   mask &= 0xff;
   mask &= 0xff;
   MemoryWrite(GPIO0_CLEAR, mask);       //clear
   MemoryWrite(GPIO0_CLEAR, mask);       //clear
   MemoryWrite(GPIO0_OUT, value & mask); //set LEDs
   MemoryWrite(GPIO0_SET, value & mask); //set LEDs
}
}
 
 
 
 
/******************************************/
/******************************************/
int OS_puts(const char *string)
int OS_puts(const char *string)
{
{
   uint8 *ptr;
   uint8 *ptr;
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
   ptr = (uint8*)string;
   ptr = (uint8*)string;
   while(*ptr)
   while(*ptr)
   {
   {
      if(*ptr == '\n')
      if(*ptr == '\n')
         UartWrite('\r');
         UartWrite('\r');
      UartWrite(*ptr++);
      UartWrite(*ptr++);
   }
   }
   OS_SemaphorePost(SemaphoreUart);
   OS_SemaphorePost(SemaphoreUart);
   return 0;
   return 0;
}
}
 
 
 
 
int OS_getch(void)
int OS_getch(void)
{
{
   return BufferRead(ReadBuffer, 1);
   return BufferRead(ReadBuffer, 1);
}
}
 
 
 
 
int OS_kbhit(void)
int OS_kbhit(void)
{
{
   return ReadBuffer->read != ReadBuffer->write;
   return ReadBuffer->read != ReadBuffer->write;
}
}
 
 
 
 
/******************************************/
/******************************************/
#if 0
#if 0
int LogArray[100], LogIndex;
int LogArray[100], LogIndex;
void LogWrite(int a)
void LogWrite(int a)
{
{
   if(LogIndex < sizeof(LogArray)/4)
   if(LogIndex < sizeof(LogArray)/4)
      LogArray[LogIndex++] = a;
      LogArray[LogIndex++] = a;
}
}
 
 
void LogDump(void)
void LogDump(void)
{
{
   int i;
   int i;
   for(i = 0; i < LogIndex; ++i)
   for(i = 0; i < LogIndex; ++i)
   {
   {
      if(LogArray[i] > 0xfff)
      if(LogArray[i] > 0xfff)
         UartPrintfCritical("\n", 0,0,0,0,0,0,0,0);
         UartPrintfCritical("\n", 0,0,0,0,0,0,0,0);
      UartPrintfCritical("0x%x ", LogArray[i], 0,0,0,0,0,0,0);
      UartPrintfCritical("0x%x ", LogArray[i], 0,0,0,0,0,0,0);
   }
   }
   LogIndex = 0;
   LogIndex = 0;
}
}
#endif
#endif
 
 
 
 

powered by: WebSVN 2.1.0

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