ChibiOS  21.6.0
hal_serial_nor.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_serial_nor.h
19  * @brief Serial NOR driver header.
20  *
21  * @addtogroup HAL_SERIAL_NOR
22  * @{
23  */
24 
25 #ifndef HAL_SERIAL_NOR_H
26 #define HAL_SERIAL_NOR_H
27 
28 #include "hal_flash.h"
29 
30 /*===========================================================================*/
31 /* Driver constants. */
32 /*===========================================================================*/
33 
34 /**
35  * @name Bus interface modes.
36  * @{
37  */
38 #define SNOR_BUS_DRIVER_SPI 0U
39 #define SNOR_BUS_DRIVER_WSPI 1U
40 /** @} */
41 
42 /*===========================================================================*/
43 /* Driver pre-compile time settings. */
44 /*===========================================================================*/
45 
46 /**
47  * @name Configuration options
48  * @{
49  */
50 /**
51  * @brief Physical transport interface.
52  */
53 #if !defined(SNOR_BUS_DRIVER) || defined(__DOXYGEN__)
54 #define SNOR_BUS_DRIVER SNOR_BUS_DRIVER_WSPI
55 #endif
56 
57 /**
58  * @brief Shared bus switch.
59  * @details If set to @p TRUE the device acquires bus ownership
60  * on each transaction.
61  * @note Requires @p SPI_USE_MUTUAL_EXCLUSION or
62  * @p WSPI_USE_MUTUAL_EXCLUSION depending on mode selected
63  * with @p SNOR_BUS_MODE.
64  */
65 #if !defined(SNOR_SHARED_BUS) || defined(__DOXYGEN__)
66 #define SNOR_SHARED_BUS TRUE
67 #endif
68 /** @} */
69 
70 /*===========================================================================*/
71 /* Derived constants and error checks. */
72 /*===========================================================================*/
73 
74 #if (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_SPI) || defined(__DOXYGEN__)
75 #define BUSConfig SPIConfig
76 #define BUSDriver SPIDriver
77 #elif SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
78 #define BUSConfig WSPIConfig
79 #define BUSDriver WSPIDriver
80 #else
81 #error "invalid SNOR_BUS_DRIVER setting"
82 #endif
83 
84 /*===========================================================================*/
85 /* Driver data structures and types. */
86 /*===========================================================================*/
87 
88 /**
89  * @brief Type of a SNOR configuration structure.
90  */
91 typedef struct {
92  BUSDriver *busp;
93  const BUSConfig *buscfg;
94 } SNORConfig;
95 
96 /**
97  * @brief @p SNORDriver specific methods.
98  */
99 #define _snor_flash_methods_alone \
100  /* Read SFDP.*/ \
101  flash_error_t (*read_sfdp)(void *instance, \
102  flash_offset_t offset, \
103  size_t n, \
104  uint8_t *rp);
105 
106 /**
107  * @brief @p SNORDriver specific methods with inherited ones.
108  */
109 #define _snor_flash_methods \
110  _base_flash_methods \
111  _snor_flash_methods_alone
112 
113 /**
114  * @extends BaseFlashVMT
115  *
116  * @brief @p SNOR virtual methods table.
117  */
120 };
121 
122 /**
123  * @extends BaseFlash
124  *
125  * @brief Type of SNOR flash class.
126  */
127 typedef struct {
128  /**
129  * @brief SNORDriver Virtual Methods Table.
130  */
131  const struct SNORDriverVMT *vmt;
133  /**
134  * @brief Current configuration data.
135  */
137  /**
138  * @brief Device ID and unique ID.
139  */
140  uint8_t device_id[20];
141 } SNORDriver;
142 
143 /*===========================================================================*/
144 /* Driver macros. */
145 /*===========================================================================*/
146 
147 #if SNOR_SHARED_BUS == FALSE
148 #define bus_acquire(busp, config)
149 #define bus_release(busp)
150 #endif
151 
152 /*===========================================================================*/
153 /* External declarations. */
154 /*===========================================================================*/
155 
156 #ifdef __cplusplus
157 extern "C" {
158 #endif
159  void bus_acquire(BUSDriver *busp, const BUSConfig *config);
160  void bus_release(BUSDriver *busp);
161  void bus_cmd(BUSDriver *busp, uint32_t cmd);
162  void bus_cmd_send(BUSDriver *busp, uint32_t cmd, size_t n, const uint8_t *p);
163  void bus_cmd_receive(BUSDriver *busp,
164  uint32_t cmd,
165  size_t n,
166  uint8_t *p);
167  void bus_cmd_addr(BUSDriver *busp, uint32_t cmd, flash_offset_t offset);
168  void bus_cmd_addr_send(BUSDriver *busp,
169  uint32_t cmd,
170  flash_offset_t offset,
171  size_t n,
172  const uint8_t *p);
173  void bus_cmd_addr_receive(BUSDriver *busp,
174  uint32_t cmd,
175  flash_offset_t offset,
176  size_t n,
177  uint8_t *p);
178 #if (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI) || defined(__DOXYGEN__)
179  void bus_cmd_dummy_receive(BUSDriver *busp,
180  uint32_t cmd,
181  uint32_t dummy,
182  size_t n,
183  uint8_t *p);
184  void bus_cmd_addr_dummy_receive(BUSDriver *busp,
185  uint32_t cmd,
186  flash_offset_t offset,
187  uint32_t dummy,
188  size_t n,
189  uint8_t *p);
190 #endif
191  void snorObjectInit(SNORDriver *devp);
192  void snorStart(SNORDriver *devp, const SNORConfig *config);
193  void snorStop(SNORDriver *devp);
194 #if (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI) || defined(__DOXYGEN__)
195 #if (WSPI_SUPPORTS_MEMMAP == TRUE) || defined(__DOXYGEN__)
196  void snorMemoryMap(SNORDriver *devp, uint8_t ** addrp);
197  void snorMemoryUnmap(SNORDriver *devp);
198 #endif /* QSPI_SUPPORTS_MEMMAP == TRUE */
199 #endif /* SNOR_BUS_MODE != SNOR_BUS_MODE_SPI */
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 /* Device-specific implementations.*/
205 #include "hal_flash_device.h"
206 
207 #endif /* HAL_SERIAL_NOR_H */
208 
209 /** @} */
210 
bus_cmd_dummy_receive
void bus_cmd_dummy_receive(BUSDriver *busp, uint32_t cmd, uint32_t dummy, size_t n, uint8_t *p)
Sends a command followed by dummy cycles and a data receive phase.
Definition: hal_serial_nor.c:591
bus_release
void bus_release(BUSDriver *busp)
Bus release.
Definition: hal_serial_nor.c:335
SNORDriver
Type of SNOR flash class.
Definition: hal_serial_nor.h:127
hal_flash.h
Generic flash driver class header.
_snor_flash_methods
#define _snor_flash_methods
SNORDriver specific methods with inherited ones.
Definition: hal_serial_nor.h:109
snorMemoryMap
void snorMemoryMap(SNORDriver *devp, uint8_t **addrp)
Enters the memory Mapping mode.
Definition: hal_serial_nor.c:728
SNORConfig
Type of a SNOR configuration structure.
Definition: hal_serial_nor.h:91
bus_cmd_addr_receive
void bus_cmd_addr_receive(BUSDriver *busp, uint32_t cmd, flash_offset_t offset, size_t n, uint8_t *p)
Sends a command followed by a flash address and a data receive phase.
Definition: hal_serial_nor.c:550
bus_acquire
void bus_acquire(BUSDriver *busp, const BUSConfig *config)
Bus acquisition and lock.
Definition: hal_serial_nor.c:318
SNORDriverVMT
SNOR virtual methods table.
Definition: hal_serial_nor.h:118
SNORDriver::vmt
const struct SNORDriverVMT * vmt
SNORDriver Virtual Methods Table.
Definition: hal_serial_nor.h:131
bus_cmd_receive
void bus_cmd_receive(BUSDriver *busp, uint32_t cmd, size_t n, uint8_t *p)
Sends a command followed by a data receive phase.
Definition: hal_serial_nor.c:442
snorObjectInit
void snorObjectInit(SNORDriver *devp)
Initializes an instance.
Definition: hal_serial_nor.c:643
bus_cmd_addr_dummy_receive
void bus_cmd_addr_dummy_receive(BUSDriver *busp, uint32_t cmd, flash_offset_t offset, uint32_t dummy, size_t n, uint8_t *p)
Sends a command followed by a flash address, dummy cycles and a data receive phase.
Definition: hal_serial_nor.c:619
_base_flash_data
#define _base_flash_data
BaseFlash specific data.
Definition: hal_flash.h:211
bus_cmd_send
void bus_cmd_send(BUSDriver *busp, uint32_t cmd, size_t n, const uint8_t *p)
Sends a command followed by a data transmit phase.
Definition: hal_serial_nor.c:411
snorStart
void snorStart(SNORDriver *devp, const SNORConfig *config)
Configures and activates SNOR driver.
Definition: hal_serial_nor.c:660
bus_cmd_addr
void bus_cmd_addr(BUSDriver *busp, uint32_t cmd, flash_offset_t offset)
Sends a command followed by a flash address.
Definition: hal_serial_nor.c:475
flash_offset_t
uint32_t flash_offset_t
Type of a flash offset.
Definition: hal_flash.h:113
SNORDriver::config
const _base_flash_data SNORConfig * config
Current configuration data.
Definition: hal_serial_nor.h:136
snorStop
void snorStop(SNORDriver *devp)
Deactivates the SNOR driver.
Definition: hal_serial_nor.c:690
snorMemoryUnmap
void snorMemoryUnmap(SNORDriver *devp)
Leaves the memory Mapping mode.
Definition: hal_serial_nor.c:752
bus_cmd_addr_send
void bus_cmd_addr_send(BUSDriver *busp, uint32_t cmd, flash_offset_t offset, size_t n, const uint8_t *p)
Sends a command followed by a flash address and a data transmit phase.
Definition: hal_serial_nor.c:510
bus_cmd
void bus_cmd(BUSDriver *busp, uint32_t cmd)
Sends a naked command.
Definition: hal_serial_nor.c:381