nRF_SDK_17.1.0 s140 pca10056 BLE_UART

Hello all,

I am trying to develop an application where the user will input some specific commands i.e hex value of 0x01 via a mobile application and then via bluetooth this command will be passed to the computer using UART.

I have compiled the ble app uart on my nrf52840 but i am struggling to modify the code for my application.

Could someone help me with this ? 

Thanks

Kleanthis

Parents
  • Thank you now when i send the command 0x01 via nrf connect from my phone i can see on termite that the device is activated. Now regarding the second part of my previous question. I would like when the user inputs that command (0x01 via BLE) to transfer it via UART to my computer. I have studied the app_uart function but i could not find any information on how i can transfer that command to UART.

    Could it be possible to help me with that ? I am guessing i shall introduce the m_device_state inside that function and when is set to 1 then it will transfer the data through the data_array ? 

    /**@brief   Function for handling app_uart events.
     *
     * @details This function will receive a single character from the app_uart module and append it to
     *          a string. The string will be be sent over BLE when the last character received was a
     *          'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
     */
    /**@snippet [Handling the data received over UART] */
    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:
                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("Ready to send data over BLE NUS");
                        NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                        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;
                }
                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;
        }
    }

  • 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("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);
    
        if (p_evt->params.rx_data.p_data[0] == COMMAND_ACTIVATION) {
          m_device_state = 1;
        }
    
        if (m_device_state ==1 ) {
          NRF_LOG_INFO("Device is activated.");
          app_uart_put(COMMAND_ACTIVATION);
        } else {
          NRF_LOG_INFO("Device not activated.");
        }
      } else if (p_evt->type == BLE_NUS_EVT_TX_RDY) {
        NRF_LOG_DEBUG(" Service is ready to accept new data to be transmitted..");
      } else if (p_evt->type == BLE_NUS_EVT_COMM_STARTED) {
        NRF_LOG_DEBUG("NUS Notification Enable.");
        //app_timer_start(m_timer_nus, APP_TIMER_TICKS(10), NULL);
      } else if (p_evt->type == BLE_NUS_EVT_COMM_STOPPED) {
        NRF_LOG_DEBUG("NUS Notification Disable.");
      }
    }
    
    Thanks a lot for clarifying this. Following your advice and after checking what the app_uart_put gets which if i am not mistaken it just generates a buffer that will send the data via UART correct ? 

    I have modified my code and added the app_uart_put command. It seems that the command needs to be in a loop to get update every transmission correct? How shall i  modify my code to make this happen ? 

    Thank you very much for your time.

Reply
  • 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("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);
    
        if (p_evt->params.rx_data.p_data[0] == COMMAND_ACTIVATION) {
          m_device_state = 1;
        }
    
        if (m_device_state ==1 ) {
          NRF_LOG_INFO("Device is activated.");
          app_uart_put(COMMAND_ACTIVATION);
        } else {
          NRF_LOG_INFO("Device not activated.");
        }
      } else if (p_evt->type == BLE_NUS_EVT_TX_RDY) {
        NRF_LOG_DEBUG(" Service is ready to accept new data to be transmitted..");
      } else if (p_evt->type == BLE_NUS_EVT_COMM_STARTED) {
        NRF_LOG_DEBUG("NUS Notification Enable.");
        //app_timer_start(m_timer_nus, APP_TIMER_TICKS(10), NULL);
      } else if (p_evt->type == BLE_NUS_EVT_COMM_STOPPED) {
        NRF_LOG_DEBUG("NUS Notification Disable.");
      }
    }
    
    Thanks a lot for clarifying this. Following your advice and after checking what the app_uart_put gets which if i am not mistaken it just generates a buffer that will send the data via UART correct ? 

    I have modified my code and added the app_uart_put command. It seems that the command needs to be in a loop to get update every transmission correct? How shall i  modify my code to make this happen ? 

    Thank you very much for your time.

Children
  • Hello again, Kleanthis

    kleanthise said:
    Thanks a lot for clarifying this.
    kleanthise said:
    Thank you very much for your time.

    No problem at all, I am happy to help!

    kleanthise said:
    Following your advice and after checking what the app_uart_put gets which if i am not mistaken it just generates a buffer that will send the data via UART correct ? 

    No this is not quite correct - the app_uart_put function takes a single byte as its argument, and places it in the hardware UART peripheral's TX buffer which will then be transfer over the hardware UART lines.

    kleanthise said:
    I have modified my code and added the app_uart_put command. It seems that the command needs to be in a loop to get update every transmission correct? How shall i  modify my code to make this happen ? 

    What do you mean when you say that it must be in a loop?
    There might be some confusion here between the hardware UART peripheral's RX and TX, and the emulated UART over BLE (NUS).
    The app_uart_put is strictly working with the hardware UART peripheral of the nRF device. It has nothing to do with BLE, and will only output the byte it is given on the hardware UART lines.
    The nus_data_handle handles NUS related events, i.e when data is received over the emulated BLE UART, such as the reception of data over the emulated BLE UART (NUS) which generates the BLE_NUS_EVT_RX_DATA event.

    Best regards,
    Karl

Related