SDFileSystem for STM32F746NG DISCOVERY with 4bit SDMMC interface on fixed pins

Dependencies:   FATFileSystem

Dependents:   DISCO-F746NG_SDFileSystem uzairkhan DISCO-F746NG_Scope_copy

Fork of SDFileSystem by Neil Thiessen

Committer:
DieterGraef
Date:
Sat Apr 16 13:01:50 2016 +0000
Revision:
27:8d192c180436
Parent:
24:698affe9560c
Changed hard locking mechanism to watchdog , solved issue with the sleep mode of SD cards

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 23:c03ef1abef0e 1 #include "SD_Helper.h"
DieterGraef 23:c03ef1abef0e 2 #include "stm32746g_discovery_sd.h"
DieterGraef 23:c03ef1abef0e 3
DieterGraef 23:c03ef1abef0e 4 #define _CCSIDR_LSSHIFT(x) (((x) & SCB_CCSIDR_LINESIZE_Msk) >> SCB_CCSIDR_LINESIZE_Pos)
DieterGraef 23:c03ef1abef0e 5
DieterGraef 23:c03ef1abef0e 6 static SD_HandleTypeDef uSdHandle;
DieterGraef 23:c03ef1abef0e 7 static int RX_Busy;
DieterGraef 23:c03ef1abef0e 8 static int TX_Busy;
DieterGraef 24:698affe9560c 9 static int SD_Busy;
DieterGraef 23:c03ef1abef0e 10
DieterGraef 23:c03ef1abef0e 11
DieterGraef 23:c03ef1abef0e 12
DieterGraef 23:c03ef1abef0e 13 /* cache flush */
DieterGraef 23:c03ef1abef0e 14 void CPU_CACHE_Flush(uint32_t * buffer, uint32_t length)
DieterGraef 23:c03ef1abef0e 15 {
DieterGraef 23:c03ef1abef0e 16 // SCB_InvalidateDCache_by_Addr(buffer,length);
DieterGraef 23:c03ef1abef0e 17 uint32_t ccsidr;
DieterGraef 23:c03ef1abef0e 18 uint32_t smask;
DieterGraef 23:c03ef1abef0e 19 uint32_t sshift;
DieterGraef 23:c03ef1abef0e 20 uint32_t ways;
DieterGraef 23:c03ef1abef0e 21 uint32_t wshift;
DieterGraef 23:c03ef1abef0e 22 uint32_t ssize;
DieterGraef 23:c03ef1abef0e 23 uint32_t sets;
DieterGraef 23:c03ef1abef0e 24 uint32_t sw;
DieterGraef 23:c03ef1abef0e 25 uint32_t start;
DieterGraef 23:c03ef1abef0e 26 uint32_t ende;
DieterGraef 23:c03ef1abef0e 27
DieterGraef 23:c03ef1abef0e 28 start=(uint32_t)buffer;
DieterGraef 23:c03ef1abef0e 29 ende=start+length;
DieterGraef 23:c03ef1abef0e 30 /* Get the characteristics of the D-Cache */
DieterGraef 23:c03ef1abef0e 31
DieterGraef 23:c03ef1abef0e 32 ccsidr = SCB->CCSIDR;
DieterGraef 23:c03ef1abef0e 33 smask = CCSIDR_SETS(ccsidr); /* (Number of sets) - 1 */
DieterGraef 23:c03ef1abef0e 34 sshift = _CCSIDR_LSSHIFT(ccsidr) + 4; /* log2(cache-line-size-in-bytes) */
DieterGraef 23:c03ef1abef0e 35 ways = CCSIDR_WAYS(ccsidr); /* (Number of ways) - 1 */
DieterGraef 23:c03ef1abef0e 36
DieterGraef 23:c03ef1abef0e 37 /* Calculate the bit offset for the way field in the DCCISW register by
DieterGraef 23:c03ef1abef0e 38 * counting the number of leading zeroes. For example:
DieterGraef 23:c03ef1abef0e 39 *
DieterGraef 23:c03ef1abef0e 40 * Number of Value of ways Field
DieterGraef 23:c03ef1abef0e 41 * Ways 'ways' Offset
DieterGraef 23:c03ef1abef0e 42 * 2 1 31
DieterGraef 23:c03ef1abef0e 43 * 4 3 30
DieterGraef 23:c03ef1abef0e 44 * 8 7 29
DieterGraef 23:c03ef1abef0e 45 * ...
DieterGraef 23:c03ef1abef0e 46 */
DieterGraef 23:c03ef1abef0e 47
DieterGraef 23:c03ef1abef0e 48 wshift = __CLZ(ways) & 0x1f;
DieterGraef 23:c03ef1abef0e 49
DieterGraef 23:c03ef1abef0e 50 /* Clean and invalidate the D-Cache over the range of addresses */
DieterGraef 23:c03ef1abef0e 51
DieterGraef 23:c03ef1abef0e 52 ssize = (1 << sshift);
DieterGraef 23:c03ef1abef0e 53 start &= ~(ssize - 1);
DieterGraef 23:c03ef1abef0e 54 __DSB();
DieterGraef 23:c03ef1abef0e 55
DieterGraef 23:c03ef1abef0e 56 do
DieterGraef 23:c03ef1abef0e 57 {
DieterGraef 23:c03ef1abef0e 58 int32_t tmpways = ways;
DieterGraef 23:c03ef1abef0e 59
DieterGraef 23:c03ef1abef0e 60 /* Isolate the cache line associated with this address. For example
DieterGraef 23:c03ef1abef0e 61 * if the cache line size is 32 bytes and the cache size is 16KB, then
DieterGraef 23:c03ef1abef0e 62 *
DieterGraef 23:c03ef1abef0e 63 * sshift = 5 : Offset to the beginning of the set field
DieterGraef 23:c03ef1abef0e 64 * smask = 0x007f : Mask of the set field
DieterGraef 23:c03ef1abef0e 65 */
DieterGraef 23:c03ef1abef0e 66
DieterGraef 23:c03ef1abef0e 67 sets = ((uint32_t)start >> sshift) & smask;
DieterGraef 23:c03ef1abef0e 68
DieterGraef 23:c03ef1abef0e 69 /* Clean and invalidate each way for this cacheline */
DieterGraef 23:c03ef1abef0e 70
DieterGraef 23:c03ef1abef0e 71 do
DieterGraef 23:c03ef1abef0e 72 {
DieterGraef 23:c03ef1abef0e 73 sw = ((tmpways << wshift) | (sets << sshift));
DieterGraef 23:c03ef1abef0e 74 SCB->DCCISW=sw;
DieterGraef 23:c03ef1abef0e 75
DieterGraef 23:c03ef1abef0e 76 }
DieterGraef 23:c03ef1abef0e 77 while (tmpways--);
DieterGraef 23:c03ef1abef0e 78
DieterGraef 23:c03ef1abef0e 79 /* Increment the address by the size of one cache line. */
DieterGraef 23:c03ef1abef0e 80
DieterGraef 23:c03ef1abef0e 81 start += ssize;
DieterGraef 23:c03ef1abef0e 82 }
DieterGraef 23:c03ef1abef0e 83 while (start < ende);
DieterGraef 23:c03ef1abef0e 84
DieterGraef 23:c03ef1abef0e 85 __DSB();
DieterGraef 23:c03ef1abef0e 86 __ISB();
DieterGraef 23:c03ef1abef0e 87 }
DieterGraef 23:c03ef1abef0e 88
DieterGraef 23:c03ef1abef0e 89 void BSP_SD_CommandTransaction(char cmd, unsigned int arg)
DieterGraef 23:c03ef1abef0e 90 {
DieterGraef 23:c03ef1abef0e 91 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
DieterGraef 23:c03ef1abef0e 92 uSdHandle.Instance = SDMMC1;
DieterGraef 23:c03ef1abef0e 93 sdmmc_cmdinitstructure.Argument = (uint32_t)arg;
DieterGraef 23:c03ef1abef0e 94 sdmmc_cmdinitstructure.CmdIndex = cmd;
DieterGraef 23:c03ef1abef0e 95 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
DieterGraef 23:c03ef1abef0e 96 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
DieterGraef 23:c03ef1abef0e 97 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
DieterGraef 23:c03ef1abef0e 98 SDMMC_SendCommand(uSdHandle.Instance, &sdmmc_cmdinitstructure);
DieterGraef 23:c03ef1abef0e 99 }
DieterGraef 23:c03ef1abef0e 100
DieterGraef 24:698affe9560c 101 void BSP_SD_Set_Busy(void)
DieterGraef 24:698affe9560c 102 {
DieterGraef 24:698affe9560c 103 SD_Busy=1;
DieterGraef 24:698affe9560c 104 }
DieterGraef 24:698affe9560c 105 void BSP_SD_Clear_Busy(void)
DieterGraef 24:698affe9560c 106 {
DieterGraef 24:698affe9560c 107 SD_Busy=0;
DieterGraef 24:698affe9560c 108 }
DieterGraef 24:698affe9560c 109 int BSP_SD_Get_Busy(void)
DieterGraef 24:698affe9560c 110 {
DieterGraef 24:698affe9560c 111 return SD_Busy;
DieterGraef 24:698affe9560c 112 }
DieterGraef 23:c03ef1abef0e 113
DieterGraef 23:c03ef1abef0e 114 void BSP_SD_Set_RX_Busy(void)
DieterGraef 23:c03ef1abef0e 115 {
DieterGraef 23:c03ef1abef0e 116 RX_Busy=1;
DieterGraef 23:c03ef1abef0e 117 }
DieterGraef 23:c03ef1abef0e 118 void BSP_SD_Clear_RX_Busy(void)
DieterGraef 23:c03ef1abef0e 119 {
DieterGraef 23:c03ef1abef0e 120 RX_Busy=0;
DieterGraef 23:c03ef1abef0e 121 }
DieterGraef 24:698affe9560c 122 int BSP_SD_Get_RX_Busy(void)
DieterGraef 23:c03ef1abef0e 123 {
DieterGraef 23:c03ef1abef0e 124 return RX_Busy;
DieterGraef 23:c03ef1abef0e 125 }
DieterGraef 23:c03ef1abef0e 126
DieterGraef 23:c03ef1abef0e 127 void BSP_SD_Set_TX_Busy(void)
DieterGraef 23:c03ef1abef0e 128 {
DieterGraef 23:c03ef1abef0e 129 TX_Busy=1;
DieterGraef 23:c03ef1abef0e 130 }
DieterGraef 23:c03ef1abef0e 131 void BSP_SD_Clear_TX_Busy(void)
DieterGraef 23:c03ef1abef0e 132 {
DieterGraef 23:c03ef1abef0e 133 TX_Busy=0;
DieterGraef 23:c03ef1abef0e 134 }
DieterGraef 24:698affe9560c 135 int BSP_SD_Get_TX_Busy(void)
DieterGraef 23:c03ef1abef0e 136 {
DieterGraef 23:c03ef1abef0e 137 return TX_Busy;
DieterGraef 23:c03ef1abef0e 138 }