Flipklok
 
Loading...
Searching...
No Matches
storeLoadMechanicalTime.c
Go to the documentation of this file.
1/*
2 * storeLoadMechanicalTime.c
3 *
4 * Created on: 1 apr. 2025
5 */
10
12
13#include <msp430.h>
14
15#define INFO_SEGMENT_ADDRESS 0x1000 // Info Memory segment D, see page 319 of SLAU144K
16
17//void storeTime(uint32_t data)
18//{
19// __no_operation();
20// uint16_t *flashPtr = (uint16_t *)INFO_SEGMENT_ADDRESS; // Pointer to flash memory (word-aligned)
21//
22// // Step 1: Unlock Flash & Erase Segment (Flash requires an erase before write)
23// FCTL3 = FWKEY; // Clear lock bit
24// FCTL1 = FWKEY + ERASE; // Set erase mode
25// *flashPtr = 0; // Dummy write to erase segment
26//
27// // Wait for erase to complete
28// while (FCTL3 & BUSY); // Wait until flash is no longer busy
29//
30// // Step 2: Enable Write Mode
31// FCTL1 = FWKEY + WRT;
32//
33// // Step 3: Write 32-bit data (two 16-bit writes)
34// flashPtr[0] = (uint16_t)(data & 0xFFFF); // Write lower 16 bits
35// flashPtr[1] = (uint16_t)((data >> 16) & 0xFFFF); // Write upper 16 bits
36//
37// // Wait for the write to complete
38// while (FCTL3 & BUSY); // Wait until flash is no longer busy
39//
40// // Step 4: Lock Flash
41// FCTL1 = FWKEY; // Disable write mode
42// FCTL3 = FWKEY + LOCK; // Lock flash
43//}
44//
45//uint32_t loadTime()
46//{
47// uint16_t *flashPtr = (uint16_t *)INFO_SEGMENT_ADDRESS;
48//
49// // Read back 32-bit value from flash (two 16-bit reads that are concatenated)
50// volatile uint32_t data = (uint32_t)flashPtr[0] | ((uint32_t)flashPtr[1] << 16);
51// __no_operation();
52// return data;
53//}
54
55
56
62void write_SegD(uint32_t value)
63{
64 uint32_t *Flash_ptr; // Flash pointer
65 unsigned int i;
66
67 Flash_ptr = (uint32_t *) 0x1000; // Initialize Flash pointer (for 32-bit writes)
68 FCTL1 = FWKEY + ERASE; // Set Erase bit
69 FCTL3 = FWKEY; // Clear Lock bit
70 *Flash_ptr = 0; // Dummy write to erase Flash segment
71
72 FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
73
74 *Flash_ptr--;
75 for (i = 0; i < 8; i++) // Write 32-bit value to flash (16 writes per 64 bytes)
76 {
77 *Flash_ptr = value; // Write 32-bit value to flash
78 *Flash_ptr++;
79 }
80
81// *Flash_ptr = value; // Write 32-bit value to flash
82
83 FCTL1 = FWKEY; // Clear WRT bit
84 FCTL3 = FWKEY + LOCK; // Set LOCK bit
85}
86
92uint32_t load_from_flash(void)
93{
94 uint32_t *Flash_ptr; // Pointer to read from Flash memory
95 uint32_t loaded_value;
96
97 Flash_ptr = (uint32_t *) 0x1000; // Point to the start of Flash segment D
98 loaded_value = *Flash_ptr; // Read the 32-bit value from Flash
99
100 return loaded_value;
101}
102
uint32_t loaded_value
Definition main.c:33
uint32_t load_from_flash(void)
Function to load a 32-bit value from Flash memory segment D used for restoring the mechanical time on...
void write_SegD(uint32_t value)
Function to write a 32-bit value to Flash segment D for storing the mechanical time,...