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

App uart error

Hi All

I am working on nrf52832 but not using the SDK. how ever I had created the uart functionality on my nrf but many times I got the communication error.

I had taken the references from the uart example provided in the sdk and using APP uart example I created this uart setup.

I am trying to communicate nrf with esp over uart.

But I am failed in sending and receiving data. 

Can I get some help in that.

/*
 * Copyright (c) 2017, CoolR Group Inc
 * All Rights Reserved.
 *
 * File: uart.c
 *
 * Description:  APIs for accessing SoC uart
 *
 * Author: VAIBHAV
 *
 */

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "uart.h"
#include "config.h"
#include "nrf_gpio.h"
#include "app_util_platform.h"
#include "coolerBoard.h"
#include "debug.h"
#include "nrf_delay.h"
#include "app_uart.h"
#include "esp.h"
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
#define UART_INST 0

//nrf_drv_uart_t  uart_nrf = NRF_DRV_UART_INSTANCE(UART_INST);

/***********************************************************//**
 * Initialization of the uart peripheral. 
 * @param uart_config_t uartConfig 
 * 		  Configuration structure for initialising uart
 * @return error code
 **************************************************************/
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[40];
    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] == 0x0A && data_array[index -2] == 0x0D)
                {
                    esp_parse_data(data_array,index);
                    index = 0;
                }
                break;
            case APP_UART_COMMUNICATION_ERROR:
                print(LL_PRINT,"COMMUNICATION ERROR\r\n");
                break;

            case APP_UART_FIFO_ERROR:
               print(LL_PRINT,"FIFO ERROR\r\n");
                break;

            default:
                break;
        }
}

ret_code_t uartInit(uart_config_t uartConfig, nrf_uart_event_handler_t uart_event_handler)
{
 /*   ret_code_t ret = NRF_SUCCESS;

    const nrf_drv_uart_config_t config =
    {
        .pseltxd = uartConfig.tx_pin,
        .pselrxd = uartConfig.rx_pin,
        .baudrate = (nrf_uart_baudrate_t)uartConfig.baudrate,
        .interrupt_priority = uartConfig.intr_priotity,
        .hwfc = uartConfig.hwfc,
        .parity = uartConfig.parity,
        .p_context = NULL,
    };

    ret = nrf_drv_uart_init( &uart_nrf, &config, uart_event_handler );

    return ret;	*/
    uint32_t err_code;
    app_uart_comm_params_t const comm_params =
        {
            .rx_pin_no = uartConfig.rx_pin,
            .tx_pin_no = uartConfig.tx_pin,
            .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
            .use_parity = false,
            .baud_rate = (nrf_uart_baudrate_t)uartConfig.baudrate,
        };

    APP_UART_FIFO_INIT(&comm_params,UART_RX_BUF_SIZE,UART_TX_BUF_SIZE,uart_event_handle,APP_IRQ_PRIORITY_HIGHEST,err_code);
    return err_code;
}

ret_code_t uartUnInit()
{
    ret_code_t ret = NRF_SUCCESS;
    
//    nrf_drv_uart_uninit(&uart_nrf);

    return ret;	
}
/***********************************************************//**
 * uart Receive
 * @param p_data - Pointer to the data 
 * @param length - Number of bytes to Receive
 *  
 * @return error code
 **************************************************************/
ret_code_t uartReceive(uint8_t * p_data,uint8_t length)
{
    ret_code_t ret = NRF_SUCCESS;
 /*   ret = nrf_drv_uart_rx(&uart_nrf, p_data, length);
    if(ret != NRF_SUCCESS)
        {
            print(LL_ERROR,"UART RECEIVE ERROR\r\n");
        }
    else
        {
               
        }    */
    return ret;   
}
/***********************************************************//**
 * uart Transmit 
 * @param p_data - Pointer to the data 
 * @param length - Number of bytes to Send
 *  
 * @return error code
 **************************************************************/
ret_code_t uartTransmit(uint8_t  const * const  p_data,uint32_t length)
{
	ret_code_t ret = NRF_SUCCESS;
    uint8_t data[200];
    if(p_data == NULL)
	{
		ret = NRF_ERROR_INVALID_PARAM;
		return ret;
	}
	
	if(length != 0)
        memcpy(&data[0], &p_data[0], length);

    for (uint32_t i = 0; i < length;i++)
    {
        //print(LL_PRINT, "%X ", data[i]);
        ret  = app_uart_put(data[i]);
        if(ret != NRF_SUCCESS)
            {
                print(LL_PRINT,"UART DATA SENDING FAILED\r\n");
            }
    }
    return ret;
	
}

Please review and let me know the issue.

Parents Reply Children
No Data
Related