Thursday, September 8, 2016

How to use two UART ports in CC2530 Z-Stack

The following steps show how to use two UART ports in CC2530 Z-Stack

1. Define HAL_UART=TRUE, HAL_UART_ISR=1, and HAL_UART_DMA=2 in compile options.
2. Initialize and use UART0 (P0_2 as RX/P0_3 as TX)  as the followings:

void initUart0(halUARTCBack_t pf)
{
  halUARTCfg_t uartConfig;
  uartConfig.configured           = TRUE;
  uartConfig.baudRate             = HAL_UART_BR_115200;
  uartConfig.flowControl          = FALSE;
  uartConfig.flowControlThreshold = 48;
  uartConfig.rx.maxBufSize        = 128;
  uartConfig.tx.maxBufSize        = 128;
  uartConfig.idleTimeout          = 6; 
  uartConfig.intEnable            = TRUE;            
  uartConfig.callBackFunc         = pf;
  HalUARTOpen (HAL_UART_PORT_0, &uartConfig);
}

void uart0RxCb( uint8 port, uint8 event )
{
  uint8  ch;
  while (Hal_UART_RxBufLen(port))
  {
    // Read one byte from UART to ch
    HalUARTRead (port, &ch, 1);
  }
}

initUart0(uart0RxCb);

//Output "UART0 output test" to P0.3
HalUARTWrite( HAL_UART_PORT_0, "UART0 output test", (byte)osal_strlen("UART0 output test"));

3. Initialize and use UART1 (P1_6 as TX/P1_7 as RX) as the followings:

void initUart1(halUARTCBack_t pf)
{
  halUARTCfg_t uartConfig;
  uartConfig.configured           = TRUE;
  uartConfig.baudRate             = HAL_UART_BR_115200;
  uartConfig.flowControl          = FALSE;
  uartConfig.flowControlThreshold = 48;
  uartConfig.rx.maxBufSize        = 128;
  uartConfig.tx.maxBufSize        = 128;
  uartConfig.idleTimeout          = 6; 
  uartConfig.intEnable            = TRUE;            
  uartConfig.callBackFunc         = pf;
  HalUARTOpen (HAL_UART_PORT_1, &uartConfig);
}

void uart1RxCb( uint8 port, uint8 event )
{
  uint8  ch;
  while (Hal_UART_RxBufLen(port))
  {
    // Read one byte from UART to ch
    HalUARTRead (port, &ch, 1);
  }
}

initUart1(uart0RxCb);

//Output "UART1 output test" to P1.6
HalUARTWrite( HAL_UART_PORT_1, "UART1 output test", (byte)osal_strlen("UART1 output test"));

2 comments:

  1. Thank for your page. But I got a error when add line "HAL_UART=TRUE" in hal_board_cfg.h
    Error[e16]: Segment XDATA_Z (size: 0x1d0e align: 0) is too long for segment definition. At least 0x10f more bytes needed. The problem occurred while processing the segment placement command "-Z(XDATA)XDATA_Z,XDATA_I=_XDATA_START-_XDATA_END", where at the moment of placement the available memory ranges were "XDATA:301-1eff"
    Reserved ranges relevant to this placement:
    XDATA:1-300 XSTACK
    XDATA:301-1eff XDATA_Z
    BIT:0-7 BREG
    BIT:80-87 SFR_AN
    BIT:90-97 SFR_AN
    BIT:a0-af SFR_AN
    BIT:b8-c7 SFR_AN
    BIT:e8-ef SFR_AN
    Error while running Linker



    ReplyDelete
  2. yes, i have same problem, z-stack 3.0

    ReplyDelete