1010#include "supervisor/shared/translate.h"
1111
1212void common_hal_countio_counter_construct (countio_counter_obj_t * self ,
13- const mcu_pin_obj_t * pin_a ) {
14- if (!pin_a -> has_extint ) {
13+ const mcu_pin_obj_t * pin , countio_edge_t edge , digitalio_pull_t pull ) {
14+ if (!pin -> has_extint ) {
1515 mp_raise_RuntimeError (translate ("Pin must support hardware interrupts" ));
1616 }
1717
1818
1919 if (eic_get_enable ()) {
20- if (!eic_channel_free (pin_a -> extint_channel )) {
20+ if (!eic_channel_free (pin -> extint_channel )) {
2121 mp_raise_RuntimeError (translate ("A hardware interrupt channel is already in use" ));
2222 }
2323 } else {
2424 turn_on_external_interrupt_controller ();
2525 }
2626
2727 // These default settings apply when the EIC isn't yet enabled.
28- self -> eic_channel_a = pin_a -> extint_channel ;
29-
30- self -> pin_a = pin_a -> number ;
31-
32- gpio_set_pin_function (self -> pin_a , GPIO_PIN_FUNCTION_A );
33- gpio_set_pin_pull_mode (self -> pin_a , GPIO_PULL_UP );
28+ self -> eic_channel = pin -> extint_channel ;
29+
30+ self -> pin = pin -> number ;
31+
32+ gpio_set_pin_function (self -> pin , GPIO_PIN_FUNCTION_A );
33+
34+ enum gpio_pull_mode asf_pull = GPIO_PULL_OFF ;
35+ switch (pull ) {
36+ case PULL_UP :
37+ asf_pull = GPIO_PULL_UP ;
38+ break ;
39+ case PULL_DOWN :
40+ asf_pull = GPIO_PULL_DOWN ;
41+ break ;
42+ case PULL_NONE :
43+ default :
44+ break ;
45+ }
46+ gpio_set_pin_pull_mode (self -> pin , asf_pull );
3447
35- set_eic_channel_data (self -> eic_channel_a , (void * )self );
48+ set_eic_channel_data (self -> eic_channel , (void * )self );
3649
3750 self -> count = 0 ;
38-
39-
40- claim_pin (pin_a );
41-
42-
43- set_eic_handler (self -> eic_channel_a , EIC_HANDLER_COUNTER );
44- turn_on_eic_channel (self -> eic_channel_a , EIC_CONFIG_SENSE0_FALL_Val );
45-
51+ claim_pin (pin );
52+
53+ set_eic_handler (self -> eic_channel , EIC_HANDLER_COUNTER );
54+
55+ uint32_t sense_setting = EIC_CONFIG_SENSE0_BOTH_Val ;
56+ switch (edge ) {
57+ case EDGE_RISE :
58+ sense_setting = EIC_CONFIG_SENSE0_RISE_Val ;
59+ break ;
60+ case EDGE_FALL :
61+ sense_setting = EIC_CONFIG_SENSE0_FALL_Val ;
62+ break ;
63+ case EDGE_RISE_AND_FALL :
64+ default :
65+ break ;
66+ }
67+ turn_on_eic_channel (self -> eic_channel , sense_setting );
4668}
4769
4870bool common_hal_countio_counter_deinited (countio_counter_obj_t * self ) {
49- return self -> pin_a == NO_PIN ;
71+ return self -> pin == NO_PIN ;
5072}
5173
5274void common_hal_countio_counter_deinit (countio_counter_obj_t * self ) {
5375 if (common_hal_countio_counter_deinited (self )) {
5476 return ;
5577 }
5678
57- set_eic_handler (self -> eic_channel_a , EIC_HANDLER_NO_INTERRUPT );
58- turn_off_eic_channel (self -> eic_channel_a );
79+ set_eic_handler (self -> eic_channel , EIC_HANDLER_NO_INTERRUPT );
80+ turn_off_eic_channel (self -> eic_channel );
5981
6082
61- reset_pin_number (self -> pin_a );
62- self -> pin_a = NO_PIN ;
83+ reset_pin_number (self -> pin );
84+ self -> pin = NO_PIN ;
6385
6486}
6587
@@ -72,10 +94,6 @@ void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
7294 self -> count = new_count ;
7395}
7496
75- void common_hal_countio_counter_reset (countio_counter_obj_t * self ) {
76- self -> count = 0 ;
77- }
78-
7997void counter_interrupt_handler (uint8_t channel ) {
8098 countio_counter_obj_t * self = get_eic_channel_data (channel );
8199
0 commit comments