2017-06-17 21:19:42 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Software SPI functions originally from Arduino Sd2Card Library
|
|
|
|
* Copyright (C) 2009 by William Greiman
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* For TARGET_LPC1768
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef TARGET_LPC1768
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Includes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "../../../MarlinConfig.h"
|
2017-07-25 16:27:53 +00:00
|
|
|
#include "../spi_api.h"
|
2017-06-17 21:19:42 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if ENABLED(SOFTWARE_SPI)
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// software SPI
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// bitbanging transfer
|
|
|
|
// run at ~100KHz (necessary for init)
|
|
|
|
static uint8_t spiTransfer(uint8_t b) { // using Mode 0
|
|
|
|
for (int bits = 0; bits < 8; bits++) {
|
|
|
|
if (b & 0x80) {
|
|
|
|
WRITE(MOSI_PIN, HIGH);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WRITE(MOSI_PIN, LOW);
|
|
|
|
}
|
|
|
|
b <<= 1;
|
|
|
|
|
|
|
|
WRITE(SCK_PIN, HIGH);
|
|
|
|
delayMicroseconds(3U);
|
|
|
|
|
|
|
|
if (READ(MISO_PIN)) {
|
|
|
|
b |= 1;
|
|
|
|
}
|
|
|
|
WRITE(SCK_PIN, LOW);
|
|
|
|
delayMicroseconds(3U);
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spiBegin() {
|
|
|
|
SET_OUTPUT(SS_PIN);
|
|
|
|
WRITE(SS_PIN, HIGH);
|
|
|
|
SET_OUTPUT(SCK_PIN);
|
|
|
|
SET_INPUT(MISO_PIN);
|
|
|
|
SET_OUTPUT(MOSI_PIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void spiInit(uint8_t spiRate) {
|
|
|
|
UNUSED(spiRate);
|
|
|
|
WRITE(SS_PIN, HIGH);
|
|
|
|
WRITE(MOSI_PIN, HIGH);
|
|
|
|
WRITE(SCK_PIN, LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t spiRec() {
|
|
|
|
WRITE(SS_PIN, LOW);
|
|
|
|
uint8_t b = spiTransfer(0xff);
|
|
|
|
WRITE(SS_PIN, HIGH);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spiRead(uint8_t*buf, uint16_t nbyte) {
|
|
|
|
if (nbyte == 0) return;
|
|
|
|
WRITE(SS_PIN, LOW);
|
|
|
|
for (int i = 0; i < nbyte; i++) {
|
|
|
|
buf[i] = spiTransfer(0xff);
|
|
|
|
}
|
|
|
|
WRITE(SS_PIN, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void spiSend(uint8_t b) {
|
|
|
|
WRITE(SS_PIN, LOW);
|
|
|
|
uint8_t response = spiTransfer(b);
|
|
|
|
UNUSED(response);
|
|
|
|
WRITE(SS_PIN, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void spiSend(const uint8_t* buf, size_t n) {
|
|
|
|
uint8_t response;
|
|
|
|
if (n == 0) return;
|
|
|
|
WRITE(SS_PIN, LOW);
|
|
|
|
for (uint16_t i = 0; i < n; i++) {
|
|
|
|
response = spiTransfer(buf[i]);
|
|
|
|
}
|
|
|
|
UNUSED(response);
|
|
|
|
WRITE(SS_PIN, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
|
|
|
uint8_t response;
|
|
|
|
|
|
|
|
WRITE(SS_PIN, LOW);
|
|
|
|
response = spiTransfer(token);
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < 512; i++) {
|
|
|
|
response = spiTransfer(buf[i]);
|
|
|
|
}
|
|
|
|
UNUSED(response);
|
|
|
|
WRITE(SS_PIN, HIGH);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void spiBegin() {
|
2017-07-25 16:27:53 +00:00
|
|
|
HAL::SPI::initialise(SD_SPI_CHANNEL);
|
2017-06-17 21:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void spiInit(uint8_t spiRate) {
|
2017-07-25 16:27:53 +00:00
|
|
|
uint32_t freq = 8000000 / (1u << spiRate);
|
|
|
|
HAL::SPI::set_frequency(SD_SPI_CHANNEL, freq);
|
2017-06-17 21:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void spiSend(byte b) {
|
2017-07-25 16:27:53 +00:00
|
|
|
HAL::SPI::write(SD_SPI_CHANNEL, b);
|
2017-06-17 21:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read single byte from SPI
|
|
|
|
uint8_t spiRec() {
|
2017-07-25 16:27:53 +00:00
|
|
|
return HAL::SPI::read(SD_SPI_CHANNEL);
|
2017-06-17 21:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read from SPI into buffer
|
|
|
|
void spiRead(uint8_t*buf, uint16_t nbyte) {
|
2017-07-25 16:27:53 +00:00
|
|
|
HAL::SPI::read(SD_SPI_CHANNEL, buf, nbyte);
|
2017-06-17 21:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write from buffer to SPI
|
|
|
|
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
2017-07-25 16:27:53 +00:00
|
|
|
HAL::SPI::write(SD_SPI_CHANNEL, token);
|
|
|
|
HAL::SPI::write(SD_SPI_CHANNEL, buf, 512);
|
2017-06-17 21:19:42 +00:00
|
|
|
}
|
|
|
|
#endif // ENABLED(SOFTWARE_SPI)
|
|
|
|
|
|
|
|
#endif // TARGET_LPC1768
|