最近在用一款silabs的cortex-m3 单片机 SiM3U167。是一款新出的单片机,资料比较少。官方提供了软件编程的api,但是给出的reference也只是使用doxygen制作出的文档,比较含糊。
SiM3U167
的uart中断,主要要有发送请求中断、发送完成中断、接收请求中断,以及一些其他的error中断。
这里把我的uart通信的一些实现方法记录如下:
1. guart0.c
UART配置,调用silabs的api:
-
void UART0_enter_default_mode_from_reset(void)
-
{
-
SI32_UART_A_set_rx_baudrate(SI32_UART_0, SystemPeripheralClock/2/115200 - 1);
-
SI32_UART_A_set_tx_baudrate(SI32_UART_0, SystemPeripheralClock/2/115200 - 1);
-
SI32_UART_A_select_rx_parity(SI32_UART_0, 3);
-
SI32_UART_A_select_tx_parity(SI32_UART_0, 3);
-
SI32_UART_A_enter_half_duplex_mode(SI32_UART_0);
-
SI32_UART_A_select_rx_fifo_threshold_1(SI32_UART_0);
-
SI32_UART_A_select_tx_fifo_threshold_for_request_to_4(SI32_UART_0);
-
SI32_UART_A_enable_rx(SI32_UART_0);
-
SI32_UART_A_enable_tx(SI32_UART_0);
-
SI32_UART_A_enable_tx_complete_interrupt(SI32_UART_0);
-
SI32_UART_A_enable_rx_data_request_interrupt(SI32_UART_0);
-
NVIC_ClearPendingIRQ(UART0_IRQn);
-
NVIC_EnableIRQ(UART0_IRQn);
-
}
2. myuart0.c
uart接收的操作。在接收请求中断中,处理uart接收,设计一个定时器,以处理超时。
-
static uint16_t data_count = 0;
-
static uint16_t fifo_count = 0;
-
static uint16_t data_length = 0;
-
-
-
void UART0_tx_complete_handler(void)
-
{
-
SI32_UART_A_clear_tx_complete_interrupt(SI32_UART_0);
-
}
-
-
//这里判断是否接收,有二个指标:数据长度,定时器超时
-
void UART0_rx_data_req_handler(void)
-
{
-
SI32_TIMER_A_stop_high_timer(SI32_TIMER_0); //定时器关
-
-
SI32_UART_A_clear_rx_data_request_interrupt(SI32_UART_0);
-
-
fifo_count = 0;
-
fifo_count = SI32_UART_A_read_rx_fifo_count (SI32_UART_0); // 读fifo count
-
while(fifo_count--)
-
{
-
UART0_RX_Buf[data_count++] = SI32_UART_A_read_data_u8 (SI32_UART_0);
-
if (data_count==12) // data_count++后,第11、12个字节是长度计数
-
{
-
data_length = ((data_length | UART0_RX_Buf[10])<<8) | UART0_RX_Buf[11]; //组合成16位的数
-
}
-
}
-
-
// data_length + 14 是数据总长度,如果读完
-
if (data_count >= data_length + 14)
-
{
-
data_count = 0;
-
data_length = 0;
-
UART0_RX_END_FLAG = 1;
-
} else {
-
SI32_TIMER_A_start_high_timer(SI32_TIMER_0);//定时器开,500ms超时,如果未再进入此中断,则说明接收结束,在外部判断是否超时。
-
}
-
}
3. mycmd.c
uart发送操作,自行控制发送。
-
void myUART_send_char(uint8_t uart_no, uint8_t val)
-
{
-
switch (uart_no)
-
{
-
case UART_0:
-
while (SI32_UART_A_read_tx_fifo_count(SI32_UART_0) >= 4);
-
SI32_UART_A_write_data_u8(SI32_UART_0, val);
-
break;
-
case UART_1:
-
while (SI32_UART_A_read_tx_fifo_count(SI32_UART_1) >= 4);
-
SI32_UART_A_write_data_u8(SI32_UART_1, val);
-
break;
-
}
-
}
-
-
uint8_t TX_Data(uint8_t uart_port, uint8_t *uart_buf)
-
{
-
uint16_t i = 0, len = 0;
-
len = (((len | uart_buf[10])<<8) | uart_buf[11]) + 14;
-
-
for (i=0; i<len; i++) {
-
myUART_send_char(uart_port, uart_buf[i]);
-
}
-
-
return (TX_END);
-
}
以上,能够实现基本的收发,如有更进一步的需求,可在进行进一步改进。
阅读(3016) | 评论(0) | 转发(0) |