Skip to content
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

Check for bus number 0 in spiSetBusInstance() #11088

Merged
merged 1 commit into from
Dec 12, 2021

Conversation

SteveCEvans
Copy link
Member

Fixes: #11087

@DusKing1
Copy link
Contributor

DusKing1 commented Dec 1, 2021

Will windows 11 cause this problem (failed to open serial port)? I guess no?
NVM, there's another com port application on my desktop occupied that com, I can now connect it without any problem!

DusKing1
DusKing1 previously approved these changes Dec 1, 2021
haslinghuis
haslinghuis previously approved these changes Dec 1, 2021
@@ -343,7 +343,7 @@ void IOConfigGPIO(IO_t io, ioConfig_t cfg)

void IOConfigGPIOAF(IO_t io, ioConfig_t cfg, uint8_t af)
{
if (!io) {
if ((!io) || (IO_GPIOPortIdx(io) < 0) || ((unsigned int)IO_GPIOPortIdx(io) >= ARRAYLEN(ioPortDefs))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The original idea was that you can't get invalid io - macros + ioTag mapping tried to ensure that.
It may be reasonable to be defensive here, but invalid io error shall be propagated somehow (other functions may fail too and validity check will cause considerable overhead). Also all targets shall implement similar handling.

Another possibility to to use macros / static assert to check that ioPortDefs is defined for all enabled pins / nonzero ioDefUsedMask entries.

Copy link
Member Author

Choose a reason for hiding this comment

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

Please hold fire on this PR. I'm not happy to just be catching a bad param being passed. I'm tracking down where that comes from.

@SteveCEvans SteveCEvans dismissed stale reviews from haslinghuis and DusKing1 via 71f8eb8 December 1, 2021 22:22
@SteveCEvans SteveCEvans changed the title Check parameter passed to IOConfigGPIOAF() to prevent G4 lockup Check for bus number 0 in spiSetBusInstance() Dec 1, 2021
@SteveCEvans
Copy link
Member Author

I've found and fixed the underlying issue which was causing memory corruption

@@ -486,7 +486,7 @@ static void spiRxIrqHandler(dmaChannelDescriptor_t* descriptor)
// Mark this bus as being SPI
bool spiSetBusInstance(extDevice_t *dev, uint32_t device)
{
if (device > SPIDEV_COUNT) {
if ((device == 0) || (device > SPIDEV_COUNT)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about

#define SPI_CFG_IS_VALID(device_cfg) (SPI_CFG_TO_DEV(device_cfg) >= 0 && SPI_CFG_TO_DEV(device_cfg) < SPIDEV_COUNT)

Also, consider refactoring uint32_t device to uint32_t device_cfg. Or even better, rename _cfg to _tag everywhere( IIRC _cfg is used as configuration struct in other parts of code).

This mixed zero and one based indexing will eventually lead to some headaches ..

Copy link
Member

Choose a reason for hiding this comment

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

@SteveCEvans please take a look at the suggestion.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are other cases where we have 0 based and 1 based numbering. Always a source of confusion. For example pinioSet(0, 1) sets PINIO 1.

I’d rather look at this in another PR, post 4.3.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reason for 1 based indexing (when implementing iotag) was to allow 'no pin' when memory is initialized to zeroes.
But C type checking makes it difficult to separate IO from io_tag, it was partially solved by using pointer type for IO.
It may be possible to use structs to force type checking, but it is quite hard to hide all implementation detail completely.

@blckmn blckmn merged commit adcc368 into betaflight:master Dec 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

G47x USB broken
9 participants