-
Notifications
You must be signed in to change notification settings - Fork 183
stm32g0xx_fdcan example project updated with eeprom storage routines #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheCitrusMan
wants to merge
15
commits into
CANopenNode:master
Choose a base branch
from
TheCitrusMan:storage/eeprom
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c05ab36
stm32g0xx_fdcan example project updated with eeprom storage routines
99fa8bd
Applied some modifications/fixes based on
2be3fc1
Correction in CO_eeprom_STM32.c
f5d0c05
Reduced I2C timeout time.
4bd3ab7
Separate handle for I2C eeprom.
300c9df
Move EEPROM I2C handle to i2c.h
7ceee2f
CO_eeprom_writeBlock() timeout error fix.
dcc0dec
Changes applied after the second run of github copilot.
1c16e1f
All projects updated with EEPROM functionality.
89da9a1
Merge branch 'master' into storage/eeprom
TheCitrusMan 00511f5
Messed something up, fixed.
bdd80fd
Fine tuning
dbfe149
Cleaned up EEPROM initialization.
TheCitrusMan 9700365
(retry push)
32a6868
Instead of invalidating the EEPROM by overwriting part of the EEPROM …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| /* | ||
| * Eeprom interface for use with CO_storageEeprom, STM32 specific | ||
| * | ||
| * @file CO_eeprom_STM32.c | ||
| * @author Marc Vandenhende | ||
| * @copyright 2025 Marc Vandenhende | ||
| * | ||
| * This file is part of CANopenNode, an opensource CANopen Stack. | ||
| * Project home page is <https://github.com/CANopenNode/CANopenNode>. | ||
| * For more information on CANopen see <http://www.can-cia.org/>. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #include "storage/CO_storage.h" | ||
| #include "storage/CO_eeprom.h" | ||
| #include "301/crc16-ccitt.h" | ||
| #include "CO_app_STM32.h" | ||
| #include "OD.h" | ||
|
|
||
| #include "eeprom.h" | ||
| #include "CO_eeprom_STM32.h" | ||
|
|
||
| #if ((CO_CONFIG_STORAGE)&CO_CONFIG_STORAGE_ENABLE) != 0 | ||
|
|
||
| /* | ||
| * Eeprom is configured for auto storage variables. Second half of | ||
| * memory locations is used for storage on command. Below are two | ||
| * internal variables, used for indicating next free address in eeprom, one for | ||
| * autonomous storage and one for protected storage | ||
| */ | ||
| static size_t eepromAddrNextAuto = 0; | ||
| static size_t eepromAddrNextProt = 0; | ||
|
|
||
| /******************************************************************************/ | ||
| bool_t CO_eeprom_init(void *storageModule) | ||
| { | ||
| // initialize canopen eeprom storage | ||
| if (!Eeprom_Init()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| eepromAddrNextAuto = device_start_co_auto; | ||
| eepromAddrNextProt = device_start_co_prot; | ||
| OD_PERSIST_COMM.x1018_identity.serialNumber = device_serial_number; | ||
|
|
||
| /* If eeprom chip is OK, this will return "true" */ | ||
| return (HAL_I2C_IsDeviceReady(HI2C_EEPROM, device_i2c_address << 1, 3, I2C_TIMEOUT_MS) == HAL_OK); | ||
| } | ||
|
|
||
| /******************************************************************************/ | ||
| size_t CO_eeprom_getAddr(void *storageModule, bool_t isAuto, size_t len, bool_t *overflow) | ||
| { | ||
| size_t addr; | ||
|
|
||
| if (isAuto) | ||
| { | ||
| /* auto storage is processed byte by byte, no alignment necessary */ | ||
| addr = eepromAddrNextAuto; | ||
| eepromAddrNextAuto += len; | ||
| if (eepromAddrNextAuto >= device_start_co_auto + (device_size_co / 2)) | ||
| { | ||
| *overflow = true; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| /* addresses for storage on command must be page aligned */ | ||
| addr = eepromAddrNextProt; | ||
| size_t lenAligned = len & (~(page_size - 1)); | ||
| if (lenAligned < len) | ||
| { | ||
| lenAligned += page_size; | ||
| } | ||
| eepromAddrNextProt += lenAligned; | ||
| if (eepromAddrNextProt >= device_start_co_prot + (device_size_co / 2)) | ||
| { | ||
| *overflow = true; | ||
| } | ||
| } | ||
|
|
||
| return addr; | ||
| } | ||
|
|
||
| /******************************************************************************/ | ||
| void CO_eeprom_readBlock(void *storageModule, uint8_t *data, size_t eepromAddr, size_t len) | ||
| { | ||
| HAL_I2C_Mem_Read(HI2C_EEPROM, device_i2c_address << 1, eepromAddr, 2, (uint8_t *) data, len, I2C_TIMEOUT_MS); | ||
| } | ||
|
|
||
| /******************************************************************************/ | ||
| bool_t CO_eeprom_writeBlock(void *storageModule, uint8_t *data, size_t eepromAddr, size_t len) | ||
| { | ||
| uint32_t idx = 0; | ||
| uint32_t eep_write_timer = 0; | ||
|
|
||
| if (HAL_I2C_IsDeviceReady(HI2C_EEPROM, device_i2c_address << 1, 3, I2C_TIMEOUT_MS) != HAL_OK) | ||
| { | ||
| // device not ready | ||
| return false; | ||
| } | ||
|
|
||
| while (len > 0) | ||
| { | ||
| size_t len_x = len; | ||
| if (len_x > page_size) | ||
| len_x = page_size; | ||
|
|
||
| if (HAL_I2C_Mem_Write(HI2C_EEPROM, device_i2c_address << 1, eepromAddr, 2, (uint8_t *) data + idx, len_x, | ||
| I2C_TIMEOUT_MS) != HAL_OK) | ||
| { | ||
| // write command error | ||
| return false; | ||
| } | ||
|
|
||
| eepromAddr += page_size; | ||
| idx += page_size; | ||
|
|
||
| if (len > page_size) | ||
| len -= page_size; | ||
| else | ||
| len = 0; | ||
|
|
||
| /* wait for completion of the write operation */ | ||
| eep_write_timer = HAL_GetTick(); | ||
| while (HAL_I2C_IsDeviceReady(HI2C_EEPROM, device_i2c_address << 1, 3, I2C_TIMEOUT_MS) != HAL_OK) | ||
| { | ||
| // device not ready yet | ||
| if (HAL_GetTick() - eep_write_timer >= EEPROM_WRITE_TIME + 1) | ||
| // + 1 to compensate for HAL_GetTick() millisecond jitter | ||
| { | ||
| // time out | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /******************************************************************************/ | ||
| uint16_t CO_eeprom_getCrcBlock(void *storageModule, size_t eepromAddr, size_t len) | ||
| { | ||
| #define BUF_SIZE 250 | ||
|
|
||
| uint16_t crc = 0; | ||
| uint8_t buf[BUF_SIZE]; | ||
| uint8_t subLen; | ||
|
|
||
| while (len > 0) | ||
| { | ||
| if (len <= BUF_SIZE) | ||
| subLen = len; | ||
| else | ||
| subLen = BUF_SIZE; | ||
|
|
||
| /* update crc from data part */ | ||
| HAL_I2C_Mem_Read(HI2C_EEPROM, device_i2c_address << 1, eepromAddr, 2, buf, subLen, I2C_TIMEOUT_MS); | ||
| crc = crc16_ccitt(buf, subLen, crc); | ||
| eepromAddr += subLen; | ||
| len -= subLen; | ||
| } | ||
|
|
||
| // return invalid CRC when CAN_CFG jumper is placed | ||
| if (!HAL_GPIO_ReadPin(CAN_CFG_GPIO_Port, CAN_CFG_Pin)) | ||
| { | ||
| crc = ~crc; | ||
| } | ||
|
|
||
| return crc; | ||
| } | ||
|
|
||
| /******************************************************************************/ | ||
| bool_t CO_eeprom_updateByte(void * storageModule, uint8_t data, | ||
| size_t eepromAddr) | ||
| { | ||
| uint8_t buf; | ||
|
|
||
| if (HAL_I2C_IsDeviceReady(HI2C_EEPROM, device_i2c_address << 1, 3, I2C_TIMEOUT_MS) | ||
| != HAL_OK) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| /* read data byte from eeprom */ | ||
| if (HAL_I2C_Mem_Read(HI2C_EEPROM, device_i2c_address << 1, eepromAddr, 2, &buf, 1, I2C_TIMEOUT_MS) != HAL_OK) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| /* If data in EEPROM differs, then write it to EEPROM. | ||
| * Don't wait for write to complete */ | ||
| if (buf != data) | ||
| { | ||
| if (HAL_I2C_Mem_Write(HI2C_EEPROM, device_i2c_address << 1, eepromAddr, 2, &data, 1, I2C_TIMEOUT_MS) != HAL_OK) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| #endif /* (CO_CONFIG_STORAGE) & CO_CONFIG_STORAGE_ENABLE */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* | ||
| * CO_eeprom_STM32.h | ||
| * | ||
| * Created on: 23 Nov 2025 | ||
| * Author: Marc Vandenhende | ||
| */ | ||
|
|
||
| #ifndef CO_EEPROM_STM32_H_ | ||
| #define CO_EEPROM_STM32_H_ | ||
|
|
||
| #endif /* CO_EEPROM_STM32_H_ */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.