I know that @ruby mentioned on ESP32 forum that he got some code working on ESP8266 Slaves connected to an ESP31 Master, but he never published his code yet.
Do you have plan to make it Official into the official github ?
Explore... Chat... Share...
Moderator: igrr
#include <SPI.h>
// Code by Me-no-dev http://www.esp8266.com/viewtopic.php?f=32&t=10579#p50011
// PIN SPI Function
// 10 CS / SS (green)
// 11 MOSI (purple)
// 12 MISO (grey)
// 13 CLOCK (orange)
void readSpiStatus(){
uint8_t data[4];
digitalWrite(SS, LOW);
SPI.transfer(0x04);
for(uint8_t i=0; i<4; i++){
data[i] = SPI.transfer(0);
}
digitalWrite(SS, HIGH);
uint32_t status = (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
Serial.print("Status: "); Serial.println(status, HEX);
}
void readSpiBuffer(){
uint8_t data[32];
digitalWrite(SS, LOW);
SPI.transfer(0x03);
SPI.transfer(0x00);
for(uint8_t i=0; i<32; i++) data[i] = SPI.transfer(0);
digitalWrite(SS, HIGH);
Serial.print("Data: "); Serial.println((char *)data);
}
void writeSpiBuffer(char *str){
uint8_t i=0;
digitalWrite(SS, LOW);
SPI.transfer(0x02);
SPI.transfer(0x00);
while(*str && i++ < 32) SPI.transfer(*str++);
while(i++ < 32) SPI.transfer(0);
digitalWrite(SS, HIGH);
}
void writeSpiStatus(uint32_t status){
digitalWrite(SS, LOW);
SPI.transfer(0x01);
// Transfer status, LSB first
SPI.transfer(status & 0xFF);
SPI.transfer((status >> 8) & 0xFF);
SPI.transfer((status >> 16) & 0xFF);
SPI.transfer((status >> 32) & 0xFF);
digitalWrite(SS, HIGH);
}
void setup() {
Serial.begin(115200);
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2);
}
void loop() {
writeSpiStatus(0x00FF0F55);
delay(500);
}
#include "esp8266_peri.h"
// Code by Me-no-dev http://www.esp8266.com/viewtopic.php?f=32&t=10579#p50011
// Pinout from http://d.av.id.au/blog/esp8266-hardware-spi-hspi-general-info-and-pinout/
// GPIO # HSPI Function
// GPIO12 MISO (grey)
// GPIO13 MOSI (purple)
// GPIO14 CLOCK (orange)
// GPIO15 CS / SS (green)
byte data = 0;
void (*hspi_slave_rx_data_cb)(uint8_t * data, uint8_t len);
void (*hspi_slave_tx_data_cb)(void);
void (*hspi_slave_rx_status_cb)(uint32_t data);
void (*hspi_slave_tx_status_cb)(void);
//set the callbacks
void hspi_slave_setCallbacks(void (*rxd_cb)(uint8_t *, uint8_t), void (*txd_cb)(void), void (*rxs_cb)(uint32_t), void (*txs_cb)(void)){
hspi_slave_rx_data_cb = rxd_cb;
hspi_slave_tx_data_cb = txd_cb;
hspi_slave_rx_status_cb = rxs_cb;
hspi_slave_tx_status_cb = txs_cb;
}
//set the status register so the master can read it
void hspi_slave_setStatus(uint32_t status){
SPI1WS = status;
}
//set the data registers (max 32 bytes at a time)
void hspi_slave_setData(uint8_t *data, uint8_t len){
uint8_t i;
uint32_t out = 0;
uint8_t bi = 0;
uint8_t wi = 8;
for(i=0; i<32; i++) {
out |= (i<len)?(data[i] << (bi * 8)):0;
bi++; bi &= 3; if(!bi) {
SPI1W(wi) = out;
out = 0;
wi++;
}
}
}
//SPI Slave Interrupt Handler
void hspi_slave_isr_handler(void *para){
Serial.print('.');
uint32_t status;
uint32_t istatus;
istatus = SPIIR;
if(istatus & (1 << SPII1)) { //SPI1 ISR
status = SPI1S;
SPI1S &= ~(0x3E0);//disable interrupts
SPI1S |= (1 << SPISSRES);//reset
SPI1S &= ~(0x1F);//clear interrupts
SPI1S |= (0x3E0);//enable interrupts
if((status & (1 << SPISWBIS)) != 0 && (hspi_slave_rx_data_cb)) {
uint8_t i;
uint32_t data;
uint8_t buffer[32];
for(i=0;i<8;i++){
data=SPI1W(i);
buffer[i<<2] = data & 0xff;
buffer[(i<<2)+1] = (data >> 8) & 0xff;
buffer[(i<<2)+2] = (data >> 16) & 0xff;
buffer[(i<<2)+3] = (data >> 24) & 0xff;
}
hspi_slave_rx_data_cb(buffer, 32);
}
if((status & (1 << SPISRBIS)) != 0 && (hspi_slave_tx_data_cb)) hspi_slave_tx_data_cb();
if((status & (1 << SPISWSIS)) != 0 && (hspi_slave_rx_status_cb)) hspi_slave_rx_status_cb(SPI1WS);
if((status & (1 << SPISRSIS)) != 0 && (hspi_slave_tx_status_cb)) hspi_slave_tx_status_cb();
}
else if(istatus & (1 << SPII0)){
//SPI0 ISR
SPI0S &= ~(0x3ff);//clear SPI ISR
}
else if(istatus & (1 << SPII2)) {
//I2S ISR
}
}
//Start SPI Slave
void hspi_slave_begin(uint8_t status_len){
status_len &= 7;
if(status_len > 4) status_len == 4;//max 32 bits
if(status_len == 0) status_len == 1;//min 8 bits
pinMode(SS, SPECIAL);
pinMode(SCK, SPECIAL);
pinMode(MISO, SPECIAL);
pinMode(MOSI, SPECIAL);
SPI1S = (1 << SPISE) | (1 << SPISBE) | 0x3E0;
SPI1U = SPIUMISOH | SPIUCOMMAND | SPIUSSE;
SPI1CLK = 0;
SPI1U2 = (7 << SPILCOMMAND);
SPI1S1 = (((status_len * 8) - 1) << SPIS1LSTA) | (0xff << SPIS1LBUF) | (7 << SPIS1LWBA) | (7 << SPIS1LRBA) | (1 << SPIS1RSTA);
SPI1P = (1 << 19);
SPI1CMD = SPIBUSY;
ETS_SPI_INTR_ATTACH(hspi_slave_isr_handler, NULL);
ETS_SPI_INTR_ENABLE();
}
void my_hspi_slave_rx_data_cb(uint8_t * data, uint8_t len) {
Serial.print("Number of data bytes received: "); Serial.println(len);
hspi_slave_setStatus(0xFF555555);
}
void my_hspi_slave_tx_data_cb(void) {
Serial.println("Data sent");
hspi_slave_setStatus(0xFF333333);
}
void my_hspi_slave_rx_status_cb(uint32_t data){
Serial.print("Status received: "); Serial.println(data, HEX);
hspi_slave_setStatus(0xFF0F0F0F);
}
void my_hspi_slave_tx_status_cb(void){
Serial.println("Status Sent");
hspi_slave_setStatus(0xFF00FF00);
}
void setup() {
Serial.begin(115200);
hspi_slave_setCallbacks(my_hspi_slave_rx_data_cb, my_hspi_slave_tx_data_cb, my_hspi_slave_rx_status_cb, my_hspi_slave_tx_status_cb);
hspi_slave_begin(4); // 4 status byte
Serial.println("SPI slave started");
hspi_slave_setStatus(0xFFFF0000);
}
void loop() {
}
.Status received: 0
Status Sent
..SPI slave started
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]