xref: /nrf52832-nimble/rt-thread/components/vbus/vbus.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2014-06-09     Grissiom     version 2.0.2; add comment
9  * 2015-01-06     Grissiom     version 2.0.3; API change, no functional changes
10  */
11 #ifndef __VBUS_H__
12 #define __VBUS_H__
13 
14 #include <vbus_api.h>
15 
16 int rt_vbus_init(void *outr, void *inr);
17 
18 void rt_vbus_resume_out_thread(void);
19 
20 /** Post data on channel.
21  *
22  * @param chnr the channel number
23  * @param prio the priority of the data
24  * @param datap pointer to the actual data
25  * @param size number of byte of the data
26  * @param timeout the value used in the blocking API
27  *
28  * Note: rt_vbus_post is an asynchronous function that when it returns, the
29  * @datap and @size is recorded in the post queue at least but there is no
30  * guarantee that the data is copied into the ring buffer. To avoid data
31  * corruption, you need to wait on the RT_VBUS_EVENT_ID_TX event.
32  *
33  * However, if you just post static data such as static string, there is no
34  * need to wait.
35  *
36  * @sa rt_vbus_register_listener .
37  */
38 rt_err_t rt_vbus_post(rt_uint8_t chnr,
39                       rt_uint8_t prio,
40                       const void *datap,
41                       rt_size_t size,
42                       rt_int32_t timeout);
43 
44 struct rt_vbus_data {
45     /* Number of bytes in current data package. */
46     unsigned char size;
47     /* Used internally in VBus. Don't modify this field as it may corrupt the
48      * receive queue. */
49     struct rt_vbus_data *next;
50     /* Data follows the struct */
51 };
52 
53 struct rt_vbus_wm_cfg {
54 	unsigned int low, high;
55 };
56 
57 struct rt_vbus_request {
58 	unsigned char prio;
59 	const char *name;
60 	int is_server;
61 	struct rt_vbus_wm_cfg recv_wm, post_wm;
62 };
63 
64 /** Request a channel.
65  *
66  * @return channel number. Negative if error happened.
67  */
68 int rt_vbus_request_chn(struct rt_vbus_request *req, int timeout);
69 
70 /** Close channel @chnr */
71 void rt_vbus_close_chn(unsigned char chnr);
72 
73 /** Set the water mark level for posting into the channel @chnr. */
74 void rt_vbus_set_post_wm(unsigned char chnr, unsigned int low, unsigned int high);
75 /** Set the water mark level for receiving from the channel @chnr. */
76 void rt_vbus_set_recv_wm(unsigned char chnr, unsigned int low, unsigned int high);
77 
78 typedef void (*rt_vbus_event_listener)(void *ctx);
79 
80 enum rt_vbus_event_id {
81     /* On a packet received in channel. */
82     RT_VBUS_EVENT_ID_RX,
83     /* On the data of rt_vbus_post has been written to the ring buffer. */
84     RT_VBUS_EVENT_ID_TX,
85     /* On the channel has been closed. */
86     RT_VBUS_EVENT_ID_DISCONN,
87     RT_VBUS_EVENT_ID_MAX,
88 };
89 
90 /** Register callback @indi on the event @eve on the @chnr.
91  *
92  * @ctx will passed to @indi on calling the @indi.
93  */
94 void rt_vbus_register_listener(unsigned char chnr,
95                                enum rt_vbus_event_id eve,
96                                rt_vbus_event_listener indi,
97                                void *ctx);
98 
99 /** Listen on any events happen on the @chnr for @timeout ticks.
100  *
101  * This function blocks until events occur or timeout happened.
102  */
103 rt_err_t rt_vbus_listen_on(rt_uint8_t chnr,
104                            rt_int32_t timeout);
105 
106 /** Push a data package into the receive queue of the channel @chnr. */
107 void rt_vbus_data_push(unsigned int chnr,
108                        struct rt_vbus_data *data);
109 /** Pop a data package from the receive queue of the channel @chnr.
110  *
111  * The actual data is following the struct rt_vbus_data. After using it, it
112  * should be freed by rt_free.
113  */
114 struct rt_vbus_data* rt_vbus_data_pop(unsigned int chnr);
115 
116 struct rt_vbus_dev
117 {
118     /* Runtime infomations. */
119     rt_uint8_t chnr;
120     struct rt_vbus_data *act;
121     rt_size_t pos;
122 
123     /* There will be a request for each channel. So no need to seperate them so
124      * clearly. */
125     struct rt_vbus_request req;
126 };
127 
128 rt_err_t rt_vbus_chnx_init(void);
129 /** Get the corresponding channel number from the VBus device @dev. */
130 rt_uint8_t rt_vbus_get_chnnr(rt_device_t dev);
131 /** Register a call back on the other side disconnect the channel.
132  *
133  * @sa rt_vbus_register_listener .
134  */
135 void rt_vbus_chnx_register_disconn(rt_device_t dev,
136                                    rt_vbus_event_listener indi,
137                                    void *ctx);
138 
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 
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 
152 int rt_vbus_shell_start(void);
153 #ifdef RT_USING_VBUS_RFS
154 int dfs_rfs_init(void);
155 #endif
156 
157 /** VBus hardware init function.
158  *
159  * BSP should implement this function to initialize the interrupts etc.
160  */
161 int rt_vbus_hw_init(void);
162 
163 /** VBus ISR function.
164  *
165  * BSP should call this function when the interrupt from other core is
166  * triggered. @param is not used by VBus and will pass to rt_vbus_hw_eoi.
167  */
168 void rt_vbus_isr(int irqnr, void *param);
169 
170 /** VBus End Of Interrupt function.
171  *
172  * This function will be called when VBus finished the ISR handling. BSP should
173  * define this function to clear the interrupt flag etc.
174  */
175 int rt_vbus_hw_eoi(int irqnr, void *param);
176 
177 #endif /* end of include guard: __VBUS_H__ */
178