|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Prime Controls |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @defgroup drivers_soft_onewire Soft 1-Wire Bus Driver |
| 11 | + * @ingroup drivers_onewire |
| 12 | + * @ingroup drivers_soft_periph |
| 13 | + * @brief Software implemented 1-Wire bus master |
| 14 | + * |
| 15 | + * This is a soft driver implementation of a 1-Wire bus master. Minimally, the |
| 16 | + * only required hardware is a GPIO pin. |
| 17 | + * |
| 18 | + * The pseudomodule `soft_onewire_hwtimer` is provided. When enabled, the driver |
| 19 | + * will use a dedicated `periph_timer` rather than `ztimer` to manage time |
| 20 | + * critical bus operations. |
| 21 | + * |
| 22 | + * @{ |
| 23 | + * @file |
| 24 | + * @brief 1-Wire driver interface |
| 25 | + * |
| 26 | + * @author Joshua DeWeese <[email protected]> |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef SOFT_ONEWIRE_H |
| 30 | +#define SOFT_ONEWIRE_H |
| 31 | + |
| 32 | +#include <stdbool.h> |
| 33 | +#include <stdint.h> |
| 34 | + |
| 35 | +#include "mutex.h" |
| 36 | +#include "onewire.h" |
| 37 | +#include "periph/gpio.h" |
| 38 | +#include "periph/timer.h" |
| 39 | +#include "ztimer.h" |
| 40 | + |
| 41 | +#ifdef __cplusplus |
| 42 | +extern "C" { |
| 43 | +#endif |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief soft_onewire_t forward declaration |
| 47 | + */ |
| 48 | +typedef struct soft_onewire_t soft_onewire_t; |
| 49 | + |
| 50 | +/** timer callback function signature internally used to schedule events */ |
| 51 | +typedef void (*soft_onewire_timer_cb_t)(soft_onewire_t*); |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief Soft 1-Wire configuration parameters |
| 55 | + */ |
| 56 | +typedef struct { |
| 57 | + gpio_t pin; /**< GPIO pin the bus is connected to */ |
| 58 | + gpio_mode_t pin_imode; /**< GPIO pin input mode */ |
| 59 | +#ifdef MODULE_SOFT_ONEWIRE_HWTIMER |
| 60 | + tim_t timer; /**< peripheral timer that driver should use */ |
| 61 | +#endif |
| 62 | +} soft_onewire_params_t; |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief Soft 1-Wire bus device descriptor |
| 66 | + */ |
| 67 | +struct soft_onewire_t { |
| 68 | + /** devices's configuration params */ |
| 69 | + const soft_onewire_params_t *params; |
| 70 | + |
| 71 | + /** mutext to sync thread with ISRs */ |
| 72 | + mutex_t sync; |
| 73 | + |
| 74 | + /** TODO: rm or doc */ |
| 75 | + uint8_t mask; |
| 76 | + |
| 77 | + /** Pointer to transmit and receive buffers. Only one at a time will be |
| 78 | + used, so they are in a union to save RAM. */ |
| 79 | + union { |
| 80 | + const uint8_t *tx_buf; /**< pointer to buffer of bits to send */ |
| 81 | + uint8_t *rx_buf; /**< pointer to buffer for received bits */ |
| 82 | + }; |
| 83 | + |
| 84 | + /** number of bits to send or receive, also used to return errors from |
| 85 | + ISR */ |
| 86 | + int buf_size; |
| 87 | + |
| 88 | +#ifdef MODULE_SOFT_ONEWIRE_HWTIMER |
| 89 | + |
| 90 | + /** pointer to callback of next bus event */ |
| 91 | + soft_onewire_timer_cb_t timer_cb; |
| 92 | +#else |
| 93 | + /** timer used to trigger bus events */ |
| 94 | + ztimer_t timer; |
| 95 | +#endif |
| 96 | +}; |
| 97 | + |
| 98 | +/** onewire driver callbacks for soft_onewire implementation */ |
| 99 | +extern const onewire_driver_t soft_onewire_driver; |
| 100 | + |
| 101 | +#endif /* SOFT_ONEWIRE_H */ |
| 102 | +/** @} */ |
0 commit comments