Skip to content

Commit dc639b3

Browse files
bilsefvgadreau
authored andcommitted
Support for Teensy 4 (MarlinFirmware#19311)
1 parent 9972b95 commit dc639b3

31 files changed

+1814
-7
lines changed

Marlin/src/HAL/TEENSY40_41/HAL.cpp

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
/**
24+
* Description: HAL for Teensy40 (IMXRT1062)
25+
*/
26+
27+
#ifdef __IMXRT1062__
28+
29+
#include "HAL.h"
30+
#include "../shared/Delay.h"
31+
#include "timers.h"
32+
33+
#include <Wire.h>
34+
35+
uint16_t HAL_adc_result, HAL_adc_select;
36+
37+
static const uint8_t pin2sc1a[] = {
38+
0x07, // 0/A0 AD_B1_02
39+
0x08, // 1/A1 AD_B1_03
40+
0x0C, // 2/A2 AD_B1_07
41+
0x0B, // 3/A3 AD_B1_06
42+
0x06, // 4/A4 AD_B1_01
43+
0x05, // 5/A5 AD_B1_00
44+
0x0F, // 6/A6 AD_B1_10
45+
0x00, // 7/A7 AD_B1_11
46+
0x0D, // 8/A8 AD_B1_08
47+
0x0E, // 9/A9 AD_B1_09
48+
0x01, // 24/A10 AD_B0_12
49+
0x02, // 25/A11 AD_B0_13
50+
0x83, // 26/A12 AD_B1_14 - only on ADC2, 3
51+
0x84, // 27/A13 AD_B1_15 - only on ADC2, 4
52+
0x07, // 14/A0 AD_B1_02
53+
0x08, // 15/A1 AD_B1_03
54+
0x0C, // 16/A2 AD_B1_07
55+
0x0B, // 17/A3 AD_B1_06
56+
0x06, // 18/A4 AD_B1_01
57+
0x05, // 19/A5 AD_B1_00
58+
0x0F, // 20/A6 AD_B1_10
59+
0x00, // 21/A7 AD_B1_11
60+
0x0D, // 22/A8 AD_B1_08
61+
0x0E, // 23/A9 AD_B1_09
62+
0x01, // 24/A10 AD_B0_12
63+
0x02, // 25/A11 AD_B0_13
64+
0x83, // 26/A12 AD_B1_14 - only on ADC2, 3
65+
0x84, // 27/A13 AD_B1_15 - only on ADC2, 4
66+
#ifdef ARDUINO_TEENSY41
67+
0xFF, // 28
68+
0xFF, // 29
69+
0xFF, // 30
70+
0xFF, // 31
71+
0xFF, // 32
72+
0xFF, // 33
73+
0xFF, // 34
74+
0xFF, // 35
75+
0xFF, // 36
76+
0xFF, // 37
77+
0x81, // 38/A14 AD_B1_12 - only on ADC2, 1
78+
0x82, // 39/A15 AD_B1_13 - only on ADC2, 2
79+
0x09, // 40/A16 AD_B1_04
80+
0x0A, // 41/A17 AD_B1_05
81+
#endif
82+
};
83+
84+
/*
85+
// disable interrupts
86+
void cli() { noInterrupts(); }
87+
88+
// enable interrupts
89+
void sei() { interrupts(); }
90+
*/
91+
92+
void HAL_adc_init() {
93+
analog_init();
94+
while (ADC1_GC & ADC_GC_CAL) ;
95+
while (ADC2_GC & ADC_GC_CAL) ;
96+
}
97+
98+
void HAL_clear_reset_source() {
99+
uint32_t reset_source = SRC_SRSR;
100+
SRC_SRSR = reset_source;
101+
}
102+
103+
uint8_t HAL_get_reset_source() {
104+
switch (SRC_SRSR & 0xFF) {
105+
case 1: return RST_POWER_ON; break;
106+
case 2: return RST_SOFTWARE; break;
107+
case 4: return RST_EXTERNAL; break;
108+
// case 8: return RST_BROWN_OUT; break;
109+
case 16: return RST_WATCHDOG; break;
110+
case 64: return RST_JTAG; break;
111+
// case 128: return RST_OVERTEMP; break;
112+
}
113+
return 0;
114+
}
115+
116+
#define __bss_end _ebss
117+
118+
extern "C" {
119+
extern char __bss_end;
120+
extern char __heap_start;
121+
extern void* __brkval;
122+
123+
// Doesn't work on Teensy 4.x
124+
uint32_t freeMemory() {
125+
uint32_t free_memory;
126+
if ((uint32_t)__brkval == 0)
127+
free_memory = ((uint32_t)&free_memory) - ((uint32_t)&__bss_end);
128+
else
129+
free_memory = ((uint32_t)&free_memory) - ((uint32_t)__brkval);
130+
return free_memory;
131+
}
132+
}
133+
134+
void HAL_adc_start_conversion(const uint8_t adc_pin) {
135+
const uint16_t pin = pin2sc1a[adc_pin];
136+
if (pin == 0xFF) {
137+
HAL_adc_select = -1; // Digital only
138+
}
139+
else if (pin & 0x80) {
140+
HAL_adc_select = 1;
141+
ADC2_HC0 = pin & 0x7F;
142+
}
143+
else {
144+
HAL_adc_select = 0;
145+
ADC1_HC0 = pin;
146+
}
147+
}
148+
149+
uint16_t HAL_adc_get_result() {
150+
switch (HAL_adc_select) {
151+
case 0:
152+
while (!(ADC1_HS & ADC_HS_COCO0)) ; // wait
153+
return ADC1_R0;
154+
case 1:
155+
while (!(ADC2_HS & ADC_HS_COCO0)) ; // wait
156+
return ADC2_R0;
157+
}
158+
return 0;
159+
}
160+
161+
bool is_output(uint8_t pin) {
162+
const struct digital_pin_bitband_and_config_table_struct *p;
163+
p = digital_pin_to_info_PGM + pin;
164+
return (*(p->reg + 1) & p->mask);
165+
}
166+
167+
#endif // __IMXRT1062__

Marlin/src/HAL/TEENSY40_41/HAL.h

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
*
4+
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5+
* Copyright (c) 2016 Bob Cousins [email protected]
6+
* Copyright (c) 2015-2016 Nico Tonnhofer [email protected]
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#pragma once
23+
24+
/**
25+
* Description: HAL for Teensy 4.0 and Teensy 4.1
26+
*/
27+
28+
#define CPU_32_BIT
29+
30+
#include "../shared/Marduino.h"
31+
#include "../shared/math_32bit.h"
32+
#include "../shared/HAL_SPI.h"
33+
34+
#include "fastio.h"
35+
#include "watchdog.h"
36+
37+
#include <stdint.h>
38+
#include <util/atomic.h>
39+
40+
//#define ST7920_DELAY_1 DELAY_NS(600)
41+
//#define ST7920_DELAY_2 DELAY_NS(750)
42+
//#define ST7920_DELAY_3 DELAY_NS(750)
43+
44+
// ------------------------
45+
// Defines
46+
// ------------------------
47+
48+
#ifdef __IMXRT1062__
49+
#define IS_32BIT_TEENSY 1
50+
#define IS_TEENSY41 1
51+
#endif
52+
53+
#if SERIAL_PORT == -1
54+
#define MYSERIAL0 SerialUSB
55+
#elif SERIAL_PORT == 0
56+
#define MYSERIAL0 Serial
57+
#elif SERIAL_PORT == 1
58+
#define MYSERIAL0 Serial1
59+
#elif SERIAL_PORT == 2
60+
#define MYSERIAL0 Serial2
61+
#elif SERIAL_PORT == 3
62+
#define MYSERIAL0 Serial3
63+
#elif SERIAL_PORT == 4
64+
#define MYSERIAL0 Serial4
65+
#elif SERIAL_PORT == 5
66+
#define MYSERIAL0 Serial5
67+
#elif SERIAL_PORT == 6
68+
#define MYSERIAL0 Serial6
69+
#elif SERIAL_PORT == 7
70+
#define MYSERIAL0 Serial7
71+
#elif SERIAL_PORT == 8
72+
#define MYSERIAL0 Serial8
73+
#else
74+
#error "The required SERIAL_PORT must be from -1 to 8. Please update your configuration."
75+
#endif
76+
77+
#ifdef SERIAL_PORT_2
78+
#if SERIAL_PORT_2 == SERIAL_PORT
79+
#error "SERIAL_PORT_2 must be different from SERIAL_PORT. Please update your configuration."
80+
#elif SERIAL_PORT_2 == -1
81+
#define MYSERIAL1 usbSerial
82+
#elif SERIAL_PORT_2 == 0
83+
#define MYSERIAL1 Serial
84+
#elif SERIAL_PORT_2 == 1
85+
#define MYSERIAL1 Serial1
86+
#elif SERIAL_PORT_2 == 2
87+
#define MYSERIAL1 Serial2
88+
#elif SERIAL_PORT_2 == 3
89+
#define MYSERIAL1 Serial3
90+
#elif SERIAL_PORT_2 == 4
91+
#define MYSERIAL1 Serial4
92+
#elif SERIAL_PORT_2 == 5
93+
#define MYSERIAL1 Serial5
94+
#elif SERIAL_PORT_2 == 6
95+
#define MYSERIAL1 Serial6
96+
#elif SERIAL_PORT_2 == 7
97+
#define MYSERIAL1 Serial7
98+
#elif SERIAL_PORT_2 == 8
99+
#define MYSERIAL1 Serial8
100+
#else
101+
#error "SERIAL_PORT_2 must be from -1 to 8. Please update your configuration."
102+
#endif
103+
#define NUM_SERIAL 2
104+
#else
105+
#define NUM_SERIAL 1
106+
#endif
107+
108+
#define HAL_SERVO_LIB libServo
109+
110+
typedef int8_t pin_t;
111+
112+
#ifndef analogInputToDigitalPin
113+
#define analogInputToDigitalPin(p) ((p < 12u) ? (p) + 54u : -1)
114+
#endif
115+
116+
#define CRITICAL_SECTION_START() uint32_t primask = __get_primask(); __disable_irq()
117+
#define CRITICAL_SECTION_END() if (!primask) __enable_irq()
118+
#define ISRS_ENABLED() (!__get_primask())
119+
#define ENABLE_ISRS() __enable_irq()
120+
#define DISABLE_ISRS() __disable_irq()
121+
122+
#undef sq
123+
#define sq(x) ((x)*(x))
124+
125+
#ifndef strncpy_P
126+
#define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
127+
#endif
128+
129+
// Don't place string constants in PROGMEM
130+
#undef PSTR
131+
#define PSTR(str) ({static const char *data = (str); &data[0];})
132+
133+
// Fix bug in pgm_read_ptr
134+
#undef pgm_read_ptr
135+
#define pgm_read_ptr(addr) (*((void**)(addr)))
136+
// Add type-checking to pgm_read_word
137+
#undef pgm_read_word
138+
#define pgm_read_word(addr) (*((uint16_t*)(addr)))
139+
140+
// Enable hooks into idle and setup for HAL
141+
#define HAL_IDLETASK 1
142+
FORCE_INLINE void HAL_idletask() {}
143+
FORCE_INLINE void HAL_init() {}
144+
145+
// Clear reset reason
146+
void HAL_clear_reset_source();
147+
148+
// Reset reason
149+
uint8_t HAL_get_reset_source();
150+
151+
FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }
152+
153+
#pragma GCC diagnostic push
154+
#pragma GCC diagnostic ignored "-Wunused-function"
155+
extern "C" {
156+
uint32_t freeMemory();
157+
}
158+
#pragma GCC diagnostic pop
159+
160+
// ADC
161+
162+
void HAL_adc_init();
163+
164+
#define HAL_ADC_VREF 3.3
165+
#define HAL_ADC_RESOLUTION 10
166+
#define HAL_ADC_FILTERED // turn off ADC oversampling
167+
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
168+
#define HAL_READ_ADC() HAL_adc_get_result()
169+
#define HAL_ADC_READY() true
170+
171+
#define HAL_ANALOG_SELECT(pin)
172+
173+
void HAL_adc_start_conversion(const uint8_t adc_pin);
174+
uint16_t HAL_adc_get_result();
175+
176+
#define GET_PIN_MAP_PIN(index) index
177+
#define GET_PIN_MAP_INDEX(pin) pin
178+
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
179+
180+
bool is_output(uint8_t pin);

0 commit comments

Comments
 (0)