-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow multiple board buses #5422
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Didn't test it as I don't have the board.
Tested this with the I2C QT Rotary Encoder. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this. We might discuss it in Monday's meeting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do this using the following method:
def I2C(instance: Optional[int] = 0) -> busio.I2C
# for primary bus:
board.I2C() = board.I2C(0)
# for secondary bus:
board.I2C(1)
# and so on...
I like microdev's method because it could apply to other boards with more than one I2C whether stemma or not. |
I think we might consider doing both. The |
Let me be describe further our naming scheme for the A few boards have defined "standard" pins to use for certain groups of pins. These usually correspond to clearly labelled pins, such as So for instance if a board had SCL, SDA, SCL1, and SDA1 pins, then it would might make sense to have My general thought about this is that if you have to refer to documentation to figure out which pins correspond to |
Here is an earlier discussion about this on Scott's Deep Dive with Lady Ada - https://youtu.be/5UKr0fT51ro?t=5957 |
Is it possible to point |
That is certainly be possible. I assume you have some specific use case in mind, probably for your own boards. Could you describe how you want to use and designate multiple buses? |
I did a crude implementation of my proposal and it can be found here.
I am proposing this method for a couple of reasons:
|
I am looking at an implementation like that. My original intent was that the new code did not take any more space that the old one when not used, which led me to do an ugly duplication, but that's not good... Also I was not sure how to implement some of that. I'll commit an update soon. I am not in favor of adding a parameter to the python API of |
I agree about discoverability of the bus and thus in the implementation above I have set up following redirect at board level: // in pins.c
STATIC mp_obj_t board_stemma(void) {
return board_uart(mp_obj_new_int(0));
}
MP_DEFINE_CONST_FUN_OBJ_0(board_stemma_obj, board_stemma);
STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_STEMMA), MP_ROM_PTR(&board_stemma_obj) },
}; # in this case
board.STEMMA() = board.UART(0) = board.UART() |
@microdev1 |
Yup, my implementation is just a poc... any name or even multiple names can be pointed to same instance at board level. |
@Neradoc Are you still looking into this? If not, I can work on maturing the poc. |
08c294a
to
77ee965
Compare
I pushed a new version, you declare the list of I2C pins and their number like this: #define DEFAULT_I2C_BUS (2)
// SCL, SDA, SCL1, SDA1
#define DEFAULT_I2C_PINS { &pin_GPIO25, &pin_GPIO24, &pin_GPIO23, &pin_GPIO22 } And declare a new I2C object with a name and then number like this: ADD_SINGLETON_I2C_OBJ(board_stemma_i2c_obj, 1)
...
{ MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_stemma_i2c_obj) }, It is compatible with the old style, thus not requiring any changes in boards that only have |
I made some tweaks in the implementation and also converted |
b503f21
to
1feaae8
Compare
1feaae8
to
6dda9cb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I implemented the suggestions that I had, need a second opinion before merging.
Actually on further thought, that board might be more trouble than its worth to do that, as it provides I2C0 and I2C1 on 3 different ports each. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@microdev1 Very nice, how you have made it backward-compatible! In the long run it would be nice to change all the existing definitions to use the _PIN
struct initialization rather than the separate pins.
I suggested some minor clarifications in the doc.
@tannewt are you liking this?
I'm still not sold on the idea of a user-facing parameter to board.I2C (and others). For me it is a name for the pins marked SDA and SCL on the silkscreen for convenience, like other entries in board are names for pins, not a function for making I2C buses. And I don't see a use case for it. It doesn't bring any portability since the number doesn't carry meaning. While board.STEMMA_I2C could be used to make portable code for a stemma QT breakout (pending a PR to alias it to board.I2C on relevant boards), board.I2C(1) doesn't do that. I don't see it used to loop through ports either (for scanning for example) since it's not a list, and does not provide a len(). |
Perhaps we can use the underlying mechanism, but not expose the parameter. Another example of multiple buses is Grand Central M4. |
I agree with @Neradoc.
I'm ok using numbers internally but would prefer a text identifier. Perhaps use the QSTR instead of an instance ID. I'd suggest adding a Thanks for iterating on this! It'll be nice to have multiple standard names for buses. |
A lot of boards have buses with pin names
Yes, we are already doing this, for example on board.STEMMA_I2C() = board.I2C(1) The numerical parameter is a secondary and hidden way of interacting with the buses. Which of the following is a better way for handling of multiple bus instances? board.BUS() = board.BUS(0), board.BUS(1)... board.BUS() = board.BUS0(), board.BUS1()... |
- minor typo fix - update documentation for board module
56c0924
to
96c6271
Compare
My preference is |
I would choose names related to the board nomenclature or silkscreen: I2C, I2C1, etc. |
Thanks for the suggestions everyone, I haven't been able to get back to this and may not able to complete this for a while. |
@microdev1 Hello! We're looking at finishing this up, but I'm unclear on exactly what is left to be done. We need to add the QT Py ESP32-S2, but I believe from the conversation above, there's at least one other thing that needs to be done? I'm hoping you can clarify so we can get this finished up. Thanks! |
237c636
to
2f6ef76
Compare
The instance parameter is now removed so this should be good to go. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. Thanks for persevering on this. I have not tested this.
The next step is to add some more extra board buses for those boards where multiple named buses make sense. And then eventually all the boards could be changed to use the new scheme, and the backwards-compatibility code could be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for removing the instance argument. I think there is a bit more cleanup to simplify and clarify what the code is doing. The main task is to remove the argument parsing bits since they are no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for persisting on this! It looks good.
This adds an optional second I2C singleton, referred to as "SECOND_I2C".
SECOND_I2C_BUS_SCL
andSECOND_I2C_BUS_SDA
board.STEMMA_I2C
is implemented on the QTPY RP2040, the only board that I know of where the STEMMA port does not share the defaultboard.I2C
pins.Other boards that have a STEMMA port don't need the second I2C and can just use an alias, which is why I chose to leave that to a future PR.