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

Implementation of uart interrupts

Hi,

I'm working now with the uart connection between NRF52832 and SIM868 module, my question is:

Is there any uart interrupts implementation (examples), i want to receive event when new data is received, and read this data (non blocking mode).

Actually i wrote a code that call an callback function when data is received, in this case i was able just to read one byte of the received data.
to resolve the problem i increased the RX buffer size to 100 bytes, and in this case if the data received is less than 100 bytes the callback doesn't be called.

This is my code:

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

#include "nrf.h"
#include "nrf_drv_clock.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_drv_power.h"
#include "nrf_serial.h"
#include "app_timer.h"


#include "app_error.h"
#include "app_util.h"


#define OP_QUEUES_SIZE          3
#define APP_TIMER_PRESCALER     NRF_SERIAL_APP_TIMER_PRESCALER

#define RX_PIN_NUMBER     3
#define TX_PIN_NUMBER     2
#define CTS_PIN_NUMBER    0xFFFFFFFF /** Value indicating that no pin is connected to this UART register. */
#define RTS_PIN_NUMBER    0xFFFFFFFF


// functions declaration
static void serial_rx_cb(struct nrf_serial_s const *p_serial, nrf_serial_event_t event);
bool serialRead();
bool serialWrite(char const *data, uint16_t length);


static void sleep_handler(void)
{
	__WFE();
	__SEV();
	__WFE();
}

NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uart0_drv_config,
                                RX_PIN_NUMBER, TX_PIN_NUMBER,
                                RTS_PIN_NUMBER, CTS_PIN_NUMBER,
                                NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
                                NRF_UART_BAUDRATE_9600,
                                UART_DEFAULT_CONFIG_IRQ_PRIORITY);

#define SERIAL_FIFO_TX_SIZE 32
#define SERIAL_FIFO_RX_SIZE 32

NRF_SERIAL_QUEUES_DEF(serial_queues, SERIAL_FIFO_TX_SIZE, SERIAL_FIFO_RX_SIZE);


#define SERIAL_BUFF_TX_SIZE 100
#define SERIAL_BUFF_RX_SIZE 100

NRF_SERIAL_BUFFERS_DEF(serial_buffs, SERIAL_BUFF_TX_SIZE, SERIAL_BUFF_RX_SIZE);

NRF_SERIAL_CONFIG_DEF(serial_config, NRF_SERIAL_MODE_IRQ,
					  &serial_queues, &serial_buffs, serial_rx_cb, sleep_handler);


NRF_SERIAL_UART_DEF(serial_uart, 0);


static void serial_rx_cb(struct nrf_serial_s const *p_serial, nrf_serial_event_t event)
{
    if (event == NRF_SERIAL_EVENT_RX_DATA)
    {
        printf("WE GOT DATA");
        serialRead();
    }
} 

int main(void)
{
    ret_code_t ret;

    ret = nrf_drv_clock_init();
    APP_ERROR_CHECK(ret);
    ret = nrf_drv_power_init(NULL);
    APP_ERROR_CHECK(ret);

    nrf_drv_clock_lfclk_request(NULL);
    ret = app_timer_init();
    APP_ERROR_CHECK(ret);

    ret = nrf_serial_init(&serial_uart, &m_uart0_drv_config, &serial_config);
    APP_ERROR_CHECK(ret);

    while(true)
    {
       printf("MAIN TASK...\n");
       serialWrite("AT\r\n", strlen("AT\r\n"));
       nrf_delay_ms(1000);
    }
}

bool serialRead()
{
    ret_code_t ret;
    uint8_t buffer[100];
    ret = nrf_serial_read(&serial_uart, buffer, sizeof(buffer), NULL, 1000);
    printf("DATA >> %s\n", buffer);
    return true;
}

bool serialWrite(char const *data, uint16_t length)
{
    printf("SENT >> %s\n", data);
    ret_code_t ret;
    ret = nrf_serial_write(&serial_uart, data, length, NULL, NRF_SERIAL_MAX_TIMEOUT);
    APP_ERROR_CHECK(ret);
    return true;
}

Related