Reading and writing flash in cortex-m051

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.