OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [drivers/] [ST/] [STM32F10xFWLib/] [src/] [stm32f10x_flash.c] - Blame information for rev 608

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 608 jeremybenn
/**
2
  ******************************************************************************
3
  * @file  stm32f10x_flash.c
4
  * @author  MCD Application Team
5
  * @version  V3.0.0
6
  * @date  04/06/2009
7
  * @brief  This file provides all the FLASH firmware functions.
8
  ******************************************************************************
9
  * @copy
10
  *
11
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17
  *
18
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
19
  */
20
 
21
/* Includes ------------------------------------------------------------------*/
22
#include "stm32f10x_flash.h"
23
 
24
/** @addtogroup StdPeriph_Driver
25
  * @{
26
  */
27
 
28
/** @defgroup FLASH
29
  * @brief FLASH driver modules
30
  * @{
31
  */
32
 
33
/** @defgroup FLASH_Private_TypesDefinitions
34
  * @{
35
  */
36
 
37
/**
38
  * @}
39
  */
40
 
41
/** @defgroup FLASH_Private_Defines
42
  * @{
43
  */
44
 
45
/* Flash Access Control Register bits */
46
#define ACR_LATENCY_Mask         ((uint32_t)0x00000038)
47
#define ACR_HLFCYA_Mask          ((uint32_t)0xFFFFFFF7)
48
#define ACR_PRFTBE_Mask          ((uint32_t)0xFFFFFFEF)
49
 
50
/* Flash Access Control Register bits */
51
#define ACR_PRFTBS_Mask          ((uint32_t)0x00000020) 
52
 
53
/* Flash Control Register bits */
54
#define CR_PG_Set                ((uint32_t)0x00000001)
55
#define CR_PG_Reset              ((uint32_t)0x00001FFE) 
56
#define CR_PER_Set               ((uint32_t)0x00000002)
57
#define CR_PER_Reset             ((uint32_t)0x00001FFD)
58
#define CR_MER_Set               ((uint32_t)0x00000004)
59
#define CR_MER_Reset             ((uint32_t)0x00001FFB)
60
#define CR_OPTPG_Set             ((uint32_t)0x00000010)
61
#define CR_OPTPG_Reset           ((uint32_t)0x00001FEF)
62
#define CR_OPTER_Set             ((uint32_t)0x00000020)
63
#define CR_OPTER_Reset           ((uint32_t)0x00001FDF)
64
#define CR_STRT_Set              ((uint32_t)0x00000040)
65
#define CR_LOCK_Set              ((uint32_t)0x00000080)
66
 
67
/* FLASH Mask */
68
#define RDPRT_Mask               ((uint32_t)0x00000002)
69
#define WRP0_Mask                ((uint32_t)0x000000FF)
70
#define WRP1_Mask                ((uint32_t)0x0000FF00)
71
#define WRP2_Mask                ((uint32_t)0x00FF0000)
72
#define WRP3_Mask                ((uint32_t)0xFF000000)
73
 
74
/* FLASH Keys */
75
#define RDP_Key                  ((uint16_t)0x00A5)
76
#define FLASH_KEY1               ((uint32_t)0x45670123)
77
#define FLASH_KEY2               ((uint32_t)0xCDEF89AB)
78
 
79
/* Delay definition */
80
#define EraseTimeout             ((uint32_t)0x00000FFF)
81
#define ProgramTimeout           ((uint32_t)0x0000000F)
82
 
83
/**
84
  * @}
85
  */
86
 
87
/** @defgroup FLASH_Private_Macros
88
  * @{
89
  */
90
 
91
/**
92
  * @}
93
  */
94
 
95
/** @defgroup FLASH_Private_Variables
96
  * @{
97
  */
98
 
99
/**
100
  * @}
101
  */
102
 
103
/** @defgroup FLASH_Private_FunctionPrototypes
104
  * @{
105
  */
106
 
107
static void delay(void);
108
/**
109
  * @}
110
  */
111
 
112
/** @defgroup FLASH_Private_Functions
113
  * @{
114
  */
115
 
116
/**
117
  * @brief  Sets the code latency value.
118
  * @param FLASH_Latency: specifies the FLASH Latency value.
119
  *   This parameter can be one of the following values:
120
  * @arg FLASH_Latency_0: FLASH Zero Latency cycle
121
  * @arg FLASH_Latency_1: FLASH One Latency cycle
122
  * @arg FLASH_Latency_2: FLASH Two Latency cycles
123
  * @retval : None
124
  */
