|
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 | +typedef struct { |
| 25 | + uint32_t divider; |
| 26 | + uint32_t regValue; |
| 27 | +} spiClockDiv_t; |
| 28 | + |
| 29 | +static const spiClockDiv_t spiClockDiv[] = { |
| 30 | + { 0, (0x80000000) }, ///< @80Mhz = 80 MHz @160Mhz = 160 MHz |
| 31 | + { 2, (0x00001001) }, ///< @80Mhz = 40 MHz @160Mhz = 80 MHz |
| 32 | + { 4, (0x00041001) }, ///< @80Mhz = 20 MHz @160Mhz = 40 MHz |
| 33 | + { 6, (0x000fffc0) }, ///< @80Mhz = 16 MHz @160Mhz = 32 MHz |
| 34 | + { 8, (0x000c1001) }, ///< @80Mhz = 10 MHz @160Mhz = 20 MHz |
| 35 | + { 10, (0x00101001) }, ///< @80Mhz = 8 MHz @160Mhz = 16 MHz |
| 36 | + { 16, (0x001c1001) }, ///< @80Mhz = 5 MHz @160Mhz = 10 MHz |
| 37 | + { 20, (0x00241001) }, ///< @80Mhz = 4 MHz @160Mhz = 8 MHz |
| 38 | + { 40, (0x004c1001) }, ///< @80Mhz = 2 MHz @160Mhz = 4 MHz |
| 39 | + { 80, (0x009c1001) }, ///< @80Mhz = 1 MHz @160Mhz = 2 MHz |
| 40 | + { 160, (0x013c1001) }, ///< @80Mhz = 500 KHz @160Mhz = 1 MHz |
| 41 | + { 320, (0x027c1001) }, ///< @80Mhz = 250 KHz @160Mhz = 500 KHz |
| 42 | + { 640, (0x04fc1001) } ///< @80Mhz = 125 KHz @160Mhz = 250 KHz |
| 43 | +}; |
| 44 | + |
| 45 | +static const uint8_t spiClockDiv_count = (sizeof(spiClockDiv) / sizeof(spiClockDiv_t)); |
23 | 46 |
|
24 | 47 | SPIClass SPI; |
25 | 48 |
|
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; |
| 49 | +SPIClass::SPIClass() { |
| 50 | +} |
| 51 | + |
| 52 | +void SPIClass::begin() { |
| 53 | + pinMode(SCK, SPECIAL); ///< GPIO14 |
| 54 | + pinMode(MISO, SPECIAL); ///< GPIO12 |
| 55 | + pinMode(MOSI, SPECIAL); ///< GPIO13 |
| 56 | + |
| 57 | + GPMUX = 0x105; // note crash if spi flash Frequency < 40MHz |
| 58 | + SPI1C = 0; |
| 59 | + setFrequency(1000000); ///< 1Mhz |
| 60 | + SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE; |
| 61 | + SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO); |
| 62 | + SPI1C1 = 0; |
39 | 63 | } |
40 | 64 |
|
41 | 65 | void SPIClass::end() { |
42 | | - pinMode(SCK, INPUT); |
43 | | - pinMode(MISO, INPUT); |
44 | | - pinMode(MOSI, INPUT); |
| 66 | + pinMode(SCK, INPUT); |
| 67 | + pinMode(MISO, INPUT); |
| 68 | + pinMode(MOSI, INPUT); |
45 | 69 | } |
46 | 70 |
|
47 | 71 | void SPIClass::beginTransaction(SPISettings settings) { |
48 | | - setClockDivider(settings._clock); |
49 | | - setBitOrder(settings._bitOrder); |
50 | | - setDataMode(settings._dataMode); |
| 72 | + setFrequency(settings._clock); |
| 73 | + setBitOrder(settings._bitOrder); |
| 74 | + setDataMode(settings._dataMode); |
51 | 75 | } |
52 | 76 |
|
53 | | -void SPIClass::endTransaction() {} |
| 77 | +void SPIClass::endTransaction() { |
| 78 | +} |
54 | 79 |
|
55 | 80 | void SPIClass::setDataMode(uint8_t dataMode) { |
56 | | - |
| 81 | + // todo find way to set |
57 | 82 | } |
58 | 83 |
|
59 | 84 | void SPIClass::setBitOrder(uint8_t bitOrder) { |
60 | | - if (bitOrder == MSBFIRST) { |
61 | | - SPI1C &= ~(SPICWBO | SPICRBO); |
62 | | - } else { |
63 | | - SPI1C |= (SPICWBO | SPICRBO); |
64 | | - } |
| 85 | + if(bitOrder == MSBFIRST) { |
| 86 | + SPI1C &= ~(SPICWBO | SPICRBO); |
| 87 | + } else { |
| 88 | + SPI1C |= (SPICWBO | SPICRBO); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void SPIClass::setFrequency(uint32_t freq) { |
| 93 | + uint8_t i = 0; |
| 94 | + // find the best match |
| 95 | + if(freq < F_CPU) { |
| 96 | + for(i = 1; i < (spiClockDiv_count-1); i++) { |
| 97 | + if(freq >= (F_CPU/spiClockDiv[i].divider)) { |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + setClockDivider(spiClockDiv[i].regValue); |
65 | 103 | } |
66 | 104 |
|
67 | 105 | void SPIClass::setClockDivider(uint32_t clockDiv) { |
68 | | - SPI1CLK = clockDiv; |
| 106 | + SPI1CLK = clockDiv; |
69 | 107 | } |
70 | 108 |
|
71 | 109 | 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); |
| 110 | + while(SPI1CMD & SPIBUSY); |
| 111 | + SPI1W0 = data; |
| 112 | + SPI1CMD |= SPIBUSY; |
| 113 | + while(SPI1CMD & SPIBUSY); |
| 114 | + return (uint8_t) (SPI1W0 & 0xff); |
77 | 115 | } |
78 | 116 |
|
79 | 117 | uint16_t SPIClass::transfer16(uint16_t data) { |
|
0 commit comments