cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 HAL for Rs485 Modbus

chetan
Associate II
Posted on October 28, 2014 at 11:27

Hi,

I am trying to change to HAl library from STD lib. One of the task is port modbus code. I want to receive data, however size is unknown and is time critical (2.5 chars depending on baud rate).

typedef struct

{

  uint8_t au8RxBuf[UART_BUF_RX_SIZE];

  uint8_t au8TxBuf[UART_BUF_TX_SIZE];

  uint16_t u16RxBufCnt;

  uint16_t u16TxBufCnt;

  uint8_t  u8TxComplete;

  uint8_t  u8Error;

  uint8_t  u8RxStatus;

  uint8_t  u8TxStatus;

}uart_data_s;

uart_data_s uart_data_485B;

void bsp_uart_485B_ISR(void)

{

  uint8_t u8dummy;

  static uart_data_s *ptr;

  static uint16_t u16ISRTxCnt = 0;

 

  ptr = &uart_data_485B;

// Receive Section

 if(USART_GetITStatus(usart_conf[UART_485B].usart_peri, USART_IT_RXNE) != RESET)

 {

    bsp_GenTimer_Load(MB_PKT_SEAL_GEN_TIMERB, mbPktSealTimeout[UART_19200_BAUD]);            // Maximum Timeout between bytes to seal packet

    u8dummy = USART_ReceiveData(usart_conf[UART_485B].usart_peri);                           // Read one byte from the receive data register

    if (UART_BUF_STATUS_FREE == (uart_buf_status_e) ptr->u8RxStatus) {

      if(ptr->u16RxBufCnt < UART_BUF_RX_SIZE)

        ptr->au8RxBuf[ptr->u16RxBufCnt++] = u8dummy;                                                                                                 // else Neglect As buffer is not free

    }

  }

// Transmit section

if(USART_GetITStatus(usart_conf[UART_485B].usart_peri, USART_IT_TXE) != RESET)

{  

    if(u16ISRTxCnt < ptr->u16TxBufCnt)

      USART_SendData(usart_conf[UART_485B].usart_peri, ptr->au8TxBuf[u16ISRTxCnt++]);         // Send One Byte

    else 

    {

      ptr->u8TxComplete = UART_STATUS_FREE;                                                                                                                 // Transmit Complete

      u16ISRTxCnt = 0;

      ptr->u16TxBufCnt = 0;

      bsp_GenTimer_Load(MB_TX_COMPLETE_TIMERB,mbPktSealTimeout[M530S_RS485B_DFLT_BAUD]);

      USART_ITConfig(usart_conf[UART_485A].usart_peri, USART_IT_TXE, DISABLE);                 // Disable Tx Interrupt

    }

  }

 }

Note: bsp_GenTimer_Load will load timeout in software tiimers and on TImeout seal the packet or post the semaphore.

Also I am trying to check UART_Hyperterminal_IT example provided with HAL. 

This example receives 10 bytes of data however if I send 15 bytes in one go code loops inside HAL_UART_IRQHandler in UART_Transmit_IT with huart->State as HAL_UART_STATE_READY. Is there any bug with this HAL lib code. Also Can you provide me the pending bug list with HAL.

I have increased RxBuffer size to 32 bytes howevr receiving 10 bytes only through HAL_UART_Receive_IT.

Is there any known bugs list with HAL.

Regards

Chetan

6 REPLIES 6
Posted on October 28, 2014 at 13:44

Hi Chetan,

Would you mind giving a little more information about the STM32Cube_FW package you are using ? That would help more easily figure out where a problem could be.

Regards,

Heisenberg.

chetan
Associate II
Posted on October 28, 2014 at 13:52

I am using V 1.3.0 Repository STM32Cube_FW_F4_V1.3.0.

For Porting Modbus II need help to configure my ISR for receiving one byte at a time so that I can load timer for timeout. Current HAL  ISR is to heavy to work at 115200 baud.

Chetan

Posted on January 22, 2015 at 11:08

Hi Chetan,

The

http://www.st.com/st-web-ui/static/active/en/st_prod_software_internet/resource/technical/software/firmware/stm32cubef4.zip

is released with the new STM32F4_HAL_Driver V1.2.0. The following updates are implemented on the HAL UART driver:

  • Add IS_LIN_WORD_LENGTH() and IS_LIN_OVERSAMPLING()  macros: to check respectively WordLength and OverSampling parameters in LIN mode
  • DMA transmit process; the code has been updated to avoid waiting on TC flag under DMA ISR, UART TC interrupt is used instead. Below the update to be done on user application:
  • Configure and enable the USART IRQ in HAL_UART_MspInit() function
  • In stm32f4xx_it.c file, USARTx_IRQHandler() function: add a call to HAL_UART_IRQHandler() function
  • IT transmit process; the code has been updated to avoid waiting on TC flag under UART ISR, UART TC interrupt is used instead. No impact on user application
  • HAL_UART_Transmit_DMA() update to follow the right procedure ''Transmission using DMA'' in the reference manual
  • Add clear the TC flag in the SR register before enabling the DMA transmit request

Regards,

Heisenberg.

rchris
Associate II
Posted on January 23, 2015 at 09:35

Hi Heisenberg,

when will the new Cube driver be released for the STM32F2xx? Will it be able to handle non-blocking reception of USART messages with unknown lenght?

Ralph

magnus2
Associate II
Posted on January 24, 2016 at 18:29

Hi Heisenberg,

Is the STM32F4_HAL_Driver V1.2.0 able to trig a recieve interrupt for each single char that is recieved?

What code lines do I need to add/modify to make the ST's example code''UART_TwoBoards_ComIT'' for stm32F4 Discovery to work with recieve interrupt for each char?

Regards

Magnus 

Posted on January 24, 2016 at 19:46

The STM32 USART is inherently only capable of dealing with a byte at a time, based on it's buffering/design. The use of DMA may mask this, but any interrupt/callback based method is going to get an RXNE type interrupt as each byte arrives.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..