cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 UART MODBUS mode fix

Posted on November 16, 2016 at 19:42

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.
2 REPLIES 2
slimen
Senior
Posted on November 17, 2016 at 15:55

Hello,

You shouldn't refer to the RM0091 as it applies for STM32F0x1/STM32F0x2/STM32F0x8.

You are using STM32F030, so referring to the 

http://www.st.com/content/ccc/resource/technical/document/reference_manual/cf/10/a8/c4/29/fb/4c/42/DM00091010.pdf/files/DM00091010.pdf/jcr:content/translations/en.DM00091010.pdf

Regards
Posted on November 17, 2016 at 18:12

Hey, thank you for answer. I have checked the datasheet you linked. Maybe incidentally, but the registers and their positions match :).