125
void FLASH_SetLatency(uint32_t FLASH_Latency)
126
{
127
  uint32_t tmpreg = 0;
128
 
129
  /* Check the parameters */
130
  assert_param(IS_FLASH_LATENCY(FLASH_Latency));
131
 
132
  /* Read the ACR register */
133
  tmpreg = FLASH->ACR;
134
 
135
  /* Sets the Latency value */
136
  tmpreg &= ACR_LATENCY_Mask;
137
  tmpreg |= FLASH_Latency;
138
 
139
  /* Write the ACR register */
140
  FLASH->ACR = tmpreg;
141
}
142
 
143
/**
144
  * @brief  Enables or disables the Half cycle flash access.
145
  * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
146
  *   This parameter can be one of the following values:
147
  * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
148
  * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
149
  * @retval : None
150
  */
151
void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)
152
{
153
  /* Check the parameters */
154
  assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));
155
 
156
  /* Enable or disable the Half cycle access */
157
  FLASH->ACR &= ACR_HLFCYA_Mask;
158
  FLASH->ACR |= FLASH_HalfCycleAccess;
159
}
160
 
161
/**
162
  * @brief  Enables or disables the Prefetch Buffer.
163
  * @param FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
164
  *   This parameter can be one of the following values:
165
  * @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
166
  * @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
167
  * @retval : None
168
  */
169
void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)
170
{
171
  /* Check the parameters */
172
  assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));
173
 
174
  /* Enable or disable the Prefetch Buffer */
175
  FLASH->ACR &= ACR_PRFTBE_Mask;
176
  FLASH->ACR |= FLASH_PrefetchBuffer;
177
}
178
 
179
/**
180
  * @brief  Unlocks the FLASH Program Erase Controller.
181
  * @param  None
182
  * @retval : None
183
  */
184
void FLASH_Unlock(void)
185
{
186
  /* Authorize the FPEC Access */
187
  FLASH->KEYR = FLASH_KEY1;
188
  FLASH->KEYR = FLASH_KEY2;
189
}
190
 
191
/**
192
  * @brief  Locks the FLASH Program Erase Controller.
193
  * @param  None
194
  * @retval : None
195
  */
196
void FLASH_Lock(void)
197
{
198
  /* Set the Lock Bit to lock the FPEC and the FCR */
199
  FLASH->CR |= CR_LOCK_Set;
200
}
201
 
202
/**
203
  * @brief  Erases a specified FLASH page.
204
  * @param Page_Address: The page address to be erased.
205
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
206
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
207
  *   FLASH_TIMEOUT.
208
  */
209
FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
210
{
211
  FLASH_Status status = FLASH_COMPLETE;
212
  /* Check the parameters */
213
  assert_param(IS_FLASH_ADDRESS(Page_Address));
214
  /* Wait for last operation to be completed */
215
  status = FLASH_WaitForLastOperation(EraseTimeout);
216
 
217
  if(status == FLASH_COMPLETE)
218
  {
219
    /* if the previous operation is completed, proceed to erase the page */
220
    FLASH->CR|= CR_PER_Set;
221
    FLASH->AR = Page_Address;
222
    FLASH->CR|= CR_STRT_Set;
223
 
224
    /* Wait for last operation to be completed */
225
    status = FLASH_WaitForLastOperation(EraseTimeout);
226
    if(status != FLASH_BUSY)
227
    {
228
      /* if the erase operation is completed, disable the PER Bit */
229
      FLASH->CR &= CR_PER_Reset;
230
    }
231
  }
232
  /* Return the Erase Status */
233
  return status;
234
}
235
 
236
/**
237
  * @brief  Erases all FLASH pages.
238
  * @param  None
239
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
240
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
241
  *   FLASH_TIMEOUT.
242
  */
