Subversion Repositories DashDisplay

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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