2019-07-21 00:39发布
nashui_sx 发表于 2019-5-28 09:26 看你好像是中断一股脑接收,一次读取全部512处理,肯定有问题呀 既然有包头,读取两个包头之间的处理 ...
最多设置5个标签!
[mw_shl_code=c,true]int UART_Read(int dev, unsigned char *buf, int size )
{
int r_len;
if( dev < 0 || dev >= UART_MAX ) return -1;
if ( size <= 0 ) return 0;
r_len = 0;
while(uart_dev[dev].recv.Rd != uart_dev[dev].recv.Wr )
{
*buf = uart_dev[dev].recv.buf[uart_dev[dev].recv.Rd];
uart_dev[dev].recv.Rd = (uart_dev[dev].recv.Rd+1) % BUFFER_LEN;
buf++;
if( ++r_len >= size ) return r_len;
}
return r_len;
}
int UART_Write( int dev, unsigned char *buf, int size )
{
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr = 0;
#endif
int w_len;
if( dev < 0 ||dev >= UART_MAX ) return -1;
if ( size <= 0 ) return 0;
w_len = 0;
while(uart_dev[dev].send.Rd != ((uart_dev[dev].send.Wr+1)%BUFFER_LEN))
{
uart_dev[dev].send.buf[uart_dev[dev].send.Wr] = *buf;
uart_dev[dev].send.Wr = ( uart_dev[dev].send.Wr + 1 )%BUFFER_LEN;
w_len++;
if ( w_len >= size ) break;
buf++;
};
#if 0
if(DEV_UART3 == dev)
{
printf("^^^%d,%d^^^ ",uart_dev[dev].send.Rd,uart_dev[dev].send.Wr);
}
#endif
OS_ENTER_CRITICAL();
if ( uart_dev[dev].send.State == SEND_IDLE )
{
OS_EXIT_CRITICAL();
uart_dev[dev].send.State = SENDING;
//(*((volatile unsigned char *) uart_dev[dev].send.reg)) = uart_dev[dev].send.buf[uart_dev[dev].send.Rd];
switch(dev)
{
case DEV_UART1:
USART_SendData(USART1,uart_dev[dev].send.buf[uart_dev[dev].send.Rd]);
#if 1
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
#endif
break;
case DEV_UART2:
USART_SendData(USART2,uart_dev[dev].send.buf[uart_dev[dev].send.Rd]);
#if 1
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
#endif
break;
}
return w_len;
}
OS_EXIT_CRITICAL();
return w_len;
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//接收数据
//uart_dev[DEV_UART1].recv.buf[uart_dev[DEV_UART1].recv.Wr] = U1RBR;
uart_dev[DEV_UART1].recv.buf[uart_dev[DEV_UART1].recv.Wr] = USART_ReceiveData(USART1);
uart_dev[DEV_UART1].recv.Wr = (uart_dev[DEV_UART1].recv.Wr +1)%BUFFER_LEN;
if(uart_dev[DEV_UART1].recv.Wr == uart_dev[DEV_UART1].recv.Rd ) {
uart_dev[DEV_UART1].recv.Rd = (uart_dev[DEV_UART1].recv.Rd +1)%BUFFER_LEN;
}
//USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
#if 1
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET){
#else
if(USART_GetITStatus(USART1, USART_IT_TC) != RESET){
USART_ClearITPendingBit(USART1, USART_IT_TC);
#endif
//发送数据
if(uart_dev[DEV_UART1].send.State == SENDING){
uart_dev[DEV_UART1].send.Rd = (uart_dev[DEV_UART1].send.Rd +1)%BUFFER_LEN;
if ( uart_dev[DEV_UART1].send.Rd == uart_dev[DEV_UART1].send.Wr) {
uart_dev[DEV_UART1].send.State = SEND_IDLE;
} else {
//U1THR = uart_dev[DEV_UART1].send.buf[uart_dev[DEV_UART1].send.Rd];
USART_SendData(USART1, uart_dev[DEV_UART1].send.buf[uart_dev[DEV_UART1].send.Rd]);
}
}
#if 1
else{
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
#endif
}
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
//接收数据
//uart_dev[DEV_UART1].recv.buf[uart_dev[DEV_UART1].recv.Wr] = U1RBR;
uart_dev[DEV_UART2].recv.buf[uart_dev[DEV_UART2].recv.Wr] = USART_ReceiveData(USART2);
uart_dev[DEV_UART2].recv.Wr = (uart_dev[DEV_UART2].recv.Wr +1)%BUFFER_LEN;
if(uart_dev[DEV_UART2].recv.Wr == uart_dev[DEV_UART2].recv.Rd ) {
uart_dev[DEV_UART2].recv.Rd = (uart_dev[DEV_UART2].recv.Rd +1)%BUFFER_LEN;
}
//USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
#if 1
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET){
#else
if(USART_GetITStatus(USART1, USART_IT_TC) != RESET){
USART_ClearITPendingBit(USART1, USART_IT_TC);
#endif
//发送数据
if(uart_dev[DEV_UART2].send.State == SENDING){
uart_dev[DEV_UART2].send.Rd = (uart_dev[DEV_UART2].send.Rd +1)%BUFFER_LEN;
if ( uart_dev[DEV_UART2].send.Rd == uart_dev[DEV_UART2].send.Wr) {
uart_dev[DEV_UART2].send.State = SEND_IDLE;
} else {
//U1THR = uart_dev[DEV_UART1].send.buf[uart_dev[DEV_UART1].send.Rd];
USART_SendData(USART2, uart_dev[DEV_UART2].send.buf[uart_dev[DEV_UART2].send.Rd]);
}
}
#if 1
else{
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
#endif
}
[/mw_shl_code]
一周热门 更多>