Skip to content

Commit c2d4d0a

Browse files
committed
nrf: Simplify pin generation and definition
This commit cleans up the pin generation code, all the pins and their AF (only ADC, for now) are specified in nrf52_af.csv and board use their own csv file to specify which pins are available on that board and if they have any special names.
1 parent ff91855 commit c2d4d0a

12 files changed

Lines changed: 102 additions & 337 deletions

File tree

ports/nrf/Makefile

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ INC += -I./device
4747
INC += -I./device/$(MCU_VARIANT)
4848
INC += -I./hal
4949
INC += -I./hal/$(MCU_VARIANT)
50-
INC += -I./modules/machine
5150
INC += -I./modules/ubluepy
5251
INC += -I./modules/music
5352
INC += -I./modules/random
@@ -109,7 +108,6 @@ SRC_HAL = $(addprefix hal/,\
109108
SRC_C += \
110109
mphalport.c \
111110
help.c \
112-
pin_named_pins.c \
113111
fatfs_port.c \
114112
fifo.c \
115113
tick.c \
@@ -295,10 +293,10 @@ SRC_QSTR_AUTO_DEPS +=
295293
$(OBJ): | $(HEADER_BUILD)/pins.h
296294

297295
# Use a pattern rule here so that make will only call make-pins.py once to make
298-
# both pins_$(BOARD).c and pins.h
299-
$(BUILD)/%_gen.c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(BUILD)/%_qstr.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE) | $(HEADER_BUILD)
296+
# both pins_g.c and pins.h
297+
$(BUILD)/%_gen.c $(HEADER_BUILD)/%.h $(BUILD)/%_qstr.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE) | $(HEADER_BUILD)
300298
$(ECHO) "Create $@"
301-
$(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) --af-const $(GEN_PINS_AF_CONST) --af-py $(GEN_PINS_AF_PY) > $(GEN_PINS_SRC)
299+
$(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) > $(GEN_PINS_SRC)
302300

303301
$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c
304302
$(call compile_c)
@@ -310,8 +308,6 @@ PREFIX_FILE = boards/$(MCU_VARIANT)_prefix.c
310308
GEN_PINS_SRC = $(BUILD)/pins_gen.c
311309
GEN_PINS_HDR = $(HEADER_BUILD)/pins.h
312310
GEN_PINS_QSTR = $(BUILD)/pins_qstr.h
313-
GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h
314-
GEN_PINS_AF_PY = $(BUILD)/pins_af.py
315311

316312
ifneq ($(FROZEN_DIR),)
317313
# To use frozen source modules, put your .py files in a subdirectory (eg scripts/)

ports/nrf/boards/feather52832/pins.csv

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
A0,P0_02,ADC0_IN0
2-
A1,P0_03,ADC0_IN1
3-
A2,P0_04,ADC0_IN2
4-
A3,P0_05,ADC0_IN3
1+
A0,P0_02
2+
A1,P0_03
3+
A2,P0_04
4+
A3,P0_05
55
TX,P0_06
66
RX,P0_08
77
NFC1,P0_09
@@ -18,7 +18,7 @@ DFU,P0_20
1818
SDA,P0_25
1919
SCL,P0_26
2020
D27,P0_27
21-
A4,P0_28,ADC0_IN4
22-
A5,P0_29,ADC0_IN5
23-
A6,P0_30,ADC0_IN6
24-
A7,P0_31,ADC0_IN7
21+
A4,P0_28
22+
A5,P0_29
23+
A6,P0_30
24+
A7,P0_31

ports/nrf/boards/feather52840/pins.csv

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
P0_00,P0_00
22
P0_01,P0_01
3-
P0_02,P0_02,ADC0_IN0
4-
P0_03,P0_03,ADC0_IN1
5-
P0_04,P0_04,ADC0_IN2
6-
P0_05,P0_05,ADC0_IN3
3+
P0_02,P0_02
4+
P0_03,P0_03
5+
P0_04,P0_04
6+
P0_05,P0_05
77
P0_06,P0_06
88
P0_07,P0_07
99
P0_08,P0_08
@@ -26,10 +26,10 @@ P0_24,P0_24
2626
P0_25,P0_25
2727
P0_26,P0_26
2828
P0_27,P0_27
29-
P0_28,P0_28,ADC0_IN4
30-
P0_29,P0_29,ADC0_IN5
31-
P0_30,P0_30,ADC0_IN6
32-
P0_31,P0_31,ADC0_IN7
29+
P0_28,P0_28
30+
P0_29,P0_29
31+
P0_30,P0_30
32+
P0_31,P0_31
3333
P1_00,P1_00
3434
P1_01,P1_01
3535
P1_02,P1_02

ports/nrf/boards/make-pins.py

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ def parse_port_pin(name_str):
2323
return (port, int(pin_str))
2424

2525

26-
def split_name_num(name_num):
27-
num = None
28-
for num_idx in range(len(name_num) - 1, -1, -1):
29-
if not name_num[num_idx].isdigit():
30-
name = name_num[0:num_idx + 1]
31-
num_str = name_num[num_idx + 1:]
32-
if len(num_str) > 0:
33-
num = int(num_str)
34-
break
35-
return name, num
36-
37-
3826
class Pin(object):
3927
"""Holds the information associated with a pin."""
4028

@@ -54,10 +42,9 @@ def set_is_board_pin(self):
5442
self.board_pin = True
5543

5644
def parse_adc(self, adc_str):
57-
if (adc_str[:3] != 'ADC'):
45+
if (adc_str[:3] != 'AIN'):
5846
return
59-
(adc, channel) = adc_str.split('_IN')
60-
self.adc_channel = 'SAADC_CH_PSELP_PSELP_AnalogInput%d' % int(channel)
47+
self.adc_channel = 'SAADC_CH_PSELP_PSELP_AnalogInput%d' % int(adc_str[3])
6148

