semperflash  2.0.0.0
semperflash.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 SEMPERFLASH_H
36 #define SEMPERFLASH_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_spi_master.h"
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
67 #define SEMPERFLASH_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
69  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
70  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
71  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
72  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
73  cfg.io2 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
74  cfg.io3 = MIKROBUS( mikrobus, MIKROBUS_INT )
75 
81 #define SEMPERFLASH_RETVAL uint8_t
82 
83 #define SEMPERFLASH_OK 0x00
84 #define SEMPERFLASH_INIT_ERROR 0xFF
85 
91 #define SEMPERFLASH_MANUFACTURER_DEVICE_ID 0x9F
92 #define SEMPERFLASH_JEDEC_SERIAL_FLASH_DISCOVER_PARAMS 0x5A
93 #define SEMPERFLASH_DEVICE_ID 0x4C
94 #define SEMPERFLASH_READ_STATUS_REG_1 0x05
95 #define SEMPERFLASH_READ_STATUS_REG_2 0x07
96 #define SEMPERFLASH_READ_CFG_REG_1 0x35
97 #define SEMPERFLASH_READ_ANY_REG 0x65
98 #define SEMPERFLASH_WRITE_ENABLE 0x06
99 #define SEMPERFLASH_WRITE_ENABLE_VOLATILE 0x50
100 #define SEMPERFLASH_WRITE_DISABLE 0x04
101 #define SEMPERFLASH_WRITE_REG 0x01
102 #define SEMPERFLASH_WRITE_ANY_REG 0x71
103 #define SEMPERFLASH_CLEAR_PROGRAM_ERASE_ERROR 0x30
104 #define SEMPERFLASH_ENTER_4_BYTE_ADDR_MODE 0xB7
105 #define SEMPERFLASH_EXIT_4_BYTE_ADDR_MODE 0xB8
106 #define SEMPERFLASH_READ_DATA_LEARN_PATT_REG 0x41
107 #define SEMPERFLASH_PROGRAM_DATA_LEARN_PATT 0x43
108 #define SEMPERFLASH_WRITE_DATA_LEARN_PATT 0x4A
109 #define SEMPERFLASH_AUTOBOOT_REG_WRITE 0x15
110 #define SEMPERFLASH_READ_ECC_STATUS 0x19
111 #define SEMPERFLASH_CLEAR_ECC_STATUS 0x1B
112 #define SEMPERFLASH_DATA_INT_CHECK 0x5B
113 #define SEMPERFLASH_READ_0 0x03
114 #define SEMPERFLASH_READ_1 0x13
115 #define SEMPERFLASH_FAST_READ_0 0x0B
116 #define SEMPERFLASH_FAST_READ_1 0x0C
117 #define SEMPERFLASH_PROGRAM_PAGE_0 0x02
118 #define SEMPERFLASH_PROGRAM_PAGE_1 0x12
119 #define SEMPERFLASH_ERASE_4K_0 0x20
120 #define SEMPERFLASH_ERASE_4K_1 0x21
121 #define SEMPERFLASH_ERASE_256K_1 0xD8
122 #define SEMPERFLASH_ERASE_256K_2 0xDC
123 #define SEMPERFLASH_ERASE_CHIP_1 0x60
124 #define SEMPERFLASH_ERASE_CHIP_2 0xC7
125 #define SEMPERFLASH_ERASE_STATUS 0xD0
126 #define SEMPERFLASH_ERASE_CNT 0x5D
127 #define SEMPERFLASH_SUSPEND_ERASE_PROG_DATA_CHECK 0x75
128 #define SEMPERFLASH_SUSPEND_ERASE_PROG 0x85
129 #define SEMPERFLASH_RESUME_ERASE_PROG_DATA_CHECK 0x7A
130 #define SEMPERFLASH_RESUME_ERASE_PROG 0x8A
131 #define SEMPERFLASH_PROG_SECURE_SILICON_REGION 0x42
132 #define SEMPERFLASH_READ_SECURE_SILICON_REGION 0x4B
133 #define SEMPERFLASH_ASP_PROG 0x2F
134 #define SEMPERFLASH_READ_DYNAMIC_PROTECION_BIT 0xFA
135 #define SEMPERFLASH_WRITE_DYNAMIC_PROTECION_BIT 0xFB
136 #define SEMPERFLASH_READ_PRESISTENT_PROTECTION_BIT 0xFC
137 #define SEMPERFLASH_PROG_PRESISTENT_PROTECTION_BIT 0xFD
138 #define SEMPERFLASH_ERASE_PRESISTENT_PROTECTION_BIT 0xE4
139 #define SEMPERFLASH_WRITE_PPB_PROTECTION_LOCK_BIT 0xA6
140 #define SEMPERFLASH_READ_PROG_PRESISTENT_PROTECION_LOCK_BIT 0xA7
141 #define SEMPERFLASH_PROG_PASSWORD 0xE8
142 #define SEMPERFLASH_PASSWORD_UNLOCK 0xE9
143 #define SEMPERFLASH_SOFT_RESET_ENABLE 0x66
144 #define SEMPERFLASH_SOFT_RESET 0x99
145 #define SEMPERFLASH_LEGACY_SOFT_RESET 0xF0
146 #define SEMPERFLASH_ENTER_DEEP_POWER_DOWN_MODE 0xB9
147 
148 #define SEMPERFLASH_PIN_STATE_HIGH 1
149 #define SEMPERFLASH_PIN_STATE_LOW 0
150 
151 #define SEMPERFLASH_ID_ERROR 0xCC
152 #define SEMPERFLASH_SIZE_ERROR 0xBB
153 #define SEMPERFLASH_SUCCESS 0xAA
154 
155 #define SEMPERFLASH_MANUFACTURER_DEVICE_ID_VALUE 0x34
156 #define SEMPERFLASH_DEVICE_ID_BYTE_SIZE 8
157 
158 #define SEMPERFLASH_STATUS_REG_1_DEFAULT 0x00
159 #define SEMPERFLASH_STATUS_REG_1_PROGRAM_ERASE_REG_WRITE_ENABLE 0x02
160 #define SEMPERFLASH_STATUS_REG_1_DEVICE_BUSY 0x01
161 #define SEMPERFLASH_CFG_1_DEFAULT 0x00
162 #define SEMPERFLASH_CFG_2_DEFAULT 0x00
163 #define SEMPERFLASH_CFG_3_DEFAULT 0x00
164 #define SEMPERFLASH_CFG_4_DEFAULT 0x00
165 
166 #define SEMPERFLASH_MANUFACTURER_DEVICE_ID_VALUE 0x34
167 #define SEMPERFLASH_MEMORY_BUF_MAX_SIZE 512
168  // End group macro
171 // --------------------------------------------------------------- PUBLIC TYPES
180 typedef struct
181 {
182  // Output pins
183 
184  digital_out_t rst;
185  digital_out_t io2;
186  digital_out_t io3;
187  digital_out_t cs;
188 
189  // Modules
190 
191  spi_master_t spi;
192  pin_name_t chip_select;
193 
194 } semperflash_t;
195 
199 typedef struct
200 {
201  // Communication gpio pins
202 
203  pin_name_t miso;
204  pin_name_t mosi;
205  pin_name_t sck;
206  pin_name_t cs;
207 
208  // Additional gpio pins
209 
210  pin_name_t rst;
211  pin_name_t io2;
212  pin_name_t io3;
213 
214  // static variable
215 
216  uint32_t spi_speed;
217  uint8_t spi_mode;
218  spi_master_chip_select_polarity_t cs_polarity;
219 
221 
225 typedef struct
226 {
227  uint8_t status_reg1;
228  uint8_t cfg1;
229  uint8_t cfg2;
230  uint8_t cfg3;
231  uint8_t cfg4;
232 
234 
235  // End types group
237 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
238 
243 #ifdef __cplusplus
244 extern "C"{
245 #endif
246 
256 
265 
274 
283 void semperflash_send_cmd ( semperflash_t *ctx, uint8_t cmd );
284 
297 (
298  semperflash_t *ctx,
299  uint8_t *write_buf,
300  uint16_t wbuf_size,
301  uint8_t *read_buf,
302  uint16_t rbuf_size
303 );
304 
315 (
316  semperflash_t *ctx,
317  uint8_t *write_buf,
318  uint16_t buf_size
319 );
320 
330 
342 (
343  semperflash_t *ctx,
344  uint32_t addr,
345  uint8_t *data_buf,
346  uint16_t buf_size
347 );
348 
360 (
361  semperflash_t *ctx,
362  uint32_t addr,
363  uint8_t *data_buf,
364  uint16_t buf_size
365 );
366 
375 void semperflash_erase_memory ( semperflash_t *ctx, uint32_t addr );
376 
385 uint8_t semperflash_get_device_id ( semperflash_t *ctx, uint8_t *id_buf );
386 
395 
404 
405 #ifdef __cplusplus
406 }
407 #endif
408 #endif // _SEMPERFLASH_H_
409  // End public_function group
412 
413 // ------------------------------------------------------------------------- END
semperflash_send_cmd
void semperflash_send_cmd(semperflash_t *ctx, uint8_t cmd)
Send command function.
semperflash_read_memory
uint8_t semperflash_read_memory(semperflash_t *ctx, uint32_t addr, uint8_t *data_buf, uint16_t buf_size)
Read memory function.
semperflash_config_t::cfg3
uint8_t cfg3
Definition: semperflash.h:230
semperflash_check_status_reg_1
uint8_t semperflash_check_status_reg_1(semperflash_t *ctx)
Check status register function.
semperflash_get_device_id
uint8_t semperflash_get_device_id(semperflash_t *ctx, uint8_t *id_buf)
Get device ID function.
semperflash_cfg_t::cs
pin_name_t cs
Definition: semperflash.h:206
semperflash_t::spi
spi_master_t spi
Definition: semperflash.h:191
semperflash_write_memory
uint8_t semperflash_write_memory(semperflash_t *ctx, uint32_t addr, uint8_t *data_buf, uint16_t buf_size)
Write memory function.
semperflash_cfg_setup
void semperflash_cfg_setup(semperflash_cfg_t *cfg)
Config Object Initialization function.
semperflash_erase_memory
void semperflash_erase_memory(semperflash_t *ctx, uint32_t addr)
Erase memory function.
semperflash_init
SEMPERFLASH_RETVAL semperflash_init(semperflash_t *ctx, semperflash_cfg_t *cfg)
Initialization function.
semperflash_cfg_t
Click configuration structure definition.
Definition: semperflash.h:200
semperflash_config_t
Flash configuration structure.
Definition: semperflash.h:226
semperflash_default_cfg
void semperflash_default_cfg(semperflash_t *ctx)
Click Default Configuration function.
semperflash_config_t::cfg2
uint8_t cfg2
Definition: semperflash.h:229
semperflash_cfg_t::io2
pin_name_t io2
Definition: semperflash.h:211
semperflash_cfg_t::rst
pin_name_t rst
Definition: semperflash.h:210
semperflash_t::chip_select
pin_name_t chip_select
Definition: semperflash.h:192
semperflash_t::rst
digital_out_t rst
Definition: semperflash.h:184
SEMPERFLASH_RETVAL
#define SEMPERFLASH_RETVAL
Definition: semperflash.h:81
semperflash_transfer_data
void semperflash_transfer_data(semperflash_t *ctx, uint8_t *write_buf, uint16_t wbuf_size, uint8_t *read_buf, uint16_t rbuf_size)
Transfer data function.
semperflash_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: semperflash.h:218
semperflash_t
Click ctx object definition.
Definition: semperflash.h:181
semperflash_cfg_t::io3
pin_name_t io3
Definition: semperflash.h:212
semperflash_check_manufacturer_id
uint8_t semperflash_check_manufacturer_id(semperflash_t *ctx)
Check manufacturer ID function.
semperflash_t::io3
digital_out_t io3
Definition: semperflash.h:186
semperflash_cfg_t::spi_speed
uint32_t spi_speed
Definition: semperflash.h:216
semperflash_config_t::status_reg1
uint8_t status_reg1
Definition: semperflash.h:227
semperflash_config_t::cfg1
uint8_t cfg1
Definition: semperflash.h:228
semperflash_config_t::cfg4
uint8_t cfg4
Definition: semperflash.h:231
semperflash_cfg_t::spi_mode
uint8_t spi_mode
Definition: semperflash.h:217
semperflash_cfg_t::sck
pin_name_t sck
Definition: semperflash.h:205
semperflash_cfg_t::miso
pin_name_t miso
Definition: semperflash.h:203
semperflash_write_config
void semperflash_write_config(semperflash_t *ctx, semperflash_config_t *cfg_data)
Write configuration function.
semperflash_cfg_t::mosi
pin_name_t mosi
Definition: semperflash.h:204
semperflash_generic_write
void semperflash_generic_write(semperflash_t *ctx, uint8_t *write_buf, uint16_t buf_size)
Writing data function.
semperflash_t::io2
digital_out_t io2
Definition: semperflash.h:185
semperflash_t::cs
digital_out_t cs
Definition: semperflash.h:187