Skip to content

Commit 39d50d1

Browse files
committed
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>
1 parent 9e0533b commit 39d50d1

File tree

15 files changed

+33
-16
lines changed

15 files changed

+33
-16
lines changed

ports/esp32/machine_i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "py/mphal.h"
2929
#include "py/mperrno.h"
3030
#include "extmod/machine_i2c.h"
31+
#include "modmachine.h"
3132

3233
#include "driver/i2c.h"
3334

@@ -45,7 +46,6 @@ typedef struct _machine_hw_i2c_obj_t {
4546
gpio_num_t sda : 8;
4647
} machine_hw_i2c_obj_t;
4748

48-
STATIC const mp_obj_type_t machine_hw_i2c_type;
4949
STATIC machine_hw_i2c_obj_t machine_hw_i2c_obj[I2C_NUM_MAX];
5050

5151
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 = {
169169
.transfer = machine_hw_i2c_transfer,
170170
};
171171

172-
STATIC const mp_obj_type_t machine_hw_i2c_type = {
172+
const mp_obj_type_t machine_hw_i2c_type = {
173173
{ &mp_type_type },
174174
.name = MP_QSTR_I2C,
175175
.print = machine_hw_i2c_print,

ports/esp32/modmachine.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,12 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
255255
{ MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) },
256256
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
257257
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
258-
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
258+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) },
259+
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
259260
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
260261
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
261-
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
262+
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hw_spi_type) },
263+
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
262264
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
263265

264266
// Reset reasons

ports/esp32/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern const mp_obj_type_t machine_touchpad_type;
1616
extern const mp_obj_type_t machine_adc_type;
1717
extern const mp_obj_type_t machine_dac_type;
1818
extern const mp_obj_type_t machine_pwm_type;
19+
extern const mp_obj_type_t machine_hw_i2c_type;
1920
extern const mp_obj_type_t machine_hw_spi_type;
2021
extern const mp_obj_type_t machine_uart_type;
2122
extern const mp_obj_type_t machine_rtc_type;

ports/esp8266/modmachine.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "extmod/machine_signal.h"
4040
#include "extmod/machine_pulse.h"
4141
#include "extmod/machine_i2c.h"
42+
#include "extmod/machine_spi.h"
4243
#include "modmachine.h"
4344

4445
#include "xtirq.h"
@@ -422,9 +423,11 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
422423
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
423424
#if MICROPY_PY_MACHINE_I2C
424425
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
426+
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
425427
#endif
426428
#if MICROPY_PY_MACHINE_SPI
427429
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hspi_type) },
430+
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
428431
#endif
429432

430433
// wake abilities

ports/nrf/modules/machine/i2c.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@
6363

6464
#endif
6565

66-
STATIC const mp_obj_type_t machine_hard_i2c_type;
67-
6866
typedef struct _machine_hard_i2c_obj_t {
6967
mp_obj_base_t base;
7068
nrfx_twi_t p_twi; // Driver instance
@@ -161,7 +159,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
161159
.transfer_single = machine_hard_i2c_transfer_single,
162160
};
163161

164-
STATIC const mp_obj_type_t machine_hard_i2c_type = {
162+
const mp_obj_type_t machine_hard_i2c_type = {
165163
{ &mp_type_type },
166164
.name = MP_QSTR_I2C,
167165
.print = machine_hard_i2c_print,

ports/nrf/modules/machine/i2c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "extmod/machine_i2c.h"
3131

32+
extern const mp_obj_type_t machine_hard_i2c_type;
33+
3234
void i2c_init0(void);
3335

3436
#endif // I2C_H__

ports/nrf/modules/machine/modmachine.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
208208
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) },
209209
#endif
210210
#if MICROPY_PY_MACHINE_I2C
211-
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
211+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) },
212+
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
212213
#endif
213214
#if MICROPY_PY_MACHINE_ADC
214215
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },

ports/stm32/machine_i2c.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
#include "py/mperrno.h"
3333
#include "extmod/machine_i2c.h"
3434
#include "i2c.h"
35+
#include "modmachine.h"
3536

