1
\$\begingroup\$

I am trying to make a small project with an STM32 which uses serial commands to control the rotation direction of a motor. I used UART interrupts and callbacks to try to achieve that.

I am using an L298N and PWM signals to run the motor. I plan on toggling the IN2 and IN3 states of the L298N to change the direction.

I looked in the documentation and few other sources and wrote a program for that. When I run debug, I am not able to receive UART messages.

Here's the main and the callback function parts of the program:

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_TIM2_Init();
  MX_USART1_UART_Init();

  HAL_UART_Transmit_IT(&huart1, "trial", sizeof("trial"));
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);

  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);

This the infinite while loop of the main (I use this part to stop the motor if either stop buttons are pressed):

  while(1)

  {
      if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_2) == 1) || (HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_7) == 1) )
      {
          pulseWidth = 0;
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
      }
  }

Here is the interrupt callback function:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart -> Instance == USART1)
    {
        HAL_UART_Receive_IT(&huart1, &rData, 1);
        receiveBuffer[pos++] = rData;

        if (rData == "\n")
                {
                        pos = 0;

                        if (strstr(receiveBuffer, "OPEN"))
                        {
                            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
                            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);

                        }
                        else if (strstr(receiveBuffer, "CLOSE"))
                        {
                            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
                            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
                        }

                        menset(receiveBuffer, 0, 8);

                }
    }
}

When I start debug and use Hercules to send serial data, the MCU does not read it. Can anyone help me to understand what is wrong?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Your code never asks to receive anything in the first place, so the receive callback is never executed.

And by looking at the way you use the callback, you seem to have an misunderstanding how the interrupt receiving works. HAL_USART_Receive_IT() is not used to read data that is previously received with interrupts. It is used to start reception of X bytes into memory buffer Y using interrupts.

The callback will be called only after X bytes have been received into the buffer and you can read the buffer in the callback.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.