2023-10-29 02:37 AM
I am stuck dont know what i am doing is wrong ... have been stuck for 2-3 days whenever i debug it just stops (not end just that debug arrow disappears and dont move forward) after HAL_UART_Transmit_IT() in int main().
#include <stdint.h>
#include "stm32f4xx_hal.h"
#include <string.h>
#include <stdio.h>
/*
* BOARD INFO :-
*
* USART 1
* USART_TX - PA9 AF7
* USART_RX - PA10 AF7
*
* */
#if !defined(__SOFT_FP__) && defined(__ARM_FP)
#warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif
/* GLOBAL VARIABLES */
//UART HandleTypeDef
UART_HandleTypeDef Uart1;
void UART1_Init(void);
const uint8_t txbuffer[10]={1,2,3,4,5,6,7,8,9,10};
uint8_t rxbuffer[10];
uint32_t tx_count,rx_count;
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
tx_count++;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
rx_count++;
}
int main(void)
{
HAL_Init();
UART1_Init();
HAL_UART_Transmit_IT(&Uart1,txbuffer,10);
HAL_UART_Receive_IT(&Uart1,rxbuffer,10);
while(1)
{
}
// /* Loop forever */
// for(;;);
}
void SysTick_Handler(void)
{
HAL_IncTick();
}
void UART1_Init(void)
{
// Configure Clock for UART
__HAL_RCC_GPIOA_CLK_ENABLE();
//GPIO init typedef
GPIO_InitTypeDef GpioStruct;
__HAL_RCC_USART1_CLK_ENABLE();
// GPIO Configuration
GpioStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GpioStruct.Mode = GPIO_MODE_AF_PP;
GpioStruct.Alternate = GPIO_AF7_USART1;
GpioStruct.Pull = GPIO_NOPULL;
GpioStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA,&GpioStruct);
// USART1 Configuration
Uart1.Instance = USART1; // USART1 Base Address is given
// Don't get confused between UART and USART there is just a bit difference
// We are configuring USART2 as UART
Uart1.Init.BaudRate = 115200;
Uart1.Init.WordLength = UART_WORDLENGTH_8B;
Uart1.Init.StopBits = UART_STOPBITS_1;
Uart1.Init.Mode = UART_MODE_TX_RX;
Uart1.Init.Parity = UART_PARITY_NONE;
Uart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
Uart1.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&Uart1);
// NVIC FOR priority and enabling IRQ
HAL_NVIC_SetPriority(USART1_IRQn,0,0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
void UART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&Uart1);
}
Solved! Go to Solution.
2023-10-30 10:10 AM
So I found What was Wrong it was my UARTIRQ_handler name which i wrote wrong its USART1_IRQHandler() , I found that from startup file .
2023-10-29 04:31 AM
If you are doing UART loopback tests by externally connecting TX and RX pins, you should start receiving before transmitting to avoid receiving errors or missing chars.
It might be helpful to implement at least a dummy UART error callback for better understanding what's going on here.
I also add some dummy code to the main loop like blinking a LED such that I can better place a breakpoint in the otherwise empty loop.
hth
KnarfB
2023-10-29 06:43 AM - edited 2023-10-29 06:43 AM
In addition, you should also define variables that are modified within the interrupt as volatile. It's unclear what metric you're using for verifying that reception isn't happening as your callbacks have no practical effect.
You can always run the code, hit pause, and see where the cpu execution is at.
2023-10-29 06:53 AM - edited 2023-10-29 06:58 AM
You could start with an example without checking register modification to start from.
If you connect RX and TX on your board the code attached should be all you need of modifications after CubeMX generated your serial application.
The printf statements are there for diagnostics purposes, do not use them in production code.
If properly configured you can see output in the ITM data console.
Properly configured would mean: debug settings (enable SWV) and in the ITM data console: (configure trace & enable trace)
Best Regards,
Johi.
2023-10-30 10:10 AM
So I found What was Wrong it was my UARTIRQ_handler name which i wrote wrong its USART1_IRQHandler() , I found that from startup file .
2023-10-30 10:11 AM
Thanks for your time.