Skip to content

Commit f1d678f

Browse files
authored
Merge pull request #3056 from LMESTM/fix_stm32_pull_overwrite
Fix stm32 pull overwrite
2 parents 725892e + 24d82d1 commit f1d678f

File tree

16 files changed

+194
-50
lines changed

16 files changed

+194
-50
lines changed

targets/TARGET_STM/TARGET_STM32F0/gpio_irq_api.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static void gpio_irq2(void) {
127127
}
128128

129129
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
130+
extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
130131

131132
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
132133
IRQn_Type irq_n = (IRQn_Type)0;
@@ -196,8 +197,8 @@ void gpio_irq_free(gpio_irq_t *obj) {
196197
gpio_channel->channel_gpio[gpio_idx] = 0;
197198
gpio_channel->channel_pin[gpio_idx] = 0;
198199

199-
// Disable EXTI line
200-
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
200+
// Disable EXTI line, but don't change pull-up config
201+
pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
201202
obj->event = EDGE_NONE;
202203
}
203204

@@ -245,7 +246,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
245246
}
246247
}
247248

248-
pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
249+
pin_function_gpiomode(obj->pin, mode);
249250
}
250251

251252
void gpio_irq_enable(gpio_irq_t *obj) {

targets/TARGET_STM/TARGET_STM32F0/pinmap.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,20 @@ void pin_mode(PinName pin, PinMode mode) {
138138
if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down
139139
gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
140140
gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
141+
}
142+
143+
/* Internal function for setting the gpiomode/function
144+
* without changing Pull mode
145+
*/
146+
void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
147+
148+
/* Read current pull state from HW to avoid over-write*/
149+
uint32_t port_index = STM_PORT(pin);
150+
uint32_t pin_index = STM_PIN(pin);
151+
GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
152+
uint32_t temp = gpio->PUPDR;
153+
uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
141154

155+
/* Then re-use global function for updating the mode part*/
156+
pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
142157
}

targets/TARGET_STM/TARGET_STM32F1/gpio_irq_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static void gpio_irq6(void)
163163
}
164164

165165
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
166+
extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
166167

167168
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
168169
{
@@ -267,15 +268,14 @@ void gpio_irq_free(gpio_irq_t *obj)
267268
gpio_channel->channel_gpio[gpio_idx] = 0;
268269
gpio_channel->channel_pin[gpio_idx] = 0;
269270

270-
// Disable EXTI line
271-
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
271+
// Disable EXTI line, but don't change pull-up config
272+
pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
272273
obj->event = EDGE_NONE;
273274
}
274275

275276
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
276277
{
277278
uint32_t mode = STM_MODE_IT_EVT_RESET;
278-
uint32_t pull = GPIO_NOPULL;
279279

280280
if (enable) {
281281
if (event == IRQ_RISE) {
@@ -317,7 +317,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
317317
}
318318
}
319319

320-
pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
320+
pin_function_gpiomode(obj->pin, mode);
321321
}
322322

323323
void gpio_irq_enable(gpio_irq_t *obj)

targets/TARGET_STM/TARGET_STM32F1/pinmap.c

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,19 @@ void pin_mode(PinName pin, PinMode mode)
162162

163163
uint32_t port_index = STM_PORT(pin);
164164
uint32_t pin_index = STM_PIN(pin);
165-
166165
// Enable GPIO clock
167166
uint32_t gpio_add = Set_GPIO_Clock(port_index);
168167
GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
168+
__IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low)
169+
uint32_t shift;
170+
171+
if (pin_index < 8) {
172+
shift = (pin_index * 4);
173+
gpio_reg_hl = &(gpio->CRL);
174+
} else {
175+
shift = (pin_index % 8) * 4;
176+
gpio_reg_hl = &(gpio->CRH);
177+
}
169178

