Skip to content

Commit

Permalink
codal_port: Correctly acquire and free I2C and SPI pins.
Browse files Browse the repository at this point in the history
This makes sure that pins that are currently used for I2C and SPI are in
the correct pin mode, and pins that were previously but no longer used for
I2C/SPI are freed.

Fixes issue #167.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Mar 25, 2024
1 parent 4a4c4c4 commit af721c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/codal_port/microbit_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

typedef struct _microbit_i2c_obj_t {
mp_obj_base_t base;
const microbit_pin_obj_t *scl;
const microbit_pin_obj_t *sda;
} microbit_i2c_obj_t;

STATIC mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
Expand All @@ -55,8 +57,12 @@ STATIC mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp
scl = microbit_obj_get_pin(args[ARG_scl].u_obj);
}

// Acquire new pins and free the previous ones.
microbit_obj_pin_acquire_and_free(&microbit_i2c_obj.scl, scl, microbit_pin_mode_i2c);
microbit_obj_pin_acquire_and_free(&microbit_i2c_obj.sda, sda, microbit_pin_mode_i2c);

// Initialise the I2C bus.
int ret =microbit_hal_i2c_init(scl->name, sda->name, args[ARG_freq].u_int);
int ret = microbit_hal_i2c_init(scl->name, sda->name, args[ARG_freq].u_int);

if (ret != 0) {
mp_raise_OSError(ret);
Expand Down Expand Up @@ -149,6 +155,8 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
locals_dict, &microbit_i2c_locals_dict
);

const microbit_i2c_obj_t microbit_i2c_obj = {
microbit_i2c_obj_t microbit_i2c_obj = {
{ &microbit_i2c_type },
.scl = &microbit_p19_obj,
.sda = &microbit_p20_obj,
};
18 changes: 11 additions & 7 deletions src/codal_port/microbit_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

typedef struct _microbit_spi_obj_t {
mp_obj_base_t base;
const microbit_pin_obj_t *sclk;
const microbit_pin_obj_t *mosi;
const microbit_pin_obj_t *miso;
} microbit_spi_obj_t;

STATIC bool microbit_spi_initialised = false;
Expand Down Expand Up @@ -71,12 +74,10 @@ STATIC mp_obj_t microbit_spi_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp
miso = microbit_obj_get_pin(args[ARG_miso].u_obj);
}

// Initialise the pins.
// Note: the pins are not freed, so init'ing the SPI a second time on
// different pins will leave the old pins still in SPI mode.
microbit_obj_pin_acquire(sclk, microbit_pin_mode_spi);
microbit_obj_pin_acquire(mosi, microbit_pin_mode_spi);
microbit_obj_pin_acquire(miso, microbit_pin_mode_spi);
// Acquire new pins and free the previous ones.
microbit_obj_pin_acquire_and_free(&microbit_spi_obj.sclk, sclk, microbit_pin_mode_spi);
microbit_obj_pin_acquire_and_free(&microbit_spi_obj.mosi, mosi, microbit_pin_mode_spi);
microbit_obj_pin_acquire_and_free(&microbit_spi_obj.miso, miso, microbit_pin_mode_spi);

// Initialise the SPI bus.
int ret = microbit_hal_spi_init(sclk->name, mosi->name, miso->name,
Expand Down Expand Up @@ -147,6 +148,9 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
locals_dict, &microbit_spi_locals_dict
);

const microbit_spi_obj_t microbit_spi_obj = {
microbit_spi_obj_t microbit_spi_obj = {
{ &microbit_spi_type },
.sclk = &microbit_p13_obj,
.mosi = &microbit_p15_obj,
.miso = &microbit_p14_obj,
};
4 changes: 2 additions & 2 deletions src/codal_port/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ extern const struct _microbit_pin_obj_t microbit_p20_obj;
extern const struct _microbit_pin_obj_t microbit_pin_logo_obj;
extern const struct _microbit_pin_obj_t microbit_pin_speaker_obj;

extern const struct _microbit_i2c_obj_t microbit_i2c_obj;
extern struct _microbit_i2c_obj_t microbit_i2c_obj;
extern const struct _microbit_uart_obj_t microbit_uart_obj;
extern const struct _microbit_spi_obj_t microbit_spi_obj;
extern struct _microbit_spi_obj_t microbit_spi_obj;

extern const struct _monochrome_5by5_t microbit_const_image_heart_obj;
extern const struct _monochrome_5by5_t microbit_const_image_heart_small_obj;
Expand Down

0 comments on commit af721c7

Please sign in to comment.