@@ -47,42 +47,51 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
47
47
48
48
static uart_irq_handler irq_handler ;
49
49
50
+ // Defined in serial_api.c
51
+ inline int8_t get_uart_index (UARTName uart_name );
52
+
50
53
/******************************************************************************
51
54
* INTERRUPTS HANDLING
52
55
******************************************************************************/
53
56
54
- static void uart_irq (int id )
57
+ static void uart_irq (UARTName uart_name )
55
58
{
56
- UART_HandleTypeDef * huart = & uart_handlers [id ];
57
- if (serial_irq_ids [id ] != 0 ) {
58
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
59
- if (__HAL_UART_GET_IT (huart , UART_IT_TXE ) != RESET ) {
60
- irq_handler (serial_irq_ids [id ], TxIrq );
59
+ int8_t id = get_uart_index (uart_name );
60
+
61
+ if (id >= 0 ) {
62
+ UART_HandleTypeDef * huart = & uart_handlers [id ];
63
+ if (serial_irq_ids [id ] != 0 ) {
64
+ if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
65
+ if (__HAL_UART_GET_IT (huart , UART_IT_TXE ) != RESET ) {
66
+ irq_handler (serial_irq_ids [id ], TxIrq );
67
+ }
61
68
}
62
- }
63
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_RXNE ) != RESET ) {
64
- if ( __HAL_UART_GET_IT ( huart , UART_IT_RXNE ) != RESET ) {
65
- irq_handler ( serial_irq_ids [ id ], RxIrq );
66
- /* Flag has been cleared when reading the content */
69
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_RXNE ) != RESET ) {
70
+ if (__HAL_UART_GET_IT (huart , UART_IT_RXNE ) != RESET ) {
71
+ irq_handler ( serial_irq_ids [ id ], RxIrq );
72
+ /* Flag has been cleared when reading the content */
73
+ }
67
74
}
68
- }
69
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_ORE ) != RESET ) {
70
- if ( __HAL_UART_GET_IT ( huart , UART_IT_ORE ) != RESET ) {
71
- __HAL_UART_CLEAR_FLAG ( huart , UART_CLEAR_OREF );
75
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_ORE ) != RESET ) {
76
+ if (__HAL_UART_GET_IT (huart , UART_IT_ORE ) != RESET ) {
77
+ __HAL_UART_CLEAR_FLAG ( huart , UART_CLEAR_OREF );
78
+ }
72
79
}
73
80
}
74
81
}
75
82
}
76
83
84
+ #if defined(USART1_BASE )
77
85
static void uart1_irq (void )
78
86
{
79
- uart_irq (0 );
87
+ uart_irq (UART_1 );
80
88
}
89
+ #endif
81
90
82
91
#if defined(USART2_BASE )
83
92
static void uart2_irq (void )
84
93
{
85
- uart_irq (1 );
94
+ uart_irq (UART_2 );
86
95
}
87
96
#endif
88
97
@@ -92,43 +101,43 @@ static void uart3_8_irq(void)
92
101
#if defined(TARGET_STM32F091RC )
93
102
#if defined(USART3_BASE )
94
103
if (__HAL_GET_PENDING_IT (HAL_ITLINE_USART3 ) != RESET ) {
95
- uart_irq (2 );
104
+ uart_irq (UART_3 );
96
105
}
97
106
#endif
98
107
#if defined(USART4_BASE )
99
108
if (__HAL_GET_PENDING_IT (HAL_ITLINE_USART4 ) != RESET ) {
100
- uart_irq (3 );
109
+ uart_irq (UART_4 );
101
110
}
102
111
#endif
103
112
#if defined(USART5_BASE )
104
113
if (__HAL_GET_PENDING_IT (HAL_ITLINE_USART5 ) != RESET ) {
105
- uart_irq (4 );
114
+ uart_irq (UART_5 );
106
115
}
107
116
#endif
108
117
#if defined(USART6_BASE )
109
118
if (__HAL_GET_PENDING_IT (HAL_ITLINE_USART6 ) != RESET ) {
110
- uart_irq (5 );
119
+ uart_irq (UART_6 );
111
120
}
112
121
#endif
113
122
#if defined(USART7_BASE )
114
123
if (__HAL_GET_PENDING_IT (HAL_ITLINE_USART7 ) != RESET ) {
115
- uart_irq (6 );
124
+ uart_irq (UART_7 );
116
125
}
117
126
#endif
118
127
#if defined(USART8_BASE )
119
128
if (__HAL_GET_PENDING_IT (HAL_ITLINE_USART8 ) != RESET ) {
120
- uart_irq (7 );
129
+ uart_irq (UART_8 );
121
130
}
122
131
#endif
123
132
#else // TARGET_STM32F070RB, TARGET_STM32F072RB
124
133
#if defined(USART3_BASE )
125
134
if (USART3 -> ISR & (UART_FLAG_TXE | UART_FLAG_RXNE | UART_FLAG_ORE )) {
126
- uart_irq (2 );
135
+ uart_irq (UART_3 );
127
136
}
128
137
#endif
129
138
#if defined(USART4_BASE )
130
139
if (USART4 -> ISR & (UART_FLAG_TXE | UART_FLAG_RXNE | UART_FLAG_ORE )) {
131
- uart_irq (3 );
140
+ uart_irq (UART_4 );
132
141
}
133
142
#endif
134
143
#endif
@@ -149,10 +158,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
149
158
IRQn_Type irq_n = (IRQn_Type )0 ;
150
159
uint32_t vector = 0 ;
151
160
161
+ #if defined(USART1_BASE )
152
162
if (obj_s -> uart == UART_1 ) {
153
163
irq_n = USART1_IRQn ;
154
164
vector = (uint32_t )& uart1_irq ;
155
165
}
166
+ #endif
156
167
157
168
#if defined(USART2_BASE )
158
169
if (obj_s -> uart == UART_2 ) {
@@ -238,7 +249,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
238
249
}
239
250
240
251
if (all_disabled ) {
241
- NVIC_DisableIRQ (irq_n );
252
+ NVIC_DisableIRQ (irq_n );
242
253
}
243
254
}
244
255
}
@@ -351,44 +362,75 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
351
362
/**
352
363
* Get index of serial object TX IRQ, relating it to the physical peripheral.
353
364
*
354
- * @param obj pointer to serial object
365
+ * @param uart_name i.e. UART_1, UART_2, ...
355
366
* @return internal NVIC TX IRQ index of U(S)ART peripheral
356
367
*/
357
- static IRQn_Type serial_get_irq_n (serial_t * obj )
368
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
358
369
{
359
- struct serial_s * obj_s = SERIAL_S (obj );
360
370
IRQn_Type irq_n ;
361
371
362
- switch (obj_s -> index ) {
372
+ switch (uart_name ) {
373
+
363
374
#if defined(USART1_BASE )
364
- case 0 :
375
+ case UART_1 :
365
376
irq_n = USART1_IRQn ;
366
377
break ;
367
378
#endif
379
+
368
380
#if defined(USART2_BASE )
369
- case 1 :
381
+ case UART_2 :
370
382
irq_n = USART2_IRQn ;
371
383
break ;
372
384
#endif
385
+
386
+ #if defined(USART3_BASE )
387
+ case UART_3 :
373
388
#if defined (TARGET_STM32F091RC )
374
- case 2 :
375
- case 3 :
376
- case 4 :
377
- case 5 :
378
- case 6 :
379
- case 7 :
380
389
irq_n = USART3_8_IRQn ;
390
+ #else
391
+ irq_n = USART3_4_IRQn ;
392
+ #endif
381
393
break ;
382
- #elif !defined (TARGET_STM32F030R8 ) && !defined (TARGET_STM32F051R8 )
383
- case 2 :
384
- case 3 :
394
+ #endif
395
+
396
+ #if defined(USART4_BASE )
397
+ case UART_4 :
398
+ #if defined (TARGET_STM32F091RC )
399
+ irq_n = USART3_8_IRQn ;
400
+ #else
385
401
irq_n = USART3_4_IRQn ;
402
+ #endif
386
403
break ;
387
404
#endif
405
+
406
+ #if defined(USART5_BASE )
407
+ case UART_5 :
408
+ irq_n = USART3_8_IRQn ;
409
+ break ;
410
+ #endif
411
+
412
+ #if defined(USART6_BASE )
413
+ case UART_6 :
414
+ irq_n = USART3_8_IRQn ;
415
+ break ;
416
+ #endif
417
+
418
+ #if defined(USART7_BASE )
419
+ case UART_7 :
420
+ irq_n = USART3_8_IRQn ;
421
+ break ;
422
+ #endif
423
+
424
+ #if defined(USART8_BASE )
425
+ case UART_8 :
426
+ irq_n = USART3_8_IRQn ;
427
+ break ;
428
+ #endif
429
+
388
430
default :
389
431
irq_n = (IRQn_Type )0 ;
390
432
}
391
-
433
+
392
434
return irq_n ;
393
435
}
394
436
@@ -434,7 +476,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
434
476
serial_enable_event (obj , event , 1 ); // Set only the wanted events
435
477
436
478
// Enable interrupt
437
- IRQn_Type irq_n = serial_get_irq_n (obj );
479
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
438
480
NVIC_ClearPendingIRQ (irq_n );
439
481
NVIC_DisableIRQ (irq_n );
440
482
NVIC_SetPriority (irq_n , 1 );
@@ -484,7 +526,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
484
526
485
527
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
486
528
487
- IRQn_Type irq_n = serial_get_irq_n (obj );
529
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
488
530
NVIC_ClearPendingIRQ (irq_n );
489
531
NVIC_DisableIRQ (irq_n );
490
532
NVIC_SetPriority (irq_n , 0 );
@@ -642,7 +684,7 @@ void serial_tx_abort_asynch(serial_t *obj)
642
684
643
685
// clear flags
644
686
__HAL_UART_CLEAR_FLAG (huart , UART_CLEAR_TCF );
645
-
687
+
646
688
// reset states
647
689
huart -> TxXferCount = 0 ;
648
690
// update handle state
0 commit comments