Skip to content

Commit ddfcab8

Browse files
authored
Merge pull request #368 from fpistm/GPIO_enhancement
GPIO enhancement
2 parents 703fdb8 + a97e0db commit ddfcab8

13 files changed

+177
-195
lines changed

Diff for: cores/arduino/pins_arduino.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ uint32_t pinNametoDigitalPin(PinName p);
232232
#define digitalPinToPort(p) (get_GPIO_Port(STM_PORT(digitalPinToPinName(p))))
233233
#define digitalPinToBitMask(p) (STM_GPIO_PIN(digitalPinToPinName(p)))
234234

235-
#define analogInPinToBit(p) (STM_PIN(digitalPinToPinName(p)))
235+
#define analogInPinToBit(p) (STM_GPIO_PIN(digitalPinToPinName(p)))
236236
#define portOutputRegister(P) (&(P->ODR))
237237
#define portInputRegister(P) (&(P->IDR))
238238

Diff for: cores/arduino/stm32/PinConfigured.c

-46
This file was deleted.

Diff for: cores/arduino/stm32/PinConfigured.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ extern "C" {
4444

4545
#define PINCONF_VAL(X, Y) ((Y >> PINCONF_SHIFT(X)) & PINCONF_MASK)
4646

47-
48-
bool is_pin_configured(PinName pin, uint32_t* map);
49-
void set_pin_configured(PinName pin, uint32_t* map);
50-
void reset_pin_configured(PinName pin, uint32_t* map);
47+
#define is_pin_configured(pin, map) \
48+
(PINCONF_VAL(pin, map[PINCONF_INDEX(pin)]))
49+
#define set_pin_configured(pin, map) \
50+
(map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] | PINCONF_BIT(pin))
51+
#define reset_pin_configured(pin, map) \
52+
(map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] & (~PINCONF_BIT(pin)))
5153

5254
#ifdef __cplusplus
5355
}

Diff for: cores/arduino/stm32/PortNames.c

+14-48
Original file line numberDiff line numberDiff line change
@@ -28,73 +28,40 @@
2828
*******************************************************************************
2929
*/
3030
#include "PortNames.h"
31-
#include "stm32_def.h"
3231

33-
// Return GPIO base address
34-
GPIO_TypeDef *get_GPIO_Port(uint32_t port_idx) {
35-
GPIO_TypeDef* gpioPort = 0;
36-
switch (port_idx) {
37-
case PortA:
38-
gpioPort = (GPIO_TypeDef *)GPIOA_BASE;
39-
break;
40-
case PortB:
41-
gpioPort = (GPIO_TypeDef *)GPIOB_BASE;
42-
break;
32+
GPIO_TypeDef * GPIOPort[MAX_NB_PORT] = {
33+
(GPIO_TypeDef *)GPIOA_BASE,
34+
(GPIO_TypeDef *)GPIOB_BASE
4335
#if defined GPIOC_BASE
44-
case PortC:
45-
gpioPort = (GPIO_TypeDef *)GPIOC_BASE;
46-
break;
36+
,(GPIO_TypeDef *)GPIOC_BASE
4737
#endif
4838
#if defined GPIOD_BASE
49-
case PortD:
50-
gpioPort = (GPIO_TypeDef *)GPIOD_BASE;
51-
break;
39+
,(GPIO_TypeDef *)GPIOD_BASE
5240
#endif
5341
#if defined GPIOE_BASE
54-
case PortE:
55-
gpioPort = (GPIO_TypeDef *)GPIOE_BASE;
56-
break;
42+
,(GPIO_TypeDef *)GPIOE_BASE
5743
#endif
5844
#if defined GPIOF_BASE
59-
case PortF:
60-
gpioPort = (GPIO_TypeDef *)GPIOF_BASE;
61-
break;
45+
,(GPIO_TypeDef *)GPIOF_BASE
6246
#endif
6347
#if defined GPIOG_BASE
64-
case PortG:
65-
gpioPort = (GPIO_TypeDef *)GPIOG_BASE;
66-
break;
48+
,(GPIO_TypeDef *)GPIOG_BASE
6749
#endif
6850
#if defined GPIOH_BASE
69-
case PortH:
70-
gpioPort = (GPIO_TypeDef *)GPIOH_BASE;
71-
break;
51+
,(GPIO_TypeDef *)GPIOH_BASE
7252
#endif
7353
#if defined GPIOI_BASE
74-
case PortI:
75-
gpioPort = (GPIO_TypeDef *)GPIOI_BASE;
76-
break;
54+
,(GPIO_TypeDef *)GPIOI_BASE
7755
#endif
7856
#if defined GPIOJ_BASE
79-
case PortJ:
80-
gpioPort = (GPIO_TypeDef *)GPIOJ_BASE;
81-
break;
57+
,(GPIO_TypeDef *)GPIOJ_BASE
8258
#endif
8359
#if defined GPIOK_BASE
84-
case PortK:
85-
gpioPort = (GPIO_TypeDef *)GPIOK_BASE;
86-
break;
60+
,(GPIO_TypeDef *)GPIOK_BASE
8761
#endif
88-
default:
89-
// wrong port number
90-
//TBD: error management
91-
gpioPort = 0;
92-
break;
93-
}
94-
return gpioPort;
95-
}
62+
};
9663

