2021-02-14 12:42 PM
my code at below. i can connect succesfull but i cant send any command (like on,off) . what is wrong? help me pls
#include "main.h"
#include "main.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_uart.h"
#include "stm32f4xx_hal_usart.h"
#include <stdbool.h> //burda neden uart yok?bilmiyorum unutmus olabilirim yazmayi
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart6;
uint8_t deger;
char rx6_buffer[50], tx6_buffer[50], rx2_buffer[50], tx2_buffer[50];
bool led_state=false;
int mod_state=0, conf_indeks=0;
void SystemClock_Config(void);
void LED_GPIO_Init(void);
void USART6_Init(void);
void clear_buffer()
{
for(int i=0;i<50;i++)
{
rx6_buffer[i] = 0;
rx2_buffer[i] = 0;
tx6_buffer[i] = 0;
tx2_buffer[i] = 0;
}
}
void system_reset()
{
if(rx6_buffer[0] == 's' || rx2_buffer[0] == 's'){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET); // The orange led blinks before the stm board is reset
HAL_Delay(200);
HAL_NVIC_SystemReset();
}
}
int main(void)
{
HAL_Init();
USART2_Init();
USART6_Init();
SystemClock_Config();
LED_GPIO_Init();
__HAL_UART_DISABLE(&huart2);
while (1)
{
system_reset();
switch(mod_state){
case 0:
HAL_UART_Receive(&huart6, (uint8_t*)rx6_buffer, 50, 500);
if(rx6_buffer[0] == '1'){
HAL_UART_Transmit(&huart6, (uint8_t *)tx6_buffer, sprintf(tx6_buffer, "/Conf_Mod"), 500);
mod_state = 1;
}
if(rx6_buffer[0] == 'o' && rx6_buffer[1] == 'n'){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET);
if(led_state != true)
HAL_UART_Transmit(&huart6, (uint8_t *)tx6_buffer, sprintf(tx6_buffer, "-->Led is on"), 500);
led_state = true;
}
else if(rx6_buffer[0] == 'o' && rx6_buffer[1] == 'f' && rx6_buffer[2] == 'f'){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
if(led_state != false)
HAL_UART_Transmit(&huart6, (uint8_t *)tx6_buffer, sprintf(tx6_buffer, "-->Led is off"), 500);
led_state = false;
}
break;
case 1:
switch(conf_indeks){
case 0:
clear_buffer();
HAL_UART_Receive(&huart6, (uint8_t*)rx6_buffer, 50, 500);
if(rx6_buffer[0] == '0'){
HAL_UART_Transmit(&huart6, (uint8_t *)tx6_buffer, sprintf(tx6_buffer, "/Normal_Mod"), 500);
mod_state = 0;
}
if(rx6_buffer[0] == 'A' && rx6_buffer[1] == 'T'){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
HAL_UART_Transmit(&huart6, (uint8_t *)tx6_buffer, sprintf(tx6_buffer, "#"), 1000);
HAL_Delay(100);
__HAL_UART_DISABLE(&huart6);
__HAL_UART_ENABLE(&huart2);
conf_indeks = 1;
}
break;
case 1:
HAL_Delay(50);
HAL_UART_Transmit(&huart2, (uint8_t *)tx2_buffer, sprintf(tx2_buffer, "%s",rx6_buffer), 500);
HAL_Delay(50);
HAL_UART_Receive(&huart2, (uint8_t*)rx2_buffer, 50, 500);
if(rx2_buffer[0] == 'O' && rx2_buffer[1] == 'K'){
__HAL_UART_DISABLE(&huart2);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
__HAL_UART_ENABLE(&huart6);
conf_indeks = 2;
}
else if(rx2_buffer[0] == '$') {
__HAL_UART_DISABLE(&huart2);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
__HAL_UART_ENABLE(&huart6);
HAL_Delay(50);
HAL_UART_Transmit(&huart6, (uint8_t *)tx6_buffer, sprintf(tx6_buffer, " -->Error, please try again...!"), 500);
conf_indeks = 0;
}
break;
case 2:
HAL_Delay(50);
HAL_UART_Receive(&huart6, (uint8_t*)rx6_buffer, 50, 500);
if(rx6_buffer[0] == '$'){
HAL_Delay(50);
HAL_UART_Transmit(&huart6, (uint8_t *)tx6_buffer, sprintf(tx6_buffer, "%s",rx2_buffer), 500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
conf_indeks = 0;
}
break;
}
break;
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
UART_HandleTypeDef UART_InitStruct;
void USART2_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
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(__FILE__, __LINE__);
}
}
void USART6_Init(void)
{
huart6.Instance = USART6;
huart6.Init.BaudRate = 9600;
huart6.Init.WordLength = UART_WORDLENGTH_8B;
huart6.Init.StopBits = UART_STOPBITS_1;
huart6.Init.Parity = UART_PARITY_NONE;
huart6.Init.Mode = UART_MODE_TX_RX;
huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart6.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart6) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
void LED_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/*GPIO PD12, PD13, PD14 ve PD15 e bagli LED'lerin oncul tanimlamasi*/
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
void _Error_Handler(char *file, int line)
{
while(1)
{
}
}
2021-02-14 01:12 PM
Trying to do 50 bytes and not checking error returns, probably not going to work.
Potential for buffer to overflow when using as a string and not have a trailing NUL.
Not clearing buffers after use.
Try doing it in stages and debug the code
2021-02-14 11:23 PM
Thank yok gör reply. But I am newbie on it. I don’t know how to debug. Please help me. It is important for me. What wrong in my code?