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

Unable to receive UART response when combined the NUS example with custom service central example

Hi,

I am using a central example to scan the peripheral and connects to it. I get data through notifications in bulk. That works fine. Then in order to add support to FONA808 module, I need to have UART communication which I have established using NUS peripheral example (because that uses UART). Both of the examples work fine when I use them separately.

Now when I have combined NUS code (without use of peripheral) inside the central example, I first got into problem of NO_MEMORY(err_code=0x04) issue because I was adding 2 services (Custom & NUS). I just can live without custom service on central so I just commented that out and now I don't have that issue. I am just having NUS(1) service in my code (although I also don't need it).

However, the problem in this combined code is that:

NUS service is added successfully, UART is initiated successfully, pins assignment is same as in working NUS example, data bits are transmitted serially through the following code:

for(int i=0;i<strlen(AT_cmd);i++)		
{
    while(app_uart_put(AT_cmd[i]) != NRF_SUCCESS);
}

Since this code does not halt inside the while loop, so (I am assuming that)  the data bits are being transferred successfully (please correct me if I am wrong, do I need to see that on oscilloscope?).

But after this, I am expecting response from FONA808 which I never receive and I am stuck in loop waiting for the response forever.

Just to mention again that FONA808 works fine with NUS-standalone code (when not mixed with central code), so I am sure FONA808 module is working fine and the Tx/Rx pins are the same in the combined code.

Can anyone think of possible reasons for this problem? I will be obliged if someone can give me a hint to debug why the serial communication not working well in my situation.

Thanks

