Skip to content

Commit 9b16584

Browse files
committed
drivers/soft_onewire: add soft onewire driver
1 parent 8b24858 commit 9b16584

File tree

7 files changed

+476
-0
lines changed

7 files changed

+476
-0
lines changed

drivers/include/soft_onewire.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
typedef struct soft_onewire_t soft_onewire_t;
46+
47+
/** timer callback function signature internally used to schedule events */
48+
typedef void (*soft_onewire_timer_cb_t)(soft_onewire_t*);
49+
50+
/**
51+
* @brief Soft 1-Wire configuration parameters
52+
*/
53+
typedef struct {
54+
gpio_t pin; /**< GPIO pin the bus is connected to */
55+
gpio_mode_t pin_imode; /**< GPIO pin input mode */
56+
#ifdef MODULE_SOFT_ONEWIRE_HWTIMER
57+
tim_t timer; /**< peripherial timer that driver should use */
58+
#endif
59+
} soft_onewire_params_t;
60+
61+
/**
62+
* @brief Soft 1-Wire bus device descriptor
63+
*/
64+
struct soft_onewire_t {
65+
/** devices's configuration params */
66+
const soft_onewire_params_t *params;
67+
68+
/** mutext to sync thread with ISRs */
69+
mutex_t sync;
70+
71+
/** TODO: rm or doc */
72+
uint8_t mask;
73+
74+
/** Pointer to transmit and receive buffers. Only one at a time will be
75+
used, so they are in a union to save RAM. */
76+
union {
77+
const uint8_t *tx_buf; /**< pointer to buffer of bits to send */
78+
uint8_t *rx_buf; /**< pointer to buffer for received bits */
79+
};
80+
81+
/** number of bits to send or receive, also used to return errors from
82+
ISR */
83+
int buf_size;
84+
85+
#ifdef MODULE_SOFT_ONEWIRE_HWTIMER
86+
87+
/** pointer to callback of next bus event */
88+
soft_onewire_timer_cb_t timer_cb;
89+
#else
90+
/** timer used to trigger bus events */
91+
ztimer_t timer;
92+
#endif
93+
};
94+
95+
/** onewire driver callbacks for soft_onewire implementation */
96+
extern const onewire_driver_t soft_onewire_driver;
97+
98+
#endif /* SOFT_ONEWIRE_H */
99+
/** @} */

drivers/soft_onewire/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include $(RIOTBASE)/Makefile.base

drivers/soft_onewire/Makefile.dep

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FEATURES_REQUIRED += periph_gpio
2+
3+
ifneq (,$(filter soft_onewire_hwtimer,$(USEMODULE)))
4+
FEATURES_REQUIRED += periph_timer
5+
else
6+
# this is needed to get ztimer accurate enough for use in this application
7+
DEFAULT_MODULE += ztimer_auto_adjust
8+
9+
USEMODULE += ztimer
10+
USEMODULE += ztimer_usec
11+
endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
USEMODULE_INCLUDES_soft_onewire := $(LAST_MAKEFILEDIR)/include
2+
USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_soft_onewire)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef SOFT_ONEWIRE_PARAMS_H
2+
3+
#include "board.h"
4+
5+
#include "container.h"
6+
#include "soft_onewire.h"
7+
8+
#ifndef SOFT_ONEWIRE_PARAMS_PIN
9+
#define SOFT_ONEWIRE_PARAMS_PIN GPIO_UNDEF
10+
#endif
11+
12+
#ifndef SOFT_ONEWIRE_PARAMS_PIN_IMODE
13+
#define SOFT_ONEWIRE_PARAMS_PIN_IMODE GPIO_IN_PU
14+
#endif
15+
16+
#ifndef SOFT_ONEWIRE_PARAMS_HWTIMER
17+
#define SOFT_ONEWIRE_PARAMS_HWTIMER TIMER_UNDEF
18+
#endif
19+
20+
#ifdef MODULE_SOFT_ONEWIRE_HWTIMER
21+
#define SOFT_ONEWIRE_PARAMS_HWTIMER_ .timer = SOFT_ONEWIRE_PARAMS_HWTIMER,
22+
#endif
23+
24+
#ifndef SOFT_ONEWIRE_PARAMS
25+
#define SOFT_ONEWIRE_PARAMS \
26+
{ \
27+
.pin = SOFT_ONEWIRE_PARAMS_PIN, \
28+
.pin_imode = SOFT_ONEWIRE_PARAMS_PIN_IMODE, \
29+
SOFT_ONEWIRE_PARAMS_HWTIMER_ \
30+
}
31+
#endif
32+
33+
static const soft_onewire_params_t soft_onewire_params[] = {
34+
SOFT_ONEWIRE_PARAMS
35+
};
36+
37+
#define SOFT_ONEWIRE_NUMOF ARRAY_SIZE(soft_onewire_params)
38+
39+
#endif

0 commit comments

Comments
 (0)