Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Vicne
#56385
Mr Mayhem wrote:I will probably need to go around a few more times to clarify stuff, but I won't abuse it, and/or I will post some pics of my setup and trial code to benefit the ESP8266 community.

Yes, I cannot promise I'll be able to spend much time on this, but I'm trying to give back the time others have given to me :-)

2. I have downloaded the zip you posted above, and will play with it and report back.
3. To try the new code, I assume I just need to back up the original SPISlave directory files and replace them with the new ones from your zip file? I am using Arduino IDE, I see the location of SPISlave directory is:
C:\Users\Me\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\SPISlave\

I worked on my sketch before Me-no-dev made it a library, so in my case those files sit next to my .ino sketch, but I guess replacing the library ones (after backing them up of course) should work indeed.

4. Newbie question: In SPISlave_Test.ino, (the sketch), how do I move the event handlers out of setup ()?

Mmmh, the event handler is *not* in setup(). It is only *registered by* setup(), and that is the right way to do it.

I noticed the event handlers in setup() end with });
presumably to keep them from running when setup() runs, yet be available.


No, you got it wrong :-)
You were mislead by the fact the sample uses "anonymous functions".

Let's start simple :
In my case, the code to be called is defined as follows:
Code: Select allvoid ICACHE_RAM_ATTR handleSpiInterrupt(uint32_t data) {
   ...
}


This is a normal function (except that it has the extra ICACHE_RAM_ATTR directive to make sure it is kept in RAM, but that's another matter).

In my setup(), I'm using the following to "register" the above function :
Code: Select allSPISlave.onTrans(&handleSpiInterrupt);

Which only means "if a trans interrupt happens in the future, then you will execute the handleSpiInterrupt() function.". Nothing else is executed at that time.
Did you notice the ampersand sign (&) before the function name? That is because onTrans() expects a pointer to a function.

Now what you see in the example is a shortcut that you can use if the implementation is only used once.
So instead of writing:
Code: Select allvoid ICACHE_RAM_ATTR handleStatusSent() {
   Serial.println("Status Sent");
}

void setup() {
    ...
    SPISlave.onStatusSent(&handleStatusSent());
    ...
}


You can replace the function by inserting its implementation instead as follows:
Code: Select allvoid setup() {
    ...
    SPISlave.onStatusSent([]() {
        Serial.println("Status Sent");
    });
    ...
}


The syntax []() {Serial.println("Status Sent");} is surprising but you can read the square brackets "[]" as "anonymous function", then you have the parentheses "()" for the parameters as usual (none in this case) and then the implementation between curly brackets "{Serial.println("Status Sent");}" as usual too.

And that whole expression []() {Serial.println("Status Sent");} can be passed as a parameter to "SPISlave.onStatusSent()", resulting in something like:
SPISlave.onStatusSent([]() {Serial.println("Status Sent");});

I was getting compile errors earlier when I tried to move them. I don't know how to flatten the }); magic, heh.

Does it make sense now ?
No magic, simply the end of the implementation of the function passed as a parameter :-)

5. Could you provide me with a simple example of copying 32 bytes from myBytes[512] array into SPISlave.setData() or similiar? I will make an honest effort to experiment here, but I am more than a little unsure still on the syntax to use, given that pointers are new to me.


Well, a simple implementation would be to add the following to SPISlave_Master.ino:
Code: Select allvoid sendBytes(const uint8_t * data, int offset, int length) {
    Serial.print("Master sending ");
    Serial.print(length);
    Serial.print(" bytes");
    char myData[32];
    memcpy(myData, data + offset, length);
    esp.writeData(myData, length);
}


And call it using sendBytes(myBytes, 0, 32);

Hope it helps...

Vicne
User avatar
By Mr Mayhem
#56409 Vince,

