Skip to content

Commit 42d37c5

Browse files
authored
Merge pull request #2126 from hierophect/stm32-pin-remap
STM32 pin remap
2 parents 89fed70 + e445e79 commit 42d37c5

File tree

7 files changed

+283
-266
lines changed

7 files changed

+283
-266
lines changed

ports/stm32f4/boards/stm32f411ve_discovery/mpconfigboard.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ USB_VID = 0x239A
22
USB_PID = 0x802A
33
USB_PRODUCT = "STM32F411VE Discovery Board - CPy"
44
USB_MANUFACTURER = "STMicroelectronics"
5-
USB_CDC_AND_MSC_ONLY = 1
5+
USB_DEVICES = "CDC,MSC"
66

77
INTERNAL_FLASH_FILESYSTEM = 1
88
LONGINT_IMPL = NONE

ports/stm32f4/common-hal/digitalio/DigitalInOut.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
void common_hal_digitalio_digitalinout_never_reset(
3535
digitalio_digitalinout_obj_t *self) {
36-
never_reset_pin_number(self->pin->port_number, self->pin->number);
36+
never_reset_pin_number(self->pin->port, self->pin->number);
3737
}
3838

3939
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
@@ -43,11 +43,11 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
4343
self->pin = pin;
4444

4545
GPIO_InitTypeDef GPIO_InitStruct = {0};
46-
GPIO_InitStruct.Pin = (1 << pin->number);
46+
GPIO_InitStruct.Pin = pin_mask(self->pin->number);
4747
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
4848
GPIO_InitStruct.Pull = GPIO_NOPULL;
4949
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
50-
HAL_GPIO_Init(pin->port, &GPIO_InitStruct);
50+
HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct);
5151

5252
return DIGITALINOUT_OK;
5353
}
@@ -61,19 +61,19 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self
6161
return;
6262
}
6363

64-
reset_pin_number(self->pin->port_number, self->pin->number);
64+
reset_pin_number(self->pin->port, self->pin->number);
6565
self->pin = mp_const_none;
6666
}
6767

6868
void common_hal_digitalio_digitalinout_switch_to_input(
6969
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
7070

7171
GPIO_InitTypeDef GPIO_InitStruct = {0};
72-
GPIO_InitStruct.Pin = (1 << self->pin->number);
72+
GPIO_InitStruct.Pin = pin_mask(self->pin->number);
7373
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
7474
GPIO_InitStruct.Pull = GPIO_NOPULL;
7575
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
76-
HAL_GPIO_Init(self->pin->port, &GPIO_InitStruct);
76+
HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct);
7777

7878
common_hal_digitalio_digitalinout_set_pull(self, pull);
7979
}
@@ -89,38 +89,38 @@ void common_hal_digitalio_digitalinout_switch_to_output(
8989
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
9090
digitalio_digitalinout_obj_t *self) {
9191

92-
return (LL_GPIO_GetPinMode(self->pin->port, (1 << self->pin->number))
92+
return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number))
9393
== LL_GPIO_MODE_INPUT) ? DIRECTION_INPUT : DIRECTION_OUTPUT;
9494
}
9595

9696
void common_hal_digitalio_digitalinout_set_value(
9797
digitalio_digitalinout_obj_t *self, bool value) {
98-
HAL_GPIO_WritePin(self->pin->port, 1 << self->pin->number, value);
98+
HAL_GPIO_WritePin(pin_port(self->pin->number), pin_mask(self->pin->number), value);
9999
}
100100

101101
bool common_hal_digitalio_digitalinout_get_value(
102102
digitalio_digitalinout_obj_t *self) {
103-
return (LL_GPIO_GetPinMode(self->pin->port, (1 << self->pin->number)) == LL_GPIO_MODE_INPUT)
104-
? HAL_GPIO_ReadPin(self->pin->port, (1 << self->pin->number))
105-
: LL_GPIO_IsOutputPinSet(self->pin->port, (1 << self->pin->number));
103+
return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number)) == LL_GPIO_MODE_INPUT)
104+
? HAL_GPIO_ReadPin(pin_port(self->pin->number), pin_mask(self->pin->number))
105+
: LL_GPIO_IsOutputPinSet(pin_port(self->pin->number), pin_mask(self->pin->number));
106106
}
107107

108108
void common_hal_digitalio_digitalinout_set_drive_mode(
109109
digitalio_digitalinout_obj_t *self,
110110
digitalio_drive_mode_t drive_mode) {
111111
GPIO_InitTypeDef GPIO_InitStruct = {0};
112-
GPIO_InitStruct.Pin = (1 << self->pin->number);
112+
GPIO_InitStruct.Pin = pin_mask(self->pin->number);
113113
GPIO_InitStruct.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ?
114114
GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP);
115115
GPIO_InitStruct.Pull = GPIO_NOPULL;
116116
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
117-
HAL_GPIO_Init(self->pin->port, &GPIO_InitStruct);
117+
HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct);
118118
}
119119

120120
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
121121
digitalio_digitalinout_obj_t *self) {
122122

123-
return LL_GPIO_GetPinOutputType(self->pin->port, (1 << self->pin->port_number))
123+
return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), pin_mask(self->pin->number))
124124
== LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL;
125125
}
126126

