Skip to content

Commit

Permalink
initial RP2350 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Nov 7, 2024
1 parent 230adb2 commit a693639
Show file tree
Hide file tree
Showing 13 changed files with 4,221 additions and 2 deletions.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extra_configs =
uCNC/src/hal/boards/lpc176x/lpc176x.ini
uCNC/src/hal/boards/esp32/esp32.ini
uCNC/src/hal/boards/rp2040/rp2040.ini
uCNC/src/hal/boards/rp2350/rp2350.ini
uCNC/src/hal/mcus/virtual/virtual.ini

[env]
Expand Down
2 changes: 1 addition & 1 deletion uCNC/cnc_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ extern "C"
* This is useful if you don't have EEPROM/FLASH storage or the divide read/write maximum cycle count is low to prevent damage
* This is also usefull if the sender provides all settings at startup/connection
* */
// #define RAM_ONLY_SETTINGS
// #define RAM_ONLY_SETTINGS

/**
* Override default configuration settings. Use _PER_AXIS parameters to
Expand Down
152 changes: 152 additions & 0 deletions uCNC/src/hal/boards/rp2350/boardmap_rpi_pico2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
Name: boardmap_rp_pico2.h
Description: Contains all MCU and PIN definitions for Raspberry Pi Pico2 to run µCNC.
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 07-11-2024
µCNC 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. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#ifndef BOARDMAP_RPI_PICO2_H
#define BOARDMAP_RPI_PICO2_H

#ifdef __cplusplus
extern "C"
{
#endif

#ifndef MCU
#define MCU MCU_RP2350
#endif

#ifndef BOARD_NAME
#define BOARD_NAME "RPi Pico2"
#endif

// SAME AS GRBL for test purposes
// Setup step pins
#define STEP2_BIT 10 // assigns STEP2 pin
#define STEP1_BIT 6 // assigns STEP1 pin
#define STEP0_BIT 2 // assigns STEP0 pin

// Setup dir pins
#define DIR2_BIT 11 // assigns DIR2 pin
#define DIR1_BIT 7 // assigns DIR1 pin
#define DIR0_BIT 3 // assigns DIR0 pin

#define LIMIT_Z_BIT 13
#define LIMIT_Y_BIT 9
#define LIMIT_X_BIT 5

#define LIMIT_Z_PULLUP
#define LIMIT_Y_PULLUP
#define LIMIT_X_PULLUP

// Setup control input pins
// #define ESTOP_BIT 0
// #define ESTOP_PORT A
// #define ESTOP_ISR

// Setup com pins
#define RX_BIT 1
#define RX_PULLUP
#define TX_BIT 0
// only uncomment this if other port other then 0 is used
// #define UART_PORT 0

// forces USB
#define MCU_HAS_USB

// Setup PWM
#define PWM0_BIT 14 // assigns PWM0 pin

// Setup generic IO Pins
// spindle dir
#define DOUT0_BIT 15

// Stepper enable pin. For Grbl on Uno board a single pin is used
#define STEP2_EN_BIT 12
#define STEP1_EN_BIT 8
#define STEP0_EN_BIT 4

// activity LED
#define DOUT31_BIT 25

// disable EEPROM emulation
// #ifndef RAM_ONLY_SETTINGS
// #define RAM_ONLY_SETTINGS
// #endif

#define I2C_CLK_BIT 27
#define I2C_DATA_BIT 26
#define I2C_PORT 1
// #define I2C_ADDRESS 1

/**
* This is an example of how to use RP2040 PIO to control
* up to 4 chainned 74hc595 (32 output pins) using only 3 pins
* from the board.
* The 3 pins should be sequential (for example GPIO's 26, 27 and 28)
*
* RP2040 does not yet support software generate PWM
*
* **/

// // Use PIO to shift data to 74HC595 shift registers (up to 4)
// // IO pins should be sequencial GPIO pins starting by data, then clock then the latch pin
// #define IC74HC595_CUSTOM_SHIFT_IO //Enables custom MCU data shift transmission. In RP2040 that is via a PIO
// #define IC74HC595_PIO_DATA 26
// #define IC74HC595_PIO_CLK 27
// #define IC74HC595_PIO_LATCH 28
// // enabling IC74HC595_CUSTOM_SHIFT_IO will force IC74HC595_COUNT to be set to 4 no matter what
// // support up to 4 chained 74HC595. Less can be used (overflow bits will be discarded like in the ESP32 I2S implementation)
// #define IC74HC595_COUNT 4

