LUA: show protocol2 only on receivers with serial1_TX or PWM pins defined (#2999)
[ExpressLRS.git] / src / lib / elrs_eeprom / elrs_eeprom.cpp
blobcd874b9a96904962183c9939ed44e901d75b802c
1 #include "elrs_eeprom.h"
2 #include "targets.h"
3 #include "logging.h"
5 #if !defined(TARGET_NATIVE)
6 #if defined(PLATFORM_STM32)
7 #if defined(TARGET_USE_EEPROM) && defined(USE_I2C)
8 #if !defined(TARGET_EEPROM_ADDR)
9 #define TARGET_EEPROM_ADDR 0x51
10 #warning "!! Using default EEPROM address (0x51) !!"
11 #endif
13 #include <Wire.h>
14 #include <extEEPROM.h>
15 extEEPROM EEPROM(kbits_2, 1, 1, TARGET_EEPROM_ADDR);
16 #else
17 #define STM32_USE_FLASH
18 #include <utility/stm32_eeprom.h>
19 #endif
20 #else
21 #include <EEPROM.h>
22 #endif
24 void
25 ELRS_EEPROM::Begin()
27 #if defined(PLATFORM_STM32)
28 #if defined(STM32_USE_FLASH)
29 eeprom_buffer_fill();
30 #else // !STM32_USE_FLASH
31 // I2C initialization is the responsibility of the caller
32 // e.g. Wire.begin(GPIO_PIN_SDA, GPIO_PIN_SCL);
34 /* Initialize EEPROM */
35 #if defined(TARGET_EEPROM_400K)
36 EEPROM.begin(extEEPROM::twiClock400kHz, &Wire);
37 #else
38 EEPROM.begin(extEEPROM::twiClock100kHz, &Wire);
39 #endif
40 #endif // STM32_USE_FLASH
41 #else /* !PLATFORM_STM32 */
42 EEPROM.begin(RESERVED_EEPROM_SIZE);
43 #endif /* PLATFORM_STM32 */
46 uint8_t
47 ELRS_EEPROM::ReadByte(const uint32_t address)
49 if (address >= RESERVED_EEPROM_SIZE)
51 // address is out of bounds
52 ERRLN("EEPROM address is out of bounds");
53 return 0;
55 #if defined(STM32_USE_FLASH)
56 return eeprom_buffered_read_byte(address);
57 #else
58 return EEPROM.read(address);
59 #endif
62 void
63 ELRS_EEPROM::WriteByte(const uint32_t address, const uint8_t value)
65 if (address >= RESERVED_EEPROM_SIZE)
67 // address is out of bounds
68 ERRLN("EEPROM address is out of bounds");
69 return;
71 #if defined(STM32_USE_FLASH)
72 eeprom_buffered_write_byte(address, value);
73 #elif defined(PLATFORM_STM32)
74 EEPROM.update(address, value);
75 #else
76 EEPROM.write(address, value);
77 #endif
80 void
81 ELRS_EEPROM::Commit()
83 #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
84 if (!EEPROM.commit())
86 ERRLN("EEPROM commit failed");
88 #elif defined(STM32_USE_FLASH)
89 eeprom_buffer_flush();
90 #endif
91 // PLATFORM_STM32 with external flash every byte is committed as it is written
94 #endif /* !TARGET_NATIVE */