cancel
Showing results for 
Search instead for 
Did you mean: 

Black pill serial communication not working!

zeus
Associate II

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!

21 REPLIES 21
Andrew Neil
Evangelist III

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

 

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?


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

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

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SofLit
ST Employee

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
zeus
Associate II

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! 

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

- If someone's post helped resolve your issue, please thank them by clicking "Accept as Solution".
- Please post an update with details once you've solved your issue. Your experience may help others.

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.

Chris21
Senior

You have:

    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);

in all cases, it is never set high.