Skip to content

Commit e3e5180

Browse files
committed
[NUCLEO_F103RB] Cleanup code
Add STM_PIN and STM_PORT macros for GPIOs.
1 parent 0af4419 commit e3e5180

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/PinNames.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ extern "C" {
4242
#define STM_PIN_MODE(X) ((X) >> 8)
4343
#define STM_PIN_AFNUM(X) ((X) & 0xFF)
4444

45+
// High nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, 6=G, 7=H)
46+
// Low nibble = pin number
47+
#define STM_PORT(X) (((uint32_t)(X) >> 4) & 0xF)
48+
#define STM_PIN(X) ((uint32_t)(X) & 0xF)
49+
4550
typedef enum {
4651
PIN_INPUT,
4752
PIN_OUTPUT
4853
} PinDirection;
4954

5055
typedef enum {
51-
52-
// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F)
53-
// low nibble = pin number
5456
PA_0 = 0x00,
5557
PA_1 = 0x01,
5658
PA_2 = 0x02,

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/gpio_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ uint32_t gpio_set(PinName pin) {
4141
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
4242
if (pin == NC) return;
4343

44+
uint32_t port_index = STM_PORT(pin);
45+
4446
// Get GPIO structure base address
45-
uint32_t pin_number = (uint32_t)pin;
46-
uint32_t port_index = (pin_number >> 4);
4747
GPIO_TypeDef *gpio = (GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10));
4848

4949
// Fill GPIO object structure for future use

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/gpio_irq_api.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
8787

8888
if (pin == NC) return -1;
8989

90-
uint32_t pin_number = (uint32_t)pin;
91-
uint32_t pin_index = (pin_number & 0xF);
92-
uint32_t port_index = (pin_number >> 4);
90+
uint32_t port_index = STM_PORT(pin);
91+
uint32_t pin_index = STM_PIN(pin);
9392

9493
// Select irq number and vector
9594
switch (pin_index) {

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pinmap.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ void pin_function(PinName pin, int data) {
5353
uint32_t mode = STM_PIN_MODE(data);
5454
uint32_t afnum = STM_PIN_AFNUM(data);
5555

56+
uint32_t port_index = STM_PORT(pin);
57+
uint32_t pin_index = STM_PIN(pin);
58+
5659
// Get GPIO structure base address
57-
uint32_t pin_number = (uint32_t)pin;
58-
uint32_t pin_index = (pin_number & 0xF);
59-
uint32_t port_index = (pin_number >> 4);
6060
GPIO_TypeDef *gpio = ((GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10)));
6161

6262
// Enable GPIO and AFIO clocks
@@ -92,11 +92,11 @@ void pin_mode(PinName pin, PinMode mode) {
9292
if (pin == NC) return;
9393

9494
GPIO_InitTypeDef GPIO_InitStructure;
95+
96+
uint32_t port_index = STM_PORT(pin);
97+
uint32_t pin_index = STM_PIN(pin);
9598

9699
// Get GPIO structure base address
97-
uint32_t pin_number = (uint32_t)pin;
98-
uint32_t pin_index = (pin_number & 0xF);
99-
uint32_t port_index = (pin_number >> 4);
100100
GPIO_TypeDef *gpio = ((GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10)));
101101

102102
// Enable GPIO clock

0 commit comments

Comments
 (0)