-
Notifications
You must be signed in to change notification settings - Fork 51
/
driver.h
374 lines (313 loc) · 8.77 KB
/
driver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
driver.h - driver code for RP2040 ARM processors
Part of grblHAL
Copyright (c) 2021-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
//
// NOTE: do NOT change configuration here - edit my_machine.h instead!
//
#ifndef __DRIVER_H__
#define __DRIVER_H__
#include <stdbool.h>
#include <stdint.h>
#include "hardware/gpio.h"
#ifndef OVERRIDE_MY_MACHINE
#include "my_machine.h"
#endif
#if SDCARD_ENABLE
#define SPI_ENABLE 1
#endif
#if ETHERNET_ENABLE
#ifndef _WIZCHIP_
#define _WIZCHIP_ 5105 // W5100S
#endif
#ifndef SPI_ENABLE
#define SPI_ENABLE 1
#endif
#endif
#include "grbl/driver_opts.h"
#if ETHERNET_ENABLE && WIFI_ENABLE
#error "WiFi and Ethernet cannot be enabled at the same time!"
#endif
#if NUM_BANK0_GPIOS <= 32
#define DIGITAL_IN(pin) (!!(gpio_get_all() & (1UL << (pin))))
#define DIGITAL_OUT(pin, on) gpio_put(pin, on)
#else
#define DIGITAL_IN(pin) (!!(gpio_get_all64() & (1ULL << (pin))))
#define DIGITAL_OUT(pin, on) gpio_put(pin, on)
#endif
#define GPIO_IRQ_ALL (GPIO_IRQ_LEVEL_HIGH|GPIO_IRQ_LEVEL_LOW|GPIO_IRQ_EDGE_RISE|GPIO_IRQ_EDGE_FALL)
// Define GPIO mode options
#define GPIO_SHIFT0 0
#define GPIO_SHIFT1 1
#define GPIO_SHIFT2 2
#define GPIO_SHIFT3 3
#define GPIO_SHIFT4 4
#define GPIO_SHIFT5 5
#define GPIO_SHIFT6 6
#define GPIO_SHIFT7 7
#define GPIO_SHIFT8 8
#define GPIO_SHIFT9 9
#define GPIO_SHIFT10 10
#define GPIO_SHIFT11 11
#define GPIO_SHIFT12 12
#define GPIO_SHIFT13 13
#define GPIO_SHIFT14 14
#define GPIO_SHIFT15 15
#define GPIO_SHIFT16 16
#define GPIO_SHIFT17 17
#define GPIO_SHIFT18 18
#define GPIO_SHIFT19 19
#define GPIO_SHIFT20 20
#define GPIO_SHIFT21 21
#define GPIO_SHIFT22 22
#define GPIO_SHIFT23 23
#define GPIO_SHIFT24 24
#define GPIO_SHIFT25 25
#define GPIO_SHIFT26 26
#define GPIO_SHIFT27 27
#define GPIO_SHIFT28 28
#define GPIO_MAP 31
#define GPIO_DIRECT 33
#define GPIO_IOEXPAND 34
#define GPIO_INPUT 35
#define GPIO_OUTPUT 36
#define GPIO_PIO 37
#define GPIO_PIO_1 38
#define GPIO_SR8 39
#define GPIO_SR16 40
#define STEPPERS_ENABLE_PINMODE 0
// Define timer allocations.
/*
#define RPM_COUNTER_N 3
#define RPM_COUNTER timer(RPM_COUNTER_N)
#define RPM_COUNTER_IRQn timerINT(RPM_COUNTER_N)
#define RPM_COUNTER_IRQHandler timerHANDLER(RPM_COUNTER_N)
#define RPM_TIMER_N 2
#define RPM_TIMER timer(RPM_TIMER_N)
#define RPM_TIMER_IRQn timerINT(RPM_TIMER_N)
#define RPM_TIMER_IRQHandler timerHANDLER(RPM_TIMER_N)
#define PPI_TIMER_N 2
#define PPI_TIMER timer(PPI_TIMER_N)
#define PPI_TIMER_IRQn timerINT(PPI_TIMER_N)
#define PPI_TIMER_IRQHandler timerHANDLER(PPI_TIMER_N)
*/
#if WEBUI_ENABLE && LITTLEFS_ENABLE
#ifdef WEBUI_INFLASH
#undef WEBUI_INFLASH
#endif
#define WEBUI_INFLASH 1
#endif
#ifdef BOARD_CNC_BOOSTERPACK
#include "cnc_boosterpack_map.h"
#elif defined(BOARD_PICO_CNC)
#include "boards/pico_cnc_map.h"
#elif defined(BOARD_RP23U5XBB)
#include "boards/RP2350B_5X_map.h"
#elif defined(BOARD_PICOBOB)
#include "boards/picobob_map.h"
#elif defined(BOARD_PICOBOB_G540)
#include "boards/picobob_g540_map.h"
#elif defined(BOARD_PICOBOB_DLX)
#include "boards/picobob_dlx_map.h"
#elif defined(BOARD_PICOBOB_DLX_G540)
#include "boards/picobob_dlx_g540_map.h"
#elif defined(BOARD_PICOHAL)
#include "boards/picohal_map.h"
#elif defined(BOARD_BTT_SKR_PICO_10)
#include "boards/btt_skr_pico_10_map.h"
#elif defined BOARD_CITOH_CX6000
#include "boards/citoh_cx6000_map.h"
#elif defined(BOARD_MY_MACHINE)
#include "boards/my_machine_map.h"
#elif defined(BOARD_GENERIC_4AXIS)
#include "boards/generic_map_4axis.h"
#else // default board
#include "boards/generic_map.h"
#endif
#if SPI_ENABLE
#define SPI_DMA_ENABLE 1
#endif
#if STEP_PORT == GPIO_PIO
#define X_STEP_PIN STEP_PINS_BASE + 0
#define Y_STEP_PIN STEP_PINS_BASE + 1
#define Z_STEP_PIN STEP_PINS_BASE + 2
#ifdef M3_AVAILABLE
#define M3_STEP_PIN STEP_PINS_BASE + 3
#endif
#ifdef M4_AVAILABLE
#define M4_STEP_PIN STEP_PINS_BASE + 4
#endif
#ifdef M5_AVAILABLE
#define M5_STEP_PIN STEP_PINS_BASE + 5
#endif
#endif
#ifdef SPINDLE_PORT
#ifndef SPINDLE_ENABLE_PORT
#define SPINDLE_ENABLE_PORT (void *)SPINDLE_PORT
#endif
#ifndef SPINDLE_PWM_PORT
#define SPINDLE_PWM_PORT (void *)SPINDLE_PORT
#endif
#ifndef SPINDLE_DIRECTION_PORT
#define SPINDLE_DIRECTION_PORT (void *)SPINDLE_PORT
#endif
#endif // SPINDLE_PORT
#ifdef COOLANT_PORT
#ifndef COOLANT_FLOOD_PORT
#define COOLANT_FLOOD_PORT (void *)COOLANT_PORT
#endif
#ifndef COOLANT_MIST_PORT
#define COOLANT_MIST_PORT (void *)COOLANT_PORT
#endif
#endif // COOLANT_PORT
// Adjust STEP_PULSE_LATENCY to get accurate step pulse length when required, e.g if using high step rates.
// The default value is calibrated for 10 microseconds length.
// NOTE: step output mode, number of axes and compiler optimization settings may all affect this value.
#ifndef STEP_PULSE_LATENCY
#define STEP_PULSE_LATENCY 1.0f // microseconds
#endif
// End configuration
#if EEPROM_ENABLE == 0
#define FLASH_ENABLE 1
#else
#define FLASH_ENABLE 0
#endif
#ifdef IOEXPAND_ENABLE
#undef I2C_ENABLE
#define I2C_ENABLE 1
#endif
#if I2C_ENABLE && !defined(I2C_PORT)
#define I2C_PORT 1
#define I2C_SDA 26
#define I2C_SCL 27
#endif
#if TRINAMIC_ENABLE
#ifndef TRINAMIC_MIXED_DRIVERS
#define TRINAMIC_MIXED_DRIVERS 1
#endif
#include "motors/trinamic.h"
#endif
#if USB_SERIAL_CDC
#define SP0 1
#else
#define SP0 0
#endif
#ifdef SERIAL1_PORT
#define SP1 1
#else
#define SP1 0
#endif
#if (MODBUS_ENABLE & MODBUS_RTU_ENABLED)
#define MODBUS_TEST 1
#else
#define MODBUS_TEST 0
#endif
#if KEYPAD_ENABLE == 2 && MPG_ENABLE == 0
#define KEYPAD_TEST 1
#else
#define KEYPAD_TEST 0
#endif
#if MPG_ENABLE
#define MPG_TEST 1
#else
#define MPG_TEST 0
#endif
#if (MODBUS_TEST + KEYPAD_TEST + (BLUETOOTH_ENABLE == 2 ? 1 : 0) + TRINAMIC_UART_ENABLE + MPG_TEST) > (SP0 + SP1)
#error "Too many options that uses the serial port are enabled!"
#endif
#undef SP0
#undef SP1
#undef MODBUS_TEST
#undef KEYPAD_TEST
#undef MPG_TEST
// End configuration
#if MPG_ENABLE == 1 && !defined(MPG_MODE_PIN)
#error "MPG_MODE_PIN must be defined!"
#endif
#if KEYPAD_ENABLE == 1 && !defined(I2C_STROBE_PIN)
#error Keypad plugin not supported!
#endif
#if SDCARD_ENABLE && !defined(SD_CS_PIN)
#error SD card plugin not supported!
#endif
#ifndef STEP_PINMODE
#define STEP_PINMODE PINMODE_OUTPUT
#endif
#ifndef DIRECTION_PINMODE
#define DIRECTION_PINMODE PINMODE_OUTPUT
#endif
#ifndef STEPPERS_DISABLE_PINMODE
#define STEPPERS_DISABLE_PINMODE PINMODE_OUTPUT
#endif
typedef struct {
pin_function_t id;
pin_cap_t cap;
pin_mode_t mode;
uint8_t pin;
uint8_t user_port;
pin_group_t group;
uint32_t port;
volatile bool active;
ioport_interrupt_callback_ptr interrupt_callback;
const char *description;
} input_signal_t;
typedef struct {
pin_function_t id;
pin_group_t group;
uint8_t pin;
uint32_t port;
pin_mode_t mode;
const char *description;
uint8_t pwm_idx;
} output_signal_t;
typedef struct {
uint8_t n_pins;
union {
input_signal_t *inputs;
output_signal_t *outputs;
} pins;
} pin_group_pins_t;
bool driver_init (void);
#if OUT_SHIFT_REGISTER
void board_init (pin_group_pins_t *aux_inputs, pin_group_pins_t *aux_outputs, output_sr_t *reg);
#else
void board_init (void);
#endif
#if SPI_RST_PORT == GPIO_SR16
void spi_reset_out (bool on);
#endif
void ioports_init (pin_group_pins_t *aux_inputs, pin_group_pins_t *aux_outputs);
void ioports_init_analog (pin_group_pins_t *aux_inputs, pin_group_pins_t *aux_outputs);
void ioports_event (input_signal_t *input);
void pinEnableIRQ (const input_signal_t *input, pin_irq_mode_t irq_mode);
/**
\brief Enable IRQ Interrupts
\details Enables IRQ interrupts by clearing the I-bit in the CPSR.
Can only be executed in Privileged modes.
*/
// While waiting for CMSIS headers...:
static inline void __enable_irq(void)
{
__asm volatile ("cpsie i" : : : "memory");
}
/**
\brief Disable IRQ Interrupts
\details Disables IRQ interrupts by setting the I-bit in the CPSR.
Can only be executed in Privileged modes.
*/
static inline void __disable_irq(void)
{
__asm volatile ("cpsid i" : : : "memory");
}
#endif // __DRIVER_H__