Skip to content

Commit 1a9b7e5

Browse files
SamantazFoxSamantazFox
authored andcommitted
canio: Add support to teensy 4.1 board
1 parent d526f4a commit 1a9b7e5

7 files changed

Lines changed: 85 additions & 2 deletions

File tree

ports/mimxrt10xx/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ SRC_SDK := \
120120
drivers/snvs_hp/fsl_snvs_hp.c \
121121
drivers/snvs_lp/fsl_snvs_lp.c \
122122
drivers/trng/fsl_trng.c \
123+
drivers/flexcan/fsl_flexcan.c \
123124

124125
ifeq ($(CIRCUITPY_ANALOGIO), 1)
125126
SRC_SDK += drivers/adc_12b1msps_sar/fsl_adc.c \

ports/mimxrt10xx/boards/teensy41/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ CHIP_VARIANT = MIMXRT1062DVJ6A
77
CHIP_FAMILY = MIMXRT1062
88
FLASH = W25Q64JV
99
CIRCUITPY__EVE = 1
10+
CIRCUITPY_CANIO = 1
1011
CIRCUITPY_USB_HOST = 1
1112
CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY = 1

ports/mimxrt10xx/boards/teensy41/pins.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,23 @@ static const mp_rom_map_elem_t board_module_globals_table[] = {
179179
{MP_OBJ_NEW_QSTR(MP_QSTR_TX7), MP_ROM_PTR(&pin_GPIO_EMC_31)},
180180
{MP_OBJ_NEW_QSTR(MP_QSTR_RX8), MP_ROM_PTR(&pin_GPIO_B1_13)},
181181
{MP_OBJ_NEW_QSTR(MP_QSTR_TX8), MP_ROM_PTR(&pin_GPIO_B1_12)},
182+
183+
// CAN and CAN-FD
184+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN1_RX), MP_ROM_PTR(&pin_GPIO_AD_B1_09)},
185+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN1_TX), MP_ROM_PTR(&pin_GPIO_AD_B1_08)},
186+
187+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN2_RX), MP_ROM_PTR(&pin_GPIO_AD_B0_03)},
188+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN2_TX), MP_ROM_PTR(&pin_GPIO_AD_B0_02)},
189+
190+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN3_RX), MP_ROM_PTR(&pin_GPIO_EMC_37)},
191+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN3_TX), MP_ROM_PTR(&pin_GPIO_EMC_36)},
192+
193+
// "CAN" is an alias for CAN1
194+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN_RX), MP_ROM_PTR(&pin_GPIO_AD_B1_09)},
195+
{MP_OBJ_NEW_QSTR(MP_QSTR_CAN_TX), MP_ROM_PTR(&pin_GPIO_AD_B1_08)},
196+
197+
// "CANFD" is an alias for CAN3
198+
{MP_OBJ_NEW_QSTR(MP_QSTR_CANFD_RX), MP_ROM_PTR(&pin_GPIO_EMC_37)},
199+
{MP_OBJ_NEW_QSTR(MP_QSTR_CANFD_TX), MP_ROM_PTR(&pin_GPIO_EMC_36)},
182200
};
183201
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);

ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/clocks.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ void clocks_init(void) {
167167
CLOCK_DisableClock(kCLOCK_Can2S);
168168
CLOCK_DisableClock(kCLOCK_Can3S);
169169
/* Set CAN_CLK_PODF. */
170-
CLOCK_SetDiv(kCLOCK_CanDiv, 1);
170+
CLOCK_SetDiv(kCLOCK_CanDiv, 2); // Clock divider for master flexcan clock source
171171
/* Set Can clock source. */
172-
CLOCK_SetMux(kCLOCK_CanMux, 2);
172+
CLOCK_SetMux(kCLOCK_CanMux, 0); // Select 60M clock divided by USB1 PLL (480 MHz) as master flexcan clock source
173173
/* Disable UART clock gate. */
174174
CLOCK_DisableClock(kCLOCK_Lpuart1);
175175
CLOCK_DisableClock(kCLOCK_Lpuart2);

ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,40 @@ const mcu_periph_obj_t mcu_mqs_right_list[3] = {
276276
PERIPH_PIN(3, 2, 0, 0, &pin_GPIO_B0_00),
277277
};
278278

279+
CAN_Type *const mcu_can_banks[3] = { CAN1, CAN2, CAN3 };
280+
281+
const mcu_periph_obj_t mcu_can_rx_list[11] = {
282+
PERIPH_PIN(1, 4, kIOMUXC_FLEXCAN1_RX_SELECT_INPUT, 0, &pin_GPIO_SD_B1_03),
283+
PERIPH_PIN(1, 3, kIOMUXC_FLEXCAN1_RX_SELECT_INPUT, 1, &pin_GPIO_EMC_18),
284+
PERIPH_PIN(1, 2, kIOMUXC_FLEXCAN1_RX_SELECT_INPUT, 2, &pin_GPIO_AD_B1_09),
285+
PERIPH_PIN(1, 2, kIOMUXC_FLEXCAN1_RX_SELECT_INPUT, 3, &pin_GPIO_B0_03),
286+
287+
PERIPH_PIN(2, 3, kIOMUXC_FLEXCAN2_RX_SELECT_INPUT, 0, &pin_GPIO_EMC_10),
288+
PERIPH_PIN(2, 0, kIOMUXC_FLEXCAN2_RX_SELECT_INPUT, 1, &pin_GPIO_AD_B0_03),
289+
PERIPH_PIN(2, 6, kIOMUXC_FLEXCAN2_RX_SELECT_INPUT, 2, &pin_GPIO_AD_B0_15),
290+
PERIPH_PIN(2, 6, kIOMUXC_FLEXCAN2_RX_SELECT_INPUT, 3, &pin_GPIO_B1_09),
291+
292+
PERIPH_PIN(3, 9, 0, 0, &pin_GPIO_EMC_37),
293+
PERIPH_PIN(3, 8, 0, 0, &pin_GPIO_AD_B0_11),
294+
PERIPH_PIN(3, 8, 0, 0, &pin_GPIO_AD_B0_15),
295+
};
296+
297+
const mcu_periph_obj_t mcu_can_tx_list[11] = {
298+
PERIPH_PIN(1, 3, 0, 0, &pin_GPIO_EMC_17),
299+
PERIPH_PIN(1, 2, 0, 0, &pin_GPIO_AD_B1_08),
300+
PERIPH_PIN(1, 2, 0, 0, &pin_GPIO_B0_02),
301+
PERIPH_PIN(1, 4, 0, 0, &pin_GPIO_SD_B1_02),
302+
303+
PERIPH_PIN(2, 3, 0, 0, &pin_GPIO_EMC_09),
304+
PERIPH_PIN(2, 0, 0, 0, &pin_GPIO_AD_B0_02),
305+
PERIPH_PIN(2, 6, 0, 0, &pin_GPIO_AD_B0_14),
306+
PERIPH_PIN(2, 6, 0, 0, &pin_GPIO_B1_08),
307+
308+
PERIPH_PIN(3, 9, 0, 0, &pin_GPIO_EMC_36),
309+
PERIPH_PIN(3, 8, 0, 0, &pin_GPIO_AD_B0_10),
310+
PERIPH_PIN(3, 8, 0, 0, &pin_GPIO_AD_B0_14),
311+
};
312+
279313
const mcu_pwm_obj_t mcu_pwm_list[67] = {
280314
PWM_PIN(PWM1, kPWM_Module_0, kPWM_PwmA, IOMUXC_GPIO_EMC_23_FLEXPWM1_PWMA00, &pin_GPIO_EMC_23),
281315
PWM_PIN(PWM1, kPWM_Module_0, kPWM_PwmA, IOMUXC_GPIO_SD_B0_00_FLEXPWM1_PWMA00, &pin_GPIO_SD_B0_00),

ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ extern const mcu_periph_obj_t mcu_i2s_mclk_list[7];
4141
extern const mcu_periph_obj_t mcu_mqs_left_list[3];
4242
extern const mcu_periph_obj_t mcu_mqs_right_list[3];
4343

44+
extern CAN_Type *const mcu_can_banks[3];
45+
extern const mcu_periph_obj_t mcu_can_rx_list[11];
46+
extern const mcu_periph_obj_t mcu_can_tx_list[11];
47+
4448
extern const mcu_pwm_obj_t mcu_pwm_list[67];

ports/mimxrt10xx/tools/gen_peripherals_data.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import re
23
import pathlib
34
import xml.etree.ElementTree as ET
45

@@ -8,6 +9,7 @@
89
"LPUART": ["RX", "TX", "RTS", "CTS"],
910
"I2S": ["RX_DATA0", "RX_SYNC", "TX_BCLK", "TX_DATA0", "TX_SYNC", "MCLK"],
1011
"MQS": ["LEFT", "RIGHT"],
12+
"CAN": ["RX", "TX"],
1113
}
1214

1315
SIGNAL_RENAME = {
@@ -21,6 +23,22 @@
2123
"RX_DATA": "RX_DATA0",
2224
}
2325

26+
INSTANCE_RENAME = {
27+
"FLEXCAN" : "CAN"
28+
}
29+
30+
INSTANCE_RE = re.compile("([a-zA-Z0-9]+?)([0-9]+)$")
31+
def rename_instance(instance:str)-> str:
32+
instance_match = INSTANCE_RE.match(instance)
33+
if instance_match is None:
34+
return instance
35+
instance_res = instance_match.groups()
36+
if len(instance_res) < 2:
37+
return instance
38+
instance_name = str(instance_res[0])
39+
instance_id = str(instance_res[1])
40+
return INSTANCE_RENAME.get(instance_name, instance_name) + instance_id
41+
2442
SKIP_LPSR = True
2543

2644
svd_folder = pathlib.Path(sys.argv[1])
@@ -54,6 +72,7 @@
5472
print(device)
5573
autogen_warning = autogen_warning_template.format(device)
5674
svd_fn = svd_folder / device / (device + ".xml")
75+
5776
if not svd_fn.exists():
5877
svd_fn = svd_folder / device / (device + "_cm7.xml")
5978

@@ -144,8 +163,10 @@
144163
if name.endswith("SELECT_INPUT"):
145164
name_split = name.split("_")
146165
instance = name_split[0]
166+
instance = rename_instance(instance)
147167
signal = "_".join(name_split[1:-2])
148168
signal = SIGNAL_RENAME.get(signal, signal)
169+
149170
if instance not in peripheral_inputs:
150171
peripheral_inputs[instance] = {}
151172
if signal not in peripheral_inputs[instance]:
@@ -161,6 +182,7 @@
161182
continue
162183
alt = int(alt[3:])
163184
value = int(evalue.find("value").text, 0)
185+
#print(f"instance: {instance}, signal: {signal}, pin_name: {pin_name}")
164186
peripheral_inputs[instance][signal][pin_name] = [alt, name, value]
165187
# Mux registers come before PAD registers.
166188
elif name.startswith("SW_MUX_CTL_PAD_GPIO"):
@@ -232,6 +254,7 @@
232254
print("skipping", pin_name, connection)
233255
continue
234256
instance, signal = connection.split("_", maxsplit=1)
257+
instance = rename_instance(instance)
235258
signal = SIGNAL_RENAME.get(signal, signal)
236259
if instance not in peripheral_inputs:
237260
peripheral_inputs[instance] = {}
@@ -288,6 +311,7 @@
288311
short_name = ptype.lower()
289312
if short_name.startswith("lp"):
290313
short_name = short_name[2:]
314+
291315
# Only one MQS exists and it is related to SAI3
292316
if ptype != "MQS":
293317
periph_h.append(
@@ -301,6 +325,7 @@
301325
for signal in SIGNALS[ptype]:
302326
pin_count = 0
303327
for instance in instances:
328+
###print(f"instance: {instance}")
304329
if instance not in peripheral_inputs or signal not in peripheral_inputs[instance]:
305330
continue
306331
pin_count += len(peripheral_inputs[instance][signal])

0 commit comments

Comments
 (0)