Skip to content

Instantly share code, notes, and snippets.

@pasindu-sandima
Last active January 6, 2021 18:02
Show Gist options
  • Save pasindu-sandima/3e98112fe42fb4df12bf9b9777b202ab to your computer and use it in GitHub Desktop.
Save pasindu-sandima/3e98112fe42fb4df12bf9b9777b202ab to your computer and use it in GitHub Desktop.
main.c
#include "main.h"
void SystemClock_Config(void);
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
SystemClock_Config();
//Unlocks the flash
HAL_FLASH_Unlock();
//Instantiate the FLASH_EraseInitTypeDef struct needed for the HAL_FLASHEx_Erase() function
FLASH_EraseInitTypeDef FLASH_EraseInitStruct = {0};
FLASH_EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; //Erase type set to erase pages( Available other type is mass erase)
FLASH_EraseInitStruct.PageAddress = 0x0801FC00; //Starting address of flash page (0x0800 0000 - 0x0801 FC00)
FLASH_EraseInitStruct.NbPages = 1; //The number of pages to be erased
uint32_t errorStatus = 0;
HAL_FLASHEx_Erase(&FLASH_EraseInitStruct,&errorStatus); //Call the HAL function to erase one page from the flash memory
uint64_t FData = 0x1A2B3C4D5E6F1234; // Initialize the data to be written into the flash memory
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x0801FC00, FData); // Calling the HAL function to write the at data at the given memory address
//Make sure that you have erased the flash memory page which the memory address belongs. Otherwise the data written can have errors.
uint64_t * RDAddr = (uint64_t *) 0x0801FC00;
uint64_t RData = *RDAddr;
while (1){}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
}
#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)
{
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment