UART

Nano-RK supports an interrupt driven or polling mode of UART operation. The polling
mode makes call directly to the UART hardware. This can be slow since the UART
hardware only supports a single byte of hardware buffering and hence tasks operating at
low frequencies may drop data. In the interrupt driven mode, reception of data will trigger
an interrupt, store the byte in a circular buffer and send a signal which your tasks can suspend on.
By default the circular buffer is 16 bytes, however this can be adjusted in nrk_cfg.h.
See signaling section for an example
of having a task wait on UART data. For examples of how to use the UART, please refer to
the basic_serial project that comes with Nano-RK: basic_serial

Note, if you need to receive serial data be sure to add NRK_NO_POWER_DOWN into your nrk_cfg.h file, otherwise the processor will sleep an miss incomming data.

 1
 2  // For interrupt based serial receiving, uncomment below
 3  // #define NRK_NO_POWER_DOWN
 4
 5  // Enable buffered and signal controlled serial RX
 6  #define NRK_UART_BUF 1
 7
 8  // Set the size of the kernel serial buffer
 9  #define MAX_RX_UART_BUF 16

Example of Non-Blocking Read

 1char c;
 2nrk_sig_t uart_rx_signal;
 3
 4...
 5
 6  // Get the signal for UART RX
 7  uart_rx_signal=nrk_uart_rx_signal_get();
 8  // Register task to wait on signal
 9  nrk_signal_register(uart_rx_signal);
10
11  nrk_kprintf( PSTR("Press 's' to start\r\n" ));
12
13do{
14  if(nrk_uart_data_ready(NRK_DEFAULT_UART))
15        // Blocking Read that should only be called if you know data is in the buffer
16        c=getchar();
17  else 
18        // Suspend until UART data arrives
19        nrk_event_wait(SIG(uart_rx_signal));
20} while(c!='s');
21

nrk_setup_uart

void nrk_setup_uart( BAUDRATE )
Parameters: uint8_t BAUDRATE #define divisor to set baudrate
Return Values: none

This function configures the desired serial parameters.

UART_BAUDRATE_2K4
UART_BAUDRATE_4K8
UART_BAUDRATE_9K6
UART_BAUDRATE_14K4
UART_BAUDRATE_19K2
UART_BAUDRATE_28K8
UART_BAUDRATE_38K4
UART_BAUDRATE_57K6
UART_BAUDRATE_115K2
UART_BAUDRATE_230K4
UART_BAUDRATE_250K
UART_BAUDRATE_500K
UART_BAUDRATE_1M

Possible baud-rate values. Usable values depend on specific platform.

nrk_uart_rx_signal

nrk_sig_t nrk_uart_rx_signal_get()
Parameters: none
Return Values: nrk_sig_t signal sent when UART data is received

This function returns the rx packet signal generated when serial data arrives when using NRK_UART_BUF mode.

1  nrk_sig_t uart_rx_signal;
2
3  // Get the signal for UART RX
4  uart_rx_signal=nrk_uart_rx_signal_get();

nrk_kprintf

void nrk_kprintf( PSTR("const-string") )
Parameters: PSTR is a constant string stored in FLASH memory
Return Values: none

This function will print a string directly from ROM not using RAM. If you have constant
strings, use this to save on the .data section of your code.

nrk_uart_data_ready

uint8_t nrk_uart_data_ready(uint8_t uart_num) )

Parameters: uint8_t uart_num denotes the communication port number
Return Values: uint8_t with 1 for data ready, and 0 if no data has been received

This function is a non-blocking call that tells if there is UART data ready to be read
with getchar() or scanf(). Use NRK_DEFAULT UART as the port_num value if
you are unsure which UART on the processor is being used. The function returns 1 if there is data waiting and 0 if there is no data available. _uart_num
takes the uart number as a parameter. If you want the default uart, use NRK_DEFAULT_UART.

nrk_uart_set_trig_thresh

void nrk_uart_set_trig_thresh(uint8_t count) )

Parameters: uint8_t count defines the number of bytes before the UART signal is generated
Return Values: none

[TO BE IMPLEMENTED] It will allow setting the number of characters buffered required
before a signal is sent to a task.

printf

void printf(char *str, param1,...,param n)

Parameters: char *str is the paramater formating string
Return Values: none

This is the standard printf provided by libc. Any constant strings in here will be stored
in RAM and then pushed on to the stack. When at all possible, use nrk_kprintf() for
constant sections of strings.

For 8-bit AVR processors:

Symbol Used For
%d int8_t, int16_t
%u uint8_t, uint16_t
%x int8_t, uint8_t, int16_t, uint16_t (hex)
%o int8_t, uint8_t, int16_t, uint16_t (octal)
%s null terminated string
%c char, int8_t, uint8_t (ascii character)
%f floating point (not supported by default)
%lu uint32_t, unsigned long
%ld int32_t, signed long
%lx uint32_t, int32_t (hex)
%lo uint32_t, int32_t (octal)

Sizeof Table

Type sizeof(type)
char 1
int 2
short 2
long 4

getchar

char getchar() )

Parameters: none
Return Values: char containing the first value entered in the UART receive buffer

This is the standard getchar() function provided by libc. It will read a single character
from the serial port. If no data is available, this will block.

putchar

void putchar(char v) )
Parameters: char v is a character to be sent over the UART
Return Values: none

This is the standard putchar() function provided by libc. It blocks while it writes a
single character out the serial port.

Using the Second UART

Currently, only the default UART configured with nrk_uart_setup() is passed to stdio's functions. In order to access the second UART, there are the following low-level commands (keep in mind that the default UART is usually 1, however this could change depending on the platform:
  • void setup_uart0(uint16_t baudrate);
  • void putc0(char x);
  • char getc0(void);
Prev: Contents Next: Stack Top