2024-06-25 12:20 PM
Hi,
I have a problem with my project. There are two boards MCU1 and MCU2. They are communicating with each other via UART. I simplified program to better understand why it is still not working but somehow I still cant see what the problem.
Program description:
1. MCU1 send to MCU2 AA (AA is array filled with two 0xAA)
2. MCU2 send to MCU1 CC (CC is array filled with two 0xCC)
3. MCU1 after receiveing 0xCC starts engine
4.MCU1 send to MCU2 BB (BB is array filled with two 0xBB)
5. MCU2 send to MCU1 DD (DD is array filled with two 0xDD)
6. MCU1 after receiving 0xDD starts horn
On 6 step there is a problem. I can see on oscilloscope that DD frame is coming to MCU1 but somehow MCU1 doesnt go to callback function and doesnt exceute code in callback function - doesnt turn on the horn, rest of the code is executing fine.
Baud rate is the same for both MCUs.
Source code MCU1:
/* USER CODE BEGIN 0 */
uint8_t buffer[2];
uint8_t AA[2]={0xAA,0xAA};
uint8_t BB[2]={0xBB,0xBB};
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(buffer[0]==0xCC && buffer[1]==0xCC)
{
engine(ON);
}
if(buffer[0]==0xDD && buffer[1]==0xDD)
{
horn(ON);
}
HAL_UART_Receive_IT(&huart2, buffer, 2);
}
/* USER CODE END 0 */
/**
* @brief The applicationentry point.
* @retvalint
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of allperipherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initializeall configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart2, bufferMCU1, 2);
HAL_Delay(500);
HAL_UART_Transmit(&huart2, AA, 2, 10); // This is send to MCU2
HAL_Delay(500);
HAL_UART_Transmit(&huart2, BB, 2, 10); // This is send to MCU2
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Source code MCU2:
/* USER CODE BEGIN 0 */
uint8_t bufferMCU2[2];
uint8_t CC[2]={0xCC,0xCC};
uint8_t DD[2]={0xDD,0xDD};
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (bufferMCU2[0]==0xAA && bufferMCU2[1]==0xAA )
HAL_UART_Transmit(&huart2, CC, 2, 10); // This is send to MCU1
if (bufferMCU2[0]==0xBB && bufferMCU2 [1]==0xBB)
HAL_UART_Transmit(&huart2, DD, 2, 10); // This is send to MCU1
HAL_UART_Receive_IT(&huart2, bufferMCU2, 2);
}
/* USER CODE END 0 */
/**
* @brief The applicationentry point.
* @retvalint
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of All peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initializeallconfiguredperipherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart2, bufferMCU2, 2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Solved! Go to Solution.
2024-06-25 01:29 PM - edited 2024-06-25 01:30 PM
@Raf_alYour problem is not related to a specific STM32 product, it is about using the HAL library and debugging.
Are you looking for help with using the debugger to trace and understand the flow of your program? Is this a student assignment?
2024-06-25 12:39 PM
Can someone move it to STM32 MCU products section? By mistake I choose wrong section for this post.
2024-06-25 01:29 PM - edited 2024-06-25 01:30 PM
@Raf_alYour problem is not related to a specific STM32 product, it is about using the HAL library and debugging.
Are you looking for help with using the debugger to trace and understand the flow of your program? Is this a student assignment?
2024-06-25 01:34 PM
MCU1 line 61:
HAL_UART_Receive_IT(&huart2, bufferMCU1, 2);
but line 9 ff is using buffer, not bufferMCU1
hth
KnarfB
2024-06-26 12:09 AM
Yes I am looking for help to understand why my program isnt working. But I see that I write in wrong section so probably I have to write my post again.
2024-06-26 12:11 AM
Good catch! But in original code there is a bufferMCU1. I just copied my test code from cubeIDE and I forgot to change this buffer to bufferMCU1.