flash  2.0.0.0
flash.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 _FLASH_H_
36 #define _FLASH_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_spi_master.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
61 #define FLASH_MEM_ADDR_FIRST_PAGE_START 0x0
62 #define FLASH_MEM_ADDR_FIRST_PAGE_END 0xFF
63 #define FLASH_MEM_ADDR_SECOND_PAGE_START 0x100
64 #define FLASH_MEM_ADDR_SECOND_PAGE_END 0x1FF
65 #define FLASH_MEM_ADDR_LAST_PAGE_START 0xFFF00
66 #define FLASH_MEM_ADDR_LAST_PAGE_END 0xFFFFF
67 #define FLASH_MEM_ADDR_FIRST_SECTOR_START 0x0
68 #define FLASH_MEM_ADDR_FIRST_SECTOR_END 0xFFF
69 #define FLASH_MEM_ADDR_LAST_SECTOR_START 0xFF000
70 #define FLASH_MEM_ADDR_LAST_SECTOR_END 0xFFFFF
71 #define FLASH_MEM_ADDR_FIRST_BLOCK_START 0x0
72 #define FLASH_MEM_ADDR_FIRST_BLOCK_END 0xFFFF
73 #define FLASH_MEM_ADDR_LAST_BLOCK_START 0xF0000
74 #define FLASH_MEM_ADDR_LAST_BLOCK_END 0xFFFFF
75 
81 #define FLASH_NDATA_TRANSFER_MIN 1
82 #define FLASH_NDATA_TRANSFER_MAX 256
83 
94 #define FLASH_STATUS_MASK_WRITE_BUSY 0x1
95 #define FLASH_STATUS_MASK_WRITE_EN 0x2
96 #define FLASH_STATUS_MASK_BLOCK_PROTECT 0x3C
97 #define FLASH_STATUS_MASK_WP_DIS 0x40
98 #define FLASH_STATUS_MASK_OTP_LOCK 0x80
99 
105 #define FLASH_STATUS_SET_BLOCK_PROT_BIT0 0x4
106 #define FLASH_STATUS_SET_BLOCK_PROT_BIT1 0x8
107 #define FLASH_STATUS_SET_BLOCK_PROT_BIT2 0x10
108 #define FLASH_STATUS_SET_BLOCK_PROT_BIT3 0x20
109 #define FLASH_STATUS_SET_WP_DIS 0x40
110 #define FLASH_STATUS_SET_OTP_LOCK 0x80
111  // End status group
114 
119 #define FLASH_MAP_MIKROBUS( cfg, mikrobus ) \
120  cfg.sdo = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
121  cfg.sdi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
122  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
123  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
124  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_RST ); \
125  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_PWM )
126  // End macros group
129 // --------------------------------------------------------------- PUBLIC TYPES
138 typedef enum
139 {
140  FLASH_OK = 0x0,
144 
146 
150 typedef enum
151 {
154 
156 
160 typedef struct
161 {
162  // Output pins
163  digital_out_t cs;
164 
165  // Control pins.
166  digital_out_t hld;
167  digital_out_t wp;
168 
169  // Module.
170  spi_master_t spi;
171  pin_name_t chip_select;
172 
173 } flash_t;
174 
178 typedef struct
179 {
180  // Communication gpio pins.
181  pin_name_t sdo;
182  pin_name_t sdi;
183  pin_name_t sck;
184  pin_name_t cs;
185 
186  // Additional gpio pins.
187  pin_name_t hld;
188  pin_name_t wp;
189 
190  // SPI configuration.
191  uint32_t spi_speed;
192  spi_master_mode_t spi_mode;
193  spi_master_chip_select_polarity_t cs_polarity;
194 
195 } flash_cfg_t;
196  // End types group
198 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
204 #ifdef __cplusplus
205 extern "C"{
206 #endif
207 
218 
232 
241 
251 void flash_reset( flash_t *ctx );
252 
262 
273 
284 void flash_write_status( flash_t *ctx, uint8_t data_tx );
285 
295 uint8_t flash_read_status( flash_t *ctx );
296 
307 void flash_read_id( flash_t *ctx, uint8_t *manufact_id, uint16_t *dev_id );
308 
324 flash_err_t flash_write_byte( flash_t *ctx, uint32_t mem_addr, uint8_t data_tx );
325 
339 flash_err_t flash_read_byte( flash_t *ctx, uint32_t mem_addr, uint8_t *data_rx );
340 
358 flash_err_t flash_write_page( flash_t *ctx, uint32_t mem_addr, uint8_t *data_tx,
359  uint16_t n_data );
360 
376 flash_err_t flash_read_page( flash_t *ctx, uint32_t mem_addr, uint8_t *data_rx,
377  uint16_t n_data );
378 
389 
403 flash_err_t flash_erase_block( flash_t *ctx, uint32_t mem_addr );
404 
419 flash_err_t flash_erase_half_block( flash_t *ctx, uint32_t mem_addr );
420 
434 flash_err_t flash_erase_sector( flash_t *ctx, uint32_t mem_addr );
435 
445 
455 
456 #ifdef __cplusplus
457 }
458 #endif
459 #endif // _FLASH_H_
460  // End public_function group
463 
464 // ------------------------------------------------------------------------ END
flash_write_status
void flash_write_status(flash_t *ctx, uint8_t data_tx)
Status Write function.
flash_read_id
void flash_read_id(flash_t *ctx, uint8_t *manufact_id, uint16_t *dev_id)
ID Read function.
flash_init
flash_err_t flash_init(flash_t *ctx, flash_cfg_t *cfg)
Click Initialization function.
FLASH_ERR_MEM_ADDR
@ FLASH_ERR_MEM_ADDR
Definition: flash.h:142
flash_t
Click context object definition.
Definition: flash.h:161
flash_cfg_t::spi_speed
uint32_t spi_speed
Definition: flash.h:191
flash_t::spi
spi_master_t spi
Definition: flash.h:170
FLASH_PIN_STATE_HIGH
@ FLASH_PIN_STATE_HIGH
Definition: flash.h:153
flash_write_disable
void flash_write_disable(flash_t *ctx)
Write Disable function.
flash_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: flash.h:192
flash_pin_state_t
flash_pin_state_t
Click pin states.
Definition: flash.h:151
flash_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: flash.h:193
flash_write_enable
void flash_write_enable(flash_t *ctx)
Write Enable function.
flash_read_byte
flash_err_t flash_read_byte(flash_t *ctx, uint32_t mem_addr, uint8_t *data_rx)
Byte Read function.
flash_cfg_t::sdo
pin_name_t sdo
Definition: flash.h:181
flash_read_page
flash_err_t flash_read_page(flash_t *ctx, uint32_t mem_addr, uint8_t *data_rx, uint16_t n_data)
Page Read function.
flash_cfg_setup
void flash_cfg_setup(flash_cfg_t *cfg)
Config Object Setup function.
flash_err_t
flash_err_t
Click error code definition.
Definition: flash.h:139
flash_erase_sector
flash_err_t flash_erase_sector(flash_t *ctx, uint32_t mem_addr)
Sector Erase function.
FLASH_PIN_STATE_LOW
@ FLASH_PIN_STATE_LOW
Definition: flash.h:152
FLASH_OK
@ FLASH_OK
Definition: flash.h:140
flash_erase_half_block
flash_err_t flash_erase_half_block(flash_t *ctx, uint32_t mem_addr)
Half Block Erase function.
flash_write_byte
flash_err_t flash_write_byte(flash_t *ctx, uint32_t mem_addr, uint8_t data_tx)
Byte Write function.
flash_set_hold_pin
void flash_set_hold_pin(flash_t *ctx, flash_pin_state_t state)
HLD Pin Set function.
flash_reset
void flash_reset(flash_t *ctx)
Reset function.
FLASH_ERR_NDATA
@ FLASH_ERR_NDATA
Definition: flash.h:143
flash_write_page
flash_err_t flash_write_page(flash_t *ctx, uint32_t mem_addr, uint8_t *data_tx, uint16_t n_data)
Page Write function.
FLASH_ERR_INIT_DRV
@ FLASH_ERR_INIT_DRV
Definition: flash.h:141
flash_cfg_t::cs
pin_name_t cs
Definition: flash.h:184
flash_erase_chip
void flash_erase_chip(flash_t *ctx)
Chip Erase function.
flash_cfg_t
Click configuration structure definition.
Definition: flash.h:179
flash_t::chip_select
pin_name_t chip_select
Definition: flash.h:171
flash_cfg_t::sck
pin_name_t sck
Definition: flash.h:183
flash_t::hld
digital_out_t hld
Definition: flash.h:166
flash_cfg_t::sdi
pin_name_t sdi
Definition: flash.h:182
flash_erase_block
flash_err_t flash_erase_block(flash_t *ctx, uint32_t mem_addr)
Block Erase function.
flash_t::cs
digital_out_t cs
Definition: flash.h:163
flash_cfg_t::wp
pin_name_t wp
Definition: flash.h:188
flash_default_cfg
void flash_default_cfg(flash_t *ctx)
Click Default Configuration function.
flash_cfg_t::hld
pin_name_t hld
Definition: flash.h:187
flash_t::wp
digital_out_t wp
Definition: flash.h:167
flash_set_write_protect_pin
void flash_set_write_protect_pin(flash_t *ctx, flash_pin_state_t state)
WP Pin Set function.
flash_read_status
uint8_t flash_read_status(flash_t *ctx)
Status Read function.