243
FLASH_Status FLASH_EraseAllPages(void)
244
{
245
  FLASH_Status status = FLASH_COMPLETE;
246
  /* Wait for last operation to be completed */
247
  status = FLASH_WaitForLastOperation(EraseTimeout);
248
 
249
  if(status == FLASH_COMPLETE)
250
  {
251
    /* if the previous operation is completed, proceed to erase all pages */
252
     FLASH->CR |= CR_MER_Set;
253
     FLASH->CR |= CR_STRT_Set;
254
 
255
    /* Wait for last operation to be completed */
256
    status = FLASH_WaitForLastOperation(EraseTimeout);
257
    if(status != FLASH_BUSY)
258
    {
259
      /* if the erase operation is completed, disable the MER Bit */
260
      FLASH->CR &= CR_MER_Reset;
261
    }
262
  }
263
  /* Return the Erase Status */
264
  return status;
265
}
266
 
267
/**
268
  * @brief  Erases the FLASH option bytes.
269
  * @param  None
270
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
271
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
272
  *   FLASH_TIMEOUT.
273
  */
274
FLASH_Status FLASH_EraseOptionBytes(void)
275
{
276
  FLASH_Status status = FLASH_COMPLETE;
277
 
278
  /* Wait for last operation to be completed */
279
  status = FLASH_WaitForLastOperation(EraseTimeout);
280
  if(status == FLASH_COMPLETE)
281
  {
282
    /* Authorize the small information block programming */
283
    FLASH->OPTKEYR = FLASH_KEY1;
284
    FLASH->OPTKEYR = FLASH_KEY2;
285
 
286
    /* if the previous operation is completed, proceed to erase the option bytes */
287
    FLASH->CR |= CR_OPTER_Set;
288
    FLASH->CR |= CR_STRT_Set;
289
    /* Wait for last operation to be completed */
290
    status = FLASH_WaitForLastOperation(EraseTimeout);
291
 
292
    if(status == FLASH_COMPLETE)
293
    {
294
      /* if the erase operation is completed, disable the OPTER Bit */
295
      FLASH->CR &= CR_OPTER_Reset;
296
 
297
      /* Enable the Option Bytes Programming operation */
298
      FLASH->CR |= CR_OPTPG_Set;
299
      /* Enable the readout access */
300
      OB->RDP= RDP_Key;
301
      /* Wait for last operation to be completed */
302
      status = FLASH_WaitForLastOperation(ProgramTimeout);
303
 
304
      if(status != FLASH_BUSY)
305
      {
306
        /* if the program operation is completed, disable the OPTPG Bit */
307
        FLASH->CR &= CR_OPTPG_Reset;
308
      }
309
    }
310
    else
311
    {
312
      if (status != FLASH_BUSY)
313
      {
314
        /* Disable the OPTPG Bit */
315
        FLASH->CR &= CR_OPTPG_Reset;
316
      }
317
    }
318
  }
319
  /* Return the erase status */
320
  return status;
321
}
322
 
323
/**
324
  * @brief  Programs a word at a specified address.
325
  * @param Address: specifies the address to be programmed.
326
  * @param Data: specifies the data to be programmed.
327
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
328
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
329
  *   FLASH_TIMEOUT.
330
  */
331
FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
332
{
333
  FLASH_Status status = FLASH_COMPLETE;
334
  /* Check the parameters */
335
  assert_param(IS_FLASH_ADDRESS(Address));
336
  /* Wait for last operation to be completed */
337
  status = FLASH_WaitForLastOperation(ProgramTimeout);
338
 
339
  if(status == FLASH_COMPLETE)
340
  {
341
    /* if the previous operation is completed, proceed to program the new first
342
    half word */
343
    FLASH->CR |= CR_PG_Set;
344
 
345
    *(__IO uint16_t*)Address = (uint16_t)Data;
346
    /* Wait for last operation to be completed */
347
    status = FLASH_WaitForLastOperation(ProgramTimeout);
348
 
349
    if(status == FLASH_COMPLETE)
350
    {
351
      /* if the previous operation is completed, proceed to program the new second
352
      half word */
353
      *(__IO uint16_t*)(Address + 2) = Data >> 16;
354
 
355
      /* Wait for last operation to be completed */
356
      status = FLASH_WaitForLastOperation(ProgramTimeout);
357
 
358
      if(status != FLASH_BUSY)
359
      {
360
        /* Disable the PG Bit */
361
        FLASH->CR &= CR_PG_Reset;
362
      }
363
    }
364
    else
365
    {
366
      if (status != FLASH_BUSY)
367
      {
368
        /* Disable the PG Bit */
369
        FLASH->CR &= CR_PG_Reset;
370
      }
371
     }
372
  }
373
  /* Return the Program Status */
374
  return status;
375
}
376
 
