41
41
*/
42
42
#define NUMOF_IRQS (16U)
43
43
44
+ /**
45
+ * @brief Mask to get PINCFG reg value from mode value
46
+ */
47
+ #define MODE_PINCFG_MASK (0x06)
48
+
44
49
/**
45
50
* @brief Mapping of pins to EXTI lines, -1 means not EXTI possible
46
51
*/
@@ -89,42 +94,38 @@ void gpio_init_mux(gpio_t pin, gpio_mux_t mux)
89
94
port -> PMUX [pin_pos >> 1 ].reg |= (mux << (4 * (pin_pos & 0x1 )));
90
95
}
91
96
92
- int gpio_init (gpio_t pin , gpio_dir_t dir , gpio_pp_t pushpull )
97
+ int gpio_init (gpio_t pin , gpio_mode_t mode )
93
98
{
94
99
PortGroup * port = _port (pin );
95
100
int pin_pos = _pin_pos (pin );
96
101
int pin_mask = _pin_mask (pin );
97
102
98
- /* configure the pin's pull resistor and reset all other configuration */
99
- switch (pushpull ) {
100
- case GPIO_PULLDOWN :
101
- port -> OUTCLR .reg = pin_mask ;
102
- port -> PINCFG [pin_pos ].reg = PORT_PINCFG_PULLEN ;
103
- break ;
104
- case GPIO_PULLUP :
105
- port -> OUTSET .reg = pin_mask ;
106
- port -> PINCFG [pin_pos ].reg = PORT_PINCFG_PULLEN ;
107
- break ;
108
- case GPIO_NOPULL :
109
- port -> PINCFG [pin_pos ].reg = 0 ;
110
- break ;
103
+ /* make sure pin mode is applicable */
104
+ if (mode > 0x7 ) {
105
+ return -1 ;
111
106
}
112
- /* set pin_pos direction */
113
- if (dir == GPIO_DIR_OUT ) {
114
- if (pushpull == GPIO_PULLDOWN ) {
115
- return -1 ;
116
- }
117
- port -> DIRSET .reg = pin_mask ; /* configure as output */
118
- port -> OUTCLR .reg = pin_mask ; /* set pin LOW on init */
107
+
108
+ /* set pin direction */
109
+ if (mode & 0x2 ) {
110
+ port -> DIRCLR .reg = pin_mask ;
119
111
}
120
112
else {
121
- port -> DIRCLR .reg = pin_mask ; /* configure as input */
122
- port -> PINCFG [pin_pos ].reg |= PORT_PINCFG_INEN ;
113
+ port -> DIRSET .reg = pin_mask ;
123
114
}
115
+
116
+ /* configure the pin cfg and clear output register */
117
+ port -> PINCFG [pin_pos ].reg = (mode & MODE_PINCFG_MASK );
118
+ port -> OUTCLR .reg = pin_mask ;
119
+
120
+ /* and set pull-up/pull-down if applicable */
121
+ if (mode == 0x7 ) {
122
+ port -> OUTSET .reg = pin_mask ;
123
+ }
124
+
124
125
return 0 ;
125
126
}
126
127
127
- int gpio_init_int (gpio_t pin , gpio_pp_t pullup , gpio_flank_t flank ,
128
+ int gpio_init_int (gpio_t pin , gpio_mode_t mode , gpio_flank_t flank ,
128
129
gpio_cb_t cb , void * arg )
129
130
{
130
131
int exti = _exti (pin );
@@ -138,7 +139,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank,
138
139
gpio_config [exti ].cb = cb ;
139
140
gpio_config [exti ].arg = arg ;
140
141
/* configure pin as input and set MUX to peripheral function A */
141
- gpio_init (pin , GPIO_DIR_IN , pullup );
142
+ gpio_init (pin , mode );
142
143
gpio_init_mux (pin , GPIO_MUX_A );
143
144
/* enable clocks for the EIC module */
144
145
MCLK -> APBAMASK .reg |= MCLK_APBAMASK_EIC ;
0 commit comments