170179
// Configure open-drain and pull-up/down
171180
switch (mode) {
@@ -174,16 +183,9 @@ void pin_mode(PinName pin, PinMode mode)
174183
case PullUp:
175184
case PullDown:
176185
// Set pull-up / pull-down for Input mode
177-
if (pin_index < 8) {
178-
if ((gpio->CRL & (0x03 << (pin_index * 4))) == 0) { // MODE bits = Input mode
179-
gpio->CRL |= (0x08 << (pin_index * 4)); // Set pull-up / pull-down
180-
gpio->CRL &= ~(0x04 << (pin_index * 4)); // ENSURES GPIOx_CRL.CNFx.bit0 = 0
181-
}
182-
} else {
183-
if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) == 0) { // MODE bits = Input mode
184-
gpio->CRH |= (0x08 << ((pin_index % 8) * 4)); // Set pull-up / pull-down
185-
gpio->CRH &= ~(0x04 << ((pin_index % 8) * 4)); // ENSURES GPIOx_CRH.CNFx.bit0 = 0
186-
}
186+
if ((*gpio_reg_hl & (0x03 << shift)) == 0) { // MODE bits = Input mode
187+
*gpio_reg_hl |= (0x08 << shift); // Set pull-up / pull-down
188+
*gpio_reg_hl &= ~(0x04 << shift); // ENSURES GPIOx_CRL.CNFx.bit0 = 0
187189
}
188190
// Now it's time to setup properly if pullup or pulldown. This is done in ODR register:
189191
// set pull-up => bit=1, set pull-down => bit = 0
@@ -195,17 +197,51 @@ void pin_mode(PinName pin, PinMode mode)
195197
break;
196198
case OpenDrain:
197199
// Set open-drain for Output mode (General Purpose or Alternate Function)
198-
if (pin_index < 8) {
199-
if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode
200-
gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain
201-
}
202-
} else {
203-
if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode
204-
gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain
205-
}
200+
if ((*gpio_reg_hl & (0x03 << shift)) > 0) { // MODE bits = Output mode
201+
*gpio_reg_hl |= (0x04 << shift); // Set open-drain
206202
}
207203
break;
208204
default:
209205
break;
210206
}
211207
}
208+
209+
/* Internal function for setting the gpiomode/function
210+
* without changing Pull mode
211+
*/
212+
void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
213+
214+
/* Read current pull state from HW to avoid over-write*/
215+
uint32_t port_index = STM_PORT(pin);
216+
uint32_t pin_index = STM_PIN(pin);
217+
GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
218+
uint32_t pull = PullNone;
219+
__IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low)
220+
uint32_t shift;
221+
222+
if (pin_index < 8) {
223+
shift = (pin_index * 4);
224+
gpio_reg_hl = &(gpio->CRL);
225+
} else {
226+
shift = (pin_index % 8) * 4;
227+
gpio_reg_hl = &(gpio->CRH);
228+
}
229+
230+
/* Check if pull/pull down is active */
231+
if ((!(*gpio_reg_hl & (0x03 << shift))) // input
232+
&& (!!(*gpio_reg_hl & (0x08 << shift))) // pull-up / down
233+
&& (!(*gpio_reg_hl & (0x04 << shift)))) { // GPIOx_CRL.CNFx.bit0 = 0
234+
if (!!(gpio->ODR & (0x01 << pin_index))) {
235+
pull = PullUp;
236+
} else {
237+
pull = PullDown;
238+
}
239+
} else { //output
240+
if (!!(*gpio_reg_hl & (0x04 << shift))) { //open drain
241+
pull = OpenDrain;
242+
}
243+
}
244+
245+
/* Then re-use global function for updating the mode part*/
246+
pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
247+
}

targets/TARGET_STM/TARGET_STM32F2/gpio_irq_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static void gpio_irq6(void)
163163
}
164164

165165
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
166+
extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
166167

167168
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
168169
{
@@ -267,15 +268,14 @@ void gpio_irq_free(gpio_irq_t *obj)
267268
gpio_channel->channel_gpio[gpio_idx] = 0;
268269
gpio_channel->channel_pin[gpio_idx] = 0;
269270

270-
// Disable EXTI line
271-
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
271+
// Disable EXTI line, but don't change pull-up config
272+
pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
272273
obj->event = EDGE_NONE;
273274
}
274275

275276
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
276277
{
277278
uint32_t mode = STM_MODE_IT_EVT_RESET;
278-
uint32_t pull = GPIO_NOPULL;
279279

280280
if (enable) {
281281
if (event == IRQ_RISE) {
@@ -317,7 +317,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
317317
}
318318
}
319319

320-
pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
320+
pin_function_gpiomode(obj->pin, mode);
321321
}
322322

323323
void gpio_irq_enable(gpio_irq_t *obj)

targets/TARGET_STM/TARGET_STM32F2/pinmap.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,20 @@ void pin_mode(PinName pin, PinMode mode)
178178
}
179179
gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
180180
gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
181+
}
182+
183+
/* Internal function for setting the gpiomode/function
184+
* without changing Pull mode
185+
*/
186+
void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
187+
188+
/* Read current pull state from HW to avoid over-write*/
189+
uint32_t port_index = STM_PORT(pin);
190+
uint32_t pin_index = STM_PIN(pin);
191+
GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
192+
uint32_t temp = gpio->PUPDR;
193+
uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
181194

195+
/* Then re-use global function for updating the mode part*/
196+
pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
182197
}

