/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8s.h"
#include "stm8s_flash.h"
#include "stm8s_gpio.h"
#include "stm8s_clk.h"
@eeprom unsigned char EePromData[10]={0,1,2,3,4,5,6,7,8,9};
/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define BLOCK_OPERATION 0 /* block 0 in data eeprom memory: address is 0x4000 */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t GBuffer[FLASH_BLOCK_SIZE];
__IO TestStatus OperationStatus;
#ifdef _RAISONANCE_
/* needed by memcpy for raisonance */
#include <string.h>
extern int __address__FLASH_EraseBlock;
extern int __size__FLASH_EraseBlock;
extern int __address__FLASH_ProgramBlock;
extern int __size__FLASH_ProgramBlock;
#endif /*_RAISONANCE_*/
/* Private function prototypes -----------------------------------------------*/
/* Declare _fctcpy function prototype as it is packaged by default in the Cosmic
machine library */
#ifdef _COSMIC_
int _fctcpy(char name);
#endif /*_COSMIC_*/
void Delay (uint16_t nCount);
static void CLK_Config(void);
static void GPIO_Config(void);
static void FLASH_Config(void);
typedef struct
{
unsigned char ODR0 : 1;
unsigned char ODR1 : 1;
unsigned char ODR2 : 1;
unsigned char ODR3 : 1;
unsigned char ODR4 : 1;
unsigned char ODR5 : 1;
unsigned char ODR6 : 1;
unsigned char ODR7 : 1;
} __BITS_ODR;
#define PB_ODR ((__BITS_ODR *) 0x5005)
#define LED PB_ODR->ODR5
void delayms(unsigned int t)
{
unsigned int tloop,ttloop;
for(tloop=0;tloop<t;tloop++)
{
for(ttloop=0;ttloop<200;ttloop++)
{
_asm("nop");
}
}
}
#define u8 unsigned char
#define U16 unsigned int
#define U8 unsigned char
#define EEPMASS1 0xAE //密码钥匙1
#define EEPMASS2 0x56 //密码钥匙2
#define EEPADDR 0x4000 //eep地址起始
#define FLASH_DUKR FLASH->DUKR
#define FLASH_IAPSR FLASH->IAPSR
#define FLASH_CR1 FLASH->CR1
#define FLASH_CR2 FLASH->CR2
#define FLASH_NCR2 FLASH->NCR2
#define FLASH_PUKR FLASH->PUKR
#define FLASH_NFPR FLASH->NFPR
void eep_word_write(U16 u16Addr, U8 *pdatas, U8 len)
{
U8 *eep=(U8*)u16Addr;
U8 j;
FLASH_DUKR=EEPMASS1; //锁1密钥
FLASH_DUKR=EEPMASS2; //锁2密钥
while(!(FLASH_IAPSR&0x08)); //等待解密就绪
// DISABLE_MAC_INTERRUPTS();
FLASH_CR2 |= 0x40; //
FLASH_NCR2 &= ~0x40; //设置字编程
for(j=0; j<len; j++)
{
eep[j] = pdatas[j];
}
delayms(100);
// while(!(FLASH_IAPSR&0x04));
// FLASH_IAPSR &=0xf7; // off the eeprom program
// ENABLE_MAC_INTERRUPTS();
}
void eeprom_read(U16 u16Addr, U8 *pdatas, U8 len)
{
U8 *eep=(U8 *)u16Addr;
U8 j;
for(j=0; j<len; j++)
{
pdatas[j]=eep[j];
}
}
main()
{
u8 tval;
u8 *p=(u8 *)0x4000;
u8 buff[128];
GPIOB->CR1=0xF0;
GPIOB->CR2=0xF0;
GPIOB->DDR=0xF0;
//eep_word_write(0x4000,buff,3);
//eeprom_read(0x4000,buff,3);
buff[0]=0x33;buff[1]=0x33;buff[2]=0x33;buff[3]=0x33;buff[4]=0x33;
FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
FLASH_Unlock(FLASH_MEMTYPE_PROG);
/* Wait until Flash Program area unlocked flag is set*/
while (FLASH_GetFlagStatus(FLASH_FLAG_PUL) == RESET)
{}
FLASH_ProgramByte(0x4005,0x55);
//EePromData[0]=9;
//eep_word_write(0x4000,buff,2);
delayms(500);
tval=EePromData[4];
tval=p[4];
while (1)
{
delayms(500);
LED=1;
delayms(500);
LED=0;
}
}
#if 0
main()
{
uint32_t add = 0, startadd = 0, stopadd = 0;
uint8_t newval = 0xAA;
uint8_t i = 0;
#ifdef _COSMIC_
/* Call the _fctcpy() function with the first segment character as parameter
"_fctcpy('F');" for a manual copy of the declared moveable code segment
(FLASH_CODE) in RAM before execution*/
_fctcpy('F');
#endif /*_COSMIC_*/
#ifdef _RAISONANCE_
/* Call the standard C library: memcpy() or fmemcpy() functions available through
the <string.h> to copy the inram function to the RAM destination address */
MEMCPY(FLASH_EraseBlock,
(void PointerAttr*)&__address__FLASH_EraseBlock,
(int)&__size__FLASH_EraseBlock);
MEMCPY(FLASH_ProgramBlock,
(void PointerAttr*)&__address__FLASH_ProgramBlock,
(int)&__size__FLASH_ProgramBlock);
#endif /*_RAISONANCE_*/
/* Clock configuration -----------------------------------------*/
CLK_Config();
/* GPIO Configuration ------------------------------------------*/
GPIO_Config();
/* FLASH Configuration ------------------------------------------*/
FLASH_Config();
/* Fill the buffer in RAM */
for (i = 0; i < FLASH_BLOCK_SIZE; i++)
{
GBuffer = newval;
}
/* This function is executed from RAM */
FLASH_ProgramBlock(BLOCK_OPERATION, FLASH_MEMTYPE_DATA, FLASH_PROGRAMMODE_STANDARD, GBuffer);
/* Wait until End of high voltage flag is set*/
// while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET)
while (FLASH_GetFlagStatus(FLASH_FLAG_EOP) == RESET)
{}
/* Check the programmed block */
startadd = FLASH_DATA_START_PHYSICAL_ADDRESS + ((uint16_t)BLOCK_OPERATION * (uint16_t)FLASH_BLOCK_SIZE);
stopadd = startadd + (uint16_t)FLASH_BLOCK_SIZE;
for (add = startadd; add < stopadd; add++)
{
if (FLASH_ReadByte(add) != newval)
{
/* Error */
OperationStatus = FAILED;
/* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */
/* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */
while (1)
{
Delay(0xFFFF);
}
}
}
/* Erase block 0 and verify it */
/* This function is executed from RAM */
FLASH_EraseBlock(BLOCK_OPERATION, FLASH_MEMTYPE_DATA);
/* Wait until End of high voltage flag is set*/
while (FLASH_GetFlagStatus(FLASH_FLAG_EOP) == RESET)
{}
for (add = startadd; add < stopadd; add++)
{
if (FLASH_ReadByte(add) != 0x00)
{
/* Error */
OperationStatus = FAILED;
/* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */
/* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */
while (1)
{
Delay(0xFFFF);
}
}
}
/* Pass */
OperationStatus = PASSED;
/* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */
/* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */
while (1)
{
Delay(0xFFFF);
}
GPIOB->CR1=0xF0;
GPIOB->CR2=0xF0;
GPIOB->DDR=0xF0;
while (1)
{
delayms(500);
LED=1;
delayms(500);
LED=0;
}
}
#endif
/**
* @brief Configure system clock to run at 16Mhz
* @param None
* @retval None
*/
void CLK_Config(void)
{
/* Initialization of the clock */
/* Clock divider to HSI/1 */
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
}
/**
* @brief Configure GPIO for LEDs available on the evaluation board
* @param None
* @retval None
*/
void GPIO_Config(void)
{
/* Initialize LEDs mounted on STM8-128 EVAL board */
}
/**
* @brief Configure the FLASH for block programming
* @param None
* @retval None
*/
void FLASH_Config(void)
{
/* Define flash programming Time*/
FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
FLASH_Unlock(FLASH_MEMTYPE_PROG);
/* Wait until Flash Program area unlocked flag is set*/
while (FLASH_GetFlagStatus(FLASH_FLAG_PUL) == RESET)
{}
/* Unlock flash data eeprom memory */
FLASH_Unlock(FLASH_MEMTYPE_DATA);
/* Wait until Data EEPROM area unlocked flag is set*/
while (FLASH_GetFlagStatus(FLASH_FLAG_DUL) == RESET)
{}
}
/**
* @brief Delay.
* @param nCount
* @retval None
*/
void Delay(uint16_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{}
}
#endif
|