px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_eeprom.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2018 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_eeprom.h : Internal EEPROM write routines
14 Author(s): Pieter Conradie
15 Creation Date: 2018-11-21
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20
21/* _____PROJECT INCLUDES_____________________________________________________ */
22#include "px_eeprom.h"
23#include "px_stm32cube.h"
24#include "px_log.h"
25
26/* _____LOCAL DEFINITIONS____________________________________________________ */
27PX_LOG_NAME("px_eeprom");
28
29/* _____MACROS_______________________________________________________________ */
30
31/* _____GLOBAL VARIABLES_____________________________________________________ */
32
33/* _____LOCAL VARIABLES______________________________________________________ */
34
35/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
36
37/* _____LOCAL FUNCTIONS______________________________________________________ */
38
39/* _____GLOBAL FUNCTIONS_____________________________________________________ */
40PX_ATTR_RAMFUNC void px_eeprom_unlock(void)
41{
42 uint32_t primask;
43
44 // Save interrupt status
45 primask = __get_PRIMASK();
46 // Disable interrupts
47 px_interrupts_disable();
48 // Unlock PELOCK
49 FLASH->PEKEYR = 0x89abcdef;
50 FLASH->PEKEYR = 0x02030405;
51 // Restore interrupt status
52 __set_PRIMASK(primask);
53}
54
55PX_ATTR_RAMFUNC void px_eeprom_lock(void)
56{
57 uint32_t primask;
58
59 // Save interrupt status
60 primask = __get_PRIMASK();
61 // Disable interrupts
62 px_interrupts_disable();
63 // Lock PELOCK
64 FLASH->PECR |= FLASH_PECR_PELOCK;
65 // Restore interrupt status
66 __set_PRIMASK(primask);
67}
68
69PX_ATTR_RAMFUNC void px_eeprom_erase_word(const uint32_t address)
70{
71 uint32_t primask;
72
73 // Save interrupt status
74 primask = __get_PRIMASK();
75 // Disable interrupts
76 px_interrupts_disable();
77 // Enable page erase
78 FLASH->PECR |= (FLASH_PECR_ERASE | FLASH_PECR_DATA);
79 // Write 32-bit zero value to specified address
80 *(uint32_t *)address = (uint32_t)0;
81 // Wait until erase has finished (not busy)
82 while((FLASH->SR & FLASH_SR_BSY) != 0) {;}
83 // EOP (End Of Programming) Flag set?
84 if((FLASH->SR & FLASH_SR_EOP) != 0)
85 {
86 // Reset EOP flag
87 FLASH->SR = FLASH_SR_EOP;
88 }
89 // Disable page erase
90 FLASH->PECR &= ~(FLASH_PECR_ERASE | FLASH_PECR_DATA);
91 // Restore interrupt status
92 __set_PRIMASK(primask);
93}
#define PX_LOG_NAME(name)
Macro to declare a log name string once for each file to reduce code size.
Definition: px_log.h:349
PX_ATTR_RAMFUNC void px_eeprom_lock(void)
Lock EEPROM to prevent programming or erase.
Definition: px_eeprom.c:55
PX_ATTR_RAMFUNC void px_eeprom_unlock(void)
Unlock EEPROM for programming and erase.
Definition: px_eeprom.c:40
PX_ATTR_RAMFUNC void px_eeprom_erase_word(const uint32_t address)
Function to erase a word (not really required).
Definition: px_eeprom.c:69