Skip to content

Commit ff208d7

Browse files
committed
Add low-level OneWire support class.
This class focuses on the timing sensitive parts of the protocol. Everything else will be done by Python code. This also establishes that its OK to back a nativeio class with a bitbang implementation when no hardware acceleration exists. When it does, then bitbangio should be used to explicitly bitbang a protocol.
1 parent 7cb5486 commit ff208d7

File tree

17 files changed

+706
-27
lines changed

17 files changed

+706
-27
lines changed

atmel-samd/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ SRC_SHARED_MODULE = \
253253
help.c \
254254
bitbangio/__init__.c \
255255
bitbangio/I2C.c \
256+
bitbangio/OneWire.c \
256257
bitbangio/SPI.c \
258+
nativeio/OneWire.c \
257259
uheap/__init__.c \
258260

259261
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \

atmel-samd/common-hal/nativeio/DigitalInOut.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ void common_hal_nativeio_digitalinout_set_value(
9999
port_base->OUTSET.reg = pin_mask;
100100
}
101101
} else {
102-
if (!self->open_drain) {
103-
port_base->DIRSET.reg = pin_mask;
104-
}
102+
port_base->DIRSET.reg = pin_mask;
105103
port_base->OUTCLR.reg = pin_mask;
106104
}
107105
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
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+
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
28+
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
const mcu_pin_obj_t * pin;
35+
bool output;
36+
bool open_drain;
37+
} nativeio_digitalinout_obj_t;
38+
39+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__

atmel-samd/common-hal/nativeio/types.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252

5353
#include "py/obj.h"
5454

