-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlash.c
180 lines (151 loc) · 4.72 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/***
* Excerpted from "Test-Driven Development for Embedded C",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/jgade for more book information.
***/
/*- ------------------------------------------------------------------ -*/
/*- Copyright (c) James W. Grenning -- All Rights Reserved -*/
/*- For use by owners of Test-Driven Development for Embedded C, -*/
/*- and attendees of Renaissance Software Consulting, Co. training -*/
/*- classes. -*/
/*- -*/
/*- Available at http://pragprog.com/titles/jgade/ -*/
/*- ISBN 1-934356-62-X, ISBN13 978-1-934356-62-3 -*/
/*- -*/
/*- Authorized users may use this source code in your own -*/
/*- projects, however the source code may not be used to -*/
/*- create training material, courses, books, articles, and -*/
/*- the like. We make no guarantees that this source code is -*/
/*- fit for any purpose. -*/
/*- -*/
/*- www.renaissancesoftware.net [email protected] -*/
/*- ------------------------------------------------------------------ -*/
#include "Flash.h"
#include "IO.h"
#include "m28w160ect.h"
#include "MicroTime.h"
#define FLASH_WRITE_TIMEOUT_IN_MICROSECONDS 5000
void Flash_Create(void)
{
}
void Flash_Destroy(void)
{
}
static int writeError(int status)
{
IO_Write(CommandRegister, Reset);
if (status & VppErrorBit)
return FLASH_VPP_ERROR;
else if (status & ProgramErrorBit)
return FLASH_PROGRAM_ERROR;
else if (status & BlockProtectionErrorBit)
return FLASH_PROTECTED_BLOCK_ERROR;
else
return FLASH_UNKNOWN_PROGRAM_ERROR;
}
int Flash_Write(ioAddress offset, ioData data)
{
ioData status = 0;
uint32_t timestamp = MicroTime_Get();
IO_Write(CommandRegister, ProgramCommand);
IO_Write(offset, data);
status = IO_Read(StatusRegister);
while ((status & ReadyBit) == 0)
{
if (MicroTime_Get() - timestamp >= FLASH_WRITE_TIMEOUT_IN_MICROSECONDS)
return FLASH_TIMEOUT_ERROR;
status = IO_Read(StatusRegister);
}
if (status != ReadyBit)
return writeError(status);
if (data != IO_Read(offset))
return FLASH_READ_BACK_ERROR;
return FLASH_SUCCESS;
}
#if 0
int Flash_Write(ioAddress address, ioData data)
{
return -1;
}
#endif
#if 0
int Flash_Write(ioAddress address, ioData data)
{
IO_Write(0x40, 0);
return -1;
}
#endif
#if 0
int Flash_Write(ioAddress address, ioData data)
{
IO_Write(0, 0x40);
IO_Write(address, data);
IO_Read(0);
IO_Read(address);
return FLASH_SUCCESS;
}
#endif
#if 0
int Flash_Write(ioAddress address, ioData data)
{
IO_Write(CommandRegister, ProgramCommand);
IO_Write(address, data);
IO_Read(StatusRegister);
IO_Read(address);
return FLASH_SUCCESS;
}
#endif
#if 0
int Flash_Write(ioAddress address, ioData data)
{
ioData status = 0;
IO_Write(CommandRegister, ProgramCommand);
IO_Write(address, data);
while ((status & ReadyBit) == 0)
status = IO_Read(StatusRegister);
IO_Read(address);
return FLASH_SUCCESS;
}
#endif
#if 0
int Flash_Write(ioAddress offset, ioData data)
{
ioData status = 0;
IO_Write(CommandRegister, ProgramCommand);
IO_Write(offset, data);
while ((status & ReadyBit) == 0)
status = IO_Read(StatusRegister);
if (status != ReadyBit)
{
IO_Write(CommandRegister, Reset);
if (status & VppErrorBit)
return FLASH_VPP_ERROR;
else if (status & ProgramErrorBit)
return FLASH_PROGRAM_ERROR;
else if (status & BlockProtectionErrorBit)
return FLASH_PROTECTED_BLOCK_ERROR;
else
return FLASH_UNKNOWN_PROGRAM_ERROR;
}
IO_Read(address);
return FLASH_SUCCESS;
}
#endif
#if 0
int Flash_Write(ioAddress offset, ioData data)
{
ioData status = 0;
IO_Write(CommandRegister, ProgramCommand);
IO_Write(offset, data);
while ((status & ReadyBit) == 0)
status = IO_Read(StatusRegister);
if (status != ReadyBit)
return writeError(status);
if (data != IO_Read(offset))
return FLASH_READ_BACK_ERROR;
return FLASH_SUCCESS;
}
#endif