From 26de051e928111e7b6fd4df87e5804e1286714fa Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 14 Jun 2019 20:16:22 -0500 Subject: [PATCH] HAL SPI pin init cleanup --- Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp | 51 ++++++------ .../HAL/HAL_TEENSY31_32/HAL_spi_Teensy.cpp | 6 +- .../HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.h | 4 +- .../HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp | 79 +++++++++++-------- .../HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp | 1 - Marlin/src/HAL/shared/SpiEeprom.cpp | 3 +- 6 files changed, 71 insertions(+), 73 deletions(-) diff --git a/Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp b/Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp index 592a4bca68..995228171d 100644 --- a/Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp +++ b/Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp @@ -21,12 +21,12 @@ */ /** - * Originally from Arduino Sd2Card Library + * Adapted from Arduino Sd2Card Library * Copyright (C) 2009 by William Greiman */ /** - * Description: HAL for AVR - SPI functions + * HAL for AVR - SPI functions */ #ifdef __AVR__ @@ -37,35 +37,24 @@ #include "../../inc/MarlinConfig.h" -// -------------------------------------------------------------------------- -// Public Variables -// -------------------------------------------------------------------------- - - -// -------------------------------------------------------------------------- -// Public functions -// -------------------------------------------------------------------------- - -void spiBegin (void) { - SET_OUTPUT(SS_PIN); - WRITE(SS_PIN, HIGH); +void spiBegin(void) { + OUT_WRITE(SS_PIN, HIGH); SET_OUTPUT(SCK_PIN); SET_INPUT(MISO_PIN); SET_OUTPUT(MOSI_PIN); #if DISABLED(SOFTWARE_SPI) // SS must be in output mode even it is not chip select - SET_OUTPUT(SS_PIN); + //SET_OUTPUT(SS_PIN); // set SS high - may be chip select for another SPI device - #if SET_SPI_SS_HIGH - WRITE(SS_PIN, HIGH); - #endif // SET_SPI_SS_HIGH + //#if SET_SPI_SS_HIGH + //WRITE(SS_PIN, HIGH); + //#endif // set a default rate spiInit(1); - #endif // SOFTWARE_SPI + #endif } - #if DISABLED(SOFTWARE_SPI, FORCE_SOFT_SPI) //------------------------------------------------------------------------------ @@ -190,22 +179,26 @@ void spiBegin (void) { } -#else +#else // SOFTWARE_SPI || FORCE_SOFT_SPI - /** nop to tune soft SPI timing */ + //------------------------------------------------------------------------------ + // Software SPI + //------------------------------------------------------------------------------ + + // nop to tune soft SPI timing #define nop asm volatile ("\tnop\n") - /** Set SPI rate */ + // Set SPI rate void spiInit(uint8_t spiRate) { UNUSED(spiRate); // nothing to do } - /** Begin SPI transaction, set clock, bit order, data mode */ + // Begin SPI transaction, set clock, bit order, data mode void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { UNUSED(spiBeginTransaction); // nothing to do } - /** Soft SPI receive byte */ + // Soft SPI receive byte uint8_t spiRec() { uint8_t data = 0; // no interrupts during byte receive - about 8µs @@ -230,13 +223,13 @@ void spiBegin (void) { return data; } - /** Soft SPI read data */ + // Soft SPI read data void spiRead(uint8_t* buf, uint16_t nbyte) { for (uint16_t i = 0; i < nbyte; i++) buf[i] = spiRec(); } - /** Soft SPI send byte */ + // Soft SPI send byte void spiSend(uint8_t data) { // no interrupts during byte send - about 8µs cli(); @@ -257,13 +250,13 @@ void spiBegin (void) { sei(); } - /** Soft SPI send block */ + // Soft SPI send block void spiSendBlock(uint8_t token, const uint8_t* buf) { spiSend(token); for (uint16_t i = 0; i < 512; i++) spiSend(buf[i]); } -#endif // SOFTWARE_SPI, FORCE_SOFT_SPI +#endif // SOFTWARE_SPI || FORCE_SOFT_SPI #endif // __AVR__ diff --git a/Marlin/src/HAL/HAL_TEENSY31_32/HAL_spi_Teensy.cpp b/Marlin/src/HAL/HAL_TEENSY31_32/HAL_spi_Teensy.cpp index 331703dc55..22e6f620c7 100644 --- a/Marlin/src/HAL/HAL_TEENSY31_32/HAL_spi_Teensy.cpp +++ b/Marlin/src/HAL/HAL_TEENSY31_32/HAL_spi_Teensy.cpp @@ -38,14 +38,12 @@ void spiBegin(void) { #if !PIN_EXISTS(SS) #error "SS_PIN not defined!" #endif - SET_OUTPUT(SS_PIN); - WRITE(SS_PIN, HIGH); + OUT_WRITE(SS_PIN, HIGH); SET_OUTPUT(SCK_PIN); SET_INPUT(MISO_PIN); SET_OUTPUT(MOSI_PIN); - //#if DISABLED(SOFTWARE_SPI) - #if 0 + #if 0 && DISABLED(SOFTWARE_SPI) // set SS high - may be chip select for another SPI device #if SET_SPI_SS_HIGH WRITE(SS_PIN, HIGH); diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.h b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.h index d02cdc9f74..4ed5eeb11b 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.h @@ -23,7 +23,7 @@ #include -// Inherit and expand on the official library +// Inherit and expand on core Servo library class libServo : public Servo { public: int8_t attach(const int pin); @@ -32,5 +32,5 @@ class libServo : public Servo { private: uint16_t min_ticks; uint16_t max_ticks; - uint8_t servoIndex; // index into the channel data for this servo + uint8_t servoIndex; // Index into the channel data for this servo }; diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp index 6220225a1d..b5a7f46cb0 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp @@ -1,3 +1,24 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program 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. + * + * This program 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 this program. If not, see . + * + */ #if defined(__MK64FX512__) || defined(__MK66FX1M0__) #include "HAL.h" @@ -8,32 +29,27 @@ static SPISettings spiConfig; -// Standard SPI functions -/** Initialize SPI bus */ void spiBegin(void) { #if !PIN_EXISTS(SS) #error SS_PIN not defined! #endif - SET_OUTPUT(SS_PIN); - WRITE(SS_PIN, HIGH); + OUT_WRITE(SS_PIN, HIGH); SET_OUTPUT(SCK_PIN); SET_INPUT(MISO_PIN); SET_OUTPUT(MOSI_PIN); - //#if DISABLED(SOFTWARE_SPI) - #if 0 + #if 0 && DISABLED(SOFTWARE_SPI) // set SS high - may be chip select for another SPI device #if SET_SPI_SS_HIGH WRITE(SS_PIN, HIGH); - #endif // SET_SPI_SS_HIGH + #endif // set a default rate spiInit(SPI_HALF_SPEED); // 1 - #endif // SOFTWARE_SPI + #endif } -/** Configure SPI for specified SPI speed */ void spiInit(uint8_t spiRate) { - // Use datarates Marlin uses + // Use Marlin data-rates uint32_t clock; switch (spiRate) { case SPI_FULL_SPEED: clock = 10000000; break; @@ -49,44 +65,39 @@ void spiInit(uint8_t spiRate) { SPI.begin(); } -//------------------------------------------------------------------------------ -/** SPI receive a byte */ uint8_t spiRec(void) { SPI.beginTransaction(spiConfig); uint8_t returnByte = SPI.transfer(0xFF); SPI.endTransaction(); return returnByte; -// SPDR = 0xFF; -// while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } -// return SPDR; + //SPDR = 0xFF; + //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + //return SPDR; } -//------------------------------------------------------------------------------ -/** SPI read data */ + void spiRead(uint8_t* buf, uint16_t nbyte) { SPI.beginTransaction(spiConfig); SPI.transfer(buf, nbyte); SPI.endTransaction(); -//if (nbyte-- == 0) return; -// SPDR = 0xFF; -//for (uint16_t i = 0; i < nbyte; i++) { -// while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } -// buf[i] = SPDR; -// SPDR = 0xFF; -//} -//while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } -//buf[nbyte] = SPDR; + //if (nbyte-- == 0) return; + // SPDR = 0xFF; + //for (uint16_t i = 0; i < nbyte; i++) { + // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + // buf[i] = SPDR; + // SPDR = 0xFF; + //} + //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + //buf[nbyte] = SPDR; } -//------------------------------------------------------------------------------ -/** SPI send a byte */ + void spiSend(uint8_t b) { SPI.beginTransaction(spiConfig); SPI.transfer(b); SPI.endTransaction(); -// SPDR = b; -// while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + //SPDR = b; + //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } } -//------------------------------------------------------------------------------ -/** SPI send block */ + void spiSendBlock(uint8_t token, const uint8_t* buf) { SPI.beginTransaction(spiConfig); SPDR = token; @@ -100,11 +111,9 @@ void spiSendBlock(uint8_t token, const uint8_t* buf) { SPI.endTransaction(); } - -/** Begin SPI transaction, set clock, bit order, data mode */ +// Begin SPI transaction, set clock, bit order, data mode void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { spiConfig = SPISettings(spiClock, bitOrder, dataMode); - SPI.beginTransaction(spiConfig); } diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp index 471d823da5..4c84715a26 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp @@ -20,7 +20,6 @@ * */ - /** * Teensy3.5 __MK64FX512__ * Teensy3.6 __MK66FX1M0__ diff --git a/Marlin/src/HAL/shared/SpiEeprom.cpp b/Marlin/src/HAL/shared/SpiEeprom.cpp index 525c8c0a7d..0efbe2d1c8 100644 --- a/Marlin/src/HAL/shared/SpiEeprom.cpp +++ b/Marlin/src/HAL/shared/SpiEeprom.cpp @@ -115,5 +115,4 @@ void eeprom_update_block(const void* src, void* eeprom_address, size_t n) { delay(7); // wait for page write to complete } - -#endif // ENABLED(SPI_EEPROM) +#endif // SPI_EEPROM