Appiko
hal_uart.c
1 
18 #include "nrf.h"
19 #include "hal_uart.h"
20 #include "boards.h"
21 #include "hal_gpio.h"
22 #include "nrf_util.h"
23 #include "tinyprintf.h"
24 #include "stdbool.h"
25 
26 #if ISR_MANAGER == 1
27 #include "isr_manager.h"
28 #endif
29 
33 #if defined NRF51
34 #define UART_ID CONCAT_2(NRF_UART,UART_USED)
35 #define UART_IRQN UART_IRQN_a(UART_USED)
36 #define UART_IRQ_Handler UART_IRQ_Handler_a(UART_USED)
37 
38 #define UART_IRQN_a(n) UART_IRQN_b(n)
39 #define UART_IRQN_b(n) UART##n##_IRQn
40 
41 #define UART_IRQ_Handler_a(n) UART_IRQ_Handler_b(n)
42 #define UART_IRQ_Handler_b(n) UART##n##_IRQHandler
43 
44 #endif
45 #if defined NRF52832 || defined NRF52810
46 #define UART_ID CONCAT_2(NRF_UARTE,UART_USED)
47 #define UART_IRQN UART_IRQN_a(UART_USED)
48 #define UART_IRQ_Handler UART_IRQ_Handler_a(UART_USED)
49 
50 #define UART_IRQN_a(n) UART_IRQN_b(n)
51 #define UART_IRQN_b(n) UARTE##n##_UART##n##_IRQn
52 
53 #define UART_IRQ_Handler_a(n) UART_IRQ_Handler_b(n)
54 #define UART_IRQ_Handler_b(n) UARTE##n##_UART##n##_IRQHandler
55 #endif
56 
57 
59 #define BUFFER_SIZE 128
60 
62 uint8_t rx_buffer[BUFFER_SIZE];
63 
65 void (*rx_handler)(uint8_t * ptr);
66 
72 static void rx_collect(uint8_t rx_data)
73 {
74  static uint32_t count = 0;
75  if (rx_data != LINE_END)
76  {
77  if (count < BUFFER_SIZE - 1)
78  {
79  rx_buffer[count] = rx_data;
80  count++;
81  }
82  }
83  else
84  {
85  rx_buffer[count] = '\0';
86  if (rx_handler != NULL)
87  {
88  rx_handler(rx_buffer);
89  }
90  count = 0;
91  }
92 }
93 
98 #if ISR_MANAGER == 1
99 void hal_uart_Handler ()
100 #else
101 void UART_IRQ_Handler (void)
102 #endif
103 {
104  /* Waits for RX data to be received, but
105  * no waiting actually since RX causes interrupt. */
106  while (UART_ID->EVENTS_RXDRDY != 1)
107  {
108  }
109 #if ISR_MANAGER == 0
110  UART_ID->EVENTS_RXDRDY = 0;
111 #endif
112  rx_collect((uint8_t) (*((uint32_t *)(0x40002518))));
113 }
114 
115 void hal_uart_putchar(uint8_t cr)
116 {
117  (*((uint32_t *)(0x4000251C))) = (uint8_t) cr;
118  UART_ID->EVENTS_TXDRDY = 0;
119  UART_ID->TASKS_STARTTX = 1;
120 
121  while (UART_ID->EVENTS_TXDRDY != 1)
122  {
123  }
124 
125  UART_ID->EVENTS_TXDRDY = 0;
126  UART_ID->TASKS_STOPTX = 1;
127 }
128 
134 void printf_callback(void* str_end, char ch)
135 {
136  if((uint32_t) str_end != START_TX)
137  {
138  //Since putchar is not re-entrant
140  hal_uart_putchar(ch);
142  }
143 }
144 
145 void hal_uart_init(hal_uart_baud_t baud, void (*handler)(uint8_t * ptr))
146 {
147  /* Configure TX and RX pins from board.h */
148  hal_gpio_cfg_output(TX_PIN_NUMBER, 1);
149  hal_gpio_cfg_input(RX_PIN_NUMBER, HAL_GPIO_PULL_DISABLED);
150  (*((uint32_t *)(0x4000250C))) = TX_PIN_NUMBER;
151  (*((uint32_t *)(0x40002514))) = RX_PIN_NUMBER;
152 
155  UART_ID->CONFIG = 0;
156 
157 #if HWFC
158  {
159  /* Configure CTS and RTS pins if HWFC is true in board.h */
160  hal_gpio_cfg_output(RTS_PIN_NUMBER, 1);
161  hal_gpio_cfg_input(CTS_PIN_NUMBER, HAL_GPIO_PULL_DISABLED);
162  (*((uint32_t *)(0x40002508))) = RTS_PIN_NUMBER;
163  (*((uint32_t *)(0x40002510))) = CTS_PIN_NUMBER;
166  UART_ID->CONFIG = 1;
167  }
168 #endif
169 
170  UART_ID->BAUDRATE = (baud);
171 
172  //Enable UART
173  UART_ID->ENABLE = (0x04);
174 
175  UART_ID->INTENCLR = 0xFFFFFFFF;
176 
177  init_printf((void *) !(START_TX), printf_callback);
178 
179  if(handler != NULL)
180  {
181  UART_ID->EVENTS_RXDRDY = 0;
182 
183  rx_handler = handler;
184 
185  // Enable UART RX interrupt only
186  UART_ID->INTENSET = (1 << 2);
187 
188  NVIC_SetPriority(UART_IRQN, APP_IRQ_PRIORITY_LOW);
189  NVIC_EnableIRQ(UART_IRQN);
190 
191  UART_ID->TASKS_STARTRX = 1;
192  }
193 }
#define LINE_END
Definition: hal_uart.h:47
#define CRITICAL_REGION_ENTER()
Macro for entering a critical region.
Definition: nrf_util.h:103
#define CRITICAL_REGION_EXIT()
Macro for leaving a critical region.
Definition: nrf_util.h:113
void hal_uart_init(hal_uart_baud_t baud, void(*handler)(uint8_t *ptr))
Definition: hal_uart.c:145
HAL_GPIO_PULL_DISABLED.
Definition: hal_gpio.h:37
void hal_uart_putchar(uint8_t cr)
Send a single character through UART This function is used by printf_callback so that printf can be u...
Definition: hal_uart.c:115
hal_uart_baud_t
Definition: hal_uart.h:52