97-
// Enable GPIO clock and return GPIO base address
64+
/* Enable GPIO clock and return GPIO base address */
9865
GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx) {
9966
GPIO_TypeDef* gpioPort = 0;
10067
switch (port_idx) {
@@ -173,4 +140,3 @@ GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx) {
173140
}
174141
return gpioPort;
175142
}
176-

Diff for: cores/arduino/stm32/PortNames.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
extern "C" {
3737
#endif
3838

39+
extern GPIO_TypeDef *GPIOPort[];
40+
3941
typedef enum {
4042
FirstPort = 0x00,
4143
PortA = FirstPort,
@@ -73,7 +75,9 @@ typedef enum {
7375

7476
#define MAX_NB_PORT (LastPort-FirstPort+1)
7577

76-
GPIO_TypeDef *get_GPIO_Port(uint32_t port_idx);
78+
/* Return GPIO base address */
79+
#define get_GPIO_Port(p) ((p < MAX_NB_PORT) ? GPIOPort[p] : (GPIO_TypeDef *)NULL)
80+
/* Enable GPIO clock and return GPIO base address */
7781
GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx);
7882

7983
#ifdef __cplusplus

Diff for: cores/arduino/stm32/digital_io.c

+5-34
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/**
22
******************************************************************************
33
* @file digital_io.c
4-
* @author WI6LABS
5-
* @version V1.0.0
6-
* @date 01-August-2016
74
* @brief Provide an interface to configure hw ios
85
*
96
******************************************************************************
@@ -36,12 +33,10 @@
3633
******************************************************************************
3734
*/
3835
#include "digital_io.h"
39-
#include "stm32_def.h"
40-
#include "hw_config.h"
4136
#include "PinAF_STM32F1.h"
4237

4338
#ifdef __cplusplus
44-
extern "C" {
39+
extern "C" {
4540
#endif
4641

4742
/**
@@ -57,7 +52,11 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull)
5752
GPIO_InitTypeDef GPIO_InitStructure;
5853
GPIO_TypeDef *port = set_GPIO_Port_Clock(STM_PORT(pin));
5954
GPIO_InitStructure.Pin = STM_GPIO_PIN(pin);
55+
#ifdef GPIO_SPEED_FREQ_VERY_HIGH
56+
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
57+
#else
6058
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
59+
#endif
6160
GPIO_InitStructure.Mode = mode;
6261
GPIO_InitStructure.Pull = pull;
6362
#ifdef STM32F1xx
@@ -66,34 +65,6 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull)
6665
HAL_GPIO_Init(port, &GPIO_InitStructure);
6766
}
6867

69-
/**
70-
* @brief This function set a value to an IO
71-
* @param port : one of the gpio port
72-
* @param pin : one of the gpio pin
73-
* @param val : 0 to set to low, any other value to set to high
74-
* @retval None
75-
*/
76-
void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val)
77-
{
78-
if(val) {
79-
HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET);
80-
} else {
81-
HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET);
82-
}
83-
}
84-
85-
86-
/**
87-
* @brief This function set a value to an IO
88-
* @param port : one of the gpio port
89-
* @param pin : one of the gpio pin
90-
* @retval The pin state (LOW or HIGH)
91-
*/
92-
uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin)
93-
{
94-
return (uint32_t)HAL_GPIO_ReadPin(port, pin);
95-
}
96-
9768
#ifdef __cplusplus
9869
}
9970
#endif

Diff for: cores/arduino/stm32/digital_io.h

