Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 00:19 17 May 2022 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : SPI help needed

     Page 1 of 2    
Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1574
Posted: 09:32am 21 Jan 2022
Copy link to clipboard 
Print this post

I'm trying to get my head around SPI communication
I need to send and recieve STRING$ via SPI and I can't understand exactly how to do it
Specifically Im using a Picomite but the same should apply to others so hopefully someone can help

I have searched the forum and read the manual
In the manual it says SPI WRITE nbr, string$

Can anyone explain in simple terms an idiot like me can understand what nbr means exactly please?

I want to send "AT+GMR"  and print out the reply

OPTION BASE 1 ' our array will start with the index 1
DIM a$ ' define the array for receiving the data
SETPIN GP11, GP8, GP10, SPI2 ' assign the I/O pins
SPI2 OPEN 5000000, 11, 8 ' speed is 5MHz, 8 bits data
SPI2 WRITE 0, string$, "AT+GMR" ' send the command
SPI2 READ 20, a$ ' get twenty bytes from the slave
SPI2 CLOSE ' and close the channel
PRINT a$


I did try SPI2 WRITE 1, string$, "AT+GMR", SPI2 WRITE 6, string$, "AT+GMR" and SPI2 WRITE 8, string$, "AT+GMR"
but I get "INCORRECT ARGUMENT COUNT"

I also tried read with many different numbers but I've no idea how many characters the returned string will be



HOW exactly do I read a string that is returned when I actually manage to send the command properly?

In the example in the manual it also has these two lines
If there is no slave SPI device attacted I hope Im right in assume they are not needed?


PIN(10) = 1 : SETPIN 10, DOUT ' pin 10 will be used as the enable signal
PIN(10) = 0 ' assert the enable line (active low)

Can anyone give me an example of code to send the string "AT+GMR" via SPI and read the reply
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 2185
Posted: 09:45am 21 Jan 2022
Copy link to clipboard 
Print this post

First, think of SPI as a string of shift registers, not buffers. As you push a bit out a bit is returned. Consequently, all information has to be already in the remote shift register when you start to send - you aren't getting a reply to a current query, you're getting a reply to the last one (if that's what is in the remote TX register). As you get back what you send, basically, your reply can never be longer than your transmission unless you specifically pad it out with null characters to keep your clock running (the remote can't produce a clock signal).

You can, in fact, run SPI devices in series so the master produces a string of clock pulses which is a total of all the shift registers in the string before it's got all the data back.

What you'll probably have to do is clock out "AT+GMR" then pause to give the remote a chance to load it's shift register. Then send a string of blanks equal to the length of the expected reply. Your returned data should have shifted in just as you complete that.

I hope that helps and doesn't make life even more confusing...  :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 2419
Posted: 09:49am 21 Jan 2022
Copy link to clipboard 
Print this post

Hi Lewis,

  lew247 said  In the manual it says SPI WRITE nbr, string$

Can anyone explain in simple terms an idiot like me can understand what nbr means exactly please?


If I'm interpreting the documentation correctly the command doesn't send the entire string, it just sends the first 'nbr' characters from that string.

So to send "AT+GMR" I would guess:

SPI2 WRITE 6, "AT+GMR"


  lew247 said  HOW exactly do I read a string that is returned when I actually manage to send the command properly?


I'm less certain about SPI READ, but from the documentation it looks like it reads bytes into an integer array, with one byte per integer (Is that correct everyone? It sounds terribly inefficient, or does it treat the array as a byte-buffer?)

Thus to read 20 characters:

DIM data%(20), i%
SPI READ 20, data%()
FOR i% = 0 To 19 : PRINT CHR$(data%(i%)); : NEXT
PRINT


Hope I've got that mostly correct and it helps,

Tom
Edited 2022-01-21 19:55 by thwill
CMM2 Welcome Tape, Creaky old text adventures
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 2419
Posted: 09:51am 21 Jan 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  I hope that helps and doesn't make life even more confusing...  :)


I'm not certain, but I think Mick's reply addresses a lower-level, i.e. if you were not using the bulk transfer WRITE and READ commands.

Best wishes,

Tom
CMM2 Welcome Tape, Creaky old text adventures
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1574
Posted: 09:55am 21 Jan 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  I hope that helps and doesn't make life even more confusing...  :)

More confusing unfortunately, thanks though :)
I "think" bulk read/write might work but again I can't understand it
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 2419
Posted: 10:07am 21 Jan 2022
Copy link to clipboard 
Print this post

  lew247 said  More confusing unfortunately, thanks though :)
I "think" bulk read/write might work but again I can't understand it


See my reply above Lewis.

If this is still that Pico/Wireless thingy then as I've offered previously, if you lend me your spare then I'll try to help out. Otherwise I'm programming blind. I don't think I even have any SPI devices that expect to be read (possibly an LCD display?) just some Max7219's which you only write to.

Best wishes,

Tom
Edited 2022-01-21 20:24 by thwill
CMM2 Welcome Tape, Creaky old text adventures
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 1987
Posted: 12:21pm 24 Jan 2022
Copy link to clipboard 
Print this post

