summaryrefslogtreecommitdiffstatshomepage
path: root/stm/sdio.c
blob: 78488df1f9888afa478348b7c2fc965a14232b62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// TODO
// make it work with DMA

#include <stdio.h>
//#include "stm32f4xx_sdio.h"
#include "stm324x7i_eval_sdio_sd.h"
#include "misc.h"
#include "systick.h"

#define BLOCK_SIZE            512 /* Block Size in Bytes */

uint8_t aBuffer_Block_Rx[BLOCK_SIZE];

void sdio_init(void) {
    SD_Error error = SD_Init();
    printf("Init: %x\n", error);
    uint8_t det = SD_Detect();
    printf("Detc: %x\n", det);

    if (!det) {
        printf("no card detected\n");
        SD_PowerOFF();
        SD_DeInit();
        return;
    }

    // read a block!
    error = SD_ReadBlock(aBuffer_Block_Rx, 512, BLOCK_SIZE);
    printf("ReadBlock: %d\n", error);

    /*
    // Check if the Transfer is finished
    error = SD_WaitReadOperation();
    printf("WaitReadOp: %d\n", error);
    */

    uint32_t stc = sys_tick_counter;
    while (SD_GetStatus() != SD_TRANSFER_OK) {
        if (sys_tick_has_passed(stc, 2000)) {
            printf("timeout waiting for read to finish\n");
            break;
        }
    }
    printf("done!!\n");

    printf("%.16s", aBuffer_Block_Rx);

    /*
    snprintf((char*)aBuffer_Block_Rx, BLOCK_SIZE, "Here is some data back for you!\nBLOCK_SIZE=%d\n", BLOCK_SIZE);
    error = SD_WriteBlock(aBuffer_Block_Rx, 512, BLOCK_SIZE);
    printf("WriteBlock: %d\n", error);

    while (SD_GetStatus() != SD_TRANSFER_OK) {
    }
    printf("done writing!\n");
    */

    SD_PowerOFF();
    SD_DeInit();
}