rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_flash_device.h
Go to the documentation of this file.
1/*
2 * hal_flash_device.h
3 *
4 * QSPI NOR flash driver with JEDEC SFDP for ChibiOS
5 * Tested and developed with Microchip SST26F064A
6 *
7 * @date Aug 14, 2021
8 * @author Andrey Gusakov, (c) 2021
9 */
10
11/**
12 * @file hal_flash_device.h
13 * @brief Jedec JESD216 SFDP
14 *
15 * @addtogroup JEDEC_SFDP
16 * @{
17 */
18
19#ifndef HAL_FLASH_DEVICE_H
20#define HAL_FLASH_DEVICE_H
21
22/*===========================================================================*/
23/* Driver constants. */
24/*===========================================================================*/
25
26/**
27 * @name Device capabilities
28 * @{
29 */
30#define SNOR_DEVICE_SUPPORTS_XIP TRUE
31/** @} */
32
33/**
34 * @name Device identification
35 * @{
36 */
37
38/** @} */
39
40/**
41 * @name Command codes
42 * @{
43 */
44
45#define JEDEC_CMD_READ_DISCOVERY_PARAMETER 0x5A
46
47/* default/fallback commands */
48#define JEDEC_CMD_WRITE_STATUS_REGISTER 0x01
49#define JEDEC_CMD_PAGE_PROGRAM 0x02
50#define JEDEC_CMD_READ 0x03
51#define JEDEC_CMD_WRITE_DISABLE 0x04
52#define JEDEC_CMD_READ_STATUS_REGISTER 0x05
53#define JEDEC_CMD_WRITE_ENABLE 0x06
54#define JEDEC_CMD_FAST_READ 0x0B
55#define JEDEC_CMD_SUBSECTOR_ERASE 0x20
56#define JEDEC_CMD_READ_CONFIGURATION_REGISTER 0x35
57#define JEDEC_CMD_READ_DUAL 0x3B
58#define JEDEC_CMD_BULK_ERASE 0xC7
59#define JEDEC_CMD_RESET_ENABLE 0x66
60#define JEDEC_CMD_READ_QUAD 0x6B
61#define JEDEC_CMD_GLOBAL_BLOCK_PROTECTION_UNLOCK 0x98
62#define JEDEC_CMD_RESET_MEMORY 0x99
63/** @} */
64
65/**
66 * @name Flags status register bits
67 * @{
68 */
69#define JEDEC_FLAGS_STS_BUSY 0x80U
70/** @} */
71
72/**
73 * @name Bus interface modes.
74 * @{
75 */
76#define JEDEC_BUS_MODE_WSPI1L 1U
77#define JEDEC_BUS_MODE_WSPI2L 2U
78#define JEDEC_BUS_MODE_WSPI4L 4U
79/** @} */
80
81/*===========================================================================*/
82/* Driver pre-compile time settings. */
83/*===========================================================================*/
84
85/**
86 * @brief Switch WSPI bus width on initialization.
87 * @details A bus width initialization is performed by writing the
88 * Enhanced Volatile Configuration Register. If the flash
89 * device is configured using the Non Volatile Configuration
90 * Register then this option is not required.
91 * @note This option is only valid in WSPI bus mode.
92 */
93#if !defined(JEDEC_SWITCH_WIDTH) || defined(__DOXYGEN__)
94#define JEDEC_SWITCH_WIDTH TRUE
95#endif
96
97/**
98 * @brief Device bus mode to be used.
99 * #note if @p JEDEC_SWITCH_WIDTH is @p FALSE then this is the bus mode
100 * that the device is expected to be using.
101 * #note if @p JEDEC_SWITCH_WIDTH is @p TRUE then this is the bus mode
102 * that the device will be switched in.
103 * @note This option is only valid in WSPI bus mode.
104 */
105#if !defined(JEDEC_BUS_MODE) || defined(__DOXYGEN__)
106#define JEDEC_BUS_MODE JEDEC_BUS_MODE_WSPI4L
107#endif
108
109/**
110 * @brief Delays insertions.
111 * @details If enabled this options inserts delays into the flash waiting
112 * routines releasing some extra CPU time for threads with lower
113 * priority, this may slow down the driver a bit however.
114 */
115#if !defined(JEDEC_NICE_WAITING) || defined(__DOXYGEN__)
116#define JEDEC_NICE_WAITING TRUE
117#endif
118
119/**
120 * @brief Uses 4kB sub-sectors rather than 64kB sectors.
121 */
122#if !defined(JEDEC_USE_SUB_SECTORS) || defined(__DOXYGEN__)
123#define JEDEC_USE_SUB_SECTORS FALSE
124#endif
125
126/**
127 * @brief Number of dummy cycles for fast read (1..15).
128 * @details This is the number of dummy cycles to be used for fast read
129 * operations.
130 */
131#if !defined(JEDEC_READ_DUMMY_CYCLES) || defined(__DOXYGEN__)
132#define JEDEC_READ_DUMMY_CYCLES 8
133#endif
134
135/*===========================================================================*/
136/* Derived constants and error checks. */
137/*===========================================================================*/
138
139#if (JEDEC_READ_DUMMY_CYCLES < 1) || (JEDEC_READ_DUMMY_CYCLES > 15)
140#error "invalid JEDEC_READ_DUMMY_CYCLES value (1..15)"
141#endif
142
143/**
144 * @brief WSPI settings for command only.
145 */
146#define SNOR_WSPI_CFG_CMD (WSPI_CFG_CMD_MODE_ONE_LINE | \
147 WSPI_CFG_ADDR_MODE_NONE | \
148 WSPI_CFG_ALT_MODE_NONE | \
149 WSPI_CFG_DATA_MODE_NONE | \
150 WSPI_CFG_CMD_SIZE_8 | \
151 WSPI_CFG_ADDR_SIZE_24)
152/**
153 * @brief WSPI settings for command and address.
154 */
155#define SNOR_WSPI_CFG_CMD_ADDR (WSPI_CFG_CMD_MODE_ONE_LINE | \
156 WSPI_CFG_ADDR_MODE_ONE_LINE | \
157 WSPI_CFG_ALT_MODE_NONE | \
158 WSPI_CFG_DATA_MODE_NONE | \
159 WSPI_CFG_CMD_SIZE_8 | \
160 WSPI_CFG_ADDR_SIZE_24)
161/**
162 * @brief WSPI settings for command and data.
163 */
164#define SNOR_WSPI_CFG_CMD_DATA (WSPI_CFG_CMD_MODE_ONE_LINE | \
165 WSPI_CFG_ADDR_MODE_NONE | \
166 WSPI_CFG_ALT_MODE_NONE | \
167 WSPI_CFG_DATA_MODE_ONE_LINE | \
168 WSPI_CFG_CMD_SIZE_8 | \
169 WSPI_CFG_ADDR_SIZE_24)
170/**
171 * @brief WSPI settings for command, address and data.
172 */
173#define SNOR_WSPI_CFG_CMD_ADDR_DATA (WSPI_CFG_CMD_MODE_ONE_LINE | \
174 WSPI_CFG_ADDR_MODE_ONE_LINE | \
175 WSPI_CFG_ALT_MODE_NONE | \
176 WSPI_CFG_DATA_MODE_ONE_LINE | \
177 WSPI_CFG_CMD_SIZE_8 | \
178 WSPI_CFG_ADDR_SIZE_24)
179
180/*===========================================================================*/
181/* Driver data structures and types. */
182/*===========================================================================*/
183
184/*===========================================================================*/
185/* Driver macros. */
186/*===========================================================================*/
187
188/*===========================================================================*/
189/* External declarations. */
190/*===========================================================================*/
191
192#if !defined(__DOXYGEN__)
193extern flash_descriptor_t snor_descriptor;
194#endif
195
196#if (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI) && (WSPI_SUPPORTS_MEMMAP == TRUE)
197extern const wspi_command_t snor_memmap_read;
198#endif
199
200#ifdef __cplusplus
201extern "C" {
202#endif
203 void snor_device_init(SNORDriver *devp);
204 flash_error_t snor_device_read(SNORDriver *devp, flash_offset_t offset,
205 size_t n, uint8_t *rp);
206 flash_error_t snor_device_program(SNORDriver *devp, flash_offset_t offset,
207 size_t n, const uint8_t *pp);
208 flash_error_t snor_device_start_erase_all(SNORDriver *devp);
209 flash_error_t snor_device_start_erase_sector(SNORDriver *devp,
210 flash_sector_t sector);
211 flash_error_t snor_device_verify_erase(SNORDriver *devp,
212 flash_sector_t sector);
213 flash_error_t snor_device_query_erase(SNORDriver *devp, uint32_t *msec);
214 flash_error_t snor_device_read_sfdp(SNORDriver *devp, flash_offset_t offset,
215 size_t n, uint8_t *rp);
216#if (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI) && \
217 (SNOR_DEVICE_SUPPORTS_XIP == TRUE)
218 void snor_activate_xip(SNORDriver *devp);
219 void snor_reset_xip(SNORDriver *devp);
220#endif
221#ifdef __cplusplus
222}
223#endif
224
225#endif /* HAL_FLASH_DEVICE_H */
226
227/** @} */
228
void snor_activate_xip(SNORDriver *devp)
flash_error_t snor_device_verify_erase(SNORDriver *devp, flash_sector_t sector)
flash_error_t snor_device_query_erase(SNORDriver *devp, uint32_t *msec)
flash_error_t snor_device_read_sfdp(SNORDriver *devp, flash_offset_t offset, size_t n, uint8_t *rp)
void snor_reset_xip(SNORDriver *devp)
flash_error_t snor_device_start_erase_sector(SNORDriver *devp, flash_sector_t sector)
const wspi_command_t snor_memmap_read
Fast read command for memory mapped mode.
flash_error_t snor_device_read(SNORDriver *devp, flash_offset_t offset, size_t n, uint8_t *rp)
flash_error_t snor_device_program(SNORDriver *devp, flash_offset_t offset, size_t n, const uint8_t *pp)
flash_descriptor_t snor_descriptor
Flash descriptor.
void snor_device_init(SNORDriver *devp)
flash_error_t snor_device_start_erase_all(SNORDriver *devp)
uint16_t offset
Definition tunerstudio.h:0