rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stm32_pins.cpp
Go to the documentation of this file.
1/**
2 * @file stm32_pins.cpp
3 * @brief STM32-compatible GPIO code
4 *
5 * @date Jun 02, 2019
6 * @author Andrey Belomutskiy, (c) 2012-2020
7 */
8
9#include "pch.h"
10
11#include "smart_gpio.h"
12
13using namespace rusefi::stringutil;
14
15#if EFI_GPIO_HARDWARE
16
17static const struct port_io {
18 const ioportid_t port;
19 const char *name;
20} ports[] = {
21 { GPIOA, "PA" },
22 { GPIOB, "PB" },
23 { GPIOC, "PC" },
24 { GPIOD, "PD" },
25#if STM32_HAS_GPIOE
26 { GPIOE, "PE" },
27#endif /* STM32_HAS_GPIOE */
28#if STM32_HAS_GPIOF
29 { GPIOF, "PF" },
30#endif /* STM32_HAS_GPIOF */
31#if STM32_HAS_GPIOG
32 { GPIOG, "PG" },
33#endif /* STM32_HAS_GPIOG */
34#if STM32_HAS_GPIOH
35 { GPIOH, "PH" },
36#endif /* STM32_HAS_GPIOH */
37#if STM32_HAS_GPIOI
38 { GPIOI, "PI" },
39#endif /* STM32_HAS_GPIOI */
40#if STM32_HAS_GPIOJ
41 { GPIOJ, "PJ" },
42#endif /* STM32_HAS_GPIOJ */
43#if STM32_HAS_GPIOK
44 { GPIOK, "PK" },
45#endif /* STM32_HAS_GPIOK */
46};
47
49 return (brainPin - Gpio::A0) % PORT_SIZE;
50}
51
53 size_t idx = (brainPin - Gpio::A0) / PORT_SIZE;
54 if (idx < efi::size(ports)) {
55 return ports[idx].port;
56 }
57
58 return nullptr;
59}
60
61const char *portname(ioportid_t port) {
62 efiAssert(ObdCode::CUSTOM_ERR_ASSERT, port != NULL, "null port", nullptr);
63 for (size_t idx = 0; idx < efi::size(ports); idx++) {
64 if (ports[idx].port == port) {
65 return ports[idx].name;
66 }
67 }
68 return "unknown";
69}
70
71static int getPortIndex(ioportid_t port) {
72 for (size_t idx = 0; idx < efi::size(ports); idx++) {
73 if (ports[idx].port == port) {
74 return idx;
75 }
76 }
77
78#ifndef EFI_BOOTLOADER
80#endif
81
82 return -1;
83}
84
86 int portIndex = getPortIndex(port);
87 return portIndex * PORT_SIZE + pin;
88}
89
90ioportid_t getHwPort(const char *msg, brain_pin_e brainPin) {
91 (void)msg;
92
93 if (!isBrainPinValid(brainPin)) {
94/*
95 * https://github.com/dron0gus please help
96 firmwareError(ObdCode::CUSTOM_ERR_INVALID_PIN, "%s: Invalid Gpio: %d", msg, brainPin);
97 */
98 return nullptr;
99 }
100 size_t idx = (brainPin - Gpio::A0) / PORT_SIZE;
101 if (idx < efi::size(ports)) {
102 return ports[idx].port;
103 }
104
105#ifndef EFI_BOOTLOADER
107#endif
108
109 return nullptr;
110}
111
112/**
113 * this method returns the numeric part of pin name. For instance, for PC13 this would return '13'
114 */
115ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin) {
116 if (!isBrainPinValid(brainPin))
117 return EFI_ERROR_CODE;
118
119 if (brain_pin_is_onchip(brainPin))
120 return getBrainPinIndex(brainPin);
121
122
123// huh why conditional on EFI_BOOTLOADER? some weird technical debt while does it fail only with debug options?
124#ifndef EFI_BOOTLOADER
125 criticalError("%s: Invalid on-chip Gpio: %d", msg, (int)brainPin);
126#endif // EFI_BOOTLOADER
127 return EFI_ERROR_CODE;
128}
129
130/**
131 * Parse string representation of physical pin into Gpio ordinal.
132 *
133 * @return Gpio::Unassigned for "none", Gpio::Invalid for invalid entry
134 */
135brain_pin_e parseBrainPin(const char *str) {
136 if (strEqual(str, "none"))
137 return Gpio::Unassigned;
138 // todo: create method toLowerCase?
139 if (str[0] != 'p' && str[0] != 'P') {
140 return Gpio::Invalid;
141 }
142 char port = str[1];
143 brain_pin_e basePin;
144 if (port >= 'a' && port <= 'z') {
145 basePin = Gpio::A0 + PORT_SIZE * (port - 'a');
146 } else if (port >= 'A' && port <= 'Z') {
147 basePin = Gpio::A0 + PORT_SIZE * (port - 'A');
148 } else {
149 return Gpio::Invalid;
150 }
151 const char *pinStr = str + 2;
152 int pin = atoi(pinStr);
153 return (brain_pin_e)(basePin + pin);
154}
155
156unsigned int getBrainPinOnchipNum(void) {
157 return BRAIN_PIN_ONCHIP_PINS;
158}
159
160static const char *od_names[2] = { "Push-Pull", "Open-Drain" };
161static const char *speed_names[4] = { "Low", "Medium", "High", "Very High" };
162static const char *pull_names[4] = { "No pull", "Pull-up", "Pull-down", "Reserved" };
163
164void debugBrainPin(char *buffer, size_t size, brain_pin_e brainPin) {
165 ioportid_t port = getBrainPinPort(brainPin);
166 int pin = getBrainPinIndex(brainPin);
167
168 if (port == nullptr) {
169 buffer[0] = '\0';
170 return;
171 }
172
173 uint32_t mode = (port->MODER >> (pin * 2)) & 0x03;
174
175 if (mode == 0) {
176 chsnprintf(buffer, size, "Input %s IN %d",
177 pull_names[(port->PUPDR >> (pin * 2)) & 0x03],
178 (port->IDR >> pin) & 0x01);
179 } else if (mode == 1) {
180 chsnprintf(buffer, size, "GP out %s %s Speed %s OUT %d IN %d",
181 od_names[(port->OTYPER >> pin) & 0x01],
182 pull_names[(port->PUPDR >> (pin * 2)) & 0x03],
183 speed_names[(port->OSPEEDR >> (pin * 2)) & 0x03],
184 (port->ODR >> pin) & 0x01,
185 (port->IDR >> pin) & 0x01);
186 } else if (mode == 2) {
187 uint32_t af = (pin < 8) ? port->AFRL : port->AFRH;
188 af = (af >> (4 * (pin & 0x07))) & 0x0f;
189 chsnprintf(buffer, size, "Mode AF%d %s %s Speed %s IN %d OUT %d",
190 af,
191 od_names[(port->OTYPER >> pin) & 0x01],
192 pull_names[(port->PUPDR >> (pin * 2)) & 0x03],
193 speed_names[(port->OSPEEDR >> (pin * 2)) & 0x03],
194 (port->IDR >> pin) & 0x01,
195 (port->ODR >> pin) & 0x01);
196 } else if (mode == 3) {
197 chsnprintf(buffer, size, "Mode Analog");
198 }
199}
200
201#endif /* EFI_GPIO_HARDWARE */
@ Unassigned
@ Invalid
void firmwareError(ObdCode code, const char *fmt,...)
uint32_t ioportmask_t
Digital I/O port sized unsigned type.
Definition hal_pal_lld.h:78
GPIO_TypeDef * ioportid_t
Port Identifier.
@ CUSTOM_ERR_UNKNOWN_PORT
@ CUSTOM_ERR_ASSERT
bool brain_pin_is_onchip(brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)
brain_pin_e pin
Definition stm32_adc.cpp:15
int getBrainPinIndex(brain_pin_e brainPin)
static const struct port_io ports[]
static const char * pull_names[4]
ioportid_t getHwPort(const char *msg, brain_pin_e brainPin)
brain_pin_e parseBrainPin(const char *str)
unsigned int getBrainPinOnchipNum(void)
static int getPortIndex(ioportid_t port)
const char * portname(ioportid_t port)
int getPortPinIndex(ioportid_t port, ioportmask_t pin)
static const char * od_names[2]
static const char * speed_names[4]
void debugBrainPin(char *buffer, size_t size, brain_pin_e brainPin)
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin)
ioportid_t getBrainPinPort(brain_pin_e brainPin)
composite packet size
static BigBufferHandle buffer