ff_sddisk.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * FreeRTOS+FAT build 191128 - Note: FreeRTOS+FAT is still in the lab!
  3. * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. * Authors include James Walmsley, Hein Tibosch and Richard Barry
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * https://www.FreeRTOS.org
  24. *
  25. */
  26. /* Standard includes. */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. /* LPC18xx includes. */
  32. #include "chip.h"
  33. #include "board.h"
  34. /* FreeRTOS includes. */
  35. #include "FreeRTOS.h"
  36. #include "task.h"
  37. #include "semphr.h"
  38. #include "portmacro.h"
  39. /* FreeRTOS+FAT includes. */
  40. #include "ff_sddisk.h"
  41. #include "ff_sys.h"
  42. #include "mmcsd_core.h"
  43. /* Misc definitions. */
  44. #define sdSIGNATURE 0x41404342UL
  45. #define sdHUNDRED_64_BIT ( 100ull )
  46. #define sdBYTES_PER_MB ( 1024ull * 1024ull )
  47. #define sdSECTORS_PER_MB ( sdBYTES_PER_MB / 512ull )
  48. #define sdIOMAN_MEM_SIZE 4096
  49. #define sdAligned( pvAddress ) ( ( ( ( size_t ) ( pvAddress ) ) & ( sizeof( size_t ) - 1 ) ) == 0 )
  50. /*-----------------------------------------------------------*/
  51. /*_RB_ Functions require comment blocks. */
  52. static int32_t prvSDMMC_Init( const char *pcName );
  53. static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
  54. static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
  55. /*-----------------------------------------------------------*/
  56. /*_RB_ Variables require a comment block where appropriate. */
  57. static BaseType_t xSDCardStatus;
  58. static SemaphoreHandle_t xPlusFATMutex;
  59. /*-----------------------------------------------------------*/
  60. static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
  61. {
  62. struct mmcsd_card *xCardInfo;
  63. int32_t iReturn;
  64. if ( pxDisk && pxDisk->pvTag) {
  65. xCardInfo = pxDisk->pvTag;
  66. if(xCardInfo->card_type == CARD_TYPE_MMC) {
  67. ulSectorNumber += OTA_MEDIA_OFFSET / 512;
  68. }
  69. if( ( xSDCardStatus == pdPASS ) &&
  70. ( pxDisk->ulSignature == sdSIGNATURE ) &&
  71. ( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
  72. ( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
  73. ( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) ) {
  74. iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber, pucBuffer, ulSectorCount, 0 );
  75. /*_RB_ I'm guessing 512 is a sector size, but that needs to be clear.
  76. Is it defined in a header somewhere? If so we can do a search and
  77. replace in files on it as it seems to be used everywhere. */
  78. if( iReturn == 0 ) {
  79. /*_RB_ Signed/unsigned mismatch (twice!) */
  80. iReturn = FF_ERR_NONE;
  81. } else {
  82. /*_RB_ Signed number used to return bitmap (again below). */
  83. iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
  84. }
  85. return iReturn;
  86. }else {
  87. if( pxDisk->xStatus.bIsInitialised != 0 )
  88. {
  89. FF_PRINTF( "prvFFRead: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
  90. }
  91. }
  92. }
  93. memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
  94. return ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
  95. }
  96. /*-----------------------------------------------------------*/
  97. static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
  98. {
  99. struct mmcsd_card *xCardInfo;
  100. int32_t iReturn;
  101. if ( pxDisk && pxDisk->pvTag) {
  102. xCardInfo = pxDisk->pvTag;
  103. if(xCardInfo->card_type == CARD_TYPE_MMC) {
  104. ulSectorNumber += OTA_MEDIA_OFFSET / 512;
  105. }
  106. if (( xSDCardStatus == pdPASS ) &&
  107. ( pxDisk->ulSignature == sdSIGNATURE ) &&
  108. ( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
  109. ( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
  110. ( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) ) {
  111. iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber, pucBuffer, ulSectorCount, 1 );
  112. if( iReturn == 0 ) {
  113. /*_RB_ Signed/unsigned mismatch (twice!) */
  114. iReturn = FF_ERR_NONE;
  115. } else {
  116. iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
  117. }
  118. return iReturn;
  119. } else {
  120. if( pxDisk->xStatus.bIsInitialised )
  121. {
  122. FF_PRINTF( "prvFFWrite: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
  123. }
  124. }
  125. }
  126. memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
  127. return ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
  128. }
  129. /*-----------------------------------------------------------*/
  130. void FF_SDDiskFlush( FF_Disk_t *pxDisk )
  131. {
  132. if( ( pxDisk != NULL ) &&
  133. ( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
  134. ( pxDisk->pxIOManager != NULL ) )
  135. {
  136. FF_FlushCache( pxDisk->pxIOManager );
  137. }
  138. }
  139. /*-----------------------------------------------------------*/
  140. #if DEVICE_TYPE_SELECT == EMMC_FLASH
  141. #define emmcSECTOR_SIZE 512
  142. #define emmcHIDDEN_SECTOR_COUNT 8
  143. #define emmcPRIMARY_PARTITIONS 1
  144. #define emmcPARTITION_NUMBER 0 /* Only a single partition is used. */
  145. static FF_Error_t PartitionAndFormatEmmcDisk( FF_Disk_t * pxDisk )
  146. {
  147. FF_PartitionParameters_t xPartition;
  148. FF_Error_t xError;
  149. /* Create a single partition that fills all available space on the disk. */
  150. memset( &xPartition, '\0', sizeof( xPartition ) );
  151. xPartition.ulSectorCount = OTA_MEDIA_SIZE / emmcSECTOR_SIZE;
  152. xPartition.ulHiddenSectors = emmcHIDDEN_SECTOR_COUNT;
  153. xPartition.xPrimaryCount = emmcPRIMARY_PARTITIONS;
  154. xPartition.eSizeType = eSizeIsQuota;
  155. /* Partition the disk */
  156. xError = FF_Partition( pxDisk, &xPartition );
  157. FF_PRINTF( "FF_Partition: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
  158. if( FF_isERR( xError ) == pdFALSE )
  159. {
  160. /* Format the partition. */
  161. xError = FF_Format( pxDisk, emmcPARTITION_NUMBER, pdFALSE, pdFALSE );
  162. FF_PRINTF( "FF_Format: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
  163. if ( FF_isERR( xError ) == pdFALSE )
  164. {
  165. xError = FF_Mount( pxDisk, 0 );
  166. FF_PRINTF( "FF_Mount: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
  167. FF_SDDiskShowPartition( pxDisk );
  168. }
  169. }
  170. return xError;
  171. }
  172. #endif
  173. /* Initialise the SDIO driver and mount an SD card */
  174. FF_Disk_t *FF_SDDiskInit( const char *pcName )
  175. {
  176. FF_Error_t xFFError;
  177. BaseType_t xPartitionNumber = 0;
  178. FF_CreationParameters_t xParameters;
  179. FF_Disk_t * pxDisk;
  180. struct mmcsd_card *xCardInfo;
  181. xSDCardStatus = prvSDMMC_Init(pcName);
  182. xCardInfo = mmcsd_get_sdmmc_card_info_by_name(pcName);
  183. if( xSDCardStatus == pdPASS )
  184. {
  185. pxDisk = ( FF_Disk_t * ) pvPortMalloc( sizeof( *pxDisk ) );
  186. if( pxDisk != NULL )
  187. {
  188. /* Initialise the created disk structure. */
  189. memset( pxDisk, '\0', sizeof( *pxDisk ) );
  190. if( xPlusFATMutex == NULL)
  191. {
  192. xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
  193. }
  194. pxDisk->ulNumberOfSectors = (xCardInfo->card_capacity / 512) << 10;
  195. pxDisk->ulSignature = sdSIGNATURE;
  196. pxDisk->pvTag = xCardInfo;
  197. if( xPlusFATMutex != NULL)
  198. {
  199. memset( &xParameters, '\0', sizeof( xParameters ) );
  200. xParameters.ulMemorySize = sdIOMAN_MEM_SIZE;
  201. xParameters.ulSectorSize = 512;
  202. xParameters.fnWriteBlocks = prvFFWrite;
  203. xParameters.fnReadBlocks = prvFFRead;
  204. xParameters.pxDisk = pxDisk;
  205. /* prvFFRead()/prvFFWrite() are not re-entrant and must be
  206. protected with the use of a semaphore. */
  207. xParameters.xBlockDeviceIsReentrant = pdFALSE;
  208. /* The semaphore will be used to protect critical sections in
  209. the +FAT driver, and also to avoid concurrent calls to
  210. prvFFRead()/prvFFWrite() from different tasks. */
  211. xParameters.pvSemaphore = ( void * ) xPlusFATMutex;
  212. pxDisk->pxIOManager = FF_CreateIOManger( &xParameters, &xFFError );
  213. if( pxDisk->pxIOManager == NULL )
  214. {
  215. FF_PRINTF( "FF_SDDiskInit: FF_CreateIOManger: %s\n", ( const char * ) FF_GetErrMessage( xFFError ) );
  216. FF_SDDiskDelete( pxDisk );
  217. pxDisk = NULL;
  218. }
  219. else
  220. {
  221. pxDisk->xStatus.bIsInitialised = pdTRUE;
  222. pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
  223. if( FF_SDDiskMount( pxDisk ) == 0 )
  224. {
  225. #if DEVICE_TYPE_SELECT == EMMC_FLASH
  226. if ( PartitionAndFormatEmmcDisk(pxDisk) != FF_ERR_NONE )
  227. {
  228. FF_PRINTF( "FF_SDDiskInit: Mounted SD-card/emmc fail.\n");
  229. FF_SDDiskDelete( pxDisk );
  230. pxDisk = NULL;
  231. }
  232. else
  233. {
  234. if( pcName == NULL )
  235. {
  236. pcName = "/";
  237. }
  238. FF_FS_Add( pcName, pxDisk );
  239. FF_PRINTF( "FF_SDDiskInit: Mount SD-card/emmc as root \"%s\"\n", pcName );
  240. }
  241. #else
  242. FF_PRINTF( "FF_SDDiskInit: Mounted SD-card/emmc fail.\n");
  243. FF_SDDiskDelete( pxDisk );
  244. pxDisk = NULL;
  245. #endif
  246. }
  247. else
  248. {
  249. if( pcName == NULL )
  250. {
  251. pcName = "/";
  252. }
  253. FF_FS_Add( pcName, pxDisk );
  254. FF_PRINTF( "FF_SDDiskInit: Mounted SD-card as root \"%s\"\n", pcName );
  255. }
  256. } /* if( pxDisk->pxIOManager != NULL ) */
  257. } /* if( xPlusFATMutex != NULL) */
  258. } /* if( pxDisk != NULL ) */
  259. else
  260. {
  261. FF_PRINTF( "FF_SDDiskInit: Malloc failed\n" );
  262. }
  263. } /* if( xSDCardStatus == pdPASS ) */
  264. else
  265. {
  266. FF_PRINTF( "FF_SDDiskInit: prvSDMMC_Init failed\n" );
  267. pxDisk = NULL;
  268. }
  269. return pxDisk;
  270. }
  271. /*-----------------------------------------------------------*/
  272. BaseType_t FF_SDDiskFormat( FF_Disk_t *pxDisk, BaseType_t xPartitionNumber )
  273. {
  274. FF_Error_t xError;
  275. BaseType_t xReturn = pdFAIL;
  276. xError = FF_Unmount( pxDisk );
  277. if( FF_isERR( xError ) != pdFALSE )
  278. {
  279. FF_PRINTF( "FF_SDDiskFormat: unmount fails: %08x\n", ( unsigned ) xError );
  280. }
  281. else
  282. {
  283. /* Format the drive - try FAT32 with large clusters. */
  284. xError = FF_Format( pxDisk, xPartitionNumber, pdFALSE, pdFALSE);
  285. if( FF_isERR( xError ) )
  286. {
  287. FF_PRINTF( "FF_SDDiskFormat: %s\n", (const char*)FF_GetErrMessage( xError ) );
  288. }
  289. else
  290. {
  291. FF_PRINTF( "FF_SDDiskFormat: OK, now remounting\n" );
  292. pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
  293. xError = FF_SDDiskMount( pxDisk );
  294. FF_PRINTF( "FF_SDDiskFormat: rc %08x\n", ( unsigned )xError );
  295. if( FF_isERR( xError ) == pdFALSE )
  296. {
  297. xReturn = pdPASS;
  298. }
  299. }
  300. }
  301. return xReturn;
  302. }
  303. /*-----------------------------------------------------------*/
  304. BaseType_t FF_SDDiskFormatRemount( FF_Disk_t *pxDisk, const char *pcName )
  305. {
  306. FF_Error_t xError;
  307. BaseType_t xReturn = pdFAIL;
  308. xError = FF_Unmount( pxDisk );
  309. if( FF_isERR( xError ) != pdFALSE )
  310. {
  311. FF_PRINTF( "FF_SDDiskFormat: unmount fails: %08x\n", ( unsigned ) xError );
  312. }
  313. else
  314. {
  315. #if DEVICE_TYPE_SELECT == EMMC_FLASH
  316. if ( PartitionAndFormatEmmcDisk(pxDisk) != FF_ERR_NONE )
  317. {
  318. FF_PRINTF( "FF_SDDiskFormatRemount: PartitionAndFormatEmmcDisk fail.\n");
  319. FF_SDDiskDelete( pxDisk );
  320. pxDisk = NULL;
  321. }
  322. else
  323. {
  324. if( pcName == NULL )
  325. {
  326. pcName = "/";
  327. }
  328. FF_FS_Add( pcName, pxDisk );
  329. FF_PRINTF( "FF_SDDiskFormatRemount: Mount SD-card/emmc as root \"%s\"\n", pcName );
  330. }
  331. #else
  332. FF_PRINTF( "FF_SDDiskInit: Mounted SD-card/emmc fail.\n");
  333. FF_SDDiskDelete( pxDisk );
  334. pxDisk = NULL;
  335. #endif
  336. }
  337. return xReturn;
  338. }
  339. /*-----------------------------------------------------------*/
  340. /* Get a pointer to IOMAN, which can be used for all FreeRTOS+FAT functions */
  341. BaseType_t FF_SDDiskMount( FF_Disk_t *pxDisk )
  342. {
  343. FF_Error_t xFFError;
  344. BaseType_t xReturn;
  345. /* Mount the partition */
  346. xFFError = FF_Mount( pxDisk, pxDisk->xStatus.bPartitionNumber );
  347. if( FF_isERR( xFFError ) )
  348. {
  349. FF_PRINTF( "FF_SDDiskMount: %08lX\n", xFFError );
  350. xReturn = pdFAIL;
  351. }
  352. else
  353. {
  354. pxDisk->xStatus.bIsMounted = pdTRUE;
  355. FF_PRINTF( "****** FreeRTOS+FAT initialized %lu sectors\n", pxDisk->pxIOManager->xPartition.ulTotalSectors );
  356. FF_SDDiskShowPartition( pxDisk );
  357. xReturn = pdPASS;
  358. }
  359. return xReturn;
  360. }
  361. /*-----------------------------------------------------------*/
  362. FF_IOManager_t *sddisk_ioman( FF_Disk_t *pxDisk )
  363. {
  364. FF_IOManager_t *pxReturn;
  365. if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsInitialised != pdFALSE ) )
  366. {
  367. pxReturn = pxDisk->pxIOManager;
  368. }
  369. else
  370. {
  371. pxReturn = NULL;
  372. }
  373. return pxReturn;
  374. }
  375. /*-----------------------------------------------------------*/
  376. /* Release all resources */
  377. BaseType_t FF_SDDiskDelete( FF_Disk_t *pxDisk )
  378. {
  379. if( pxDisk != NULL )
  380. {
  381. pxDisk->ulSignature = 0;
  382. pxDisk->xStatus.bIsInitialised = 0;
  383. if( pxDisk->pxIOManager != NULL )
  384. {
  385. if( FF_Mounted( pxDisk->pxIOManager ) != pdFALSE )
  386. {
  387. FF_Unmount( pxDisk );
  388. }
  389. FF_DeleteIOManager( pxDisk->pxIOManager );
  390. }
  391. vPortFree( pxDisk );
  392. }
  393. return 1;
  394. }
  395. /*-----------------------------------------------------------*/
  396. BaseType_t FF_SDDiskShowPartition( FF_Disk_t *pxDisk )
  397. {
  398. FF_Error_t xError;
  399. uint64_t ullFreeSectors;
  400. uint32_t ulTotalSizeMB, ulFreeSizeMB;
  401. int iPercentageFree;
  402. FF_IOManager_t *pxIOManager;
  403. const char *pcTypeName = "unknown type";
  404. BaseType_t xReturn = pdPASS;
  405. if( pxDisk == NULL )
  406. {
  407. xReturn = pdFAIL;
  408. }
  409. else
  410. {
  411. pxIOManager = pxDisk->pxIOManager;
  412. FF_PRINTF( "Reading FAT and calculating Free Space\n" );
  413. switch( pxIOManager->xPartition.ucType )
  414. {
  415. case FF_T_FAT12:
  416. pcTypeName = "FAT12";
  417. break;
  418. case FF_T_FAT16:
  419. pcTypeName = "FAT16";
  420. break;
  421. case FF_T_FAT32:
  422. pcTypeName = "FAT32";
  423. break;
  424. default:
  425. pcTypeName = "UNKOWN";
  426. break;
  427. }
  428. FF_GetFreeSize( pxIOManager, &xError );
  429. ullFreeSectors = pxIOManager->xPartition.ulFreeClusterCount * pxIOManager->xPartition.ulSectorsPerCluster;
  430. iPercentageFree = ( int ) ( ( sdHUNDRED_64_BIT * ullFreeSectors + pxIOManager->xPartition.ulDataSectors / 2 ) /
  431. ( ( uint64_t )pxIOManager->xPartition.ulDataSectors ) );
  432. ulTotalSizeMB = pxIOManager->xPartition.ulDataSectors / sdSECTORS_PER_MB;
  433. ulFreeSizeMB = ( uint32_t ) ( ullFreeSectors / sdSECTORS_PER_MB );
  434. /* It is better not to use the 64-bit format such as %Lu because it
  435. might not be implemented. */
  436. FF_PRINTF( "Partition Nr %8u\n", pxDisk->xStatus.bPartitionNumber );
  437. FF_PRINTF( "Type %8u (%s)\n", pxIOManager->xPartition.ucType, pcTypeName );
  438. FF_PRINTF( "VolLabel '%8s' \n", pxIOManager->xPartition.pcVolumeLabel );
  439. FF_PRINTF( "TotalSectors %8lu\n", pxIOManager->xPartition.ulTotalSectors );
  440. FF_PRINTF( "SecsPerCluster %8lu\n", pxIOManager->xPartition.ulSectorsPerCluster );
  441. FF_PRINTF( "Size %8lu MB\n", ulTotalSizeMB );
  442. FF_PRINTF( "FreeSize %8lu MB ( %d perc free )\n", ulFreeSizeMB, iPercentageFree );
  443. }
  444. return xReturn;
  445. }
  446. /*-----------------------------------------------------------*/
  447. static int32_t prvSDMMC_Init(char const*name)
  448. {
  449. struct mmcsd_card *xCardInfo = mmcsd_get_sdmmc_card_info_by_name(name);
  450. if (!xCardInfo)
  451. return pdFAIL;
  452. mmcsd_set_blksize(xCardInfo);
  453. return pdPASS;
  454. }
  455. /*-----------------------------------------------------------*/
  456. #if DEVICE_TYPE_SELECT == EMMC_FLASH
  457. static uint32_t cached_sector = 0xffffffff;
  458. static uint8_t cached_data[emmcSECTOR_SIZE];
  459. static SemaphoreHandle_t cached_mutex = NULL;
  460. int32_t phyRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount)
  461. {
  462. int32_t iReturn;
  463. /*_RB_ Many of the comments in this file apply to other functions in the file. */
  464. iReturn = mmcsd_req_blk(mmcsd_get_sdmmc_card_info_by_name("/emmc"), ulSectorNumber, pucBuffer, ulSectorCount, 0 );
  465. /*_RB_ I'm guessing 512 is a sector size, but that needs to be clear.
  466. Is it defined in a header somewhere? If so we can do a search and
  467. replace in files on it as it seems to be used everywhere. */
  468. if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
  469. {
  470. iReturn = FF_ERR_NONE;
  471. }
  472. else
  473. {
  474. /*_RB_ Signed number used to return bitmap (again below). */
  475. iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
  476. }
  477. return iReturn;
  478. }
  479. /*-----------------------------------------------------------*/
  480. int32_t phyWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount)
  481. {
  482. int32_t iReturn;
  483. iReturn = mmcsd_req_blk(mmcsd_get_sdmmc_card_info_by_name("/emmc"), ulSectorNumber, pucBuffer, ulSectorCount, 1 );
  484. if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
  485. {
  486. iReturn = FF_ERR_NONE;
  487. }
  488. else
  489. {
  490. iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
  491. }
  492. return iReturn;
  493. }
  494. #define EMMC_RW_MAX_SIZE 0x100000
  495. static int raw_emmc_read(uint32_t offset, size_t size, uint8_t *data)
  496. {
  497. unsigned int blkcnt = 0;
  498. unsigned int blkstart = 0;
  499. unsigned char *ptembuf = NULL;
  500. unsigned int inoffset = 0;
  501. unsigned int readsize = 0;
  502. int ret = 0;
  503. blkstart = offset / emmcSECTOR_SIZE;
  504. inoffset = offset - blkstart * emmcSECTOR_SIZE;
  505. readsize = size + inoffset;
  506. blkcnt = (readsize + emmcSECTOR_SIZE - 1) / emmcSECTOR_SIZE;
  507. ptembuf = (uint8_t *)pvPortMalloc(blkcnt * emmcSECTOR_SIZE);
  508. if(!ptembuf)
  509. printf("malloc Error!!1\n");
  510. ret = phyRead(ptembuf, blkstart, blkcnt);
  511. memcpy(data, ptembuf + inoffset, size);
  512. if(ptembuf) {
  513. vPortFree(ptembuf);
  514. ptembuf = NULL;
  515. }
  516. return ret;
  517. }
  518. static int raw_emmc_write(uint32_t offset, size_t size, uint8_t *data)
  519. {
  520. unsigned int blkcnt = 0;
  521. unsigned int blkstart = 0, blkend = 0;
  522. unsigned char *ptembuf = NULL;
  523. unsigned int inoffset = 0;
  524. unsigned int endoffset = 0;
  525. unsigned int wsize = 0;
  526. int ret = 0;
  527. blkstart = offset / emmcSECTOR_SIZE;
  528. inoffset = offset % emmcSECTOR_SIZE;
  529. wsize = size + inoffset;
  530. blkcnt = (wsize + emmcSECTOR_SIZE - 1) / emmcSECTOR_SIZE;
  531. blkend = blkstart + blkcnt - 1;
  532. endoffset = wsize % emmcSECTOR_SIZE;
  533. ptembuf = (uint8_t *)pvPortMalloc(blkcnt * emmcSECTOR_SIZE);
  534. if(ptembuf) {
  535. if (inoffset) {
  536. if (blkstart != cached_sector) {
  537. phyRead(cached_data, blkstart, 1);
  538. cached_sector = blkstart;
  539. }
  540. memcpy(ptembuf, cached_data, inoffset);
  541. }
  542. if (data)
  543. memcpy(ptembuf + inoffset, data, size);
  544. else
  545. memset(ptembuf + inoffset, 0xff, size);
  546. if (cached_sector == blkstart) {
  547. if (size > emmcSECTOR_SIZE - inoffset)
  548. memcpy(cached_data + inoffset, ptembuf + inoffset, emmcSECTOR_SIZE - inoffset);
  549. else
  550. memcpy(cached_data + inoffset, ptembuf + inoffset, size);
  551. }
  552. if (endoffset) {
  553. if (blkend != cached_sector) {
  554. phyRead(cached_data, blkend, 1);
  555. cached_sector = blkend;
  556. }
  557. memcpy(ptembuf + wsize, cached_data + endoffset, emmcSECTOR_SIZE - endoffset);
  558. memcpy(cached_data, ptembuf + wsize - endoffset, endoffset);
  559. }
  560. ret = phyWrite(ptembuf,blkstart,blkcnt);
  561. if(ptembuf) {
  562. vPortFree(ptembuf);
  563. //free(ptembuf);
  564. ptembuf = NULL;
  565. }
  566. } else
  567. printf("emmc_write malloc Error!!1\n");
  568. return ret;
  569. }
  570. int emmc_read(uint32_t offset, size_t size, uint8_t *data)
  571. {
  572. int32_t leftsize = size;
  573. int32_t off = offset;
  574. uint8_t *buf = data;
  575. uint32_t rsize;
  576. int ret;
  577. while (leftsize > 0) {
  578. rsize = leftsize > EMMC_RW_MAX_SIZE ? EMMC_RW_MAX_SIZE : leftsize;
  579. ret = raw_emmc_read(off, rsize, buf);
  580. if (ret)
  581. return ret;
  582. leftsize -= rsize;
  583. off += rsize;
  584. buf += rsize;
  585. }
  586. return 0;
  587. }
  588. int emmc_write(uint32_t offset, size_t size, uint8_t *data)
  589. {
  590. int32_t leftsize = size;
  591. int32_t off = offset;
  592. uint8_t *buf = data;
  593. uint32_t wsize;
  594. int ret;
  595. if (cached_mutex == NULL)
  596. cached_mutex = xSemaphoreCreateMutex();
  597. xSemaphoreTake(cached_mutex, portMAX_DELAY);
  598. while (leftsize > 0) {
  599. wsize = leftsize > EMMC_RW_MAX_SIZE ? EMMC_RW_MAX_SIZE : leftsize;
  600. ret = raw_emmc_write(off, wsize, buf);
  601. if (ret) {
  602. xSemaphoreGive(cached_mutex);
  603. return ret;
  604. }
  605. leftsize -= wsize;
  606. off += wsize;
  607. if (buf)
  608. buf += wsize;
  609. }
  610. xSemaphoreGive(cached_mutex);
  611. return 0;
  612. }
  613. #endif