Parents
  • UPDATE: I have just checked on the oscilloscope that both Tx and Rx pins of nRF52 are getting data on them.

    Meaning UART is working fine while transmitting the data at least (UART of nRF sends data and gets the response back from FONA808).

    However, due to some reason the UART in nRF is not detecting the received response, because of which the application keeps on waiting for the response.

    Here is my code for the UART event handler which is supposed to handle the received packets from FONA808 module.

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        static uint8_t data_array[FONA_CMD_RET_MAX_DATA_LEN];
        static uint8_t index = 0;
        uint32_t       err_code;
    		static char* http_read;
    
        switch (p_event->evt_type)
        {
    				//Test
    				SEGGER_RTT_printf(0, "\nReceived some response from UART\n");
            case APP_UART_DATA_READY:			//Gets the event at each byte receive
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
    						
                if (data_array[index - 1] == '\n') 															//AK removed the the upper limit on BLE_NUS_MAX_DATA_LEN==20
    																																						//FONA reply consists of at least 3 "\n" if SUCCESS
    																																						// 1: Command repeats
    																																						// 2: Command response
    																																						// 3: OK etc. 
                {
    						
    								/*			AK47			*/
    								// Data is ready to be displayed
    								fona_cmd_t.ret_cr_cnt++;		//New CR '\n' found in the command
    								uint8_t i=0;
    								do{ 
    								//	SEGGER_RTT_printf(0, "Rx[%d] = %c\n",i,data_array[i]);
    									 cmd_ret[fona_cmd_t.ret_cr_cnt][i]=data_array[i];
    									//SEGGER_RTT_printf(0, "cmd_ret [%d] = %x\n",i,cmd_ret[fona_cmd_t.ret_cr_cnt][i]);
    									i++;
    								}while(data_array[i] != '\n');
    								
    								
    								fona_cmd_t.ret_len=i-1;
    								if(fona_cmd_t.ret_len == 0)
    								{
    									fona_cmd_t.ret_type		=	 CMD_NULL;
    									fona_cmd_t.cmd_status =  CMD_PROCESS;
    								}
    								else
    								{									
    										//If command length is Non-Zero :: Characterizing the command return types (wnhich type of return code is it)
    										char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],fona_cmd_t.cmd);		//Finding same command
    										if(ret_type!=NULL)																	//Meaning "SAME" command found in return type
    										{
    											fona_cmd_t.ret_type		=	 CMD_REPEAT;
    											fona_cmd_t.cmd_status =  CMD_PROCESS;
    										}
    										else		//Same command was not returned
    										{
    											char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"OK");						//Finding OK in response
    											if(ret_type!=NULL)																//Found OK
    											{
    												fona_cmd_t.ret_type=CMD_OK;
    												if(strstr(fona_cmd_t.cmd,"AT+HTTPACTION=1")!=NULL)		//If command is AT+HTTPACTION=1 ; in that case "OK" does not indicated the end of response
    												{
    													SEGGER_RTT_printf(0, "OK response of AT+HTTPACTION=1 does not mean EOC\n");
    													fona_cmd_t.cmd_status =  CMD_PROCESS;								
    												}
    												else
    												{
    													fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after "OK"
    													fona_cmd_t.ret_cr_cnt=0;													//Cleared
    												}
    											}
    											else		// Response was not OK
    											{
    												char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"ERROR");						//Finding ERROR in response
    												if(ret_type!=NULL)																//Found ERROR
    												{
    													fona_cmd_t.ret_type=CMD_ERROR;
    													fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after "ERROR"
    													fona_cmd_t.ret_cr_cnt=0;												//Cleared
    												}
    												else	//ERROR was not found in response
    												{
    													char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"0.0.0.0");						//Finding GPRS network release
    													if(ret_type!=NULL)																//Found ERROR
    													{
    														fona_cmd_t.ret_type=CMD_GPRS_INVALID_IP;
    														fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after "CMD_GPRS_DEACT"
    														fona_cmd_t.ret_cr_cnt=0;												//Cleared
    													}
    													else		//NULL ip was not found in response
    													{
    														char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"DOWNLOAD");						//Finding ERROR in response
    														if(ret_type!=NULL)																//Found ERROR
    														{
    															fona_cmd_t.ret_type   =  CMD_DOWNLOAD;
    															fona_cmd_t.cmd_status =  CMD_PROCESS;								//User needs to process (input data) further
    														}
    														else
    														{
    															char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"+HTTPACTION"); // Denotes that HTTP POST executed OK :: CMD_HTTP_POST_OK
    															if(ret_type!=NULL)																//Found respose from server on +HTTPACTION:1
    															{
    																SEGGER_RTT_printf(0, "Response from server = %s \n",cmd_ret[fona_cmd_t.ret_cr_cnt]);
    																char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"200");	//200 status code=SUCCESS
    																if(ret_type!=NULL)																							//Found 200 in the response from server
    																{
    																	SEGGER_RTT_printf(0, "\n*** Data successfully uploaded to server ***\n");
    																	fona_cmd_t.cmd_repeat = 0;			//Don't repeat cmd
    																	fona_cmd_t.ret_type   =  CMD_HTTP_POST_OK;
    																	fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after this
    																	fona_cmd_t.ret_cr_cnt=0;												//Cleared
    																}
    																else
    																{
    																	SEGGER_RTT_printf(0, "\n*** Some error in uploading data to server. Retry ***\n");
    																	fona_cmd_t.cmd_repeat = 1;			//cmd_repeat flag raised
    																	fona_cmd_t.ret_type   =  CMD_HTTP_POST_REPEAT;
    																	fona_cmd_t.cmd_status =  CMD_END;								//Ending the command with repeat type[CMD_HTTP_POST_REPEAT]: That will force to re-execute the command
    																	fona_cmd_t.ret_cr_cnt=0;												//Cleared
    																}
    																//http_read = cmd_ret[fona_cmd_t.ret_cr_cnt];
    															}
    															else
    															{
    																char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"+HTTPREAD");	//Message from the server
    																if(ret_type!=NULL)																//Found respose from server on +HTTPACTION:1
    																{
    																	SEGGER_RTT_printf(0, "Message from server = %s \n",cmd_ret[fona_cmd_t.ret_cr_cnt]);
    																	fona_cmd_t.ret_type   =  CMD_HTTP_POST_READ;
    																	fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after this
    																	fona_cmd_t.ret_cr_cnt=0;												//Cleared
    																}
    																else
    																{
    																	fona_cmd_t.ret_type		=  CMD_RESPONSE;
    																	fona_cmd_t.cmd_status =  CMD_PROCESS;						//Command return string : more to expect
    																}
    															}
    														}
    													}
    												}
    											}
    										}
    									}	
    								/*			AK47			*/
                    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;
        }
    }

    BTW, the same piece of code (UART event handler) is working fine (with the same hardware of FONA808) when I don't combine it with central example. Any hints why nRF UART event handler is not being executed?

