Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ SD- and SPI-related cleanup #27650

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,7 @@
*
* SCLK, MOSI, MISO --> SCLK, MOSI, MISO
* INT --> SD_DETECT_PIN [1]
* SS --> SDSS
* SS --> SD_SS_PIN
*
* [1] On AVR an interrupt-capable pin is best for UHS3 compatibility.
*/
Expand All @@ -1896,7 +1896,7 @@
//#define USE_OTG_USB_HOST

#if DISABLED(USE_OTG_USB_HOST)
#define USB_CS_PIN SDSS
#define USB_CS_PIN SD_SS_PIN
#define USB_INTR_PIN SD_DETECT_PIN
#endif
#endif
Expand Down
50 changes: 24 additions & 26 deletions Marlin/src/HAL/AVR/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,41 @@

/**
* Define SPI Pins: SCK, MISO, MOSI, SS
* Platform pins have parentheses, e.g., "(53)", so we cannot use them.
*/
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
#define AVR_SCK_PIN 13
#define AVR_MISO_PIN 12
#define AVR_MOSI_PIN 11
#define AVR_SS_PIN 10
#define _PIN_SPI_SCK 13
#define _PIN_SPI_MISO 12
#define _PIN_SPI_MOSI 11
#define _PIN_SPI_SS 10
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__)
#define AVR_SCK_PIN 7
#define AVR_MISO_PIN 6
#define AVR_MOSI_PIN 5
#define AVR_SS_PIN 4
#define _PIN_SPI_SCK 7
#define _PIN_SPI_MISO 6
#define _PIN_SPI_MOSI 5
#define _PIN_SPI_SS 4
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define AVR_SCK_PIN 52
#define AVR_MISO_PIN 50
#define AVR_MOSI_PIN 51
#define AVR_SS_PIN 53
#define _PIN_SPI_SCK 52
#define _PIN_SPI_MISO 50
#define _PIN_SPI_MOSI 51
#define _PIN_SPI_SS 53
#elif defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__)
#define AVR_SCK_PIN 21
#define AVR_MISO_PIN 23
#define AVR_MOSI_PIN 22
#define AVR_SS_PIN 20
#define _PIN_SPI_SCK 21
#define _PIN_SPI_MISO 23
#define _PIN_SPI_MOSI 22
#define _PIN_SPI_SS 20
#elif defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
#define AVR_SCK_PIN 10
#define AVR_MISO_PIN 12
#define AVR_MOSI_PIN 11
#define AVR_SS_PIN 16
#define _PIN_SPI_SCK 10
#define _PIN_SPI_MISO 12
#define _PIN_SPI_MOSI 11
#define _PIN_SPI_SS 16
#endif

#ifndef SD_SCK_PIN
#define SD_SCK_PIN AVR_SCK_PIN
#define SD_SCK_PIN _PIN_SPI_SCK
#endif
#ifndef SD_MISO_PIN
#define SD_MISO_PIN AVR_MISO_PIN
#define SD_MISO_PIN _PIN_SPI_MISO
#endif
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN AVR_MOSI_PIN
#endif
#ifndef SD_SS_PIN
#define SD_SS_PIN AVR_SS_PIN
#define SD_MOSI_PIN _PIN_SPI_MOSI
#endif
2 changes: 1 addition & 1 deletion Marlin/src/HAL/DUE/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ uint16_t MarlinHAL::adc_result;

