rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
cypress_pins.cpp
Go to the documentation of this file.
1/**
2 * @file cypress_pins.cpp
3 * @brief Cypress-compatible GPIO code
4 *
5 * @date Jun 02, 2019
6 * @author Andrey Belomutskiy, (c) 2012-2020
7 * @author andreika <prometheus.pcb@gmail.com>
8 */
9
10#include "pch.h"
11
12#if EFI_GPIO_HARDWARE
13
14static ioportid_t ports[] = {
15 GPIOA,
16 GPIOB,
17 GPIOC,
18 GPIOD,
19 GPIOE,
20 GPIOF,
21 GPIOG,
22 GPIOH,
23 GPIOI,
24 GPIOJ,
25 GPIOK,
26};
27
31
32#include "pin_repository.h"
33#include "io_pins.h"
34
36 return ports;
37}
38
40 return (brainPin - Gpio::A0) % PORT_SIZE;
41}
42
44 return getGpioPorts()[(brainPin - Gpio::A0) / PORT_SIZE];
45}
46
47/**
48 * @deprecated - use hwPortname() instead
49 */
50const char *portname(ioportid_t GPIOx) {
51 if (GPIOx == GPIOA)
52 return "PA";
53 if (GPIOx == GPIOB)
54 return "PB";
55 if (GPIOx == GPIOC)
56 return "PC";
57 if (GPIOx == GPIOD)
58 return "PD";
59#if defined(GPIOF)
60 if (GPIOx == GPIOE)
61 return "PE";
62#endif /* GPIOE */
63#if defined(GPIOF)
64 if (GPIOx == GPIOF)
65 return "PF";
66#endif /* GPIOF */
67#if defined(GPIOG)
68 if (GPIOx == GPIOG)
69 return "PG";
70#endif /* GPIOG */
71#if defined(GPIOH)
72 if (GPIOx == GPIOH)
73 return "PH";
74#endif /* GPIOH */
75#if defined(GPIOI)
76 if (GPIOx == GPIOI)
77 return "PI";
78#endif /* GPIOI */
79#if defined(GPIOJ_BASE)
80 if (GPIOx == GPIOJ)
81 return "PJ";
82#endif /* GPIOJ_BASE */
83#if defined(GPIOK_BASE)
84 if (GPIOx == GPIOK)
85 return "PK";
86#endif /* GPIOK_BASE */
87 return "unknown";
88}
89
90static int getPortIndex(ioportid_t port) {
91 efiAssert(ObdCode::CUSTOM_ERR_ASSERT, port != NULL, "null port", -1);
92 if (port == GPIOA)
93 return 0;
94 if (port == GPIOB)
95 return 1;
96 if (port == GPIOC)
97 return 2;
98 if (port == GPIOD)
99 return 3;
100#if defined(GPIOF)
101 if (port == GPIOE)
102 return 4;
103#endif /* GPIOE */
104#if defined(GPIOF)
105 if (port == GPIOF)
106 return 5;
107#endif /* GPIOF */
108#if defined(GPIOG)
109 if (port == GPIOG)
110 return 6;
111#endif /* GPIOG */
112#if defined(GPIOH)
113 if (port == GPIOH)
114 return 7;
115#endif /* GPIOH */
116#if defined(GPIOI)
117 if (port == GPIOI)
118 return 8;
119#endif /* STM32_HAS_GPIOI */
120#if defined(GPIOJ_BASE)
121 if (port == GPIOJ)
122 return 9;
123#endif /* GPIOJ_BASE */
124#if defined(GPIOK_BASE)
125 if (port == GPIOK)
126 return 10;
127#endif /* GPIOK_BASE */
129 return -1;
130}
131
133 int portIndex = getPortIndex(port);
134 return portIndex * PORT_SIZE + pin;
135}
136
137ioportid_t getHwPort(const char *msg, brain_pin_e brainPin) {
138 (void)msg;
139
140 if (!isBrainPinValid(brainPin)) {
141/*
142 * https://github.com/dron0gus please help
143 firmwareError(ObdCode::CUSTOM_ERR_INVALID_PIN, "%s: Invalid Gpio: %d", msg, brainPin);
144 */
145 return nullptr;
146 }
147 return getGpioPorts()[(brainPin - Gpio::A0) / PORT_SIZE];
148}
149
150/**
151 * this method returns the numeric part of pin name. For instance, for PC13 this would return '13'
152 */
153ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin) {
154 if (!isBrainPinValid(brainPin))
155 return EFI_ERROR_CODE;
156
157 if (brain_pin_is_onchip(brainPin))
158 return getBrainPinIndex(brainPin);
159
160 firmwareError(ObdCode::CUSTOM_ERR_INVALID_PIN, "%s: Invalid on-chip Gpio: %d", msg, brainPin);
161 return EFI_ERROR_CODE;
162}
163
164/**
165 * Parse string representation of physical pin into brain_pin_e ordinal.
166 *
167 * @return Gpio::Unassigned for "none", Gpio::Invalid for invalid entry
168 */
169brain_pin_e parseBrainPin(const char *str) {
170 if (strEqual(str, "none"))
171 return Gpio::Unassigned;
172 // todo: create method toLowerCase?
173 if (str[0] != 'p' && str[0] != 'P') {
174 return Gpio::Invalid;
175 }
176 char port = str[1];
177 if (port >= 'a' && port <= 'z') {
178 port = 10 + (port - 'a');
179 } else if (port >= 'A' && port <= 'Z') {
180 port = 10 + (port - 'A');
181 } else if (port >= '0' && port <= '9') {
182// cypress-specific code
183 port = 0 + (port - '0');
184 } else {
185 return Gpio::Invalid;
186 }
187 brain_pin_e basePin = portMap[(int)port];
188 if (basePin == Gpio::Invalid)
189 return Gpio::Invalid;
190 const char *pinStr = str + 2;
191 int pin = atoi(pinStr);
192 return basePin + pin;
193}
194
195unsigned int getBrainPinOnchipNum(void) {
196 return BRAIN_PIN_ONCHIP_PINS;
197}
198
199void debugBrainPin(char *buffer, size_t, brain_pin_e)
200{
201 buffer[0] = '\0';
202}
203
204#endif /* EFI_GPIO_HARDWARE */
@ Unassigned
@ Invalid
ioportid_t * getGpioPorts()
static brain_pin_e portMap[16]
int getBrainPinIndex(brain_pin_e brainPin)
static ioportid_t ports[]
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)
int getPortPinIndex(ioportid_t port, ioportmask_t pin)
void debugBrainPin(char *buffer, size_t, brain_pin_e)
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin)
const char * portname(ioportid_t GPIOx)
ioportid_t getBrainPinPort(brain_pin_e brainPin)
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.
this file is about general input/output utility methods, not much EFI-specifics
@ CUSTOM_ERR_UNKNOWN_PORT
@ CUSTOM_ERR_INVALID_PIN
@ CUSTOM_ERR_ASSERT
bool brain_pin_is_onchip(brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)
I/O pin registry header.
brain_pin_e pin
Definition stm32_adc.cpp:15
static BigBufferHandle buffer