Search code examples
stm32uartat-commandkeil

STM32 Keil - Can not access target while debugging (AT Command UART)


I am trying to communicate with GSM module via UART communication. I could get message from the module as I expected. However when it comes to while loop (it is empty), debug session ends with "can not access target" error. Stepo by step, I am going to share my code:

Function 1 is AT_Send. (Note: Some of variables are declared globally.)

int AT_Send(UART_HandleTypeDef *huart, ATHandleTypedef *hat, unsigned char *sendBuffer, uint8_t ssize, unsigned char *responseBuffer, uint8_t rsize) {

    if (HAL_UART_Transmit_IT(huart,sendBuffer,ssize) != HAL_OK) {
        return -1;
    }

    while ((HAL_UART_GetState(huart) & HAL_UART_STATE_BUSY_TX) == HAL_UART_STATE_BUSY_TX) {
        continue;
    }

    //;HAL_Delay(1000);
    if (strstr((char*)receiveBuffer,(char*)responseBuffer) != NULL) {
     rxIndex = 0;
     memset(command, 0, sizeof(command));
     return 0;
   }
   rxIndex = 0;
   memset(command, 0, sizeof(command));

    return 1;
}

Second function is AT_Init function. It sends AT to get OK response. At this point on, if I am not wrong, I am opening receive interrrupt and I am trying to get 1 byte.

int AT_Init(UART_HandleTypeDef *huart, ATHandleTypedef *hat)
{
    HAL_UART_Receive_IT(huart,&rData,1);
    tx = AT_Send(huart,hat,"AT\r",sizeof("AT\r\n"),"OK\r\n",sizeof("OK\r\n"));

    return tx;
}

After these two functions, I am calling receive IT function in the call back while there are data on the bus.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1){
        command[rxIndex] = rData;
        rxIndex++;

        if((rxIndex == 2) && (strstr((char*)command,"\r\n") != NULL)) {
            rxIndex = 0;
        } else if (strstr((char*)command,"\r\n") != NULL) {
            memcpy(receiveBuffer, command, sizeof(command));
            rxIndex = 0;
            memset(command,0,sizeof(command));
        }
        HAL_UART_Receive_IT(&huart1,&rData,1);
    }

}

Moreover, I am going to send a few HTTP commands simultaneously if I can get rid of this problem. Can anyone share his/her knowledge?

Edit: Main function is shown below

  tx = AT_Init(&huart1,&hat);
  while (1)
  {
    HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_3);
    HAL_Delay(500);
  }

Edit 2: I had replaced uart channel by USART2, and debugger worked. I suppose that it is related to hardware. Still, I am curious about possible reasons that cause to this problem.


Solution

  • The question doesn't mention on which µC the program is running, I only see the "stm32" tag. Similarly, we don't know which debug protocol is used (JTAG or SWD?).

    Still, I dare to guess that the toggle command for GPIO port PB3 in the main loop is causing the observations: On many (most? all?) STM32 controllers, PB3 is used as JTDO pin, which is needed for JTAG debug connections.

    Please make sure to configure the debug connection to SWD (without SWO, i.e., neither SWV is correct). It may also help to check the wiring of the debug cable, the fast toggle at the PB3/JTDO line may influence the signal levels on some neighbouring SWD lines if the wiring is low quality or a fast SWD speed has been chosen.

    My hypothesis can be falsified by removing all actions to PB3. If the problem remains, I'm wrong.