stm32f4xx_hal_pccard.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pccard.c
  4. * @author MCD Application Team
  5. * @brief PCCARD HAL module driver.
  6. * This file provides a generic firmware to drive PCCARD memories mounted
  7. * as external device.
  8. *
  9. @verbatim
  10. ===============================================================================
  11. ##### How to use this driver #####
  12. ===============================================================================
  13. [..]
  14. This driver is a generic layered driver which contains a set of APIs used to
  15. control PCCARD/compact flash memories. It uses the FMC/FSMC layer functions
  16. to interface with PCCARD devices. This driver is used for:
  17. (+) PCCARD/Compact Flash memory configuration sequence using the function
  18. HAL_PCCARD_Init()/HAL_CF_Init() with control and timing parameters for
  19. both common and attribute spaces.
  20. (+) Read PCCARD/Compact Flash memory maker and device IDs using the function
  21. HAL_PCCARD_Read_ID()/HAL_CF_Read_ID(). The read information is stored in
  22. the CompactFlash_ID structure declared by the function caller.
  23. (+) Access PCCARD/Compact Flash memory by read/write operations using the functions
  24. HAL_PCCARD_Read_Sector()/ HAL_PCCARD_Write_Sector() -
  25. HAL_CF_Read_Sector()/HAL_CF_Write_Sector(), to read/write sector.
  26. (+) Perform PCCARD/Compact Flash Reset chip operation using the function
  27. HAL_PCCARD_Reset()/HAL_CF_Reset.
  28. (+) Perform PCCARD/Compact Flash erase sector operation using the function
  29. HAL_PCCARD_Erase_Sector()/HAL_CF_Erase_Sector.
  30. (+) Read the PCCARD/Compact Flash status operation using the function
  31. HAL_PCCARD_ReadStatus()/HAL_CF_ReadStatus().
  32. (+) You can monitor the PCCARD/Compact Flash device HAL state by calling
  33. the function HAL_PCCARD_GetState()/HAL_CF_GetState()
  34. [..]
  35. (@) This driver is a set of generic APIs which handle standard PCCARD/compact flash
  36. operations. If a PCCARD/Compact Flash device contains different operations
  37. and/or implementations, it should be implemented separately.
  38. *** 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) 2017 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 "stm32f4xx_hal.h"
  87. /** @addtogroup STM32F4xx_HAL_Driver
  88. * @{
  89. */
  90. #ifdef HAL_PCCARD_MODULE_ENABLED
  91. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  92. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  93. /** @defgroup PCCARD PCCARD
  94. * @brief PCCARD HAL module driver
  95. * @{
  96. */
  97. /* Private typedef -----------------------------------------------------------*/
  98. /* Private define ------------------------------------------------------------*/
  99. /** @defgroup PCCARD_Private_Defines PCCARD Private Defines
  100. * @{
  101. */
  102. #define PCCARD_TIMEOUT_READ_ID 0x0000FFFFU
  103. #define PCCARD_TIMEOUT_READ_WRITE_SECTOR 0x0000FFFFU
  104. #define PCCARD_TIMEOUT_ERASE_SECTOR 0x00000400U
  105. #define PCCARD_TIMEOUT_STATUS 0x01000000U
  106. #define PCCARD_STATUS_OK (uint8_t)0x58
  107. #define PCCARD_STATUS_WRITE_OK (uint8_t)0x50
  108. /**
  109. * @}
  110. */
  111. /* Private macro -------------------------------------------------------------*/
  112. /* Private variables ---------------------------------------------------------*/
  113. /* Private function ----------------------------------------------------------*/
  114. /* Exported functions --------------------------------------------------------*/
  115. /** @defgroup PCCARD_Exported_Functions PCCARD Exported Functions
  116. * @{
  117. */
  118. /** @defgroup PCCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  119. * @brief Initialization and Configuration functions
  120. *
  121. @verbatim
  122. ==============================================================================
  123. ##### PCCARD Initialization and de-initialization functions #####
  124. ==============================================================================
  125. [..]
  126. This section provides functions allowing to initialize/de-initialize
  127. the PCCARD memory
  128. @endverbatim
  129. * @{
  130. */
  131. /**
  132. * @brief Perform the PCCARD memory Initialization sequence
  133. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  134. * the configuration information for PCCARD module.
  135. * @param ComSpaceTiming Common space timing structure
  136. * @param AttSpaceTiming Attribute space timing structure
  137. * @param IOSpaceTiming IO space timing structure
  138. * @retval HAL status
  139. */
  140. HAL_StatusTypeDef HAL_PCCARD_Init(PCCARD_HandleTypeDef *hpccard, FMC_NAND_PCC_TimingTypeDef *ComSpaceTiming, FMC_NAND_PCC_TimingTypeDef *AttSpaceTiming, FMC_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. FMC_PCCARD_Init(hpccard->Instance, &(hpccard->Init));
  168. /* Init PCCARD common space timing Interface */
  169. FMC_PCCARD_CommonSpace_Timing_Init(hpccard->Instance, ComSpaceTiming);
  170. /* Init PCCARD attribute space timing Interface */
  171. FMC_PCCARD_AttributeSpace_Timing_Init(hpccard->Instance, AttSpaceTiming);
  172. /* Init PCCARD IO space timing Interface */
  173. FMC_PCCARD_IOSpace_Timing_Init(hpccard->Instance, IOSpaceTiming);
  174. /* Enable the PCCARD device */
  175. __FMC_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. FMC_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) = 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, 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)0x00;
  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. }while((status == 0x80) && timeout);
  335. if(timeout == 0U)
  336. {
  337. *pStatus = PCCARD_TIMEOUT_ERROR;
  338. }
  339. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  340. do
  341. {
  342. /* wait till the Status = PCCARD_STATUS_OK */
  343. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  344. timeout--;
  345. }while((status != PCCARD_STATUS_OK) && timeout);
  346. if(timeout == 0U)
  347. {
  348. *pStatus = PCCARD_TIMEOUT_ERROR;
  349. }
  350. /* Read bytes */
  351. for(; index < PCCARD_SECTOR_SIZE; index++)
  352. {
  353. *(uint16_t *)pBuffer++ = *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR);
  354. }
  355. /* Update the PCCARD controller state */
  356. hpccard->State = HAL_PCCARD_STATE_READY;
  357. /* Process unlocked */
  358. __HAL_UNLOCK(hpccard);
  359. return HAL_OK;
  360. }
  361. /**
  362. * @brief Write sector to PCCARD memory
  363. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  364. * the configuration information for PCCARD module.
  365. * @param pBuffer pointer to source write buffer
  366. * @param SectorAddress Sector address to write
  367. * @param pStatus pointer to PCCARD status
  368. * @retval HAL status
  369. */
  370. HAL_StatusTypeDef HAL_PCCARD_Write_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  371. {
  372. uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0U;
  373. uint8_t status = 0;
  374. /* Process Locked */
  375. __HAL_LOCK(hpccard);
  376. /* Check the PCCARD controller state */
  377. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  378. {
  379. return HAL_BUSY;
  380. }
  381. /* Update the PCCARD controller state */
  382. hpccard->State = HAL_PCCARD_STATE_BUSY;
  383. /* Initialize PCCARD status */
  384. *pStatus = PCCARD_READY;
  385. /* Set the parameters to write a sector */
  386. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x00;
  387. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  388. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0x30A0;
  389. do
  390. {
  391. /* Wait till the Status = PCCARD_STATUS_OK */
  392. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  393. timeout--;
  394. }while((status != PCCARD_STATUS_OK) && timeout);
  395. if(timeout == 0U)
  396. {
  397. *pStatus = PCCARD_TIMEOUT_ERROR;
  398. }
  399. /* Write bytes */
  400. for(; index < PCCARD_SECTOR_SIZE; index++)
  401. {
  402. *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR) = *(uint16_t *)pBuffer++;
  403. }
  404. do
  405. {
  406. /* Wait till the Status = PCCARD_STATUS_WRITE_OK */
  407. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  408. timeout--;
  409. }while((status != PCCARD_STATUS_WRITE_OK) && timeout);
  410. if(timeout == 0U)
  411. {
  412. *pStatus = PCCARD_TIMEOUT_ERROR;
  413. }
  414. /* Update the PCCARD controller state */
  415. hpccard->State = HAL_PCCARD_STATE_READY;
  416. /* Process unlocked */
  417. __HAL_UNLOCK(hpccard);
  418. return HAL_OK;
  419. }
  420. /**
  421. * @brief Erase sector from PCCARD memory
  422. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  423. * the configuration information for PCCARD module.
  424. * @param SectorAddress Sector address to erase
  425. * @param pStatus pointer to PCCARD status
  426. * @retval HAL status
  427. */
  428. HAL_StatusTypeDef HAL_PCCARD_Erase_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t SectorAddress, uint8_t *pStatus)
  429. {
  430. uint32_t timeout = PCCARD_TIMEOUT_ERASE_SECTOR;
  431. uint8_t status = 0;
  432. /* Process Locked */
  433. __HAL_LOCK(hpccard);
  434. /* Check the PCCARD controller state */
  435. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  436. {
  437. return HAL_BUSY;
  438. }
  439. /* Update the PCCARD controller state */
  440. hpccard->State = HAL_PCCARD_STATE_BUSY;
  441. /* Initialize PCCARD status */
  442. *pStatus = PCCARD_READY;
  443. /* Set the parameters to write a sector */
  444. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_LOW) = 0x00;
  445. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = 0x00;
  446. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_NUMBER) = SectorAddress;
  447. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = 0x01;
  448. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CARD_HEAD) = 0xA0;
  449. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = ATA_ERASE_SECTOR_CMD;
  450. /* wait till the PCCARD is ready */
  451. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  452. while((status != PCCARD_STATUS_WRITE_OK) && timeout)
  453. {
  454. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  455. timeout--;
  456. }
  457. if(timeout == 0U)
  458. {
  459. *pStatus = PCCARD_TIMEOUT_ERROR;
  460. }
  461. /* Check the PCCARD controller state */
  462. hpccard->State = HAL_PCCARD_STATE_READY;
  463. /* Process unlocked */
  464. __HAL_UNLOCK(hpccard);
  465. return HAL_OK;
  466. }
  467. /**
  468. * @brief Reset the PCCARD memory
  469. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  470. * the configuration information for PCCARD module.
  471. * @retval HAL status
  472. */
  473. HAL_StatusTypeDef HAL_PCCARD_Reset(PCCARD_HandleTypeDef *hpccard)
  474. {
  475. /* Process Locked */
  476. __HAL_LOCK(hpccard);
  477. /* Check the PCCARD controller state */
  478. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  479. {
  480. return HAL_BUSY;
  481. }
  482. /* Provide a SW reset and Read and verify the:
  483. - PCCard Configuration Option Register at address 0x98000200 --> 0x80
  484. - Card Configuration and Status Register at address 0x98000202 --> 0x00
  485. - Pin Replacement Register at address 0x98000204 --> 0x0C
  486. - Socket and Copy Register at address 0x98000206 --> 0x00
  487. */
  488. /* Check the PCCARD controller state */
  489. hpccard->State = HAL_PCCARD_STATE_BUSY;
  490. *(__IO uint8_t *)(PCCARD_ATTRIBUTE_SPACE_ADDRESS | ATA_CARD_CONFIGURATION ) = 0x01;
  491. /* Check the PCCARD controller state */
  492. hpccard->State = HAL_PCCARD_STATE_READY;
  493. /* Process unlocked */
  494. __HAL_UNLOCK(hpccard);
  495. return HAL_OK;
  496. }
  497. /**
  498. * @brief This function handles PCCARD device interrupt request.
  499. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  500. * the configuration information for PCCARD module.
  501. * @retval HAL status
  502. */
  503. void HAL_PCCARD_IRQHandler(PCCARD_HandleTypeDef *hpccard)
  504. {
  505. /* Check PCCARD interrupt Rising edge flag */
  506. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE))
  507. {
  508. /* PCCARD interrupt callback*/
  509. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  510. hpccard->ItCallback(hpccard);
  511. #else
  512. HAL_PCCARD_ITCallback(hpccard);
  513. #endif
  514. /* Clear PCCARD interrupt Rising edge pending bit */
  515. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE);
  516. }
  517. /* Check PCCARD interrupt Level flag */
  518. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_LEVEL))
  519. {
  520. /* PCCARD interrupt callback*/
  521. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  522. hpccard->ItCallback(hpccard);
  523. #else
  524. HAL_PCCARD_ITCallback(hpccard);
  525. #endif
  526. /* Clear PCCARD interrupt Level pending bit */
  527. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_LEVEL);
  528. }
  529. /* Check PCCARD interrupt Falling edge flag */
  530. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE))
  531. {
  532. /* PCCARD interrupt callback*/
  533. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  534. hpccard->ItCallback(hpccard);
  535. #else
  536. HAL_PCCARD_ITCallback(hpccard);
  537. #endif
  538. /* Clear PCCARD interrupt Falling edge pending bit */
  539. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE);
  540. }
  541. /* Check PCCARD interrupt FIFO empty flag */
  542. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FEMPT))
  543. {
  544. /* PCCARD interrupt callback*/
  545. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  546. hpccard->ItCallback(hpccard);
  547. #else
  548. HAL_PCCARD_ITCallback(hpccard);
  549. #endif
  550. /* Clear PCCARD interrupt FIFO empty pending bit */
  551. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FEMPT);
  552. }
  553. }
  554. /**
  555. * @brief PCCARD interrupt feature callback
  556. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  557. * the configuration information for PCCARD module.
  558. * @retval None
  559. */
  560. __weak void HAL_PCCARD_ITCallback(PCCARD_HandleTypeDef *hpccard)
  561. {
  562. /* Prevent unused argument(s) compilation warning */
  563. UNUSED(hpccard);
  564. /* NOTE : This function Should not be modified, when the callback is needed,
  565. the HAL_PCCARD_ITCallback could be implemented in the user file
  566. */
  567. }
  568. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  569. /**
  570. * @brief Register a User PCCARD Callback
  571. * To be used instead of the weak (surcharged) predefined callback
  572. * @param hpccard : PCCARD handle
  573. * @param CallbackId : ID of the callback to be registered
  574. * This parameter can be one of the following values:
  575. * @arg @ref HAL_PCCARD_MSP_INIT_CB_ID PCCARD MspInit callback ID
  576. * @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID PCCARD MspDeInit callback ID
  577. * @arg @ref HAL_PCCARD_IT_CB_ID PCCARD IT callback ID
  578. * @param pCallback : pointer to the Callback function
  579. * @retval status
  580. */
  581. HAL_StatusTypeDef HAL_PCCARD_RegisterCallback (PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId, pPCCARD_CallbackTypeDef pCallback)
  582. {
  583. HAL_StatusTypeDef status = HAL_OK;
  584. if(pCallback == NULL)
  585. {
  586. return HAL_ERROR;
  587. }
  588. /* Process locked */
  589. __HAL_LOCK(hpccard);
  590. if(hpccard->State == HAL_PCCARD_STATE_READY)
  591. {
  592. switch (CallbackId)
  593. {
  594. case HAL_PCCARD_MSP_INIT_CB_ID :
  595. hpccard->MspInitCallback = pCallback;
  596. break;
  597. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  598. hpccard->MspDeInitCallback = pCallback;
  599. break;
  600. case HAL_PCCARD_IT_CB_ID :
  601. hpccard->ItCallback = pCallback;
  602. break;
  603. default :
  604. /* update return status */
  605. status = HAL_ERROR;
  606. break;
  607. }
  608. }
  609. else if(hpccard->State == HAL_PCCARD_STATE_RESET)
  610. {
  611. switch (CallbackId)
  612. {
  613. case HAL_PCCARD_MSP_INIT_CB_ID :
  614. hpccard->MspInitCallback = pCallback;
  615. break;
  616. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  617. hpccard->MspDeInitCallback = pCallback;
  618. break;
  619. default :
  620. /* update return status */
  621. status = HAL_ERROR;
  622. break;
  623. }
  624. }
  625. else
  626. {
  627. /* update return status */
  628. status = HAL_ERROR;
  629. }
  630. /* Release Lock */
  631. __HAL_UNLOCK(hpccard);
  632. return status;
  633. }
  634. /**
  635. * @brief Unregister a User PCCARD Callback
  636. * PCCARD Callback is redirected to the weak (surcharged) predefined callback
  637. * @param hpccard : PCCARD handle
  638. * @param CallbackId : ID of the callback to be unregistered
  639. * This parameter can be one of the following values:
  640. * @arg @ref HAL_PCCARD_MSP_INIT_CB_ID PCCARD MspInit callback ID
  641. * @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID PCCARD MspDeInit callback ID
  642. * @arg @ref HAL_PCCARD_IT_CB_ID PCCARD IT callback ID
  643. * @retval status
  644. */
  645. HAL_StatusTypeDef HAL_PCCARD_UnRegisterCallback (PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId)
  646. {
  647. HAL_StatusTypeDef status = HAL_OK;
  648. /* Process locked */
  649. __HAL_LOCK(hpccard);
  650. if(hpccard->State == HAL_PCCARD_STATE_READY)
  651. {
  652. switch (CallbackId)
  653. {
  654. case HAL_PCCARD_MSP_INIT_CB_ID :
  655. hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  656. break;
  657. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  658. hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  659. break;
  660. case HAL_PCCARD_IT_CB_ID :
  661. hpccard->ItCallback = HAL_PCCARD_ITCallback;
  662. break;
  663. default :
  664. /* update return status */
  665. status = HAL_ERROR;
  666. break;
  667. }
  668. }
  669. else if(hpccard->State == HAL_PCCARD_STATE_RESET)
  670. {
  671. switch (CallbackId)
  672. {
  673. case HAL_PCCARD_MSP_INIT_CB_ID :
  674. hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  675. break;
  676. case HAL_PCCARD_MSP_DEINIT_CB_ID :
  677. hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  678. break;
  679. default :
  680. /* update return status */
  681. status = HAL_ERROR;
  682. break;
  683. }
  684. }
  685. else
  686. {
  687. /* update return status */
  688. status = HAL_ERROR;
  689. }
  690. /* Release Lock */
  691. __HAL_UNLOCK(hpccard);
  692. return status;
  693. }
  694. #endif
  695. /**
  696. * @}
  697. */
  698. /** @defgroup PCCARD_Exported_Functions_Group3 State functions
  699. * @brief Peripheral State functions
  700. *
  701. @verbatim
  702. ==============================================================================
  703. ##### PCCARD State functions #####
  704. ==============================================================================
  705. [..]
  706. This subsection permits to get in run-time the status of the PCCARD controller
  707. and the data flow.
  708. @endverbatim
  709. * @{
  710. */
  711. /**
  712. * @brief return the PCCARD controller state
  713. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  714. * the configuration information for PCCARD module.
  715. * @retval HAL state
  716. */
  717. HAL_PCCARD_StateTypeDef HAL_PCCARD_GetState(PCCARD_HandleTypeDef *hpccard)
  718. {
  719. return hpccard->State;
  720. }
  721. /**
  722. * @brief Get the compact flash memory status
  723. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  724. * the configuration information for PCCARD module.
  725. * @retval New status of the PCCARD operation. This parameter can be:
  726. * - CompactFlash_TIMEOUT_ERROR: when the previous operation generate
  727. * a Timeout error
  728. * - CompactFlash_READY: when memory is ready for the next operation
  729. */
  730. HAL_PCCARD_StatusTypeDef HAL_PCCARD_GetStatus(PCCARD_HandleTypeDef *hpccard)
  731. {
  732. uint32_t timeout = PCCARD_TIMEOUT_STATUS, status_pccard = 0U;
  733. /* Check the PCCARD controller state */
  734. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  735. {
  736. return HAL_PCCARD_STATUS_ONGOING;
  737. }
  738. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  739. while((status_pccard == PCCARD_BUSY) && timeout)
  740. {
  741. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  742. timeout--;
  743. }
  744. if(timeout == 0U)
  745. {
  746. status_pccard = PCCARD_TIMEOUT_ERROR;
  747. }
  748. /* Return the operation status */
  749. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  750. }
  751. /**
  752. * @brief Reads the Compact Flash memory status using the Read status command
  753. * @param hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  754. * the configuration information for PCCARD module.
  755. * @retval The status of the Compact Flash memory. This parameter can be:
  756. * - CompactFlash_BUSY: when memory is busy
  757. * - CompactFlash_READY: when memory is ready for the next operation
  758. * - CompactFlash_ERROR: when the previous operation generates error
  759. */
  760. HAL_PCCARD_StatusTypeDef HAL_PCCARD_ReadStatus(PCCARD_HandleTypeDef *hpccard)
  761. {
  762. uint8_t data = 0U, status_pccard = PCCARD_BUSY;
  763. /* Check the PCCARD controller state */
  764. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  765. {
  766. return HAL_PCCARD_STATUS_ONGOING;
  767. }
  768. /* Read status operation */
  769. data = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  770. if((data & PCCARD_TIMEOUT_ERROR) == PCCARD_TIMEOUT_ERROR)
  771. {
  772. status_pccard = PCCARD_TIMEOUT_ERROR;
  773. }
  774. else if((data & PCCARD_READY) == PCCARD_READY)
  775. {
  776. status_pccard = PCCARD_READY;
  777. }
  778. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  779. }
  780. /**
  781. * @}
  782. */
  783. /**
  784. * @}
  785. */
  786. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  787. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  788. #endif /* HAL_PCCARD_MODULE_ENABLED */
  789. /**
  790. * @}
  791. */
  792. /**
  793. * @}
  794. */
  795. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/