Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225194
  • 博文数量: 14
  • 博客积分: 167
  • 博客等级: 入伍新兵
  • 技术积分: 274
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-29 13:28
文章分类

全部博文(14)

文章存档

2014年(5)

2013年(4)

2012年(5)

我的朋友

分类: C/C++

2014-01-02 08:15:20

最近在用一款silabs的cortex-m3 单片机 SiM3U167。是一款新出的单片机,资料比较少。官方提供了软件编程的api,但是给出的reference也只是使用doxygen制作出的文档,比较含糊。

SiM3U167的uart中断,主要要有发送请求中断、发送完成中断、接收请求中断,以及一些其他的error中断。

这里把我的uart通信的一些实现方法记录如下:

1. guart0.c
UART配置,调用silabs的api:

点击(此处)折叠或打开

  1. void UART0_enter_default_mode_from_reset(void)
  2. {
  3.   SI32_UART_A_set_rx_baudrate(SI32_UART_0, SystemPeripheralClock/2/115200 - 1);
  4.   SI32_UART_A_set_tx_baudrate(SI32_UART_0, SystemPeripheralClock/2/115200 - 1);
  5.   SI32_UART_A_select_rx_parity(SI32_UART_0, 3);
  6.   SI32_UART_A_select_tx_parity(SI32_UART_0, 3);
  7.   SI32_UART_A_enter_half_duplex_mode(SI32_UART_0);
  8.   SI32_UART_A_select_rx_fifo_threshold_1(SI32_UART_0);
  9.   SI32_UART_A_select_tx_fifo_threshold_for_request_to_4(SI32_UART_0);
  10.   SI32_UART_A_enable_rx(SI32_UART_0);
  11.   SI32_UART_A_enable_tx(SI32_UART_0);
  12.   SI32_UART_A_enable_tx_complete_interrupt(SI32_UART_0);
  13.   SI32_UART_A_enable_rx_data_request_interrupt(SI32_UART_0);
  14.   NVIC_ClearPendingIRQ(UART0_IRQn);
  15.   NVIC_EnableIRQ(UART0_IRQn);
  16. }

2. myuart0.c
uart接收的操作。在接收请求中断中,处理uart接收,设计一个定时器,以处理超时。

点击(此处)折叠或打开

  1. static uint16_t data_count = 0;
  2. static uint16_t fifo_count = 0;
  3. static uint16_t data_length = 0;


  4. void UART0_tx_complete_handler(void)
  5. {
  6.     SI32_UART_A_clear_tx_complete_interrupt(SI32_UART_0);
  7. }

  8. //这里判断是否接收,有二个指标:数据长度,定时器超时
  9. void UART0_rx_data_req_handler(void)
  10. {
  11.     SI32_TIMER_A_stop_high_timer(SI32_TIMER_0); //定时器关
  12.     
  13.     SI32_UART_A_clear_rx_data_request_interrupt(SI32_UART_0);
  14.     
  15.     fifo_count = 0;
  16.     fifo_count = SI32_UART_A_read_rx_fifo_count (SI32_UART_0); // 读fifo count
  17.     while(fifo_count--)
  18.     {
  19.         UART0_RX_Buf[data_count++] = SI32_UART_A_read_data_u8 (SI32_UART_0);
  20.         if (data_count==12) // data_count++后,第11、12个字节是长度计数
  21.         {
  22.             data_length = ((data_length | UART0_RX_Buf[10])<<8) | UART0_RX_Buf[11]; //组合成16位的数
  23.         }
  24.     }
  25.     
  26.     // data_length + 14 是数据总长度,如果读完
  27.     if (data_count >= data_length + 14)
  28.     {
  29.         data_count = 0;
  30.         data_length = 0;
  31.         UART0_RX_END_FLAG = 1;
  32.     } else {
  33.         SI32_TIMER_A_start_high_timer(SI32_TIMER_0);//定时器开,500ms超时,如果未再进入此中断,则说明接收结束,在外部判断是否超时。
  34.     }
  35. }

3. mycmd.c
uart发送操作,自行控制发送。

点击(此处)折叠或打开

  1. void myUART_send_char(uint8_t uart_no, uint8_t val)
  2. {
  3.     switch (uart_no)
  4.     {
  5.     case UART_0:
  6.         while (SI32_UART_A_read_tx_fifo_count(SI32_UART_0) >= 4);
  7.         SI32_UART_A_write_data_u8(SI32_UART_0, val);
  8.         break;
  9.     case UART_1:
  10.         while (SI32_UART_A_read_tx_fifo_count(SI32_UART_1) >= 4);
  11.         SI32_UART_A_write_data_u8(SI32_UART_1, val);
  12.         break;
  13.     }
  14. }

  15. uint8_t TX_Data(uint8_t uart_port, uint8_t *uart_buf)
  16. {
  17.     uint16_t i = 0, len = 0;
  18.     len = (((len | uart_buf[10])<<8) | uart_buf[11]) + 14;
  19.     
  20.     for (i=0; i<len; i++) {
  21.         myUART_send_char(uart_port, uart_buf[i]);
  22.     }
  23.     
  24.     return (TX_END);
  25. }
以上,能够实现基本的收发,如有更进一步的需求,可在进行进一步改进。



阅读(2941) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~