aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/lib/peripherals/flash/hal_jesd216_flash.h
blob: 69d8a8533f2cd9b3b13031f2bf09967314c62c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    hal_jesd216_flash.h
 * @brief   JESD216 compliant flash driver class header.
 *
 * @addtogroup HAL_JESD216_FLASH
 * @{
 */

#ifndef HAL_JESD216_FLASH_H
#define HAL_JESD216_FLASH_H

#include "hal_flash.h"

/*===========================================================================*/
/* Driver constants.                                                         */
/*===========================================================================*/

/**
 * @name    Common command codes
 * @{
 */
#define JESD216_CMD_READ_ID                 0x9F
#define JESD216_CMD_READ                    0x03
#define JESD216_CMD_WRITE_ENABLE            0x06
#define JESD216_CMD_WRITE_DISABLE           0x04
#define JESD216_CMD_READ_STATUS_REGISTER    0x05
#define JESD216_CMD_WRITE_STATUS_REGISTER   0x01
#define JESD216_CMD_PAGE_PROGRAM            0x02
#define JESD216_CMD_ERASE_4K                0x20
#define JESD216_CMD_ERASE_BULK              0xC7
#define JESD216_CMD_PROGRAM_ERASE_RESUME    0x7A
#define JESD216_CMD_PROGRAM_ERASE_SUSPEND   0x75
#define JESD216_CMD_READ_OTP_ARRAY          0x4B
#define JESD216_CMD_PROGRAM_OTP_ARRAY       0x42
/** @} */

/**
 * @name    Bus interface.
 * @{
 */
#define JESD216_BUS_MODE_SPI                0
#define JESD216_BUS_MODE_QSPI1L             1
#define JESD216_BUS_MODE_QSPI2L             2
#define JESD216_BUS_MODE_QSPI4L             4
/** @} */

/*===========================================================================*/
/* Driver pre-compile time settings.                                         */
/*===========================================================================*/

/**
 * @name    Configuration options
 * @{
 */
/**
 * @brief   Physical transport interface.
 */
#if !defined(JESD216_USE_SPI) || defined(__DOXYGEN__)
#define JESD216_BUS_MODE                    JESD216_BUS_MODE_QSPI4L
#endif

/**
 * @brief   Shared bus switch.
 * @details If set to @p TRUE the device acquires bus ownership
 *          on each transaction.
 * @note    Requires @p SPI_USE_MUTUAL_EXCLUSION or
 *          @p SPI_USE_MUTUAL_EXCLUSION.
 */
#if !defined(JESD216_SHARED_BUS) || defined(__DOXYGEN__)
#define JESD216_SHARED_BUS                  TRUE
#endif
/** @} */

/*===========================================================================*/
/* Derived constants and error checks.                                       */
/*===========================================================================*/

#if (JESD216_BUS_MODE == JESD216_BUS_MODE_SPI) && (HAL_USE_SPI == FALSE)
#error "JESD216_BUS_MODE_SPI requires HAL_USE_SPI"
#endif

#if (JESD216_BUS_MODE != JESD216_BUS_MODE_SPI) && (HAL_USE_QSPI == FALSE)
#error "JESD216_BUS_MODE_QSPIxL requires HAL_USE_QSPI"
#endif

#if (JESD216_BUS_MODE == JESD216_BUS_MODE_SPI) &&                           \
    (JESD216_SHARED_SPI == TRUE) &&                                         \
    (SPI_USE_MUTUAL_EXCLUSION == FALSE)
#error "JESD216_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
#endif

#if (JESD216_BUS_MODE != JESD216_BUS_MODE_SPI) &&                           \
    (JESD216_BUS_MODE != JESD216_BUS_MODE_QSPI1L) &&                        \
    (JESD216_BUS_MODE != JESD216_BUS_MODE_QSPI2L) &&                        \
    (JESD216_BUS_MODE != JESD216_BUS_MODE_QSPI4L)
#error "invalid JESD216_BUS_MODE selected"
#endif

/*===========================================================================*/
/* Driver data structures and types.                                         */
/*===========================================================================*/

#if (JESD216_BUS_MODE != JESD216_BUS_MODE_SPI) || defined(__DOXYGEN__)
#define BUSConfig QSPIConfig
#define BUSDriver QSPIDriver
#else
#define BUSConfig SPIConfig
#define BUSDriver SPIDriver
#endif

#define _jesd216_config                                                     \
  BUSDriver                 *busp;                                          \
  const BUSConfig           *buscfg;

/**
 * @brief   @p JESD215Flash specific methods.
 * @note    No methods so far, just a common ancestor interface.
 */
#define _jesd216_flash_methods_alone                                        \
  /* Read SFDP.*/                                                           \
  flash_error_t (*read_sfdp)(void *instance, uint8_t *rp,                   \
                 flash_address_t addr,                                      \
                 size_t n);

/**
 * @brief   @p JESD215Flash specific methods with inherited ones.
 */
#define _jesd216_flash_methods                                              \
  _base_flash_methods                                                       \
  _jesd216_flash_methods_alone

/**
 * @brief   @p JESD215Flash virtual methods table.
 */
struct JESD215FlashVMT {
  _jesd216_flash_methods
};

/**
 * @brief   @p JESD215Flash specific data.
 */
#define _jesd216_flash_data                                                 \
  _base_flash_data

/**
 * @brief   Base flash class.
 */
typedef struct {
  /** @brief Virtual Methods Table.*/
  const struct JESD215FlashVMT *vmt;
  _jesd216_flash_data
} JESD215Flash;

/*===========================================================================*/
/* Driver macros.                                                            */
/*===========================================================================*/

/**
 * @name    Macro Functions (BaseFlash)
 * @{
 */
/** @} */

/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/

#ifdef __cplusplus
extern "C" {
#endif
  void jesd216_cmd(BUSDriver *busp, uint8_t cmd);
  void jesd216_cmd_receive(BUSDriver *busp, uint8_t cmd, size_t n, uint8_t *p);
  void jesd216_cmd_send(BUSDriver *busp, uint8_t cmd, size_t n, const uint8_t *p);
  void jesd216_cmd_addr(BUSDriver *busp, uint8_t cmd, flash_address_t addr);
  void jesd216_cmd_addr_send(BUSDriver *busp, uint8_t cmd, flash_address_t addr,
                             size_t n, const uint8_t *p);
  void jesd216_cmd_addr_receive(BUSDriver *busp, uint8_t cmd, flash_address_t addr,
                                size_t n, uint8_t *p);
#if JESD216_BUS_MODE != JESD216_BUS_MODE_SPI
  void jesd216_cmd_addr_dummy_receive(BUSDriver *busp, uint8_t cmd, flash_address_t addr,
                                      uint8_t dummy, size_t n, uint8_t *p);
#endif /* JESD216_BUS_MODE != JESD216_BUS_MODE_SPI */
#if JESD216_SHARED_BUS == TRUE
  void jesd216_bus_acquire(BUSDriver *busp);
  void jesd216_bus_release(BUSDriver *busp);
#endif
#ifdef __cplusplus
}
#endif

#endif /* HAL_JESD216_FLASH_H */

/** @} */