worked on SoftSPI and RFID-Reader MFRC522

This commit is contained in:
2017-09-15 18:02:15 +02:00
parent d028b79325
commit 0fc4c78d72
2 changed files with 651 additions and 26 deletions

View File

@@ -4,6 +4,26 @@
//#include "IO.h"
#include "fastGPIO.h"
// wemos D1 mini
#define SPI_CS_OUTPUT GPIO15_OUTPUT_SET
#define SPI_MOSI_OUTPUT GPIO13_OUTPUT_SET
#define SPI_MISO_INPUT GPIO12_INPUT_SET
#define SPI_MISO_NO_PULLUP GPIO12_INPUT_PULLUP_UNSET
#define SPI_CLK_OUTPUT GPIO14_OUTPUT_SET
#define SPI_CS_LO GPIO15_L
#define SPI_CS_HI GPIO15_H
#define SPI_CLK_LO GPIO14_L
#define SPI_CLK_HI GPIO14_H
#define SPI_MOSI_LO GPIO13_L
#define SPI_MOSI_HI GPIO13_H
#define SPI_MISO_READ GPIO12_IN
#define SPI_FAST
class SoftSPI {
// MTDI GPIO12 MISO (DIN) D6
@@ -25,10 +45,10 @@ public:
}
void init() const {
GPIO12_INPUT_SET; GPIO12_INPUT_PULLUP_UNSET; // D6: MISO
GPIO13_OUTPUT_SET; // D7: MOSI
GPIO14_OUTPUT_SET; // D5: CLK
GPIO15_OUTPUT_SET; // D8: CS
SPI_MISO_INPUT; SPI_MISO_NO_PULLUP; // D6: MISO
SPI_MOSI_OUTPUT; // D7: MOSI
SPI_CLK_OUTPUT; // D5: CLK
SPI_CS_OUTPUT; // D8: CS
}
inline void chipSelect() const {
@@ -46,13 +66,13 @@ public:
}
inline void csLo() const { GPIO15_L; } // D8
inline void csHi() const { GPIO15_H; } // D8
inline void csLo() const { SPI_CS_LO; } // D8
inline void csHi() const { SPI_CS_HI; } // D8
inline void clkHi() const { GPIO14_H; } // D5
inline void clkLo() const { GPIO14_L; } // D5
inline void clkLo() const { SPI_CLK_LO; } // D5
inline void clkHi() const { SPI_CLK_HI; } // D5
inline bool getMISO() const {return GPIO12_IN;} // D6
inline bool getMISO() const {return SPI_MISO_READ;} // D6
/** read 16 bits */
@@ -77,7 +97,7 @@ public:
}
/** read 8 bits */
inline uint16_t readByte() const {
inline uint8_t readByte() const {
return
(readBit() << 7) |
(readBit() << 6) |
@@ -156,23 +176,25 @@ private:
inline uint8_t readWriteBit(const bool out) const {
if(out) {GPIO13_H;} else {GPIO13_L;}
if(out) {SPI_MOSI_HI;} else {SPI_MOSI_LO;}
wait();
clkHi();
wait();
const bool inp = getMISO();
wait();
clkLo();
wait();
return (inp) ? 1 : 0;
}
/** write one bit to the bus */
inline void writeBit(const bool out) const {
if(out) {GPIO13_H;} else {GPIO13_L;}
if(out) {SPI_MOSI_HI;} else {SPI_MOSI_LO;}
wait();
clkHi();
wait();
clkLo();
wait();
}
/** read one bit from the bus */
@@ -186,14 +208,25 @@ private:
return (val) ? 1 : 0;
}
#ifdef SPI_FAST
inline void wait() const {
__asm__ __volatile__("nop");
}
inline void waitLong() const {
os_delay_us(1);
}
#else
inline void wait() const {
os_delay_us(2);
}
inline void waitLong() const {
os_delay_us(50);
}
#endif
};
#endif // SOFTSPI_H