vbus.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_local_conf.h"
  33. #include <vbus_api.h>
  34. int rt_vbus_init(void *outr, void *inr);
  35. void rt_vbus_resume_out_thread(void);
  36. /** Post data on channel.
  37. *
  38. * @param chnr the channel number
  39. * @param prio the priority of the data
  40. * @param datap pointer to the actual data
  41. * @param size number of byte of the data
  42. * @param timeout the value used in the blocking API
  43. *
  44. * Note: rt_vbus_post is an asynchronous function that when it returns, the
  45. * @datap and @size is recorded in the post queue at least but there is no
  46. * guarantee that the data is copied into the ring buffer. To avoid data
  47. * corruption, you need to wait on the RT_VBUS_EVENT_ID_TX event.
  48. *
  49. * However, if you just post static data such as static string, there is no
  50. * need to wait.
  51. *
  52. * @sa rt_vbus_register_listener .
  53. */
  54. rt_err_t rt_vbus_post(rt_uint8_t chnr,
  55. rt_uint8_t prio,
  56. const void *datap,
  57. rt_size_t size,
  58. rt_int32_t timeout);
  59. struct rt_vbus_data {
  60. /* Number of bytes in current data package. */
  61. unsigned char size;
  62. /* Used internally in VBus. Don't modify this field as it may corrupt the
  63. * receive queue. */
  64. struct rt_vbus_data *next;
  65. /* Data follows the struct */
  66. };
  67. struct rt_vbus_wm_cfg {
  68. unsigned int low, high;
  69. };
  70. struct rt_vbus_request {
  71. unsigned char prio;
  72. const char *name;
  73. int is_server;
  74. struct rt_vbus_wm_cfg recv_wm, post_wm;
  75. };
  76. /** Request a channel.
  77. *
  78. * @return channel number. Negative if error happened.
  79. */
  80. int rt_vbus_request_chn(struct rt_vbus_request *req, int timeout);
  81. /** Close channel @chnr */
  82. void rt_vbus_close_chn(unsigned char chnr);
  83. /** Set the water mark level for posting into the channel @chnr. */
  84. void rt_vbus_set_post_wm(unsigned char chnr, unsigned int low, unsigned int high);
  85. /** Set the water mark level for receiving from the channel @chnr. */
  86. void rt_vbus_set_recv_wm(unsigned char chnr, unsigned int low, unsigned int high);
  87. typedef void (*rt_vbus_event_listener)(void *ctx);
  88. enum rt_vbus_event_id {
  89. /* On a packet received in channel. */
  90. RT_VBUS_EVENT_ID_RX,
  91. /* On the data of rt_vbus_post has been written to the ring buffer. */
  92. RT_VBUS_EVENT_ID_TX,
  93. /* On the channel has been closed. */
  94. RT_VBUS_EVENT_ID_DISCONN,
  95. RT_VBUS_EVENT_ID_MAX,
  96. };
  97. /** Register callback @indi on the event @eve on the @chnr.
  98. *
  99. * @ctx will passed to @indi on calling the @indi.
  100. */
  101. void rt_vbus_register_listener(unsigned char chnr,
  102. enum rt_vbus_event_id eve,
  103. rt_vbus_event_listener indi,
  104. void *ctx);
  105. /** Listen on any events happen on the @chnr for @timeout ticks.
  106. *
  107. * This function blocks until events occur or timeout happened.
  108. */
  109. rt_err_t rt_vbus_listen_on(rt_uint8_t chnr,
  110. rt_int32_t timeout);
  111. /** Push a data package into the receive queue of the channel @chnr. */
  112. void rt_vbus_data_push(unsigned int chnr,
  113. struct rt_vbus_data *data);
  114. /** Pop a data package from the receive queue of the channel @chnr.
  115. *
  116. * The actual data is following the struct rt_vbus_data. After using it, it
  117. * should be freed by rt_free.
  118. */
  119. struct rt_vbus_data* rt_vbus_data_pop(unsigned int chnr);
  120. struct rt_vbus_dev
  121. {
  122. /* Runtime infomations. */
  123. rt_uint8_t chnr;
  124. struct rt_vbus_data *act;
  125. rt_size_t pos;
  126. /* There will be a request for each channel. So no need to seperate them so
  127. * clearly. */
  128. struct rt_vbus_request req;
  129. };
  130. rt_err_t rt_vbus_chnx_init(void);
  131. /** Get the corresponding channel number from the VBus device @dev. */
  132. rt_uint8_t rt_vbus_get_chnnr(rt_device_t dev);
  133. /** Register a call back on the other side disconnect the channel.
  134. *
  135. * @sa rt_vbus_register_listener .
  136. */
  137. void rt_vbus_chnx_register_disconn(rt_device_t dev,
  138. rt_vbus_event_listener indi,
  139. void *ctx);
  140. /* Commands for the device control interface. */
  141. #define VBUS_IOCRECV_WM 0xD1
  142. #define VBUS_IOCPOST_WM 0xD2
  143. /** Configure event listener */
  144. #define VBUS_IOC_LISCFG 0xD3
  145. struct rt_vbus_dev_liscfg
  146. {
  147. enum rt_vbus_event_id event;
  148. rt_vbus_event_listener listener;
  149. void *ctx;
  150. };
  151. int rt_vbus_shell_start(void);
  152. #ifdef RT_USING_VBUS_RFS
  153. int dfs_rfs_init(void);
  154. #endif
  155. /** VBus hardware init function.
  156. *
  157. * BSP should implement this function to initialize the interrupts etc.
  158. */
  159. int rt_vbus_hw_init(void);
  160. /** VBus ISR function.
  161. *
  162. * BSP should call this function when the interrupt from other core is
  163. * triggered. @param is not used by VBus and will pass to rt_vbus_hw_eoi.
  164. */
  165. void rt_vbus_isr(int irqnr, void *param);
  166. /** VBus End Of Interrupt function.
  167. *
  168. * This function will be called when VBus finished the ISR handling. BSP should
  169. * define this function to clear the interrupt flag etc.
  170. */
  171. int rt_vbus_hw_eoi(int irqnr, void *param);
  172. #endif /* end of include guard: __VBUS_H__ */