aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/os/hal/include/hal_onewire.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/os/hal/include/hal_onewire.h')
-rw-r--r--lib/chibios-contrib/os/hal/include/hal_onewire.h367
1 files changed, 367 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/hal/include/hal_onewire.h b/lib/chibios-contrib/os/hal/include/hal_onewire.h
new file mode 100644
index 000000000..bbaf77b73
--- /dev/null
+++ b/lib/chibios-contrib/os/hal/include/hal_onewire.h
@@ -0,0 +1,367 @@
1/*
2 ChibiOS/RT - Copyright (C) 2014 Uladzimir Pylinsky aka barthess
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file hal_onewire.h
19 * @brief 1-wire Driver macros and structures.
20 *
21 * @addtogroup onewire
22 * @{
23 */
24
25#ifndef HAL_ONEWIRE_H_
26#define HAL_ONEWIRE_H_
27
28#if (HAL_USE_ONEWIRE == TRUE) || defined(__DOXYGEN__)
29
30/*===========================================================================*/
31/* Driver constants. */
32/*===========================================================================*/
33/**
34 * @brief Enable synthetic test for 'search ROM' procedure.
35 * @note Only for debugging/testing!
36 */
37#define ONEWIRE_SYNTH_SEARCH_TEST FALSE
38
39/**
40 * @brief Aliases for 1-wire protocol.
41 */
42#define ONEWIRE_CMD_READ_ROM 0x33
43#define ONEWIRE_CMD_SEARCH_ROM 0xF0
44#define ONEWIRE_CMD_MATCH_ROM 0x55
45#define ONEWIRE_CMD_SKIP_ROM 0xCC
46#define ONEWIRE_CMD_CONVERT_TEMP 0x44
47#define ONEWIRE_CMD_READ_SCRATCHPAD 0xBE
48
49/**
50 * @brief How many bits will be used for transaction length storage.
51 */
52#define ONEWIRE_REG_BYTES_WIDTH 16U
53
54/**
55 * @brief Precalculated maximum transaction length.
56 */
57#define ONEWIRE_MAX_TRANSACTION_LEN ((1U << ONEWIRE_REG_BYTES_WIDTH) - 1U)
58
59/*===========================================================================*/
60/* Driver pre-compile time settings. */
61/*===========================================================================*/
62#if ONEWIRE_SYNTH_SEARCH_TEST && !ONEWIRE_USE_SEARCH_ROM
63#error "Synthetic search rom test needs ONEWIRE_USE_SEARCH_ROM"
64#endif
65
66/*===========================================================================*/
67/* Derived constants and error checks. */
68/*===========================================================================*/
69#if !HAL_USE_PWM
70#error "1-wire Driver requires HAL_USE_PWM"
71#endif
72
73#if !HAL_USE_PAL
74#error "1-wire Driver requires HAL_USE_PAL"
75#endif
76
77/*===========================================================================*/
78/* Driver data structures and types. */
79/*===========================================================================*/
80#if ONEWIRE_USE_STRONG_PULLUP
81/**
82 * @brief 1-wire strong pull up assert callback type.
83 */
84typedef void (*onewire_pullup_assert_t)(void);
85
86/**
87 * @brief 1-wire strong pull up release callback type.
88 */
89typedef void (*onewire_pullup_release_t)(void);
90#endif /* ONEWIRE_USE_STRONG_PULLUP */
91
92/**
93 * @brief Driver state machine possible states.
94 */
95typedef enum {
96 ONEWIRE_UNINIT = 0, /**< Not initialized. */
97 ONEWIRE_STOP = 1, /**< Stopped. */
98 ONEWIRE_READY = 2, /**< Ready. */
99#if ONEWIRE_USE_STRONG_PULLUP
100 ONEWIRE_PULL_UP /**< Pull up asserted. */
101#endif
102} onewire_state_t;
103
104#if ONEWIRE_USE_SEARCH_ROM
105/**
106 * @brief Search ROM procedure possible state.
107 */
108typedef enum {
109 ONEWIRE_SEARCH_ROM_SUCCESS = 0, /**< ROM successfully discovered. */
110 ONEWIRE_SEARCH_ROM_LAST = 1, /**< Last ROM successfully discovered. */
111 ONEWIRE_SEARCH_ROM_ERROR = 2 /**< Error happened during search. */
112} search_rom_result_t;
113
114/**
115 * @brief Search ROM procedure iteration enum.
116 */
117typedef enum {
118 ONEWIRE_SEARCH_ROM_FIRST = 0, /**< First search run. */
119 ONEWIRE_SEARCH_ROM_NEXT = 1 /**< Next search run. */
120} search_iteration_t;
121#endif /* ONEWIRE_USE_SEARCH_ROM */
122
123/**
124 * @brief Driver configuration structure.
125 */
126typedef struct {
127 /**
128 * @brief Pointer to @p PWM driver used for communication.
129 */
130 PWMDriver *pwmd;
131 /**
132 * @brief Pointer to configuration structure for underlying PWM driver.
133 * @note It is NOT constant because 1-wire driver needs to change them
134 * during normal functioning.
135 */
136 PWMConfig *pwmcfg;
137 /**
138 * @brief Active logic level for master channel.
139 * @details Just set it to @p PWM_OUTPUT_ACTIVE_LOW when 1-wire bus
140 * connected to direct (not complementary) output of the timer.
141 * In opposite case you need to check documentation to choose
142 * correct value.
143 */
144 pwmmode_t pwmmode;
145 /**
146 * @brief Number of PWM channel used as master pulse generator.
147 */
148 size_t master_channel;
149 /**
150 * @brief Number of PWM channel used as sample interrupt generator.
151 */
152 size_t sample_channel;
153 /**
154 * @brief Port Identifier.
155 * @details This type can be a scalar or some kind of pointer, do not make
156 * any assumption about it, use the provided macros when populating
157 * variables of this type.
158 */
159 ioportid_t port;
160 /**
161 * @brief Digital I/O port pad.
162 */
163 ioportmask_t pad;
164#if defined(STM32F1XX)
165 /**
166 * @brief Digital I/O mode for idle bus.
167 * @details This is a kind of workaround against F1x realization of alternate
168 * function. Alternate function mode will be activated only
169 * when you starts appropriate peripheral.
170 */
171 iomode_t pad_mode_idle;
172#endif
173 /**
174 * @brief Digital I/O mode for active bus.
175 */
176 iomode_t pad_mode_active;
177#if ONEWIRE_USE_STRONG_PULLUP
178 /**
179 * @brief Pointer to function asserting of strong pull up.
180 */
181 onewire_pullup_assert_t pullup_assert;
182 /**
183 * @brief Pointer to function releasing of strong pull up.
184 */
185 onewire_pullup_release_t pullup_release;
186#endif
187} onewireConfig;
188
189#if ONEWIRE_USE_SEARCH_ROM
190/**
191 * @brief Search ROM registry. Contains small variables used
192 * in 'search ROM' procedure.
193 */
194typedef struct {
195 /**
196 * @brief Bool flag. True when bus has single slave device.
197 */
198 uint32_t single_device: 1;
199 /**
200 * @brief Search iteration (@p search_iteration_t enum).
201 */
202 uint32_t search_iter: 1;
203 /**
204 * @brief Result of discovery procedure (@p search_rom_result_t enum).
205 */
206 uint32_t result: 2;
207 /**
208 * @brief One of 3 steps of bit discovery.
209 * @details 0 - direct, 1 - complemented, 2 - generated by master.
210 */
211 uint32_t bit_step: 2;
212 /**
213 * @brief Values acquired during bit discovery.
214 */
215 uint32_t bit_buf: 2;
216 /**
217 * @brief Currently processing ROM bit.
218 * @note Must be big enough to store number 64.
219 */
220 uint32_t rombit: 7;
221 /**
222 * @brief Total device count discovered on bus.
223 * @note Maximum 256.
224 */
225 uint32_t devices_found: 8;
226} search_rom_reg_t;
227
228/**
229 * @brief Helper structure for 'search ROM' procedure
230 */
231typedef struct {
232 /**
233 * @brief Search ROM registry.
234 */
235 search_rom_reg_t reg;
236 /**
237 * @brief Pointer to buffer with currently discovering ROM
238 */
239 uint8_t *retbuf;
240 /**
241 * @brief Previously discovered ROM.
242 */
243 uint8_t prev_path[8];
244 /**
245 * @brief Last zero turn branch.
246 * @note Negative values use to point out of device tree's root.
247 */
248 int8_t last_zero_branch;
249 /**
250 * @brief Previous zero turn branch.
251 * @note Negative values use to point out of device tree's root.
252 */
253 int8_t prev_zero_branch;
254} onewire_search_rom_t;
255#endif /* ONEWIRE_USE_SEARCH_ROM */
256
257/**
258 * @brief Onewire registry. Some small variables combined
259 * in single machine word to save RAM.
260 */
261typedef struct {
262#if ONEWIRE_USE_STRONG_PULLUP
263 /**
264 * @brief This flag will be asserted by driver to signalizes
265 * ISR part when strong pull up needed.
266 */
267 uint32_t need_pullup: 1;
268#endif
269 /**
270 * @brief Bool flag. If @p true than at least one device presence on bus.
271 */
272 uint32_t slave_present: 1;
273 /**
274 * @brief Driver internal state (@p onewire_state_t enum).
275 */
276 uint32_t state: 2;
277 /**
278 * @brief Bit number in currently receiving/sending byte.
279 * @note Must be big enough to store 8.
280 */
281 uint32_t bit: 4;
282 /**
283 * @brief Bool flag for premature timer stop prevention.
284 */
285 uint32_t final_timeslot: 1;
286 /**
287 * @brief Bytes number to be processing in current transaction.
288 */
289 uint32_t bytes: ONEWIRE_REG_BYTES_WIDTH;
290} onewire_reg_t;
291
292/**
293 * @brief Structure representing an 1-wire driver.
294 */
295typedef struct {
296 /**
297 * @brief Onewire registry.
298 */
299 onewire_reg_t reg;
300 /**
301 * @brief Onewire config.
302 */
303 const onewireConfig *config;
304 /**
305 * @brief Pointer to I/O data buffer.
306 */
307 uint8_t *buf;
308#if ONEWIRE_USE_SEARCH_ROM
309 /**
310 * @brief Search ROM helper structure.
311 */
312 onewire_search_rom_t search_rom;
313#endif /* ONEWIRE_USE_SEARCH_ROM */
314 /**
315 * @brief Thread waiting for I/O completion.
316 */
317 thread_reference_t thread;
318} onewireDriver;
319
320/*===========================================================================*/
321/* Driver macros. */
322/*===========================================================================*/
323
324/*===========================================================================*/
325/* External declarations. */
326/*===========================================================================*/
327
328extern onewireDriver OWD1;
329
330#ifdef __cplusplus
331extern "C" {
332#endif
333 void onewireObjectInit(onewireDriver *owp);
334 void onewireStart(onewireDriver *owp, const onewireConfig *config);
335 void onewireStop(onewireDriver *owp);
336 bool onewireReset(onewireDriver *owp);
337 void onewireRead(onewireDriver *owp, uint8_t *rxbuf, size_t rxbytes);
338 uint8_t onewireCRC(const uint8_t *buf, size_t len);
339 void onewireWrite(onewireDriver *owp, uint8_t *txbuf,
340 size_t txbytes, systime_t pullup_time);
341#if ONEWIRE_USE_SEARCH_ROM
342 size_t onewireSearchRom(onewireDriver *owp,
343 uint8_t *result, size_t max_rom_cnt);
344#endif /* ONEWIRE_USE_SEARCH_ROM */
345#if ONEWIRE_SYNTH_SEARCH_TEST
346 void _synth_ow_write_bit(onewireDriver *owp, ioline_t bit);
347 ioline_t _synth_ow_read_bit(void);
348 void synthSearchRomTest(onewireDriver *owp);
349#endif /* ONEWIRE_SYNTH_SEARCH_TEST */
350#ifdef __cplusplus
351}
352#endif
353
354#endif /* HAL_USE_ONEWIRE */
355
356#endif /* HAL_ONEWIRE_H_ */
357
358/** @} */
359
360
361
362
363
364
365
366
367