Skip to content

Commit 516ac16

Browse files
committed
Add constexpr utility functions to search for pin mapping
1 parent d94c076 commit 516ac16

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "analogout_api.h"
2424
#include "i2c_api.h"
2525
#include "serial_api.h"
26+
#include "explicit_pinmap.h"
2627

2728
#if DEVICE_SPI
2829
MBED_WEAK void spi_init_direct(spi_t *obj, const spi_pinmap_t *pinmap)

hal/explicit_pinmap.h

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
#include "PeripheralPinMaps.h"
20+
21+
MSTD_CONSTEXPR_FN_14 PinMap get_pwm_pinmap(const PinName pin)
22+
{
23+
for (const PinMap &pinmap : PINMAP_PWM) {
24+
if (pinmap.pin == pin) {
25+
return {pin, pinmap.peripheral, pinmap.function};
26+
}
27+
}
28+
return {NC, NC, NC};
29+
}
30+
31+
MSTD_CONSTEXPR_FN_14 PinMap get_analogin_pinmap(const PinName pin)
32+
{
33+
for (const PinMap &pinmap : PINMAP_ANALOGIN) {
34+
if (pinmap.pin == pin) {
35+
return {pin, pinmap.peripheral, pinmap.function};
36+
}
37+
}
38+
return {NC, NC, NC};
39+
}
40+
41+
MSTD_CONSTEXPR_FN_14 PinMap get_analogout_pinmap(const PinName pin)
42+
{
43+
for (const PinMap &pinmap : PINMAP_ANALOGIN) {
44+
if (pinmap.pin == pin) {
45+
return {pin, pinmap.peripheral, pinmap.function};
46+
}
47+
}
48+
return {NC, NC, NC};
49+
}
50+
51+
MSTD_CONSTEXPR_FN_14 i2c_pinmap_t get_i2c_pinmap(const PinName sda, const PinName scl)
52+
{
53+
int sda_idx = NC;
54+
int scl_idx = NC;
55+
56+
for (uint32_t i = 0; i < (sizeof(PINMAP_I2C_SDA)/sizeof(PINMAP_I2C_SDA[0])); i++) {
57+
if (PINMAP_I2C_SDA[i].pin == sda) {
58+
sda_idx = i;
59+
break;
60+
}
61+
}
62+
63+
for (uint32_t i = 0; i < (sizeof(PINMAP_I2C_SCL)/sizeof(PINMAP_I2C_SCL[0])); i++) {
64+
if (PINMAP_I2C_SCL[i].pin == scl) {
65+
scl_idx = i;
66+
break;
67+
}
68+
}
69+
70+
if (sda_idx == NC || scl_idx == NC || PINMAP_I2C_SDA[sda_idx].peripheral != PINMAP_I2C_SCL[scl_idx].peripheral) {
71+
return {NC, NC, NC, NC, NC};
72+
}
73+
74+
return {PINMAP_I2C_SDA[sda_idx].peripheral, PINMAP_I2C_SDA[sda_idx].pin, PINMAP_I2C_SDA[sda_idx].function, PINMAP_I2C_SCL[scl_idx].pin, PINMAP_I2C_SCL[scl_idx].function};
75+
}
76+
77+
MSTD_CONSTEXPR_FN_14 serial_pinmap_t get_uart_pinmap(const PinName tx, const PinName rx)
78+
{
79+
int tx_idx = NC;
80+
int rx_idx = NC;
81+
82+
for (uint32_t i = 0; i < (sizeof(PINMAP_UART_TX)/sizeof(PINMAP_UART_TX[0])); i++) {
83+
if (PINMAP_UART_TX[i].pin == tx) {
84+
tx_idx = i;
85+
break;
86+
}
87+
}
88+
89+
for (uint32_t i = 0; i < (sizeof(PINMAP_UART_RX)/sizeof(PINMAP_UART_RX[0])); i++) {
90+
if (PINMAP_UART_RX[i].pin == rx) {
91+
rx_idx = i;
92+
break;
93+
}
94+
}
95+
96+
if (tx_idx == NC || rx_idx == NC || PINMAP_UART_TX[tx_idx].peripheral != PINMAP_UART_RX[rx_idx].peripheral) {
97+
return {NC, NC, NC, NC, NC};
98+
}
99+
100+
return {PINMAP_UART_TX[tx_idx].peripheral, PINMAP_UART_TX[tx_idx].pin, PINMAP_UART_TX[tx_idx].function, PINMAP_UART_RX[rx_idx].pin, PINMAP_UART_RX[rx_idx].function};
101+
}
102+
103+
MSTD_CONSTEXPR_FN_14 serial_fc_pinmap_t get_uart_fc_pinmap(const PinName rxflow, const PinName txflow)
104+
{
105+
int rxflow_idx = NC;
106+
int txflow_idx = NC;
107+
108+
for (uint32_t i = 0; i < (sizeof(PINMAP_UART_RTS)/sizeof(PINMAP_UART_RTS[0])); i++) {
109+
if (PINMAP_UART_RTS[i].pin == rxflow) {
110+
rxflow_idx = i;
111+
break;
112+
}
113+
}
114+
115+
for (uint32_t i = 0; i < (sizeof(PINMAP_UART_CTS)/sizeof(PINMAP_UART_CTS[0])); i++) {
116+
if (PINMAP_UART_CTS[i].pin == txflow) {
117+
txflow_idx = i;
118+
break;
119+
}
120+
}
121+
122+
if (rxflow_idx == NC || txflow_idx == NC || PINMAP_UART_RTS[rxflow_idx].peripheral != PINMAP_UART_CTS[txflow_idx].peripheral) {
123+
return {NC, NC, NC, NC, NC};
124+
}
125+
126+
return {PINMAP_UART_CTS[txflow_idx].peripheral, PINMAP_UART_CTS[txflow_idx].pin, PINMAP_UART_CTS[txflow_idx].function, PINMAP_UART_RTS[rxflow_idx].pin, PINMAP_UART_RTS[rxflow_idx].function};
127+
}
128+
129+
MSTD_CONSTEXPR_FN_14 spi_pinmap_t get_spi_pinmap(const PinName mosi, const PinName miso, const PinName sclk, const PinName ssel)
130+
{
131+
int mosi_idx = NC;
132+
int miso_idx = NC;
133+
int sclk_idx = NC;
134+
int ssel_idx = NC;
135+
136+
for (uint32_t i = 0; i < (sizeof(PINMAP_SPI_MOSI)/sizeof(PINMAP_SPI_MOSI[0])); i++) {
137+
if (PINMAP_SPI_MOSI[i].pin == mosi) {
138+
mosi_idx = i;
139+
break;
140+
}
141+
}
142+
143+
for (uint32_t i = 0; i < (sizeof(PINMAP_SPI_MISO)/sizeof(PINMAP_SPI_MISO[0])); i++) {
144+
if (PINMAP_SPI_MISO[i].pin == miso) {
145+
miso_idx = i;
146+
break;
147+
}
148+
}
149+
150+
for (uint32_t i = 0; i < (sizeof(PINMAP_SPI_SCLK)/sizeof(PINMAP_SPI_SCLK[0])); i++) {
151+
if (PINMAP_SPI_SCLK[i].pin == sclk) {
152+
sclk_idx = i;
153+
break;
154+
}
155+
}
156+
157+
if (ssel != NC) {
158+
for (uint32_t i = 0; i < (sizeof(PINMAP_SPI_SSEL)/sizeof(PINMAP_SPI_SSEL[0])); i++) {
159+
if (PINMAP_SPI_SSEL[i].pin == ssel) {
160+
ssel_idx = i;
161+
break;
162+
}
163+
}
164+
}
165+
166+
if (mosi_idx == NC || miso_idx == NC || sclk_idx == NC || (ssel != NC && ssel_idx == NC) ||
167+
PINMAP_SPI_MOSI[mosi_idx].peripheral != PINMAP_SPI_MISO[miso_idx].peripheral ||
168+
PINMAP_SPI_MOSI[mosi_idx].peripheral != PINMAP_SPI_SCLK[sclk_idx].peripheral ||
169+
(ssel != NC && PINMAP_SPI_MOSI[mosi_idx].peripheral != PINMAP_SPI_SSEL[ssel_idx].peripheral)) {
170+
return {NC, NC, NC, NC, NC, NC, NC, NC, NC};
171+
}
172+
173+
if (ssel != NC) {
174+
return {PINMAP_SPI_MOSI[mosi_idx].peripheral, PINMAP_SPI_MOSI[mosi_idx].pin, PINMAP_SPI_MOSI[mosi_idx].function, PINMAP_SPI_MISO[miso_idx].pin, PINMAP_SPI_MISO[miso_idx].function, PINMAP_SPI_SCLK[sclk_idx].pin, PINMAP_SPI_SCLK[sclk_idx].function, PINMAP_SPI_SSEL[ssel_idx].pin, PINMAP_SPI_SSEL[ssel_idx].function};
175+
} else {
176+
return {PINMAP_SPI_MOSI[mosi_idx].peripheral, PINMAP_SPI_MOSI[mosi_idx].pin, PINMAP_SPI_MOSI[mosi_idx].function, PINMAP_SPI_MISO[miso_idx].pin, PINMAP_SPI_MISO[miso_idx].function, PINMAP_SPI_SCLK[sclk_idx].pin, PINMAP_SPI_SCLK[sclk_idx].function, NC, NC};
177+
}
178+
}
179+
180+
#endif

0 commit comments

Comments
 (0)