1 /* mbed Microcontroller Library
2 * Copyright (c) 2013 Nordic Semiconductor
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 // math.h required for floating point operations for baud rate calculation
19 #include "mbed_assert.h"
21 #include "serial_api.h"
25 /******************************************************************************
27 ******************************************************************************/
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
}};
50 int stdio_uart_inited
= 0;
54 void serial_init(serial_t
*obj
, PinName tx
, PinName rx
) {
55 UARTName uart
= UART_0
;
56 obj
->uart
= (NRF_UART_Type
*)uart
;
58 //pin configurations --
59 NRF_GPIO
->DIR |= (1 << tx
); //TX_PIN_NUMBER);
60 NRF_GPIO
->DIR |= (1 << RTS_PIN_NUMBER
);
62 NRF_GPIO
->DIR &= ~(1 << rx
); //RX_PIN_NUMBER);
63 NRF_GPIO
->DIR &= ~(1 << CTS_PIN_NUMBER
);
66 // set default baud rate and format
67 serial_baud (obj
, 9600);
68 serial_format(obj
, 8, ParityNone
, 1);
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
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;
85 // set rx/tx pins in PullUp mode
93 if (uart
== STDIO_UART
) {
94 stdio_uart_inited
= 1;
95 memcpy(&stdio_uart
, obj
, sizeof(serial_t
));
99 void serial_free(serial_t
*obj
)
101 serial_irq_ids
[obj
->index
] = 0;
105 // set the baud rate, taking in to account the current SystemFrequency
106 void serial_baud(serial_t
*obj
, int baudrate
)
108 if (baudrate
<=1200) {
109 obj
->uart
->BAUDRATE
= UART_BAUDRATE_BAUDRATE_Baud1200
;
113 for (int i
= 1; i
<17; i
++) {
114 if (baudrate
<acceptedSpeeds
[i
][0]) {
115 obj
->uart
->BAUDRATE
= acceptedSpeeds
[i
- 1][1];
119 obj
->uart
->BAUDRATE
= UART_BAUDRATE_BAUDRATE_Baud1M
;
122 void serial_format(serial_t
*obj
, int data_bits
, SerialParity parity
, int stop_bits
)
124 // 0: 1 stop bits, 1: 2 stop bits
125 // int parity_enable, parity_select;
128 obj
->uart
->CONFIG
= 0;
131 obj
->uart
->CONFIG
= (UART_CONFIG_PARITY_Included
<< UART_CONFIG_PARITY_Pos
);
137 //******************************************************************************
138 // * INTERRUPT HANDLING
139 //******************************************************************************
140 static inline void uart_irq(uint32_t iir
, uint32_t index
)
155 if (serial_irq_ids
[index
] != 0) {
156 irq_handler(serial_irq_ids
[index
], irq_type
);
163 void UART0_IRQHandler()
167 if((NRF_UART0
->INTENSET
& 0x80) && NRF_UART0
->EVENTS_TXDRDY
) {
169 } else if((NRF_UART0
->INTENSET
& 0x04) && NRF_UART0
->EVENTS_RXDRDY
) {
178 void serial_irq_handler(serial_t
*obj
, uart_irq_handler handler
, uint32_t id
)
180 irq_handler
= handler
;
181 serial_irq_ids
[obj
->index
] = id
;
184 void serial_irq_set(serial_t
*obj
, SerialIrq irq
, uint32_t enable
)
186 IRQn_Type irq_n
= (IRQn_Type
)0;
188 switch ((int)obj
->uart
) {
197 obj
->uart
->INTENSET
= (UART_INTENSET_RXDRDY_Msk
);
200 obj
->uart
->INTENSET
= (UART_INTENSET_TXDRDY_Msk
);
203 NVIC_SetPriority(irq_n
, 3);
204 NVIC_EnableIRQ(irq_n
);
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;
212 obj
->uart
->INTENCLR
= (UART_INTENCLR_RXDRDY_Msk
);
213 all_disabled
= (obj
->uart
->INTENCLR
& (UART_INTENCLR_TXDRDY_Msk
)) == 0;
216 obj
->uart
->INTENCLR
= (UART_INTENCLR_TXDRDY_Msk
);
217 all_disabled
= (obj
->uart
->INTENCLR
& (UART_INTENCLR_RXDRDY_Msk
)) == 0;
222 NVIC_DisableIRQ(irq_n
);
227 //******************************************************************************
229 //******************************************************************************
230 int serial_getc(serial_t
*obj
)
232 while (!serial_readable(obj
)) {
235 obj
->uart
->EVENTS_RXDRDY
= 0;
237 return (uint8_t)obj
->uart
->RXD
;
240 void serial_putc(serial_t
*obj
, int c
)
242 while (!serial_writable(obj
)) {
245 obj
->uart
->EVENTS_TXDRDY
= 0;
246 obj
->uart
->TXD
= (uint8_t)c
;
249 int serial_readable(serial_t
*obj
)
251 return (obj
->uart
->EVENTS_RXDRDY
== 1);
254 int serial_writable(serial_t
*obj
)
256 return (obj
->uart
->EVENTS_TXDRDY
== 1);
259 void serial_break_set(serial_t
*obj
)
261 obj
->uart
->TASKS_SUSPEND
= 1;
264 void serial_break_clear(serial_t
*obj
)
266 obj
->uart
->TASKS_STARTTX
= 1;
267 obj
->uart
->TASKS_STARTRX
= 1;
270 void serial_set_flow_control(serial_t
*obj
, FlowControl type
, PinName rxflow
, PinName txflow
)
273 if (type
== FlowControlRTSCTS
|| type
== FlowControlRTS
) {
274 NRF_GPIO
->DIR |= (1<<rxflow
);
275 pin_mode(rxflow
, PullUp
);
276 obj
->uart
->PSELRTS
= rxflow
;
278 obj
->uart
->CONFIG
|= 0x01; // Enable HWFC
281 if (type
== FlowControlRTSCTS
|| type
== FlowControlCTS
) {
282 NRF_GPIO
->DIR &= ~(1<<txflow
);
283 pin_mode(txflow
, PullUp
);
284 obj
->uart
->PSELCTS
= txflow
;
286 obj
->uart
->CONFIG
|= 0x01; // Enable HWFC;
289 if (type
== FlowControlNone
) {
290 obj
->uart
->PSELRTS
= 0xFFFFFFFF; // Disable RTS
291 obj
->uart
->PSELCTS
= 0xFFFFFFFF; // Disable CTS
293 obj
->uart
->CONFIG
&= ~0x01; // Enable HWFC;
297 void serial_clear(serial_t
*obj
) {