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 
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 FLASH5_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.wp = MIKROBUS( mikrobus, MIKROBUS_RST ); \
69  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_PWM );
70 
76 #define FLASH5_RETVAL uint8_t
77 
78 #define FLASH5_OK 0x00
79 #define FLASH5_INIT_ERROR 0xFF
80 
86 #define FLASH5_MAX_addr 0xFFFF
87 #define FLASH5_PAGESIZE 2048
88 
94 #define FLASH5_CMD_RESET 0xFF
95 #define FLASH5_CMD_DEVICE_ID 0x9F
96 #define FLASH5_CMD_READ_REG_STATUS1 0x05
97 #define FLASH5_CMD_READ_REG_STATUS2 0x0F
98 #define FLASH5_CMD_WRITE_REG_STATUS1 0x01
99 #define FLASH5_CMD_WRITE_REG_STATUS2 0x1F
100 #define FLASH5_CMD_WRITE_ENABLE 0x06
101 #define FLASH5_CMD_WRITE_DISABLE 0x04
102 #define FLASH5_CMD_BAD_BLOCK_MANAGE 0xA1
103 #define FLASH5_CMD_READ_BBM_TABLE 0xA5
104 #define FLASH5_CMD_LAST_ECC_FAIL_PAGE 0xA9
105 #define FLASH5_CMD_BLOCK_ERASE 0xD8
106 #define FLASH5_CMD_LOAD_DATA 0x02
107 #define FLASH5_CMD__RND_LOAD_DATA 0x84
108 #define FLASH5_CMD_QUAD_LOAD_DATA 0x32
109 #define FLASH5_CMD_QUAD_RND_LOAD_DATA 0x34
110 #define FLASH5_CMD_EXECUTE 0x10
111 #define FLASH5_CMD_PAGE_READ_DATA 0x13
112 #define FLASH5_CMD_READ_DATA 0x03
113 #define FLASH5_CMD_FAST_READ 0x0B
114 #define FLASH5_CMD_FAST_READ_4BYTE_addr 0x0C
115 #define FLASH5_CMD_FAST_READ_DUAL_OUT 0x3B
116 #define FLASH5_CMD_FAST_READ_DUAL_OUT_4BYTE_addr 0x3C
117 #define FLASH5_CMD_FAST_READ_QUAD_OUT 0x6B
118 #define FLASH5_CMD_FAST_READ_QUAD_OUT_4BYTE_addr 0x6C
119 #define FLASH5_CMD_FAST_READ_DUAL_IO 0xBB
120 #define FLASH5_CMD_FAST_READ_IO_4BYTE_addr 0xBC
121 #define FLASH5_CMD_FAST_READ_QUAD_IO 0xEB
122 #define FLASH5_CMD_FAST_READ_QUAD_IO_4BYTE_addr 0xEC
123 
129 #define FLASH5_REG_STATUS_1 0xA0
130 #define FLASH5_REG_STATUS_2 0xB0
131 #define FLASH5_REG_STATUS_3 0xC0
132 
138 #define FLASH5_RS1_WRITE_PROTECTION_ENABLE 0x02
139 #define FLASH5_RS1_WRITE_PROTECTION_DISABLE 0x00
140 #define FLASH5_RS1_SRP1_ENABLE 0x01
141 #define FLASH5_RS1_SRP2_ENABLE 0x80
142 
148 #define FLASH5_RS2_PAGE_READ_MODE 0x08
149 #define FLASH5_RS2_PAGE_CONTINOUS_READ_MODE 0x00
150 #define FLASH5_RS2_ECC_ENABLED 0x10
151 #define FLASH5_RS2_ECC_DISABLED 0x10
152 #define FLASH5_RS2_OTP_LOCK 0x80
153 #define FLASH5_RS2_OTP_ACCESS 0x40
154 #define FLASH5_RS2_REG_STATUS_1_LOCK 0x20
155 
161 #define FLASH5_DEVICE_OK 0xFF
162 #define FLASH5_DEVICE_ERROR 0xAA
163 
169 #define FLASH5_PIN_STATUS_HIGH 1
170 #define FLASH5_PIN_STATUS_LOW 0
171  // End group macro
174 // --------------------------------------------------------------- PUBLIC TYPES
183 typedef struct
184 {
185  // Output pins
186 
187  digital_out_t cs;
188  digital_out_t wp;
189  digital_out_t hld;
190 
191  // Modules
192 
193  spi_master_t spi;
194  pin_name_t chip_select;
195 
196 } flash5_t;
197 
201 typedef struct
202 {
203  // Communication gpio pins
204 
205  pin_name_t miso;
206  pin_name_t mosi;
207  pin_name_t sck;
208  pin_name_t cs;
209 
210  // Additional gpio pins
211 
212  pin_name_t wp;
213  pin_name_t hld;
214 
215  // static variable
216 
217  uint32_t spi_speed;
218  spi_master_mode_t spi_mode;
219  spi_master_chip_select_polarity_t cs_polarity;
220 
221 } flash5_cfg_t;
222  // End types group
224 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
225 
230 #ifdef __cplusplus
231 extern "C"{
232 #endif
233 
243 
253 
265 void flash5_generic_transfer ( flash5_t *ctx, uint8_t *wr_buf,
266  uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
267 
276 void flash5_read_data ( flash5_t *ctx, uint8_t reg_addr, uint8_t *data_buf, uint16_t n_buf_size );
277 
286 void flash5_write_data ( flash5_t *ctx, uint8_t reg_addr, uint8_t *data_buf, uint16_t n_buf_size );
287 
294 void flash5_send_cmd ( flash5_t *ctx, uint8_t cmd_data );
295 
304 uint8_t flash5_set_wp_pin ( flash5_t *ctx, uint8_t status );
305 
314 uint8_t flash5_set_hold_pin ( flash5_t *ctx, uint8_t status );
315 
324 
331 void flash5_page_read ( flash5_t *ctx, uint16_t page_num );
332 
339 void flash5_execute_load ( flash5_t *ctx, uint16_t page_num );
340 
349 void flash5_page_read_memory ( flash5_t *ctx, uint16_t column_addr, uint8_t *data_buf, uint16_t buf_size );
350 
360 void flash5_continous_read_memory ( flash5_t *ctx, uint8_t *data_buf, uint16_t buf_size );
361 
370 void flash5_page_load_memory ( flash5_t *ctx, uint16_t column_addr, uint8_t *data_buf, uint16_t buf_size );
371 
381 uint8_t flash5_read_status_data ( flash5_t *ctx, uint8_t status_cmd, uint8_t status_addr );
382 
391 void flash5_write_status_data ( flash5_t *ctx, uint8_t status_cmd, uint8_t status_addr, uint8_t status_data );
392 
399 
406 void flash5_erase_page_data ( flash5_t *ctx, uint16_t page_addr );
407 
408 #ifdef __cplusplus
409 }
410 #endif
411 #endif // _FLASH5_H_
412  // End public_function group
415 
416 // ------------------------------------------------------------------------- END
flash5_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash5.h:219
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:76
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:207
flash5_cfg_t::miso
pin_name_t miso
Definition: flash5.h:205
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:188
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:189
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:212
flash5_t
Click ctx object definition.
Definition: flash5.h:184
flash5_cfg_t
Click configuration structure definition.
Definition: flash5.h:202
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:206
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:213
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:217
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:194
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:218
flash5_t::cs
digital_out_t cs
Definition: flash5.h:187
flash5_t::spi
spi_master_t spi
Definition: flash5.h:193
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:208
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.