377
/**
378
  * @brief  Programs a half word at a specified address.
379
  * @param Address: specifies the address to be programmed.
380
  * @param Data: specifies the data to be programmed.
381
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
382
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
383
  *   FLASH_TIMEOUT.
384
  */
385
FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
386
{
387
  FLASH_Status status = FLASH_COMPLETE;
388
  /* Check the parameters */
389
  assert_param(IS_FLASH_ADDRESS(Address));
390
  /* Wait for last operation to be completed */
391
  status = FLASH_WaitForLastOperation(ProgramTimeout);
392
 
393
  if(status == FLASH_COMPLETE)
394
  {
395
    /* if the previous operation is completed, proceed to program the new data */
396
    FLASH->CR |= CR_PG_Set;
397
 
398
    *(__IO uint16_t*)Address = Data;
399
    /* Wait for last operation to be completed */
400
    status = FLASH_WaitForLastOperation(ProgramTimeout);
401
    if(status != FLASH_BUSY)
402
    {
403
      /* if the program operation is completed, disable the PG Bit */
404
      FLASH->CR &= CR_PG_Reset;
405
    }
406
  }
407
  /* Return the Program Status */
408
  return status;
409
}
410
 
411
/**
412
  * @brief  Programs a half word at a specified Option Byte Data address.
413
  * @param Address: specifies the address to be programmed.
414
  *   This parameter can be 0x1FFFF804 or 0x1FFFF806.
415
  * @param Data: specifies the data to be programmed.
416
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
417
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
418
  *   FLASH_TIMEOUT.
419
  */
420
FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
421
{
422
  FLASH_Status status = FLASH_COMPLETE;
423
  /* Check the parameters */
424
  assert_param(IS_OB_DATA_ADDRESS(Address));
425
  status = FLASH_WaitForLastOperation(ProgramTimeout);
426
  if(status == FLASH_COMPLETE)
427
  {
428
    /* Authorize the small information block programming */
429
    FLASH->OPTKEYR = FLASH_KEY1;
430
    FLASH->OPTKEYR = FLASH_KEY2;
431
    /* Enables the Option Bytes Programming operation */
432
    FLASH->CR |= CR_OPTPG_Set;
433
    *(__IO uint16_t*)Address = Data;
434
 
435
    /* Wait for last operation to be completed */
436
    status = FLASH_WaitForLastOperation(ProgramTimeout);
437
    if(status != FLASH_BUSY)
438
    {
439
      /* if the program operation is completed, disable the OPTPG Bit */
440
      FLASH->CR &= CR_OPTPG_Reset;
441
    }
442
  }
443
  /* Return the Option Byte Data Program Status */
444
  return status;
445
}
446
 
447
/**
448
  * @brief  Write protects the desired pages
449
  * @param FLASH_Pages: specifies the address of the pages to be
450
  *   write protected. This parameter can be:
451
  * @arg For STM32F10Xxx Medium-density devices (FLASH page size equal to 1 KB)
452
  * A value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages124to127
453
  * @arg For STM32F10Xxx High-density devices (FLASH page size equal to 2 KB)
454
  * A value between FLASH_WRProt_Pages0to1 and  FLASH_WRProt_Pages60to61
455
  * or FLASH_WRProt_Pages62to255
456
  * @arg FLASH_WRProt_AllPages
457
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
458
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
459
  *   FLASH_TIMEOUT.
460
  */
461
FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)
462
{
463
  uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
464
 
465
  FLASH_Status status = FLASH_COMPLETE;
466
 
467
  /* Check the parameters */
468
  assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
469
 
470
  FLASH_Pages = (uint32_t)(~FLASH_Pages);
471
  WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);
472
  WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);
473
  WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);
474
  WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);
475
 
476
  /* Wait for last operation to be completed */
477
  status = FLASH_WaitForLastOperation(ProgramTimeout);
478
 
479
  if(status == FLASH_COMPLETE)
