1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __HAL_FSMC_H__
9 #define __HAL_FSMC_H__
10 
11 #include "hal_common.h"
12 
13 /*
14 * There are still some configurations in SYSCFG_CFGR1:
15 * - switch between 8080 mode and NOR FLASH mode.
16 * - disable the reuse of data pins as address pins.
17 * - enable the fsmc sync.
18 */
19 
20 /*!
21  * @addtogroup FSMC
22  * @{
23  */
24 
25 /*!
26  * @brief FSMC driver version number.
27  */
28 #define FSMC_DRIVER_VERSION 0u /*!< fsmc_0. */
29 
30 /*!
31  * @brief FSMC memory type.
32  */
33 typedef enum
34 {
35     FSMC_MemType_SRAM     = 1u, /*!< Mem type: SRAM. */
36     FSMC_MemType_NorFlash = 2u, /*!< Mem type: NorFlash. */
37     FSMC_MemType_PSRAM    = 3u, /*!< Mem type: PSRAM. */
38 } FSMC_MemType_Type;
39 
40 /*!
41  * @brief FSMC memory size type.
42  */
43 typedef enum
44 {
45     FSMC_MemSize_NoDevice = 0u,  /*!< Mem Size type: NoDevice. */
46     FSMC_MemSize_64KB     = 1u,  /*!< Mem Size type: 64KB. */
47     FSMC_MemSize_128KB    = 2u,  /*!< Mem Size type: 128KB. */
48     FSMC_MemSize_256KB    = 3u,  /*!< Mem Size type: 256KB. */
49     FSMC_MemSize_512KB    = 4u,  /*!< Mem Size type: 512KB. */
50     FSMC_MemSize_1MB      = 5u,  /*!< Mem Size type: 1MB. */
51     FSMC_MemSize_2MB      = 6u,  /*!< Mem Size type: 2MB. */
52     FSMC_MemSize_4MB      = 7u,  /*!< Mem Size type: 4MB. */
53     FSMC_MemSize_8MB      = 8u,  /*!< Mem Size type: 8MB. */
54     FSMC_MemSize_16MB     = 9u,  /*!< Mem Size type: 16MB. */
55     FSMC_MemSize_32MB     = 10u, /*!< Mem Size type: 32MB. */
56     FSMC_MemSize_64MB     = 11u, /*!< Mem Size type: 64MB. */
57     FSMC_MemSize_128MB    = 12u, /*!< Mem Size type: 128MB. */
58     FSMC_MemSize_256MB    = 13u, /*!< Mem Size type: 256MB. */
59     FSMC_MemSize_512MB    = 14u, /*!< Mem Size type: 512MB. */
60     FSMC_MemSize_1GB      = 15u, /*!< Mem Size type: 1GB. */
61     FSMC_MemSize_2GB      = 16u, /*!< Mem Size type: 2GB. */
62     FSMC_MemSize_4GB      = 17u, /*!< Mem Size type: 4GB. */
63 } FSMC_MemSize_Type;
64 
65 /*!
66  * @brief FSMC ready signal type.
67  */
68 typedef enum
69 {
70     FSMC_ReadySignal_Internal = 0u, /*!< The ready signal is inside the FSMC, do not need the external ready signal. */
71     FSMC_ReadySignal_External = 1u, /*!< The ready signal is from external device. */
72 } FSMC_ReadySignal_Type;
73 
74 /*!
75  * @brief FSMC bus width type.
76  */
77 typedef enum
78 {
79     FSMC_BusWidth_8b  = 4u, /*!< Bus width: 8bit.  */
80     FSMC_BusWidth_16b = 0u, /*!< Bus width: 16bit. */
81     FSMC_BusWidth_32b = 1u, /*!< Bus width: 32bit. */
82 } FSMC_BusWidth_Type;
83 
84 /*!
85  * @brief This type of structure instance is used to keep the settings when calling the @ref FSMC_Init() to initialize the FSMC module.
86  */
87 typedef struct
88 {
89     FSMC_MemType_Type MemType;  /*!< Specify the memory type. */
90     FSMC_MemSize_Type MemSize;  /*!< Specify the memory size. */
91 } FSMC_Init_Type;
92 
93 /*!
94  * @brief This type of structure instance is used to keep the settings when calling the @ref FSMC_SetConf() to initialize the FSMC config.
95  */
96 typedef struct
97 {
98     uint32_t                ReadPeriod;     /*!< the cycles for 'reading data', NOE available period.  */
99     uint32_t                AddrSetTime;    /*!< the cycles for 'setup' when writing. */
100     uint32_t                WriteHoldTime;  /*!< the cycles between the master write action and slave capture done. */
101     uint32_t                WritePeriod;    /*!< the cycles for 'keep data' in writing operation, NWE available period.  */
102     FSMC_ReadySignal_Type   ReadySignal;    /*!< select if capture the external ready signal. */
103     uint32_t                SMReadPipe;     /*!< the cycles between the read action and release the bus.*/
104     FSMC_BusWidth_Type      BusWidth;       /*!< Specify the bus width. */
105 } FSMC_Conf_Type;
106 
107 /*!
108  * @brief Initialize the FSMC module.
109  *
110  * @param FSMCx FSMC instance.
111  * @param init Pointer to the initialization structure. See to @ref FSMC_Init_Type.
112  * @return None.
113  */
114 void FSMC_Init(FSMC_Type * FSMCx, FSMC_Init_Type * init);
115 
116 /*!
117  * @brief Set the FSMC config.
118  *
119  * @param FSMCx FSMC instance.
120  * @param index Config index.
121  * @param conf Pointer to the config structure. See to @ref FSMC_Conf_Type.
122  * @return None.
123  */
124 void FSMC_SetConf(FSMC_Type * FSMCx, uint32_t index, FSMC_Conf_Type * conf);
125 
126 /*!
127  * @brief Enable the FSMC config.
128  *
129  * Only last enabled config is available.
130  *
131  * @param FSMCx FSMC instance.
132  * @param index Config index.
133  * @return None.
134  */
135 void FSMC_EnableConf(FSMC_Type * FSMCx, uint32_t index);
136 
137 /*!
138  * @brief Get the Xfer data reg addr.
139  *
140  * @param FSMCx FSMC instance.
141  * @param bankn FSMC bank number.
142  * @param offset offset of data in FSMC bank.
143  * @return The addr value of Xfer data reg.
144  */
145 uint32_t FSMC_GetXferDataRegAddr(FSMC_Type *FSMCx, uint32_t bankn, uint32_t offset);
146 
147 /*!
148  * @brief Put the 32bit data by FSMC module.
149  *
150  * @param FSMCx FSMC instance.
151  * @param bankn FSMC bank number.
152  * @param offset offset of data in FSMC bank.
153  * @param data Data value to be send through the transmiter.
154  * @return None.
155  */
156 void FSMC_PutData32(FSMC_Type * FSMCx, uint32_t bankn, uint32_t offset, uint32_t data);
157 
158 /*!
159  * @brief Get the 32bit data by FSMC module.
160  *
161  * @param FSMCx FSMC instance.
162  * @param bankn FSMC bank number.
163  * @param offset offset of data in FSMC bank.
164  * @return The data value received from FSMC.
165  */
166 uint32_t FSMC_GetData32(FSMC_Type * FSMCx, uint32_t bankn, uint32_t offset);
167 
168 /*!
169  * @brief Put the 16bit data by FSMC module.
170  *
171  * @param FSMCx FSMC instance.
172  * @param bankn FSMC bank number.
173  * @param offset offset of data in FSMC bank.
174  * @param data Data value to be send through the transmiter.
175  * @return None.
176  */
177 void FSMC_PutData16(FSMC_Type * FSMCx, uint32_t bankn, uint32_t offset, uint16_t data);
178 
179 /*!
180  * @brief Get the 16bit data by FSMC module.
181  *
182  * @param FSMCx FSMC instance.
183  * @param bankn FSMC bank number.
184  * @param offset offset of data in FSMC bank.
185  * @return The data value received from FSMC.
186  */
187 uint16_t FSMC_GetData16(FSMC_Type * FSMCx, uint32_t bankn, uint32_t offset);
188 
189 /*!
190  * @brief Put the 8bit data by FSMC module.
191  *
192  * @param FSMCx FSMC instance.
193  * @param bankn FSMC bank number.
194  * @param offset offset of data in FSMC bank.
195  * @param data Data value to be send through the transmiter.
196  * @return None.
197  */
198 void FSMC_PutData8(FSMC_Type * FSMCx, uint32_t bankn, uint32_t offset, uint8_t data);
199 
200 /*!
201  * @brief Get the 8bit data by FSMC module.
202  *
203  * @param FSMCx FSMC instance.
204  * @param bankn FSMC bank number.
205  * @param offset offset of data in FSMC bank.
206  * @return The data value received from FSMC.
207  */
208 uint8_t FSMC_GetData8(FSMC_Type * FSMCx, uint32_t bankn, uint32_t offset);
209 
210 /*!
211  *@}
212  */
213 
214 #endif /* __HAL_FSMC_H__ */
215 
216