flash3  2.0.0.0
flash3.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef FLASH3_H
36 #define FLASH3_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_spi_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
63 #define FLASH3_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.miso= MIKROBUS( mikrobus, MIKROBUS_MISO ); \
65  cfg.mosi= MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
66  cfg.sck= MIKROBUS( mikrobus, MIKROBUS_SCK ); \
67  cfg.cs= MIKROBUS( mikrobus, MIKROBUS_CS ); \
68  cfg.hlo= MIKROBUS( mikrobus, MIKROBUS_RST ); \
69  cfg.wp= MIKROBUS( mikrobus, MIKROBUS_PWM )
70 
76 #define FLASH3_RETVAL uint8_t
77 
78 #define FLASH3_OK 0x00
79 #define FLASH3_INIT_ERROR 0xFF
80 
86 #define FLASH3_STATUS_WIP 0x01
87 #define FLASH3_STATUS_WEL 0x02
88 #define FLASH3_STATUS_BP_0 0x04
89 #define FLASH3_STATUS_BP_1 0x08
90 #define FLASH3_STATUS_BP_2 0x10
91 #define FLASH3_STATUS_BP_3 0x20
92 #define FLASH3_STATUS_QE 0x40
93 #define FLASH3_STATUS_SRWD 0x80
94 
100 #define FLASH3_FUNC_TBS 0x02
101 #define FLASH3_FUNC_PSUS 0x04
102 #define FLASH3_FUNC_ESUS 0x08
103 #define FLASH3_FUNC_IRL_0 0x10
104 #define FLASH3_FUNC_IRL_1 0x20
105 #define FLASH3_FUNC_IRL_2 0x40
106 #define FLASH3_FUNC_IRL_3 0x80
107 
113 #define FLASH3_NOP 0x00
114 #define FLASH3_WRSR 0x01
115 #define FLASH3_PP 0x02
116 #define FLASH3_WRDI 0x04
117 #define FLASH3_RDSR 0x05
118 #define FLASH3_WREN 0x06
119 #define FLASH3_RDFR 0x48
120 #define FLASH3_WRFR 0x42
121 #define FLASH3_PERSUS 0x75
122 #define FLASH3_PERRSM 0x7A
123 #define FLASH3_DP 0xB9
124 #define FLASH3_RDID 0xAB
125 #define FLASH3_SRP 0xC0
126 #define FLASH3_RDJDID 0x9F
127 #define FLASH3_RDMDID 0x90
128 #define FLASH3_RDJDIDQ 0xAF
129 #define FLASH3_RDUID 0x4B
130 #define FLASH3_RDSFDP 0x5A
131 #define FLASH3_RSTEN 0x66
132 #define FLASH3_RST 0x99
133 #define FLASH3_IRER 0x64
134 #define FLASH3_IRP 0x62
135 #define FLASH3_IRRD 0x68
136 #define FLASH3_SECUN_LOCK 0x26
137 #define FLASH3_SECLOCK 0x24
138 
144 #define FLASH3_SECTOR_ER 0xD7
145 #define FLASH3_BLOCK_ER32 0x52
146 #define FLASH3_BLOCK_ER64 0xD8
147 #define FLASH3_CHIP_ER 0xC7
148 
154 #define FLASH3_NORMAL_RD 0x03
155 #define FLASH3_FAST_RD 0x0B
156  // End group macro
159 // --------------------------------------------------------------- PUBLIC TYPES
168 typedef struct
169 {
170  // Output pins
171 
172  digital_out_t hlo;
173  digital_out_t wp;
174  digital_out_t cs;
175 
176  // Modules
177 
178  spi_master_t spi;
179  pin_name_t chip_select;
180 
181 } flash3_t;
182 
186 typedef struct
187 {
188  // Communication gpio pins
189 
190  pin_name_t miso;
191  pin_name_t mosi;
192  pin_name_t sck;
193  pin_name_t cs;
194 
195  // Additional gpio pins
196 
197  pin_name_t hlo;
198  pin_name_t wp;
199 
200  // static variable
201 
202  uint32_t spi_speed;
203  spi_master_mode_t spi_mode;
204  spi_master_chip_select_polarity_t cs_polarity;
205 
206 } flash3_cfg_t;
207  // End types group
209 
210 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
211 
216 #ifdef __cplusplus
217 extern "C"{
218 #endif
219 
229 
239 
251 void flash3_generic_transfer ( flash3_t *ctx, uint8_t *wr_buf,
252  uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
253 
261 void flash3_pause ( flash3_t *ctx );
262 
270 void flash3_unpause ( flash3_t *ctx );
271 
280 
289 
297 void flash3_setting ( flash3_t *ctx );
298 
307 
316 
325 void flash3_write_cmd ( flash3_t *ctx, uint8_t cmd );
326 
337 void flash3_write_adv_cmd ( flash3_t *ctx, uint8_t cmd, uint8_t *arg, uint32_t cnt );
338 
346 uint8_t flash3_rd_stat_reg ( flash3_t *ctx );
347 
355 void flash3_wr_stat_reg ( flash3_t *ctx, uint8_t status );
356 
364 uint8_t flash3_rd_func_reg ( flash3_t *ctx );
365 
374 void flash3_wr_func_reg ( flash3_t *ctx, uint8_t function );
375 
386 void flash3_normal_read( flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt );
387 
398 void flash3_fast_read ( flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt );
399 
410 void flash3_write ( flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt );
411 
420 
429 void flash3_sector_erase ( flash3_t *ctx, uint32_t address );
430 
439 void flash3_block32_erase( flash3_t *ctx, uint32_t address );
440 
449 void flash3_block64_erase( flash3_t *ctx, uint32_t address );
450 
451 
452 #ifdef __cplusplus
453 }
454 #endif
455 #endif // _FLASH3_H_
456  // End public_function group
459 
460 // ------------------------------------------------------------------------- END
flash3_pause
void flash3_pause(flash3_t *ctx)
Pause function.
flash3_cfg_t::sck
pin_name_t sck
Definition: flash3.h:192
flash3_fast_read
void flash3_fast_read(flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt)
Fast data read function.
flash3_wr_stat_reg
void flash3_wr_stat_reg(flash3_t *ctx, uint8_t status)
Write Status Register function.
flash3_cfg_t::mosi
pin_name_t mosi
Definition: flash3.h:191
flash3_t::spi
spi_master_t spi
Definition: flash3.h:178
flash3_wr_func_reg
void flash3_wr_func_reg(flash3_t *ctx, uint8_t function)
Reads Function Register function.
flash3_normal_read
void flash3_normal_read(flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt)
Normal data read function.
flash3_write_adv_cmd
void flash3_write_adv_cmd(flash3_t *ctx, uint8_t cmd, uint8_t *arg, uint32_t cnt)
Flash 3 Command Advanced function.
flash3_cfg_t::cs
pin_name_t cs
Definition: flash3.h:193
flash3_wr_prot_disable
void flash3_wr_prot_disable(flash3_t *ctx)
Disable write function.
flash3_write_cmd
void flash3_write_cmd(flash3_t *ctx, uint8_t cmd)
Write command function.
flash3_t::wp
digital_out_t wp
Definition: flash3.h:173
flash3_cfg_setup
void flash3_cfg_setup(flash3_cfg_t *cfg)
Config Object Initialization function.
flash3_t::hlo
digital_out_t hlo
Definition: flash3.h:172
flash3_t::chip_select
pin_name_t chip_select
Definition: flash3.h:179
flash3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash3.h:204
flash3_t
Click ctx object definition.
Definition: flash3.h:169
flash3_cfg_t::hlo
pin_name_t hlo
Definition: flash3.h:197
flash3_init
FLASH3_RETVAL flash3_init(flash3_t *ctx, flash3_cfg_t *cfg)
Initialization function.
flash3_cfg_t
Click configuration structure definition.
Definition: flash3.h:187
flash3_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash3.h:203
flash3_write_enable
void flash3_write_enable(flash3_t *ctx)
Enable write function.
flash3_write
void flash3_write(flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt)
Fast data read function.
flash3_chip_erase
void flash3_chip_erase(flash3_t *ctx)
Chip erase function.
flash3_rd_func_reg
uint8_t flash3_rd_func_reg(flash3_t *ctx)
Reads Function Register function.
flash3_write_disable
void flash3_write_disable(flash3_t *ctx)
Enable write function.
flash3_cfg_t::miso
pin_name_t miso
Definition: flash3.h:190
flash3_sector_erase
void flash3_sector_erase(flash3_t *ctx, uint32_t address)
Sector erase function.
flash3_wr_prot_enable
void flash3_wr_prot_enable(flash3_t *ctx)
Write Protect enable function.
flash3_block64_erase
void flash3_block64_erase(flash3_t *ctx, uint32_t address)
Block 64 erase function.
FLASH3_RETVAL
#define FLASH3_RETVAL
Definition: flash3.h:76
flash3_block32_erase
void flash3_block32_erase(flash3_t *ctx, uint32_t address)
Block 32 erase function.
flash3_generic_transfer
void flash3_generic_transfer(flash3_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
flash3_cfg_t::wp
pin_name_t wp
Definition: flash3.h:198
flash3_rd_stat_reg
uint8_t flash3_rd_stat_reg(flash3_t *ctx)
Reads Status Register function.
flash3_unpause
void flash3_unpause(flash3_t *ctx)
Unpause function.
flash3_cfg_t::spi_speed
uint32_t spi_speed
Definition: flash3.h:202
flash3_t::cs
digital_out_t cs
Definition: flash3.h:174
flash3_setting
void flash3_setting(flash3_t *ctx)
Initialisation function.