480
  {
481
    /* Authorizes the small information block programming */
482
    FLASH->OPTKEYR = FLASH_KEY1;
483
    FLASH->OPTKEYR = FLASH_KEY2;
484
    FLASH->CR |= CR_OPTPG_Set;
485
    if(WRP0_Data != 0xFF)
486
    {
487
      OB->WRP0 = WRP0_Data;
488
 
489
      /* Wait for last operation to be completed */
490
      status = FLASH_WaitForLastOperation(ProgramTimeout);
491
    }
492
    if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
493
    {
494
      OB->WRP1 = WRP1_Data;
495
 
496
      /* Wait for last operation to be completed */
497
      status = FLASH_WaitForLastOperation(ProgramTimeout);
498
    }
499
    if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
500
    {
501
      OB->WRP2 = WRP2_Data;
502
 
503
      /* Wait for last operation to be completed */
504
      status = FLASH_WaitForLastOperation(ProgramTimeout);
505
    }
506
 
507
    if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
508
    {
509
      OB->WRP3 = WRP3_Data;
510
 
511
      /* Wait for last operation to be completed */
512
      status = FLASH_WaitForLastOperation(ProgramTimeout);
513
    }
514
 
515
    if(status != FLASH_BUSY)
516
    {
517
      /* if the program operation is completed, disable the OPTPG Bit */
518
      FLASH->CR &= CR_OPTPG_Reset;
519
    }
520
  }
521
  /* Return the write protection operation Status */
522
  return status;
523
}
524
 
525
/**
526
  * @brief  Enables or disables the read out protection.
527
  *   If the user has already programmed the other option bytes before
528
  *   calling this function, he must re-program them since this
529
  *   function erases all option bytes.
530
  * @param Newstate: new state of the ReadOut Protection.
531
  *   This parameter can be: ENABLE or DISABLE.
532
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
533
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
534
  *   FLASH_TIMEOUT.
535
  */
536
FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
537
{
538
  FLASH_Status status = FLASH_COMPLETE;
539
  /* Check the parameters */
540
  assert_param(IS_FUNCTIONAL_STATE(NewState));
541
  status = FLASH_WaitForLastOperation(EraseTimeout);
542
  if(status == FLASH_COMPLETE)
543
  {
544
    /* Authorizes the small information block programming */
545
    FLASH->OPTKEYR = FLASH_KEY1;
546
    FLASH->OPTKEYR = FLASH_KEY2;
547
    FLASH->CR |= CR_OPTER_Set;
548
    FLASH->CR |= CR_STRT_Set;
549
    /* Wait for last operation to be completed */
550
    status = FLASH_WaitForLastOperation(EraseTimeout);
551
    if(status == FLASH_COMPLETE)
552
    {
553
      /* if the erase operation is completed, disable the OPTER Bit */
554
      FLASH->CR &= CR_OPTER_Reset;
555
      /* Enable the Option Bytes Programming operation */
556
      FLASH->CR |= CR_OPTPG_Set;
557
      if(NewState != DISABLE)
558
      {
559
        OB->RDP = 0x00;
560
      }
561
      else
562
      {
563
        OB->RDP = RDP_Key;
564
      }
565
      /* Wait for last operation to be completed */
566
      status = FLASH_WaitForLastOperation(EraseTimeout);
567
 
568
      if(status != FLASH_BUSY)
569
      {
570
        /* if the program operation is completed, disable the OPTPG Bit */
571
        FLASH->CR &= CR_OPTPG_Reset;
572
      }
573
    }
574
    else
575
    {
576
      if(status != FLASH_BUSY)
577
      {
578
        /* Disable the OPTER Bit */
579
        FLASH->CR &= CR_OPTER_Reset;
580
      }
581
    }
582
  }
583
  /* Return the protection operation Status */
584
  return status;
585
}
586
 
587
/**
588
  * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP /
589
  *   RST_STDBY.
590
  * @param OB_IWDG: Selects the IWDG mode
591
  *   This parameter can be one of the following values:
592
  * @arg OB_IWDG_SW: Software IWDG selected
593
  * @arg OB_IWDG_HW: Hardware IWDG selected
594
  * @param OB_STOP: Reset event when entering STOP mode.
595
  *   This parameter can be one of the following values:
596
  * @arg OB_STOP_NoRST: No reset generated when entering in STOP
597
  * @arg OB_STOP_RST: Reset generated when entering in STOP
598
  * @param OB_STDBY: Reset event when entering Standby mode.
599
  *   This parameter can be one of the following values:
600
  * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
601
  * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
602
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
603
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
604
  *   FLASH_TIMEOUT.
605
  */
