Subversion Repositories LedShow

Rev

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

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