/
uart.c
464 lines (333 loc) · 10.5 KB
/
uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_usart.h>
#include <misc.h>
#include "uart.h"
int RxOverflow = 0;
// TxPrimed is used to signal that Tx send buffer needs to be primed
// to commence sending -- it is cleared by the IRQ, set by uart_write
static int TxPrimed = 0;
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
struct Queue {
uint16_t pRD, pWR;
uint8_t q[QUEUE_SIZE];
};
static struct Queue UART1_TXq, UART1_RXq;
static int QueueFull(struct Queue *q)
{
return (((q->pWR + 1) % QUEUE_SIZE) == q->pRD);
}
static int QueueEmpty(struct Queue *q)
{
return (q->pWR == q->pRD);
}
/*static int QueueAvail(struct Queue *q)
{
return (QUEUE_SIZE + q->pWR - q->pRD) % QUEUE_SIZE;
}*/
static int Enqueue(struct Queue *q, const uint8_t *data, uint16_t len)
{
int i;
for (i = 0; !QueueFull(q) && (i < len); i++)
{
q->q[q->pWR] = data[i];
q->pWR = ((q->pWR + 1) == QUEUE_SIZE) ? 0 : q->pWR + 1;
}
return i;
}
static int Dequeue(struct Queue *q, uint8_t *data, uint16_t len)
{
int i;
for (i = 0; !QueueEmpty(q) && (i < len); i++)
{
data[i] = q->q[q->pRD];
q->pRD = ((q->pRD + 1) == QUEUE_SIZE) ? 0 : q->pRD + 1;
}
return i;
}
static void InitQueue(struct Queue *q)
{
/*int i;
for (i = 0; i < QUEUE_SIZE; i++)
{
q->q[i] = 0;
}*/
q->pRD = 0;
q->pWR = 0;
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/*int uart_close(uint8_t uart)
{
}*/
int uart_open (uint8_t uart, uint32_t baud, uint32_t flags)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
if (uart == 1) {
// get things to a known state
USART_DeInit(USART1);
// Enable clock for GPIOA
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
// Turn on clocks for USART1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// DEBUG
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure TX pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure RX pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO_Mode_IN;//GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#ifdef HWFLOWCTRL
// Configure CTS pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure RTS pin -- software controlled
GPIO_WriteBit(GPIOA, GPIO_Pin_12, 1); //TODO // nRTS disabled
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
// Configure the UART
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = baud;
#ifdef HWFLOWCTRL
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS;
#else
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
#endif
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
// Enable RX Interrupt. TX interrupt enabled in send routine
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//disable Transmit Data Register empty interrupt
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
//enable Receive Data register not empty interrupt
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
InitQueue(&UART1_RXq);
InitQueue(&UART1_TXq);
// Configure NVIC
/* Configure the NVIC Preemption Priority Bits */
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Enable USART1
USART_Cmd(USART1, ENABLE);
#ifdef HWFLOWCTRL
// nRTS enabled
GPIO_WriteBit(GPIOA, GPIO_Pin_12, 0);
#endif
return 0;
}
return 1; // only handle UART2
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t data;
// clear the interrupt
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
// buffer the data (or toss it if there's no room
// Flow control is supposed to prevent this
data = USART_ReceiveData(USART1) & 0xff;
if (!Enqueue(&UART1_RXq, &data, 1))
RxOverflow = 1;
#ifdef HWFLOWCTRL
// If queue is above high water mark, disable nRTS
if (QueueAvail(&UART1_RXq) > HIGH_WATER)
GPIO_WriteBit(GPIOA, GPIO_Pin_11, 1);
#endif
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 1);
// Write one byte to the transmit data register
uint8_t data;
if (Dequeue(&UART1_TXq, &data, 1))
{
USART_SendData(USART1, data);
}
else
{
// if we have nothing to send, disable the interrupt
// and wait for a kick
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
TxPrimed = 0;
}
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 0);
}
}
/*
void USART1_IRQHandler(void)
{
static int tx_index = 0;
static int rx_index = 0;
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop
{
USART_SendData(USART1, StringLoop[tx_index++]);
if (tx_index >= (sizeof(StringLoop) - 1))
tx_index = 0;
}
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string
{
StringLoop[rx_index++] = USART_ReceiveData(USART1);
if (rx_index >= (sizeof(StringLoop) - 1))
rx_index = 0;
}
}*/
uint16_t uart_write(uint8_t uart, const uint8_t *buf, uint16_t nbyte)
{
uint8_t data;
int i = 0;
if (uart == 1 && nbyte)
{
i = Enqueue(&UART1_TXq, buf, nbyte);
// if we added something and the Transmitter isn't working
// give it a kick by turning on the buffer empty interrupt
if (!TxPrimed)
{
TxPrimed = 1;
// This implementation guarantees that USART_IT_Config
// is not called simultaneously in the interrupt handler and here.
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
}
return i;
}
uint16_t uart_read (uint8_t uart, uint8_t *buf, uint16_t nbyte)
{
int i = 0;
if (uart == 1)
{
i = Dequeue(&UART1_RXq, buf, nbyte);
#ifdef HWFLOWCTRL
// If the queue has fallen below high water mark, enable nRTS
if (QueueAvail(&UART1_RXq) <= HIGH_WATER)
GPIO_WriteBit(GPIOA, GPIO_Pin_11, 0);
#endif
}
return i;
}
/*
uint8_t _putchar(const uint8_t c)
{
while (! Enqueue (& UART2_TXq , &c, 1))
if (! TxPrimed ) {
TxPrimed = 1;
USART_ITConfig (USART1 , USART_IT_TXE , ENABLE );
}
}
uint8_t _getchar(void)
{
uint8_t data;
while (! Dequeue (& UART2_RXq , &data, 1));
return data;
}
uint8_t getchar (void)
{
uint8_t data;
while ( Dequeue (& UART2_RXq , &data , 1) != 1);
#ifdef HWFLOWCTRL
// If the queue has fallen below high water mark, enable nRTS
if (QueueAvail(&UAR2_RXq) <= HIGH_WATER)
GPIO_WriteBit(GPIOD, GPIO_Pin_3, 0);
#endif
return data;
}
*/
/*
int uart_open ( USART_TypeDef * USARTx , uint32_t baud , uint32_t flags)
{
GPIO_InitTypeDef GPIO_InitStruct ;
GPIO_StructInit (& GPIO_InitStruct );
// Initialize USART1_Tx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 ; //PIO_Pin_6 ; //GPIO_Pin_5 ;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP ;
GPIO_Init (GPIOD , & GPIO_InitStruct );
// Initialize USART1_RX
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 ; //GPIO_Pin_5 ;//GPIO_Pin_6 ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING ;
GPIO_Init (GPIOD , & GPIO_InitStruct );
// Remap USART, as USART1 is used as alternate pins on PD8/9
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
// Connect USART pins to AF
//GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART1);
//GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART1);
// see stm32f9x_usart.h
USART_InitTypeDef USART_InitStructure ;
// Initialize USART structure
USART_StructInit (&USART_InitStructure );
// Modify USART_InitStructure for non - default values , e.g.
// USART_InitStructure . USART_BaudRate = 38400;
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx ;
USART_Init (USARTx ,&USART_InitStructure );
USART_Cmd (USARTx , ENABLE );
return 0;
}
int uart_close ( USART_TypeDef * USARTx )
{
USART_Cmd (USARTx , DISABLE );
return 0;
}
int uart_putc (int c, USART_TypeDef * USARTx )
{
while ( USART_GetFlagStatus (USARTx , USART_FLAG_TXE ) == RESET);
USARTx->DR = (c & 0xff);
return 0;
}
int uart_getc ( USART_TypeDef * USARTx )
{
while ( USART_GetFlagStatus (USARTx , USART_FLAG_RXNE ) == RESET);
return USARTx->DR & 0xff;
}
int uart_puts (char* string , USART_TypeDef * USARTx )
{
for(char* it = string; *it; ++it) {
uart_putc(*it, USARTx);
}
return 0;
}
char* uart_gets (USART_TypeDef * USARTx )
{
return "";
}
*/