打印
[STM8]

【转】STM8 块操作

[复制链接]
981|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
众里寻她兜错路,蓦然回首,她却在那库函数!
    一直以来我未曾在网上找到一个专门解析eeprom块编程的库函数,要么遇到的就说库函数太烂了,自己写挺好,要么就是王志杰的例程,这对于初学者来说无非是件很悲催的事情,今天我就了了这个心愿,给后来者栽棵树,希望能茁壮成长!

    我个人觉得stm8s的库函数很强大,用起来很方便,看着她的源代码也是那么的有条理,堪称规范,对于初学者来说她的库函数代码很值得学习,学习她的思路,她的布局会使你获益匪浅!
    步入正题,先上代码!(我用的是STVD4.3.2 外挂Cosmic4.3.4**版)如有需要我会上传分享给大家,这里不讨论开发环境孰好孰坏,用好了奥拓便是奥迪,就这么简单!
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8s.h"
#include <stdio.h>
int _fctcpy(char name);
main()
{

u8 block_buff[128] ={0};
u8 i = 0;
u8 c = 0;
_fctcpy('F');

CLK_DeInit();
  CLK_HSICmd(ENABLE);
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);


  UART1_DeInit();
  UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE,UART1_MODE_TXRX_ENABLE);
  UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
  UART1_Cmd(ENABLE);
  
GPIO_DeInit(GPIOD);
  GPIO_Init(GPIOD, GPIO_PIN_LNIB, GPIO_MODE_OUT_PP_LOW_FAST);

for(i=0;i<128;i++)
   block_buff=i;


  FLASH_DeInit();
  FLASH_Unlock(FLASH_MEMTYPE_DATA);
  FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_TPROG);
  FLASH_ProgramBlock(0x0001, FLASH_MEMTYPE_DATA, FLASH_PROGRAMMODE_STANDARD, block_buff);
FLASH_EraseBlock(0x0001, FLASH_MEMTYPE_DATA);

c = FLASH_ReadByte(0x40ff);//读出值到变量中
printf("c=%x\r\n",(u16)c);//打印
c = FLASH_ReadByte(0x407f);
printf("c=%x\r\n",(u16)c);
c = FLASH_ReadByte(0x4080);
printf("c=%x\r\n",(u16)c);
c = FLASH_ReadByte(0x4100);
printf("c=%x\r\n",(u16)c);

_asm("rim");
while (1)
  {
  GPIO_WriteHigh(GPIOD, GPIO_PIN_LNIB);

}
}

char putchar (char c)    //串口打印重定向,只要有这个函数,然后包含stdio.h就可以直接调用打印函数了,若有不成功时请把参数前强转为(u16)
{
  if (c == '\n')
  {
    /* put '\r' to hardware here */
    /* Wait transmission is completed : otherwise the first data is not sent */
    while (!(UART1->SR & 0x40));
    UART1->DR = ('\r');
    /* Wait transmission is completed */
    while (!(UART1->SR & 0x40));
}
  /* put c to hardware here */
  /* Wait transmission is completed : otherwise the first data is not sent */
  while (!(UART1->SR & 0x80));
  UART1->DR = (c);
  /* Wait transmission is completed */
  while (!(UART1->SR & 0x80));
  return (c);
}

沙发
叫我女王思密达|  楼主 | 2016-11-3 00:10 | 只看该作者

这是我的实验例程,板子是stm8s207s6,函数的意思很明确,不再解释,下面开始解析配置步骤!
/**
@code
All the functions defined below must be executed from RAM exclusively, except
for the FLASH_WaitForLastOperation function which can be executed from Flash.
Steps of the execution from RAM differs from one toolchain to another:
- For Cosmic Compiler:
    1- Define a segment FLASH_CODE by the mean of " #pragma section (FLASH_CODE)".
    This segment is defined in the stm8s_flash.c file.
  2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8s.h file,
    or define it in Cosmic compiler preprocessor to enable the FLASH_CODE segment
   definition.
  3- In STVD Select Project\Settings\Linker\Category "input" and in the RAM section
    add the FLASH_CODE segment with "-ic" options.
  4- In main.c file call the _fctcpy() function with first segment character as
    parameter "_fctcpy('F');" to load the declared moveable code segment
    (FLASH_CODE) in RAM before execution.
  5- By default the _fctcpy function is packaged in the Cosmic machine library,
    so the function prototype "int _fctcopy(char name);" must be added in main.c
    file.
解析(stvd+cosmic+4.0官方库函数):
  第一步:#pragma section (FLASH_CODE)库函数stm8s_flash.c中代码第520行已经有了,并且没被注释掉,原型为:
#if defined (_COSMIC_) && defined (RAM_EXECUTION)
#pragma section (FLASH_CODE)
#endif  /* _COSMIC_ && RAM_EXECUTION */
IN_RAM(FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef FLASH_MemType))
{
      ......//库函数代码
}
IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType))
{
    ......库函数代码
}
IN_RAM(void FLASH_ProgramBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType,
                        FLASH_ProgramMode_TypeDef FLASH_ProgMode, uint8_t *Buffer))
{
     ......//库函数代码
}
#if defined (_COSMIC_) && defined (RAM_EXECUTION)
/* End of FLASH_CODE section */
#pragma section ()
#endif /* _COSMIC_ && RAM_EXECUTION */

