flash5  2.0.0.0
flash5.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 FLASH5_H
36 #define FLASH5_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 FLASH5_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.wp = MIKROBUS( mikrobus, MIKROBUS_RST ); \
73  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_PWM );
74 
80 #define FLASH5_RETVAL uint8_t
81 
82 #define FLASH5_OK 0x00
83 #define FLASH5_INIT_ERROR 0xFF
84 
90 #define FLASH5_MAX_addr 0xFFFF
91 #define FLASH5_PAGESIZE 2048
92 
98 #define FLASH5_CMD_RESET 0xFF
99 #define FLASH5_CMD_DEVICE_ID 0x9F
100 #define FLASH5_CMD_READ_REG_STATUS1 0x05
101 #define FLASH5_CMD_READ_REG_STATUS2 0x0F
102 #define FLASH5_CMD_WRITE_REG_STATUS1 0x01
103 #define FLASH5_CMD_WRITE_REG_STATUS2 0x1F
104 #define FLASH5_CMD_WRITE_ENABLE 0x06
105 #define FLASH5_CMD_WRITE_DISABLE 0x04
106 #define FLASH5_CMD_BAD_BLOCK_MANAGE 0xA1
107 #define FLASH5_CMD_READ_BBM_TABLE 0xA5
108 #define FLASH5_CMD_LAST_ECC_FAIL_PAGE 0xA9
109 #define FLASH5_CMD_BLOCK_ERASE 0xD8
110 #define FLASH5_CMD_LOAD_DATA 0x02
111 #define FLASH5_CMD__RND_LOAD_DATA 0x84
112 #define FLASH5_CMD_QUAD_LOAD_DATA 0x32
113 #define FLASH5_CMD_QUAD_RND_LOAD_DATA 0x34
114 #define FLASH5_CMD_EXECUTE 0x10
115 #define FLASH5_CMD_PAGE_READ_DATA 0x13
116 #define FLASH5_CMD_READ_DATA 0x03
117 #define FLASH5_CMD_FAST_READ 0x0B
118 #define FLASH5_CMD_FAST_READ_4BYTE_addr 0x0C
119 #define FLASH5_CMD_FAST_READ_DUAL_OUT 0x3B
120 #define FLASH5_CMD_FAST_READ_DUAL_OUT_4BYTE_addr 0x3C
121 #define FLASH5_CMD_FAST_READ_QUAD_OUT 0x6B
122 #define FLASH5_CMD_FAST_READ_QUAD_OUT_4BYTE_addr 0x6C
123 #define FLASH5_CMD_FAST_READ_DUAL_IO 0xBB
124 #define FLASH5_CMD_FAST_READ_IO_4BYTE_addr 0xBC
125 #define FLASH5_CMD_FAST_READ_QUAD_IO 0xEB
126 #define FLASH5_CMD_FAST_READ_QUAD_IO_4BYTE_addr 0xEC
127 
133 #define FLASH5_REG_STATUS_1 0xA0
134 #define FLASH5_REG_STATUS_2 0xB0
135 #define FLASH5_REG_STATUS_3 0xC0
136 
142 #define FLASH5_RS1_WRITE_PROTECTION_ENABLE 0x02
143 #define FLASH5_RS1_WRITE_PROTECTION_DISABLE 0x00
144 #define FLASH5_RS1_SRP1_ENABLE 0x01
145 #define FLASH5_RS1_SRP2_ENABLE 0x80
146 
152 #define FLASH5_RS2_PAGE_READ_MODE 0x08
153 #define FLASH5_RS2_PAGE_CONTINOUS_READ_MODE 0x00
154 #define FLASH5_RS2_ECC_ENABLED 0x10
155 #define FLASH5_RS2_ECC_DISABLED 0x10
156 #define FLASH5_RS2_OTP_LOCK 0x80
157 #define FLASH5_RS2_OTP_ACCESS 0x40
158 #define FLASH5_RS2_REG_STATUS_1_LOCK 0x20
159 
165 #define FLASH5_DEVICE_OK 0xFF
166 #define FLASH5_DEVICE_ERROR 0xAA
167 
173 #define FLASH5_PIN_STATUS_HIGH 1
174 #define FLASH5_PIN_STATUS_LOW 0
175  // End group macro
178 // --------------------------------------------------------------- PUBLIC TYPES
187 typedef struct
188 {
189  // Output pins
190 
191  digital_out_t cs;
192  digital_out_t wp;
193  digital_out_t hld;
194 
195  // Modules
196 
197  spi_master_t spi;
198  pin_name_t chip_select;
199 
200 } flash5_t;
201 
205 typedef struct
206 {
207  // Communication gpio pins
208 
209  pin_name_t miso;
210  pin_name_t mosi;
211  pin_name_t sck;
212  pin_name_t cs;
213 
214  // Additional gpio pins
215 
216  pin_name_t wp;
217  pin_name_t hld;
218 
219  // static variable
220 
221  uint32_t spi_speed;
222  spi_master_mode_t spi_mode;
223  spi_master_chip_select_polarity_t cs_polarity;
224 
225 } flash5_cfg_t;
226  // End types group
228 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
229 
234 #ifdef __cplusplus
235 extern "C"{
236 #endif
237 
247 
257 
269 void flash5_generic_transfer ( flash5_t *ctx, uint8_t *wr_buf,
270  uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
271 
280 void flash5_read_data ( flash5_t *ctx, uint8_t reg_addr, uint8_t *data_buf, uint16_t n_buf_size );
281 
290 void flash5_write_data ( flash5_t *ctx, uint8_t reg_addr, uint8_t *data_buf, uint16_t n_buf_size );
291 
298 void flash5_send_cmd ( flash5_t *ctx, uint8_t cmd_data );
299 
308 uint8_t flash5_set_wp_pin ( flash5_t *ctx, uint8_t status );
309 
318 uint8_t flash5_set_hold_pin ( flash5_t *ctx, uint8_t status );
319 
328 
335 void flash5_page_read ( flash5_t *ctx, uint16_t page_num );
336 
343 void flash5_execute_load ( flash5_t *ctx, uint16_t page_num );
344 
353 void flash5_page_read_memory ( flash5_t *ctx, uint16_t column_addr, uint8_t *data_buf, uint16_t buf_size );
354 
364 void flash5_continous_read_memory ( flash5_t *ctx, uint8_t *data_buf, uint16_t buf_size );
365 
374 void flash5_page_load_memory ( flash5_t *ctx, uint16_t column_addr, uint8_t *data_buf, uint16_t buf_size );
375 
385 uint8_t flash5_read_status_data ( flash5_t *ctx, uint8_t status_cmd, uint8_t status_addr );
386 
395 void flash5_write_status_data ( flash5_t *ctx, uint8_t status_cmd, uint8_t status_addr, uint8_t status_data );
396 
403 
410 void flash5_erase_page_data ( flash5_t *ctx, uint16_t page_addr );
411 
412 #ifdef __cplusplus
413 }
414 #endif
415 #endif // _FLASH5_H_
416  // End public_function group
419 
420 // ------------------------------------------------------------------------- END
flash5_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash5.h:223
flash5_set_hold_pin
uint8_t flash5_set_hold_pin(flash5_t *ctx, uint8_t status)
Sets hold pin to one state.
FLASH5_RETVAL
#define FLASH5_RETVAL
Definition: flash5.h:80
flash5_send_cmd
void flash5_send_cmd(flash5_t *ctx, uint8_t cmd_data)
Function for sending one Byte od data.
flash5_erase_page_data
void flash5_erase_page_data(flash5_t *ctx, uint16_t page_addr)
Function for eraseing one page.
flash5_cfg_t::sck
pin_name_t sck
Definition: flash5.h:211
flash5_cfg_t::miso
pin_name_t miso
Definition: flash5.h:209
flash5_init
FLASH5_RETVAL flash5_init(flash5_t *ctx, flash5_cfg_t *cfg)
Initialization function.
flash5_t::wp
digital_out_t wp
Definition: flash5.h:192
flash5_write_data
void flash5_write_data(flash5_t *ctx, uint8_t reg_addr, uint8_t *data_buf, uint16_t n_buf_size)
Generic funcion for writing.
flash5_read_data
void flash5_read_data(flash5_t *ctx, uint8_t reg_addr, uint8_t *data_buf, uint16_t n_buf_size)
Generic funcion for reading.
flash5_generic_transfer
void flash5_generic_transfer(flash5_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
flash5_t::hld
digital_out_t hld
Definition: flash5.h:193
flash5_page_read
void flash5_page_read(flash5_t *ctx, uint16_t page_num)
Function for setting page read.
flash5_cfg_t::wp
pin_name_t wp
Definition: flash5.h:216
flash5_t
Click ctx object definition.
Definition: flash5.h:188
flash5_cfg_t
Click configuration structure definition.
Definition: flash5.h:206
flash5_execute_load
void flash5_execute_load(flash5_t *ctx, uint16_t page_num)
Function for executing page load.
flash5_cfg_t::mosi
pin_name_t mosi
Definition: flash5.h:210
flash5_write_status_data
void flash5_write_status_data(flash5_t *ctx, uint8_t status_cmd, uint8_t status_addr, uint8_t status_data)
Function for writing status data.
flash5_cfg_t::hld
pin_name_t hld
Definition: flash5.h:217
flash5_software_reset
void flash5_software_reset(flash5_t *ctx)
Function for reseting device.
flash5_cfg_t::spi_speed
uint32_t spi_speed
Definition: flash5.h:221
flash5_page_read_memory
void flash5_page_read_memory(flash5_t *ctx, uint16_t column_addr, uint8_t *data_buf, uint16_t buf_size)
Function for reading one page.
flash5_t::chip_select
pin_name_t chip_select
Definition: flash5.h:198
flash5_read_status_data
uint8_t flash5_read_status_data(flash5_t *ctx, uint8_t status_cmd, uint8_t status_addr)
Function for reading status data.
flash5_device_id_check
uint8_t flash5_device_id_check(flash5_t *ctx)
Funcion for checking device id.
flash5_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash5.h:222
flash5_t::cs
digital_out_t cs
Definition: flash5.h:191
flash5_t::spi
spi_master_t spi
Definition: flash5.h:197
flash5_continous_read_memory
void flash5_continous_read_memory(flash5_t *ctx, uint8_t *data_buf, uint16_t buf_size)
Function for continous reading.
flash5_cfg_setup
void flash5_cfg_setup(flash5_cfg_t *cfg)
Config Object Initialization function.
flash5_cfg_t::cs
pin_name_t cs
Definition: flash5.h:212
flash5_page_load_memory
void flash5_page_load_memory(flash5_t *ctx, uint16_t column_addr, uint8_t *data_buf, uint16_t buf_size)
Function for loading one page.
flash5_set_wp_pin
uint8_t flash5_set_wp_pin(flash5_t *ctx, uint8_t status)
Sets wp pin to one state.