rti_uart_sample.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * File : rti_uart_sample.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-06-05 flybreak first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "rti.h"
  27. #ifdef RT_USING_FINSH
  28. #include <finsh.h>
  29. #endif
  30. #ifndef PKG_RTI_UART_BAUD_RATE
  31. #define PKG_RTI_UART_BAUD_RATE BAUD_RATE_460800
  32. #endif
  33. #define TRANSIT_BUF_SIZE (PKG_RTI_BUFFER_SIZE / 8)
  34. #define RTI_UART_BAUD_RATE (PKG_RTI_UART_BAUD_RATE)
  35. #define RTI_UART_NAME "rti_uart"
  36. static rt_uint8_t transit_buf[TRANSIT_BUF_SIZE];
  37. static rt_device_t rti_dev;
  38. #define REV_START_FIRSE_FARAM_BYTE0 0x53
  39. #define REV_START_FIRSE_FARAM_BYTE1 0x56
  40. #define REV_START_SECOND_FARAM_BYTE 0x01
  41. #define REV_STOP_FARAM_BYTE 0x02
  42. #define REV_START_FIRST_FARAM_SIZE 4
  43. static struct rt_semaphore rx_sem;
  44. static char rti_rx_stack[1024];
  45. static struct rt_thread rti_rx_th;
  46. void rti_data_new_data_notify(void)
  47. {
  48. int ret = rti_data_get(transit_buf, TRANSIT_BUF_SIZE);
  49. rt_device_write(rti_dev, 0, transit_buf, ret);
  50. }
  51. rt_err_t rt_ind(rt_device_t dev, rt_size_t size)
  52. {
  53. //rti_data_new_data_notify_set_hook(rti_data_new_data_notify);
  54. //rti_start();
  55. rt_sem_release(&rx_sem);
  56. return 0;
  57. }
  58. static void rti_rx_entry(void *param)
  59. {
  60. char ch;
  61. rt_uint8_t rx_cnt = 0;
  62. rt_uint8_t rx_buff[REV_START_FIRST_FARAM_SIZE];
  63. while(1)
  64. {
  65. while (rt_device_read(rti_dev, -1, &ch, 1) != 1)
  66. {
  67. rx_cnt = 0;
  68. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  69. }
  70. //rt_kprintf("%02X ",ch);
  71. rx_buff[rx_cnt++] = ch;
  72. if(rx_cnt==REV_START_FIRST_FARAM_SIZE)
  73. {
  74. if(rx_buff[0] == REV_START_FIRSE_FARAM_BYTE0 && rx_buff[1] == REV_START_FIRSE_FARAM_BYTE1)
  75. {
  76. rti_data_new_data_notify_set_hook(rti_data_new_data_notify);
  77. rti_start();
  78. }
  79. rx_cnt = 0;
  80. }
  81. if(ch == REV_START_SECOND_FARAM_BYTE)
  82. {
  83. rti_data_new_data_notify_set_hook(rti_data_new_data_notify);
  84. rti_start();
  85. }
  86. if(ch == REV_STOP_FARAM_BYTE)
  87. {
  88. rti_stop();
  89. rti_data_new_data_notify_set_hook(NULL);
  90. }
  91. }
  92. }
  93. void rti_uart_sample(void)
  94. {
  95. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  96. rt_sem_init(&rx_sem, "rti_sem", 0, RT_IPC_FLAG_FIFO);
  97. rti_dev = rt_console_get_device();
  98. #ifdef RT_USING_FINSH
  99. finsh_set_device(RTI_UART_NAME);
  100. #endif
  101. rt_console_set_device(RTI_UART_NAME);
  102. rti_dev->open_flag &= ~RT_DEVICE_FLAG_STREAM;
  103. config.baud_rate = RTI_UART_BAUD_RATE;
  104. rt_device_control(rti_dev, RT_DEVICE_CTRL_CONFIG, &config);
  105. rt_device_set_rx_indicate(rti_dev, rt_ind);
  106. rt_device_open(rti_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  107. rt_thread_init(&rti_rx_th,"rti_rx",rti_rx_entry,RT_NULL,&rti_rx_stack[0],sizeof(rti_rx_stack),20 - 1, 20);
  108. rt_thread_startup(&rti_rx_th);
  109. }
  110. #ifdef RT_USING_FINSH
  111. MSH_CMD_EXPORT(rti_uart_sample, start record by uart.);
  112. #endif
  113. /*
  114. * rti uart device
  115. */
  116. #define RTI_UART_BUF (128)
  117. static char uart_buf[RTI_UART_BUF];
  118. static struct rt_serial_device rti_serial;
  119. static rt_err_t rti_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  120. {
  121. return RT_EOK;
  122. }
  123. static rt_err_t rti_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  124. {
  125. return RT_EOK;
  126. }
  127. static int rti_uart_putc(struct rt_serial_device *serial, char c)
  128. {
  129. static int index = 0;
  130. uart_buf[index++] = c;
  131. if (c == '\n' || index == RTI_UART_BUF - 1)
  132. {
  133. uart_buf[index] = '\0';
  134. rti_print(uart_buf);
  135. index = 0;
  136. }
  137. return 1;
  138. }
  139. static int rti_uart_getc(struct rt_serial_device *serial)
  140. {
  141. return -1;
  142. }
  143. static const struct rt_uart_ops rti_uart_ops =
  144. {
  145. .configure = rti_uart_configure,
  146. .control = rti_uart_control,
  147. .putc = rti_uart_putc,
  148. .getc = rti_uart_getc,
  149. .dma_transmit = RT_NULL
  150. };
  151. static int rti_uart_init(void)
  152. {
  153. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  154. rt_err_t result = 0;
  155. /* init rti_uart object */
  156. rti_serial.ops = &rti_uart_ops;
  157. rti_serial.config = config;
  158. /* register rti_uart device */
  159. result = rt_hw_serial_register(&rti_serial, RTI_UART_NAME,
  160. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  161. NULL);
  162. return result;
  163. }
  164. INIT_APP_EXPORT(rti_uart_init);