-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.c
75 lines (59 loc) · 1.88 KB
/
flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <msp430.h>
#include <flash.h>
void init_flash(void)
{
// Configure Flash Timing Generator with MCLK/3
FCTL2 = FWKEY + FSSEL0 + FN1;
}
unsigned char flash_write(char *data_ptr, unsigned int data_size, unsigned int offset)
{
char *flash_data_partition_ptr;
unsigned int index;
unsigned char ret_val = 0;
if((data_ptr != 0) && (data_size != 0) && ((data_size + offset) < 64))
{
//set flash module address
flash_data_partition_ptr = (char *) (FLASH_BLOCK_ADDRESS + offset);
//first erase segment
FCTL1 = FWKEY + ERASE;
FCTL3 = FWKEY;
*flash_data_partition_ptr = 0;
//set flash part for write mode
FCTL1 = FWKEY + WRT;
//write data to the block
for (index=0; index<data_size; index++)
{
*flash_data_partition_ptr++ = data_ptr[index];
}
//clear write bit and lock segment
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
ret_val = 1;
}
return ret_val;
}
unsigned char flash_read(char *data_ret_ptr, unsigned int data_size, unsigned int offset)
{
char *flash_data_partition_ptr;
unsigned int index;
unsigned char ret_val = 0;
//verify pointer is not null and size range
if((data_ret_ptr != 0) && (data_size > 0) && ((data_size + offset) < 64))
{
//set flash module address
flash_data_partition_ptr = (char *) (FLASH_BLOCK_ADDRESS + offset);
//read data
for (index=0; index<data_size; index++)
{
data_ret_ptr[index] = *flash_data_partition_ptr++;
}
ret_val = 1;
}
return ret_val;
}
char flash_read_byte(unsigned int offset)
{
char *flash_data_partition_ptr = (char *) (FLASH_BLOCK_ADDRESS + offset);
char ret_byte = *flash_data_partition_ptr;
return ret_byte;
}