@@ -129,13 +129,13 @@ void common_hal_digitalio_digitalinout_set_pull(
129129

130130
switch (pull) {
131131
case PULL_UP:
132-
LL_GPIO_SetPinPull(self->pin->port,(1 << self->pin->number),LL_GPIO_PULL_UP);
132+
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_UP);
133133
break;
134134
case PULL_DOWN:
135-
LL_GPIO_SetPinPull(self->pin->port,(1 << self->pin->number),LL_GPIO_PULL_DOWN);
135+
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_DOWN);
136136
break;
137137
case PULL_NONE:
138-
LL_GPIO_SetPinPull(self->pin->port,(1 << self->pin->number),LL_GPIO_PULL_NO);
138+
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_NO);
139139
break;
140140
default:
141141
break;
@@ -146,7 +146,7 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
146146
digitalio_digitalinout_obj_t *self) {
147147

148148

149-
switch (LL_GPIO_GetPinPull(self->pin->port,(1 << self->pin->number))) {
149+
switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number))) {
150150
case LL_GPIO_PULL_UP:
151151
return PULL_UP;
152152
case LL_GPIO_PULL_DOWN:

ports/stm32f4/common-hal/microcontroller/Pin.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,38 @@ void reset_all_pins(void) {
5454

5555
// Mark pin as free and return it to a quiescent state.
5656
void reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
57-
if (pin_number == NO_PIN) {
57+
if (pin_port == 0x0F) {
5858
return;
5959
}
6060

6161
// Clear claimed bit.
62-
claimed_pins[pin_port] &= ~(1 << pin_number);
62+
claimed_pins[pin_port] &= ~(1<<pin_number);
6363
// Reset the pin
64-
HAL_GPIO_DeInit(ports[pin_port], (1 << pin_number));
64+
HAL_GPIO_DeInit(ports[pin_port], 1<<pin_number);
6565
}
6666

6767

6868
void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
69-
never_reset_pins[pin_port] |= 1 << pin_number;
69+
never_reset_pins[pin_port] |= 1<<pin_number;
7070
}
7171

7272
void claim_pin(const mcu_pin_obj_t* pin) {
7373
// Set bit in claimed_pins bitmask.
74-
claimed_pins[pin->port_number] |= 1 << pin->number;
74+
claimed_pins[pin->port] |= 1<<pin->number;
7575
}
7676

77-
7877
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) {
79-
return !(claimed_pins[pin_port] & (1 << pin_number));
78+
return !(claimed_pins[pin_port] & 1<<pin_number);
8079
}
8180

8281
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
82+
return pin_number_is_free(pin->port, pin->number);
83+
}
8384

84-
return pin_number_is_free(pin->port_number, pin->number);
85+
GPIO_TypeDef * pin_port(uint8_t pin_port) {
86+
return ports[pin_port];
87+
}
8588

89+
uint16_t pin_mask(uint8_t pin_number) {
90+
return 1<<pin_number;
8691
}

ports/stm32f4/common-hal/microcontroller/Pin.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
void reset_all_pins(void);
3535
// reset_pin_number takes the pin number instead of the pointer so that objects don't
3636
// need to store a full pointer.
37-
void reset_pin_number(uint8_t pin_port, uint8_t pin);
37+
void reset_pin_number(uint8_t pin_port, uint8_t pin_number);
3838
void claim_pin(const mcu_pin_obj_t* pin);
3939
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number);
4040
void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number);
41+
GPIO_TypeDef * pin_port(uint8_t pin_port);
42+
uint16_t pin_mask(uint8_t pin_number);
4143

4244
#endif // MICROPY_INCLUDED_STM34F4_COMMON_HAL_MICROCONTROLLER_PIN_H

ports/stm32f4/peripherals/stm32f4/pins.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,38 @@
3838

3939
typedef struct {
4040
mp_obj_base_t base;
41-
//uint8_t number; //(3)port,(5)pin
42-
uint8_t port_number;
43-
GPIO_TypeDef * port;
44-
uint8_t number;
41+
uint8_t port:4;
42+
uint8_t number:4;
43+
uint8_t adc_unit:3;
44+
uint8_t adc_channel:5;
4545
} mcu_pin_obj_t;
4646

47-
// extern GPIO_TypeDef *port_lookup_table[];
47+
#define ADC_1 1
48+
#define ADC_123 7
49+
#define ADC_12 3
50+
#define ADC_3 4
4851

49-
// static inline GPIO_TypeDef * get_GPIO_ptr(const mcu_pin_obj_t *p)
50-
// {
51-
// return port_lookup_table[0x7&( (p->number) >> 5)];
52-
// }
52+
//STM32 ADC pins can have a combination of 1, 2 or all 3 ADCs on a single pin,
53+
//but all 3 ADCs will share the same input number per pin.
54+
//F4 family has 3 ADC max, 24 channels max.
55+
#define ADC_INPUT(mask, number) \
56+
.adc_unit = mask, \
57+
.adc_channel = number,
58+
59+
#define NO_ADC \
60+
.adc_unit = 0x00, \
61+
.adc_channel = 0x1f
5362

5463
extern const mp_obj_type_t mcu_pin_type;
5564

56-
// Used in device-specific pins.c
57-
#define PIN(p_port_num, p_port, p_number) \
65+
// STM32 can have up to 9 ports, each restricted to 16 pins
66+
// We split the pin/port evenly, in contrast to nrf.
67+
#define PIN(p_port, p_number, p_adc) \
5868
{ \
5969
{ &mcu_pin_type }, \
60-
.port_number = (p_port_num), \
61-
.port = (p_port), \
62-
.number = (p_number), \
70+
.port = p_port, \
71+
.number = p_number, \
72+
p_adc \
6373
}
6474

6575
// Use illegal pin value to mark unassigned pins.

0 commit comments

Comments
 (0)