Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ SPI anything Receive Int From Slave
|
SPI anything Receive Int From Slave
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Jayjayuk
(2 posts) Bio
|
| Date
| Wed 18 Feb 2015 01:22 PM (UTC) |
| Message
| Hello... i have just joined this forum in hope to seek some advice. i am currently working on two 328p barebones and i want to communicate via SPI. i have been trying to modify the SPI anything functions provided by Nick Gammon so that i can return data and not just send it to the slave.
I understand how SPI works enough to follow the flow and return of SPDR on master and slave (following Nick Gammons tutorial) but i dont want to send a structure of data... i just need to be able to send an INT to the slave... then slave will decide if its a command and set the command... then i will send another INT to the slave for it to update a variable and pull SS HIGH... this i can do and the functions already allow for this. The problem i face is sending a command byte, the command byte tells the slave to prepare some data (INT) for the master so it is ready the next time the master calls. then when the master calls, return the INT (from slave to master) in the same manner the master sends and INT to the slave... so usually i would use (pseudo code)
///master///
int var = 555;
SPI_writeAnything(var)
///slave///
int var;
SPI_readAnything_ISR(var)
This will pass var over to the slave ok but i want to reverse that process... so when the slave gets a command byte which tells the slave to return data to the master the slave will put a byte (of the int) into the SPDR and wait for the master to call... then the slave will shift in the next byte (from the int) and wait until the master calls... something like this
///slave///
int varTwo
SPI_writeAnything(varTwo)
///master///
int varTwo;
SPI_readAnything_ISR(varTwo)
what i expect from this reverse process is that the slave will load the SPDR register and wait until the master starts to send data again... at which point the slave will transfer and then shift a byte every time the master sends data (dummy data for the receive)
the slave will go inside the SPI_ISR and check the command byte... and assign it to command... then before i leave the SPI_ISR i do another switch case on command and check to see if the master is requesting data... if so then i want to load the data straight away ready for the next transmission.
i have nearly got this working a few times in a few different ways but it has never been good/consistent enough to use in code, in fact it only properly worked once but i had to use 4 different 1 second delays on the master to so the data was correct or i would just get all ZERO's... i dont need to send floats... just INTs and occasionally the odd byte here and there.
i have been stuck on this for three solid nights and dont seem to be getting anywhere... can anybody on this forum help me get around this problem please? i can provide code if needed for master and slave but thought i would try explaining first as all i really need to do is reverse the process of SPI_anything from the master and slave point of view. maybe i would get a little further if i fully understood what is happening in the SPI_anything functions then i might be able to get a little further with it myself... but as i am new to arduino programming there is somethings that i dont understand/cant read the code for lack of knowledge/experience.
To Nick Gammon... if you have some spare helpful time could you please explain to me the process (step by step) of the SPI_writeAnything & SPI_readAnything functions as i think if i fully understood this i might be able to get further
Cheers | | Top |
|
| Posted by
| Jayjayuk
(2 posts) Bio
|
| Date
| Reply #1 on Wed 18 Feb 2015 10:08 PM (UTC) |
| Message
| Turns out i just needed a little read on pointers to understand how the functions work. I found that data can be received in this way (slave to master) but use SPI_readAnything() on the master and not SPI_readAnything_ISR() or it will store an old SPDR byte in the INT before it has done the first transfer and will start at the wrong address on the INT when the transfer happens. This took me longer than it should, but always better to figure things out yourself :)
@ Nick Gammon - Thank You for the SPI write up, it has everything you need to know even if you know nothing about SPI to start with. Could possibly add how to return data from slave at the very end if you get time? as you talk about sending and receiving a single byte at the same time but not how to return structures from the slave, could help the readers.
Cheers | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #2 on Wed 18 Feb 2015 10:19 PM (UTC) |
| Message
| |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
16,710 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top