forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
ESP32S2: Support for CountIO #3615
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ msgid "" | |
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2020-10-21 20:13-0500\n" | ||
"POT-Creation-Date: 2020-11-04 21:18+0530\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
|
@@ -296,6 +296,10 @@ msgstr "" | |
msgid "All I2C peripherals are in use" | ||
msgstr "" | ||
|
||
#: ports/esp32s2/peripherals/pcnt_handler.c | ||
msgid "All PCNT units in use" | ||
msgstr "" | ||
|
||
#: ports/atmel-samd/common-hal/canio/Listener.c | ||
#: ports/esp32s2/common-hal/canio/Listener.c | ||
#: ports/stm/common-hal/canio/Listener.c | ||
|
@@ -1109,7 +1113,8 @@ msgid "Invalid phase" | |
msgstr "" | ||
|
||
#: ports/atmel-samd/common-hal/audioio/AudioOut.c | ||
#: ports/atmel-samd/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c | ||
#: ports/atmel-samd/common-hal/touchio/TouchIn.c | ||
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c | ||
#: shared-module/rgbmatrix/RGBMatrix.c | ||
msgid "Invalid pin" | ||
msgstr "" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 microDev | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include "common-hal/countio/Counter.h" | ||
#include "common-hal/microcontroller/Pin.h" | ||
|
||
#include "py/runtime.h" | ||
#include "supervisor/shared/translate.h" | ||
|
||
void common_hal_countio_counter_construct(countio_counter_obj_t* self, | ||
const mcu_pin_obj_t* pin) { | ||
claim_pin(pin); | ||
|
||
// Prepare configuration for the PCNT unit | ||
const pcnt_config_t pcnt_config = { | ||
// Set PCNT input signal and control GPIOs | ||
.pulse_gpio_num = pin->number, | ||
.ctrl_gpio_num = PCNT_PIN_NOT_USED, | ||
.channel = PCNT_CHANNEL_0, | ||
// What to do on the positive / negative edge of pulse input? | ||
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge | ||
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge | ||
}; | ||
|
||
// Initialize PCNT unit | ||
const int8_t unit = peripherals_pcnt_init(pcnt_config); | ||
if (unit == -1) { | ||
mp_raise_RuntimeError(translate("All PCNT units in use")); | ||
} | ||
|
||
self->pin = pin->number; | ||
self->unit = (pcnt_unit_t)unit; | ||
} | ||
|
||
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) { | ||
return self->unit == PCNT_UNIT_MAX; | ||
} | ||
|
||
void common_hal_countio_counter_deinit(countio_counter_obj_t* self) { | ||
if (common_hal_countio_counter_deinited(self)) { | ||
return; | ||
} | ||
reset_pin_number(self->pin); | ||
peripherals_pcnt_deinit(&self->unit); | ||
} | ||
|
||
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) { | ||
int16_t count; | ||
pcnt_get_counter_value(self->unit, &count); | ||
return count+self->count; | ||
} | ||
|
||
void common_hal_countio_counter_set_count(countio_counter_obj_t* self, | ||
mp_int_t new_count) { | ||
self->count = new_count; | ||
pcnt_counter_clear(self->unit); | ||
} | ||
|
||
void common_hal_countio_counter_reset(countio_counter_obj_t* self) { | ||
common_hal_countio_counter_set_count(self, 0); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 microDev | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H | ||
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H | ||
|
||
#include "py/obj.h" | ||
#include "peripherals/pcnt.h" | ||
|
||
typedef struct { | ||
mp_obj_base_t base; | ||
uint8_t pin; | ||
mp_int_t count; | ||
pcnt_unit_t unit; | ||
} countio_counter_obj_t; | ||
|
||
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//No countio module functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 microDev | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include "peripherals/pcnt.h" | ||
|
||
#define PCNT_UNIT_ACTIVE 1 | ||
#define PCNT_UNIT_INACTIVE 0 | ||
|
||
static uint8_t pcnt_state[4]; | ||
|
||
int peripherals_pcnt_init(pcnt_config_t pcnt_config) { | ||
// Look for available pcnt unit | ||
for (uint8_t i = 0; i<=3; i++) { | ||
if (pcnt_state[i] == PCNT_UNIT_INACTIVE) { | ||
pcnt_config.unit = (pcnt_unit_t)i; | ||
pcnt_state[i] = PCNT_UNIT_ACTIVE; | ||
break; | ||
} else if (i == 3) { | ||
return -1; | ||
} | ||
} | ||
|
||
// Initialize PCNT unit | ||
pcnt_unit_config(&pcnt_config); | ||
|
||
// Configure and enable the input filter | ||
pcnt_set_filter_value(pcnt_config.unit, 100); | ||
pcnt_filter_enable(pcnt_config.unit); | ||
|
||
// Initialize PCNT's counter | ||
pcnt_counter_pause(pcnt_config.unit); | ||
pcnt_counter_clear(pcnt_config.unit); | ||
|
||
// Everything is set up, now go to counting | ||
pcnt_counter_resume(pcnt_config.unit); | ||
|
||
return pcnt_config.unit; | ||
} | ||
|
||
void peripherals_pcnt_deinit(pcnt_unit_t* unit) { | ||
pcnt_state[*unit] = PCNT_UNIT_INACTIVE; | ||
*unit = PCNT_UNIT_MAX; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 microDev | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H | ||
#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H | ||
|
||
#include "driver/pcnt.h" | ||
|
||
extern int peripherals_pcnt_init(pcnt_config_t pcnt_config); | ||
extern void peripherals_pcnt_deinit(pcnt_unit_t* unit); | ||
|
||
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.