This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Uart init problem

I work with ble_app_uart example and want to move uart init and event handler into separate file( e.g uart.c). I just copy uart_init, uart_event_handler functions with some macros into uart.c file.

after that uart module initilized (check in registers) but not working. Can't get any events.

here's my new ab_uart.c

 #include "ab_uart.h"
#include "ble_nus.h"
#include "SEGGER_RTT.h"

static uint16_t   m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3;            /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */


     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++;
        
        				
        				SEGGER_RTT_printf(0, "app_uart_data_ready_flag\n\r");
        				
        				
        				
                    if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len)))
                    {
                        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_string_send(&m_nus, data_array, &length);
                            if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) )
                            {
                                APP_ERROR_CHECK(err_code);
                            }
                        } while (err_code == NRF_ERROR_BUSY);
        
                        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;
            }
        }
        
        
        void uart_init(void)
        {
            uint32_t                     err_code;
            app_uart_comm_params_t const comm_params =
            {
                .rx_pin_no    = 8,
                .tx_pin_no    = 6,
                .rts_pin_no   = 5,
                .cts_pin_no   = 7,
                .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
                .use_parity   = false,
                .baud_rate    = UART_BAUDRATE_BAUDRATE_Baud115200
            };
        
            APP_UART_FIFO_INIT(&comm_params,
                               UART_RX_BUF_SIZE,
                               UART_TX_BUF_SIZE,
                               uart_event_handle,
                               APP_IRQ_PRIORITY_LOWEST,
                               err_code);
            APP_ERROR_CHECK(err_code);
        }

this ab_uart.h file

#ifndef AB_UART_H
#define AB_UART_H

#define UART_TX_BUF_SIZE                256                                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE                256                                         /**< UART RX buffer size. */

#include "app_uart.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "ble_nus.h"

BLE_NUS_DEF(m_nus);
void uart_init(void);
void uart_event_handle(app_uart_evt_t * p_event);

#endif
Related