stm32f4xx_hal_pccard.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pccard.c
  4. * @author MCD Application Team
  5. * @brief PCCARD HAL module driver.
  6. * This file provides a generic firmware to drive PCCARD memories mounted
  7. * as external device.
  8. *
  9. @verbatim
  10. ===============================================================================
  11. ##### How to use this driver #####
  12. ===============================================================================
  13. [..]
  14. This driver is a generic layered driver which contains a set of APIs used to
  15. control PCCARD/compact flash memories. It uses the FMC/FSMC layer functions
  16. to interface with PCCARD devices. This driver is used for:
  17. (+) PCCARD/Compact Flash memory configuration sequence using the function
  18. HAL_PCCARD_Init()/HAL_CF_Init() with control and timing parameters for
  19. both common and attribute spaces.
  20. (+) Read PCCARD/Compact Flash memory maker and device IDs using the function
  21. HAL_PCCARD_Read_ID()/HAL_CF_Read_ID(). The read information is stored in
  22. the CompactFlash_ID structure declared by the function caller.
  23. (+) Access PCCARD/Compact Flash memory by read/write operations using the functions
  24. HAL_PCCARD_Read_Sector()/ HAL_PCCARD_Write_Sector() -
  25. HAL_CF_Read_Sector()/HAL_CF_Write_Sector(), to read/write sector.
  26. (+) Perform PCCARD/Compact Flash Reset chip operation using the function
  27. HAL_PCCARD_Reset()/HAL_CF_Reset.
  28. (+) Perform PCCARD/Compact Flash erase sector operation using the function
  29. HAL_PCCARD_Erase_Sector()/HAL_CF_Erase_Sector.
  30. (+) Read the PCCARD/Compact Flash status operation using the function
  31. HAL_PCCARD_ReadStatus()/HAL_CF_ReadStatus().
  32. (+) You can monitor the PCCARD/Compact Flash device HAL state by calling
  33. the function HAL_PCCARD_GetState()/HAL_CF_GetState()
  34. [..]
  35. (@) This driver is a set of generic APIs which handle standard PCCARD/compact flash
  36. operations. If a PCCARD/Compact Flash device contains different operations
  37. and/or implementations, it should be implemented separately.
  38. @endverbatim
  39. ******************************************************************************
  40. * @attention
  41. *
  42. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  43. *
  44. * Redistribution and use in source and binary forms, with or without modification,
  45. * are permitted provided that the following conditions are met:
  46. * 1. Redistributions of source code must retain the above copyright notice,
  47. * this list of conditions and the following disclaimer.
  48. * 2. Redistributions in binary form must reproduce the above copyright notice,
  49. * this list of conditions and the following disclaimer in the documentation
  50. * and/or other materials provided with the distribution.
  51. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  52. * may be used to endorse or promote products derived from this software
  53. * without specific prior written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  56. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  58. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  59. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  60. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  61. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  62. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  63. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. ******************************************************************************
  67. */
  68. /* Includes ------------------------------------------------------------------*/
  69. #include "stm32f4xx_hal.h"
  70. /** @addtogroup STM32F4xx_HAL_Driver
  71. * @{
  72. */
  73. #ifdef HAL_PCCARD_MODULE_ENABLED
  74. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  75. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  76. /** @defgroup PCCARD PCCARD
  77. * @brief PCCARD HAL module driver
  78. * @{
  79. */
  80. /* Private typedef -----------------------------------------------------------*/
  81. /* Private define ------------------------------------------------------------*/
  82. /** @defgroup PCCARD_Private_Defines PCCARD Private Defines
  83. * @{
  84. */
  85. #define PCCARD_TIMEOUT_READ_ID 0x0000FFFFU
  86. #define PCCARD_TIMEOUT_READ_WRITE_SECTOR 0x0000FFFFU
  87. #define PCCARD_TIMEOUT_ERASE_SECTOR 0x00000400U
  88. #define PCCARD_TIMEOUT_STATUS 0x01000000U
  89. #define PCCARD_STATUS_OK (uint8_t)0x58
  90. #define PCCARD_STATUS_WRITE_OK (uint8_t)0x50
  91. /**
  92. * @}
  93. */
  94. /* Private macro -------------------------------------------------------------*/
  95. /* Private variables ---------------------------------------------------------*/
  96. /* Private function ----------------------------------------------------------*/
  97. /* Exported functions --------------------------------------------------------*/
  98. /** @defgroup PCCARD_Exported_Functions PCCARD Exported Functions
  99. * @{
  100. */
  101. /** @defgroup PCCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  102. * @brief Initialization and Configuration functions
  103. *
  104. @verbatim
  105. ==============================================================================
  106. ##### PCCARD Initialization and de-initialization functions #####
  107. ==============================================================================
  108. [..]
  109. This section provides functions allowing to initialize/de-initialize
  110. the PCCARD memory
  111. @endverbatim
  112. * @{
  113. */
  114. /**
  115. * @brief Perform the PCCARD memory Initialization sequence
  116. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  117. * the configuration information for PCCARD module.
  118. * @param ComSpaceTiming Common space timing structure
  119. * @param AttSpaceTiming Attribute space timing structure
  120. * @param IOSpaceTiming IO space timing structure
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_PCCARD_Init(PCCARD_HandleTypeDef *hpccard, FMC_NAND_PCC_TimingTypeDef *ComSpaceTiming, FMC_NAND_PCC_TimingTypeDef *AttSpaceTiming, FMC_NAND_PCC_TimingTypeDef *IOSpaceTiming)
  124. {
  125. /* Check the PCCARD controller state */
  126. if(hpccard == NULL)
  127. {
  128. return HAL_ERROR;
  129. }
  130. if(hpccard->State == HAL_PCCARD_STATE_RESET)
  131. {
  132. /* Allocate lock resource and initialize it */
  133. hpccard->Lock = HAL_UNLOCKED;
  134. /* Initialize the low level hardware (MSP) */
  135. HAL_PCCARD_MspInit(hpccard);
  136. }
  137. /* Initialize the PCCARD state */
  138. hpccard->State = HAL_PCCARD_STATE_BUSY;
  139. /* Initialize PCCARD control Interface */
  140. FMC_PCCARD_Init(hpccard->Instance, &(hpccard->Init));
  141. /* Init PCCARD common space timing Interface */
  142. FMC_PCCARD_CommonSpace_Timing_Init(hpccard->Instance, ComSpaceTiming);
  143. /* Init PCCARD attribute space timing Interface */
  144. FMC_PCCARD_AttributeSpace_Timing_Init(hpccard->Instance, AttSpaceTiming);
  145. /* Init PCCARD IO space timing Interface */
  146. FMC_PCCARD_IOSpace_Timing_Init(hpccard->Instance, IOSpaceTiming);
  147. /* Enable the PCCARD device */
  148. __FMC_PCCARD_ENABLE(hpccard->Instance);
  149. /* Update the PCCARD state */
  150. hpccard->State = HAL_PCCARD_STATE_READY;
  151. return HAL_OK;
  152. }
  153. /**
  154. * @brief Perform the PCCARD memory De-initialization sequence
  155. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  156. * the configuration information for PCCARD module.
  157. * @retval HAL status
  158. */
  159. HAL_StatusTypeDef HAL_PCCARD_DeInit(PCCARD_HandleTypeDef *hpccard)
  160. {
  161. /* De-Initialize the low level hardware (MSP) */
  162. HAL_PCCARD_MspDeInit(hpccard);
  163. /* Configure the PCCARD registers with their reset values */
  164. FMC_PCCARD_DeInit(hpccard->Instance);
  165. /* Update the PCCARD controller state */
  166. hpccard->State = HAL_PCCARD_STATE_RESET;
  167. /* Release Lock */
  168. __HAL_UNLOCK(hpccard);
  169. return HAL_OK;
  170. }
  171. /**
  172. * @brief PCCARD MSP Init
  173. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  174. * the configuration information for PCCARD module.
  175. * @retval None
  176. */
  177. __weak void HAL_PCCARD_MspInit(PCCARD_HandleTypeDef *hpccard)
  178. {
  179. /* Prevent unused argument(s) compilation warning */
  180. UNUSED(hpccard);
  181. /* NOTE : This function Should not be modified, when the callback is needed,
  182. the HAL_PCCARD_MspInit could be implemented in the user file
  183. */
  184. }
  185. /**
  186. * @brief PCCARD MSP DeInit
  187. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  188. * the configuration information for PCCARD module.
  189. * @retval None
  190. */
  191. __weak void HAL_PCCARD_MspDeInit(PCCARD_HandleTypeDef *hpccard)
  192. {
  193. /* Prevent unused argument(s) compilation warning */
  194. UNUSED(hpccard);
  195. /* NOTE : This function Should not be modified, when the callback is needed,
  196. the HAL_PCCARD_MspDeInit could be implemented in the user file
  197. */
  198. }
  199. /**
  200. * @}
  201. */
  202. /** @defgroup PCCARD_Exported_Functions_Group2 Input and Output functions
  203. * @brief Input Output and memory control functions
  204. *
  205. @verbatim
  206. ==============================================================================
  207. ##### PCCARD Input and Output functions #####
  208. ==============================================================================
  209. [..]
  210. This section provides functions allowing to use and control the PCCARD memory
  211. @endverbatim
  212. * @{
  213. */
  214. /**
  215. * @brief Read Compact Flash's ID.
  216. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  217. * the configuration information for PCCARD module.
  218. * @param CompactFlash_ID Compact flash ID structure.
  219. * @param pStatus pointer to compact flash status
  220. * @retval HAL status
  221. *
  222. */
  223. HAL_StatusTypeDef HAL_PCCARD_Read_ID(PCCARD_HandleTypeDef *hpccard, uint8_t CompactFlash_ID[], uint8_t *pStatus)
  224. {
  225. uint32_t timeout = PCCARD_TIMEOUT_READ_ID, index = 0U;
  226. uint8_t status = 0;
  227. /* Process Locked */
  228. __HAL_LOCK(hpccard);
  229. /* Check the PCCARD controller state */
  230. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  231. {
  232. return HAL_BUSY;
  233. }
  234. /* Update the PCCARD controller state */
  235. hpccard->State = HAL_PCCARD_STATE_BUSY;
  236. /* Initialize the PCCARD status */
  237. *pStatus = PCCARD_READY;
  238. /* Send the Identify Command */
  239. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = 0xECEC;
  240. /* Read PCCARD IDs and timeout treatment */
  241. do
  242. {
  243. /* Read the PCCARD status */
  244. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  245. timeout--;
  246. }while((status != PCCARD_STATUS_OK) && timeout);
  247. if(timeout == 0U)
  248. {
  249. *pStatus = PCCARD_TIMEOUT_ERROR;
  250. }
  251. else
  252. {
  253. /* Read PCCARD ID bytes */
  254. for(index = 0U; index < 16U; index++)
  255. {
  256. CompactFlash_ID[index] = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_DATA);
  257. }
  258. }
  259. /* Update the PCCARD controller state */
  260. hpccard->State = HAL_PCCARD_STATE_READY;
  261. /* Process unlocked */
  262. __HAL_UNLOCK(hpccard);
  263. return HAL_OK;
  264. }
  265. /**
  266. * @brief Read sector from PCCARD memory
  267. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  268. * the configuration information for PCCARD module.
  269. * @param pBuffer pointer to destination read buffer
  270. * @param SectorAddress Sector address to read
  271. * @param pStatus pointer to PCCARD status
  272. * @retval HAL status
  273. */
  274. HAL_StatusTypeDef HAL_PCCARD_Read_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  275. {
  276. uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0U;
  277. uint8_t status = 0;
  278. /* Process Locked */
  279. __HAL_LOCK(hpccard);
  280. /* Check the PCCARD controller state */
  281. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  282. {
  283. return HAL_BUSY;
  284. }
  285. /* Update the PCCARD controller state */
  286. hpccard->State = HAL_PCCARD_STATE_BUSY;
  287. /* Initialize PCCARD status */
  288. *pStatus = PCCARD_READY;
  289. /* Set the parameters to write a sector */
  290. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x00;
  291. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  292. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0xE4A0;
  293. do
  294. {
  295. /* wait till the Status = 0x80 */
  296. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  297. timeout--;
  298. }while((status == 0x80) && timeout);
  299. if(timeout == 0U)
  300. {
  301. *pStatus = PCCARD_TIMEOUT_ERROR;
  302. }
  303. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  304. do
  305. {
  306. /* wait till the Status = PCCARD_STATUS_OK */
  307. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  308. timeout--;
  309. }while((status != PCCARD_STATUS_OK) && timeout);
  310. if(timeout == 0U)
  311. {
  312. *pStatus = PCCARD_TIMEOUT_ERROR;
  313. }
  314. /* Read bytes */
  315. for(; index < PCCARD_SECTOR_SIZE; index++)
  316. {
  317. *(uint16_t *)pBuffer++ = *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR);
  318. }
  319. /* Update the PCCARD controller state */
  320. hpccard->State = HAL_PCCARD_STATE_READY;
  321. /* Process unlocked */
  322. __HAL_UNLOCK(hpccard);
  323. return HAL_OK;
  324. }
  325. /**
  326. * @brief Write sector to PCCARD memory
  327. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  328. * the configuration information for PCCARD module.
  329. * @param pBuffer pointer to source write buffer
  330. * @param SectorAddress Sector address to write
  331. * @param pStatus pointer to PCCARD status
  332. * @retval HAL status
  333. */
  334. HAL_StatusTypeDef HAL_PCCARD_Write_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  335. {
  336. uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0U;
  337. uint8_t status = 0;
  338. /* Process Locked */
  339. __HAL_LOCK(hpccard);
  340. /* Check the PCCARD controller state */
  341. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  342. {
  343. return HAL_BUSY;
  344. }
  345. /* Update the PCCARD controller state */
  346. hpccard->State = HAL_PCCARD_STATE_BUSY;
  347. /* Initialize PCCARD status */
  348. *pStatus = PCCARD_READY;
  349. /* Set the parameters to write a sector */
  350. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x00;
  351. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  352. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0x30A0;
  353. do
  354. {
  355. /* Wait till the Status = PCCARD_STATUS_OK */
  356. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  357. timeout--;
  358. }while((status != PCCARD_STATUS_OK) && timeout);
  359. if(timeout == 0U)
  360. {
  361. *pStatus = PCCARD_TIMEOUT_ERROR;
  362. }
  363. /* Write bytes */
  364. for(; index < PCCARD_SECTOR_SIZE; index++)
  365. {
  366. *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR) = *(uint16_t *)pBuffer++;
  367. }
  368. do
  369. {
  370. /* Wait till the Status = PCCARD_STATUS_WRITE_OK */
  371. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  372. timeout--;
  373. }while((status != PCCARD_STATUS_WRITE_OK) && timeout);
  374. if(timeout == 0U)
  375. {
  376. *pStatus = PCCARD_TIMEOUT_ERROR;
  377. }
  378. /* Update the PCCARD controller state */
  379. hpccard->State = HAL_PCCARD_STATE_READY;
  380. /* Process unlocked */
  381. __HAL_UNLOCK(hpccard);
  382. return HAL_OK;
  383. }
  384. /**
  385. * @brief Erase sector from PCCARD memory
  386. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  387. * the configuration information for PCCARD module.
  388. * @param SectorAddress Sector address to erase
  389. * @param pStatus pointer to PCCARD status
  390. * @retval HAL status
  391. */
  392. HAL_StatusTypeDef HAL_PCCARD_Erase_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t SectorAddress, uint8_t *pStatus)
  393. {
  394. uint32_t timeout = PCCARD_TIMEOUT_ERASE_SECTOR;
  395. uint8_t status = 0;
  396. /* Process Locked */
  397. __HAL_LOCK(hpccard);
  398. /* Check the PCCARD controller state */
  399. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  400. {
  401. return HAL_BUSY;
  402. }
  403. /* Update the PCCARD controller state */
  404. hpccard->State = HAL_PCCARD_STATE_BUSY;
  405. /* Initialize PCCARD status */
  406. *pStatus = PCCARD_READY;
  407. /* Set the parameters to write a sector */
  408. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_LOW) = 0x00;
  409. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = 0x00;
  410. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_NUMBER) = SectorAddress;
  411. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = 0x01;
  412. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CARD_HEAD) = 0xA0;
  413. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = ATA_ERASE_SECTOR_CMD;
  414. /* wait till the PCCARD is ready */
  415. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  416. while((status != PCCARD_STATUS_WRITE_OK) && timeout)
  417. {
  418. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  419. timeout--;
  420. }
  421. if(timeout == 0U)
  422. {
  423. *pStatus = PCCARD_TIMEOUT_ERROR;
  424. }
  425. /* Check the PCCARD controller state */
  426. hpccard->State = HAL_PCCARD_STATE_READY;
  427. /* Process unlocked */
  428. __HAL_UNLOCK(hpccard);
  429. return HAL_OK;
  430. }
  431. /**
  432. * @brief Reset the PCCARD memory
  433. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  434. * the configuration information for PCCARD module.
  435. * @retval HAL status
  436. */
  437. HAL_StatusTypeDef HAL_PCCARD_Reset(PCCARD_HandleTypeDef *hpccard)
  438. {
  439. /* Process Locked */
  440. __HAL_LOCK(hpccard);
  441. /* Check the PCCARD controller state */
  442. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  443. {
  444. return HAL_BUSY;
  445. }
  446. /* Provide a SW reset and Read and verify the:
  447. - PCCard Configuration Option Register at address 0x98000200 --> 0x80
  448. - Card Configuration and Status Register at address 0x98000202 --> 0x00
  449. - Pin Replacement Register at address 0x98000204 --> 0x0C
  450. - Socket and Copy Register at address 0x98000206 --> 0x00
  451. */
  452. /* Check the PCCARD controller state */
  453. hpccard->State = HAL_PCCARD_STATE_BUSY;
  454. *(__IO uint8_t *)(PCCARD_ATTRIBUTE_SPACE_ADDRESS | ATA_CARD_CONFIGURATION ) = 0x01;
  455. /* Check the PCCARD controller state */
  456. hpccard->State = HAL_PCCARD_STATE_READY;
  457. /* Process unlocked */
  458. __HAL_UNLOCK(hpccard);
  459. return HAL_OK;
  460. }
  461. /**
  462. * @brief This function handles PCCARD device interrupt request.
  463. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  464. * the configuration information for PCCARD module.
  465. * @retval HAL status
  466. */
  467. void HAL_PCCARD_IRQHandler(PCCARD_HandleTypeDef *hpccard)
  468. {
  469. /* Check PCCARD interrupt Rising edge flag */
  470. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE))
  471. {
  472. /* PCCARD interrupt callback*/
  473. HAL_PCCARD_ITCallback(hpccard);
  474. /* Clear PCCARD interrupt Rising edge pending bit */
  475. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE);
  476. }
  477. /* Check PCCARD interrupt Level flag */
  478. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_LEVEL))
  479. {
  480. /* PCCARD interrupt callback*/
  481. HAL_PCCARD_ITCallback(hpccard);
  482. /* Clear PCCARD interrupt Level pending bit */
  483. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_LEVEL);
  484. }
  485. /* Check PCCARD interrupt Falling edge flag */
  486. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE))
  487. {
  488. /* PCCARD interrupt callback*/
  489. HAL_PCCARD_ITCallback(hpccard);
  490. /* Clear PCCARD interrupt Falling edge pending bit */
  491. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE);
  492. }
  493. /* Check PCCARD interrupt FIFO empty flag */
  494. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FEMPT))
  495. {
  496. /* PCCARD interrupt callback*/
  497. HAL_PCCARD_ITCallback(hpccard);
  498. /* Clear PCCARD interrupt FIFO empty pending bit */
  499. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FEMPT);
  500. }
  501. }
  502. /**
  503. * @brief PCCARD interrupt feature callback
  504. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  505. * the configuration information for PCCARD module.
  506. * @retval None
  507. */
  508. __weak void HAL_PCCARD_ITCallback(PCCARD_HandleTypeDef *hpccard)
  509. {
  510. /* Prevent unused argument(s) compilation warning */
  511. UNUSED(hpccard);
  512. /* NOTE : This function Should not be modified, when the callback is needed,
  513. the HAL_PCCARD_ITCallback could be implemented in the user file
  514. */
  515. }
  516. /**
  517. * @}
  518. */
  519. /** @defgroup PCCARD_Exported_Functions_Group3 State functions
  520. * @brief Peripheral State functions
  521. *
  522. @verbatim
  523. ==============================================================================
  524. ##### PCCARD State functions #####
  525. ==============================================================================
  526. [..]
  527. This subsection permits to get in run-time the status of the PCCARD controller
  528. and the data flow.
  529. @endverbatim
  530. * @{
  531. */
  532. /**
  533. * @brief return the PCCARD controller state
  534. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  535. * the configuration information for PCCARD module.
  536. * @retval HAL state
  537. */
  538. HAL_PCCARD_StateTypeDef HAL_PCCARD_GetState(PCCARD_HandleTypeDef *hpccard)
  539. {
  540. return hpccard->State;
  541. }
  542. /**
  543. * @brief Get the compact flash memory status
  544. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  545. * the configuration information for PCCARD module.
  546. * @retval New status of the PCCARD operation. This parameter can be:
  547. * - CompactFlash_TIMEOUT_ERROR: when the previous operation generate
  548. * a Timeout error
  549. * - CompactFlash_READY: when memory is ready for the next operation
  550. */
  551. HAL_PCCARD_StatusTypeDef HAL_PCCARD_GetStatus(PCCARD_HandleTypeDef *hpccard)
  552. {
  553. uint32_t timeout = PCCARD_TIMEOUT_STATUS, status_pccard = 0U;
  554. /* Check the PCCARD controller state */
  555. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  556. {
  557. return HAL_PCCARD_STATUS_ONGOING;
  558. }
  559. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  560. while((status_pccard == PCCARD_BUSY) && timeout)
  561. {
  562. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  563. timeout--;
  564. }
  565. if(timeout == 0U)
  566. {
  567. status_pccard = PCCARD_TIMEOUT_ERROR;
  568. }
  569. /* Return the operation status */
  570. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  571. }
  572. /**
  573. * @brief Reads the Compact Flash memory status using the Read status command
  574. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  575. * the configuration information for PCCARD module.
  576. * @retval The status of the Compact Flash memory. This parameter can be:
  577. * - CompactFlash_BUSY: when memory is busy
  578. * - CompactFlash_READY: when memory is ready for the next operation
  579. * - CompactFlash_ERROR: when the previous operation generates error
  580. */
  581. HAL_PCCARD_StatusTypeDef HAL_PCCARD_ReadStatus(PCCARD_HandleTypeDef *hpccard)
  582. {
  583. uint8_t data = 0U, status_pccard = PCCARD_BUSY;
  584. /* Check the PCCARD controller state */
  585. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  586. {
  587. return HAL_PCCARD_STATUS_ONGOING;
  588. }
  589. /* Read status operation */
  590. data = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  591. if((data & PCCARD_TIMEOUT_ERROR) == PCCARD_TIMEOUT_ERROR)
  592. {
  593. status_pccard = PCCARD_TIMEOUT_ERROR;
  594. }
  595. else if((data & PCCARD_READY) == PCCARD_READY)
  596. {
  597. status_pccard = PCCARD_READY;
  598. }
  599. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  600. }
  601. /**
  602. * @}
  603. */
  604. /**
  605. * @}
  606. */
  607. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  608. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  609. #endif /* HAL_PCCARD_MODULE_ENABLED */
  610. /**
  611. * @}
  612. */
  613. /**
  614. * @}
  615. */
  616. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/