606
FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)
607
{
608
  FLASH_Status status = FLASH_COMPLETE;
609
  /* Check the parameters */
610
  assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
611
  assert_param(IS_OB_STOP_SOURCE(OB_STOP));
612
  assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
613
  /* Authorize the small information block programming */
614
  FLASH->OPTKEYR = FLASH_KEY1;
615
  FLASH->OPTKEYR = FLASH_KEY2;
616
 
617
  /* Wait for last operation to be completed */
618
  status = FLASH_WaitForLastOperation(ProgramTimeout);
619
 
620
  if(status == FLASH_COMPLETE)
621
  {
622
    /* Enable the Option Bytes Programming operation */
623
    FLASH->CR |= CR_OPTPG_Set;
624
 
625
    OB->USER = ( OB_IWDG | OB_STOP |OB_STDBY) | (uint16_t)0xF8;
626
 
627
    /* Wait for last operation to be completed */
628
    status = FLASH_WaitForLastOperation(ProgramTimeout);
629
    if(status != FLASH_BUSY)
630
    {
631
      /* if the program operation is completed, disable the OPTPG Bit */
632
      FLASH->CR &= CR_OPTPG_Reset;
633
    }
634
  }
635
  /* Return the Option Byte program Status */
636
  return status;
637
}
638
 
639
/**
640
  * @brief  Returns the FLASH User Option Bytes values.
641
  * @param  None
642
  * @retval : The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
643
  *   and RST_STDBY(Bit2).
644
  */
645
uint32_t FLASH_GetUserOptionByte(void)
646
{
647
  /* Return the User Option Byte */
648
  return (uint32_t)(FLASH->OBR >> 2);
649
}
650
 
651
/**
652
  * @brief  Returns the FLASH Write Protection Option Bytes Register value.
653
  * @param  None
654
  * @retval : The FLASH Write Protection  Option Bytes Register value
655
  */
656
uint32_t FLASH_GetWriteProtectionOptionByte(void)
657
{
658
  /* Return the Falsh write protection Register value */
659
  return (uint32_t)(FLASH->WRPR);
660
}
661
 
662
/**
663
  * @brief  Checks whether the FLASH Read Out Protection Status is set
664
  *   or not.
665
  * @param  None
666
  * @retval : FLASH ReadOut Protection Status(SET or RESET)
667
  */
668
FlagStatus FLASH_GetReadOutProtectionStatus(void)
669
{
670
  FlagStatus readoutstatus = RESET;
671
  if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)
672
  {
673
    readoutstatus = SET;
674
  }
675
  else
676
  {
677
    readoutstatus = RESET;
678
  }
679
  return readoutstatus;
680
}
681
 
682
/**
683
  * @brief  Checks whether the FLASH Prefetch Buffer status is set or not.
684
  * @param  None
685
  * @retval : FLASH Prefetch Buffer Status (SET or RESET).
686
  */
687
FlagStatus FLASH_GetPrefetchBufferStatus(void)
688
{
689
  FlagStatus bitstatus = RESET;
690
 
691
  if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)
692
  {
693
    bitstatus = SET;
694
  }
695
  else
696
  {
697
    bitstatus = RESET;
698
  }
699
  /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
700
  return bitstatus;
701
}
702
 
703
/**
704
  * @brief  Enables or disables the specified FLASH interrupts.
705
  * @param FLASH_IT: specifies the FLASH interrupt sources to be
706
  *   enabled or disabled.
707
  *   This parameter can be any combination of the following values:
708
  * @arg FLASH_IT_ERROR: FLASH Error Interrupt
709
  * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
710
  * @param NewState: new state of the specified Flash interrupts.
711
  *   This parameter can be: ENABLE or DISABLE.
712
  * @retval : None
713
  */
714
void FLASH_ITConfig(uint16_t FLASH_IT, FunctionalState NewState)
715
{
716
  /* Check the parameters */
717
  assert_param(IS_FLASH_IT(FLASH_IT));
718
  assert_param(IS_FUNCTIONAL_STATE(NewState));
719
  if(NewState != DISABLE)
720
  {
721
    /* Enable the interrupt sources */
722
    FLASH->CR |= FLASH_IT;
723
  }
724
  else
725
  {
726
    /* Disable the interrupt sources */
727
    FLASH->CR &= ~(uint32_t)FLASH_IT;
728
  }
729
}
730
 
