vbus.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef __VBUS_H__
  2. #define __VBUS_H__
  3. /*
  4. * VBus
  5. *
  6. * COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
  7. * http://www.rt-thread.com
  8. *
  9. * This file is part of RT-Thread (http://www.rt-thread.org)
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. *
  27. * Change Logs:
  28. * Date Author Notes
  29. * 2014-06-09 Grissiom version 2.0.2; add comment
  30. * 2015-01-06 Grissiom version 2.0.3; API change, no functional changes
  31. */
  32. #include <vbus_api.h>
  33. int rt_vbus_init(void *outr, void *inr);
  34. void rt_vbus_resume_out_thread(void);
  35. /** Post data on channel.
  36. *
  37. * @param chnr the channel number
  38. * @param prio the priority of the data
  39. * @param datap pointer to the actual data
  40. * @param size number of byte of the data
  41. * @param timeout the value used in the blocking API
  42. *
  43. * Note: rt_vbus_post is an asynchronous function that when it returns, the
  44. * @datap and @size is recorded in the post queue at least but there is no
  45. * guarantee that the data is copied into the ring buffer. To avoid data
  46. * corruption, you need to wait on the RT_VBUS_EVENT_ID_TX event.
  47. *
  48. * However, if you just post static data such as static string, there is no
  49. * need to wait.
  50. *
  51. * @sa rt_vbus_register_listener .
  52. */
  53. rt_err_t rt_vbus_post(rt_uint8_t chnr,
  54. rt_uint8_t prio,
  55. const void *datap,
  56. rt_size_t size,
  57. rt_int32_t timeout);
  58. struct rt_vbus_data {
  59. /* Number of bytes in current data package. */
  60. unsigned char size;
  61. /* Used internally in VBus. Don't modify this field as it may corrupt the
  62. * receive queue. */
  63. struct rt_vbus_data *next;
  64. /* Data follows the struct */
  65. };
  66. struct rt_vbus_wm_cfg {
  67. unsigned int low, high;
  68. };
  69. struct rt_vbus_request {
  70. unsigned char prio;
  71. const char *name;
  72. int is_server;
  73. struct rt_vbus_wm_cfg recv_wm, post_wm;
  74. };
  75. /** Request a channel.
  76. *
  77. * @return channel number. Negative if error happened.
  78. */
  79. int rt_vbus_request_chn(struct rt_vbus_request *req, int timeout);
  80. /** Close channel @chnr */
  81. void rt_vbus_close_chn(unsigned char chnr);
  82. /** Set the water mark level for posting into the channel @chnr. */
  83. void rt_vbus_set_post_wm(unsigned char chnr, unsigned int low, unsigned int high);
  84. /** Set the water mark level for receiving from the channel @chnr. */
  85. void rt_vbus_set_recv_wm(unsigned char chnr, unsigned int low, unsigned int high);
  86. typedef void (*rt_vbus_event_listener)(void *ctx);
  87. enum rt_vbus_event_id {
  88. /* On a packet received in channel. */
  89. RT_VBUS_EVENT_ID_RX,
  90. /* On the data of rt_vbus_post has been written to the ring buffer. */
  91. RT_VBUS_EVENT_ID_TX,
  92. /* On the channel has been closed. */
  93. RT_VBUS_EVENT_ID_DISCONN,
  94. RT_VBUS_EVENT_ID_MAX,
  95. };
  96. /** Register callback @indi on the event @eve on the @chnr.
  97. *
  98. * @ctx will passed to @indi on calling the @indi.
  99. */
  100. void rt_vbus_register_listener(unsigned char chnr,
  101. enum rt_vbus_event_id eve,
  102. rt_vbus_event_listener indi,
  103. void *ctx);
  104. /** Listen on any events happen on the @chnr for @timeout ticks.
  105. *
  106. * This function blocks until events occur or timeout happened.
  107. */
  108. rt_err_t rt_vbus_listen_on(rt_uint8_t chnr,
  109. rt_int32_t timeout);
  110. /** Push a data package into the receive queue of the channel @chnr. */
  111. void rt_vbus_data_push(unsigned int chnr,
  112. struct rt_vbus_data *data);
  113. /** Pop a data package from the receive queue of the channel @chnr.
  114. *
  115. * The actual data is following the struct rt_vbus_data. After using it, it
  116. * should be freed by rt_free.
  117. */
  118. struct rt_vbus_data* rt_vbus_data_pop(unsigned int chnr);
  119. struct rt_vbus_dev
  120. {
  121. /* Runtime infomations. */
  122. rt_uint8_t chnr;
  123. struct rt_vbus_data *act;
  124. rt_size_t pos;
  125. /* There will be a request for each channel. So no need to seperate them so
  126. * clearly. */
  127. struct rt_vbus_request req;
  128. };
  129. rt_err_t rt_vbus_chnx_init(void);
  130. /** Get the corresponding channel number from the VBus device @dev. */
  131. rt_uint8_t rt_vbus_get_chnnr(rt_device_t dev);
  132. /** Register a call back on the other side disconnect the channel.
  133. *
  134. * @sa rt_vbus_register_listener .
  135. */
  136. void rt_vbus_chnx_register_disconn(rt_device_t dev,
  137. rt_vbus_event_listener indi,
  138. void *ctx);
  139. /* Commands for the device control interface. */
  140. #define VBUS_IOCRECV_WM 0xD1
  141. #define VBUS_IOCPOST_WM 0xD2
  142. /** Configure event listener */
  143. #define VBUS_IOC_LISCFG 0xD3
  144. struct rt_vbus_dev_liscfg
  145. {
  146. enum rt_vbus_event_id event;
  147. rt_vbus_event_listener listener;
  148. void *ctx;
  149. };
  150. int rt_vbus_shell_start(void);
  151. #ifdef RT_USING_VBUS_RFS
  152. int dfs_rfs_init(void);
  153. #endif
  154. /** VBus hardware init function.
  155. *
  156. * BSP should implement this function to initialize the interrupts etc.
  157. */
  158. int rt_vbus_hw_init(void);
  159. /** VBus ISR function.
  160. *
  161. * BSP should call this function when the interrupt from other core is
  162. * triggered. @param is not used by VBus and will pass to rt_vbus_hw_eoi.
  163. */
  164. void rt_vbus_isr(int irqnr, void *param);
  165. /** VBus End Of Interrupt function.
  166. *
  167. * This function will be called when VBus finished the ISR handling. BSP should
  168. * define this function to clear the interrupt flag etc.
  169. */
  170. int rt_vbus_hw_eoi(int irqnr, void *param);
  171. #endif /* end of include guard: __VBUS_H__ */