2024-09-05 10:36 AM
Hey guys!
I'm trying to run the eaxmple found in this link: https://deepbluembedded.com/stm32-serial-port-uart-with-usb-ttl-converter-pc-interfacing/
I'm able to read the data from MCU serially, but when I write, the GPIO is not getting toggled. I've configured the MCU as described. I'm using blackpill v3 board. What I suspect is there is an issue with type conversions. But not sure. It would be very helpful if someone can help me with this.
Thanks in advance!
2024-09-05 10:46 AM
@zeus wrote:I'm able to read the data from MCU serially, but when I write, the GPIO is not getting toggled.
Not sure what you mean by "read from the MCU" and "write" here ?
How do you determine that, "the GPIO is not getting toggled" ?
@zeus wrote:What I suspect is there is an issue with type conversions.
What do you mean by that, and why do you suspect it?
2024-09-05 10:54 AM
What I meant by read and write is RX and TX of the UART. I've a if condition which basically checks the serially transmitted data and sets the GPIO high ot low accordingly.
By type conversions what I meant is, Do I've to convert the received byte to char to compare in the if statement?
2024-09-05 02:11 PM
@zeus wrote:I've a if condition which basically checks the serially transmitted data and sets the GPIO high ot low accordingly.
what "if condition"? Where? How does it check?
2024-09-05 02:40 PM
Present the code you're actually running so we can see what you see..
Paste in via </> tools, or on your own GitHub
2024-09-05 02:46 PM
Hello @zeus and welcome to the community.
@zeus wrote:
Hey guys!
I'm trying to run the eaxmple found in this link: https://deepbluembedded.com/stm32-serial-port-uart-with-usb-ttl-converter-pc-interfacing/
I don't think you can expect that someone will be going to look at this link and read all of its content.
Better to summurize and provide what you did and share the exact code you written (not the one in the link).
Need also to check if you have a genuine ST MCU on board or it's fake ..
Thank you.
2024-09-05 08:55 PM
Hello, Thanks for the responses. Please find the piece of code I'm trying to run below.
#include "main.h"
#include <stdio.h>
UART_HandleTypeDef huart1;
uint8_t RX1_Char = 0x00;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
void HAL_USART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Receive_IT(&huart1, &RX1_Char, 1);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1, &RX1_Char, 1);
while (1)
{
if(RX1_Char == 'a')
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
HAL_UART_Receive_IT(&huart1, &RX1_Char, 1);
RX1_Char = 0x00;
}
if(RX1_Char == 'b')
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
HAL_UART_Receive_IT(&huart1, &RX1_Char, 1);
RX1_Char = 0x00;
}
HAL_Delay(100);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
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();
}
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();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
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();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
Also, The board I'm using currently is this:
So, When I run the above code. The corresponding GPIO is not responding. Kindly help. Thanks in advance!
2024-09-06 01:46 AM - edited 2024-09-14 03:18 AM
- Create a standalone basic program that toggles the LED, make sure that part works as expected. If not, debug that first. There may be a missing jumper on the board required to connect the pin to the LED. Also check the schematic to ensure C13 designates a pin and not a capacitor (!).
- Use a debugger to check the value stored in the variable after the UART RX to ensure it matches your expectation.
- In the code shown, it doesn't look like casting is the cause behind your issue.
2024-09-06 11:14 AM - edited 2024-09-06 11:16 AM
Hi,
Thanks for your response. As you ,mentioned, I already tested the GPIO pin C13 and it's working fine. But When I try to fetch serial command and then set the GPIO, It's not working. I'm still trying to debug and fix the issue.
I don't have a debugger. I'm using the serial transmit to try printing the value. But still no luck.
2024-09-06 11:24 AM
You have:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
in all cases, it is never set high.