731
/**
732
  * @brief  Checks whether the specified FLASH flag is set or not.
733
  * @param FLASH_FLAG: specifies the FLASH flag to check.
734
  *   This parameter can be one of the following values:
735
  * @arg FLASH_FLAG_BSY: FLASH Busy flag
736
  * @arg FLASH_FLAG_PGERR: FLASH Program error flag
737
  * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
738
  * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
739
  * @arg FLASH_FLAG_OPTERR:  FLASH Option Byte error flag
740
  * @retval : The new state of FLASH_FLAG (SET or RESET).
741
  */
742
FlagStatus FLASH_GetFlagStatus(uint16_t FLASH_FLAG)
743
{
744
  FlagStatus bitstatus = RESET;
745
  /* Check the parameters */
746
  assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
747
  if(FLASH_FLAG == FLASH_FLAG_OPTERR)
748
  {
749
    if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
750
    {
751
      bitstatus = SET;
752
    }
753
    else
754
    {
755
      bitstatus = RESET;
756
    }
757
  }
758
  else
759
  {
760
   if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
761
    {
762
      bitstatus = SET;
763
    }
764
    else
765
    {
766
      bitstatus = RESET;
767
    }
768
  }
769
  /* Return the new state of FLASH_FLAG (SET or RESET) */
770
  return bitstatus;
771
}
772
 
773
/**
774
  * @brief  Clears the FLASH’s pending flags.
775
  * @param FLASH_FLAG: specifies the FLASH flags to clear.
776
  *   This parameter can be any combination of the following values:
777
  * @arg FLASH_FLAG_BSY: FLASH Busy flag
778
  * @arg FLASH_FLAG_PGERR: FLASH Program error flag
779
  * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
780
  * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
781
  * @retval : None
782
  */
783
void FLASH_ClearFlag(uint16_t FLASH_FLAG)
784
{
785
  /* Check the parameters */
786
  assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
787
 
788
  /* Clear the flags */
789
  FLASH->SR = FLASH_FLAG;
790
}
791
 
792
/**
793
  * @brief  Returns the FLASH Status.
794
  * @param  None
795
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
796
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP or FLASH_COMPLETE
797
  */
798
FLASH_Status FLASH_GetStatus(void)
799
{
800
  FLASH_Status flashstatus = FLASH_COMPLETE;
801
 
802
  if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
803
  {
804
    flashstatus = FLASH_BUSY;
805
  }
806
  else
807
  {
808
    if(FLASH->SR & FLASH_FLAG_PGERR)
809
    {
810
      flashstatus = FLASH_ERROR_PG;
811
    }
812
    else
813
    {
814
      if(FLASH->SR & FLASH_FLAG_WRPRTERR)
815
      {
816
        flashstatus = FLASH_ERROR_WRP;
817
      }
818
      else
819
      {
820
        flashstatus = FLASH_COMPLETE;
821
      }
822
    }
823
  }
824
  /* Return the Flash Status */
825
  return flashstatus;
826
}
827
 
828
/**
829
  * @brief  Waits for a Flash operation to complete or a TIMEOUT to occur.
830
  * @param Timeout: FLASH progamming Timeout
831
  * @retval : FLASH Status: The returned value can be: FLASH_BUSY,
832
  *   FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
833
  *   FLASH_TIMEOUT.
834
  */
835
FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
836
{
837
  FLASH_Status status = FLASH_COMPLETE;
838
 
839
  /* Check for the Flash Status */
840
  status = FLASH_GetStatus();
841
  /* Wait for a Flash operation to complete or a TIMEOUT to occur */
842
  while((status == FLASH_BUSY) && (Timeout != 0x00))
843
  {
844
    delay();
845
    status = FLASH_GetStatus();
846
    Timeout--;
847
  }
848
  if(Timeout == 0x00 )
849
  {
850
    status = FLASH_TIMEOUT;
851
  }
852
  /* Return the operation status */
853
  return status;
854
}
855
 
856
/**
857
  * @brief  Inserts a time delay.
858
  * @param  None
859
  * @retval : None
860
  */
861
static void delay(void)
862
{
863
  __IO uint32_t i = 0;
864
  for(i = 0xFF; i != 0; i--)
865
  {
866
  }
867
}
868
 
869
/**
870
  * @}
871
  */
872
 
873
/**
874
  * @}
875
  */
876
 
877
/**
878
  * @}
879
  */
880
 
881
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.