|
void BSP_UART_Init(BUFF_TcbType* pbcb);
void BSP_UART_RxStart(UART_HandleTypeDef* huart, BUFF_TcbType* pbcb);
void BSP_UART_Getchars(UART_HandleTypeDef* huart, BUFF_TcbType* pbcb);
void BSP_UART_FlushRxbuff(UART_HandleTypeDef* huart, BUFF_TcbType* pbcb);
void BSP_UART_Putchars(UART_HandleTypeDef *huart, BUFF_TcbType* pbcb, uint8_t* pd, uint8_t size);
#define USART_FPGA USART1
#define USART_PC USART3
/* Description :
Initialize the BUFF_TcbType structure
*/
void BSP_UART_Init(BUFF_TcbType* pbcb)
{
pbcb->pr = pbcb->rxbuff;
pbcb->pti = pbcb->txbuff;
pbcb->pto = pbcb->txbuff;
pbcb->rxcount = 0;
pbcb->txcount = 0;
pbcb->hal_txcount = 0;
}
/* Description:
main routine calls this function to launch UART data Recieving (Enable the RXNE INT),
and the receive loop between bsp layer and hal layer will keep on going.
*/
void BSP_UART_RxStart(UART_HandleTypeDef* huart, BUFF_TcbType* pbcb)
{
HAL_UART_Receive_IT(huart, pbcb->hal_rxbuff,HAL_RX_BUFF_SIZE);
}
/* Description:
Obtain or move the received datas from HAL buffer to BSP buffer
@ *ps , pointer of source
@ *pd , pointer of destination
*/
void BSP_UART_Getchars(UART_HandleTypeDef* huart, BUFF_TcbType* pbcb)
{ /*main routine move data from hal layer to bsp layer periodically */
/*HAL_UART_RxCpltCallback(huart) wouldn't be called if the main routine works correctlly*/
/*the main program must be blocked if this funcition was called*/
if(huart->RxXferCount)
{
/*transfer data from HAL to BSP*/
*pbcb->pr=*huart->pRxBuffOutPtr;
/*update BSP layer data structure*/
pbcb->rxcount++;
pbcb->pr++;
/*update HAL layer structure*/
__disable_irq();
huart->RxXferCount--;
__enable_irq();
huart->pRxBuffOutPtr++;
/*update HAL layer data structure*/
if(huart->pRxBuffOutPtr == &pbcb->hal_rxbuff[HAL_RX_BUFF_SIZE])
{
huart->pRxBuffOutPtr = &pbcb->hal_rxbuff[0];
}
}
}
/* Description:
This function nerve be called unless overflow occured or main rountine was blocked
and didn't obtain data from rx bsp buffer in time.
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
//BSP_Debug();
}
/* Description:
Flush the RX buffer of bsp layer after the received msg was accepted
*/
void BSP_UART_FlushRxbuff( UART_HandleTypeDef *huart,BUFF_TcbType* pbcb)
{
/*reload control information for buff of BSP layer*/
uint8_t i;
__disable_irq();
for(i = 0; i < pbcb->rxcount; i++)
{
pbcb->rxbuff[i] = 0;
}
pbcb->rxcount = 0;
pbcb->pr = pbcb->rxbuff;
__enable_irq();
}
/* Description:
While datas in the hal layer were transmitted , this function will be called automatically
by HW system, we shall load datas from bsp layer to hal layer in this function , and launch
the next transmition, the Tx loop between bsp layer and hal layer will keep on going.
*/
void HAL_UART_TxCpltCallbackExt(UART_HandleTypeDef *huart, BUFF_TcbType* pbcb)
{
/*channel uart to pc tx completed*/
int8_t tmp ,i ,idx;
int16_t d;
if(huart->Instance == USART_PC)
{
idx=0; /*subscript*/
}
if(huart->Instance == USART_FPGA)
{
idx=1;
}
/*step 1: check if all the datas in bsp tx buff were transmitted */
if(pbcb->txcount > 0)
{
/*step 2: check how many datas are in the tx buff*/
if(pbcb->txcount > HAL_TX_BUFF_SIZE)
{
tmp = HAL_TX_BUFF_SIZE;
}
else
{
tmp = pbcb->txcount;
}
/*step 3: remove data from bsp layer to hal layer */
for(i = 0; i < tmp; i++)
{
/*refresh pbcb */
pbcb->hal_txbuff[i] = *pbcb->pto++;
pbcb->txcount--;
pbcb->hal_txcount++;
/*ring buffer ,pointer move to head*/
if( pbcb->pto == (pbcb->txbuff + BSP_TX_BUFF_SIZE))
{
pbcb->pto = pbcb->txbuff;
}
}
if(huart->Instance == USART_PC)
{
/*if it is 485 bus , set the dircetion to tx*/
__BSP_RS485_TXD();
}
/*step 4: arm to transimit bsp data*/
HAL_UART_Transmit_IT(huart, pbcb->hal_txbuff, pbcb->hal_txcount);
pbcb->hal_txcount = 0;
}
/*data in bsp buff has been tx*/
else
{
if(huart->Instance == USART_PC)
{
for(d = 5000; d >0; d--) /*hold on here until the datas on the bus were transmitted*/
;
__BSP_RS485_RXD();
}
}
}
/* Description
put chars into bsp layer buffer, if huart->State !=HAL_UART_STATE_BUSY_TX
launch transimition process ,then the loop between bsp layer and hal layer keep going
@ huart ,hal layer object
@ pbcb, bsp layer object
@ *pd, pointer to datas to be transfer
@ size, number of data to be transfer
*/
void BSP_UART_Putchars(UART_HandleTypeDef *huart, BUFF_TcbType* pbcb, uint8_t* pd, uint8_t size)
{
int8_t i,tmp;
/*step 1: put the chars into bsp buffer*/
__disable_irq();
for(i=0;i<size;i++)
{
*pbcb->pti++= *pd++;
pbcb->txcount++;
/*ring buffer ,pointer move to head from rear*/
if( pbcb->pti == (pbcb->txbuff + BSP_TX_BUFF_SIZE))
{
pbcb->pti = pbcb->txbuff;
}
}
__enable_irq();
/*step 2: if huart->state is free for tx, move data from bsp to hal and start a transfer*/
if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_RX))
{
if(pbcb->txcount > HAL_TX_BUFF_SIZE)
{
tmp = HAL_TX_BUFF_SIZE;
}
else
{
tmp = pbcb->txcount;
}
/*refresh pbcb */
for(i = 0; i < tmp; i++)
{
pbcb->hal_txbuff[i] = *pbcb->pto++;
pbcb->txcount--;
pbcb->hal_txcount++;
/*ring buffer ,pointer move to head*/
if( pbcb->pto == (pbcb->txbuff + BSP_TX_BUFF_SIZE))
{
pbcb->pto = pbcb->txbuff;
}
}
/*if it is 485 bus , set the dircetion to tx*/
if(huart->Instance ==USART_PC)
{
__BSP_RS485_TXD();
}
/*arm to transimit bsp data*/
HAL_UART_Transmit_IT(huart, pbcb->hal_txbuff, pbcb->hal_txcount);
pbcb->hal_txcount = 0;
}
/*if huart->state is busy for tx ,do nothing*/
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|