OpenCores
URL https://opencores.org/ocsvn/usb_nand_reader/usb_nand_reader/trunk

Subversion Repositories usb_nand_reader

[/] [usb_nand_reader/] [trunk/] [mini32/] [main.c] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 pradd
/*
2
 * Project name:
3
     HID_Read_Write_Polling (USB HID Read & Write Test)
4
 * Copyright:
5
     (c) Mikroelektronika, 2011.
6
 * Revision History:
7
     20110615:
8
       - initial release (FJ);
9
 * Description:
10
     This example establishes connection with the HID terminal that is active
11
     on the PC. Upon connection establishment, the HID Device Name will appear
12
     in the respective window. The character that user sends to PIC from the HID
13
     terminal will be re-sent back to user.
14
 * Test configuration:
15
     MCU:             P32MX534F064H
16
                      http://ww1.microchip.com/downloads/en/DeviceDoc/61156G.pdf
17
     Dev.Board:       MINI-32 Board
18
                      http://www.mikroe.com/eng/products/view/711/mini-32-board/
19
     Oscillator:      XT-PLL10x, 80.000MHz
20
     Ext. Modules:    None.
21
     SW:              mikroC PRO for PIC32
22
                      http://www.mikroe.com/eng/categories/view/89/pic32-compilers/
23
 * NOTES:
24
     - None.
25
*/
26
 
27
#include "NandDataLine.h"
28
#include "NandControl.h"
29
#include "CMD.h"
30
 
31
 
32
sbit CE_ON at LATG6_bit;
33
sbit DAT_ON at LATD6_bit;
34
 
35
char cnt;
36
char kk;
37
char readbuff[64];
38
//char writebuff[64];
39
char writebuff[1024*3];
40
 
41
unsigned char* onfiParamPage = (unsigned char*)0;
42
int pageSize = 0;
43
int addressCycles = 0;
44
int busWidth = 8;
45
char isOnfi = 0;
46
unsigned char nandId[5];
47
 
