stm32f1xx_hal_pccard.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 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 "stm32f1xx_hal.h"
  87. #if defined(FSMC_BANK4)
  88. /** @addtogroup STM32F1xx_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, FSMC_NAND_PCC_TimingTypeDef *ComSpaceTiming,
  140. FSMC_NAND_PCC_TimingTypeDef *AttSpaceTiming,
  141. FSMC_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. FSMC_PCCARD_Init(hpccard->Instance, &(hpccard->Init));
  169. /* Init PCCARD common space timing Interface */
  170. FSMC_PCCARD_CommonSpace_Timing_Init(hpccard->Instance, ComSpaceTiming);
  171. /* Init PCCARD attribute space timing Interface */
  172. FSMC_PCCARD_AttributeSpace_Timing_Init(hpccard->Instance, AttSpaceTiming);
  173. /* Init PCCARD IO space timing Interface */
  174. FSMC_PCCARD_IOSpace_Timing_Init(hpccard->Instance, IOSpaceTiming);
  175. /* Enable the PCCARD device */
  176. __FSMC_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. FSMC_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. /* Update the PCCARD controller state */
  273. hpccard->State = HAL_PCCARD_STATE_BUSY;
  274. /* Initialize the PCCARD status */
  275. *pStatus = PCCARD_READY;
  276. /* Send the Identify Command */
  277. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0xECEC;
  278. /* Read PCCARD IDs and timeout treatment */
  279. do
  280. {
  281. /* Read the PCCARD status */
  282. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  283. timeout--;
  284. } while ((status != PCCARD_STATUS_OK) && timeout);
  285. if (timeout == 0U)
  286. {
  287. *pStatus = PCCARD_TIMEOUT_ERROR;
  288. }
  289. else
  290. {
  291. /* Read PCCARD ID bytes */
  292. for (index = 0U; index < 16U; index++)
  293. {
  294. CompactFlash_ID[index] = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_DATA);
  295. }
  296. }
  297. /* Update the PCCARD controller state */
  298. hpccard->State = HAL_PCCARD_STATE_READY;
  299. /* Process unlocked */
  300. __HAL_UNLOCK(hpccard);
  301. return HAL_OK;
  302. }
  303. /**
  304. * @brief Read sector from PCCARD memory
  305. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  306. * the configuration information for PCCARD module.
  307. * @param pBuffer pointer to destination read buffer
  308. * @param SectorAddress Sector address to read
  309. * @param pStatus pointer to PCCARD status
  310. * @retval HAL status
  311. */
  312. HAL_StatusTypeDef HAL_PCCARD_Read_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress,
  313. uint8_t *pStatus)
  314. {
  315. uint32_t timeout = 0U;
  316. uint32_t index = 0U;
  317. uint8_t status = 0U;
  318. /* Process Locked */
  319. __HAL_LOCK(hpccard);
  320. /* Check the PCCARD controller state */
  321. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  322. {
  323. return HAL_BUSY;
  324. }
  325. /* Update the PCCARD controller state */
  326. hpccard->State = HAL_PCCARD_STATE_BUSY;
  327. /* Initialize PCCARD status */
  328. *pStatus = PCCARD_READY;
  329. /* Set the parameters to write a sector */
  330. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x0000;
  331. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  332. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0xE4A0;
  333. do
  334. {
  335. /* wait till the Status = 0x80 */
  336. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  337. timeout--;
  338. } while ((status == 0x80U) && timeout);
  339. if (timeout == 0U)
  340. {
  341. *pStatus = PCCARD_TIMEOUT_ERROR;
  342. }
  343. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  344. do
  345. {
  346. /* wait till the Status = PCCARD_STATUS_OK */
  347. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  348. timeout--;
  349. } while ((status != PCCARD_STATUS_OK) && timeout);
  350. if (timeout == 0U)
  351. {
  352. *pStatus = PCCARD_TIMEOUT_ERROR;
  353. }
  354. /* Read bytes */
  355. for (; index < PCCARD_SECTOR_SIZE; index++)
  356. {
  357. *(uint16_t *)pBuffer++ = *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR);
  358. }
  359. /* Update the PCCARD controller state */
  360. hpccard->State = HAL_PCCARD_STATE_READY;
  361. /* Process unlocked */
  362. __HAL_UNLOCK(hpccard);
  363. return HAL_OK;
  364. }
  365. /**
  366. * @brief Write sector to PCCARD memory
  367. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  368. * the configuration information for PCCARD module.
  369. * @param pBuffer pointer to source write buffer
  370. * @param SectorAddress Sector address to write
  371. * @param pStatus pointer to PCCARD status
  372. * @retval HAL status
  373. */
  374. HAL_StatusTypeDef HAL_PCCARD_Write_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress,
  375. uint8_t *pStatus)
  376. {
  377. uint32_t timeout = 0U;
  378. uint32_t index = 0U;
  379. uint8_t status = 0U;
  380. /* Process Locked */
  381. __HAL_LOCK(hpccard);
  382. /* Check the PCCARD controller state */
  383. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  384. {
  385. return HAL_BUSY;
  386. }
  387. /* Update the PCCARD controller state */
  388. hpccard->State = HAL_PCCARD_STATE_BUSY;
  389. /* Initialize PCCARD status */
  390. *pStatus = PCCARD_READY;
  391. /* Set the parameters to write a sector */
  392. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x0000;
  393. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  394. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0x30A0;
  395. do
  396. {
  397. /* Wait till the Status = PCCARD_STATUS_OK */
  398. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  399. timeout--;
  400. } while ((status != PCCARD_STATUS_OK) && timeout);
  401. if (timeout == 0U)
  402. {
  403. *pStatus = PCCARD_TIMEOUT_ERROR;
  404. }
  405. /* Write bytes */
  406. for (; index < PCCARD_SECTOR_SIZE; index++)
  407. {
  408. *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR) = *(uint16_t *)pBuffer++;
  409. }
  410. do
  411. {
  412. /* Wait till the Status = PCCARD_STATUS_WRITE_OK */
  413. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  414. timeout--;
  415. } while ((status != PCCARD_STATUS_WRITE_OK) && timeout);
  416. if (timeout == 0U)
  417. {
  418. *pStatus = PCCARD_TIMEOUT_ERROR;
  419. }
  420. /* Update the PCCARD controller state */
  421. hpccard->State = HAL_PCCARD_STATE_READY;
  422. /* Process unlocked */
  423. __HAL_UNLOCK(hpccard);
  424. return HAL_OK;
  425. }
  426. /**
  427. * @brief Erase sector from PCCARD memory
  428. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  429. * the configuration information for PCCARD module.
  430. * @param SectorAddress Sector address to erase
  431. * @param pStatus pointer to PCCARD status
  432. * @retval HAL status
  433. */
  434. HAL_StatusTypeDef HAL_PCCARD_Erase_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t SectorAddress, uint8_t *pStatus)
  435. {
  436. uint32_t timeout = PCCARD_TIMEOUT_ERASE_SECTOR;
  437. uint8_t status = 0U;
  438. /* Process Locked */
  439. __HAL_LOCK(hpccard);
  440. /* Check the PCCARD controller state */
  441. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  442. {
  443. return HAL_BUSY;
  444. }
  445. /* Update the PCCARD controller state */
  446. hpccard->State = HAL_PCCARD_STATE_BUSY;
  447. /* Initialize PCCARD status */
  448. *pStatus = PCCARD_READY;
  449. /* Set the parameters to write a sector */
  450. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_LOW) = 0x00;
  451. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = 0x00;
  452. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_NUMBER) = SectorAddress;
  453. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = 0x01;
  454. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CARD_HEAD) = 0xA0;
  455. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = ATA_ERASE_SECTOR_CMD;
  456. /* wait till the PCCARD is ready */
  457. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  458. while ((status != PCCARD_STATUS_WRITE_OK) && timeout)
  459. {
  460. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  461. timeout--;
  462. }
  463. if (timeout == 0U)
  464. {
  465. *pStatus = PCCARD_TIMEOUT_ERROR;
  466. }
  467. /* Check the PCCARD controller state */
  468. hpccard->State = HAL_PCCARD_STATE_READY;
  469. /* Process unlocked */
  470. __HAL_UNLOCK(hpccard);
  471. return HAL_OK;
  472. }
  473. /**
  474. * @brief Reset the PCCARD memory
  475. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  476. * the configuration information for PCCARD module.
  477. * @retval HAL status
  478. */
  479. HAL_StatusTypeDef HAL_PCCARD_Reset(PCCARD_HandleTypeDef *hpccard)
  480. {
  481. /* Process Locked */
  482. __HAL_LOCK(hpccard);
  483. /* Check the PCCARD controller state */
  484. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  485. {
  486. return HAL_BUSY;
  487. }
  488. /* Provide a SW reset and Read and verify the:
  489. - PCCard Configuration Option Register at address 0x98000200 --> 0x80
  490. - Card Configuration and Status Register at address 0x98000202 --> 0x00
  491. - Pin Replacement Register at address 0x98000204 --> 0x0C
  492. - Socket and Copy Register at address 0x98000206 --> 0x00
  493. */
  494. /* Check the PCCARD controller state */
  495. hpccard->State = HAL_PCCARD_STATE_BUSY;
  496. *(__IO uint8_t *)(PCCARD_ATTRIBUTE_SPACE_ADDRESS | ATA_CARD_CONFIGURATION) = 0x01;
  497. /* Check the PCCARD controller state */
  498. hpccard->State = HAL_PCCARD_STATE_READY;
  499. /* Process unlocked */
  500. __HAL_UNLOCK(hpccard);
  501. return HAL_OK;
  502. }
  503. /**
  504. * @brief This function handles PCCARD device interrupt request.
  505. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  506. * the configuration information for PCCARD module.
  507. * @retval HAL status
  508. */
  509. void HAL_PCCARD_IRQHandler(PCCARD_HandleTypeDef *hpccard)
  510. {
  511. /* Check PCCARD interrupt Rising edge flag */
  512. if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_RISING_EDGE))
  513. {
  514. /* PCCARD interrupt callback*/
  515. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  516. hpccard->ItCallback(hpccard);
  517. #else
  518. HAL_PCCARD_ITCallback(hpccard);
  519. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  520. /* Clear PCCARD interrupt Rising edge pending bit */
  521. __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_RISING_EDGE);
  522. }
  523. /* Check PCCARD interrupt Level flag */
  524. if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_LEVEL))
  525. {
  526. /* PCCARD interrupt callback*/
  527. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  528. hpccard->ItCallback(hpccard);
  529. #else
  530. HAL_PCCARD_ITCallback(hpccard);
  531. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  532. /* Clear PCCARD interrupt Level pending bit */
  533. __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_LEVEL);
  534. }
  535. /* Check PCCARD interrupt Falling edge flag */
  536. if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_FALLING_EDGE))
  537. {
  538. /* PCCARD interrupt callback*/
  539. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  540. hpccard->ItCallback(hpccard);
  541. #else
  542. HAL_PCCARD_ITCallback(hpccard);
  543. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  544. /* Clear PCCARD interrupt Falling edge pending bit */
  545. __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_FALLING_EDGE);
  546. }
  547. /* Check PCCARD interrupt FIFO empty flag */
  548. if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_FEMPT))
  549. {
  550. /* PCCARD interrupt callback*/
  551. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  552. hpccard->ItCallback(hpccard);
  553. #else
  554. HAL_PCCARD_ITCallback(hpccard);
  555. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  556. /* Clear PCCARD interrupt FIFO empty pending bit */
  557. __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_FEMPT);
  558. }
  559. }
  560. /**
  561. * @brief PCCARD interrupt feature callback
  562. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  563. * the configuration information for PCCARD module.
  564. * @retval None
  565. */
  566. __weak void HAL_PCCARD_ITCallback(PCCARD_HandleTypeDef *hpccard)
  567. {
  568. /* Prevent unused argument(s) compilation warning */
  569. UNUSED(hpccard);
  570. /* NOTE : This function Should not be modified, when the callback is needed,
  571. the HAL_PCCARD_ITCallback could be implemented in the user file
  572. */
  573. }
  574. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  575. /**
  576. * @brief Register a User PCCARD Callback
  577. * To be used instead of the weak (surcharged) predefined callback
  578. * @param hpccard : PCCARD handle
  579. * @param CallbackId : ID of the callback to be registered
  580. * This parameter can be one of the following values:
  581. * @arg @ref HAL_PCCARD_MSP_INIT_CB_ID PCCARD MspInit callback ID
  582. * @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID PCCARD MspDeInit callback ID
  583. * @arg @ref HAL_PCCARD_IT_CB_ID PCCARD IT callback ID
  584. * @param pCallback : pointer to the Callback function
  585. * @retval status
  586. */
  587. HAL_StatusTypeDef HAL_PCCARD_RegisterCallback(PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId,
  588. pPCCARD_CallbackTypeDef pCallback)
  589. {
  590. HAL_StatusTypeDef status = HAL_OK;
  591. if (pCallback == NULL)
  592. {
  593. return HAL_ERROR;
  594. }
  595. /* Process locked */
  596. __HAL_LOCK(hpccard);
  597. if (hpccard->State == HAL_PCCARD_STATE_READY)
  598. {
  599. switch (CallbackId)
  600. {
  601. case HAL_PCCARD_MSP_INIT_CB_ID :
  602. hpccard->MspInitCallback = pCallback;
  603. break;
  604. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  605. hpccard->MspDeInitCallback = pCallback;
  606. break;
  607. case HAL_PCCARD_IT_CB_ID :
  608. hpccard->ItCallback = pCallback;
  609. break;
  610. default :
  611. /* update return status */
  612. status = HAL_ERROR;
  613. break;
  614. }
  615. }
  616. else if (hpccard->State == HAL_PCCARD_STATE_RESET)
  617. {
  618. switch (CallbackId)
  619. {
  620. case HAL_PCCARD_MSP_INIT_CB_ID :
  621. hpccard->MspInitCallback = pCallback;
  622. break;
  623. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  624. hpccard->MspDeInitCallback = pCallback;
  625. break;
  626. default :
  627. /* update return status */
  628. status = HAL_ERROR;
  629. break;
  630. }
  631. }
  632. else
  633. {
  634. /* update return status */
  635. status = HAL_ERROR;
  636. }
  637. /* Release Lock */
  638. __HAL_UNLOCK(hpccard);
  639. return status;
  640. }
  641. /**
  642. * @brief Unregister a User PCCARD Callback
  643. * PCCARD Callback is redirected to the weak (surcharged) predefined callback
  644. * @param hpccard : PCCARD handle
  645. * @param CallbackId : ID of the callback to be unregistered
  646. * This parameter can be one of the following values:
  647. * @arg @ref HAL_PCCARD_MSP_INIT_CB_ID PCCARD MspInit callback ID
  648. * @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID PCCARD MspDeInit callback ID
  649. * @arg @ref HAL_PCCARD_IT_CB_ID PCCARD IT callback ID
  650. * @retval status
  651. */
  652. HAL_StatusTypeDef HAL_PCCARD_UnRegisterCallback(PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId)
  653. {
  654. HAL_StatusTypeDef status = HAL_OK;
  655. /* Process locked */
  656. __HAL_LOCK(hpccard);
  657. if (hpccard->State == HAL_PCCARD_STATE_READY)
  658. {
  659. switch (CallbackId)
  660. {
  661. case HAL_PCCARD_MSP_INIT_CB_ID :
  662. hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  663. break;
  664. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  665. hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  666. break;
  667. case HAL_PCCARD_IT_CB_ID :
  668. hpccard->ItCallback = HAL_PCCARD_ITCallback;
  669. break;
  670. default :
  671. /* update return status */
  672. status = HAL_ERROR;
  673. break;
  674. }
  675. }
  676. else if (hpccard->State == HAL_PCCARD_STATE_RESET)
  677. {
  678. switch (CallbackId)
  679. {
  680. case HAL_PCCARD_MSP_INIT_CB_ID :
  681. hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  682. break;
  683. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  684. hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  685. break;
  686. default :
  687. /* update return status */
  688. status = HAL_ERROR;
  689. break;
  690. }
  691. }
  692. else
  693. {
  694. /* update return status */
  695. status = HAL_ERROR;
  696. }
  697. /* Release Lock */
  698. __HAL_UNLOCK(hpccard);
  699. return status;
  700. }
  701. #endif /* USE_HAL_PCCARD_REGISTER_CALLBACKS */
  702. /**
  703. * @}
  704. */
  705. /** @defgroup PCCARD_Exported_Functions_Group3 State functions
  706. * @brief Peripheral State functions
  707. *
  708. @verbatim
  709. ==============================================================================
  710. ##### PCCARD State functions #####
  711. ==============================================================================
  712. [..]
  713. This subsection permits to get in run-time the status of the PCCARD controller
  714. and the data flow.
  715. @endverbatim
  716. * @{
  717. */
  718. /**
  719. * @brief return the PCCARD controller state
  720. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  721. * the configuration information for PCCARD module.
  722. * @retval HAL state
  723. */
  724. HAL_PCCARD_StateTypeDef HAL_PCCARD_GetState(PCCARD_HandleTypeDef *hpccard)
  725. {
  726. return hpccard->State;
  727. }
  728. /**
  729. * @brief Get the compact flash memory status
  730. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  731. * the configuration information for PCCARD module.
  732. * @retval New status of the PCCARD operation. This parameter can be:
  733. * - CompactFlash_TIMEOUT_ERROR: when the previous operation generate
  734. * a Timeout error
  735. * - CompactFlash_READY: when memory is ready for the next operation
  736. */
  737. HAL_PCCARD_StatusTypeDef HAL_PCCARD_GetStatus(PCCARD_HandleTypeDef *hpccard)
  738. {
  739. uint32_t timeout = PCCARD_TIMEOUT_STATUS;
  740. uint32_t status_pccard = 0U;
  741. /* Check the PCCARD controller state */
  742. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  743. {
  744. return HAL_PCCARD_STATUS_ONGOING;
  745. }
  746. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  747. while ((status_pccard == PCCARD_BUSY) && timeout)
  748. {
  749. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  750. timeout--;
  751. }
  752. if (timeout == 0U)
  753. {
  754. status_pccard = PCCARD_TIMEOUT_ERROR;
  755. }
  756. /* Return the operation status */
  757. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  758. }
  759. /**
  760. * @brief Reads the Compact Flash memory status using the Read status command
  761. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  762. * the configuration information for PCCARD module.
  763. * @retval The status of the Compact Flash memory. This parameter can be:
  764. * - CompactFlash_BUSY: when memory is busy
  765. * - CompactFlash_READY: when memory is ready for the next operation
  766. * - CompactFlash_ERROR: when the previous operation generates error
  767. */
  768. HAL_PCCARD_StatusTypeDef HAL_PCCARD_ReadStatus(PCCARD_HandleTypeDef *hpccard)
  769. {
  770. uint8_t data = 0U;
  771. uint8_t status_pccard = PCCARD_BUSY;
  772. /* Check the PCCARD controller state */
  773. if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  774. {
  775. return HAL_PCCARD_STATUS_ONGOING;
  776. }
  777. /* Read status operation */
  778. data = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  779. if ((data & PCCARD_TIMEOUT_ERROR) == PCCARD_TIMEOUT_ERROR)
  780. {
  781. status_pccard = PCCARD_TIMEOUT_ERROR;
  782. }
  783. else if ((data & PCCARD_READY) == PCCARD_READY)
  784. {
  785. status_pccard = PCCARD_READY;
  786. }
  787. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  788. }
  789. /**
  790. * @}
  791. */
  792. /**
  793. * @}
  794. */
  795. /**
  796. * @}
  797. */
  798. #endif /* HAL_PCCARD_MODULE_ENABLED */
  799. /**
  800. * @}
  801. */
  802. #endif /* FSMC_BANK4 */