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 "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 #include "spi_specifics.h"
39 
60 #define FLASH9_CMD_READ_STATUS_REGISTER_1 0x05
61 #define FLASH9_CMD_READ_STATUS_REGISTER_2 0x35
62 #define FLASH9_CMD_READ_STATUS_REGISTER_3 0x15
63 #define FLASH9_CMD_READ_SFDP_REGISTER 0x5A
64 #define FLASH9_CMD_ERASE_PROGRAM_SUSPEND 0x75
65 #define FLASH9_CMD_ERASE_PROGRAM_RESUME 0x7A
66 
71 #define FLASH9_CMD_WRITE_ENABLE 0x06
72 #define FLASH9_CMD_VOLATILE_SR_WRITE_ENABLE 0x50
73 #define FLASH9_CMD_WRITE_DISABLE 0x04
74 #define FLASH9_CMD_READ_UNIQUE_ID 0x4B
75 #define FLASH9_CMD_RELEASE_POWER_DOWN_ID 0xAB
76 #define FLASH9_CMD_MANUFACTURER_DEVICE_ID 0x90
77 #define FLASH9_CMD_JEDEC_ID 0x9F
78 #define FLASH9_CMD_CHIP_ERASE 0xC7
79 #define FLASH9_CMD_WRITE_STATUS_REGISTER_1 0x01
80 #define FLASH9_CMD_WRITE_STATUS_REGISTER_2 0x31
81 #define FLASH9_CMD_WRITE_STATUS_REGISTER_3 0x11
82 #define FLASH9_CMD_GLOBAL_BLOCK_LOCK 0x7E
83 #define FLASH9_CMD_GLOBAL_BLOCK_UNLOCK 0x98
84 #define FLASH9_CMD_POWER_DOWN 0xB9
85 #define FLASH9_CMD_ENTER_4BYTE_ADDRESS_MODE 0xB7
86 #define FLASH9_CMD_EXIT_4BYTE_ADDRESS_MODE 0xE9
87 #define FLASH9_CMD_ENABLE_RESET 0x66
88 #define FLASH9_CMD_RESET_DEVICE 0x99
89 #define FLASH9_CMD_SOFTWARE_DIE_SELECT 0xC2
90 
95 #define FLASH9_CMD_READ_DATA 0x03
96 #define FLASH9_CMD_READ_DATA_WITH_4BYTE_ADDRESS 0x13
97 #define FLASH9_CMD_FAST_READ 0x0B
98 #define FLASH9_CMD_FAST_READ_WITH_4BYTE_ADDRESS 0x0C
99 #define FLASH9_CMD_PAGE_PROGRAM 0x02
100 #define FLASH9_CMD_PAGE_PROGRAM_WITH_4BYTE_ADDRESS 0x12
101 #define FLASH9_CMD_SECTOR_ERASE 0x20
102 #define FLASH9_CMD_SECTOR_ERASE_WITH_4BYTE_ADDRESS 0x21
103 #define FLASH9_CMD_BLOCK_ERASE_32KB 0x52
104 #define FLASH9_CMD_BLOCK_ERASE_64KB 0xD8
105 #define FLASH9_CMD_BLOCK_ERASE_64KB_WITH_4BYTE_ADDRESS 0xDC
106 #define FLASH9_CMD_ERASE_SECURITY_REGISTER 0x44
107 #define FLASH9_CMD_PROGRAM_SECURITY_REGISTER 0x42
108 #define FLASH9_CMD_READ_SECURITY_REGISTER 0x48
109 #define FLASH9_CMD_READ_BLOCK_LOCK 0x3D
110 #define FLASH9_CMD_INDIVIDUAL_BLOCK_LOCK 0x36
111 #define FLASH9_CMD_INDIVIDUAL_BLOCK_UNLOCK 0x39
112  // flash9_reg
114 
129 #define FLASH9_JEDEC_MANUFACTURER_ID 0xEF
130 #define FLASH9_JEDEC_MEMORY_TYPE 0x70
131 #define FLASH9_JEDEC_CAPACITY 0x22
132 
137 #define FLASH9_MIN_ADDRESS 0x00000000
138 #define FLASH9_MAX_ADDRESS 0x0FFFFFFFul
139 
148 #define FLASH9_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
149 #define FLASH9_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
150  // flash9_set
152 
167 #define FLASH9_MAP_MIKROBUS( cfg, mikrobus ) \
168  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
169  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
170  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
171  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
172  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_AN ); \
173  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
174  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_PWM )
175  // flash9_map // flash9
178 
183 typedef struct
184 {
185  // Output pins
186  digital_out_t wp;
187  digital_out_t rst;
188  digital_out_t hld;
190  // Modules
191  spi_master_t spi;
193  pin_name_t chip_select;
195 } flash9_t;
196 
201 typedef struct
202 {
203  // Communication gpio pins
204  pin_name_t miso;
205  pin_name_t mosi;
206  pin_name_t sck;
207  pin_name_t cs;
209  // Additional gpio pins
210  pin_name_t wp;
211  pin_name_t rst;
212  pin_name_t hld;
214  // static variable
215  uint32_t spi_speed;
216  spi_master_mode_t spi_mode;
217  spi_master_chip_select_polarity_t cs_polarity;
219 } flash9_cfg_t;
220 
225 typedef enum
226 {
228  FLASH9_ERROR = -1
229 
231 
248 
263 err_t flash9_init ( flash9_t *ctx, flash9_cfg_t *cfg );
264 
279 
292 err_t flash9_generic_write ( flash9_t *ctx, uint8_t *data_in, uint16_t in_len );
293 
309 err_t flash9_generic_read ( flash9_t *ctx, uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint16_t out_len ) ;
310 
325 err_t flash9_write_register ( flash9_t *ctx, uint8_t reg, uint8_t *data_in, uint16_t in_len );
326 
341 err_t flash9_read_register ( flash9_t *ctx, uint8_t reg, uint8_t *data_out, uint16_t out_len );
342 
355 err_t flash9_write_command ( flash9_t *ctx, uint8_t cmd );
356 
366 void flash9_set_wp_pin ( flash9_t *ctx, uint8_t state );
367 
377 void flash9_set_hld_pin ( flash9_t *ctx, uint8_t state );
378 
388 void flash9_set_rst_pin ( flash9_t *ctx, uint8_t state );
389 
402 
416 err_t flash9_erase_memory ( flash9_t *ctx, uint8_t erase_cmd, uint32_t address );
417 
431 err_t flash9_read_status ( flash9_t *ctx, uint8_t status_cmd, uint8_t *status );
432 
446 err_t flash9_write_status ( flash9_t *ctx, uint8_t status_cmd, uint8_t status );
447 
463 err_t flash9_memory_write ( flash9_t *ctx, uint32_t address, uint8_t *data_in, uint16_t len );
464 
480 err_t flash9_memory_read ( flash9_t *ctx, uint32_t address, uint8_t *data_out, uint16_t len );
481 
497 err_t flash9_memory_read_fast ( flash9_t *ctx, uint32_t address, uint8_t *data_out, uint16_t len );
498 
499 #ifdef __cplusplus
500 }
501 #endif
502 #endif // FLASH9_H
503  // flash9
505 
506 // ------------------------------------------------------------------------ END
FLASH9_ERROR
@ FLASH9_ERROR
Definition: flash9.h:228
flash9_cfg_t::sck
pin_name_t sck
Definition: flash9.h:206
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:211
spi_specifics.h
This file contains SPI specific macros, functions, etc.
flash9_t::rst
digital_out_t rst
Definition: flash9.h:187
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:210
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:204
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:205
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:215
flash9_cfg_t
Flash 9 Click configuration object.
Definition: flash9.h:202
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:184
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:191
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:212
flash9_cfg_t::cs
pin_name_t cs
Definition: flash9.h:207
FLASH9_OK
@ FLASH9_OK
Definition: flash9.h:227
flash9_t::wp
digital_out_t wp
Definition: flash9.h:186
flash9_t::chip_select
pin_name_t chip_select
Definition: flash9.h:193
flash9_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash9.h:217
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:188
flash9_return_value_t
flash9_return_value_t
Flash 9 Click return value data.
Definition: flash9.h:226
flash9_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash9.h:216
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.