How could I change the baud rate of my STM32 UART by receiving input from another microcontroller? - stm32

I'm currently trying to change the baudrate at runtime for my STM32 based on whatever input my ESP32 sends it over UART. I'm. not really sure how to go about it or if its even possible to do so.
Basically I'm sending a uint32 over UART to the STM32 and hoping it is receiving it as the number I'm sending then aborting and deinitializing the UART, reinitializing it with the new baud rate and then initializing the UART again.
However, this does not seem to be having any effect. I can't tell if what I'm sending it just not making it to the STM32 as the value I intended or if it's receiving the value and just not doing anything. Any help or advice would be greatly appreciated.
uint8_t Rx_data[100] = "<Hello World\n>";
int baud = 115200;
uint8_t Tx_data[100];
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();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
// Transmit data via UART
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Blink
// HAL_Delay(1000); // Delay blink for 1 second
HAL_UART_Receive(&huart1, (uint8_t*)Tx_data, 100, 100); //
Receive comms from ESP32
uint8_t Tx_value = Tx_data;
if(baud != Tx_value) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Blink
HAL_Delay(1000); // Delay blink for 1 second
baud = Tx_value;
Change_baud(baud);
}
HAL_UART_Transmit(&huart2,(uint8_t*) Tx_data, 100, 100); // Transmit comms from ESP32 to terminal
HAL_UART_Transmit(&huart1, Rx_data, sizeof(Rx_data), 100);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//HAL_UART_Receive(&huart2, Rx_data, 4, 1000);
}
/* USER CODE END 3 */
}
void Change_baud(int baud) {
HAL_UART_DeInit(&huart2);
MX_USART2_UART_Init_baud(baud);
HAL_UART_Receive_IT(&huart2, (uint8_t*)Rx_data, 100);
}
static void MX_USART2_UART_Init_baud(int baud)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = baud;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
I realize this is not the best written code, I'm VERY new to STM32 and how to program it.

Related

Receive Uart Problem with IDLE state STM32

I am using Uart1 to serial write function. And I want to use this uart1 interface for the receive unknown length data.
If I do not use the serial write function, I get data correctly (with check LINE idle and count DMA buffer size)
But If I use the serial write function line couldnt be idle on correct time, If I want to print getting data with serial write uartIdleStateActive2 flag is always ENABLE
How can I use this if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET)) case, only receive proccess.
I am using like theese if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET) && (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET)) but it didnt work. How can I check it to look at the idle state at receive time?
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
/* UART IDLE Interrupt */
// SerialPrint("a");
if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET))
{
HAL_UART_DMAStop(&huart1);
DMA_CounterCounted2 = __HAL_DMA_GET_COUNTER(huart1.hdmarx);
if(((DMA_CounterCounted2) <= DMA_BUF_SIZE) && (DMA_CounterCounted2 != 0))
{
uartIdleStateActive2 = (_Bool)ENABLE;
// SerialPrint("X");
}
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
}
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART2_IRQn 1 */
/* USER CODE END USART2_IRQn 1 */
}
UART 1 configurations:
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 = 230400U;
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 */
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
// __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
/* USER CODE END USART1_Init 2 */
}

Measure analog value using Internal reference voltage on STM32L422