hi lewis,
   is this the board/module you are dealing with:
https://wiki.seeedstudio.com/Wio_RP2040_mini_Dev_Board-Onboard_Wifi/
https://wiki.seeedstudio.com/Wio_RP2040_Module_Build-in_Wireless_2.4G/

if so, i've not managed to find the schematic for the module as yet - this will document the connections between the Rp2040 and ESP8285. first step is to find out if there is a UART connection between the two, as this will be far easier to work with than SPI.


there is a good summary of SPI here:
https://en.wikipedia.org/wiki/Serial_Peripheral_Interface
(this same page is referenced by the MicroMite manual)

while it may at first appear daunting, the crux of the matter is that there only a few fixed features of SPI, and a great many things that are left up to the designer of any particular device: there must always be a clock (SCK) line; on every slave device there must always be either a data in line (MOSI) or a data out line (MISO), or possibly both. there may be a chip select (SS) line, there may be some sort of handshaking line.

each clock pulse may send data in just one direction (master to slave or vice versa), or data may be exchanged in both directions at once. the number of bits transferred per transaction may be absolutely anything between 1-bit and many hundreds of bits. it does not have to be a multiple of 8.

depending on how intelligent a slave device is, it may only act on SCK and the one data line (MOSI or MISO), for instance like a TTL shift register chip, or it may use SS to signal that a message sent to it should be acted upon in some complicated way (an example being a WiFi node).


the SPI functionality implemented by MMbasic may cover the requirements of the device you are dealing with, or it may not. even if not, the good thing is that it should be fairly easy to 'roll your own' SPI routines in BASIC. they may be a little slower than using the hardware SPI of the RP2040, but they will work.


cheers,
rob   :-)
Edited 2022-01-24 22:26 by robert.rozee
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 2185
Posted: 01:16pm 24 Jan 2022
Copy link to clipboard 
Print this post

What are you expecting the reply to be, lew?

  Quote  In the manual it says SPI WRITE nbr, string$

Can anyone explain in simple terms an idiot like me can understand what nbr means exactly please?


nbr is the number of characters out of string$ to send. So:

string$ = "12345"
SPI WRITE 3, string$
will send "123"
The length of the string$ must always be longer than or equal to nbr.
The three data bytes received will be discarded automatically.

SPI READ 5,data%()
will read 5 values into data%(). In order to do that it will send 0,0,0,0,0
It can only receive integer values, not strings.
Edited 2022-01-24 23:28 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs
 
flasherror
Senior Member

Joined: 07/01/2019
Location: United States
Posts: 155
Posted: 01:34pm 24 Jan 2022
Copy link to clipboard 
Print this post

  robert.rozee said  if so, i've not managed to find the schematic for the module as yet - this will document the connections between the Rp2040 and ESP8285. first step is to find out if there is a UART connection between the two, as this will be far easier to work with than SPI.


In my opinion using a UART solution may be more productive since AT command firmware already exists and can be used by every MMbasic device on cheap ESP8266 modules. As a starting point for using Espressif AT command firmware see  https://www.thebackshed.com/forum/ViewTopic.php?FID=16&TID=14494 on how to flash 8266.
Unless the specific board is popular or has cheap clones it might not be worth the effort (except as a hacking challenge).
Edited 2022-01-24 23:35 by flasherror
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 2185
Posted: 01:53pm 24 Jan 2022
Copy link to clipboard 
Print this post

Unfortunately SPI is not the system to use if you don't know the length of the returned string.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 1997
Posted: 03:49pm 24 Jan 2022
Copy link to clipboard 
Print this post

  lew247 said  . . . I want to send "AT+GMR"  and print out the reply

SPI2 OPEN 5000000, 11, 8 ' speed is 5MHz, 8 bits data


Why do you have a mode of 11? Valid modes are 0 through 3 (0x00000011 is 3).

For your specific device, is Clock active high or low, and is data captured on the rising edge and output on the falling edge or vice versa?

I'd also wonder about the speed--how did you determine what speed you wanted?

But aside from the particular device you want to connect to, what do you want the program that you would be running to do? In other words, what does the user interface look like, and what would the output look like? Perhaps there are easier ways to achieve this than with the device you are looking at.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 4679
Posted: 09:04pm 24 Jan 2022
Copy link to clipboard 
Print this post

Lew,
there are a number of things you need to know if you want to use the board you have.
You know that it uses SPI to communicate with the ESP module. That's the easy part.

Does the ESP use the AT command set or something different?
If it does use the AT command set, does the SPI version require the CRLF end of line to initiate commands. Probably Yes.

It is feasible that the AT commands are being use through SPI but it could just as easily be something totally unique.
If you have access a working firmware in any language (I believe you have that) and access to the SPI pins, you could put a logic analyzer on it and do some capturing of working communications. The AT commands should be obvious.

Even it does have the AT commands, there are lots of different interpretations of them so life will still be 'interesting'

If it doesn't use the AT command set, you will have to do a lot of analyzing.

