|
1 | 1 | /*
|
2 |
| - SPI.cpp - SPI library for esp8266 |
| 2 | + SPI.cpp - SPI library for esp8266 |
3 | 3 |
|
4 |
| - Copyright (c) 2015 Hristo Gochkov. All rights reserved. |
5 |
| - This file is part of the esp8266 core for Arduino environment. |
| 4 | + Copyright (c) 2015 Hristo Gochkov. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
6 | 6 |
|
7 |
| - This library is free software; you can redistribute it and/or |
8 |
| - modify it under the terms of the GNU Lesser General Public |
9 |
| - License as published by the Free Software Foundation; either |
10 |
| - version 2.1 of the License, or (at your option) any later version. |
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
11 | 11 |
|
12 |
| - This library is distributed in the hope that it will be useful, |
13 |
| - but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
| - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
| - Lesser General Public License for more details. |
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
16 | 16 |
|
17 |
| - You should have received a copy of the GNU Lesser General Public |
18 |
| - License along with this library; if not, write to the Free Software |
19 |
| - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
| -*/ |
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
21 | 21 |
|
22 | 22 | #include "SPI.h"
|
| 23 | +#include "HardwareSerial.h" |
| 24 | + |
| 25 | +typedef union { |
| 26 | + uint32_t regValue; |
| 27 | + struct { |
| 28 | + unsigned regL :6; |
| 29 | + unsigned regH :6; |
| 30 | + unsigned regN :6; |
| 31 | + unsigned regPre :13; |
| 32 | + unsigned regEQU :1; |
| 33 | + }; |
| 34 | +} spiClk_t; |
23 | 35 |
|
24 | 36 | SPIClass SPI;
|
25 | 37 |
|
26 |
| -SPIClass::SPIClass(){} |
27 |
| - |
28 |
| -void SPIClass::begin(){ |
29 |
| - pinMode(SCK, SPECIAL); |
30 |
| - pinMode(MISO, SPECIAL); |
31 |
| - pinMode(MOSI, SPECIAL); |
32 |
| - |
33 |
| - GPMUX = 0x105; |
34 |
| - SPI1C = 0; |
35 |
| - SPI1CLK = SPI_CLOCK_DIV16;//1MHz |
36 |
| - SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE; |
37 |
| - SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO); |
38 |
| - SPI1C1 = 0; |
| 38 | +SPIClass::SPIClass() { |
| 39 | +} |
| 40 | + |
| 41 | +void SPIClass::begin() { |
| 42 | + pinMode(SCK, SPECIAL); ///< GPIO14 |
| 43 | + pinMode(MISO, SPECIAL); ///< GPIO12 |
| 44 | + pinMode(MOSI, SPECIAL); ///< GPIO13 |
| 45 | + |
| 46 | + GPMUX = 0x105; // note crash if SPI flash Frequency < 40MHz |
| 47 | + SPI1C = 0; |
| 48 | + setFrequency(1000000); ///< 1MHz |
| 49 | + SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE; |
| 50 | + SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO); |
| 51 | + SPI1C1 = 0; |
39 | 52 | }
|
40 | 53 |
|
41 | 54 | void SPIClass::end() {
|
42 |
| - pinMode(SCK, INPUT); |
43 |
| - pinMode(MISO, INPUT); |
44 |
| - pinMode(MOSI, INPUT); |
| 55 | + pinMode(SCK, INPUT); |
| 56 | + pinMode(MISO, INPUT); |
| 57 | + pinMode(MOSI, INPUT); |
45 | 58 | }
|
46 | 59 |
|
47 | 60 | void SPIClass::beginTransaction(SPISettings settings) {
|
48 |
| - setClockDivider(settings._clock); |
49 |
| - setBitOrder(settings._bitOrder); |
50 |
| - setDataMode(settings._dataMode); |
| 61 | + setFrequency(settings._clock); |
| 62 | + setBitOrder(settings._bitOrder); |
| 63 | + setDataMode(settings._dataMode); |
51 | 64 | }
|
52 | 65 |
|
53 |
| -void SPIClass::endTransaction() {} |
| 66 | +void SPIClass::endTransaction() { |
| 67 | +} |
54 | 68 |
|
55 | 69 | void SPIClass::setDataMode(uint8_t dataMode) {
|
56 |
| - |
| 70 | + |
| 71 | + /** |
| 72 | + SPI_MODE0 0x00 - CPOL: 0 CPHA: 0 |
| 73 | + SPI_MODE1 0x01 - CPOL: 0 CPHA: 1 |
| 74 | + SPI_MODE2 0x10 - CPOL: 1 CPHA: 0 |
| 75 | + SPI_MODE3 0x11 - CPOL: 1 CPHA: 1 |
| 76 | + */ |
| 77 | + |
| 78 | + bool CPOL = (dataMode & 0x10); ///< CPOL (Clock Polarity) |
| 79 | + bool CPHA = (dataMode & 0x01); ///< CPHA (Clock Phase) |
| 80 | + |
| 81 | + if(CPHA) { |
| 82 | + SPI1U |= (SPIUSME); |
| 83 | + } else { |
| 84 | + SPI1U &= ~(SPIUSME); |
| 85 | + } |
| 86 | + |
| 87 | + if(CPOL) { |
| 88 | + //todo How set CPOL??? |
| 89 | + } |
| 90 | + |
57 | 91 | }
|
58 | 92 |
|
59 | 93 | void SPIClass::setBitOrder(uint8_t bitOrder) {
|
60 |
| - if (bitOrder == MSBFIRST) { |
61 |
| - SPI1C &= ~(SPICWBO | SPICRBO); |
62 |
| - } else { |
63 |
| - SPI1C |= (SPICWBO | SPICRBO); |
64 |
| - } |
| 94 | + if(bitOrder == MSBFIRST) { |
| 95 | + SPI1C &= ~(SPICWBO | SPICRBO); |
| 96 | + } else { |
| 97 | + SPI1C |= (SPICWBO | SPICRBO); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * calculate the Frequency based on the register value |
| 103 | + * @param reg |
| 104 | + * @return |
| 105 | + */ |
| 106 | +static uint32_t ClkRegToFreq(spiClk_t * reg) { |
| 107 | + return (F_CPU / ((reg->regPre + 1) * (reg->regN + 1))); |
| 108 | +} |
| 109 | + |
| 110 | +void SPIClass::setFrequency(uint32_t freq) { |
| 111 | + static uint32_t lastSetFrequency = 0; |
| 112 | + static uint32_t lastSetRegister = 0; |
| 113 | + |
| 114 | + if(freq >= F_CPU) { |
| 115 | + setClockDivider(0x80000000); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if(lastSetFrequency == freq && lastSetRegister == SPI1CLK) { |
| 120 | + // do nothing (speed optimization) |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + const spiClk_t minFreqReg = { 0x7FFFF000 }; |
| 125 | + uint32_t minFreq = ClkRegToFreq((spiClk_t*) &minFreqReg); |
| 126 | + if(freq < minFreq) { |
| 127 | + freq = minFreq; |
| 128 | + } |
| 129 | + |
| 130 | + uint8_t calN = 1; |
| 131 | + |
| 132 | + spiClk_t bestReg = { 0 }; |
| 133 | + int32_t bestFreq = 0; |
| 134 | + |
| 135 | + // find the best match |
| 136 | + while(calN <= 0x3F) { // 0x3F max for N |
| 137 | + |
| 138 | + spiClk_t reg = { 0 }; |
| 139 | + int32_t calFreq; |
| 140 | + int32_t calPre; |
| 141 | + int8_t calPreVari = -2; |
| 142 | + |
| 143 | + reg.regN = calN; |
| 144 | + |
| 145 | + while(calPreVari++ <= 1) { // test different variants for Pre (we calculate in int so we miss the decimals, testing is the easyest and fastest way) |
| 146 | + calPre = (((F_CPU / (reg.regN + 1)) / freq) - 1) + calPreVari; |
| 147 | + if(calPre > 0x1FFF) { |
| 148 | + reg.regPre = 0x1FFF; // 8191 |
| 149 | + } else if(calPre <= 0) { |
| 150 | + reg.regPre = 0; |
| 151 | + } else { |
| 152 | + reg.regPre = calPre; |
| 153 | + } |
| 154 | + |
| 155 | + reg.regL = ((reg.regN + 1) / 2); |
| 156 | + // reg.regH = (reg.regN - reg.regL); |
| 157 | + |
| 158 | + // test calculation |
| 159 | + calFreq = ClkRegToFreq(®); |
| 160 | + //os_printf("-----[0x%08X][%d]\t EQU: %d\t Pre: %d\t N: %d\t H: %d\t L: %d = %d\n", reg.regValue, freq, reg.regEQU, reg.regPre, reg.regN, reg.regH, reg.regL, calFreq); |
| 161 | + |
| 162 | + if(calFreq == (int32_t) freq) { |
| 163 | + // accurate match use it! |
| 164 | + memcpy(&bestReg, ®, sizeof(bestReg)); |
| 165 | + break; |
| 166 | + } else if(calFreq < (int32_t) freq) { |
| 167 | + // never go over the requested frequency |
| 168 | + if(abs(freq - calFreq) < abs(freq - bestFreq)) { |
| 169 | + bestFreq = calFreq; |
| 170 | + memcpy(&bestReg, ®, sizeof(bestReg)); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + if(calFreq == (int32_t) freq) { |
| 175 | + // accurate match use it! |
| 176 | + break; |
| 177 | + } |
| 178 | + calN++; |
| 179 | + } |
| 180 | + |
| 181 | + // os_printf("[0x%08X][%d]\t EQU: %d\t Pre: %d\t N: %d\t H: %d\t L: %d\t - Real Frequency: %d\n", bestReg.regValue, freq, bestReg.regEQU, bestReg.regPre, bestReg.regN, bestReg.regH, bestReg.regL, ClkRegToFreq(&bestReg)); |
| 182 | + |
| 183 | + setClockDivider(bestReg.regValue); |
| 184 | + lastSetRegister = SPI1CLK; |
| 185 | + lastSetFrequency = freq; |
| 186 | + |
65 | 187 | }
|
66 | 188 |
|
67 | 189 | void SPIClass::setClockDivider(uint32_t clockDiv) {
|
68 |
| - SPI1CLK = clockDiv; |
| 190 | + SPI1CLK = clockDiv; |
69 | 191 | }
|
70 | 192 |
|
71 | 193 | uint8_t SPIClass::transfer(uint8_t data) {
|
72 |
| - while(SPI1CMD & SPIBUSY); |
73 |
| - SPI1W0 = data; |
74 |
| - SPI1CMD |= SPIBUSY; |
75 |
| - while(SPI1CMD & SPIBUSY); |
76 |
| - return (uint8_t)(SPI1W0 & 0xff); |
| 194 | + while(SPI1CMD & SPIBUSY) |
| 195 | + ; |
| 196 | + SPI1W0 = data; |
| 197 | + SPI1CMD |= SPIBUSY; |
| 198 | + while(SPI1CMD & SPIBUSY) |
| 199 | + ; |
| 200 | + return (uint8_t) (SPI1W0 & 0xff); |
77 | 201 | }
|
78 | 202 |
|
79 | 203 | uint16_t SPIClass::transfer16(uint16_t data) {
|
|
0 commit comments