-3

I am stuck on the following problem. Consider this code:

int main(void)
{
            SysTickInit();
            USART_GPIOInits();
            USART_Inits();
            char data[] = "hello\n";
            for(uint8_t i=0; i<10; i++)
            {
            HAL_UART_Transmit(&Usart1, (uint8_t*)data, strlen(data), 1000);
            }
                while(1){}
}

I try to send hello\n to Hercules in 10 times, but Hercules did not receive what i sent this is what Hercules got , it had þ every the first time I reset the MCU. But , when I used Debugger mode, it did not get any error.

enter image description here

below is transmit function

enter image description here

below is Init function

enter image description here

but want to communicate with fingerprint , but because of this wrong i cant communicate

metisai02
  • 11
  • 5
  • 4
    code should be included as text in the question as [mcve]. Questions about C should not be tagged C++ and vice versa. Last but not least, "please help me" is not a specific question – 463035818_is_not_an_ai Dec 10 '22 at 10:55
  • I'm sorry I just started using this website. I have corrected my question, thank you for letting me know – metisai02 Dec 10 '22 at 11:01
  • 1
    https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors – 463035818_is_not_an_ai Dec 10 '22 at 11:02
  • You can use the `edit` button below your question to fix the issues. – Gerhardh Dec 10 '22 at 11:39
  • When do you receive that first character? Do you get it already when GPIO or UART are initialized and no data was sent or do you get it when you send data only? – Gerhardh Dec 10 '22 at 11:41
  • I get it every time I send data, but it's only at the first position of the string as shown in the picture,I tried with HAL library from cubeMX but it doesn't happen like above – metisai02 Dec 10 '22 at 11:55
  • Your claim is not correct. You send the text in a loop but your log only shows one error. That means you only get it either as part of the first transmission or before the first transmission. BTW: You forgot to replace the pictures of text with text. – Gerhardh Dec 10 '22 at 22:10

1 Answers1

2

Change the order of initialization.

From

USART_GPIOInits();
USART_Inits();

to

USART_Inits();
USART_GPIOInits();

UART's default line state is logic high, logic low (start bit) launches a new transfer.

When GPIO is inialized first, with the corresponding peripheral module disabled, most likely you'll gen a logic low level on the TX pin, because there is no one to set it to a logic high (since UART is still disabled). When UART is initialized, it sets the TX line to a logic high (stop bit), and the terminal appication receives it as a broken byte.

Check your schematics

During and after reset CPU outputs are tri-stated. Most likely they'll stay at zero level until the configuration code will do it's job, leading to the same issue - receiving a garbage byte after the reset.

To prevent it, voltage levels on the interface pins must be defined during reset phase with an external pull-up resistor, like 10kOm, from TX and RX pins to VCC.

Flexz
  • 686
  • 1
  • 3
  • 13
  • i tried your code, but `FE` and `RXNE` were set and way as it was error. i saw HAL_LIBRARY using `MX_GPIO_Init();` first – metisai02 Dec 10 '22 at 17:25
  • you know what, i used `Delayms(200);` before i turned on `USART_CR1_UE` , The error did not seem to happen. but when I try to press the reset button a little fast which is on the CHIP sometime and then it goes wrong again. I know The problem here is the voltage. But how can I fix it effectively? – metisai02 Dec 10 '22 at 17:40
  • Do you have external pull-up resistors on the UART TX/RX pins? – Flexz Dec 10 '22 at 17:56
  • `GPIO_Handle_t USARTPins; USARTPins.pGPIOx = GPIOA; USARTPins.GPIO_PinConfig.GPIO_CRF = GPIO_CNF_ALOUT_PP; USARTPins.GPIO_PinConfig.GPIO_PinModeAndSpeed = GPIO_MODE_OUT_SPEED_50MHZ; // TX USARTPins.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_9; GPIO_Init(&USARTPins); // RX USARTPins.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_10; GPIO_Init(&USARTPins);` – metisai02 Dec 10 '22 at 17:58
  • just using the alternate function – metisai02 Dec 10 '22 at 17:59
  • 2
    This glitch happens before the configuration, because during and after reset CPU outputs are tri-stated. You need an extrnal pull-up resistor, line 10kOm, from TX and RX pins to VCC, to set the UART lines to inactive state. – Flexz Dec 10 '22 at 18:23
  • omg, I forgot that one. Thanks, sir for sharing. All the best to you! – metisai02 Dec 10 '22 at 18:52
  • @Flexz it would really improve the answer to move your last comment into your answer. Many will miss it several comments down. – David C. Rankin Dec 11 '22 at 08:06
  • Updated my answer with the final solution. – Flexz Dec 11 '22 at 11:26