第二步:打开宏开关#define RAM_EXECUTION  (1),在stm8s.h中
#if !defined (RAM_EXECUTION)
#define RAM_EXECUTION  (1)
#endif /* RAM_EXECUTION */
第三步:工程设置

第四步:在main.c中声明int _fctcpy(char name);在主函数运行前调用_fctcpy('F');具体操作可参考主函数代码
      这几步都配置完成后就可以编译通过了,然后烧录调试完美块编程,包括快擦除等,已经验证!
      借此说下王志杰的例程我实验过,但是写0就挂掉,不知道什么原因,还有两把密钥的顺序不是随便的,解锁eeprom时顺序是0xae,0x56,解锁flash时顺序是0x56,0xae,我见过网上有人说顺序不分的,纠正下,不要误人子弟!
      网上关于stm8s的学习资料很多很泛滥,其实有用的就那点,等我抽时间整理出一套来分享,以便后人学习!

/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8s.h"
#include <stdio.h>
int _fctcpy(char name);
main()
{
u8 block_buff[128] ={0};
u8 i = 0;
u8 c = 0;
_fctcpy('F');

CLK_DeInit();
  CLK_HSICmd(ENABLE);
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);

  UART1_DeInit();
  UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE,UART1_MODE_TXRX_ENABLE);
  UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
  UART1_Cmd(ENABLE);
  
GPIO_DeInit(GPIOD);
  GPIO_Init(GPIOD, GPIO_PIN_LNIB, GPIO_MODE_OUT_PP_LOW_FAST);

for(i=0;i<128;i++)
   block_buff=i;

  FLASH_DeInit();
  FLASH_Unlock(FLASH_MEMTYPE_DATA);
  FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_TPROG);
  FLASH_ProgramBlock(0x0001, FLASH_MEMTYPE_DATA, FLASH_PROGRAMMODE_STANDARD, block_buff);
FLASH_EraseBlock(0x0001, FLASH_MEMTYPE_DATA);

c = FLASH_ReadByte(0x40ff);//读出值到变量中
printf("c=%x\r\n",(u16)c);//打印
c = FLASH_ReadByte(0x407f);
printf("c=%x\r\n",(u16)c);
c = FLASH_ReadByte(0x4080);
printf("c=%x\r\n",(u16)c);
c = FLASH_ReadByte(0x4100);
printf("c=%x\r\n",(u16)c);

_asm("rim");
while (1)
  {
  GPIO_WriteHigh(GPIOD, GPIO_PIN_LNIB);
}
}
char putchar (char c)    //串口打印重定向,只要有这个函数,然后包含stdio.h就可以直接调用打印函数了,若有不成功时请把参数前强转为(u16)
{
  if (c == '\n')
  {
    /* put '\r' to hardware here */
    /* Wait transmission is completed : otherwise the first data is not sent */
    while (!(UART1->SR & 0x40));
    UART1->DR = ('\r');
    /* Wait transmission is completed */
    while (!(UART1->SR & 0x40));
}
  /* put c to hardware here */
  /* Wait transmission is completed : otherwise the first data is not sent */
  while (!(UART1->SR & 0x80));
  UART1->DR = (c);
  /* Wait transmission is completed */
  while (!(UART1->SR & 0x80));
  return (c);
}

copy到RAM里面执行,在STVD中有两种方法,1;通过定义一个数组code_to_flash[]将程序代码拷贝到该数组中然后通过汇编指令_asm("call _code_to_flash")的方法实现
2;另一种方法是通过stvd的的函数stcopy(),结合段得定义的来实现。


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

51

主题

81

帖子

1

粉丝