ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pdo_storage.cpp
Go to the documentation of this file.
1// Store / restore of the PDO parameters (0x1010 / 0x1011) in the preferences: canopen.request_pdos(..., storage=True).
2#include "canopen.h"
3
4#ifdef USE_CANOPEN_PDO_STORAGE
5
6#include <array>
7#include <span>
8#include <type_traits>
9
10#include "esphome/core/log.h"
11
12namespace esphome::canopen {
13
14ESPHOME_LOG_TAG(TAG, "canopen");
15
16// sub 0 of 0x1010 / 0x1011, and subs 1 (all) and 2 (communication), bit 0: saves on command / restores. CO_storage
17// never writes them (it reads with OD_readOriginal), so all hubs share them.
18static constexpr uint8_t STORAGE_HIGHEST_SUB = 2;
19static constexpr std::array<uint32_t, 2> STORAGE_CAPS{1, 1};
20
22 if (!this->has_pdos_())
23 return;
24 // stored parameters replace the defaults before the records point into them
25 this->load_pdos_();
26 auto *highest = const_cast<uint8_t *>(&STORAGE_HIGHEST_SUB);
27 auto *caps = const_cast<uint32_t *>(STORAGE_CAPS.data());
28 this->od_.add_array(0x1010, ODA_SDO_RW, highest, caps, sizeof(uint32_t), STORAGE_CAPS.size());
29 this->od_.add_array(0x1011, ODA_SDO_RW, highest, caps, sizeof(uint32_t), STORAGE_CAPS.size());
30}
31
33 if (!this->has_pdos_())
34 return true;
35 // one entry: the PDO parameters, 0x1010:2 / 0x1011:2 (and sub 1, "all"); the callbacks don't use addr / len
36 this->storage_entry_.addr = this;
37 this->storage_entry_.len = 0;
38 this->storage_entry_.subIndexOD = 2;
39 this->storage_entry_.attr = CO_storage_cmd | CO_storage_restore;
40 CO_ReturnError_t err =
41 CO_storage_init(&this->storage_, this->co_->CANmodule, this->od_.find(0x1010), this->od_.find(0x1011),
42 storage_store, storage_restore, &this->storage_entry_, 1);
43 if (err != CO_ERROR_NO) {
44 ESP_LOGE(TAG, "CO_storage_init failed: %d", err);
45 return false;
46 }
47 this->storage_.enabled = true;
48 return true;
49}
50
51// "load" (0x1011) sets the marker to this
52static constexpr uint32_t PDO_STORE_INVALID = 0;
53
54// The preference keys are a hash of the defaults, the node id and the hub (set_pdo_storage_key()), so parameters
55// stored from other defaults (another firmware, another PDO layout) are ignored instead of being applied to mappings
56// they weren't made for. The marker holds the whole hash too: some backends (ESP8266, RP2) find a preference by its
57// position and check the key only through a short checksum.
59 static_assert(std::is_trivially_copyable_v<PdoParams>, "PdoParams are stored as bytes");
60 // 42 bytes of fields and 2 of tail padding
61 static_assert(sizeof(PdoParams) == 44, "PdoParams stored layout");
62 uint32_t key = fnv1a_hash("canopen_pdo");
67 for (const PdoParams &p : std::span(this->pdos_, this->pdo_count_())) {
68 key = fnv1a_hash_extend(key, p.cob_id);
69 key = fnv1a_hash_extend(key, p.transmission_type);
70 key = fnv1a_hash_extend(key, p.inhibit_time);
71 key = fnv1a_hash_extend(key, p.event_timer);
72 key = fnv1a_hash_extend(key, p.map_count);
73 for (uint32_t m : p.map)
75 }
76 this->pdo_fingerprint_ = key != PDO_STORE_INVALID ? key : 1;
78 this->pdo_pref_ = global_preferences->make_preference(this->pdo_count_() * sizeof(PdoParams), key + 1, true);
79
80 uint32_t marker = PDO_STORE_INVALID;
81 if (!this->pdo_valid_pref_.load(&marker) || marker != this->pdo_fingerprint_)
82 return;
83 // straight into the table: the key covers its size, and a failed load leaves it as it was
84 if (!this->pdo_pref_.load(reinterpret_cast<uint8_t *>(this->pdos_), this->pdo_count_() * sizeof(PdoParams))) {
85 ESP_LOGW(TAG, "Stored PDO parameters unreadable, using the defaults");
86 return;
87 }
88 ESP_LOGI(TAG, "PDO parameters loaded from storage (0x1011 \"load\" restores the defaults)");
89}
90
92 // The table is committed before the marker (sync() goes on after a failed write): a failed table write leaves the
93 // previous save in place; once the table is synced, the save has taken effect
94 bool ok =
95 this->pdo_pref_.save(reinterpret_cast<const uint8_t *>(this->pdos_), this->pdo_count_() * sizeof(PdoParams)) &&
96 this->pdo_pref_.sync() && this->pdo_valid_pref_.save(&this->pdo_fingerprint_) && this->pdo_valid_pref_.sync();
97 if (ok) {
98 ESP_LOGI(TAG, "PDO parameters stored");
99 } else {
100 ESP_LOGE(TAG, "Storing the PDO parameters failed");
101 }
102 return ok;
103}
104
106 bool ok = this->pdo_valid_pref_.save(&PDO_STORE_INVALID) && this->pdo_valid_pref_.sync();
107 if (ok) {
108 ESP_LOGI(TAG, "PDO parameters: the defaults apply from the next boot");
109 } else {
110 ESP_LOGE(TAG, "Restoring the default PDO parameters failed");
111 }
112 return ok;
113}
114
115ODR_t CANopenComponent::storage_store(CO_storage_entry_t * /*entry*/, CO_CANmodule_t *module) {
116 return static_cast<CANopenComponent *>(module->CANptr)->store_pdos_() ? ODR_OK : ODR_HW;
117}
118
119ODR_t CANopenComponent::storage_restore(CO_storage_entry_t * /*entry*/, CO_CANmodule_t *module) {
120 return static_cast<CANopenComponent *>(module->CANptr)->restore_pdo_defaults_() ? ODR_OK : ODR_HW;
121}
122
123} // namespace esphome::canopen
124
125#endif // USE_CANOPEN_PDO_STORAGE
uint8_t m
Definition bl0906.h:1
bool sync()
Write this preference to flash now; ESP8266, RP2040, LibreTiny and host also write the other pending ...
ESPPreferenceObject pdo_pref_
Definition canopen.h:509
ESPPreferenceObject pdo_valid_pref_
Definition canopen.h:510
static ODR_t storage_store(CO_storage_entry_t *entry, CO_CANmodule_t *module)
CO_storage_entry_t storage_entry_
Definition canopen.h:507
static ODR_t storage_restore(CO_storage_entry_t *entry, CO_CANmodule_t *module)
void add_array(uint16_t index, OD_attr_t attr, uint8_t *count, void *data, OD_size_t elem_len, uint8_t n, OD_attr_t count_attr=ODA_SDO_R)
Definition od.cpp:39
const char * key
ESPHOME_LOG_TAG(TAG, "canopen")
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
Definition helpers.h:1169
ESPPreferences * global_preferences
constexpr uint32_t fnv1a_hash(const char *str)
Calculate a FNV-1a hash of str.
Definition helpers.h:1192
static void uint32_t
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
Definition preferences.h:24