@@ -73,6 +73,7 @@ STATIC void pin_validate_mode (uint mode);
7373STATIC void pin_validate_pull (uint pull );
7474STATIC void pin_validate_drive (uint strength );
7575STATIC void pin_validate_af (const pin_obj_t * pin , int8_t idx , uint8_t * fn , uint8_t * unit , uint8_t * type );
76+ STATIC uint8_t pin_get_value (const pin_obj_t * self );
7677STATIC void GPIOA0IntHandler (void );
7778STATIC void GPIOA1IntHandler (void );
7879STATIC void GPIOA2IntHandler (void );
@@ -456,6 +457,29 @@ STATIC void pin_validate_af(const pin_obj_t* pin, int8_t idx, uint8_t *fn, uint8
456457 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
457458}
458459
460+ STATIC uint8_t pin_get_value (const pin_obj_t * self ) {
461+ uint32_t value ;
462+ bool setdir = false;
463+ if (self -> mode == PIN_TYPE_OD || self -> mode == GPIO_DIR_MODE_ALT_OD ) {
464+ setdir = true;
465+ // configure the direction to IN for a moment in order to read the pin value
466+ MAP_GPIODirModeSet (self -> port , self -> bit , GPIO_DIR_MODE_IN );
467+ }
468+ // now get the value
469+ value = MAP_GPIOPinRead (self -> port , self -> bit );
470+ if (setdir ) {
471+ // set the direction back to output
472+ MAP_GPIODirModeSet (self -> port , self -> bit , GPIO_DIR_MODE_OUT );
473+ if (self -> value ) {
474+ MAP_GPIOPinWrite (self -> port , self -> bit , self -> bit );
475+ } else {
476+ MAP_GPIOPinWrite (self -> port , self -> bit , 0 );
477+ }
478+ }
479+ // return it
480+ return value ? 1 : 0 ;
481+ }
482+
459483STATIC void GPIOA0IntHandler (void ) {
460484 EXTI_Handler (GPIOA0_BASE );
461485}
@@ -648,8 +672,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(pin_init_obj, 1, pin_obj_init);
648672STATIC mp_obj_t pin_value (mp_uint_t n_args , const mp_obj_t * args ) {
649673 pin_obj_t * self = args [0 ];
650674 if (n_args == 1 ) {
651- // get the pin value
652- return MP_OBJ_NEW_SMALL_INT (MAP_GPIOPinRead (self -> port , self -> bit ) ? 1 : 0 );
675+ // get the value
676+ return MP_OBJ_NEW_SMALL_INT (pin_get_value (self ) );
653677 } else {
654678 // set the pin value
655679 if (mp_obj_is_true (args [1 ])) {
0 commit comments