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
// Enable buffered and signal controlled serial RX #define NRK_UART_BUF 1 // Set the size of the kernel serial buffer #define MAX_RX_UART_BUF 16
Example of Non-Blocking Read
char c; nrk_sig_t uart_rx_signal; ... // Get the signal for UART RX uart_rx_signal=nrk_uart_rx_signal_get(); // Register task to wait on signal nrk_signal_register(uart_rx_signal); nrk_kprintf( PSTR("Press 's' to start\r\n" )); do{ if(nrk_uart_data_ready(NRK_DEFAULT_UART)) // Blocking Read that should only be called if you know data is in the buffer c=getchar(); else // Suspend until UART data arrives nrk_event_wait(SIG(uart_rx_signal)); } while(c!='s');
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.
nrk_sig_t uart_rx_signal;
// Get the signal for UART RX
uart_rx_signal=nrk_uart_rx_signal_get();
nrk_kprintf
| void nrk_kprintf( PSTR("const-string") ) |
| Parameters: PSTR("const-string") 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.