I have to measure adc value using internal reference voltage in STM32L422. According to datasheet STM32L422 internal reference voltage is 1.2 V. I enabled internal Vref from CubeMX.
Analog input is 1V but I get 0.58 V. what is the problem here anyone ?
STM32L422 datasheet says that internal Vref = 1.2 V. When I measured Vref pin, it was 0.53 V.
What is wrong over here ?
For 1 V input which is obtained using 20K, 10K voltage divider, for 3V, digital and analog values obtained are are follows:
Digital: 1983, Analog value: 0.581099
#define VREFINT_ADDR 0x1FFF75AA // VREF voltage
#define VREFINT (*((uint16_t *) VREFINT_ADDR))
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_ADC1_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_TIM6_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
printf("System Booting On! \n");
printf("VREFINT: %ud\n", VREFINT);
printf("ADC Calibration! \n");
while(HAL_ADCEx_Calibration_Start(&hadc1,0) != HAL_OK);
printf("Battery Voltage Check ...\n");
HAL_Delay(100); // 1000ms OK => 100ms OK
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,100);
ADC_Val=HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
float Result = (float)((1.200 * ADC_Val) / 4095);
printf("****************************************************************\n");
printf("Digital: %d, Analog value : %f\r\n", ADC_Val, Result);
printf("****************************************************************\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
;
}
/* USER CODE END 3 */
}
void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV8;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure the ADC multi-mode
*/
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_12;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_92CYCLES_5;
// sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
STM32 ADC works a bit different way than you think and the Vrefint name is a bit confusing. Always read the Reference Manual.
Vrefint is not the reference voltage for ADC. It is simply connected one of the channels. When you measure this voltage you can know your Vref for ADC (in your case it is Vdda) So the formula you use is wrong.
STM32L422 datasheet says that internal Vref = 1.2 V
Do not read the datasheet only Reference Manual
Then you need to measure your Vref (Vdda) and rthen you can measure your voltage on the pin.

Not Getting sim800 response correctly

