Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Ignore .pre-commit-config.yaml
.pre-commit-config.yaml

#Ignore Makefile
Makefile
# Ignore other Branch specific dirs
PeripheralDriversSubmodule/NAU7802/*
PeripheralDriversSubmodule/MX66L1G45GMI/*
2 changes: 2 additions & 0 deletions DMA/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore txt files containing notes about DMA
DMA-NOTES.txt
19 changes: 17 additions & 2 deletions MX66L1G45GMI/Inc/MX66L1G45GMI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @file MX66L1G45GMI.hpp
* @author shiva
* @date Mar 26, 2025
* @brief
* @brief Header for MX66L1G45GMI Flash Memory Driver.
********************************************************************************
*/

Expand All @@ -28,6 +28,19 @@
* TYPEDEFS
************************************/

// ---< Config Struct >---
typedef void (*MX66_WriteFunc)(const uint8_t* data, uint16_t len); // Write function pointer
typedef void (*MX66_ReadFunc)(uint8_t* data, uint16_t len); // Read function pointer
typedef void (*MX66_CSFunc)(bool state); // Chip Select function pointer
typedef void (*MX66_DelayFunc)(uint32_t ms); // Delay function pointer

struct MX66_Config {
MX66_WriteFunc Write;
MX66_ReadFunc Read;
MX66_CSFunc SetCS;
MX66_DelayFunc Delay;
};

/************************************
* CLASS DEFINITIONS
************************************/
Expand All @@ -36,10 +49,12 @@
* FUNCTION DECLARATIONS
************************************/

uint32_t MX66_ReadID(void);
uint32_t MX66_ReadID(void); // Read Manufacturer and Device ID

uint8_t MX66_ReadStatus(int reg); // Read status reg1,2,3

void MX66_Init(MX66_Config* config);

void MX66_WriteStatus(int reg, uint8_t newstatus);

