1 /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
3 Copyright (c) 2014-2015 Datalight, Inc.
4 All Rights Reserved Worldwide.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; use version 2 of the License.
10 This program is distributed in the hope that it will be useful,
11 but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /* Businesses and individuals that for commercial or other reasons cannot
20 comply with the terms of the GPLv2 license may obtain a commercial license
21 before incorporating Reliance Edge into proprietary software for
22 distribution in any form. Visit http://www.datalight.com/reliance-edge for
26 @brief Implements block device I/O.
31 #include <redvolume.h>
32 #include <redosdeviations.h>
35 /*------------------------------------------------------------------------------
38 Several example implementations of this module for FreeRTOS are available.
39 If you are lucky, you can use one of these implementations; otherwise, these
40 can serve as examples of how to implement this service.
41 ------------------------------------------------------------------------------*/
43 /** @brief The F_DRIVER example implementation.
45 This implementation is designed to reuse an existing block device driver
46 that was written for FreeRTOS+FAT SL. If you have such a driver, with
47 little work it can be "dropped in" and used for Reliance Edge. The only
48 customization required is that gpfnRedOsBDevInit needs to be defined and
49 pointed at the F_DRIVERINIT function. This can be done in this module or in
52 The disadantage of using the FreeRTOS F_DRIVER functions is that they only
53 support single-sector reads and writes. Reliance Edge will issue
54 multi-sector requests, and servicing these one sector at a time will
55 significantly slow down the file system.
57 #define BDEV_F_DRIVER (0U)
59 /** @brief The FatFs example implementation.
61 This implementation is designed to reuse an existing block device driver
62 that was written for FatFs. If you have such a driver, it can be linked
63 in and used immediately. The FatFs `diskio.h` header must be in the include
66 #define BDEV_FATFS (1U)
68 /** @brief The Atmel Studio Framework SD/MMC driver example implementation.
70 This implementation uses a modified version of the open source SD/MMC driver
71 included in the Atmel Studio Framework (ASF) and will work as-is for many
72 varieties of Atmel hardware. This example assumes relatively minor
73 modifications to the ASF SD/MMC driver to make it support multi-sector read
74 and write requests, which greatly improves performance. The modified driver
75 is distributed with Reliance Edge and is included in FreeRTOS Atmel projects
76 (such as in projects/freertos/atmel/sam4e-ek/src/ASF).
78 This example can easily be modified to work with an unmodified version of
79 the ASF SD/MMC driver. Simply replace sd_mmc_mem_2_ram_multi() and
80 sd_mmc_ram_2_mem_multi() with sd_mmc_mem_2_ram() and sd_mmc_ram_2_mem()
81 respectively, and add a for loop to loop over each sector in the request.
82 However, as described in the manual, there are considerable performance
83 advantages to issuing real multi-sector requests, so using the modified
84 driver is recommended.
86 #define BDEV_ATMEL_SDMMC (2U)
88 /** @brief The ST Microelectronics STM32 SDIO driver example implementation.
90 This implementation accesses the microSD card through the BSP utilities
91 provided as part of the STM32Cube package, used with the STM32 HAL drivers.
92 The STM3240G-EVAL and STM32F746NG-Discovery boards are currently supported.
94 #define BDEV_STM32_SDIO (3U)
96 /** @brief The RAM disk example implementation.
98 This implementation uses a RAM disk. It will allow you to compile and test
99 Reliance Edge even if your storage driver is not yet ready. On typical
100 target hardware, the amount of spare RAM will be limited so generally only
101 very small disks will be available.
103 #define BDEV_RAM_DISK (4U)
105 /** @brief Pick which example implementation is compiled.
114 #define BDEV_EXAMPLE_IMPLEMENTATION BDEV_RAM_DISK
117 static REDSTATUS DiskOpen(uint8_t bVolNum, BDEVOPENMODE mode);
118 static REDSTATUS DiskClose(uint8_t bVolNum);
119 static REDSTATUS DiskRead(uint8_t bVolNum, uint64_t ullSectorStart, uint32_t ulSectorCount, void *pBuffer);
120 #if REDCONF_READ_ONLY == 0
121 static REDSTATUS DiskWrite(uint8_t bVolNum, uint64_t ullSectorStart, uint32_t ulSectorCount, const void *pBuffer);
122 static REDSTATUS DiskFlush(uint8_t bVolNum);
126 /** @brief Initialize a block device.
128 This function is called when the file system needs access to a block
131 Upon successful return, the block device should be fully initialized and
132 ready to service read/write/flush/close requests.
134 The behavior of calling this function on a block device which is already
137 @param bVolNum The volume number of the volume whose block device is being
139 @param mode The open mode, indicating the type of access required.
141 @return A negated ::REDSTATUS code indicating the operation result.
143 @retval 0 Operation was successful.
144 @retval -RED_EINVAL @p bVolNum is an invalid volume number.
145 @retval -RED_EIO A disk I/O error occurred.
147 REDSTATUS RedOsBDevOpen(
153 if(bVolNum >= REDCONF_VOLUME_COUNT)
159 ret = DiskOpen(bVolNum, mode);
166 /** @brief Uninitialize a block device.
168 This function is called when the file system no longer needs access to a
169 block device. If any resource were allocated by RedOsBDevOpen() to service
170 block device requests, they should be freed at this time.
172 Upon successful return, the block device must be in such a state that it
175 The behavior of calling this function on a block device which is already
178 @param bVolNum The volume number of the volume whose block device is being
181 @return A negated ::REDSTATUS code indicating the operation result.
183 @retval 0 Operation was successful.
184 @retval -RED_EINVAL @p bVolNum is an invalid volume number.
186 REDSTATUS RedOsBDevClose(
191 if(bVolNum >= REDCONF_VOLUME_COUNT)
197 ret = DiskClose(bVolNum);
204 /** @brief Read sectors from a physical block device.
206 The behavior of calling this function is undefined if the block device is
207 closed or if it was opened with ::BDEV_O_WRONLY.
209 @param bVolNum The volume number of the volume whose block device
211 @param ullSectorStart The starting sector number.
212 @param ulSectorCount The number of sectors to read.
213 @param pBuffer The buffer into which to read the sector data.
215 @return A negated ::REDSTATUS code indicating the operation result.
217 @retval 0 Operation was successful.
218 @retval -RED_EINVAL @p bVolNum is an invalid volume number, @p pBuffer is
219 `NULL`, or @p ullStartSector and/or @p ulSectorCount
220 refer to an invalid range of sectors.
221 @retval -RED_EIO A disk I/O error occurred.
223 REDSTATUS RedOsBDevRead(
225 uint64_t ullSectorStart,
226 uint32_t ulSectorCount,
231 if( (bVolNum >= REDCONF_VOLUME_COUNT)
232 || (ullSectorStart >= gaRedVolConf[bVolNum].ullSectorCount)
233 || ((gaRedVolConf[bVolNum].ullSectorCount - ullSectorStart) < ulSectorCount)
234 || (pBuffer == NULL))
240 ret = DiskRead(bVolNum, ullSectorStart, ulSectorCount, pBuffer);
247 #if REDCONF_READ_ONLY == 0
248 /** @brief Write sectors to a physical block device.
250 The behavior of calling this function is undefined if the block device is
251 closed or if it was opened with ::BDEV_O_RDONLY.
253 @param bVolNum The volume number of the volume whose block device
255 @param ullSectorStart The starting sector number.
256 @param ulSectorCount The number of sectors to write.
257 @param pBuffer The buffer from which to write the sector data.
259 @return A negated ::REDSTATUS code indicating the operation result.
261 @retval 0 Operation was successful.
262 @retval -RED_EINVAL @p bVolNum is an invalid volume number, @p pBuffer is
263 `NULL`, or @p ullStartSector and/or @p ulSectorCount
264 refer to an invalid range of sectors.
265 @retval -RED_EIO A disk I/O error occurred.
267 REDSTATUS RedOsBDevWrite(
269 uint64_t ullSectorStart,
270 uint32_t ulSectorCount,
275 if( (bVolNum >= REDCONF_VOLUME_COUNT)
276 || (ullSectorStart >= gaRedVolConf[bVolNum].ullSectorCount)
277 || ((gaRedVolConf[bVolNum].ullSectorCount - ullSectorStart) < ulSectorCount)
278 || (pBuffer == NULL))
284 ret = DiskWrite(bVolNum, ullSectorStart, ulSectorCount, pBuffer);
291 /** @brief Flush any caches beneath the file system.
293 This function must synchronously flush all software and hardware caches
294 beneath the file system, ensuring that all sectors written previously are
295 committed to permanent storage.
297 If the environment has no caching beneath the file system, the
298 implementation of this function can do nothing and return success.
300 The behavior of calling this function is undefined if the block device is
301 closed or if it was opened with ::BDEV_O_RDONLY.
303 @param bVolNum The volume number of the volume whose block device is being
306 @return A negated ::REDSTATUS code indicating the operation result.
308 @retval 0 Operation was successful.
309 @retval -RED_EINVAL @p bVolNum is an invalid volume number.
310 @retval -RED_EIO A disk I/O error occurred.
312 REDSTATUS RedOsBDevFlush(
317 if(bVolNum >= REDCONF_VOLUME_COUNT)
323 ret = DiskFlush(bVolNum);
328 #endif /* REDCONF_READ_ONLY == 0 */
331 #if BDEV_EXAMPLE_IMPLEMENTATION == BDEV_F_DRIVER
333 #include <api_mdriver.h>
336 /* This must be declared and initialized elsewere (e.g., in project code) to
337 point at the initialization function for the F_DRIVER block device.
339 extern const F_DRIVERINIT gpfnRedOsBDevInit;
341 static F_DRIVER *gapFDriver[REDCONF_VOLUME_COUNT];
344 /** @brief Initialize a disk.
346 @param bVolNum The volume number of the volume whose block device is being
348 @param mode The open mode, indicating the type of access required.
350 @return A negated ::REDSTATUS code indicating the operation result.
352 @retval 0 Operation was successful.
353 @retval -RED_EIO A disk I/O error occurred.
355 static REDSTATUS DiskOpen(
363 if((gpfnRedOsBDevInit == NULL) || (gapFDriver[bVolNum] != NULL))
371 pDriver = gpfnRedOsBDevInit(bVolNum);
377 /* Validate that the geometry is consistent with the volume
380 iErr = pDriver->getphy(pDriver, &geom);
383 if( (geom.bytes_per_sector != gaRedVolConf[bVolNum].ulSectorSize)
384 || (geom.number_of_sectors < gaRedVolConf[bVolNum].ullSectorCount))
390 gapFDriver[bVolNum] = pDriver;
401 pDriver->release(pDriver);
414 /** @brief Uninitialize a disk.
416 @param bVolNum The volume number of the volume whose block device is being
419 @return A negated ::REDSTATUS code indicating the operation result.
421 @retval 0 Operation was successful.
423 static REDSTATUS DiskClose(
428 if(gapFDriver[bVolNum] == NULL)
434 gapFDriver[bVolNum]->release(gapFDriver[bVolNum]);
435 gapFDriver[bVolNum] = NULL;
444 /** @brief Read sectors from a disk.
446 @param bVolNum The volume number of the volume whose block device
448 @param ullSectorStart The starting sector number.
449 @param ulSectorCount The number of sectors to read.
450 @param pBuffer The buffer into which to read the sector data.
452 @return A negated ::REDSTATUS code indicating the operation result.
454 @retval 0 Operation was successful.
455 @retval -RED_EIO A disk I/O error occurred.
457 static REDSTATUS DiskRead(
459 uint64_t ullSectorStart,
460 uint32_t ulSectorCount,
464 F_DRIVER *pDriver = gapFDriver[bVolNum];
472 uint8_t *pbBuffer = CAST_VOID_PTR_TO_UINT8_PTR(pBuffer);
473 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
474 uint32_t ulSectorIdx;
477 for(ulSectorIdx = 0U; ulSectorIdx < ulSectorCount; ulSectorIdx++)
479 iErr = pDriver->readsector(pDriver, &pbBuffer[ulSectorIdx * ulSectorSize],
480 CAST_ULONG(ullSectorStart + ulSectorIdx));
493 #if REDCONF_READ_ONLY == 0
494 /** @brief Write sectors to a disk.
496 @param bVolNum The volume number of the volume whose block device
498 @param ullSectorStart The starting sector number.
499 @param ulSectorCount The number of sectors to write.
500 @param pBuffer The buffer from which to write the sector data.
502 @return A negated ::REDSTATUS code indicating the operation result.
504 @retval 0 Operation was successful.
505 @retval -RED_EINVAL The block device is not open.
506 @retval -RED_EIO A disk I/O error occurred.
508 static REDSTATUS DiskWrite(
510 uint64_t ullSectorStart,
511 uint32_t ulSectorCount,
515 F_DRIVER *pDriver = gapFDriver[bVolNum];
523 const uint8_t *pbBuffer = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pBuffer);
524 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
525 uint32_t ulSectorIdx;
528 for(ulSectorIdx = 0U; ulSectorIdx < ulSectorCount; ulSectorIdx++)
530 /* We have to cast pbBuffer to non-const since the writesector
531 prototype is flawed, using a non-const pointer for the buffer.
533 iErr = pDriver->writesector(pDriver, CAST_AWAY_CONST(uint8_t, &pbBuffer[ulSectorIdx * ulSectorSize]),
534 CAST_ULONG(ullSectorStart + ulSectorIdx));
547 /** @brief Flush any caches beneath the file system.
549 @param bVolNum The volume number of the volume whose block device is being
552 @return A negated ::REDSTATUS code indicating the operation result.
554 @retval 0 Operation was successful.
556 static REDSTATUS DiskFlush(
561 if(gapFDriver[bVolNum] == NULL)
567 /* The F_DRIVER interface does not include a flush function, so to be
568 reliable the F_DRIVER implementation must use synchronous writes.
575 #endif /* REDCONF_READ_ONLY == 0 */
578 #elif BDEV_EXAMPLE_IMPLEMENTATION == BDEV_FATFS
583 /* disk_read() and disk_write() use an unsigned 8-bit value to specify the
584 sector count, so no transfer can be larger than 255 sectors.
586 #define MAX_SECTOR_TRANSFER UINT8_MAX
589 /** @brief Initialize a disk.
591 @param bVolNum The volume number of the volume whose block device is being
593 @param mode The open mode, indicating the type of access required.
595 @return A negated ::REDSTATUS code indicating the operation result.
597 @retval 0 Operation was successful.
598 @retval -RED_EIO A disk I/O error occurred.
600 static REDSTATUS DiskOpen(
608 /* With some implementations of disk_initialize(), such as the one
609 implemented by Atmel for the ASF, the first time the disk is opened, the
610 SD card can take a while to get ready, in which time disk_initialize()
611 returns an error. Try numerous times, waiting half a second after each
612 failure. Empirically, this has been observed to succeed on the second
613 try, so trying 10x more than that provides a margin of error.
615 for(ulTries = 0U; ulTries < 20U; ulTries++)
617 /* Assuming that the volume number is also the correct drive number.
618 If this is not the case in your environment, a static constant array
619 can be declared to map volume numbers to the correct driver number.
621 status = disk_initialize(bVolNum);
627 vTaskDelay(500U / portTICK_PERIOD_MS);
635 /* Retrieve the sector size and sector count to ensure they are compatible
636 with our compile-time geometry.
644 result = disk_ioctl(bVolNum, GET_SECTOR_SIZE, &wSectorSize);
647 result = disk_ioctl(bVolNum, GET_SECTOR_COUNT, &dwSectorCount);
650 if( (wSectorSize != gaRedVolConf[bVolNum].ulSectorSize)
651 || (dwSectorCount < gaRedVolConf[bVolNum].ullSectorCount))
671 /** @brief Uninitialize a disk.
673 @param bVolNum The volume number of the volume whose block device is being
676 @return A negated ::REDSTATUS code indicating the operation result.
678 @retval 0 Operation was successful.
680 static REDSTATUS DiskClose(
688 /** @brief Read sectors from a disk.
690 @param bVolNum The volume number of the volume whose block device
692 @param ullSectorStart The starting sector number.
693 @param ulSectorCount The number of sectors to read.
694 @param pBuffer The buffer into which to read the sector data.
696 @return A negated ::REDSTATUS code indicating the operation result.
698 @retval 0 Operation was successful.
699 @retval -RED_EIO A disk I/O error occurred.
701 static REDSTATUS DiskRead(
703 uint64_t ullSectorStart,
704 uint32_t ulSectorCount,
708 uint32_t ulSectorIdx = 0U;
709 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
710 uint8_t *pbBuffer = CAST_VOID_PTR_TO_UINT8_PTR(pBuffer);
712 while(ulSectorIdx < ulSectorCount)
714 uint32_t ulTransfer = REDMIN(ulSectorCount - ulSectorIdx, MAX_SECTOR_TRANSFER);
717 result = disk_read(bVolNum, &pbBuffer[ulSectorIdx * ulSectorSize], (DWORD)(ullSectorStart + ulSectorIdx), (BYTE)ulTransfer);
724 ulSectorIdx += ulTransfer;
731 #if REDCONF_READ_ONLY == 0
732 /** @brief Write sectors to a disk.
734 @param bVolNum The volume number of the volume whose block device
736 @param ullSectorStart The starting sector number.
737 @param ulSectorCount The number of sectors to write.
738 @param pBuffer The buffer from which to write the sector data.
740 @return A negated ::REDSTATUS code indicating the operation result.
742 @retval 0 Operation was successful.
743 @retval -RED_EIO A disk I/O error occurred.
745 static REDSTATUS DiskWrite(
747 uint64_t ullSectorStart,
748 uint32_t ulSectorCount,
752 uint32_t ulSectorIdx = 0U;
753 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
754 const uint8_t *pbBuffer = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pBuffer);
756 while(ulSectorIdx < ulSectorCount)
758 uint32_t ulTransfer = REDMIN(ulSectorCount - ulSectorIdx, MAX_SECTOR_TRANSFER);
761 result = disk_write(bVolNum, &pbBuffer[ulSectorIdx * ulSectorSize], (DWORD)(ullSectorStart + ulSectorIdx), (BYTE)ulTransfer);
768 ulSectorIdx += ulTransfer;
775 /** @brief Flush any caches beneath the file system.
777 @param bVolNum The volume number of the volume whose block device is being
780 @return A negated ::REDSTATUS code indicating the operation result.
782 @retval 0 Operation was successful.
784 static REDSTATUS DiskFlush(
790 result = disk_ioctl(bVolNum, CTRL_SYNC, NULL);
802 #endif /* REDCONF_READ_ONLY == 0 */
805 #elif BDEV_EXAMPLE_IMPLEMENTATION == BDEV_ATMEL_SDMMC
809 #include <conf_sd_mmc.h>
811 #include <sd_mmc_mem.h>
812 #include <ctrl_access.h>
814 /* sd_mmc_mem_2_ram_multi() and sd_mmc_ram_2_mem_multi() use an unsigned
815 16-bit value to specify the sector count, so no transfer can be larger
816 than UINT16_MAX sectors.
818 #define MAX_SECTOR_TRANSFER UINT16_MAX
821 /** @brief Initialize a disk.
823 @param bVolNum The volume number of the volume whose block device is being
825 @param mode The open mode, indicating the type of access required.
827 @return A negated ::REDSTATUS code indicating the operation result.
829 @retval 0 Operation was successful.
830 @retval -RED_EIO A disk I/O error occurred.
831 @retval -RED_EROFS The device is read-only media and write access was
834 static REDSTATUS DiskOpen(
842 /* Note: Assuming the volume number is the same as the SD card slot. The
843 ASF SD/MMC driver supports two SD slots. This implementation will need
844 to be modified if multiple volumes share a single SD card.
847 /* The first time the disk is opened, the SD card can take a while to get
848 ready, in which time sd_mmc_test_unit_ready() returns either CTRL_BUSY
849 or CTRL_NO_PRESENT. Try numerous times, waiting half a second after
850 each failure. Empirically, this has been observed to succeed on the
851 second try, so trying 10x more than that provides a margin of error.
853 for(ulTries = 0U; ulTries < 20U; ulTries++)
855 cs = sd_mmc_test_unit_ready(bVolNum);
856 if((cs != CTRL_NO_PRESENT) && (cs != CTRL_BUSY))
861 vTaskDelay(500U / portTICK_PERIOD_MS);
866 #if REDCONF_READ_ONLY == 0
867 if(mode != BDEV_O_RDONLY)
869 if(sd_mmc_wr_protect(bVolNum))
878 uint32_t ulSectorLast;
880 IGNORE_ERRORS(sd_mmc_read_capacity(bVolNum, &ulSectorLast));
882 /* The ASF SD/MMC driver only supports 512-byte sectors.
884 if( (gaRedVolConf[bVolNum].ulSectorSize != 512U)
885 || (((uint64_t)ulSectorLast + 1U) < gaRedVolConf[bVolNum].ullSectorCount))
900 /** @brief Uninitialize a disk.
902 @param bVolNum The volume number of the volume whose block device is being
905 @return A negated ::REDSTATUS code indicating the operation result.
907 @retval 0 Operation was successful.
909 static REDSTATUS DiskClose(
917 /** @brief Read sectors from a disk.
919 @param bVolNum The volume number of the volume whose block device
921 @param ullSectorStart The starting sector number.
922 @param ulSectorCount The number of sectors to read.
923 @param pBuffer The buffer into which to read the sector data.
925 @return A negated ::REDSTATUS code indicating the operation result.
927 @retval 0 Operation was successful.
929 static REDSTATUS DiskRead(
931 uint64_t ullSectorStart,
932 uint32_t ulSectorCount,
936 uint32_t ulSectorIdx = 0U;
937 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
938 uint8_t *pbBuffer = CAST_VOID_PTR_TO_UINT8_PTR(pBuffer);
940 while(ulSectorIdx < ulSectorCount)
942 uint32_t ulTransfer = REDMIN(ulSectorCount - ulSectorIdx, MAX_SECTOR_TRANSFER);
945 cs = sd_mmc_mem_2_ram_multi(bVolNum, (uint32_t)(ullSectorStart + ulSectorIdx),
946 (uint16_t)ulTransfer, &pbBuffer[ulSectorIdx * ulSectorSize]);
953 ulSectorIdx += ulTransfer;
960 #if REDCONF_READ_ONLY == 0
961 /** @brief Write sectors to a disk.
963 @param bVolNum The volume number of the volume whose block device
965 @param ullSectorStart The starting sector number.
966 @param ulSectorCount The number of sectors to write.
967 @param pBuffer The buffer from which to write the sector data.
969 @return A negated ::REDSTATUS code indicating the operation result.
971 @retval 0 Operation was successful.
973 static REDSTATUS DiskWrite(
975 uint64_t ullSectorStart,
976 uint32_t ulSectorCount,
980 uint32_t ulSectorIdx = 0U;
981 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
982 const uint8_t *pbBuffer = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pBuffer);
984 while(ulSectorIdx < ulSectorCount)
986 uint32_t ulTransfer = REDMIN(ulSectorCount - ulSectorIdx, MAX_SECTOR_TRANSFER);
989 cs = sd_mmc_ram_2_mem_multi(bVolNum, (uint32_t)(ullSectorStart + ulSectorIdx),
990 (uint16_t)ulTransfer, &pbBuffer[ulSectorIdx * ulSectorSize]);
997 ulSectorIdx += ulTransfer;
1004 /** @brief Flush any caches beneath the file system.
1006 @param bVolNum The volume number of the volume whose block device is being
1009 @return A negated ::REDSTATUS code indicating the operation result.
1011 @retval 0 Operation was successful.
1013 static REDSTATUS DiskFlush(
1019 /* The ASF SD/MMC driver appears to write sectors synchronously, so it
1020 should be fine to do nothing and return success. However, Atmel's
1021 implementation of the FatFs diskio.c file does the equivalent of the
1022 below when the disk is flushed. Just in case this is important for some
1023 non-obvious reason, do the same.
1025 cs = sd_mmc_test_unit_ready(bVolNum);
1037 #endif /* REDCONF_READ_ONLY == 0 */
1039 #elif BDEV_EXAMPLE_IMPLEMENTATION == BDEV_STM32_SDIO
1041 #ifdef USE_STM324xG_EVAL
1042 #include <stm324xg_eval.h>
1043 #include <stm324xg_eval_sd.h>
1044 #elif defined(USE_STM32746G_DISCO)
1045 #include <stm32746g_discovery.h>
1046 #include <stm32746g_discovery_sd.h>
1048 /* If you are using a compatible STM32 device other than the two listed above
1049 and you have SD card driver headers, you can try adding them to the above
1052 #error "Unsupported device."
1055 #if REDCONF_VOLUME_COUNT > 1
1056 #error "The STM32 SDIO block device implementation does not support multiple volumes."
1060 #ifndef USE_HAL_DRIVER
1061 #error "The STM32 StdPeriph driver is not supported. Please use the HAL driver or modify the Reliance Edge block device interface."
1065 /** @brief Number of times to call BSP_SD_GetStatus() before timing out and
1068 See ::CheckStatus().
1070 NOTE: Datalight has not observed a scenario where BSP_SD_GetStatus()
1071 returns SD_TRANSFER_BUSY after a transfer command returns successfully.
1072 Set SD_STATUS_TIMEOUT to 0U to skip checking BSP_SD_GetStatus().
1074 #define SD_STATUS_TIMEOUT (100000U)
1076 /** @brief 4-byte aligned buffer to use for DMA transfers when passed in
1077 an unaligned buffer.
1079 static uint32_t gaulAlignedBuffer[512U / sizeof(uint32_t)];
1082 #if SD_STATUS_TIMEOUT > 0U
1083 static REDSTATUS CheckStatus(void);
1087 /** @brief Initialize a disk.
1089 @param bVolNum The volume number of the volume whose block device is being
1091 @param mode The open mode, indicating the type of access required.
1093 @return A negated ::REDSTATUS code indicating the operation result.
1095 @retval 0 Operation was successful.
1096 @retval -RED_EIO No SD card was found; or BSP_SD_Init() failed.
1097 @retval -RED_EINVAL The SD card's block size is not the same as the
1098 configured sector size; or the SD card is not large
1099 enough for the volume; or the volume size is above
1100 4GiB, meaning that part of it cannot be accessed
1101 through the STM32 SDIO driver.
1103 static REDSTATUS DiskOpen(
1108 static bool fSdInitted = false;
1114 if(BSP_SD_Init() == MSD_OK)
1122 /* Above initialization attempt failed.
1126 else if(BSP_SD_IsDetected() == SD_NOT_PRESENT)
1132 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
1133 HAL_SD_CardInfoTypedef sdCardInfo = {{0}};
1135 BSP_SD_GetCardInfo(&sdCardInfo);
1137 /* Note: the actual card block size is sdCardInfo.CardBlockSize,
1138 but the interface only supports a 512 byte block size. Further,
1139 one card has been observed to report a 1024-byte block size,
1140 but it worked fine with a 512-byte Reliance Edge ulSectorSize.
1142 if( (ulSectorSize != 512U)
1143 || (sdCardInfo.CardCapacity < (gaRedVolConf[bVolNum].ullSectorCount * ulSectorSize)))
1153 /** @brief Uninitialize a disk.
1155 @param bVolNum The volume number of the volume whose block device is being
1158 @return A negated ::REDSTATUS code indicating the operation result.
1160 @retval 0 Operation was successful.
1162 static REDSTATUS DiskClose(
1170 /** @brief Read sectors from a disk.
1172 @param bVolNum The volume number of the volume whose block device
1174 @param ullSectorStart The starting sector number.
1175 @param ulSectorCount The number of sectors to read.
1176 @param pBuffer The buffer into which to read the sector data.
1178 @return A negated ::REDSTATUS code indicating the operation result.
1180 @retval 0 Operation was successful.
1181 @retval -RED_EIO A disk I/O error occurred.
1183 static REDSTATUS DiskRead(
1185 uint64_t ullSectorStart,
1186 uint32_t ulSectorCount,
1189 REDSTATUS redStat = 0;
1190 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
1193 if(IS_UINT32_ALIGNED_PTR(pBuffer))
1195 bSdError = BSP_SD_ReadBlocks_DMA(CAST_UINT32_PTR(pBuffer), ullSectorStart * ulSectorSize, ulSectorSize, ulSectorCount);
1197 if(bSdError != MSD_OK)
1201 #if SD_STATUS_TIMEOUT > 0U
1204 redStat = CheckStatus();
1210 uint32_t ulSectorIdx;
1212 for(ulSectorIdx = 0U; ulSectorIdx < ulSectorCount; ulSectorIdx++)
1214 bSdError = BSP_SD_ReadBlocks_DMA(gaulAlignedBuffer, (ullSectorStart + ulSectorIdx) * ulSectorSize, ulSectorSize, 1U);
1216 if(bSdError != MSD_OK)
1220 #if SD_STATUS_TIMEOUT > 0U
1223 redStat = CheckStatus();
1229 uint8_t *pbBuffer = CAST_VOID_PTR_TO_UINT8_PTR(pBuffer);
1231 RedMemCpy(&pbBuffer[ulSectorIdx * ulSectorSize], gaulAlignedBuffer, ulSectorSize);
1244 #if REDCONF_READ_ONLY == 0
1245 /** @brief Write sectors to a disk.
1247 @param bVolNum The volume number of the volume whose block device
1248 is being written to.
1249 @param ullSectorStart The starting sector number.
1250 @param ulSectorCount The number of sectors to write.
1251 @param pBuffer The buffer from which to write the sector data.
1253 @return A negated ::REDSTATUS code indicating the operation result.
1255 @retval 0 Operation was successful.
1256 @retval -RED_EIO A disk I/O error occurred.
1258 static REDSTATUS DiskWrite(
1260 uint64_t ullSectorStart,
1261 uint32_t ulSectorCount,
1262 const void *pBuffer)
1264 REDSTATUS redStat = 0;
1265 uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize;
1268 if(IS_UINT32_ALIGNED_PTR(pBuffer))
1270 bSdError = BSP_SD_WriteBlocks_DMA(CAST_UINT32_PTR(CAST_AWAY_CONST(void, pBuffer)), ullSectorStart * ulSectorSize,
1271 ulSectorSize, ulSectorCount);
1273 if(bSdError != MSD_OK)
1277 #if SD_STATUS_TIMEOUT > 0U
1280 redStat = CheckStatus();
1286 uint32_t ulSectorIdx;
1288 for(ulSectorIdx = 0U; ulSectorIdx < ulSectorCount; ulSectorIdx++)
1290 const uint8_t *pbBuffer = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pBuffer);
1292 RedMemCpy(gaulAlignedBuffer, &pbBuffer[ulSectorIdx * ulSectorSize], ulSectorSize);
1294 bSdError = BSP_SD_WriteBlocks_DMA(gaulAlignedBuffer, (ullSectorStart + ulSectorIdx) * ulSectorSize, ulSectorSize, 1U);
1296 if(bSdError != MSD_OK)
1300 #if SD_STATUS_TIMEOUT > 0U
1303 redStat = CheckStatus();
1318 /** @brief Flush any caches beneath the file system.
1320 @param bVolNum The volume number of the volume whose block device is being
1323 @return A negated ::REDSTATUS code indicating the operation result.
1325 @retval 0 Operation was successful.
1327 static REDSTATUS DiskFlush(
1330 /* Disk transfer is synchronous; nothing to flush.
1337 #if SD_STATUS_TIMEOUT > 0U
1338 /** @brief Wait until BSP_SD_GetStatus returns SD_TRANSFER_OK.
1340 This function calls BSP_SD_GetStatus repeatedly as long as it returns
1341 SD_TRANSFER_BUSY up to SD_STATUS_TIMEOUT times.
1343 @return A negated ::REDSTATUS code indicating the operation result.
1345 @retval 0 SD_TRANSFER_OK was returned.
1346 @retval -RED_EIO SD_TRANSFER_ERROR received, or timed out waiting for
1349 static REDSTATUS CheckStatus(void)
1351 REDSTATUS redStat = 0;
1352 uint32_t ulTimeout = SD_STATUS_TIMEOUT;
1353 HAL_SD_TransferStateTypedef transferState;
1357 transferState = BSP_SD_GetStatus();
1359 } while((transferState == SD_TRANSFER_BUSY) && (ulTimeout > 0U));
1361 if(transferState != SD_TRANSFER_OK)
1370 #endif /* REDCONF_READ_ONLY == 0 */
1372 #elif BDEV_EXAMPLE_IMPLEMENTATION == BDEV_RAM_DISK
1374 #include <stdlib.h> /* For ALLOCATE_CLEARED_MEMORY(), which expands to calloc(). */
1377 static uint8_t *gapbRamDisk[REDCONF_VOLUME_COUNT];
1380 /** @brief Initialize a disk.
1382 @param bVolNum The volume number of the volume whose block device is being
1384 @param mode The open mode, indicating the type of access required.
1386 @return A negated ::REDSTATUS code indicating the operation result.
1388 @retval 0 Operation was successful.
1389 @retval -RED_EIO A disk I/O error occurred.
1391 static REDSTATUS DiskOpen(
1399 if(gapbRamDisk[bVolNum] == NULL)
1401 gapbRamDisk[bVolNum] = ALLOCATE_CLEARED_MEMORY(gaRedVolume[bVolNum].ulBlockCount, REDCONF_BLOCK_SIZE);
1402 if(gapbRamDisk[bVolNum] == NULL)
1412 /** @brief Uninitialize a disk.
1414 @param bVolNum The volume number of the volume whose block device is being
1417 @return A negated ::REDSTATUS code indicating the operation result.
1419 @retval 0 Operation was successful.
1421 static REDSTATUS DiskClose(
1426 if(gapbRamDisk[bVolNum] == NULL)
1432 /* This implementation uses dynamically allocated memory, but must
1433 retain previously written data after the block device is closed, and
1434 thus the memory cannot be freed and will remain allocated until
1444 /** @brief Read sectors from a disk.
1446 @param bVolNum The volume number of the volume whose block device
1448 @param ullSectorStart The starting sector number.
1449 @param ulSectorCount The number of sectors to read.
1450 @param pBuffer The buffer into which to read the sector data.
1452 @return A negated ::REDSTATUS code indicating the operation result.
1454 @retval 0 Operation was successful.
1456 static REDSTATUS DiskRead(
1458 uint64_t ullSectorStart,
1459 uint32_t ulSectorCount,
1464 if(gapbRamDisk[bVolNum] == NULL)
1470 uint64_t ullByteOffset = ullSectorStart * gaRedVolConf[bVolNum].ulSectorSize;
1471 uint32_t ulByteCount = ulSectorCount * gaRedVolConf[bVolNum].ulSectorSize;
1473 RedMemCpy(pBuffer, &gapbRamDisk[bVolNum][ullByteOffset], ulByteCount);
1482 #if REDCONF_READ_ONLY == 0
1483 /** @brief Write sectors to a disk.
1485 @param bVolNum The volume number of the volume whose block device
1486 is being written to.
1487 @param ullSectorStart The starting sector number.
1488 @param ulSectorCount The number of sectors to write.
1489 @param pBuffer The buffer from which to write the sector data.
1491 @return A negated ::REDSTATUS code indicating the operation result.
1493 @retval 0 Operation was successful.
1495 static REDSTATUS DiskWrite(
1497 uint64_t ullSectorStart,
1498 uint32_t ulSectorCount,
1499 const void *pBuffer)
1503 if(gapbRamDisk[bVolNum] == NULL)
1509 uint64_t ullByteOffset = ullSectorStart * gaRedVolConf[bVolNum].ulSectorSize;
1510 uint32_t ulByteCount = ulSectorCount * gaRedVolConf[bVolNum].ulSectorSize;
1512 RedMemCpy(&gapbRamDisk[bVolNum][ullByteOffset], pBuffer, ulByteCount);
1521 /** @brief Flush any caches beneath the file system.
1523 @param bVolNum The volume number of the volume whose block device is being
1526 @return A negated ::REDSTATUS code indicating the operation result.
1528 @retval 0 Operation was successful.
1530 static REDSTATUS DiskFlush(
1535 if(gapbRamDisk[bVolNum] == NULL)
1546 #endif /* REDCONF_READ_ONLY == 0 */
1550 #error "Invalid BDEV_EXAMPLE_IMPLEMENTATION value"
1552 #endif /* BDEV_EXAMPLE_IMPLEMENTATION == ... */