Interface functions for the Universal Asynchronous Receiver-Transmitter (UART).
The MCU system is configured with one serial port that is identical in operation to the standard 8051 serial port (Serial interface 0). The two serial port signals RXD and TXD are available on device pins UART/RSD and UART/TXD. The serial port (UART) derives its clock from the MCU clock.
This module contains functions for configuring the baud rate of the UART and for "getting" and "putting" character from/to the serial interface. Circular buffering is added in firmware to ensure a user friendly and reliable communication.
Enumerations | |
enum | hal_uart_baudrate_t { UART_BAUD_600, UART_BAUD_1K2, UART_BAUD_2K4, UART_BAUD_4K8, UART_BAUD_9K6, UART_BAUD_19K2, UART_BAUD_38K4, UART_BAUD_57K6, UART_BAUD_115K2, UART_BAUD_600, UART_BAUD_1K2, UART_BAUD_2K4, UART_BAUD_4K8, UART_BAUD_9K6, UART_BAUD_19K2, UART_BAUD_38K4, UART_BAUD_57K6, UART_BAUD_115K2 } |
Functions | |
void | hal_uart_init (hal_uart_baudrate_t baud) |
void | hal_uart_putchar (uint8_t ch) |
_Bool | hal_uart_tx_complete () |
uint8_t | hal_uart_chars_available (void) |
Find number of characters in the UART receive buffer. | |
uint8_t | hal_uart_getchar (void) |
enum hal_uart_baudrate_t |
Available Baud rates. The input argument of hal_uart_init must be defined in this enum
Definition at line 42 of file hal_uart.h.
void hal_uart_init | ( | hal_uart_baudrate_t | baud ) |
Function to initialize the UART This function initializes the UART for interrupt operation. An 8 byte receive buffer and an 8 byte transmit buffer are used.
baud | a constant for the baud rate (0 = 600 Baud, ..., 7=57600 baud) |
Function to initialize the UART. This function initializes the UART for interrupt operation. An 8 byte receive buffer and an 8 byte transmit buffer is used.
baud | a constant for the baud rate (0 = 600 Baud, ..., 7=57600 baud) |
Definition at line 65 of file hal_uart.c.
{ uint16_t temp; ES0 = 0; // Disable UART0 interrupt while initializing uart_tx_wp = uart_tx_rp = 0; uart_tx_cnt = 0; uart_rx_wp = uart_rx_rp = 0; uart_rx_cnt = 0; REN0 = 1; // Enable receiver SM0 = 0; // Mode 1.. SM1 = 1; // ..8 bit variable baud rate PCON |= 0x80; // SMOD = 1 ADCON |= 0x80; // Select internal baud rate generator switch(baud) { case UART_BAUD_57K6: temp = BAUD_57K6; break; case UART_BAUD_38K4: temp = BAUD_38K4; break; case UART_BAUD_9K6: temp = BAUD_9K6; break; case UART_BAUD_19K2: default: temp = BAUD_19K2; break; } S0RELL = (uint8_t)temp; S0RELH = (uint8_t)(temp >> 8); TI0 = 0; ES0 = 1; // Enable UART0 interrupt }
void hal_uart_putchar | ( | uint8_t | ch ) |
Function to write a character to the UART transmit buffer.
ch | Character to write |
Definition at line 102 of file hal_uart.c.
{ // Wait until there is space in the TX buffer: while(uart_tx_cnt > UART_NBUF) ; ES0 = 0; if (uart_tx_cnt == 0) { S0BUF = ch; // Write first char directly to the UART SFR } else { uart_tx[uart_tx_wp] = ch; uart_tx_wp = (uart_tx_wp + 1) % UART_NBUF; } uart_tx_cnt++; ES0 = 1; }
_Bool hal_uart_tx_complete | ( | ) |
Function for checking the entire TX FIFO is empty.
Function for checking entire TX FIFO is empty.
Definition at line 127 of file hal_uart.c.
{ if(uart_tx_cnt == 0) { return true; } else { return false; } }
uint8_t hal_uart_chars_available | ( | void | ) |
Find number of characters in the UART receive buffer.
This function returns the number of characters available for reading in the UART receive buffer.
Definition at line 122 of file hal_uart.c.
{ return uart_rx_cnt; }
uint8_t hal_uart_getchar | ( | void | ) |
Function to read a character from the UART receive buffer.
Definition at line 138 of file hal_uart.c.
{ uint8_t ch; // Wait until a character is available: while(uart_rx_cnt == 0) {} ES0 = 0; ch = uart_rx[uart_rx_rp]; uart_rx_rp = (uart_rx_rp + 1) % UART_NBUF; uart_rx_cnt--; ES0 = 1; return ch; }