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

regarding the uart send and receiving on baudrate 115200

Hi Nordic, 

I am trying to use the nrf52840 to talk with my 4G module on baud rate of 115200, but I find it always come into error for the recving the uart data. Also it keep on lost data when it is running in baudrate of 115200. I have test the nrf52840 alone with the usb to uart converter the same issue. but for the 9600/19200/56000 baud rate with the usb to uart converter it works fine. 

 Since system need to run for 115200 instead of lower baud rate, I need to make 115200 works. Anyway to fix this?

I have attached my reference code here.

#include "simcom_uart_7600.h"
#include "app_uart.h"
#include "app_util_platform.h"
#include "nrf_drv_timer.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_delay.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif
#include "boards.h"

// AT commands
static const char AT_HEADER[] = "AT\r";
static const char AT_IPTR[] = "AT+IPR\r";
static const char AT_IPEX_SET_9600[] = "AT+IPEX=9600\r";
static const char AT_CGATT[] = "AT+CGATT?\r\r\n"; // Packet domain status (should attach automatically)
static const char AT_CGATT_ATTACH[] = "AT+CGATT=1\r\r\n"; // Packet domain attach
static const char AT_CGATT_DETACH[] = "AT+CGATT=0\r\r\n"; // Packet domain detach
static const char AT_GSN[] = "AT+CGSN\r\r\n"; // Show IMEI
static const char AT_CCID[] = "AT+CICCID\r\r\n";  // Read ICCID
static const char AT_CIPOPEN[] = "AT+CIPOPEN=0,\"TCP\",\"main.iot.voiapp.io\",1883\r\r\n";
static const char AT_CIPMODE_TRANSP[] = "AT+CIPMODE=1\r\r\n"; // Transparent mode
static const char AT_CIPMODE_NORMAL[] = "AT+CIPMODE=0\r\r\n"; // Normal mode
static const char AT_CIPSENDMODE[] = "AT+CIPSENDMODE=1\r\r\n";
static char AT_CIPSEND[] = "AT+CIPSEND=0,999\r\r\n";    // Send client data
static const char AT_NETOPEN[] = "AT+NETOPEN\r\r\n"; // bring up wireless connection
static const char AT_NETCLOSE[] = "AT+NETCLOSE\r\r\n"; // close wireless connection
static const char AT_CIFSR[] = "AT+IPADDR\r\r\n"; // Get IP address
static const char AT_CSQ[] = "AT+CSQ\r\r\n";   // Query signal quality
static const char AT_IFC_SET[] = "AT+IFC=2,0\r\r\n"; // Set local data flow control
static const char AT_IFC_OFF[] = "AT+IFC=0,0\r\r\n"; // Set local data flow control
static const char AT_CCLK[] = "AT+CCLK?\r\r\n"; // Real time clock management

static const char AT_CMEE_VERBOSE[] = "AT+CMEE=2\r\r\n"; // Enable +CME ERROR: result code and use verbose values
static const char AT_CMEE[] = "AT+CMEE?\r\r\n"; // Get current setting for error reporting
static const char AT_CREG_INFO[] = "AT+CREG?\r\r\n";
static const char AT_CREG[] = "AT+CREG\r\r\n";
static const char AT_CGREG[] = "AT+CGREG\r\r\n";
static const char AT_CIPSTATUS[] = "AT+CIPCLOSE?\r\r\n"; // returns the current connection status
static const char AT_CIPCLOSE[] = "AT+CIPCLOSE=0\r\r\n"; // closes the TCP/UDP connection
static const char AT_CIPRXGET[] = "AT+CIPRXGET=1\r\r\n"; // enables getting data from network manually
static const char AT_CIPRXGET_3_730[] = "AT+CIPRXGET=3,0,730\r\r\n";
static const char AT_CIPRXGET_2_1460[] = "AT+CIPRXGET=2,0,1460\r\r\n";
static const char AT_CNMP[] = "AT+CNMP?\r\r\n";

// GNSS
static const char AT_CGPSURL[] = "AT+CGPSURL=\"supl.google.com:7276\"\r\r\n";
static const char AT_CGNSSMODE[] = "AT+CGNSSMODE=15,1\r\r\n";
static const char AT_CGPSSSL[] = "AT+CGPSSSL=0\r\r\n";
static const char AT_GPSPWR[] = "AT+CGPS=1,2\r\r\n";
static const char AT_CGNSINFO[] = "AT+CGNSSINFO\r\r\n";

// NTP
static const char AT_CNTPCID[] = "AT+CNTPCID=1\r\r\n"; // ???
static const char AT_CNTP_SYNC[] = "AT+CNTP\r\r\n";
static const char AT_CNTP_SET_SERVER[] = "AT+CNTP=\"pool.ntp.org\",0\r\r\n";

static const char AT_HEARTBEAT[] = "AT\r\r\n";