So far, I successfully copied the myByte[] array into the SPI pipeline using your suggested code. Now I am about to try related things like the "flattened" interrupt c files you posted in that zip. I am still wrapping my head around the beast. I am using Arduino IDE, and it doesn't like using char in one place and uint8_t in the other places. This is what was throwing me off the first time around. The SPISlave_Master.ino worked with the myBytes code you just suggested, after changing all the "char" to "uint8_t". But the send doing "Are You Alive?" is now broken. It doesn't matter, I am concerned with bytes, not chars anyway, but if you have a suggestion for making both work, I am all ears.

It appears that there is a lot more documentation and example code available in the newer EspressIf ESP8266 technical guide v1.2 than the previous one. Wow, they really puffed it up with more useful stuff!

http://espressif.com/sites/default/file ... nce_en.pdf

I see the name "slave test" as one of their new demos, heh, and color oscilloscope shots of the SPI waveforms, more explicit SPI code examples, a mux system for sharing either SPI or HSPI to a set of pins, etc. Wow, OK, before I do anything else, I am going to study this new guide for a while. Thanks for your help again Vince, you were very helpful both in getting something to behave, and for my morale on this pursuit.
User avatar
By Vicne
#56428
Mr Mayhem wrote:So far, I successfully copied the myByte[] array into the SPI pipeline using your suggested code. Now I am about to try related things like the "flattened" interrupt c files you posted in that zip. I am still wrapping my head around the beast. I am using Arduino IDE, and it doesn't like using char in one place and uint8_t in the other places. This is what was throwing me off the first time around. The SPISlave_Master.ino worked with the myBytes code you just suggested, after changing all the "char" to "uint8_t". But the send doing "Are You Alive?" is now broken. It doesn't matter, I am concerned with bytes, not chars anyway, but if you have a suggestion for making both work, I am all ears.

If you get an error message regarding incompatible types (and if you are sure they are compatible, like char and uint8_t), you can simply "typecast" one into another, by adding the target type before variable like so:
Code: Select allchar myData[32];
memcpy(myData, (char*)(data + offset), length);

(assuming the issue is at that line, which I doubt...)
It appears that there is a lot more documentation and example code available in the newer EspressIf ESP8266 technical guide v1.2 than the previous one. Wow, they really puffed it up with more useful stuff!

http://espressif.com/sites/default/file ... nce_en.pdf

I see the name "slave test" as one of their new demos, heh, and color oscilloscope shots of the SPI waveforms, more explicit SPI code examples, a mux system for sharing either SPI or HSPI to a set of pins, etc. Wow, OK, before I do anything else, I am going to study this new guide for a while. Thanks for your help again Vince, you were very helpful both in getting something to behave, and for my morale on this pursuit.


You're welcome.

Thanks for the link.
Note that it clearly says "The communication format is CMD + ADDR + Data when ESP8266 is in slave mode. The transmission only with DATA is not supported currently." (section 4.5.3)
Once again, I'm not a specialist, but I find it silly that they chose to implement that specific protocol (which I admit seems a de facto standard for interfacing memory chips) in hardware but no way to perform generic buffer read / buffer write logic. That's OK if you decide the protocol and the one propose suits your need, but if you're faced with a predefined protocol that is incompatible, if can be painful or even impossible to adapt...
Kind regards,

Vicne
User avatar
By romuye
#58385 Hi Vicne, me-no-dev and all,

I am trying to use STM32 microcontroller as master and ESP8266 as slave for SPI communication.
But unable to get it working...
Following is my console for ESP8266 output


Status Sent
Answer Sent
Answer Sent
Rcd: ��������������������������������Question: ��������������������������������
Status Sent
Status Sent
Answer Sent
Status Sent
Answer Sent
Rcd: ��-�����������������������������Question: ��-�����������������������������
Rcd: ��-��2�]�� Question: ��-��2�]��
Status Sent
Rcd: �Question: �
Status Sent
Answer Sent
Status Sent
Status Sent
Answer Sent
Rcd: �����y���������������������Question: �����y���������������������
Status Sent
Answer Sent
Rcd: �k�_�����[���6K���w}������j��Question: �k�_�����[���6K���w}������j��
Answer Sent