#include <stdio.h>
void DebugUsartSend(uint8_t data)
{
MAX3485_DE_TX;
while ( (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_TXE) ? SET : RESET) == RESET );
USART3->DR = (data & (uint8_t)0xff);
MAX3485_DE_RX;
}
int fputc(int ch, FILE *f)
{
DebugUsartSend((uint8_t)ch);
return ch;
}
#define RX_FIFO_NUMBER_BLOCK 32U
#define RX_FIFO_NUMBER_BYTE 8U
#define TX_FIFO_NUMBER_BLOCK 50U
#define TX_FIFO_NUMBER_BYTE 8U
typedef struct TX_BACKUP_BUF_STRUCT
{
uint8_t block_count;
uint32_t block_mark;
uint8_t sed_fifo[TX_FIFO_NUMBER_BLOCK][TX_FIFO_NUMBER_BYTE];
}sTx_BackUp_Buf_TypeDef;
typedef struct RX_BUF_STRUCT
{
uint8_t block_count;
uint32_t block_mark;
uint8_t rec_fifo[RX_FIFO_NUMBER_BLOCK][RX_FIFO_NUMBER_BYTE];
}sRx_Buf_TypeDef;
static inline bool bsp_send_data_copy_to_fifo( uint8_t *dat_ptr, sTx_BackUp_Buf_TypeDef *fifo_ptr )
{
if ( NULL == dat_ptr || NULL == fifo_ptr )
return false;
for ( uint8_t i = 0U; i < TX_FIFO_NUMBER_BYTE; i++ )
{
fifo_ptr->sed_fifo[fifo_ptr->block_count][i] = dat_ptr[i];
}
fifo_ptr->block_mark |= (uint32_t)(1U<<(fifo_ptr->block_count));
if ( ++fifo_ptr->block_count == TX_FIFO_NUMBER_BLOCK )
fifo_ptr->block_count = 0U;
return true;
}
static inline bool bsp_recive_data_copy_to_fifo( uint8_t *dat_ptr, sRx_Buf_TypeDef *fifo_ptr )
{
if ( NULL == dat_ptr || NULL == fifo_ptr )
return false;
for ( uint8_t i = 0U; i < RX_FIFO_NUMBER_BYTE; i++ )
{
fifo_ptr->rec_fifo[fifo_ptr->block_count][i] = dat_ptr[i];
}
fifo_ptr->block_mark |= (uint32_t)(1U<<(fifo_ptr->block_count));
if ( ++fifo_ptr->block_count == RX_FIFO_NUMBER_BLOCK )
fifo_ptr->block_count = 0U;
return true;
}
static inline uint32_t get_send_data_backup_fifo_mark( sTx_BackUp_Buf_TypeDef *fifo_ptr )
{
if ( NULL == fifo_ptr )
return 0U;
return (fifo_ptr->block_mark);
}
static inline uint32_t get_received_data_fifo_mark( sRx_Buf_TypeDef *fifo_ptr )
{
if ( NULL == fifo_ptr )
return 0U;
return (fifo_ptr->block_mark);
}
static inline uint8_t *get_send_data_backup_fifo_block( sTx_BackUp_Buf_TypeDef *fifo_ptr, uint8_t block_number )
{
if ( NULL == fifo_ptr || block_number > TX_FIFO_NUMBER_BLOCK )
return NULL;
return ( fifo_ptr->sed_fifo[block_number] );
}
static inline uint8_t *get_received_data_fifo_block( sRx_Buf_TypeDef *fifo_ptr, uint8_t block_number )
{
if ( NULL == fifo_ptr || block_number > RX_FIFO_NUMBER_BLOCK )
return NULL;
return ( fifo_ptr->rec_fifo[block_number] );
}
static inline bool clear_send_data_backup_fifo_mark( sTx_BackUp_Buf_TypeDef *fifo_ptr, uint8_t block_number )
{
if ( NULL == fifo_ptr || block_number > TX_FIFO_NUMBER_BLOCK )
return false;
fifo_ptr->block_mark &= ~(uint32_t)(1U<<block_number);
memset(fifo_ptr->sed_fifo[block_number], 0U, TX_FIFO_NUMBER_BYTE );
return true;
}
static inline bool clear_received_data_fifo_mark( sRx_Buf_TypeDef *fifo_ptr, uint8_t block_number )
{
if ( NULL == fifo_ptr || block_number > RX_FIFO_NUMBER_BLOCK )
return false;
fifo_ptr->block_mark &= ~(uint32_t)(1U<<block_number);
memset(fifo_ptr->rec_fifo[block_number], 0U, RX_FIFO_NUMBER_BYTE );
return true;
}
static sRx_Buf_TypeDef usart1_fifo = {0};
static sRx_Buf_TypeDef usart3_fifo = {0};
static sRx_Buf_TypeDef usart6_fifo = {0};
static sTx_BackUp_Buf_TypeDef usart1_tx_backup_fifo = {0};
static sTx_BackUp_Buf_TypeDef usart3_tx_backup_fifo = {0};
sRx_Buf_TypeDef *get_usart_fifo_address( uint8_t usart_num )
{
if ( usart_num > 5U || usart_num < 3U )
return NULL;
return (usart_fifo_ptr[usart_num-3U]);
}
sTx_BackUp_Buf_TypeDef *get_usart_backup_fifo_address( uint8_t usart_num )
{
if ( usart_num != 3U && usart_num != 4U )
return NULL;
return (usart_num == 3U ? &usart3_tx_backup_fifo : &usart1_tx_backup_fifo);
}
void HAL_UART_RxCpltCallback( UART_HandleTypeDef *huart )
{
if ( huart == &huart1 )
{
bsp_recive_data_copy_to_fifo( rec_buf, &usart1_fifo );
}
else if ( huart == &huart3 )
{
bsp_recive_data_copy_to_fifo( rec_buf, &usart3_fifo );
}
else if ( huart == &huart6 )
{
bsp_recive_data_copy_to_fifo( rec_buf, &usart6_fifo );
}
while ( HAL_UART_Receive_IT(huart, rec_buf, REC_BUFF_LENGTH ) == HAL_OK );
}
void send_pedal_the_mail( UINT8 type, UINT8 func, UINT8 *param, UINT8 param_number )
{
if ( USER_DATA_BYTE_LENGTH < param_number)
return ;
UINT8 i, send_data[8] = {0};
UINT32 sum = 0;
sTx_BackUp_Buf_TypeDef *fifo_ptr = NULL;
if ( ++pedal_send_msg_count == 0xFFFF )
{
pedal_send_msg_count = 0;
}
send_data[0] |= pedal_send_msg_count;
send_data[1] |= pedal_send_msg_count>>8U;
send_data[2] = type;
send_data[3] = func;
if ( NULL != param && 0U != param_number )
{
for ( i = 0; i < param_number; i++ )
send_data[i+4] = param[i];
}
for ( i = 0; i < 7; i++ )
{
sum += send_data[i];
}
send_data[7] |= sum;
fifo_ptr = get_usart_backup_fifo_address( (UINT8)TO_PEDAL_MSG );
if ( fifo_ptr != NULL )
bsp_send_data_copy_to_fifo( send_data, fifo_ptr );
if ( true != bsp_usart_transmit_func( TO_PEDAL_MSG, send_data ))
{
HAL_Delay(5);
bsp_usart_transmit_func( TO_PEDAL_MSG, send_data );
}
}
void polling_each_port_message( void )
{
sRx_Buf_TypeDef *fifo_ptr = NULL;
fifo_ptr = get_can_fifo_address( (UINT8)FROM_HANDLE_MSG );
if ( fifo_ptr != NULL )
parsing_received_messages( fifo_ptr, (UINT8)FROM_DISPLAY_MSG );
fifo_ptr = get_usart_fifo_address( (UINT8)FROM_PEDAL_MSG );
}
static void parsing_received_messages( sRx_Buf_TypeDef *fifo_ptr , UINT8 port )
{
UINT32 received_messages_mark;
if ( 0U == (received_messages_mark = get_received_data_fifo_mark( fifo_ptr )) )
return;
UINT8 bit = 0, *messages_ptr = NULL;
do
{
if ( ( received_messages_mark>>bit )&( (uint32_t)1U ) )
{
messages_ptr = get_received_data_fifo_block( fifo_ptr , bit );
if ( NULL == messages_ptr )
continue;
classification_parsing_messages( messages_ptr, port );
if ( true != clear_received_data_fifo_mark( fifo_ptr , bit ) )
continue;
}
}while( ++bit < RX_FIFO_NUMBER_BLOCK );
}
网友评论