sd_mmc_mem.c
Go to the documentation of this file.
1 
33 /*
34  * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
35  */
36 
37 #include "conf_access.h"
38 
39 #if (SD_MMC_0_MEM == ENABLE) || (SD_MMC_1_MEM == ENABLE)
40 
41 #include "conf_sd_mmc.h"
42 #include "sd_mmc.h"
43 #include "sd_mmc_mem.h"
44 
56 static bool sd_mmc_ejected[2] = {false, false};
57 
59 {
60  switch (sd_mmc_check(slot))
61  {
62  case SD_MMC_OK:
63  if (sd_mmc_ejected[slot]) {
64  return CTRL_NO_PRESENT;
65  }
66  if (sd_mmc_get_type(slot) & (CARD_TYPE_SD | CARD_TYPE_MMC)) {
67  return CTRL_GOOD;
68  }
69  // It is not a memory card
70  return CTRL_NO_PRESENT;
71 
73  return CTRL_BUSY;
74 
75  case SD_MMC_ERR_NO_CARD:
76  sd_mmc_ejected[slot] = false;
77  return CTRL_NO_PRESENT;
78 
79  default:
80  return CTRL_FAIL;
81  }
82 }
83 
85 {
86  return sd_mmc_test_unit_ready(0);
87 }
88 
89 
91 {
92  return sd_mmc_test_unit_ready(1);
93 }
94 
95 Ctrl_status sd_mmc_read_capacity(uint8_t slot, uint32_t *nb_sector)
96 {
97  // Return last sector address (-1)
98  *nb_sector = (sd_mmc_get_capacity(slot) * 2) - 1;
99  return sd_mmc_test_unit_ready(slot);
100 }
101 
103 {
104  return sd_mmc_read_capacity(0, nb_sector);
105 }
106 
108 {
109  return sd_mmc_read_capacity(1, nb_sector);
110 }
111 
112 bool sd_mmc_unload(uint8_t slot, bool unload)
113 {
114  sd_mmc_ejected[slot] = unload;
115  return true;
116 }
117 
118 bool sd_mmc_unload_0(bool unload)
119 {
120  return sd_mmc_unload(0, unload);
121 }
122 
123 bool sd_mmc_unload_1(bool unload)
124 {
125  return sd_mmc_unload(1, unload);
126 }
127 
128 bool sd_mmc_wr_protect(uint8_t slot)
129 {
130  return sd_mmc_is_write_protected(slot);
131 }
132 
134 {
135  return sd_mmc_wr_protect(0);
136 }
137 
139 {
140  return sd_mmc_wr_protect(1);
141 }
142 
143 bool sd_mmc_removal(uint8_t slot)
144 {
145  UNUSED(slot);
146  return true;
147 }
148 
150 {
151  return sd_mmc_removal(0);
152 }
153 
155 {
156  return sd_mmc_removal(1);
157 }
159 
160 #if ACCESS_USB == true
161 
166 #include "udi_msc.h"
167 
168 COMPILER_WORD_ALIGNED
170 
171 COMPILER_WORD_ALIGNED
173 
174 Ctrl_status sd_mmc_usb_read_10(uint8_t slot, uint32_t addr, uint16_t nb_sector)
175 {
176  bool b_first_step = true;
177  uint16_t nb_step;
178 
179  switch (sd_mmc_init_read_blocks(slot, addr, nb_sector)) {
180  case SD_MMC_OK:
181  break;
182  case SD_MMC_ERR_NO_CARD:
183  return CTRL_NO_PRESENT;
184  default:
185  return CTRL_FAIL;
186  }
187  // Pipeline the 2 transfer in order to speed-up the performances
188  nb_step = nb_sector + 1;
189  while (nb_step--) {
190  if (nb_step) { // Skip last step
191  // MCI -> RAM
192  if (SD_MMC_OK != sd_mmc_start_read_blocks(((nb_step % 2) == 0) ?
193  sector_buf_0 : sector_buf_1, 1)) {
194  return CTRL_FAIL;
195  }
196  }
197  if (!b_first_step) { // Skip first step
198  // RAM -> USB
199  if (!udi_msc_trans_block(true,
200  ((nb_step % 2) == 0) ?
203  NULL)) {
204  if (!b_first_step) {
206  }
207  return CTRL_FAIL;
208  }
209  } else {
210  b_first_step = false;
211  }
212  if (nb_step) { // Skip last step
214  return CTRL_FAIL;
215  }
216  }
217  b_first_step = false;
218  }
219  return CTRL_GOOD;
220 }
221 
222 Ctrl_status sd_mmc_usb_read_10_0(uint32_t addr, uint16_t nb_sector)
223 {
224  return sd_mmc_usb_read_10(0, addr, nb_sector);
225 }
226 
227 Ctrl_status sd_mmc_usb_read_10_1(uint32_t addr, uint16_t nb_sector)
228 {
229  return sd_mmc_usb_read_10(1, addr, nb_sector);
230 }
231 
232 Ctrl_status sd_mmc_usb_write_10(uint8_t slot, uint32_t addr, uint16_t nb_sector)
233 {
234  bool b_first_step = true;
235  uint16_t nb_step;
236 
237  switch (sd_mmc_init_write_blocks(slot, addr, nb_sector)) {
238  case SD_MMC_OK:
239  break;
240  case SD_MMC_ERR_NO_CARD:
241  return CTRL_NO_PRESENT;
242  default:
243  return CTRL_FAIL;
244  }
245  // Pipeline the 2 transfer in order to speed-up the performances
246  nb_step = nb_sector + 1;
247  while (nb_step--) {
248  if (!b_first_step) { // Skip first step
249  // RAM -> MCI
250  if (SD_MMC_OK != sd_mmc_start_write_blocks(((nb_step % 2) == 0) ?
251  sector_buf_0 : sector_buf_1, 1)) {
252  return CTRL_FAIL;
253  }
254  }
255  if (nb_step) { // Skip last step
256  // USB -> RAM
257  if (!udi_msc_trans_block(false,
258  ((nb_step % 2) == 0) ?
261  NULL)) {
262  if (!b_first_step) {
264  }
265  return CTRL_FAIL;
266  }
267  }
268  if (!b_first_step) { // Skip first step
270  return CTRL_FAIL;
271  }
272  } else {
273  b_first_step = false;
274  }
275  }
276  return CTRL_GOOD;
277 }
278 
279 Ctrl_status sd_mmc_usb_write_10_0(uint32_t addr, uint16_t nb_sector)
280 {
281  return sd_mmc_usb_write_10(0, addr, nb_sector);
282 }
283 
284 Ctrl_status sd_mmc_usb_write_10_1(uint32_t addr, uint16_t nb_sector)
285 {
286  return sd_mmc_usb_write_10(1, addr, nb_sector);
287 }
289 #endif // ACCESS_USB == true
290 
291 
292 #if ACCESS_MEM_TO_RAM == true
293 
297 Ctrl_status sd_mmc_mem_2_ram(uint8_t slot, uint32_t addr, void *ram)
298 {
299  switch (sd_mmc_init_read_blocks(slot, addr, 1)) {
300  case SD_MMC_OK:
301  break;
302  case SD_MMC_ERR_NO_CARD:
303  return CTRL_NO_PRESENT;
304  default:
305  return CTRL_FAIL;
306  }
307  if (SD_MMC_OK != sd_mmc_start_read_blocks(ram, 1)) {
308  return CTRL_FAIL;
309  }
311  return CTRL_FAIL;
312  }
313  return CTRL_GOOD;
314 }
315 
316 Ctrl_status sd_mmc_mem_2_ram_0(uint32_t addr, void *ram)
317 {
318  return sd_mmc_mem_2_ram(0, addr, ram);
319 }
320 
321 Ctrl_status sd_mmc_mem_2_ram_1(uint32_t addr, void *ram)
322 {
323  return sd_mmc_mem_2_ram(1, addr, ram);
324 }
325 
326 Ctrl_status sd_mmc_ram_2_mem(uint8_t slot, uint32_t addr, const void *ram)
327 {
328  switch (sd_mmc_init_write_blocks(slot, addr, 1)) {
329  case SD_MMC_OK:
330  break;
331  case SD_MMC_ERR_NO_CARD:
332  return CTRL_NO_PRESENT;
333  default:
334  return CTRL_FAIL;
335  }
336  if (SD_MMC_OK != sd_mmc_start_write_blocks(ram, 1)) {
337  return CTRL_FAIL;
338  }
340  return CTRL_FAIL;
341  }
342  return CTRL_GOOD;
343 }
344 
345 Ctrl_status sd_mmc_ram_2_mem_0(uint32_t addr, const void *ram)
346 {
347  return sd_mmc_ram_2_mem(0, addr, ram);
348 }
349 
350 Ctrl_status sd_mmc_ram_2_mem_1(uint32_t addr, const void *ram)
351 {
352  return sd_mmc_ram_2_mem(1, addr, ram);
353 }
355 
357 #endif // ACCESS_MEM_TO_RAM == true
358 
359 #endif // SD_MMC_0_MEM == ENABLE || SD_MMC_1_MEM == ENABLE
Ctrl_status sd_mmc_test_unit_ready_0(void)
Instance Declaration for sd_mmc_test_unit_ready Slot O.
Definition: sd_mmc_mem.c:84
#define UNUSED(v)
Marking v as a unused parameter or value.
Definition: compiler.h:86
Ctrl_status sd_mmc_mem_2_ram(uint8_t slot, uint32_t addr, void *ram)
Copies 1 data sector from the memory to RAM.
Definition: sd_mmc_mem.c:297
Ctrl_status sd_mmc_ram_2_mem(uint8_t slot, uint32_t addr, const void *ram)
Copies 1 data sector from RAM to the memory.
Definition: sd_mmc_mem.c:326
bool sd_mmc_removal_0(void)
Instance Declaration for sd_mmc_removal Slot O.
Definition: sd_mmc_mem.c:149
bool sd_mmc_removal(uint8_t slot)
Tells whether the memory is removable.
Definition: sd_mmc_mem.c:143
bool sd_mmc_unload_0(bool unload)
Instance Declaration for sd_mmc_unload Slot O.
Definition: sd_mmc_mem.c:118
sd_mmc_err_t sd_mmc_init_write_blocks(uint8_t slot, uint32_t start, uint16_t nb_block)
Initialize the write blocks of data.
Definition: sd_mmc.c:1932
Ctrl_status sd_mmc_read_capacity(uint8_t slot, uint32_t *nb_sector)
Returns the address of the last valid sector in the memory.
Definition: sd_mmc_mem.c:95
Ctrl_status sd_mmc_test_unit_ready(uint8_t slot)
Tests the memory state and initializes the memory if required.
Definition: sd_mmc_mem.c:58
#define SD_MMC_ERR_NO_CARD
Definition: sd_mmc.h:68
Ctrl_status sd_mmc_mem_2_ram_1(uint32_t addr, void *ram)
Instance Declaration for sd_mmc_mem_2_ram Slot 1.
Definition: sd_mmc_mem.c:321
Ctrl_status sd_mmc_usb_write_10_0(uint32_t addr, uint16_t nb_sector)
Instance Declaration for sd_mmc_usb_write_10 Slot O.
Definition: sd_mmc_mem.c:279
Ctrl_status sd_mmc_ram_2_mem_1(uint32_t addr, const void *ram)
Instance Declaration for sd_mmc_mem_2_ram Slot 1.
Definition: sd_mmc_mem.c:350
Memory not initialized or changed.
Definition: ctrl_access.h:79
sd_mmc_err_t sd_mmc_wait_end_of_write_blocks(bool abort)
Wait the end of write blocks of data.
Definition: sd_mmc.c:1991
COMPILER_WORD_ALIGNED uint8_t sector_buf_1[SD_MMC_BLOCK_SIZE]
Definition: sd_mmc_mem.c:172
sd_mmc_err_t sd_mmc_check(uint8_t slot)
Performs a card checks.
Definition: sd_mmc.c:1776
#define SD_MMC_BLOCK_SIZE
This SD MMC stack uses the maximum block size autorized (512 bytes)
Definition: sd_mmc.h:106
#define NULL
Definition: nm_bsp.h:52
An error occurred.
Definition: ctrl_access.h:77
static bool sd_mmc_ejected[2]
Definition: sd_mmc_mem.c:56
Ctrl_status sd_mmc_read_capacity_1(uint32_t *nb_sector)
Instance Declaration for sd_mmc_read_capacity Slot 1.
Definition: sd_mmc_mem.c:107
sd_mmc_err_t sd_mmc_start_read_blocks(void *dest, uint16_t nb_block)
Start the read blocks of data from the card.
Definition: sd_mmc.c:1893
Ctrl_status sd_mmc_read_capacity_0(uint32_t *nb_sector)
Instance Declaration for sd_mmc_read_capacity Slot O.
Definition: sd_mmc_mem.c:102
card_type_t sd_mmc_get_type(uint8_t slot)
Get the card type.
Definition: sd_mmc.c:1802
bool sd_mmc_wr_protect(uint8_t slot)
Returns the write-protection state of the memory.
Definition: sd_mmc_mem.c:128
Ctrl_status sd_mmc_usb_write_10(uint8_t slot, uint32_t addr, uint16_t nb_sector)
Transfers data from USB to the memory.
Definition: sd_mmc_mem.c:232
Ctrl_status sd_mmc_usb_read_10(uint8_t slot, uint32_t addr, uint16_t nb_sector)
Transfers data from the memory to USB.
Definition: sd_mmc_mem.c:174
bool sd_mmc_unload_1(bool unload)
Instance Declaration for sd_mmc_unload Slot 1.
Definition: sd_mmc_mem.c:123
#define CARD_TYPE_MMC
MMC card.
Definition: sd_mmc.h:82
bool sd_mmc_wr_protect_0(void)
Instance Declaration for sd_mmc_wr_protect Slot O.
Definition: sd_mmc_mem.c:133
bool sd_mmc_wr_protect_1(void)
Instance Declaration for sd_mmc_wr_protect Slot 1.
Definition: sd_mmc_mem.c:138
#define CARD_TYPE_SD
SD card.
Definition: sd_mmc.h:81
Ctrl_status
Status returned by CTRL_ACCESS interfaces.
Definition: ctrl_access.h:74
SD/MMC stack configuration file.
Success, memory ready.
Definition: ctrl_access.h:76
sd_mmc_err_t sd_mmc_init_read_blocks(uint8_t slot, uint32_t start, uint16_t nb_block)
Initialize the read blocks of data from the card.
Definition: sd_mmc.c:1842
Common SD/MMC stack header file.
Memory unplugged.
Definition: ctrl_access.h:78
COMPILER_WORD_ALIGNED uint8_t sector_buf_0[SD_MMC_BLOCK_SIZE]
Definition: sd_mmc_mem.c:169
Ctrl_status sd_mmc_usb_write_10_1(uint32_t addr, uint16_t nb_sector)
Instance Declaration for sd_mmc_usb_write_10 Slot 1.
Definition: sd_mmc_mem.c:284
Ctrl_status sd_mmc_usb_read_10_1(uint32_t addr, uint16_t nb_sector)
Instance Declaration for sd_mmc_usb_read_10 Slot 1.
Definition: sd_mmc_mem.c:227
#define SD_MMC_OK
Definition: sd_mmc.h:66
Ctrl_status sd_mmc_mem_2_ram_0(uint32_t addr, void *ram)
Instance Declaration for sd_mmc_mem_2_ram Slot O.
Definition: sd_mmc_mem.c:316
sd_mmc_err_t sd_mmc_start_write_blocks(const void *src, uint16_t nb_block)
Start the write blocks of data.
Definition: sd_mmc.c:1980
uint32_t sd_mmc_get_capacity(uint8_t slot)
Get the memory capacity.
Definition: sd_mmc.c:1820
Ctrl_status sd_mmc_test_unit_ready_1(void)
Instance Declaration for sd_mmc_test_unit_ready Slot 1.
Definition: sd_mmc_mem.c:90
Ctrl_status sd_mmc_usb_read_10_0(uint32_t addr, uint16_t nb_sector)
Instance Declaration for sd_mmc_usb_read_10 Slot O.
Definition: sd_mmc_mem.c:222
bool sd_mmc_unload(uint8_t slot, bool unload)
Unload/Load the SD/MMC card selected.
Definition: sd_mmc_mem.c:112
CTRL_ACCESS interface for common SD/MMC stack.
bool sd_mmc_removal_1(void)
Instance Declaration for sd_mmc_removal Slot 1.
Definition: sd_mmc_mem.c:154
sd_mmc_err_t sd_mmc_wait_end_of_read_blocks(bool abort)
Wait the end of read blocks of data from the card.
Definition: sd_mmc.c:1905
Ctrl_status sd_mmc_ram_2_mem_0(uint32_t addr, const void *ram)
Instance Declaration for sd_mmc_mem_2_ram Slot O.
Definition: sd_mmc_mem.c:345
#define SD_MMC_INIT_ONGOING
Definition: sd_mmc.h:67
bool sd_mmc_is_write_protected(uint8_t slot)
Get the card write protection status.
Definition: sd_mmc.c:1829


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58