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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_spi_master.h"
48 
70 #define NVSRAM4_STATUS_RDY 0x01
71 #define NVSRAM4_STATUS_WEN 0x02
72 #define NVSRAM4_STATUS_BP0 0x04
73 #define NVSRAM4_STATUS_BP1 0x08
74 #define NVSRAM4_STATUS_SNL 0x40
75 #define NVSRAM4_STATUS_WPEN 0x80
76 
77 #define NVSRAM4_RTC_FLAGS 0x00
78 #define NVSRAM4_RTC_TK_CENTURIES 0x01
79 #define NVSRAM4_RTC_ALARM_SECONDS 0x02
80 #define NVSRAM4_RTC_ALARM_MINUTES 0x03
81 #define NVSRAM4_RTC_ALARM_HOURS 0x04
82 #define NVSRAM4_RTC_ALARM_DAY 0x05
83 #define NVSRAM4_RTC_INTERRUPT_STATUS 0x06
84 #define NVSRAM4_RTC_WATCHDOG_TIMER 0x07
85 #define NVSRAM4_RTC_CALIBRATION 0x08
86 #define NVSRAM4_RTC_TK_SECONDS 0x09
87 #define NVSRAM4_RTC_TK_MINUTES 0x0A
88 #define NVSRAM4_RTC_TK_HOURS 0x0B
89 #define NVSRAM4_RTC_TK_DAY 0x0C
90 #define NVSRAM4_RTC_TK_DATE 0x0D
91 #define NVSRAM4_RTC_TK_MONTHS 0x0E
92 #define NVSRAM4_RTC_TK_YEARS 0x0F
93  // nvsram4_reg
95 
121 #define NVSRAM4_STATUS_RDSR 0x05
122 #define NVSRAM4_STATUS_FRDSR 0x0A
123 #define NVSRAM4_STATUS_WRSR 0x01
124 #define NVSRAM4_STATUS_WREN 0x06
125 #define NVSRAM4_STATUS_WRDI 0x04
126  // status_reg
128 
139 #define NVSRAM4_SRAM_READ 0x03
140 #define NVSRAM4_SRAM_FREAD 0x0B
141 #define NVSRAM4_SRAM_WRITE 0x02
142  // sram_cmd
144 
155 #define NVSRAM4_SRAM_RDRTC 0x13
156 #define NVSRAM4_SRAM_FAST_RDRTC 0x1D
157 #define NVSRAM4_SRAM_WRTC 0x12
158  // rtc_cmd
160 
171 #define NVSRAM4_SPEC_NV_STORE 0x3C
172 #define NVSRAM4_SPEC_NV_RECALL 0x60
173 #define NVSRAM4_SPEC_NV_ASENB 0x59
174 #define NVSRAM4_SPEC_NV_ASDISB 0x19
175  // nv_spc_cmd
177 
188 #define NVSRAM4_SPEC_SLEEP 0xB9
189 #define NVSRAM4_SPEC_WRSN 0xC2
190 #define NVSRAM4_SPEC_RDSN 0xC3
191 #define NVSRAM4_SPEC_FRDSN 0xC9
192 #define NVSRAM4_SPEC_RDID 0x9F
193 #define NVSRAM4_SPEC_FRDID 0x99
194  // spc_cmd
196 
207 #define NVSRAM4_STATUS_RDY 0x01
208 #define NVSRAM4_STATUS_WEN 0x02
209 #define NVSRAM4_STATUS_BP0 0x04
210 #define NVSRAM4_STATUS_BP1 0x08
211 #define NVSRAM4_STATUS_SNL 0x40
212 #define NVSRAM4_STATUS_WPEN 0x80
213  // status_reg_cont
215 
226 #define NVSRAM4_HOLD_ENABLE 0x00
227 #define NVSRAM4_HOLD_DISABLE 0x01
228 
229  // en_hold
231 
242 #define NVSRAM4_HSB_ENABLE 0x00
243 #define NVSRAM4_HSB_DISABLE 0x01
244 
245  // en_hw_store
247 
258 #define NVSRAM4_WP_ENABLE 0x00
259 #define NVSRAM4_WP_DISABLE 0x01
260 
261  // en_hw_store
263 
264  // nvsram4_set
266 
277 #define NVSRAM4_SUCCESS 0
278 #define NVSRAM4_ERROR -1
279 
280  // status
282 
297 #define NVSRAM4_MAP_MIKROBUS( cfg, mikrobus ) \
298  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
299  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
300  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
301  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
302  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_AN ); \
303  cfg.hsb = MIKROBUS( mikrobus, MIKROBUS_RST ); \
304  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
305  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
306  // nvsram4_map // nvsram4
309 
314 typedef struct
315 {
316  // Output pins
317 
318  digital_out_t hld;
319  digital_out_t hsb;
320  digital_out_t wp;
322  // Input pins
323 
324  digital_in_t int_pin;
326  // Modules
327 
328  spi_master_t spi;
330  pin_name_t chip_select;
332 } nvsram4_t;
333 
338 typedef struct
339 {
340  // Communication gpio pins
341 
342  pin_name_t miso;
343  pin_name_t mosi;
344  pin_name_t sck;
345  pin_name_t cs;
347  // Additional gpio pins
348 
349  pin_name_t hld;
350  pin_name_t hsb;
351  pin_name_t wp;
352  pin_name_t int_pin;
354  // static variable
355 
356  uint32_t spi_speed;
357  spi_master_mode_t spi_mode;
358  spi_master_chip_select_polarity_t cs_polarity;
360 } nvsram4_cfg_t;
361 
362 typedef struct
363 {
364 
365  uint8_t hours;
366  uint8_t min;
367  uint8_t sec;
368 
370 
371 typedef struct
372 {
373 
374  uint8_t year;
375  uint8_t month;
376  uint8_t day;
377  uint8_t day_of_week;
378 
379 
381 
382 typedef struct
383 {
384 
385  uint8_t day;
386  uint8_t hours;
387  uint8_t min;
388  uint8_t sec;
389 
391 
408 
423 err_t nvsram4_init ( nvsram4_t *ctx, nvsram4_cfg_t *cfg );
424 
436 
454 err_t nvsram4_generic_write ( nvsram4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
455 
473 err_t nvsram4_generic_read ( nvsram4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
474 
488 err_t nvsram4_set_cmd ( nvsram4_t *ctx, uint8_t cmd );
489 
502 
514 void nvsram4_write_status ( nvsram4_t *ctx, uint8_t st_data );
515 
534 err_t nvsram4_burst_read_memory ( nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_out, uint8_t n_bytes );
535 
554 err_t nvsram4_burst_write_memory ( nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_in, uint8_t n_bytes );
555 
569 uint32_t nvsram4_read_id ( nvsram4_t *ctx );
570 
585 uint8_t nvsram4_rtc_read_reg ( nvsram4_t *ctx, uint8_t rtc_reg );
586 
602 void nvsram4_rtc_write_reg ( nvsram4_t *ctx, uint8_t rtc_reg, uint8_t data_in );
603 
617 
631 
646 
662 
677 
693 
708 
724 
742 err_t nvsram4_hold ( nvsram4_t *ctx, uint8_t en_hold );
743 
761 err_t nvsram4_hardware_store ( nvsram4_t *ctx, uint8_t en_hw_store );
762 
780 err_t nvsram4_hw_write_protection ( nvsram4_t *ctx, uint8_t en_wp );
781 
782 #ifdef __cplusplus
783 }
784 #endif
785 #endif // NVSRAM4_H
786  // nvsram4
788 
789 // ------------------------------------------------------------------------ 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:377
nvsram4_cfg_t::hsb
pin_name_t hsb
Definition: nvsram4.h:350
nvsram4_rtc_date_t::year
uint8_t year
Definition: nvsram4.h:374
nvsram4_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: nvsram4.h:357
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:344
nvsram4_cfg_t::hld
pin_name_t hld
Definition: nvsram4.h:349
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:385
nvsram4_rtc_time_t::min
uint8_t min
Definition: nvsram4.h:366
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:363
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:388
nvsram4_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: nvsram4.h:358
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:319
nvsram4_rtc_date_t::day
uint8_t day
Definition: nvsram4.h:376
nvsram4_rtc_alarm_t::min
uint8_t min
Definition: nvsram4.h:387
nvsram4_cfg_t::spi_speed
uint32_t spi_speed
Definition: nvsram4.h:356
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:330
nvsram4_cfg_t
nvSRAM 4 Click configuration object.
Definition: nvsram4.h:339
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:315
nvsram4_rtc_time_t::sec
uint8_t sec
Definition: nvsram4.h:367
nvsram4_cfg_t::mosi
pin_name_t mosi
Definition: nvsram4.h:343
nvsram4_rtc_date_t::month
uint8_t month
Definition: nvsram4.h:375
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:352
nvsram4_rtc_alarm_t::hours
uint8_t hours
Definition: nvsram4.h:386
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:318
nvsram4_rtc_time_t::hours
uint8_t hours
Definition: nvsram4.h:365
nvsram4_rtc_alarm_t
Definition: nvsram4.h:383
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:342
nvsram4_t::wp
digital_out_t wp
Definition: nvsram4.h:320
nvsram4_cfg_t::wp
pin_name_t wp
Definition: nvsram4.h:351
nvsram4_rtc_date_t
Definition: nvsram4.h:372
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:324
nvsram4_cfg_t::cs
pin_name_t cs
Definition: nvsram4.h:345
nvsram4_t::spi
spi_master_t spi
Definition: nvsram4.h:328