Skip to content

Commit 18fa941

Browse files
author
Marcus Chang
committed
Add resource management for serial for the NRF52 family
Instance counter keeps track of how many objects have been initialized and freed. On the first object the instance is enabled and on the last object the instance is disabled.
1 parent cafa8ae commit 18fa941

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/serial_api.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ typedef struct {
188188
uint8_t buffer[NUMBER_OF_BANKS][DMA_BUFFER_SIZE];
189189
uint32_t rxdrdy_counter;
190190
uint32_t endrx_counter;
191+
uint32_t usage_counter;
191192
uint8_t tx_data;
192193
volatile uint8_t tx_in_progress;
193194
volatile uint8_t rx_in_progress;
@@ -1006,7 +1007,6 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
10061007
/* Enable interrupts for UARTE0. */
10071008
NVIC_SetVector(UARTE0_UART0_IRQn, (uint32_t) nordic_nrf5_uart0_handler);
10081009
nordic_nrf5_uart_irq_enable(0);
1009-
nrf_uarte_enable(nordic_nrf5_uart_register[0]);
10101010

10111011
#if UART1_ENABLED
10121012
/* Initialize FIFO buffer for UARTE1. */
@@ -1019,7 +1019,6 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
10191019
/* Enable interrupts for UARTE1. */
10201020
NVIC_SetVector(UARTE1_IRQn, (uint32_t) nordic_nrf5_uart1_handler);
10211021
nordic_nrf5_uart_irq_enable(1);
1022-
nrf_uarte_enable(nordic_nrf5_uart_register[1]);
10231022
#endif
10241023
}
10251024

@@ -1028,6 +1027,15 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
10281027

10291028
uart_object->instance = instance;
10301029

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+
10311039
/* Store pins in serial object. */
10321040
if (tx == NC) {
10331041

@@ -1086,7 +1094,27 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
10861094
*/
10871095
void serial_free(serial_t *obj)
10881096
{
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) {
10891108

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+
}
10901118
}
10911119

10921120
/** Configure the baud rate

0 commit comments

Comments
 (0)