From 297666ea141886e039e62e426e5455299b83087f Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 5 Nov 2017 19:13:40 -0600 Subject: [PATCH] Use fastio for Alligator dac084s085 --- Marlin/src/feature/dac/dac_dac084s085.cpp | 97 ++++++++++------------- Marlin/src/feature/dac/dac_dac084s085.h | 4 +- 2 files changed, 44 insertions(+), 57 deletions(-) diff --git a/Marlin/src/feature/dac/dac_dac084s085.cpp b/Marlin/src/feature/dac/dac_dac084s085.cpp index 09ab08b3a8..e1aeb7e438 100644 --- a/Marlin/src/feature/dac/dac_dac084s085.cpp +++ b/Marlin/src/feature/dac/dac_dac084s085.cpp @@ -13,100 +13,85 @@ #include "../../Marlin.h" #include "../../module/stepper.h" -dac084s085::dac084s085() { - return ; -} +dac084s085::dac084s085() { } void dac084s085::begin() { - uint8_t externalDac_buf[2] = {0x20,0x00};//all off + uint8_t externalDac_buf[] = { 0x20, 0x00 }; // all off // All SPI chip-select HIGH - pinMode(DAC0_SYNC, OUTPUT); - digitalWrite( DAC0_SYNC , HIGH ); + SET_OUTPUT(DAC0_SYNC); #if EXTRUDERS > 1 - pinMode(DAC1_SYNC, OUTPUT); - digitalWrite( DAC1_SYNC , HIGH ); + SET_OUTPUT(DAC1_SYNC); #endif - digitalWrite( SPI_EEPROM1_CS , HIGH ); - digitalWrite( SPI_EEPROM2_CS , HIGH ); - digitalWrite( SPI_FLASH_CS , HIGH ); - digitalWrite( SS_PIN , HIGH ); + cshigh(); spiBegin(); //init onboard DAC delayMicroseconds(2U); - digitalWrite( DAC0_SYNC , LOW ); + WRITE(DAC0_SYNC, LOW); delayMicroseconds(2U); - digitalWrite( DAC0_SYNC , HIGH ); + WRITE(DAC0_SYNC, HIGH); delayMicroseconds(2U); - digitalWrite( DAC0_SYNC , LOW ); + WRITE(DAC0_SYNC, LOW); - spiSend(SPI_CHAN_DAC,externalDac_buf , 2); - digitalWrite( DAC0_SYNC , HIGH ); + spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf)); + WRITE(DAC0_SYNC, HIGH); #if EXTRUDERS > 1 //init Piggy DAC delayMicroseconds(2U); - digitalWrite( DAC1_SYNC , LOW ); + WRITE(DAC1_SYNC, LOW); delayMicroseconds(2U); - digitalWrite( DAC1_SYNC , HIGH ); + WRITE(DAC1_SYNC, HIGH); delayMicroseconds(2U); - digitalWrite( DAC1_SYNC , LOW ); + WRITE(DAC1_SYNC, LOW); - spiSend(SPI_CHAN_DAC,externalDac_buf , 2); - digitalWrite( DAC1_SYNC , HIGH ); + spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf)); + WRITE(DAC1_SYNC, HIGH); #endif return; } -void dac084s085::setValue(uint8_t channel, uint8_t value) { - if(channel >= 7) // max channel (X,Y,Z,E0,E1,E2,E3) - return; - if(value > 255) value = 255; +void dac084s085::setValue(const uint8_t channel, const uint8_t value) { + if (channel >= 7) return; // max channel (X,Y,Z,E0,E1,E2,E3) - uint8_t externalDac_buf[2] = {0x10,0x00}; - - if(channel > 3) - externalDac_buf[0] |= (7 - channel << 6); - else - externalDac_buf[0] |= (3 - channel << 6); - - externalDac_buf[0] |= (value >> 4); - externalDac_buf[1] |= (value << 4); + const uint8_t externalDac_buf[] = { + 0x10 | ((channel > 3 ? 7 : 3) - channel << 6) | (value >> 4), + 0x00 | (value << 4) + }; // All SPI chip-select HIGH - digitalWrite( DAC0_SYNC , HIGH ); - #if EXTRUDERS > 1 - digitalWrite( DAC1_SYNC , HIGH ); - #endif - digitalWrite( SPI_EEPROM1_CS , HIGH ); - digitalWrite( SPI_EEPROM2_CS , HIGH ); - digitalWrite( SPI_FLASH_CS , HIGH ); - digitalWrite( SS_PIN , HIGH ); + cshigh(); - if(channel > 3) { // DAC Piggy E1,E2,E3 - - digitalWrite(DAC1_SYNC , LOW); + if (channel > 3) { // DAC Piggy E1,E2,E3 + WRITE(DAC1_SYNC, LOW); delayMicroseconds(2U); - digitalWrite(DAC1_SYNC , HIGH); + WRITE(DAC1_SYNC, HIGH); delayMicroseconds(2U); - digitalWrite(DAC1_SYNC , LOW); + WRITE(DAC1_SYNC, LOW); } - - else { // DAC onboard X,Y,Z,E0 - - digitalWrite(DAC0_SYNC , LOW); + else { // DAC onboard X,Y,Z,E0 + WRITE(DAC0_SYNC, LOW); delayMicroseconds(2U); - digitalWrite(DAC0_SYNC , HIGH); + WRITE(DAC0_SYNC, HIGH); delayMicroseconds(2U); - digitalWrite(DAC0_SYNC , LOW); + WRITE(DAC0_SYNC, LOW); } delayMicroseconds(2U); - spiSend(SPI_CHAN_DAC,externalDac_buf , 2); + spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf)); +} - return; +void dac084s085::cshigh() { + WRITE(DAC0_SYNC, HIGH); + #if EXTRUDERS > 1 + WRITE(DAC1_SYNC, HIGH); + #endif + WRITE(SPI_EEPROM1_CS, HIGH); + WRITE(SPI_EEPROM2_CS, HIGH); + WRITE(SPI_FLASH_CS, HIGH); + WRITE(SS_PIN, HIGH); } #endif // MB(ALLIGATOR) diff --git a/Marlin/src/feature/dac/dac_dac084s085.h b/Marlin/src/feature/dac/dac_dac084s085.h index f7d823a3b7..af98c16776 100644 --- a/Marlin/src/feature/dac/dac_dac084s085.h +++ b/Marlin/src/feature/dac/dac_dac084s085.h @@ -5,7 +5,9 @@ class dac084s085 { public: dac084s085(); static void begin(void); - static void setValue(uint8_t channel, uint8_t value); + static void setValue(const uint8_t channel, const uint8_t value); + private: + static void cshigh(); }; #endif // DAC084S085_H