static const char AT_CGSOCKCONT[] = "AT+CGSOCKCONT=1,\"IP\",\"m2m.tele2.com\"\r\r\n"; // sets up the apn, user name and password for the PDP context.
static const char AT_CTZU[] = "AT+CTZU=1\r\r\n";
static const char AT_CLBS[] = "AT+CLBS=1,1\r\r\n";

static const char AT_CGMM[] = "AT+CGMM\r\r\n";
static const char AT_CFUN[] = "AT+CFUN=1\r\r\n";
static const char AT_CRESET[] = "AT+CRESET\r\r\n";

static const char NETOPEN[] = "+NETOPEN: ";
static const char NETOPEN_0[] = "+NETOPEN: 0\r\n";
static const char NETOPEN_1[] = "+NETOPEN: 1\r\n";

static const char NETCLOSE_OK[] = "+NETCLOSE: 0";
static const char NETCLOSE_NOT_OPENED[] = "+NETCLOSE: 2";
static const char NETCLOSE_PREFIX[] = "+NETCLOSE: ";

static const char CIOPEN_RES_OK[] = "+CIPOPEN: 0,0";
static const char CIOPEN_RES_PREFIX[] = "+CIPOPEN: 0,";
static const char CIOPEN_ALREADY_OPEN[] = "+CIPOPEN: 0,4";

static const char CIPCLOSE_NOT_OPENED[] = "+CIPCLOSE: 0,4";
static const char CIPCLOSE_CLOSED[] = "+CIPCLOSE: 0,0";
static const char CIPCLOSE_OFF_PREFIX[] = "+CIPCLOSE: 0,";
static const char CIPCLOSE_ON_PREFIX[] = "+CIPCLOSE: 1,";

static const char IP_ERROR_PREFIX[] = "+IP ERROR:";
static const char IP_ERROR_MSG[] = "+IP ERROR: Network is already opened\r\n";
static const char IP_ERROR_NODATA_MSG[] = "+IP ERROR: No data\r\n";

static const char IPADDR_PREFIX[] = "+IPADDR: ";
static const char CIPSTAT_PREFIX[] = "+CIPSTAT: 0,";
static const char CIPSEND_PREFIX[] = "+CIPSEND: 0,";

static const char AT_CIPSEND_PREFIX[] = "AT+CIPSEND=0,";
static const char CIPRXGET_PREFIX[] = "+CIPRXGET: 2,0,";
static const char IPCLOSE_PREFIX[] = "+IPCLOSE: 0,";
static const char CIPSEND_SEND_PREFIX[] = "AT+CIPSEND: 0,";
static const char CREG_REGISTERED[] = "+CREG: 1";
static const char CREG_PREFIX[] = "+CREG: ";
static const char CREG_ERROR[] = "+CME ERROR: ";

static const char PB_DONE[] = "PB DONE.";
static const char SMS_DONE[] = "SMS DONE";
static const char CPIN_READY[] = "+CPIN: READY";


void sim7600_uart_config_15200(void)

{
  uint32_t err_code;
  //nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_PULLUP);
  const app_uart_comm_params_t comm_params =
  {
    RX_PIN_NUMBER,//??uart????
    TX_PIN_NUMBER,//??uart????
    RTS_PIN_NUMBER,//??uart RTS??,????????????RTS?CTS??,??????
    CTS_PIN_NUMBER,//??uart CTS??
    APP_UART_FLOW_CONTROL_DISABLED,//APP_UART_FLOW_CONTROL_ENABLED,//??uart????
    false,//??????
    NRF_UART_BAUDRATE_115200//uart??????115200bps
  };
  
  
  //reference why below 
  //https://devzone.nordicsemi.com/f/nordic-q-a/13585/nrf52832-uart-tx-pin-floating		
 
  
  
  nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_PULLDOWN);
  nrf_delay_ms(50);
  APP_UART_FIFO_INIT(&comm_params,
                         SIM_UART_TX_BUF_SIZE,
                         SIM_UART_RX_BUF_SIZE,
                         sim7600_uart_error_handle,
                         APP_IRQ_PRIORITY_HIGH,//APP_IRQ_PRIORITY_LOWEST,
                         err_code);
					
  //nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_PULLUP);
  APP_ERROR_CHECK(err_code);	
}

 void sim7600_uart_config_9600(void)
{
  uint32_t err_code;
  //nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_PULLUP);
  const app_uart_comm_params_t comm_params =
  {
    RX_PIN_NUMBER,//??uart????
    TX_PIN_NUMBER,//??uart????
    RTS_PIN_NUMBER,//??uart RTS??,????????????RTS?CTS??,??????
    CTS_PIN_NUMBER,//??uart CTS??
    APP_UART_FLOW_CONTROL_DISABLED,//APP_UART_FLOW_CONTROL_ENABLED,//??uart????
    false,//??????
    NRF_UART_BAUDRATE_9600//uart??????115200bps
  };
  
  
  //reference why below 
  //https://devzone.nordicsemi.com/f/nordic-q-a/13585/nrf52832-uart-tx-pin-floating		
 
  
  
  nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_PULLDOWN);
  nrf_delay_ms(50);
  APP_UART_FIFO_INIT(&comm_params,
                         SIM_UART_TX_BUF_SIZE,
                         SIM_UART_RX_BUF_SIZE,
                         sim7600_uart_error_handle,
                         APP_IRQ_PRIORITY_HIGH,//APP_IRQ_PRIORITY_LOWEST,
                         err_code);
					
  //nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_PULLUP);
  APP_ERROR_CHECK(err_code);	
}


