Pages: [1]   Go Down
Author Topic: SPI chip select pin on shift register, will it work in sd card libs?  (Read 774 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have not tried any SPI devices yet so here is a basic question smiley-red

If I want to use SD card library (there are several on playground), can I toggle the CS pin on a shift register?

As I understand, if you are only talking to one chip, you just keep the CS LOW, right?

Say if I have two SPI devices and two separate libs, do you think they will often need to access CS line in the code? Can I manually set the CS and then call the respective lib? If that is the case, using shift register would not be good choice then. Thanks!
Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Each device needs its own CS line -that is how the slaves know to clock data in or to send data out. It generally must be high when not in use to a device.
You can probably manually set it, but the lib will likely take it over and toggle it anyway in accordance with the SPI requirements

What happens to the shift register when you toggle the line while talking to the SD card?

I am thinking as long as you are not doing spi.transfer with CS low the SD card does not see the clock so that may be okay.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!

So in this particular lib (http://arduino.cc/playground/Code/SDCARD) here is a command in sdc_cmd :

PORTB &= ~(1<<PIN_CS);   // assert chip select for the card

I've never used SPI so just checking my understanding here:

The library actually pulls CS to LOW before sending a command. Is this a typical way?

The only other place the library used CS line was in sdc_initialization(), where it deasserts the chip and then asserts it.

PORTB |=  (1<<PIN_CS);          // deasserts card for warmup
...
PORTB &= ~(1<<PIN_CS);          // assert chip select for the card


What I don't quite understand is why it asserts the card in sdc_cmd(). Since init gets called before other calls, it has asserted the chip, why would sdc_cmd() pull the CS again? Unnecessary?

BTW, we just finished bidding/booking all the hotels. Seems we'll be in West metro for a few nights and then scatter all across MA. I'll PM you my schedule to see if we can meet up.

Logged


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You can probably manually set it, but the lib will likely take it over and toggle it anyway in accordance with the SPI requirements

In context with the arduino spi master library, the user code has to directly control the SS pins at all times, activating a low on the desired SS pin before performing any SPI library commands and setting it high when the desired spi commands are completed. The master library does not have knowledge of what pin(s) you are using for remote SS selection outputs, nor does it manipulate them directly in the SPI master library.
« Last Edit: July 01, 2011, 10:38:56 am by retrolefty » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Lefty, I suppose I can always make small changes to contributed libraries when it comes to toggling the line.
Logged


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8684
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I like to think of an SPI device as two shift registers: a parallel to serial for input and serial to parallel for output.  Pulling SS low enables the shift registers and that falling edge latches the inputs in the input register.  Then 8 clocks shift data in from the input register and out to the output register.  The rising edge of SS latches the output register.  If the registers are 16 bits instead of 8 you do another 8-bit transaction before raising SS.

I'd be surprised if the SD library truly doesn't raise the SS pin after a data transfer.

BTW, we just finished bidding/booking all the hotels. Seems we'll be in West metro for a few nights and then scatter all across MA. I'll PM you my schedule to see if we can meet up.

If you are having a MetroWest Boston meet-up maybe I can join in. smiley
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Couldn't spi.end be called also to free the SS pin for his other uses, then spi.begin called again?

SS is only called low at the start of a series of transfers, then high againat the end, so you analogy isn't quite correct.
At least it works that way for the maxim LED displayu driver chips I have used.

I am off all next week, how flexible is your schedule John? Was thinking to drive in to Boston or something to meet up with liudr for lunch or something.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8684
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am off all next week, how flexible is your schedule John? Was thinking to drive in to Boston or something to meet up with liudr for lunch or something.

My schedule is almost wide open.  Tuesday I'm going down to Randolph to work on the balloon stuff but the rest of the week is open.  You should set up a meet convenient to you and let me know when and where.  I'll let you know if I can be there.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, PM me and we can trade phone #s.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: