How to execute a function in main.c instead of model.cpp?
I am using the STM32H745XIH6 board with touchgfx. And I am calling the HAL_UART_Transmit_DMA function in model.cpp whenever the user entered a string on the LCD.
But I would like this HAL_UART function to be execute to main.c instead. How do I do this codewise? I am not very experienced with interacting C++ with C. The function that I want to transfer to main.c is SendData2MCU(int UART, int DMA, uint8_t* data)
#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>
#ifndef SIMULATOR
#include "main.h"
#include "string.h"
extern "C"
{
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart3;
extern UART_HandleTypeDef huart7;
extern uint8_t RX1_String_cpy[512];
extern uint8_t RX3_String_cpy[512];
extern uint8_t RX7_String_cpy[512];
extern int DMA_complete;
}
#endif
Model::Model() : modelListener(0), toggleGreenLEDButtonState(false), toggleRedLEDButtonState(false)
{
}
void Model::tick()
{
#ifndef SIMULATOR
modelListener->RX1_3Data((char*) RX1_String_cpy, (char*) RX3_String_cpy);
modelListener->RX7_Data((char*) RX7_String_cpy);
modelListener->klima_sim_receive((char*) RX1_String_cpy);
#endif
}
// The following code needs to be executed in main.c
void Model::SendData2MCU(int UART, int DMA, uint8_t* data)
{
#ifndef SIMULATOR
if(UART==0)
{
if(DMA==0)
{
HAL_UART_Transmit(&huart3, (uint8_t*) data, strlen((char*)data)-2, 100);
}
else if(DMA==1)
{
DMA_complete = 0;
HAL_UART_Transmit_DMA(&huart3, (uint8_t*) data, strlen((char*)data)-2);
while(DMA_complete == 0){};
}
}
if(UART==1)
{
if(DMA==0)
{
HAL_UART_Transmit(&huart1, (uint8_t*) data, strlen((char*)data), 100);
}
else if(DMA==1)
{
DMA_complete = 0;
HAL_UART_Transmit_DMA(&huart1, (uint8_t*) data, strlen((char*)data));
while(DMA_complete == 0){};
}
}
#endif
}
The reason for this is, DMA has been shown to not be working reliably when compiled in C++. I have been told that the compiler can convert C directly to assembly language but for C++, it has to be translated into another form first and this therefore causes unreliability.