Skip to content

Commit 30a352e

Browse files
author
Amanda Butler
authored
Merge pull request #1156 from mprse/explicit_pinmap_porting_guide
Add the porting guide for the explicit pin-map extension.
2 parents 834b570 + 108effe commit 30a352e

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

docs/porting/target/static_pinmap.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<h1 id="static-pinmap-port">Static Pinmap extension</h1>
2+
3+
The **Static Pinmap extension** allows you to statically specify the peripheral configuration (pin, peripheral or function) in the HAL API function.
4+
5+
In modern MCUs, you can often map peripherals to different pins, and each pin can have multiple functions. Mbed supports dynamic pin mapping, meaning you can reconfigure pins at run time for different drivers to use. That provides great flexibility, but it's not free. There's a nontrivial ROM cost to maintain the pin map tables and infrastructure to parse it. In some use cases, this flexibility is worth the cost. Often, pin configuration is frozen at the hardware design stage and doesn't require run time modification. Shifting this configuration to compile time allows free memory associated with the dynamic approach.
6+
7+
HAL APIs using pins take these pins in their constructor and use those pins to look up which peripheral or function to use. The process of looking up the peripheral or function requires there to be a pin map table that maps pins to peripherals or functions. This pin map table takes up ROM. Static pinmap extension provides additional HAL API constructors, which take pin map as a parameter where the pin, peripheral or function is specified statically, and there is no need to use the pin map tables.
8+
9+
Supported peripherals:
10+
11+
- `PWM`.
12+
- `AnalogIn`.
13+
- `AnalogOut`.
14+
- `SPI`.
15+
- `I2C`.
16+
- `UART`.
17+
- `QSPI`.
18+
- `CAN`.
19+
 
