stm32f4xx_hal_pccard.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pccard.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief PCCARD HAL module driver.
  8. * This file provides a generic firmware to drive PCCARD memories mounted
  9. * as external device.
  10. *
  11. @verbatim
  12. ===============================================================================
  13. ##### How to use this driver #####
  14. ===============================================================================
  15. [..]
  16. This driver is a generic layered driver which contains a set of APIs used to
  17. control PCCARD/compact flash memories. It uses the FMC/FSMC layer functions
  18. to interface with PCCARD devices. This driver is used for:
  19. (+) PCCARD/Compact Flash memory configuration sequence using the function
  20. HAL_PCCARD_Init()/HAL_CF_Init() with control and timing parameters for
  21. both common and attribute spaces.
  22. (+) Read PCCARD/Compact Flash memory maker and device IDs using the function
  23. HAL_PCCARD_Read_ID()/HAL_CF_Read_ID(). The read information is stored in
  24. the CompactFlash_ID structure declared by the function caller.
  25. (+) Access PCCARD/Compact Flash memory by read/write operations using the functions
  26. HAL_PCCARD_Read_Sector()/ HAL_PCCARD_Write_Sector() -
  27. HAL_CF_Read_Sector()/HAL_CF_Write_Sector(), to read/write sector.
  28. (+) Perform PCCARD/Compact Flash Reset chip operation using the function
  29. HAL_PCCARD_Reset()/HAL_CF_Reset.
  30. (+) Perform PCCARD/Compact Flash erase sector operation using the function
  31. HAL_PCCARD_Erase_Sector()/HAL_CF_Erase_Sector.
  32. (+) Read the PCCARD/Compact Flash status operation using the function
  33. HAL_PCCARD_ReadStatus()/HAL_CF_ReadStatus().
  34. (+) You can monitor the PCCARD/Compact Flash device HAL state by calling
  35. the function HAL_PCCARD_GetState()/HAL_CF_GetState()
  36. [..]
  37. (@) This driver is a set of generic APIs which handle standard PCCARD/compact flash
  38. operations. If a PCCARD/Compact Flash device contains different operations
  39. and/or implementations, it should be implemented separately.
  40. @endverbatim
  41. ******************************************************************************
  42. * @attention
  43. *
  44. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  45. *
  46. * Redistribution and use in source and binary forms, with or without modification,
  47. * are permitted provided that the following conditions are met:
  48. * 1. Redistributions of source code must retain the above copyright notice,
  49. * this list of conditions and the following disclaimer.
  50. * 2. Redistributions in binary form must reproduce the above copyright notice,
  51. * this list of conditions and the following disclaimer in the documentation
  52. * and/or other materials provided with the distribution.
  53. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  54. * may be used to endorse or promote products derived from this software
  55. * without specific prior written permission.
  56. *
  57. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  58. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  59. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  60. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  61. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  62. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  63. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  64. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  65. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  66. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  67. *
  68. ******************************************************************************
  69. */
  70. /* Includes ------------------------------------------------------------------*/
  71. #include "stm32f4xx_hal.h"
  72. /** @addtogroup STM32F4xx_HAL_Driver
  73. * @{
  74. */
  75. #ifdef HAL_PCCARD_MODULE_ENABLED
  76. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  77. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  78. /** @defgroup PCCARD PCCARD
  79. * @brief PCCARD HAL module driver
  80. * @{
  81. */
  82. /* Private typedef -----------------------------------------------------------*/
  83. /* Private define ------------------------------------------------------------*/
  84. /** @defgroup PCCARD_Private_Defines PCCARD Private Defines
  85. * @{
  86. */
  87. #define PCCARD_TIMEOUT_READ_ID (uint32_t)0x0000FFFF
  88. #define PCCARD_TIMEOUT_READ_WRITE_SECTOR (uint32_t)0x0000FFFF
  89. #define PCCARD_TIMEOUT_ERASE_SECTOR (uint32_t)0x00000400
  90. #define PCCARD_TIMEOUT_STATUS (uint32_t)0x01000000
  91. #define PCCARD_STATUS_OK (uint8_t)0x58
  92. #define PCCARD_STATUS_WRITE_OK (uint8_t)0x50
  93. /**
  94. * @}
  95. */
  96. /* Private macro -------------------------------------------------------------*/
  97. /* Private variables ---------------------------------------------------------*/
  98. /* Private function ----------------------------------------------------------*/
  99. /* Exported functions --------------------------------------------------------*/
  100. /** @defgroup PCCARD_Exported_Functions PCCARD Exported Functions
  101. * @{
  102. */
  103. /** @defgroup PCCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  104. * @brief Initialization and Configuration functions
  105. *
  106. @verbatim
  107. ==============================================================================
  108. ##### PCCARD Initialization and de-initialization functions #####
  109. ==============================================================================
  110. [..]
  111. This section provides functions allowing to initialize/de-initialize
  112. the PCCARD memory
  113. @endverbatim
  114. * @{
  115. */
  116. /**
  117. * @brief Perform the PCCARD memory Initialization sequence
  118. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  119. * the configuration information for PCCARD module.
  120. * @param ComSpaceTiming: Common space timing structure
  121. * @param AttSpaceTiming: Attribute space timing structure
  122. * @param IOSpaceTiming: IO space timing structure
  123. * @retval HAL status
  124. */
  125. HAL_StatusTypeDef HAL_PCCARD_Init(PCCARD_HandleTypeDef *hpccard, FMC_NAND_PCC_TimingTypeDef *ComSpaceTiming, FMC_NAND_PCC_TimingTypeDef *AttSpaceTiming, FMC_NAND_PCC_TimingTypeDef *IOSpaceTiming)
  126. {
  127. /* Check the PCCARD controller state */
  128. if(hpccard == NULL)
  129. {
  130. return HAL_ERROR;
  131. }
  132. if(hpccard->State == HAL_PCCARD_STATE_RESET)
  133. {
  134. /* Allocate lock resource and initialize it */
  135. hpccard->Lock = HAL_UNLOCKED;
  136. /* Initialize the low level hardware (MSP) */
  137. HAL_PCCARD_MspInit(hpccard);
  138. }
  139. /* Initialize the PCCARD state */
  140. hpccard->State = HAL_PCCARD_STATE_BUSY;
  141. /* Initialize PCCARD control Interface */
  142. FMC_PCCARD_Init(hpccard->Instance, &(hpccard->Init));
  143. /* Init PCCARD common space timing Interface */
  144. FMC_PCCARD_CommonSpace_Timing_Init(hpccard->Instance, ComSpaceTiming);
  145. /* Init PCCARD attribute space timing Interface */
  146. FMC_PCCARD_AttributeSpace_Timing_Init(hpccard->Instance, AttSpaceTiming);
  147. /* Init PCCARD IO space timing Interface */
  148. FMC_PCCARD_IOSpace_Timing_Init(hpccard->Instance, IOSpaceTiming);
  149. /* Enable the PCCARD device */
  150. __FMC_PCCARD_ENABLE(hpccard->Instance);
  151. /* Update the PCCARD state */
  152. hpccard->State = HAL_PCCARD_STATE_READY;
  153. return HAL_OK;
  154. }
  155. /**
  156. * @brief Perform the PCCARD memory De-initialization sequence
  157. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  158. * the configuration information for PCCARD module.
  159. * @retval HAL status
  160. */
  161. HAL_StatusTypeDef HAL_PCCARD_DeInit(PCCARD_HandleTypeDef *hpccard)
  162. {
  163. /* De-Initialize the low level hardware (MSP) */
  164. HAL_PCCARD_MspDeInit(hpccard);
  165. /* Configure the PCCARD registers with their reset values */
  166. FMC_PCCARD_DeInit(hpccard->Instance);
  167. /* Update the PCCARD controller state */
  168. hpccard->State = HAL_PCCARD_STATE_RESET;
  169. /* Release Lock */
  170. __HAL_UNLOCK(hpccard);
  171. return HAL_OK;
  172. }
  173. /**
  174. * @brief PCCARD MSP Init
  175. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  176. * the configuration information for PCCARD module.
  177. * @retval None
  178. */
  179. __weak void HAL_PCCARD_MspInit(PCCARD_HandleTypeDef *hpccard)
  180. {
  181. /* Prevent unused argument(s) compilation warning */
  182. UNUSED(hpccard);
  183. /* NOTE : This function Should not be modified, when the callback is needed,
  184. the HAL_PCCARD_MspInit could be implemented in the user file
  185. */
  186. }
  187. /**
  188. * @brief PCCARD MSP DeInit
  189. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  190. * the configuration information for PCCARD module.
  191. * @retval None
  192. */
  193. __weak void HAL_PCCARD_MspDeInit(PCCARD_HandleTypeDef *hpccard)
  194. {
  195. /* Prevent unused argument(s) compilation warning */
  196. UNUSED(hpccard);
  197. /* NOTE : This function Should not be modified, when the callback is needed,
  198. the HAL_PCCARD_MspDeInit could be implemented in the user file
  199. */
  200. }
  201. /**
  202. * @}
  203. */
  204. /** @defgroup PCCARD_Exported_Functions_Group2 Input and Output functions
  205. * @brief Input Output and memory control functions
  206. *
  207. @verbatim
  208. ==============================================================================
  209. ##### PCCARD Input and Output functions #####
  210. ==============================================================================
  211. [..]
  212. This section provides functions allowing to use and control the PCCARD memory
  213. @endverbatim
  214. * @{
  215. */
  216. /**
  217. * @brief Read Compact Flash's ID.
  218. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  219. * the configuration information for PCCARD module.
  220. * @param CompactFlash_ID: Compact flash ID structure.
  221. * @param pStatus: pointer to compact flash status
  222. * @retval HAL status
  223. *
  224. */
  225. HAL_StatusTypeDef HAL_PCCARD_Read_ID(PCCARD_HandleTypeDef *hpccard, uint8_t CompactFlash_ID[], uint8_t *pStatus)
  226. {
  227. uint32_t timeout = PCCARD_TIMEOUT_READ_ID, index = 0;
  228. uint8_t status = 0;
  229. /* Process Locked */
  230. __HAL_LOCK(hpccard);
  231. /* Check the PCCARD controller state */
  232. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  233. {
  234. return HAL_BUSY;
  235. }
  236. /* Update the PCCARD controller state */
  237. hpccard->State = HAL_PCCARD_STATE_BUSY;
  238. /* Initialize the PCCARD status */
  239. *pStatus = PCCARD_READY;
  240. /* Send the Identify Command */
  241. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = 0xECEC;
  242. /* Read PCCARD IDs and timeout treatment */
  243. do
  244. {
  245. /* Read the PCCARD status */
  246. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  247. timeout--;
  248. }while((status != PCCARD_STATUS_OK) && timeout);
  249. if(timeout == 0)
  250. {
  251. *pStatus = PCCARD_TIMEOUT_ERROR;
  252. }
  253. else
  254. {
  255. /* Read PCCARD ID bytes */
  256. for(index = 0; index < 16; index++)
  257. {
  258. CompactFlash_ID[index] = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_DATA);
  259. }
  260. }
  261. /* Update the PCCARD controller state */
  262. hpccard->State = HAL_PCCARD_STATE_READY;
  263. /* Process unlocked */
  264. __HAL_UNLOCK(hpccard);
  265. return HAL_OK;
  266. }
  267. /**
  268. * @brief Read sector from PCCARD memory
  269. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  270. * the configuration information for PCCARD module.
  271. * @param pBuffer: pointer to destination read buffer
  272. * @param SectorAddress: Sector address to read
  273. * @param pStatus: pointer to PCCARD status
  274. * @retval HAL status
  275. */
  276. HAL_StatusTypeDef HAL_PCCARD_Read_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  277. {
  278. uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0;
  279. uint8_t status = 0;
  280. /* Process Locked */
  281. __HAL_LOCK(hpccard);
  282. /* Check the PCCARD controller state */
  283. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  284. {
  285. return HAL_BUSY;
  286. }
  287. /* Update the PCCARD controller state */
  288. hpccard->State = HAL_PCCARD_STATE_BUSY;
  289. /* Initialize PCCARD status */
  290. *pStatus = PCCARD_READY;
  291. /* Set the parameters to write a sector */
  292. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x00;
  293. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100 ) | ((uint16_t)SectorAddress);
  294. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0xE4A0;
  295. do
  296. {
  297. /* wait till the Status = 0x80 */
  298. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  299. timeout--;
  300. }while((status == 0x80) && timeout);
  301. if(timeout == 0)
  302. {
  303. *pStatus = PCCARD_TIMEOUT_ERROR;
  304. }
  305. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  306. do
  307. {
  308. /* wait till the Status = PCCARD_STATUS_OK */
  309. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  310. timeout--;
  311. }while((status != PCCARD_STATUS_OK) && timeout);
  312. if(timeout == 0)
  313. {
  314. *pStatus = PCCARD_TIMEOUT_ERROR;
  315. }
  316. /* Read bytes */
  317. for(; index < PCCARD_SECTOR_SIZE; index++)
  318. {
  319. *(uint16_t *)pBuffer++ = *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR);
  320. }
  321. /* Update the PCCARD controller state */
  322. hpccard->State = HAL_PCCARD_STATE_READY;
  323. /* Process unlocked */
  324. __HAL_UNLOCK(hpccard);
  325. return HAL_OK;
  326. }
  327. /**
  328. * @brief Write sector to PCCARD memory
  329. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  330. * the configuration information for PCCARD module.
  331. * @param pBuffer: pointer to source write buffer
  332. * @param SectorAddress: Sector address to write
  333. * @param pStatus: pointer to PCCARD status
  334. * @retval HAL status
  335. */
  336. HAL_StatusTypeDef HAL_PCCARD_Write_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  337. {
  338. uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0;
  339. uint8_t status = 0;
  340. /* Process Locked */
  341. __HAL_LOCK(hpccard);
  342. /* Check the PCCARD controller state */
  343. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  344. {
  345. return HAL_BUSY;
  346. }
  347. /* Update the PCCARD controller state */
  348. hpccard->State = HAL_PCCARD_STATE_BUSY;
  349. /* Initialize PCCARD status */
  350. *pStatus = PCCARD_READY;
  351. /* Set the parameters to write a sector */
  352. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x00;
  353. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100 ) | ((uint16_t)SectorAddress);
  354. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0x30A0;
  355. do
  356. {
  357. /* Wait till the Status = PCCARD_STATUS_OK */
  358. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  359. timeout--;
  360. }while((status != PCCARD_STATUS_OK) && timeout);
  361. if(timeout == 0)
  362. {
  363. *pStatus = PCCARD_TIMEOUT_ERROR;
  364. }
  365. /* Write bytes */
  366. for(; index < PCCARD_SECTOR_SIZE; index++)
  367. {
  368. *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR) = *(uint16_t *)pBuffer++;
  369. }
  370. do
  371. {
  372. /* Wait till the Status = PCCARD_STATUS_WRITE_OK */
  373. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  374. timeout--;
  375. }while((status != PCCARD_STATUS_WRITE_OK) && timeout);
  376. if(timeout == 0)
  377. {
  378. *pStatus = PCCARD_TIMEOUT_ERROR;
  379. }
  380. /* Update the PCCARD controller state */
  381. hpccard->State = HAL_PCCARD_STATE_READY;
  382. /* Process unlocked */
  383. __HAL_UNLOCK(hpccard);
  384. return HAL_OK;
  385. }
  386. /**
  387. * @brief Erase sector from PCCARD memory
  388. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  389. * the configuration information for PCCARD module.
  390. * @param SectorAddress: Sector address to erase
  391. * @param pStatus: pointer to PCCARD status
  392. * @retval HAL status
  393. */
  394. HAL_StatusTypeDef HAL_PCCARD_Erase_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t SectorAddress, uint8_t *pStatus)
  395. {
  396. uint32_t timeout = PCCARD_TIMEOUT_ERASE_SECTOR;
  397. uint8_t status = 0;
  398. /* Process Locked */
  399. __HAL_LOCK(hpccard);
  400. /* Check the PCCARD controller state */
  401. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  402. {
  403. return HAL_BUSY;
  404. }
  405. /* Update the PCCARD controller state */
  406. hpccard->State = HAL_PCCARD_STATE_BUSY;
  407. /* Initialize PCCARD status */
  408. *pStatus = PCCARD_READY;
  409. /* Set the parameters to write a sector */
  410. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_LOW) = 0x00;
  411. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = 0x00;
  412. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_NUMBER) = SectorAddress;
  413. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = 0x01;
  414. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CARD_HEAD) = 0xA0;
  415. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = ATA_ERASE_SECTOR_CMD;
  416. /* wait till the PCCARD is ready */
  417. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  418. while((status != PCCARD_STATUS_WRITE_OK) && timeout)
  419. {
  420. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  421. timeout--;
  422. }
  423. if(timeout == 0)
  424. {
  425. *pStatus = PCCARD_TIMEOUT_ERROR;
  426. }
  427. /* Check the PCCARD controller state */
  428. hpccard->State = HAL_PCCARD_STATE_READY;
  429. /* Process unlocked */
  430. __HAL_UNLOCK(hpccard);
  431. return HAL_OK;
  432. }
  433. /**
  434. * @brief Reset the PCCARD memory
  435. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  436. * the configuration information for PCCARD module.
  437. * @retval HAL status
  438. */
  439. HAL_StatusTypeDef HAL_PCCARD_Reset(PCCARD_HandleTypeDef *hpccard)
  440. {
  441. /* Process Locked */
  442. __HAL_LOCK(hpccard);
  443. /* Check the PCCARD controller state */
  444. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  445. {
  446. return HAL_BUSY;
  447. }
  448. /* Provide a SW reset and Read and verify the:
  449. - PCCard Configuration Option Register at address 0x98000200 --> 0x80
  450. - Card Configuration and Status Register at address 0x98000202 --> 0x00
  451. - Pin Replacement Register at address 0x98000204 --> 0x0C
  452. - Socket and Copy Register at address 0x98000206 --> 0x00
  453. */
  454. /* Check the PCCARD controller state */
  455. hpccard->State = HAL_PCCARD_STATE_BUSY;
  456. *(__IO uint8_t *)(PCCARD_ATTRIBUTE_SPACE_ADDRESS | ATA_CARD_CONFIGURATION ) = 0x01;
  457. /* Check the PCCARD controller state */
  458. hpccard->State = HAL_PCCARD_STATE_READY;
  459. /* Process unlocked */
  460. __HAL_UNLOCK(hpccard);
  461. return HAL_OK;
  462. }
  463. /**
  464. * @brief This function handles PCCARD device interrupt request.
  465. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  466. * the configuration information for PCCARD module.
  467. * @retval HAL status
  468. */
  469. void HAL_PCCARD_IRQHandler(PCCARD_HandleTypeDef *hpccard)
  470. {
  471. /* Check PCCARD interrupt Rising edge flag */
  472. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE))
  473. {
  474. /* PCCARD interrupt callback*/
  475. HAL_PCCARD_ITCallback(hpccard);
  476. /* Clear PCCARD interrupt Rising edge pending bit */
  477. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE);
  478. }
  479. /* Check PCCARD interrupt Level flag */
  480. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_LEVEL))
  481. {
  482. /* PCCARD interrupt callback*/
  483. HAL_PCCARD_ITCallback(hpccard);
  484. /* Clear PCCARD interrupt Level pending bit */
  485. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_LEVEL);
  486. }
  487. /* Check PCCARD interrupt Falling edge flag */
  488. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE))
  489. {
  490. /* PCCARD interrupt callback*/
  491. HAL_PCCARD_ITCallback(hpccard);
  492. /* Clear PCCARD interrupt Falling edge pending bit */
  493. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE);
  494. }
  495. /* Check PCCARD interrupt FIFO empty flag */
  496. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FEMPT))
  497. {
  498. /* PCCARD interrupt callback*/
  499. HAL_PCCARD_ITCallback(hpccard);
  500. /* Clear PCCARD interrupt FIFO empty pending bit */
  501. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FEMPT);
  502. }
  503. }
  504. /**
  505. * @brief PCCARD interrupt feature callback
  506. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  507. * the configuration information for PCCARD module.
  508. * @retval None
  509. */
  510. __weak void HAL_PCCARD_ITCallback(PCCARD_HandleTypeDef *hpccard)
  511. {
  512. /* Prevent unused argument(s) compilation warning */
  513. UNUSED(hpccard);
  514. /* NOTE : This function Should not be modified, when the callback is needed,
  515. the HAL_PCCARD_ITCallback could be implemented in the user file
  516. */
  517. }
  518. /**
  519. * @}
  520. */
  521. /** @defgroup PCCARD_Exported_Functions_Group3 State functions
  522. * @brief Peripheral State functions
  523. *
  524. @verbatim
  525. ==============================================================================
  526. ##### PCCARD State functions #####
  527. ==============================================================================
  528. [..]
  529. This subsection permits to get in run-time the status of the PCCARD controller
  530. and the data flow.
  531. @endverbatim
  532. * @{
  533. */
  534. /**
  535. * @brief return the PCCARD controller state
  536. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  537. * the configuration information for PCCARD module.
  538. * @retval HAL state
  539. */
  540. HAL_PCCARD_StateTypeDef HAL_PCCARD_GetState(PCCARD_HandleTypeDef *hpccard)
  541. {
  542. return hpccard->State;
  543. }
  544. /**
  545. * @brief Get the compact flash memory status
  546. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  547. * the configuration information for PCCARD module.
  548. * @retval New status of the PCCARD operation. This parameter can be:
  549. * - CompactFlash_TIMEOUT_ERROR: when the previous operation generate
  550. * a Timeout error
  551. * - CompactFlash_READY: when memory is ready for the next operation
  552. */
  553. HAL_PCCARD_StatusTypeDef HAL_PCCARD_GetStatus(PCCARD_HandleTypeDef *hpccard)
  554. {
  555. uint32_t timeout = PCCARD_TIMEOUT_STATUS, status_pccard = 0;
  556. /* Check the PCCARD controller state */
  557. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  558. {
  559. return HAL_PCCARD_STATUS_ONGOING;
  560. }
  561. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  562. while((status_pccard == PCCARD_BUSY) && timeout)
  563. {
  564. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  565. timeout--;
  566. }
  567. if(timeout == 0)
  568. {
  569. status_pccard = PCCARD_TIMEOUT_ERROR;
  570. }
  571. /* Return the operation status */
  572. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  573. }
  574. /**
  575. * @brief Reads the Compact Flash memory status using the Read status command
  576. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  577. * the configuration information for PCCARD module.
  578. * @retval The status of the Compact Flash memory. This parameter can be:
  579. * - CompactFlash_BUSY: when memory is busy
  580. * - CompactFlash_READY: when memory is ready for the next operation
  581. * - CompactFlash_ERROR: when the previous operation generates error
  582. */
  583. HAL_PCCARD_StatusTypeDef HAL_PCCARD_ReadStatus(PCCARD_HandleTypeDef *hpccard)
  584. {
  585. uint8_t data = 0, status_pccard = PCCARD_BUSY;
  586. /* Check the PCCARD controller state */
  587. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  588. {
  589. return HAL_PCCARD_STATUS_ONGOING;
  590. }
  591. /* Read status operation */
  592. data = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  593. if((data & PCCARD_TIMEOUT_ERROR) == PCCARD_TIMEOUT_ERROR)
  594. {
  595. status_pccard = PCCARD_TIMEOUT_ERROR;
  596. }
  597. else if((data & PCCARD_READY) == PCCARD_READY)
  598. {
  599. status_pccard = PCCARD_READY;
  600. }
  601. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  602. }
  603. /**
  604. * @}
  605. */
  606. /**
  607. * @}
  608. */
  609. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  610. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  611. #endif /* HAL_PCCARD_MODULE_ENABLED */
  612. /**
  613. * @}
  614. */
  615. /**
  616. * @}
  617. */
  618. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/