bit banging spi, dont think im getting anywhere

tony_g

Senior Member
lately i have decided to get around to playing with some wireless modules i have had for a while, they are spi interface and as i only have M2 series picaxe to play with i have to use the bit banging method to try and get these working.

the modules have been used before by goeytex and with thorough write ups and info provided for register settings ect however that was with X2 chips and i have been trying to get a working bit banging test done to just read the status of the devices or the different registers to verify i am getting back data that should show me the default values of the different registers.

Code:
[color=Green]; *******************************************
; ***** INHAOS RF2400 Transmit / Receive ****
; *******************************************[/color]

[color=Black]DIRECTIVES: 
  [/color][color=Navy]#picaxe [/color][color=Black]14m2
  [/color][color=Navy]#com 6
  #terminal 19200[/color]

[color=Black]SYMBOLS:[/color]
[color=Green];===========================================================================================
;============================ 14m2 pin assignments =======================================
;===========================================================================================
  [/color][color=Blue]symbol sclk [/color][color=DarkCyan]= [/color][color=Blue]b.1           [/color][color=Green]' clock (output pin)
  [/color][color=Blue]symbol mosi [/color][color=DarkCyan]= [/color][color=Blue]b.2             [/color][color=Green]' data (output pin for shiftout)
  [/color][color=Blue]symbol [/color][color=Purple]miso [/color][color=DarkCyan]= [/color][color=Purple]pinb.3          [/color][color=Green]' data (input pin for shiftin)
  [/color][color=Blue]symbol csn [/color][color=DarkCyan]= [/color][color=Blue]b.4              [/color][color=Green]' spi chip select (output, active low)
  [/color][color=Blue]symbol ce [/color][color=DarkCyan]= [/color][color=Blue]b.5               [/color][color=Green]' chip enable (output, 1=TX, 0=standby 1)


;===========================================================================================
;============================ bit bang spi symbols/variables ===============================
;===========================================================================================
  [/color][color=Blue]symbol [/color][color=Purple]counter [/color][color=DarkCyan]= [/color][color=Purple]b7           [/color][color=Green]' variable used during loop
  [/color][color=Blue]symbol [/color][color=Purple]mask [/color][color=DarkCyan]= [/color][color=Purple]b8              [/color][color=Green]' bit masking variable
  [/color][color=Blue]symbol [/color][color=Purple]var_in [/color][color=DarkCyan]= [/color][color=Purple]b9            [/color][color=Green]' data variable used durig shiftin
  [/color][color=Blue]symbol [/color][color=Purple]var_out [/color][color=DarkCyan]= [/color][color=Purple]b10          [/color][color=Green]' data variable used during shiftout

  [/color][color=Blue]symbol bits [/color][color=DarkCyan]= [/color][color=Navy]8               [/color][color=Green]' number of bits
  [/color][color=Blue]symbol MSBvalue [/color][color=DarkCyan]= [/color][color=Navy]128         [/color][color=Green]' MSBvalue (=128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)


  [/color][color=Blue]symbol Config [/color][color=DarkCyan]= [/color][color=Navy]$00           [/color][color=Green]'Reg_address of Config Register                      
  [/color][color=Blue]symbol Status [/color][color=DarkCyan]= [/color][color=Navy]$07           [/color][color=Green]'Reg_address of Status Register 
  [/color][color=Blue]symbol NOP [/color][color=DarkCyan]= [/color][color=Navy]255              [/color][color=Green]'no operation command, also used to get status/config register data


  [/color][color=Blue]symbol [/color][color=Purple]data1 [/color][color=DarkCyan]= [/color][color=Purple]b0


  [/color][color=Blue]let [/color][color=Purple]dirsb [/color][color=DarkCyan]= [/color][color=Navy]%00110111
  [/color][color=Blue]let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%00000110

  [/color][color=Blue]high csn      
  low ce

  pause [/color][color=Navy]3000
  
  [/color][color=Blue]setfreq m8[/color]

[color=Black]main:[/color]


[color=Blue]do
      pause [/color][color=Navy]1[/color]