You may also be able to workout when the handshake pin changes and which end is doing the changing.
In AT modem language, it may be a 'ring indicator' or similar, or perhaps 'data available'

The SPI is the easy part.

Jim
VK7JH
MMedit   MMBasic Help
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 1293
Posted: 09:25pm 24 Jan 2022
Copy link to clipboard 
Print this post

When espressiv makes the firmware for the wifi chip, there may be some info about the spi protocol on their site.

But regardless, lew, you are on a quest that far exceeds the time you would have to invest learning python.

Volhout
If nothing goes right ... turn left
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 2185
Posted: 09:31pm 24 Jan 2022
Copy link to clipboard 
Print this post

Actually, I hadn't thought of that...
The AT command set is a way for humans to write to registers. Using SPI there's no reason for humans to be involved at all. It's probably more likely that you write (and read) the registers just as you would for a SPI display, for example. Attempting to send AT commands may well be just a waste of time. A lot more info is needed.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 1987
Posted: 10:11pm 24 Jan 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  Actually, I hadn't thought of that...
The AT command set is a way for humans to write to registers. Using SPI there's no reason for humans to be involved at all. It's probably more likely that you write (and read) the registers just as you would for a SPI display, for example. Attempting to send AT commands may well be just a waste of time. A lot more info is needed.


we need to know/find EXACTLY two things:

(1) is this the board/module Lewis is dealing with:
https://wiki.seeedstudio.com/Wio_RP2040_mini_Dev_Board-Onboard_Wifi/
https://wiki.seeedstudio.com/Wio_RP2040_Module_Build-in_Wireless_2.4G/

if so,
(2) we need the schematic for the module - this will document the connections between the RP2040 and ESP8285. this in itself is sufficient information to access and use the module. without knowing how the two chips are connected together, we can do nothing.

my GUESS is that an SPI connection is used to allow the RP2040 to load firmware into the ESP8285's flash. thereafter, the same pins are reconfigured as TTL serial (RxD, TxD), allowing normal communications of AT commands and any data to send/receive.


cheers,
rob   :-)
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 8081
Posted: 10:37pm 24 Jan 2022
Copy link to clipboard 
Print this post

I'm curious why you are using the separate bulk WRITE and READ commands, and not the combined single-line command that does both at the same time.

  Manual page 92 (Appendix D) said  When the SPI channel is open data can be sent and received using the SPI function. The syntax is:
received_data = SPI(data_to_send)
Note that a single SPI transaction will send data while simultaneously receiving data from the slave.
'data_to_send' is the data to send and the function will return the data received during the transaction.
'data_to_send' can be an integer or a floating point variable or a constant.
If you do not want to send any data (ie, you wish to receive only) any number (eg, zero) can be used for the
data to send. Similarly if you do not want to use the data received it can be assigned to a variable and ignored.


Using your example from the first post, you would:

RXD$=SPI("AT+GMR")

That will send the AT+GMR command, and save any reply in RXD$ automatically, all in one simple command.

If you needed to add CR(or LF or both) to the command, you would:

RXD$=SPI("AT+GMR"+CHR$(13))

...for example.

With SPI WRITE, any data returned from the slave device is ignored. (see page 93)
With SPI READ, zeros are sent as commands while reading from the slave.

This might be totally mucking up your attempts to do what you are doing.

The single command that BOTH read AND write in the same transaction would seem to me to be the more simple approach, but perhaps I am missing something.
Smoke makes things work. When the smoke gets out, it stops!
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 1997
Posted: 11:32pm 24 Jan 2022
Copy link to clipboard 
Print this post

  Grogster said  The single command that BOTH read AND write in the same transaction would seem to me to be the more simple approach, but perhaps I am missing something.

I think this is unlikely to work in this circumstance, since the receiving device must get the whole command, "AT+GMR" (and possibly CR & LF) before it can know what it is supposed to reply with.

Part of the problem as I understand it is that +if+ the device responds to an "AT+GMR", the master has no way of knowing how many bytes the device is going to respond with, so it must keep clocking out NULLs and gathering what returns until it recognizes that the sender is finished.

Without documentation, this seems like a pig in a poke.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 8081
Posted: 06:19am 25 Jan 2022
Copy link to clipboard 
Print this post

Oh, OK.
Smoke makes things work. When the smoke gets out, it stops!
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 2185
Posted: 07:45am 25 Jan 2022
Copy link to clipboard 
Print this post

It really is a hell of a lot easier to use COM ports and AT commands. You can just have a big RX buffer and hope. :)  Yes, I know parsing is a pain, but compared to SPI without documentation that's easy.

Seeed give nothing away on their site, do they? No data sheet, no links to reference material, no help apart from a pinout diagram. I assume everything is discussed (in Python - and possibly in Chinese) on a forum somewhere. I seriously wouldn't bother with them. If they aren't going to support it I don't want it.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 2765
Posted: 08:49am 25 Jan 2022
Copy link to clipboard 
Print this post

There's the code posted.

At a quick glance it looks like it uses states rather than AT commands.

Where the states are doc'ed - pass!!

John
 
     Page 1 of 2    
Print this page
© JAQ Software 2022