stm32f4xx_hal_pccard.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pccard.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-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. /* NOTE : This function Should not be modified, when the callback is needed,
  182. the HAL_PCCARD_MspInit could be implemented in the user file
  183. */
  184. }
  185. /**
  186. * @brief PCCARD MSP DeInit
  187. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  188. * the configuration information for PCCARD module.
  189. * @retval None
  190. */
  191. __weak void HAL_PCCARD_MspDeInit(PCCARD_HandleTypeDef *hpccard)
  192. {
  193. /* NOTE : This function Should not be modified, when the callback is needed,
  194. the HAL_PCCARD_MspDeInit could be implemented in the user file
  195. */
  196. }
  197. /**
  198. * @}
  199. */
  200. /** @defgroup PCCARD_Exported_Functions_Group2 Input and Output functions
  201. * @brief Input Output and memory control functions
  202. *
  203. @verbatim
  204. ==============================================================================
  205. ##### PCCARD Input and Output functions #####
  206. ==============================================================================
  207. [..]
  208. This section provides functions allowing to use and control the PCCARD memory
  209. @endverbatim
  210. * @{
  211. */
  212. /**
  213. * @brief Read Compact Flash's ID.
  214. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  215. * the configuration information for PCCARD module.
  216. * @param CompactFlash_ID: Compact flash ID structure.
  217. * @param pStatus: pointer to compact flash status
  218. * @retval HAL status
  219. *
  220. */
  221. HAL_StatusTypeDef HAL_PCCARD_Read_ID(PCCARD_HandleTypeDef *hpccard, uint8_t CompactFlash_ID[], uint8_t *pStatus)
  222. {
  223. uint32_t timeout = PCCARD_TIMEOUT_READ_ID, index = 0;
  224. uint8_t status = 0;
  225. /* Process Locked */
  226. __HAL_LOCK(hpccard);
  227. /* Check the PCCARD controller state */
  228. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  229. {
  230. return HAL_BUSY;
  231. }
  232. /* Update the PCCARD controller state */
  233. hpccard->State = HAL_PCCARD_STATE_BUSY;
  234. /* Initialize the PCCARD status */
  235. *pStatus = PCCARD_READY;
  236. /* Send the Identify Command */
  237. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = 0xECEC;
  238. /* Read PCCARD IDs and timeout treatment */
  239. do
  240. {
  241. /* Read the PCCARD status */
  242. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  243. timeout--;
  244. }while((status != PCCARD_STATUS_OK) && timeout);
  245. if(timeout == 0)
  246. {
  247. *pStatus = PCCARD_TIMEOUT_ERROR;
  248. }
  249. else
  250. {
  251. /* Read PCCARD ID bytes */
  252. for(index = 0; index < 16; index++)
  253. {
  254. CompactFlash_ID[index] = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_DATA);
  255. }
  256. }
  257. /* Update the PCCARD controller state */
  258. hpccard->State = HAL_PCCARD_STATE_READY;
  259. /* Process unlocked */
  260. __HAL_UNLOCK(hpccard);
  261. return HAL_OK;
  262. }
  263. /**
  264. * @brief Read sector from PCCARD memory
  265. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  266. * the configuration information for PCCARD module.
  267. * @param pBuffer: pointer to destination read buffer
  268. * @param SectorAddress: Sector address to read
  269. * @param pStatus: pointer to PCCARD status
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef HAL_PCCARD_Read_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  273. {
  274. uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0;
  275. uint8_t status = 0;
  276. /* Process Locked */
  277. __HAL_LOCK(hpccard);
  278. /* Check the PCCARD controller state */
  279. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  280. {
  281. return HAL_BUSY;
  282. }
  283. /* Update the PCCARD controller state */
  284. hpccard->State = HAL_PCCARD_STATE_BUSY;
  285. /* Initialize PCCARD status */
  286. *pStatus = PCCARD_READY;
  287. /* Set the parameters to write a sector */
  288. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x00;
  289. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100 ) | ((uint16_t)SectorAddress);
  290. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0xE4A0;
  291. do
  292. {
  293. /* wait till the Status = 0x80 */
  294. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  295. timeout--;
  296. }while((status == 0x80) && timeout);
  297. if(timeout == 0)
  298. {
  299. *pStatus = PCCARD_TIMEOUT_ERROR;
  300. }
  301. timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  302. do
  303. {
  304. /* wait till the Status = PCCARD_STATUS_OK */
  305. status = *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  306. timeout--;
  307. }while((status != PCCARD_STATUS_OK) && timeout);
  308. if(timeout == 0)
  309. {
  310. *pStatus = PCCARD_TIMEOUT_ERROR;
  311. }
  312. /* Read bytes */
  313. for(; index < PCCARD_SECTOR_SIZE; index++)
  314. {
  315. *(uint16_t *)pBuffer++ = *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR);
  316. }
  317. /* Update the PCCARD controller state */
  318. hpccard->State = HAL_PCCARD_STATE_READY;
  319. /* Process unlocked */
  320. __HAL_UNLOCK(hpccard);
  321. return HAL_OK;
  322. }
  323. /**
  324. * @brief Write sector to PCCARD memory
  325. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  326. * the configuration information for PCCARD module.
  327. * @param pBuffer: pointer to source write buffer
  328. * @param SectorAddress: Sector address to write
  329. * @param pStatus: pointer to PCCARD status
  330. * @retval HAL status
  331. */
  332. HAL_StatusTypeDef HAL_PCCARD_Write_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  333. {
  334. uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0;
  335. uint8_t status = 0;
  336. /* Process Locked */
  337. __HAL_LOCK(hpccard);
  338. /* Check the PCCARD controller state */
  339. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  340. {
  341. return HAL_BUSY;
  342. }
  343. /* Update the PCCARD controller state */
  344. hpccard->State = HAL_PCCARD_STATE_BUSY;
  345. /* Initialize PCCARD status */
  346. *pStatus = PCCARD_READY;
  347. /* Set the parameters to write a sector */
  348. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x00;
  349. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = ((uint16_t)0x0100 ) | ((uint16_t)SectorAddress);
  350. *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = (uint16_t)0x30A0;
  351. do
  352. {
  353. /* Wait till the Status = PCCARD_STATUS_OK */
  354. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  355. timeout--;
  356. }while((status != PCCARD_STATUS_OK) && timeout);
  357. if(timeout == 0)
  358. {
  359. *pStatus = PCCARD_TIMEOUT_ERROR;
  360. }
  361. /* Write bytes */
  362. for(; index < PCCARD_SECTOR_SIZE; index++)
  363. {
  364. *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR) = *(uint16_t *)pBuffer++;
  365. }
  366. do
  367. {
  368. /* Wait till the Status = PCCARD_STATUS_WRITE_OK */
  369. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  370. timeout--;
  371. }while((status != PCCARD_STATUS_WRITE_OK) && timeout);
  372. if(timeout == 0)
  373. {
  374. *pStatus = PCCARD_TIMEOUT_ERROR;
  375. }
  376. /* Update the PCCARD controller state */
  377. hpccard->State = HAL_PCCARD_STATE_READY;
  378. /* Process unlocked */
  379. __HAL_UNLOCK(hpccard);
  380. return HAL_OK;
  381. }
  382. /**
  383. * @brief Erase sector from PCCARD memory
  384. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  385. * the configuration information for PCCARD module.
  386. * @param SectorAddress: Sector address to erase
  387. * @param pStatus: pointer to PCCARD status
  388. * @retval HAL status
  389. */
  390. HAL_StatusTypeDef HAL_PCCARD_Erase_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t SectorAddress, uint8_t *pStatus)
  391. {
  392. uint32_t timeout = PCCARD_TIMEOUT_ERASE_SECTOR;
  393. uint8_t status = 0;
  394. /* Process Locked */
  395. __HAL_LOCK(hpccard);
  396. /* Check the PCCARD controller state */
  397. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  398. {
  399. return HAL_BUSY;
  400. }
  401. /* Update the PCCARD controller state */
  402. hpccard->State = HAL_PCCARD_STATE_BUSY;
  403. /* Initialize PCCARD status */
  404. *pStatus = PCCARD_READY;
  405. /* Set the parameters to write a sector */
  406. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_LOW) = 0x00;
  407. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = 0x00;
  408. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_NUMBER) = SectorAddress;
  409. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT) = 0x01;
  410. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CARD_HEAD) = 0xA0;
  411. *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD) = ATA_ERASE_SECTOR_CMD;
  412. /* wait till the PCCARD is ready */
  413. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  414. while((status != PCCARD_STATUS_WRITE_OK) && timeout)
  415. {
  416. status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  417. timeout--;
  418. }
  419. if(timeout == 0)
  420. {
  421. *pStatus = PCCARD_TIMEOUT_ERROR;
  422. }
  423. /* Check the PCCARD controller state */
  424. hpccard->State = HAL_PCCARD_STATE_READY;
  425. /* Process unlocked */
  426. __HAL_UNLOCK(hpccard);
  427. return HAL_OK;
  428. }
  429. /**
  430. * @brief Reset the PCCARD memory
  431. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  432. * the configuration information for PCCARD module.
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_PCCARD_Reset(PCCARD_HandleTypeDef *hpccard)
  436. {
  437. /* Process Locked */
  438. __HAL_LOCK(hpccard);
  439. /* Check the PCCARD controller state */
  440. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  441. {
  442. return HAL_BUSY;
  443. }
  444. /* Provide a SW reset and Read and verify the:
  445. - PCCard Configuration Option Register at address 0x98000200 --> 0x80
  446. - Card Configuration and Status Register at address 0x98000202 --> 0x00
  447. - Pin Replacement Register at address 0x98000204 --> 0x0C
  448. - Socket and Copy Register at address 0x98000206 --> 0x00
  449. */
  450. /* Check the PCCARD controller state */
  451. hpccard->State = HAL_PCCARD_STATE_BUSY;
  452. *(__IO uint8_t *)(PCCARD_ATTRIBUTE_SPACE_ADDRESS | ATA_CARD_CONFIGURATION ) = 0x01;
  453. /* Check the PCCARD controller state */
  454. hpccard->State = HAL_PCCARD_STATE_READY;
  455. /* Process unlocked */
  456. __HAL_UNLOCK(hpccard);
  457. return HAL_OK;
  458. }
  459. /**
  460. * @brief This function handles PCCARD device interrupt request.
  461. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  462. * the configuration information for PCCARD module.
  463. * @retval HAL status
  464. */
  465. void HAL_PCCARD_IRQHandler(PCCARD_HandleTypeDef *hpccard)
  466. {
  467. /* Check PCCARD interrupt Rising edge flag */
  468. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE))
  469. {
  470. /* PCCARD interrupt callback*/
  471. HAL_PCCARD_ITCallback(hpccard);
  472. /* Clear PCCARD interrupt Rising edge pending bit */
  473. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_RISING_EDGE);
  474. }
  475. /* Check PCCARD interrupt Level flag */
  476. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_LEVEL))
  477. {
  478. /* PCCARD interrupt callback*/
  479. HAL_PCCARD_ITCallback(hpccard);
  480. /* Clear PCCARD interrupt Level pending bit */
  481. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_LEVEL);
  482. }
  483. /* Check PCCARD interrupt Falling edge flag */
  484. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE))
  485. {
  486. /* PCCARD interrupt callback*/
  487. HAL_PCCARD_ITCallback(hpccard);
  488. /* Clear PCCARD interrupt Falling edge pending bit */
  489. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FALLING_EDGE);
  490. }
  491. /* Check PCCARD interrupt FIFO empty flag */
  492. if(__FMC_PCCARD_GET_FLAG(hpccard->Instance, FMC_FLAG_FEMPT))
  493. {
  494. /* PCCARD interrupt callback*/
  495. HAL_PCCARD_ITCallback(hpccard);
  496. /* Clear PCCARD interrupt FIFO empty pending bit */
  497. __FMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FMC_FLAG_FEMPT);
  498. }
  499. }
  500. /**
  501. * @brief PCCARD interrupt feature callback
  502. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  503. * the configuration information for PCCARD module.
  504. * @retval None
  505. */
  506. __weak void HAL_PCCARD_ITCallback(PCCARD_HandleTypeDef *hpccard)
  507. {
  508. /* NOTE : This function Should not be modified, when the callback is needed,
  509. the HAL_PCCARD_ITCallback could be implemented in the user file
  510. */
  511. }
  512. /**
  513. * @}
  514. */
  515. /** @defgroup PCCARD_Exported_Functions_Group3 State functions
  516. * @brief Peripheral State functions
  517. *
  518. @verbatim
  519. ==============================================================================
  520. ##### PCCARD State functions #####
  521. ==============================================================================
  522. [..]
  523. This subsection permits to get in run-time the status of the PCCARD controller
  524. and the data flow.
  525. @endverbatim
  526. * @{
  527. */
  528. /**
  529. * @brief return the PCCARD controller state
  530. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  531. * the configuration information for PCCARD module.
  532. * @retval HAL state
  533. */
  534. HAL_PCCARD_StateTypeDef HAL_PCCARD_GetState(PCCARD_HandleTypeDef *hpccard)
  535. {
  536. return hpccard->State;
  537. }
  538. /**
  539. * @brief Get the compact flash memory status
  540. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  541. * the configuration information for PCCARD module.
  542. * @retval New status of the PCCARD operation. This parameter can be:
  543. * - CompactFlash_TIMEOUT_ERROR: when the previous operation generate
  544. * a Timeout error
  545. * - CompactFlash_READY: when memory is ready for the next operation
  546. */
  547. HAL_PCCARD_StatusTypeDef HAL_PCCARD_GetStatus(PCCARD_HandleTypeDef *hpccard)
  548. {
  549. uint32_t timeout = PCCARD_TIMEOUT_STATUS, status_pccard = 0;
  550. /* Check the PCCARD controller state */
  551. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  552. {
  553. return HAL_PCCARD_STATUS_ONGOING;
  554. }
  555. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  556. while((status_pccard == PCCARD_BUSY) && timeout)
  557. {
  558. status_pccard = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  559. timeout--;
  560. }
  561. if(timeout == 0)
  562. {
  563. status_pccard = PCCARD_TIMEOUT_ERROR;
  564. }
  565. /* Return the operation status */
  566. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  567. }
  568. /**
  569. * @brief Reads the Compact Flash memory status using the Read status command
  570. * @param hpccard: pointer to a PCCARD_HandleTypeDef structure that contains
  571. * the configuration information for PCCARD module.
  572. * @retval The status of the Compact Flash memory. This parameter can be:
  573. * - CompactFlash_BUSY: when memory is busy
  574. * - CompactFlash_READY: when memory is ready for the next operation
  575. * - CompactFlash_ERROR: when the previous operation generates error
  576. */
  577. HAL_PCCARD_StatusTypeDef HAL_PCCARD_ReadStatus(PCCARD_HandleTypeDef *hpccard)
  578. {
  579. uint8_t data = 0, status_pccard = PCCARD_BUSY;
  580. /* Check the PCCARD controller state */
  581. if(hpccard->State == HAL_PCCARD_STATE_BUSY)
  582. {
  583. return HAL_PCCARD_STATUS_ONGOING;
  584. }
  585. /* Read status operation */
  586. data = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  587. if((data & PCCARD_TIMEOUT_ERROR) == PCCARD_TIMEOUT_ERROR)
  588. {
  589. status_pccard = PCCARD_TIMEOUT_ERROR;
  590. }
  591. else if((data & PCCARD_READY) == PCCARD_READY)
  592. {
  593. status_pccard = PCCARD_READY;
  594. }
  595. return (HAL_PCCARD_StatusTypeDef) status_pccard;
  596. }
  597. /**
  598. * @}
  599. */
  600. /**
  601. * @}
  602. */
  603. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  604. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  605. #endif /* HAL_PCCARD_MODULE_ENABLED */
  606. /**
  607. * @}
  608. */
  609. /**
  610. * @}
  611. */
  612. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/