-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
361 lines (315 loc) · 8.12 KB
/
Copy pathmain.cpp
File metadata and controls
361 lines (315 loc) · 8.12 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "pico/stdlib.h"
#include <stdio.h>
// CONTROLS PIN
#define Read_Beasy_2 0 // R/B2/#^3 -->PIN6
#define Read_Beasy_1 1 // R/B/#^ ---->PIN7
#define Read_Enable 2 // RE# ------->PIN8
#define Chip_Enable_1 3 // CE# ------->PIN9
#define Chip_Enable_2 4 // CE2#^3----->PIN10
#define Command_Latch_Enable 5 // CLE ------->PIN16
#define Address_Latch_Enable 6 // ALE ------->PIN17
#define Write_Enable 7 // WE -------->PIN18
#define Write_Protect 8 // WP -------->PIN19
// DATA PIN
#define IO_0 16
#define IO_1 17
#define IO_2 18
#define IO_3 19
#define IO_4 20
#define IO_5 21
#define IO_6 22
#define IO_7 26
#define UART_ID uart0
#define BAUD_RATE 115200
const uint data_bus_pins[8] = {IO_0, IO_1, IO_2, IO_3, IO_4, IO_5, IO_6, IO_7};
// const uint data_bus_pins[8] = {IO_7, IO_6, IO_5, IO_4, IO_3, IO_2, IO_1, IO_0};
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
void Nand_Flash_Gpio_Init(void);
void Enable_Address_Latch_Pin(void);
void Disable_Address_Latch_Pin(void);
/*ACTIVE HIGH
Loads an address from I/O[7:0] into the address register
*/
void Chip_Pin_Enable(void);
void Chip_Pin_Disable(void);
/* ACTIVE LOW
Enables or disables one or more die (LUNs) in a target*/
void Enable_Command_Latch_Pin(void);
void Disable_Command_Latch_Pin(void);
/*ACTIVE HIGH
Loads a command from I/O[7:0] into the command register*/
void Enable_Read_Pin(void);
void Disable_Read_Pin(void);
/* ACTIVE LOW
Transfers serial data from the NAND Flash to the host system*/
void Enable_Write_Pin(void);
void Disable_Write_Pin(void);
/*ACTIVE LOW
Transfers commands, addresses, and serial data from the host system to the
NAND Flash.*/
void Enable_Write_Protect_Pin(void);
void Disable_Write_Protect_Pin(void);
/* ACTIVE LOW
Enables or disables array PROGRAM and ERASE operations.
*/
void Enable_Read_Busy_Pin(void);
void Disable_Read_Busy_Pin(void);
/*ACTIVE LOW
An open-drain, active-low output that requires an external pull-up resistor.
This signal indicates target array activity.
*/
void Set_Data_Bus_As_Input();
void Set_Data_Bus_As_Output();
uint8_t Read_Data_Bus();
void Write_Data_Bus(uint8_t Data);
// READ WRITE OPERATION FUNCTIONS
uint8_t Read_Data(void);
void Write_Data(uint8_t Data);
void NAND_Idle_Bus(void);
void nandSendCommand(uint8_t cmd, bool last = true);
void nandReadSignature(void);
void nandEnable(bool e);
int main()
{
stdio_init_all();
// uart_init(UART_ID, BAUD_RATE);
while (true)
{
while(getchar_timeout_us(10) != '/');
printf("INITIALIZING NAND \n");
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
Nand_Flash_Gpio_Init();
NAND_Idle_Bus();
nandEnable(true);
nandReadSignature();
gpio_put(LED_PIN, 1);
sleep_ms(250);
gpio_put(LED_PIN, 0);
sleep_ms(250);
// printf("IN VOID LOOP \n");
}
}
void Nand_Flash_Gpio_Init(void)
{
// INIT GPIO INTENDED TO BE USED
gpio_init(Read_Beasy_2);
gpio_init(Read_Beasy_1);
gpio_init(Read_Enable);
gpio_init(Chip_Enable_1);
gpio_init(Chip_Enable_2);
gpio_init(Command_Latch_Enable);
gpio_init(Address_Latch_Enable);
gpio_init(Write_Enable);
gpio_init(Write_Protect);
// SET INPUT/OUTPUT DIRECTION OF COMMAND PINS
gpio_set_dir(Read_Beasy_2, GPIO_IN);
gpio_set_dir(Read_Beasy_1, GPIO_IN);
gpio_pull_up(Read_Beasy_1);
gpio_set_dir(Read_Enable, GPIO_OUT);
gpio_set_dir(Chip_Enable_1, GPIO_OUT);
gpio_set_dir(Chip_Enable_2, GPIO_OUT);
gpio_set_dir(Command_Latch_Enable, GPIO_OUT);
gpio_set_dir(Address_Latch_Enable, GPIO_OUT);
gpio_set_dir(Write_Enable, GPIO_OUT);
gpio_set_dir(Write_Protect, GPIO_OUT);
// INITIALIZE IO GPIO
gpio_init(IO_0);
gpio_init(IO_1);
gpio_init(IO_2);
gpio_init(IO_3);
gpio_init(IO_4);
gpio_init(IO_5);
gpio_init(IO_6);
gpio_init(IO_7);
}
void Enable_Address_Latch_Pin(void) // ACTIVE HIGH
{
gpio_put(Address_Latch_Enable, 1);
}
void Disable_Address_Latch_Pin(void)
{
gpio_put(Address_Latch_Enable, 0);
}
void Enable_Chip_Enable_Pin(void) // ACTIVE LOW
{
gpio_put(Chip_Enable_1, 1);
}
void Disable_Chip_Disable_Pin(void)
{
gpio_put(Chip_Enable_1, 0);
}
void Enable_Command_Latch_Pin(void) // ACTIVE HIGH
{
gpio_put(Command_Latch_Enable, 1);
}
void Disable_Command_Latch_Pin(void)
{
gpio_put(Command_Latch_Enable, 0);
}
void Enable_Read_Pin(void) // ACTIVE LOW
{
gpio_put(Read_Enable, 1);
}
void Disable_Read_Pin(void)
{
gpio_put(Read_Enable, 0);
}
void Enable_Write_Pin(void) // ACTIVE LOW
{
gpio_put(Write_Enable, 1);
}
void Disable_Write_Pin(void)
{
gpio_put(Write_Enable, 0);
}
void Enable_Write_Protect_Pin(void) // ACTIVE LOW
{
gpio_put(Write_Protect, 1);
}
void Disable_Write_Protect_Pin(void)
{
gpio_put(Write_Protect, 0);
}
void Enable_Read_Busy_Pin(void) // ACTIVE LOW
{
gpio_put(Read_Beasy_1, 1);
}
void Disable_Read_Busy_Pin(void)
{
gpio_put(Read_Beasy_1, 0);
}
void Set_Data_Bus_As_Input()
{
gpio_set_dir(IO_0, GPIO_IN);
gpio_set_dir(IO_1, GPIO_IN);
gpio_set_dir(IO_2, GPIO_IN);
gpio_set_dir(IO_3, GPIO_IN);
gpio_set_dir(IO_4, GPIO_IN);
gpio_set_dir(IO_5, GPIO_IN);
gpio_set_dir(IO_6, GPIO_IN);
gpio_set_dir(IO_7, GPIO_IN);
}
void Set_Data_Bus_As_Output()
{
gpio_set_dir(IO_0, GPIO_OUT);
gpio_set_dir(IO_1, GPIO_OUT);
gpio_set_dir(IO_2, GPIO_OUT);
gpio_set_dir(IO_3, GPIO_OUT);
gpio_set_dir(IO_4, GPIO_OUT);
gpio_set_dir(IO_5, GPIO_OUT);
gpio_set_dir(IO_6, GPIO_OUT);
gpio_set_dir(IO_7, GPIO_OUT);
}
// printf("Data Written: 0x%02X\n", data_to_write);
// printf("Data Read: 0x%02X\n", data_read);
uint8_t Read_Data_Bus()
{
uint8_t Read_Data;
// READ DATA BUS
for (int i = 0; i < 8; i++)
{
Read_Data |= (gpio_get(data_bus_pins[i]) << i);
}
return Read_Data;
}
void Write_Data_Bus(uint8_t Data)
{
for (int i = 0; i < 8; i++)
{
// Set the GPIO pin to the corresponding data bit
gpio_put(data_bus_pins[i], (Data >> i) & 0x01);
}
}
uint8_t Read_Data(void)
{
Set_Data_Bus_As_Input();
uint8_t Data_Read = Read_Data_Bus();
return Data_Read;
}
void Write_Data(uint8_t Data)
{
Set_Data_Bus_As_Output();
Write_Data_Bus(Data);
}
uint8_t dataInput();
void sendAddress(uint8_t addr);
void nandReadSignature()
{
uint8_t sm = 0x00, sd = 0x00;
nandSendCommand(0x90);
sendAddress(0x00);
// Enable_Address_Latch_Pin();
// Disable_Write_Pin();
// Write_Data(0x00);
// Enable_Write_Pin();
// Disable_Address_Latch_Pin();
// // Disable_Read_Pin();
// sm = dataInput();
// // Enable_Read_Pin();
// // Disable_Read_Pin();
// sd = dataInput();
// // Enable_Read_Pin();
// printf("Nand Manufacturer: 0x%02X\n", sm);
// printf("NAND device ID: 0x%02X\n", sd);
for(uint8_t i = 0; i < 5; i++){
sm = dataInput();
printf("0x%02X ", sm);
}
printf("\n");
nandSendCommand(0x90);
sendAddress(0x20);
for(uint8_t i = 0; i < 5; i++){
sm = dataInput();
printf("0x%02X ", sm);
}
printf("\n");
nandSendCommand(0xEC);
sendAddress(0x00);
for(uint8_t i = 0; i < 44; i++){
sm = dataInput();
printf("0x%02X ", sm);
}
printf("\n");
}
void nandSendCommand(uint8_t cmd, bool last)
{
Disable_Write_Pin();
Enable_Read_Pin();
Disable_Address_Latch_Pin();
Enable_Command_Latch_Pin();
Write_Data(cmd);
Enable_Write_Pin();
Disable_Command_Latch_Pin();
}
void sendAddress(uint8_t addr){
Disable_Command_Latch_Pin();
Disable_Write_Pin();
Enable_Read_Pin();
Enable_Address_Latch_Pin();
Write_Data(addr);
Enable_Write_Pin();
Disable_Address_Latch_Pin();
}
uint8_t dataInput(){
Disable_Command_Latch_Pin();
Disable_Address_Latch_Pin();
Enable_Write_Pin();
Enable_Read_Pin();
Disable_Read_Pin();
uint8_t data = Read_Data();
Enable_Read_Pin();
return data;
}
void NAND_Idle_Bus()
{
Enable_Read_Pin();
Enable_Write_Pin();
Disable_Command_Latch_Pin();
Disable_Address_Latch_Pin();
Enable_Chip_Enable_Pin();
}
void nandEnable(bool e)
{
gpio_put(Chip_Enable_1, !e);
}