Switch to TMC2130 compatible hardware SPI & misc (#10299)
This commit is contained in:
parent
1c36606886
commit
902c885782
|
@ -1,23 +1,21 @@
|
||||||
/* **************************************************************************
|
/**
|
||||||
|
* Marlin 3D Printer Firmware
|
||||||
Marlin 3D Printer Firmware
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||||
Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
||||||
Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
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
|
||||||
it under the terms of the GNU General Public License as published by
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
* (at your option) any later version.
|
||||||
(at your option) any later version.
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
This program is distributed in the hope that it will be useful,
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* GNU General Public License for more details.
|
||||||
GNU General Public License for more details.
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
You should have received a copy of the GNU General Public License
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
*/
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description: HAL for Arduino Due and compatible (SAM3X8E)
|
* Description: HAL for Arduino Due and compatible (SAM3X8E)
|
||||||
|
@ -77,6 +75,9 @@ uint16_t HAL_adc_result;
|
||||||
// HAL initialization task
|
// HAL initialization task
|
||||||
void HAL_init(void) {
|
void HAL_init(void) {
|
||||||
// Initialize the USB stack
|
// Initialize the USB stack
|
||||||
|
#if ENABLED(SDSUPPORT)
|
||||||
|
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
|
||||||
|
#endif
|
||||||
usb_task_init();
|
usb_task_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,10 +87,10 @@ void HAL_idletask(void) {
|
||||||
usb_task_idle();
|
usb_task_idle();
|
||||||
}
|
}
|
||||||
|
|
||||||
// disable interrupts
|
// Disable interrupts
|
||||||
void cli(void) { noInterrupts(); }
|
void cli(void) { noInterrupts(); }
|
||||||
|
|
||||||
// enable interrupts
|
// Enable interrupts
|
||||||
void sei(void) { interrupts(); }
|
void sei(void) { interrupts(); }
|
||||||
|
|
||||||
void HAL_clear_reset_source(void) { }
|
void HAL_clear_reset_source(void) { }
|
||||||
|
@ -106,7 +107,7 @@ uint8_t HAL_get_reset_source(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _delay_ms(const int delay_ms) {
|
void _delay_ms(const int delay_ms) {
|
||||||
// todo: port for Due?
|
// Todo: port for Due?
|
||||||
delay(delay_ms);
|
delay(delay_ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,7 +115,7 @@ extern "C" {
|
||||||
extern unsigned int _ebss; // end of bss section
|
extern unsigned int _ebss; // end of bss section
|
||||||
}
|
}
|
||||||
|
|
||||||
// return free memory between end of heap (or end bss) and whatever is current
|
// Return free memory between end of heap (or end bss) and whatever is current
|
||||||
int freeMemory() {
|
int freeMemory() {
|
||||||
int free_memory, heap_end = (int)_sbrk(0);
|
int free_memory, heap_end = (int)_sbrk(0);
|
||||||
return (int)&free_memory - (heap_end ? heap_end : (int)&_ebss);
|
return (int)&free_memory - (heap_end ? heap_end : (int)&_ebss);
|
||||||
|
|
|
@ -486,7 +486,7 @@
|
||||||
|
|
||||||
static void spiRxBlockX(uint8_t* buf, uint32_t todo) {
|
static void spiRxBlockX(uint8_t* buf, uint32_t todo) {
|
||||||
do {
|
do {
|
||||||
*buf++ = spiTransferRx(0xff);
|
*buf++ = spiTransferRx(0xFF);
|
||||||
} while (--todo);
|
} while (--todo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -629,7 +629,7 @@
|
||||||
bool spiInitMaded = false;
|
bool spiInitMaded = false;
|
||||||
|
|
||||||
void spiBegin() {
|
void spiBegin() {
|
||||||
if(spiInitMaded == false) {
|
if (spiInitMaded == false) {
|
||||||
// Configure SPI pins
|
// Configure SPI pins
|
||||||
PIO_Configure(
|
PIO_Configure(
|
||||||
g_APinDescription[SCK_PIN].pPort,
|
g_APinDescription[SCK_PIN].pPort,
|
||||||
|
@ -681,8 +681,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
void spiInit(uint8_t spiRate) {
|
void spiInit(uint8_t spiRate) {
|
||||||
if(spiInitMaded == false) {
|
if (spiInitMaded == false) {
|
||||||
if(spiRate > 6) spiRate = 1;
|
if (spiRate > 6) spiRate = 1;
|
||||||
|
|
||||||
#if MB(ALLIGATOR)
|
#if MB(ALLIGATOR)
|
||||||
// Set SPI mode 1, clock, select not active after transfer, with delay between transfers
|
// Set SPI mode 1, clock, select not active after transfer, with delay between transfers
|
||||||
|
@ -789,7 +789,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read from SPI into buffer
|
// Read from SPI into buffer
|
||||||
void spiRead(uint8_t*buf, uint16_t nbyte) {
|
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
||||||
if (nbyte-- == 0) return;
|
if (nbyte-- == 0) return;
|
||||||
|
|
||||||
for (int i = 0; i < nbyte; i++) {
|
for (int i = 0; i < nbyte; i++) {
|
||||||
|
@ -830,38 +830,37 @@
|
||||||
#define SPI_MODE_2_DUE_HW 0
|
#define SPI_MODE_2_DUE_HW 0
|
||||||
#define SPI_MODE_3_DUE_HW 1
|
#define SPI_MODE_3_DUE_HW 1
|
||||||
|
|
||||||
void spiInit(uint8_t spiRate = 6 ) { // default to slowest rate if not specified)
|
void spiInit(uint8_t spiRate=6) { // Default to slowest rate if not specified)
|
||||||
// 8.4 MHz, 4 MHz, 2 MHz, 1 MHz, 0.5 MHz, 0.329 MHz, 0.329 MHz
|
// 8.4 MHz, 4 MHz, 2 MHz, 1 MHz, 0.5 MHz, 0.329 MHz, 0.329 MHz
|
||||||
int spiDueDividors[] = { 10, 21, 42, 84, 168, 255, 255 };
|
int spiDueDividors[] = { 10, 21, 42, 84, 168, 255, 255 };
|
||||||
if(spiRate > 6) spiRate = 1;
|
if (spiRate > 6) spiRate = 1;
|
||||||
|
|
||||||
/* enable PIOA and SPI0 */
|
// Enable PIOA and SPI0
|
||||||
REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
|
REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
|
||||||
|
|
||||||
/* disable PIO on A26 and A27 */
|
// Disable PIO on A26 and A27
|
||||||
REG_PIOA_PDR = 0x0c000000;
|
REG_PIOA_PDR = 0x0c000000;
|
||||||
OUT_WRITE(SDSS, 1);
|
OUT_WRITE(SDSS, 1);
|
||||||
|
|
||||||
/* reset SPI0 (from sam lib) */
|
// Reset SPI0 (from sam lib)
|
||||||
SPI0->SPI_CR = SPI_CR_SPIDIS;
|
SPI0->SPI_CR = SPI_CR_SPIDIS;
|
||||||
SPI0->SPI_CR = SPI_CR_SWRST;
|
SPI0->SPI_CR = SPI_CR_SWRST;
|
||||||
SPI0->SPI_CR = SPI_CR_SWRST;
|
SPI0->SPI_CR = SPI_CR_SWRST;
|
||||||
SPI0->SPI_CR = SPI_CR_SPIEN;
|
SPI0->SPI_CR = SPI_CR_SPIEN;
|
||||||
|
|
||||||
|
// TMC2103 compatible setup
|
||||||
/* master mode, no fault detection, chip select 0 */
|
// Master mode, no fault detection, PCS bits in data written to TDR select CSR register
|
||||||
SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PCSDEC | SPI_MR_MODFDIS;
|
SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS;
|
||||||
|
// SPI mode 0, 8 Bit data transfer, baud rate
|
||||||
/* SPI mode 0, 8 Bit data transfer, baud rate */
|
SPI0->SPI_CSR[3] = SPI_CSR_SCBR(spiDueDividors[spiRate]) | SPI_CSR_CSAAT | SPI_MODE_0_DUE_HW; // use same CSR as TMC2130
|
||||||
SPI0->SPI_CSR[0] = SPI_CSR_SCBR(spiDueDividors[spiRate]) | SPI_MODE_0_DUE_HW;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t spiTransfer(uint8_t data) {
|
static uint8_t spiTransfer(uint8_t data) {
|
||||||
|
|
||||||
/* wait until tx register is empty */
|
// Wait until tx register is empty
|
||||||
while( (SPI0->SPI_SR & SPI_SR_TDRE) == 0 );
|
while( (SPI0->SPI_SR & SPI_SR_TDRE) == 0 );
|
||||||
/* send data */
|
// Send data
|
||||||
SPI0->SPI_TDR = (uint32_t)data; // | SPI_PCS(0xF);
|
SPI0->SPI_TDR = (uint32_t)data | 0x00070000UL; // Add TMC2130 PCS bits to every byte
|
||||||
|
|
||||||
// wait for transmit register empty
|
// wait for transmit register empty
|
||||||
while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
|
while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
|
||||||
|
@ -877,15 +876,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t spiRec() {
|
uint8_t spiRec() {
|
||||||
uint8_t data = spiTransfer(0xff);
|
uint8_t data = spiTransfer(0xFF);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void spiRead(uint8_t*buf, uint16_t nbyte) {
|
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
||||||
if (nbyte == 0) return;
|
if (nbyte == 0) return;
|
||||||
for (int i = 0; i < nbyte; i++) {
|
for (int i = 0; i < nbyte; i++)
|
||||||
buf[i] = spiTransfer(0xff);
|
buf[i] = spiTransfer(0xFF);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void spiSend(uint8_t data) {
|
void spiSend(uint8_t data) {
|
||||||
|
|
|
@ -27,3 +27,11 @@
|
||||||
/**
|
/**
|
||||||
* Require gcc 4.7 or newer (first included with Arduino 1.6.8) for C++11 features.
|
* Require gcc 4.7 or newer (first included with Arduino 1.6.8) for C++11 features.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#if ENABLED(SDSUPPORT) && ENABLED(HAVE_TMC2130)
|
||||||
|
#if ENABLED(DUE_SOFTWARE_SPI) && !ENABLED(TMC_USE_SW_SPI)
|
||||||
|
#error "DUE software SPI is incompatible with TMC2130 hardware SPI. Enable TMC_USE_SW_SPI to fix."
|
||||||
|
#elif !ENABLED(DUE_SOFTWARE_SPI) && ENABLED(TMC_USE_SW_SPI)
|
||||||
|
#error "DUE hardware SPI is incompatible with TMC2130 software SPI. Disable TMC_USE_SW_SPI to fix."
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
|
@ -892,14 +892,15 @@ void setup() {
|
||||||
* - Call LCD update
|
* - Call LCD update
|
||||||
*/
|
*/
|
||||||
void loop() {
|
void loop() {
|
||||||
if (commands_in_queue < BUFSIZE) get_available_commands();
|
|
||||||
|
|
||||||
#if ENABLED(SDSUPPORT)
|
#if ENABLED(SDSUPPORT)
|
||||||
card.checkautostart(false);
|
card.checkautostart(false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
advance_command_queue();
|
for (;;) {
|
||||||
|
if (commands_in_queue < BUFSIZE) get_available_commands();
|
||||||
endstops.report_state();
|
advance_command_queue();
|
||||||
idle();
|
endstops.report_state();
|
||||||
|
idle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -249,15 +249,16 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
|
||||||
watchdog_reset();
|
watchdog_reset();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// set pin modes
|
// Set pin modes
|
||||||
pinMode(chipSelectPin_, OUTPUT); // Solution for #8746 by @benlye
|
digitalWrite(chipSelectPin_, HIGH); // For some CPUs pinMode can write the wrong data so init desired data value first
|
||||||
|
pinMode(chipSelectPin_, OUTPUT); // Solution for #8746 by @benlye
|
||||||
spiBegin();
|
spiBegin();
|
||||||
|
|
||||||
// set SCK rate for initialization commands
|
// Set SCK rate for initialization commands
|
||||||
spiRate_ = SPI_SD_INIT_RATE;
|
spiRate_ = SPI_SD_INIT_RATE;
|
||||||
spiInit(spiRate_);
|
spiInit(spiRate_);
|
||||||
|
|
||||||
// must supply min of 74 clock cycles with CS high.
|
// Must supply min of 74 clock cycles with CS high.
|
||||||
for (uint8_t i = 0; i < 10; i++) spiSend(0xFF);
|
for (uint8_t i = 0; i < 10; i++) spiSend(0xFF);
|
||||||
|
|
||||||
// Initialization can cause the watchdog to timeout, so reinit it here
|
// Initialization can cause the watchdog to timeout, so reinit it here
|
||||||
|
@ -265,7 +266,7 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
|
||||||
watchdog_reset();
|
watchdog_reset();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// command to go idle in SPI mode
|
// Command to go idle in SPI mode
|
||||||
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
|
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
|
||||||
if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
|
if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
|
||||||
error(SD_CARD_ERROR_CMD0);
|
error(SD_CARD_ERROR_CMD0);
|
||||||
|
|
Loading…
Reference in a new issue