nvsram4  2.0.0.0
nvsram4.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 NVSRAM4_H
29 #define NVSRAM4_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_spi_master.h"
52 
74 #define NVSRAM4_STATUS_RDY 0x01
75 #define NVSRAM4_STATUS_WEN 0x02
76 #define NVSRAM4_STATUS_BP0 0x04
77 #define NVSRAM4_STATUS_BP1 0x08
78 #define NVSRAM4_STATUS_SNL 0x40
79 #define NVSRAM4_STATUS_WPEN 0x80
80 
81 #define NVSRAM4_RTC_FLAGS 0x00
82 #define NVSRAM4_RTC_TK_CENTURIES 0x01
83 #define NVSRAM4_RTC_ALARM_SECONDS 0x02
84 #define NVSRAM4_RTC_ALARM_MINUTES 0x03
85 #define NVSRAM4_RTC_ALARM_HOURS 0x04
86 #define NVSRAM4_RTC_ALARM_DAY 0x05
87 #define NVSRAM4_RTC_INTERRUPT_STATUS 0x06
88 #define NVSRAM4_RTC_WATCHDOG_TIMER 0x07
89 #define NVSRAM4_RTC_CALIBRATION 0x08
90 #define NVSRAM4_RTC_TK_SECONDS 0x09
91 #define NVSRAM4_RTC_TK_MINUTES 0x0A
92 #define NVSRAM4_RTC_TK_HOURS 0x0B
93 #define NVSRAM4_RTC_TK_DAY 0x0C
94 #define NVSRAM4_RTC_TK_DATE 0x0D
95 #define NVSRAM4_RTC_TK_MONTHS 0x0E
96 #define NVSRAM4_RTC_TK_YEARS 0x0F
97  // nvsram4_reg
99 
125 #define NVSRAM4_STATUS_RDSR 0x05
126 #define NVSRAM4_STATUS_FRDSR 0x0A
127 #define NVSRAM4_STATUS_WRSR 0x01
128 #define NVSRAM4_STATUS_WREN 0x06
129 #define NVSRAM4_STATUS_WRDI 0x04
130  // status_reg
132 
143 #define NVSRAM4_SRAM_READ 0x03
144 #define NVSRAM4_SRAM_FREAD 0x0B
145 #define NVSRAM4_SRAM_WRITE 0x02
146  // sram_cmd
148 
159 #define NVSRAM4_SRAM_RDRTC 0x13
160 #define NVSRAM4_SRAM_FAST_RDRTC 0x1D
161 #define NVSRAM4_SRAM_WRTC 0x12
162  // rtc_cmd
164 
175 #define NVSRAM4_SPEC_NV_STORE 0x3C
176 #define NVSRAM4_SPEC_NV_RECALL 0x60
177 #define NVSRAM4_SPEC_NV_ASENB 0x59
178 #define NVSRAM4_SPEC_NV_ASDISB 0x19
179  // nv_spc_cmd
181 
192 #define NVSRAM4_SPEC_SLEEP 0xB9
193 #define NVSRAM4_SPEC_WRSN 0xC2
194 #define NVSRAM4_SPEC_RDSN 0xC3
195 #define NVSRAM4_SPEC_FRDSN 0xC9
196 #define NVSRAM4_SPEC_RDID 0x9F
197 #define NVSRAM4_SPEC_FRDID 0x99
198  // spc_cmd
200 
211 #define NVSRAM4_STATUS_RDY 0x01
212 #define NVSRAM4_STATUS_WEN 0x02
213 #define NVSRAM4_STATUS_BP0 0x04
214 #define NVSRAM4_STATUS_BP1 0x08
215 #define NVSRAM4_STATUS_SNL 0x40
216 #define NVSRAM4_STATUS_WPEN 0x80
217  // status_reg_cont
219 
230 #define NVSRAM4_HOLD_ENABLE 0x00
231 #define NVSRAM4_HOLD_DISABLE 0x01
232 
233  // en_hold
235 
246 #define NVSRAM4_HSB_ENABLE 0x00
247 #define NVSRAM4_HSB_DISABLE 0x01
248 
249  // en_hw_store
251 
262 #define NVSRAM4_WP_ENABLE 0x00
263 #define NVSRAM4_WP_DISABLE 0x01
264 
265  // en_hw_store
267 
268  // nvsram4_set
270 
281 #define NVSRAM4_SUCCESS 0
282 #define NVSRAM4_ERROR -1
283 
284  // status
286 
301 #define NVSRAM4_MAP_MIKROBUS( cfg, mikrobus ) \
302  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
303  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
304  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
305  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
306  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_AN ); \
307  cfg.hsb = MIKROBUS( mikrobus, MIKROBUS_RST ); \
308  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
309  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
310  // nvsram4_map // nvsram4
313 
318 typedef struct
319 {
320  // Output pins
321 
322  digital_out_t hld;
323  digital_out_t hsb;
324  digital_out_t wp;
326  // Input pins
327 
328  digital_in_t int_pin;
330  // Modules
331 
332  spi_master_t spi;
334  pin_name_t chip_select;
336 } nvsram4_t;
337 
342 typedef struct
343 {
344  // Communication gpio pins
345 
346  pin_name_t miso;
347  pin_name_t mosi;
348  pin_name_t sck;
349  pin_name_t cs;
351  // Additional gpio pins
352 
353  pin_name_t hld;
354  pin_name_t hsb;
355  pin_name_t wp;
356  pin_name_t int_pin;
358  // static variable
359 
360  uint32_t spi_speed;
361  spi_master_mode_t spi_mode;
362  spi_master_chip_select_polarity_t cs_polarity;
364 } nvsram4_cfg_t;
365 
366 typedef struct
367 {
368 
369  uint8_t hours;
370  uint8_t min;
371  uint8_t sec;
372 
374 
375 typedef struct
376 {
377 
378  uint8_t year;
379  uint8_t month;
380  uint8_t day;
381  uint8_t day_of_week;
382 
383 
385 
386 typedef struct
387 {
388 
389  uint8_t day;
390  uint8_t hours;
391  uint8_t min;
392  uint8_t sec;
393 
395 
412 
427 err_t nvsram4_init ( nvsram4_t *ctx, nvsram4_cfg_t *cfg );
428 
440 
458 err_t nvsram4_generic_write ( nvsram4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
459 
477 err_t nvsram4_generic_read ( nvsram4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
478 
492 err_t nvsram4_set_cmd ( nvsram4_t *ctx, uint8_t cmd );
493 
506 
518 void nvsram4_write_status ( nvsram4_t *ctx, uint8_t st_data );
519 
538 err_t nvsram4_burst_read_memory ( nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_out, uint8_t n_bytes );
539 
558 err_t nvsram4_burst_write_memory ( nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_in, uint8_t n_bytes );
559 
573 uint32_t nvsram4_read_id ( nvsram4_t *ctx );
574 
589 uint8_t nvsram4_rtc_read_reg ( nvsram4_t *ctx, uint8_t rtc_reg );
590 
606 void nvsram4_rtc_write_reg ( nvsram4_t *ctx, uint8_t rtc_reg, uint8_t data_in );
607 
621 
635 
650 
666 
681 
697 
712 
728 
746 err_t nvsram4_hold ( nvsram4_t *ctx, uint8_t en_hold );
747 
765 err_t nvsram4_hardware_store ( nvsram4_t *ctx, uint8_t en_hw_store );
766 
784 err_t nvsram4_hw_write_protection ( nvsram4_t *ctx, uint8_t en_wp );
785 
786 #ifdef __cplusplus
787 }
788 #endif
789 #endif // NVSRAM4_H
790  // nvsram4
792 
793 // ------------------------------------------------------------------------ END
nvsram4_init
err_t nvsram4_init(nvsram4_t *ctx, nvsram4_cfg_t *cfg)
nvSRAM 4 initialization function.
nvsram4_rtc_date_t::day_of_week
uint8_t day_of_week
Definition: nvsram4.h:381
nvsram4_cfg_t::hsb
pin_name_t hsb
Definition: nvsram4.h:354
nvsram4_rtc_date_t::year
uint8_t year
Definition: nvsram4.h:378
nvsram4_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: nvsram4.h:361
nvsram4_default_cfg
void nvsram4_default_cfg(nvsram4_t *ctx)
nvSRAM 4 default configuration function.
nvsram4_cfg_t::sck
pin_name_t sck
Definition: nvsram4.h:348
nvsram4_cfg_t::hld
pin_name_t hld
Definition: nvsram4.h:353
nvsram4_set_rtc_alarm
err_t nvsram4_set_rtc_alarm(nvsram4_t *ctx, nvsram4_rtc_alarm_t rtc_alarm)
nvSRAM 4 set RTC alarm function.
nvsram4_disable_rtc_write
void nvsram4_disable_rtc_write(nvsram4_t *ctx)
nvSRAM 4 disable rtc register write function.
nvsram4_get_rtc_date
void nvsram4_get_rtc_date(nvsram4_t *ctx, nvsram4_rtc_date_t *rtc_date)
nvSRAM 4 get RTC date function.
nvsram4_rtc_alarm_t::day
uint8_t day
Definition: nvsram4.h:389
nvsram4_rtc_time_t::min
uint8_t min
Definition: nvsram4.h:370
nvsram4_rtc_read_reg
uint8_t nvsram4_rtc_read_reg(nvsram4_t *ctx, uint8_t rtc_reg)
nvSRAM 4 rtc read register function.
nvsram4_set_rtc_date
err_t nvsram4_set_rtc_date(nvsram4_t *ctx, nvsram4_rtc_date_t rtc_date)
nvSRAM 4 set RTC date function.
nvsram4_read_id
uint32_t nvsram4_read_id(nvsram4_t *ctx)
nvSRAM 4 read device ID function.
nvsram4_get_rtc_alarm
void nvsram4_get_rtc_alarm(nvsram4_t *ctx, nvsram4_rtc_alarm_t *rtc_alarm)
nvSRAM 4 get RTC alarm function.
nvsram4_rtc_time_t
Definition: nvsram4.h:367
nvsram4_write_status
void nvsram4_write_status(nvsram4_t *ctx, uint8_t st_data)
nvSRAM 4 write status function.
nvsram4_rtc_alarm_t::sec
uint8_t sec
Definition: nvsram4.h:392
nvsram4_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: nvsram4.h:362
nvsram4_cfg_setup
void nvsram4_cfg_setup(nvsram4_cfg_t *cfg)
nvSRAM 4 configuration object setup function.
nvsram4_hardware_store
err_t nvsram4_hardware_store(nvsram4_t *ctx, uint8_t en_hw_store)
nvSRAM 4 hardware store function.
nvsram4_t::hsb
digital_out_t hsb
Definition: nvsram4.h:323
nvsram4_rtc_date_t::day
uint8_t day
Definition: nvsram4.h:380
nvsram4_rtc_alarm_t::min
uint8_t min
Definition: nvsram4.h:391
nvsram4_cfg_t::spi_speed
uint32_t spi_speed
Definition: nvsram4.h:360
nvsram4_read_status
uint8_t nvsram4_read_status(nvsram4_t *ctx)
nvSRAM 4 read status function.
nvsram4_t::chip_select
pin_name_t chip_select
Definition: nvsram4.h:334
nvsram4_cfg_t
nvSRAM 4 Click configuration object.
Definition: nvsram4.h:343
nvsram4_rtc_write_reg
void nvsram4_rtc_write_reg(nvsram4_t *ctx, uint8_t rtc_reg, uint8_t data_in)
nvSRAM 4 rtc write register function.
nvsram4_t
nvSRAM 4 Click context object.
Definition: nvsram4.h:319
nvsram4_rtc_time_t::sec
uint8_t sec
Definition: nvsram4.h:371
nvsram4_cfg_t::mosi
pin_name_t mosi
Definition: nvsram4.h:347
nvsram4_rtc_date_t::month
uint8_t month
Definition: nvsram4.h:379
nvsram4_set_cmd
err_t nvsram4_set_cmd(nvsram4_t *ctx, uint8_t cmd)
nvSRAM 4 set command function.
nvsram4_enable_rtc_write
void nvsram4_enable_rtc_write(nvsram4_t *ctx)
nvSRAM 4 enable rtc register write function.
nvsram4_cfg_t::int_pin
pin_name_t int_pin
Definition: nvsram4.h:356
nvsram4_rtc_alarm_t::hours
uint8_t hours
Definition: nvsram4.h:390
nvsram4_hold
err_t nvsram4_hold(nvsram4_t *ctx, uint8_t en_hold)
nvSRAM 4 set hold function.
nvsram4_burst_read_memory
err_t nvsram4_burst_read_memory(nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_out, uint8_t n_bytes)
nvSRAM 4 burst read memory function.
nvsram4_generic_read
err_t nvsram4_generic_read(nvsram4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
nvSRAM 4 data reading function.
nvsram4_set_rtc_time
err_t nvsram4_set_rtc_time(nvsram4_t *ctx, nvsram4_rtc_time_t rtc_time)
nvSRAM 4 set RTC time function.
nvsram4_t::hld
digital_out_t hld
Definition: nvsram4.h:322
nvsram4_rtc_time_t::hours
uint8_t hours
Definition: nvsram4.h:369
nvsram4_rtc_alarm_t
Definition: nvsram4.h:387
nvsram4_get_rtc_time
void nvsram4_get_rtc_time(nvsram4_t *ctx, nvsram4_rtc_time_t *rtc_time)
nvSRAM 4 get RTC time function.
nvsram4_burst_write_memory
err_t nvsram4_burst_write_memory(nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_in, uint8_t n_bytes)
nvSRAM 4 burst write memory function.
nvsram4_generic_write
err_t nvsram4_generic_write(nvsram4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
nvSRAM 4 data writing function.
nvsram4_cfg_t::miso
pin_name_t miso
Definition: nvsram4.h:346
nvsram4_t::wp
digital_out_t wp
Definition: nvsram4.h:324
nvsram4_cfg_t::wp
pin_name_t wp
Definition: nvsram4.h:355
nvsram4_rtc_date_t
Definition: nvsram4.h:376
nvsram4_hw_write_protection
err_t nvsram4_hw_write_protection(nvsram4_t *ctx, uint8_t en_wp)
nvSRAM 4 write protection function.
nvsram4_t::int_pin
digital_in_t int_pin
Definition: nvsram4.h:328
nvsram4_cfg_t::cs
pin_name_t cs
Definition: nvsram4.h:349
nvsram4_t::spi
spi_master_t spi
Definition: nvsram4.h:332