TMCStepper
SW_SPI.cpp
Go to the documentation of this file.
1 #include "SW_SPI.h"
2 
3 SW_SPIClass::SW_SPIClass(uint16_t mosi, uint16_t miso, uint16_t sck) :
4  mosi_pin(mosi),
5  miso_pin(miso),
6  sck_pin(sck)
7  {}
8 
10  pinMode(mosi_pin, OUTPUT);
11  pinMode(sck_pin, OUTPUT);
12  pinMode(miso_pin, INPUT_PULLUP);
13  #ifdef ARDUINO_ARCH_AVR
14  mosi_register = portOutputRegister(getPort(mosi_pin));
15  miso_register = portInputRegister(getPort(miso_pin));
16  sck_register = portOutputRegister(getPort(sck_pin));
17  mosi_bm = digitalPinToBitMask(mosi_pin);
18  miso_bm = digitalPinToBitMask(miso_pin);
19  sck_bm = digitalPinToBitMask(sck_pin);
20  #endif
21  writeSCK_H;
22 }
23 
24 uint8_t SW_SPIClass::transfer(uint8_t ulVal) {
25  uint8_t value = 0;
26  writeSCK_L;
27 
28  for (uint8_t i=7 ; i>=1 ; i--) {
29  // Write bit
30  !!(ulVal & (1 << i)) ? writeMOSI_H : writeMOSI_L;
31  // Start clock pulse
32  writeSCK_H;
33  // Read bit
34  value |= ( readMISO ? 1 : 0) << i;
35  // Stop clock pulse
36  writeSCK_L;
37  }
38 
39  !!(ulVal & (1 << 0)) ? writeMOSI_H : writeMOSI_L;
40  writeSCK_H;
41  value |= ( readMISO ? 1 : 0) << 0;
42 
43  return value;
44 }
45 
46 uint16_t SW_SPIClass::transfer16(uint16_t data) {
47  uint16_t returnVal = 0x0000;
48  returnVal |= transfer((data>>8)&0xFF) << 8;
49  returnVal |= transfer(data&0xFF) & 0xFF;
50  return returnVal;
51 }
SW_SPIClass::transfer16
uint16_t transfer16(uint16_t data)
Definition: SW_SPI.cpp:46
writeMOSI_H
#define writeMOSI_H
Definition: TMC_platforms.h:32
readMISO
#define readMISO
Definition: TMC_platforms.h:36
SW_SPIClass::transfer
uint8_t transfer(uint8_t ulVal)
Definition: SW_SPI.cpp:24
SW_SPIClass::init
void init()
Definition: SW_SPI.cpp:9
writeSCK_H
#define writeSCK_H
Definition: TMC_platforms.h:34
writeMOSI_L
#define writeMOSI_L
Definition: TMC_platforms.h:33
SW_SPI.h
writeSCK_L
#define writeSCK_L
Definition: TMC_platforms.h:35
SW_SPIClass::SW_SPIClass
SW_SPIClass(uint16_t sw_mosi_pin, uint16_t sw_miso_pin, uint16_t sw_sck_pin)
Definition: SW_SPI.cpp:3