Reply
  • UPDATE: I have just checked on the oscilloscope that both Tx and Rx pins of nRF52 are getting data on them.

    Meaning UART is working fine while transmitting the data at least (UART of nRF sends data and gets the response back from FONA808).

    However, due to some reason the UART in nRF is not detecting the received response, because of which the application keeps on waiting for the response.

    Here is my code for the UART event handler which is supposed to handle the received packets from FONA808 module.

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        static uint8_t data_array[FONA_CMD_RET_MAX_DATA_LEN];
        static uint8_t index = 0;
        uint32_t       err_code;
    		static char* http_read;
    
        switch (p_event->evt_type)
        {
    				//Test
    				SEGGER_RTT_printf(0, "\nReceived some response from UART\n");
            case APP_UART_DATA_READY:			//Gets the event at each byte receive
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
    						
                if (data_array[index - 1] == '\n') 															//AK removed the the upper limit on BLE_NUS_MAX_DATA_LEN==20
    																																						//FONA reply consists of at least 3 "\n" if SUCCESS
    																																						// 1: Command repeats
    																																						// 2: Command response
    																																						// 3: OK etc. 
                {
    						
    								/*			AK47			*/
    								// Data is ready to be displayed
    								fona_cmd_t.ret_cr_cnt++;		//New CR '\n' found in the command
    								uint8_t i=0;
    								do{ 
    								//	SEGGER_RTT_printf(0, "Rx[%d] = %c\n",i,data_array[i]);
    									 cmd_ret[fona_cmd_t.ret_cr_cnt][i]=data_array[i];
    									//SEGGER_RTT_printf(0, "cmd_ret [%d] = %x\n",i,cmd_ret[fona_cmd_t.ret_cr_cnt][i]);
    									i++;
    								}while(data_array[i] != '\n');
    								
    								
    								fona_cmd_t.ret_len=i-1;
    								if(fona_cmd_t.ret_len == 0)
    								{
    									fona_cmd_t.ret_type		=	 CMD_NULL;
    									fona_cmd_t.cmd_status =  CMD_PROCESS;
    								}
    								else
    								{									
    										//If command length is Non-Zero :: Characterizing the command return types (wnhich type of return code is it)
    										char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],fona_cmd_t.cmd);		//Finding same command
    										if(ret_type!=NULL)																	//Meaning "SAME" command found in return type
    										{
    											fona_cmd_t.ret_type		=	 CMD_REPEAT;
    											fona_cmd_t.cmd_status =  CMD_PROCESS;
    										}
    										else		//Same command was not returned
    										{
    											char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"OK");						//Finding OK in response
    											if(ret_type!=NULL)																//Found OK
    											{
    												fona_cmd_t.ret_type=CMD_OK;
    												if(strstr(fona_cmd_t.cmd,"AT+HTTPACTION=1")!=NULL)		//If command is AT+HTTPACTION=1 ; in that case "OK" does not indicated the end of response
    												{
    													SEGGER_RTT_printf(0, "OK response of AT+HTTPACTION=1 does not mean EOC\n");
    													fona_cmd_t.cmd_status =  CMD_PROCESS;								
    												}
    												else
    												{
    													fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after "OK"
    													fona_cmd_t.ret_cr_cnt=0;													//Cleared
    												}
    											}
    											else		// Response was not OK
    											{
    												char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"ERROR");						//Finding ERROR in response
    												if(ret_type!=NULL)																//Found ERROR
    												{
    													fona_cmd_t.ret_type=CMD_ERROR;
    													fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after "ERROR"
    													fona_cmd_t.ret_cr_cnt=0;												//Cleared
    												}
    												else	//ERROR was not found in response
    												{
    													char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"0.0.0.0");						//Finding GPRS network release
    													if(ret_type!=NULL)																//Found ERROR
    													{
    														fona_cmd_t.ret_type=CMD_GPRS_INVALID_IP;
    														fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after "CMD_GPRS_DEACT"
    														fona_cmd_t.ret_cr_cnt=0;												//Cleared
    													}
    													else		//NULL ip was not found in response
    													{
    														char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"DOWNLOAD");						//Finding ERROR in response
    														if(ret_type!=NULL)																//Found ERROR
    														{
    															fona_cmd_t.ret_type   =  CMD_DOWNLOAD;
    															fona_cmd_t.cmd_status =  CMD_PROCESS;								//User needs to process (input data) further
    														}
    														else
    														{
    															char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"+HTTPACTION"); // Denotes that HTTP POST executed OK :: CMD_HTTP_POST_OK
    															if(ret_type!=NULL)																//Found respose from server on +HTTPACTION:1
    															{
    																SEGGER_RTT_printf(0, "Response from server = %s \n",cmd_ret[fona_cmd_t.ret_cr_cnt]);
    																char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"200");	//200 status code=SUCCESS
    																if(ret_type!=NULL)																							//Found 200 in the response from server
    																{
    																	SEGGER_RTT_printf(0, "\n*** Data successfully uploaded to server ***\n");
    																	fona_cmd_t.cmd_repeat = 0;			//Don't repeat cmd
    																	fona_cmd_t.ret_type   =  CMD_HTTP_POST_OK;
    																	fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after this
    																	fona_cmd_t.ret_cr_cnt=0;												//Cleared
    																}
    																else
    																{
    																	SEGGER_RTT_printf(0, "\n*** Some error in uploading data to server. Retry ***\n");
    																	fona_cmd_t.cmd_repeat = 1;			//cmd_repeat flag raised
    																	fona_cmd_t.ret_type   =  CMD_HTTP_POST_REPEAT;
    																	fona_cmd_t.cmd_status =  CMD_END;								//Ending the command with repeat type[CMD_HTTP_POST_REPEAT]: That will force to re-execute the command
    																	fona_cmd_t.ret_cr_cnt=0;												//Cleared
    																}
    																//http_read = cmd_ret[fona_cmd_t.ret_cr_cnt];
    															}
    															else
    															{
    																char* ret_type = strstr(cmd_ret[fona_cmd_t.ret_cr_cnt],"+HTTPREAD");	//Message from the server
    																if(ret_type!=NULL)																//Found respose from server on +HTTPACTION:1
    																{
    																	SEGGER_RTT_printf(0, "Message from server = %s \n",cmd_ret[fona_cmd_t.ret_cr_cnt]);
    																	fona_cmd_t.ret_type   =  CMD_HTTP_POST_READ;
    																	fona_cmd_t.cmd_status =  CMD_END;								//Nothing to expect after this
    																	fona_cmd_t.ret_cr_cnt=0;												//Cleared
    																}
    																else
    																{
    																	fona_cmd_t.ret_type		=  CMD_RESPONSE;
    																	fona_cmd_t.cmd_status =  CMD_PROCESS;						//Command return string : more to expect
    																}
    															}
    														}
    													}
    												}
    											}
    										}
    									}	
    								/*			AK47			*/
                    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;
        }
    }

    BTW, the same piece of code (UART event handler) is working fine (with the same hardware of FONA808) when I don't combine it with central example. Any hints why nRF UART event handler is not being executed?

Children
No Data
Related