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 "drv_digital_out.h"
39#include "drv_spi_master.h"
40
41// -------------------------------------------------------------- PUBLIC MACROS
51#define FLASH_MEM_ADDR_FIRST_PAGE_START 0x0
52#define FLASH_MEM_ADDR_FIRST_PAGE_END 0xFF
53#define FLASH_MEM_ADDR_SECOND_PAGE_START 0x100
54#define FLASH_MEM_ADDR_SECOND_PAGE_END 0x1FF
55#define FLASH_MEM_ADDR_LAST_PAGE_START 0xFFF00
56#define FLASH_MEM_ADDR_LAST_PAGE_END 0xFFFFF
57#define FLASH_MEM_ADDR_FIRST_SECTOR_START 0x0
58#define FLASH_MEM_ADDR_FIRST_SECTOR_END 0xFFF
59#define FLASH_MEM_ADDR_LAST_SECTOR_START 0xFF000
60#define FLASH_MEM_ADDR_LAST_SECTOR_END 0xFFFFF
61#define FLASH_MEM_ADDR_FIRST_BLOCK_START 0x0
62#define FLASH_MEM_ADDR_FIRST_BLOCK_END 0xFFFF
63#define FLASH_MEM_ADDR_LAST_BLOCK_START 0xF0000
64#define FLASH_MEM_ADDR_LAST_BLOCK_END 0xFFFFF
71#define FLASH_NDATA_TRANSFER_MIN 1
72#define FLASH_NDATA_TRANSFER_MAX 256
84#define FLASH_STATUS_MASK_WRITE_BUSY 0x1
85#define FLASH_STATUS_MASK_WRITE_EN 0x2
86#define FLASH_STATUS_MASK_BLOCK_PROTECT 0x3C
87#define FLASH_STATUS_MASK_WP_DIS 0x40
88#define FLASH_STATUS_MASK_OTP_LOCK 0x80
95#define FLASH_STATUS_SET_BLOCK_PROT_BIT0 0x4
96#define FLASH_STATUS_SET_BLOCK_PROT_BIT1 0x8
97#define FLASH_STATUS_SET_BLOCK_PROT_BIT2 0x10
98#define FLASH_STATUS_SET_BLOCK_PROT_BIT3 0x20
99#define FLASH_STATUS_SET_WP_DIS 0x40
100#define FLASH_STATUS_SET_OTP_LOCK 0x80 // End status group
104
109#define FLASH_MAP_MIKROBUS( cfg, mikrobus ) \
110 cfg.sdo = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
111 cfg.sdi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
112 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
113 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
114 cfg.hld = MIKROBUS( mikrobus, MIKROBUS_RST ); \
115 cfg.wp = MIKROBUS( mikrobus, MIKROBUS_PWM ) // End macros group
119// --------------------------------------------------------------- PUBLIC TYPES
128typedef enum
129{
130 FLASH_OK = 0x0,
134
136
140typedef enum
141{
144
146
150typedef struct
151{
152 // Output pins
153 digital_out_t cs;
154
155 // Control pins.
156 digital_out_t hld;
157 digital_out_t wp;
158
159 // Module.
160 spi_master_t spi;
161 pin_name_t chip_select;
162
163} flash_t;
164
168typedef struct
169{
170 // Communication gpio pins.
171 pin_name_t sdo;
172 pin_name_t sdi;
173 pin_name_t sck;
174 pin_name_t cs;
175
176 // Additional gpio pins.
177 pin_name_t hld;
178 pin_name_t wp;
179
180 // SPI configuration.
181 uint32_t spi_speed;
182 spi_master_mode_t spi_mode;
183 spi_master_chip_select_polarity_t cs_polarity;
184
186 // End types group
188// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
194#ifdef __cplusplus
195extern "C"{
196#endif
197
208
222
231
241void flash_reset( flash_t *ctx );
242
252
263
274void flash_write_status( flash_t *ctx, uint8_t data_tx );
275
286
297void flash_read_id( flash_t *ctx, uint8_t *manufact_id, uint16_t *dev_id );
298
314flash_err_t flash_write_byte( flash_t *ctx, uint32_t mem_addr, uint8_t data_tx );
315
329flash_err_t flash_read_byte( flash_t *ctx, uint32_t mem_addr, uint8_t *data_rx );
330
348flash_err_t flash_write_page( flash_t *ctx, uint32_t mem_addr, uint8_t *data_tx,
349 uint16_t n_data );
350
366flash_err_t flash_read_page( flash_t *ctx, uint32_t mem_addr, uint8_t *data_rx,
367 uint16_t n_data );
368
379
393flash_err_t flash_erase_block( flash_t *ctx, uint32_t mem_addr );
394
409flash_err_t flash_erase_half_block( flash_t *ctx, uint32_t mem_addr );
410
424flash_err_t flash_erase_sector( flash_t *ctx, uint32_t mem_addr );
425
435
445
446#ifdef __cplusplus
447}
448#endif
449#endif // _FLASH_H_
450 // End public_function group
453
454// ------------------------------------------------------------------------ END
void flash_default_cfg(flash_t *ctx)
Click Default Configuration function.
uint8_t flash_read_status(flash_t *ctx)
Status Read function.
void flash_write_disable(flash_t *ctx)
Write Disable function.
void flash_write_enable(flash_t *ctx)
Write Enable function.
flash_err_t flash_erase_half_block(flash_t *ctx, uint32_t mem_addr)
Half Block Erase function.
void flash_set_write_protect_pin(flash_t *ctx, flash_pin_state_t state)
WP Pin Set function.
flash_err_t flash_write_byte(flash_t *ctx, uint32_t mem_addr, uint8_t data_tx)
Byte Write function.
flash_err_t flash_read_byte(flash_t *ctx, uint32_t mem_addr, uint8_t *data_rx)
Byte Read function.
void flash_set_hold_pin(flash_t *ctx, flash_pin_state_t state)
HLD Pin Set function.
void flash_cfg_setup(flash_cfg_t *cfg)
Config Object Setup function.
void flash_erase_chip(flash_t *ctx)
Chip Erase function.
void flash_write_status(flash_t *ctx, uint8_t data_tx)
Status Write function.
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_t flash_erase_block(flash_t *ctx, uint32_t mem_addr)
Block Erase function.
void flash_read_id(flash_t *ctx, uint8_t *manufact_id, uint16_t *dev_id)
ID Read function.
void flash_reset(flash_t *ctx)
Reset function.
flash_err_t flash_erase_sector(flash_t *ctx, uint32_t mem_addr)
Sector Erase function.
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_err_t flash_init(flash_t *ctx, flash_cfg_t *cfg)
Click Initialization function.
flash_err_t
Click error code definition.
Definition: flash.h:129
flash_pin_state_t
Click pin states.
Definition: flash.h:141
@ FLASH_ERR_MEM_ADDR
Definition: flash.h:132
@ FLASH_ERR_INIT_DRV
Definition: flash.h:131
@ FLASH_ERR_NDATA
Definition: flash.h:133
@ FLASH_OK
Definition: flash.h:130
@ FLASH_PIN_STATE_LOW
Definition: flash.h:142
@ FLASH_PIN_STATE_HIGH
Definition: flash.h:143
Click configuration structure definition.
Definition: flash.h:169
pin_name_t hld
Definition: flash.h:177
pin_name_t sdi
Definition: flash.h:172
spi_master_chip_select_polarity_t cs_polarity
Definition: flash.h:183
pin_name_t sck
Definition: flash.h:173
spi_master_mode_t spi_mode
Definition: flash.h:182
uint32_t spi_speed
Definition: flash.h:181
pin_name_t wp
Definition: flash.h:178
pin_name_t sdo
Definition: flash.h:171
pin_name_t cs
Definition: flash.h:174
Click context object definition.
Definition: flash.h:151
digital_out_t cs
Definition: flash.h:153
digital_out_t hld
Definition: flash.h:156
spi_master_t spi
Definition: flash.h:160
pin_name_t chip_select
Definition: flash.h:161
digital_out_t wp
Definition: flash.h:157