flash4  2.0.0.0
flash4.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 FLASH4_H
36 #define FLASH4_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_spi_master.h"
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
67 #define FLASH4_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
69  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
70  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
71  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
72  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
73  cfg.io2 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
74  cfg.io3 = MIKROBUS( mikrobus, MIKROBUS_INT )
75 
81 #define FLASH4_RETVAL uint8_t
82 
83 #define FLASH4_OK 0x00
84 #define FLASH4_INIT_ERROR 0xFF
85 
90 #define FLASH4_MANUFACTURER_ID 0x01
91 #define FLASH4_DEVICE_ID 0x19
92 
97 #define FLASH4_CMD_READ_ID 0x90
98 #define FLASH4_CMD_READ_IDENTIFICATION 0x9F
99 #define FLASH4_CMD_READ_ELECTRONIC_SIGNATURE 0xAB
100 #define FLASH4_CMD_READ_SERIAL_FLASH_PARAMETERS 0x5A
101 #define FLASH4_CMD_READ_STATUS_REG_1 0x05
102 #define FLASH4_CMD_READ_STATUS_REG_2 0x07
103 #define FLASH4_CMD_READ_CONFIGURATION_REG 0x35
104 #define FLASH4_CMD_READ_BANK_REG 0x16
105 #define FLASH4_CMD_WRITE_BANK_REG 0x17
106 #define FLASH4_CMD_ACCESS_BANK_REG 0xB9
107 #define FLASH4_CMD_WRITE_REGISTER_WRR 0x01
108 #define FLASH4_CMD_WRITE_ENABLE_WREN 0x06
109 #define FLASH4_CMD_WRITE_DISABLE_WRDI 0x04
110 #define FLASH4_CMD_CLEAR_STATUS_REG 0x30
111 #define FLASH4_CMD_READ_ECC_STATUS_REG 0x18
112 #define FLASH4_CMD_READ_AUTO_BOOT_REG 0x14
113 #define FLASH4_CMD_WRITE_AUTO_BOOT_REG 0x15
114 #define FLASH4_CMD_PROGRAM_NVDLR 0x43
115 #define FLASH4_CMD_WRITE_VDLR 0x4A
116 #define FLASH4_CMD_READ_DATA_LEARNING_PATTERN 0x41
117 #define FLASH4_CMD_READ_FLASH 0x03
118 #define FLASH4_CMD_4READ_FLASH 0x13
119 #define FLASH4_CMD_FAST_READ_FLASH 0x0B
120 #define FLASH4_CMD_FAST_4READ_FLASH 0x0C
121 #define FLASH4_CMD_FAST_READ_DDR 0x0D
122 #define FLASH4_CMD_FAST_4READ_DDR 0x0E
123 #define FLASH4_CMD_PAGE_PROGRAM 0x02
124 #define FLASH4_CMD_PAGE_4PROGRAM 0x12
125 #define FLASH4_CMD_PROGRAM_SUSPEND 0x85
126 #define FLASH4_CMD_PROGRAM_RESUME 0x8A
127 #define FLASH4_CMD_SECTOR_ERASE 0xD8
128 #define FLASH4_CMD_SECTOR_4ERASE 0xDC
129 #define FLASH4_CMD_BULK_ERASE 0xC7
130 #define FLASH4_CMD_ERASE_SUSPEND 0x75
131 #define FLASH4_CMD_ERASE_RESUME 0x7A
132 #define FLASH4_CMD_WRITE_OTP_PROGRAM 0x42
133 #define FLASH4_CMD_READ_OTP 0x4B
134 #define FLASH4_CMD_READ_ASP 0x2B
135 #define FLASH4_CMD_WRITE_ASP_PROGRAM 0x2F
136 #define FLASH4_CMD_READ_DYB 0xE0
137 #define FLASH4_CMD_WRITE_DYB 0xE1
138 #define FLASH4_CMD_READ_PPB 0xE2
139 #define FLASH4_CMD_WRITE_PPB 0xE3
140 #define FLASH4_CMD_ERASE_PPB 0xE4
141 #define FLASH4_CMD_READ_PPB_LOCK_BIT 0xA7
142 #define FLASH4_CMD_WRITE_PPB_LOCK_BIT 0xA6
143 #define FLASH4_CMD_SOFTWARE_RESET 0xF0
144 #define FLASH4_CMD_MODE_BIT_RESET 0xFF
145  // End group macro
147 // --------------------------------------------------------------- PUBLIC TYPES
156 typedef struct
157 {
158  // Output pins
159 
160  digital_out_t cs;
161  digital_out_t rst;
162  digital_out_t io3;
163  digital_out_t io2;
164 
165  // Modules
166 
167  spi_master_t spi;
168  pin_name_t chip_select;
169 
170 } flash4_t;
171 
175 typedef struct
176 {
177  // Communication gpio pins
178 
179  pin_name_t miso;
180  pin_name_t mosi;
181  pin_name_t sck;
182  pin_name_t cs;
183 
184  // Additional gpio pins
185 
186  pin_name_t rst;
187  pin_name_t io2;
188  pin_name_t io3;
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 } flash4_cfg_t;
197  // End types group
199 
200 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
201 
206 #ifdef __cplusplus
207 extern "C"{
208 #endif
209 
219 
229 
241 void flash4_generic_transfer ( flash4_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
242 
251 void flash4_read_manufacturer_id ( flash4_t *ctx, uint8_t *device_id );
252 
262 void flash4_read_identification ( flash4_t *ctx, uint8_t *out_data, uint8_t n_data );
263 
264 
273 
283 void flash4_read_serial_flash_parameters ( flash4_t *ctx, uint8_t *out_params, uint8_t c );
284 
293 uint8_t flash4_read_byte ( flash4_t *ctx, uint8_t reg );
294 
304 void flash4_write_byte ( flash4_t *ctx, uint8_t reg, uint8_t data_buff );
305 
314 void flash4_write_command ( flash4_t *ctx, uint8_t cmd );
315 
325 void flash4_write_reg ( flash4_t *ctx, uint8_t status, uint8_t config );
326 
336 void flash4_read_ecc ( flash4_t *ctx, uint8_t *out_ecc, uint8_t n_dummy);
337 
346 void flash4_read_auto_boot ( flash4_t *ctx, uint8_t *out_ab );
347 
358 void flash4_read_flash( flash4_t *ctx, uint8_t *out_data, uint32_t addr, uint8_t n_data);
359 
370 void flash4_read_flash_4 ( flash4_t *ctx, uint8_t *out_data, uint32_t addr, uint8_t n_data );
371 
382 void flash4_fast_read_flash ( flash4_t *ctx, uint8_t *out_data, uint32_t addr, uint8_t n_data_dummy);
383 
394 void flash4_fast_read_flash_4 ( flash4_t *ctx, uint8_t *out_data,uint8_t addr, uint8_t n_data );
395 
406 void flash4_page_program_4 ( flash4_t *ctx, uint8_t *in_data, uint32_t addr, uint8_t n_data );
407 
416 void flash4_sector_erase ( flash4_t *ctx, uint32_t addr );
417 
426 void flash4_sector_erase_4( flash4_t *ctx, uint32_t addr );
427 
435 uint8_t flash4_check_wip ( flash4_t *ctx );
436 
444 uint8_t flash4_check_wel ( flash4_t *ctx );
445 
453 uint8_t flash4_check_ps ( flash4_t *ctx );
454 
462 uint8_t flash4_check_es ( flash4_t *ctx );
463 
471 void flash4_reset ( flash4_t *ctx );
472 
473 #ifdef __cplusplus
474 }
475 #endif
476 #endif // _FLASH4_H_
477  // End public_function group
480 
481 // ------------------------------------------------------------------------- END
flash4_t::chip_select
pin_name_t chip_select
Definition: flash4.h:168
flash4_read_ecc
void flash4_read_ecc(flash4_t *ctx, uint8_t *out_ecc, uint8_t n_dummy)
Read ECC Status Register function.
flash4_read_manufacturer_id
void flash4_read_manufacturer_id(flash4_t *ctx, uint8_t *device_id)
Read Manufacturer ID function.
flash4_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash4.h:194
flash4_write_byte
void flash4_write_byte(flash4_t *ctx, uint8_t reg, uint8_t data_buff)
Write one byte to register function.
flash4_check_es
uint8_t flash4_check_es(flash4_t *ctx)
Function for es.
flash4_fast_read_flash
void flash4_fast_read_flash(flash4_t *ctx, uint8_t *out_data, uint32_t addr, uint8_t n_data_dummy)
Fast read Flash function.
flash4_reset
void flash4_reset(flash4_t *ctx)
Function for reset.
flash4_cfg_setup
void flash4_cfg_setup(flash4_cfg_t *cfg)
Config Object Initialization function.
flash4_t::cs
digital_out_t cs
Definition: flash4.h:160
flash4_read_serial_flash_parameters
void flash4_read_serial_flash_parameters(flash4_t *ctx, uint8_t *out_params, uint8_t c)
Read Serial Flash Discoverable Parameters function.
flash4_read_auto_boot
void flash4_read_auto_boot(flash4_t *ctx, uint8_t *out_ab)
Read AutoBoot Register function.
flash4_cfg_t::mosi
pin_name_t mosi
Definition: flash4.h:180
flash4_page_program_4
void flash4_page_program_4(flash4_t *ctx, uint8_t *in_data, uint32_t addr, uint8_t n_data)
Write data to address with 4 byte address function.
flash4_check_wip
uint8_t flash4_check_wip(flash4_t *ctx)
Function for wip.
flash4_cfg_t::sck
pin_name_t sck
Definition: flash4.h:181
flash4_cfg_t::spi_speed
uint32_t spi_speed
Definition: flash4.h:192
FLASH4_RETVAL
#define FLASH4_RETVAL
Definition: flash4.h:81
flash4_cfg_t::rst
pin_name_t rst
Definition: flash4.h:186
flash4_check_wel
uint8_t flash4_check_wel(flash4_t *ctx)
Function for wel.
flash4_init
FLASH4_RETVAL flash4_init(flash4_t *ctx, flash4_cfg_t *cfg)
Initialization function. *.
flash4_t::rst
digital_out_t rst
Definition: flash4.h:161
flash4_cfg_t::io3
pin_name_t io3
Definition: flash4.h:188
flash4_t::spi
spi_master_t spi
Definition: flash4.h:167
flash4_t::io2
digital_out_t io2
Definition: flash4.h:163
flash4_cfg_t::io2
pin_name_t io2
Definition: flash4.h:187
flash4_read_flash_4
void flash4_read_flash_4(flash4_t *ctx, uint8_t *out_data, uint32_t addr, uint8_t n_data)
Read Flash with 4 byte address function.
flash4_cfg_t
Click configuration structure definition.
Definition: flash4.h:176
flash4_cfg_t::cs
pin_name_t cs
Definition: flash4.h:182
flash4_t
Click ctx object definition.
Definition: flash4.h:157
flash4_check_ps
uint8_t flash4_check_ps(flash4_t *ctx)
Function for ps.
flash4_sector_erase_4
void flash4_sector_erase_4(flash4_t *ctx, uint32_t addr)
Sector Erase for 4 bytes address function.
flash4_cfg_t::miso
pin_name_t miso
Definition: flash4.h:179
flash4_sector_erase
void flash4_sector_erase(flash4_t *ctx, uint32_t addr)
Sector Erase function.
flash4_read_flash
void flash4_read_flash(flash4_t *ctx, uint8_t *out_data, uint32_t addr, uint8_t n_data)
Read Flash function.
flash4_read_identification
void flash4_read_identification(flash4_t *ctx, uint8_t *out_data, uint8_t n_data)
Read Identification function.
flash4_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash4.h:193
flash4_read_byte
uint8_t flash4_read_byte(flash4_t *ctx, uint8_t reg)
Read byte from register function.
flash4_generic_transfer
void flash4_generic_transfer(flash4_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
flash4_t::io3
digital_out_t io3
Definition: flash4.h:162
flash4_write_command
void flash4_write_command(flash4_t *ctx, uint8_t cmd)
Write command function.
flash4_write_reg
void flash4_write_reg(flash4_t *ctx, uint8_t status, uint8_t config)
Write data to register function.
flash4_read_electronic_id
uint8_t flash4_read_electronic_id(flash4_t *ctx)
Read Electronic Signature function.
flash4_fast_read_flash_4
void flash4_fast_read_flash_4(flash4_t *ctx, uint8_t *out_data, uint8_t addr, uint8_t n_data)
Fast read Flash with 4 byte address function.