void MX66_ReadSFDP(uint8_t *rData);
Expand Down
70 changes: 53 additions & 17 deletions MX66L1G45GMI/MX66L1G45GMI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,65 @@ extern SPI_HandleTypeDef hspi1;
//-------------------------------------------------------------------------------------------------
// STM32 SPI Driver
//-------------------------------------------------------------------------------------------------
void MX66_Delay(uint32_t time)
{
HAL_Delay(time);

// Create a static instance of the config
static MX66_Config io_driver = {nullptr, nullptr, nullptr, nullptr};

// Init function
void MX66_Init(MX66_Config* config) {
if(config != nullptr) {
io_driver = *config;
}
else {
SOAR_PRINT("MX66_Init: config is nullptr!\n");
}
}

void csLOW(void)
{
HAL_GPIO_WritePin(SPI_FLASH_CS_GPIO_Port, SPI_FLASH_CS_Pin, GPIO_PIN_RESET);
// Internal Helper Wrappers


void MX66_Delay(uint32_t ms) {
if(io_driver.Delay) {
io_driver.Delay(ms);
} else {
SOAR_PRINT("[MX66 ERROR] MX66_Delay called before MX66_Init!\n");
}
}

void csHIGH(void)
{
HAL_GPIO_WritePin(SPI_FLASH_CS_GPIO_Port, SPI_FLASH_CS_Pin, GPIO_PIN_SET);

void csLOW(void) {
if(io_driver.SetCS) {
io_driver.SetCS(false);
} else {
SOAR_PRINT("[MX66 ERROR] csLOW called before MX66_Init!\n");
}
}

void SPI_Write(uint8_t *data, uint16_t len)
{
HAL_SPI_Transmit(&MX66_SPI, data, len, 2000);

void csHIGH(void) {
if(io_driver.SetCS) {
io_driver.SetCS(true);
} else {
SOAR_PRINT("[MX66 ERROR] csHIGH called before MX66_Init!\n");
}
}

void SPI_Read(uint8_t *data, uint16_t len)
{
HAL_SPI_Receive(&MX66_SPI, data, len, 5000);

void SPI_Write(uint8_t *data, uint16_t len) {
if(io_driver.Write) {
io_driver.Write(data, len);
} else {
SOAR_PRINT("[MX66 ERROR] SPI_Write called before MX66_Init!\n");
}
}


void SPI_Read(uint8_t *data, uint16_t len) {
if(io_driver.Read) {
io_driver.Read(data, len);
} else {
SOAR_PRINT("[MX66 ERROR] SPI_Read called before MX66_Init!\n");
}
}

/************************************
Expand Down Expand Up @@ -173,7 +209,7 @@ void write_enable(void)
csLOW();
SPI_Write(&tData, 1);
csHIGH();
HAL_Delay(5);
MX66_Delay(5);
}

void write_disable(void)
Expand All @@ -182,7 +218,7 @@ void write_disable(void)
csLOW();
SPI_Write(&tData, 1);
csHIGH();
HAL_Delay(5);
MX66_Delay(5);
}

void MX66_Erase_Chip(void)
Expand Down
93 changes: 93 additions & 0 deletions MX66L1G45GMI/MX66L1G45GMI_Interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
********************************************************************************
* @file MX66L1G45GMI_Interface.cpp
* @author Javier
* @date Jan 28, 2026
* @brief Interface Implementation for MX66L1G45GMI Flash Memory Driver.
********************************************************************************
*/

/************************************
* INCLUDES
************************************/
#include "MX66L1G45GMI.hpp"
#include "DMATransfer.hpp"
#include "stm32g4xx_hal.h"

#include "main.h"

/************************************
* PRIVATE MACROS AND DEFINES
************************************/
extern SPI_HandleTypeDef hspi1;


/************************************
* VARIABLES
************************************/

/************************************
* FUNCTION DECLARATIONS
************************************/

/************************************
* FUNCTION DEFINITIONS
************************************/

// Chip Select Implementation
void IMPL_SetCS(bool high) {
HAL_GPIO_WritePin(SPI_FLASH_CS_GPIO_Port, SPI_FLASH_CS_Pin, high ? GPIO_PIN_SET : GPIO_PIN_RESET);
}

// Delay Implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have a note that this uses HAL delay, what is the point of this if we are renaming it only? And why is there two delays and explain why you used each one at that point and why

void IMPL_Delay(uint32_t ms) {
HAL_Delay(ms);
}

void IMPL_Write(const uint8_t* data, uint16_t len) {
// Use Polling for tiny commands (<=32 bytes) to avoid DMA overhead
if (len <= 32) {
HAL_SPI_Transmit(&hspi1, (uint8_t*)data, len, 100);
} else {
// Use DMA for larger data transfers (TX-only)
if (DMAControl::Transfer(&hspi1, 0, (uint8_t*)data, nullptr, len) != HAL_OK)
{
SOAR_PRINT("IMPL_Write: DMA Transfer Error\n");
return;
}

// BLOCKING WAIT: Protects the stack buffer 'tData' inside the driver
while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY)
;
}
}

// Read Implementation
void IMPL_Read(uint8_t* data, uint16_t len) {
// Use DMA Tool
DMAControl::Transfer(&hspi1, 0, nullptr, data, len);
// BLOCKING WAIT
int tick1 = HAL_GetTick();

while (HAL_GetTick() - tick1 < 500) { // 500ms timeout
if(HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_READY) break;
MX66_Delay(1);
}
if (HAL_GetTick() - tick1 >= 500) {
SOAR_PRINT("IMPL_Read: DMA Transfer Timeout\n");
return;
}
}
// Setup Function
void Setup_Flash_Interface() {
static MX66_Config cfg;

cfg.Write = IMPL_Write;
cfg.Read = IMPL_Read;
cfg.SetCS = IMPL_SetCS;
cfg.Delay = IMPL_Delay;

// Inject dependencies into the abstract driver
MX66_Init(&cfg);
}
// End of MX66L1G45GMI_Interface.cpp
Loading