3637
#if MICROPY_HW_ENABLE_HW_I2C
3738

38-
STATIC const mp_obj_type_t machine_hard_i2c_type;
39-
4039
#define I2C_POLL_DEFAULT_TIMEOUT_US (50000) // 50ms
4140

4241
#if defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
@@ -266,7 +265,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
266265
.transfer = machine_hard_i2c_transfer,
267266
};
268267

269-
STATIC const mp_obj_type_t machine_hard_i2c_type = {
268+
const mp_obj_type_t machine_hard_i2c_type = {
270269
{ &mp_type_type },
271270
.name = MP_QSTR_I2C,
272271
.print = machine_hard_i2c_print,

ports/stm32/modmachine.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "extmod/machine_signal.h"
3838
#include "extmod/machine_pulse.h"
3939
#include "extmod/machine_i2c.h"
40+
#include "extmod/machine_spi.h"
4041
#include "lib/utils/pyexec.h"
4142
#include "lib/oofatfs/ff.h"
4243
#include "extmod/vfs.h"
@@ -415,9 +416,15 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
415416
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
416417
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
417418
#if MICROPY_PY_MACHINE_I2C
419+
#if MICROPY_HW_ENABLE_HW_I2C
420+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) },
421+
#else
418422
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
419423
#endif
424+
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
425+
#endif
420426
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) },
427+
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
421428
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
422429
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) },
423430
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },

ports/stm32/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
extern const mp_obj_type_t machine_adc_type;
3232
extern const mp_obj_type_t machine_timer_type;
33+
extern const mp_obj_type_t machine_hard_i2c_type;
3334

3435
void machine_init(void);
3536
void machine_deinit(void);

ports/stm32/spi.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ extern const spi_t spi_obj[6];
6565

6666
extern const mp_spi_proto_t spi_proto;
6767
extern const mp_obj_type_t pyb_spi_type;
68-
extern const mp_obj_type_t machine_soft_spi_type;
6968
extern const mp_obj_type_t machine_hard_spi_type;
7069

7170
// A transfer of "len" bytes should take len*8*1000/baudrate milliseconds.

ports/zephyr/machine_i2c.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
#include "py/mphal.h"
3838
#include "py/mperrno.h"
3939
#include "extmod/machine_i2c.h"
40-
41-
STATIC const mp_obj_type_t machine_hard_i2c_type;
40+
#include "modmachine.h"
4241

4342
typedef struct _machine_hard_i2c_obj_t {
4443
mp_obj_base_t base;
@@ -127,7 +126,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
127126
.transfer_single = machine_hard_i2c_transfer_single,
128127
};
129128

130-
STATIC const mp_obj_type_t machine_hard_i2c_type = {
129+
const mp_obj_type_t machine_hard_i2c_type = {
131130
{ &mp_type_type },
132131
.name = MP_QSTR_I2C,
133132
.print = machine_hard_i2c_print,

ports/zephyr/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
6060
#endif
6161
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
6262

63-
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
63+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) },
6464
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
6565
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
6666

ports/zephyr/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "py/obj.h"
55

66
extern const mp_obj_type_t machine_pin_type;
7+
extern const mp_obj_type_t machine_hard_i2c_type;
78

89
MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj);
910

ports/zephyr/mphalport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ static inline uint64_t mp_hal_time_ns(void) {
3030
}
3131

3232
#define mp_hal_delay_us_fast(us) (mp_hal_delay_us(us))
33+
34+
// C-level pin API is not currently implemented
35+
#define MP_HAL_PIN_FMT "%d"
36+
#define mp_hal_pin_name(p) (0)
3337
#define mp_hal_pin_od_low(p) (mp_raise_NotImplementedError("mp_hal_pin_od_low"))
3438
#define mp_hal_pin_od_high(p) (mp_raise_NotImplementedError("mp_hal_pin_od_high"))
3539
#define mp_hal_pin_open_drain(p) (mp_raise_NotImplementedError("mp_hal_pin_open_drain"))

0 commit comments

Comments
 (0)