[STM8] iarstm8 STM8L101F3访问EEPROM

[复制链接]
5520|2
 楼主| CC2530 发表于 2011-6-19 12:45 | 显示全部楼层 |阅读模式

  1. #include "stm8l10x.h"
  2. #include "periph_io.h"
  3. #include "delay.h"

  4. extern __no_init volatile __eeprom  char  eep_u8;
  5. extern __no_init volatile __eeprom  short eep_u16;
  6. extern __no_init volatile __eeprom  long  eep_u32;

  7. volatile  char   ram_u8;
  8. volatile  short  ram_u16;
  9. volatile  long   ram_u32;

  10. void main( void )
  11. {   
  12.   ram_u8=eep_u8;
  13.   ram_u16=eep_u16;
  14.   ram_u32=eep_u32;
  15.    
  16.   eep_u8=1;
  17.   eep_u16=2;
  18.   eep_u32=3;
  19.      
  20.   ram_u8=eep_u8;
  21.   ram_u16=eep_u16;
  22.   ram_u32=eep_u32;
  23.      
  24.   while(1);

  25. }


  26. #include "stm8l10x_conf.h"  

  27. void __eeprom_write_8(unsigned short addr_eep,unsigned char data)  
  28. {  
  29.   FLASH_WaitForLastOperation();   
  30.   FLASH_Unlock(FLASH_MemType_Data);  
  31.    
  32.   FLASH_ProgramByte(addr_eep, data);   
  33.    
  34.   FLASH_WaitForLastOperation();   
  35.   FLASH_Lock(FLASH_MemType_Data);   
  36. }  

  37. void __eeprom_write_16(unsigned short addr_eep,unsigned short data)  
  38. {  
  39.   FLASH_WaitForLastOperation();   
  40.   FLASH_Unlock(FLASH_MemType_Data);  
  41.    
  42.   FLASH_ProgramByte(addr_eep,   data/256);   
  43.   FLASH_WaitForLastOperation();   
  44.    
  45.   FLASH_ProgramByte(addr_eep+1, data%256);  
  46.   FLASH_WaitForLastOperation();   
  47.    
  48.   FLASH_Lock(FLASH_MemType_Data);   
  49. }  

  50. void __eeprom_write_32(unsigned short addr_eep,unsigned long data)  
  51. {  
  52.   FLASH_WaitForLastOperation();   
  53.   FLASH_Unlock(FLASH_MemType_Data);  
  54.    
  55.   FLASH_ProgramByte(addr_eep,   (unsigned char)(data>>24));   
  56.   FLASH_WaitForLastOperation();   
  57.    
  58.   FLASH_ProgramByte(addr_eep+1, (unsigned char)(data>>16));  
  59.   FLASH_WaitForLastOperation();   
  60.    
  61.   FLASH_ProgramByte(addr_eep+2,   (unsigned char)(data>>8));   
  62.   FLASH_WaitForLastOperation();   
  63.    
  64.   FLASH_ProgramByte(addr_eep+3, (unsigned char)(data>>0));  
  65.   FLASH_WaitForLastOperation();  
  66.    
  67.   FLASH_Lock(FLASH_MemType_Data);   
  68. }  


  69. void __eeprom_write_many(unsigned short addr_eep,unsigned short size,unsigned short dummy,unsigned short addr_ram)  
  70. {   
  71.   FLASH_WaitForLastOperation();   
  72.   FLASH_Unlock(FLASH_MemType_Data);  
  73.    
  74.   for(unsigned short i=0;i<size;i++)  
  75.   {  
  76.      FLASH_ProgramByte(addr_eep+i,   *((unsigned char *)(addr_ram)+i));   
  77.      FLASH_WaitForLastOperation();   
  78.   }  
  79.       
  80.   FLASH_Lock(FLASH_MemType_Data);      
  81. }  

  82.         public  eep_u8
  83.         public  eep_u16
  84.         public  eep_u32
  85.          
  86.         SECTION `.eeprom.text`:CONST:REORDER:ROOT(0)
  87. eep_u8:
  88.         DC8 0x12
  89.          
  90.         SECTION `.eeprom.text`:CONST:REORDER:ROOT(0)
  91. eep_u16:
  92.         DC16 0x3456

  93.         SECTION `.eeprom.text`:CONST:REORDER:ROOT(0)
  94. eep_u32:
  95.         DC32 0x789ABCEE

  96.         END

  97. /////////////////////////////////////////////////////////////////
  98. //      Example ILINK command file for
  99. //      STM8 IAR C/C++ Compiler and Assembler.
  100. //
  101. //      Copyright 2010 IAR Systems AB.
  102. //
  103. //      $Revision$
  104. //
  105. /////////////////////////////////////////////////////////////////

  106. define memory with size = 16M;

  107. define region TinyData = [from 0x00 to 0xFF];

  108. define region NearData = [from 0x0000 to 0x05FF];

  109. define region NearFuncCode = [from 0x8000 to 0x9FFF];

  110. define region FarFuncCode = [from 0x8000 to 0x9FFF];

  111. define region HugeFuncCode = [from 0x8000 to 0x9FFF];

  112. define region Eeprom = [from 0x9800 to 0x9FFF];

  113. /////////////////////////////////////////////////////////////////

  114. define block CSTACK with size = _CSTACK_SIZE  {};

  115. define block HEAP  with size = _HEAP_SIZE {};

  116. define block INTVEC with size = 0x80 { ro section .intvec };

  117. // Initialization
  118. initialize by copy { rw section .far.bss,
  119.                      rw section .far.data,
  120.                      rw section .far_func.textrw,
  121.                      rw section .huge.bss,
  122.                      rw section .huge.data,
  123.                      rw section .huge_func.textrw,
  124.                      rw section .iar.dynexit,
  125.                      rw section .near.bss,
  126.                      rw section .near.data,
  127.                      rw section .near_func.textrw,
  128.                      rw section .tiny.bss,
  129.                      rw section .tiny.data,
  130.                      ro section .tiny.rodata };

  131. do not initialize  { rw section .far.noinit,
  132.                      rw section .huge.noinit,
  133.                      rw section .near.noinit,
  134.                      rw section .tiny.noinit,
  135.                      rw section .vregs };

  136. // Placement
  137. place at start of TinyData      { rw section .vregs };
  138. place in TinyData               { rw section .tiny.bss,
  139.                                   rw section .tiny.data,
  140.                                   rw section .tiny.noinit,
  141.                                   rw section .tiny.rodata };

  142. place at end of NearData        { block CSTACK };
  143. place in NearData               { block HEAP,
  144.                                   rw section .far.bss,
  145.                                   rw section .far.data,
  146.                                   rw section .far.noinit,
  147.                                   rw section .far_func.textrw,
  148.                                   rw section .huge.bss,
  149.                                   rw section .huge.data,
  150.                                   rw section .huge.noinit,
  151.                                   rw section .huge_func.textrw,
  152.                                   rw section .iar.dynexit,
  153.                                   rw section .near.bss,
  154.                                   rw section .near.data,
  155.                                   rw section .near.noinit,
  156.                                   rw section .near_func.textrw };

  157. place at start of NearFuncCode  { block INTVEC };
  158. place in NearFuncCode           { ro section .far.data_init,
  159.                                   ro section .far_func.textrw_init,
  160.            ro section .huge.data_init,
  161.                                   ro section .huge_func.textrw_init,
  162.   ro section .iar.init_table,
  163.   ro section .init_array,
  164.                                   ro section .near.data_init,
  165.                                   ro section .near.rodata,
  166.   ro section .near_func.text,
  167.   ro section .near_func.textrw_init,
  168.                                   ro section .tiny.data_init,
  169.                                   ro section .tiny.rodata_init };

  170. place in FarFuncCode            { ro section .far.rodata,
  171.                                   ro section .far_func.text };

  172. place in HugeFuncCode           { ro section .huge.rodata,
  173.                                   ro section .huge_func.text };
  174.                                    
  175. place at end of Eeprom          { rw section .eeprom.noinit ,
  176.                                   ro section .eeprom.text,
  177.                                   ro section .eeprom.textrw_init };                                 

stm8l101_demo.rar

480.08 KB, 下载次数: 114

bili 发表于 2011-6-20 09:47 | 显示全部楼层
标记!好贴!
lwylwy1 发表于 2019-6-26 14:41 | 显示全部楼层
好贴,学习了;
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

262

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部