[color=Blue]loop until [/color][color=Purple]pinc.3[/color][color=DarkCyan]=[/color][color=Navy]1[/color]

[color=Blue]pause [/color][color=Navy]4000[/color]


[color=Blue]let [/color][color=Purple]var_out [/color][color=DarkCyan]= [/color][color=Navy]%00000001    [/color][color=Green]'read bank 0, address 1 location,default value should be %00111111[/color]
[color=Blue]gosub [/color][color=Black]shiftout_MSBFirst[/color]


[color=Blue]gosub [/color][color=Black]shiftin_MSB_Pre[/color]
[color=Blue]let [/color][color=Purple]data1 [/color][color=DarkCyan]= [/color][color=Purple]var_in[/color]


[color=Blue]sertxd ([/color][color=Red]"Status  = "[/color][color=Black], #[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Black],[/color][color=Blue]CR[/color][color=Black],[/color][color=Blue]LF)


goto [/color][color=Black]main[/color]



[color=Green];===========================================================================================
;=========================== bit bang spi shiftout msb first ===============================
;===========================================================================================[/color]

[color=Black]shiftout_MSBFirst:

      [/color][color=Blue]low csn

      for [/color][color=Purple]counter [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]to bits             [/color][color=Green]' number of bits
            [/color][color=Purple]mask [/color][color=DarkCyan]= [/color][color=Purple]var_out [/color][color=DarkCyan]& [/color][color=Blue]MSBValue     [/color][color=Green]' mask MSB 
            [/color][color=Blue]high mosi                [/color][color=Green]' data high
            [/color][color=Blue]if [/color][color=Purple]mask [/color][color=DarkCyan]= [/color][color=Blue]MSBValue then [/color][color=Black]skipMSB     
            [/color][color=Blue]low mosi                 [/color][color=Green]' data low[/color]
[color=Black]skipMSB:
            [/color][color=Blue]pulsout sclk[/color][color=Black],[/color][color=Navy]1          [/color][color=Green]' pulse clock 
            [/color][color=Purple]var_out [/color][color=DarkCyan]= [/color][color=Purple]var_out [/color][color=DarkCyan]* [/color][color=Navy]2         [/color][color=Green]' shift variable left for MSB 
      [/color][color=Blue]next [/color][color=Purple]counter

      [/color][color=Blue]high csn

      return[/color]

[color=Green];===========================================================================================
;=========================== bit bang spi shiftin msb pre clock ===============================
;===========================================================================================[/color]

[color=Black]shiftin_MSB_Pre:[/color]

[color=Blue]let [/color][color=Purple]var_in [/color][color=DarkCyan]= [/color][color=Navy]0

      [/color][color=Blue]low csn

      for [/color][color=Purple]counter [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]to bits             [/color][color=Green]' number of bits
            [/color][color=Purple]var_in [/color][color=DarkCyan]= [/color][color=Purple]var_in [/color][color=DarkCyan]* [/color][color=Navy]2           [/color][color=Green]' shift left as MSB first
            [/color][color=Blue]if [/color][color=Purple]miso [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then [/color][color=Black]skipMSBPre
            [/color][color=Purple]var_in [/color][color=DarkCyan]= [/color][color=Purple]var_in [/color][color=DarkCyan]+ [/color][color=Navy]1           [/color][color=Green]' set LSB if serdata = 1[/color]
[color=Black]skipMSBPre:
            [/color][color=Blue]pulsout sclk[/color][color=Black],[/color][color=Navy]1          [/color][color=Green]' pulse clock to get next data bit
      [/color][color=Blue]next [/color][color=Purple]counter

      [/color][color=Blue]high csn

      return[/color]
regardless of what i change in this code all i ever get back in B0 is %00001110, even if i try to read different registers nothing changes even with trying different setfreq speeds or looking at the miso line from module back to picaxe with a logic analyzer shows no data.

i have tried the different bit banged modes for shiftin relating to MSB first as the datasheet states but am still getting nowhere.

the datasheet is too large and wont let me attach it but links for it are provided in the original post by goeytex and others, its the BK2421 i.c

http://www.picaxeforum.co.uk/showthread.php?19036-Using-INHAOS-RF2400-2-4-gHz-RF-Modules


as this is my first time with bit banging and spi im sure their is something im missing even to just read the registers default values from power on but i dont know where to start

rf nano no luck.png
 
Last edited:

Goeytex

Senior Member
That MISO (Master In Slave Out) signal does not look quite right. Seems to be some high frequency oscillation going on there. It seems to follow the CS signal i.e, when CS is high the MISO signal oscillates?

Things to check for.

Supply voltage - must be between 1.9 and 3.6V. (5v will destroy these chips)
Bypass cap close to Nano supply pins
Solder bridges on nano pads.
MISO Connection from Picaxe to Nano
 
Last edited:

tony_g

Senior Member
i swapped out the 14m2 for a 20m2 just incase the chip has suffered from years of abuse but still similar results with a barely used and not abused 20m2 lol

20m2 config read.png
 

tony_g

Senior Member
the modules i have are the "nano" version which meant i had to etch some smd to dip adapters for breadboard use so i will reflow and remove them from the adapter boards and check to see if there is any traces of solder paste that didnt completely reflow under the pads and possibly be causing problems.

the voltage is running 3.3v for both the module and 20m2, shamefully i didnt add the bypass to the nano so i will do that and replace the wire jumper for the miso line just incase that is causing issues as i have had dodgy breadboard jumper wires cause issues in the past.

ill report back when all the above has been checked and verified good.
 

tony_g

Senior Member
well after checking all above it still gives the same results visible on the logic analyzer and miso line and no luck just reading the status of any of the registers to verify good/correct data is coming back.


power for both is good, bypass/decoupling caps for module and 20m2 in place, no bridged/poor solder joints between connections and new wire replaced on miso line. hmm stumped.
 

Goeytex

Senior Member
Tony,

If you can wait a day or two, I will get out my bag of RF modules, slap a 2400 on a breadboard, hook up the logic analyzer, and then see what happens with your code. I did at one time have an RF2400 working with bit-banged SPI, but I think I deleted/overwrote the code after I moved to a 20X2 with HSPI. (The bit-banged SPI code was agonizingly slow)
 

tony_g

Senior Member
that would be great bill,

im not really bothered about speed with the comms but i think i should add a few X2 chips to my collection lol.

thanks for the help with this.


tony
 

Goeytex

Senior Member
Tony,

Westaust55 posted some code HERE for a Dorji RF module. His code uses bit-banged SPI on an 18M2.

You might want to check out his software SPI routines and compare them to what you are doing. I know his SPI routines worked because I tried them out.

I have found the 20X2 to be ideal for interfacing to various RF modules via HSPI.
 

tony_g

Senior Member
thanks bill, i will have a good read over them.

i was hoping to get these working with bit banging as even the surface mount 20x2 was too big size wise for some of the projects i wanted to use these nano's in which would have involved a 14m2 soic.

i shall continue to keep trying and see if i can get any further along with them.

thanks,

tony
 

hippy

Technical Support
Staff member
That MISO (Master In Slave Out) signal does not look quite right. Seems to be some high frequency oscillation going on there. It seems to follow the CS signal i.e, when CS is high the MISO signal oscillates?
Not familiar with the device myself but it could be that the MISO signal goes open-circuit when not chip selected, leaving that signal line floating. It does seem to be quite a high frequency signal unless the analyser shows floating as a solid block.

Adding a 10K pull-down might improve things, but that doesn't seem to be the actual problem as one can see data being clocked out. Could it be that this is a static value because that is what's meant to be sent; perhaps a status byte and it hasn't / doesn't think it's been told to send something else ?
 

AllyCat

Senior Member
Hi,

it could be that the MISO signal goes open-circuit when not chip selected,.
I'm certainly no expert on SPI, but isn't that virtually a requirement for a multi-slave system?

It does seem to be quite a high frequency signal unless the analyser shows floating as a solid block.
That could be quite a "nice" feature of a logic analyser and perhaps not difficult to do* (but I would a prefer a "grey" block to distinguish from an actual high frequency oscillation). The analyser could use two detectors (comparators) corresponding to the "min" and "max" upper and lower digital thresholds and then apply a high impedance bias voltage somewhere in the middle.

Using SPI (e.g. with transceivers) is very much on my (sadly very long) "to do" list, so I'm following this thread with interest. But on srnet's timing figures, the peeksfr/pokesfr solution must be the way to go (with M2s), unless you need to support both SPI and I2C busses at the same time. The bit-banging code could perhaps be speeded-up by a factor of 2, but that still leaves the peeksfr/pokesfr about 10 times faster. Working out the SFR data values from the "base" Microchip data sheet can be rather tedious, but the eventual results can be very rewarding.

Cheers, Alan.

*EDIT: Simply apply a high impedance ac signal at half the sampling frequency to each loigic input signal and get the function almost "for free".
 
Last edited:

hippy

Technical Support
Staff member
Ignoring the speed of bit-banged SPI it should still work even if it proves problematic in the longer run. I would have started by software bit-banging before moving on to hardware HSPI.

@ AllyCat : Good point that it must go tri-state / open circuit for multiple devices to work ! I'm so used to just controlling one chip at a time I forgot CS is more than just a 'start of command signal', but that got me thinking ...

Looking at the datasheet ( tony_g's link in post #2 ) it shows CS being asserted during the full command, not for each byte written or read, which is what I guess is the problem here, why the data is always the same, status as I suspected. Though oddly that datasheet does not seem to indicate what the status register bits are!

So try removing the "LOW csn" and "HIGH csn" commands from the "shiftout_MSBFirst" and "shiftin_MSB_Pre" subroutines and put the "LOW csn" before sending the command and the "HIGH csn" after reading the reply.
 

tony_g

Senior Member
well after finally dragging myself out of bed thanks to my kids spreading their winter germs to all in the house i shall give it another go.

i have not bit banged spi before so its definately something new to understand so i will write up the test code again as i didnt save it last night when i closed pe6 and make the changes you mentioned hippy and see how it goes from their.

for the time being ill just be happy to get some data back for the status of the registers to show its working, according to datasheet their are several commands that will cause the modules to send back the current state of which ever register im wanting to know the status of.

one of them was %000AAAAA, the A being for which register to check so i guess from the datasheet that sending %00000001 would send me back the status of the bits at register address $00 which at powerup should be %00001000, thats assuming my understanding of that is correct lol.

i shall load up on caffiene and sugar and have another go and see where i get today.

@stuart thanks for that info, ill have a read and try to understand more about that, always good to know there are plenty of ways to skin a cat lol



tony
 

tony_g

Senior Member
20m2 reg01.png

hopefully this is a good sign but i finally got the data for one command i was expecting to see.

hippy i put a 10k pulldown on the miso line and cleared up the noise and then moved my chip select commands to the main portion of the code as you suggested with them before the shiftout gosub and after the return from the shiftin gosub.

the command sent was the read register command from page 12 at the start of the list 000A AAAA, i sent %00000001 which i presume was read status of reg address $01 which would be the status of the "enable auto ack function", the default value of this register should be %00111111 and low and behold the screenshot


now before i get too excited i will read some of the other registers to verify the returned data and post back.

thanks for the suggestions hippy, and also to bill and stuart for your info, i will let you know how it goes shortly.


tony
 

tony_g

Senior Member
reading register 02, default values are %00000011 and logic says..........
20m2 reg02.png

now i just have to start looking at tidying things up and maybe a short pause before the gosub as my logic keeps saying the cs leading edge is missing, it doesnt do it all the time so maybe upping the sampling rate but for now im seeing the data i expect to which is a good start :D
 

Goeytex

Senior Member
I hooked up an RF2400P to a 20X2 using the code I posted in finished projects. It works fine with HSPI. Then I looked up the bit-banging code in Manual 2 and after reading through it I asked myself, why in the world would anyone want to do this when a 20X2 chip only costs about $6.00 (US)?

Tony, since you seem to have it somewhat working with bit-banging, I will leave you to your own devices. I am not too keen on rewriting code for bit-banging. However if you have problems with HSPI to the RF2400, I'll be glad to help out.
 

Buzby

Senior Member
.... Then I looked up the bit-banging code in Manual 2 and after reading through it I asked myself, why in the world would anyone want to do this when a 20X2 chip only costs about $6.00 (US)? .....
Because sometimes HSPI does not do what the device wants, such as the idle condition.
Sometimes because you only need to send, but HSPI always needs to have the 'in' pin at a certain level, so you lose a pin.
Sometimes it's so much easier to debug bit-bang, whereas you need a logic analyser to debug HSPI.

I use both, each has it's pros and cons.

Cheers,

Buzby
 

tony_g

Senior Member
I hooked up an RF2400P to a 20X2 using the code I posted in finished projects. It works fine with HSPI. Then I looked up the bit-banging code in Manual 2 and after reading through it I asked myself, why in the world would anyone want to do this when a 20X2 chip only costs about $6.00 (US)?

Tony, since you seem to have it somewhat working with bit-banging, I will leave you to your own devices. I am not too keen on rewriting code for bit-banging. However if you have problems with HSPI to the RF2400, I'll be glad to help out.

thanks bill, i now have it sending and receiving and displaying the incrementing packets.

i understand your point with using the X2 chips for it but the only issue for me was more to do with the size of the available smt chips, although i have been tempted at times to buy one of those 28x2 modules to reflow and remove the 28 qfn chip lol, if size limits where not an issue a 2ox2 soic would have been good but seeing as i have an unhealthy addiction with making smt boards and trying to get smaller and smaller it would have been too big with the current smt X2 line up lol.

thanks for those writeups and code you posted, it definately helps, just my missing the occasional thing from the datasheet that slowed down progress lol.

even when bit banging the speed is not an issue for me as im not consistently pumping large and fast chunks of data but finding something that small and with the features it has and price when i bought them makes them better for my needs than some of the "dumb" modules.



tony
 

srnet

Senior Member
i understand your point with using the X2 chips for it but the only issue for me was more to do with the size of the available smt chips, although i have been tempted at times to buy one of those 28x2 modules to reflow and remove the 28 qfn chip lol,
One of the things promised to go with PE6 is the ability to take a PICAXE program and turn it into a PIC Hex file, thus you could program up your PICAXE program on the smaller device.

However, whilst I have used a 18F26K22 in a QFN in one design, the PCB can get very congested, a TSSOP design could end up smaller.
 

tony_g

Senior Member
thats one feature i will be eagerly awaiting when its finally released, i have recently ordered a pickit 3 programmer and in between my picaxe projects learning to use the mplab x ide and creating some simple programs to test and get used to using C and mixing in some assembly in with it too, although converting from picaxe basic to pic hex would certainly speed up migrating code from picaxe to pic

i have been looking at the different package sizes available if using the raw native pic and i think i will have to look at going down a few sizes in smt components to maybe 0402 lol.

i dont know why im obsessed with trying to micro size everything i do but i think i need help, my excuse is saving space in my rc models lol :p
 

Goeytex

Senior Member
I understand making things as small as possible when the application demands it. However, small just for the sake of being small could be an addiction. I'm not sure if this is curable. or even treatable. I feel for you.

Need to go now, I am late for my weekly meeting at the local Alien Abductee Support Group.
 

tony_g

Senior Member
i have given up trying to curb my addiction, now i just enjoy the strange satisfaction and joy it gives me, even when the wife doesnt understand my ear to ear grin when i make something smaller than before lol, yes i think i have a problem hahaha :rolleyes:
 

inglewoodpete

Senior Member
i have given up trying to curb my addiction, now i just enjoy the strange satisfaction and joy it gives me, even when the wife doesnt understand my ear to ear grin when i make something smaller than before lol, yes i think i have a problem hahaha :rolleyes:
Yes technology is one of very few areas where grown men strive to have the smallest.
 
Top