@@ -60,6 +60,8 @@ DECLARE PRIVATE FUNCTIONS
6060******************************************************************************/
6161STATIC pin_obj_t * pin_find_named_pin (const mp_obj_dict_t * named_pins , mp_obj_t name );
6262STATIC pin_obj_t * pin_find_pin_by_port_bit (const mp_obj_dict_t * named_pins , uint port , uint bit );
63+ STATIC int8_t pin_obj_find_af (const pin_obj_t * pin , uint8_t fn , uint8_t unit , uint8_t type );
64+ STATIC void pin_deassign (pin_obj_t * pin );
6365STATIC void pin_obj_configure (const pin_obj_t * self );
6466STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t * self , uint * wake_pin , uint * idx );
6567STATIC void pin_extint_enable (mp_obj_t self_in );
@@ -68,6 +70,7 @@ STATIC void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t prio
6870STATIC void pin_validate_mode (uint mode );
6971STATIC void pin_validate_pull (uint pull );
7072STATIC void pin_validate_drive (uint strength );
73+ STATIC void pin_validate_af (const pin_obj_t * pin , int8_t idx , uint8_t * fn , uint8_t * unit , uint8_t * type );
7174STATIC void GPIOA0IntHandler (void );
7275STATIC void GPIOA1IntHandler (void );
7376STATIC void GPIOA2IntHandler (void );
@@ -115,9 +118,7 @@ void pin_init0(void) {
115118 mp_map_t * named_map = mp_obj_dict_get_map ((mp_obj_t )& pin_board_pins_locals_dict );
116119 for (uint i = 0 ; i < named_map -> used - 1 ; i ++ ) {
117120 pin_obj_t * pin = (pin_obj_t * )named_map -> table [i ].value ;
118- pin_config (pin , PIN_MODE_0 , GPIO_DIR_MODE_IN , PIN_TYPE_STD , -1 , PIN_STRENGTH_2MA );
119- // mark it as unused again
120- pin -> used = false;
121+ pin_deassign (pin );
121122 }
122123#endif
123124}
@@ -147,10 +148,12 @@ void pin_config (pin_obj_t *self, int af, uint mode, uint pull, int value, uint
147148 if (af != -1 ) {
148149 self -> af = af ;
149150 }
151+
150152 // if value is -1, then we want to keep it as it is
151153 if (value != -1 ) {
152154 self -> value = value ;
153155 }
156+
154157 // mark the pin as used
155158 self -> used = true;
156159 pin_obj_configure ((const pin_obj_t * )self );
@@ -160,12 +163,27 @@ void pin_config (pin_obj_t *self, int af, uint mode, uint pull, int value, uint
160163}
161164
162165int8_t pin_find_af_index (const pin_obj_t * pin , uint8_t fn , uint8_t unit , uint8_t type ) {
163- for (int i = 0 ; i < pin -> num_afs ; i ++ ) {
164- if (pin -> af_list [i ].fn == fn && pin -> af_list [i ].unit == unit && pin -> af_list [i ].type == type ) {
165- return pin -> af_list [i ].idx ;
166+ int8_t af = pin_obj_find_af (pin , fn , unit , type );
167+ if (af < 0 ) {
168+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
169+ }
170+ return af ;
171+ }
172+
173+ void pin_free_af_from_pins (uint8_t fn , uint8_t unit , uint8_t type ) {
174+ mp_map_t * named_map = mp_obj_dict_get_map ((mp_obj_t )& pin_board_pins_locals_dict );
175+ for (uint i = 0 ; i < named_map -> used - 1 ; i ++ ) {
176+ pin_obj_t * pin = (pin_obj_t * )named_map -> table [i ].value ;
177+ // af is different than GPIO
178+ if (pin -> af > PIN_MODE_0 ) {
179+ // check if the pin supports the target af
180+ int af = pin_obj_find_af (pin , fn , unit , type );
181+ if (af > 0 && af == pin -> af ) {
182+ // the pin is assigned to the target af, de-assign it
183+ pin_deassign (pin );
184+ }
166185 }
167186 }
168- nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
169187}
170188
171189/******************************************************************************
@@ -191,6 +209,20 @@ STATIC pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uin
191209 return NULL ;
192210}
193211
212+ STATIC int8_t pin_obj_find_af (const pin_obj_t * pin , uint8_t fn , uint8_t unit , uint8_t type ) {
213+ for (int i = 0 ; i < pin -> num_afs ; i ++ ) {
214+ if (pin -> af_list [i ].fn == fn && pin -> af_list [i ].unit == unit && pin -> af_list [i ].type == type ) {
215+ return pin -> af_list [i ].idx ;
216+ }
217+ }
218+ return -1 ;
219+ }
220+
221+ STATIC void pin_deassign (pin_obj_t * pin ) {
222+ pin_config (pin , PIN_MODE_0 , GPIO_DIR_MODE_IN , PIN_TYPE_STD , -1 , PIN_STRENGTH_4MA );
223+ pin -> used = false;
224+ }
225+
194226STATIC void pin_obj_configure (const pin_obj_t * self ) {
195227 uint32_t type ;
196228 if (self -> mode == PIN_TYPE_ANALOG ) {
@@ -370,6 +402,18 @@ STATIC void pin_validate_drive(uint strength) {
370402 }
371403}
372404
405+ STATIC void pin_validate_af (const pin_obj_t * pin , int8_t idx , uint8_t * fn , uint8_t * unit , uint8_t * type ) {
406+ for (int i = 0 ; i < pin -> num_afs ; i ++ ) {
407+ if (pin -> af_list [i ].idx == idx ) {
408+ * fn = pin -> af_list [i ].fn ;
409+ * unit = pin -> af_list [i ].unit ;
410+ * type = pin -> af_list [i ].type ;
411+ return ;
412+ }
413+ }
414+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
415+ }
416+
373417STATIC void GPIOA0IntHandler (void ) {
374418 EXTI_Handler (GPIOA0_BASE );
375419}
@@ -455,6 +499,12 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_
455499 goto invalid_args ;
456500 }
457501
502+ // check for a valid af and then free it from any other pins
503+ if (af > PIN_MODE_0 ) {
504+ uint8_t fn , unit , type ;
505+ pin_validate_af (self , af , & fn , & unit , & type );
506+ pin_free_af_from_pins (fn , unit , type );
507+ }
458508 pin_config (self , af , mode , pull , value , strength );
459509
460510 return mp_const_none ;
0 commit comments