• R/O
  • SSH
  • HTTPS

chibios: 提交


Commit MetaInfo

修订版15836 (tree)
时间2022-11-14 23:03:46
作者gdisirio

Log Message

(empty log message)

更改概述

差异

--- trunk/os/hal/src/hal_mmc_spi.c (revision 15835)
+++ trunk/os/hal/src/hal_mmc_spi.c (revision 15836)
@@ -74,7 +74,7 @@
7474 /**
7575 * @brief Lookup table for CRC-7 ( based on polynomial x^7 + x^3 + 1).
7676 */
77-static const uint8_t crc7_lookup_table[256] = {
77+static const uint8_t mmc_crc7_lookup_table[256] = {
7878 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53,
7979 0x6c, 0x65, 0x7e, 0x77, 0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26,
8080 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e, 0x32, 0x3b, 0x20, 0x29,
@@ -277,10 +277,10 @@
277277 * @param[in] len length of data
278278 * @return Calculated CRC
279279 */
280-static uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len) {
280+static uint8_t mmc_crc7(uint8_t crc, const uint8_t *buffer, size_t len) {
281281
282282 while (len > 0U) {
283- crc = crc7_lookup_table[(crc << 1) ^ (*buffer++)];
283+ crc = mmc_crc7_lookup_table[(crc << 1) ^ (*buffer++)];
284284 len--;
285285 }
286286 return crc;
@@ -290,10 +290,13 @@
290290 * @brief Waits an idle condition.
291291 *
292292 * @param[in] mmcp pointer to the @p MMCDriver object
293+ * @return The operation status.
294+ * @retval HAL_SUCCESS if the operation succeeded.
295+ * @retval HAL_FAILED if the operation failed.
293296 *
294297 * @notapi
295298 */
296-static void wait(MMCDriver *mmcp) {
299+static bool mmc_wait_idle(MMCDriver *mmcp) {
297300 int i;
298301 uint8_t buf[4];
299302
@@ -300,14 +303,14 @@
300303 for (i = 0; i < 16; i++) {
301304 spiReceive(mmcp->config->spip, 1, buf);
302305 if (buf[0] == 0xFFU) {
303- return;
306+ return HAL_SUCCESS;
304307 }
305308 }
306309 /* Looks like it is a long wait.*/
307- while (true) {
310+ while (true) { /* todo timeout */
308311 spiReceive(mmcp->config->spip, 1, buf);
309312 if (buf[0] == 0xFFU) {
310- break;
313+ return HAL_SUCCESS;
311314 }
312315 #if MMC_NICE_WAITING == TRUE
313316 /* Trying to be nice with the other threads.*/
@@ -314,6 +317,8 @@
314317 osalThreadSleepMilliseconds(1);
315318 #endif
316319 }
320+
321+ return HAL_FAILED;
317322 }
318323
319324 /**
@@ -322,14 +327,19 @@
322327 * @param[in] mmcp pointer to the @p MMCDriver object
323328 * @param[in] cmd the command id
324329 * @param[in] arg the command argument
330+ * @return The operation status.
331+ * @retval HAL_SUCCESS if the operation succeeded.
332+ * @retval HAL_FAILED if the operation failed.
325333 *
326334 * @notapi
327335 */
328-static void send_hdr(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) {
336+static bool mmc_send_hdr(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) {
329337 uint8_t buf[6];
330338
331339 /* Wait for the bus to become idle if a write operation was in progress.*/
332- wait(mmcp);
340+ if (mmc_wait_idle(mmcp) == HAL_FAILED) {
341+ return HAL_FAILED;
342+ }
333343
334344 buf[0] = (uint8_t)0x40U | cmd;
335345 buf[1] = (uint8_t)(arg >> 24U);
@@ -337,9 +347,11 @@
337347 buf[3] = (uint8_t)(arg >> 8U);
338348 buf[4] = (uint8_t)arg;
339349 /* Calculate CRC for command header, shift to right position, add stop bit.*/
340- buf[5] = ((crc7(0, buf, 5U) & 0x7FU) << 1U) | 0x01U;
350+ buf[5] = ((mmc_crc7(0, buf, 5U) & 0x7FU) << 1U) | 0x01U;
341351
342352 spiSend(mmcp->config->spip, 6, buf);
353+
354+ return HAL_SUCCESS;
343355 }
344356
345357 /**
@@ -351,7 +363,7 @@
351363 *
352364 * @notapi
353365 */
354-static uint8_t recvr1(MMCDriver *mmcp) {
366+static uint8_t mmc_recvr1(MMCDriver *mmcp) {
355367 int i;
356368 uint8_t r1[1];
357369
@@ -374,10 +386,10 @@
374386 *
375387 * @notapi
376388 */
377-static uint8_t recvr3(MMCDriver *mmcp, uint8_t* buffer) {
389+static uint8_t mmc_recvr3(MMCDriver *mmcp, uint8_t* buffer) {
378390 uint8_t r1;
379391
380- r1 = recvr1(mmcp);
392+ r1 = mmc_recvr1(mmcp);
381393 spiReceive(mmcp->config->spip, 4, buffer);
382394
383395 return r1;
@@ -394,12 +406,12 @@
394406 *
395407 * @notapi
396408 */
397-static uint8_t send_command_R1(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) {
409+static uint8_t mmc_send_command_R1(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) {
398410 uint8_t r1;
399411
400412 spiSelect(mmcp->config->spip);
401- send_hdr(mmcp, cmd, arg);
402- r1 = recvr1(mmcp);
413+ (void) mmc_send_hdr(mmcp, cmd, arg); /* todo */
414+ r1 = mmc_recvr1(mmcp);
403415 spiUnselect(mmcp->config->spip);
404416 return r1;
405417 }
@@ -417,13 +429,13 @@
417429 *
418430 * @notapi
419431 */
420-static uint8_t send_command_R3(MMCDriver *mmcp, uint8_t cmd, uint32_t arg,
421- uint8_t *response) {
432+static uint8_t mmc_send_command_R3(MMCDriver *mmcp, uint8_t cmd, uint32_t arg,
433+ uint8_t *response) {
422434 uint8_t r1;
423435
424436 spiSelect(mmcp->config->spip);
425- send_hdr(mmcp, cmd, arg);
426- r1 = recvr3(mmcp, response);
437+ (void) mmc_send_hdr(mmcp, cmd, arg); /* todo */
438+ r1 = mmc_recvr3(mmcp, response);
427439 spiUnselect(mmcp->config->spip);
428440 return r1;
429441 }
@@ -436,18 +448,18 @@
436448 * @param[out] cxd pointer to the CSD/CID buffer
437449 *
438450 * @return The operation status.
439- * @retval HAL_SUCCESS the operation succeeded.
440- * @retval HAL_FAILED the operation failed.
451+ * @retval HAL_SUCCESS if the operation succeeded.
452+ * @retval HAL_FAILED if the operation failed.
441453 *
442454 * @notapi
443455 */
444-static bool read_CxD(MMCDriver *mmcp, uint8_t cmd, uint32_t cxd[4]) {
456+static bool mmc_read_CxD(MMCDriver *mmcp, uint8_t cmd, uint32_t cxd[4]) {
445457 unsigned i;
446458 uint8_t *bp, buf[16];
447459
448460 spiSelect(mmcp->config->spip);
449- send_hdr(mmcp, cmd, 0);
450- if (recvr1(mmcp) != 0x00U) {
461+ (void) mmc_send_hdr(mmcp, cmd, 0); /* todo */
462+ if (mmc_recvr1(mmcp) != 0x00U) {
451463 spiUnselect(mmcp->config->spip);
452464 return HAL_FAILED;
453465 }
@@ -483,7 +495,7 @@
483495 *
484496 * @notapi
485497 */
486-static void sync(MMCDriver *mmcp) {
498+static void mmc_wait_sync(MMCDriver *mmcp) {
487499 uint8_t buf[1];
488500
489501 spiSelect(mmcp->config->spip);
@@ -580,9 +592,8 @@
580592 * @param[in] mmcp pointer to the @p MMCDriver object
581593 *
582594 * @return The operation status.
583- * @retval HAL_SUCCESS the operation succeeded and the driver is now
584- * in the @p MMC_READY state.
585- * @retval HAL_FAILED the operation failed.
595+ * @retval HAL_SUCCESS if the operation succeeded.
596+ * @retval HAL_FAILED if the operation failed.
586597 *
587598 * @api
588599 */
@@ -606,7 +617,7 @@
606617 /* SPI mode selection.*/
607618 i = 0;
608619 while (true) {
609- if (send_command_R1(mmcp, MMCSD_CMD_GO_IDLE_STATE, 0) == 0x01U) {
620+ if (mmc_send_command_R1(mmcp, MMCSD_CMD_GO_IDLE_STATE, 0) == 0x01U) {
610621 break;
611622 }
612623 if (++i >= MMC_CMD0_RETRY) {
@@ -619,15 +630,15 @@
619630 addresses if possible.
620631 This method is based on "How to support SDC Ver2 and high capacity cards"
621632 by ElmChan.*/
622- if (send_command_R3(mmcp, MMCSD_CMD_SEND_IF_COND,
623- MMCSD_CMD8_PATTERN, r3) != 0x05U) {
633+ if (mmc_send_command_R3(mmcp, MMCSD_CMD_SEND_IF_COND,
634+ MMCSD_CMD8_PATTERN, r3) != 0x05U) {
624635
625636 /* Switch to SDHC mode.*/
626637 i = 0;
627638 while (true) {
628639 /*lint -save -e9007 [13.5] Side effect unimportant.*/
629- if ((send_command_R1(mmcp, MMCSD_CMD_APP_CMD, 0) <= 0x01U) &&
630- (send_command_R3(mmcp, MMCSD_CMD_APP_OP_COND, 0x400001AAU, r3) == 0x00U)) {
640+ if ((mmc_send_command_R1(mmcp, MMCSD_CMD_APP_CMD, 0) <= 0x01U) &&
641+ (mmc_send_command_R3(mmcp, MMCSD_CMD_APP_OP_COND, 0x400001AAU, r3) == 0x00U)) {
631642 /*lint -restore*/
632643 break;
633644 }
@@ -639,7 +650,7 @@
639650 }
640651
641652 /* Execute dedicated read on OCR register */
642- (void) send_command_R3(mmcp, MMCSD_CMD_READ_OCR, 0, r3);
653+ (void) mmc_send_command_R3(mmcp, MMCSD_CMD_READ_OCR, 0, r3);
643654
644655 /* Check if CCS is set in response. Card operates in block mode if set.*/
645656 if ((r3[0] & 0x40U) != 0U) {
@@ -650,7 +661,7 @@
650661 /* Initialization.*/
651662 i = 0;
652663 while (true) {
653- uint8_t b = send_command_R1(mmcp, MMCSD_CMD_INIT, 0);
664+ uint8_t b = mmc_send_command_R1(mmcp, MMCSD_CMD_INIT, 0);
654665 if (b == 0x00U) {
655666 break;
656667 }
@@ -667,13 +678,13 @@
667678 spiStart(mmcp->config->spip, mmcp->config->hscfg);
668679
669680 /* Setting block size.*/
670- if (send_command_R1(mmcp, MMCSD_CMD_SET_BLOCKLEN,
671- MMCSD_BLOCK_SIZE) != 0x00U) {
681+ if (mmc_send_command_R1(mmcp, MMCSD_CMD_SET_BLOCKLEN,
682+ MMCSD_BLOCK_SIZE) != 0x00U) {
672683 goto failed;
673684 }
674685
675686 /* Determine capacity.*/
676- if (read_CxD(mmcp, MMCSD_CMD_SEND_CSD, mmcp->csd)) {
687+ if (mmc_read_CxD(mmcp, MMCSD_CMD_SEND_CSD, mmcp->csd)) {
677688 goto failed;
678689 }
679690
@@ -682,7 +693,7 @@
682693 goto failed;
683694 }
684695
685- if (read_CxD(mmcp, MMCSD_CMD_SEND_CID, mmcp->cid)) {
696+ if (mmc_read_CxD(mmcp, MMCSD_CMD_SEND_CID, mmcp->cid)) {
686697 goto failed;
687698 }
688699
@@ -702,9 +713,8 @@
702713 * @param[in] mmcp pointer to the @p MMCDriver object
703714 * @return The operation status.
704715 *
705- * @retval HAL_SUCCESS the operation succeeded and the driver is now
706- * in the @p MMC_INSERTED state.
707- * @retval HAL_FAILED the operation failed.
716+ * @retval HAL_SUCCESS if the operation succeeded.
717+ * @retval HAL_FAILED if the operation failed.
708718 *
709719 * @api
710720 */
@@ -724,7 +734,7 @@
724734
725735 /* Wait for the pending write operations to complete.*/
726736 spiStart(mmcp->config->spip, mmcp->config->hscfg);
727- sync(mmcp);
737+ mmc_wait_sync(mmcp);
728738
729739 spiStop(mmcp->config->spip);
730740 mmcp->state = BLK_ACTIVE;
@@ -738,8 +748,8 @@
738748 * @param[in] startblk first block to read
739749 *
740750 * @return The operation status.
741- * @retval HAL_SUCCESS the operation succeeded.
742- * @retval HAL_FAILED the operation failed.
751+ * @retval HAL_SUCCESS if the operation succeeded.
752+ * @retval HAL_FAILED if the operation failed.
743753 *
744754 * @api
745755 */
@@ -757,13 +767,13 @@
757767 spiSelect(mmcp->config->spip);
758768
759769 if (mmcp->block_addresses) {
760- send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk);
770+ (void) mmc_send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk); /* todo */
761771 }
762772 else {
763- send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk * MMCSD_BLOCK_SIZE);
773+ (void) mmc_send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk * MMCSD_BLOCK_SIZE); /* todo */
764774 }
765775
766- if (recvr1(mmcp) != 0x00U) {
776+ if (mmc_recvr1(mmcp) != 0x00U) {
767777 spiStop(mmcp->config->spip);
768778 mmcp->state = BLK_READY;
769779 return HAL_FAILED;
@@ -778,8 +788,8 @@
778788 * @param[out] buffer pointer to the read buffer
779789 *
780790 * @return The operation status.
781- * @retval HAL_SUCCESS the operation succeeded.
782- * @retval HAL_FAILED the operation failed.
791+ * @retval HAL_SUCCESS if the operation succeeded.
792+ * @retval HAL_FAILED if the operation failed.
783793 *
784794 * @api
785795 */
@@ -814,8 +824,8 @@
814824 * @param[in] mmcp pointer to the @p MMCDriver object
815825 *
816826 * @return The operation status.
817- * @retval HAL_SUCCESS the operation succeeded.
818- * @retval HAL_FAILED the operation failed.
827+ * @retval HAL_SUCCESS if the operation succeeded.
828+ * @retval HAL_FAILED if the operation failed.
819829 *
820830 * @api
821831 */
@@ -831,9 +841,9 @@
831841 }
832842
833843 spiSend(mmcp->config->spip, sizeof(stopcmd), stopcmd);
834-/* result = recvr1(mmcp) != 0x00U;*/
844+/* result = mmc_recvr1(mmcp) != 0x00U;*/
835845 /* Note, ignored r1 response, it can be not zero, unknown issue.*/
836- (void) recvr1(mmcp);
846+ (void) mmc_recvr1(mmcp);
837847
838848 /* Read operation finished.*/
839849 spiUnselect(mmcp->config->spip);
@@ -848,8 +858,8 @@
848858 * @param[in] startblk first block to write
849859 *
850860 * @return The operation status.
851- * @retval HAL_SUCCESS the operation succeeded.
852- * @retval HAL_FAILED the operation failed.
861+ * @retval HAL_SUCCESS if the operation succeeded.
862+ * @retval HAL_FAILED if the operation failed.
853863 *
854864 * @api
855865 */
@@ -864,14 +874,14 @@
864874 spiStart(mmcp->config->spip, mmcp->config->hscfg);
865875 spiSelect(mmcp->config->spip);
866876 if (mmcp->block_addresses) {
867- send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK, startblk);
877+ (void) mmc_send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK, startblk); /* todo */
868878 }
869879 else {
870- send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK,
871- startblk * MMCSD_BLOCK_SIZE);
880+ (void) mmc_send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK,
881+ startblk * MMCSD_BLOCK_SIZE); /* todo */
872882 }
873883
874- if (recvr1(mmcp) != 0x00U) {
884+ if (mmc_recvr1(mmcp) != 0x00U) {
875885 spiStop(mmcp->config->spip);
876886 mmcp->state = BLK_READY;
877887 return HAL_FAILED;
@@ -886,8 +896,8 @@
886896 * @param[out] buffer pointer to the write buffer
887897 *
888898 * @return The operation status.
889- * @retval HAL_SUCCESS the operation succeeded.
890- * @retval HAL_FAILED the operation failed.
899+ * @retval HAL_SUCCESS if the operation succeeded.
900+ * @retval HAL_FAILED if the operation failed.
891901 *
892902 * @api
893903 */
@@ -906,8 +916,7 @@
906916 spiIgnore(mmcp->config->spip, 2); /* CRC ignored. */
907917 spiReceive(mmcp->config->spip, 1, b);
908918 if ((b[0] & 0x1FU) == 0x05U) {
909- wait(mmcp);
910- return HAL_SUCCESS;
919+ return mmc_wait_idle(mmcp);
911920 }
912921
913922 /* Error.*/
@@ -923,8 +932,8 @@
923932 * @param[in] mmcp pointer to the @p MMCDriver object
924933 *
925934 * @return The operation status.
926- * @retval HAL_SUCCESS the operation succeeded.
927- * @retval HAL_FAILED the operation failed.
935+ * @retval HAL_SUCCESS if the operation succeeded.
936+ * @retval HAL_FAILED if the operation failed.
928937 *
929938 * @api
930939 */
@@ -951,8 +960,8 @@
951960 * @param[in] mmcp pointer to the @p MMCDriver object
952961 *
953962 * @return The operation status.
954- * @retval HAL_SUCCESS the operation succeeded.
955- * @retval HAL_FAILED the operation failed.
963+ * @retval HAL_SUCCESS if the operation succeeded.
964+ * @retval HAL_FAILED if the operation failed.
956965 *
957966 * @api
958967 */
@@ -968,7 +977,7 @@
968977 mmcp->state = BLK_SYNCING;
969978
970979 spiStart(mmcp->config->spip, mmcp->config->hscfg);
971- sync(mmcp);
980+ mmc_wait_sync(mmcp);
972981
973982 /* Synchronization operation finished.*/
974983 mmcp->state = BLK_READY;
@@ -982,8 +991,8 @@
982991 * @param[out] bdip pointer to a @p BlockDeviceInfo structure
983992 *
984993 * @return The operation status.
985- * @retval HAL_SUCCESS the operation succeeded.
986- * @retval HAL_FAILED the operation failed.
994+ * @retval HAL_SUCCESS if the operation succeeded.
995+ * @retval HAL_FAILED if the operation failed.
987996 *
988997 * @api
989998 */
@@ -1009,8 +1018,8 @@
10091018 * @param[in] endblk ending block number
10101019 *
10111020 * @return The operation status.
1012- * @retval HAL_SUCCESS the operation succeeded.
1013- * @retval HAL_FAILED the operation failed.
1021+ * @retval HAL_SUCCESS if the operation succeeded.
1022+ * @retval HAL_FAILED if the operation failed.
10141023 *
10151024 * @api
10161025 */
@@ -1027,15 +1036,15 @@
10271036 endblk *= MMCSD_BLOCK_SIZE;
10281037 }
10291038
1030- if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_START, startblk) != 0x00U) {
1039+ if (mmc_send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_START, startblk) != 0x00U) {
10311040 goto failed;
10321041 }
10331042
1034- if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_END, endblk) != 0x00U) {
1043+ if (mmc_send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_END, endblk) != 0x00U) {
10351044 goto failed;
10361045 }
10371046
1038- if (send_command_R1(mmcp, MMCSD_CMD_ERASE, 0) != 0x00U) {
1047+ if (mmc_send_command_R1(mmcp, MMCSD_CMD_ERASE, 0) != 0x00U) {
10391048 goto failed;
10401049 }
10411050
Show on old repository browser