> 文章列表 > 3-uart 示例

3-uart 示例

3-uart 示例

1.关键文件
1.1 nrf52811.h
芯片寄存器定义
nRF5_SDK_17.1.0_ddde560\\modules\\nrfx\\mdk\\nrf52811.h

1.2nrf52811_bitfields.h
nRF5_SDK_17.1.0_ddde560\\modules\\nrfx\\mdk\\nrf52811_bitfields.h

1.3nrfx_common.h
nRF5_SDK_17.1.0_ddde560\\modules\\nrfx\\drivers\\nrfx_common.h

1.4PCA10040.h
目标板管脚定义
nRF5_SDK_17.1.0_ddde560\\components\\boards\\PCA10040.h

1.5nrf_uart.h
串口寄存器操作
NnRF5_SDK_17.1.0_ddde560\\modules\\nrfx\\hal\\nrf_uart.h

1.6nrfx_uart.c
uart驱动,调用nrf_uart.h
nRF5_SDK_17.1.0_ddde560\\modules\\nrfx\\drivers\\src\\nrfx_uart.c

1.7nrf_drv_uart.h/nrf_drv_uart.c(legacy)
nRF5_SDK_17.1.0_ddde560\\integration\\nrfx\\legacy\\nrf_drv_uart.h
SDK\\nRF5_SDK_17.1.0_ddde560\\integration\\nrfx\\legacy\\nrf_drv_uart.c

1.8app_uart.h
nRF5_SDK_17.1.0_ddde560\\components\\libraries\\uart\\app_uart.h

2.示例
 

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

//#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */

#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */

#define UART_BUFFER_SIZE 256
static uint8_t m_uart_buffer[UART_BUFFER_SIZE];
static uint8_t m_rx_data;
static uint8_t m_rx_index = 0;

static void uart_event_handler(app_uart_evt_t *p_event)
{
  switch (p_event->evt_type)
  {
    case APP_UART_DATA_READY:
      while (app_uart_get(&m_rx_data) == NRF_SUCCESS)
      {
        if (m_rx_data == '\\r' || m_rx_data == '\\n')
        {
          m_uart_buffer[m_rx_index] = '\\0';
          //NRF_LOG_INFO("Received data: %s", m_uart_buffer);
          //printf("\\r\\nrecv data:%s\\r\\n",m_uart_buffer);
          m_rx_index = 0;
        }
        else if (m_rx_index < UART_BUFFER_SIZE - 1)
        {
          m_uart_buffer[m_rx_index++] = m_rx_data;
        }
      }
      break;

    case APP_UART_COMMUNICATION_ERROR:
      //NRF_LOG_ERROR("Communication error occurred");
      APP_ERROR_HANDLER(p_event->data.error_communication);
      break;

    case APP_UART_FIFO_ERROR:
      //NRF_LOG_ERROR("FIFO error occurred");
      APP_ERROR_HANDLER(p_event->data.error_code);
      break;

    default:
      break;
  }
}

# if 0
static uint8_t m_uart_buffer[UART_BUFFER_SIZE];
static uint8_t m_rx_data;
static uint8_t m_rx_index = 0;

void uart_error_handle(app_uart_evt_t * p_event)
{

    
   
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
     else if(p_event->evt_type == APP_UART_DATA_READY)
     {
        UNUSED_VARIABLE(app_uart_get(&data_recv[index]));
     index++;

     if((data_recv[index-1]=='\\n')||(data_recv[index-1]=='\\r')||(index>=255))
    {
        if(index>1)
        {
          printf("\\r\\nrecv data:%s\\r\\n",data_recv);
        }
            index=0;
            data_recv[0]='\\n';
            //memset(data_recv,0,sizeof(data_recv));
    }
     }
}
#endif

#ifdef ENABLE_LOOPBACK_TEST
/* Use flow control in loopback test. */
#define UART_HWFC APP_UART_FLOW_CONTROL_ENABLED

/** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
 */
static void show_error(void)
{

    bsp_board_leds_on();
    while (true)
    {
        // Do nothing.
    }
}

/** @brief Function for testing UART loop back.
 *  @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
 *  @note  @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
 */
static void uart_loopback_test()
{
    uint8_t * tx_data = (uint8_t *)("\\r\\nLOOPBACK_TEST\\r\\n");
    uint8_t   rx_data;

    // Start sending one byte and see if you get the same
    for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
    {
        uint32_t err_code;
        while (app_uart_put(tx_data[i]) != NRF_SUCCESS);

        nrf_delay_ms(10);
        err_code = app_uart_get(&rx_data);

        if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
        {
            show_error();
        }
    }
    return;
}
#else
/* When UART is used for communication with the host do not use flow control.*/
#define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
#endif

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t err_code;

    bsp_board_init(BSP_INIT_LEDS);

    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
#if defined (UART_PRESENT)
          NRF_UART_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_event_handler,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);

#ifndef ENABLE_LOOPBACK_TEST
    printf("\\r\\nUART example started.\\r\\n");

    while (true)
    {
        uint8_t cr;
        while (app_uart_get(&cr) != NRF_SUCCESS);
        while (app_uart_put(cr) != NRF_SUCCESS);

        if (cr == 'q' || cr == 'Q')
        {
            printf(" \\r\\nExit!\\r\\n");

            while (true)
            {
                // Do nothing.
            }
        }
    }
#else

    // This part of the example is just for testing the loopback .
    while (true)
    {
        uart_loopback_test();
    }
#endif
}