You are on page 1of 6

Now let's start writing to the chip's memory.

To do this, go to the file w25q_spi.c and add a macro for the write command

1 #define W25_READ 0x03

2 #define W25_PAGE_PROGRAMM 0x02

From the function bodies W25_Write_Enable and W25_Write_Disable remove the delay<
/span>

cs_reset();
HAL_Delay(1);

Below the function W25_Write_Disable add a function to enable and disable security bits

1 //-------------------------------------
------------------------
2
void W25_Set_Block_Protect(uint8_t val)
3
{
4
buf[0] = 0x50;
5
cs_set();
6
SPI1_Send(buf, 1);
7
cs_reset();
8
buf[0] = W25_WRITE_STATUS_1;
9
buf[1] = ((val & 0x0F) << 2);
10
cs_set();
11
SPI1_Send(buf, 2);
12
cs_reset();
13
}
14
//-------------------------------------
------------------------
There is nothing special about this function; it first allows writing to the status register, and
then turns on or off the necessary bits in it.
Now in the function W25_Erase_Sector we will first unlock the protection

1 void W25_Erase_Sector(uint32_t addr)

2 {

3 W25_Wait_Write_End();

4 W25_Set_Block_Protect(0x00);

Then at the bottom of the function body we will first remove the delay

W25_Wait_Write_End();
HAL_Delay(1);

Instead, we will disable recording and enable protection

1 W25_Wait_Write_End();

2 W25_Write_Disable();

3 W25_Set_Block_Protect(0x0F);

Then we will do exactly the same with the functions W25_Erase_Block and
W25_Erase_Chip.
Below the function W25_Erase_Chip we will add a data recording function, in which we
first wait until the chip is ready for the data change operation, unlock the protection, enable the
recording option and lower the selection leg

1 TFT9341_String(1,150,"Write Data
");
2
pageCount = *(unsigned
3 int*)(rx_buf);
4 pageSize = *(unsigned
int*)(rx_buf + 4);
5 dt1[0] = 0xAA;

HAL_UART_Transmit(&huart1,(uint8_t*)dt1
,1,0x1000);

Then we will add an endless loop, in which we will receive from the computer the number of
the page to be written, the bytes of the page, write these bytes into the chip and send the
computer a command to finish writing the page, and if this is the last page, then we will exit the
endless loop.

1
HAL_UART_Transmit(&huart1,(uint8_t*)dt1
2 ,1,0x1000);
3 while(1)
4 {
5
HAL_UART_Receive(&huart1,(uint8_t*)rx_b
6
uf,4,0x1000);
7
cur_page = *(unsigned
8 int*)(rx_buf);

9
HAL_UART_Receive(&huart1,(uint8_t*)rx_b
10 uf,256,0x1000);

11 W25_Write_Page(rx_buf,
cur_page, 0, pageSize);

dt1[0] = 0xEE;

HAL_UART_Transmit(&huart1,(uint8_t*)dt1
,1,0x1000);

if(cur_page >= (pageCount - 1))


break;

After this cycle, as usual, we will report the end of the process and light the LEDs

C
1 if(cur_page >= (pageCount - 1))
break;
2
}
3
TFT9341_String(1,176,"Done!");
4
HAL_GPIO_WritePin(GPIOG,
5 GPIO_PIN_13, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOG,
GPIO_PIN_14, GPIO_PIN_SET);

Well, the time has finally come to check our work.


The PC program, as I think you understand, I have finalized and you can also download it
from the link at the bottom of this page.
Let's launch our program, respectively, having first collected our code for the controller and
flashed it, and first, as always, we count the service information so that the buttons we need
become active.
As we see on the display, information about the beginning and end of the process appeared
almost instantly due to its fast response

Use the button Open File to open the file with the firmware
We see that the file has opened and the Write button has become active, click it to begin the
process of writing the firmware to the chip.
First, we will receive a corresponding warning that all information will be overwritten. We
agree and the recording process will begin, which will be clear from the progress indicator, as
well as from the inscription on the display of the debug board

At the end of the process, we will receive a corresponding message on the display and also
see the LEDs lit
We can read the firmware from the board, write it to a file and compare the file that we wrote
to the chip with what we read. They should be the same. I have already been convinced of this
many times, so you can take my word for it.
So, in this lesson we learned how to flash a W25Q series microcircuit, that is, write data to it.
We also managed to use the display, after which we can display the information we need on it.
Thank you all for your attention!

You might also like