btstack_uart_rtthread.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2016 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "btstack_uart_rtthread.c"
  38. /*
  39. * btstack_uart_rtthread.c
  40. *
  41. * Common code to access serial port via asynchronous block read/write commands
  42. *
  43. */
  44. #include "btstack_uart.h"
  45. #include "btstack_run_loop.h"
  46. #include "btstack_debug.h"
  47. #include "btstack_run_loop_rtthread.h"
  48. #include <rtthread.h>
  49. #include <rtdevice.h>
  50. enum {
  51. CONFIG_PARITY = 0,
  52. CONFIG_BAUDRATE,
  53. CONFIG_FLOWCONTROL
  54. };
  55. // btstack uart config
  56. static const btstack_uart_config_t * uart_config;
  57. // data source for integration with BTstack Runloop
  58. static btstack_data_source_t write_data_source;
  59. static btstack_data_source_t read_data_source;
  60. // rtthread uart config
  61. static struct serial_configure rt_uart_config = RT_SERIAL_CONFIG_DEFAULT;
  62. // block write
  63. static int btstack_uart_block_write_bytes_len;
  64. static const uint8_t * btstack_uart_block_write_bytes_data;
  65. static int btstack_uart_block_writing;
  66. // block read
  67. static uint16_t btstack_uart_block_read_bytes_len;
  68. static uint8_t * btstack_uart_block_read_bytes_data;
  69. static int btstack_uart_block_reading;
  70. // callbacks
  71. static void (*block_sent)(void);
  72. static void (*block_received)(void);
  73. static void uart_write_poll_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type);
  74. static void uart_read_poll_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type);
  75. static int btstack_uart_rtthread_init(const btstack_uart_config_t * config) {
  76. uart_config = config;
  77. return 0;
  78. }
  79. static void btstack_uart_rtthread_set_block_received( void (*block_handler)(void)){
  80. block_received = block_handler;
  81. }
  82. static void btstack_uart_rtthread_set_block_sent( void (*block_handler)(void)){
  83. block_sent = block_handler;
  84. }
  85. static void btstack_uart_rtthread_set_parity_option(const int parity) {
  86. switch (parity) {
  87. case BTSTACK_UART_PARITY_OFF:
  88. rt_uart_config.parity = PARITY_NONE;
  89. break;
  90. case BTSTACK_UART_PARITY_EVEN:
  91. rt_uart_config.parity = PARITY_EVEN;
  92. break;
  93. case BTSTACK_UART_PARITY_ODD:
  94. rt_uart_config.parity = PARITY_ODD;
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. static int btstack_uart_rtthread_set_baudrate_option(const uint32_t baudrate) {
  101. switch (baudrate) {
  102. case 9600: rt_uart_config.baud_rate = BAUD_RATE_9600; break;
  103. case 19200: rt_uart_config.baud_rate = BAUD_RATE_19200; break;
  104. case 38400: rt_uart_config.baud_rate = BAUD_RATE_38400; break;
  105. case 57600: rt_uart_config.baud_rate = BAUD_RATE_57600; break;
  106. case 115200: rt_uart_config.baud_rate = BAUD_RATE_115200; break;
  107. case 230400: rt_uart_config.baud_rate = BAUD_RATE_230400; break;
  108. case 460800: rt_uart_config.baud_rate = BAUD_RATE_460800; break;
  109. case 921600: rt_uart_config.baud_rate = BAUD_RATE_921600; break;
  110. default:
  111. log_error("Baudrate %d is not support now", (int)baudrate);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static void btstack_uart_rtthread_set_flowcontrol_option(const int flowcontrol) {
  117. // Flowcontrol unsupported now
  118. UNUSED(flowcontrol);
  119. }
  120. static int btstack_uart_rtthread_set(int config, void *data)
  121. {
  122. int err = 0;
  123. switch (config) {
  124. case CONFIG_PARITY:
  125. btstack_uart_rtthread_set_parity_option(*(int *)data);
  126. break;
  127. case CONFIG_BAUDRATE:
  128. err = btstack_uart_rtthread_set_baudrate_option(*(uint32_t *)data);
  129. if (err)
  130. return -1;
  131. break;
  132. case CONFIG_FLOWCONTROL:
  133. btstack_uart_rtthread_set_flowcontrol_option(*(int *)data);
  134. break;
  135. default:
  136. return -1;
  137. }
  138. rt_device_t uart_device = write_data_source.source.handle;
  139. RT_ASSERT(uart_device != NULL);
  140. err = rt_device_control(uart_device, RT_DEVICE_CTRL_CONFIG, &rt_uart_config);
  141. if (err != RT_EOK) {
  142. log_error("Unable to configure device %s", uart_device->parent.name);
  143. return -1;
  144. }
  145. return 0;
  146. }
  147. static int btstack_uart_rtthread_open(void) {
  148. const char * device_name = uart_config->device_name;
  149. const uint32_t baudrate = uart_config->baudrate;
  150. const int flowcontrol = uart_config->flowcontrol;
  151. const int parity = uart_config->parity;
  152. rt_device_t uart_device = rt_device_find(device_name);
  153. if (uart_device == RT_NULL) {
  154. log_error("Unable to find device %s", device_name);
  155. return -1;
  156. }
  157. rt_err_t err = rt_device_open(uart_device, RT_DEVICE_FLAG_INT_RX);
  158. if (err != RT_EOK) {
  159. log_error("Unable to open device %s", device_name);
  160. return -1;
  161. }
  162. btstack_uart_rtthread_set_parity_option(parity);
  163. btstack_uart_rtthread_set_flowcontrol_option(flowcontrol);
  164. if (btstack_uart_rtthread_set_baudrate_option(baudrate) < 0) {
  165. return -1;
  166. }
  167. err = rt_device_control(uart_device, RT_DEVICE_CTRL_CONFIG, &rt_uart_config);
  168. if (err != RT_EOK) {
  169. log_error("Unable to configure device %s", device_name);
  170. return -1;
  171. }
  172. log_info("Open device %s success", device_name);
  173. // set up data_source
  174. btstack_run_loop_set_data_source_handle(&write_data_source, uart_device);
  175. btstack_run_loop_set_data_source_handler(&write_data_source, &uart_write_poll_process);
  176. btstack_run_loop_add_data_source(&write_data_source);
  177. btstack_run_loop_set_data_source_handle(&read_data_source, uart_device);
  178. btstack_run_loop_set_data_source_handler(&read_data_source, &uart_read_poll_process);
  179. btstack_run_loop_add_data_source(&read_data_source);
  180. // wait a bit - at least cheap FTDI232 clones might send the first byte out incorrectly
  181. rt_thread_mdelay(100);
  182. return 0;
  183. }
  184. static int btstack_uart_rtthread_close_new(void) {
  185. // first remove run loop handler
  186. btstack_run_loop_remove_data_source(&write_data_source);
  187. btstack_run_loop_remove_data_source(&read_data_source);
  188. // then close device
  189. rt_device_close((rt_device_t)write_data_source.source.handle);
  190. write_data_source.source.handle = NULL;
  191. read_data_source.source.handle = NULL;
  192. return 0;
  193. }
  194. static int btstack_uart_rtthread_set_baudrate(uint32_t baudrate) {
  195. return btstack_uart_rtthread_set(CONFIG_BAUDRATE, &baudrate);
  196. }
  197. static int btstack_uart_rtthread_set_parity(int parity) {
  198. return btstack_uart_rtthread_set(CONFIG_PARITY, &parity);
  199. }
  200. static int btstack_uart_rtthread_set_flowcontrol(int flowcontrol) {
  201. return btstack_uart_rtthread_set(CONFIG_FLOWCONTROL, &flowcontrol);
  202. }
  203. static void btstack_uart_rtthread_send_block(const uint8_t *data, uint16_t size){
  204. // setup async write
  205. btstack_uart_block_write_bytes_data = data;
  206. btstack_uart_block_write_bytes_len = size;
  207. btstack_uart_block_writing = 1;
  208. btstack_run_loop_enable_data_source_callbacks(&write_data_source, DATA_SOURCE_CALLBACK_POLL);
  209. btstack_run_loop_rtthread_trigger();
  210. }
  211. static void btstack_uart_rtthread_receive_block(uint8_t *buffer, uint16_t len){
  212. btstack_uart_block_read_bytes_data = buffer;
  213. btstack_uart_block_read_bytes_len = len;
  214. btstack_uart_block_reading = 1;
  215. btstack_run_loop_enable_data_source_callbacks(&read_data_source, DATA_SOURCE_CALLBACK_POLL);
  216. btstack_run_loop_rtthread_trigger();
  217. }
  218. static void uart_write_poll_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type)
  219. {
  220. if (callback_type != DATA_SOURCE_CALLBACK_POLL)
  221. return ;
  222. rt_device_t uart_device = (rt_device_t) ds->source.handle;
  223. size_t bytes_written = rt_device_write(uart_device, 0, btstack_uart_block_write_bytes_data, btstack_uart_block_write_bytes_len);
  224. if (bytes_written == 0){
  225. log_error("wrote zero bytes\n");
  226. }
  227. btstack_uart_block_write_bytes_data += bytes_written;
  228. btstack_uart_block_write_bytes_len -= bytes_written;
  229. if (btstack_uart_block_write_bytes_len) {
  230. btstack_run_loop_rtthread_trigger();
  231. return ;
  232. }
  233. btstack_run_loop_disable_data_source_callbacks(ds, DATA_SOURCE_CALLBACK_POLL);
  234. // notify done
  235. if (block_sent) {
  236. block_sent();
  237. }
  238. }
  239. static void uart_read_poll_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type)
  240. {
  241. if (callback_type != DATA_SOURCE_CALLBACK_POLL)
  242. return ;
  243. if (btstack_uart_block_read_bytes_len == 0) {
  244. log_info("called but no read pending");
  245. btstack_run_loop_disable_data_source_callbacks(ds, DATA_SOURCE_CALLBACK_POLL);
  246. }
  247. rt_device_t uart_device = (rt_device_t) ds->source.handle;
  248. size_t bytes_read = rt_device_read(uart_device, 0, btstack_uart_block_read_bytes_data, btstack_uart_block_read_bytes_len);
  249. if (bytes_read == 0){
  250. // log_error("read zero bytes\n");
  251. }
  252. btstack_uart_block_read_bytes_len -= bytes_read;
  253. btstack_uart_block_read_bytes_data += bytes_read;
  254. if (btstack_uart_block_read_bytes_len > 0) {
  255. btstack_run_loop_rtthread_trigger();
  256. return;
  257. }
  258. btstack_run_loop_disable_data_source_callbacks(ds, DATA_SOURCE_CALLBACK_POLL);
  259. if (block_received) {
  260. block_received();
  261. }
  262. }
  263. static const btstack_uart_t btstack_uart_rtthread = {
  264. /* int (*init)(btstack_uart_config_t * config); */ &btstack_uart_rtthread_init,
  265. /* int (*open)(void); */ &btstack_uart_rtthread_open,
  266. /* int (*close)(void); */ &btstack_uart_rtthread_close_new,
  267. /* void (*set_block_received)(void (*handler)(void)); */ &btstack_uart_rtthread_set_block_received,
  268. /* void (*set_block_sent)(void (*handler)(void)); */ &btstack_uart_rtthread_set_block_sent,
  269. /* int (*set_baudrate)(uint32_t baudrate); */ &btstack_uart_rtthread_set_baudrate,
  270. /* int (*set_parity)(int parity); */ &btstack_uart_rtthread_set_parity,
  271. /* int (*set_flowcontrol)(int flowcontrol); */ &btstack_uart_rtthread_set_flowcontrol,
  272. /* void (*receive_block)(uint8_t *buffer, uint16_t len); */ &btstack_uart_rtthread_receive_block,
  273. /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &btstack_uart_rtthread_send_block,
  274. /* int (*get_supported_sleep_modes); */ NULL,
  275. /* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */ NULL,
  276. /* void (*set_wakeup_handler)(void (*handler)(void)); */ NULL,
  277. /* void (*set_frame_received)(void (*handler)(uint16_t frame_size); */ NULL,
  278. /* void (*set_fraae_sent)(void (*handler)(void)); */ NULL,
  279. /* void (*receive_frame)(uint8_t *buffer, uint16_t len); */ NULL,
  280. /* void (*send_frame)(const uint8_t *buffer, uint16_t length); */ NULL,
  281. };
  282. const btstack_uart_t * btstack_uart_rtthread_instance(void){
  283. return &btstack_uart_rtthread;
  284. }