I have an application running a serial communication through a RS485 interface. I have two sensors attached through I2C/TWi.
I have a app_timer that quwry the sensors once every sec.
I have a serial communication set up as such.
define SERIAL_FIFO_TX_SIZE 128
#define SERIAL_FIFO_RX_SIZE 128
#define SERIAL_BUFF_TX_SIZE 1
#define SERIAL_BUFF_RX_SIZE 1
static void serial1_rx_cb(struct nrf_serial_s const *p_serial, nrf_serial_event_t event);
NRF_SERIAL_UART_DEF(serial1_uarte, 0);
NRF_SERIAL_BUFFERS_DEF(serial1_buffs, SERIAL_BUFF_TX_SIZE, SERIAL_BUFF_RX_SIZE);
NRF_SERIAL_QUEUES_DEF(serial1_queues, SERIAL_FIFO_TX_SIZE, SERIAL_FIFO_RX_SIZE);
NRF_SERIAL_CONFIG_DEF(serial1_config, NRF_SERIAL_MODE_DMA,
&serial1_queues, &serial1_buffs, serial1_rx_cb, sleep_handler);
NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uarte1_drv_config,
MODBUS_RX_PIN, MODBUS_TX_PIN,
0, 0,
NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
NRF_UART_BAUDRATE_9600,
UART_DEFAULT_CONFIG_IRQ_PRIORITY);
I have initiated the serial port with this code:
void init_serial()
{
ret_code_t ret;
ret = app_timer_create(&m_app_timer_comm, APP_TIMER_MODE_REPEATED, sensor_timer_handler);
APP_ERROR_CHECK(ret);
ret = app_timer_start(m_app_timer_comm, APP_TIMER_TICKS(10), NULL); // RTC timer.
NRF_LOG_INFO("INITIALIZE SERIAL PORT")
ret = nrf_serial_uninit(&serial1_uarte);
APP_ERROR_CHECK(ret);
ret = nrf_serial_init(&serial1_uarte, &m_uarte1_drv_config, &serial1_config);
APP_ERROR_CHECK(ret);
NRF_LOG_INFO("CHANGE SERIAL CONFIGURATION")
setup_serial();
}
My receive evet is writte as such
static void serial1_rx_cb(struct nrf_serial_s const *p_serial, nrf_serial_event_t event)
{
static uint16_t index = 0;
uint32_t ret_val;
uint32_t Readed;
switch (event)
{
case NRF_SERIAL_EVENT_TX_DONE:
NRF_LOG_ERROR("NRF_SERIAL_EVENT_TX_DONE\r\n");
break;
case NRF_SERIAL_EVENT_FIFO_ERR: //!< RX FIFO overrun.
NRF_LOG_ERROR("NRF_SERIAL_EVENT_FIFO_ERR\r\n");
break;
case NRF_SERIAL_EVENT_DRV_ERR: //!< Internal driver error.
NRF_LOG_ERROR("NRF_SERIAL_EVENT_DRV_ERR\r\n");
break;
// fall through..
case NRF_SERIAL_EVENT_RX_DATA:
{
NRF_LOG_ERROR("NRF_SERIAL_EVENT_RX_DATA\r\n");
uint8_t MESSAGE[56];
nrf_serial_read(p_serial, &ResponseString[ResponseIndex], SERIAL_BUFF_RX_SIZE, &Readed, 0); // read at max 20 bytes
ResponseIndex+=Readed;
ResponseString[ResponseIndex]=0;
process_request();
}break;
} // end switch
}
When a character is received the process_request is triggered.
void process_request(){
if (ResponseIndex>0)
{
sprintf(MESSAGE2,"MODBUS:Message received:%s:\n\r",ResponseString);
ret_code_t err_code;
nrf_serial_rx_drain(&serial1_uarte);
nrf_gpio_pin_write(RS485_1_SND, 1);
COMM_Delay(1);
err_code = nrf_serial_write(&serial1_uarte, MESSAGE2, strlen(MESSAGE2), NULL, NRF_SERIAL_MAX_TIMEOUT);
nrf_serial_flush(&serial1_uarte, 50);
APP_ERROR_CHECK(err_code);
nrf_gpio_pin_write(RS485_1_SND,0);
COMM_Delay(1);
}
ResponseIndex=0;
}
The problem is that the entire application stops working when the nrf_serial_flush is called. The function remains in a while.
What is happening? It looks that every the timer inside nrf_serial_flush does not triggered anymore.
Please help!