Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
Simple UART driver

Functions

uint8_t simple_uart_get (void)
 Function for reading a character from UART. Execution is blocked until UART peripheral detects character has been received. More...
 
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 received. Execution is blocked until UART peripheral detects character has been received or until the timeout expires, which even occurs first. More...
 
void simple_uart_put (uint8_t cr)
 Function for sending a character to UART. Execution is blocked until UART peripheral reports character to have been send. More...
 
void simple_uart_putstring (const uint8_t *str)
 Function for sending a string to UART. Execution is blocked until UART peripheral reports all characters to have been send. Maximum string length is 254 characters including null character in the end. More...
 
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. More...
 

Detailed Description

Function Documentation

uint8_t simple_uart_get ( void  )

Function for reading a character from UART. Execution is blocked until UART peripheral detects character has been received.

Returns
cr Received character.

Definition at line 22 of file simple_uart.c.

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 }
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 received. Execution is blocked until UART peripheral detects character has been received or until the timeout expires, which even occurs first.

Returns
bool True, if byte is received before timeout, else returns False.
Parameters
timeout_msmaximum time to wait for the data.
rx_datapointer to the memory where the received data is stored.

Definition at line 34 of file simple_uart.c.

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 }
void simple_uart_put ( uint8_t  cr)

Function for sending a character to UART. Execution is blocked until UART peripheral reports character to have been send.

Parameters
crCharacter to send.

Definition at line 62 of file simple_uart.c.

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 }
void simple_uart_putstring ( const uint8_t *  str)

Function for sending a string to UART. Execution is blocked until UART peripheral reports all characters to have been send. Maximum string length is 254 characters including null character in the end.

Parameters
strNull terminated string to send.

Definition at line 75 of file simple_uart.c.

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 }
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
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.

Parameters
baudrateBaudrate to use.
rts_pin_numberChip pin number to be used for UART RTS
txd_pin_numberChip pin number to be used for UART TXD
cts_pin_numberChip pin number to be used for UART CTS
rxd_pin_numberChip pin number to be used for UART RXD
hwfcEnable hardware flow control

Definition at line 87 of file simple_uart.c.

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 }