From 33ec599ca0bcb10a8e104869fde975ad211a1b68 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 3 Feb 2018 20:25:57 -0600 Subject: [PATCH] Tweaky LPC1768 SPI spacing/style --- Marlin/src/HAL/HAL_LPC1768/HAL_spi.cpp | 2 +- Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.cpp | 50 ++++++---------------- Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.h | 36 ++++++++-------- 3 files changed, 31 insertions(+), 57 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL_spi.cpp b/Marlin/src/HAL/HAL_LPC1768/HAL_spi.cpp index 16e8eb744..bdc0b2053 100644 --- a/Marlin/src/HAL/HAL_LPC1768/HAL_spi.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/HAL_spi.cpp @@ -47,7 +47,7 @@ * https://github.com/MarlinFirmware/Marlin/tree/071c7a78f27078fd4aee9a3ef365fcf5e143531e */ -#include "src/inc/MarlinConfig.h" +#include "../../inc/MarlinConfig.h" #ifdef TARGET_LPC1768 diff --git a/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.cpp b/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.cpp index 03f9fcff8..14c01e83b 100644 --- a/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.cpp @@ -26,11 +26,10 @@ */ /** - * * For TARGET_LPC1768 */ -#include "src/inc/MarlinConfig.h" +#include "../../inc/MarlinConfig.h" #ifdef TARGET_LPC1768 @@ -49,68 +48,43 @@ * 6 - about 125 kHz peak */ -uint8_t swSpiTransfer(uint8_t b, uint8_t spi_speed, pin_t sck_pin, pin_t miso_pin, pin_t mosi_pin) { +uint8_t swSpiTransfer(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin) { for (uint8_t i = 0; i < 8; i++) { if (spi_speed == 0) { - if (b & 0x80) - WRITE(mosi_pin, HIGH); - else - WRITE(mosi_pin, LOW); - + WRITE(mosi_pin, !!(b & 0x80)); WRITE(sck_pin, HIGH); - b <<= 1; - - if (miso_pin >= 0) - if (READ(miso_pin)) b |= 1; - + if (miso_pin >= 0 && READ(miso_pin)) b |= 1; WRITE(sck_pin, LOW); } else { - if (b & 0x80) - for (uint8_t j = 0; j < spi_speed; j++) - WRITE(mosi_pin, HIGH); - else - for (uint8_t j = 0; j < spi_speed; j++) - WRITE(mosi_pin, LOW); + const uint8_t state = (b & 0x80) ? HIGH : LOW; + for (uint8_t j = 0; j < spi_speed; j++) + WRITE(mosi_pin, state); for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++) WRITE(sck_pin, HIGH); b <<= 1; - - if (miso_pin >= 0) - if (READ(miso_pin)) b |= 1; + if (miso_pin >= 0 && READ(miso_pin)) b |= 1; for (uint8_t j = 0; j < spi_speed; j++) WRITE(sck_pin, LOW); } } - return b; } -void swSpiBegin(pin_t sck_pin, pin_t miso_pin, pin_t mosi_pin) { +void swSpiBegin(const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin) { SET_OUTPUT(sck_pin); - if (VALID_PIN(miso_pin)) - SET_INPUT(miso_pin); + if (VALID_PIN(miso_pin)) SET_INPUT(miso_pin); SET_OUTPUT(mosi_pin); } -uint8_t swSpiInit(uint8_t spiRate, pin_t sck_pin, pin_t mosi_pin) { - uint8_t spi_speed = 0; - - spiRate = MIN(spiRate, 6); - - if (SystemCoreClock == 120000000) - spi_speed = 44 / POW(2, 6 - spiRate); - else - spi_speed = 38 / POW(2, 6 - spiRate); - +uint8_t swSpiInit(const uint8_t spiRate, const pin_t sck_pin, const pin_t mosi_pin) { WRITE(mosi_pin, HIGH); WRITE(sck_pin, LOW); - - return spi_speed; + return (SystemCoreClock == 120000000 ? 44 : 38) / POW(2, 6 - min(spiRate, 6)); } #endif // TARGET_LPC1768 diff --git a/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.h b/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.h index 4214cd5f4..6631c0f32 100644 --- a/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.h +++ b/Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.h @@ -23,28 +23,28 @@ #ifndef _SOFTWARE_SPI_H_ #define _SOFTWARE_SPI_H_ - #include "pinmapping.h" +#include "pinmapping.h" - // -------------------------------------------------------------------------- - // software SPI - // -------------------------------------------------------------------------- +// -------------------------------------------------------------------------- +// Software SPI +// -------------------------------------------------------------------------- - /** - * This software SPI runs at multiple rates. The SD software provides an index - * (spiRate) of 0-6. The mapping is: - * 0 - about 5 MHz peak (6 MHz on LPC1769) - * 1-2 - about 2 MHz peak - * 3 - about 1 MHz peak - * 4 - about 500 kHz peak - * 5 - about 250 kHz peak - * 6 - about 125 kHz peak - */ +/** + * This software SPI runs at multiple rates. The SD software provides an index + * (spiRate) of 0-6. The mapping is: + * 0 - about 5 MHz peak (6 MHz on LPC1769) + * 1-2 - about 2 MHz peak + * 3 - about 1 MHz peak + * 4 - about 500 kHz peak + * 5 - about 250 kHz peak + * 6 - about 125 kHz peak + */ - void swSpiBegin(pin_t sck_pin, pin_t miso_pin, pin_t mosi_pin); +void swSpiBegin(const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin); - // Returns the spi_speed value to be passed to swSpiTransfer - uint8_t swSpiInit(uint8_t spiRate, pin_t sck_pin, pin_t mosi_pin); +// Returns the spi_speed value to be passed to swSpiTransfer +uint8_t swSpiInit(const uint8_t spiRate, const pin_t sck_pin, const pin_t mosi_pin); - uint8_t swSpiTransfer(uint8_t b, uint8_t spi_speed, pin_t sck_pin, pin_t miso_pin, pin_t mosi_pin); +uint8_t swSpiTransfer(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin); #endif // _SOFTWARE_SPI_H_