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

Subversion Repositories plasma

[/] [plasma/] [tags/] [V3_0/] [kernel/] [uart.c] - Blame information for rev 393

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 138 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma Uart Driver
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 12/31/05
5
 * FILENAME: uart.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Plasma Uart Driver
11
 *--------------------------------------------------------------------*/
12
#define NO_ELLIPSIS2
13
#include "plasma.h"
14
#include "rtos.h"
15
 
16
#define BUFFER_WRITE_SIZE 128
17
#define BUFFER_READ_SIZE 128
18
#define BUFFER_PRINTF_SIZE 128
19
#undef UartPrintf
20
 
21
typedef struct Buffer_s {
22
   uint8 *data;
23
   int size;
24
   volatile int read, write;
25
   volatile int pendingRead, pendingWrite;
26
   OS_Semaphore_t *semaphoreRead, *semaphoreWrite;
27
} Buffer_t;
28
 
29
static Buffer_t *WriteBuffer, *ReadBuffer;
30
static OS_Semaphore_t *SemaphoreUart;
31
static char PrintfString[BUFFER_PRINTF_SIZE];  //Used in UartPrintf
32
 
33
 
34
/******************************************/
35
Buffer_t *BufferCreate(int size)
36
{
37
   Buffer_t *buffer;
38
   buffer = (Buffer_t*)OS_HeapMalloc(NULL, sizeof(Buffer_t) + size);
39
   if(buffer == NULL)
40
      return NULL;
41
   buffer->data = (uint8*)(buffer + 1);
42
   buffer->read = 0;
43
   buffer->write = 0;
44
   buffer->size = size;
45
   buffer->pendingRead = 0;
46
   buffer->pendingWrite = 0;
47
   buffer->semaphoreRead = OS_SemaphoreCreate("BufferRead", 0);
48
   buffer->semaphoreWrite = OS_SemaphoreCreate("BufferWrite", 0);
49
   return buffer;
50
}
51
 
52
 
53
void BufferWrite(Buffer_t *Buffer, int Value, int Pend)
54
{
55
   int writeNext;
56
 
57
   writeNext = Buffer->write + 1;
58
   if(writeNext >= Buffer->size)
59
      writeNext = 0;
60
 
61
   //Check if room for value
62
   if(writeNext == Buffer->read)
63
   {
64
      if(Pend == 0)
65
         return;
66
      ++Buffer->pendingWrite;
67
      OS_SemaphorePend(Buffer->semaphoreWrite, OS_WAIT_FOREVER);
68
   }
69
 
70
   Buffer->data[Buffer->write] = (uint8)Value;
71
   Buffer->write = writeNext;
72
   if(Buffer->pendingRead)
73
   {
74
      --Buffer->pendingRead;
75
      OS_SemaphorePost(Buffer->semaphoreRead);
76
   }
77
}
78
 
79
 
80
int BufferRead(Buffer_t *Buffer, int Pend)
81
{
82
   int value;
83
 
84
   //Check if empty buffer
85
   if(Buffer->read == Buffer->write)
86
   {
87
      if(Pend == 0)
88
         return 0;
89
      ++Buffer->pendingRead;
90
      OS_SemaphorePend(Buffer->semaphoreRead, OS_WAIT_FOREVER);
91
   }
92
 
93
   value = Buffer->data[Buffer->read];
94
   if(++Buffer->read >= Buffer->size)
95
      Buffer->read = 0;
96
   if(Buffer->pendingWrite)
97
   {
98
      --Buffer->pendingWrite;
99
      OS_SemaphorePost(Buffer->semaphoreWrite);
100
   }
101
   return value;
102
}
103
 
104
 
105
/******************************************/
106
static void UartInterrupt(void *Arg)
107
{
108
   uint32 status, value;
109
   (void)Arg;
110
 
111
   status = OS_InterruptStatus();
112
   if(status & IRQ_UART_READ_AVAILABLE)
113
   {
114
      value = MemoryRead(UART_READ);
115
      BufferWrite(ReadBuffer, value, 0);
116
   }
117
   if(status & IRQ_UART_WRITE_AVAILABLE)
118
   {
119
      if(WriteBuffer->read != WriteBuffer->write)
120
      {
121
         value = BufferRead(WriteBuffer, 0);
122
         MemoryWrite(UART_WRITE, value);
123
      }
124
      else
125
      {
126
         OS_InterruptMaskClear(IRQ_UART_WRITE_AVAILABLE);
127
      }
128
   }
129
}
130
 
131
 
132
void UartInit(void)
133
{
134
   uint32 mask;
135
 
136
   SemaphoreUart = OS_SemaphoreCreate("Uart", 1);
137
   WriteBuffer = BufferCreate(BUFFER_WRITE_SIZE);
138
   ReadBuffer = BufferCreate(BUFFER_READ_SIZE);
139
 
140
   mask = IRQ_UART_READ_AVAILABLE | IRQ_UART_WRITE_AVAILABLE;
141
   OS_InterruptRegister(mask, UartInterrupt);
142
   OS_InterruptMaskSet(IRQ_UART_READ_AVAILABLE);
143
}
144
 
145
 
146
void UartWrite(int C)
147
{
148
   BufferWrite(WriteBuffer, C, 1);
149
   OS_InterruptMaskSet(IRQ_UART_WRITE_AVAILABLE);
150
}
151
 
