summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-09-16 14:09:49 +1000
committerDamien George <damien@micropython.org>2020-10-01 12:57:10 +1000
commit39d50d129ce428858332523548f0594503d0f45b (patch)
treeb00ef4832fdd15a5e6215f31fba6af44760e7c13
parent9e0533b9e158a455be9284b70011d0515096a5f6 (diff)
ports: Add SoftI2C and SoftSPI to machine module where appropriate.
Previous commits removed the ability for one I2C/SPI constructor to construct both software- or hardware-based peripheral instances. Such construction is now split to explicit soft and non-soft types. This commit makes both types available in all ports that previously could create both software and hardware peripherals: machine.I2C and machine.SPI construct hardware instances, while machine.SoftI2C and machine.SoftSPI create software instances. This is a breaking change for use of software-based I2C and SPI. Code that constructed I2C/SPI peripherals in the following way will need to be changed: machine.I2C(-1, ...) -> machine.SoftI2C(...) machine.I2C(scl=scl, sda=sda) -> machine.SoftI2C(scl=scl, sda=sda) machine.SPI(-1, ...) -> machine.SoftSPI(...) machine.SPI(sck=sck, mosi=mosi, miso=miso) -> machine.SoftSPI(sck=sck, mosi=mosi, miso=miso) Code which uses machine.I2C and machine.SPI classes to access hardware peripherals does not need to change. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/esp32/machine_i2c.c4
-rw-r--r--ports/esp32/modmachine.c6
-rw-r--r--ports/esp32/modmachine.h1
-rw-r--r--ports/esp8266/modmachine.c3
-rw-r--r--ports/nrf/modules/machine/i2c.c4
-rw-r--r--ports/nrf/modules/machine/i2c.h2
-rw-r--r--ports/nrf/modules/machine/modmachine.c3
-rw-r--r--ports/stm32/machine_i2c.c5
-rw-r--r--ports/stm32/modmachine.c7
-rw-r--r--ports/stm32/modmachine.h1
-rw-r--r--ports/stm32/spi.h1
-rw-r--r--ports/zephyr/machine_i2c.c5
-rw-r--r--ports/zephyr/modmachine.c2
-rw-r--r--ports/zephyr/modmachine.h1
-rw-r--r--ports/zephyr/mphalport.h4
15 files changed, 33 insertions, 16 deletions
diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c
index 8362ed5e7..075d0ded6 100644
--- a/ports/esp32/machine_i2c.c
+++ b/ports/esp32/machine_i2c.c
@@ -28,6 +28,7 @@
#include "py/mphal.h"
#include "py/mperrno.h"
#include "extmod/machine_i2c.h"
+#include "modmachine.h"
#include "driver/i2c.h"
@@ -45,7 +46,6 @@ typedef struct _machine_hw_i2c_obj_t {
gpio_num_t sda : 8;
} machine_hw_i2c_obj_t;
-STATIC const mp_obj_type_t machine_hw_i2c_type;
STATIC machine_hw_i2c_obj_t machine_hw_i2c_obj[I2C_NUM_MAX];
STATIC void machine_hw_i2c_init(machine_hw_i2c_obj_t *self, uint32_t freq, uint32_t timeout_us, bool first_init) {
@@ -169,7 +169,7 @@ STATIC const mp_machine_i2c_p_t machine_hw_i2c_p = {
.transfer = machine_hw_i2c_transfer,
};
-STATIC const mp_obj_type_t machine_hw_i2c_type = {
+const mp_obj_type_t machine_hw_i2c_type = {
{ &mp_type_type },
.name = MP_QSTR_I2C,
.print = machine_hw_i2c_print,
diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c
index d56f6fcef..303d25ee8 100644
--- a/ports/esp32/modmachine.c
+++ b/ports/esp32/modmachine.c
@@ -255,10 +255,12 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) },
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
- { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
+ { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) },
+ { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
- { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
+ { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hw_spi_type) },
+ { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
// Reset reasons
diff --git a/ports/esp32/modmachine.h b/ports/esp32/modmachine.h
index fbb43250f..98b36e32b 100644
--- a/ports/esp32/modmachine.h
+++ b/ports/esp32/modmachine.h
@@ -16,6 +16,7 @@ extern const mp_obj_type_t machine_touchpad_type;
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_dac_type;
extern const mp_obj_type_t machine_pwm_type;
+extern const mp_obj_type_t machine_hw_i2c_type;
extern const mp_obj_type_t machine_hw_spi_type;
extern const mp_obj_type_t machine_uart_type;
extern const mp_obj_type_t machine_rtc_type;
diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c
index 4917ed9b9..86c1d728d 100644
--- a/ports/esp8266/modmachine.c
+++ b/ports/esp8266/modmachine.c
@@ -39,6 +39,7 @@
#include "extmod/machine_signal.h"
#include "extmod/machine_pulse.h"
#include "extmod/machine_i2c.h"
+#include "extmod/machine_spi.h"
#include "modmachine.h"
#include "xtirq.h"
@@ -422,9 +423,11 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
#if MICROPY_PY_MACHINE_I2C
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
+ { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
#endif
#if MICROPY_PY_MACHINE_SPI
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hspi_type) },
+ { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
#endif
// wake abilities
diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c
index afa906c35..704d8615a 100644
--- a/ports/nrf/modules/machine/i2c.c
+++ b/ports/nrf/modules/machine/i2c.c
@@ -63,8 +63,6 @@
#endif
-STATIC const mp_obj_type_t machine_hard_i2c_type;
-
typedef struct _machine_hard_i2c_obj_t {
mp_obj_base_t base;
nrfx_twi_t p_twi; // Driver instance
@@ -161,7 +159,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
.transfer_single = machine_hard_i2c_transfer_single,
};
-STATIC const mp_obj_type_t machine_hard_i2c_type = {
+const mp_obj_type_t machine_hard_i2c_type = {
{ &mp_type_type },
.name = MP_QSTR_I2C,
.print = machine_hard_i2c_print,
diff --git a/ports/nrf/modules/machine/i2c.h b/ports/nrf/modules/machine/i2c.h
index 3c4fde983..1dfb1f077 100644
--- a/ports/nrf/modules/machine/i2c.h
+++ b/ports/nrf/modules/machine/i2c.h
@@ -29,6 +29,8 @@
#include "extmod/machine_i2c.h"
+extern const mp_obj_type_t machine_hard_i2c_type;
+
void i2c_init0(void);
#endif // I2C_H__
diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c
index 786abac7c..3330349cd 100644
--- a/ports/nrf/modules/machine/modmachine.c
+++ b/ports/nrf/modules/machine/modmachine.c
@@ -208,7 +208,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) },
#endif
#if MICROPY_PY_MACHINE_I2C
- { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
+ { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) },
+ { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
#endif
#if MICROPY_PY_MACHINE_ADC
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c
index 14dd88b78..31defcb17 100644
--- a/ports/stm32/machine_i2c.c
+++ b/ports/stm32/machine_i2c.c
@@ -32,11 +32,10 @@
#include "py/mperrno.h"
#include "extmod/machine_i2c.h"
#include "i2c.h"
+#include "modmachine.h"
#if MICROPY_HW_ENABLE_HW_I2C
-STATIC const mp_obj_type_t machine_hard_i2c_type;
-
#define I2C_POLL_DEFAULT_TIMEOUT_US (50000) // 50ms
#if defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
@@ -266,7 +265,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
.transfer = machine_hard_i2c_transfer,
};
-STATIC const mp_obj_type_t machine_hard_i2c_type = {
+const mp_obj_type_t machine_hard_i2c_type = {
{ &mp_type_type },
.name = MP_QSTR_I2C,
.print = machine_hard_i2c_print,
diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c
index ff3b842c8..9fa00a915 100644
--- a/ports/stm32/modmachine.c
+++ b/ports/stm32/modmachine.c
@@ -37,6 +37,7 @@
#include "extmod/machine_signal.h"
#include "extmod/machine_pulse.h"
#include "extmod/machine_i2c.h"
+#include "extmod/machine_spi.h"
#include "lib/utils/pyexec.h"
#include "lib/oofatfs/ff.h"
#include "extmod/vfs.h"
@@ -415,9 +416,15 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
#if MICROPY_PY_MACHINE_I2C
+ #if MICROPY_HW_ENABLE_HW_I2C
+ { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) },
+ #else
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
#endif
+ { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
+ #endif
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) },
+ { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) },
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
diff --git a/ports/stm32/modmachine.h b/ports/stm32/modmachine.h
index e4ccf6388..e84620854 100644
--- a/ports/stm32/modmachine.h
+++ b/ports/stm32/modmachine.h
@@ -30,6 +30,7 @@
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_timer_type;
+extern const mp_obj_type_t machine_hard_i2c_type;
void machine_init(void);
void machine_deinit(void);
diff --git a/ports/stm32/spi.h b/ports/stm32/spi.h
index 885fb0bd6..17f1bf6c4 100644
--- a/ports/stm32/spi.h
+++ b/ports/stm32/spi.h
@@ -65,7 +65,6 @@ extern const spi_t spi_obj[6];
extern const mp_spi_proto_t spi_proto;
extern const mp_obj_type_t pyb_spi_type;
-extern const mp_obj_type_t machine_soft_spi_type;
extern const mp_obj_type_t machine_hard_spi_type;
// A transfer of "len" bytes should take len*8*1000/baudrate milliseconds.
diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c
index 0efcd331f..2d1c7d5c5 100644
--- a/ports/zephyr/machine_i2c.c
+++ b/ports/zephyr/machine_i2c.c
@@ -37,8 +37,7 @@
#include "py/mphal.h"
#include "py/mperrno.h"
#include "extmod/machine_i2c.h"
-
-STATIC const mp_obj_type_t machine_hard_i2c_type;
+#include "modmachine.h"
typedef struct _machine_hard_i2c_obj_t {
mp_obj_base_t base;
@@ -127,7 +126,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
.transfer_single = machine_hard_i2c_transfer_single,
};
-STATIC const mp_obj_type_t machine_hard_i2c_type = {
+const mp_obj_type_t machine_hard_i2c_type = {
{ &mp_type_type },
.name = MP_QSTR_I2C,
.print = machine_hard_i2c_print,
diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c
index fc4d3b3ca..29e6c889c 100644
--- a/ports/zephyr/modmachine.c
+++ b/ports/zephyr/modmachine.c
@@ -60,7 +60,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
#endif
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
- { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
+ { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h
index 5d7b2e1ed..b67da5533 100644
--- a/ports/zephyr/modmachine.h
+++ b/ports/zephyr/modmachine.h
@@ -4,6 +4,7 @@
#include "py/obj.h"
extern const mp_obj_type_t machine_pin_type;
+extern const mp_obj_type_t machine_hard_i2c_type;
MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj);
diff --git a/ports/zephyr/mphalport.h b/ports/zephyr/mphalport.h
index 9ef213ddb..b3154b649 100644
--- a/ports/zephyr/mphalport.h
+++ b/ports/zephyr/mphalport.h
@@ -30,6 +30,10 @@ static inline uint64_t mp_hal_time_ns(void) {
}
#define mp_hal_delay_us_fast(us) (mp_hal_delay_us(us))
+
+// C-level pin API is not currently implemented
+#define MP_HAL_PIN_FMT "%d"
+#define mp_hal_pin_name(p) (0)
#define mp_hal_pin_od_low(p) (mp_raise_NotImplementedError("mp_hal_pin_od_low"))
#define mp_hal_pin_od_high(p) (mp_raise_NotImplementedError("mp_hal_pin_od_high"))
#define mp_hal_pin_open_drain(p) (mp_raise_NotImplementedError("mp_hal_pin_open_drain"))