]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c
Merge commit '5a0132f1c1c9a14fd2941f0a5e29bbf5e31da20c' into master-core-pull
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NORDIC / TARGET_MCU_NRF51822 / serial_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2013 Nordic Semiconductor
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 // math.h required for floating point operations for baud rate calculation
17 //#include <math.h>
18 #include <string.h>
19 #include "mbed_assert.h"
20
21 #include "serial_api.h"
22 #include "cmsis.h"
23 #include "pinmap.h"
24
25 /******************************************************************************
26 * INITIALIZATION
27 ******************************************************************************/
28 #define UART_NUM 1
29
30 static uint32_t serial_irq_ids[UART_NUM] = {0};
31 static uart_irq_handler irq_handler;
32 static uint32_t acceptedSpeeds[17][2] = {{1200, UART_BAUDRATE_BAUDRATE_Baud1200},
33 {2400, UART_BAUDRATE_BAUDRATE_Baud2400},
34 {4800, UART_BAUDRATE_BAUDRATE_Baud4800},
35 {9600, UART_BAUDRATE_BAUDRATE_Baud9600},
36 {14400, UART_BAUDRATE_BAUDRATE_Baud14400},
37 {19200, UART_BAUDRATE_BAUDRATE_Baud19200},
38 {28800, UART_BAUDRATE_BAUDRATE_Baud28800},
39 {31250, (0x00800000UL) /* 31250 baud */},
40 {38400, UART_BAUDRATE_BAUDRATE_Baud38400},
41 {57600, UART_BAUDRATE_BAUDRATE_Baud57600},
42 {76800, UART_BAUDRATE_BAUDRATE_Baud76800},
43 {115200, UART_BAUDRATE_BAUDRATE_Baud115200},
44 {230400, UART_BAUDRATE_BAUDRATE_Baud230400},
45 {250000, UART_BAUDRATE_BAUDRATE_Baud250000},
46 {460800, UART_BAUDRATE_BAUDRATE_Baud460800},
47 {921600, UART_BAUDRATE_BAUDRATE_Baud921600},
48 {1000000, UART_BAUDRATE_BAUDRATE_Baud1M}};
49
50 int stdio_uart_inited = 0;
51 serial_t stdio_uart;
52
53
54 void serial_init(serial_t *obj, PinName tx, PinName rx) {
55 UARTName uart = UART_0;
56 obj->uart = (NRF_UART_Type *)uart;
57
58 //pin configurations --
59 NRF_GPIO->DIR |= (1 << tx); //TX_PIN_NUMBER);
60 NRF_GPIO->DIR |= (1 << RTS_PIN_NUMBER);
61
62 NRF_GPIO->DIR &= ~(1 << rx); //RX_PIN_NUMBER);
63 NRF_GPIO->DIR &= ~(1 << CTS_PIN_NUMBER);
64
65
66 // set default baud rate and format
67 serial_baud (obj, 9600);
68 serial_format(obj, 8, ParityNone, 1);
69
70 obj->uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
71 obj->uart->TASKS_STARTTX = 1;
72 obj->uart->TASKS_STARTRX = 1;
73 obj->uart->EVENTS_RXDRDY = 0;
74 // dummy write needed or TXDRDY trails write rather than leads write.
75 // pins are disconnected so nothing is physically transmitted on the wire
76 obj->uart->TXD = 0;
77
78 obj->index = 0;
79
80 obj->uart->PSELRTS = RTS_PIN_NUMBER;
81 obj->uart->PSELTXD = tx; //TX_PIN_NUMBER;
82 obj->uart->PSELCTS = CTS_PIN_NUMBER;
83 obj->uart->PSELRXD = rx; //RX_PIN_NUMBER;
84
85 // set rx/tx pins in PullUp mode
86 if (tx != NC) {
87 pin_mode(tx, PullUp);
88 }
89 if (rx != NC) {
90 pin_mode(rx, PullUp);
91 }
92
93 if (uart == STDIO_UART) {
94 stdio_uart_inited = 1;
95 memcpy(&stdio_uart, obj, sizeof(serial_t));
96 }
97 }
98
99 void serial_free(serial_t *obj)
100 {
101 serial_irq_ids[obj->index] = 0;
102 }
103
104 // serial_baud
105 // set the baud rate, taking in to account the current SystemFrequency
106 void serial_baud(serial_t *obj, int baudrate)
107 {
108 if (baudrate<=1200) {
109 obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200;
110 return;
111 }
112
113 for (int i = 1; i<17; i++) {
114 if (baudrate<acceptedSpeeds[i][0]) {
115 obj->uart->BAUDRATE = acceptedSpeeds[i - 1][1];
116 return;
117 }
118 }
119 obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1M;
120 }
121
122 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
123 {
124 // 0: 1 stop bits, 1: 2 stop bits
125 // int parity_enable, parity_select;
126 switch (parity) {
127 case ParityNone:
128 obj->uart->CONFIG = 0;
129 break;
130 default:
131 obj->uart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos);
132 return;
133 }
134 //no Flow Control
135 }
136
137 //******************************************************************************
138 // * INTERRUPT HANDLING
139 //******************************************************************************
140 static inline void uart_irq(uint32_t iir, uint32_t index)
141 {
142 SerialIrq irq_type;
143 switch (iir) {
144 case 1:
145 irq_type = TxIrq;
146 break;
147 case 2:
148 irq_type = RxIrq;
149 break;
150
151 default:
152 return;
153 }
154
155 if (serial_irq_ids[index] != 0) {
156 irq_handler(serial_irq_ids[index], irq_type);
157 }
158 }
159
160 #ifdef __cplusplus
161 extern "C" {
162 #endif
163 void UART0_IRQHandler()
164 {
165 uint32_t irtype = 0;
166
167 if((NRF_UART0->INTENSET & 0x80) && NRF_UART0->EVENTS_TXDRDY) {
168 irtype = 1;
169 } else if((NRF_UART0->INTENSET & 0x04) && NRF_UART0->EVENTS_RXDRDY) {
170 irtype = 2;
171 }
172 uart_irq(irtype, 0);
173 }
174
175 #ifdef __cplusplus
176 }
177 #endif
178 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
179 {
180 irq_handler = handler;
181 serial_irq_ids[obj->index] = id;
182 }
183
184 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
185 {
186 IRQn_Type irq_n = (IRQn_Type)0;
187
188 switch ((int)obj->uart) {
189 case UART_0:
190 irq_n = UART0_IRQn;
191 break;
192 }
193
194 if (enable) {
195 switch (irq) {
196 case RxIrq:
197 obj->uart->INTENSET = (UART_INTENSET_RXDRDY_Msk);
198 break;
199 case TxIrq:
200 obj->uart->INTENSET = (UART_INTENSET_TXDRDY_Msk);
201 break;
202 }
203 NVIC_SetPriority(irq_n, 3);
204 NVIC_EnableIRQ(irq_n);
205 } else { // disable
206 // maseked writes to INTENSET dont disable and masked writes to
207 // INTENCLR seemed to clear the entire register, not bits.
208 // Added INTEN to memory map and seems to allow set and clearing of specific bits as desired
209 int all_disabled = 0;
210 switch (irq) {
211 case RxIrq:
212 obj->uart->INTENCLR = (UART_INTENCLR_RXDRDY_Msk);
213 all_disabled = (obj->uart->INTENCLR & (UART_INTENCLR_TXDRDY_Msk)) == 0;
214 break;
215 case TxIrq:
216 obj->uart->INTENCLR = (UART_INTENCLR_TXDRDY_Msk);
217 all_disabled = (obj->uart->INTENCLR & (UART_INTENCLR_RXDRDY_Msk)) == 0;
218 break;
219 }
220
221 if (all_disabled) {
222 NVIC_DisableIRQ(irq_n);
223 }
224 }
225 }
226
227 //******************************************************************************
228 //* READ/WRITE
229 //******************************************************************************
230 int serial_getc(serial_t *obj)
231 {
232 while (!serial_readable(obj)) {
233 }
234
235 obj->uart->EVENTS_RXDRDY = 0;
236
237 return (uint8_t)obj->uart->RXD;
238 }
239
240 void serial_putc(serial_t *obj, int c)
241 {
242 while (!serial_writable(obj)) {
243 }
244
245 obj->uart->EVENTS_TXDRDY = 0;
246 obj->uart->TXD = (uint8_t)c;
247 }
248
249 int serial_readable(serial_t *obj)
250 {
251 return (obj->uart->EVENTS_RXDRDY == 1);
252 }
253
254 int serial_writable(serial_t *obj)
255 {
256 return (obj->uart->EVENTS_TXDRDY == 1);
257 }
258
259 void serial_break_set(serial_t *obj)
260 {
261 obj->uart->TASKS_SUSPEND = 1;
262 }
263
264 void serial_break_clear(serial_t *obj)
265 {
266 obj->uart->TASKS_STARTTX = 1;
267 obj->uart->TASKS_STARTRX = 1;
268 }
269
270 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow)
271 {
272
273 if (type == FlowControlRTSCTS || type == FlowControlRTS) {
274 NRF_GPIO->DIR |= (1<<rxflow);
275 pin_mode(rxflow, PullUp);
276 obj->uart->PSELRTS = rxflow;
277
278 obj->uart->CONFIG |= 0x01; // Enable HWFC
279 }
280
281 if (type == FlowControlRTSCTS || type == FlowControlCTS) {
282 NRF_GPIO->DIR &= ~(1<<txflow);
283 pin_mode(txflow, PullUp);
284 obj->uart->PSELCTS = txflow;
285
286 obj->uart->CONFIG |= 0x01; // Enable HWFC;
287 }
288
289 if (type == FlowControlNone) {
290 obj->uart->PSELRTS = 0xFFFFFFFF; // Disable RTS
291 obj->uart->PSELCTS = 0xFFFFFFFF; // Disable CTS
292
293 obj->uart->CONFIG &= ~0x01; // Enable HWFC;
294 }
295 }
296
297 void serial_clear(serial_t *obj) {
298 }
Imprint / Impressum