SPI chip select pin on shift register, will it work in sd card libs?

I have not tried any SPI devices yet so here is a basic question :blush:

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!

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.

Thanks!

So in this particular lib (Arduino Playground - 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.

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.

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

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.

liudr:
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. :slight_smile:

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.

CrossRoads:
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.

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