Browse Source

New SPI (do not use Arduino SPI class)
saved ~300bytes flash, 4bytes ram

Robert Pelnar 6 years ago
parent
commit
186e630299
6 changed files with 134 additions and 1 deletions
  1. 3 0
      Firmware/Configuration_prusa.h
  2. 8 0
      Firmware/Marlin_main.cpp
  3. 6 0
      Firmware/config.h
  4. 8 0
      Firmware/spi.c
  5. 41 0
      Firmware/spi.h
  6. 68 1
      Firmware/tmc2130.cpp

+ 3 - 0
Firmware/Configuration_prusa.h

@@ -107,6 +107,9 @@ const bool Z_MIN_ENDSTOP_INVERTING = false; // set to true to invert the logic o
 // New XYZ calibration
 #define NEW_XYZCAL
 
+// Do not use Arduino SPI 
+#define NEW_SPI
+
 // Watchdog support
 #define WATCHDOG
 

+ 8 - 0
Firmware/Marlin_main.cpp

@@ -65,6 +65,10 @@
 #include "swspi.h"
 #endif //SWSPI
 
+#ifdef NEW_SPI
+#include "spi.h"
+#endif //NEW_SPI
+
 #ifdef SWI2C
 #include "swi2c.h"
 #endif //SWI2C
@@ -1106,6 +1110,10 @@ void setup()
 
 #endif //TMC2130
 
+#ifdef NEW_SPI
+	spi_init();
+#endif //NEW_SPI
+
 	st_init();    // Initialize stepper, this enables interrupts!
 
 #ifdef TMC2130

+ 6 - 0
Firmware/config.h

@@ -11,5 +11,11 @@
 //SM4 configuration
 #define SM4_DEFDELAY      500       //default step delay [us]
 
+//TMC2130 - Trinamic stepper driver
+//pinout - hardcoded
+//spi:
+#define TMC2130_SPI_RATE       0 // fosc/4 = 4MHz
+#define TMC2130_SPCR           SPI_SPCR(TMC2130_SPI_RATE, 1, 1, 1, 0)
+#define TMC2130_SPSR           SPI_SPSR(TMC2130_SPI_RATE)
 
 #endif //_CONFIG_H

+ 8 - 0
Firmware/spi.c

@@ -0,0 +1,8 @@
+//spi.c - hardware SPI
+//#ifdef __SPI
+
+#include "spi.h"
+#include <avr/io.h>
+
+
+//#endif //__SPI

+ 41 - 0
Firmware/spi.h

@@ -0,0 +1,41 @@
+//spi.h - hardware SPI
+#ifndef SPI_H
+#define SPI_H
+
+#include <inttypes.h>
+#include <avr/io.h>
+#include "config.h"
+
+
+#define SPI_SPCR(rat, pha, pol, mst, dor) ((rat & 3) | (pha?(1<<CPHA):0) | (pol?(1<<CPOL):0) | (mst?(1<<MSTR):0) | (dor?(1<<DORD):0) | (1<<SPE))
+#define SPI_SPSR(rat) ((rat & 4)?(1<<SPI2X):0)
+
+#define DD_SS   0
+#define DD_SCK  1
+#define DD_MOSI 2
+#define DD_MISO 3
+
+inline void spi_init()
+{
+	DDRB &= ~((1 << DD_SCK) | (1 << DD_MOSI) | (1 << DD_MISO));
+	DDRB |= (1 << DD_SS) | (1 << DD_SCK) | (1 << DD_MOSI);
+	PORTB &= ~((1 << DD_SCK) | (1 << DD_MOSI) | (1 << DD_MISO));
+	PORTB |= (1 << DD_SS);
+	SPCR = SPI_SPCR(0, 0, 0, 1, 0); //SPE=1, MSTR=1 (0x50)
+	SPSR = 0x00;
+}
+
+inline void spi_setup(uint8_t spcr, uint8_t spsr)
+{
+	SPCR = spcr;
+	SPSR = spsr;
+}
+
+inline uint8_t spi_txrx(uint8_t tx)
+{
+	SPDR = tx;
+	while (!(SPSR & (1 << SPIF)));
+	return SPDR;
+}
+
+#endif //SPI_H

+ 68 - 1
Firmware/tmc2130.cpp

@@ -3,9 +3,14 @@
 #ifdef TMC2130
 
 #include "tmc2130.h"
-#include <SPI.h>
 #include "LiquidCrystal.h"
 #include "ultralcd.h"
+#ifndef NEW_SPI
+#include <SPI.h>
+#else //NEW_SPI
+#include "spi.h"
+#endif //NEW_SPI
+
 
 extern LiquidCrystal lcd;
 
@@ -143,7 +148,9 @@ void tmc2130_init()
 	SET_INPUT(Y_TMC2130_DIAG);
 	SET_INPUT(Z_TMC2130_DIAG);
 	SET_INPUT(E0_TMC2130_DIAG);
+#ifndef NEW_SPI
 	SPI.begin();
+#endif //NEW_SPI
 	for (int axis = 0; axis < 2; axis++) // X Y axes
 	{
 		tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
@@ -594,11 +601,15 @@ inline void tmc2130_cs_high(uint8_t axis)
 	}
 }
 
+#ifndef NEW_SPI
 
 uint8_t tmc2130_tx(uint8_t axis, uint8_t addr, uint32_t wval)
 {
 	//datagram1 - request
+	printf_P(PSTR("tmc2130_tx %d 0x%02hhx, 0x%08lx\n"), axis, addr, wval);
 	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
+	printf_P(PSTR(" SPCR = 0x%02hhx\n"), SPCR);
+	printf_P(PSTR(" SPSR = 0x%02hhx\n"), SPSR);
 	tmc2130_cs_low(axis);
 	SPI.transfer(addr); // address
 	SPI.transfer((wval >> 24) & 0xff); // MSB
@@ -636,6 +647,62 @@ uint8_t tmc2130_rx(uint8_t axis, uint8_t addr, uint32_t* rval)
 	return stat;
 }
 
+#else //NEW_SPI
+
+//Arduino SPI
+//#define TMC2130_SPI_ENTER()    SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3))
+//#define TMC2130_SPI_TXRX       SPI.transfer
+//#define TMC2130_SPI_LEAVE      SPI.endTransaction
+
+//spi
+#define TMC2130_SPI_ENTER()    spi_setup(TMC2130_SPCR, TMC2130_SPSR)
+#define TMC2130_SPI_TXRX       spi_txrx
+#define TMC2130_SPI_LEAVE()
+
+uint8_t tmc2130_tx(uint8_t axis, uint8_t addr, uint32_t wval)
+{
+	//datagram1 - request
+	TMC2130_SPI_ENTER();
+	tmc2130_cs_low(axis);
+	TMC2130_SPI_TXRX(addr); // address
+	TMC2130_SPI_TXRX((wval >> 24) & 0xff); // MSB
+	TMC2130_SPI_TXRX((wval >> 16) & 0xff);
+	TMC2130_SPI_TXRX((wval >> 8) & 0xff);
+	TMC2130_SPI_TXRX(wval & 0xff); // LSB
+	tmc2130_cs_high(axis);
+	TMC2130_SPI_LEAVE();
+}
+
+uint8_t tmc2130_rx(uint8_t axis, uint8_t addr, uint32_t* rval)
+{
+	//datagram1 - request
+	TMC2130_SPI_ENTER();
+	tmc2130_cs_low(axis);
+	TMC2130_SPI_TXRX(addr); // address
+	TMC2130_SPI_TXRX(0); // MSB
+	TMC2130_SPI_TXRX(0);
+	TMC2130_SPI_TXRX(0);
+	TMC2130_SPI_TXRX(0); // LSB
+	tmc2130_cs_high(axis);
+	TMC2130_SPI_LEAVE();
+	//datagram2 - response
+	TMC2130_SPI_ENTER();
+	tmc2130_cs_low(axis);
+	uint8_t stat = TMC2130_SPI_TXRX(0); // status
+	uint32_t val32 = 0;
+	val32 = TMC2130_SPI_TXRX(0); // MSB
+	val32 = (val32 << 8) | TMC2130_SPI_TXRX(0);
+	val32 = (val32 << 8) | TMC2130_SPI_TXRX(0);
+	val32 = (val32 << 8) | TMC2130_SPI_TXRX(0); // LSB
+	tmc2130_cs_high(axis);
+	TMC2130_SPI_LEAVE();
+	if (rval != 0) *rval = val32;
+	return stat;
+}
+
+#endif //NEW_SPI
+
+
 void tmc2130_eeprom_load_config()
 {
 }