Writing to absolute sectors on SD Card

Chris Johns chrisj at rtems.org
Thu Oct 23 23:53:36 UTC 2008


Robert S. Grimes wrote:
> Is it possible to write to absolute sectors using the spi-sd-card driver?

Yes but you are on your own with how this interacts with the cache. I wonder 
why the SD card is implemented this way. I would have thought the 
rtems_blkdev_generic_* calls such as rtems_blkdev_generic_write was the way to 
do this. This removes the need for sd_card_driver_read and 
sd_card_driver_write. Maybe Thomas could help here.

The sd_card_driver_ops structure has a NULL for the open. This seems to work 
as rtems_io_open takes an open entry of NULL as ok. I am not sure why this is 
so. Maybe a low level open is not always needed so this saves space. Anyway 
this is why the open returns no error. You will have to debug the 
sd_card_driver_write code to see why the write operates returns the results it 
does.

FWIW it may pay to change device_lseek to update iop->offset so you can seek 
around the card. Please send me the patch if you do make the change.

To clarify the cache comment above. If you have mounted the card then the 
cache could have blocks it is writing too so you need to make sure the cache 
has flushed all data to the disk before using this code. I do not see any 
protection between the different ways the SD card could be accessed. This 
means you need to careful how to use these functions and the disk cache and 
the file system to make sure it all works as you expect.

Regards
Chris

> 
> Here is my approach, in rough terms:
> 
>   // Open the SD card device
>   int raw = open("/dev/sdcarda", O_WRONLY);
>   printf("Open device returned %d\n", raw);
>   if (raw > 0) {
>     // Get some data into buffer
>     uint8_t buffer[512];
>     for (int i = 0; i < 512; ++i) {
>       buffer[i] = (uint8_t)i;
>     }
> 
>     // Write it to the first sector
>     unsigned block = 0;
>     int numWritten = 1;
>     numWritten = write(raw, buffer, 512);
>     if (numWritten != 512) {
>       printf("NumWritten = %d\n", numWritten);
>       perror("Aborting");
>     }
> 
>     // Close the file
>     close(raw);
>   }
>      
> I would expect this to either work, or perror would print some error; 
> however, neither event occurs; instead, this is what I get:
> 
>     Open device returned 4
>     NumWritten = 0
>     Aborting:
> 
> 
> So nothing was written ("NumWritten = 0"), and no error occurred (no 
> error after "Aborting:")!  Any ideas what I've done wrong here?
> 
> Thanks,
> -Bob
> _______________________________________________
> rtems-users mailing list
> rtems-users at rtems.com
> http://rtems.rtems.org/mailman/listinfo/rtems-users



More information about the users mailing list