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);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy