stm32f1xx_hal_pccard.c 29 KB

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