152
 
153
uint8 UartRead(void)
154
{
155
   return (uint8)BufferRead(ReadBuffer, 1);
156
}
157
 
158
 
159
void UartWriteData(uint8 *Data, int Length)
160
{
161
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
162
   while(Length--)
163
      UartWrite(*Data++);
164
   OS_SemaphorePost(SemaphoreUart);
165
}
166
 
167
 
168
void UartReadData(uint8 *Data, int Length)
169
{
170
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
171
   while(Length--)
172
      *Data++ = UartRead();
173
   OS_SemaphorePost(SemaphoreUart);
174
}
175
 
176
 
177
void UartPrintf(const char *format,
178
                int arg0, int arg1, int arg2, int arg3,
179
                int arg4, int arg5, int arg6, int arg7)
180
{
181
   uint8 *ptr;
182
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
183
   sprintf(PrintfString, format, arg0, arg1, arg2, arg3,
184
           arg4, arg5, arg6, arg7);
185
   ptr = (uint8*)PrintfString;
186
   while(*ptr)
187
   {
188
      if(*ptr == '\n')
189
         UartWrite('\r');
190
      UartWrite(*ptr++);
191
   }
192
   OS_SemaphorePost(SemaphoreUart);
193
}
194
 
195
 
196
void UartPrintfPoll(const char *format,
197
                    int arg0, int arg1, int arg2, int arg3,
198
                    int arg4, int arg5, int arg6, int arg7)
199
{
200
   uint8 *ptr;
201
 
202
   if(SemaphoreUart)
203
      OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
204
   sprintf(PrintfString, format, arg0, arg1, arg2, arg3,
205
           arg4, arg5, arg6, arg7);
206
   ptr = (uint8*)PrintfString;
207
   while(*ptr)
208
   {
209
      while((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) == 0)
210
         ;
211
      MemoryWrite(UART_WRITE, *ptr++);
212
   }
213
   if(SemaphoreUart)
214
      OS_SemaphorePost(SemaphoreUart);
215
}
216
 
217
 
218
void UartPrintfCritical(const char *format,
219
                        int arg0, int arg1, int arg2, int arg3,
220
                        int arg4, int arg5, int arg6, int arg7)
221
{
222
   uint8 *ptr;
223
   uint32 state;
224
 
225
   state = OS_CriticalBegin();
226
   sprintf(PrintfString, format, arg0, arg1, arg2, arg3,
227
           arg4, arg5, arg6, arg7);
228
   ptr = (uint8*)PrintfString;
229
   while(*ptr)
230
   {
231
      while((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) == 0)
232
         ;
233
      MemoryWrite(UART_WRITE, *ptr++);
234
   }
235
   memset(PrintfString, 0, sizeof(PrintfString));
236
   OS_CriticalEnd(state);
237
}
238
 
239
 
240
void UartScanf(const char *format,
241
               int arg0, int arg1, int arg2, int arg3,
242
               int arg4, int arg5, int arg6, int arg7)
243
{
244
   int index = 0, ch;
245
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
246
   for(;;)
247
   {
248
      ch = UartRead();
249
      if(ch != '\b' || index)
250
         UartWrite(ch);
251
      if(ch == '\n' || ch == '\r')
252
         break;
253
      else if(ch == '\b')
254
      {
255
         if(index)
256
         {
257
            UartWrite(' ');
258
            UartWrite(ch);
259
            --index;
260
         }
261
      }
262
      else if(index < sizeof(PrintfString))
263
         PrintfString[index++] = (uint8)ch;
264
   }
265
   UartWrite('\n');
266
   PrintfString[index] = 0;
267
   sscanf(PrintfString, format, arg0, arg1, arg2, arg3,
268
          arg4, arg5, arg6, arg7);
269
   OS_SemaphorePost(SemaphoreUart);
270
}
271
 
272
 
273
/******************************************/
274
#ifndef WIN32
275
int puts(const char *string)
276
{
277
   uint8 *ptr;
278
   OS_SemaphorePend(SemaphoreUart, OS_WAIT_FOREVER);
279
   ptr = (uint8*)string;
280
   while(*ptr)
281
   {
282
      if(*ptr == '\n')
283
         UartWrite('\r');
284
      UartWrite(*ptr++);
285
   }
286
   OS_SemaphorePost(SemaphoreUart);
287
   return 0;
288
}
289
 
290
 
291
int getch(void)
292
{
293
   return BufferRead(ReadBuffer, 1);
294
}
295
 
296
 
297
int kbhit(void)
298
{
299
   return ReadBuffer->read != ReadBuffer->write;
300
}
301
#endif
302
 
303
 
304
/******************************************/
305
#if 0
306
int LogArray[100], LogIndex;
307
void LogWrite(int a)
308
{
309
   if(LogIndex < sizeof(LogArray)/4)
310
      LogArray[LogIndex++] = a;
311
}
312
 
313
void LogDump(void)
314
{
315
   int i;
316
   for(i = 0; i < LogIndex; ++i)
317
   {
318
      if(LogArray[i] > 0xfff)
319
         UartPrintfCritical("\n", 0,0,0,0,0,0,0,0);
320
      UartPrintfCritical("0x%x ", LogArray[i], 0,0,0,0,0,0,0);
321
   }
322
   LogIndex = 0;
323
}
324
#endif
325
 

powered by: WebSVN 2.1.0

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