I am struggling to get correct response (OK) from sim800L module that is connected with stm32f103. But through serial terminal (using USB to serial module) everything(sms, call,gprs) is working fine.
I am getting series of AAAAAAA........ . I tried it with echo off but same response
Reduced the timeout too.
here is my piece of code
#include "main.h"
#include "stdio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
unsigned char E[]={10},G[]={34},S[]={26},C[]={13};
#define SUB HAL_UART_Transmit(&huart1,(unsigned char*)S,1,1000);
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
unsigned char tx_buffer[50],rx_buffer[50]={'\0'};
uint8_t x=26;
char send_str[33]={'\0'};
char Num_str[11]={"xxxxxxxxxx"};
char sending_sms_str[65] = {'\0'};
unsigned char msg_str[50]="good morning shubham";
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART3_UART_Init(void);
/* USER CODE BEGIN PFP */
void sim800_init(void);
void sim800_call(void);
void sim800_sms(void);
void sim800_gprs(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/*
void sim800_response(void)
{
char response[200];
for(int i = 0 ; i<200 ; i++)
{
response[i++] = HAL_UART_Receive(&huart1,(unsigned char *)rx_buffer,1,100);
}
HAL_Delay(200);
HAL_UART_Transmit(&huart3,(unsigned char *)response,strlen(response),100);
}
*/
void sim800_test(void)
{
HAL_UART_Transmit(&huart3,(uint8_t *)"AT\r",strlen("AT\r"),100);
HAL_Delay(300);
//HAL_UART_Transmit(&huart1,(uint8_t *) "AT+IPR=9600\\n" , strlen ("AT+IPR=9600\r\n"),100);
//HAL_Delay(200);
HAL_UART_Receive(&huart3,(uint8_t *)rx_buffer,sizeof(rx_buffer),100);
HAL_Delay(200);
//sim800_response();
HAL_UART_Transmit(&huart1,(uint8_t *)rx_buffer,sizeof(rx_buffer),100);// to Uart1 for debugging the response
HAL_Delay(200);
rx_buffer[0] = '\0';
}
void sim800_init(void)
{
HAL_Delay(10000);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT\r\n",strlen("AT\r\n"),1000);
HAL_Delay(500);
//-----------------------------
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+IPR=9600\r\n",strlen("AT+IPR=9600\r\n"),1000);
HAL_Delay(500);
}
void sim800_call(void) // working
{
char rxbuf[50];
HAL_UART_Transmit(&huart3,(uint8_t *)"ATD xxxxxxxxxx;\r\n",strlen("ATD xxxxxxxxxx;\r\n"),1000);
HAL_Delay(100);
HAL_UART_Receive(&huart3,(uint8_t *)rxbuf,strlen(rxbuf),300);
HAL_Delay(100);
HAL_UART_Transmit(&huart1,(uint8_t *)rxbuf,strlen(rxbuf),300);//to Uart1 for debugging the response
rxbuf[0]='\0';
HAL_Delay(10000);
HAL_UART_Transmit(&huart3,(uint8_t *)"ATH\r\n",strlen("ATH\r\n"),1000);
HAL_Delay(100);
HAL_UART_Receive(&huart3,(uint8_t *)rxbuf,strlen(rxbuf),300);
HAL_Delay(100);
HAL_UART_Transmit(&huart1,(uint8_t *)rxbuf,strlen(rxbuf),300);//to Uart1 for debugging the response
rxbuf[0]='\0';
}
void sim800_sms(void) // function 1 working
{
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CMGF=1\r\n",strlen("AT+CMGF=1\r\n"),1000);
//HAL_Delay(500);
//HAL_UART_Transmit(&huart1,(uint8_t *)"AT+CMGS=\"+91xxxxxxxxxx\"\r\n",strlen("AT+CMGS=\"+91xxxxxxxxxx\"\r\n"),1000);
//HAL_Delay(500);
//HAL_UART_Transmit(&huart1,(unsigned char *)"it is working",strlen("it is working"),1000);
//HAL_Delay(500);
//HAL_UART_Transmit(&huart1,(uint8_t *)&x,1,10);
HAL_Delay(500);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CMGS=\"+91xxxxxxxxxxx\"\r\n",strlen("AT+CMGS=\"+91xxxxxxxxxx\"\r\n"),1000);
HAL_Delay(500);
HAL_UART_Transmit(&huart3,(unsigned char *)msg_str,strlen((const char *)msg_str),1000);
HAL_Delay(500);
HAL_UART_Transmit(&huart3,(uint8_t *)&x,1,10);
HAL_Delay(5000);
}
void send_sms(void) // function 2 working
{
LED_GPIO_Port -> BRR = LED_Pin;
send_str[0] = '\0';
sprintf(send_str,"AT+CMGF=1\r\n");
HAL_UART_Transmit(&huart3,(unsigned char *)send_str,strlen(send_str),1000);
HAL_Delay(1000);
send_str[0] = '\0';
sprintf(send_str,"AT+CMGS=\"+91%s\"\r\n",Num_str);
HAL_UART_Transmit(&huart3,(unsigned char *)send_str,strlen(send_str),1000);
HAL_Delay(1000);
sprintf(sending_sms_str,"HELLO\r\n");
HAL_UART_Transmit(&huart3,(unsigned char *)sending_sms_str,strlen(sending_sms_str),1000);
//HAL_Delay(10);
//HAL_UART_Transmit(&huart1,(unsigned char*)S,1,1000); // or SUB
SUB
HAL_Delay(1000);
LED_GPIO_Port -> BSRR = LED_Pin;
}
void sim800_gprs(void)
{
HAL_UART_Transmit(&huart3,(uint8_t *)"AT\r\n",strlen("AT\r\n"),1000);
HAL_Delay(300);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CIPSHUT\r\n",strlen("AT+CIPSHUT\r\n"),1000);
HAL_Delay(300);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CIPMUX=0\r\n",strlen("AT+CIPMUX=0\r\n"),1000);
HAL_Delay(300);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CGATT=1\r\n",strlen("AT+CGATT=1\r\n"),1000);
HAL_Delay(300);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CSTT=\"www\",\"\",\"\"\r\n",strlen("AT+CSTT=\"www\",\"\",\"\"\r\n"),1000);
HAL_Delay(2000);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CIICR\r\n",strlen("AT+CIICR\r\n"),5000);
HAL_Delay(2000);
HAL_UART_Transmit(&huart3,(uint8_t *)"AT+CIFSR\r\n",strlen("AT+CIFSR\r\n"),1000);
HAL_Delay(1000);
HAL_UART_Transmit(&huart1,(uint8_t *)"All cmd sent\r\n",strlen("All cmd sent\r\n"),1000);
}
//---------------------------------------------------------------------//
/* 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();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
sim800_init();
sim800_gprs();
//sim800_call();
//sim800_test();
//sim800_init();
//sim800_call(); // working but reliability low
//send_sms(); // working but reliability very low
// sim800_sms(); // working
//HAL_Delay(25000);
/* 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_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_2) != 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 USART3 Initialization Function
* #param None
* #retval None
*/
static void MX_USART3_UART_Init(void)
{
/* USER CODE BEGIN USART3_Init 0 */
/* USER CODE END USART3_Init 0 */
/* USER CODE BEGIN USART3_Init 1 */
/* USER CODE END USART3_Init 1 */
huart3.Instance = USART3;
huart3.Init.BaudRate = 9600;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
/**
* #brief GPIO Initialization Function
* #param None
* #retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
}
/* 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
I would suggest the following to debug the problems you're facing:
You need to make sure that your UART code is working correctly, to
do that you can connect your STM32 to a serial terminal on your PC
via an FTDI cable and check if the messages sent from the STM32 are
received on the serial terminal.
Once step (1) is resolved, you need to check that you are powering
your SIM800 module correctly (ie: correct power pin and enough
current).
Once step (2) is resolved try to connect the SIM800 to your PC via
an FTDI cable and send & receive AT commands and responses via a serial monitor.
Back to the code here are some simple examples on how to send an AT command and wait for its known response:
With Echo:
/* Wait at most 1000 ms for the command to be sent completely */
HAL_UART_Transmit(&huart3,(uint8_t *)"AT\r", (sizeof("AT\r")-1), 1000);
/* Wait 5000 ms for response with echo */
HAL_UART_Receive(&huart3, (uint8_t *)rx_buffer, (sizeof("AT\r\r\nOK\r\n")-1), 5000);
Without echo:
/* Wait at most 1000 ms for the command to be sent completely */
HAL_UART_Transmit(&huart3,(uint8_t *)"AT\r", (sizeof("AT\r")-1), 1000);
/* Wait 50000 ms for response without echo */
HAL_UART_Receive(&huart3, (uint8_t *)rx_buffer, (sizeof("\r\nOK\r\n")-1), 5000);
OR you can do it asynchronously via the UART receive callback:
With Echo:
/* Wait at most 1000 ms for the command to be sent completely */
HAL_UART_Transmit(&huart3,(uint8_t *)"AT\r", (sizeof("AT\r")-1), 1000);
/* This will raise a callback when the response length is reached */
HAL_UART_Receive_IT(&huart3, (uint8_t *)rx_buffer, (sizeof("AT\r\r\nOK\r\n")-1));
Without echo:
/* Wait at most 1000 ms for the command to be sent completely */
HAL_UART_Transmit(&huart3,(uint8_t *)"AT\r", (sizeof("AT\r")-1), 1000);
/* This will raise a callback when the response length is reached */
HAL_UART_Receive_IT(&huart3, (uint8_t *)rx_buffer, (sizeof("\r\nOK\r\n")-1));
Callback:
HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART3)
{
/* if you are here then you received a response */
}
}

