4242#include "prcm.h"
4343#include "gpio.h"
4444#include "pybpin.h"
45+ #include "pybsleep.h"
4546#include "mpexception.h"
4647
4748
7879/// 2. Supply a string which matches a CPU pin name
7980/// 3. Provide a pin number
8081
82+
83+ STATIC mp_obj_t pin_obj_init_helper (const pin_obj_t * pin , mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args );
84+ STATIC void pin_obj_configure (const pin_obj_t * self );
85+
86+
8187void pin_init0 (void ) {
88+
8289}
8390
8491// C API used to convert a user-supplied pin name into an ordinal pin number.
@@ -116,11 +123,26 @@ void pin_verify_af (uint af) {
116123 }
117124}
118125
119- void pin_config (const pin_obj_t * self , uint af , uint mode , uint type , uint strength ) {
126+ void pin_config (pin_obj_t * self , uint af , uint mode , uint type , uint strength ) {
127+ // configure the pin in analog mode
128+ ((pin_obj_t * )self )-> af = af ;
129+ ((pin_obj_t * )self )-> mode = mode ;
130+ ((pin_obj_t * )self )-> type = type ;
131+ ((pin_obj_t * )self )-> strength = strength ;
132+ pin_obj_configure ((const pin_obj_t * )self );
133+ // mark the pin as used
134+ ((pin_obj_t * )self )-> used = true;
135+ // register it with the sleep module
136+ pybsleep_add (self , (WakeUpCB_t )pin_obj_configure );
137+ }
138+
139+ STATIC void pin_obj_configure (const pin_obj_t * self ) {
120140 // Skip all this if the pin is to be used in analog mode
121- if (type != PIN_TYPE_ANALOG ) {
122- // PIN_MODE_0 means it stays as a Pin, else, another peripheral will take control of it
123- if (af == PIN_MODE_0 ) {
141+ if (self -> type != PYBPIN_ANALOG_TYPE ) {
142+ // verify the alternate function
143+ pin_verify_af (self -> af );
144+ // PIN_MODE_0 means it stays as a pin, else, another peripheral will take control of it
145+ if (self -> af == PIN_MODE_0 ) {
124146 // enable the peripheral clock for the GPIO port of this pin
125147 switch (self -> port ) {
126148 case PORT_A0 :
@@ -139,14 +161,12 @@ void pin_config(const pin_obj_t *self, uint af, uint mode, uint type, uint stren
139161 break ;
140162 }
141163 // configure the direction
142- MAP_GPIODirModeSet (self -> port , self -> bit , mode );
164+ MAP_GPIODirModeSet (self -> port , self -> bit , self -> mode );
143165 }
144- // verify the alternate function
145- pin_verify_af (af );
146166 // now set the alternate function, strenght and type
147- MAP_PinModeSet (self -> pin_num , af );
167+ MAP_PinModeSet (self -> pin_num , self -> af );
148168 }
149- MAP_PinConfigSet (self -> pin_num , strength , type );
169+ MAP_PinConfigSet (self -> pin_num , self -> strength , self -> type );
150170}
151171
152172/// \method print()
@@ -201,8 +221,6 @@ STATIC void pin_print(void (*print)(void *env, const char *fmt, ...), void *env,
201221 print (env , ", strength=Pin.%s)" , qstr_str (str_qst ));
202222}
203223
204- STATIC mp_obj_t pin_obj_init_helper (const pin_obj_t * pin , mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args );
205-
206224/// \classmethod \constructor(id, ...)
207225/// Create a new Pin object associated with the id. If additional arguments are given,
208226/// they are used to initialise the pin. See `init`.
@@ -280,7 +298,7 @@ STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
280298 }
281299
282300 // configure the pin as requested
283- pin_config (self , af , mode , type , strength );
301+ pin_config (( pin_obj_t * ) self , af , mode , type , strength );
284302
285303 return mp_const_none ;
286304}
0 commit comments