48
void main(void)
49
{
50
  char   hasId = 0;
51
  char   hasOnfiParameterPage = 0;
52
 
53
  AD1PCFG = 0xFFFF;
54
  TRISG6_bit = 0;
55
  LATG6_bit = 0;
56
  TRISD6_bit = 0;
57
  LATD6_bit = 0;
58
 
59
  init_nand_data_line();
60
  init_nand_control_line();
61
 
62
  MM_Init();
63
 
64
  HID_Enable(&readbuff,&writebuff);
65
  if(U1CON.JSTATE == 0)
66
  {
67
     LATG6_bit = 1;
68
     Delay_ms(1000);
69
     LATG6_bit = 0;
70
  }
71
  else
72
  {
73
     LATD6_bit = 1;
74
     Delay_ms(1000);
75
     LATD6_bit = 0;
76
  }
77
 
78
 
79
  while(1)
80
  {
81
    USB_Polling_Proc();
82
 
83
    kk = HID_Read();
84
    if(kk != 0)
85
    {
86
        switch(readbuff[0])
87
        {
88
            case NAND_CHIP_ENABLE:
89
                cmd_chip_enable(readbuff);
90
                CE_ON = 1;
91
                break;
92
 
93
            case NAND_CHIP_DISABLE:
94
                cmd_chip_disable();
95
                CE_ON = 0;
96
                break;
97
 
98
            case NAND_CHIP_RESET:
99
                DAT_ON = 1;
100
                cmd_chip_reset();
101
                DAT_ON = 0;
102
                break;
103
 
104
            case NAND_CHIP_READ_ID:
105
                DAT_ON = 1;
106
                cmd_chip_read_id(writebuff, 0);
107
                nandId[0] = writebuff[0];
108
                nandId[1] = writebuff[1];
109
                nandId[2] = writebuff[2];
110
                nandId[3] = writebuff[3];
111
                nandId[4] = writebuff[4];
112
                HID_Write(&writebuff, 64);
113
                hasId = 1;
114
                DAT_ON = 0;
115
                break;
116
 
117
            case NAND_CHIP_READ_ID_ONFI:
118
                DAT_ON = 1;
119
                cmd_chip_read_id(writebuff, 0x20);
120
                if('O' == writebuff[0] && 'N' == writebuff[1] && 'F' == writebuff[2] && 'I' == writebuff[3])
121
                {
122
                    isOnfi = 1;
123
                    onfiParamPage = Malloc(0x100);
124
                    if(0 == onfiParamPage)
125
                        isOnfi = 0;
126
                }
127
                HID_Write(&writebuff, 64);
128
                DAT_ON = 0;
129
                break;
130
 
131
            case NAND_CHIP_READ_PARAM_PAGE:
132
                DAT_ON = 1;
133
                if(0 != onfiParamPage)
134
                {
135
                    int i, j;
136
                    cmd_chip_read_param_page(onfiParamPage);
137
                    for(i = 0; i < 4; i++)
138
                    {
139
                        for(j = 0; j < 64; j++)
140
                        {
141
                            writebuff[j] = onfiParamPage[i * 64 + j];
142
                        }
143
                        HID_Write(&writebuff, 64);
144
                    }
145
                    hasOnfiParameterPage = 1;
146
                    busWidth = 8 << (onfiParamPage[6] & 1);
147
                }
148
                else
149
                {
150
                    writebuff[0] = 0;
151
                    HID_Write(&writebuff, 64);
152
                }
153
                DAT_ON = 0;
154
                break;
155
 
156
            case NAND_CHIP_READ_PAGE:
157
                /*  Check whether we have all the needed information (e.g. ID or ONFI parameter page) */
158
                if(0 == hasId && 0 == hasOnfiParameterPage)
159
                {
160
                    DAT_ON = 0;
161
                    break;
162
                }
163
 
164
                if(hasOnfiParameterPage)
165
                {
166
                    pageSize = *(unsigned int*)(onfiParamPage + 80) + *(unsigned short*)(onfiParamPage + 84);
167
                    addressCycles = (*(onfiParamPage + 101) & 0x0f) + (*(onfiParamPage + 101) >> 4);
168
                }
169
                DAT_ON = 1;
170
                cmd_chip_read_page(readbuff, /*pageBuffer*/ writebuff, pageSize, addressCycles);
171
                DAT_ON = 0;
172
                break;
173
 
174
            case NAND_SET_CONFIG_DATA:
175
                DAT_ON = 1;
176
                pageSize = *(int*)(readbuff + 1);
177
                addressCycles = *(readbuff + 5);
178
                busWidth = *(readbuff + 6);
179
                DAT_ON = 0;
180
                break;
181
 
182
            case NAND_CHIP_READ_CACHE_SEQ:
183
                /*  Check whether we have all the needed information (e.g. ID or ONFI parameter page) */
184
                if(0 == hasId && 0 == hasOnfiParameterPage)
185
                {
186
                    DAT_ON = 0;
187
                    break;
188
                }
189
 
190
                if(hasOnfiParameterPage)
191
                {
192
                    pageSize = *(unsigned int*)(onfiParamPage + 80) + *(unsigned short*)(onfiParamPage + 84);
193
                    addressCycles = (*(onfiParamPage + 101) & 0x0f) + (*(onfiParamPage + 101) >> 4);
194
                }
195
                DAT_ON = 1;
196
                if(0 != pageSize)
197
                {
198
                    USB_Polling_Proc();
199
                    cmd_chip_read_page_cache_sequential(readbuff, /*pageBuffer*/ writebuff, pageSize, addressCycles);
200
                }
201
                DAT_ON = 0;
202
                break;
203
 
204
            case NAND_CHIP_READ_STATUS:
205
                writebuff[0] = cmd_chip_read_status();
206
                HID_Write(&writebuff, 64);
207
                break;
208
 
209
            case NAND_CHIP_READ_UNIQUE_ID:
210
                DAT_ON = 1;
211
                cmd_chip_read_unique_id(writebuff);
212
                HID_Write(&writebuff, 64);
213
                DAT_ON = 0;
214
                break;
215
 
216
            case NAND_CHIP_BLOCK_ERASE:
217
                DAT_ON = 1;
218
                cmd_chip_block_erase(readbuff);
219
                DAT_ON = 0;
220
                break;
221
 
222
            case NAND_CHIP_TOGGLE_WP:
223
                nand_toggle_wp();
224
                break;
225
 
226
            case NAND_CHIP_PAGE_PROGRAM:
227
                DAT_ON = 1;
228
                if(0 != pageSize)
229
                    cmd_chip_page_program(readbuff, addressCycles, pageSize);
230
                DAT_ON = 0;
231
                break;
232
 
233
            case NAND_CHIP_READ_STATUS_ENHANCED:
234
                writebuff[0] = cmd_chip_read_status_enhanced(readbuff, addressCycles);
235
                HID_Write(&writebuff, 64);
236
                break;
237
 
238
            default:
239
                break;
240
        }
241
    }
242
  }
243
  /*
244
  while(1){
245
    USB_Polling_Proc();               // Call this routine periodically
246
    kk = HID_Read();
247
    if(kk != 0){
248
      PIN = ~PIN;
249
      STAT = 1;
250
      for(cnt=0;cnt<64;cnt++)
251
        writebuff[cnt]=readbuff[cnt];
252
      STAT = 0;
253
      DATA = 1;
254
      HID_Write(&writebuff,64);
255
      DATA = 0;
256
    }
257
  }
258
  */
259
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.