Skip to content

Commit cd75a9b

Browse files
committed
drivers/soft_onewire: add soft onewire driver
1 parent 31d5ef7 commit cd75a9b

File tree

7 files changed

+507
-0
lines changed

7 files changed

+507
-0
lines changed

drivers/include/soft_onewire.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
/** @} */

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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
* @ingroup drivers_soft_onewire
11+
*
12+
* @{
13+
* @file
14+
* @brief Default configuration for soft 1-Wire driver
15+
*
16+
* @author Joshua DeWeese <[email protected]>
17+
*/
18+
19+
#ifndef SOFT_ONEWIRE_PARAMS_H
20+
#define SOFT_ONEWIRE_PARAMS_H
21+
22+
#include "board.h"
23+
24+
#include "container.h"
25+
#include "soft_onewire.h"
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
#ifndef SOFT_ONEWIRE_PARAMS_PIN
32+
#define SOFT_ONEWIRE_PARAMS_PIN GPIO_UNDEF
33+
#endif
34+
35+
#ifndef SOFT_ONEWIRE_PARAMS_PIN_IMODE
36+
#define SOFT_ONEWIRE_PARAMS_PIN_IMODE GPIO_IN_PU
37+
#endif
38+
39+
#ifndef SOFT_ONEWIRE_PARAMS_HWTIMER
40+
#define SOFT_ONEWIRE_PARAMS_HWTIMER TIMER_UNDEF
41+
#endif
42+
43+
#ifdef MODULE_SOFT_ONEWIRE_HWTIMER
44+
#define SOFT_ONEWIRE_PARAMS_HWTIMER_ .timer = SOFT_ONEWIRE_PARAMS_HWTIMER,
45+
#endif
46+
47+
#ifndef SOFT_ONEWIRE_PARAMS
48+
#define SOFT_ONEWIRE_PARAMS \
49+
{ \
50+
.pin = SOFT_ONEWIRE_PARAMS_PIN, \
51+
.pin_imode = SOFT_ONEWIRE_PARAMS_PIN_IMODE, \
52+
SOFT_ONEWIRE_PARAMS_HWTIMER_ \
53+
}
54+
#endif
55+
56+
static const soft_onewire_params_t soft_onewire_params[] = {
57+
SOFT_ONEWIRE_PARAMS
58+
};
59+
60+
#define SOFT_ONEWIRE_NUMOF ARRAY_SIZE(soft_onewire_params)
61+
62+
#ifdef __cplusplus
63+
}
64+
#endif
65+
66+
#endif /* SOFT_ONEWIRE_PARAMS_H */
67+
/** @} */

0 commit comments

Comments
 (0)