// #define STEP0_EN_IO_OFFSET 0
// #define STEP0_IO_OFFSET 1
// #define DIR0_IO_OFFSET 2
// #define STEP1_EN_IO_OFFSET 3
// #define STEP1_IO_OFFSET 4
// #define DIR1_IO_OFFSET 5
// #define STEP2_EN_IO_OFFSET 6
// #define STEP2_IO_OFFSET 7
// #define DIR2_IO_OFFSET 8
// #define STEP3_EN_IO_OFFSET 9
// #define STEP3_IO_OFFSET 10
// #define DIR3_IO_OFFSET 11
// #define STEP4_EN_IO_OFFSET 12
// #define STEP4_IO_OFFSET 13
// #define DIR4_IO_OFFSET 14
// #define DOUT0_IO_OFFSET 15

#define SPI2_CLK_BIT 18
#define SPI2_SDO_BIT 19
#define SPI2_SDI_BIT 16
#define SPI2_CS_BIT 17
#define SPI2_PORT 0

// #define SPI2_CLK_BIT 10
// #define SPI2_SDO_BIT 11
// #define SPI2_SDI_BIT 12
// #define SPI2_CS_BIT 13
// #define SPI2_PORT 1

/**
* New multicore mode - Experimental
* */
// #define RP2040_RUN_MULTICORE

#ifdef __cplusplus
}
#endif

#endif
46 changes: 46 additions & 0 deletions uCNC/src/hal/boards/rp2350/rp2350.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
##################
# RP2350 Boards #
##################

[common_rp2350]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git
framework = arduino
board_build.core = earlephilhower
board_build.filesystem_size = 0.5m
; change microcontroller
board_build.mcu = rp2350
; change MCU frequency
board_build.f_cpu = 150000000L
; lib_deps = adafruit/Adafruit TinyUSB Library@^2.0.3
build_flags = ${env.build_flags} -std=gnu99 -Wall -fdata-sections -ffunction-sections -fno-exceptions -Wl,--gc-sections -DRAM_ONLY_SETTINGS
debug_tool = cmsis-dap
lib_deps =
${env.lib_deps}
SPI
Wire
EEPROM
lib_ignore =
HTTPUpdateServer
LittleFS
WiFi
WebServer
SerialBT
DNSServer
Hash
BluetoothSerial

[env:RP2350-PICO2]
extends = common_rp2350
board = generic_rp2350
build_flags = ${common_rp2350.build_flags} -D BOARD=\"src/hal/boards/rp2350/boardmap_rpi_pico2.h\"
lib_deps =
${env.lib_deps}
SPI
Wire
EEPROM

[env:RP2350-CUSTOM]
extends = common_rp2350
build_flags = ${common_rp2350.build_flags} -DMCU=MCU_CUSTOM
board = ${env.board}
7 changes: 7 additions & 0 deletions uCNC/src/hal/mcus/mcudefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ extern "C"
#endif
#endif

#if (MCU == MCU_RP2350)
#include "rp2350/mcumap_rp2350.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_RP2350
#endif
#endif

#if (MCU == MCU_VIRTUAL_WIN)
#include "virtual/mcumap_virtual.h"
#endif
Expand Down
1 change: 1 addition & 0 deletions uCNC/src/hal/mcus/mcus.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C"
#define MCU_ESP8266 40
#define MCU_ESP32 50
#define MCU_RP2040 60
#define MCU_RP2350 61
#define MCU_VIRTUAL_WIN 99

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/mcus/rp2040/rp2040_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the GNU General Public License for more details.
*/

#ifdef ARDUINO_ARCH_RP2040
#if defined(ARDUINO_ARCH_RP2040) && !defined(TARGET_RP2350)
#include <stdint.h>
#include <stdbool.h>
#include <Arduino.h>
Expand Down
17 changes: 17 additions & 0 deletions uCNC/src/hal/mcus/rp2350/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
_µCNC for RP2040 can be built this way_

## Method one - PlatformIO (preferred)

