2024-02-15 08:12 AM - edited 2024-02-15 08:21 AM
Hi Community,
I am trying to integrate SIM800L module with STM32F103C6T6 using UART (POLL method), code given below, but all I am getting a response is a single char 'A' or sometimes a random value as shown in screenshots.
I have also tried setting up the Baud rate at 9600. But no luck, everytime I am getting the HAL_TIMEOUT error message with buffer sometimes a single digit character 'A' or some values from command itself populated in the response buffer.
Also, I have tried with Putty and it works fine, I get the complete response for a given AT command as expected from manual
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t rx_data[100] = { 0 };
uint8_t test_buffer=90;
uint16_t timeout_tx_rx = 2000;
char AT_OK_RESPONSE[2] = "OK";
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
static inline uint8_t SendAtCmd(const char *cmd, const char *ack) {
if (HAL_OK
== HAL_UART_Transmit(&huart1, (uint8_t*) cmd, strlen(cmd),
timeout_tx_rx)) {
HAL_StatusTypeDef rx_response = HAL_UART_Receive(&huart1, rx_data,
strlen(ack), timeout_tx_rx);
if (strstr(rx_data, ack) != NULL) {
return 1;
}
}
return 0;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
// SendAtCmd("AT+IPR=9600\r\n", AT_OK_RESPONSE);
// SendAtCmd("AT&W\r\n",AT_OK_RESPONSE);
SendAtCmd("AT\r\n", AT_OK_RESPONSE);
SendAtCmd("AT+CSQ\r\n", "+CSQ");
SendAtCmd("AT+CREG?\r\n", "+CREG");
// GPRS init
SendAtCmd("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n", AT_OK_RESPONSE);
SendAtCmd("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"\r\n", AT_OK_RESPONSE);
SendAtCmd("AT+SAPBR=1,1\r\n", AT_OK_RESPONSE);
// get sim details
SendAtCmd("AT+CIPGSMLOC=1,1\r\n", "+CIPGSMLOC"); /// geo location
// put the geo location code here , variables - latitude & longitude
SendAtCmd("AT+GSN\r\n", AT_OK_RESPONSE); // IMEI D_ID
SendAtCmd("AT+CCID\r\n", AT_OK_RESPONSE); //CCID S_ID
SendAtCmd("AT+SAPBR=0,1\r\n", AT_OK_RESPONSE);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @PAram None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @PAram None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @PAram file: pointer to the source file name
* @PAram line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
2024-02-15 09:15 AM
The module may echo back what you've sent, as you send the characters; and if you don't read out from UART the characters sent back during transmission, the receiver overflows and stops receiving further characters. So you really may need a duplex (RxTx) process in place. Best, don't use Cube/HAL functions, write your own.
JW
2024-02-16 02:30 AM
@waclawek.jan wrote:The module may echo back what you've sent
Indeed.
@veeshaljha - should be easy to check with PuTTY ...
2024-02-16 02:45 AM - edited 2024-02-16 02:46 AM
@veeshaljha wrote:POLL method
Maybe you are not polling fast enough?
This seems flawed:
HAL_StatusTypeDef rx_response = HAL_UART_Receive(&huart1, rx_data, strlen(ack), timeout_tx_rx);
How do you know exactly what the length of the response will be before it's arrived?
Are you sure you've correctly accounted for all formatting characters - CR, LF, etc ?
Your code only looks at the expected responses:
SendAtCmd("AT\r\n", AT_OK_RESPONSE); SendAtCmd("AT+CSQ\r\n", "+CSQ"); SendAtCmd("AT+CREG?\r\n", "+CREG"); // GPRS init SendAtCmd("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n", AT_OK_RESPONSE); SendAtCmd("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"\r\n", AT_OK_RESPONSE); SendAtCmd("AT+SAPBR=1,1\r\n", AT_OK_RESPONSE);
You have nothing to deal with any unexpected responses!
Mobile networks are inherently unreliable - you need to be able to cope with failures & unexpected responses!
Also this would require that all unsolicited responses are disabled.
Also:
SendAtCmd("AT\r\n", AT_OK_RESPONSE); SendAtCmd("AT+CSQ\r\n", "+CSQ"); SendAtCmd("AT+CREG?\r\n", "+CREG");
Note that the correct terminator for AT commands is just CR - no LF is needed.
The extra spurious LF shouldn't hurt - but may affect what gets echoed back ...
@veeshaljha wrote:location using SIM800L GSM MODULE
I don't see where "location" comes into this? :thinking_face:
2024-02-16 06:45 AM - edited 2024-02-16 06:51 AM
> I don't see where "location" comes into this?
That module probably is a GSM+GPS combo. It may also provide some location based on GSM triangulation, I'm not sure about that.
Not relevant to the problem, indeed; just explaining the OP's motivation.
Root of the problem is, that the UART functions in Cube/HAL are quite unsuitable for the vast majority of practical UART applications, and there are no useful examples/guidelines.
Key would be to receive characters into ring buffer in interrupt and process them in main; transmission does not really matter but interrupt is OK for it, too.
JW
2024-02-16 10:20 AM
@waclawek.jan wrote:Not relevant to the problem
Indeed.
@waclawek.jan wrote:Key would be to receive characters into ring buffer in interrupt and process them in main; transmission does not really matter but interrupt is OK for it, too.
Absolutely!