-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvsinternal.c
127 lines (97 loc) · 3.97 KB
/
nvsinternal.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
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
/* Driver Header files */
#include <ti/display/Display.h>
#include <ti/drivers/NVS.h>
/* Driver configuration */
#include "ti_drivers_config.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
#define FOOTER "=================================================="
#define CHUNK 250
static char buffer[CHUNK]; //Buffer placed in RAM to hold bytes read from non-volatile storage.
//static const char signature[1024] = {"SimpleLink 1234567890 Non-Volatile Storage"};
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
FILE *fp;
// char fileBuffer[250];
// char signature[ARRAY_SIZE(buffer)];
size_t nread;
fp = fopen("/home/pramodh/rdnvs.txt", "r");
fseek(fp, 0, SEEK_END); //read file size
int totalbytes = ftell(fp);
fseek(fp, 0L, SEEK_SET);//reset pointer
// fgets(fileBuffer, ARRAY_SIZE(buffer), (FILE*)fp);
// memcpy(signature, fileBuffer,strlen(fileBuffer)+1);
// memset(fileBuffer, ARRAY_SIZE(buffer), ARRAY_SIZE(fileBuffer) ); //Clear the contents of fileBuffer
NVS_Handle nvsHandle;
NVS_Attrs regionAttrs;
NVS_Params nvsParams;
Display_Handle displayHandle;
Display_init();
NVS_init();
displayHandle = Display_open(Display_Type_UART, NULL);
if (displayHandle == NULL)
{
/* Display_open() failed */
while (1);
}
NVS_Params_init(&nvsParams);
nvsHandle = NVS_open(CONFIG_NVSINTERNAL, &nvsParams);
if (nvsHandle == NULL)
{
Display_printf(displayHandle, 0, 0, "NVS_open() failed.");
return (NULL);
}
//////////////////////Loop Start Here////////////////////////////////
Display_printf(displayHandle, 0, 0, "\n");
int segment = totalbytes/sizeof(buffer);
int start=0;
int remain;
do{
NVS_getAttrs(nvsHandle, ®ionAttrs);
remain = (totalbytes - start);
start = ftell(fp);
if(remain > 0)
{
Display_printf(displayHandle, 0, 0, FOOTER);
Display_printf(displayHandle, 0, 0, "Total bytes: %ibytes", totalbytes);
Display_printf(displayHandle, 0, 0, "Region Base Address: 0x%x",regionAttrs.regionBase);
Display_printf(displayHandle, 0, 0, "Sector Size: 0x%x",regionAttrs.sectorSize);
Display_printf(displayHandle, 0, 0, "Region Size: 0x%x",regionAttrs.regionSize);
Display_printf(displayHandle, 0, 0, "Writing signature to flash...\n");
Display_printf(displayHandle, 0, 0, "starting at: %i byte \t", (start+1));
Display_printf(displayHandle, 0, 0, "remains: %i bytes", remain);
while ((nread = fread(buffer, (start+1), sizeof buffer, fp)) > 0)
Display_printf(displayHandle, 0, 0, "fread %i byte \t", (start+1));
NVS_write(nvsHandle, start, (void *) buffer, sizeof(buffer),NVS_WRITE_ERASE | NVS_WRITE_POST_VERIFY);
NVS_read(nvsHandle, start, (void *) buffer, sizeof(buffer));
start = start + sizeof(buffer);
fseek(fp,(start),SEEK_SET);
/* Write signature directly from the NVS region to the UART console. */
Display_printf(displayHandle, 0, 0, "%s", regionAttrs.regionBase);
Display_printf(displayHandle, 0, 0, "\n===== Elements =====\n");
//Display_printf(displayHandle, 0, 0, "Index\tFile\tNVS");
Display_printf(displayHandle, 0, 0, "Index\tNVS");
int i;
for(i=0; i < ARRAY_SIZE(buffer); i++)
{
// Display_printf(displayHandle, 0, 0, "%d\t%c\t%c", i, signature[i], buffer[i]);
Display_printf(displayHandle, 0, 0, "%d\t%c", i, buffer[i]);
}
// Display_printf(displayHandle, 0, 0, "Erasing flash sector...");
// NVS_erase(nvsHandle, 0, regionAttrs.sectorSize);
// NVS_close(nvsHandle);
// Display_printf(displayHandle, 0, 0, "Reset the device.");
Display_printf(displayHandle, 0, 0, FOOTER);
} //if end here from line 82
} while(remain > 0);
fclose(fp);
return (NULL);
}