IoT Supervisor 0.0
Loading...
Searching...
No Matches
EFM8BB1_FlashPrimitives.c
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2// EFM8BB1_FlashPrimitives.c
3//-----------------------------------------------------------------------------
4// Copyright 2014 Silicon Laboratories, Inc.
5// http://developer.silabs.com/legal/version/v11/Silicon_Labs_Software_License_Agreement.txt
6//
7// Program Description:
8//
9// This program contains several useful utilities for writing and updating
10// flash memory.
11//
12// Target: EFM8BB1
13// Tool chain: Keil C51
14//
15// Release 1.0
16// -Initial Revision (ST)
17// -10 OCT 2014
18//
19
20//-----------------------------------------------------------------------------
21// Includes
22//-----------------------------------------------------------------------------
23#include <SI_EFM8BB1_Register_Enums.h>
25
26//-----------------------------------------------------------------------------
27// FLASH_ByteWrite
28//-----------------------------------------------------------------------------
29//
30// Return Value : None
31// Parameters :
32// 1) FLADDR addr - address of the byte to write to
33// valid range is from 0x0000 to 0x1FFF for 8 kB devices
34// valid range is from 0x0000 to 0x0FFF for 4 kB devices
35// valid range is from 0x0000 to 0x07FF for 2 kB devices
36// 2) char byte - byte to write to flash.
37//
38// This routine writes <byte> to the linear flash address <addr>.
39//
40//-----------------------------------------------------------------------------
41void FLASH_ByteWrite (FLADDR addr, uint8_t byte)
42{
43 bool EA_SAVE = IE_EA; // Preserve IE_EA
44 SI_VARIABLE_SEGMENT_POINTER(pwrite, uint8_t, SI_SEG_XDATA); // Flash write pointer
45
46 IE_EA = 0; // Disable interrupts
47
48 VDM0CN = 0x80; // Enable VDD monitor
49
50 RSTSRC = 0x02; // Enable VDD monitor as a reset source
51
52 pwrite = (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_XDATA)) addr;
53
54 FLKEY = 0xA5; // Key Sequence 1
55 FLKEY = 0xF1; // Key Sequence 2
56 PSCTL |= 0x01; // PSWE = 1 which enables writes
57
58 VDM0CN = 0x80; // Enable VDD monitor
59
60 RSTSRC = 0x02; // Enable VDD monitor as a reset source
61 *pwrite = byte; // Write the byte
62
63 PSCTL &= ~0x01; // PSWE = 0 which disable writes
64
65 IE_EA = EA_SAVE; // Restore interrupts
66}
67
68//-----------------------------------------------------------------------------
69// FLASH_ByteRead
70//-----------------------------------------------------------------------------
71//
72// Return Value :
73// uint8_t - byte read from flash
74// Parameters :
75// 1) FLADDR addr - address of the byte to read to
76// valid range is from 0x0000 to 0x1FFF for 8 kB devices
77// valid range is from 0x0000 to 0x0FFF for 4 kB devices
78// valid range is from 0x0000 to 0x07FF for 2 kB devices
79//
80// This routine reads a <byte> from the linear flash address <addr>.
81//
82//-----------------------------------------------------------------------------
83uint8_t FLASH_ByteRead (FLADDR addr)
84{
85 bool EA_SAVE = IE_EA; // Preserve IE_EA
86 SI_VARIABLE_SEGMENT_POINTER(pread, uint8_t, const SI_SEG_CODE); // Flash read pointer
87 uint8_t byte;
88
89 IE_EA = 0; // Disable interrupts
90
91 pread = (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, const SI_SEG_CODE)) addr;
92
93 byte = *pread; // Read the byte
94
95 IE_EA = EA_SAVE; // Restore interrupts
96
97 return byte;
98}
99
100//-----------------------------------------------------------------------------
101// FLASH_PageErase
102//-----------------------------------------------------------------------------
103//
104// Return Value : None
105// Parameters :
106// 1) FLADDR addr - address of any byte in the page to erase
107// valid range is from 0x0000 to 0x1FFF for 8 kB devices
108// valid range is from 0x0000 to 0x0FFF for 4 kB devices
109// valid range is from 0x0000 to 0x07FF for 2 kB devices
110//
111// This routine erases the flash page containing the linear flash address
112// <addr>. Note that the page of flash page containing the Lock Byte cannot be
113// erased if the Lock Byte is set.
114//
115//-----------------------------------------------------------------------------
117{
118 bool EA_SAVE = IE_EA; // Preserve IE_EA
119 SI_VARIABLE_SEGMENT_POINTER(pwrite, uint8_t, SI_SEG_XDATA); // Flash write pointer
120
121 IE_EA = 0; // Disable interrupts
122
123 VDM0CN = 0x80; // Enable VDD monitor
124
125 RSTSRC = 0x02; // Enable VDD monitor as a reset source
126
127 pwrite = (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_XDATA)) addr;
128
129 FLKEY = 0xA5; // Key Sequence 1
130 FLKEY = 0xF1; // Key Sequence 2
131 PSCTL |= 0x03; // PSWE = 1; PSEE = 1
132
133 VDM0CN = 0x80; // Enable VDD monitor
134
135 RSTSRC = 0x02; // Enable VDD monitor as a reset source
136 *pwrite = 0; // Initiate page erase
137
138 PSCTL &= ~0x03; // PSWE = 0; PSEE = 0
139
140 IE_EA = EA_SAVE; // Restore interrupts
141}
uint8_t FLASH_ByteRead(FLADDR addr)
void FLASH_ByteWrite(FLADDR addr, uint8_t byte)
void FLASH_PageErase(FLADDR addr)
uint16_t FLADDR