Skip to content

Commit 6afe413

Browse files
committed
esp32: Add support for onewire protocol via OneWire module.
Reuses a lot of code from esp8266.
1 parent 145bf94 commit 6afe413

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

esp32/Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ SRC_C = \
116116
mphalport.c \
117117
fatfs_port.c \
118118
help.c \
119+
esponewire.c \
119120
modutime.c \
120121
moduos.c \
121122
machine_pin.c \
@@ -133,6 +134,10 @@ STM_SRC_C = $(addprefix stmhal/,\
133134
input.c \
134135
)
135136

137+
ESP8266_SRC_C = $(addprefix esp8266/,\
138+
modonewire.c \
139+
)
140+
136141
EXTMOD_SRC_C = $(addprefix extmod/,\
137142
)
138143

@@ -176,12 +181,13 @@ OBJ_MP =
176181
OBJ_MP += $(PY_O)
177182
OBJ_MP += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
178183
OBJ_MP += $(addprefix $(BUILD)/, $(STM_SRC_C:.c=.o))
184+
OBJ_MP += $(addprefix $(BUILD)/, $(ESP8266_SRC_C:.c=.o))
179185
OBJ_MP += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o))
180186
OBJ_MP += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
181187
OBJ_MP += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
182188

183189
# List of sources for qstr extraction
184-
SRC_QSTR += $(SRC_C) $(STM_SRC_C) $(EXTMOD_SRC_C) $(DRIVERS_SRC_C)
190+
SRC_QSTR += $(SRC_C) $(STM_SRC_C) $(ESP8266_SRC_C) $(EXTMOD_SRC_C) $(DRIVERS_SRC_C)
185191
# Append any auto-generated sources that are needed by sources listed in SRC_QSTR
186192
SRC_QSTR_AUTO_DEPS +=
187193

esp32/esponewire.c

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015-2017 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mphal.h"
28+
#include "esp8266/esponewire.h"
29+
30+
#define TIMING_RESET1 (0)
31+
#define TIMING_RESET2 (1)
32+
#define TIMING_RESET3 (2)
33+
#define TIMING_READ1 (3)
34+
#define TIMING_READ2 (4)
35+
#define TIMING_READ3 (5)
36+
#define TIMING_WRITE1 (6)
37+
#define TIMING_WRITE2 (7)
38+
#define TIMING_WRITE3 (8)
39+
40+
uint16_t esp_onewire_timings[9] = {480, 40, 420, 5, 5, 40, 10, 50, 10};
41+
42+
#define DELAY_US mp_hal_delay_us_fast
43+
44+
int esp_onewire_reset(mp_hal_pin_obj_t pin) {
45+
mp_hal_pin_write(pin, 0);
46+
DELAY_US(esp_onewire_timings[TIMING_RESET1]);
47+
uint32_t i = disable_irq();
48+
mp_hal_pin_write(pin, 1);
49+
DELAY_US(esp_onewire_timings[TIMING_RESET2]);
50+
int status = !mp_hal_pin_read(pin);
51+
enable_irq(i);
52+
DELAY_US(esp_onewire_timings[TIMING_RESET3]);
53+
return status;
54+
}
55+
56+
int esp_onewire_readbit(mp_hal_pin_obj_t pin) {
57+
mp_hal_pin_write(pin, 1);
58+
uint32_t i = disable_irq();
59+
mp_hal_pin_write(pin, 0);
60+
DELAY_US(esp_onewire_timings[TIMING_READ1]);
61+
mp_hal_pin_write(pin, 1);
62+
DELAY_US(esp_onewire_timings[TIMING_READ2]);
63+
int value = mp_hal_pin_read(pin);
64+
enable_irq(i);
65+
DELAY_US(esp_onewire_timings[TIMING_READ3]);
66+
return value;
67+
}
68+
69+
void esp_onewire_writebit(mp_hal_pin_obj_t pin, int value) {
70+
uint32_t i = disable_irq();
71+
mp_hal_pin_write(pin, 0);
72+
DELAY_US(esp_onewire_timings[TIMING_WRITE1]);
73+
if (value) {
74+
mp_hal_pin_write(pin, 1);
75+
}
76+
DELAY_US(esp_onewire_timings[TIMING_WRITE2]);
77+
mp_hal_pin_write(pin, 1);
78+
DELAY_US(esp_onewire_timings[TIMING_WRITE3]);
79+
enable_irq(i);
80+
}

esp32/modules/onewire.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../esp8266/modules/onewire.py

esp32/mpconfigport.h

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150

151151
// extra built in modules to add to the list of known ones
152152
extern const struct _mp_obj_module_t esp_module;
153+
extern const struct _mp_obj_module_t onewire_module;
153154
extern const struct _mp_obj_module_t utime_module;
154155
extern const struct _mp_obj_module_t uos_module;
155156
extern const struct _mp_obj_module_t mp_module_usocket;
@@ -158,6 +159,7 @@ extern const struct _mp_obj_module_t mp_module_network;
158159

159160
#define MICROPY_PORT_BUILTIN_MODULES \
160161
{ MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \
162+
{ MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&onewire_module }, \
161163
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module }, \
162164
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \
163165
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \

esp32/mphalport.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ uint32_t mp_hal_get_cpu_freq(void);
6060
#define mp_hal_pin_obj_t gpio_num_t
6161
mp_hal_pin_obj_t machine_pin_get_id(mp_obj_t pin_in);
6262
#define mp_hal_get_pin_obj(o) machine_pin_get_id(o)
63+
#define mp_obj_get_pin(o) machine_pin_get_id(o) // legacy name; only to support esp8266/modonewire
6364
#define mp_hal_pin_name(p) (p)
6465
static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) {
6566
gpio_set_direction(pin, GPIO_MODE_INPUT);

0 commit comments

Comments
 (0)