Enumerations | Functions

Universal Asynchronous Receiver-Transmitter (hal_uart)
[nRF24LE1 HAL]


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)

Enumeration Type Documentation

Available Baud rates. The input argument of hal_uart_init must be defined in this enum

Enumerator:
UART_BAUD_600 

600 baud

UART_BAUD_1K2 

1200 baud

UART_BAUD_2K4 

2400 baud

UART_BAUD_4K8 

4800 baud

UART_BAUD_9K6 

9600 baud

UART_BAUD_19K2 

19.2 kbaud

UART_BAUD_38K4 

38.4 kbaud

UART_BAUD_57K6 

57.6 kbaud

UART_BAUD_115K2 

115.2 kbaud

UART_BAUD_600 

600 baud

UART_BAUD_1K2 

1200 baud

UART_BAUD_2K4 

2400 baud

UART_BAUD_4K8 

4800 baud

UART_BAUD_9K6 

9600 baud

UART_BAUD_19K2 

19.2 kbaud

UART_BAUD_38K4 

38.4 kbaud

UART_BAUD_57K6 

57.6 kbaud

UART_BAUD_115K2 

115.2 kbaud

Definition at line 41 of file hal_uart.h.


Function Documentation

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 is used.

Parameters:
bauda 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.

Parameters:
chCharacter 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 entire TX FIFO is empty.

Returns:
true if all data sent, else false.

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.

Returns:
Number of characters available

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.

Returns:
Character read

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