cancel
Showing results for 
Search instead for 
Did you mean: 

UART_Mutex not working properly!

Mujtaba
Senior

Dear friends,

I've created a UART mutex and when threads only send data over uart, everything works fine but when it is time to also receive data, after a while data is corrupted and the information is wrong and no data is valid!

		if( osMutexWait(modbus_com1_mutex_id, MAX_MODBUS_MUTEX_TIMEOUT_MS) == osOK)
			{
			  DE_SIGNAL_1_GPIO_Port->BSRR = DE_SIGNAL_1_Pin;
				HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_RESET);
				HAL_UART_Transmit(&HAL_UART_COM1_HANDLE, txBuff, (txSize + 2), 20);			
				DE_SIGNAL_1_GPIO_Port->BSRR = (uint32_t)DE_SIGNAL_1_Pin << 16U;
				HAL_UART_Receive_IT(&HAL_UART_COM1_HANDLE, rxBuff, rxSize);
				mb_rcvd = MH_MB_WaitforRespond(MH_MB_TIMEOUT_VALUE, COM_1);
				
				if(mb_rcvd == MH_MB_OK)
				{
					HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_SET);
					rcv_check = MH_Mod_RTU_Receive_Check(rxBuff);
					
					if(rcv_check == MH_MB_OK)
					{
						osMutexRelease(modbus_com1_mutex_id);
						return MH_MB_OK;
					}
				}		
			}
if( osMutexWait(modbus_com1_mutex_id, MAX_MODBUS_MUTEX_TIMEOUT_MS) == osOK)
{
   /* Transmit data over RS485 media */
    DE_SIGNAL_1_GPIO_Port->BSRR = DE_SIGNAL_1_Pin;
    HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_RESET);
    HAL_UART_Transmit(&HAL_UART_COM1_HANDLE, txBuff, (txSize + 2), 20);			
    DE_SIGNAL_1_GPIO_Port->BSRR = (uint32_t)DE_SIGNAL_1_Pin << 16U;
    HAL_UART_Receive_IT(&HAL_UART_COM1_HANDLE, rxBuff, rxSize);
    /* Wait for the slave module to respond */
    mb_rcvd = MH_MB_WaitforRespond(MH_MB_TIMEOUT_VALUE, COM_1);
				
     if(mb_rcvd == MH_MB_OK)
     {
	HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_SET);
        /* Check if received data is valid */
         rcv_check = MH_Mod_RTU_Receive_Check(rxBuff);
					
	  if(rcv_check == MH_MB_OK)
	   {
		osMutexRelease(modbus_com1_mutex_id);
		return MH_MB_OK;
	    }
	}		
 }

Above is some part of the code related for handling the mutex and uart resource.

I'll be happy if anyone could help me with this problem.

Best regards,

Mujtaba

1 REPLY 1
Pavel A.
Evangelist III

Does MH_MB_WaitforRespond() wait for completion of HAL_UART_Receive_IT?

In case of RX timeout (mb_rcvd != MH_MB_OK) will it properly cancel the operation?

-- pa