6249
def print(self):
6350
print('const pin_obj_t pin_{:s} = PIN({:s}, {:d}, {:d}, {:s});'.format(
@@ -94,20 +81,17 @@ def find_pin(self, port_num, pin_num):
9481
if pin.port == port_num and pin.pin == pin_num:
9582
return pin
9683

97-
def parse_af_file(self, filename, pinname_col, af_col, af_col_end):
84+
def parse_af_file(self, filename):
9885
with open(filename, 'r') as csvfile:
9986
rows = csv.reader(csvfile)
10087
for row in rows:
10188
try:
102-
(port_num, pin_num) = parse_port_pin(row[pinname_col])
89+
(port_num, pin_num) = parse_port_pin(row[0])
10390
except:
10491
continue
10592
pin = Pin(port_num, pin_num)
106-
for af_idx in range(af_col, len(row)):
107-
if af_idx < af_col_end:
108-
pin.parse_af(af_idx - af_col, row[af_idx])
109-
elif af_idx == af_col_end:
110-
pin.parse_adc(row[af_idx])
93+
if len(row) > 1:
94+
pin.parse_adc(row[1])
11195
self.cpu_pins.append(NamedPin(pin.cpu_pin_name(), pin))
11296

11397
def parse_board_file(self, filename):
@@ -121,8 +105,6 @@ def parse_board_file(self, filename):
121105
pin = self.find_pin(port_num, pin_num)
122106
if pin:
123107
pin.set_is_board_pin()
124-
if len(row) == 3:
125-
pin.parse_adc(row[2])
126108
self.board_pins.append(NamedPin(row[0], pin))
127109

128110
def print_named(self, label, named_pins):
@@ -173,19 +155,7 @@ def main():
173155
"-a", "--af",
174156
dest="af_filename",
175157
help="Specifies the alternate function file for the chip",
176-
default="nrf.csv"
177-
)
178-
parser.add_argument(
179-
"--af-const",
180-
dest="af_const_filename",
181-
help="Specifies header file for alternate function constants.",
182-
default="build/pins_af_const.h"
183-
)
184-
parser.add_argument(
185-
"--af-py",
186-
dest="af_py_filename",
187-
help="Specifies the filename for the python alternate function mappings.",
188-
default="build/pins_af.py"
158+
default="nrf_af.csv"
189159
)
190160
parser.add_argument(
191161
"-b", "--board",
@@ -218,7 +188,7 @@ def main():
218188
print('//')
219189
if args.af_filename:
220190
print('// --af {:s}'.format(args.af_filename))
221-
pins.parse_af_file(args.af_filename, 1, 2, 2)
191+
pins.parse_af_file(args.af_filename)
222192

223193
if args.board_filename:
224194
print('// --board {:s}'.format(args.board_filename))

ports/nrf/boards/nrf52_prefix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{ \
1111
{ &mcu_pin_type }, \
1212
.name = MP_QSTR_ ## p_name, \
13-
.port = PORT_ ## p_port, \
13+
.port = (p_port), \
1414
.pin = (p_pin), \
15-
.adc_channel = p_adc_channel, \
15+
.adc_channel = (p_adc_channel), \
1616
}

ports/nrf/boards/pca10056/pins.csv

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
P0_00,PA0
1+
P0_00,P0_01
22
P0_01,P0_01
3-
P0_02,P0_02,ADC0_IN0
4-
P0_03,P0_03,ADC0_IN1
5-
P0_04,P0_04,ADC0_IN2
6-
P0_05,P0_05,ADC0_IN3
3+
P0_02,P0_02
4+
P0_03,P0_03
5+
P0_04,P0_04
6+
P0_05,P0_05
77
P0_06,P0_06
88
P0_07,P0_07
99
P0_08,P0_08
@@ -26,10 +26,10 @@ P0_24,P0_24
2626
P0_25,P0_25
2727
P0_26,P0_26
2828
P0_27,P0_27
29-
P0_28,P0_28,ADC0_IN4
30-
P0_29,P0_29,ADC0_IN5
31-
P0_30,P0_30,ADC0_IN6
32-
P0_31,P0_31,ADC0_IN7
29+
P0_28,P0_28
30+
P0_29,P0_29
31+
P0_30,P0_30
32+
P0_31,P0_31
3333
P1_00,P1_00
3434
P1_01,P1_01
3535
P1_02,P1_02

ports/nrf/common-hal/microcontroller/Pin.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@
2727
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_MICROCONTROLLER_PIN_H
2828
#define MICROPY_INCLUDED_NRF_COMMON_HAL_MICROCONTROLLER_PIN_H
2929

30+
#include "pin.h"
3031
#include "py/mphal.h"
31-
#include "modules/machine/pin.h"
3232

33-
//typedef pin_obj_t mcu_pin_obj_t;
3433
#define mcu_pin_obj_t pin_obj_t
3534
void reset_all_pins(void);
3635
void reset_pin(uint8_t pin);
37-
//void claim_pin(const mcu_pin_obj_t* pin);
38-
3936

4037
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_MICROCONTROLLER_PIN_H

ports/nrf/modules/machine/pin.h

Lines changed: 0 additions & 112 deletions
This file was deleted.

ports/nrf/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ extern const struct _mp_obj_module_t ble_module;
289289
// We need to provide a declaration/definition of alloca()
290290
#include <alloca.h>
291291

292-
#define MICROPY_PIN_DEFS_PORT_H "pin_defs_nrf5.h"
293292
//#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
294293

295294
#endif

0 commit comments

Comments
 (0)