STM32F4 HAL_UART_Transmit() Unexpected Behavior Debugging Help
So I have a abstracted function to read the INA226 component. The function reads the data completely fine over I2C. The problem occurs when I try to display the read data on my dev terminal (Terra Term) over the COM port for STM32.
The output on the TerraTerm Terminal shows, the INA260 data with a hallucinated sentence. Something that should not be printed or should not be possible to print with the existing logic in the code.
I am also calling the INA226Read() directly from main() without any other function calls before it.
Example Output:
0.00500
INA226 not present
INA226 b4.96600
INA226 not present
INA226 b0.03000
INA226 not present
INA226 b
The phrase "INA226 not present" should not print with the logic in my code.
The function that is executing:
void INA226Read()
{
INA226_Init();
COM_Init();
uint8_t exit_counter = 0;
uint16_t data;
char tes[32];
float current;
float power;
float bus_voltage;
// Initialize the buffer with 0
memset(tes, 0, sizeof(tes));
if (INA226_present() != HAL_OK)
{
COM_printf("INA226 not present\n");
return;
}
for (;;)
{
HAL_StatusTypeDef status = readCurrent(&data);
current = (float)data / 1000;
HAL_StatusTypeDef status2 = readBusVoltage(&data);
bus_voltage = (float)data / 1000;
HAL_StatusTypeDef status3 = readPower(&data);
power = (float)data / 1000;
if (status != HAL_OK || status2 != HAL_OK || status3 != HAL_OK)
{
if (status == HAL_BUSY)
{
COM_printf("INA226 busy");
continue;
}
COM_printf("Error reading INA226 \n");
exit_counter++;
if (exit_counter == 5)
{
COM_printf("INA226 not responding, exiting\n");
break;
}
vTaskDelay(1000);
continue;
}
float_to_char(current, tes);
COM_printf_chararray(tes, 32);
COM_printf("\n");
memset(tes, 0, sizeof(tes));
float_to_char(bus_voltage, tes);
COM_printf_chararray(tes, 32);
COM_printf("\n");
memset(tes, 0, sizeof(tes));
float_to_char(power, tes);
COM_printf_chararray(tes, 32);
COM_printf("\n");
break;
}
The functions for float_to_char() work as intended, so I will leave that out to not bloat this question. However I will add the functions Com_printf() and Com_printf_chararray().
Com_printf_chararray:
void COM_printf_chararray(char *str, uint16_t size)
{
if (HAL_UART_Transmit(&uart_handle, (uint8_t *)str, size, HAL_MAX_DELAY) != HAL_OK)
{
Error_Handler();
}
}
Com_printf:
void COM_printf(char *str)
{
if (HAL_UART_Transmit(&uart_handle, (uint8_t *)str, 32, HAL_MAX_DELAY) != HAL_OK)
{
Error_Handler();
}
}
Also when debugging the code, I get a HardFault error after the break at the end of the INA226Read() function
Call Stack:
HardFault_Handler@0x08000c0e (c:\Zaid\Fun_Projects_Code\CASE\Core\Src\stm32f4xx_it.c:90)
<signal handler called>@0xffffffe9 (Unknown Source:0)
HAL_UART_Transmit@0x08002902 (c:\Zaid\Fun_Projects_Code\CASE\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_uart.c:1207)
??@0x00000000 (Unknown Source:0)
For full context, you can checkout the github for this project: Github
Question I have: Is there some type of config I am missing in my code, or is this a bug in the HAL?
