2020-2021 Sunseeker Telemetry and Lighting System
flashctl.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // flashctl.c - Driver for the flashctl Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_FLASH__
17 #include "flashctl.h"
18 
19 #include <assert.h>
20 
21 void FlashCtl_eraseSegment ( uint8_t *flash_ptr){
22  //Clear Lock bit
23  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY;
24 
25  //Set Erase bit
26  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY + ERASE;
27 
28  //Dummy write to erase Flash seg
29  *flash_ptr = 0;
30 
31  //test busy
32  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY) ;
33 
34  //Clear ERASE bit
35  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY;
36 
37  //Set LOCK bit
38  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY + LOCK;
39 }
40 
41 void FlashCtl_eraseBank ( uint8_t *flash_ptr){
42  uint16_t interruptState;
43 
44  //Clear Lock bit
45  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY;
46 
47  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY);
48 
49  //FLASH34 errata: no concurrent access to flash bank while erasing
50  interruptState = __get_interrupt_state();
51  __disable_interrupt();
52  __no_operation();
53 
54  //Set MERAS bit
55  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY + MERAS;
56 
57  //Dummy write to erase Flash seg
58  *flash_ptr = 0;
59 
60  //test busy
61  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY);
62 
63  //FLASH34 errata
64  //Re-enable interrupt state to whatever it was before
65  if (interruptState & GIE)
66  {
67  __enable_interrupt();
68  }
69 
70  //Clear MERAS bit
71  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY;
72 
73  //Set LOCK bit
74  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY + LOCK;
75 }
76 
77 void FlashCtl_performMassErase ( uint8_t *flash_ptr){
78  //Clear Lock bit
79  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY;
80 
81  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY) ;
82 
83  //Set MERAS bit
84  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY + MERAS + ERASE;
85 
86  //Dummy write to erase Flash seg
87  *flash_ptr = 0;
88 
89  //test busy
90  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY) ;
91 
92  //Clear MERAS bit
93  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY;
94 
95  //Set LOCK bit
96  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY + LOCK;
97 }
98 
99 bool FlashCtl_performEraseCheck (uint8_t *flash_ptr,
100  uint16_t numberOfBytes
101  )
102 {
103  uint16_t i;
104 
105  for (i = 0; i < numberOfBytes; i++)
106  {
107  //was erasing successfull?
108  if ((*(flash_ptr + i)) != 0xFF){
109  return ( STATUS_FAIL) ;
110  }
111  }
112  return ( STATUS_SUCCESS) ;
113 }
114 
115 void FlashCtl_write8 (uint8_t *data_ptr,
116  uint8_t *flash_ptr,
117  uint16_t count
118  )
119 {
120  //Clear Lock bit
121  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY;
122 
123  //Enable byte/word write mode
124  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY + WRT;
125 
126  while (count > 0)
127  {
128  //test busy
129  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY) ;
130 
131  //Write to Flash
132  *flash_ptr++ = *data_ptr++;
133  count--;
134  }
135 
136  //Clear WRT bit
137  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY;
138 
139  //Set LOCK bit
140  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY + LOCK;
141 }
142 
143 void FlashCtl_write16 (uint16_t *data_ptr,
144  uint16_t *flash_ptr,
145  uint16_t count
146  )
147 {
148  //Clear Lock bit
149  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY;
150 
151  //Enable byte/word write mode
152  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY + WRT;
153 
154  while (count > 0)
155  {
156  //test busy
157  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY) ;
158 
159  //Write to Flash
160  *flash_ptr++ = *data_ptr++;
161  count--;
162  }
163 
164  //Clear WRT bit
165  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY;
166 
167  //Set LOCK bit
168  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY + LOCK;
169 }
170 
171 void FlashCtl_write32 (uint32_t *data_ptr,
172  uint32_t *flash_ptr,
173  uint16_t count
174  )
175 {
176  //Clear Lock bit
177  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY;
178 
179  //Enable long-word write
180  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY + BLKWRT;
181 
182  while (count > 0)
183  {
184  //test busy
185  while (HWREG8(FLASH_BASE + OFS_FCTL3) & BUSY) ;
186 
187  //Write to Flash
188  *flash_ptr++ = *data_ptr++;
189 
190  count--;
191  }
192 
193  //Clear BLKWRT bit
194  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY;
195 
196  //Set LOCK bit
197  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY + LOCK;
198 }
199 
200 void FlashCtl_fillMemory32 (uint32_t value,
201  uint32_t *flash_ptr,
202  uint16_t count
203  )
204 {
205  //Clear Lock bit
206  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY;
207 
208  //Enable long-word write
209  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY + BLKWRT;
210 
211  //test busy
212  while (count > 0)
213  {
214  while ((HWREG8(FLASH_BASE + OFS_FCTL3)) & BUSY) ;
215 
216  //Write to Flash
217  *flash_ptr++ = value;
218 
219  count--;
220  }
221 
222  //Clear BLKWRT bit
223  HWREG16(FLASH_BASE + OFS_FCTL1) = FWKEY;
224 
225  //Set LOCK bit
226  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY + LOCK;
227 }
228 
229 uint8_t FlashCtl_getStatus (uint8_t mask
230  )
231 {
232  return ((HWREG8(FLASH_BASE + OFS_FCTL3) & mask ));
233 }
234 
235 void FlashCtl_lockInfoA (void)
236 {
237  //Disable global interrupts while doing RMW operation on LOCKA bit
238  uint16_t gieStatus;
239  gieStatus = __get_SR_register() & GIE; //Store current SR register
240  __disable_interrupt(); //Disable global interrupt
241 
242  //Set the LOCKA bit in FCTL3.
243  //Since LOCKA toggles when you write a 1 (and writing 0 has no effect),
244  //read the register, XOR with LOCKA mask, mask the lower byte
245  //and write it back.
246  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY
247  + ((HWREG16(FLASH_BASE + OFS_FCTL3) ^ LOCKA) & 0xFF);
248 
249  //Reinstate SR register to restore global interrupt enable status
250  __bis_SR_register(gieStatus);
251 }
252 
253 void FlashCtl_unlockInfoA (void)
254 {
255  //Disable global interrupts while doing RMW operation on LOCKA bit
256  uint16_t gieStatus;
257  gieStatus = __get_SR_register() & GIE; //Store current SR register
258  __disable_interrupt(); //Disable global interrupt
259 
260  //Clear the LOCKA bit in FCTL3.
261  //Since LOCKA toggles when you write a 1 (and writing 0 has no effect),
262  //read the register, mask the lower byte, and write it back.
263  HWREG16(FLASH_BASE + OFS_FCTL3) = FWKEY
264  + (HWREG16(FLASH_BASE + OFS_FCTL3) & 0xFF);
265 
266  //Reinstate SR register to restore global interrupt enable status
267  __bis_SR_register(gieStatus);
268 }
269 
270 
271 #endif
272 //*****************************************************************************
273 //
276 //
277 //*****************************************************************************
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_FAIL
Definition: hw_memmap.h:23
#define STATUS_SUCCESS
Definition: hw_memmap.h:22