cancel
Showing results for 
Search instead for 
Did you mean: 

UART transmission Issue in stm32g4

sireevenkat1
Senior

Hi,
I am working with STM32G4.I am communicating with STM32G4 with other controller through  UART communication.
I can able to receive the data from other MCU to STM32G4 all the time but sometimes I can't able to transmit data from stm32g4 to other MCU.I used UART PollForConversion method  for development.
I receive the data from other MCU to STM32g4 for every 1sec.
Below is the STM32G4 side code:

 

uint8_t ocpp_txdata[64];
uint8_t ocpp_rxdata[8];
while(){
  HAL_UART_Receive(&huart5,ocpp_rxdata,7, 1000);
	uint16_t calculatedCRC;
	uint16_t receivedCRC;
      if(ocpp_rxdata[0]==0x22)
       { 	
    	  calculatedCRC = HAL_CRC_Calculate(&hcrc,(uint32_t *)&ocpp_rxdata, 4);    	 
    	  receivedCRC=ocpp_rxdata[4]<<8 | ocpp_rxdata[5];
    	  if(calculatedCRC==receivedCRC){
          switch(ocpp_rxdata[2])
          {
           case 0x01:
             //trasmit data ocpp_txdata[0] to ocpp_txdata[18] 
             //ocpp_txdata[16],ocpp_txdata[17] is crc
              HAL_UART_Transmit(&huart5, ocpp_txdata,19, 100);
                break;
             case 0x02:
             //trasmit data ocpp_txdata[0] to ocpp_txdata[13] 
             //ocpp_txdata[11],ocpp_txdata[12] is crc
              HAL_UART_Transmit(&huart5, ocpp_txdata,14, 100);
                break;
}
}
}

 

I can able to transmit the data from stm32g4 to other mcu but sometimes I can't.
What I observed is whenever I restated the system and if I remove the power in those cases initially I can't able to transmit.

Can anyone suggest what may be the issue.
I know interrupt mode for UART works better but I have to do with pollforconversion method only.

Thanks 

12 REPLIES 12

 if the interrupt is reenabled within the interrupt handler / call-back

A higher priority interrupt can cut in and here you get it. The precious CPU cycles in the handler could be better used to save or compare the received bytes.

 


@Pavel A. wrote:

whenever I restated the system and if I remove the power in those cases initially I can't able to transmit.

What means initially? Will the transmission start working by itself after some time or you have to reset the STM32 several times?


Hi @Pavel A. ,
As per my observation till now most of the times either I have to reset the STM32 or I have to debug the program again to get it worked.
Few times transmission starting by it self after some time.
Initially I observed this issue while using the breakpoints whenever breakpoint occurs, transmission from STM32 to other MCU getting stopped but reception of data from other MCU to STM32 happening.
After that now I am observing above issue.
Boot0 pin is connected to GND through 10K resistor.
NRST pin is connected to reset switch setup.
Is both pins are correct?

Thanks

sireevenkat1
Senior

Hi @Pavel A. @Tesla DeLorean ,

I checked UART communication from pollforconversion to interrupt method. but I am facing the issue.The data is transmitting and receiving between stm32g4 and other mcu.Some time I am not receiving the data properly like SOF always 0x22 and EOF 0x04 always. data bytes not coming peroperly as expected so then i can't be tramit and receive the proper data.
below added code

int main(){ 
//all initializations
HAL_UART_RegisterCallback(&huart5,HAL_UART_RX_COMPLETE_CB_ID,HAL_UART_RxCpltCallback);
  HAL_UART_RegisterCallback(&huart5,HAL_UART_TX_COMPLETE_CB_ID,HAL_UART_TxCpltCallback);
  HAL_UART_Receive_IT(&huart5, ocpp_rxdata, 7);  /* USER CODE END 2 */
while(1){
  request();
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
   if (huart->Instance == huart5.Instance)  {
         alldata_received = 1;
}
}
void request()
{
	if (alldata_received) {
        uint16_t calculatedCRC;
	uint16_t receivedCRC;
      if(ocpp_rxdata[0]==0x22)
       { 	
    	  calculatedCRC = HAL_CRC_Calculate(&hcrc,(uint32_t *)&ocpp_rxdata, 4);    	 
    	  receivedCRC=ocpp_rxdata[4]<<8 | ocpp_rxdata[5];
    	  if(calculatedCRC==receivedCRC){
          switch(ocpp_rxdata[2])
          {
           case 0x01:
             //trasmit data ocpp_txdata[0] to ocpp_txdata[18] 
             //ocpp_txdata[16],ocpp_txdata[17] is crc
              HAL_UART_Transmit_IT(&huart5, ocpp_txdata,19);
                break;
             case 0x02:
             //trasmit data ocpp_txdata[0] to ocpp_txdata[13] 
             //ocpp_txdata[11],ocpp_txdata[12] is crc
              HAL_UART_Transmit_IT(&huart5, ocpp_txdata,14);
                break;
          }
       }
    else{
     crc_error();
    }
   }
	alldata_received = 0;
	HAL_UART_Receive_IT(&huart5, ocpp_rxdata, 7);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	memset(ocpp_rxdata, '\0',7); //empty the reception data buffer

}


Sometimes data reception and tramission happening normally only but sometimes I am receiving the in the proper format so receiver data byte are changing so can't able to receive or transmit.
Please suggest .
Thanks