读圣人书,明圣人志,遵圣人训。
分类: 嵌入式
2016-12-02 18:30:40
-------- email:
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size); |
HAL_UART_Receive_IT(&huart1, &pData, 1); |
uint8_t pData=0;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit(&huart1, &pData, 1, 0);
HAL_UART_Receive_IT(&huart1, &pData, 1);
}
|
void terminal1_data_process(uint8_t data)
{
HAL_UART_Transmit(&huart1, &data, 1, 1);
}
|
void USART1_IRQHandler(void)
{
terminal1_data_process(USART1->RDR & 0xFF);
HAL_UART_IRQHandler(&huart1);
}
|
void terminal1_data_process(uint8_t data)
{
macro_queue_write(data,cache1_terminal);
}
|
while (1)
{
if(0==cache_queue_read_byte(&cache1_terminal, &_byte))
{
HAL_UART_Transmit(&huart1, &_byte, 1, 1);
}
HAL_Delay(1);
}
|
#define macro_queue_write(_byte,_queue) do{ \
_queue.w_tmp = _queue.index_w+1; \
if(_queue.w_tmp>=sizeof(_queue.buf)) _queue.w_tmp=0; \
_queue.buf[_queue.w_tmp] = _byte; \
if(_queue.w_tmp != _queue.index_r) _queue.index_w=_queue.w_tmp; \
}while(0);
static __inline int cache_queue_read_byte(cache_queue* _cache, uint8_t* _byte)
{
int ret=-1;
_cache->r_tmp = _cache->index_r+1;
if(_cache->r_tmp>=sizeof(_cache->buf)) _cache->r_tmp=0;
if(_cache->r_tmp != _cache->index_w)
{
*_byte = _cache->buf[_cache->r_tmp];
_cache->index_r = _cache->r_tmp;
ret = 0;
}
return ret;
}
#define CACHE_QUEUE_LEN 256
typedef struct{
uint16_t index_r;
uint16_t index_w;
uint16_t w_tmp;
uint16_t r_tmp;
uint8_t buf[CACHE_QUEUE_LEN];
}cache_queue;
|