targets/TARGET_STM/TARGET_STM32F3/gpio_irq_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static void gpio_irq6(void)
163163
}
164164

165165
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
166+
extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
166167

167168
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
168169
{
@@ -267,15 +268,14 @@ void gpio_irq_free(gpio_irq_t *obj)
267268
gpio_channel->channel_gpio[gpio_idx] = 0;
268269
gpio_channel->channel_pin[gpio_idx] = 0;
269270

270-
// Disable EXTI line
271-
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
271+
// Disable EXTI line, but don't change pull-up config
272+
pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
272273
obj->event = EDGE_NONE;
273274
}
274275

275276
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
276277
{
277278
uint32_t mode = STM_MODE_IT_EVT_RESET;
278-
uint32_t pull = GPIO_NOPULL;
279279

280280
if (enable) {
281281
if (event == IRQ_RISE) {
@@ -317,7 +317,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
317317
}
318318
}
319319

320-
pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
320+
pin_function_gpiomode(obj->pin, mode);
321321
}
322322

323323
void gpio_irq_enable(gpio_irq_t *obj)

targets/TARGET_STM/TARGET_STM32F3/pinmap.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,19 @@ void pin_mode(PinName pin, PinMode mode)
177177
gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
178178
gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
179179
}
180+
181+
/* Internal function for setting the gpiomode/function
182+
* without changing Pull mode
183+
*/
184+
void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
185+
186+
/* Read current pull state from HW to avoid over-write*/
187+
uint32_t port_index = STM_PORT(pin);
188+
uint32_t pin_index = STM_PIN(pin);
189+
GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
190+
uint32_t temp = gpio->PUPDR;
191+
uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
192+
193+
/* Then re-use global function for updating the mode part*/
194+
pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
195+
}

targets/TARGET_STM/TARGET_STM32F7/gpio_irq_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static void gpio_irq6(void)
163163
}
164164

165165
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
166+
extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
166167

167168
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
168169
{
@@ -267,15 +268,14 @@ void gpio_irq_free(gpio_irq_t *obj)
267268
gpio_channel->channel_gpio[gpio_idx] = 0;
268269
gpio_channel->channel_pin[gpio_idx] = 0;
269270

270-
// Disable EXTI line
271-
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
271+
// Disable EXTI line, but don't change pull-up config
272+
pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
272273
obj->event = EDGE_NONE;
273274
}
274275

275276
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
276277
{
277278
uint32_t mode = STM_MODE_IT_EVT_RESET;
278-
uint32_t pull = GPIO_NOPULL;
279279

280280
if (enable) {
281281
if (event == IRQ_RISE) {
@@ -317,7 +317,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
317317
}
318318
}
319319

320-
pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
320+
pin_function_gpiomode(obj->pin, mode);
321321
}
322322

323323
void gpio_irq_enable(gpio_irq_t *obj)

targets/TARGET_STM/TARGET_STM32F7/pinmap.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,20 @@ void pin_mode(PinName pin, PinMode mode)
177177
pupd = 0; // Open-drain = No pull-up/No pull-down
178178
gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
179179
gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
180+
}
181+
182+
/* Internal function for setting the gpiomode/function
183+
* without changing Pull mode
184+
*/
185+
void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
186+
187+
/* Read current pull state from HW to avoid over-write*/
188+
uint32_t port_index = STM_PORT(pin);
189+
uint32_t pin_index = STM_PIN(pin);
190+
GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
191+
uint32_t temp = gpio->PUPDR;
192+
uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
180193

194+
/* Then re-use global function for updating the mode part*/
195+
pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
181196
}

targets/TARGET_STM/TARGET_STM32L0/gpio_irq_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ static void gpio_irq2(void)
131131
}
132132

133133
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
134+
extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
134135

135136
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
136137
{
@@ -202,15 +203,14 @@ void gpio_irq_free(gpio_irq_t *obj)
202203
gpio_channel->channel_gpio[gpio_idx] = 0;
203204
gpio_channel->channel_pin[gpio_idx] = 0;
204205

205-
// Disable EXTI line
206-
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
206+
// Disable EXTI line, but don't change pull-up config
207+
pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
207208
obj->event = EDGE_NONE;
208209
}
209210

210211
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
211212
{
212213
uint32_t mode = STM_MODE_IT_EVT_RESET;
213-
uint32_t pull = GPIO_NOPULL;
214214

215215
if (enable) {
216216
if (event == IRQ_RISE) {
@@ -252,7 +252,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
252252
}
253253
}
254254

255-
pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
255+
pin_function_gpiomode(obj->pin, mode);
256256
}
257257

258258
void gpio_irq_enable(gpio_irq_t *obj)

0 commit comments

Comments
 (0)