rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
board_storage.cpp
Go to the documentation of this file.
1/**
2 * @file boards/microrusefi/board_storage.cpp
3 *
4 * @brief Storage configuration file
5 *
6 * @date May 27, 2024
7 * @author Andrey Gusakov, 2023
8 */
9
10#include "pch.h"
11
12/* This board stores settings in external SPI flash */
13#if !defined(EFI_BOOTLOADER) && (EFI_STORAGE_MFS == TRUE)
14
15#include "hal_serial_nor.h"
16#include "hal_mfs.h"
17
18/* SPI storage */
19#define EFI_FLASH_SPI_CS_GPIO GPIOE
20#define EFI_FLASH_SPI_CS_PIN 15
21
22#define EFI_FLASH_SPI_AF 5U
23#define EFI_FLASH_SPI_SCK Gpio::B13
24#define EFI_FLASH_SPI_MISO Gpio::B14
25#define EFI_FLASH_SPI_MOSI Gpio::B15
26#define EFI_FLASH_SDPID SPID2
27
28#define EFI_FLASH_WP Gpio::B10
29#define EFI_FLASH_HOLD Gpio::B11
30
31/* 8 Mbytes */
32/* Not used, just FYI */
33#define EFI_FLASH_SIZE (8 * 1024 * 1024)
34
35/* Some fields in following struct are used for DMA transfers, so do not cache */
36/* TODO: can we drop NO_CACHE for snor1 since snor1buf? */
37static NO_CACHE SNORDriver snor1;
38static NO_CACHE snor_nocache_buffer_t snor1buf;
39
40/*
41 * Maximum speed SPI configuration (Clock = Fpclk / 2 = 21 MHz, CPHA=0, CPOL=0, MSb first).
42 * SPI2 is clocked from APB1, APB1 clock is 42MHz
43 * W25Qxx supports up to 133MHz in single SPI mode
44 */
45static const SPIConfig W25SpiCfg = {
46 .circular = false,
47#ifdef _CHIBIOS_RT_CONF_VER_6_1_
48 .end_cb = NULL,
49#else
50 .slave = false,
51 .data_cb = NULL,
52 .error_cb = NULL,
53#endif
54 .ssport = EFI_FLASH_SPI_CS_GPIO,
55 .sspad = EFI_FLASH_SPI_CS_PIN,
56 .cr1 =
57 SPI_CR1_8BIT_MODE |
58 ((0 << SPI_CR1_BR_Pos) & SPI_CR1_BR) |
59 0,
60 .cr2 =
61 SPI_CR2_8BIT_MODE |
62 0,
63};
64
65/*
66 * Flash driver configuration.
67 */
68static const SNORConfig W25FlashConfig = {
69 .busp = &EFI_FLASH_SDPID,
70 .buscfg = &W25SpiCfg
71};
72
73const MFSConfig mfsd_nor_config = {
74 .flashp = (BaseFlash *)&snor1,
75 .erased = 0xFFFFFFFFU,
76#if 1
77 /* it takes:
78 * 147 mS to write 25K of settings whithout garbage collector (when there is free space in current bank)
79 * 4750 mS to write settings with garbage collection (packing and moving to another bank, erasing old one)
80 * GC happens rougly every ((512 / 25) - 1) ~= 19 write */
81 .bank_size = 512 * 1024U,
82 .bank0_start = 0U,
83 .bank0_sectors = 128U, /* 128 * 4 K = 0.5 Mb */
84 .bank1_start = 128U,
85 .bank1_sectors = 128U
86#else
87 /* it takes:
88 * same 147 mS to write setting without GC
89 * 1500 mS to write setting with GC, but GC happens every time we write settings */
90 .bank_size = 64 * 1024U,
91 .bank0_start = 0U,
92 .bank0_sectors = 16U, /* 16 * 4 K = 64 Kb */
93 .bank1_start = 16U,
94 .bank1_sectors = 16U
95#endif
96};
97
99{
100#if SNOR_SHARED_BUS == FALSE
101 spiStart(&EFI_FLASH_SDPID, &W25SpiCfg);
102#endif
103
104 palSetPad(EFI_FLASH_SPI_CS_GPIO, EFI_FLASH_SPI_CS_PIN);
105 palSetPadMode(EFI_FLASH_SPI_CS_GPIO, EFI_FLASH_SPI_CS_PIN,
106 PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
107 palSetPadMode(getBrainPinPort(EFI_FLASH_SPI_SCK), getBrainPinIndex(EFI_FLASH_SPI_SCK),
108 PAL_MODE_ALTERNATE(EFI_FLASH_SPI_AF) | PAL_STM32_OSPEED_HIGHEST);
109 palSetPadMode(getBrainPinPort(EFI_FLASH_SPI_MISO), getBrainPinIndex(EFI_FLASH_SPI_MISO),
110 PAL_MODE_ALTERNATE(EFI_FLASH_SPI_AF) | PAL_STM32_OSPEED_HIGHEST);
111 palSetPadMode(getBrainPinPort(EFI_FLASH_SPI_MOSI), getBrainPinIndex(EFI_FLASH_SPI_MOSI),
112 PAL_MODE_ALTERNATE(EFI_FLASH_SPI_AF) | PAL_STM32_OSPEED_HIGHEST);
113 /* Deactivate WP */
114 palSetPad(getBrainPinPort(EFI_FLASH_WP), getBrainPinIndex(EFI_FLASH_WP));
115 palSetPadMode(getBrainPinPort(EFI_FLASH_WP), getBrainPinIndex(EFI_FLASH_WP),
116 PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
117 /* Deactivate HOLD */
118 palSetPad(getBrainPinPort(EFI_FLASH_HOLD), getBrainPinIndex(EFI_FLASH_HOLD));
119 palSetPadMode(getBrainPinPort(EFI_FLASH_HOLD), getBrainPinIndex(EFI_FLASH_HOLD),
120 PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
121
122 /*
123 * Initializing and starting flash driver.
124 */
125 snorObjectInit(&snor1, &snor1buf);
126 snorStart(&snor1, &W25FlashConfig);
127
128 return true;
129}
130
131const MFSConfig *boardGetMfsConfig()
132{
133 return &mfsd_nor_config;
134}
135
136#endif /* EFI_STORAGE_MFS == TRUE */
bool boardInitMfs()
const MFSConfig * boardGetMfsConfig()
static NO_CACHE SNORDriver snor1
const MFSConfig mfsd_nor_config
static NO_CACHE snor_nocache_buffer_t snor1buf
static const SPIConfig W25SpiCfg
static const SNORConfig W25FlashConfig
static union @47 NO_CACHE
int getBrainPinIndex(Gpio brainPin)
ioportid_t getBrainPinPort(brain_pin_e brainPin)