Skip to content

Commit 7258706

Browse files
committed
improved read and write functions to check bounds and enforce bit alignment
1 parent 27cb4f1 commit 7258706

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

include/core/io/platform/f4xx/FMCf4xx.hpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
#ifndef EVT_FMC_HPP
22
#define EVT_FMC_HPP
33

4+
/**
5+
* FMC SDRAM driver for STM32F4 series.
6+
*
7+
* Provides a C++ abstraction for configuring and accessing external SDRAM
8+
* using the Flexible Memory Controller (FMC) peripheral.
9+
*
10+
* This driver wraps STM32 HAL functionality and simplifies:
11+
* - GPIO configuration for FMC pins
12+
* - SDRAM timing configuration
13+
* - SDRAM read/write operations
14+
*
15+
* @note Requires STM32 HAL libraries.
16+
*/
17+
418
#include "HALf4/stm32f4xx_hal.h"
519
#include "HALf4/stm32f4xx_ll_fmc.h"
620
#include "HALf4/stm32f4xx_hal_sdram.h"
@@ -106,12 +120,20 @@ typedef FMC_GPIO FMC_CMD;
106120

107121
namespace core::io {
108122

123+
/**
124+
* Driver for configuring and accessing external SDRAM via FMC.
125+
*
126+
* Class initializes the FMC peripheral and associated GPIO pins,
127+
* configures SDRAM timing parameters, and provides simple 32-bit
128+
* memory read/write access methods.
129+
*/
109130
class FMCf4xx {
110131
public:
111132
/**
112133
* Structure to simplify SDRAM initialization, pre-filled with default values
113134
*
114-
* Holds all the parameters needed to configure the SDRAM
135+
* Holds all SDRAM controller settings that map directly to
136+
* the HAL_SDRAM_Init configuration structure.
115137
*/
116138
struct SdramInitConfig {
117139
FMC_SDRAM_TypeDef* sdramDevice = FMC_SDRAM_DEVICE;
@@ -130,7 +152,7 @@ class FMCf4xx {
130152
/**
131153
* Structure to simplify SDRAM timing initialization, pre-filled with default values
132154
*
133-
* Holds all the parameters needed to configure the SDRAM timing
155+
* Contains all required SDRAM timing delays in clock cycles.
134156
*/
135157
struct SdramTimingConfig {
136158
uint32_t loadToActiveDelay = LOAD_MODE_REGISTER_TO_ACTIVE;
@@ -183,7 +205,14 @@ class FMCf4xx {
183205
};
184206

185207
/**
186-
* Structure to hold all FMC GPIO pins
208+
* Groups all FMC GPIO pin configurations.
209+
*
210+
* Contains arrays of:
211+
* - Address pins
212+
* - Data pins
213+
* - Bank select pins
214+
* - Command pins
215+
* - Byte enable pins
187216
*/
188217
struct FMCPinConfig {
189218
FMCAddressPins address;
@@ -196,11 +225,16 @@ class FMCf4xx {
196225
/**
197226
* Initializes an FMC device
198227
*
199-
* @param[in] pin_config a struct containing all FMC GPIO pin group arrays
200-
* @param[in] sdramInitConfig a struct containing all FMC SDRAM configuration
201-
* @param[in] sdramTimingConfig a struct containing all FMC SDRAM timing configuration variables
228+
* @param[in] pinConfig All FMC GPIO pin configurations.
229+
* @param[in] sdramInitConfig SDRAM controller configuration parameters.
230+
* @param[in] sdramTimingConfig SDRAM timing configuration parameters.
231+
*
232+
* - Enables FMC peripheral clock
233+
* - Initializes GPIO pins
234+
* - Configures SDRAM controller
235+
* - Calls HAL_SDRAM_Init()
202236
*/
203-
FMCf4xx(FMCPinConfig pin_config, SdramInitConfig sdramInitConfig, SdramTimingConfig sdramTimingConfig);
237+
FMCf4xx(FMCPinConfig pinConfig, SdramInitConfig sdramInitConfig, SdramTimingConfig sdramTimingConfig);
204238

205239
/**
206240
* Write a value to SDRAM at the specified byte offset.

src/core/io/platform/f4xx/FMCf4xx.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,31 @@ sdramTiming({0}) {
3737
sdramMemoryAddress = 0xD0000000;
3838
}
3939

40-
void FMCf4xx::write32(uint32_t offset, uint32_t value) const
41-
{
42-
volatile auto* ptr =
40+
void FMCf4xx::write32(uint32_t offset, uint32_t value) const {
41+
// Ensure 4-byte alignment
42+
if (offset % sizeof(uint32_t) != 0)
43+
return;
44+
45+
// Ensure within SDRAM bounds
46+
if (offset >= RAM_SIZE)
47+
return;
48+
49+
auto* const ptr =
4350
reinterpret_cast<volatile uint32_t*>(sdramMemoryAddress + offset);
4451

4552
*ptr = value;
4653
}
4754

48-
uint32_t FMCf4xx::read32(uint32_t offset) const
49-
{
50-
volatile auto* ptr =
55+
uint32_t FMCf4xx::read32(uint32_t offset) const {
56+
// Ensure 4-byte alignment
57+
if (offset % sizeof(uint32_t) != 0)
58+
return 0;
59+
60+
// Ensure within SDRAM bounds
61+
if (offset >= RAM_SIZE)
62+
return 0;
63+
64+
auto* const ptr =
5165
reinterpret_cast<volatile uint32_t*>(sdramMemoryAddress + offset);
5266

5367
return *ptr;

0 commit comments

Comments
 (0)