From ec065de5edafd280f8d6c9e6a343e721eee4b15b Mon Sep 17 00:00:00 2001 From: prosper00 <brad.roy@gmail.com> Date: Tue, 23 May 2023 21:52:55 -0600 Subject: [PATCH] replaced multiplies with bitshifts. Padded GPIO enum def's --- examples/GPIO/wiring.c | 39 ++++++++++++++++++++++--------- examples/GPIO/wiring.h | 15 ++++++++++++ examples/GPIO_analogRead/wiring.c | 39 ++++++++++++++++++++++--------- examples/GPIO_analogRead/wiring.h | 15 ++++++++++++ 4 files changed, 86 insertions(+), 22 deletions(-) diff --git a/examples/GPIO/wiring.c b/examples/GPIO/wiring.c index ce706a3..b8a71d3 100644 --- a/examples/GPIO/wiring.c +++ b/examples/GPIO/wiring.c @@ -6,9 +6,12 @@ enum GPIOports getPort (enum GPIOpins pin) { - if (pin <= pin_A2) { + if (pin <= pin_A7) { return port_A; } + else if (pin <= pin_B7) { + return port_B; + } else if (pin <= pin_C7) { return port_C; } @@ -26,6 +29,8 @@ void portEnable(enum GPIOports port) { case port_A: RCC->APB2PCENR |= RCC_APB2Periph_GPIOA; break; + case port_B: + break; case port_C: RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; break; @@ -41,19 +46,23 @@ void portEnable(enum GPIOports port) { void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) { GPIO_TypeDef * GPIOx; - uint16_t PinOffset = 4; - - if (pin <= pin_A2) { + uint16_t PinOffset = 0; + + if (pin <= pin_A7) { GPIOx = GPIOA; - PinOffset *= pin; + PinOffset = pin << 2; + } + else if (pin <= pin_B7) { /* GPIOB doesn't actually exist (yet?) + GPIOx = GPIOB; + PinOffset = (pin-8) << 2; */ } else if (pin <= pin_C7) { GPIOx = GPIOC; - PinOffset *= (pin - 2); + PinOffset = (pin - 16) << 2; } else if (pin <= pin_D7) { GPIOx = GPIOD; - PinOffset *= (pin - 10); + PinOffset = (pin - 24) << 2; } else { return; @@ -114,13 +123,17 @@ void digitalWrite(enum GPIOpins pin, uint8_t value) { GPIOx = GPIOA; PinOffset = pin; } + else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?) + GPIOx = GPIOB; + PinOffset = (pin - 8); */ + } else if (pin <= pin_C7) { GPIOx = GPIOC; - PinOffset = (pin - 2); + PinOffset = (pin - 16); } else if (pin <= pin_D7) { GPIOx = GPIOD; - PinOffset = (pin - 10); + PinOffset = (pin - 24); } else { return; @@ -144,13 +157,17 @@ uint8_t digitalRead(uint8_t pin) { GPIOx = GPIOA; PinOffset = pin; } + else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?) + GPIOx = GPIOB; + PinOffset = (pin - 8); */ + } else if (pin <= pin_C7) { GPIOx = GPIOC; - PinOffset = (pin - 2); + PinOffset = (pin - 16); } else if (pin <= pin_D7) { GPIOx = GPIOD; - PinOffset = (pin - 10); + PinOffset = (pin - 24); } else { return 0; diff --git a/examples/GPIO/wiring.h b/examples/GPIO/wiring.h index c54f9fb..2baa44f 100644 --- a/examples/GPIO/wiring.h +++ b/examples/GPIO/wiring.h @@ -14,14 +14,29 @@ enum lowhigh { enum GPIOports{ port_A, + port_B, port_C, port_D, port_none, }; enum GPIOpins{ + pin_A0, pin_A1, pin_A2, + pin_A3, + pin_A4, + pin_A5, + pin_A6, + pin_A7, + pin_B0, + pin_B1, + pin_B2, + pin_B3, + pin_B4, + pin_B5, + pin_B6, + pin_B7, pin_C0, pin_C1, pin_C2, diff --git a/examples/GPIO_analogRead/wiring.c b/examples/GPIO_analogRead/wiring.c index ce706a3..b8a71d3 100644 --- a/examples/GPIO_analogRead/wiring.c +++ b/examples/GPIO_analogRead/wiring.c @@ -6,9 +6,12 @@ enum GPIOports getPort (enum GPIOpins pin) { - if (pin <= pin_A2) { + if (pin <= pin_A7) { return port_A; } + else if (pin <= pin_B7) { + return port_B; + } else if (pin <= pin_C7) { return port_C; } @@ -26,6 +29,8 @@ void portEnable(enum GPIOports port) { case port_A: RCC->APB2PCENR |= RCC_APB2Periph_GPIOA; break; + case port_B: + break; case port_C: RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; break; @@ -41,19 +46,23 @@ void portEnable(enum GPIOports port) { void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) { GPIO_TypeDef * GPIOx; - uint16_t PinOffset = 4; - - if (pin <= pin_A2) { + uint16_t PinOffset = 0; + + if (pin <= pin_A7) { GPIOx = GPIOA; - PinOffset *= pin; + PinOffset = pin << 2; + } + else if (pin <= pin_B7) { /* GPIOB doesn't actually exist (yet?) + GPIOx = GPIOB; + PinOffset = (pin-8) << 2; */ } else if (pin <= pin_C7) { GPIOx = GPIOC; - PinOffset *= (pin - 2); + PinOffset = (pin - 16) << 2; } else if (pin <= pin_D7) { GPIOx = GPIOD; - PinOffset *= (pin - 10); + PinOffset = (pin - 24) << 2; } else { return; @@ -114,13 +123,17 @@ void digitalWrite(enum GPIOpins pin, uint8_t value) { GPIOx = GPIOA; PinOffset = pin; } + else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?) + GPIOx = GPIOB; + PinOffset = (pin - 8); */ + } else if (pin <= pin_C7) { GPIOx = GPIOC; - PinOffset = (pin - 2); + PinOffset = (pin - 16); } else if (pin <= pin_D7) { GPIOx = GPIOD; - PinOffset = (pin - 10); + PinOffset = (pin - 24); } else { return; @@ -144,13 +157,17 @@ uint8_t digitalRead(uint8_t pin) { GPIOx = GPIOA; PinOffset = pin; } + else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?) + GPIOx = GPIOB; + PinOffset = (pin - 8); */ + } else if (pin <= pin_C7) { GPIOx = GPIOC; - PinOffset = (pin - 2); + PinOffset = (pin - 16); } else if (pin <= pin_D7) { GPIOx = GPIOD; - PinOffset = (pin - 10); + PinOffset = (pin - 24); } else { return 0; diff --git a/examples/GPIO_analogRead/wiring.h b/examples/GPIO_analogRead/wiring.h index c54f9fb..2baa44f 100644 --- a/examples/GPIO_analogRead/wiring.h +++ b/examples/GPIO_analogRead/wiring.h @@ -14,14 +14,29 @@ enum lowhigh { enum GPIOports{ port_A, + port_B, port_C, port_D, port_none, }; enum GPIOpins{ + pin_A0, pin_A1, pin_A2, + pin_A3, + pin_A4, + pin_A5, + pin_A6, + pin_A7, + pin_B0, + pin_B1, + pin_B2, + pin_B3, + pin_B4, + pin_B5, + pin_B6, + pin_B7, pin_C0, pin_C1, pin_C2, -- GitLab