ARMEBS4  revision-26.06.2015
bus.h
Go to the documentation of this file.
1 /************************************************************************//**
2  * \file heivs/bus.h
3  * \brief bus abstraction
4  * \author marc dot pignat at hevs dot ch
5  *
6  * \defgroup bus Bus
7  * \ingroup libheivs_stm32
8  *
9  * @{
10  * \brief Bus abstraction layer.
11  *
12  * This abstraction will be used to hide bus (i2c, spi, ...) behind a easy to
13  * use interface
14  ***************************************************************************/
15 #ifndef HEIVS_BUS_H
16 #define HEIVS_BUS_H
17 
18 #ifdef __cplusplus
19  extern "C" {
20 #endif
21 
22 #include <stdint.h>
23 #include <string.h>
24 #include "heivs/error.h"
25 
26 #include "heivs/config.h"
27 #if USE_FREERTOS
28 #include "freertos/FreeRTOS.h"
29 #endif
30 
31 #define BUS_PARANOIA 1 /**< 0 = less check, 1 = more checks */
32 
33 /**
34  * \brief State of the bus
35  */
37 {
38  /**
39  * Bus never initialized
40  *
41  * Choosen value is zero because C uninitialized variables are zeros
42  *
43  * \see bus_init
44  */
46 
47  /**
48  * Initialized, but free to use
49  *
50  * \see bus_get
51  * \see bus_release
52  */
54 
55  /**
56  * In use
57  *
58  * \see bus_release
59  * \see bus_get
60  */
62 
63  /**
64  * Low power mode, use bus_init to re-enable
65  *
66  * \see bus_suspend
67  * \see bus_init
68  */
70 };
71 
72 /**
73  * \brief Variable part of the bus description
74  *
75  * This part has been taken out of heivs_bus_t to save some RAM.
76  *
77  * \see heivs_bus_t
78  */
80 {
81  enum heivs_bus_state_t state;
82 
83  #if USE_FREERTOS
84  SemaphoreHandle_t mutex;
85  uint32_t mutex_ignore;
86  #endif
87 };
88 
89 /**
90  * \brief Bus handler
91  */
93 {
94  /**
95  * Current state of the bus
96  */
98 
99  /**
100  * Bus name
101  */
102  const char *name;
103 
104  /**
105  * private data for the underlying driver
106  */
107  const void *priv;
108 
109  /**
110  * Init
111  */
112  status_e (*_init)(const struct heivs_bus_t *);
113 
114  /**
115  * Suspend (disable the bus controller, put all used IOs to zero)
116  */
117  status_e (*_suspend)(const struct heivs_bus_t *);
118 
119  /**
120  * Resume
121  */
122  status_e (*_resume)(const struct heivs_bus_t *);
123 
124  /**
125  * Read/write/readwrite
126  *
127  * BUS must be get before and released after
128  *
129  * \see bus_get
130  * \see bus_release
131  */
132  status_e (*_read)(const struct heivs_bus_t *, uint32_t address, uint8_t *data, size_t len, size_t *rlen);
133  status_e (*_write)(const struct heivs_bus_t *, uint32_t address, const uint8_t *data, size_t len);
134  status_e (*_writeread)(const struct heivs_bus_t *, uint32_t address, const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_len, size_t *rlen);
135 };
136 
137 /**
138  * \brief initialize the bus
139  *
140  * \param bus
141  * \return #NO_ERROR for no problem
142  */
143 status_e bus_init(const struct heivs_bus_t *bus);
144 
145 /**
146  * \brief Get exclusive access to the bus
147  *
148  * \param bus
149  * \return #NO_ERROR for no problem
150  * \see bus_release
151  */
152 status_e bus_get(const struct heivs_bus_t *bus);
153 
154 /**
155  * \brief Release exclusive access to the bus
156  *
157  * \param bus
158  * \return #NO_ERROR for no problem
159  * \see bus_get
160  */
161 status_e bus_release(const struct heivs_bus_t *bus);
162 
163 /**
164  * \brief Suspend the bus (low power mode)
165  *
166  * \param bus
167  * \return #NO_ERROR for no problem
168  *
169  * This function will disable the bus controller, put all pins used to logical
170  * level zero to prevent power leakage.
171  *
172  * The bus must be owned (bus_get) before suspend and can be resumed using
173  * bus_resume
174  *
175  * \warning The bus is automatically released when an error occurs
176  *
177  * \see bus_get
178  * \see bus_resume
179  */
180 status_e bus_suspend(const struct heivs_bus_t *bus);
181 
182 /**
183  * \brief Resume the bus (from low power mode)
184  *
185  * \param bus
186  * \return #NO_ERROR for no problem
187  *
188  * \warning The bus is automatically released when an error occurs
189  *
190  * \see bus_release
191  * \see bus_suspend
192  */
193 status_e bus_resume(const struct heivs_bus_t *bus);
194 
195 /**
196  * \brief Resume the bus (from low power mode)
197  *
198  * \param bus
199  * \return #NO_ERROR for no problem
200  *
201  * \see bus_release
202  * \see bus_suspend
203  */
204 
205 /**
206  * \brief Read data from the bus
207  *
208  * \param bus
209  * \param address for i2c, the chip address, unused for spi and uart
210  * \param data buffer where the data will be stored
211  * \param len buffer len
212  * \param rlen effective count of read bytes, can be NULL if not interested
213  *
214  * \return #NO_ERROR for no problem
215  *
216  * \warning The bus is automatically released when an error occurs
217  *
218  * \see bus_release
219  * \see bus_suspend
220  */
221 status_e bus_read(const struct heivs_bus_t *bus, uint32_t address, void *data, size_t len, size_t *rlen);
222 
223 /**
224  * \brief Write data to the bus
225  *
226  * \param bus
227  * \param address for i2c, the chip address, unused for spi and uart
228  * \param data to be sent
229  * \param len of data
230  *
231  * \return #NO_ERROR for no problem
232  *
233  * \warning The bus is automatically released when an error occurs
234  *
235  * \see bus_release
236  * \see bus_suspend
237  */
238 status_e bus_write(const struct heivs_bus_t *bus, uint32_t address, const void *data, size_t len);
239 
240 /**
241  * \brief Combined write and read data
242  *
243  * \param bus
244  * \param address for i2c, the chip address, unused for spi and uart
245  * \param src data to be sent
246  * \param src_len len of src
247  * \param dst data to be read
248  * \param dst_len len of dst
249  * \param rlen effective count of read bytes, can be NULL if not interested
250  *
251  * \return #NO_ERROR for no problem
252  *
253  * \warning The bus is automatically released when an error occurs
254  *
255  * \see bus_release
256  * \see bus_suspend
257  */
258 status_e bus_writeread(const struct heivs_bus_t *bus, uint32_t address, const void *src, size_t src_len, void *dst, size_t dst_len, size_t *rlen);
259 
260 #ifdef __cplusplus
261  }
262 #endif
263 
264 /**
265  * @}
266  */
267 #endif /* HEIVS_BUS_H */
Bus handler.
Definition: bus.h:92
const char * name
Definition: bus.h:102
const void * priv
Definition: bus.h:107
status_e(* _suspend)(const struct heivs_bus_t *)
Definition: bus.h:117
status_e bus_release(const struct heivs_bus_t *bus)
Release exclusive access to the bus.
Definition: bus.c:149
status_e bus_resume(const struct heivs_bus_t *bus)
Resume the bus (from low power mode)
Definition: bus.c:180
Variable part of the bus description.
Definition: bus.h:79
libheivs configuration file
struct heivs_bus_var_t * var
Definition: bus.h:97
Errors definitions.
status_e(* _init)(const struct heivs_bus_t *)
Definition: bus.h:112
status_e bus_write(const struct heivs_bus_t *bus, uint32_t address, const void *data, size_t len)
Write data to the bus.
Definition: bus.c:243
status_e(* _read)(const struct heivs_bus_t *, uint32_t address, uint8_t *data, size_t len, size_t *rlen)
Definition: bus.h:132
status_e bus_init(const struct heivs_bus_t *bus)
initialize the bus
Definition: bus.c:59
status_e(* _resume)(const struct heivs_bus_t *)
Definition: bus.h:122
status_e bus_read(const struct heivs_bus_t *bus, uint32_t address, void *data, size_t len, size_t *rlen)
Resume the bus (from low power mode)
Definition: bus.c:198
status_e bus_get(const struct heivs_bus_t *bus)
Get exclusive access to the bus.
Definition: bus.c:134
Definition: bus.h:61
heivs_bus_state_t
State of the bus.
Definition: bus.h:36
status_e
Known errors.
Definition: error.h:21
status_e bus_writeread(const struct heivs_bus_t *bus, uint32_t address, const void *src, size_t src_len, void *dst, size_t dst_len, size_t *rlen)
Combined write and read data.
Definition: bus.c:279
status_e bus_suspend(const struct heivs_bus_t *bus)
Suspend the bus (low power mode)
Definition: bus.c:163
Definition: bus.h:45