Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

962 linhas
30 KiB

  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. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2016 STMicroelectronics.
  13. * All rights reserved.
  14. *
  15. * This software is licensed under terms that can be found in the LICENSE file
  16. * in the root directory of this software component.
  17. * If no LICENSE file comes with this software, it is provided AS-IS.
  18. *
  19. ******************************************************************************
  20. @verbatim
  21. ===============================================================================
  22. ##### How to use this driver #####
  23. ===============================================================================
  24. [..]
  25. This driver is a generic layered driver which contains a set of APIs used to
  26. control PCCARD/compact flash memories. It uses the FMC/FSMC layer functions
  27. to interface with PCCARD devices. This driver is used for:
  28. (+) PCCARD/Compact Flash memory configuration sequence using the function
  29. HAL_PCCARD_Init()/HAL_CF_Init() with control and timing parameters for
  30. both common and attribute spaces.
  31. (+) Read PCCARD/Compact Flash memory maker and device IDs using the function
  32. HAL_PCCARD_Read_ID()/HAL_CF_Read_ID(). The read information is stored in
  33. the CompactFlash_ID structure declared by the function caller.
  34. (+) Access PCCARD/Compact Flash memory by read/write operations using the functions
  35. HAL_PCCARD_Read_Sector()/ HAL_PCCARD_Write_Sector() -
  36. HAL_CF_Read_Sector()/HAL_CF_Write_Sector(), to read/write sector.
  37. (+) Perform PCCARD/Compact Flash Reset chip operation using the function
  38. HAL_PCCARD_Reset()/HAL_CF_Reset.
  39. (+) Perform PCCARD/Compact Flash erase sector operation using the function
  40. HAL_PCCARD_Erase_Sector()/HAL_CF_Erase_Sector.
  41. (+) Read the PCCARD/Compact Flash status operation using the function
  42. HAL_PCCARD_ReadStatus()/HAL_CF_ReadStatus().
  43. (+) You can monitor the PCCARD/Compact Flash device HAL state by calling
  44. the function HAL_PCCARD_GetState()/HAL_CF_GetState()
  45. [..]
  46. (@) This driver is a set of generic APIs which handle standard PCCARD/compact flash
  47. operations. If a PCCARD/Compact Flash device contains different operations
  48. and/or implementations, it should be implemented separately.
  49. *** Callback registration ***
  50. =============================================
  51. [..]
  52. The compilation define USE_HAL_PCCARD_REGISTER_CALLBACKS when set to 1
  53. allows the user to configure dynamically the driver callbacks.
  54. Use Functions HAL_PCCARD_RegisterCallback() to register a user callback,
  55. it allows to register following callbacks:
  56. (+) MspInitCallback : PCCARD MspInit.
  57. (+) MspDeInitCallback : PCCARD MspDeInit.
  58. This function takes as parameters the HAL peripheral handle, the Callback ID
  59. and a pointer to the user callback function.
  60. Use function HAL_PCCARD_UnRegisterCallback() to reset a callback to the default
  61. weak (surcharged) function. It allows to reset following callbacks:
  62. (+) MspInitCallback : PCCARD MspInit.
  63. (+) MspDeInitCallback : PCCARD MspDeInit.
  64. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  65. By default, after the HAL_PCCARD_Init and if the state is HAL_PCCARD_STATE_RESET
  66. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  67. Exception done for MspInit and MspDeInit callbacks that are respectively
  68. reset to the legacy weak (surcharged) functions in the HAL_PCCARD_Init
  69. and HAL_PCCARD_DeInit only when these callbacks are null (not registered beforehand).
  70. If not, MspInit or MspDeInit are not null, the HAL_PCCARD_Init and HAL_PCCARD_DeInit
  71. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  72. Callbacks can be registered/unregistered in READY state only.
  73. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  74. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  75. during the Init/DeInit.
  76. In that case first register the MspInit/MspDeInit user callbacks
  77. using HAL_PCCARD_RegisterCallback before calling HAL_PCCARD_DeInit
  78. or HAL_PCCARD_Init function.
  79. When The compilation define USE_HAL_PCCARD_REGISTER_CALLBACKS is set to 0 or
  80. not defined, the callback registering feature is not available
  81. and weak (surcharged) callbacks are used.
  82. @endverbatim
  83. ******************************************************************************
  84. */
  85. /* Includes ------------------------------------------------------------------*/
  86. #include "stm32f4xx_hal.h"
  87. #if defined(FMC_Bank4) || defined(FSMC_Bank4)
  88. /** @addtogroup STM32F4xx_HAL_Driver
  89. * @{
  90. */
  91. #ifdef HAL_PCCARD_MODULE_ENABLED
  92. /** @defgroup PCCARD PCCARD
  93. * @brief PCCARD HAL module driver
  94. * @{
  95. */
  96. /* Private typedef -----------------------------------------------------------*/
  97. /* Private define ------------------------------------------------------------*/
  98. /** @defgroup PCCARD_Private_Defines PCCARD Private Defines
  99. * @{
  100. */
  101. #define PCCARD_TIMEOUT_READ_ID 0x0000FFFFU
  102. #define PCCARD_TIMEOUT_READ_WRITE_SECTOR 0x0000FFFFU
  103. #define PCCARD_TIMEOUT_ERASE_SECTOR 0x00000400U
  104. #define PCCARD_TIMEOUT_STATUS 0x01000000U
  105. #define PCCARD_STATUS_OK (uint8_t)0x58
  106. #define PCCARD_STATUS_WRITE_OK (uint8_t)0x50
  107. /**
  108. * @}
  109. */
  110. /* Private macro -------------------------------------------------------------*/
  111. /* Private variables ---------------------------------------------------------*/
  112. /* Private function ----------------------------------------------------------*/
  113. /* Exported functions --------------------------------------------------------*/
  114. /** @defgroup PCCARD_Exported_Functions PCCARD Exported Functions
  115. * @{
  116. */
  117. /** @defgroup PCCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  118. * @brief Initialization and Configuration functions
  119. *
  120. @verbatim
  121. ==============================================================================
  122. ##### PCCARD Initialization and de-initialization functions #####
  123. ==============================================================================
  124. [..]
  125. This section provides functions allowing to initialize/de-initialize
  126. the PCCARD memory
  127. @endverbatim
  128. * @{
  129. */
  130. /**
  131. * @brief Perform the PCCARD memory Initialization sequence
  132. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  133. * the configuration information for PCCARD module.
  134. * @param ComSpaceTiming Common space timing structure
  135. * @param AttSpaceTiming Attribute space timing structure
  136. * @param IOSpaceTiming IO space timing structure
  137. * @retval HAL status
  138. */
  139. HAL_StatusTypeDef HAL_PCCARD_Init(PCCARD_HandleTypeDef *hpccard, FMC_NAND_PCC_TimingTypeDef *ComSpaceTiming,
  140. FMC_NAND_PCC_TimingTypeDef *AttSpaceTiming,
  141. FMC_NAND_PCC_TimingTypeDef *IOSpaceTiming)
  142. {
  143. /* Check the PCCARD controller state */
  144. if (hpccard == NULL)
  145. {
  146. return HAL_ERROR;
  147. }
  148. if (hpccard->State == HAL_PCCARD_STATE_RESET)
  149. {
  150. /* Allocate lock resource and initialize it */
  151. hpccard->Lock = HAL_UNLOCKED;
  152. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  153. if (hpccard->MspInitCallback == NULL)
  154. {
  155. hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  156. }
  157. hpccard->ItCallback = HAL_PCCARD_ITCallback;
  158. /* Init the low level hardware */
  159. hpccard->MspInitCallback(hpccard);
  160. #else
  161. /* Initialize the low level hardware (MSP) */
  162. HAL_PCCARD_MspInit(hpccard);
  163. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  164. }
  165. /* Initialize the PCCARD state */
  166. hpccard->State = HAL_PCCARD_STATE_BUSY;
  167. /* Initialize PCCARD control Interface */
  168. FMC_PCCARD_Init(hpccard->Instance, &(hpccard->Init));
  169. /* Init PCCARD common space timing Interface */
  170. FMC_PCCARD_CommonSpace_Timing_Init(hpccard->Instance, ComSpaceTiming);
  171. /* Init PCCARD attribute space timing Interface */
  172. FMC_PCCARD_AttributeSpace_Timing_Init(hpccard->Instance, AttSpaceTiming);
  173. /* Init PCCARD IO space timing Interface */
  174. FMC_PCCARD_IOSpace_Timing_Init(hpccard->Instance, IOSpaceTiming);
  175. /* Enable the PCCARD device */
  176. __FMC_PCCARD_ENABLE(hpccard->Instance);
  177. /* Update the PCCARD state */
  178. hpccard->State = HAL_PCCARD_STATE_READY;
  179. return HAL_OK;
  180. }
  181. /**
  182. * @brief Perform the PCCARD memory De-initialization sequence
  183. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  184. * the configuration information for PCCARD module.
  185. * @retval HAL status
  186. */
  187. HAL_StatusTypeDef HAL_PCCARD_DeInit(PCCARD_HandleTypeDef *hpccard)
  188. {
  189. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  190. if (hpccard->MspDeInitCallback == NULL)
  191. {
  192. hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  193. }
  194. /* DeInit the low level hardware */
  195. hpccard->MspDeInitCallback(hpccard);
  196. #else
  197. /* De-Initialize the low level hardware (MSP) */
  198. HAL_PCCARD_MspDeInit(hpccard);
  199. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  200. /* Configure the PCCARD registers with their reset values */
  201. FMC_PCCARD_DeInit(hpccard->Instance);
  202. /* Update the PCCARD controller state */
  203. hpccard->State = HAL_PCCARD_STATE_RESET;
  204. /* Release Lock */
  205. __HAL_UNLOCK(hpccard);
  206. return HAL_OK;
  207. }
  208. /**
  209. * @brief PCCARD MSP Init
  210. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  211. * the configuration information for PCCARD module.
  212. * @retval None
  213. */
  214. __weak void HAL_PCCARD_MspInit(PCCARD_HandleTypeDef *hpccard)
  215. {
  216. /* Prevent unused argument(s) compilation warning */
  217. UNUSED(hpccard);
  218. /* NOTE : This function Should not be modified, when the callback is needed,
  219. the HAL_PCCARD_MspInit could be implemented in the user file
  220. */
  221. }
  222. /**
  223. * @brief PCCARD MSP DeInit
  224. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  225. * the configuration information for PCCARD module.
  226. * @retval None
  227. */
  228. __weak void HAL_PCCARD_MspDeInit(PCCARD_HandleTypeDef *hpccard)
  229. {
  230. /* Prevent unused argument(s) compilation warning */
  231. UNUSED(hpccard);
  232. /* NOTE : This function Should not be modified, when the callback is needed,
  233. the HAL_PCCARD_MspDeInit could be implemented in the user file
  234. */
  235. }
  236. /**
  237. * @}
  238. */
  239. /** @defgroup PCCARD_Exported_Functions_Group2 Input and Output functions
  240. * @brief Input Output and memory control functions
  241. *
  242. @verbatim
  243. ==============================================================================
  244. ##### PCCARD Input and Output functions #####
  245. ==============================================================================
  246. [..]
  247. This section provides functions allowing to use and control the PCCARD memory
  248. @endverbatim
  249. * @{
  250. */
  251. /**
  252. * @brief Read Compact Flash's ID.
  253. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  254. * the configuration information for PCCARD module.
  255. * @param CompactFlash_ID Compact flash ID structure.
  256. * @param pStatus pointer to compact flash status
  257. * @retval HAL status
  258. *
  259. */
  260. HAL_StatusTypeDef HAL_PCCARD_Read_ID(PCCARD_HandleTypeDef *hpccard, uint8_t CompactFlash_ID[], uint8_t *pStatus)
  261. {
  262. uint32_t timeout = 0U;
  263. uint32_t index = 0U;
  264. uint8_t status = 0U;
  265. /* Process Locked */
  266. __HAL_LOCK(hpccard);
  267. /* Check the PCCARD controller state */
  268. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  269. {
  270. return HAL_BUSY;
  271. }
  272. /* Initialize timeout value */
  273. timeout = PCCARD_TIMEOUT_READ_ID;
  274. /* Update the PCCARD controller state */
  275. hpccard->State = HAL_PCCARD_STATE_BUSY;
  276. /* Initialize the PCCARD status */
  277. *pStatus = PCCARD_READY;
  278. /* Send the Identify Command */
  279. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0xECEC;
  280. /* Read PCCARD IDs and timeout treatment */
  281. do
  282. {
  283. /* Read the PCCARD status */
  284. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  285. timeout--;
  286. } while ((status != PCCARD_STATUS_OK) && timeout);
  287. if (timeout == 0U)
  288. {
  289. *pStatus = PCCARD_TIMEOUT_ERROR;
  290. }
  291. else
  292. {
  293. /* Read PCCARD ID bytes */
  294. for (index = 0U; index < 16U; index++)
  295. {
  296. CompactFlash_ID[index] = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_DATA);
  297. }
  298. }
  299. /* Update the PCCARD controller state */
  300. hpccard->State = HAL_PCCARD_STATE_READY;
  301. /* Process unlocked */
  302. __HAL_UNLOCK(hpccard);
  303. return HAL_OK;
  304. }
  305. /**
  306. * @brief Read sector from PCCARD memory
  307. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  308. * the configuration information for PCCARD module.
  309. * @param pBuffer pointer to destination read buffer
  310. * @param SectorAddress Sector address to read
  311. * @param pStatus pointer to PCCARD status
  312. * @retval HAL status
  313. */
  314. HAL_StatusTypeDef HAL_PCCARD_Read_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress,
  315. uint8_t *pStatus)
  316. {
  317. uint32_t timeout = 0U;
  318. uint32_t index = 0U;
  319. uint8_t status = 0U;
  320. /* Process Locked */
  321. __HAL_LOCK(hpccard);
  322. /* Check the PCCARD controller state */
  323. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  324. {
  325. return HAL_BUSY;
  326. }
  327. /* Initialize timeout value */
  328. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  329. /* Update the PCCARD controller state */
  330. hpccard->State = HAL_PCCARD_STATE_BUSY;
  331. /* Initialize PCCARD status */
  332. *pStatus = PCCARD_READY;
  333. /* Set the parameters to write a sector */
  334. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x0000;
  335. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  336. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0xE4A0;
  337. do
  338. {
  339. /* wait till the Status = 0x80 */
  340. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  341. timeout--;
  342. } while ((status == 0x80U) && timeout);
  343. if (timeout == 0U)
  344. {
  345. *pStatus = PCCARD_TIMEOUT_ERROR;
  346. }
  347. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  348. do
  349. {
  350. /* wait till the Status = PCCARD_STATUS_OK */
  351. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  352. timeout--;
  353. } while ((status != PCCARD_STATUS_OK) && timeout);
  354. if (timeout == 0U)
  355. {
  356. *pStatus = PCCARD_TIMEOUT_ERROR;
  357. }
  358. /* Read bytes */
  359. for (; index < PCCARD_SECTOR_SIZE; index++)
  360. {
  361. *(uint16_t *)pBuffer++ = *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR);
  362. }
  363. /* Update the PCCARD controller state */
  364. hpccard->State = HAL_PCCARD_STATE_READY;
  365. /* Process unlocked */
  366. __HAL_UNLOCK(hpccard);
  367. return HAL_OK;
  368. }
  369. /**
  370. * @brief Write sector to PCCARD memory
  371. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  372. * the configuration information for PCCARD module.
  373. * @param pBuffer pointer to source write buffer
  374. * @param SectorAddress Sector address to write
  375. * @param pStatus pointer to PCCARD status
  376. * @retval HAL status
  377. */
  378. HAL_StatusTypeDef HAL_PCCARD_Write_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress,
  379. uint8_t *pStatus)
  380. {
  381. uint32_t timeout = 0U;
  382. uint32_t index = 0U;
  383. uint8_t status = 0U;
  384. /* Process Locked */
  385. __HAL_LOCK(hpccard);
  386. /* Check the PCCARD controller state */
  387. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  388. {
  389. return HAL_BUSY;
  390. }
  391. /* Initialize timeout value */
  392. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  393. /* Update the PCCARD controller state */
  394. hpccard->State = HAL_PCCARD_STATE_BUSY;
  395. /* Initialize PCCARD status */
  396. *pStatus = PCCARD_READY;
  397. /* Set the parameters to write a sector */
  398. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x0000;
  399. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  400. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0x30A0;
  401. do
  402. {
  403. /* Wait till the Status = PCCARD_STATUS_OK */
  404. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  405. timeout--;
  406. } while ((status != PCCARD_STATUS_OK) && timeout);
  407. if (timeout == 0U)
  408. {
  409. *pStatus = PCCARD_TIMEOUT_ERROR;
  410. }
  411. /* Write bytes */
  412. for (; index < PCCARD_SECTOR_SIZE; index++)
  413. {
  414. *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR) = *(uint16_t *)pBuffer++;
  415. }
  416. do
  417. {
  418. /* Wait till the Status = PCCARD_STATUS_WRITE_OK */
  419. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  420. timeout--;
  421. } while ((status != PCCARD_STATUS_WRITE_OK) && timeout);
  422. if (timeout == 0U)
  423. {
  424. *pStatus = PCCARD_TIMEOUT_ERROR;
  425. }
  426. /* Update the PCCARD controller state */
  427. hpccard->State = HAL_PCCARD_STATE_READY;
  428. /* Process unlocked */
  429. __HAL_UNLOCK(hpccard);
  430. return HAL_OK;
  431. }
  432. /**
  433. * @brief Erase sector from PCCARD memory
  434. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  435. * the configuration information for PCCARD module.
  436. * @param SectorAddress Sector address to erase
  437. * @param pStatus pointer to PCCARD status
  438. * @retval HAL status
  439. */
  440. HAL_StatusTypeDef HAL_PCCARD_Erase_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t SectorAddress, uint8_t *pStatus)
  441. {
  442. uint32_t timeout = PCCARD_TIMEOUT_ERASE_SECTOR;
  443. uint8_t status = 0U;
  444. /* Process Locked */
  445. __HAL_LOCK(hpccard);
  446. /* Check the PCCARD controller state */
  447. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  448. {
  449. return HAL_BUSY;
  450. }
  451. /* Update the PCCARD controller state */
  452. hpccard->State = HAL_PCCARD_STATE_BUSY;
  453. /* Initialize PCCARD status */
  454. *pStatus = PCCARD_READY;
  455. /* Set the parameters to write a sector */
  456. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_LOW) = 0x00;
  457. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = 0x00;
  458. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_NUMBER) = SectorAddress;
  459. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = 0x01;
  460. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CARD_HEAD) = 0xA0;
  461. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = ATA_ERASE_SECTOR_CMD;
  462. /* wait till the PCCARD is ready */
  463. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  464. while ((status != PCCARD_STATUS_WRITE_OK) && timeout)
  465. {
  466. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  467. timeout--;
  468. }
  469. if (timeout == 0U)
  470. {
  471. *pStatus = PCCARD_TIMEOUT_ERROR;
  472. }
  473. /* Check the PCCARD controller state */
  474. hpccard->State = HAL_PCCARD_STATE_READY;
  475. /* Process unlocked */
  476. __HAL_UNLOCK(hpccard);
  477. return HAL_OK;
  478. }
  479. /**
  480. * @brief Reset the PCCARD memory
  481. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  482. * the configuration information for PCCARD module.
  483. * @retval HAL status
  484. */
  485. HAL_StatusTypeDef HAL_PCCARD_Reset(PCCARD_HandleTypeDef *hpccard)
  486. {
  487. /* Process Locked */
  488. __HAL_LOCK(hpccard);
  489. /* Check the PCCARD controller state */
  490. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  491. {
  492. return HAL_BUSY;
  493. }
  494. /* Provide a SW reset and Read and verify the:
  495. - PCCard Configuration Option Register at address 0x98000200 --> 0x80
  496. - Card Configuration and Status Register at address 0x98000202 --> 0x00
  497. - Pin Replacement Register at address 0x98000204 --> 0x0C
  498. - Socket and Copy Register at address 0x98000206 --> 0x00
  499. */
  500. /* Check the PCCARD controller state */
  501. hpccard->State = HAL_PCCARD_STATE_BUSY;
  502. *(__IO uint8_t *)(PCCARD_ATTRIBUTE_SPACE_ADDRESS | ATA_CARD_CONFIGURATION) = 0x01;
  503. /* Check the PCCARD controller state */
  504. hpccard->State = HAL_PCCARD_STATE_READY;
  505. /* Process unlocked */
  506. __HAL_UNLOCK(hpccard);
  507. return HAL_OK;
  508. }
  509. /**
  510. * @brief This function handles PCCARD device interrupt request.
  511. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  512. * the configuration information for PCCARD module.
  513. * @retval HAL status
  514. */
  515. void HAL_PCCARD_IRQHandler(PCCARD_HandleTypeDef *hpccard)
  516. {
  517. /* Check PCCARD interrupt Rising edge flag */
  518. if (__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE))
  519. {
  520. /* PCCARD interrupt callback*/
  521. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  522. hpccard->ItCallback(hpccard);
  523. #else
  524. HAL_PCCARD_ITCallback(hpccard);
  525. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  526. /* Clear PCCARD interrupt Rising edge pending bit */
  527. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE);
  528. }
  529. /* Check PCCARD interrupt Level flag */
  530. if (__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_LEVEL))
  531. {
  532. /* PCCARD interrupt callback*/
  533. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  534. hpccard->ItCallback(hpccard);
  535. #else
  536. HAL_PCCARD_ITCallback(hpccard);
  537. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  538. /* Clear PCCARD interrupt Level pending bit */
  539. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_LEVEL);
  540. }
  541. /* Check PCCARD interrupt Falling edge flag */
  542. if (__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE))
  543. {
  544. /* PCCARD interrupt callback*/
  545. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  546. hpccard->ItCallback(hpccard);
  547. #else
  548. HAL_PCCARD_ITCallback(hpccard);
  549. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  550. /* Clear PCCARD interrupt Falling edge pending bit */
  551. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE);
  552. }
  553. /* Check PCCARD interrupt FIFO empty flag */
  554. if (__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FEMPT))
  555. {
  556. /* PCCARD interrupt callback*/
  557. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  558. hpccard->ItCallback(hpccard);
  559. #else
  560. HAL_PCCARD_ITCallback(hpccard);
  561. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  562. /* Clear PCCARD interrupt FIFO empty pending bit */
  563. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FEMPT);
  564. }
  565. }
  566. /**
  567. * @brief PCCARD interrupt feature callback
  568. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  569. * the configuration information for PCCARD module.
  570. * @retval None
  571. */
  572. __weak void HAL_PCCARD_ITCallback(PCCARD_HandleTypeDef *hpccard)
  573. {
  574. /* Prevent unused argument(s) compilation warning */
  575. UNUSED(hpccard);
  576. /* NOTE : This function Should not be modified, when the callback is needed,
  577. the HAL_PCCARD_ITCallback could be implemented in the user file
  578. */
  579. }
  580. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  581. /**
  582. * @brief Register a User PCCARD Callback
  583. * To be used instead of the weak (surcharged) predefined callback
  584. * @param hpccard : PCCARD handle
  585. * @param CallbackId : ID of the callback to be registered
  586. * This parameter can be one of the following values:
  587. * @arg @ref HAL_PCCARD_MSP_INIT_CB_ID PCCARD MspInit callback ID
  588. * @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID PCCARD MspDeInit callback ID
  589. * @arg @ref HAL_PCCARD_IT_CB_ID PCCARD IT callback ID
  590. * @param pCallback : pointer to the Callback function
  591. * @retval status
  592. */
  593. HAL_StatusTypeDef HAL_PCCARD_RegisterCallback(PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId,
  594. pPCCARD_CallbackTypeDef pCallback)
  595. {
  596. HAL_StatusTypeDef status = HAL_OK;
  597. if (pCallback == NULL)
  598. {
  599. return HAL_ERROR;
  600. }
  601. /* Process locked */
  602. __HAL_LOCK(hpccard);
  603. if (hpccard->State == HAL_PCCARD_STATE_READY)
  604. {
  605. switch (CallbackId)
  606. {
  607. case HAL_PCCARD_MSP_INIT_CB_ID :
  608. hpccard->MspInitCallback = pCallback;
  609. break;
  610. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  611. hpccard->MspDeInitCallback = pCallback;
  612. break;
  613. case HAL_PCCARD_IT_CB_ID :
  614. hpccard->ItCallback = pCallback;
  615. break;
  616. default :
  617. /* update return status */
  618. status = HAL_ERROR;
  619. break;
  620. }
  621. }
  622. else if (hpccard->State == HAL_PCCARD_STATE_RESET)
  623. {
  624. switch (CallbackId)
  625. {
  626. case HAL_PCCARD_MSP_INIT_CB_ID :
  627. hpccard->MspInitCallback = pCallback;
  628. break;
  629. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  630. hpccard->MspDeInitCallback = pCallback;
  631. break;
  632. default :
  633. /* update return status */
  634. status = HAL_ERROR;
  635. break;
  636. }
  637. }
  638. else
  639. {
  640. /* update return status */
  641. status = HAL_ERROR;
  642. }
  643. /* Release Lock */
  644. __HAL_UNLOCK(hpccard);
  645. return status;
  646. }
  647. /**
  648. * @brief Unregister a User PCCARD Callback
  649. * PCCARD Callback is redirected to the weak (surcharged) predefined callback
  650. * @param hpccard : PCCARD handle
  651. * @param CallbackId : ID of the callback to be unregistered
  652. * This parameter can be one of the following values:
  653. * @arg @ref HAL_PCCARD_MSP_INIT_CB_ID PCCARD MspInit callback ID
  654. * @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID PCCARD MspDeInit callback ID
  655. * @arg @ref HAL_PCCARD_IT_CB_ID PCCARD IT callback ID
  656. * @retval status
  657. */
  658. HAL_StatusTypeDef HAL_PCCARD_UnRegisterCallback(PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId)
  659. {
  660. HAL_StatusTypeDef status = HAL_OK;
  661. /* Process locked */
  662. __HAL_LOCK(hpccard);
  663. if (hpccard->State == HAL_PCCARD_STATE_READY)
  664. {
  665. switch (CallbackId)
  666. {
  667. case HAL_PCCARD_MSP_INIT_CB_ID :
  668. hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  669. break;
  670. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  671. hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  672. break;
  673. case HAL_PCCARD_IT_CB_ID :
  674. hpccard->ItCallback = HAL_PCCARD_ITCallback;
  675. break;
  676. default :
  677. /* update return status */
  678. status = HAL_ERROR;
  679. break;
  680. }
  681. }
  682. else if (hpccard->State == HAL_PCCARD_STATE_RESET)
  683. {
  684. switch (CallbackId)
  685. {
  686. case HAL_PCCARD_MSP_INIT_CB_ID :
  687. hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  688. break;
  689. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  690. hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  691. break;
  692. default :
  693. /* update return status */
  694. status = HAL_ERROR;
  695. break;
  696. }
  697. }
  698. else
  699. {
  700. /* update return status */
  701. status = HAL_ERROR;
  702. }
  703. /* Release Lock */
  704. __HAL_UNLOCK(hpccard);
  705. return status;
  706. }
  707. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  708. /**
  709. * @}
  710. */
  711. /** @defgroup PCCARD_Exported_Functions_Group3 State functions
  712. * @brief Peripheral State functions
  713. *
  714. @verbatim
  715. ==============================================================================
  716. ##### PCCARD State functions #####
  717. ==============================================================================
  718. [..]
  719. This subsection permits to get in run-time the status of the PCCARD controller
  720. and the data flow.
  721. @endverbatim
  722. * @{
  723. */
  724. /**
  725. * @brief return the PCCARD controller state
  726. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  727. * the configuration information for PCCARD module.
  728. * @retval HAL state
  729. */
  730. HAL_PCCARD_StateTypeDef HAL_PCCARD_GetState(PCCARD_HandleTypeDef *hpccard)
  731. {
  732. return hpccard->State;
  733. }
  734. /**
  735. * @brief Get the compact flash memory status
  736. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  737. * the configuration information for PCCARD module.
  738. * @retval New status of the PCCARD operation. This parameter can be:
  739. * - CompactFlash_TIMEOUT_ERROR: when the previous operation generate
  740. * a Timeout error
  741. * - CompactFlash_READY: when memory is ready for the next operation
  742. */
  743. HAL_PCCARD_StatusTypeDef HAL_PCCARD_GetStatus(PCCARD_HandleTypeDef *hpccard)
  744. {
  745. uint32_t timeout = PCCARD_TIMEOUT_STATUS;
  746. uint32_t status_pccard = 0U;
  747. /* Check the PCCARD controller state */
  748. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  749. {
  750. return HAL_PCCARD_STATUS_ONGOING;
  751. }
  752. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  753. while ((status_pccard == PCCARD_BUSY) && timeout)
  754. {
  755. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  756. timeout--;
  757. }
  758. if (timeout == 0U)
  759. {
  760. status_pccard = PCCARD_TIMEOUT_ERROR;
  761. }
  762. /* Return the operation status */
  763. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  764. }
  765. /**
  766. * @brief Reads the Compact Flash memory status using the Read status command
  767. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  768. * the configuration information for PCCARD module.
  769. * @retval The status of the Compact Flash memory. This parameter can be:
  770. * - CompactFlash_BUSY: when memory is busy
  771. * - CompactFlash_READY: when memory is ready for the next operation
  772. * - CompactFlash_ERROR: when the previous operation generates error
  773. */
  774. HAL_PCCARD_StatusTypeDef HAL_PCCARD_ReadStatus(PCCARD_HandleTypeDef *hpccard)
  775. {
  776. uint8_t data = 0U;
  777. uint8_t status_pccard = PCCARD_BUSY;
  778. /* Check the PCCARD controller state */
  779. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  780. {
  781. return HAL_PCCARD_STATUS_ONGOING;
  782. }
  783. /* Read status operation */
  784. data = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  785. if ((data & PCCARD_TIMEOUT_ERROR) == PCCARD_TIMEOUT_ERROR)
  786. {
  787. status_pccard = PCCARD_TIMEOUT_ERROR;
  788. }
  789. else if ((data & PCCARD_READY) == PCCARD_READY)
  790. {
  791. status_pccard = PCCARD_READY;
  792. }
  793. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  794. }
  795. /**
  796. * @}
  797. */
  798. /**
  799. * @}
  800. */
  801. /**
  802. * @}
  803. */
  804. #endif /* HAL_PCCARD_MODULE_ENABLED */
  805. /**
  806. * @}
  807. */
  808. #endif /* FMC_Bank4 || FSMC_Bank4 */