Skip to content

Commit 4810722

Browse files
committed
esp8266: Change nativeio.I2C to use bitbangio under the hood rather than throwing an error.
1 parent 3afa09b commit 4810722

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

esp8266/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ SRC_COMMON_HAL = \
113113
nativeio/AnalogIn.c \
114114
nativeio/AnalogOut.c \
115115
nativeio/DigitalInOut.c \
116-
nativeio/I2C.c \
117116
nativeio/PulseIn.c \
118117
nativeio/PulseOut.c \
119118
nativeio/PWMOut.c \
@@ -132,6 +131,7 @@ SRC_SHARED_MODULE = \
132131
bitbangio/I2C.c \
133132
bitbangio/OneWire.c \
134133
bitbangio/SPI.c \
134+
nativeio/I2C.c \
135135
nativeio/OneWire.c \
136136

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

esp8266/common-hal/nativeio/types.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
#include "py/obj.h"
3636

37-
// Use the bitbang wrapper for OneWire
38-
// TODO(tannewt): Wrap bitbangio for I2C too.
37+
// Use the bitbang wrapper for I2C and OneWire
38+
#include "shared-module/nativeio/I2C.h"
3939
#include "shared-module/nativeio/OneWire.h"
4040

4141
typedef struct {
@@ -48,11 +48,6 @@ typedef struct {
4848
mp_obj_base_t base;
4949
} nativeio_analogout_obj_t;
5050

51-
// Not supported, throws error on construction.
52-
typedef struct {
53-
mp_obj_base_t base;
54-
} nativeio_i2c_obj_t;
55-
5651
typedef struct {
5752
mp_obj_base_t base;
5853
bool locked;

esp8266/common-hal/nativeio/I2C.c renamed to shared-module/nativeio/I2C.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2016 Scott Shawcroft
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -25,39 +25,42 @@
2525
*/
2626

2727
#include "shared-bindings/nativeio/I2C.h"
28+
#include "shared-bindings/bitbangio/I2C.h"
2829
#include "py/mperrno.h"
2930
#include "py/nlr.h"
3031

3132
void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
3233
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t freq) {
33-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
34-
"No hardware support for I2C. Use bitbangio instead."));
34+
shared_module_bitbangio_i2c_construct(&self->bitbang, scl, sda, freq);
3535
}
3636

3737
void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self) {
38+
shared_module_bitbangio_i2c_deinit(&self->bitbang);
3839
}
3940

4041
bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) {
41-
return false;
42+
return shared_module_bitbangio_i2c_probe(&self->bitbang, addr);
4243
}
4344

4445
bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self) {
45-
return false;
46+
return shared_module_bitbangio_i2c_try_lock(&self->bitbang);
4647
}
4748

4849
bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self) {
49-
return false;
50+
return shared_module_bitbangio_i2c_has_lock(&self->bitbang);
5051
}
5152

5253
void common_hal_nativeio_i2c_unlock(nativeio_i2c_obj_t *self) {
54+
shared_module_bitbangio_i2c_unlock(&self->bitbang);
5355
}
5456

5557
uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t addr,
5658
const uint8_t * data, size_t len, bool transmit_stop_bit) {
57-
return MP_EIO;
59+
return shared_module_bitbangio_i2c_write(&self->bitbang, addr, data, len,
60+
transmit_stop_bit);
5861
}
5962

6063
uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t addr,
6164
uint8_t * data, size_t len) {
62-
return MP_EIO;
65+
return shared_module_bitbangio_i2c_read(&self->bitbang, addr, data, len);
6366
}

shared-module/nativeio/I2C.h

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 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+
// This defines the types used to underly the standard nativeio Python objects.
28+
// The shared API is defined in terms of these types.
29+
30+
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_I2C_H__
31+
#define __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_I2C_H__
32+
33+
#include "shared-module/bitbangio/types.h"
34+
35+
#include "py/obj.h"
36+
37+
typedef struct {
38+
mp_obj_base_t base;
39+
bitbangio_i2c_obj_t bitbang;
40+
} nativeio_i2c_obj_t;
41+
42+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_I2C_H__

0 commit comments

Comments
 (0)