STM8S/A Standard Peripherals Firmware Library: main.c Source File

STM8S/A

STM8S_StdPeriph_Examples/FLASH/FLASH_DataProgram/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file     FLASH/FLASH_DataProgram/main.c
00004   * @author   MCD Application Team
00005   * @version  V2.2.0
00006   * @date     30-September-2014
00007   ******************************************************************************
00008   * @attention
00009   *
00010   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00011   *
00012   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00013   * You may not use this file except in compliance with the License.
00014   * You may obtain a copy of the License at:
00015   *
00016   *        http://www.st.com/software_license_agreement_liberty_v2
00017   *
00018   * Unless required by applicable law or agreed to in writing, software 
00019   * distributed under the License is distributed on an "AS IS" BASIS, 
00020   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021   * See the License for the specific language governing permissions and
00022   * limitations under the License.
00023   *
00024   ******************************************************************************
00025   */ 
00026 
00027 /* Includes ------------------------------------------------------------------*/
00028 #include "stm8s.h"
00029 #include "stm8s_eval.h"
00030 
00031 /**
00032   * @addtogroup FLASH_DataProgram
00033   * @{
00034   */
00035 
00036 /* Private typedef -----------------------------------------------------------*/
00037 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
00038 /* Private define ------------------------------------------------------------*/
00039 #define BLOCK_OPERATION    0    /* block 0 in data eeprom memory: address is 0x4000 */
00040 /* Private macro -------------------------------------------------------------*/
00041 /* Private variables ---------------------------------------------------------*/
00042 uint8_t GBuffer[FLASH_BLOCK_SIZE];
00043 __IO TestStatus OperationStatus;
00044 
00045 #ifdef _RAISONANCE_
00046 /* needed by memcpy for raisonance */
00047  #include <string.h>
00048  extern int __address__FLASH_EraseBlock;
00049  extern int __size__FLASH_EraseBlock;
00050  extern int __address__FLASH_ProgramBlock;
00051  extern int __size__FLASH_ProgramBlock;
00052 #endif /*_RAISONANCE_*/
00053 
00054 /* Private function prototypes -----------------------------------------------*/
00055 /* Declare _fctcpy function prototype as it is packaged by default in the Cosmic
00056    machine library */
00057 #ifdef _COSMIC_
00058  int _fctcpy(char name);
00059 #endif /*_COSMIC_*/
00060 void Delay (uint16_t nCount);
00061 static void CLK_Config(void);
00062 static void GPIO_Config(void);
00063 static void FLASH_Config(void);
00064 /* Private functions ---------------------------------------------------------*/
00065 
00066 /**
00067   * @brief  Main program.
00068   * @param  None
00069   * @retval None
00070   */
00071 void main(void)
00072 {
00073   uint32_t add = 0, startadd = 0, stopadd = 0;
00074   uint8_t newval = 0xAA;
00075   uint8_t i = 0;
00076 
00077 #ifdef _COSMIC_
00078 /* Call the _fctcpy() function with the first segment character as parameter 
00079    "_fctcpy('F');"  for a manual copy of the declared moveable code segment
00080    (FLASH_CODE) in RAM before execution*/
00081   _fctcpy('F');
00082 #endif /*_COSMIC_*/
00083 
00084 #ifdef _RAISONANCE_
00085 /* Call the standard C library: memcpy() or fmemcpy() functions available through 
00086    the <string.h> to copy the inram function to the RAM destination address */
00087   MEMCPY(FLASH_EraseBlock,
00088          (void PointerAttr*)&__address__FLASH_EraseBlock,
00089          (int)&__size__FLASH_EraseBlock);
00090   MEMCPY(FLASH_ProgramBlock,
00091          (void PointerAttr*)&__address__FLASH_ProgramBlock,
00092          (int)&__size__FLASH_ProgramBlock);
00093 #endif /*_RAISONANCE_*/
00094 
00095 
00096   /* Clock configuration -----------------------------------------*/
00097   CLK_Config();
00098 
00099   /* GPIO Configuration ------------------------------------------*/
00100   GPIO_Config();
00101 
00102   /* FLASH Configuration ------------------------------------------*/
00103   FLASH_Config();
00104 
00105   /* Fill the buffer in RAM */
00106   for (i = 0; i < FLASH_BLOCK_SIZE; i++)
00107   {
00108     GBuffer[i] = newval;
00109   }
00110   /* This function is executed from RAM */
00111   FLASH_ProgramBlock(BLOCK_OPERATION, FLASH_MEMTYPE_DATA, FLASH_PROGRAMMODE_STANDARD, GBuffer);
00112   
00113   /* Wait until End of high voltage flag is set*/
00114   while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET)
00115   {}
00116   /* Check the programmed block */
00117   startadd = FLASH_DATA_START_PHYSICAL_ADDRESS + ((uint16_t)BLOCK_OPERATION * (uint16_t)FLASH_BLOCK_SIZE);
00118   stopadd = startadd + (uint16_t)FLASH_BLOCK_SIZE;
00119   for (add = startadd; add < stopadd; add++)
00120       {
00121         if (FLASH_ReadByte(add) != newval)
00122         {
00123           /* Error */
00124           OperationStatus = FAILED;
00125           /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */
00126           /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */
00127           while (1)
00128           {
00129             STM_EVAL_LEDToggle(LED1); /*FAIL: write error */
00130             Delay(0xFFFF);
00131           }
00132         }
00133       }
00134   /* Erase block 0 and verify it */
00135   /* This function is executed from RAM */
00136   FLASH_EraseBlock(BLOCK_OPERATION, FLASH_MEMTYPE_DATA);
00137 
00138   /* Wait until End of high voltage flag is set*/
00139   while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET)
00140   {}
00141 
00142   for (add = startadd; add < stopadd; add++)
00143       {
00144         if (FLASH_ReadByte(add) != 0x00)
00145         {
00146           /* Error */
00147           OperationStatus = FAILED;
00148           /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */
00149           /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */
00150           while (1)
00151           {
00152             STM_EVAL_LEDToggle(LED2); /* FAIL: Erase error */
00153             Delay(0xFFFF);
00154           }
00155         }
00156       }
00157 
00158   /* Pass */
00159   OperationStatus = PASSED;
00160   /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */
00161   /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */
00162   while (1)
00163   {
00164     STM_EVAL_LEDToggle(LED3); /* PASS: without errors*/
00165     Delay(0xFFFF);
00166   }
00167 }
00168 
00169 /**
00170   * @brief  Configure system clock to run at 16Mhz
00171   * @param  None
00172   * @retval None
00173   */
00174 void CLK_Config(void)
00175 {
00176     /* Initialization of the clock */
00177     /* Clock divider to HSI/1 */
00178     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00179 }
00180 
00181 /**
00182   * @brief  Configure GPIO for LEDs available on the evaluation board
00183   * @param  None
00184   * @retval None
00185   */
00186 void GPIO_Config(void)
00187 {
00188     /* Initialize LEDs mounted on STM8-128 EVAL board */
00189     STM_EVAL_LEDInit(LED2);
00190     STM_EVAL_LEDInit(LED3);
00191     STM_EVAL_LEDInit(LED1);
00192                 
00193                 /* All leds are Off */
00194     STM_EVAL_LEDOff(LED2);
00195     STM_EVAL_LEDOff(LED3);
00196     STM_EVAL_LEDOff(LED1);
00197 }
00198 
00199 /**
00200   * @brief  Configure the FLASH for block programming
00201   * @param  None
00202   * @retval None
00203   */
00204 void FLASH_Config(void)
00205 {
00206  /* Define flash programming Time*/
00207   FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
00208 
00209   FLASH_Unlock(FLASH_MEMTYPE_PROG);
00210   /* Wait until Flash Program area unlocked flag is set*/
00211   while (FLASH_GetFlagStatus(FLASH_FLAG_PUL) == RESET)
00212   {}
00213 
00214   /* Unlock flash data eeprom memory */
00215   FLASH_Unlock(FLASH_MEMTYPE_DATA);
00216   /* Wait until Data EEPROM area unlocked flag is set*/
00217   while (FLASH_GetFlagStatus(FLASH_FLAG_DUL) == RESET)
00218   {}
00219 }
00220 
00221 /**
00222   * @brief  Delay.
00223   * @param  nCount
00224   * @retval None
00225   */
00226 void Delay(uint16_t nCount)
00227 {
00228     /* Decrement nCount value */
00229     while (nCount != 0)
00230     {
00231         nCount--;
00232     }
00233 }
00234 
00235 #ifdef  USE_FULL_ASSERT
00236 /**
00237   * @brief  Reports the name of the source file and the source line number
00238   *   where the assert_param error has occurred.
00239   * @param  file: pointer to the source file name
00240   * @param  line: assert_param error line source number
00241   * @retval None
00242   */
00243 void assert_failed(uint8_t* file, uint32_t line)
00244 {
00245   /* User can add his own implementation to report the file name and line number,
00246      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00247 
00248   /* Infinite loop */
00249   while (1)
00250   {}
00251 }
00252 #endif
00253 
00254 /**
00255   * @}
00256   */
00257 
00258 /**
00259   * @}
00260   */
00261 
00262 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com