HAL_CAN_ERROR_PARAM with STM32f103c8t6 Bluepill and can bus

I'm struggling to send messages via can bus with stm32f103 bluepill board and SN65HVD230 transceiver(terminated).
On the other end I use terminated usb can adapter, that works fine. CAN Bus speed is 500kbit/s.
Connections of SN65HVD230 :
PA11 -> RX
PA12 -> TX
GND -> GND
3.3V -> 3.3V
Configuration in CubeMX
RCC -> High Speed Clock = Crystal/Ceramic resonator
CAN -> Master mode
Prescaler = 4
Time Quanta in Bit Segment 1 = 13
Time Quanta in Bit Segment 2 = 2
Resyncronization jump width = 1
SYSCLK = 32 MHz
HCLK = 32 MHz
APB2 = 32 Mhz
APB1 = 32 Mhz
FCLK = 32 MHZ
Firmware version 1.8.0. Here is main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* #file : main.c
* #brief : Main program body
******************************************************************************
* #attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "can.h"
#include "i2c.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailbox;
uint8_t TxData[8];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void blink(int count, int duration);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 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_CAN_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
blink(3, 100);
HAL_Delay(1000);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
TxHeader.StdId = 0x321;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.DLC = 4;
TxHeader.TransmitGlobalTime = DISABLE;
TxData[0] = 0;
TxData[1] = 1;
TxData[2] = 2;
TxData[3] = 3;
if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox) == HAL_OK) {
blink(1, 500);
} else {
blink(2, 100);
}
HAL_Delay(1000);
}
/* 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 CPU, AHB and APB busses clocks
*/
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_MUL8;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses 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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void blink(int count, int duration) {
for (int i = 0; i < count; i++) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_Delay(duration / 2);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
HAL_Delay(duration / 2);
}
}
/* 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 */
/* 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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
After 3 times of calling HAL_CAN_AddTxMessage 3 times (3 mailboxes?) in debugger is shows 0x200 error in hcan.ErrorCode, that is defined as HAL_CAN_ERROR_PARAM. It looks like all mailboxes are full and there are no messages sent into bus.
Any ideas how to debug this problem and get it to work?
I heard SN65HVD230 doesnt work with bluepill
The problemm seems to be the CAN TX buffer busy state, you can implement a time offset between can TX messages in order to let the driver to complete the transmission.

trying to get data from MAX144 external ADC using STM32L452RE micro controller but no success :(

I m quite new with using ADC. I am using an external ADC MAX144 to get sensor data using STM32L452RE. I am using both channels of this ADC for two sensors. ADC is continuously sending data and I want to read it.
datasheet of ADC is here
what I am doing is:
created a 2 byte buffer
i am using DMA for both spi and uart
receiving data in buffer with HAL_SPI_Receive_DMA()
transmitting data over usart using HAL_UART_Transmit_DMA()
code that i have made is given below:
#include "main.h"
#include "stm32l4xx_hal.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi3;
DMA_HandleTypeDef hdma_spi3_rx;
TIM_HandleTypeDef htim1;
UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart3_tx;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------
------*/
static uint8_t readBuffer[2];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_SPI3_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi){
while(1);
}
void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma){
while(1);
}
`enter code here`void delay(uint16_t delay)
{
__HAL_TIM_SET_COUNTER(&htim1,0);
while(__HAL_TIM_GET_COUNTER(&htim1) < delay);
}
/* USER CODE END 0 */
/**
* #brief The application entry point.
*
* #retval None
*/
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_DMA_Init();
MX_USART3_UART_Init();
MX_SPI3_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, SET);
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
HAL_Delay(80);
HAL_SPI_Receive_DMA(&hspi3, readBuffer, 2);
HAL_UART_Transmit_DMA(&huart3, readBuffer, 2);
}
/* USER CODE END 3 */
}
I am stuck in this problem for the last two weeks. any guides in this regards will be highly appreciated.
regards.
These Callbacks doesn't work
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi){
while(1);
}
void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma){
while(1);
}
The Callbacks are called directly from the Interrupt routines. If you put a while(1) in there the interrupt will never finish.
Make a first try without the DMA functions.
HAL_SPI_Receive(&hspi3, readBuffer, 2);
HAL_UART_Transmit(&huart3, readBuffer, 2);
And see if your SPI ready ind USART writing works. As a second step you could try to setup the DMA transfer.