serial_drv_stm32.c
25.5 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
*********************************************************************************************************
* SERIAL (BYTE) COMMUNICATION
*
* (c) Copyright 2007-2009; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may NOT be used to develop a similar product.
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* SERIAL (BYTE) COMMUNICATION
* DEVICE DRIVER
*
* STM32
*
* Filename : serial_drv_stm32.c
* Version : V2.00
* Programmer(s) : FGK
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include <serial.h>
#include <serial_drv_stm32.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
#define SR_TXE DEF_BIT_07
#define SR_TC DEF_BIT_06
#define SR_RXNE DEF_BIT_05
#define SR_ORE DEF_BIT_03
#define CR1_UE DEF_BIT_13
#define CR1_M DEF_BIT_12
#define CR1_PCE DEF_BIT_10
#define CR1_PS DEF_BIT_09
#define CR1_TXEIE DEF_BIT_07
#define CR1_TCIE DEF_BIT_06
#define CR1_RXNEIE DEF_BIT_05
#define CR1_TE DEF_BIT_03
#define CR1_RE DEF_BIT_02
#define CR1_PARITY_NONE DEF_BIT_NONE
#define CR1_PARITY_EVEN DEF_BIT_10
#define CR1_PARITY_ODD DEF_BIT_FIELD(2, 9)
#define CR2_STOP_BITS_1 DEF_BIT_MASK (0, 12)
#define CR2_STOP_BITS_2 DEF_BIT_MASK (2, 12)
#define CR2_STOP_BITS_1_5 DEF_BIT_MASK (3, 12)
#define CR2_STOP_MASK DEF_BIT_FIELD(2, 12)
#define CR2_CLKEN DEF_BIT_11
#define CR2_CPOL DEF_BIT_10
#define CR2_CPHA DEF_BIT_09
#define CR2_LBCL DEF_BIT_08
#define CR3_CTSE DEF_BIT_09
#define CR3_RTSE DEF_BIT_08
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
typedef struct serial_reg { /* Serial dev reg struct. */
CPU_REG16 SR;
CPU_REG16 RESERVED0;
CPU_REG16 DR;
CPU_REG16 RESERVED1;
CPU_REG16 BRR;
CPU_REG16 RESERVED2;
CPU_REG16 CR1;
CPU_REG16 RESERVED3;
CPU_REG16 CR2;
CPU_REG16 RESERVED4;
CPU_REG16 CR3;
CPU_REG16 RESERVED5;
CPU_REG16 GTPR;
CPU_REG16 RESERVED6;
} SERIAL_REG;
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/* Driver initialization. */
static void SerialDrv_Init (SERIAL_ERR *perr);
/* Driver open. */
static void SerialDrv_Open (SERIAL_DEV *pdev,
SERIAL_IF_CFG *pcfg,
SERIAL_ERR *perr);
/* Driver close. */
static void SerialDrv_Close (SERIAL_DEV *pdev,
SERIAL_ERR *perr);
/* Driver receiver start. */
static void SerialDrv_RxStart (SERIAL_DEV *pdev,
SERIAL_ERR *perr);
/* Driver receiver stop. */
static void SerialDrv_RxStop (SERIAL_DEV *pdev,
SERIAL_ERR *perr);
/* Driver octet receive. */
static void SerialDrv_RxOctet (SERIAL_DEV *pdev,
CPU_INT08U *pdatum,
SERIAL_ERR *perr);
/* Driver transmitter start. */
static void SerialDrv_TxStart (SERIAL_DEV *pdev,
SERIAL_ERR *perr);
/* Driver transmitter stop. */
static void SerialDrv_TxStop (SERIAL_DEV *pdev,
SERIAL_ERR *perr);
/* Driver octet transmit. */
static void SerialDrv_TxOctet (SERIAL_DEV *pdev,
CPU_INT08U datum,
SERIAL_ERR *perr);
/* Driver ISR handler. */
static void SerialDrv_ISR_Handler(SERIAL_DEV *pdev,
CPU_INT08U type);
/*
*********************************************************************************************************
*********************************************************************************************************
* SERIAL INTERFACE DEVICE DRIVER API
*********************************************************************************************************
*********************************************************************************************************
*/
SERIAL_DRV_API SerialDrv_STM32_API = {
SerialDrv_Init,
SerialDrv_Open,
SerialDrv_Close,
SerialDrv_RxStart,
SerialDrv_RxStop,
SerialDrv_RxOctet,
SerialDrv_TxStart,
SerialDrv_TxStop,
SerialDrv_TxOctet,
SerialDrv_ISR_Handler
};
/*$PAGE*/
/*
*********************************************************************************************************
*********************************************************************************************************
* DRIVER INTERFACE FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* SerialDrv_Init()
*
* Description : Initialize serial device driver.
*
* Argument(s) : perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Driver initialized.
*
* Return(s) : None.
*
* Caller(s) : Serial_DrvAdd() via 'pdrv_api->Init()'.
*
* Note(s) : None.
*********************************************************************************************************
*/
static void SerialDrv_Init (SERIAL_ERR *perr)
{
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_Open()
*
* Description : Open a serial device for communication.
*
* Argument(s) : pdev Pointer to device.
*
* pcfg Pointer to interface configuration.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device opened.
* SERIAL_ERR_DRV_OPEN Device could NOT be opened.
* SERIAL_ERR_DRV_INVALID Device configuration invalid.
* SERIAL_ERR_MEM_ALLOC Memory could NOT be allocated for device
* internal data.
*
* Return(s) : None.
*
* Caller(s) : Serial_Open() via 'pdev->Drv_API->Open()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_Open (SERIAL_DEV *pdev,
SERIAL_IF_CFG *pcfg,
SERIAL_ERR *perr)
{
SERIAL_DEV_CFG *p_cfg;
SERIAL_REG *p_reg;
CPU_BOOLEAN flow_ctrl;
CPU_INT16U cr1;
CPU_INT16U cr2;
CPU_INT16U stop;
CPU_INT16U parity;
CPU_INT32U clk_freq;
CPU_INT32U baudrate;
CPU_INT32U div_int;
CPU_INT32U div_frac;
p_cfg = pdev->Dev_Cfg;
p_cfg->BSP_API->ClkEn(perr); /* En dev-specific HW clk. */
if (*perr != SERIAL_ERR_NONE) {
return;
}
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
switch (pcfg->StopBits) {
case SERIAL_STOPBITS_1:
stop = CR2_STOP_BITS_1;
break;
case SERIAL_STOPBITS_1_5:
stop = CR2_STOP_BITS_1_5;
break;
case SERIAL_STOPBITS_2:
default:
stop = CR2_STOP_BITS_2;
break;
}
cr2 = p_reg->CR2;
cr2 &= ~CR2_STOP_MASK;
cr2 |= stop;
p_reg->CR2 = cr2;
switch (pcfg->DataBits) {
case SERIAL_DATABITS_8:
break;
case SERIAL_DATABITS_5:
case SERIAL_DATABITS_6:
case SERIAL_DATABITS_7:
default:
*perr = SERIAL_ERR_DRV_INVALID;
return;
}
switch (pcfg->Parity) {
case SERIAL_PARITY_ODD:
parity = CR1_PARITY_ODD;
break;
case SERIAL_PARITY_EVEN:
parity = CR1_PARITY_EVEN;
break;
case SERIAL_PARITY_NONE:
parity = CR1_PARITY_NONE;
break;
case SERIAL_PARITY_MARK:
case SERIAL_PARITY_SPACE:
default:
*perr = SERIAL_ERR_DRV_INVALID;
return;
}
cr1 = p_reg->CR1;
cr1 &= ~(CR1_M |
CR1_PCE |
CR1_PS);
cr1 |= parity |
CR1_TE |
CR1_RE;
p_reg->CR1 = cr1;
switch (pcfg->FlowCtrl) {
case SERIAL_FLOW_CTRL_HARDWARE:
flow_ctrl = DEF_ENABLED;
p_reg->CR3 |= CR3_CTSE |
CR3_RTSE;
break;
case SERIAL_FLOW_CTRL_NONE:
default:
flow_ctrl = DEF_DISABLED;
p_reg->CR3 &= ~(CR3_CTSE |
CR3_RTSE);
break;
}
p_cfg->BSP_API->CfgGPIO(flow_ctrl, perr); /* Cfg dev-specific GPIO. */
if (*perr != SERIAL_ERR_NONE) {
return;
}
p_cfg->BSP_API->CfgInt(pdev, perr); /* Cfg dev-specific int. */
if (*perr != SERIAL_ERR_NONE) {
return;
}
/* ------------------- CFG BAUD RATE ------------------ */
baudrate = pcfg->Baudrate;
clk_freq = p_cfg->BSP_API->ClkFreqGet(); /* Get periph clk freq. */
div_int = (25 * clk_freq + 2 * baudrate) / (4 * baudrate);
div_frac = ((div_int % 100) * 16 + 50) / 100;
div_int /= 100;
if (div_int > 4095) {
*perr = SERIAL_ERR_DRV_INVALID;
return;
}
p_reg->BRR = (div_int << 4) | /* Set baud rate. */
(div_frac & 0xF);
cr2 = p_reg->CR2;
cr2 &= ~(CR2_CLKEN |
CR2_CPOL |
CR2_LBCL);
cr2 |= CR2_CPHA;
p_reg->CR2 = cr2;
p_reg->CR1 |= CR1_UE; /* En rx'er & tx'er. */
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_Close()
*
* Description : Close a serial device for communication.
*
* Argument(s) : pdev Pointer to device.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device closed.
*
* Return(s) : None.
*
* Caller(s) : Serial_Close() via 'pdev->Drv_API->Close()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_Close (SERIAL_DEV *pdev,
SERIAL_ERR *perr)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
/* -------------------- RESET UART -------------------- */
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
p_reg->CR1 &= ~CR1_UE; /* Dis rx'er & tx'er. */
p_reg->CR1 &= ~(CR1_RXNEIE | /* Dis rx int. */
CR1_TXEIE); /* Dis tx int. */
p_cfg->BSP_API->ClkDis(perr); /* Dis dev-specific HW clk. */
if (*perr != SERIAL_ERR_NONE) {
return;
}
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_RxStart()
*
* Description : Start receiving data & getting data received interrupts.
*
* Argument(s) : pdev Pointer to device.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device reception started.
*
* Return(s) : None.
*
* Caller(s) : Serial_Open(),
* Serial_IF_RxStart() via 'pdev->Drv_API->RxStart()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_RxStart (SERIAL_DEV *pdev,
SERIAL_ERR *perr)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
p_reg->CR1 |= CR1_RXNEIE; /* En rx int. */
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_RxStop()
*
* Description : Stop receiving data and data received interrupts.
*
* Argument(s) : pdev Pointer to device.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device reception stopped.
*
* Return(s) : None.
*
* Caller(s) : Serial_Close(),
* Serial_IF_Rx(),
* Serial_IF_RxStop() via 'pdev->Drv_API->RxStop()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_RxStop (SERIAL_DEV *pdev,
SERIAL_ERR *perr)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
p_reg->CR1 &= ~CR1_RXNEIE; /* Dis rx int. */
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_RxOctet()
*
* Description : Receive data octet.
*
* Argument(s) : pdev Pointer to device.
*
* pdatum Pointer to variable that will receive the received data octet.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device octet transmitted,
* SERIAL_ERR_DRV_IO_ERR Device octet NOT transmitted.
*
* Return(s) : None.
*
* Caller(s) : Serial_IF_Rx() via 'pdrv_api->RxOctet()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_RxOctet (SERIAL_DEV *pdev,
CPU_INT08U *pdatum,
SERIAL_ERR *perr)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
if ((p_reg->SR & SR_RXNE) == 0) { /* If rx not rdy, return err. */
*perr = SERIAL_ERR_DRV_IO_ERR;
return;
}
*pdatum = p_reg->DR & 0xFF;
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_TxStart()
*
* Description : Start transmitting data & getting data transmitted interrupts.
*
* Argument(s) : pdev Pointer to device.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device transmission started.
*
* Return(s) : None.
*
* Caller(s) : Serial_IF_Tx(),
* Serial_WrStart() via 'pdev->Drv_API->TxStart()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_TxStart (SERIAL_DEV *pdev,
SERIAL_ERR *perr)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
p_reg->CR1 |= CR1_TCIE; /* En tx int. */
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_TxStop()
*
* Description : Stop transmitting data & getting data transmitted interrupts.
*
* Argument(s) : pdev Pointer to device.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device transmission stopped.
*
* Return(s) : None.
*
* Caller(s) : Serial_Close(),
* Serial_IF_Tx(),
* Serial_WrStartNext() via 'pdev->Drv_API->TxStop()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_TxStop (SERIAL_DEV *pdev,
SERIAL_ERR *perr)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
p_reg->CR1 &= ~CR1_TCIE; /* Dis tx int. */
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_TxOctet()
*
* Description : Transmit data octet.
*
* Argument(s) : pdev Pointer to device.
*
* datum Data octet to transmit.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Device octet transmitted,
* SERIAL_ERR_DRV_IO_ERR Device octet NOT transmitted.
*
* Return(s) : None.
*
* Caller(s) : Serial_IF_Tx() via 'pdrv_api->TxOctet()',
* Serial_WrStart() via 'pdev->Drv_API->TxOctet()'.
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialDrv_TxOctet (SERIAL_DEV *pdev,
CPU_INT08U datum,
SERIAL_ERR *perr)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
if ((p_reg->SR & SR_TXE) == 0) { /* If tx not rdy, return err. */
*perr = SERIAL_ERR_DRV_IO_ERR;
return;
}
p_reg->DR = datum; /* Tx octet. */
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialDrv_ISR_Handler()
*
* Description : Handle serial interface's device interrupt service routine (ISR) function(s).
*
* Argument(s) : pdev Pointer to device.
*
* type Device interrupt type(s) to handle :
*
* SERIAL_ISR_TYPE_UNKNOWN Handle unknown device ISR(s).
* SERIAL_ISR_TYPE_RX Handle device receive ISR(s).
* SERIAL_ISR_TYPE_RX_OVERRUN Handle device receive overrun ISR(s).
* SERIAL_ISR_TYPE_TX_RDY Handle device transmit ready ISR(s).
* SERIAL_ISR_TYPE_TX_COMPLETE Handle device transmit complete ISR(s).
* SERIAL_ISR_TYPE_ERR Handle device error ISR(s).
*
* Return(s) : None.
*
* Caller(s) : Device driver(s)' Board Support Package (BSP) Interrupt Service Routine (ISR) handler(s).
*
* Note(s) : None.
*********************************************************************************************************
*/
static void SerialDrv_ISR_Handler (SERIAL_DEV *pdev,
CPU_INT08U type)
{
SERIAL_REG *p_reg;
SERIAL_DEV_CFG *p_cfg;
CPU_INT08U datum;
CPU_INT16U status;
CPU_INT16U mask;
(void)&type;
p_cfg = pdev->Dev_Cfg;
p_reg = (SERIAL_REG *)p_cfg->BaseAddr;
status = p_reg->SR;
mask = p_reg->CR1;
/* --------------- HANDLE WR COMPLETION --------------- */
if (DEF_BIT_IS_SET(status, SR_TC) &&
DEF_BIT_IS_SET(mask, CR1_TCIE)) {
SerialIF_Tx(pdev); /* Inform serial core of wr completion. */
}
/* --------------- HANDLE RD COMPLETION --------------- */
if (DEF_BIT_IS_SET(status, SR_RXNE) &&
DEF_BIT_IS_SET(mask, CR1_RXNEIE)) {
datum = p_reg->DR & 0xFF;
SerialIF_Rx(pdev, datum); /* Inform serial core of rx completion. */
} else if (DEF_BIT_IS_SET(status, SR_ORE)) { /* Clr overrun condition. */
datum = p_reg->DR & 0xFF;
}
}