#include "trans_recieve_buff_control.h" #include "ringbuffer.h" RingBuffer m_Modbus_Master_RX_RingBuff; uint8_t m_Modbus_Master_RX_Buff[200]; extern MODBUS_HandleTypeDef uart0; /** * @brief Initialize the ringbuffer ring queue configuration for interrupt reception, and the bytes received by interrupt are managed by the structure pointer of m_Modbus_Master_RX_RingBuff * @param * @note * @retval void * @author xiaodaqi */ uint8_t Modbus_Master_RB_Initialize(void) { /*Initialize ringbuffer related configuration*/ rbInitialize(&m_Modbus_Master_RX_RingBuff, m_Modbus_Master_RX_Buff, sizeof(m_Modbus_Master_RX_Buff)); return 1 ; } /** * @brief Clear the circular queue * @param * @note * @retval void * @author xiaodaqi */ uint8_t Modbus_Master_Rece_Flush(void) { rbClear(&m_Modbus_Master_RX_RingBuff); } /** * @brief Determine whether there are unprocessed bytes in the GPS ringbuffer * @param * @note * @retval void * @author xiaodaqi */ uint8_t Modbus_Master_Rece_Available(void) { /*If the data packet buffer overflows, clear it and re-count*/ if(m_Modbus_Master_RX_RingBuff.flagOverflow==1) { rbClear(&m_Modbus_Master_RX_RingBuff); } return !rbIsEmpty(&m_Modbus_Master_RX_RingBuff); } /**************************************************************************** *** Hardware Related Implementation ****************************************************************************/ /*The following is the part associated with the hardware interface layer, transplanted according to the processing mode of different processors*/ /** * @brief Get the value in the receive register * @param * @note * @retval void * @author xiaodaqi */ modbus_status_t Modbus_Master_GetByte(uint8_t *getbyte) { //if(HAL_UART_Receive (&huart2 ,(uint8_t *)getbyte,1,0x01) != HAL_OK ) //{ // return HAL_ERROR; //} //else //{ // return HAL_OK; //} if(LPUART_DRV_ReceiveDataBlocking(MODBUS_DRV_INSTANCE ,(uint8_t *)getbyte,1,MODBUS_UART_TIME_OUT ) != MODBUS_SUCCESS ) { return MODBUS_ERROR; } else { return MODBUS_SUCCESS; } } /** * @brief Interrupt processing function, called in the serial port receiving interrupt: the value of the register is pressed into the buffer * @param * @note * @retval void * @author xiaodaqi */ uint8_t Modbus_Master_Rece_Handler(void) { uint8_t byte; //Read the data in the register and push the data into the circular queue if(Modbus_Master_GetByte(&byte)==MODBUS_SUCCESS) { rbPush(&m_Modbus_Master_RX_RingBuff, (uint8_t)(byte & (uint8_t)0xFFU)); } } /** * @brief Read the data in the buffer * @param * @note * @retval void * @author xiaodaqi */ uint8_t Modbus_Master_Read(void) { uint8_t cur =0xff; if( !rbIsEmpty(&m_Modbus_Master_RX_RingBuff)) { cur = rbPop(&m_Modbus_Master_RX_RingBuff); } return cur; } /** * @brief Send out the packet * @param * @note * @retval void * @author xiaodaqi */ modbus_status_t Modbus_Master_Write(uint8_t *buf,uint8_t length) { //if(HAL_UART_Transmit(&huart2 ,(uint8_t *)buf,length,0xff)) //{ // return HAL_ERROR; //} // else // { // return HAL_OK; // } if(LPUART_DRV_SendDataBlocking(MODBUS_DRV_INSTANCE,(uint8_t *)buf,length,MODBUS_UART_TIME_OUT) != MODBUS_SUCCESS) { return MODBUS_ERROR; } else { return MODBUS_SUCCESS; } } /** * @brief 1ms period timer * @param * @note * @retval void * @author xiaodaqi */ uint32_t Modbus_Master_Millis(void) { return HAL_GetTick(); }