Skip to main content
Visitor II
September 5, 2024
Solved

Black pill serial communication not working!

  • September 5, 2024
  • 9 replies
  • 4548 views

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!

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    ST resources are dedicated to supporting genuine ST products. We are not committed to ensuring that clones/fakes work properly with the firmware we provide.

    Thank you for your understanding.

    9 replies

    Super User
    September 5, 2024

    @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?

     

    zeusAuthor
    Visitor II
    September 5, 2024

    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?

    Super User
    September 5, 2024

    @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?

    Graduate II
    September 5, 2024

    Present the code you're actually running so we can see what you see..

    Paste in via </> tools, or on your own GitHub

    Technical Moderator
    September 5, 2024

    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.

    zeusAuthor
    Visitor II
    September 6, 2024

    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:

    STM32F411CEU6_WeAct_Black_Pill_V2.0-1.jpg

    So, When I run the above code. The corresponding GPIO is not responding. Kindly help. Thanks in advance! 

    Graduate II
    September 6, 2024

    - 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.

    zeusAuthor
    Visitor II
    September 6, 2024

    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.

    Graduate
    September 6, 2024

    You have:

     HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);

    in all cases, it is never set high.

    zeusAuthor
    Visitor II
    September 7, 2024
     HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);

    Even If I do the above It's not working!

    zeusAuthor
    Visitor II
    September 7, 2024

    Okay, So I tried debugging the code with debugger. I'm able to see that the if statement is not satisifed. I'm new to STM32 and I'm not sure how to print the values on debugger console. Can anyone please help me with that? Also, check the image for reference:

    zeus_0-1725726878648.png

     

    Super User
    September 7, 2024

    @zeus So are your questions now about Nucleo-H723ZG (with genuine ST-LINK on-board) or black pill? 

    zeusAuthor
    Visitor II
    September 8, 2024

    Well, my question is how to make the uart code work as I'm not sure what am I missing. I'm using Nucleo-H723ZG (with genuine ST-LINK on-board) to debug the code as I don't have sepate debugger to test on black pill. So, how can I print the variables in the debugger console while using the st-link onboard?

    Graduate II
    September 8, 2024

    Have you checked if you have connected all signal grounds properly? to test rs232 you can do loopbak. Do loopback towards data source and see if you get back what you transmit. 

    Graduate II
    September 8, 2024

    What happens if you get any other symbol in the serial line? First chek if your processor gets anything at all, later you can start filtering and processing 

    You can  try HAL_GPIO_TogglePin

     

    zeusAuthor
    Visitor II
    September 9, 2024

    @Techn I did check the signal grounds and everything is proper. I also tried the HAL_GPIO_TogglePin. It's working fine, also the processor is able to transmit data via UART and I was able to get it printed on CoolTerm.

    mƎALLEmAnswer
    Technical Moderator
    April 14, 2025

    Hello,

    ST resources are dedicated to supporting genuine ST products. We are not committed to ensuring that clones/fakes work properly with the firmware we provide.

    Thank you for your understanding.