1
\$\begingroup\$

I have to send data by UART1 from my STM32F429. The problem is that the data is not sent correctly.

I debugged and I got this. For testing I want to send buffer[0]=60;, and on the other side I have to hear a sound as data is received, but it doesn't work.

Here is my debugger screen shot, as you see it has wrongly occupied.

enter image description here

This is my UART configuration:

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_8;
if (HAL_UART_Init(&huart1) != HAL_OK) {
  Error_Handler();
}

This is my code to send data:

buffer[0]=60;

HAL_UART_Transmit_IT(&huart1,(uint8_t*)buffer[0],1);
HAL_UART_TxCpltCallback(&huart1);
HAL_GPIO_WritePin(RED_GPIO_Port, RED_Pin, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(RED_GPIO_Port, RED_Pin, GPIO_PIN_RESET);
        HAL_Delay(500);
\$\endgroup\$
7
  • 1
    \$\begingroup\$ 60 decimal is 0x3C (hex) \$\endgroup\$ Commented Dec 25, 2022 at 7:02
  • \$\begingroup\$ @Peter Bennett I have to write in hex format? \$\endgroup\$ Commented Dec 25, 2022 at 7:09
  • \$\begingroup\$ It does not make sense... \$\endgroup\$ Commented Dec 25, 2022 at 7:13
  • 2
    \$\begingroup\$ You set buffer[0] to 60 decimal. The screen shot shows buffer[0] is 0x00003C which is the same value but shown in hexadecimal. You have to be aware of what number base is used where - a leading "0x" indicates a hex value. \$\endgroup\$ Commented Dec 25, 2022 at 7:18
  • 2
    \$\begingroup\$ Read the reference/user manual for a description of the stm32 peripherals. This should describe in more detail what the control and data registers do. As for your core problem, in the transmit line you want (uint8_t*)&buffer[0]. Note the & for ‘address of’. Currently your code casts 60 as the address which is 0x0000003c which is probably not what you intended. \$\endgroup\$ Commented Dec 25, 2022 at 9:43

4 Answers 4

1
\$\begingroup\$

Your code sends a byte from address 60.

What you likely want to do here is send a byte from the address of your buffer variable, where 60 is stored, to send 60.

\$\endgroup\$
0
\$\begingroup\$

In your code, you have a line where you send a byte to the UART to be transmitted:

HAL_UART_Transmit_IT(&huart1,(uint8_t*)buffer[0],1);

In the middle of that line, you have a "type-cast": (uint8_t*). Why did you put that there? Did the compiler give you a message telling you that it was expecting a different kind of variable?

A uint8_t * is a pointer to a uint8_t, in other words, it is the address of a byte - that's what the HAL_UART_Transmit_IT function wants - the address of the thing you want to send (not the thing itself).
By casting your buffer[0] like that, you're telling the compiler to take the value of buffer[0] and treat it as an address (pointer), even though it really isn't one.

What you should have done is take the address of buffer[0] and given that to the HAL_UART_Transmit_IT function instead, using:

HAL_UART_Transmit_IT(&huart1, &buffer[0], 1);
\$\endgroup\$
0
\$\begingroup\$

Assuming the type of buffer is uint32_t[], depending on the endianness, you may try

uint8_t val = buffer[0];

or

uint8_t val = buffer[0] >> 24;

and then

HAL_UART_Transmit_IT(&huart1,&val,1);
\$\endgroup\$
2
  • \$\begingroup\$ You edited the whole answer. Now the problem is that the answer depends on endianness so you are guessing - the endianess is known. And of course it will work if you send from the address of variable, but moving the data from buffer to variable is an useless step. You can just send from buffer directly. \$\endgroup\$ Commented Dec 27, 2022 at 15:25
  • \$\begingroup\$ you are welcome \$\endgroup\$ Commented Dec 27, 2022 at 15:54
0
\$\begingroup\$

To send data through UART1 on an STM32F429 microcontroller using the current Hardware Abstraction Layer (HAL), you can use the following steps:

  1. Configure the UART1 peripheral by using the HAL_UART_Init function. This function takes a pointer to a UART_HandleTypeDef structure as an argument. You will need to fill in the fields of this structure with the appropriate configuration values for the UART1 peripheral.
  2. Once the UART1 peripheral is configured, you can send data through it by using the HAL_UART_Transmit function. This function takes a pointer to a UART_HandleTypeDef structure, a pointer to the data buffer, and the length of the data to be transmitted as arguments.
  3. If you want to receive data through UART1, you can use the HAL_UART_Receive function. This function takes a pointer to a UART_HandleTypeDef structure, a pointer to a buffer to store the received data, and the length of the data to be received as arguments.

Here is an example of how you might use these functions in your code:

#include "stm32f4xx_hal.h"

UART_HandleTypeDef huart1;
uint8_t txData[] = "Hello World!";
uint8_t rxData[20];

int main(void)
{
  // Initialize the UART1 peripheral
  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 handling
  }

  // Transmit data through UART1
  if (HAL_UART_Transmit(&huart1, txData, sizeof(txData), 1000) != HAL_OK)
  {
    // Error handling
  }

  // Receive data through UART1
  if (HAL_UART_Receive(&huart1, rxData, sizeof(rxData), 1000) != HAL_OK)
  {
    // Error handling
  }

  return 0;
}

Note that the HAL_UART_Init, HAL_UART_Transmit, and HAL_UART_Receive functions all return a status code (HAL_OK if the operation was successful, or HAL_ERROR if there was an error). It is important to check the return value of these functions and handle any errors appropriately in your code.

\$\endgroup\$
0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.