stm32f1xx_hal_pccard.c 24 KB

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