Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
simple_uart.c
Go to the documentation of this file.
1 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
2  *
3  * The information contained herein is property of Nordic Semiconductor ASA.
4  * Terms and conditions of usage are described in detail in NORDIC
5  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6  *
7  * Licensees are granted free, non-transferable use of the information. NO
8  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
9  * the file.
10  *
11  */
12 
15 #include <stdint.h>
16 
17 #include "nrf.h"
18 #include "simple_uart.h"
19 #include "nrf_delay.h"
20 #include "nrf_gpio.h"
21 
22 uint8_t simple_uart_get(void)
23 {
24  while (NRF_UART0->EVENTS_RXDRDY != 1)
25  {
26  // Wait for RXD data to be received
27  }
28 
29  NRF_UART0->EVENTS_RXDRDY = 0;
30  return (uint8_t)NRF_UART0->RXD;
31 }
32 
33 
34 bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t * rx_data)
35 {
36  bool ret = true;
37 
38  while (NRF_UART0->EVENTS_RXDRDY != 1)
39  {
40  if (timeout_ms-- >= 0)
41  {
42  // wait in 1ms chunk before checking for status.
43  nrf_delay_us(1000);
44  }
45  else
46  {
47  ret = false;
48  break;
49  }
50  } // Wait for RXD data to be received.
51 
52  if (timeout_ms >= 0)
53  {
54  // clear the event and set rx_data with received byte.
55  NRF_UART0->EVENTS_RXDRDY = 0;
56  *rx_data = (uint8_t)NRF_UART0->RXD;
57  }
58 
59  return ret;
60 }
61 
62 void simple_uart_put(uint8_t cr)
63 {
64  NRF_UART0->TXD = (uint8_t)cr;
65 
66  while (NRF_UART0->EVENTS_TXDRDY != 1)
67  {
68  // Wait for TXD data to be sent.
69  }
70 
71  NRF_UART0->EVENTS_TXDRDY = 0;
72 }
73 
74 
75 void simple_uart_putstring(const uint8_t * str)
76 {
77  uint_fast8_t i = 0;
78  uint8_t ch = str[i++];
79 
80  while (ch != '\0')
81  {
82  simple_uart_put(ch);
83  ch = str[i++];
84  }
85 }
86 
87 void simple_uart_config(uint32_t baudrate,
88  uint8_t rts_pin_number,
89  uint8_t txd_pin_number,
90  uint8_t cts_pin_number,
91  uint8_t rxd_pin_number,
92  bool hwfc)
93 {
94  nrf_gpio_cfg_output(txd_pin_number);
95  nrf_gpio_cfg_input(rxd_pin_number, NRF_GPIO_PIN_NOPULL);
96 
97  NRF_UART0->PSELTXD = txd_pin_number;
98  NRF_UART0->PSELRXD = rxd_pin_number;
99  if (hwfc)
100  {
101  nrf_gpio_cfg_output(rts_pin_number);
102  nrf_gpio_cfg_input(cts_pin_number, NRF_GPIO_PIN_NOPULL);
103  NRF_UART0->PSELCTS = cts_pin_number;
104  NRF_UART0->PSELRTS = rts_pin_number;
105  NRF_UART0->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);
106  }
107 
108  NRF_UART0->BAUDRATE = (baudrate << UART_BAUDRATE_BAUDRATE_Pos);
109  NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
110  NRF_UART0->TASKS_STARTTX = 1;
111  NRF_UART0->TASKS_STARTRX = 1;
112  NRF_UART0->EVENTS_RXDRDY = 0;
113 }
uint8_t simple_uart_get(void)
Function for reading a character from UART. Execution is blocked until UART peripheral detects charac...
Definition: simple_uart.c:22
void simple_uart_putstring(const uint8_t *str)
Function for sending a string to UART. Execution is blocked until UART peripheral reports all charact...
Definition: simple_uart.c:75
void simple_uart_put(uint8_t cr)
Function for sending a character to UART. Execution is blocked until UART peripheral reports characte...
Definition: simple_uart.c:62
bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t *rx_data)
Function for reading a character from UART with timeout on how long to wait for the byte to be receiv...
Definition: simple_uart.c:34
void simple_uart_config(uint32_t baudrate, uint8_t rts_pin_number, uint8_t txd_pin_number, uint8_t cts_pin_number, uint8_t rxd_pin_number, bool hwfc)
UART configuration.
Definition: simple_uart.c:87
Simple UART driver.