HAL SPI pin init cleanup

This commit is contained in:
Scott Lahteine 2019-06-14 20:16:22 -05:00
parent cfb44e4a26
commit 26de051e92
6 changed files with 71 additions and 73 deletions

View file

@ -21,12 +21,12 @@
*/ */
/** /**
* Originally from Arduino Sd2Card Library * Adapted from Arduino Sd2Card Library
* Copyright (C) 2009 by William Greiman * Copyright (C) 2009 by William Greiman
*/ */
/** /**
* Description: HAL for AVR - SPI functions * HAL for AVR - SPI functions
*/ */
#ifdef __AVR__ #ifdef __AVR__
@ -37,35 +37,24 @@
#include "../../inc/MarlinConfig.h" #include "../../inc/MarlinConfig.h"
// -------------------------------------------------------------------------- void spiBegin(void) {
// Public Variables OUT_WRITE(SS_PIN, HIGH);
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// Public functions
// --------------------------------------------------------------------------
void spiBegin (void) {
SET_OUTPUT(SS_PIN);
WRITE(SS_PIN, HIGH);
SET_OUTPUT(SCK_PIN); SET_OUTPUT(SCK_PIN);
SET_INPUT(MISO_PIN); SET_INPUT(MISO_PIN);
SET_OUTPUT(MOSI_PIN); SET_OUTPUT(MOSI_PIN);
#if DISABLED(SOFTWARE_SPI) #if DISABLED(SOFTWARE_SPI)
// SS must be in output mode even it is not chip select // 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 // set SS high - may be chip select for another SPI device
#if SET_SPI_SS_HIGH //#if SET_SPI_SS_HIGH
WRITE(SS_PIN, HIGH); //WRITE(SS_PIN, HIGH);
#endif // SET_SPI_SS_HIGH //#endif
// set a default rate // set a default rate
spiInit(1); spiInit(1);
#endif // SOFTWARE_SPI #endif
} }
#if DISABLED(SOFTWARE_SPI, FORCE_SOFT_SPI) #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") #define nop asm volatile ("\tnop\n")
/** Set SPI rate */ // Set SPI rate
void spiInit(uint8_t spiRate) { void spiInit(uint8_t spiRate) {
UNUSED(spiRate); // nothing to do 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) { void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
UNUSED(spiBeginTransaction); // nothing to do UNUSED(spiBeginTransaction); // nothing to do
} }
/** Soft SPI receive byte */ // Soft SPI receive byte
uint8_t spiRec() { uint8_t spiRec() {
uint8_t data = 0; uint8_t data = 0;
// no interrupts during byte receive - about 8µs // no interrupts during byte receive - about 8µs
@ -230,13 +223,13 @@ void spiBegin (void) {
return data; return data;
} }
/** Soft SPI read data */ // Soft SPI read data
void spiRead(uint8_t* buf, uint16_t nbyte) { void spiRead(uint8_t* buf, uint16_t nbyte) {
for (uint16_t i = 0; i < nbyte; i++) for (uint16_t i = 0; i < nbyte; i++)
buf[i] = spiRec(); buf[i] = spiRec();
} }
/** Soft SPI send byte */ // Soft SPI send byte
void spiSend(uint8_t data) { void spiSend(uint8_t data) {
// no interrupts during byte send - about 8µs // no interrupts during byte send - about 8µs
cli(); cli();
@ -257,13 +250,13 @@ void spiBegin (void) {
sei(); sei();
} }
/** Soft SPI send block */ // Soft SPI send block
void spiSendBlock(uint8_t token, const uint8_t* buf) { void spiSendBlock(uint8_t token, const uint8_t* buf) {
spiSend(token); spiSend(token);
for (uint16_t i = 0; i < 512; i++) for (uint16_t i = 0; i < 512; i++)
spiSend(buf[i]); spiSend(buf[i]);
} }
#endif // SOFTWARE_SPI, FORCE_SOFT_SPI #endif // SOFTWARE_SPI || FORCE_SOFT_SPI
#endif // __AVR__ #endif // __AVR__

View file

@ -38,14 +38,12 @@ void spiBegin(void) {
#if !PIN_EXISTS(SS) #if !PIN_EXISTS(SS)
#error "SS_PIN not defined!" #error "SS_PIN not defined!"
#endif #endif
SET_OUTPUT(SS_PIN); OUT_WRITE(SS_PIN, HIGH);
WRITE(SS_PIN, HIGH);
SET_OUTPUT(SCK_PIN); SET_OUTPUT(SCK_PIN);
SET_INPUT(MISO_PIN); SET_INPUT(MISO_PIN);
SET_OUTPUT(MOSI_PIN); SET_OUTPUT(MOSI_PIN);
//#if DISABLED(SOFTWARE_SPI) #if 0 && DISABLED(SOFTWARE_SPI)
#if 0
// set SS high - may be chip select for another SPI device // set SS high - may be chip select for another SPI device
#if SET_SPI_SS_HIGH #if SET_SPI_SS_HIGH
WRITE(SS_PIN, HIGH); WRITE(SS_PIN, HIGH);

View file

@ -23,7 +23,7 @@
#include <Servo.h> #include <Servo.h>
// Inherit and expand on the official library // Inherit and expand on core Servo library
class libServo : public Servo { class libServo : public Servo {
public: public:
int8_t attach(const int pin); int8_t attach(const int pin);
@ -32,5 +32,5 @@ class libServo : public Servo {
private: private:
uint16_t min_ticks; uint16_t min_ticks;
uint16_t max_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
}; };

View file

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#if defined(__MK64FX512__) || defined(__MK66FX1M0__) #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
#include "HAL.h" #include "HAL.h"
@ -8,32 +29,27 @@
static SPISettings spiConfig; static SPISettings spiConfig;
// Standard SPI functions
/** Initialize SPI bus */
void spiBegin(void) { void spiBegin(void) {
#if !PIN_EXISTS(SS) #if !PIN_EXISTS(SS)
#error SS_PIN not defined! #error SS_PIN not defined!
#endif #endif
SET_OUTPUT(SS_PIN); OUT_WRITE(SS_PIN, HIGH);
WRITE(SS_PIN, HIGH);
SET_OUTPUT(SCK_PIN); SET_OUTPUT(SCK_PIN);
SET_INPUT(MISO_PIN); SET_INPUT(MISO_PIN);
SET_OUTPUT(MOSI_PIN); SET_OUTPUT(MOSI_PIN);
//#if DISABLED(SOFTWARE_SPI) #if 0 && DISABLED(SOFTWARE_SPI)
#if 0
// set SS high - may be chip select for another SPI device // set SS high - may be chip select for another SPI device
#if SET_SPI_SS_HIGH #if SET_SPI_SS_HIGH
WRITE(SS_PIN, HIGH); WRITE(SS_PIN, HIGH);
#endif // SET_SPI_SS_HIGH #endif
// set a default rate // set a default rate
spiInit(SPI_HALF_SPEED); // 1 spiInit(SPI_HALF_SPEED); // 1
#endif // SOFTWARE_SPI #endif
} }
/** Configure SPI for specified SPI speed */
void spiInit(uint8_t spiRate) { void spiInit(uint8_t spiRate) {
// Use datarates Marlin uses // Use Marlin data-rates
uint32_t clock; uint32_t clock;
switch (spiRate) { switch (spiRate) {
case SPI_FULL_SPEED: clock = 10000000; break; case SPI_FULL_SPEED: clock = 10000000; break;
@ -49,44 +65,39 @@ void spiInit(uint8_t spiRate) {
SPI.begin(); SPI.begin();
} }
//------------------------------------------------------------------------------
/** SPI receive a byte */
uint8_t spiRec(void) { uint8_t spiRec(void) {
SPI.beginTransaction(spiConfig); SPI.beginTransaction(spiConfig);
uint8_t returnByte = SPI.transfer(0xFF); uint8_t returnByte = SPI.transfer(0xFF);
SPI.endTransaction(); SPI.endTransaction();
return returnByte; return returnByte;
// SPDR = 0xFF; //SPDR = 0xFF;
// while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
// return SPDR; //return SPDR;
} }
//------------------------------------------------------------------------------
/** SPI read data */
void spiRead(uint8_t* buf, uint16_t nbyte) { void spiRead(uint8_t* buf, uint16_t nbyte) {
SPI.beginTransaction(spiConfig); SPI.beginTransaction(spiConfig);
SPI.transfer(buf, nbyte); SPI.transfer(buf, nbyte);
SPI.endTransaction(); SPI.endTransaction();
//if (nbyte-- == 0) return; //if (nbyte-- == 0) return;
// SPDR = 0xFF; // SPDR = 0xFF;
//for (uint16_t i = 0; i < nbyte; i++) { //for (uint16_t i = 0; i < nbyte; i++) {
// while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
// buf[i] = SPDR; // buf[i] = SPDR;
// SPDR = 0xFF; // SPDR = 0xFF;
//} //}
//while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
//buf[nbyte] = SPDR; //buf[nbyte] = SPDR;
} }
//------------------------------------------------------------------------------
/** SPI send a byte */
void spiSend(uint8_t b) { void spiSend(uint8_t b) {
SPI.beginTransaction(spiConfig); SPI.beginTransaction(spiConfig);
SPI.transfer(b); SPI.transfer(b);
SPI.endTransaction(); SPI.endTransaction();
// SPDR = b; //SPDR = b;
// while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
} }
//------------------------------------------------------------------------------
/** SPI send block */
void spiSendBlock(uint8_t token, const uint8_t* buf) { void spiSendBlock(uint8_t token, const uint8_t* buf) {
SPI.beginTransaction(spiConfig); SPI.beginTransaction(spiConfig);
SPDR = token; SPDR = token;
@ -100,11 +111,9 @@ void spiSendBlock(uint8_t token, const uint8_t* buf) {
SPI.endTransaction(); 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) { void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
spiConfig = SPISettings(spiClock, bitOrder, dataMode); spiConfig = SPISettings(spiClock, bitOrder, dataMode);
SPI.beginTransaction(spiConfig); SPI.beginTransaction(spiConfig);
} }

View file

@ -20,7 +20,6 @@
* *
*/ */
/** /**
* Teensy3.5 __MK64FX512__ * Teensy3.5 __MK64FX512__
* Teensy3.6 __MK66FX1M0__ * Teensy3.6 __MK66FX1M0__

View file

@ -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 delay(7); // wait for page write to complete
} }
#endif // SPI_EEPROM
#endif // ENABLED(SPI_EEPROM)