@@ -188,6 +188,7 @@ typedef struct {
188
188
uint8_t buffer [NUMBER_OF_BANKS ][DMA_BUFFER_SIZE ];
189
189
uint32_t rxdrdy_counter ;
190
190
uint32_t endrx_counter ;
191
+ uint32_t usage_counter ;
191
192
uint8_t tx_data ;
192
193
volatile uint8_t tx_in_progress ;
193
194
volatile uint8_t rx_in_progress ;
@@ -1006,7 +1007,6 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
1006
1007
/* Enable interrupts for UARTE0. */
1007
1008
NVIC_SetVector (UARTE0_UART0_IRQn , (uint32_t ) nordic_nrf5_uart0_handler );
1008
1009
nordic_nrf5_uart_irq_enable (0 );
1009
- nrf_uarte_enable (nordic_nrf5_uart_register [0 ]);
1010
1010
1011
1011
#if UART1_ENABLED
1012
1012
/* Initialize FIFO buffer for UARTE1. */
@@ -1019,7 +1019,6 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
1019
1019
/* Enable interrupts for UARTE1. */
1020
1020
NVIC_SetVector (UARTE1_IRQn , (uint32_t ) nordic_nrf5_uart1_handler );
1021
1021
nordic_nrf5_uart_irq_enable (1 );
1022
- nrf_uarte_enable (nordic_nrf5_uart_register [1 ]);
1023
1022
#endif
1024
1023
}
1025
1024
@@ -1028,6 +1027,15 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
1028
1027
1029
1028
uart_object -> instance = instance ;
1030
1029
1030
+ /* Increment usage counter for this instance. */
1031
+ nordic_nrf5_uart_state [instance ].usage_counter ++ ;
1032
+
1033
+ /* Enable instance on first usage. */
1034
+ if (nordic_nrf5_uart_state [instance ].usage_counter == 1 ) {
1035
+
1036
+ nrf_uarte_enable (nordic_nrf5_uart_register [instance ]);
1037
+ }
1038
+
1031
1039
/* Store pins in serial object. */
1032
1040
if (tx == NC ) {
1033
1041
@@ -1086,7 +1094,27 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
1086
1094
*/
1087
1095
void serial_free (serial_t * obj )
1088
1096
{
1097
+ MBED_ASSERT (obj );
1098
+
1099
+ #if DEVICE_SERIAL_ASYNCH
1100
+ struct serial_s * uart_object = & obj -> serial ;
1101
+ #else
1102
+ struct serial_s * uart_object = obj ;
1103
+ #endif
1104
+
1105
+ int instance = uart_object -> instance ;
1106
+
1107
+ if (nordic_nrf5_uart_state [instance ].usage_counter > 1 ) {
1089
1108
1109
+ /* Decrement usage counter for this instance. */
1110
+ nordic_nrf5_uart_state [instance ].usage_counter -- ;
1111
+
1112
+ /* Disable instance when not in use. */
1113
+ if (nordic_nrf5_uart_state [instance ].usage_counter == 0 ) {
1114
+
1115
+ nrf_uarte_disable (nordic_nrf5_uart_register [instance ]);
1116
+ }
1117
+ }
1090
1118
}
1091
1119
1092
1120
/** Configure the baud rate
0 commit comments