1 /*********************************************************************
  2     FileName:           Flash.c
  3     Dependencies:       See #includes
  4     Processor:          PIC32MZ
  5     Hardware:           MainBrain MZ
  6     Complier:           XC32 4.40
  7     Author:             Larry Knight 2023
  8 /*********************************************************************
  9  
 10     Software License Agreement:
 11  
 12     Licensed under the Apache License, Version 2.0 (the "License");
 13     you may not use this file except in compliance with the License.
 14     You may obtain a copy of the License at
 15 
 16     http://www.apache.org/licenses/LICENSE-2.0
 17 
 18     Unless required by applicable law or agreed to in writing, software
 19     distributed under the License is distributed on an "AS IS" BASIS,
 20     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 21     See the License for the specific language governing permissions and
 22     limitations under the License.
 23  
 24     Description:
 25         System Clock = 200 - 250 MHz
 26 
 27     File Description:
 28  * this is all the functions for the MX29LV040C flash memory chip
 29 
 30     Change History:
 31  * last updated 11/14/2026
 32  
 33 /***********************************************************************/
 34 
 35 #include <xc.h>
 36 #include <stdbool.h>
 37 #include "MainBrain.h"
 38 
 39 //Flash Sector Size
 40 const uint16_t SECTOR_SIZE = 4095;
 41 
 42 uint8_t data_flash;
 43 uint8_t Flash_MID;
 44 uint8_t Flash_DID;
 45 uint8_t CFI_buffer[80];
 46 uint32_t dly = 10000;
 47 
 48 void Flash_Init(void)
 49 {
 50     //RB2 = A16
 51     TRISBbits.TRISB2 = 0;
 52     PORTBbits.RB2 = 0;
 53     
 54     //RB4 = A17
 55     TRISBbits.TRISB4 = 0;
 56     PORTBbits.RB4 = 0;
 57     
 58     //RB3 = A18
 59     TRISBbits.TRISB3 = 0;
 60     PORTBbits.RB3 = 0;
 61     
 62     //RA10 = /CS2
 63     TRISAbits.TRISA10 = 0;
 64     PORTAbits.RA10 = 1;
 65     
 66 //******************************************************    
 67 //          Get Manufacturer ID
 68 //******************************************************    
 69     Get_Flash_Device_ID();
 70     
 71 //******************************************************    
 72 //          Get Device ID
 73 //******************************************************    
 74     Get_Flash_Manufacturer_ID();
 75 }
 76 
 77 void Flash_High_Address(unsigned address_flash)
 78 {
 79     unsigned sector = address_flash >> 16;
 80 
 81     PORTBbits.RB2 = (sector & 0x01) ? 1 : 0;   // A16
 82     PORTBbits.RB4 = (sector & 0x02) ? 1 : 0;   // A17
 83     PORTBbits.RB3 = (sector & 0x04) ? 1 : 0;   // A18
 84 }
 85 
 86 uint8_t Flash_RD(unsigned address_flash)
 87 {
 88     uint8_t value;
 89 
 90     Flash_High_Address(address_flash);      // A16-A18
 91 
 92     PMRADDR = address_flash & 0x0FFFF;
 93     while(PMMODEbits.BUSY);
 94 
 95     // /CS2
 96     PORTAbits.RA10 = 0;
 97 
 98     value = (uint8_t)(PMRDIN & 0xFF);       // dummy read, discard
 99     while(PMMODEbits.BUSY);
100 
101     value = (uint8_t)(PMRDIN & 0xFF);
102     while(PMMODEbits.BUSY);
103 
104     // /CS2
105     PORTAbits.RA10 = 1;
106 
107     return value;
108 }
109 
110 bool Flash_WR(unsigned address_flash, uint8_t data_flash)
111 {
112     uint8_t  status;
113     uint16_t timeout = 0;
114     
115     // Data cycle: real sector bits, no dummy write
116     Flash_High_Address(address_flash);
117 
118     uint8_t blank_test = Flash_RD(address_flash);
119 
120     if(blank_test != 0xff)
121     {
122         return false;
123     }
124 
125     // Command cycles: A18-A16 must be 0
126     Flash_High_Address(0x00000);
127     
128     // /CS2
129     PORTAbits.RA10 = 0;
130 
131     PMWADDR = 0x555;
132     while(PMMODEbits.BUSY);
133     
134     PMDOUT  = 0xAA;
135     while(PMMODEbits.BUSY);
136     
137     PMWADDR = 0x2AA;
138     while(PMMODEbits.BUSY);
139     
140     PMDOUT  = 0x55;
141     while(PMMODEbits.BUSY);
142     
143     PMWADDR = 0x555;
144     while(PMMODEbits.BUSY);
145     
146     PMDOUT  = 0xA0;
147     while(PMMODEbits.BUSY);
148 
149     // Data cycle: real sector bits, no dummy write
150     Flash_High_Address(address_flash);
151     
152     PMWADDR = address_flash & 0x0FFFF;
153     
154     PMDOUT  = data_flash;
155     while(PMMODEbits.BUSY);
156 
157     // Point the read address at the target for status polling
158     PMRADDR = address_flash & 0x0FFFF;
159 
160     while(timeout++ < 200)
161     {
162         status = (uint8_t)(PMRDIN & 0xFF);   // dummy read
163         while(PMMODEbits.BUSY);
164         
165         status = (uint8_t)(PMRDIN & 0xFF);   // real sample
166         while(PMMODEbits.BUSY);
167         
168         if((status & 0x80) == (data_flash & 0x80))
169         {
170             if(!(status & 0x20))
171             {
172                 if((status & 0xFF) == data_flash)
173                 {
174                     //Reset Flash
175                     PMWADDR = 0;
176 
177                     PMDOUT  = 0xF0;
178                     while(PMMODEbits.BUSY);
179 
180                     // /CS2
181                     PORTAbits.RA10 = 1;
182                     
183                     return true;
184                 }
185             }
186         }
187     }
188 
189     Flash_High_Address(0x00000);
190     
191     PMWADDR = 0;
192      
193     PMDOUT  = 0xF0;
194     while(PMMODEbits.BUSY);
195     
196     // /CS2
197     PORTAbits.RA10 = 1;
198     
199     return false;
200 }
201 
202 bool Flash_Chip_Erase(void)
203 {
204     uint8_t  status;
205     uint16_t timeout;
206     
207     Flash_High_Address(0x00000);
208 
209     // /CS2
210     PORTAbits.RA10 = 0;
211 
212     PMWADDR = 0x555;
213     PMDOUT = 0xAA;
214     while(PMMODEbits.BUSY);
215     
216     PMWADDR = 0x2AA;
217     PMDOUT = 0x55;
218     while(PMMODEbits.BUSY);
219     
220     PMWADDR = 0x555;
221     PMDOUT = 0x80;
222     while(PMMODEbits.BUSY);
223     
224     PMWADDR = 0x555;
225     PMDOUT = 0xAA;
226     while(PMMODEbits.BUSY);
227     
228     PMWADDR = 0x2AA;
229     PMDOUT = 0x55;
230     while(PMMODEbits.BUSY);
231     
232 
233     PMWADDR = 0x555;
234     PMDOUT  = 0x10;
235     while(PMMODEbits.BUSY);
236     
237     while(timeout++ < 100000000)
238     {
239         status = (uint8_t)(PMRDIN & 0xFF);   // dummy
240         while(PMMODEbits.BUSY);
241         
242         status = (uint8_t)(PMRDIN & 0xFF);   // real
243         while(PMMODEbits.BUSY);
244 
245         if(status & 0xA0)   // Q7 done, Q2 done
246         {
247             PMWADDR = 0;
248             
249             PMDOUT  = 0xF0;
250             while(PMMODEbits.BUSY);
251             
252             PORTAbits.RA10 = 1;
253             
254             return true;
255         }
256     }
257 
258     //Reset Flash
259     PMWADDR = 0;
260 
261     PMDOUT  = 0xF0;
262     while(PMMODEbits.BUSY);
263 
264     // /CS2
265     PORTAbits.RA10 = 1;
266 
267     return false;
268 }
269 
270 bool Flash_Sector_Erase(uint8_t erase_sector)
271 {
272     uint8_t  status;
273     uint16_t timeout;
274 
275     if(erase_sector > 7)
276     {
277         return false;
278     }
279 
280     Flash_High_Address(0x00000);
281     
282     // /CS2
283     PORTAbits.RA10 = 0;
284 
285     PMWADDR = 0x555;
286     PMDOUT = 0xAA;
287     while(PMMODEbits.BUSY);
288     
289     PMWADDR = 0x2AA;
290     PMDOUT = 0x55;
291     while(PMMODEbits.BUSY);
292     
293     PMWADDR = 0x555;
294     PMDOUT = 0x80;
295     while(PMMODEbits.BUSY);
296     
297     PMWADDR = 0x555;
298     PMDOUT = 0xAA;
299     while(PMMODEbits.BUSY);
300     
301     PMWADDR = 0x2AA;
302     PMDOUT = 0x55;
303     while(PMMODEbits.BUSY);
304 
305     Flash_High_Address((unsigned)erase_sector << 16);
306     
307     PMWADDR = 0x0000;
308     PMDOUT  = 0x30;
309     while(PMMODEbits.BUSY);
310 
311     PMRADDR = 0;
312 
313     timeout = 0;
314     
315     while(timeout++ < 1000000)
316     {
317         status = (uint8_t)(PMRDIN & 0xFF);   // dummy
318         while(PMMODEbits.BUSY);
319         
320         status = (uint8_t)(PMRDIN & 0xFF);   // real
321         while(PMMODEbits.BUSY);
322 
323         if(status & 0x80)   // Q7 done, Q2 done
324         {
325             PMWADDR = 0;
326             
327             PMDOUT  = 0xF0;
328             while(PMMODEbits.BUSY);
329             
330             // /CS2
331             PORTAbits.RA10 = 1;
332             
333             return true;
334         }
335     }
336     
337     //Reset Flash
338     PMWADDR = 0;
339 
340     PMDOUT  = 0xF0;
341     while(PMMODEbits.BUSY);
342 
343     // /CS2
344     PORTAbits.RA10 = 1;
345 
346     return false;
347 }
348 
349 void Get_Flash_Device_ID()
350 {
351     Flash_High_Address(0x00000);
352     
353     //CS2
354     PORTAbits.RA10 = 0;
355     
356     //Enter sequence
357     PMWADDR = 0x555;
358     PMDOUT = 0xaa;
359     while(PMMODEbits.BUSY == 1);  
360 
361     PMWADDR = 0x2aa;
362     PMDOUT = 0x55;
363     while(PMMODEbits.BUSY == 1);  
364 
365     PMWADDR = 0x555;
366     PMDOUT = 0x90;
367     while(PMMODEbits.BUSY == 1);  
368                 
369     //read device ID
370     PMRADDR = 0x0;
371     
372     //dummy read for PMP
373     Flash_MID = PMRDIN;
374     while(PMMODEbits.BUSY == 1); 
375     
376     Flash_MID = PMRDIN;
377     while(PMMODEbits.BUSY == 1);  
378     
379     //Exit sequence
380     PMWADDR = 0;
381     PMDOUT = 0xf0;
382     while(PMMODEbits.BUSY == 1);  
383       
384     // /CS2
385     PORTAbits.RA10 = 1;
386     
387     //If the manufacturer ID is not 0xC2 then set error bit 0
388     if(Flash_MID != 0xc2)
389     {
390         lastError = lastError | 1;
391     }
392 }
393 
394 void Get_Flash_Manufacturer_ID(void)
395 {
396     Flash_High_Address(0x00000);
397     
398     //CS2
399     PORTAbits.RA10 = 0;
400         
401     //Get Chip ID
402     //Enter sequence
403     PMWADDR = 0x555;
404     PMDOUT = 0xaa;
405     while(PMMODEbits.BUSY == 1);  
406    
407     PMWADDR = 0x2aa;
408     PMDOUT = 0x55;
409     while(PMMODEbits.BUSY == 1);  
410     
411     PMWADDR = 0x555;
412     PMDOUT = 0x90;
413     while(PMMODEbits.BUSY == 1);  
414     
415     //read device ID
416     PMRADDR = 0x1;
417     
418     //dummy read for PMP
419     Flash_DID = PMRDIN;
420     while(PMMODEbits.BUSY == 1); 
421     
422     Flash_DID = PMRDIN;
423     while(PMMODEbits.BUSY == 1);  
424     
425     //Exit sequence
426     PMWADDR = 1;
427     PMDOUT = 0xf0;
428     while(PMMODEbits.BUSY == 1);  
429             
430     //CS2
431     PORTAbits.RA10 = 1;    
432     
433     //If device code is not 0x4F the set error bit 1
434     if(Flash_DID != 0x4f)
435     {
436         lastError = lastError | 2;
437     }
438 }
439 
440 void CFI_Read_Flash(void)
441 {
442     //CS2
443     PORTAbits.RA10 = 0;
444     
445     Flash_High_Address(0x00000);
446         
447     PMWADDR = 0x55;
448     
449     PMDOUT  = 0x98;
450     while(PMMODEbits.BUSY);
451 
452     //Read the data
453     for(int i=0;i<80;i++)
454     {
455         PMRADDR = i + 0x10;
456         
457         CFI_buffer[i] = (uint8_t)(PMRDIN & 0xFF);   // dummy
458         while(PMMODEbits.BUSY);
459         
460         CFI_buffer[i] = (uint8_t)(PMRDIN & 0xFF);   // real
461         while(PMMODEbits.BUSY);
462     }    
463 
464     //Reset Flash
465     PMWADDR = 0;
466     
467     PMDOUT  = 0xF0;
468     while(PMMODEbits.BUSY);
469 
470     // /CS2
471     PORTAbits.RA10 = 1;
472 }
473 
474