annotate Common/Drivers/STM32F4xx_HAL_DRIVER_v120/Src/stm32f4xx_hal_sd.c @ 60:96026ba1834f

more language updates
author heinrichsweikamp
date Fri, 10 Aug 2018 19:40:55 +0200
parents 5f11787b4f42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 * @file stm32f4xx_hal_sd.c
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 * @author MCD Application Team
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 * @version V1.2.0
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 * @date 26-December-2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 * @brief SD card HAL module driver.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 * This file provides firmware functions to manage the following
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 * functionalities of the Secure Digital (SD) peripheral:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 * + Initialization and de-initialization functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 * + IO operation functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 * + Peripheral Control functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 * + Peripheral State functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 ##### How to use this driver #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 This driver implements a high level communication layer for read and write from/to
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 the user in HAL_SD_MspInit() function (MSP layer).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 Basically, the MSP layer configuration should be the same as we provide in the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 examples.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 You can easily tailor this configuration according to hardware resources.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 This driver is a generic layered driver for SDIO memories which uses the HAL
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 SDIO driver functions to interface with SD and uSD cards devices.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 It is used as follows:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 (##) SDIO pins configuration for SD card
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 and according to your pin assignment;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 and HAL_SD_WriteBlocks_DMA() APIs).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 (+++) Configure the SDIO and DMA interrupt priorities using functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 (+++) SDIO interrupts are managed using the macros __HAL_SD_SDIO_ENABLE_IT()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 and __HAL_SD_SDIO_DISABLE_IT() inside the communication process.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_SDIO_GET_IT()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 and __HAL_SD_SDIO_CLEAR_IT()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 *** SD Card Initialization and configuration ***
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 ================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 the SD Card and put it into Standby State (Ready for data transfer).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 This function provide the following operations:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 type (Standard Capacity or High Capacity). You can change or adapt this
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 frequency by adjusting the "ClockDiv" field.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 The SD Card frequency (SDIO_CK) is computed as follows:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 SDIO_CK = SDIOCLK / (ClockDiv + 2)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 In initialization mode and according to the SD Card standard,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 make sure that the SDIO_CK frequency doesn't exceed 400KHz.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 structure. This structure provide also ready computed SD Card capacity
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 and Block size.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 -@- These information are stored in SD handle structure in case of future use.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 frequency is set to 24MHz. You can change or adapt this frequency by adjusting
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 the "ClockDiv" field.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 In transfer mode and according to the SD Card standard, make sure that the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 To be able to use a frequency higher than 24MHz, you should use the SDIO
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 peripheral in bypass mode. Refer to the corresponding reference manual
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 for more details.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 (#) Select the corresponding SD Card according to the address read with the step 2.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 (#) Configure the SD Card in wide bus mode: 4-bits data.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 *** SD Card Read operation ***
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 ==============================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 This function support only 512-bytes block length (the block size should be
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 chosen as 512 bytes).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 You can choose either one block read operation or multiple block read operation
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 by adjusting the "NumberOfBlocks" parameter.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 This function support only 512-bytes block length (the block size should be
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 chosen as 512 bytes).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 You can choose either one block read operation or multiple block read operation
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 by adjusting the "NumberOfBlocks" parameter.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 After this, you have to call the function HAL_SD_CheckReadOperation(), to insure
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 that the read transfer is done correctly in both DMA and SD sides.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 *** SD Card Write operation ***
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 ===============================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 This function support only 512-bytes block length (the block size should be
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 chosen as 512 bytes).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 You can choose either one block read operation or multiple block read operation
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 by adjusting the "NumberOfBlocks" parameter.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 This function support only 512-bytes block length (the block size should be
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 chosen as 512 byte).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 You can choose either one block read operation or multiple block read operation
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 by adjusting the "NumberOfBlocks" parameter.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 that the write transfer is done correctly in both DMA and SD sides.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 *** SD card status ***
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 ======================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 (+) At any time, you can check the SD Card status and get the SD card state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 by using the HAL_SD_GetStatus() function. This function checks first if the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 SD card is still connected and then get the internal SD Card transfer state.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 function.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 *** SD HAL driver macros list ***
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 ==================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 Below the list of most used macros in SD HAL driver.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137 (+) __HAL_SD_SDIO_ENABLE : Enable the SD device
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 (+) __HAL_SD_SDIO_DISABLE : Disable the SD device
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 (+) __HAL_SD_SDIO_DMA_ENABLE: Enable the SDIO DMA transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 (+) __HAL_SD_SDIO_DMA_DISABLE: Disable the SDIO DMA transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 (+) __HAL_SD_SDIO_ENABLE_IT: Enable the SD device interrupt
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 (+) __HAL_SD_SDIO_DISABLE_IT: Disable the SD device interrupt
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143 (+) __HAL_SD_SDIO_GET_FLAG:Check whether the specified SD flag is set or not
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 (+) __HAL_SD_SDIO_CLEAR_FLAG: Clear the SD's pending flags
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 (@) You can refer to the SD HAL driver header file for more useful macros
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 * @attention
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 * Redistribution and use in source and binary forms, with or without modification,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 * are permitted provided that the following conditions are met:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 * 1. Redistributions of source code must retain the above copyright notice,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 * this list of conditions and the following disclaimer.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 * 2. Redistributions in binary form must reproduce the above copyright notice,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 * this list of conditions and the following disclaimer in the documentation
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 * and/or other materials provided with the distribution.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 * 3. Neither the name of STMicroelectronics nor the names of its contributors
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 * may be used to endorse or promote products derived from this software
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163 * without specific prior written permission.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
173 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
174 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
175 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
176 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
177 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
178
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
179 /* Includes ------------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
180 #include "stm32f4xx_hal.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
181
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
182 #ifdef HAL_SD_MODULE_ENABLED
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
183
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
184 /** @addtogroup STM32F4xx_HAL_Driver
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
185 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
186 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
187
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
188 /** @addtogroup SD
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
189 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
190 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
191
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
192 /* Private typedef -----------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
193 /* Private define ------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
194 /** @addtogroup SD_Private_Defines
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
195 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
196 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
197 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
198 * @brief SDIO Data block size
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
199 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
200 #define DATA_BLOCK_SIZE ((uint32_t)(9 << 4))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
201 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
202 * @brief SDIO Static flags, Timeout, FIFO Address
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
203 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
204 #define SDIO_STATIC_FLAGS ((uint32_t)(SDIO_FLAG_CCRCFAIL | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_CTIMEOUT |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
205 SDIO_FLAG_DTIMEOUT | SDIO_FLAG_TXUNDERR | SDIO_FLAG_RXOVERR |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
206 SDIO_FLAG_CMDREND | SDIO_FLAG_CMDSENT | SDIO_FLAG_DATAEND |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
207 SDIO_FLAG_DBCKEND))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
208
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
209 #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
210
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
211 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
212 * @brief Mask for errors Card Status R1 (OCR Register)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
213 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
214 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
215 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
216 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
217 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
218 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
219 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
220 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
221 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
222 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
223 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
224 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
225 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
226 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
227 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
228 #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
229 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
230 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
231 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
232 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
233 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
234
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
235 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
236 * @brief Masks for R6 Response
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
237 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
238 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
239 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
240 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
241
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
242 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
243 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
244 #define SD_STD_CAPACITY ((uint32_t)0x00000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
245 #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
246
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
247 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
248 #define SD_ALLZERO ((uint32_t)0x00000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
249
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
250 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
251 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
252 #define SD_CARD_LOCKED ((uint32_t)0x02000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
253
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
254 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
255 #define SD_0TO7BITS ((uint32_t)0x000000FF)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
256 #define SD_8TO15BITS ((uint32_t)0x0000FF00)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
257 #define SD_16TO23BITS ((uint32_t)0x00FF0000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
258 #define SD_24TO31BITS ((uint32_t)0xFF000000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
259 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
260
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
261 #define SD_HALFFIFO ((uint32_t)0x00000008)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
262 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
263
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
264 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
265 * @brief Command Class Supported
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
266 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
267 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
268 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
269 #define SD_CCCC_ERASE ((uint32_t)0x00000020)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
270
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
271 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
272 * @brief Following commands are SD Card Specific commands.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
273 * SDIO_APP_CMD should be sent before sending these commands.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
274 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
275 #define SD_SDIO_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
276
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
277 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
278 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
279 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
280
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
281 /* Private macro -------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
282 /* Private variables ---------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
283 /* Private function prototypes -----------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
284 /** @addtogroup SD_Private_Functions_Prototypes
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
285 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
286 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
287 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
288 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
289 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
290 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
291 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
292 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
293 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
294 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
295 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
296 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
297 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
298 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
299 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
300 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
301 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
302 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
303 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
304 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
305 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
306 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
307 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
308 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
309 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
310 /* Exported functions --------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
311 /** @addtogroup SD_Exported_Functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
312 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
313 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
314
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
315 /** @addtogroup SD_Exported_Functions_Group1
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
316 * @brief Initialization and de-initialization functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
317 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
318 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
319 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
320 ##### Initialization and de-initialization functions #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
321 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
322 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
323 This section provides functions allowing to initialize/de-initialize the SD
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
324 card device to be ready for use.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
325
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
326
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
327 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
328 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
329 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
330
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
331 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
332 * @brief Initializes the SD card according to the specified parameters in the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
333 SD_HandleTypeDef and create the associated handle.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
334 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
335 * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
336 * @retval HAL SD error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
337 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
338 HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
339 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
340 __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
341 SD_InitTypeDef tmpinit;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
342
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
343 /* Initialize the low level hardware (MSP) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
344 HAL_SD_MspInit(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
345
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
346 /* Default SDIO peripheral configuration for SD card initialization */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
347 tmpinit.ClockEdge = SDIO_CLOCK_EDGE_RISING;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
348 tmpinit.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
349 tmpinit.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
350 tmpinit.BusWide = SDIO_BUS_WIDE_1B;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
351 tmpinit.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
352 tmpinit.ClockDiv = SDIO_INIT_CLK_DIV;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
353
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
354 /* Initialize SDIO peripheral interface with default configuration */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
355 SDIO_Init(hsd->Instance, tmpinit);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
356
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
357 /* Identify card operating voltage */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
358 errorstate = SD_PowerON(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
359
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
360 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
361 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
362 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
363 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
364
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
365 /* Initialize the present SDIO card(s) and put them in idle state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
366 errorstate = SD_Initialize_Cards(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
367
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
368 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
369 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
370 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
371 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
372
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
373 /* Read CSD/CID MSD registers */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
374 errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
375
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
376 if (errorstate == SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
377 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
378 /* Select the Card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
379 errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
380 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
381
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
382 /* Configure SDIO peripheral interface */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
383 SDIO_Init(hsd->Instance, hsd->Init);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
384
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
385 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
386 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
387
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
388 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
389 * @brief De-Initializes the SD card.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
390 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
391 * @retval HAL status
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
392 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
393 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
394 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
395
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
396 /* Set SD power state to off */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
397 SD_PowerOFF(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
398
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
399 /* De-Initialize the MSP layer */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
400 HAL_SD_MspDeInit(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
401
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
402 return HAL_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
403 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
404
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
405
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
406 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
407 * @brief Initializes the SD MSP.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
408 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
409 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
410 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
411 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
412 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
413 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
414 the HAL_SD_MspInit could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
415 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
416 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
417
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
418 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
419 * @brief De-Initialize SD MSP.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
420 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
421 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
422 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
423 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
424 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
425 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
426 the HAL_SD_MspDeInit could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
427 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
428 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
429
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
430 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
431 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
432 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
433
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
434 /** @addtogroup SD_Exported_Functions_Group2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
435 * @brief Data transfer functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
436 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
437 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
438 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
439 ##### IO operation functions #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
440 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
441 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
442 This subsection provides a set of functions allowing to manage the data
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
443 transfer from/to SD card.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
444
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
445 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
446 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
447 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
448
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
449 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
450 * @brief Reads block(s) from a specified address in a card. The Data transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
451 * is managed by polling mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
452 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
453 * @param pReadBuffer: pointer to the buffer that will contain the received data
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
454 * @param ReadAddr: Address from where data is to be read
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
455 * @param BlockSize: SD card Data block size
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
456 * @note BlockSize must be 512 bytes.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
457 * @param NumberOfBlocks: Number of SD blocks to read
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
458 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
459 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
460 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
461 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
462 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
463 SDIO_DataInitTypeDef sdio_datainitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
464 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
465 uint32_t count = 0, *tempbuff = (uint32_t *)pReadBuffer;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
466
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
467 /* Initialize data control register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
468 hsd->Instance->DCTRL = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
469
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
470 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
471 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
472 BlockSize = 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
473 ReadAddr /= 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
474 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
475
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
476 /* Set Block Size for Card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
477 sdio_cmdinitstructure.Argument = (uint32_t) BlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
478 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
479 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
480 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
481 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
482 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
483
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
484 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
485 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
486
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
487 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
488 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
489 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
490 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
491
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
492 /* Configure the SD DPSM (Data Path State Machine) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
493 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
494 sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
495 sdio_datainitstructure.DataBlockSize = DATA_BLOCK_SIZE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
496 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
497 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
498 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
499 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
500
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
501 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
502 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
503 /* Send CMD18 READ_MULT_BLOCK with argument data address */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
504 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
505 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
506 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
507 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
508 /* Send CMD17 READ_SINGLE_BLOCK */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
509 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
510 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
511
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
512 sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
513 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
514
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
515 /* Read block(s) in polling mode */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
516 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
517 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
518 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
519 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
520
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
521 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
522 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
523 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
524 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
525
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
526 /* Poll on SDIO flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
527 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
528 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
529 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
530 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
531 /* Read data from SDIO Rx FIFO */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
532 for (count = 0; count < 8; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
533 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
534 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
535 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
536
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
537 tempbuff += 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
538 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
539 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
540 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
541 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
542 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
543 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
544 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
545
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
546 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
547 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
548 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
549 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
550
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
551 /* In case of single block transfer, no need of stop transfer at all */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
552 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
553 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
554 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
555 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
556 /* Read data from SDIO Rx FIFO */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
557 for (count = 0; count < 8; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
558 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
559 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
560 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
561
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
562 tempbuff += 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
563 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
564 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
565 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
566
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
567 /* Send stop transmission command in case of multiblock read */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
568 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
569 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
570 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
571 (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
572 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
573 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
574 /* Send stop transmission command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
575 errorstate = HAL_SD_StopTransfer(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
576 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
577 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
578
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
579 /* Get error state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
580 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
581 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
582 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
583
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
584 errorstate = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
585
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
586 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
587 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
588 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
589 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
590 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
591
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
592 errorstate = SD_DATA_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
593
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
594 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
595 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
596 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
597 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
598 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
599
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
600 errorstate = SD_RX_OVERRUN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
601
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
602 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
603 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
604 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
605 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
606 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
607
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
608 errorstate = SD_START_BIT_ERR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
609
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
610 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
611 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
612 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
613 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
614 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
615 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
616
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
617 count = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
618
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
619 /* Empty FIFO if there is still any data */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
620 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
621 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
622 *tempbuff = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
623 tempbuff++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
624 count--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
625 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
626
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
627 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
628 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
629
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
630 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
631 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
632
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
633 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
634 * @brief Allows to write block(s) to a specified address in a card. The Data
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
635 * transfer is managed by polling mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
636 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
637 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
638 * @param WriteAddr: Address from where data is to be written
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
639 * @param BlockSize: SD card Data block size
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
640 * @note BlockSize must be 512 bytes.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
641 * @param NumberOfBlocks: Number of SD blocks to write
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
642 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
643 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
644 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
645 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
646 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
647 SDIO_DataInitTypeDef sdio_datainitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
648 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
649 uint32_t totalnumberofbytes = 0, bytestransferred = 0, count = 0, restwords = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
650 uint32_t *tempbuff = (uint32_t *)pWriteBuffer;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
651 uint8_t cardstate = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
652
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
653 /* Initialize data control register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
654 hsd->Instance->DCTRL = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
655
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
656 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
657 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
658 BlockSize = 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
659 WriteAddr /= 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
660 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
661
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
662 /* Set Block Size for Card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
663 sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
664 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
665 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
666 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
667 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
668 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
669
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
670 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
671 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
672
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
673 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
674 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
675 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
676 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
677
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
678 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
679 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
680 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
681 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
682 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
683 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
684 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
685 /* Send CMD24 WRITE_SINGLE_BLOCK */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
686 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
687 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
688
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
689 sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
690 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
691
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
692 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
693 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
694 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
695 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
696 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
697 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
698 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
699 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
700 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
701
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
702 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
703 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
704 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
705 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
706
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
707 /* Set total number of bytes to write */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
708 totalnumberofbytes = NumberOfBlocks * BlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
709
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
710 /* Configure the SD DPSM (Data Path State Machine) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
711 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
712 sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
713 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
714 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
715 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
716 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
717 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
718
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
719 /* Write block(s) in polling mode */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
720 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
721 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
722 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
723 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
724 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
725 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
726 if ((totalnumberofbytes - bytestransferred) < 32)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
727 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
728 restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
729
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
730 /* Write data to SDIO Tx FIFO */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
731 for (count = 0; count < restwords; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
732 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
733 SDIO_WriteFIFO(hsd->Instance, tempbuff);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
734 tempbuff++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
735 bytestransferred += 4;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
736 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
737 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
738 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
739 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
740 /* Write data to SDIO Tx FIFO */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
741 for (count = 0; count < 8; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
742 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
743 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
744 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
745
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
746 tempbuff += 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
747 bytestransferred += 32;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
748 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
749 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
750 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
751 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
752 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
753 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
754 /* In case of single data block transfer no need of stop command at all */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
755 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
756 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
757 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
758 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
759 if ((totalnumberofbytes - bytestransferred) < 32)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
760 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
761 restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
762
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
763 /* Write data to SDIO Tx FIFO */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
764 for (count = 0; count < restwords; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
765 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
766 SDIO_WriteFIFO(hsd->Instance, tempbuff);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
767 tempbuff++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
768 bytestransferred += 4;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
769 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
770 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
771 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
772 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
773 /* Write data to SDIO Tx FIFO */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
774 for (count = 0; count < 8; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
775 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
776 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
777 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
778
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
779 tempbuff += 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
780 bytestransferred += 32;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
781 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
782 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
783 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
784 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
785
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
786 /* Send stop transmission command in case of multiblock write */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
787 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
788 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
789 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
790 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
791 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
792 /* Send stop transmission command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
793 errorstate = HAL_SD_StopTransfer(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
794 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
795 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
796
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
797 /* Get error state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
798 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
799 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
800 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
801
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
802 errorstate = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
803
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
804 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
805 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
806 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
807 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
808 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
809
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
810 errorstate = SD_DATA_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
811
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
812 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
813 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
814 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
815 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
816 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
817
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
818 errorstate = SD_TX_UNDERRUN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
819
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
820 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
821 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
822 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
823 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
824 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
825
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
826 errorstate = SD_START_BIT_ERR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
827
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
828 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
829 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
830 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
831 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
832 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
833 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
834
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
835 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
836 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
837
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
838 /* Wait till the card is in programming state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
839 errorstate = SD_IsCardProgramming(hsd, &cardstate);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
840
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
841 while ((errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
842 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
843 errorstate = SD_IsCardProgramming(hsd, &cardstate);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
844 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
845
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
846 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
847 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
848
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
849 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
850 * @brief Reads block(s) from a specified address in a card. The Data transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
851 * is managed by DMA mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
852 * @note This API should be followed by the function HAL_SD_CheckReadOperation()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
853 * to check the completion of the read process
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
854 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
855 * @param pReadBuffer: Pointer to the buffer that will contain the received data
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
856 * @param ReadAddr: Address from where data is to be read
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
857 * @param BlockSize: SD card Data block size
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
858 * @note BlockSize must be 512 bytes.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
859 * @param NumberOfBlocks: Number of blocks to read.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
860 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
861 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
862 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
863 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
864 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
865 SDIO_DataInitTypeDef sdio_datainitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
866 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
867
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
868 /* Initialize data control register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
869 hsd->Instance->DCTRL = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
870
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
871 /* Initialize handle flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
872 hsd->SdTransferCplt = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
873 hsd->DmaTransferCplt = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
874 hsd->SdTransferErr = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
875
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
876 /* Initialize SD Read operation */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
877 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
878 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
879 hsd->SdOperation = SD_READ_MULTIPLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
880 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
881 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
882 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
883 hsd->SdOperation = SD_READ_SINGLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
884 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
885
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
886 /* Enable transfer interrupts */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
887 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
888 SDIO_IT_DTIMEOUT |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
889 SDIO_IT_DATAEND |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
890 SDIO_IT_RXOVERR |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
891 SDIO_IT_STBITERR));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
892
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
893 /* Enable SDIO DMA transfer */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
894 __HAL_SD_SDIO_DMA_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
895
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
896 /* Configure DMA user callbacks */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
897 hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
898 hsd->hdmarx->XferErrorCallback = SD_DMA_RxError;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
899
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
900 /* Enable the DMA Stream */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
901 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks)/4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
902
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
903 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
904 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
905 BlockSize = 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
906 ReadAddr /= 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
907 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
908
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
909 /* Set Block Size for Card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
910 sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
911 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
912 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
913 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
914 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
915 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
916
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
917 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
918 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
919
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
920 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
921 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
922 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
923 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
924
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
925 /* Configure the SD DPSM (Data Path State Machine) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
926 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
927 sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
928 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
929 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
930 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
931 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
932 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
933
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
934 /* Check number of blocks command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
935 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
936 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
937 /* Send CMD18 READ_MULT_BLOCK with argument data address */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
938 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
939 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
940 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
941 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
942 /* Send CMD17 READ_SINGLE_BLOCK */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
943 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
944 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
945
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
946 sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
947 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
948
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
949 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
950 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
951 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
952 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
953 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
954 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
955 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
956 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
957 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
958
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
959 /* Update the SD transfer error in SD handle */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
960 hsd->SdTransferErr = errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
961
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
962 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
963 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
964
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
965
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
966 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
967 * @brief Writes block(s) to a specified address in a card. The Data transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
968 * is managed by DMA mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
969 * @note This API should be followed by the function HAL_SD_CheckWriteOperation()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
970 * to check the completion of the write process (by SD current status polling).
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
971 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
972 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
973 * @param WriteAddr: Address from where data is to be read
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
974 * @param BlockSize: the SD card Data block size
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
975 * @note BlockSize must be 512 bytes.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
976 * @param NumberOfBlocks: Number of blocks to write
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
977 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
978 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
979 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
980 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
981 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
982 SDIO_DataInitTypeDef sdio_datainitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
983 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
984
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
985 /* Initialize data control register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
986 hsd->Instance->DCTRL = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
987
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
988 /* Initialize handle flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
989 hsd->SdTransferCplt = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
990 hsd->DmaTransferCplt = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
991 hsd->SdTransferErr = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
992
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
993 /* Initialize SD Write operation */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
994 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
995 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
996 hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
997 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
998 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
999 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1000 hsd->SdOperation = SD_WRITE_SINGLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1001 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1002
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1003 /* Enable transfer interrupts */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1004 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1005 SDIO_IT_DTIMEOUT |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1006 SDIO_IT_DATAEND |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1007 SDIO_IT_TXUNDERR |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1008 SDIO_IT_STBITERR));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1009
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1010 /* Configure DMA user callbacks */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1011 hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1012 hsd->hdmatx->XferErrorCallback = SD_DMA_TxError;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1013
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1014 /* Enable the DMA Stream */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1015 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BlockSize * NumberOfBlocks)/4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1016
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1017 /* Enable SDIO DMA transfer */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1018 __HAL_SD_SDIO_DMA_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1019
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1020 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1021 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1022 BlockSize = 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1023 WriteAddr /= 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1024 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1025
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1026 /* Set Block Size for Card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1027 sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1028 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1029 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1030 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1031 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1032 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1033
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1034 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1035 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1036
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1037 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1038 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1039 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1040 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1041
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1042 /* Check number of blocks command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1043 if(NumberOfBlocks <= 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1044 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1045 /* Send CMD24 WRITE_SINGLE_BLOCK */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1046 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1047 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1048 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1049 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1050 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1051 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1052 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1053
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1054 sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1055 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1056
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1057 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1058 if(NumberOfBlocks > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1059 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1060 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1061 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1062 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1063 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1064 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1065 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1066
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1067 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1068 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1069 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1070 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1071
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1072 /* Configure the SD DPSM (Data Path State Machine) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1073 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1074 sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1075 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1076 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1077 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1078 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1079 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1080
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1081 hsd->SdTransferErr = errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1082
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1083 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1084 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1085
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1086 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1087 * @brief This function waits until the SD DMA data read transfer is finished.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1088 * This API should be called after HAL_SD_ReadBlocks_DMA() function
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1089 * to insure that all data sent by the card is already transferred by the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1090 * DMA controller.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1091 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1092 * @param Timeout: Timeout duration
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1093 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1094 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1095 HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1096 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1097 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1098 uint32_t timeout = Timeout;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1099 uint32_t tmp1, tmp2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1100 HAL_SD_ErrorTypedef tmp3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1101
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1102 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1103 tmp1 = hsd->DmaTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1104 tmp2 = hsd->SdTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1105 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1106
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1107 while ((tmp1 == 0) && (tmp2 == 0) && (tmp3 == SD_OK) && (timeout > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1108 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1109 tmp1 = hsd->DmaTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1110 tmp2 = hsd->SdTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1111 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1112 timeout--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1113 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1114
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1115 timeout = Timeout;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1116
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1117 /* Wait until the Rx transfer is no longer active */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1118 while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXACT)) && (timeout > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1119 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1120 timeout--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1121 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1122
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1123 /* Send stop command in multiblock read */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1124 if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1125 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1126 errorstate = HAL_SD_StopTransfer(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1127 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1128
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1129 if ((timeout == 0) && (errorstate == SD_OK))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1130 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1131 errorstate = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1132 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1133
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1134 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1135 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1136
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1137 /* Return error state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1138 if (hsd->SdTransferErr != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1139 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1140 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1141 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1142
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1143 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1144 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1145
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1146 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1147 * @brief This function waits until the SD DMA data write transfer is finished.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1148 * This API should be called after HAL_SD_WriteBlocks_DMA() function
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1149 * to insure that all data sent by the card is already transferred by the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1150 * DMA controller.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1151 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1152 * @param Timeout: Timeout duration
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1153 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1154 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1155 HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1156 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1157 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1158 uint32_t timeout = Timeout;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1159 uint32_t tmp1, tmp2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1160 HAL_SD_ErrorTypedef tmp3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1161
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1162 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1163 tmp1 = hsd->DmaTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1164 tmp2 = hsd->SdTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1165 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1166
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1167 while ((tmp1 == 0) && (tmp2 == 0) && (tmp3 == SD_OK) && (timeout > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1168 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1169 tmp1 = hsd->DmaTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1170 tmp2 = hsd->SdTransferCplt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1171 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1172 timeout--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1173 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1174
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1175 timeout = Timeout;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1176
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1177 /* Wait until the Tx transfer is no longer active */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1178 while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXACT)) && (timeout > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1179 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1180 timeout--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1181 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1182
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1183 /* Send stop command in multiblock write */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1184 if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1185 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1186 errorstate = HAL_SD_StopTransfer(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1187 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1188
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1189 if ((timeout == 0) && (errorstate == SD_OK))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1190 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1191 errorstate = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1192 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1193
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1194 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1195 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1196
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1197 /* Return error state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1198 if (hsd->SdTransferErr != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1199 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1200 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1201 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1202
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1203 /* Wait until write is complete */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1204 while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1205 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1206 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1207
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1208 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1209 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1210
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1211 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1212 * @brief Erases the specified memory area of the given SD card.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1213 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1214 * @param startaddr: Start byte address
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1215 * @param endaddr: End byte address
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1216 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1217 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1218 HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1219 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1220 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1221 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1222
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1223 uint32_t delay = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1224 __IO uint32_t maxdelay = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1225 uint8_t cardstate = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1226
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1227 /* Check if the card command class supports erase command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1228 if (((hsd->CSD[1] >> 20) & SD_CCCC_ERASE) == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1229 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1230 errorstate = SD_REQUEST_NOT_APPLICABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1231
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1232 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1233 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1234
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1235 /* Get max delay value */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1236 maxdelay = 120000 / (((hsd->Instance->CLKCR) & 0xFF) + 2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1237
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1238 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1239 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1240 errorstate = SD_LOCK_UNLOCK_FAILED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1241
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1242 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1243 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1244
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1245 /* Get start and end block for high capacity cards */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1246 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1247 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1248 startaddr /= 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1249 endaddr /= 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1250 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1251
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1252 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1253 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1254 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1255 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1256 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1257 sdio_cmdinitstructure.Argument =(uint32_t)startaddr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1258 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1259 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1260 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1261 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1262 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1263
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1264 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1265 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1266
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1267 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1268 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1269 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1270 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1271
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1272 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1273 sdio_cmdinitstructure.Argument = (uint32_t)endaddr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1274 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1275 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1276
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1277 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1278 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1279
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1280 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1281 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1282 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1283 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1284 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1285
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1286 /* Send CMD38 ERASE */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1287 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1288 sdio_cmdinitstructure.CmdIndex = SD_CMD_ERASE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1289 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1290
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1291 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1292 errorstate = SD_CmdResp1Error(hsd, SD_CMD_ERASE);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1293
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1294 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1295 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1296 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1297 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1298
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1299 for (; delay < maxdelay; delay++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1300 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1301 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1302
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1303 /* Wait until the card is in programming state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1304 errorstate = SD_IsCardProgramming(hsd, &cardstate);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1305
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1306 delay = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1307
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1308 while ((delay > 0) && (errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1309 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1310 errorstate = SD_IsCardProgramming(hsd, &cardstate);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1311 delay--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1312 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1313
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1314 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1315 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1316
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1317 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1318 * @brief This function handles SD card interrupt request.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1319 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1320 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1321 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1322 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1323 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1324 /* Check for SDIO interrupt flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1325 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DATAEND))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1326 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1327 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_IT_DATAEND);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1328
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1329 /* SD transfer is complete */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1330 hsd->SdTransferCplt = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1331
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1332 /* No transfer error */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1333 hsd->SdTransferErr = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1334
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1335 HAL_SD_XferCpltCallback(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1336 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1337 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1338 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1339 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1340
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1341 hsd->SdTransferErr = SD_DATA_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1342
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1343 HAL_SD_XferErrorCallback(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1344
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1345 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1346 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1347 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1348 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1349
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1350 hsd->SdTransferErr = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1351
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1352 HAL_SD_XferErrorCallback(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1353 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1354 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_RXOVERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1355 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1356 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1357
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1358 hsd->SdTransferErr = SD_RX_OVERRUN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1359
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1360 HAL_SD_XferErrorCallback(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1361 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1362 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_TXUNDERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1363 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1364 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1365
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1366 hsd->SdTransferErr = SD_TX_UNDERRUN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1367
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1368 HAL_SD_XferErrorCallback(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1369 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1370 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1371 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1372 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1373
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1374 hsd->SdTransferErr = SD_START_BIT_ERR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1375
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1376 HAL_SD_XferErrorCallback(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1377 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1378 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1379 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1380 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1381 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1382
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1383 /* Disable all SDIO peripheral interrupt sources */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1384 __HAL_SD_SDIO_DISABLE_IT(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1385 SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1386 SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1387 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1388
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1389
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1390 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1391 * @brief SD end of transfer callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1392 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1393 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1394 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1395 __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1396 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1397 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1398 the HAL_SD_XferCpltCallback could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1399 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1400 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1401
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1402 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1403 * @brief SD Transfer Error callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1404 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1405 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1406 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1407 __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1408 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1409 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1410 the HAL_SD_XferErrorCallback could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1411 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1412 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1413
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1414 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1415 * @brief SD Transfer complete Rx callback in non blocking mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1416 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1417 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1418 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1419 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1420 __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1421 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1422 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1423 the HAL_SD_DMA_RxCpltCallback could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1424 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1425 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1426
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1427 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1428 * @brief SD DMA transfer complete Rx error callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1429 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1430 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1431 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1432 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1433 __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1434 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1435 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1436 the HAL_SD_DMA_RxErrorCallback could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1437 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1438 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1439
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1440 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1441 * @brief SD Transfer complete Tx callback in non blocking mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1442 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1443 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1444 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1445 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1446 __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1447 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1448 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1449 the HAL_SD_DMA_TxCpltCallback could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1450 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1451 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1452
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1453 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1454 * @brief SD DMA transfer complete error Tx callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1455 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1456 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1457 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1458 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1459 __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1460 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1461 /* NOTE : This function Should not be modified, when the callback is needed,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1462 the HAL_SD_DMA_TxErrorCallback could be implemented in the user file
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1463 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1464 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1465
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1466 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1467 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1468 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1469
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1470 /** @addtogroup SD_Exported_Functions_Group3
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1471 * @brief management functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1472 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1473 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1474 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1475 ##### Peripheral Control functions #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1476 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1477 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1478 This subsection provides a set of functions allowing to control the SD card
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1479 operations.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1480
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1481 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1482 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1483 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1484
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1485 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1486 * @brief Returns information about specific card.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1487 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1488 * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1489 * contains all SD cardinformation
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1490 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1491 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1492 HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1493 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1494 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1495 uint32_t tmp = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1496
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1497 pCardInfo->CardType = (uint8_t)(hsd->CardType);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1498 pCardInfo->RCA = (uint16_t)(hsd->RCA);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1499
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1500 /* Byte 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1501 tmp = (hsd->CSD[0] & 0xFF000000) >> 24;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1502 pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1503 pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1504 pCardInfo->SD_csd.Reserved1 = tmp & 0x03;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1505
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1506 /* Byte 1 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1507 tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1508 pCardInfo->SD_csd.TAAC = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1509
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1510 /* Byte 2 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1511 tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1512 pCardInfo->SD_csd.NSAC = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1513
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1514 /* Byte 3 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1515 tmp = hsd->CSD[0] & 0x000000FF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1516 pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1517
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1518 /* Byte 4 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1519 tmp = (hsd->CSD[1] & 0xFF000000) >> 24;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1520 pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1521
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1522 /* Byte 5 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1523 tmp = (hsd->CSD[1] & 0x00FF0000) >> 16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1524 pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1525 pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0F);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1526
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1527 /* Byte 6 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1528 tmp = (hsd->CSD[1] & 0x0000FF00) >> 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1529 pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1530 pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1531 pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1532 pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1533 pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1534
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1535 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1536 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1537 pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1538
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1539 /* Byte 7 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1540 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1541 pCardInfo->SD_csd.DeviceSize |= (tmp) << 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1542
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1543 /* Byte 8 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1544 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1545 pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1546
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1547 pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1548 pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1549
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1550 /* Byte 9 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1551 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1552 pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1553 pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1554 pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1555 /* Byte 10 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1556 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1557 pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1558
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1559 pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1) ;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1560 pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1561 pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1562 pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1563 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1564 else if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1565 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1566 /* Byte 7 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1567 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1568 pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1569
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1570 /* Byte 8 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1571 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1572
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1573 pCardInfo->SD_csd.DeviceSize |= (tmp << 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1574
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1575 /* Byte 9 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1576 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1577
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1578 pCardInfo->SD_csd.DeviceSize |= (tmp);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1579
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1580 /* Byte 10 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1581 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1582
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1583 pCardInfo->CardCapacity = (uint64_t)((((uint64_t)pCardInfo->SD_csd.DeviceSize + 1)) * 512 * 1024);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1584 pCardInfo->CardBlockSize = 512;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1585 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1586 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1587 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1588 /* Not supported card type */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1589 errorstate = SD_ERROR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1590 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1591
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1592 pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1593 pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1594
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1595 /* Byte 11 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1596 tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1597 pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1598 pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1599
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1600 /* Byte 12 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1601 tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1602 pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1603 pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1604 pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1605 pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1606
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1607 /* Byte 13 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1608 tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1609 pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1610 pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1611 pCardInfo->SD_csd.Reserved3 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1612 pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1613
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1614 /* Byte 14 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1615 tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1616 pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1617 pCardInfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1618 pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1619 pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1620 pCardInfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1621 pCardInfo->SD_csd.ECC = (tmp & 0x03);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1622
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1623 /* Byte 15 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1624 tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1625 pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1626 pCardInfo->SD_csd.Reserved4 = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1627
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1628 /* Byte 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1629 tmp = (uint8_t)((hsd->CID[0] & 0xFF000000) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1630 pCardInfo->SD_cid.ManufacturerID = tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1631
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1632 /* Byte 1 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1633 tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1634 pCardInfo->SD_cid.OEM_AppliID = tmp << 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1635
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1636 /* Byte 2 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1637 tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1638 pCardInfo->SD_cid.OEM_AppliID |= tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1639
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1640 /* Byte 3 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1641 tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1642 pCardInfo->SD_cid.ProdName1 = tmp << 24;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1643
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1644 /* Byte 4 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1645 tmp = (uint8_t)((hsd->CID[1] & 0xFF000000) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1646 pCardInfo->SD_cid.ProdName1 |= tmp << 16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1647
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1648 /* Byte 5 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1649 tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1650 pCardInfo->SD_cid.ProdName1 |= tmp << 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1651
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1652 /* Byte 6 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1653 tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1654 pCardInfo->SD_cid.ProdName1 |= tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1655
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1656 /* Byte 7 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1657 tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1658 pCardInfo->SD_cid.ProdName2 = tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1659
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1660 /* Byte 8 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1661 tmp = (uint8_t)((hsd->CID[2] & 0xFF000000) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1662 pCardInfo->SD_cid.ProdRev = tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1663
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1664 /* Byte 9 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1665 tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1666 pCardInfo->SD_cid.ProdSN = tmp << 24;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1667
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1668 /* Byte 10 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1669 tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1670 pCardInfo->SD_cid.ProdSN |= tmp << 16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1671
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1672 /* Byte 11 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1673 tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1674 pCardInfo->SD_cid.ProdSN |= tmp << 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1675
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1676 /* Byte 12 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1677 tmp = (uint8_t)((hsd->CID[3] & 0xFF000000) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1678 pCardInfo->SD_cid.ProdSN |= tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1679
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1680 /* Byte 13 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1681 tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1682 pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1683 pCardInfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1684
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1685 /* Byte 14 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1686 tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1687 pCardInfo->SD_cid.ManufactDate |= tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1688
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1689 /* Byte 15 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1690 tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1691 pCardInfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1692 pCardInfo->SD_cid.Reserved2 = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1693
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1694 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1695 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1696
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1697 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1698 * @brief Enables wide bus operation for the requested card if supported by
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1699 * card.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1700 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1701 * @param WideMode: Specifies the SD card wide bus mode
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1702 * This parameter can be one of the following values:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1703 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1704 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1705 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1706 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1707 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1708 HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1709 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1710 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1711 SDIO_InitTypeDef tmpinit;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1712
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1713 /* MMC Card does not support this feature */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1714 if (hsd->CardType == MULTIMEDIA_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1715 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1716 errorstate = SD_UNSUPPORTED_FEATURE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1717
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1718 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1719 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1720 else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1721 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1722 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1723 if (WideMode == SDIO_BUS_WIDE_8B)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1724 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1725 errorstate = SD_UNSUPPORTED_FEATURE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1726 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1727 else if (WideMode == SDIO_BUS_WIDE_4B)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1728 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1729 errorstate = SD_WideBus_Enable(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1730 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1731 else if (WideMode == SDIO_BUS_WIDE_1B)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1732 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1733 errorstate = SD_WideBus_Disable(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1734 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1735 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1736 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1737 /* WideMode is not a valid argument*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1738 errorstate = SD_INVALID_PARAMETER;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1739 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1740
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1741 if (errorstate == SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1742 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1743 /* Configure the SDIO peripheral */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1744 tmpinit.ClockEdge = hsd->Init.ClockEdge;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1745 tmpinit.ClockBypass = hsd->Init.ClockBypass;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1746 tmpinit.ClockPowerSave = hsd->Init.ClockPowerSave;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1747 tmpinit.BusWide = WideMode;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1748 tmpinit.HardwareFlowControl = hsd->Init.HardwareFlowControl;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1749 tmpinit.ClockDiv = hsd->Init.ClockDiv;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1750 SDIO_Init(hsd->Instance, tmpinit);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1751 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1752 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1753
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1754 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1755 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1756
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1757 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1758 * @brief Aborts an ongoing data transfer.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1759 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1760 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1761 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1762 HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1763 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1764 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1765 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1766
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1767 /* Send CMD12 STOP_TRANSMISSION */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1768 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1769 sdio_cmdinitstructure.CmdIndex = SD_CMD_STOP_TRANSMISSION;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1770 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1771 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1772 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1773 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1774
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1775 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1776 errorstate = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1777
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1778 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1779 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1780
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1781 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1782 * @brief Switches the SD card to High Speed mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1783 * This API must be used after "Transfer State"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1784 * @note This operation should be followed by the configuration
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1785 * of PLL to have SDIOCK clock between 67 and 75 MHz
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1786 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1787 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1788 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1789 HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1790 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1791 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1792 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1793 SDIO_DataInitTypeDef sdio_datainitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1794
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1795 uint8_t SD_hs[64] = {0};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1796 uint32_t SD_scr[2] = {0, 0};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1797 uint32_t SD_SPEC = 0 ;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1798 uint32_t count = 0, *tempbuff = (uint32_t *)SD_hs;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1799
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1800 /* Initialize the Data control register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1801 hsd->Instance->DCTRL = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1802
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1803 /* Get SCR Register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1804 errorstate = SD_FindSCR(hsd, SD_scr);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1805
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1806 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1807 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1808 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1809 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1810
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1811 /* Test the Version supported by the card*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1812 SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1813
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1814 if (SD_SPEC != SD_ALLZERO)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1815 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1816 /* Set Block Size for Card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1817 sdio_cmdinitstructure.Argument = (uint32_t)64;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1818 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1819 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1820 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1821 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1822 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1823
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1824 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1825 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1826
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1827 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1828 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1829 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1830 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1831
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1832 /* Configure the SD DPSM (Data Path State Machine) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1833 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1834 sdio_datainitstructure.DataLength = 64;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1835 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B ;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1836 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1837 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1838 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1839 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1840
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1841 /* Send CMD6 switch mode */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1842 sdio_cmdinitstructure.Argument = 0x80FFFF01;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1843 sdio_cmdinitstructure.CmdIndex = SD_CMD_HS_SWITCH;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1844 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1845
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1846 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1847 errorstate = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1848
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1849 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1850 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1851 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1852 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1853
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1854 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1855 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1856 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1857 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1858 for (count = 0; count < 8; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1859 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1860 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1861 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1862
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1863 tempbuff += 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1864 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1865 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1866
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1867 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1868 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1869 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1870
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1871 errorstate = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1872
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1873 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1874 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1875 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1876 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1877 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1878
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1879 errorstate = SD_DATA_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1880
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1881 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1882 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1883 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1884 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1885 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1886
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1887 errorstate = SD_RX_OVERRUN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1888
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1889 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1890 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1891 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1892 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1893 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1894
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1895 errorstate = SD_START_BIT_ERR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1896
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1897 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1898 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1899 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1900 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1901 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1902 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1903
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1904 count = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1905
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1906 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1907 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1908 *tempbuff = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1909 tempbuff++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1910 count--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1911 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1912
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1913 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1914 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1915
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1916 /* Test if the switch mode HS is ok */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1917 if ((SD_hs[13]& 2) != 2)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1918 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1919 errorstate = SD_UNSUPPORTED_FEATURE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1920 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1921 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1922
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1923 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1924 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1925
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1926 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1927 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1928 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1929
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1930 /** @addtogroup SD_Exported_Functions_Group4
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1931 * @brief Peripheral State functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1932 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1933 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1934 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1935 ##### Peripheral State functions #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1936 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1937 [..]
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1938 This subsection permits to get in runtime the status of the peripheral
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1939 and the data flow.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1940
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1941 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1942 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1943 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1944
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1945 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1946 * @brief Returns the current SD card's status.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1947 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1948 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1949 * SD Status register)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1950 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1951 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1952 HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1953 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1954 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1955 SDIO_DataInitTypeDef sdio_datainitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1956 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1957 uint32_t count = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1958
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1959 /* Check SD response */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1960 if ((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1961 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1962 errorstate = SD_LOCK_UNLOCK_FAILED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1963
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1964 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1965 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1966
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1967 /* Set block size for card if it is not equal to current block size for card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1968 sdio_cmdinitstructure.Argument = 64;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1969 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1970 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1971 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1972 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1973 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1974
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1975 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1976 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1977
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1978 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1979 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1980 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1981 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1982
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1983 /* Send CMD55 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1984 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1985 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1986 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1987
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1988 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1989 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1990
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1991 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1992 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1993 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1994 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1995
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1996 /* Configure the SD DPSM (Data Path State Machine) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1997 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1998 sdio_datainitstructure.DataLength = 64;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1999 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2000 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2001 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2002 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2003 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2004
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2005 /* Send ACMD13 (SD_APP_STATUS) with argument as card's RCA */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2006 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2007 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_STATUS;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2008 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2009
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2010 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2011 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STATUS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2012
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2013 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2014 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2015 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2016 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2017
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2018 /* Get status data */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2019 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2020 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2021 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2022 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2023 for (count = 0; count < 8; count++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2024 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2025 *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2026 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2027
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2028 pSDstatus += 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2029 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2030 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2031
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2032 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2033 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2034 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2035
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2036 errorstate = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2037
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2038 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2039 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2040 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2041 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2042 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2043
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2044 errorstate = SD_DATA_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2045
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2046 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2047 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2048 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2049 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2050 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2051
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2052 errorstate = SD_RX_OVERRUN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2053
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2054 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2055 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2056 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2057 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2058 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2059
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2060 errorstate = SD_START_BIT_ERR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2061
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2062 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2063 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2064 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2065 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2066 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2067 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2068
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2069 count = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2070 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2071 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2072 *pSDstatus = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2073 pSDstatus++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2074 count--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2075 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2076
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2077 /* Clear all the static status flags*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2078 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2079
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2080 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2081 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2082
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2083 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2084 * @brief Gets the current sd card data status.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2085 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2086 * @retval Data Transfer state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2087 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2088 HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2089 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2090 HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2091
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2092 /* Get SD card state */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2093 cardstate = SD_GetState(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2094
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2095 /* Find SD status according to card state*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2096 if (cardstate == SD_CARD_TRANSFER)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2097 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2098 return SD_TRANSFER_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2099 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2100 else if(cardstate == SD_CARD_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2101 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2102 return SD_TRANSFER_ERROR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2103 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2104 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2105 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2106 return SD_TRANSFER_BUSY;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2107 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2108 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2109
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2110 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2111 * @brief Gets the SD card status.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2112 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2113 * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2114 * will contain the SD card status information
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2115 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2116 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2117 HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2118 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2119 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2120 uint32_t tmp = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2121 uint32_t sd_status[16];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2122
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2123 errorstate = HAL_SD_SendSDStatus(hsd, sd_status);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2124
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2125 if (errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2126 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2127 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2128 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2129
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2130 /* Byte 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2131 tmp = (sd_status[0] & 0xC0) >> 6;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2132 pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2133
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2134 /* Byte 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2135 tmp = (sd_status[0] & 0x20) >> 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2136 pCardStatus->SECURED_MODE = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2137
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2138 /* Byte 2 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2139 tmp = (sd_status[2] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2140 pCardStatus->SD_CARD_TYPE = (uint8_t)(tmp << 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2141
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2142 /* Byte 3 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2143 tmp = (sd_status[3] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2144 pCardStatus->SD_CARD_TYPE |= (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2145
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2146 /* Byte 4 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2147 tmp = (sd_status[4] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2148 pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(tmp << 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2149
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2150 /* Byte 5 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2151 tmp = (sd_status[5] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2152 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2153
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2154 /* Byte 6 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2155 tmp = (sd_status[6] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2156 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2157
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2158 /* Byte 7 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2159 tmp = (sd_status[7] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2160 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2161
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2162 /* Byte 8 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2163 tmp = (sd_status[8] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2164 pCardStatus->SPEED_CLASS = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2165
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2166 /* Byte 9 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2167 tmp = (sd_status[9] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2168 pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2169
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2170 /* Byte 10 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2171 tmp = (sd_status[10] & 0xF0) >> 4;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2172 pCardStatus->AU_SIZE = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2173
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2174 /* Byte 11 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2175 tmp = (sd_status[11] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2176 pCardStatus->ERASE_SIZE = (uint8_t)(tmp << 8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2177
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2178 /* Byte 12 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2179 tmp = (sd_status[12] & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2180 pCardStatus->ERASE_SIZE |= (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2181
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2182 /* Byte 13 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2183 tmp = (sd_status[13] & 0xFC) >> 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2184 pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2185
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2186 /* Byte 13 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2187 tmp = (sd_status[13] & 0x3);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2188 pCardStatus->ERASE_OFFSET = (uint8_t)tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2189
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2190 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2191 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2192
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2193 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2194 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2195 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2196
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2197 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2198 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2199 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2200
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2201 /* Private function ----------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2202 /** @addtogroup SD_Private_Functions
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2203 * @{
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2204 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2205
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2206 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2207 * @brief SD DMA transfer complete Rx callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2208 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2209 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2210 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2211 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2212 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2213 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2214 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2215
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2216 /* DMA transfer is complete */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2217 hsd->DmaTransferCplt = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2218
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2219 /* Wait until SD transfer is complete */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2220 while(hsd->SdTransferCplt == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2221 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2222 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2223
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2224 /* Disable the DMA channel */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2225 HAL_DMA_Abort(hdma);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2226
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2227 /* Transfer complete user callback */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2228 HAL_SD_DMA_RxCpltCallback(hsd->hdmarx);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2229 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2230
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2231 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2232 * @brief SD DMA transfer Error Rx callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2233 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2234 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2235 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2236 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2237 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2238 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2239 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2240
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2241 /* Transfer complete user callback */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2242 HAL_SD_DMA_RxErrorCallback(hsd->hdmarx);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2243 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2244
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2245 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2246 * @brief SD DMA transfer complete Tx callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2247 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2248 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2249 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2250 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2251 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2252 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2253 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2254
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2255 /* DMA transfer is complete */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2256 hsd->DmaTransferCplt = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2257
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2258 /* Wait until SD transfer is complete */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2259 while(hsd->SdTransferCplt == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2260 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2261 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2262
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2263 /* Disable the DMA channel */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2264 HAL_DMA_Abort(hdma);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2265
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2266 /* Transfer complete user callback */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2267 HAL_SD_DMA_TxCpltCallback(hsd->hdmatx);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2268 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2269
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2270 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2271 * @brief SD DMA transfer Error Tx callback.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2272 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2273 * the configuration information for the specified DMA module.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2274 * @retval None
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2275 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2276 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2277 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2278 SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2279
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2280 /* Transfer complete user callback */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2281 HAL_SD_DMA_TxErrorCallback(hsd->hdmatx);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2282 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2283
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2284 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2285 * @brief Returns the SD current state.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2286 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2287 * @retval SD card current state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2288 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2289 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2290 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2291 uint32_t resp1 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2292
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2293 if (SD_SendStatus(hsd, &resp1) != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2294 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2295 return SD_CARD_ERROR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2296 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2297 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2298 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2299 return (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2300 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2301 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2302
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2303 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2304 * @brief Initializes all cards or single card as the case may be Card(s) come
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2305 * into standby state.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2306 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2307 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2308 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2309 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2310 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2311 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2312 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2313 uint16_t sd_rca = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2314
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2315 if(SDIO_GetPowerState(hsd->Instance) == 0) /* Power off */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2316 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2317 errorstate = SD_REQUEST_NOT_APPLICABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2318
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2319 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2320 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2321
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2322 if(hsd->CardType != SECURE_DIGITAL_IO_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2323 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2324 /* Send CMD2 ALL_SEND_CID */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2325 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2326 sdio_cmdinitstructure.CmdIndex = SD_CMD_ALL_SEND_CID;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2327 sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2328 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2329 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2330 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2331
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2332 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2333 errorstate = SD_CmdResp2Error(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2334
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2335 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2336 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2337 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2338 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2339
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2340 /* Get Card identification number data */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2341 hsd->CID[0] = SDIO_GetResponse(SDIO_RESP1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2342 hsd->CID[1] = SDIO_GetResponse(SDIO_RESP2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2343 hsd->CID[2] = SDIO_GetResponse(SDIO_RESP3);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2344 hsd->CID[3] = SDIO_GetResponse(SDIO_RESP4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2345 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2346
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2347 if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2348 (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2349 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2350 /* Send CMD3 SET_REL_ADDR with argument 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2351 /* SD Card publishes its RCA. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2352 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_REL_ADDR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2353 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2354 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2355
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2356 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2357 errorstate = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2358
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2359 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2360 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2361 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2362 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2363 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2364
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2365 if (hsd->CardType != SECURE_DIGITAL_IO_CARD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2366 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2367 /* Get the SD card RCA */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2368 hsd->RCA = sd_rca;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2369
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2370 /* Send CMD9 SEND_CSD with argument as card's RCA */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2371 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2372 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_CSD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2373 sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2374 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2375
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2376 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2377 errorstate = SD_CmdResp2Error(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2378
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2379 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2380 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2381 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2382 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2383
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2384 /* Get Card Specific Data */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2385 hsd->CSD[0] = SDIO_GetResponse(SDIO_RESP1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2386 hsd->CSD[1] = SDIO_GetResponse(SDIO_RESP2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2387 hsd->CSD[2] = SDIO_GetResponse(SDIO_RESP3);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2388 hsd->CSD[3] = SDIO_GetResponse(SDIO_RESP4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2389 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2390
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2391 /* All cards are initialized */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2392 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2393 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2394
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2395 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2396 * @brief Selects of Deselects the corresponding card.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2397 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2398 * @param addr: Address of the card to be selected
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2399 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2400 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2401 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2402 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2403 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2404 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2405
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2406 /* Send CMD7 SDIO_SEL_DESEL_CARD */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2407 sdio_cmdinitstructure.Argument = (uint32_t)addr;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2408 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEL_DESEL_CARD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2409 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2410 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2411 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2412 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2413
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2414 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2415 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2416
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2417 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2418 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2419
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2420 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2421 * @brief Enquires cards about their operating voltage and configures clock
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2422 * controls and stores SD information that will be needed in future
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2423 * in the SD handle.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2424 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2425 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2426 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2427 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2428 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2429 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2430 __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2431 uint32_t response = 0, count = 0, validvoltage = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2432 uint32_t sdtype = SD_STD_CAPACITY;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2433
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2434 /* Power ON Sequence -------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2435 /* Disable SDIO Clock */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2436 __HAL_SD_SDIO_DISABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2437
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2438 /* Set Power State to ON */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2439 SDIO_PowerState_ON(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2440
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2441 /* 1ms: required power up waiting time before starting the SD initialization
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2442 sequence */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2443 HAL_Delay(1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2444
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2445 /* Enable SDIO Clock */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2446 __HAL_SD_SDIO_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2447
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2448 /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2449 /* No CMD response required */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2450 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2451 sdio_cmdinitstructure.CmdIndex = SD_CMD_GO_IDLE_STATE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2452 sdio_cmdinitstructure.Response = SDIO_RESPONSE_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2453 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2454 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2455 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2456
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2457 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2458 errorstate = SD_CmdError(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2459
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2460 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2461 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2462 /* CMD Response Timeout (wait for CMDSENT flag) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2463 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2464 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2465
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2466 /* CMD8: SEND_IF_COND ------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2467 /* Send CMD8 to verify SD card interface operating condition */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2468 /* Argument: - [31:12]: Reserved (shall be set to '0')
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2469 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2470 - [7:0]: Check Pattern (recommended 0xAA) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2471 /* CMD Response: R7 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2472 sdio_cmdinitstructure.Argument = SD_CHECK_PATTERN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2473 sdio_cmdinitstructure.CmdIndex = SD_SDIO_SEND_IF_COND;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2474 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2475 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2476
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2477 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2478 errorstate = SD_CmdResp7Error(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2479
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2480 if (errorstate == SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2481 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2482 /* SD Card 2.0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2483 hsd->CardType = STD_CAPACITY_SD_CARD_V2_0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2484 sdtype = SD_HIGH_CAPACITY;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2485 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2486
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2487 /* Send CMD55 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2488 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2489 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2490 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2491
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2492 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2493 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2494
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2495 /* If errorstate is Command Timeout, it is a MMC card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2496 /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2497 or SD card 1.x */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2498 if(errorstate == SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2499 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2500 /* SD CARD */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2501 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2502 while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2503 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2504
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2505 /* SEND CMD55 APP_CMD with RCA as 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2506 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2507 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2508 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2509 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2510 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2511 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2512
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2513 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2514 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2515
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2516 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2517 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2518 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2519 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2520
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2521 /* Send CMD41 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2522 sdio_cmdinitstructure.Argument = SD_VOLTAGE_WINDOW_SD | sdtype;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2523 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_OP_COND;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2524 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2525 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2526 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2527 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2528
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2529 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2530 errorstate = SD_CmdResp3Error(hsd);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2531
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2532 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2533 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2534 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2535 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2536
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2537 /* Get command response */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2538 response = SDIO_GetResponse(SDIO_RESP1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2539
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2540 /* Get operating voltage*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2541 validvoltage = (((response >> 31) == 1) ? 1 : 0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2542
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2543 count++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2544 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2545
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2546 if(count >= SD_MAX_VOLT_TRIAL)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2547 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2548 errorstate = SD_INVALID_VOLTRANGE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2549
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2550 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2551 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2552
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2553 if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2554 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2555 hsd->CardType = HIGH_CAPACITY_SD_CARD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2556 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2557
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2558 } /* else MMC Card */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2559
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2560 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2561 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2562
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2563 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2564 * @brief Turns the SDIO output signals off.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2565 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2566 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2567 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2568 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2569 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2570 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2571
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2572 /* Set Power State to OFF */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2573 SDIO_PowerState_OFF(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2574
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2575 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2576 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2577
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2578 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2579 * @brief Returns the current card's status.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2580 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2581 * @param pCardStatus: pointer to the buffer that will contain the SD card
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2582 * status (Card Status register)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2583 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2584 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2585 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2586 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2587 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2588 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2589
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2590 if(pCardStatus == NULL)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2591 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2592 errorstate = SD_INVALID_PARAMETER;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2593
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2594 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2595 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2596
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2597 /* Send Status command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2598 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2599 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2600 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2601 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2602 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2603 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2604
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2605 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2606 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2607
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2608 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2609 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2610 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2611 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2612
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2613 /* Get SD card status */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2614 *pCardStatus = SDIO_GetResponse(SDIO_RESP1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2615
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2616 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2617 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2618
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2619 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2620 * @brief Checks for error conditions for CMD0.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2621 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2622 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2623 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2624 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2625 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2626 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2627 uint32_t timeout, tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2628
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2629 timeout = SDIO_CMD0TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2630
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2631 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2632
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2633 while((timeout > 0) && (!tmp))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2634 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2635 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2636 timeout--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2637 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2638
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2639 if(timeout == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2640 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2641 errorstate = SD_CMD_RSP_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2642 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2643 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2644
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2645 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2646 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2647
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2648 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2649 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2650
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2651 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2652 * @brief Checks for error conditions for R7 response.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2653 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2654 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2655 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2656 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2657 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2658 HAL_SD_ErrorTypedef errorstate = SD_ERROR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2659 uint32_t timeout = SDIO_CMD0TIMEOUT, tmp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2660
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2661 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2662
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2663 while((!tmp) && (timeout > 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2664 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2665 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2666 timeout--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2667 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2668
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2669 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2670
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2671 if((timeout == 0) || tmp)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2672 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2673 /* Card is not V2.0 compliant or card does not support the set voltage range */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2674 errorstate = SD_CMD_RSP_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2675
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2676 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2677
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2678 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2679 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2680
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2681 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDREND))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2682 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2683 /* Card is SD V2.0 compliant */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2684 errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2685
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2686 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CMDREND);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2687
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2688 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2689 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2690
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2691 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2692 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2693
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2694 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2695 * @brief Checks for error conditions for R1 response.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2696 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2697 * @param SD_CMD: The sent command index
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2698 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2699 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2700 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2701 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2702 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2703 uint32_t response_r1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2704
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2705 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2706 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2707 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2708
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2709 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2710 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2711 errorstate = SD_CMD_RSP_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2712
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2713 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2714
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2715 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2716 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2717 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2718 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2719 errorstate = SD_CMD_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2720
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2721 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2722
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2723 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2724 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2725
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2726 /* Check response received is of desired command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2727 if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2728 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2729 errorstate = SD_ILLEGAL_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2730
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2731 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2732 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2733
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2734 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2735 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2736
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2737 /* We have received response, retrieve it for analysis */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2738 response_r1 = SDIO_GetResponse(SDIO_RESP1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2739
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2740 if((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2741 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2742 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2743 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2744
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2745 if((response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2746 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2747 return(SD_ADDR_OUT_OF_RANGE);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2748 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2749
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2750 if((response_r1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2751 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2752 return(SD_ADDR_MISALIGNED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2753 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2754
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2755 if((response_r1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2756 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2757 return(SD_BLOCK_LEN_ERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2758 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2759
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2760 if((response_r1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2761 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2762 return(SD_ERASE_SEQ_ERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2763 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2764
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2765 if((response_r1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2766 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2767 return(SD_BAD_ERASE_PARAM);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2768 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2769
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2770 if((response_r1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2771 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2772 return(SD_WRITE_PROT_VIOLATION);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2773 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2774
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2775 if((response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2776 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2777 return(SD_LOCK_UNLOCK_FAILED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2778 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2779
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2780 if((response_r1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2781 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2782 return(SD_COM_CRC_FAILED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2783 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2784
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2785 if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2786 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2787 return(SD_ILLEGAL_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2788 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2789
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2790 if((response_r1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2791 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2792 return(SD_CARD_ECC_FAILED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2793 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2794
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2795 if((response_r1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2796 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2797 return(SD_CC_ERROR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2798 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2799
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2800 if((response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2801 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2802 return(SD_GENERAL_UNKNOWN_ERROR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2803 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2804
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2805 if((response_r1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2806 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2807 return(SD_STREAM_READ_UNDERRUN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2808 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2809
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2810 if((response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2811 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2812 return(SD_STREAM_WRITE_OVERRUN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2813 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2814
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2815 if((response_r1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2816 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2817 return(SD_CID_CSD_OVERWRITE);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2818 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2819
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2820 if((response_r1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2821 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2822 return(SD_WP_ERASE_SKIP);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2823 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2824
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2825 if((response_r1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2826 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2827 return(SD_CARD_ECC_DISABLED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2828 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2829
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2830 if((response_r1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2831 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2832 return(SD_ERASE_RESET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2833 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2834
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2835 if((response_r1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2836 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2837 return(SD_AKE_SEQ_ERROR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2838 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2839
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2840 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2841 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2842
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2843 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2844 * @brief Checks for error conditions for R3 (OCR) response.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2845 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2846 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2847 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2848 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2849 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2850 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2851
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2852 while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2853 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2854 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2855
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2856 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2857 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2858 errorstate = SD_CMD_RSP_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2859
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2860 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2861
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2862 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2863 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2864
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2865 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2866 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2867
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2868 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2869 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2870
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2871 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2872 * @brief Checks for error conditions for R2 (CID or CSD) response.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2873 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2874 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2875 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2876 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2877 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2878 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2879
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2880 while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2881 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2882 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2883
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2884 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2885 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2886 errorstate = SD_CMD_RSP_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2887
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2888 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2889
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2890 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2891 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2892 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2893 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2894 errorstate = SD_CMD_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2895
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2896 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2897
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2898 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2899 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2900 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2901 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2902 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2903 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2904
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2905 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2906 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2907
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2908 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2909 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2910
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2911 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2912 * @brief Checks for error conditions for R6 (RCA) response.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2913 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2914 * @param SD_CMD: The sent command index
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2915 * @param pRCA: Pointer to the variable that will contain the SD card relative
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2916 * address RCA
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2917 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2918 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2919 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2920 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2921 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2922 uint32_t response_r1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2923
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2924 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2925 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2926 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2927
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2928 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2929 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2930 errorstate = SD_CMD_RSP_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2931
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2932 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2933
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2934 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2935 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2936 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2937 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2938 errorstate = SD_CMD_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2939
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2940 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2941
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2942 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2943 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2944 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2945 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2946 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2947 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2948
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2949 /* Check response received is of desired command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2950 if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2951 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2952 errorstate = SD_ILLEGAL_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2953
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2954 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2955 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2956
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2957 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2958 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2959
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2960 /* We have received response, retrieve it. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2961 response_r1 = SDIO_GetResponse(SDIO_RESP1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2962
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2963 if((response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2964 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2965 *pRCA = (uint16_t) (response_r1 >> 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2966
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2967 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2968 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2969
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2970 if((response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2971 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2972 return(SD_GENERAL_UNKNOWN_ERROR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2973 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2974
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2975 if((response_r1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2976 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2977 return(SD_ILLEGAL_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2978 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2979
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2980 if((response_r1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2981 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2982 return(SD_COM_CRC_FAILED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2983 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2984
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2985 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2986 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2987
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2988 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2989 * @brief Enables the SDIO wide bus mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2990 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2991 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2992 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2993 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2994 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2995 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2996 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2997
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2998 uint32_t scr[2] = {0, 0};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2999
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3000 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3001 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3002 errorstate = SD_LOCK_UNLOCK_FAILED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3003
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3004 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3005 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3006
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3007 /* Get SCR Register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3008 errorstate = SD_FindSCR(hsd, scr);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3009
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3010 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3011 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3012 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3013 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3015 /* If requested card supports wide bus operation */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3016 if((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3017 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3018 /* Send CMD55 APP_CMD with argument as card's RCA.*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3019 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3020 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3021 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3022 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3023 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3024 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3025
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3026 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3027 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3028
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3029 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3030 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3031 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3032 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3033
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3034 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3035 sdio_cmdinitstructure.Argument = 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3036 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3037 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3038
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3039 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3040 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3041
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3042 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3043 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3044 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3045 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3046
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3047 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3048 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3049 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3050 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3051 errorstate = SD_REQUEST_NOT_APPLICABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3052
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3053 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3054 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3055 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3056
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3057 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3058 * @brief Disables the SDIO wide bus mode.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3059 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3060 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3061 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3062 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3063 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3064 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3065 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3066
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3067 uint32_t scr[2] = {0, 0};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3068
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3069 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3070 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3071 errorstate = SD_LOCK_UNLOCK_FAILED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3072
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3073 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3074 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3075
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3076 /* Get SCR Register */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3077 errorstate = SD_FindSCR(hsd, scr);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3078
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3079 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3080 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3081 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3082 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3083
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3084 /* If requested card supports 1 bit mode operation */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3085 if((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3086 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3087 /* Send CMD55 APP_CMD with argument as card's RCA */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3088 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3089 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3090 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3091 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3092 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3093 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3094
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3095 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3096 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3097
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3098 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3099 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3100 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3101 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3102
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3103 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3104 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3105 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3106 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3107
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3108 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3109 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3110
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3111 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3112 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3113 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3114 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3115
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3116 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3117 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3118 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3119 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3120 errorstate = SD_REQUEST_NOT_APPLICABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3121
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3122 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3123 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3124 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3125
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3126
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3127 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3128 * @brief Finds the SD card SCR register value.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3129 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3130 * @param pSCR: pointer to the buffer that will contain the SCR value
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3131 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3132 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3133 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3134 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3135 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3136 SDIO_DataInitTypeDef sdio_datainitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3137 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3138 uint32_t index = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3139 uint32_t tempscr[2] = {0, 0};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3140
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3141 /* Set Block Size To 8 Bytes */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3142 /* Send CMD55 APP_CMD with argument as card's RCA */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3143 sdio_cmdinitstructure.Argument = (uint32_t)8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3144 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3145 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3146 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3147 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3148 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3149
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3150 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3151 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3152
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3153 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3154 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3155 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3156 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3157
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3158 /* Send CMD55 APP_CMD with argument as card's RCA */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3159 sdio_cmdinitstructure.Argument = (uint32_t)((hsd->RCA) << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3160 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3161 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3162
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3163 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3164 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3165
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3166 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3167 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3168 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3169 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3170 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3171 sdio_datainitstructure.DataLength = 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3172 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3173 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3174 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3175 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3176 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3177
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3178 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3179 sdio_cmdinitstructure.Argument = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3180 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3181 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3182
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3183 /* Check for error conditions */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3184 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3185
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3186 if(errorstate != SD_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3187 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3188 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3189 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3190
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3191 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3192 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3193 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3194 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3195 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3196 index++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3197 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3198 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3199
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3200 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3201 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3202 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3203
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3204 errorstate = SD_DATA_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3205
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3206 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3207 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3208 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3209 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3210 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3211
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3212 errorstate = SD_DATA_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3213
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3214 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3215 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3216 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3217 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3218 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3219
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3220 errorstate = SD_RX_OVERRUN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3221
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3222 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3223 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3224 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3225 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3226 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3227
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3228 errorstate = SD_START_BIT_ERR;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3229
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3230 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3231 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3232 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3233 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3234 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3235 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3236
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3237 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3238 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3239
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3240 *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3241 ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3242
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3243 *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |\
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3244 ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3245
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3246 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3247 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3248
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3249 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3250 * @brief Checks if the SD card is in programming state.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3251 * @param hsd: SD handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3252 * @param pStatus: pointer to the variable that will contain the SD card state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3253 * @retval SD Card error state
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3254 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3255 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3256 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3257 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3258 HAL_SD_ErrorTypedef errorstate = SD_OK;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3259 __IO uint32_t responseR1 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3260
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3261 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3262 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3263 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3264 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3265 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3266 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3267
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3268 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3269 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3270 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3271
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3272 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3273 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3274 errorstate = SD_CMD_RSP_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3275
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3276 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3277
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3278 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3279 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3280 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3281 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3282 errorstate = SD_CMD_CRC_FAIL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3283
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3284 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3285
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3286 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3287 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3288 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3289 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3290 /* No error flag set */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3291 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3292
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3293 /* Check response received is of desired command */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3294 if((uint32_t)SDIO_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3295 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3296 errorstate = SD_ILLEGAL_CMD;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3297
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3298 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3299 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3300
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3301 /* Clear all the static flags */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3302 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3303
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3304
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3305 /* We have received response, retrieve it for analysis */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3306 responseR1 = SDIO_GetResponse(SDIO_RESP1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3307
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3308 /* Find out card status */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3309 *pStatus = (uint8_t)((responseR1 >> 9) & 0x0000000F);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3310
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3311 if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3312 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3313 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3314 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3315
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3316 if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3317 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3318 return(SD_ADDR_OUT_OF_RANGE);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3319 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3320
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3321 if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3322 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3323 return(SD_ADDR_MISALIGNED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3324 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3325
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3326 if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3327 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3328 return(SD_BLOCK_LEN_ERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3329 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3330
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3331 if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3332 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3333 return(SD_ERASE_SEQ_ERR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3334 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3335
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3336 if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3337 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3338 return(SD_BAD_ERASE_PARAM);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3339 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3340
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3341 if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3342 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3343 return(SD_WRITE_PROT_VIOLATION);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3344 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3345
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3346 if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3347 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3348 return(SD_LOCK_UNLOCK_FAILED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3349 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3350
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3351 if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3352 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3353 return(SD_COM_CRC_FAILED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3354 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3355
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3356 if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3357 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3358 return(SD_ILLEGAL_CMD);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3359 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3360
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3361 if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3362 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3363 return(SD_CARD_ECC_FAILED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3364 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3365
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3366 if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3367 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3368 return(SD_CC_ERROR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3369 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3370
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3371 if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3372 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3373 return(SD_GENERAL_UNKNOWN_ERROR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3374 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3375
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3376 if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3377 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3378 return(SD_STREAM_READ_UNDERRUN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3379 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3380
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3381 if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3382 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3383 return(SD_STREAM_WRITE_OVERRUN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3384 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3385
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3386 if((responseR1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3387 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3388 return(SD_CID_CSD_OVERWRITE);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3389 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3390
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3391 if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3392 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3393 return(SD_WP_ERASE_SKIP);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3394 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3395
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3396 if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3397 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3398 return(SD_CARD_ECC_DISABLED);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3399 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3400
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3401 if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3402 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3403 return(SD_ERASE_RESET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3404 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3405
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3406 if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3407 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3408 return(SD_AKE_SEQ_ERROR);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3409 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3410
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3411 return errorstate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3412 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3413
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3414 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3415 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3416 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3417
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3418 #endif /* HAL_SD_MODULE_ENABLED */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3419
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3420 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3421 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3422 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3423
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3424 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3425 * @}
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3426 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3427
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3428 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/