Skip to content
Snippets Groups Projects
Commit ec065de5 authored by prosper00's avatar prosper00
Browse files

replaced multiplies with bitshifts. Padded GPIO enum def's

parent a277a274
No related branches found
No related tags found
No related merge requests found
...@@ -6,9 +6,12 @@ ...@@ -6,9 +6,12 @@
enum GPIOports getPort (enum GPIOpins pin) { enum GPIOports getPort (enum GPIOpins pin) {
if (pin <= pin_A2) { if (pin <= pin_A7) {
return port_A; return port_A;
} }
else if (pin <= pin_B7) {
return port_B;
}
else if (pin <= pin_C7) { else if (pin <= pin_C7) {
return port_C; return port_C;
} }
...@@ -26,6 +29,8 @@ void portEnable(enum GPIOports port) { ...@@ -26,6 +29,8 @@ void portEnable(enum GPIOports port) {
case port_A: case port_A:
RCC->APB2PCENR |= RCC_APB2Periph_GPIOA; RCC->APB2PCENR |= RCC_APB2Periph_GPIOA;
break; break;
case port_B:
break;
case port_C: case port_C:
RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; RCC->APB2PCENR |= RCC_APB2Periph_GPIOC;
break; break;
...@@ -41,19 +46,23 @@ void portEnable(enum GPIOports port) { ...@@ -41,19 +46,23 @@ void portEnable(enum GPIOports port) {
void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) { void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) {
GPIO_TypeDef * GPIOx; GPIO_TypeDef * GPIOx;
uint16_t PinOffset = 4; uint16_t PinOffset = 0;
if (pin <= pin_A2) { if (pin <= pin_A7) {
GPIOx = GPIOA; 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) { else if (pin <= pin_C7) {
GPIOx = GPIOC; GPIOx = GPIOC;
PinOffset *= (pin - 2); PinOffset = (pin - 16) << 2;
} }
else if (pin <= pin_D7) { else if (pin <= pin_D7) {
GPIOx = GPIOD; GPIOx = GPIOD;
PinOffset *= (pin - 10); PinOffset = (pin - 24) << 2;
} }
else { else {
return; return;
...@@ -114,13 +123,17 @@ void digitalWrite(enum GPIOpins pin, uint8_t value) { ...@@ -114,13 +123,17 @@ void digitalWrite(enum GPIOpins pin, uint8_t value) {
GPIOx = GPIOA; GPIOx = GPIOA;
PinOffset = pin; PinOffset = pin;
} }
else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?)
GPIOx = GPIOB;
PinOffset = (pin - 8); */
}
else if (pin <= pin_C7) { else if (pin <= pin_C7) {
GPIOx = GPIOC; GPIOx = GPIOC;
PinOffset = (pin - 2); PinOffset = (pin - 16);
} }
else if (pin <= pin_D7) { else if (pin <= pin_D7) {
GPIOx = GPIOD; GPIOx = GPIOD;
PinOffset = (pin - 10); PinOffset = (pin - 24);
} }
else { else {
return; return;
...@@ -144,13 +157,17 @@ uint8_t digitalRead(uint8_t pin) { ...@@ -144,13 +157,17 @@ uint8_t digitalRead(uint8_t pin) {
GPIOx = GPIOA; GPIOx = GPIOA;
PinOffset = pin; PinOffset = pin;
} }
else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?)
GPIOx = GPIOB;
PinOffset = (pin - 8); */
}
else if (pin <= pin_C7) { else if (pin <= pin_C7) {
GPIOx = GPIOC; GPIOx = GPIOC;
PinOffset = (pin - 2); PinOffset = (pin - 16);
} }
else if (pin <= pin_D7) { else if (pin <= pin_D7) {
GPIOx = GPIOD; GPIOx = GPIOD;
PinOffset = (pin - 10); PinOffset = (pin - 24);
} }
else { else {
return 0; return 0;
......
...@@ -14,14 +14,29 @@ enum lowhigh { ...@@ -14,14 +14,29 @@ enum lowhigh {
enum GPIOports{ enum GPIOports{
port_A, port_A,
port_B,
port_C, port_C,
port_D, port_D,
port_none, port_none,
}; };
enum GPIOpins{ enum GPIOpins{
pin_A0,
pin_A1, pin_A1,
pin_A2, 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_C0,
pin_C1, pin_C1,
pin_C2, pin_C2,
......
...@@ -6,9 +6,12 @@ ...@@ -6,9 +6,12 @@
enum GPIOports getPort (enum GPIOpins pin) { enum GPIOports getPort (enum GPIOpins pin) {
if (pin <= pin_A2) { if (pin <= pin_A7) {
return port_A; return port_A;
} }
else if (pin <= pin_B7) {
return port_B;
}
else if (pin <= pin_C7) { else if (pin <= pin_C7) {
return port_C; return port_C;
} }
...@@ -26,6 +29,8 @@ void portEnable(enum GPIOports port) { ...@@ -26,6 +29,8 @@ void portEnable(enum GPIOports port) {
case port_A: case port_A:
RCC->APB2PCENR |= RCC_APB2Periph_GPIOA; RCC->APB2PCENR |= RCC_APB2Periph_GPIOA;
break; break;
case port_B:
break;
case port_C: case port_C:
RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; RCC->APB2PCENR |= RCC_APB2Periph_GPIOC;
break; break;
...@@ -41,19 +46,23 @@ void portEnable(enum GPIOports port) { ...@@ -41,19 +46,23 @@ void portEnable(enum GPIOports port) {
void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) { void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) {
GPIO_TypeDef * GPIOx; GPIO_TypeDef * GPIOx;
uint16_t PinOffset = 4; uint16_t PinOffset = 0;
if (pin <= pin_A2) { if (pin <= pin_A7) {
GPIOx = GPIOA; 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) { else if (pin <= pin_C7) {
GPIOx = GPIOC; GPIOx = GPIOC;
PinOffset *= (pin - 2); PinOffset = (pin - 16) << 2;
} }
else if (pin <= pin_D7) { else if (pin <= pin_D7) {
GPIOx = GPIOD; GPIOx = GPIOD;
PinOffset *= (pin - 10); PinOffset = (pin - 24) << 2;
} }
else { else {
return; return;
...@@ -114,13 +123,17 @@ void digitalWrite(enum GPIOpins pin, uint8_t value) { ...@@ -114,13 +123,17 @@ void digitalWrite(enum GPIOpins pin, uint8_t value) {
GPIOx = GPIOA; GPIOx = GPIOA;
PinOffset = pin; PinOffset = pin;
} }
else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?)
GPIOx = GPIOB;
PinOffset = (pin - 8); */
}
else if (pin <= pin_C7) { else if (pin <= pin_C7) {
GPIOx = GPIOC; GPIOx = GPIOC;
PinOffset = (pin - 2); PinOffset = (pin - 16);
} }
else if (pin <= pin_D7) { else if (pin <= pin_D7) {
GPIOx = GPIOD; GPIOx = GPIOD;
PinOffset = (pin - 10); PinOffset = (pin - 24);
} }
else { else {
return; return;
...@@ -144,13 +157,17 @@ uint8_t digitalRead(uint8_t pin) { ...@@ -144,13 +157,17 @@ uint8_t digitalRead(uint8_t pin) {
GPIOx = GPIOA; GPIOx = GPIOA;
PinOffset = pin; PinOffset = pin;
} }
else if (pin <= pin_B7) { /* GPIOB doesn't exist (yet?)
GPIOx = GPIOB;
PinOffset = (pin - 8); */
}
else if (pin <= pin_C7) { else if (pin <= pin_C7) {
GPIOx = GPIOC; GPIOx = GPIOC;
PinOffset = (pin - 2); PinOffset = (pin - 16);
} }
else if (pin <= pin_D7) { else if (pin <= pin_D7) {
GPIOx = GPIOD; GPIOx = GPIOD;
PinOffset = (pin - 10); PinOffset = (pin - 24);
} }
else { else {
return 0; return 0;
......
...@@ -14,14 +14,29 @@ enum lowhigh { ...@@ -14,14 +14,29 @@ enum lowhigh {
enum GPIOports{ enum GPIOports{
port_A, port_A,
port_B,
port_C, port_C,
port_D, port_D,
port_none, port_none,
}; };
enum GPIOpins{ enum GPIOpins{
pin_A0,
pin_A1, pin_A1,
pin_A2, 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_C0,
pin_C1, pin_C1,
pin_C2, pin_C2,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment