[543] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : sdc_driver.c |
---|
| 3 | // Date : 31/08/2012 |
---|
| 4 | // Author : cesar fuguet |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | |
---|
[550] | 8 | // #include <hard_config.h> |
---|
[543] | 9 | #include <soclib_sdc.h> |
---|
| 10 | //#include <tty0.h> |
---|
| 11 | //#include <utils.h> |
---|
[550] | 12 | #include <hal_kernel_types.h> |
---|
| 13 | #include <chdev.h> |
---|
| 14 | #include <dev_ioc.h> |
---|
| 15 | #include <thread.h> |
---|
[543] | 16 | #include <printk.h> |
---|
| 17 | |
---|
| 18 | #define SYSCLK_FREQ (RESET_SYSTEM_CLK * 1000) |
---|
| 19 | #define SDCARD_RESET_ITER_MAX 4 |
---|
| 20 | |
---|
[558] | 21 | extern chdev_directory_t chdev_dir; // allocated in the kernel_init.c file. |
---|
| 22 | |
---|
[543] | 23 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 24 | // Global variables |
---|
| 25 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 26 | |
---|
| 27 | __attribute__((section(".kdata"))) |
---|
[558] | 28 | static struct sdcard_dev global_sdcard; |
---|
[543] | 29 | |
---|
| 30 | __attribute__((section(".kdata"))) |
---|
| 31 | static struct spi_dev* spi; |
---|
| 32 | |
---|
| 33 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 34 | // This function enables SD Card select signal |
---|
| 35 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 36 | static void _sdc_enable(sdcard_dev_t *sdcard) |
---|
[543] | 37 | { |
---|
[558] | 38 | spi_ss_assert(sdcard->spi, sdcard->slave_id); |
---|
[543] | 39 | } |
---|
| 40 | |
---|
| 41 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 42 | // This function disables SD Card select signal |
---|
| 43 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 44 | static void _sdc_disable(sdcard_dev_t *sdcard) |
---|
[543] | 45 | { |
---|
[558] | 46 | spi_ss_deassert(sdcard->spi, sdcard->slave_id); |
---|
[543] | 47 | } |
---|
| 48 | |
---|
| 49 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 50 | // This function writes on the SPI tx register to generate SD card clock ticks |
---|
| 51 | // - tick_count: number of ticks to generate (1 tick -> 8 clocks) |
---|
| 52 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 53 | static void _sdc_gen_tick(sdcard_dev_t *sdcard, unsigned int tick_count) |
---|
[543] | 54 | { |
---|
| 55 | register int i = 0; |
---|
[558] | 56 | while(i++ < tick_count) spi_put_tx(sdcard->spi, 0xFF, 0); |
---|
[543] | 57 | } |
---|
| 58 | |
---|
| 59 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 60 | // This function changes the SD card access pointer position in terms of |
---|
| 61 | // blocks |
---|
| 62 | // - lba: number of logical block to move the pointer |
---|
| 63 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 64 | void _sdc_lseek(sdcard_dev_t *sdcard, unsigned int lba) |
---|
[543] | 65 | { |
---|
[558] | 66 | sdcard->access_pointer = sdcard->block_length * lba; |
---|
[543] | 67 | } |
---|
| 68 | |
---|
| 69 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 70 | // This function gets a byte from the SD card |
---|
| 71 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 72 | static unsigned char _sdc_receive_char(sdcard_dev_t *sdcard) |
---|
[543] | 73 | { |
---|
[558] | 74 | _sdc_gen_tick(sdcard, 1); |
---|
[543] | 75 | |
---|
[558] | 76 | return spi_get_rx(sdcard->spi, 0); |
---|
[543] | 77 | } |
---|
| 78 | |
---|
| 79 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 80 | // This function returns when a valid response from the SD card is received or |
---|
| 81 | // a timeout has been triggered |
---|
| 82 | // Returns the SD card response value |
---|
| 83 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 84 | static unsigned char _sdc_wait_response(sdcard_dev_t *sdcard) |
---|
[543] | 85 | { |
---|
| 86 | unsigned char sdcard_rsp; |
---|
| 87 | register int iter; |
---|
| 88 | |
---|
| 89 | iter = 0; |
---|
[558] | 90 | sdcard_rsp = _sdc_receive_char(sdcard); |
---|
[543] | 91 | while ( |
---|
| 92 | (iter < SDCARD_COMMAND_TIMEOUT) && |
---|
| 93 | !SDCARD_CHECK_R1_VALID(sdcard_rsp) |
---|
| 94 | ) |
---|
| 95 | { |
---|
[558] | 96 | sdcard_rsp = _sdc_receive_char(sdcard); |
---|
[543] | 97 | iter++; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | return sdcard_rsp; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 104 | // This function returns when a data block from the SD card is received (data |
---|
| 105 | // block start marker received). |
---|
| 106 | // It must be called after a read command |
---|
| 107 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 108 | static void _sdc_wait_data_block(sdcard_dev_t *sdcard) |
---|
[543] | 109 | { |
---|
[558] | 110 | while (_sdc_receive_char(sdcard) != 0xFE); |
---|
[543] | 111 | } |
---|
| 112 | |
---|
| 113 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 114 | // This function sends a command to the SD card |
---|
| 115 | // - index: CMD index |
---|
| 116 | // - app: type of command |
---|
| 117 | // - args: CMD arguments vector |
---|
| 118 | // - crc7: CRC (7 bits) to send |
---|
| 119 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 120 | static int _sdc_send_command ( sdcard_dev_t *sdcard, |
---|
| 121 | int index, |
---|
[543] | 122 | int app , |
---|
| 123 | void * args , |
---|
| 124 | unsigned crc7 ) |
---|
| 125 | { |
---|
| 126 | unsigned char sdcard_rsp; |
---|
| 127 | unsigned char * _args; |
---|
| 128 | |
---|
[558] | 129 | _sdc_gen_tick(sdcard, 5); |
---|
[543] | 130 | |
---|
| 131 | if (app == SDCARD_ACMD) |
---|
| 132 | { |
---|
[558] | 133 | spi_put_tx(sdcard->spi, 0x40 | 55 , 0 );// CMD and START bit |
---|
| 134 | spi_put_tx(sdcard->spi, 0x00 , 0 );// Argument[0] |
---|
| 135 | spi_put_tx(sdcard->spi, 0x00 , 0 );// Argument[1] |
---|
| 136 | spi_put_tx(sdcard->spi, 0x00 , 0 );// Argument[2] |
---|
| 137 | spi_put_tx(sdcard->spi, 0x00 , 0 );// Argument[3] |
---|
| 138 | spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 );// END bit |
---|
[543] | 139 | |
---|
[558] | 140 | sdcard_rsp = _sdc_wait_response(sdcard); |
---|
[543] | 141 | if (SDCARD_CHECK_R1_ERROR(sdcard_rsp)) |
---|
| 142 | { |
---|
| 143 | return sdcard_rsp; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | _args = (unsigned char *) args; |
---|
| 148 | |
---|
[558] | 149 | _sdc_gen_tick(sdcard, 1); |
---|
[543] | 150 | |
---|
[558] | 151 | spi_put_tx(sdcard->spi, 0x40 | index , 0 ); |
---|
| 152 | spi_put_tx(sdcard->spi, _args[0] , 0 ); |
---|
| 153 | spi_put_tx(sdcard->spi, _args[1] , 0 ); |
---|
| 154 | spi_put_tx(sdcard->spi, _args[2] , 0 ); |
---|
| 155 | spi_put_tx(sdcard->spi, _args[3] , 0 ); |
---|
| 156 | spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 ); |
---|
[543] | 157 | |
---|
[558] | 158 | return _sdc_wait_response(sdcard); |
---|
[543] | 159 | } |
---|
| 160 | |
---|
| 161 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 162 | // This function initializes the SD card (reset procedure) |
---|
| 163 | // - channel: channel index (only channel 0 is supported) |
---|
| 164 | // Returns 0 if success, other value if failure |
---|
| 165 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 166 | static int _sdc_open( sdcard_dev_t *sdcard, xptr_t base, unsigned int channel ) |
---|
[543] | 167 | { |
---|
| 168 | unsigned char args[4]; |
---|
| 169 | unsigned char sdcard_rsp; |
---|
| 170 | unsigned int iter, ersp; |
---|
| 171 | |
---|
[558] | 172 | sdcard->spi = base; |
---|
| 173 | sdcard->slave_id = channel; |
---|
[543] | 174 | |
---|
| 175 | // supply SD card ramp up time (min 74 cycles) |
---|
[558] | 176 | _sdc_gen_tick(sdcard, 10); |
---|
[543] | 177 | |
---|
| 178 | // Assert slave select signal |
---|
| 179 | // Send CMD0 (Reset Command) |
---|
| 180 | // Deassert slave select signal |
---|
[558] | 181 | _sdc_enable(sdcard); |
---|
[543] | 182 | |
---|
| 183 | args[0] = 0; |
---|
| 184 | args[1] = 0; |
---|
| 185 | args[2] = 0; |
---|
| 186 | args[3] = 0; |
---|
[558] | 187 | sdcard_rsp = _sdc_send_command(sdcard, 0, SDCARD_CMD, args, 0x4A); |
---|
[543] | 188 | |
---|
| 189 | if ( sdcard_rsp != 0x01 ) |
---|
| 190 | { |
---|
| 191 | printk("[SDC ERROR] card CMD0 failed\n"); |
---|
| 192 | return sdcard_rsp; |
---|
| 193 | } |
---|
| 194 | |
---|
[558] | 195 | _sdc_disable(sdcard); |
---|
[543] | 196 | |
---|
| 197 | // send CMD8. If card is pre-v2, It will reply with illegal command. |
---|
| 198 | // Otherwise we announce sdhc support. |
---|
[558] | 199 | _sdc_enable(sdcard); |
---|
[543] | 200 | args[0] = 0; |
---|
| 201 | args[1] = 0; |
---|
| 202 | args[2] = 0x01; |
---|
| 203 | args[3] = 0x01; |
---|
[558] | 204 | sdcard_rsp = _sdc_send_command(sdcard, 8, SDCARD_CMD, args, 0x63); |
---|
[543] | 205 | if (!SDCARD_CHECK_R1_VALID(sdcard_rsp)) |
---|
| 206 | { |
---|
| 207 | printk("[SDC ERROR] card CMD8 failed\n"); |
---|
| 208 | return sdcard_rsp; |
---|
| 209 | } |
---|
| 210 | if (!SDCARD_CHECK_R1_ERROR(sdcard_rsp)) |
---|
| 211 | { |
---|
| 212 | // no error, command accepted. get whole reply |
---|
[558] | 213 | ersp = _sdc_receive_char(sdcard); |
---|
| 214 | ersp = (ersp << 8) | _sdc_receive_char(sdcard); |
---|
| 215 | ersp = (ersp << 8) | _sdc_receive_char(sdcard); |
---|
| 216 | ersp = (ersp << 8) | _sdc_receive_char(sdcard); |
---|
[543] | 217 | if ((ersp & 0xffff) != 0x0101) |
---|
| 218 | { |
---|
| 219 | // voltage mismatch |
---|
| 220 | printk("[SDC ERROR] card CMD8 mismatch : ersp = %x\n"); |
---|
| 221 | return sdcard_rsp; |
---|
| 222 | } |
---|
| 223 | printk("[SDC WARNING] v2 or later "); |
---|
[558] | 224 | sdcard->sdhc = 1; |
---|
[543] | 225 | } |
---|
| 226 | else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0) |
---|
| 227 | { |
---|
| 228 | // other error |
---|
| 229 | printk("[SDC ERROR] card CMD8 error\n"); |
---|
| 230 | return sdcard_rsp; |
---|
| 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
[558] | 234 | sdcard->sdhc = 0; |
---|
[543] | 235 | } |
---|
[558] | 236 | _sdc_disable(sdcard); |
---|
[543] | 237 | |
---|
| 238 | // send CMD41, enabling the card |
---|
[558] | 239 | _sdc_enable(sdcard); |
---|
| 240 | args[0] = sdcard->sdhc ? 0x40: 0; |
---|
[543] | 241 | args[1] = 0; |
---|
| 242 | args[2] = 0; |
---|
| 243 | args[3] = 0; |
---|
| 244 | |
---|
| 245 | iter = 0; |
---|
| 246 | while( iter++ < SDCARD_COMMAND_TIMEOUT ) |
---|
| 247 | { |
---|
[558] | 248 | sdcard_rsp = _sdc_send_command(sdcard, 41, SDCARD_ACMD, args, 0x00); |
---|
[543] | 249 | if( sdcard_rsp == 0x01 ) |
---|
| 250 | { |
---|
| 251 | continue; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | break; |
---|
| 255 | } |
---|
| 256 | |
---|
[558] | 257 | _sdc_disable(sdcard); |
---|
[543] | 258 | if (sdcard_rsp) |
---|
| 259 | { |
---|
| 260 | printk("[SDC ERROR] ACMD41 failed\n"); |
---|
| 261 | return sdcard_rsp; |
---|
| 262 | } |
---|
[558] | 263 | if (sdcard->sdhc != 0) |
---|
[543] | 264 | { |
---|
| 265 | // get the card capacity to see if it's really HC |
---|
[558] | 266 | _sdc_enable(sdcard); |
---|
| 267 | args[0] = sdcard->sdhc ? 0x40: 0; |
---|
[543] | 268 | args[1] = 0; |
---|
| 269 | args[2] = 0; |
---|
| 270 | args[3] = 0; |
---|
[558] | 271 | sdcard_rsp = _sdc_send_command(sdcard, 58, SDCARD_CMD, args, 0x00); |
---|
[543] | 272 | if (sdcard_rsp) |
---|
| 273 | { |
---|
| 274 | printk("[SDC ERROR] CMD58 failed\n"); |
---|
| 275 | return sdcard_rsp; |
---|
| 276 | } |
---|
[558] | 277 | ersp = _sdc_receive_char(sdcard); |
---|
| 278 | ersp = (ersp << 8) | _sdc_receive_char(sdcard); |
---|
| 279 | ersp = (ersp << 8) | _sdc_receive_char(sdcard); |
---|
| 280 | ersp = (ersp << 8) | _sdc_receive_char(sdcard); |
---|
[543] | 281 | if (ersp & 0x40000000) |
---|
| 282 | { |
---|
| 283 | printk(" SDHC "); |
---|
| 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
[558] | 287 | sdcard->sdhc = 0; |
---|
[543] | 288 | } |
---|
[558] | 289 | _sdc_disable(sdcard); |
---|
[543] | 290 | } |
---|
| 291 | printk("card detected\n"); |
---|
| 292 | return 0; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 296 | // This function sets the block size in the SD card. |
---|
| 297 | // - len: block size in bytes (only 512 bytes supported) |
---|
| 298 | // Returns 0 if success, other value if failure |
---|
| 299 | /////////////////////////////////////////////////////////////////////////////// |
---|
[558] | 300 | static unsigned int _sdc_set_block_size(sdcard_dev_t *sdcard, unsigned int len) |
---|
[543] | 301 | { |
---|
| 302 | unsigned char args[4]; |
---|
| 303 | unsigned char sdcard_rsp; |
---|
| 304 | register int i; |
---|
| 305 | |
---|
| 306 | // For now, supported block size is 512 bytes |
---|
| 307 | if (len != 512) return 1; |
---|
| 308 | |
---|
| 309 | // When using high capacity SDCARD, the block_length is not a number of bytes |
---|
| 310 | // but a number of blocks (transfer unit) |
---|
[558] | 311 | if (sdcard->sdhc) |
---|
[543] | 312 | { |
---|
[558] | 313 | sdcard->block_length = len / 512; |
---|
[543] | 314 | return 0; |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | for (i = 0; i < 4; i++) |
---|
| 318 | { |
---|
| 319 | args[i] = (len >> (32 - (i+1)*8)) & 0xFF; |
---|
| 320 | } |
---|
| 321 | |
---|
[558] | 322 | _sdc_enable(sdcard); |
---|
[543] | 323 | |
---|
[558] | 324 | sdcard_rsp = _sdc_send_command(sdcard, 16, SDCARD_CMD, args, 0x00); |
---|
[543] | 325 | if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) |
---|
| 326 | { |
---|
[558] | 327 | _sdc_disable(sdcard); |
---|
[543] | 328 | return sdcard_rsp; |
---|
| 329 | } |
---|
| 330 | |
---|
[558] | 331 | _sdc_disable(sdcard); |
---|
[543] | 332 | |
---|
[558] | 333 | sdcard->block_length = len; |
---|
[543] | 334 | |
---|
| 335 | return 0; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 339 | // Extern functions |
---|
| 340 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 341 | |
---|
| 342 | //////////////////////// |
---|
[550] | 343 | void soclib_sdc_init( chdev_t* chdev ) |
---|
[543] | 344 | { |
---|
| 345 | // initializing the SPI controller |
---|
| 346 | _spi_init ( |
---|
[558] | 347 | chdev->base , |
---|
[543] | 348 | 200000 , /**< SPI_clk: 200 Khz */ |
---|
| 349 | SYSCLK_FREQ , /**< Sys_clk */ |
---|
| 350 | 8 , /**< Charlen: 8 */ |
---|
| 351 | SPI_TX_NEGEDGE, |
---|
| 352 | SPI_RX_POSEDGE |
---|
| 353 | ); |
---|
| 354 | |
---|
| 355 | // initializing the SD Card |
---|
| 356 | unsigned int iter = 0; |
---|
| 357 | unsigned char sdcard_rsp; |
---|
| 358 | unsigned int i; |
---|
| 359 | |
---|
| 360 | while(1) |
---|
| 361 | { |
---|
| 362 | printk("[SDC WARNING] Trying to initialize SD card...\n"); |
---|
| 363 | |
---|
[558] | 364 | sdcard_rsp = _sdc_open(&global_sdcard, chdev->base, 0 ); // only channel 0 |
---|
[543] | 365 | if (sdcard_rsp == 0) |
---|
| 366 | { |
---|
| 367 | printk("OK\n"); |
---|
| 368 | break; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | printk("KO\n"); |
---|
| 372 | |
---|
| 373 | for (i = 0; i < 1000; i++); |
---|
| 374 | |
---|
| 375 | if (++iter >= SDCARD_RESET_ITER_MAX) |
---|
| 376 | { |
---|
| 377 | assert( false, __FUNCTION__, "\n[SDC ERROR] During SD card reset / card response = %x \n", sdcard_rsp); |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | // set the block length of the SD Card |
---|
[558] | 382 | sdcard_rsp = _sdc_set_block_size(&global_sdcard, 512); |
---|
[543] | 383 | if (sdcard_rsp) |
---|
| 384 | { |
---|
| 385 | assert( false, __FUNCTION__, "[SDC ERROR] During SD card block size initialization\n"); |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | // incrementing SDCARD clock frequency for normal function |
---|
| 389 | _spi_init ( |
---|
[558] | 390 | chdev->base , |
---|
[543] | 391 | 10000000 , // SPI_clk 10 Mhz |
---|
| 392 | SYSCLK_FREQ , // Sys_clk |
---|
| 393 | -1 , // Charlen: 8 |
---|
| 394 | -1 , |
---|
| 395 | -1 |
---|
| 396 | ); |
---|
| 397 | |
---|
| 398 | printk("[SDC WARNING] Finish SD card initialization\n\r"); |
---|
| 399 | |
---|
[550] | 400 | /* Initialize the chdev */ |
---|
| 401 | // get extended pointer on SOCLIB_BDV peripheral base address |
---|
| 402 | xptr_t sdc_xp = chdev->base; |
---|
[543] | 403 | |
---|
[550] | 404 | // set driver specific fields |
---|
| 405 | chdev->cmd = &soclib_sdc_cmd; |
---|
| 406 | chdev->isr = &soclib_sdc_isr; |
---|
[543] | 407 | |
---|
[550] | 408 | } // end soclib_sdc_init() |
---|
| 409 | |
---|
[543] | 410 | ///////////////////////////////////////////////////// |
---|
[550] | 411 | void __attribute__ ((noinline)) soclib_sdc_cmd( xptr_t th_xp ) |
---|
[543] | 412 | { |
---|
[550] | 413 | cxy_t th_cxy = GET_CXY( th_xp ); |
---|
| 414 | thread_t * th_ptr = GET_PTR( th_xp ); |
---|
| 415 | |
---|
| 416 | // get command arguments and extended pointer on IOC device |
---|
| 417 | uint32_t cmd_type = hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.type ) ); |
---|
| 418 | unsigned int lba = hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.lba ) ); |
---|
| 419 | xptr_t buf_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->ioc_cmd.buf_xp ) ); |
---|
| 420 | unsigned int count = hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.count ) ); |
---|
| 421 | |
---|
[543] | 422 | unsigned char args[4]; |
---|
| 423 | unsigned char sdcard_rsp; |
---|
| 424 | unsigned int i; |
---|
| 425 | unsigned int curr = lba; |
---|
| 426 | unsigned int last = lba + count; |
---|
| 427 | |
---|
[550] | 428 | if ( cmd_type == IOC_SYNC_READ ) // read access |
---|
[543] | 429 | { |
---|
| 430 | for ( ; curr < last ; curr++ ) |
---|
| 431 | { |
---|
[558] | 432 | _sdc_lseek(&global_sdcard, curr); |
---|
[543] | 433 | |
---|
| 434 | for (i = 0; i < 4; i++) |
---|
| 435 | { |
---|
[558] | 436 | args[i] = (global_sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF; |
---|
[543] | 437 | } |
---|
| 438 | |
---|
[558] | 439 | _sdc_enable(&global_sdcard); |
---|
[543] | 440 | |
---|
[558] | 441 | sdcard_rsp = _sdc_send_command(&global_sdcard, 17, SDCARD_CMD, args, 0x00); |
---|
[543] | 442 | if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) |
---|
| 443 | { |
---|
[558] | 444 | _sdc_disable(&global_sdcard); |
---|
[543] | 445 | return sdcard_rsp; |
---|
| 446 | } |
---|
| 447 | |
---|
[558] | 448 | _sdc_wait_data_block(&global_sdcard); |
---|
[543] | 449 | |
---|
[558] | 450 | if (spi_get_data(global_sdcard.spi, buf_xp, 512 )) |
---|
[543] | 451 | { |
---|
[558] | 452 | _sdc_disable(&global_sdcard); |
---|
[543] | 453 | return 1; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | // Get the CRC16 (comes at the end of the data block) |
---|
[558] | 457 | _sdc_receive_char(&global_sdcard); // first byte |
---|
| 458 | _sdc_receive_char(&global_sdcard); // second byte |
---|
[543] | 459 | |
---|
[558] | 460 | _sdc_disable(&global_sdcard); |
---|
[543] | 461 | |
---|
[550] | 462 | buf_xp += 512; |
---|
[543] | 463 | } |
---|
| 464 | } |
---|
| 465 | else // write access |
---|
| 466 | { |
---|
| 467 | assert( false, __FUNCTION__, "[SDC ERROR] function _sdc_write() not iplemented yet\n"); |
---|
| 468 | } |
---|
| 469 | } // _end sdc_access() |
---|
| 470 | |
---|
| 471 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 472 | // This ISR handles the IRQ generated by a SDC controler |
---|
| 473 | /////////////////////////////////////////////////////////////////////////////// |
---|
[550] | 474 | void __attribute__ ((noinline)) soclib_sdc_isr( chdev_t * chdev ) |
---|
[543] | 475 | { |
---|
| 476 | assert( false, __FUNCTION__, "\n[GIET ERROR] _sdc_isr() not implemented\n"); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | // Local Variables: |
---|
| 480 | // tab-width: 4 |
---|
| 481 | // c-basic-offset: 4 |
---|
| 482 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 483 | // indent-tabs-mode: nil |
---|
| 484 | // End: |
---|
[619] | 485 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|