What I do today is read and write flash. Because the encoding learned from infrared must be stored in flash, reading and writing flash is an important module!
However, there is no data flash read/write in the New Tang routine, and only ldrom and aprom read/write are actually not much worse. In order not to give more detours to future colleagues, the author's experience is written here for your reference!
The data flash size of this board is 4 kb, from 0x0001f000 to 0x0001ffff. At the beginning, the author wrote:
# Define unsigned int uint32_t
Uint32_t * flash;
Flash = (uint32_t *) 0x0001f000;
* Flash = 0x55555555;
I plan to write the value 0x55555555 to the starting address. When the result is executed here, a hardware exception occurs and the hardware exception processing function is entered!
Note that writing to flash requires high security. If the corresponding registers are not set, a hardware exception will occur!
So how can we write data normally? Remember, you must erase the slice and write it again. I did not erase it at first and wrote it directly, but it was not successful!
The following is a function written by the author to write values to a specific address:
Void writetoflash (uint32_t offset, uint32_t date)
{/* Erase before writing */
Int I;
Un_lock_reg (); // when writing data to flash, it is written through the system isp. Therefore, you need to write three values first. Friends who have routines can understand this.
Ispcon | = ispen; // enable the ISP Function
Ispcmd = page_erase; // select the erased mode.
For (I = 0; I <4096/512; I ++) // 4kb = 4096b. When erased, the page size is 512 bytes.
{
Ispadr = 0x0001f000 + I * 512;
Isptrg | = ispgo; // trigger the ISP
While (isptrg & ispgo) = ispgo); // wait until the ISP finishes executing
}
Ispcmd = Program; // set to programming mode
Ispadr = dfbadr + (offset <2); // get the address to be written, equivalent to 0x0001f000 + 4 * offset. offset indicates the number of values.
Ispdat = date; // write the value to the data register
Isptrg | = ispgo; // to be executed
While (isptrg & ispgo) = ispgo );
Un_lock_reg ();
Ispcon & = ~ Ispen; // disable the ISP Function
}
Then read the function:
Uint32_t readfromflash (uint32_t offset)
{
Uint32_t data;
Un_lock_reg (); // functions are as follows:
Ispcon | = ispen; // enable the ISP Function
Ispcmd = read; // set to read mode
Ispadr = dfbadr + (offset <2); // obtain the address to be read.
Data = ispdat; // get the data
Un_lock_reg ();
Ispcon & = ~ Ispen; // disable the ISP Function
Return data;
}
To sum up, you must first enable the ISP function, then set the mode, give the address to be operated, then operate the data, and finally disable the ISP function, but you must erase it before writing it!