AnsweredAssumed Answered

STM32F0 UART MODBUS mode fix

Question asked by Lukasz Przenioslo on Nov 16, 2016
Latest reply on Nov 17, 2016 by Lukasz Przenioslo
Hello there,
I was trying to get going with ModBus RTU communication on the STM32F030 chip, specifically the t3,5 interval counting, since its supported by hardware in this chip (RM0091 page 701 on the bottom).

HAL library in CubeMX version 4.17 does not suppor it however. Those would be the defines and macros needed to be added to the code (more or less):

Clearing RTOCF flag in UART interrupt routine.
// lacking UART_Interrupt_definition
// Z = 11, X = 1, Y = 26
#define UART_IT_RTOF                    ((uint16_t)0x0B3A)
#define UART_CLEAR_RTOCF                USART_ICR_RTOCF

/*
 * @brief   Place this function in the \ref USART1_IRQHandler handler because HAL
 *          doesnt support RTOCF flag clearing.
 * @param   uHandle: uart handle struct pointer.
 */
void mbg_ClearRTOCF_Flag(UART_HandleTypeDef* uHandle)
{
    assert_param(uHandle);
 
    // check RTOF flag
    if((__HAL_UART_GET_IT(uHandle, UART_IT_RTOF) != RESET) &&
          (__HAL_UART_GET_IT_SOURCE(uHandle, UART_IT_RTOF) != RESET))
    {
        __HAL_UART_CLEAR_IT(uHandle, UART_CLEAR_RTOCF);
    }
}

Enabling the t3,5 char interrupt timer when awaiting for data:
/*
 * @brief   Enables the MODBUS t3,5 char timer.
 * @param   uHandle: pointer to an uart handle struct.
 */
void mbg_EnableT35(UART_HandleTypeDef* uHandle)
{
    assert_param(uHandle);
 
    // manually initialize registers for t3.5 timing
    uHandle->Instance->CR2 |= USART_CR2_RTOEN; // Receiver timeout enable
    uHandle->Instance->CR1 |= USART_CR1_RTOIE; // Receiver timeout interrupt enable
    uHandle->Instance->RTOR |= 38;             // Receiver timeout value (11 * 3.5)
}

Also as I failed to notice, the HAL functions for half-duplex uart communication correspond to only 1 wire communication, not 2 wire half duplex communication. What I still need to check is either the DE line in RS485 mode is toogling properly, but I dont have the scope on me. Ill post a followup as soon as I check this. Hope this helps and that the ModBus functionality will be implemented in future HAL libs.

Outcomes