|
| 1 | +/* |
| 2 | +Changes: |
| 3 | +30.10.2018: Ack: Timing for MISO corrected, some comments added. |
| 4 | + hspi_slave_setStatus in RAM |
| 5 | +13.01.2019: Ack: Comments added |
| 6 | +
|
| 7 | + SPISlave library for esp8266 |
| 8 | +
|
| 9 | + Copyright (c) 2015 Hristo Gochkov. All rights reserved. |
| 10 | + This file is part of the esp8266 core for Arduino environment. |
| 11 | +
|
| 12 | + This library is free software; you can redistribute it and/or |
| 13 | + modify it under the terms of the GNU Lesser General Public |
| 14 | + License as published by the Free Software Foundation; either |
| 15 | + version 2.1 of the License, or (at your option) any later version. |
| 16 | +
|
| 17 | + This library is distributed in the hope that it will be useful, |
| 18 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | + Lesser General Public License for more details. |
| 21 | +
|
| 22 | + You should have received a copy of the GNU Lesser General Public |
| 23 | + License along with this library; if not, write to the Free Software |
| 24 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | +*/ |
| 26 | +#include "hspi_slave.h" |
| 27 | +#include "esp8266_peri.h" |
| 28 | +#include "ets_sys.h" |
| 29 | + |
| 30 | +static void (*_hspi_slave_rx_data_cb)(void * arg, uint8_t * data, uint8_t len) = NULL; |
| 31 | +static void (*_hspi_slave_tx_data_cb)(void * arg) = NULL; |
| 32 | +static void (*_hspi_slave_rx_status_cb)(void * arg, uint32_t data) = NULL; |
| 33 | +static void (*_hspi_slave_tx_status_cb)(void * arg) = NULL; |
| 34 | +static uint8_t _hspi_slave_buffer[33]; |
| 35 | + |
| 36 | +void ICACHE_RAM_ATTR _hspi_slave_isr_handler(void *arg) |
| 37 | +{ |
| 38 | + uint32_t status; |
| 39 | + uint32_t istatus; |
| 40 | + |
| 41 | + istatus = SPIIR; |
| 42 | + |
| 43 | + if(istatus & (1 << SPII1)) { //SPI1 ISR |
| 44 | + status = SPI1S; |
| 45 | + SPI1S &= ~(0x3E0);//disable interrupts |
| 46 | + SPI1S |= SPISSRES;//reset |
| 47 | + SPI1S &= ~(0x1F);//clear interrupts |
| 48 | + SPI1S |= (0x3E0);//enable interrupts |
| 49 | + |
| 50 | + if((status & SPISRBIS) != 0 && (_hspi_slave_tx_data_cb)) { |
| 51 | + _hspi_slave_tx_data_cb(arg); |
| 52 | + } |
| 53 | + if((status & SPISRSIS) != 0 && (_hspi_slave_tx_status_cb)) { |
| 54 | + _hspi_slave_tx_status_cb(arg); |
| 55 | + } |
| 56 | + if((status & SPISWSIS) != 0 && (_hspi_slave_rx_status_cb)) { |
| 57 | + uint32_t s = SPI1WS; |
| 58 | + _hspi_slave_rx_status_cb(arg, s); |
| 59 | + } |
| 60 | + if((status & SPISWBIS) != 0 && (_hspi_slave_rx_data_cb)) { |
| 61 | + uint8_t i; |
| 62 | + uint32_t data; |
| 63 | + _hspi_slave_buffer[32] = 0; |
| 64 | + for(i=0; i<8; i++) { |
| 65 | + data=SPI1W(i); |
| 66 | + _hspi_slave_buffer[i<<2] = data & 0xff; |
| 67 | + _hspi_slave_buffer[(i<<2)+1] = (data >> 8) & 0xff; |
| 68 | + _hspi_slave_buffer[(i<<2)+2] = (data >> 16) & 0xff; |
| 69 | + _hspi_slave_buffer[(i<<2)+3] = (data >> 24) & 0xff; |
| 70 | + } |
| 71 | + _hspi_slave_rx_data_cb(arg, &_hspi_slave_buffer[0], 32); |
| 72 | + } |
| 73 | + } else if(istatus & (1 << SPII0)) { //SPI0 ISR |
| 74 | + SPI0S &= ~(0x3ff);//clear SPI ISR |
| 75 | + } else if(istatus & (1 << SPII2)) {} //I2S ISR |
| 76 | +} |
| 77 | + |
| 78 | +void hspi_slave_begin(uint8_t status_len, void * arg) |
| 79 | +{ |
| 80 | + status_len &= 7; |
| 81 | + if(status_len > 4) { |
| 82 | + status_len = 4; //max 32 bits |
| 83 | + } |
| 84 | + if(status_len == 0) { |
| 85 | + status_len = 1; //min 8 bits |
| 86 | + } |
| 87 | + |
| 88 | + pinMode(SS, SPECIAL); |
| 89 | + pinMode(SCK, SPECIAL); |
| 90 | + pinMode(MISO, SPECIAL); |
| 91 | + pinMode(MOSI, SPECIAL); |
| 92 | + |
| 93 | + SPI1S = SPISE | SPISBE | 0x3E0; // SPI_SLAVE_REG |
| 94 | + SPI1U = SPIUMISOH | SPIUCOMMAND | SPIUSSE; //SPI_USER_REG |
| 95 | + SPI1CLK = 0; |
| 96 | + SPI1U2 = (7 << SPILCOMMAND); // SPI_USER2_REG |
| 97 | + SPI1S1 = (((status_len * 8) - 1) << SPIS1LSTA) | (0xff << SPIS1LBUF) | (7 << SPIS1LWBA) | (7 << SPIS1LRBA) | SPIS1RSTA; //SPI_SLAVE1_REG |
| 98 | + SPI1P = (1 << 19); // not described in ESP32-reference |
| 99 | + SPI1CMD = SPIBUSY; // not described in ESP32-reference |
| 100 | +// (no settings in SPI1C2 in the original version.) |
| 101 | + SPI1C2=(0x2<<SPIC2MOSIDN_S) | (0x1<<SPIC2MISODM_S); // Ack. 30.10.2018; timing of MISO// |
| 102 | +// SPIC2MISODM_S (SPI_MISO_DELAY_MODE) = 1, makes slave to change MISO value on falling edge on CLK signal |
| 103 | +// es it should for SPI-Mode = 1 |
| 104 | +// SPIC2MOSIDN_S (SPI_MOSI_DELAY_NUM) = 2: Probably not required, but all tests are done with this setting |
| 105 | + ETS_SPI_INTR_ATTACH(_hspi_slave_isr_handler,arg); |
| 106 | + ETS_SPI_INTR_ENABLE(); |
| 107 | +} |
| 108 | + |
| 109 | +void ICACHE_RAM_ATTR hspi_slave_setStatus(uint32_t status) |
| 110 | +// put in RAM as setStatus is often called from an interrupt routine |
| 111 | + |
| 112 | +{ |
| 113 | + SPI1WS = status; |
| 114 | +} |
| 115 | + |
| 116 | +void hspi_slave_setData(uint8_t *data, uint8_t len) |
| 117 | +{ |
| 118 | + uint8_t i; |
| 119 | + uint32_t out = 0; |
| 120 | + uint8_t bi = 0; |
| 121 | + uint8_t wi = 8; |
| 122 | + |
| 123 | + for(i=0; i<32; i++) { |
| 124 | + out |= (i<len)?(data[i] << (bi * 8)):0; |
| 125 | + bi++; |
| 126 | + bi &= 3; |
| 127 | + if(!bi) { |
| 128 | + SPI1W(wi) = out; |
| 129 | + out = 0; |
| 130 | + wi++; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +void hspi_slave_onData(void (*rxd_cb)(void *, uint8_t *, uint8_t)) |
| 136 | +{ |
| 137 | + _hspi_slave_rx_data_cb = rxd_cb; |
| 138 | +} |
| 139 | + |
| 140 | +void hspi_slave_onDataSent(void (*txd_cb)(void *)) |
| 141 | +{ |
| 142 | + _hspi_slave_tx_data_cb = txd_cb; |
| 143 | +} |
| 144 | + |
| 145 | +void hspi_slave_onStatus(void (*rxs_cb)(void *, uint32_t)) |
| 146 | +{ |
| 147 | + _hspi_slave_rx_status_cb = rxs_cb; |
| 148 | +} |
| 149 | + |
| 150 | +void hspi_slave_onStatusSent(void (*txs_cb)(void *)) |
| 151 | +{ |
| 152 | + _hspi_slave_tx_status_cb = txs_cb; |
| 153 | +} |
0 commit comments