Skip to content

Commit 696b573

Browse files
committed
NRF52840_DK: Add explicit pinmap support
1 parent 1f30b62 commit 696b573

File tree

5 files changed

+163
-55
lines changed

5 files changed

+163
-55
lines changed

hal/explicit_pinmap.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "PeripheralPinMaps.h"
3333

3434

35-
#if DEVICE_PWMOUT
35+
#if defined(DEVICE_PWMOUT) && defined(PINMAP_PWM)
3636
MSTD_CONSTEXPR_FN_14 PinMap get_pwm_pinmap(const PinName pin)
3737
{
3838
for (const PinMap &pinmap : PINMAP_PWM) {
@@ -44,7 +44,7 @@ MSTD_CONSTEXPR_FN_14 PinMap get_pwm_pinmap(const PinName pin)
4444
}
4545
#endif // DEVICE_PWMOUT
4646

47-
#if DEVICE_ANALOGIN
47+
#if defined(DEVICE_ANALOGIN) && defined(PINMAP_ANALOGIN)
4848
MSTD_CONSTEXPR_FN_14 PinMap get_analogin_pinmap(const PinName pin)
4949
{
5050
for (const PinMap &pinmap : PINMAP_ANALOGIN) {
@@ -56,7 +56,7 @@ MSTD_CONSTEXPR_FN_14 PinMap get_analogin_pinmap(const PinName pin)
5656
}
5757
#endif // DEVICE_ANALOGIN
5858

59-
#if DEVICE_ANALOGOUT
59+
#if defined(DEVICE_ANALOGOUT) && defined(PINMAP_ANALOGOUT)
6060
MSTD_CONSTEXPR_FN_14 PinMap get_analogout_pinmap(const PinName pin)
6161
{
6262
for (const PinMap &pinmap : PINMAP_ANALOGOUT) {
@@ -68,7 +68,7 @@ MSTD_CONSTEXPR_FN_14 PinMap get_analogout_pinmap(const PinName pin)
6868
}
6969
#endif // DEVICE_ANALOGOUT
7070

71-
#if DEVICE_I2C
71+
#if defined(DEVICE_I2C) && defined(PINMAP_I2C_SDA) && defined(PINMAP_I2C_SCL)
7272
MSTD_CONSTEXPR_FN_14 i2c_pinmap_t get_i2c_pinmap(const PinName sda, const PinName scl)
7373
{
7474
const PinMap *sda_map = nullptr;
@@ -95,7 +95,7 @@ MSTD_CONSTEXPR_FN_14 i2c_pinmap_t get_i2c_pinmap(const PinName sda, const PinNam
9595
}
9696
#endif //DEVICE_I2C
9797

98-
#if DEVICE_SERIAL
98+
#if defined(DEVICE_SERIAL) && defined(PINMAP_UART_TX) && defined(PINMAP_UART_RX)
9999
MSTD_CONSTEXPR_FN_14 serial_pinmap_t get_uart_pinmap(const PinName tx, const PinName rx)
100100
{
101101
const PinMap *tx_map = nullptr;
@@ -125,7 +125,7 @@ MSTD_CONSTEXPR_FN_14 serial_pinmap_t get_uart_pinmap(const PinName tx, const Pin
125125
}
126126
}
127127

128-
#if DEVICE_SERIAL_FC
128+
#if defined(DEVICE_SERIAL_FC) && defined(PINMAP_UART_RTS) && defined(PINMAP_UART_CTS)
129129
MSTD_CONSTEXPR_FN_14 serial_fc_pinmap_t get_uart_fc_pinmap(const PinName rxflow, const PinName txflow)
130130
{
131131
const PinMap *rts_map = nullptr;
@@ -153,7 +153,7 @@ MSTD_CONSTEXPR_FN_14 serial_fc_pinmap_t get_uart_fc_pinmap(const PinName rxflow,
153153
#endif // DEVICE_SERIAL_FC
154154
#endif // DEVICE_SERIAL
155155

156-
#if DEVICE_SPI
156+
#if defined(DEVICE_SPI) && defined(PINMAP_SPI_MOSI) && defined(PINMAP_SPI_MISO) && defined(PINMAP_SPI_SCLK) && defined(PINMAP_SPI_SSEL)
157157
MSTD_CONSTEXPR_FN_14 spi_pinmap_t get_spi_pinmap(const PinName mosi, const PinName miso, const PinName sclk, const PinName ssel)
158158
{
159159
const PinMap *mosi_map = nullptr;
@@ -198,7 +198,7 @@ MSTD_CONSTEXPR_FN_14 spi_pinmap_t get_spi_pinmap(const PinName mosi, const PinNa
198198
}
199199
#endif // DEVICE_SPI
200200

201-
#if DEVICE_CAN
201+
#if defined(DEVICE_CAN) && defined(PINMAP_CAN_RD) && defined(PINMAP_CAN_TD)
202202
MSTD_CONSTEXPR_FN_14 can_pinmap_t get_can_pinmap(const PinName rd, const PinName td)
203203
{
204204
const PinMap *rd_map = nullptr;
@@ -225,7 +225,7 @@ MSTD_CONSTEXPR_FN_14 can_pinmap_t get_can_pinmap(const PinName rd, const PinName
225225
}
226226
#endif //DEVICE_CAN
227227

228-
#if DEVICE_QSPI
228+
#if defined(DEVICE_QSPI) && defined(PINMAP_QSPI_DATA0) && defined(PINMAP_QSPI_DATA1) && defined(PINMAP_QSPI_DATA2) && defined(PINMAP_QSPI_DATA3) && defined(PINMAP_QSPI_SCLK) && defined(PINMAP_QSPI_SSEL)
229229
MSTD_CONSTEXPR_FN_14 qspi_pinmap_t get_qspi_pinmap(const PinName data0, const PinName data1, const PinName data2, const PinName data3, const PinName sclk, const PinName ssel)
230230
{
231231
const PinMap *data0_map = nullptr;
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MBED_PERIPHERALPINMAPS_H
18+
#define MBED_PERIPHERALPINMAPS_H
19+
20+
#include <mstd_cstddef>
21+
22+
typedef enum {
23+
NRF_SAADC_CHANNEL_0 = 0,
24+
NRF_SAADC_CHANNEL_1 = 1,
25+
NRF_SAADC_CHANNEL_2 = 2,
26+
NRF_SAADC_CHANNEL_3 = 3,
27+
NRF_SAADC_CHANNEL_4 = 4,
28+
NRF_SAADC_CHANNEL_5 = 5,
29+
NRF_SAADC_CHANNEL_6 = 6,
30+
NRF_SAADC_CHANNEL_7 = 7,
31+
} nrf_saadc_channel_t;
32+
33+
/************ADC***************/
34+
/* The third "function" value is used to select the correct ADC channel */
35+
#if DEVICE_ANALOGIN
36+
MSTD_CONSTEXPR_OBJ_11 PinMap PinMap_ADC[] = {
37+
{ p2, ADC0_0, NRF_SAADC_CHANNEL_0 },
38+
{ p3, ADC0_0, NRF_SAADC_CHANNEL_1 },
39+
{ p4, ADC0_0, NRF_SAADC_CHANNEL_2 },
40+
{ p5, ADC0_0, NRF_SAADC_CHANNEL_3 },
41+
{ p28, ADC0_0, NRF_SAADC_CHANNEL_4 },
42+
{ p29, ADC0_0, NRF_SAADC_CHANNEL_5 },
43+
{ p30, ADC0_0, NRF_SAADC_CHANNEL_6 },
44+
{ p31, ADC0_0, NRF_SAADC_CHANNEL_7 },
45+
{ NC, NC, NC }
46+
};
47+
#endif
48+
49+
// Pinmap used for testing only
50+
MSTD_CONSTEXPR_OBJ_11 PinMap All_pins[] = {
51+
{P0_0, 0, 0},
52+
{P0_1, 0, 0},
53+
{P0_2, 0, 0},
54+
{P0_3, 0, 0},
55+
{P0_4, 0, 0},
56+
{P0_5, 0, 0},
57+
{P0_6, 0, 0},
58+
{P0_7, 0, 0},
59+
{P0_8, 0, 0},
60+
{P0_9, 0, 0},
61+
{P0_10, 0, 0},
62+
{P0_11, 0, 0},
63+
{P0_12, 0, 0},
64+
{P0_13, 0, 0},
65+
{P0_14, 0, 0},
66+
{P0_15, 0, 0},
67+
{P0_16, 0, 0},
68+
{P0_17, 0, 0},
69+
{P0_18, 0, 0},
70+
{P0_19, 0, 0},
71+
{P0_20, 0, 0},
72+
{P0_21, 0, 0},
73+
{P0_22, 0, 0},
74+
{P0_23, 0, 0},
75+
{P0_24, 0, 0},
76+
{P0_25, 0, 0},
77+
{P0_28, 0, 0},
78+
{P0_29, 0, 0},
79+
{P0_30, 0, 0},
80+
{P0_31, 0, 0},
81+
82+
{P1_0, 0, 0},
83+
{P1_1, 0, 0},
84+
{P1_2, 0, 0},
85+
{P1_3, 0, 0},
86+
{P1_4, 0, 0},
87+
{P1_5, 0, 0},
88+
{P1_6, 0, 0},
89+
{P1_7, 0, 0},
90+
{P1_8, 0, 0},
91+
{P1_9, 0, 0},
92+
{P1_10, 0, 0},
93+
{P1_11, 0, 0},
94+
{P1_12, 0, 0},
95+
{P1_13, 0, 0},
96+
{P1_14, 0, 0},
97+
{P1_15, 0, 0},
98+
99+
{NC, NC, 0}
100+
};
101+
102+
#define PINMAP_ANALOGIN PinMap_ADC
103+
#define PINMAP_ANALOGOUT All_pins
104+
#define PINMAP_I2C_SDA All_pins
105+
#define PINMAP_I2C_SCL All_pins
106+
#define PINMAP_UART_TX All_pins
107+
#define PINMAP_UART_RX All_pins
108+
#define PINMAP_UART_CTS All_pins
109+
#define PINMAP_UART_RTS All_pins
110+
#define PINMAP_SPI_SCLK All_pins
111+
#define PINMAP_SPI_MOSI All_pins
112+
#define PINMAP_SPI_MISO All_pins
113+
#define PINMAP_SPI_SSEL All_pins
114+
#define PINMAP_PWM All_pins
115+
116+
#endif

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52840/PeripheralPins.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,7 @@
3737
*/
3838

3939
#include "PeripheralPins.h"
40-
41-
typedef enum {
42-
NRF_SAADC_CHANNEL_0 = 0,
43-
NRF_SAADC_CHANNEL_1 = 1,
44-
NRF_SAADC_CHANNEL_2 = 2,
45-
NRF_SAADC_CHANNEL_3 = 3,
46-
NRF_SAADC_CHANNEL_4 = 4,
47-
NRF_SAADC_CHANNEL_5 = 5,
48-
NRF_SAADC_CHANNEL_6 = 6,
49-
NRF_SAADC_CHANNEL_7 = 7,
50-
} nrf_saadc_channel_t;
51-
52-
/************ADC***************/
53-
/* The third "function" value is used to select the correct ADC channel */
54-
#if DEVICE_ANALOGIN
55-
const PinMap PinMap_ADC[] = {
56-
{ p2, ADC0_0, NRF_SAADC_CHANNEL_0 },
57-
{ p3, ADC0_0, NRF_SAADC_CHANNEL_1 },
58-
{ p4, ADC0_0, NRF_SAADC_CHANNEL_2 },
59-
{ p5, ADC0_0, NRF_SAADC_CHANNEL_3 },
60-
{ p28, ADC0_0, NRF_SAADC_CHANNEL_4 },
61-
{ p29, ADC0_0, NRF_SAADC_CHANNEL_5 },
62-
{ p30, ADC0_0, NRF_SAADC_CHANNEL_6 },
63-
{ p31, ADC0_0, NRF_SAADC_CHANNEL_7 },
64-
{ NC, NC, NC }
65-
};
66-
#endif
40+
#include "PeripheralPinMaps.h"
6741

6842
// Pinmap used for testing only
6943
const PinMap PinMap_I2C_testing[] = {

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52840/PortNames.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
/*
1+
/*
22
* Copyright (c) 2013 Nordic Semiconductor ASA
33
* All rights reserved.
4-
*
4+
*
55
* Redistribution and use in source and binary forms, with or without modification,
66
* are permitted provided that the following conditions are met:
7-
*
8-
* 1. Redistributions of source code must retain the above copyright notice, this list
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list
99
* of conditions and the following disclaimer.
1010
*
11-
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
12-
* integrated circuit in a product or a software update for such product, must reproduce
13-
* the above copyright notice, this list of conditions and the following disclaimer in
11+
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
12+
* integrated circuit in a product or a software update for such product, must reproduce
13+
* the above copyright notice, this list of conditions and the following disclaimer in
1414
* the documentation and/or other materials provided with the distribution.
1515
*
16-
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
17-
* used to endorse or promote products derived from this software without specific prior
16+
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software without specific prior
1818
* written permission.
1919
*
20-
* 4. This software, with or without modification, must only be used with a
20+
* 4. This software, with or without modification, must only be used with a
2121
* Nordic Semiconductor ASA integrated circuit.
2222
*
23-
* 5. Any software provided in binary or object form under this license must not be reverse
24-
* engineered, decompiled, modified and/or disassembled.
25-
*
23+
* 5. Any software provided in binary or object form under this license must not be reverse
24+
* engineered, decompiled, modified and/or disassembled.
25+
*
2626
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2727
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2828
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -33,7 +33,7 @@
3333
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3434
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3535
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36-
*
36+
*
3737
*/
3838

3939
#ifndef MBED_PORTNAMES_H
@@ -43,6 +43,9 @@
4343
extern "C" {
4444
#endif
4545

46+
/* If this macro is defined, then constexpr utility functions for pin-map seach can be used. */
47+
#define EXPLICIT_PINMAP_READY 1
48+
4649
typedef enum
4750
{
4851
Port0 = 0, //GPIO pins 0-31 -> 0.0-0.31

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/analogin_api.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
17+
1818
#if DEVICE_ANALOGIN
1919

2020
#include "hal/analogin_api.h"
@@ -45,7 +45,13 @@ void SAADC_IRQHandler(void);
4545
* @param obj The analogin object to initialize
4646
* @param pin The analogin pin name
4747
*/
48-
void analogin_init(analogin_t *obj, PinName pin)
48+
#if EXPLICIT_PINMAP_READY
49+
#define ANALOGIN_INIT_DIRECT analogin_init_direct
50+
void analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
51+
#else
52+
#define ANALOGIN_INIT_DIRECT _analogin_init_direct
53+
static void _analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
54+
#endif
4955
{
5056
MBED_ASSERT(obj);
5157

@@ -78,7 +84,7 @@ void analogin_init(analogin_t *obj, PinName pin)
7884
}
7985

8086
/* Use pinmap function to get associated channel. */
81-
uint32_t channel = pinmap_function(pin, PinMap_ADC);
87+
uint32_t channel = (uint32_t) pinmap->function;
8288
MBED_ASSERT(channel != (uint32_t) NC);
8389

8490
/* Account for an off-by-one in Channel definition and Input definition. */
@@ -106,19 +112,28 @@ void analogin_init(analogin_t *obj, PinName pin)
106112
obj->channel = channel;
107113
}
108114

115+
void analogin_init(analogin_t *obj, PinName pin)
116+
{
117+
int peripheral = (int)pinmap_peripheral(pin, PinMap_ADC);
118+
int function = (int)pinmap_find_function(pin, PinMap_ADC);
119+
120+
const PinMap explicit_pinmap = {pin, peripheral, function};
121+
122+
ANALOGIN_INIT_DIRECT(obj, &explicit_pinmap);
123+
}
109124

110125
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
111126
*
112127
* @param obj The analogin object
113128
* @return A floating value representing the current input voltage
114129
*/
115130
uint16_t analogin_read_u16(analogin_t *obj)
116-
{
131+
{
117132
MBED_ASSERT(obj);
118133

119134
/* Default return value is 0. */
120135
uint16_t retval = 0;
121-
136+
122137
/* Read single channel, blocking. */
123138
nrf_saadc_value_t value = { 0 };
124139
ret_code_t result = nrfx_saadc_sample_convert(obj->channel, &value);

0 commit comments

Comments
 (0)