Merge pull request #1269 from pkendall64/crsf-max-output
[ExpressLRS.git] / src / lib / EEPROM / elrs_eeprom.cpp
blobae1cb512c2810d12197c4faa307b80cd8b742ad0
1 #include "elrs_eeprom.h"
2 #include "targets.h"
3 #include "logging.h"
5 #if defined(PLATFORM_STM32)
6 #if defined(TARGET_USE_EEPROM) && \
7 defined(GPIO_PIN_SDA) && (GPIO_PIN_SDA != UNDEF_PIN) && \
8 defined(GPIO_PIN_SCL) && (GPIO_PIN_SCL != UNDEF_PIN)
9 #if !defined(TARGET_EEPROM_ADDR)
10 #define TARGET_EEPROM_ADDR 0x51
11 #warning "!! Using default EEPROM address (0x51) !!"
12 #endif
14 #include <Wire.h>
15 #include <extEEPROM.h>
16 extEEPROM EEPROM(kbits_2, 1, 1, TARGET_EEPROM_ADDR);
17 #else
18 #define STM32_USE_FLASH
19 #include <utility/stm32_eeprom.h>
20 #endif
21 #else
22 #include <EEPROM.h>
23 #endif
25 void
26 ELRS_EEPROM::Begin()
28 #if defined(PLATFORM_STM32)
29 #if defined(STM32_USE_FLASH)
30 eeprom_buffer_fill();
31 #else // !STM32_USE_FLASH
32 /* Initialize I2C */
33 Wire.setSDA(GPIO_PIN_SDA);
34 Wire.setSCL(GPIO_PIN_SCL);
35 Wire.begin();
36 /* Initialize EEPROM */
37 #if defined(TARGET_EEPROM_400K)
38 EEPROM.begin(extEEPROM::twiClock400kHz, &Wire);
39 #else
40 EEPROM.begin(extEEPROM::twiClock100kHz, &Wire);
41 #endif
42 #endif // STM32_USE_FLASH
43 #else /* !PLATFORM_STM32 */
44 EEPROM.begin(RESERVED_EEPROM_SIZE);
45 #endif /* PLATFORM_STM32 */
48 uint8_t
49 ELRS_EEPROM::ReadByte(const uint32_t address)
51 if (address >= RESERVED_EEPROM_SIZE)
53 // address is out of bounds
54 ERRLN("EEPROM address is out of bounds");
55 return 0;
57 #if defined(STM32_USE_FLASH)
58 return eeprom_buffered_read_byte(address);
59 #else
60 return EEPROM.read(address);
61 #endif
64 void
65 ELRS_EEPROM::WriteByte(const uint32_t address, const uint8_t value)
67 if (address >= RESERVED_EEPROM_SIZE)
69 // address is out of bounds
70 ERRLN("EEPROM address is out of bounds");
71 return;
73 #if defined(STM32_USE_FLASH)
74 eeprom_buffered_write_byte(address, value);
75 #elif defined(PLATFORM_STM32)
76 EEPROM.update(address, value);
77 #else
78 EEPROM.write(address, value);
79 #endif
82 void
83 ELRS_EEPROM::Commit()
85 #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
86 if (!EEPROM.commit())
88 ERRLN("EEPROM commit failed");
90 #elif defined(STM32_USE_FLASH)
91 eeprom_buffer_flush();
92 #endif