|
54 | 54 | #define LED_PMODE 7
|
55 | 55 | #define PROG_FLICKER true
|
56 | 56 |
|
57 |
| -#if !defined(ARDUINO_ARCH_SAM) and !defined(ARDUINO_ARCH_SAMD) |
| 57 | +// Configure SPI clock. |
| 58 | +// E.g. for an attiny @128 kHz: |
| 59 | +// (pulseWidth should be > 2 cpu cycles, so take 3 cycles:) |
| 60 | +// #define SPI_CLOCK (128000/3) |
| 61 | +// |
| 62 | +// A clock slow enough for an attiny85 @ 1MHz, is a reasonable default: |
| 63 | + |
| 64 | +#define SPI_CLOCK (1000000/3) |
| 65 | + |
| 66 | +// Select hardware or software SPI, depending on SPI clock. |
| 67 | +// Currently only for AVR, for other archs (Due, Zero,...), |
| 68 | +// hardware SPI is probably too fast anyway. |
58 | 69 |
|
| 70 | +#if defined(ARDUINO_ARCH_AVR) |
| 71 | + |
| 72 | +#if SPI_CLOCK > (F_CPU / 128) |
59 | 73 | #define USE_HARDWARE_SPI
|
| 74 | +#endif |
60 | 75 |
|
61 | 76 | #endif
|
62 | 77 |
|
| 78 | +#ifdef USE_HARDWARE_SPI |
| 79 | +#warning hw spi !!! |
| 80 | +#else |
| 81 | +#warning NOT usung hw spi |
| 82 | +#endif |
63 | 83 |
|
64 | 84 | // Configure the serial port to use.
|
65 | 85 | //
|
@@ -96,27 +116,34 @@ void pulse(int pin, int times);
|
96 | 116 |
|
97 | 117 | #ifdef USE_HARDWARE_SPI
|
98 | 118 | #include "SPI.h"
|
99 |
| -#define SPI_CLOCK_DIV_MAX SPI_CLOCK_DIV128 |
100 | 119 | #else
|
101 | 120 |
|
102 |
| -#define SPI_CLOCK_DIV_MAX 255 |
| 121 | +#define SPI_MODE0 0x00 |
| 122 | + |
| 123 | +class SPISettings { |
| 124 | +public: |
| 125 | + // clock is in Hz |
| 126 | + SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clock(clock){ |
| 127 | + (void) bitOrder; |
| 128 | + (void) dataMode; |
| 129 | + }; |
| 130 | + |
| 131 | +private: |
| 132 | + uint32_t clock; |
| 133 | + |
| 134 | +friend class BitBangedSPI; |
| 135 | +}; |
103 | 136 |
|
104 | 137 | class BitBangedSPI {
|
105 | 138 | public:
|
106 |
| - void setDataMode(uint8_t dataMode) { |
107 |
| - (void) dataMode; |
108 |
| - } |
109 |
| - void setBitOrder(uint8_t bitOrder) { |
110 |
| - (void) bitOrder; |
111 |
| - } |
112 |
| - void setClockDivider(uint8_t clockDiv) { |
113 |
| - (void) clockDiv; |
| 139 | + |
| 140 | + void beginTransaction(SPISettings settings) { |
| 141 | + pulseWidth = 1000 / (settings.clock / 1000); |
| 142 | + if (pulseWidth == 0) |
| 143 | + pulseWidth = 1; |
114 | 144 | }
|
115 | 145 |
|
116 | 146 | void begin() {
|
117 |
| - // slow enough for an attiny85 @ 1MHz |
118 |
| - // (pulseWidth should be > 2 cpu cycles, so take 3 cycles:) |
119 |
| - pulseWidth = 3; |
120 | 147 |
|
121 | 148 | pinMode(MISO, INPUT);
|
122 | 149 | pinMode(RESET, OUTPUT);
|
@@ -148,10 +175,9 @@ static BitBangedSPI SPI;
|
148 | 175 |
|
149 | 176 | void setup() {
|
150 | 177 | SERIAL.begin(19200);
|
151 |
| - SPI.setDataMode(0); |
152 |
| - SPI.setBitOrder(MSBFIRST); |
153 |
| - // Select the slowest possible clock |
154 |
| - SPI.setClockDivider(SPI_CLOCK_DIV_MAX); |
| 178 | + |
| 179 | + SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0)); |
| 180 | + |
155 | 181 | pinMode(LED_PMODE, OUTPUT);
|
156 | 182 | pulse(LED_PMODE, 2);
|
157 | 183 | pinMode(LED_ERR, OUTPUT);
|
|
0 commit comments