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

Fix/check valid pins #2910

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Sming/Arch/Esp32/Components/driver/include/driver/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

#pragma once

#ifdef SOC_LEDC_CHANNEL_NUM
#include <soc/soc_caps.h>

#define PWM_CHANNEL_NUM_MAX SOC_LEDC_CHANNEL_NUM
#else
// this should not happen if the correct esp32 includes are used, just to be absolutely sure
#define PWM_CHANNEL_NUM_MAX 8
#endif
9 changes: 5 additions & 4 deletions Sming/Arch/Esp32/Core/HardwarePWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ uint32_t maxDuty(ledc_timer_bit_t bits)

HardwarePWM::HardwarePWM(uint8_t* pins, uint8_t no_of_pins) : channel_count(no_of_pins)
{
debug_d("starting HardwarePWM init");
assert(no_of_pins > 0 && no_of_pins <= SOC_LEDC_CHANNEL_NUM);
no_of_pins = std::min(uint8_t(SOC_LEDC_CHANNEL_NUM), no_of_pins);
debug_i("initializing PERIPH_LEDC_MODULE");
periph_module_enable(PERIPH_LEDC_MODULE);
if((no_of_pins == 0) || (no_of_pins > SOC_LEDC_CHANNEL_NUM)) {
return;
}

for(uint8_t i = 0; i < no_of_pins; i++) {
//make sure we don't try to use pins unavailable for the SoC
assert(SOC_GPIO_VALID_OUTPUT_GPIO_MASK & (1U << pins[i]));
Comment on lines +149 to +150
Copy link
Contributor

Choose a reason for hiding this comment

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

If you look at ledc_channel_config you'll see that this exact check is already provided LEDC_ARG_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "gpio_num");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sigh. I came from observing the bug that I described above (my current setting, as used on the 8266, uses the two flash pins). First, I started with asserts that would have tripped on using those, but then thought that this is the wrong place to do this.
I'm now adding a list of allowable pins per SoC to the configuration (that would be a great use case for defaults for arrays with objects in ConfigDB, btw).
This assert just seemed to be the minimal sensible thing to do. I should have assumed that someone was already sensible enough to add it.
I'll close the pr then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I've seen designs for the esp8266 which doesn't use flash at all but boots over SDIO so can't even assume they'll never be used!

channels[i] = pins[i];

/*
Expand Down
Loading