20+
## Implementing static pin map extension
21+
22+
If you want to make static pin map available on your platform please perform the following steps:  
23+
24+
- Provide implementation of `xxx_init_direct(xxx_t *obj, static_pinmap_t *)` function and update implementation of `xxx_init()`.
25+
- `xxx_init()` uses pin map tables to determine the associated peripheral or function with the given pins, populates the pin map structure and calls void `xxx_init_direct()`.
26+
- `xxx_init_direct()` performs peripheral initialization using given static pin map structure.
27+
28+
Example implementation:
29+
30+
```
31+
void xxx_init_direct(xxx_t *obj, const PinMap *pinmap)
32+
{
33+
obj->spi.instance = pinmap->peripheral;
34+
35+
// pin out the xxx pins
36+
pin_function(pinmap->pin, pinmap->function);
37+
pin_mode(pinmap->pin, PullNone);
38+
39+
// Some additional init code
40+
}
41+
42+
void xxx_init(xxx_t *obj, PinName pin)
43+
{
44+
int peripheral = (int)pinmap_peripheral(pin, PinMap_xxx);
45+
int function = (int)pinmap_find_function(pin, PinMap_xxx);
46+
47+
const PinMap static_pinmap = {pin, peripheral, function};
48+
49+
xxx_init_direct(obj, &static_pinmap);
50+
}
51+
```
52+
53+
- Provide `constexpr` pin-map tables in the header file.
54+
55+
Move pin map tables from `PeripheralPins.c` to `PeripheralPinMaps.h` (create new file) and add `constexpr` specifier in the pin map table declarations.
56+
57+
The tables are required in the header file, so constant expression utility functions can include and use them to find and return mapping without pulling the pin map table into the image.
58+
59+
<span class="notes">**Note:** Please include the `<mstd_cstddef>` module, and use the `MSTD_CONSTEXPR_OBJ_11` macro instead of the `constexpr` specifier. When `PeripheralPinMaps.h` is included from the Mbed OS C++ code, we need to see it as `constexpr`, but if the target code includes it from C, it has to have it as `const`.</span>
60+
61+
Example pin map table:
62+
63+
```
64+
#include <mstd_cstddef>
65+
66+
MSTD_CONSTEXPR_OBJ_11 PinMap PinMap_ADC[] = {
67+
{P0_23, ADC0_SE0, 0},
68+
{P0_10, ADC0_SE1, 0},
69+
{P0_31, ADC0_SE3, 0},
70+
{P1_8, ADC0_SE4, 0},
71+
{P2_0, ADC0_SE5, 0},
72+
{P2_13, ADC0_SE6, 0},
73+
{P2_11, ADC0_SE7, 0},
74+
{NC , NC , 0}
75+
};
76+
77+
```
78+
79+
- Provide macros for pin map tables.
80+
81+
Because pin map table names are not common across all targets, the following macros for available pin map tables are required in the `PeripheralPinMaps.h` file:
82+
83+
```
84+
#define PINMAP_ANALOGIN [PinMap ADC]
85+
#define PINMAP_ANALOGOUT [PinMap DAC]
86+
#define PINMAP_I2C_SDA [PinMap I2C SDA]
87+
#define PINMAP_I2C_SCL [PinMap I2C SCL]
88+
#define PINMAP_UART_TX [PinMap UART TX]
89+
#define PINMAP_UART_RX [PinMap UART RX]
90+
#define PINMAP_UART_CTS [PinMap UART CTS]
91+
#define PINMAP_UART_RTS [PinMap UART RTS]
92+
#define PINMAP_SPI_SCLK [PinMap SPI SCLK]
93+
#define PINMAP_SPI_MOSI [PinMap SPI MOSI]
94+
#define PINMAP_SPI_MISO [PinMap SPI MISO]
95+
#define PINMAP_SPI_SSEL [PinMap SPI SSEL]
96+
#define PINMAP_PWM [PinMap PWM]
97+
#define PINMAP_QSPI_DATA0 [PinMap QSPI DATA0]
98+
#define PINMAP_QSPI_DATA1 [PinMap QSPI DATA1]
99+
#define PINMAP_QSPI_DATA2 [PinMap QSPI DATA2]
100+
#define PINMAP_QSPI_DATA3 [PinMap QSPI DATA3]
101+
#define PINMAP_QSPI_SCLK [PinMap QSPI SCLK]
102+
#define PINMAP_QSPI_SSEL [PinMap QSPI SSEL]
103+
#define PINMAP_CAN_RD [PinMap CAN RD]
104+
#define PINMAP_CAN_TD [PinMap CAN RD]
105+
```
106+
107+
- Provide `STATIC_PINMAP_READY` macro in `PinNames.h`  
108+
109+
Adding this macro enables the static pin map support for the target.
110+
111+
```
112+
/* If this macro is defined, you can use constexpr utility functions for pin map search. */
113+
#define STATIC_PINMAP_READY 1
114+
```
115+
116+
## Testing
117+
118+
Use the code below to test the static pin map extension:
119+
120+
```
121+
int main()
122+
{
123+
    /* Regular use */
124+
    SPI spi(D1, D2, D3, D4);
125+
126+
    /* Static pinmap */
127+
    const spi_pinmap_t static_spi_pinmap = {SPI_1, D1, 2, D2, 2, D3, 2, D4, 2};
128+
    SPI spi(static_spi_pinmap);
129+
130+
    /* Static pinmap with constexpr utility function */
131+
    constexpr spi_pinmap_t static_spi_pinmap = get_spi_pinmap(D1, D2, D3, D4);
132+
    SPI spi(static_spi_pinmap);
133+
134+
    return 0;
135+
}
136+
```
137+
138+
When you use the static pin map extension, you save on ROM:  
139+
140+
```
141+
| Module | .text | .data | .bss |
142+
|---------------------------------|--------------|---------|------------|
143+
| [lib]\c_w.l | 11175(+0) | 16(+0) | 348(+0) |
144+
| [lib]\fz_wm.l | 34(+0) | 0(+0) | 0(+0) |
145+
| [lib]\m_wm.l | 48(+0) | 0(+0) | 0(+0) |
146+
| anon$$obj.o | 32(+0) | 0(+0) | 197888(+0) |
147+
| drivers\source | 192(+0) | 0(+0) | 0(+0) |
148+
| features\netsocket | 143(+0) | 0(+0) | 0(+0) |
149+
| hal\mbed_critical_section_api.o | 154(+0) | 0(+0) | 2(+0) |
150+
| hal\mbed_gpio.o | 96(+0) | 0(+0) | 0(+0) |
151+
| hal\mbed_pinmap_common.o | 0(-272) | 0(+0) | 0(+0) | // removed pinmap lib (this is common for all peripherals)
152+
| hal\mbed_pinmap_common.o | 0(-272) | 0(+0) | 0(+0) | // remove pinmap lib (this is common for all peripherals)
153+
| hal\mbed_ticker_api.o | 978(+0) | 0(+0) | 0(+0) |
154+
| hal\mbed_us_ticker_api.o | 114(+0) | 4(+0) | 65(+0) |
155+
| main.o | 70(+32) | 0(+0) | 0(+0) | // extra space for static pinmap structure in application
156+
| platform\source | 5683(+46) | 64(+0) | 249(+0) | // extra space for UART static pinmap structure to initialize the console
157+
| main.o | 70(+32) | 0(+0) | 0(+0) | // extra space for static pin map structure in application
158+
| platform\source | 5683(+46) | 64(+0) | 249(+0) | // extra space for UART static pin map structure to initialize the console
159+
| rtos\source | 8990(+0) | 168(+0) | 6626(+0) |
160+
| targets\TARGET_Freescale | 16581(-816) | 12(+0) | 340(+0) | // removed pinmaps + driver code reduction
161+
| targets\TARGET_Freescale | 16581(-816) | 12(+0) | 340(+0) | // remove pin maps and driver code reduction
162+
| Subtotals | 44290(-1010) | 264(+0) | 205518(+0) |
163+
Total Static RAM memory (data + bss): 205782(+0) bytes
164+
Total Flash memory (text + data): 44554(-1010) bytes
165+
Total static RAM memory (data + bss): 205782(+0) bytes
166+
Total flash memory (text + data): 44554(-1010) bytes
167+
```
168+
169+
Run FPGA tests to check whether your implementation is valid:
170+
171+
```
172+
 mbed test -t ARM -m K64F -n tests-mbed_hal_fpga_ci_test_shield*
173+
```
174+
175+
<span class="notes">**Note:** Your target must be ready to run FPGA-Test-Shield tests.</span>

0 commit comments

Comments
 (0)