stm32f1xx_hal_pccard.c 29 KB

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