flash2  2.0.0.0
flash2.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 FLASH2_H
36 #define FLASH2_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 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define FLASH2_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
66  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
67  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
68  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_PWM )
71 
73 #define FLASH2_STATUS_WEL 0x02
74 #define FLASH2_STATUS_WSE 0x04
75 #define FLASH2_STATUS_WSP 0x08
76 #define FLASH2_STATUS_WPLD 0x10
77 #define FLASH2_STATUS_SEC 0x20
78 #define FLASH2_STATUS_RES 0x40
79 #define FLASH2_STATUS_BUSY 0x80
80 
81 
82 #define FLASH2_CFG_RES 0x01
83 #define FLASH2_CFG_IOC 0x02
84 #define FLASH2_CFG_BPNV 0x08
85 #define FLASH2_CFG_WPEN 0x80
86 
87 
88 #define FLASH2_INSTR_NOP 0x00
89 #define FLASH2_INSTR_RSTEN 0x66
90 #define FLASH2_INSTR_RST 0x99
91 #define FLASH2_INSTR_EQIO 0x38
92 #define FLASH2_INSTR_RSTQIO 0xFF
93 #define FLASH2_INSTR_RDSR 0x05
94 #define FLASH2_INSTR_WRSR 0x01
95 #define FLASH2_INSTR_RDCR 0x35
96 #define FLASH2_INSTR_READ 0x03
97 #define FLASH2_INSTR_HS_READ 0x0B
98 #define FLASH2_INSTR_SQOR 0x6B
99 #define FLASH2_INSTR_SQIOR 0xEB
100 #define FLASH2_INSTR_SDOR 0x3B
101 #define FLASH2_INSTR_SDIOR 0xBB
102 #define FLASH2_INSTR_SB 0xC0
103 #define FLASH2_INSTR_RBSQI 0x0C
104 #define FLASH2_INSTR_RBSPI 0xEC
105 #define FLASH2_INSTR_JEDECID 0x9F
106 #define FLASH2_INSTR_QUAD_JID 0xAF
107 #define FLASH2_INSTR_SFDP 0x5A
108 #define FLASH2_INSTR_WREN 0x06
109 #define FLASH2_INSTR_WRDI 0x04
110 #define FLASH2_INSTR_SE 0x20
111 #define FLASH2_INSTR_BE 0xD8
112 #define FLASH2_INSTR_CE 0xC7
113 #define FLASH2_INSTR_PP 0x02
114 #define FLASH2_INSTR_SPI_QUAD 0x32
115 #define FLASH2_INSTR_WRSU 0xB0
116 #define FLASH2_INSTR_WRRE 0x30
117 #define FLASH2_INSTR_RBPR 0x72
118 #define FLASH2_INSTR_WBPR 0x42
119 #define FLASH2_INSTR_LBPR 0x8D
120 #define FLASH2_INSTR_NVWLDR 0xE8
121 #define FLASH2_INSTR_ULBPR 0x98
122 #define FLASH2_INSTR_RSID 0x88
123 #define FLASH2_INSTR_PSID 0xA5
124 #define FLASH2_INSTR_LSID 0x85
127 #define FLASH2_START_PAGE_ADDRESS 0x010000
128 #define FLASH2_END_PAGE_ADDRESS 0x7FFFFF
129 #define FLASH2_FLASH_PAGE_SIZE 256
130 
134 #define FLASH2_RETVAL uint8_t
135 
136 #define FLASH2_OK 0x00
137 #define FLASH2_INIT_ERROR 0xFF
138  // End group macro
141 // --------------------------------------------------------------- PUBLIC TYPES
150 typedef struct
151 {
152  digital_out_t cs;
153 
154  // Output pins
155 
156  digital_out_t hld;
157  digital_out_t wp;
158 
159  // Modules
160 
161  spi_master_t spi;
162  pin_name_t chip_select;
163 
164 } flash2_t;
165 
169 typedef struct
170 {
171  // Communication gpio pins
172 
173  pin_name_t miso;
174  pin_name_t mosi;
175  pin_name_t sck;
176  pin_name_t cs;
177 
178  // Additional gpio pins
179 
180  pin_name_t hld;
181  pin_name_t wp;
182 
183  // static variable
184 
185  uint32_t spi_speed;
186  spi_master_mode_t spi_mode;
187  spi_master_chip_select_polarity_t cs_polarity;
188 
189 } flash2_cfg_t;
190  // End types group
192 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
193 
198 #ifdef __cplusplus
199 extern "C"{
200 #endif
201 
211 
221 
233 void flash2_generic_transfer ( flash2_t *ctx, uint8_t *wr_buf,
234  uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
235 
246 uint8_t flash2_busy( flash2_t *ctx );
247 
259 
269 
281 
293 
305 
316 
328 
338 
348 
358 
368 
393 
394 
418 
428 void flash2_spi_get_security_id( flash2_t *ctx, uint8_t *buffer, uint32_t data_count);
429 
440 void flash2_sqi_get_security_id( flash2_t *ctx, uint8_t *buffer, uint32_t data_count);
441 
457 void flash2_set_security_id( flash2_t *ctx, uint8_t *buffer, uint32_t data_count );
458 
469 
482 void flash2_spi_get_bpr( flash2_t *ctx, uint8_t *buffer, uint32_t data_count );
483 
496 void flash2_sqi_get_bpr( flash2_t *ctx, uint8_t *buffer, uint32_t data_count );
497 
508 void flash2_set_bpr( flash2_t *ctx, uint8_t *buffer );
509 
523 
535 void flash2_nonvolatile_write_lock( flash2_t *ctx, uint8_t *buffer);
536 
549 
572 void flash2_read_generic( flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count );
573 
590 void flash2_highspeedRread( flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count );
591 
610 void flash2_quadWrite( flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count );
611 
629 void flash2_write_generic( flash2_t *ctx, uint32_t address, uint8_t *buffer,
630  uint32_t data_count );
631 
647 
664 void flash2_quad_out_read( flash2_t *ctx, uint32_t address, uint8_t *buffer,
665  uint32_t data_count);
666 
684 void flash2_quad_io_read( flash2_t *ctx, uint32_t address, uint8_t mode, uint8_t *buffer, uint32_t data_count);
685 
706 
726 void flash2_set_burst( flash2_t *ctx, uint8_t length );
727 
754 void flash2_read_sqi_burst_wrap( flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count );
755 
784 void flash2_read_spi_burst_wrap( flash2_t *ctx, uint32_t address, uint8_t *buffer,
785  uint32_t data_count );
786 
800 void flash2_read_dual_output( flash2_t *ctx, uint32_t address, uint8_t *buffer,
801  uint32_t data_count );
802 
842 void flash2_read_dual_io ( flash2_t *ctx, uint32_t address, uint8_t *buffer,
843  uint32_t data_count );
844 
856 void flash2_sector_erase( flash2_t *ctx, uint32_t address );
857 
872 void flash2_block_erase( flash2_t *ctx, uint32_t address );
873 
886 
903 void flash2_get_sfdp_params( flash2_t *ctx, uint32_t address, uint8_t *buffer,
904  uint32_t data_count );
905 
919 
933 
947 
961 
974 uint8_t flash2_device_type( flash2_t *ctx );
975 
988 uint8_t flash2_device_id( flash2_t *ctx );
989 
1000 void flash2_reset( flash2_t *ctx );
1001 
1011 void flash2_write_status_reg( flash2_t *ctx, uint8_t s_reg );
1012 
1024 
1035 
1036 #ifdef __cplusplus
1037 }
1038 #endif
1039 #endif // _FLASH2_H_
1040  // End public_function group
1043 
1044 // ------------------------------------------------------------------------- END
flash2_hold_enable
void flash2_hold_enable(flash2_t *ctx)
Flash 2 Hold Enable.
flash2_t::spi
spi_master_t spi
Definition: flash2.h:161
flash2_device_type
uint8_t flash2_device_type(flash2_t *ctx)
Flash 2 Device Type.
flash2_nonvolatile_write_lock
void flash2_nonvolatile_write_lock(flash2_t *ctx, uint8_t *buffer)
Flash 2 Non-Volatile Write-Lock.
flash2_quad_device_id
uint8_t flash2_quad_device_id(flash2_t *ctx)
Flash 2 Quad Device ID.
flash2_write_generic
void flash2_write_generic(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Write.
flash2_read_dual_io
void flash2_read_dual_io(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Read Dual I/O.
flash2_sector_erase
void flash2_sector_erase(flash2_t *ctx, uint32_t address)
Flash 2 Sector Erase.
flash2_t::wp
digital_out_t wp
Definition: flash2.h:157
flash2_generic_transfer
void flash2_generic_transfer(flash2_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
flash2_quadWrite
void flash2_quadWrite(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Quad Write.
flash2_cfg_t::sck
pin_name_t sck
Definition: flash2.h:175
flash2_quad_device_type
uint8_t flash2_quad_device_type(flash2_t *ctx)
Flash 2 Quad Device Type.
flash2_quad_io_read
void flash2_quad_io_read(flash2_t *ctx, uint32_t address, uint8_t mode, uint8_t *buffer, uint32_t data_count)
Flash 2 Quad I/O Read.
flash2_quad_device_manufac
uint8_t flash2_quad_device_manufac(flash2_t *ctx)
Flash 2 Quad Device Manufacturer.
flash2_cfg_t::wp
pin_name_t wp
Definition: flash2.h:181
flash2_read_dual_output
void flash2_read_dual_output(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Read Dual Output.
flash2_security_status
uint8_t flash2_security_status(flash2_t *ctx)
Flash 2 Security Status.
flash2_t::hld
digital_out_t hld
Definition: flash2.h:156
flash2_cfg_t::hld
pin_name_t hld
Definition: flash2.h:180
flash2_init
FLASH2_RETVAL flash2_init(flash2_t *ctx, flash2_cfg_t *cfg)
Initialization function.
flash2_cfg_setup
void flash2_cfg_setup(flash2_cfg_t *cfg)
Config Object Initialization function.
flash2_device_manufac
uint8_t flash2_device_manufac(flash2_t *ctx)
Flash 2 Device Manufacturer.
flash2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash2.h:187
flash2_reset
void flash2_reset(flash2_t *ctx)
Flash 2 Reset.
flash2_spi_get_bpr
void flash2_spi_get_bpr(flash2_t *ctx, uint8_t *buffer, uint32_t data_count)
Flash 2 Get Block Protection Register SPI.
flash2_protect_status
uint8_t flash2_protect_status(flash2_t *ctx)
Flash 2 Protect Status.
flash2_read_generic
void flash2_read_generic(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Read.
flash2_cfg_t::mosi
pin_name_t mosi
Definition: flash2.h:174
flash2_cfg_t::cs
pin_name_t cs
Definition: flash2.h:176
flash2_quad_reset
void flash2_quad_reset(flash2_t *ctx)
Flash 2 Quad Reset.
flash2_quad_enable
void flash2_quad_enable(flash2_t *ctx)
Flash 2 Quad Enable.
flash2_write_status_reg
void flash2_write_status_reg(flash2_t *ctx, uint8_t s_reg)
Flash 2 Write Status Register.
FLASH2_RETVAL
#define FLASH2_RETVAL
Definition: flash2.h:134
flash2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash2.h:186
flash2_write_status
uint8_t flash2_write_status(flash2_t *ctx)
Flash 2 Write Status.
flash2_hold_disable
void flash2_hold_disable(flash2_t *ctx)
Flash 2 Hold Disable.
flash2_global_block_unlock
void flash2_global_block_unlock(flash2_t *ctx)
Flash 2 Global Block Unlock.
flash2_get_config_reg
uint8_t flash2_get_config_reg(flash2_t *ctx)
Flash 2 Get Config Register.
flash2_cfg_t
Click configuration structure definition.
Definition: flash2.h:170
flash2_device_id
uint8_t flash2_device_id(flash2_t *ctx)
Flash 2 Device ID.
flash2_t::chip_select
pin_name_t chip_select
Definition: flash2.h:162
flash2_lock_security_id
void flash2_lock_security_id(flash2_t *ctx)
flash2_lock_security_id
flash2_cfg_t::spi_speed
uint32_t spi_speed
Definition: flash2.h:185
flash2_quad_out_read
void flash2_quad_out_read(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Quad Output Read.
flash2_read_sqi_burst_wrap
void flash2_read_sqi_burst_wrap(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Read Burst with Wrap through SQI.
flash2_program_status
uint8_t flash2_program_status(flash2_t *ctx)
Flash 2 Program Status.
flash2_set_burst
void flash2_set_burst(flash2_t *ctx, uint8_t length)
Flash 2 Set Burst.
flash2_lockBpr
void flash2_lockBpr(flash2_t *ctx)
Flash 2 Lock Block Protection Register.
flash2_write_suspend
void flash2_write_suspend(flash2_t *ctx)
Flash 2 Write Suspend.
flash2_get_sfdp_params
void flash2_get_sfdp_params(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 get Serial Flash Discoverable \ Parameters.
flash2_set_bpr
void flash2_set_bpr(flash2_t *ctx, uint8_t *buffer)
Flash 2 Set Block Protection Register.
flash2_sqi_get_security_id
void flash2_sqi_get_security_id(flash2_t *ctx, uint8_t *buffer, uint32_t data_count)
Flash 2 Get Security ID SQI.
flash2_highspeedRread
void flash2_highspeedRread(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 High Speed Read.
flash2_t
Click ctx object definition.
Definition: flash2.h:151
flash2_write_protect_enable
void flash2_write_protect_enable(flash2_t *ctx)
Flash 2 Write Protect Enable.
flash2_erase_status
uint8_t flash2_erase_status(flash2_t *ctx)
Flash 2 Erase Status.
flash2_set_security_id
void flash2_set_security_id(flash2_t *ctx, uint8_t *buffer, uint32_t data_count)
Flash 2 Set Security ID.
flash2_write_disable
void flash2_write_disable(flash2_t *ctx)
Flash 2 Write Disable.
flash2_write_protect_disable
void flash2_write_protect_disable(flash2_t *ctx)
Flash 2 Write Protect Disable.
flash2_sqi_get_bpr
void flash2_sqi_get_bpr(flash2_t *ctx, uint8_t *buffer, uint32_t data_count)
Flash 2 Get Block Protection Register SQI.
flash2_write_enable
void flash2_write_enable(flash2_t *ctx)
Flash 2 Write Enable.
flash2_busy
uint8_t flash2_busy(flash2_t *ctx)
Flash 2 Busy.
flash2_block_erase
void flash2_block_erase(flash2_t *ctx, uint32_t address)
Flash 2 Block Erase.
flash2_get_status_reg
uint8_t flash2_get_status_reg(flash2_t *ctx)
Flash 2 Get Status Register.
flash2_chip_erase
void flash2_chip_erase(flash2_t *ctx)
Flash 2 Chip Erase.
flash2_cfg_t::miso
pin_name_t miso
Definition: flash2.h:173
flash2_t::cs
digital_out_t cs
Definition: flash2.h:152
flash2_spi_get_security_id
void flash2_spi_get_security_id(flash2_t *ctx, uint8_t *buffer, uint32_t data_count)
Flash 2 Get Security ID SPI.
flash2_write_resume
void flash2_write_resume(flash2_t *ctx)
Flash 2 Write Resume.
flash2_read_spi_burst_wrap
void flash2_read_spi_burst_wrap(flash2_t *ctx, uint32_t address, uint8_t *buffer, uint32_t data_count)
Flash 2 Read Burst with Wrap through SPI.