//recall function for the uart
void sim7600_uart_error_handle(app_uart_evt_t * p_event)
{
	uint8_t cr;

	//communication error
	if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
	{
			NRF_LOG_INFO("error");
			//APP_ERROR_HANDLER(p_event->data.error_communication);
	}
	//FIFO error 
	else if (p_event->evt_type == APP_UART_FIFO_ERROR)
	{
			APP_ERROR_HANDLER(p_event->data .error_code);
	}
	//uart rx 
	else if (p_event->evt_type == APP_UART_DATA_READY)
	{
			
			//nrf_gpio_pin_toggle(LED_1);
			//from FIFO, reading
			app_uart_get(&cr);
			//output fata 
			//printf("%c",cr);
			NRF_LOG_INFO("%c",cr);
			app_uart_flush();
	}
	//send finish
	else if (p_event->evt_type == APP_UART_TX_EMPTY)
	{
			
			//nrf_gpio_pin_toggle(LED_2);
			//nrf_gpio_pin_toggle(BLUE_LED);
			app_uart_flush();
	}
}
/*
void sim7600_uart_error_handle(app_uart_evt_t * p_event)
{
    //??????
	
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        //APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    //FIFO????
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
}
*/
void sim7600_power_pin_init(void)
{
	nrf_gpio_cfg_output(SIM_PEN);
	nrf_gpio_cfg_output(SIM_PWRKEY);
}

void sim7600_power_on(void)
{
	nrf_gpio_pin_set(SIM_PEN);
	nrf_delay_ms(1000);
	nrf_gpio_pin_set(SIM_PWRKEY);
	
}


void sim7600_power_off(void)
{
    nrf_gpio_pin_clear(SIM_PEN);
	nrf_gpio_pin_clear(SIM_PWRKEY);
}

void UART_WriteData(uint8_t *pData, uint8_t dataLen)
{
    uint8_t i;
        for(i = 0; i < dataLen; i++)
        {  
            app_uart_put( pData[i]);
        }
}

void sim7600_init(void)
{
	
	sim7600_power_pin_init();
	#ifdef DEBUG_UART_INFO_RTT
    NRF_LOG_INFO("sim7600_power_pin_init\n");
	#endif	
	
	sim7600_power_on();
	#ifdef DEBUG_UART_INFO_RTT
    NRF_LOG_INFO("sim7600_power_on\n");
	#endif
	nrf_delay_ms(500);
	#ifdef DEBUG_UART_INFO_RTT
	NRF_LOG_INFO("sim7600_PWR_clr\n");
	#endif
	nrf_gpio_pin_clear(SIM_PWRKEY);
	nrf_delay_ms(500);

	
	#ifdef DEBUG_UART_INFO_RTT
    NRF_LOG_INFO("uart_config\n");
	#endif
	
	sim7600_uart_config_15200();
	nrf_delay_ms(15000);
	NRF_LOG_INFO("sim1");
	UART_WriteData(AT_HEADER,sizeof(AT_HEADER));
	//UART_WriteData(AT_IPEX_SET_9600,sizeof(AT_IPEX_SET_9600));
	
    
	//nrf_delay_ms(1000);
	//sim7600_uart_config_9600();

	nrf_delay_ms(1000);
	NRF_LOG_INFO("sim2");
	UART_WriteData(AT_CGATT,sizeof(AT_CGATT));
	nrf_delay_ms(1000);
		NRF_LOG_INFO("sim3");
	UART_WriteData(AT_CGATT_ATTACH,sizeof(AT_CGATT_ATTACH));
	nrf_delay_ms(1000);
		NRF_LOG_INFO("sim4");
	UART_WriteData(AT_CIPSTATUS,sizeof(AT_CIPSTATUS));
	nrf_delay_ms(1000);
			NRF_LOG_INFO("sim4");
	UART_WriteData(AT_CIPSTATUS,sizeof(AT_CIPSTATUS));
	nrf_delay_ms(1000);
			NRF_LOG_INFO("sim4");
	UART_WriteData(AT_CIPSTATUS,sizeof(AT_CIPSTATUS));
	nrf_delay_ms(1000);
    /*	*/
}

Parents Reply Children
No Data
Related