Subversion Repositories dashGPS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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 layer functions
  16.      to interface with PCCARD devices. This driver is used for:
  17.  
  18.     (+) PCCARD/Compact Flash memory configuration sequence using the function
  19.         HAL_PCCARD_Init()/HAL_CF_Init() with control and timing parameters for
  20.         both common and attribute spaces.
  21.  
  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.  
  26.     (+) Access PCCARD/Compact Flash memory by read/write operations using the functions
  27.         HAL_PCCARD_Read_Sector()/ HAL_PCCARD_Write_Sector() -
  28.         HAL_CF_Read_Sector()/HAL_CF_Write_Sector(), to read/write sector.
  29.  
  30.     (+) Perform PCCARD/Compact Flash Reset chip operation using the function
  31.         HAL_PCCARD_Reset()/HAL_CF_Reset.
  32.  
  33.     (+) Perform PCCARD/Compact Flash erase sector operation using the function
  34.         HAL_PCCARD_Erase_Sector()/HAL_CF_Erase_Sector.
  35.  
  36.     (+) Read the PCCARD/Compact Flash status operation using the function
  37.         HAL_PCCARD_ReadStatus()/HAL_CF_ReadStatus().
  38.  
  39.     (+) You can monitor the PCCARD/Compact Flash  device HAL state by calling
  40.         the function HAL_PCCARD_GetState()/HAL_CF_GetState()
  41.  
  42.    [..]
  43.      (@) This driver is a set of generic APIs which handle standard PCCARD/compact flash
  44.          operations. If a PCCARD/Compact Flash device contains different operations
  45.          and/or implementations, it should be implemented separately.
  46.  
  47.       *** Callback registration ***
  48.     =============================================
  49.     [..]
  50.       The compilation define  USE_HAL_PCCARD_REGISTER_CALLBACKS when set to 1
  51.       allows the user to configure dynamically the driver callbacks.
  52.  
  53.       Use Functions @ref HAL_PCCARD_RegisterCallback() to register a user callback,
  54.       it allows to register following callbacks:
  55.         (+) MspInitCallback    : PCCARD MspInit.
  56.         (+) MspDeInitCallback  : PCCARD MspDeInit.
  57.       This function takes as parameters the HAL peripheral handle, the Callback ID
  58.       and a pointer to the user callback function.
  59.  
  60.       Use function @ref HAL_PCCARD_UnRegisterCallback() to reset a callback to the default
  61.       weak (surcharged) function. It allows to reset following callbacks:
  62.         (+) MspInitCallback    : PCCARD MspInit.
  63.         (+) MspDeInitCallback  : PCCARD MspDeInit.
  64.       This function) takes as parameters the HAL peripheral handle and the Callback ID.
  65.  
  66.       By default, after the @ref HAL_PCCARD_Init and if the state is HAL_PCCARD_STATE_RESET
  67.       all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  68.       Exception done for MspInit and MspDeInit callbacks that are respectively
  69.       reset to the legacy weak (surcharged) functions in the @ref HAL_PCCARD_Init
  70.       and @ref  HAL_PCCARD_DeInit only when these callbacks are null (not registered beforehand).
  71.       If not, MspInit or MspDeInit are not null, the @ref HAL_PCCARD_Init and @ref HAL_PCCARD_DeInit
  72.       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  73.  
  74.       Callbacks can be registered/unregistered in READY state only.
  75.       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  76.       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  77.       during the Init/DeInit.
  78.       In that case first register the MspInit/MspDeInit user callbacks
  79.       using @ref HAL_PCCARD_RegisterCallback before calling @ref HAL_PCCARD_DeInit
  80.       or @ref HAL_PCCARD_Init function.
  81.  
  82.       When The compilation define USE_HAL_PCCARD_REGISTER_CALLBACKS is set to 0 or
  83.       not defined, the callback registering feature is not available
  84.       and weak (surcharged) callbacks are used.
  85.  
  86.   @endverbatim
  87.   ******************************************************************************
  88.   * @attention
  89.   *
  90.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  91.   * All rights reserved.</center></h2>
  92.   *
  93.   * This software component is licensed by ST under BSD 3-Clause license,
  94.   * the "License"; You may not use this file except in compliance with the
  95.   * License. You may obtain a copy of the License at:
  96.   *                       opensource.org/licenses/BSD-3-Clause
  97.   *
  98.   ******************************************************************************
  99.   */
  100.  
  101. /* Includes ------------------------------------------------------------------*/
  102. #include "stm32f1xx_hal.h"
  103.  
  104. #if defined(FSMC_BANK4)
  105.  
  106. /** @addtogroup STM32F1xx_HAL_Driver
  107.   * @{
  108.   */
  109.  
  110. #ifdef HAL_PCCARD_MODULE_ENABLED
  111.  
  112. /** @defgroup PCCARD PCCARD
  113.   * @brief PCCARD HAL module driver
  114.   * @{
  115.   */
  116. /* Private typedef -----------------------------------------------------------*/
  117. /* Private define ------------------------------------------------------------*/
  118.  
  119. /** @defgroup PCCARD_Private_Defines PCCARD Private Defines
  120.   * @{
  121.   */
  122. #define PCCARD_TIMEOUT_READ_ID                 0x0000FFFFU
  123. #define PCCARD_TIMEOUT_READ_WRITE_SECTOR       0x0000FFFFU
  124. #define PCCARD_TIMEOUT_ERASE_SECTOR            0x00000400U
  125. #define PCCARD_TIMEOUT_STATUS                  0x01000000U
  126.  
  127. #define PCCARD_STATUS_OK                       (uint8_t)0x58
  128. #define PCCARD_STATUS_WRITE_OK                 (uint8_t)0x50
  129. /**
  130.   * @}
  131.   */
  132.  
  133. /* Private macro -------------------------------------------------------------*/
  134. /* Private variables ---------------------------------------------------------*/
  135. /* Private function ----------------------------------------------------------*/
  136. /* Exported functions --------------------------------------------------------*/
  137. /** @defgroup PCCARD_Exported_Functions PCCARD Exported Functions
  138.   * @{
  139.   */
  140.  
  141. /** @defgroup PCCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  142.   * @brief    Initialization and Configuration functions
  143.   *
  144.   @verbatim
  145.   ==============================================================================
  146.           ##### PCCARD Initialization and de-initialization functions #####
  147.   ==============================================================================
  148.   [..]
  149.     This section provides functions allowing to initialize/de-initialize
  150.     the PCCARD memory
  151.  
  152. @endverbatim
  153.   * @{
  154.   */
  155.  
  156. /**
  157.   * @brief  Perform the PCCARD memory Initialization sequence
  158.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  159.   *                the configuration information for PCCARD module.
  160.   * @param  ComSpaceTiming Common space timing structure
  161.   * @param  AttSpaceTiming Attribute space timing structure
  162.   * @param  IOSpaceTiming IO space timing structure
  163.   * @retval HAL status
  164.   */
  165. HAL_StatusTypeDef HAL_PCCARD_Init(PCCARD_HandleTypeDef *hpccard, FSMC_NAND_PCC_TimingTypeDef *ComSpaceTiming, FSMC_NAND_PCC_TimingTypeDef *AttSpaceTiming, FSMC_NAND_PCC_TimingTypeDef *IOSpaceTiming)
  166. {
  167.   /* Check the PCCARD controller state */
  168.   if (hpccard == NULL)
  169.   {
  170.     return HAL_ERROR;
  171.   }
  172.  
  173.   if (hpccard->State == HAL_PCCARD_STATE_RESET)
  174.   {
  175.     /* Allocate lock resource and initialize it */
  176.     hpccard->Lock = HAL_UNLOCKED;
  177. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  178.     if(hpccard->MspInitCallback == NULL)
  179.     {
  180.       hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  181.     }
  182.     hpccard->ItCallback = HAL_PCCARD_ITCallback;
  183.  
  184.     /* Init the low level hardware */
  185.     hpccard->MspInitCallback(hpccard);
  186. #else
  187.     /* Initialize the low level hardware (MSP) */
  188.     HAL_PCCARD_MspInit(hpccard);
  189. #endif
  190.   }
  191.  
  192.   /* Initialize the PCCARD state */
  193.   hpccard->State = HAL_PCCARD_STATE_BUSY;
  194.  
  195.   /* Initialize PCCARD control Interface */
  196.   FSMC_PCCARD_Init(hpccard->Instance, &(hpccard->Init));
  197.  
  198.   /* Init PCCARD common space timing Interface */
  199.   FSMC_PCCARD_CommonSpace_Timing_Init(hpccard->Instance, ComSpaceTiming);
  200.  
  201.   /* Init PCCARD attribute space timing Interface */
  202.   FSMC_PCCARD_AttributeSpace_Timing_Init(hpccard->Instance, AttSpaceTiming);
  203.  
  204.   /* Init PCCARD IO space timing Interface */
  205.   FSMC_PCCARD_IOSpace_Timing_Init(hpccard->Instance, IOSpaceTiming);
  206.  
  207.   /* Enable the PCCARD device */
  208.   __FSMC_PCCARD_ENABLE(hpccard->Instance);
  209.  
  210.   /* Update the PCCARD state */
  211.   hpccard->State = HAL_PCCARD_STATE_READY;
  212.  
  213.   return HAL_OK;
  214.  
  215. }
  216.  
  217. /**
  218.   * @brief  Perform the PCCARD memory De-initialization sequence
  219.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  220.   *                the configuration information for PCCARD module.
  221.   * @retval HAL status
  222.   */
  223. HAL_StatusTypeDef  HAL_PCCARD_DeInit(PCCARD_HandleTypeDef *hpccard)
  224. {
  225. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  226.   if(hpccard->MspDeInitCallback == NULL)
  227.   {
  228.     hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  229.   }
  230.  
  231.   /* DeInit the low level hardware */
  232.   hpccard->MspDeInitCallback(hpccard);
  233. #else
  234.   /* De-Initialize the low level hardware (MSP) */
  235.   HAL_PCCARD_MspDeInit(hpccard);
  236. #endif
  237.  
  238.   /* Configure the PCCARD registers with their reset values */
  239.   FSMC_PCCARD_DeInit(hpccard->Instance);
  240.  
  241.   /* Update the PCCARD controller state */
  242.   hpccard->State = HAL_PCCARD_STATE_RESET;
  243.  
  244.   /* Release Lock */
  245.   __HAL_UNLOCK(hpccard);
  246.  
  247.   return HAL_OK;
  248. }
  249.  
  250. /**
  251.   * @brief  PCCARD MSP Init
  252.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  253.   *                the configuration information for PCCARD module.
  254.   * @retval None
  255.   */
  256. __weak void HAL_PCCARD_MspInit(PCCARD_HandleTypeDef *hpccard)
  257. {
  258.   /* Prevent unused argument(s) compilation warning */
  259.   UNUSED(hpccard);
  260.   /* NOTE : This function Should not be modified, when the callback is needed,
  261.             the HAL_PCCARD_MspInit could be implemented in the user file
  262.    */
  263. }
  264.  
  265. /**
  266.   * @brief  PCCARD MSP DeInit
  267.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  268.   *                the configuration information for PCCARD module.
  269.   * @retval None
  270.   */
  271. __weak void HAL_PCCARD_MspDeInit(PCCARD_HandleTypeDef *hpccard)
  272. {
  273.   /* Prevent unused argument(s) compilation warning */
  274.   UNUSED(hpccard);
  275.   /* NOTE : This function Should not be modified, when the callback is needed,
  276.             the HAL_PCCARD_MspDeInit could be implemented in the user file
  277.    */
  278. }
  279.  
  280. /**
  281.   * @}
  282.   */
  283.  
  284. /** @defgroup PCCARD_Exported_Functions_Group2 Input and Output functions
  285.   * @brief    Input Output and memory control functions
  286.   *
  287.   @verbatim
  288.   ==============================================================================
  289.                     ##### PCCARD Input and Output functions #####
  290.   ==============================================================================
  291.   [..]
  292.     This section provides functions allowing to use and control the PCCARD memory
  293.  
  294. @endverbatim
  295.   * @{
  296.   */
  297.  
  298. /**
  299.   * @brief  Read Compact Flash's ID.
  300.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  301.   *                the configuration information for PCCARD module.
  302.   * @param  CompactFlash_ID Compact flash ID structure.
  303.   * @param  pStatus pointer to compact flash status
  304.   * @retval HAL status
  305.   *
  306.   */
  307. HAL_StatusTypeDef HAL_PCCARD_Read_ID(PCCARD_HandleTypeDef *hpccard, uint8_t CompactFlash_ID[], uint8_t *pStatus)
  308. {
  309.   uint32_t timeout = PCCARD_TIMEOUT_READ_ID, index = 0U;
  310.   uint8_t status = 0;
  311.  
  312.   /* Process Locked */
  313.   __HAL_LOCK(hpccard);
  314.  
  315.   /* Check the PCCARD controller state */
  316.   if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  317.   {
  318.     return HAL_BUSY;
  319.   }
  320.  
  321.   /* Update the PCCARD controller state */
  322.   hpccard->State = HAL_PCCARD_STATE_BUSY;
  323.  
  324.   /* Initialize the PCCARD status */
  325.   *pStatus = PCCARD_READY;
  326.  
  327.   /* Send the Identify Command */
  328.   *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD)  = (uint16_t)0xECEC;
  329.  
  330.   /* Read PCCARD IDs and timeout treatment */
  331.   do
  332.   {
  333.     /* Read the PCCARD status */
  334.     status = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  335.  
  336.     timeout--;
  337.   }
  338.   while ((status != PCCARD_STATUS_OK) && timeout);
  339.  
  340.   if (timeout == 0U)
  341.   {
  342.     *pStatus = PCCARD_TIMEOUT_ERROR;
  343.   }
  344.   else
  345.   {
  346.     /* Read PCCARD ID bytes */
  347.     for (index = 0U; index < 16U; index++)
  348.     {
  349.       CompactFlash_ID[index] = *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_DATA);
  350.     }
  351.   }
  352.  
  353.   /* Update the PCCARD controller state */
  354.   hpccard->State = HAL_PCCARD_STATE_READY;
  355.  
  356.   /* Process unlocked */
  357.   __HAL_UNLOCK(hpccard);
  358.  
  359.   return HAL_OK;
  360. }
  361.  
  362. /**
  363.   * @brief  Read sector from PCCARD memory
  364.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  365.   *                the configuration information for PCCARD module.
  366.   * @param  pBuffer pointer to destination read buffer
  367.   * @param  SectorAddress Sector address to read
  368.   * @param  pStatus pointer to PCCARD status
  369.   * @retval HAL status
  370.   */
  371. HAL_StatusTypeDef HAL_PCCARD_Read_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress, uint8_t *pStatus)
  372. {
  373.   uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0U;
  374.   uint8_t status = 0;
  375.  
  376.   /* Process Locked */
  377.   __HAL_LOCK(hpccard);
  378.  
  379.   /* Check the PCCARD controller state */
  380.   if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  381.   {
  382.     return HAL_BUSY;
  383.   }
  384.  
  385.   /* Update the PCCARD controller state */
  386.   hpccard->State = HAL_PCCARD_STATE_BUSY;
  387.  
  388.   /* Initialize PCCARD status */
  389.   *pStatus = PCCARD_READY;
  390.  
  391.   /* Set the parameters to write a sector */
  392.   *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x0000;
  393.   *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT)  = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  394.   *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD)    = (uint16_t)0xE4A0;
  395.  
  396.   do
  397.   {
  398.     /* wait till the Status = 0x80 */
  399.     status =  *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  400.     timeout--;
  401.   }
  402.   while ((status == 0x80U) && timeout);
  403.  
  404.   if (timeout == 0U)
  405.   {
  406.     *pStatus = PCCARD_TIMEOUT_ERROR;
  407.   }
  408.  
  409.   timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR;
  410.  
  411.   do
  412.   {
  413.     /* wait till the Status = PCCARD_STATUS_OK */
  414.     status =  *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  415.     timeout--;
  416.   }
  417.   while ((status != PCCARD_STATUS_OK) && timeout);
  418.  
  419.   if (timeout == 0U)
  420.   {
  421.     *pStatus = PCCARD_TIMEOUT_ERROR;
  422.   }
  423.  
  424.   /* Read bytes */
  425.   for (; index < PCCARD_SECTOR_SIZE; index++)
  426.   {
  427.     *(uint16_t *)pBuffer++ = *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR);
  428.   }
  429.  
  430.   /* Update the PCCARD controller state */
  431.   hpccard->State = HAL_PCCARD_STATE_READY;
  432.  
  433.   /* Process unlocked */
  434.   __HAL_UNLOCK(hpccard);
  435.  
  436.   return HAL_OK;
  437. }
  438.  
  439.  
  440. /**
  441.   * @brief  Write sector to PCCARD memory
  442.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  443.   *                the configuration information for PCCARD module.
  444.   * @param  pBuffer pointer to source write buffer
  445.   * @param  SectorAddress Sector address to write
  446.   * @param  pStatus pointer to PCCARD status
  447.   * @retval HAL status
  448.   */
  449. HAL_StatusTypeDef HAL_PCCARD_Write_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t *pBuffer, uint16_t SectorAddress,  uint8_t *pStatus)
  450. {
  451.   uint32_t timeout = PCCARD_TIMEOUT_READ_WRITE_SECTOR, index = 0U;
  452.   uint8_t status = 0;
  453.  
  454.   /* Process Locked */
  455.   __HAL_LOCK(hpccard);
  456.  
  457.   /* Check the PCCARD controller state */
  458.   if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  459.   {
  460.     return HAL_BUSY;
  461.   }
  462.  
  463.   /* Update the PCCARD controller state */
  464.   hpccard->State = HAL_PCCARD_STATE_BUSY;
  465.  
  466.   /* Initialize PCCARD status */
  467.   *pStatus = PCCARD_READY;
  468.  
  469.   /* Set the parameters to write a sector */
  470.   *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = (uint16_t)0x0000;
  471.   *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT)  = ((uint16_t)0x0100) | ((uint16_t)SectorAddress);
  472.   *(__IO uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD)    = (uint16_t)0x30A0;
  473.  
  474.   do
  475.   {
  476.     /* Wait till the Status = PCCARD_STATUS_OK */
  477.     status =  *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  478.     timeout--;
  479.   }
  480.   while ((status != PCCARD_STATUS_OK) && timeout);
  481.  
  482.   if (timeout == 0U)
  483.   {
  484.     *pStatus = PCCARD_TIMEOUT_ERROR;
  485.   }
  486.  
  487.   /* Write bytes */
  488.   for (; index < PCCARD_SECTOR_SIZE; index++)
  489.   {
  490.     *(uint16_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR) = *(uint16_t *)pBuffer++;
  491.   }
  492.  
  493.   do
  494.   {
  495.     /* Wait till the Status = PCCARD_STATUS_WRITE_OK */
  496.     status =  *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  497.     timeout--;
  498.   }
  499.   while ((status != PCCARD_STATUS_WRITE_OK) && timeout);
  500.  
  501.   if (timeout == 0U)
  502.   {
  503.     *pStatus = PCCARD_TIMEOUT_ERROR;
  504.   }
  505.  
  506.   /* Update the PCCARD controller state */
  507.   hpccard->State = HAL_PCCARD_STATE_READY;
  508.  
  509.   /* Process unlocked */
  510.   __HAL_UNLOCK(hpccard);
  511.  
  512.   return HAL_OK;
  513. }
  514.  
  515.  
  516. /**
  517.   * @brief  Erase sector from PCCARD memory
  518.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  519.   *                the configuration information for PCCARD module.
  520.   * @param  SectorAddress Sector address to erase
  521.   * @param  pStatus pointer to PCCARD status
  522.   * @retval HAL status
  523.   */
  524. HAL_StatusTypeDef  HAL_PCCARD_Erase_Sector(PCCARD_HandleTypeDef *hpccard, uint16_t SectorAddress, uint8_t *pStatus)
  525. {
  526.   uint32_t timeout = PCCARD_TIMEOUT_ERASE_SECTOR;
  527.   uint8_t status = 0;
  528.  
  529.   /* Process Locked */
  530.   __HAL_LOCK(hpccard);
  531.  
  532.   /* Check the PCCARD controller state */
  533.   if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  534.   {
  535.     return HAL_BUSY;
  536.   }
  537.  
  538.   /* Update the PCCARD controller state */
  539.   hpccard->State = HAL_PCCARD_STATE_BUSY;
  540.  
  541.   /* Initialize PCCARD status */
  542.   *pStatus = PCCARD_READY;
  543.  
  544.   /* Set the parameters to write a sector */
  545.   *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_LOW)  = 0x00;
  546.   *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CYLINDER_HIGH) = 0x00;
  547.   *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_NUMBER) = SectorAddress;
  548.   *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_SECTOR_COUNT)  = 0x01;
  549.   *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_CARD_HEAD)     = 0xA0;
  550.   *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD)    = ATA_ERASE_SECTOR_CMD;
  551.  
  552.   /* wait till the PCCARD is ready */
  553.   status =  *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  554.  
  555.   while ((status != PCCARD_STATUS_WRITE_OK) && timeout)
  556.   {
  557.     status =  *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  558.     timeout--;
  559.   }
  560.  
  561.   if (timeout == 0U)
  562.   {
  563.     *pStatus = PCCARD_TIMEOUT_ERROR;
  564.   }
  565.  
  566.   /* Check the PCCARD controller state */
  567.   hpccard->State = HAL_PCCARD_STATE_READY;
  568.  
  569.   /* Process unlocked */
  570.   __HAL_UNLOCK(hpccard);
  571.  
  572.   return HAL_OK;
  573. }
  574.  
  575. /**
  576.   * @brief  Reset the PCCARD memory
  577.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  578.   *                the configuration information for PCCARD module.
  579.   * @retval HAL status
  580.   */
  581. HAL_StatusTypeDef HAL_PCCARD_Reset(PCCARD_HandleTypeDef *hpccard)
  582. {
  583.   /* Process Locked */
  584.   __HAL_LOCK(hpccard);
  585.  
  586.   /* Check the PCCARD controller state */
  587.   if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  588.   {
  589.     return HAL_BUSY;
  590.   }
  591.  
  592.   /* Provide a SW reset and Read and verify the:
  593.    - PCCard Configuration Option Register at address 0x98000200 --> 0x80
  594.    - Card Configuration and Status Register at address 0x98000202 --> 0x00
  595.    - Pin Replacement Register at address 0x98000204 --> 0x0C
  596.    - Socket and Copy Register at address 0x98000206 --> 0x00
  597.   */
  598.  
  599.   /* Check the PCCARD controller state */
  600.   hpccard->State = HAL_PCCARD_STATE_BUSY;
  601.  
  602.   *(__IO uint8_t *)(PCCARD_ATTRIBUTE_SPACE_ADDRESS | ATA_CARD_CONFIGURATION) = 0x01;
  603.  
  604.   /* Check the PCCARD controller state */
  605.   hpccard->State = HAL_PCCARD_STATE_READY;
  606.  
  607.   /* Process unlocked */
  608.   __HAL_UNLOCK(hpccard);
  609.  
  610.   return HAL_OK;
  611. }
  612.  
  613. /**
  614.   * @brief  This function handles PCCARD device interrupt request.
  615.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  616.   *                the configuration information for PCCARD module.
  617.   * @retval HAL status
  618. */
  619. void HAL_PCCARD_IRQHandler(PCCARD_HandleTypeDef *hpccard)
  620. {
  621.   /* Check PCCARD interrupt Rising edge flag */
  622.   if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_RISING_EDGE))
  623.   {
  624.     /* PCCARD interrupt callback*/
  625. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  626.     hpccard->ItCallback(hpccard);
  627. #else
  628.     HAL_PCCARD_ITCallback(hpccard);
  629. #endif
  630.  
  631.     /* Clear PCCARD interrupt Rising edge pending bit */
  632.     __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_RISING_EDGE);
  633.   }
  634.  
  635.   /* Check PCCARD interrupt Level flag */
  636.   if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_LEVEL))
  637.   {
  638.     /* PCCARD interrupt callback*/
  639. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  640.     hpccard->ItCallback(hpccard);
  641. #else
  642.     HAL_PCCARD_ITCallback(hpccard);
  643. #endif
  644.  
  645.     /* Clear PCCARD interrupt Level pending bit */
  646.     __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_LEVEL);
  647.   }
  648.  
  649.   /* Check PCCARD interrupt Falling edge flag */
  650.   if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_FALLING_EDGE))
  651.   {
  652.     /* PCCARD interrupt callback*/
  653. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  654.     hpccard->ItCallback(hpccard);
  655. #else
  656.     HAL_PCCARD_ITCallback(hpccard);
  657. #endif
  658.  
  659.     /* Clear PCCARD interrupt Falling edge pending bit */
  660.     __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_FALLING_EDGE);
  661.   }
  662.  
  663.   /* Check PCCARD interrupt FIFO empty flag */
  664.   if (__FSMC_PCCARD_GET_FLAG(hpccard->Instance, FSMC_FLAG_FEMPT))
  665.   {
  666.     /* PCCARD interrupt callback*/
  667. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  668.     hpccard->ItCallback(hpccard);
  669. #else
  670.     HAL_PCCARD_ITCallback(hpccard);
  671. #endif
  672.  
  673.     /* Clear PCCARD interrupt FIFO empty pending bit */
  674.     __FSMC_PCCARD_CLEAR_FLAG(hpccard->Instance, FSMC_FLAG_FEMPT);
  675.   }
  676. }
  677.  
  678. /**
  679.   * @brief  PCCARD interrupt feature callback
  680.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  681.   *                the configuration information for PCCARD module.
  682.   * @retval None
  683.   */
  684. __weak void HAL_PCCARD_ITCallback(PCCARD_HandleTypeDef *hpccard)
  685. {
  686.   /* Prevent unused argument(s) compilation warning */
  687.   UNUSED(hpccard);
  688.   /* NOTE : This function Should not be modified, when the callback is needed,
  689.             the HAL_PCCARD_ITCallback could be implemented in the user file
  690.    */
  691. }
  692.  
  693. #if (USE_HAL_PCCARD_REGISTER_CALLBACKS == 1)
  694. /**
  695.   * @brief  Register a User PCCARD Callback
  696.   *         To be used instead of the weak (surcharged) predefined callback
  697.   * @param hpccard : PCCARD handle
  698.   * @param CallbackId : ID of the callback to be registered
  699.   *        This parameter can be one of the following values:
  700.   *          @arg @ref HAL_PCCARD_MSP_INIT_CB_ID       PCCARD MspInit callback ID
  701.   *          @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID     PCCARD MspDeInit callback ID
  702.   *          @arg @ref HAL_PCCARD_IT_CB_ID             PCCARD IT callback ID
  703.   * @param pCallback : pointer to the Callback function
  704.   * @retval status
  705.   */
  706. HAL_StatusTypeDef HAL_PCCARD_RegisterCallback (PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId, pPCCARD_CallbackTypeDef pCallback)
  707. {
  708.   HAL_StatusTypeDef status = HAL_OK;
  709.  
  710.   if(pCallback == NULL)
  711.   {
  712.     return HAL_ERROR;
  713.   }
  714.  
  715.   /* Process locked */
  716.   __HAL_LOCK(hpccard);
  717.  
  718.   if(hpccard->State == HAL_PCCARD_STATE_READY)
  719.   {
  720.     switch (CallbackId)
  721.     {
  722.     case HAL_PCCARD_MSP_INIT_CB_ID :
  723.       hpccard->MspInitCallback = pCallback;
  724.       break;
  725.     case HAL_PCCARD_MSP_DEINIT_CB_ID :
  726.       hpccard->MspDeInitCallback = pCallback;
  727.       break;
  728.     case HAL_PCCARD_IT_CB_ID :
  729.       hpccard->ItCallback = pCallback;
  730.       break;
  731.     default :
  732.       /* update return status */
  733.       status =  HAL_ERROR;
  734.       break;
  735.     }
  736.   }
  737.   else if(hpccard->State == HAL_PCCARD_STATE_RESET)
  738.   {
  739.     switch (CallbackId)
  740.     {
  741.     case HAL_PCCARD_MSP_INIT_CB_ID :
  742.       hpccard->MspInitCallback = pCallback;
  743.       break;
  744.     case HAL_PCCARD_MSP_DEINIT_CB_ID :
  745.       hpccard->MspDeInitCallback = pCallback;
  746.       break;
  747.     default :
  748.       /* update return status */
  749.       status =  HAL_ERROR;
  750.       break;
  751.     }
  752.   }
  753.   else
  754.   {
  755.     /* update return status */
  756.     status =  HAL_ERROR;
  757.   }
  758.  
  759.   /* Release Lock */
  760.   __HAL_UNLOCK(hpccard);
  761.   return status;
  762. }
  763.  
  764. /**
  765.   * @brief  Unregister a User PCCARD Callback
  766.   *         PCCARD Callback is redirected to the weak (surcharged) predefined callback
  767.   * @param hpccard : PCCARD handle
  768.   * @param CallbackId : ID of the callback to be unregistered
  769.   *        This parameter can be one of the following values:
  770.   *          @arg @ref HAL_PCCARD_MSP_INIT_CB_ID       PCCARD MspInit callback ID
  771.   *          @arg @ref HAL_PCCARD_MSP_DEINIT_CB_ID     PCCARD MspDeInit callback ID
  772.   *          @arg @ref HAL_PCCARD_IT_CB_ID             PCCARD IT callback ID
  773.   * @retval status
  774.   */
  775. HAL_StatusTypeDef HAL_PCCARD_UnRegisterCallback (PCCARD_HandleTypeDef *hpccard, HAL_PCCARD_CallbackIDTypeDef CallbackId)
  776. {
  777.   HAL_StatusTypeDef status = HAL_OK;
  778.  
  779.   /* Process locked */
  780.   __HAL_LOCK(hpccard);
  781.  
  782.   if(hpccard->State == HAL_PCCARD_STATE_READY)
  783.   {
  784.     switch (CallbackId)
  785.     {
  786.     case HAL_PCCARD_MSP_INIT_CB_ID :
  787.       hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  788.       break;
  789.     case HAL_PCCARD_MSP_DEINIT_CB_ID :
  790.       hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  791.       break;
  792.     case HAL_PCCARD_IT_CB_ID :
  793.       hpccard->ItCallback = HAL_PCCARD_ITCallback;
  794.       break;
  795.     default :
  796.       /* update return status */
  797.       status =  HAL_ERROR;
  798.       break;
  799.     }
  800.   }
  801.   else if(hpccard->State == HAL_PCCARD_STATE_RESET)
  802.   {
  803.     switch (CallbackId)
  804.     {
  805.     case HAL_PCCARD_MSP_INIT_CB_ID :
  806.       hpccard->MspInitCallback = HAL_PCCARD_MspInit;
  807.       break;
  808.     case HAL_PCCARD_MSP_DEINIT_CB_ID :
  809.       hpccard->MspDeInitCallback = HAL_PCCARD_MspDeInit;
  810.       break;
  811.     default :
  812.       /* update return status */
  813.       status =  HAL_ERROR;
  814.       break;
  815.     }
  816.   }
  817.   else
  818.   {
  819.     /* update return status */
  820.     status =  HAL_ERROR;
  821.   }
  822.  
  823.   /* Release Lock */
  824.   __HAL_UNLOCK(hpccard);
  825.   return status;
  826. }
  827. #endif
  828.  
  829. /**
  830.   * @}
  831.   */
  832.  
  833. /** @defgroup PCCARD_Exported_Functions_Group3 State functions
  834.  *  @brief   Peripheral State functions
  835.  *
  836. @verbatim
  837.   ==============================================================================
  838.                       ##### PCCARD State functions #####
  839.   ==============================================================================
  840.   [..]
  841.     This subsection permits to get in run-time the status of the PCCARD controller
  842.     and the data flow.
  843.  
  844. @endverbatim
  845.   * @{
  846.   */
  847.  
  848. /**
  849.   * @brief  return the PCCARD controller state
  850.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  851.   *                the configuration information for PCCARD module.
  852.   * @retval HAL state
  853.   */
  854. HAL_PCCARD_StateTypeDef HAL_PCCARD_GetState(PCCARD_HandleTypeDef *hpccard)
  855. {
  856.   return hpccard->State;
  857. }
  858.  
  859. /**
  860.   * @brief  Get the compact flash memory status
  861.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  862.   *                the configuration information for PCCARD module.
  863.   * @retval New status of the PCCARD operation. This parameter can be:
  864.   *          - CompactFlash_TIMEOUT_ERROR: when the previous operation generate
  865.   *            a Timeout error
  866.   *          - CompactFlash_READY: when memory is ready for the next operation
  867.   */
  868. HAL_PCCARD_StatusTypeDef HAL_PCCARD_GetStatus(PCCARD_HandleTypeDef *hpccard)
  869. {
  870.   uint32_t timeout = PCCARD_TIMEOUT_STATUS, status_pccard = 0U;
  871.  
  872.   /* Check the PCCARD controller state */
  873.   if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  874.   {
  875.     return HAL_PCCARD_STATUS_ONGOING;
  876.   }
  877.  
  878.   status_pccard =  *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  879.  
  880.   while ((status_pccard == PCCARD_BUSY) && timeout)
  881.   {
  882.     status_pccard =  *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  883.     timeout--;
  884.   }
  885.  
  886.   if (timeout == 0U)
  887.   {
  888.     status_pccard =  PCCARD_TIMEOUT_ERROR;
  889.   }
  890.  
  891.   /* Return the operation status */
  892.   return (HAL_PCCARD_StatusTypeDef) status_pccard;
  893. }
  894.  
  895. /**
  896.   * @brief  Reads the Compact Flash memory status using the Read status command
  897.   * @param  hpccard pointer to a PCCARD_HandleTypeDef structure that contains
  898.   *                the configuration information for PCCARD module.
  899.   * @retval The status of the Compact Flash memory. This parameter can be:
  900.   *          - CompactFlash_BUSY: when memory is busy
  901.   *          - CompactFlash_READY: when memory is ready for the next operation
  902.   *          - CompactFlash_ERROR: when the previous operation generates error
  903.   */
  904. HAL_PCCARD_StatusTypeDef HAL_PCCARD_ReadStatus(PCCARD_HandleTypeDef *hpccard)
  905. {
  906.   uint8_t data = 0U, status_pccard = PCCARD_BUSY;
  907.  
  908.   /* Check the PCCARD controller state */
  909.   if (hpccard->State == HAL_PCCARD_STATE_BUSY)
  910.   {
  911.     return HAL_PCCARD_STATUS_ONGOING;
  912.   }
  913.  
  914.   /* Read status operation */
  915.   data =  *(__IO uint8_t *)(PCCARD_IO_SPACE_PRIMARY_ADDR | ATA_STATUS_CMD_ALTERNATE);
  916.  
  917.   if ((data & PCCARD_TIMEOUT_ERROR) == PCCARD_TIMEOUT_ERROR)
  918.   {
  919.     status_pccard = PCCARD_TIMEOUT_ERROR;
  920.   }
  921.   else if ((data & PCCARD_READY) == PCCARD_READY)
  922.   {
  923.     status_pccard = PCCARD_READY;
  924.   }
  925.  
  926.   return (HAL_PCCARD_StatusTypeDef) status_pccard;
  927. }
  928.  
  929. /**
  930.   * @}
  931.   */
  932.  
  933. /**
  934.   * @}
  935.   */
  936.  
  937. /**
  938.   * @}
  939.   */
  940.  
  941. #endif /* HAL_PCCARD_MODULE_ENABLED */
  942.  
  943. /**
  944.   * @}
  945.   */
  946.  
  947. #endif /* FSMC_BANK4 */
  948.  
  949. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  950.