1. Get [Visual Studio Code](https://code.visualstudio.com/download) and install it.
2. Install the PlatformIO extension.
3. Open uCNC folder in VSCode.
4. Edit ```cnc_config.h file``` and ```cnc_hal_config.h file``` to fit your needs and board.
5. If needed edit the platformio.ini file environment for your board. Compile the sketch and upload it to your board.

## Method two - Arduino IDE (easiest)

1. Get [Arduino IDE](https://www.arduino.cc/en/software) and install it.
2. If you don't have install ESP8266 for Arduino with Arduino board manager has explained [here](https://github.com/earlephilhower/arduino-pico#installing-via-arduino-boards-manager)
3. Go to uCNC folder and open uCNC.ino sketch.
4. Edit ```cnc_config.h file``` and ```cnc_hal_config.h file``` to fit your needs and board.
5. Compile the sketch and upload it to your board.
57 changes: 57 additions & 0 deletions uCNC/src/hal/mcus/rp2350/ic74hc595.pio
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
;
;Name: ic74hc595.pio
;Description: Implements a custom PIO for 74HC595 shift register in µCNC for RP2040.
;
;Copyright: Copyright (c) João Martins
;Author: João Martins
;Date: 02-11-2023
;
;µCNC 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. Please see <http://www.gnu.org/licenses/>
;
;µCNC is distributed WITHOUT ANY WARRANTY;
;Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;See the GNU General Public License for more details.
;

.program ic74hc595
.side_set 1 opt

pull ; shift TX to OSR
set x, 31 ; load loop counter
set pins, 0 ; set all pins low (latch)
bitloop: ; loop bits
out pins, 1 side 0 ; Shift 1 bit from OSR to the data pin and the remaining down
jmp x-- bitloop side 1 ; decrement loop and bring clock up
set pins, 2 ; set 3rd pin (4=0b100) up (latch)

% c-sdk {
#include "hardware/clocks.h"

static inline void ic74hc595_program_init(PIO pio, uint sm, uint offset, uint pin_data, uint pin_clk, uint pin_latch, uint freq) {
pio_sm_config c = ic74hc595_program_get_default_config(offset);
sm_config_set_out_pins(&c, pin_data, 1); // define one pin to respond to the out instruction
sm_config_set_set_pins(&c, pin_clk, 2); // define all pins to respond to the set instruction
sm_config_set_sideset_pins(&c, pin_clk); // define clock as the side set base pin (and only)
// Only support MSB-first in this example code (shift to left, no auto push/pull, threshold=nbits)
sm_config_set_out_shift(&c, false, false, 32);
// All pins output
pio_sm_set_consecutive_pindirs(pio, sm, pin_data, 3, true);
pio_gpio_init(pio, pin_data);
pio_gpio_init(pio, pin_clk);
pio_gpio_init(pio, pin_latch);
// We only need TX, so get an 8-deep FIFO!
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
// SM transmits 1 bit per 8 execution cycles.
float div = (float)clock_get_hz(clk_sys) / (freq << 1);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}

static inline void ic74hc595_program_write(PIO pio, uint sm, uint out) {
pio_sm_put_blocking(pio, sm, out);
}
%}
71 changes: 71 additions & 0 deletions uCNC/src/hal/mcus/rp2350/ic74hc595.pio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// -------------------------------------------------- //
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //

#pragma once

#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif

// --------- //
// ic74hc595 //
// --------- //

#define ic74hc595_wrap_target 0
#define ic74hc595_wrap 5

static const uint16_t ic74hc595_program_instructions[] = {
// .wrap_target
0x80a0, // 0: pull block
0xe03f, // 1: set x, 31
0xe000, // 2: set pins, 0
0x7001, // 3: out pins, 1 side 0
0x1843, // 4: jmp x--, 3 side 1
0xe002, // 5: set pins, 2
// .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program ic74hc595_program = {
.instructions = ic74hc595_program_instructions,
.length = 6,
.origin = -1,
};

static inline pio_sm_config ic74hc595_program_get_default_config(uint offset)
{
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + ic74hc595_wrap_target, offset + ic74hc595_wrap);
sm_config_set_sideset(&c, 2, true, false);
return c;
}

#include "hardware/clocks.h"
static inline void ic74hc595_program_init(PIO pio, uint sm, uint offset, uint pin_data, uint pin_clk, uint pin_latch, uint freq)
{
pio_sm_config c = ic74hc595_program_get_default_config(offset);
sm_config_set_out_pins(&c, pin_data, 1); // define one pin to respond to the out instruction
sm_config_set_set_pins(&c, pin_clk, 2); // define all pins to respond to the set instruction
sm_config_set_sideset_pins(&c, pin_clk); // define clock as the side set base pin (and only)
// Only support MSB-first in this example code (shift to left, no auto push/pull, threshold=nbits)
sm_config_set_out_shift(&c, false, false, 32);
// All pins output
pio_sm_set_consecutive_pindirs(pio, sm, pin_data, 3, true);
pio_gpio_init(pio, pin_data);
pio_gpio_init(pio, pin_clk);
pio_gpio_init(pio, pin_latch);
// We only need TX, so get an 8-deep FIFO!
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
// SM transmits 1 bit per 8 execution cycles.
float div = (float)clock_get_hz(clk_sys) / (freq << 1);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
static inline void ic74hc595_program_write(PIO pio, uint sm, uint out)
{
pio_sm_put_blocking(pio, sm, out);
}

#endif
Loading

0 comments on commit a693639

Please sign in to comment.