当前位置:网站首页>Nrf52832 -- official routine ble_ app_ UART adds the LED feature to enable the computer UART and mobile app to control the LED on and off of the development board

Nrf52832 -- official routine ble_ app_ UART adds the LED feature to enable the computer UART and mobile app to control the LED on and off of the development board

2022-06-12 05:46:00 m0_ seven thousand seven hundred and eighty-eight

Hardware :nrf52832 Development board
Software : Compile environment :keil, SDK edition :nRF5_SDK_17.1.0_ddde560, Protocol stack version :s132_nrf52_7.2.0_softdevice
Realization function : In the official serial port routine ble_app_uart Add a feature based on , Used to control the led.

          1. adopt nRF Connect official APP Of RX Characteristic Write 1, control led2 bright , Write 0 control led destroy 
          2.win Computer serial assistant custom communication protocol ,TX Hair 16 Base number aa01 control led2 bright ,16 Base number aa00 control led destroy 

stay nodic official ble_app_uart There is no need to add other driver component libraries in , The above functions can be achieved by adding at the following location ( Bold is the new part ).

#define BLE_UART_LED

services_init register NUS,nus_init.data_handler = nus_data_handler;

static void nus_data_handler(ble_nus_evt_t * p_evt)
{
    

    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
    
        uint32_t err_code;

        NRF_LOG_DEBUG("nus_data_handler:Received data from BLE NUS. Writing data on UART.");
        NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);

        ****#ifdef BLE_UART_LED
            if (p_evt->params.rx_data.p_data[0] == '1') // Custom communication protocol on 
            {
    
                bsp_board_led_on(LED_2);
                NRF_LOG_INFO("led2 is open");
            }
            else if(p_evt->params.rx_data.p_data[0] == '0') // Custom communication protocol off 
            {
    
                bsp_board_led_off(LED_2);
                NRF_LOG_INFO("led2 is close");
            }
        #endif //BLE_UART_LED****

        for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
        {
    
            do
            {
    
                err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
                if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                {
    
                    NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
                    APP_ERROR_CHECK(err_code);
                }
            } while (err_code == NRF_ERROR_BUSY);
        }
        if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
        {
    
            while (app_uart_put('\n') == NRF_ERROR_BUSY);
        }
    }
}

uart_init Of APP_UART_FIFO_INIT call uart_event_handle,

void uart_event_handle(app_uart_evt_t * p_event)
{
    
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
    
        case APP_UART_DATA_READY:
            //data_array[index] Get the data sent from the computer serial port 
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {
    
                if (index > 1)
                {
    
                    NRF_LOG_DEBUG("uart_event_handle:Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                    ****#ifdef BLE_UART_LED
                    if (data_array[0] == 0xaa && data_array[1] == 0x01) // Custom communication protocol on 
                    {
    
                        pte_led_on(LED_3);
                    }
                    else if(data_array[0] == 0xaa && data_array[1] == 0x00) // Custom communication protocol off 
                    {
    
                        pte_led_off(LED_3);
                    }
                    
                    #endif //BLE_UART_LED****

                    do
                    {
    
                        uint16_t length = (uint16_t)index;
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ((err_code != NRF_ERROR_INVALID_STATE) &&
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))
                        {
    
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_RESOURCES);
                }

                index = 0; // Clear the array subscript after sending data . To continue caching subsequent serial port data .
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
原网站

版权声明
本文为[m0_ seven thousand seven hundred and eighty-eight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120544312473.html