+77-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/**
22
******************************************************************************
33
* @file digital_io.h
4-
* @author WI6LABS
5-
* @version V1.0.0
6-
* @date 01-August-2016
74
* @brief Header for digital_io module
85
******************************************************************************
96
* @attention
@@ -40,20 +37,89 @@
4037
#define __DIGITAL_IO_H
4138

4239
/* Includes ------------------------------------------------------------------*/
43-
#include "stm32_def.h"
44-
#include "PeripheralPins.h"
40+
#include "wiring_constants.h"
41+
#include "PinNames.h"
42+
#include "pinmap.h"
43+
#include "stm32yyxx_ll_gpio.h"
4544

4645
#ifdef __cplusplus
47-
extern "C" {
46+
extern "C" {
4847
#endif
4948

50-
/* Exported types ------------------------------------------------------------*/
51-
/* Exported constants --------------------------------------------------------*/
52-
/* Exported macro ------------------------------------------------------------*/
5349
/* Exported functions ------------------------------------------------------- */
5450
void digital_io_init(PinName pin, uint32_t mode, uint32_t pull);
55-
void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val);
56-
uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin);
51+
52+
/**
53+
* @brief This function set a value to an IO
54+
* @param port : one of the gpio port
55+
* @param pin : one of the gpio pin
56+
* @param val : 0 to set to low, any other value to set to high
57+
* @retval None
58+
*/
59+
static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val)
60+
{
61+
if (val) {
62+
LL_GPIO_SetOutputPin(port, pin);
63+
} else {
64+
LL_GPIO_ResetOutputPin(port, pin);
65+
}
66+
}
67+
68+
/**
69+
* @brief This function read the value of an IO
70+
* @param port : one of the gpio port
71+
* @param pin : one of the gpio pin
72+
* @retval The pin state (LOW or HIGH)
73+
*/
74+
static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin)
75+
{
76+
return LL_GPIO_IsInputPinSet(port, pin);
77+
}
78+
79+
/**
80+
* @brief This function toggle value of an IO
81+
* @param port : one of the gpio port
82+
* @param pin : one of the gpio pin
83+
* @retval None
84+
*/
85+
static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin)
86+
{
87+
LL_GPIO_TogglePin(port, pin);
88+
}
89+
90+
/**
91+
* @brief This function set a value to an IO
92+
* @param pn : Pin name
93+
* @param val : 0 to set to low, any other value to set to high
94+
* @retval None
95+
*/
96+
static inline void digitalWriteFast(PinName pn, uint32_t ulVal)
97+
{
98+
digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn), ulVal);
99+
}
100+
101+
/**
102+
* @brief This function read the value of an IO
103+
* @param pn : Pin name
104+
* @retval The pin state (LOW or HIGH)
105+
*/
106+
static inline int digitalReadFast(PinName pn)
107+
{
108+
uint8_t level = 0;
109+
level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));
110+
return (level) ? HIGH : LOW;
111+
}
112+
113+
/**
114+
* @brief This function toggle value of an IO
115+
* @param port : one of the gpio port
116+
* @param pin : one of the gpio pin
117+
* @retval None
118+
*/
119+
static inline void digitalToggleFast(PinName pn)
120+
{
121+
digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));
122+
}
57123

58124
#ifdef __cplusplus
59125
}

Diff for: cores/arduino/stm32/pinmap.c

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@
1616
//Based on mbed-os/hal/mbed_pinmap_common.c
1717

1818
#include "pinmap.h"
19+
#include "stm32yyxx_ll_gpio.h"
20+
21+
/* Map STM_PIN to LL */
22+
const uint32_t pin_map_ll[16] = {
23+
LL_GPIO_PIN_0,
24+
LL_GPIO_PIN_1,
25+
LL_GPIO_PIN_2,
26+
LL_GPIO_PIN_3,
27+
LL_GPIO_PIN_4,
28+
LL_GPIO_PIN_5,
29+
LL_GPIO_PIN_6,
30+
LL_GPIO_PIN_7,
31+
LL_GPIO_PIN_8,
32+
LL_GPIO_PIN_9,
33+
LL_GPIO_PIN_10,
34+
LL_GPIO_PIN_11,
35+
LL_GPIO_PIN_12,
36+
LL_GPIO_PIN_13,
37+
LL_GPIO_PIN_14,
38+
LL_GPIO_PIN_15
39+
};
1940

2041
void* pinmap_find_peripheral(PinName pin, const PinMap* map) {
2142
while (map->pin != NC) {

0 commit comments

Comments
 (0)