void MarlinHAL::init() {
#if HAS_MEDIA
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
OUT_WRITE(SD_SS_PIN, HIGH); // Try to set SDSS inactive before any other SPI users start up
#endif
usb_task_init(); // Initialize the USB stack
TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler
Expand Down
7 changes: 3 additions & 4 deletions Marlin/src/HAL/DUE/HAL_SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,8 @@
OUT_WRITE(SPI_EEPROM1_CS_PIN, HIGH);
OUT_WRITE(SPI_EEPROM2_CS_PIN, HIGH);
OUT_WRITE(SPI_FLASH_CS_PIN, HIGH);
WRITE(SD_SS_PIN, HIGH);

OUT_WRITE(SDSS, LOW);
OUT_WRITE(SD_SS_PIN, HIGH);
WRITE(SD_SS_PIN, LOW);

PIO_Configure(
g_APinDescription[SPI_PIN].pPort,
Expand Down Expand Up @@ -767,7 +766,7 @@

// Disable PIO on A26 and A27
REG_PIOA_PDR = 0x0C000000;
OUT_WRITE(SDSS, HIGH);
OUT_WRITE(SD_SS_PIN, HIGH);

// Reset SPI0 (from sam lib)
SPI0->SPI_CR = SPI_CR_SPIDIS;
Expand Down
51 changes: 24 additions & 27 deletions Marlin/src/HAL/DUE/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,36 @@
*
* Available chip select pins for HW SPI are 4 10 52 77 87
*/
#if SDSS == 4 || SDSS == 10 || SDSS == 52 || SDSS == 77 || SDSS == 87
#if SDSS == 4
#define SPI_PIN 87
#define SPI_CHAN 1
#elif SDSS == 10
#define SPI_PIN 77
#define SPI_CHAN 0
#elif SDSS == 52
#define SPI_PIN 86
#define SPI_CHAN 2
#elif SDSS == 77
#define SPI_PIN 77
#define SPI_CHAN 0
#else
#define SPI_PIN 87
#define SPI_CHAN 1
#endif
#define SD_SCK_PIN 76
#define SD_MISO_PIN 74
#define SD_MOSI_PIN 75
#if SD_SS_PIN == 4 || SD_SS_PIN == 10 || SD_SS_PIN == 52 || SD_SS_PIN == 77 || SD_SS_PIN == 87
#define SD_SCK_PIN 76
#define SD_MISO_PIN 74
#define SD_MOSI_PIN 75
#endif

#if SD_SS_PIN == 4
#define SPI_PIN 87
#define SPI_CHAN 1
#elif SD_SS_PIN == 10
#define SPI_PIN 77
#define SPI_CHAN 0
#elif SD_SS_PIN == 52
#define SPI_PIN 86
#define SPI_CHAN 2
#elif SD_SS_PIN == 77
#define SPI_PIN 77
#define SPI_CHAN 0
#elif SD_SS_PIN == 87
#define SPI_PIN 87
#define SPI_CHAN 1
#else
// defaults
#define SOFTWARE_SPI
#ifndef SD_SCK_PIN
#define SD_SCK_PIN 52
#define SD_SCK_PIN 52
#endif
#ifndef SD_MISO_PIN
#define SD_MISO_PIN 50
#define SD_MISO_PIN 50
#endif
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN 51
#define SD_MOSI_PIN 51
#endif
#endif

/* A.28, A.29, B.21, C.26, C.29 */
#define SD_SS_PIN SDSS
17 changes: 13 additions & 4 deletions Marlin/src/HAL/ESP32/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
*/
#pragma once

#define SD_SS_PIN SDSS
#define SD_SCK_PIN 18
#define SD_MISO_PIN 19
#define SD_MOSI_PIN 23
#define PIN_SPI_SCK 18
#define PIN_SPI_MISO 19
#define PIN_SPI_MOSI 23

#ifndef SD_SCK_PIN
#define SD_SCK_PIN PIN_SPI_SCK
#endif
#ifndef SD_MISO_PIN
#define SD_MISO_PIN PIN_SPI_MISO
#endif
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN PIN_SPI_MOSI
#endif
6 changes: 0 additions & 6 deletions Marlin/src/HAL/LINUX/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,3 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN 52
#endif
#ifndef SD_SS_PIN
#define SD_SS_PIN 53
#endif
#ifndef SDSS
#define SDSS SD_SS_PIN
#endif
6 changes: 3 additions & 3 deletions Marlin/src/HAL/LPC1768/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static_assert(DISABLED(BAUD_RATE_GCODE), "BAUD_RATE_GCODE is not yet supported o
#if IS_TX1(BTN_EN2) || IS_RX1(BTN_EN1)
#error "Serial port pins (1) conflict with Encoder Buttons!"
#elif ANY_TX(1, SD_SCK_PIN, LCD_PINS_D4, DOGLCD_SCK, LCD_RESET_PIN, LCD_PINS_RS, SHIFT_CLK_PIN) \
|| ANY_RX(1, LCD_SDSS, LCD_PINS_RS, SD_MISO_PIN, DOGLCD_A0, SD_SS_PIN, LCD_SDSS, DOGLCD_CS, LCD_RESET_PIN, LCD_BACKLIGHT_PIN)
|| ANY_RX(1, LCD_SDSS_PIN, LCD_PINS_RS, SD_MISO_PIN, DOGLCD_A0, SD_SS_PIN, DOGLCD_CS, LCD_RESET_PIN, LCD_BACKLIGHT_PIN)
#error "Serial port pins (1) conflict with LCD pins!"
#endif
#endif
Expand Down Expand Up @@ -211,8 +211,8 @@ static_assert(DISABLED(BAUD_RATE_GCODE), "BAUD_RATE_GCODE is not yet supported o
#error "SCL0 overlaps with Encoder Button!"
#elif IS_SCL0(SD_SS_PIN)
#error "SCL0 overlaps with SD_SS_PIN!"
#elif IS_SCL0(LCD_SDSS)
#error "SCL0 overlaps with LCD_SDSS!"
#elif IS_SCL0(LCD_SDSS_PIN)
#error "SCL0 overlaps with LCD_SDSS_PIN!"
#endif
#undef PIN_IS_SDA0
#undef IS_SCL0
Expand Down
8 changes: 0 additions & 8 deletions Marlin/src/HAL/LPC1768/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
//#define SD_SCK_PIN P0_07
//#define SD_MISO_PIN P0_08
//#define SD_MOSI_PIN P0_09
//#define SD_SS_PIN P0_06

// External SD
#ifndef SD_SCK_PIN
Expand All @@ -44,10 +43,3 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN P0_18
#endif
#ifndef SD_SS_PIN
#define SD_SS_PIN P1_23
#endif
#if !defined(SDSS) || SDSS == P_NC // gets defaulted in pins.h
#undef SDSS
#define SDSS SD_SS_PIN
#endif
7 changes: 0 additions & 7 deletions Marlin/src/HAL/NATIVE_SIM/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
//#define SD_SCK_PIN P0_07
//#define SD_MISO_PIN P0_08
//#define SD_MOSI_PIN P0_09
//#define SD_SS_PIN P0_06

// External SD
#ifndef SD_SCK_PIN
Expand All @@ -44,9 +43,3 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN 52
#endif
#ifndef SD_SS_PIN
#define SD_SS_PIN 53
#endif
#ifndef SDSS
#define SDSS SD_SS_PIN
#endif
7 changes: 2 additions & 5 deletions Marlin/src/HAL/RP2040/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ void MarlinHAL::init() {
constexpr int cpuFreq = F_CPU;
UNUSED(cpuFreq);

#undef SDSS
#define SDSS 2
#define PIN_EXISTS_(P1,P2) (defined(P1##P2) && P1##P2 >= 0)
#if HAS_MEDIA && DISABLED(SDIO_SUPPORT) && PIN_EXISTS_(SDSS,)
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
#if HAS_MEDIA && DISABLED(SDIO_SUPPORT) && PIN_EXISTS(SD_SS)
OUT_WRITE(SD_SS_PIN, HIGH); // Try to set SD_SS_PIN inactive before any other SPI users start up
#endif

#if PIN_EXISTS(LED)
Expand Down
5 changes: 2 additions & 3 deletions Marlin/src/HAL/RP2040/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
/**
* Define SPI Pins: SCK, MISO, MOSI, SS
*/
#define PIN_SPI_SS 2

#ifndef SD_SCK_PIN
#define SD_SCK_PIN PIN_SPI_SCK
#endif
Expand All @@ -33,6 +35,3 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN PIN_SPI_MOSI
#endif
#ifndef SD_SS_PIN
#define SD_SS_PIN PIN_SPI_SS
#endif
2 changes: 1 addition & 1 deletion Marlin/src/HAL/SAMD21/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void MarlinHAL::init() {
#if HAS_SD_DETECT && SD_CONNECTION_IS(ONBOARD)
SET_INPUT_PULLUP(SD_DETECT_PIN);
#endif
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
OUT_WRITE(SD_SS_PIN, HIGH); // Try to set SDSS inactive before any other SPI users start up
#endif
}

Expand Down
7 changes: 0 additions & 7 deletions Marlin/src/HAL/SAMD21/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,3 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN 37
#endif
#ifndef SDSS
#define SDSS 18
#endif

#ifndef SD_SS_PIN
#define SD_SS_PIN SDSS
#endif
2 changes: 1 addition & 1 deletion Marlin/src/HAL/SAMD51/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ void MarlinHAL::init() {
#if HAS_SD_DETECT && SD_CONNECTION_IS(ONBOARD)
SET_INPUT_PULLUP(SD_DETECT_PIN);
#endif
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
OUT_WRITE(SD_SS_PIN, HIGH); // Try to set SDSS inactive before any other SPI users start up
#endif
}

Expand Down
5 changes: 0 additions & 5 deletions Marlin/src/HAL/SAMD51/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN 51
#endif
#ifndef SDSS
#define SDSS 53
#endif

#else

#error "Unsupported board!"

#endif

#define SD_SS_PIN SDSS
4 changes: 2 additions & 2 deletions Marlin/src/HAL/STM32/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ void MarlinHAL::init() {
constexpr int cpuFreq = F_CPU;
UNUSED(cpuFreq);

#if HAS_MEDIA && DISABLED(ONBOARD_SDIO) && (defined(SDSS) && SDSS != -1)
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
#if HAS_MEDIA && DISABLED(ONBOARD_SDIO) && PIN_EXISTS(SD_SS)
OUT_WRITE(SD_SS_PIN, HIGH); // Try to set SDSS inactive before any other SPI users start up
#endif

#if PIN_EXISTS(LED)
Expand Down
3 changes: 0 additions & 3 deletions Marlin/src/HAL/STM32/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN PIN_SPI_MOSI
#endif
#ifndef SD_SS_PIN
#define SD_SS_PIN PIN_SPI_SS
#endif
5 changes: 0 additions & 5 deletions Marlin/src/HAL/STM32F1/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
#ifndef SD_MOSI_PIN
#define SD_MOSI_PIN PA7
#endif
#ifndef SD_SS_PIN
#define SD_SS_PIN PA4
#endif
#undef SDSS
#define SDSS SD_SS_PIN

#ifndef SPI_DEVICE
#define SPI_DEVICE 1
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/HAL/TEENSY31_32/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@
#define SD_SCK_PIN 13
#define SD_MISO_PIN 12
#define SD_MOSI_PIN 11
#define SD_SS_PIN 20 // SDSS // A.28, A.29, B.21, C.26, C.29
1 change: 0 additions & 1 deletion Marlin/src/HAL/TEENSY35_36/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@
#define SD_SCK_PIN 13
#define SD_MISO_PIN 12
#define SD_MOSI_PIN 11
#define SD_SS_PIN 20 // SDSS // A.28, A.29, B.21, C.26, C.29
1 change: 0 additions & 1 deletion Marlin/src/HAL/TEENSY40_41/spi_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@
#define SD_SCK_PIN 13
#define SD_MISO_PIN 12
#define SD_MOSI_PIN 11
#define SD_SS_PIN 20 // SDSS // A.28, A.29, B.21, C.26, C.29
Loading
Loading