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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
// 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"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include "sdcard.h"
#if 0
#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();
}
#endif
void sdcard_init(void) {
// init the SD card detect pin
SD_LowLevel_Init_Detect();
}
bool sdcard_is_present(void) {
return SD_Detect() != 0;
}
bool sdcard_power_on(void) {
if (!SD_Detect()) {
return false;
}
SD_Error status = SD_Init();
if (status != SD_OK) {
SD_PowerOFF();
SD_DeInit();
return false;
}
return true;
}
void sdcard_power_off(void) {
SD_PowerOFF();
SD_DeInit();
}
uint64_t sdcard_get_capacity_in_bytes(void) {
SD_CardInfo SDCardInfo;
SD_GetCardInfo(&SDCardInfo);
return SDCardInfo.CardCapacity;
}
bool sdcard_read_block(uint8_t *dest, uint32_t block_num) {
// TODO return error if not powered on
SD_Error status;
status = SD_ReadBlock(dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE);
if (status != SD_OK) {
return false;
}
#ifdef SD_DMA_MODE
// wait for DMA transfer to finish
status = SD_WaitReadOperation();
if (status != SD_OK) {
return false;
}
#endif
// wait for SD controller to finish
uint32_t stc = sys_tick_counter;
while (SD_GetStatus() != SD_TRANSFER_OK) {
if (sys_tick_has_passed(stc, 5000)) {
//printf("[ERROR] timeout waiting for SD card read to finish\n");
return false;
}
}
return true;
}
bool sdcard_write_block(const uint8_t *src, uint32_t block_num) {
// TODO return error if not powered on
SD_Error status;
status = SD_WriteBlock((uint8_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE);
if (status != SD_OK) {
return false;
}
#ifdef SD_DMA_MODE
// wait for DMA transfer to finish
status = SD_WaitReadOperation();
if (status != SD_OK) {
return false;
}
#endif
// wait for SD controller to finish
uint32_t stc = sys_tick_counter;
while (SD_GetStatus() != SD_TRANSFER_OK) {
if (sys_tick_has_passed(stc, 5000)) {
//printf("[ERROR] timeout waiting for SD card write to finish\n");
return false;
}
}
return true;
}
/******************************************************************************/
// Micro Python bindings
static mp_obj_t sd_present(mp_obj_t self) {
return MP_BOOL(sdcard_is_present());
}
static MP_DEFINE_CONST_FUN_OBJ_1(sd_present_obj, sd_present);
static mp_obj_t sd_power(mp_obj_t self, mp_obj_t state) {
bool result;
if (mp_obj_is_true(state)) {
result = sdcard_power_on();
} else {
sdcard_power_off();
result = true;
}
return MP_BOOL(result);
}
static MP_DEFINE_CONST_FUN_OBJ_2(sd_power_obj, sd_power);
static mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) {
uint8_t *dest = m_new(uint8_t, SDCARD_BLOCK_SIZE);
if (!sdcard_read_block(dest, mp_obj_get_int(block_num))) {
m_free(dest, SDCARD_BLOCK_SIZE);
return mp_const_none;
}
return mp_obj_new_bytearray_by_ref(SDCARD_BLOCK_SIZE, dest);
}
static MP_DEFINE_CONST_FUN_OBJ_2(sd_read_obj, sd_read);
STATIC const mp_map_elem_t sdcard_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_present), (mp_obj_t)&sd_present_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_power), (mp_obj_t)&sd_power_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&sd_read_obj },
};
STATIC MP_DEFINE_CONST_DICT(sdcard_locals_dict, sdcard_locals_dict_table);
static const mp_obj_type_t sdcard_type = {
{ &mp_type_type },
.name = MP_QSTR_SDcard,
.locals_dict = (mp_obj_t)&sdcard_locals_dict,
};
const mp_obj_base_t pyb_sdcard_obj = {&sdcard_type};
|