Skip to content

Commit 9a62195

Browse files
committed
Add constexpr utility functions to search for pin mapping
1 parent a7b298f commit 9a62195

File tree

2 files changed

+252
-6
lines changed

2 files changed

+252
-6
lines changed

hal/explicit_pinmap.c renamed to hal/explicit_pinmap.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
*/
1717

1818
#include "mbed_error.h"
19-
#include "spi_api.h"
20-
#include "pwmout_api.h"
21-
#include "analogin_api.h"
22-
#include "analogout_api.h"
23-
#include "i2c_api.h"
24-
#include "serial_api.h"
19+
#include "explicit_pinmap.h"
2520

2621

2722
/*

hal/explicit_pinmap.h

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018-2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef EXPLICIT_PINMAP_H
18+
#define EXPLICIT_PINMAP_H
19+
20+
#include "PinNames.h"
21+
#include "spi_api.h"
22+
#include "pwmout_api.h"
23+
#include "analogin_api.h"
24+
#include "analogout_api.h"
25+
#include "i2c_api.h"
26+
#include "serial_api.h"
27+
#include <mstd_cstddef>
28+
29+
#if EXPLICIT_PINMAP_READY
30+
#include "PeripheralPinMaps.h"
31+
32+
#if DEVICE_PWMOUT
33+
MSTD_CONSTEXPR_FN_14 PinMap get_pwm_pinmap(const PinName pin)
34+
{
35+
for (const PinMap &pinmap : PINMAP_PWM) {
36+
if (pinmap.pin == pin) {
37+
return {pin, pinmap.peripheral, pinmap.function};
38+
}
39+
}
40+
return {NC, NC, NC};
41+
}
42+
#endif // DEVICE_PWMOUT
43+
44+
#if DEVICE_ANALOGIN
45+
MSTD_CONSTEXPR_FN_14 PinMap get_analogin_pinmap(const PinName pin)
46+
{
47+
for (const PinMap &pinmap : PINMAP_ANALOGIN) {
48+
if (pinmap.pin == pin) {
49+
return {pin, pinmap.peripheral, pinmap.function};
50+
}
51+
}
52+
return {NC, NC, NC};
53+
}
54+
#endif // DEVICE_ANALOGIN
55+
56+
#if DEVICE_ANALOGOUT
57+
MSTD_CONSTEXPR_FN_14 PinMap get_analogout_pinmap(const PinName pin)
58+
{
59+
for (const PinMap &pinmap : PINMAP_ANALOGOUT) {
60+
if (pinmap.pin == pin) {
61+
return {pin, pinmap.peripheral, pinmap.function};
62+
}
63+
}
64+
return {NC, NC, NC};
65+
}
66+
#endif // DEVICE_ANALOGOUT
67+
68+
#if DEVICE_I2C
69+
MSTD_CONSTEXPR_FN_14 i2c_pinmap_t get_i2c_pinmap(const PinName sda, const PinName scl)
70+
{
71+
const PinMap *sda_map = nullptr;
72+
for (const PinMap &pinmap : PINMAP_I2C_SDA) {
73+
if (pinmap.pin == sda) {
74+
sda_map = &pinmap;
75+
break;
76+
}
77+
}
78+
79+
const PinMap *scl_map = nullptr;
80+
for (const PinMap &pinmap : PINMAP_I2C_SCL) {
81+
if (pinmap.pin == scl) {
82+
scl_map = &pinmap;
83+
break;
84+
}
85+
}
86+
87+
if (!sda_map || !scl_map || sda_map->peripheral != scl_map->peripheral) {
88+
return {NC, NC, NC, NC, NC};
89+
}
90+
91+
return {sda_map->peripheral, sda_map->pin, sda_map->function, scl_map->pin, scl_map->function};
92+
}
93+
#endif //DEVICE_I2C
94+
95+
#if DEVICE_SERIAL
96+
MSTD_CONSTEXPR_FN_14 serial_pinmap_t get_uart_pinmap(const PinName tx, const PinName rx)
97+
{
98+
const PinMap *tx_map = nullptr;
99+
for (const PinMap &pinmap : PINMAP_UART_TX) {
100+
if (pinmap.pin == tx) {
101+
tx_map = &pinmap;
102+
break;
103+
}
104+
}
105+
106+
const PinMap *rx_map = nullptr;
107+
for (const PinMap &pinmap : PINMAP_UART_RX) {
108+
if (pinmap.pin == rx) {
109+
rx_map = &pinmap;
110+
break;
111+
}
112+
}
113+
114+
if (!tx_map || !rx_map || rx_map->peripheral != tx_map->peripheral) {
115+
return {NC, NC, NC, NC, NC, false};
116+
}
117+
118+
if (tx_map->pin == STDIO_UART_TX && rx_map->pin == STDIO_UART_RX) {
119+
return {tx_map->peripheral, tx_map->pin, tx_map->function, rx_map->pin, rx_map->function, true};
120+
} else {
121+
return {tx_map->peripheral, tx_map->pin, tx_map->function, rx_map->pin, rx_map->function, false};
122+
}
123+
}
124+
125+
#if DEVICE_SERIAL_FC
126+
MSTD_CONSTEXPR_FN_14 serial_fc_pinmap_t get_uart_fc_pinmap(const PinName rxflow, const PinName txflow)
127+
{
128+
const PinMap *rts_map = nullptr;
129+
for (const PinMap &pinmap : PINMAP_UART_RTS) {
130+
if (pinmap.pin == rxflow) {
131+
rts_map = &pinmap;
132+
break;
133+
}
134+
}
135+
136+
const PinMap *cts_map = nullptr;
137+
for (const PinMap &pinmap : PINMAP_UART_CTS) {
138+
if (pinmap.pin == txflow) {
139+
cts_map = &pinmap;
140+
break;
141+
}
142+
}
143+
144+
if ((!rts_map || !cts_map) || (rts_map->peripheral != cts_map->peripheral)) {
145+
return {NC, NC, NC, NC, NC};
146+
}
147+
148+
return {cts_map->peripheral, cts_map->pin, cts_map->function, rts_map->pin, rts_map->function};
149+
}
150+
#endif // DEVICE_SERIAL_FC
151+
#endif // DEVICE_SERIAL
152+
153+
#if DEVICE_SPI
154+
MSTD_CONSTEXPR_FN_14 spi_pinmap_t get_spi_pinmap(const PinName mosi, const PinName miso, const PinName sclk, const PinName ssel)
155+
{
156+
const PinMap *mosi_map = nullptr;
157+
for (const PinMap &pinmap : PINMAP_SPI_MOSI) {
158+
if (pinmap.pin == mosi) {
159+
mosi_map = &pinmap;
160+
break;
161+
}
162+
}
163+
164+
const PinMap *miso_map = nullptr;
165+
for (const PinMap &pinmap : PINMAP_SPI_MISO) {
166+
if (pinmap.pin == miso) {
167+
miso_map = &pinmap;
168+
break;
169+
}
170+
}
171+
172+
const PinMap *sclk_map = nullptr;
173+
for (const PinMap &pinmap : PINMAP_SPI_SCLK) {
174+
if (pinmap.pin == sclk) {
175+
sclk_map = &pinmap;
176+
break;
177+
}
178+
}
179+
180+
const PinMap *ssel_map = nullptr;
181+
for (const PinMap &pinmap : PINMAP_SPI_SSEL) {
182+
if (pinmap.pin == ssel) {
183+
ssel_map = &pinmap;
184+
break;
185+
}
186+
}
187+
188+
if ((!mosi_map || !miso_map || !sclk_map || !ssel_map) ||
189+
(mosi_map->peripheral != miso_map->peripheral || mosi_map->peripheral != sclk_map->peripheral) ||
190+
(ssel_map->pin != NC && mosi_map->peripheral != ssel_map->peripheral)) {
191+
return {NC, NC, NC, NC, NC, NC, NC, NC, NC};
192+
}
193+
194+
return {mosi_map->peripheral, mosi_map->pin, mosi_map->function, miso_map->pin, miso_map->function, sclk_map->pin, sclk_map->function, ssel_map->pin, ssel_map->function};
195+
}
196+
#endif // DEVICE_SPI
197+
198+
#else // EXPLICIT_PINMAP_READY
199+
200+
#if DEVICE_PWMOUT
201+
MSTD_CONSTEXPR_FN_14 PinMap get_pwm_pinmap(const PinName pin)
202+
{
203+
return {pin, NC, NC};
204+
}
205+
#endif // DEVICE_PWMOUT
206+
207+
#if DEVICE_ANALOGIN
208+
MSTD_CONSTEXPR_FN_14 PinMap get_analogin_pinmap(const PinName pin)
209+
{
210+
return {pin, NC, NC};
211+
}
212+
#endif // DEVICE_ANALOGIN
213+
214+
#if DEVICE_ANALOGOUT
215+
MSTD_CONSTEXPR_FN_14 PinMap get_analogout_pinmap(const PinName pin)
216+
{
217+
return {pin, NC, NC};
218+
}
219+
#endif // DEVICE_ANALOGOUT
220+
221+
#if DEVICE_I2C
222+
MSTD_CONSTEXPR_FN_14 i2c_pinmap_t get_i2c_pinmap(const PinName sda, const PinName scl)
223+
{
224+
return {NC, sda, NC, scl, NC};
225+
}
226+
#endif //DEVICE_I2C
227+
228+
#if DEVICE_SERIAL
229+
MSTD_CONSTEXPR_FN_14 serial_pinmap_t get_uart_pinmap(const PinName tx, const PinName rx)
230+
{
231+
return {NC, tx, NC, rx, NC, false};
232+
}
233+
234+
#if DEVICE_SERIAL_FC
235+
MSTD_CONSTEXPR_FN_14 serial_fc_pinmap_t get_uart_fc_pinmap(const PinName rxflow, const PinName txflow)
236+
{
237+
return {NC, txflow, NC, rxflow, NC};
238+
}
239+
#endif // DEVICE_SERIAL_FC
240+
#endif // DEVICE_SERIAL
241+
242+
#if DEVICE_SPI
243+
MSTD_CONSTEXPR_FN_14 spi_pinmap_t get_spi_pinmap(const PinName mosi, const PinName miso, const PinName sclk, const PinName ssel)
244+
{
245+
return {NC, mosi, NC, miso, NC, sclk, NC, ssel, NC};
246+
}
247+
#endif // DEVICE_SERIAL
248+
249+
#endif // EXPLICIT_PINMAP_READY
250+
251+
#endif // EXPLICIT_PINMAP_H

0 commit comments

Comments
 (0)