10000 Feature: shortcut I2C support for atmel-samd boards by matt-land · Pull Request #841 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

Feature: shortcut I2C support for atmel-samd boards #841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 17, 2018
Prev Previous commit
Next Next commit
fix non- builds
  • Loading branch information
Matt Land committed May 15, 2018
commit cdeb7ddff8b3f82e3cf303c04682d041fe51db31
41 changes: 27 additions & 14 deletions ports/atmel-samd/board_busses.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,35 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "mpconfigboard.h"
#include "pins.h"
#include "py/runtime.h"

STATIC mp_obj_t i2c_singleton = NULL;

STATIC mp_obj_t board_i2c(void) {
#if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL)
mp_raise_NotImplementedError('no default bus');
#endif
if (i2c_singleton == NULL) {
busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t);
self->base.type = &busio_i2c_type;

assert_pin_free(DEFAULT_I2C_BUS_SDA);
assert_pin_free(DEFAULT_I2C_BUS_SCL);
common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000);
i2c_singleton = (mp_obj_t)self;
#if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL)

STATIC mp_obj_t board_i2c(void) {
//board_i2c_obj = NULL;
mp_raise_NotImplementedError("no default bus");
return NULL;
}

#else
STATIC mp_obj_t i2c_singleton = NULL;

STATIC mp_obj_t board_i2c(void) {

if (i2c_singleton == NULL) {
busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t);
self->base.type = &busio_i2c_type;

assert_pin_free(DEFAULT_I2C_BUS_SDA);
assert_pin_free(DEFAULT_I2C_BUS_SCL);
common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the frequency always fixed at 400000? If so, is this what is wanted? Can this be overridden somehow?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't be over-ridden. What do you recommend? I'm still around for sprints tomorrow.

I followed what @tannewt recommended.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can set your own fequency when you explicitly create the i2c object.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah - so this is an alternative to the way we do it now? I'm just trying to understand what is being done, not objecting to it...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to default to the board's standard I2C pins when they haven't been specified.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #340

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - thanks for clarifying - so if you omit SCL/SDA it will trigger this. Will it still accept the full set of keyword arguments in the current API? Sorry if I am just confused.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gives you a board.I2C that is an instance of the default I2C bus for the board, which you can then pass to your drivers. If you need to change the pins or frequency, construct your own I2C object instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - that make sense - Thanks for explaining it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW - I think this will have a conflict with PR #829 but we can work that out

i2c_singleton = (mp_obj_t)self;
}
return i2c_singleton;

}
return i2c_singleton;
}
#endif

MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c);
0