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 "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
53 #define FLASH3_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.miso= MIKROBUS( mikrobus, MIKROBUS_MISO ); \
55  cfg.mosi= MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
56  cfg.sck= MIKROBUS( mikrobus, MIKROBUS_SCK ); \
57  cfg.cs= MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.hlo= MIKROBUS( mikrobus, MIKROBUS_RST ); \
59  cfg.wp= MIKROBUS( mikrobus, MIKROBUS_PWM )
60 
66 #define FLASH3_RETVAL uint8_t
67 
68 #define FLASH3_OK 0x00
69 #define FLASH3_INIT_ERROR 0xFF
70 
76 #define FLASH3_STATUS_WIP 0x01
77 #define FLASH3_STATUS_WEL 0x02
78 #define FLASH3_STATUS_BP_0 0x04
79 #define FLASH3_STATUS_BP_1 0x08
80 #define FLASH3_STATUS_BP_2 0x10
81 #define FLASH3_STATUS_BP_3 0x20
82 #define FLASH3_STATUS_QE 0x40
83 #define FLASH3_STATUS_SRWD 0x80
84 
90 #define FLASH3_FUNC_TBS 0x02
91 #define FLASH3_FUNC_PSUS 0x04
92 #define FLASH3_FUNC_ESUS 0x08
93 #define FLASH3_FUNC_IRL_0 0x10
94 #define FLASH3_FUNC_IRL_1 0x20
95 #define FLASH3_FUNC_IRL_2 0x40
96 #define FLASH3_FUNC_IRL_3 0x80
97 
103 #define FLASH3_NOP 0x00
104 #define FLASH3_WRSR 0x01
105 #define FLASH3_PP 0x02
106 #define FLASH3_WRDI 0x04
107 #define FLASH3_RDSR 0x05
108 #define FLASH3_WREN 0x06
109 #define FLASH3_RDFR 0x48
110 #define FLASH3_WRFR 0x42
111 #define FLASH3_PERSUS 0x75
112 #define FLASH3_PERRSM 0x7A
113 #define FLASH3_DP 0xB9
114 #define FLASH3_RDID 0xAB
115 #define FLASH3_SRP 0xC0
116 #define FLASH3_RDJDID 0x9F
117 #define FLASH3_RDMDID 0x90
118 #define FLASH3_RDJDIDQ 0xAF
119 #define FLASH3_RDUID 0x4B
120 #define FLASH3_RDSFDP 0x5A
121 #define FLASH3_RSTEN 0x66
122 #define FLASH3_RST 0x99
123 #define FLASH3_IRER 0x64
124 #define FLASH3_IRP 0x62
125 #define FLASH3_IRRD 0x68
126 #define FLASH3_SECUN_LOCK 0x26
127 #define FLASH3_SECLOCK 0x24
128 
134 #define FLASH3_SECTOR_ER 0xD7
135 #define FLASH3_BLOCK_ER32 0x52
136 #define FLASH3_BLOCK_ER64 0xD8
137 #define FLASH3_CHIP_ER 0xC7
138 
144 #define FLASH3_NORMAL_RD 0x03
145 #define FLASH3_FAST_RD 0x0B
146  // End group macro
149 // --------------------------------------------------------------- PUBLIC TYPES
158 typedef struct
159 {
160  // Output pins
161 
162  digital_out_t hlo;
163  digital_out_t wp;
164  digital_out_t cs;
165 
166  // Modules
167 
168  spi_master_t spi;
169  pin_name_t chip_select;
170 
171 } flash3_t;
172 
176 typedef struct
177 {
178  // Communication gpio pins
179 
180  pin_name_t miso;
181  pin_name_t mosi;
182  pin_name_t sck;
183  pin_name_t cs;
184 
185  // Additional gpio pins
186 
187  pin_name_t hlo;
188  pin_name_t wp;
189 
190  // static variable
191 
192  uint32_t spi_speed;
193  spi_master_mode_t spi_mode;
194  spi_master_chip_select_polarity_t cs_polarity;
195 
196 } flash3_cfg_t;
197  // End types group
199 
200 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
201 
206 #ifdef __cplusplus
207 extern "C"{
208 #endif
209 
218 void flash3_cfg_setup ( flash3_cfg_t *cfg );
219 
229 
241 void flash3_generic_transfer ( flash3_t *ctx, uint8_t *wr_buf,
242  uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
243 
251 void flash3_pause ( flash3_t *ctx );
252 
260 void flash3_unpause ( flash3_t *ctx );
261 
269 void flash3_wr_prot_enable ( flash3_t *ctx );
270 
278 void flash3_wr_prot_disable ( flash3_t *ctx );
279 
287 void flash3_setting ( flash3_t *ctx );
288 
296 void flash3_write_enable ( flash3_t *ctx );
297 
305 void flash3_write_disable ( flash3_t *ctx );
306 
315 void flash3_write_cmd ( flash3_t *ctx, uint8_t cmd );
316 
327 void flash3_write_adv_cmd ( flash3_t *ctx, uint8_t cmd, uint8_t *arg, uint32_t cnt );
328 
336 uint8_t flash3_rd_stat_reg ( flash3_t *ctx );
337 
345 void flash3_wr_stat_reg ( flash3_t *ctx, uint8_t status );
346 
354 uint8_t flash3_rd_func_reg ( flash3_t *ctx );
355 
364 void flash3_wr_func_reg ( flash3_t *ctx, uint8_t function );
365 
376 void flash3_normal_read( flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt );
377 
388 void flash3_fast_read ( flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt );
389 
400 void flash3_write ( flash3_t *ctx, uint32_t addr, uint8_t *buff, uint32_t cnt );
401 
409 void flash3_chip_erase ( flash3_t *ctx );
410 
419 void flash3_sector_erase ( flash3_t *ctx, uint32_t address );
420 
429 void flash3_block32_erase( flash3_t *ctx, uint32_t address );
430 
439 void flash3_block64_erase( flash3_t *ctx, uint32_t address );
440 
441 
442 #ifdef __cplusplus
443 }
444 #endif
445 #endif // _FLASH3_H_
446  // End public_function group
449 
450 // ------------------------------------------------------------------------- END
flash3_pause
void flash3_pause(flash3_t *ctx)
Pause function.
flash3_cfg_t::sck
pin_name_t sck
Definition: flash3.h:182
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:181
flash3_t::spi
spi_master_t spi
Definition: flash3.h:168
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:183
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:163
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:162
flash3_t::chip_select
pin_name_t chip_select
Definition: flash3.h:169
flash3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash3.h:194
flash3_t
Click ctx object definition.
Definition: flash3.h:158
flash3_cfg_t::hlo
pin_name_t hlo
Definition: flash3.h:187
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:176
flash3_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash3.h:193
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:180
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:66
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:188
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:192
flash3_t::cs
digital_out_t cs
Definition: flash3.h:164
flash3_setting
void flash3_setting(flash3_t *ctx)
Initialisation function.