[MCU Notes] STM8S series MCU FLASH operation

Publisher:灵感之翼Latest update time:2019-02-18 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    1. Modify the comments of stm8s_conf.h to allow #include "stm8s_flash.h" to compile


    2. Modify the injection of stm8s.h to enable the Flash function to run from RAM


#if !defined (RAM_EXECUTION)

    #define RAM_EXECUTION (1) // When writing to a block, you must enable injection here

#endif /* RAM_EXECUTION */



   3. After the modification is completed, a lot of warnings will be generated. The method to eliminate them has not been found yet, but it will not affect the operation of the code.




Attach the code:



#include "fy_flash.h"



//Before operating the flash, you need to uncomment line 154 in stm8s.h /* #define RAM_EXECUTION (1) */



//Read data from the specified third byte of the block (entry number)

u8 Flash_ReadByte_3(u8 Block)

  u32 addr;

   

  addr = FLASH_PROG_START_PHYSICAL_ADDRESS + Block*FLASH_BLOCK_SIZE;

  return FLASH_ReadByte(addr+2);

}


//Write the content to the specified block of Flash

void Flash_WriteBlock(u8 Block , u8 *buf)

{  

  if(Block>FLASH_PROG_BLOCKS_NUMBER) return;

  

  FLASH_Unlock(FLASH_MEMTYPE_PROG); //解锁flash

  while (FLASH_GetFlagStatus(FLASH_FLAG_PUL) == RESET); //Wait for the unlock flag to be set

 

  //Block standard programming, before writing, the hardware automatically deletes the content

  FLASH_ProgramBlock(Block, FLASH_MEMTYPE_PROG, FLASH_PROGRAMMODE_STANDARD,buf);

  //Wait for the operation to complete

  FLASH_WaitForLastOperation(FLASH_MEMTYPE_PROG);

  //Lock after operation

  FLASH_Lock(FLASH_MEMTYPE_PROG);   

}


//Read the contents of the specified number of blocks from Flash

void Flash_ReadBlock(u8 Block ,u8 *buf)

{

  u8 i;

  u32 addr;

  u8 sta=0;

  if(Block>FLASH_PROG_BLOCKS_NUMBER) return;

  

  //Calculate the address

  addr = FLASH_PROG_START_PHYSICAL_ADDRESS + Block*FLASH_BLOCK_SIZE;

  

  for(i=0;i

  {

*(buf+i) = FLASH_ReadByte(addr+i); //Read a block of data

/* if(buf[i]=='\r' && sta==0) sta=1;

else if(buf[i]=='\n' && sta==1) return;

else state=0;

*/

  }

}



void Flash_Test(void)

{

u8 write_buf[FLASH_BLOCK_SIZE]="MARS->This is a Flash Data Memory write and read test...\r\n";

u8 read_buf[FLASH_BLOCK_SIZE];

Flash_WriteBlock(120,write_buf);

Flash_ReadBlock(120,read_buf);

UART1_SendString(read_buf);

while(1)

{

  LED_TOG;Delay_ms(350);

}

}

/*********************************************END OF FILE**********************************************/


Keywords:MCU Reference address:[MCU Notes] STM8S series MCU FLASH operation

Previous article:[MCU Notes] OLED controller SSD1306 and driver code
Next article:[MCU Notes] Active Buzzer Driver-Efficiency Programming

Recommended ReadingLatest update time:2024-11-15 17:39

Allocation of FLASH space in STM32 microcontroller bootloader
According to the program running process: Normal program startup process: when starting from FLASH, first enter from the physical address entry 0x08000000 of the flash memory, then the reset interrupt jumps to the reset interrupt service routine; after the reset interrupt service routine is executed, it jumps to the m
[Microcontroller]
Schematic diagram and detailed explanation of the minimum system of single chip microcomputer
The minimum system of the single-chip microcomputer is mainly composed of power supply, reset, oscillation circuit and expansion part. The minimum system schematic is shown in the figure. Power supply module For a complete electronic design, the first problem is to provide a power supply module for the entire
[Microcontroller]
Schematic diagram and detailed explanation of the minimum system of single chip microcomputer
Classic MCU C51 multi-tasking code (improved version)
I have published an article before http://www.51hei.com/mcu/1616.html   This is an improved version with several minor issues fixed. //Note that this source code comes from the Internet (DIY ultra-lightweight multi-tasking operating system article) //===================================================================
[Microcontroller]
Characteristics of 8051 series microcontrollers
A microcontroller (also known as a microcontroller) is a microcomputer that integrates various components on a silicon chip. These components include central processing unit CPU, data memory RAM, program memory ROM, timers/counters and various I/O interface circuits. The basic structure of the 8051 series microcontrol
[Microcontroller]
Characteristics of 8051 series microcontrollers
MCU + ADC0832 simple digital voltmeter proteus simulation and program source code
The ADC0832 voltmeter simulation schematic is as follows The source program of the microcontroller voltmeter is as follows: #include reg52.h #include intrins.h #define uint unsigned int #define uchar unsigned char          sbit CS =P3^4; //Chip select enable terminal sbit DI =P1^1; //Data signal input terminal, s
[Microcontroller]
MCU + ADC0832 simple digital voltmeter proteus simulation and program source code
Discussion on common problems of PIC microcontroller decryption and MCU decryption
  There are always many problems in MCU chip decryption, which may make you stuck for a while, but as long as you write down the problems you encounter and make a good summary, there will always be a solution. Not only does it help to accumulate knowledge, but it also helps to avoid making the same mistake again.   
[Microcontroller]
STC89C52RC MCU + Serial Port + Infrared Control 16-channel Servo Program
There is only a program to control 16-channel servos. I am posting it here for reference. The servos can be controlled via the serial port or infrared. The microcontroller source program is as follows: #include "PCA9685_TJ.h" #include stdio.h #include math.h /*Instructions for use:         In the main program  
[Microcontroller]
STC89C52RC MCU + Serial Port + Infrared Control 16-channel Servo Program
Next Generation Automotive Microcontrollers
STMicroelectronics is committed to helping the automotive industry meet the challenges of electrification and digitalization. It not only provides the solutions needed at this stage, but also provides a more powerful unified MCU platform development strategy in the future, supporting the development of next-generati
[Automotive Electronics]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号