-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataflash_flash.c
executable file
·150 lines (115 loc) · 2.96 KB
/
dataflash_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "dataflash_flash.h"
#define FLASH_DEV "test"
static uint8_t msg_buffer[2][PAGESIZE]; //0:write 1:read
static int flash_fd;
struct msg_data* Init()
{
if (flash_fd == 0) {
flash_fd = open(FLASH_DEV, O_RDWR | O_CREAT | O_TRUNC, 0777);
if (flash_fd == -1) {
printf("DataFlash_Flash open failed.\n");
return NULL;
}
}
#if 1
uint8_t buff[STORAGE_SIZE];
memset(buff, 0xff, STORAGE_SIZE);
write(flash_fd, buff, STORAGE_SIZE);
#endif
struct msg_data *msg = (struct msg_data *)malloc(sizeof(struct msg_data));
if(msg == NULL)
{
printf("DataFlash_Flash open failed.\n");
return NULL;
}
msg->df_PageSize = PAGESIZE;
msg->df_NumPages = NUMPAGES - 1;
return msg;
}
void BlockRead(uint16_t IntPageAdr, void *pBuffer, uint16_t size)
{
// printf("BlockRead IntPageAdr %d size %d\n", IntPageAdr, size);
assert(IntPageAdr <= PAGESIZE);
memset(pBuffer, 0, size);
memcpy(pBuffer, &msg_buffer[1][IntPageAdr], size);
return;
}
void BlockWrite(uint16_t IntPageAdr, const void *pHeader, uint8_t hdr_size,
const void *pBuffer, uint16_t size)
{
printf("BlockWrite IntPageAdr %d hdr_size %d size %d\n", IntPageAdr, hdr_size, size);
assert(IntPageAdr <= PAGESIZE);
if (hdr_size) {
memset(msg_buffer[0], 0, PAGESIZE);
memcpy(msg_buffer[0], pHeader, hdr_size);
}
memcpy(&msg_buffer[0][IntPageAdr+hdr_size], pBuffer, size);
#if 1
int i = 0;
printf("BlockWrite pBuffer IntPageAdr %d size+hdr_size %d:", IntPageAdr, size+hdr_size);
for(i = IntPageAdr; i < (IntPageAdr + (size + hdr_size)); i++)
{
printf("%d ", msg_buffer[0][i]);
}
printf("\n");
#endif
}
void BufferToPage (uint16_t PageAdr, unsigned char wait)
{
printf("BufferToPage here PageAdr %d.\n", PageAdr);
assert(PageAdr <= NUMPAGES);
PageAdr -= 1;
off_t ofs = PageAdr * PAGESIZE;
if (flash_fd == -1) {
return;
}
if (lseek(flash_fd, ofs, SEEK_SET) != ofs) {
printf("BufferToPage lseek err.\n");
return;
}
#if 1
int i = 0;
printf("DataFlash_Flash BufferToPage:");
for(i = 0; i < PAGESIZE; i++)
{
printf("%d ", msg_buffer[0][i]);
}
printf("\n");
#endif
if (write(flash_fd, msg_buffer[0], PAGESIZE) != PAGESIZE)
{
printf("BufferToPage write err.\n");
return;
}
return;
}
void PageToBuffer(uint16_t PageAdr)
{
// printf("PageToBuffer PageAdr %d.\n", PageAdr);
assert(PageAdr <= NUMPAGES);
PageAdr -= 1;
off_t ofs = PageAdr * PAGESIZE;
memset(msg_buffer[1], 0, PAGESIZE);
if (lseek(flash_fd, ofs, SEEK_SET) != ofs) {
printf("PageToBuffer lseek err.\n");
return;
}
if (read(flash_fd, msg_buffer[1], PAGESIZE) != PAGESIZE)
{
printf("PageToBuffer read err.\n");
return;
}
#if 0
int i = 0;
printf("DataFlash_Flash PageToBuffer:");
for(i = 0; i < PAGESIZE; i++)
{
printf("%d ", msg_buffer[1][i]);
}
printf("\n");
#endif
}
void WaitReady()
{
return;
}