20 #include "uart_printf.h" 24 #include "tinyprintf.h" 28 #include "isr_manager.h" 31 #define UARTE_ID CONCAT_2(NRF_UARTE,UARTE_USED_UART_PRINTF) 34 #define BUFFER_SIZE 128 57 static const struct buffer_state
61 } BUF_STATE[2] = {{ PING_DONE, PING_TX },
62 { PONG_DONE, PONG_TX } };
68 uart_buffer_states state;
71 static struct uart_context
73 volatile struct uart_tx_buffer tx_buf[2];
74 volatile uart_tx_states tx_state;
75 } uart_ctx = { { { { 0 }, 0, EMPTY }, { { 0 }, 0, EMPTY } }, PING_DONE };
77 static void stop_uart(
void)
79 UARTE_ID->TASKS_STOPTX = 1;
82 static void start_uart_tx(uart_buffers tx_buf)
84 UARTE_ID->EVENTS_ENDTX = 0;
86 UARTE_ID->TXD.PTR = (uint32_t) uart_ctx.tx_buf[tx_buf].buf;
87 UARTE_ID->TXD.MAXCNT = uart_ctx.tx_buf[tx_buf].count;
89 UARTE_ID->TASKS_STARTTX = 1;
96 void uart_printf_uart_Handler (
void)
98 void UARTE0_UART0_IRQHandler(
void)
101 if (1 == UARTE_ID->EVENTS_ENDTX)
104 UARTE_ID->EVENTS_ENDTX = 0;
105 (void) UARTE_ID->EVENTS_ENDTX;
107 uart_buffers current_buf = PONG, other_buf = PING;
108 if (BUF_STATE[PING].TX == uart_ctx.tx_state)
114 uart_ctx.tx_buf[current_buf].state = EMPTY;
115 uart_ctx.tx_buf[current_buf].count = 0;
117 if (FILLED == uart_ctx.tx_buf[other_buf].state)
119 start_uart_tx(other_buf);
120 uart_ctx.tx_state = BUF_STATE[other_buf].TX;
124 uart_ctx.tx_state = BUF_STATE[current_buf].DONE;
132 void printf_callback(
void* str_end,
char ch)
134 uart_buffers current_buf = PONG, other_buf = PING;
135 if ((BUF_STATE[PING].TX == uart_ctx.tx_state) || (BUF_STATE[PING].DONE == uart_ctx.tx_state))
142 if (START_TX == ((uint32_t) str_end))
144 if (PING_DONE == uart_ctx.tx_state)
147 uart_ctx.tx_state = PONG_TX;
149 if (PONG_DONE == uart_ctx.tx_state)
152 uart_ctx.tx_state = PING_TX;
154 uart_ctx.tx_buf[other_buf].state = FILLED;
160 if (uart_ctx.tx_buf[other_buf].count < BUFFER_SIZE)
162 uart_ctx.tx_buf[other_buf].buf[uart_ctx.tx_buf[other_buf].count] = ch;
163 uart_ctx.tx_buf[other_buf].count++;
164 uart_ctx.tx_buf[other_buf].state = FILLING;
168 while (BUF_STATE[current_buf].TX == uart_ctx.tx_state)
172 start_uart_tx(other_buf);
173 uart_ctx.tx_buf[other_buf].state = FILLED;
174 uart_ctx.tx_state = BUF_STATE[other_buf].TX;
182 hal_gpio_cfg_output(TX_PIN_NUMBER, 0);
183 hal_gpio_cfg_input(RX_PIN_NUMBER, GPIO_PIN_CNF_PULL_Disabled);
184 UARTE_ID->PSEL.TXD = TX_PIN_NUMBER;
185 UARTE_ID->PSEL.RXD = RX_PIN_NUMBER;
191 hal_gpio_cfg_output(RTS_PIN_NUMBER, 0);
192 hal_gpio_cfg_input(CTS_PIN_NUMBER, GPIO_PIN_CNF_PULL_Disabled);
193 UARTE_ID->PSEL.RTS = RTS_PIN_NUMBER;
194 UARTE_ID->PSEL.CTS = CTS_PIN_NUMBER;
195 UARTE_ID->CONFIG = (UARTE_CONFIG_HWFC_Enabled << UARTE_CONFIG_HWFC_Pos);
200 UARTE_ID->BAUDRATE = (baud_rate << UARTE_BAUDRATE_BAUDRATE_Pos);
201 UARTE_ID->ENABLE = (UARTE_ENABLE_ENABLE_Enabled << UARTE_ENABLE_ENABLE_Pos);
204 init_printf((
void *) !(START_TX), printf_callback);
207 UARTE_ID->INTENSET = (UARTE_INTENSET_ENDTX_Set << UARTE_INTENSET_ENDTX_Pos);
209 NVIC_SetPriority(UARTE0_UART0_IRQn, APP_IRQ_PRIORITY_LOW);
210 NVIC_EnableIRQ(UARTE0_UART0_IRQn);
Strcture for Operation time.
void uart_printf_init(uart_printf_baud_t baud_rate)
Function to initialize the parameters of UART based on the configurations in boards....