flash9  2.0.0.0
flash9.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef FLASH9_H
29 #define FLASH9_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_spi_master.h"
48 #include "spi_specifics.h"
49 
70 #define FLASH9_CMD_READ_STATUS_REGISTER_1 0x05
71 #define FLASH9_CMD_READ_STATUS_REGISTER_2 0x35
72 #define FLASH9_CMD_READ_STATUS_REGISTER_3 0x15
73 #define FLASH9_CMD_READ_SFDP_REGISTER 0x5A
74 #define FLASH9_CMD_ERASE_PROGRAM_SUSPEND 0x75
75 #define FLASH9_CMD_ERASE_PROGRAM_RESUME 0x7A
76 
81 #define FLASH9_CMD_WRITE_ENABLE 0x06
82 #define FLASH9_CMD_VOLATILE_SR_WRITE_ENABLE 0x50
83 #define FLASH9_CMD_WRITE_DISABLE 0x04
84 #define FLASH9_CMD_READ_UNIQUE_ID 0x4B
85 #define FLASH9_CMD_RELEASE_POWER_DOWN_ID 0xAB
86 #define FLASH9_CMD_MANUFACTURER_DEVICE_ID 0x90
87 #define FLASH9_CMD_JEDEC_ID 0x9F
88 #define FLASH9_CMD_CHIP_ERASE 0xC7
89 #define FLASH9_CMD_WRITE_STATUS_REGISTER_1 0x01
90 #define FLASH9_CMD_WRITE_STATUS_REGISTER_2 0x31
91 #define FLASH9_CMD_WRITE_STATUS_REGISTER_3 0x11
92 #define FLASH9_CMD_GLOBAL_BLOCK_LOCK 0x7E
93 #define FLASH9_CMD_GLOBAL_BLOCK_UNLOCK 0x98
94 #define FLASH9_CMD_POWER_DOWN 0xB9
95 #define FLASH9_CMD_ENTER_4BYTE_ADDRESS_MODE 0xB7
96 #define FLASH9_CMD_EXIT_4BYTE_ADDRESS_MODE 0xE9
97 #define FLASH9_CMD_ENABLE_RESET 0x66
98 #define FLASH9_CMD_RESET_DEVICE 0x99
99 #define FLASH9_CMD_SOFTWARE_DIE_SELECT 0xC2
100 
105 #define FLASH9_CMD_READ_DATA 0x03
106 #define FLASH9_CMD_READ_DATA_WITH_4BYTE_ADDRESS 0x13
107 #define FLASH9_CMD_FAST_READ 0x0B
108 #define FLASH9_CMD_FAST_READ_WITH_4BYTE_ADDRESS 0x0C
109 #define FLASH9_CMD_PAGE_PROGRAM 0x02
110 #define FLASH9_CMD_PAGE_PROGRAM_WITH_4BYTE_ADDRESS 0x12
111 #define FLASH9_CMD_SECTOR_ERASE 0x20
112 #define FLASH9_CMD_SECTOR_ERASE_WITH_4BYTE_ADDRESS 0x21
113 #define FLASH9_CMD_BLOCK_ERASE_32KB 0x52
114 #define FLASH9_CMD_BLOCK_ERASE_64KB 0xD8
115 #define FLASH9_CMD_BLOCK_ERASE_64KB_WITH_4BYTE_ADDRESS 0xDC
116 #define FLASH9_CMD_ERASE_SECURITY_REGISTER 0x44
117 #define FLASH9_CMD_PROGRAM_SECURITY_REGISTER 0x42
118 #define FLASH9_CMD_READ_SECURITY_REGISTER 0x48
119 #define FLASH9_CMD_READ_BLOCK_LOCK 0x3D
120 #define FLASH9_CMD_INDIVIDUAL_BLOCK_LOCK 0x36
121 #define FLASH9_CMD_INDIVIDUAL_BLOCK_UNLOCK 0x39
122  // flash9_reg
124 
139 #define FLASH9_JEDEC_MANUFACTURER_ID 0xEF
140 #define FLASH9_JEDEC_MEMORY_TYPE 0x70
141 #define FLASH9_JEDEC_CAPACITY 0x22
142 
147 #define FLASH9_MIN_ADDRESS 0x00000000
148 #define FLASH9_MAX_ADDRESS 0x0FFFFFFFul
149 
158 #define FLASH9_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
159 #define FLASH9_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
160  // flash9_set
162 
177 #define FLASH9_MAP_MIKROBUS( cfg, mikrobus ) \
178  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
179  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
180  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
181  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
182  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_AN ); \
183  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
184  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_PWM )
185  // flash9_map // flash9
188 
193 typedef struct
194 {
195  // Output pins
196  digital_out_t wp;
197  digital_out_t rst;
198  digital_out_t hld;
200  // Modules
201  spi_master_t spi;
203  pin_name_t chip_select;
205 } flash9_t;
206 
211 typedef struct
212 {
213  // Communication gpio pins
214  pin_name_t miso;
215  pin_name_t mosi;
216  pin_name_t sck;
217  pin_name_t cs;
219  // Additional gpio pins
220  pin_name_t wp;
221  pin_name_t rst;
222  pin_name_t hld;
224  // static variable
225  uint32_t spi_speed;
226  spi_master_mode_t spi_mode;
227  spi_master_chip_select_polarity_t cs_polarity;
229 } flash9_cfg_t;
230 
235 typedef enum
236 {
238  FLASH9_ERROR = -1
239 
241 
258 
273 err_t flash9_init ( flash9_t *ctx, flash9_cfg_t *cfg );
274 
289 
302 err_t flash9_generic_write ( flash9_t *ctx, uint8_t *data_in, uint16_t in_len );
303 
319 err_t flash9_generic_read ( flash9_t *ctx, uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint16_t out_len ) ;
320 
335 err_t flash9_write_register ( flash9_t *ctx, uint8_t reg, uint8_t *data_in, uint16_t in_len );
336 
351 err_t flash9_read_register ( flash9_t *ctx, uint8_t reg, uint8_t *data_out, uint16_t out_len );
352 
365 err_t flash9_write_command ( flash9_t *ctx, uint8_t cmd );
366 
376 void flash9_set_wp_pin ( flash9_t *ctx, uint8_t state );
377 
387 void flash9_set_hld_pin ( flash9_t *ctx, uint8_t state );
388 
398 void flash9_set_rst_pin ( flash9_t *ctx, uint8_t state );
399 
412 
426 err_t flash9_erase_memory ( flash9_t *ctx, uint8_t erase_cmd, uint32_t address );
427 
441 err_t flash9_read_status ( flash9_t *ctx, uint8_t status_cmd, uint8_t *status );
442 
456 err_t flash9_write_status ( flash9_t *ctx, uint8_t status_cmd, uint8_t status );
457 
473 err_t flash9_memory_write ( flash9_t *ctx, uint32_t address, uint8_t *data_in, uint16_t len );
474 
490 err_t flash9_memory_read ( flash9_t *ctx, uint32_t address, uint8_t *data_out, uint16_t len );
491 
507 err_t flash9_memory_read_fast ( flash9_t *ctx, uint32_t address, uint8_t *data_out, uint16_t len );
508 
509 #ifdef __cplusplus
510 }
511 #endif
512 #endif // FLASH9_H
513  // flash9
515 
516 // ------------------------------------------------------------------------ END
FLASH9_ERROR
@ FLASH9_ERROR
Definition: flash9.h:238
flash9_cfg_t::sck
pin_name_t sck
Definition: flash9.h:216
flash9_memory_write
err_t flash9_memory_write(flash9_t *ctx, uint32_t address, uint8_t *data_in, uint16_t len)
Flash 9 memory write function.
flash9_cfg_t::rst
pin_name_t rst
Definition: flash9.h:221
spi_specifics.h
This file contains SPI specific macros, functions, etc.
flash9_t::rst
digital_out_t rst
Definition: flash9.h:197
flash9_check_communication
err_t flash9_check_communication(flash9_t *ctx)
Flash 9 check communication function.
flash9_cfg_t::wp
pin_name_t wp
Definition: flash9.h:220
flash9_generic_read
err_t flash9_generic_read(flash9_t *ctx, uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint16_t out_len)
Flash 9 generic read function.
flash9_cfg_setup
void flash9_cfg_setup(flash9_cfg_t *cfg)
Flash 9 configuration object setup function.
flash9_cfg_t::miso
pin_name_t miso
Definition: flash9.h:214
flash9_default_cfg
err_t flash9_default_cfg(flash9_t *ctx)
Flash 9 default configuration function.
flash9_set_rst_pin
void flash9_set_rst_pin(flash9_t *ctx, uint8_t state)
Flash 9 set rst pin function.
flash9_cfg_t::mosi
pin_name_t mosi
Definition: flash9.h:215
flash9_write_status
err_t flash9_write_status(flash9_t *ctx, uint8_t status_cmd, uint8_t status)
Flash 9 write status function.
flash9_erase_memory
err_t flash9_erase_memory(flash9_t *ctx, uint8_t erase_cmd, uint32_t address)
Flash 9 erase memory function.
flash9_cfg_t::spi_speed
uint32_t spi_speed
Definition: flash9.h:225
flash9_cfg_t
Flash 9 Click configuration object.
Definition: flash9.h:212
flash9_generic_write
err_t flash9_generic_write(flash9_t *ctx, uint8_t *data_in, uint16_t in_len)
Flash 9 generic write function.
flash9_read_register
err_t flash9_read_register(flash9_t *ctx, uint8_t reg, uint8_t *data_out, uint16_t out_len)
Flash 9 read register function.
flash9_t
Flash 9 Click context object.
Definition: flash9.h:194
flash9_write_command
err_t flash9_write_command(flash9_t *ctx, uint8_t cmd)
Flash 9 write command function.
flash9_set_wp_pin
void flash9_set_wp_pin(flash9_t *ctx, uint8_t state)
Flash 9 set wp pin function.
flash9_t::spi
spi_master_t spi
Definition: flash9.h:201
flash9_memory_read_fast
err_t flash9_memory_read_fast(flash9_t *ctx, uint32_t address, uint8_t *data_out, uint16_t len)
Flash 9 memory read fast function.
flash9_write_register
err_t flash9_write_register(flash9_t *ctx, uint8_t reg, uint8_t *data_in, uint16_t in_len)
Flash 9 write register function.
flash9_read_status
err_t flash9_read_status(flash9_t *ctx, uint8_t status_cmd, uint8_t *status)
Flash 9 read status function.
flash9_init
err_t flash9_init(flash9_t *ctx, flash9_cfg_t *cfg)
Flash 9 initialization function.
flash9_cfg_t::hld
pin_name_t hld
Definition: flash9.h:222
flash9_cfg_t::cs
pin_name_t cs
Definition: flash9.h:217
FLASH9_OK
@ FLASH9_OK
Definition: flash9.h:237
flash9_t::wp
digital_out_t wp
Definition: flash9.h:196
flash9_t::chip_select
pin_name_t chip_select
Definition: flash9.h:203
flash9_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash9.h:227
flash9_set_hld_pin
void flash9_set_hld_pin(flash9_t *ctx, uint8_t state)
Flash 9 set hld pin function.
flash9_t::hld
digital_out_t hld
Definition: flash9.h:198
flash9_return_value_t
flash9_return_value_t
Flash 9 Click return value data.
Definition: flash9.h:236
flash9_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash9.h:226
flash9_memory_read
err_t flash9_memory_read(flash9_t *ctx, uint32_t address, uint8_t *data_out, uint16_t len)
Flash 9 memory read function.