55+
// Import bitbang implemented types. Must be after DigitalInOut because they
56+
// likely depend on it.
57+
#include "shared-module/nativeio/OneWire.h"
58+
5559
typedef struct {
5660
mp_obj_base_t base;
5761
const mcu_pin_obj_t * pin;
@@ -63,13 +67,6 @@ typedef struct {
6367
struct dac_module dac_instance;
6468
} nativeio_analogout_obj_t;
6569

66-
typedef struct {
67-
mp_obj_base_t base;
68-
const mcu_pin_obj_t * pin;
69-
bool output;
70-
bool open_drain;
71-
} nativeio_digitalinout_obj_t;
72-
7370
typedef struct {
7471
mp_obj_base_t base;
7572
struct i2c_master_module i2c_master_instance;

esp8266/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
130130
SRC_SHARED_MODULE = \
131131
bitbangio/__init__.c \
132132
bitbangio/I2C.c \
133+
bitbangio/OneWire.c \
133134
bitbangio/SPI.c \
135+
nativeio/OneWire.c \
134136

135137
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
136138
$(addprefix shared-module/, $(SRC_SHARED_MODULE))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
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+
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
28+
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
const mcu_pin_obj_t * pin;
35+
bool output;
36+
bool open_drain;
37+
} nativeio_digitalinout_obj_t;
38+
39+
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__

esp8266/common-hal/nativeio/types.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
#include "py/obj.h"
3636

37+
// Use the bitbang wrapper for OneWire
38+
// TODO(tannewt): Wrap bitbangio for I2C too.
39+
#include "shared-module/nativeio/OneWire.h"
40+
3741
typedef struct {
3842
mp_obj_base_t base;
3943
const mcu_pin_obj_t * pin;
@@ -44,13 +48,6 @@ typedef struct {
4448
mp_obj_base_t base;
4549
} nativeio_analogout_obj_t;
4650

47-
typedef struct {
48-
mp_obj_base_t base;
49-
const mcu_pin_obj_t * pin;
50-
bool output;
51-
bool open_drain;
52-
} nativeio_digitalinout_obj_t;
53-
5451
// Not supported, throws error on construction.
5552
typedef struct {
5653
mp_obj_base_t base;
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
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 <stdint.h>
28+
29+
#include "lib/utils/context_manager_helpers.h"
30+
#include "py/objproperty.h"
31+
#include "py/runtime.h"
32+
#include "py/runtime0.h"
33+
#include "shared-bindings/microcontroller/Pin.h"
34+
#include "shared-bindings/bitbangio/OneWire.h"
35+
36+
//| .. currentmodule:: bitbangio
37+
//|
38+
//| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol
39+
//| ===============================================================
40+
//|
41+
//| :class:`~bitbangio.OneWire` implements the timing-sensitive foundation of
42+
//| the Maxim (formerly Dallas Semi) OneWire protocol.
43+
//|
44+
//| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
45+
//|
46+
//| .. class:: OneWire(pin)
47+
//|
48+
//| Create a OneWire object associated with the given pin. The object
49+
//| implements the lowest level timing-sensitive bits of the protocol.
50+
//|
51+
//| :param ~microcontroller.Pin pin: Pin to read pulses from.
52+
//|
53+
//| Read a short series of pulses::
54+
//|
55+
//| import bitbangio
56+
//| import board
57+
//|
58+
//| with bitbangio.OneWire(board.D7) as onewire:
59+
//| onewire.reset()
60+
//| onewire.write_bit(True)
61+
//| onewire.write_bit(False)
62+
//| print(onewire.read_bit())
63+
//|
64+
STATIC mp_obj_t bitbangio_onewire_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
65+
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
66+
mp_map_t kw_args;
67+
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
68+
enum { ARG_pin };
69+
static const mp_arg_t allowed_args[] = {
70+
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
71+
};
72+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
73+
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
74+
assert_pin(args[ARG_pin].u_obj, false);
75+
const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj);
76+
assert_pin_free(pin);
77+
78+
bitbangio_onewire_obj_t *self = m_new_obj(bitbangio_onewire_obj_t);
79+
self->base.type = &bitbangio_onewire_type;
80+
81+
shared_module_bitbangio_onewire_construct(self, pin);
82+
return MP_OBJ_FROM_PTR(self);
83+
}
84+
85+
//| .. method:: deinit()
86+
//|
87+
//| Deinitialize the OneWire bus and release any hardware resources for reuse.
88+
//|
89+
STATIC mp_obj_t bitbangio_onewire_deinit(mp_obj_t self_in) {
90+
bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
91+
shared_module_bitbangio_onewire_deinit(self);
92+
return mp_const_none;
93+
}
94+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_deinit_obj, bitbangio_onewire_deinit);
95+
96+
//| .. method:: __enter__()
97+
//|
98+
//| No-op used by Context Managers.
99+
//|
100+
// Provided by context manager helper.
101+
102+
//| .. method:: __exit__()
103+
//|
104+
//| Automatically deinitializes the hardware when exiting a context.
105+
//|
106+
STATIC mp_obj_t bitbangio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) {
107+
(void)n_args;
108+
shared_module_bitbangio_onewire_deinit(args[0]);
109+
return mp_const_none;
110+
}
111+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_onewire___exit___obj, 4, 4, bitbangio_onewire_obj___exit__);
112+
113+
//| .. method:: reset()
114+
//|
115+
//| Reset the OneWire bus
116+
//|
117+
STATIC mp_obj_t bitbangio_onewire_obj_reset(mp_obj_t self_in) {
118+
bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
119+
120+
return mp_obj_new_bool(shared_module_bitbangio_onewire_reset(self));
121+
}
122+
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_reset_obj, bitbangio_onewire_obj_reset);
123+
124+
//| .. method:: read_bit()
125+
//|
126+
//| Read in a bit
127+
//|
128+
//| :returns: bit state read
129+
//| :rtype: bool
130+
//|
131+
STATIC mp_obj_t bitbangio_onewire_obj_read_bit(mp_obj_t self_in) {
132+
bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
133+
134+
return mp_obj_new_bool(shared_module_bitbangio_onewire_read_bit(self));
135+
}
136+
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_read_bit_obj, bitbangio_onewire_obj_read_bit);
137+
138+
//| .. method:: write_bit(value)
139+
//|
140+
//| Write out a bit based on value.
141+
//|
142+
STATIC mp_obj_t bitbangio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) {
143+
bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
144+
145+
shared_module_bitbangio_onewire_write_bit(self, mp_obj_is_true(bool_obj));
146+
return mp_const_none;
147+
}
148+
MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_onewire_write_bit_obj, bitbangio_onewire_obj_write_bit);
149+
150+
STATIC const mp_rom_map_elem_t bitbangio_onewire_locals_dict_table[] = {
151+
// Methods
152+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bitbangio_onewire_deinit_obj) },
153+
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
154+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_onewire___exit___obj) },
155+
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&bitbangio_onewire_reset_obj) },
156+
{ MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&bitbangio_onewire_read_bit_obj) },
157+
{ MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&bitbangio_onewire_write_bit_obj) },
158+
};
159+
STATIC MP_DEFINE_CONST_DICT(bitbangio_onewire_locals_dict, bitbangio_onewire_locals_dict_table);
160+
161+
const mp_obj_type_t bitbangio_onewire_type = {
162+
{ &mp_type_type },
163+
.name = MP_QSTR_OneWire,
164+
.make_new = bitbangio_onewire_make_new,
165+
.locals_dict = (mp_obj_dict_t*)&bitbangio_onewire_locals_dict,
166+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
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+
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
28+
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
29+
30+
#include "common-hal/microcontroller/types.h"
31+
#include "shared-module/bitbangio/types.h"
32+
33+
extern const mp_obj_type_t bitbangio_onewire_type;
34+
35+
extern void shared_module_bitbangio_onewire_construct(bitbangio_onewire_obj_t* self,
36+
const mcu_pin_obj_t* pin);
37+
extern void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self);
38+
extern bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self);
39+
extern bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self);
40+
extern void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit);
41+
42+
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__

0 commit comments

Comments
 (0)