stm32f1xx_hal_pccard.c 24 KB

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