Skip to content

Commit 660d0b1

Browse files
committed
Refactor SPI flash API to C++ namespaces
Replace C-style spi_flash_* function names with a C++ namespace API (spi::flash::Probe, spi::flash::cmd::Read, etc.). Rename constants to kCamelCase, move debug macros to the header, switch from time() to timing::Millis() for timeouts (now in ms), use common::ArraySize instead of ARRAY_SIZE macro, and add const correctness to idcode parameters.
1 parent 7bd403b commit 660d0b1

8 files changed

Lines changed: 410 additions & 402 deletions

File tree

lib-configstore/device/spi/storedevice.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@
3737
StoreDevice::StoreDevice() {
3838
CONFIGSTORE_DEBUG_ENTRY();
3939

40-
if (!spi_flash_probe()) {
40+
if (!spi::flash::Probe()) {
4141
puts("StoreDevice: No SPI flash chip.");
4242
} else {
43-
printf("StoreDevice: %s sector size %u total %u bytes [%u kB]\n", spi_flash_get_name(), static_cast<unsigned int>(spi_flash_get_sector_size()), static_cast<unsigned int>(spi_flash_get_size()),
44-
static_cast<unsigned int>(spi_flash_get_size() / 1024U));
43+
printf("StoreDevice: SPI flash %s sector size %u total %u bytes [%u kB]\n",
44+
spi::flash::Name(),
45+
static_cast<unsigned int>(spi::flash::SectorSize()),
46+
static_cast<unsigned int>(spi::flash::Size()),
47+
static_cast<unsigned int>(spi::flash::Size() / 1024U));
4548
detected_ = true;
4649
}
4750

@@ -54,17 +57,17 @@ StoreDevice::~StoreDevice() {
5457
}
5558

5659
uint32_t StoreDevice::GetSize() const {
57-
return spi_flash_get_size();
60+
return spi::flash::Size();
5861
}
5962

6063
uint32_t StoreDevice::GetSectorSize() const {
61-
return spi_flash_get_sector_size();
64+
return spi::flash::SectorSize();
6265
}
6366

6467
bool StoreDevice::Read(uint32_t offset, uint32_t length, uint8_t* buffer, storedevice::Result& result) {
6568
CONFIGSTORE_DEBUG_ENTRY();
6669

67-
result = spi_flash_cmd_read_fast(offset, length, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
70+
result = spi::flash::cmd::Read(offset, length, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
6871

6972
CONFIGSTORE_DEBUG_PRINTF("result=%d", static_cast<int>(result));
7073
CONFIGSTORE_DEBUG_EXIT();
@@ -74,7 +77,7 @@ bool StoreDevice::Read(uint32_t offset, uint32_t length, uint8_t* buffer, stored
7477
bool StoreDevice::Erase(uint32_t offset, uint32_t length, storedevice::Result& result) {
7578
CONFIGSTORE_DEBUG_ENTRY();
7679

77-
result = spi_flash_cmd_erase(offset, length) ? storedevice::Result::kOk : storedevice::Result::kError;
80+
result = spi::flash::cmd::Erase(offset, length) ? storedevice::Result::kOk : storedevice::Result::kError;
7881

7982
CONFIGSTORE_DEBUG_PRINTF("result=%d", static_cast<int>(result));
8083
CONFIGSTORE_DEBUG_EXIT();
@@ -84,7 +87,7 @@ bool StoreDevice::Erase(uint32_t offset, uint32_t length, storedevice::Result& r
8487
bool StoreDevice::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, storedevice::Result& result) {
8588
CONFIGSTORE_DEBUG_ENTRY();
8689

87-
result = spi_flash_cmd_write_multi(offset, length, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
90+
result = spi::flash::cmd::Write(offset, length, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
8891

8992
CONFIGSTORE_DEBUG_PRINTF("result=%d", static_cast<int>(result));
9093
CONFIGSTORE_DEBUG_EXIT();

lib-flash/include/spi/spi_flash.h

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file spi_flash.h
33
*
44
*/
5-
/* Copyright (C) 2018-2024 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2018-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -28,25 +28,47 @@
2828

2929
#include <cstdint>
3030

31-
namespace spi::flash
32-
{
33-
inline constexpr uint32_t PAGE_SIZE = 256;
34-
inline constexpr uint32_t SECTOR_SIZE = 4096;
35-
} // namespace spi::flash
31+
#ifdef DEBUG_SPI_FLASH
32+
#include "firmware/debug/debug_debug.h"
33+
34+
#define SPI_FLASH_DEBUG_ENTRY() DEBUG_ENTRY()
35+
#define SPI_FLASH_DEBUG_EXIT() DEBUG_EXIT()
36+
#define SPI_FLASH_DEBUG_PRINTF(...) DEBUG_PRINTF(__VA_ARGS__)
37+
#define SPI_FLASH_DEBUG_PUTS(...) DEBUG_PUTS(__VA_ARGS__)
38+
#else
39+
#define SPI_FLASH_DEBUG_ENTRY() \
40+
do { \
41+
} while (false)
42+
#define SPI_FLASH_DEBUG_EXIT() \
43+
do { \
44+
} while (false)
45+
#define SPI_FLASH_DEBUG_PRINTF(...) \
46+
do { \
47+
} while (false)
48+
#define SPI_FLASH_DEBUG_PUTS(...) \
49+
do { \
50+
} while (false)
51+
#endif
3652

37-
bool spi_flash_probe();
53+
namespace spi::flash {
54+
inline constexpr uint32_t kPageSize = 256;
55+
inline constexpr uint32_t kSectorSize = 4096;
3856

39-
const char* spi_flash_get_name();
40-
uint32_t spi_flash_get_size();
57+
bool Probe();
4158

42-
inline uint32_t spi_flash_get_sector_size()
43-
{
44-
return spi::flash::SECTOR_SIZE;
59+
const char* Name();
60+
uint32_t Size();
61+
62+
inline uint32_t SectorSize() {
63+
return spi::flash::kSectorSize;
4564
}
4665

47-
bool spi_flash_cmd_read_fast(uint32_t offset, uint32_t length, uint8_t* data);
48-
bool spi_flash_cmd_write_multi(uint32_t offset, uint32_t length, const uint8_t* buffer);
49-
bool spi_flash_cmd_erase(uint32_t offset, uint32_t length);
50-
bool spi_flash_cmd_write_status(uint8_t sr);
66+
namespace cmd {
67+
bool Read(uint32_t offset, uint32_t length, uint8_t* data);
68+
bool Write(uint32_t offset, uint32_t length, const uint8_t* buffer);
69+
bool Erase(uint32_t offset, uint32_t length);
70+
bool WriteStatus(uint8_t status);
71+
} // namespace cmd
72+
} // namespace spi::flash
5173

52-
#endif // SPI_SPI_FLASH_H_
74+
#endif // SPI_SPI_FLASH_H_

lib-flash/src/spi/gd32/spi_flash.cpp

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file spi_flash.cpp
33
*
44
*/
5-
/* Copyright (C) 2022-2024 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2022-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -28,49 +28,50 @@
2828
#include "./../../spi/spi_flash_internal.h"
2929
#include "gd32_spi.h"
3030
#include "gd32_gpio.h"
31-
#include "gd32.h"
32-
#include "firmware/debug/debug_debug.h"
31+
#include "gd32.h" // IWYU pragma: keep
32+
33+
namespace {
34+
void SpiTransfern(char* buffer, uint32_t length) {
35+
Gd32SpiTransfernb(buffer, buffer, length);
36+
}
37+
} // namespace
3338

3439
void SpiInit() {
35-
Gd32SpiBegin();
36-
Gd32SpiChipSelect(GD32_SPI_CS_NONE);
37-
Gd32SpiSetSpeedHz(SPI_XFER_SPEED_HZ);
38-
Gd32SpiSetDataMode(GD32_SPI_MODE0);
40+
Gd32SpiBegin();
41+
Gd32SpiChipSelect(GD32_SPI_CS_NONE);
42+
Gd32SpiSetSpeedHz(SPI_XFER_SPEED_HZ);
43+
Gd32SpiSetDataMode(GD32_SPI_MODE0);
3944

40-
Gd32GpioFsel(SPI_FLASH_CS_GPIOx, SPI_FLASH_CS_GPIO_PINx, GPIO_FSEL_OUTPUT);
41-
GPIO_BOP(SPI_FLASH_CS_GPIOx) = SPI_FLASH_CS_GPIO_PINx;
45+
Gd32GpioFsel(SPI_FLASH_CS_GPIOx, SPI_FLASH_CS_GPIO_PINx, GPIO_FSEL_OUTPUT);
46+
GPIO_BOP(SPI_FLASH_CS_GPIOx) = SPI_FLASH_CS_GPIO_PINx;
4247

43-
#if defined (SPI_FLASH_WP_GPIO_PINx)
44-
Gd32GpioFsel(SPI_GPIOx, SPI_FLASH_WP_GPIO_PINx, GPIO_FSEL_OUTPUT);
45-
GPIO_BOP(SPI_GPIOx) = SPI_FLASH_WP_GPIO_PINx;
48+
#if defined(SPI_FLASH_WP_GPIO_PINx)
49+
Gd32GpioFsel(SPI_GPIOx, SPI_FLASH_WP_GPIO_PINx, GPIO_FSEL_OUTPUT);
50+
GPIO_BOP(SPI_GPIOx) = SPI_FLASH_WP_GPIO_PINx;
4651
#endif
4752

48-
#if defined (SPI_FLASH_HOLD_GPIO_PINx)
49-
Gd32GpioFsel(SPI_GPIOx, SPI_FLASH_HOLD_GPIO_PINx, GPIO_FSEL_OUTPUT);
50-
GPIO_BOP(SPI_GPIOx) = SPI_FLASH_HOLD_GPIO_PINx;
53+
#if defined(SPI_FLASH_HOLD_GPIO_PINx)
54+
Gd32GpioFsel(SPI_GPIOx, SPI_FLASH_HOLD_GPIO_PINx, GPIO_FSEL_OUTPUT);
55+
GPIO_BOP(SPI_GPIOx) = SPI_FLASH_HOLD_GPIO_PINx;
5156
#endif
5257
}
5358

54-
inline static void SpiTransfern(char *buffer, uint32_t length) {
55-
Gd32SpiTransfernb(buffer, buffer, length);
56-
}
57-
58-
void SpiXfer(uint32_t length, const uint8_t *out, uint8_t *in, uint32_t flags) {
59-
if (flags & SPI_XFER_BEGIN) {
60-
GPIO_BC(SPI_FLASH_CS_GPIOx) = SPI_FLASH_CS_GPIO_PINx;
61-
}
59+
void SpiXfer(uint32_t length, const uint8_t* data_out, uint8_t* data_in, uint32_t flags) {
60+
if (flags & SPI_XFER_BEGIN) {
61+
GPIO_BC(SPI_FLASH_CS_GPIOx) = SPI_FLASH_CS_GPIO_PINx;
62+
}
6263

63-
if (length != 0) {
64-
if (in == nullptr) {
65-
Gd32SpiWritenb(reinterpret_cast<const char *>(out), length);
66-
} else if (out == nullptr) {
67-
SpiTransfern(reinterpret_cast<char *>(in), length);
68-
} else {
69-
Gd32SpiTransfernb(reinterpret_cast<const char *>(out), reinterpret_cast<char *>(in), length);
70-
}
71-
}
64+
if (length != 0) {
65+
if (data_in == nullptr) {
66+
Gd32SpiWritenb(reinterpret_cast<const char*>(data_out), length);
67+
} else if (data_out == nullptr) {
68+
SpiTransfern(reinterpret_cast<char*>(data_in), length);
69+
} else {
70+
Gd32SpiTransfernb(reinterpret_cast<const char*>(data_out), reinterpret_cast<char*>(data_in), length);
71+
}
72+
}
7273

73-
if (flags & SPI_XFER_END) {
74-
GPIO_BOP(SPI_FLASH_CS_GPIOx) = SPI_FLASH_CS_GPIO_PINx;
75-
}
74+
if (flags & SPI_XFER_END) {
75+
GPIO_BOP(SPI_FLASH_CS_GPIOx) = SPI_FLASH_CS_GPIO_PINx;
76+
}
7677
}

lib-flash/src/spi/gigadevice.cpp

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/*
1313
* Original code : https://github.com/martinezjavier/u-boot/blob/master/drivers/mtd/spi/gigadevice.c
1414
*/
15-
/* Copyright (C) 2021-2024 by Arjan van Vught mailto:info@gd32-dmx.org
15+
/* Copyright (C) 2021-2026 by Arjan van Vught mailto:info@gd32-dmx.org
1616
*
1717
* Permission is hereby granted, free of charge, to any person obtaining a copy
1818
* of this software and associated documentation files (the "Software"), to deal
@@ -35,57 +35,56 @@
3535

3636
#include <cstdint>
3737

38+
#include "common/utils/utils_array.h"
3839
#include "spi/spi_flash.h"
3940
#include "spi_flash_internal.h"
40-
#include "firmware/debug/debug_debug.h"
4141

42-
struct GigadeviceSpiFlashParams
43-
{
42+
struct GigadeviceSpiFlashParams {
4443
const uint16_t kId;
4544
const uint16_t kNrBlocks;
4645
const char* const kName;
4746
};
4847

4948
static constexpr struct GigadeviceSpiFlashParams kGigadeviceSpiFlashTable[] = {
50-
{
51-
0x6016,
52-
64,
53-
"GD25LQ",
54-
},
55-
{
56-
0x4015,
57-
8,
58-
"GD25Q40",
59-
},
60-
{
61-
0x4017,
62-
128,
63-
"GD25Q64B",
64-
},
49+
{
50+
.kId = 0x6016,
51+
.kNrBlocks = 64,
52+
.kName = "GD25LQ",
53+
},
54+
{
55+
.kId = 0x4015,
56+
.kNrBlocks = 8,
57+
.kName = "GD25Q40",
58+
},
59+
{
60+
.kId = 0x4017,
61+
.kNrBlocks = 128,
62+
.kName = "GD25Q64B",
63+
},
6564
};
6665

67-
bool SpiFlashProbeGigadevice(struct SpiFlashInfo* flash, uint8_t* idcode)
68-
{
66+
bool SpiFlashProbeGigadevice(struct SpiFlashInfo* flash, const uint8_t* idcode) {
67+
SPI_FLASH_DEBUG_ENTRY();
68+
6969
const struct GigadeviceSpiFlashParams* params;
70-
unsigned int i;
70+
size_t index;
7171

72-
for (i = 0; i < ARRAY_SIZE(kGigadeviceSpiFlashTable); i++)
73-
{
74-
params = &kGigadeviceSpiFlashTable[i];
75-
if (params->kId == ((idcode[1] << 8) | idcode[2]))
76-
{
72+
for (index = 0; index < common::ArraySize(kGigadeviceSpiFlashTable); index++) {
73+
params = &kGigadeviceSpiFlashTable[index];
74+
if (params->kId == ((idcode[1] << 8) | idcode[2])) {
7775
break;
7876
}
7977
}
8078

81-
if (i == ARRAY_SIZE(kGigadeviceSpiFlashTable))
82-
{
83-
DEBUG_PRINTF("SF: Unsupported GigaDevice ID %02x%02x", idcode[1], idcode[2]);
79+
if (index == common::ArraySize(kGigadeviceSpiFlashTable)) {
80+
SPI_FLASH_DEBUG_PRINTF("SF: Unsupported GigaDevice ID %02x%02x", idcode[1], idcode[2]);
81+
SPI_FLASH_DEBUG_EXIT();
8482
return false;
8583
}
8684

8785
flash->name = params->kName;
88-
flash->size = 16U * spi::flash::SECTOR_SIZE * params->kNrBlocks;
86+
flash->size = 16U * spi::flash::kSectorSize * params->kNrBlocks;
8987

88+
SPI_FLASH_DEBUG_EXIT();
9089
return true;
9190
}

0 commit comments

Comments
 (0)