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 "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 
60 #define NVSRAM4_STATUS_RDY 0x01
61 #define NVSRAM4_STATUS_WEN 0x02
62 #define NVSRAM4_STATUS_BP0 0x04
63 #define NVSRAM4_STATUS_BP1 0x08
64 #define NVSRAM4_STATUS_SNL 0x40
65 #define NVSRAM4_STATUS_WPEN 0x80
66 
67 #define NVSRAM4_RTC_FLAGS 0x00
68 #define NVSRAM4_RTC_TK_CENTURIES 0x01
69 #define NVSRAM4_RTC_ALARM_SECONDS 0x02
70 #define NVSRAM4_RTC_ALARM_MINUTES 0x03
71 #define NVSRAM4_RTC_ALARM_HOURS 0x04
72 #define NVSRAM4_RTC_ALARM_DAY 0x05
73 #define NVSRAM4_RTC_INTERRUPT_STATUS 0x06
74 #define NVSRAM4_RTC_WATCHDOG_TIMER 0x07
75 #define NVSRAM4_RTC_CALIBRATION 0x08
76 #define NVSRAM4_RTC_TK_SECONDS 0x09
77 #define NVSRAM4_RTC_TK_MINUTES 0x0A
78 #define NVSRAM4_RTC_TK_HOURS 0x0B
79 #define NVSRAM4_RTC_TK_DAY 0x0C
80 #define NVSRAM4_RTC_TK_DATE 0x0D
81 #define NVSRAM4_RTC_TK_MONTHS 0x0E
82 #define NVSRAM4_RTC_TK_YEARS 0x0F
83  // nvsram4_reg
85 
111 #define NVSRAM4_STATUS_RDSR 0x05
112 #define NVSRAM4_STATUS_FRDSR 0x0A
113 #define NVSRAM4_STATUS_WRSR 0x01
114 #define NVSRAM4_STATUS_WREN 0x06
115 #define NVSRAM4_STATUS_WRDI 0x04
116  // status_reg
118 
129 #define NVSRAM4_SRAM_READ 0x03
130 #define NVSRAM4_SRAM_FREAD 0x0B
131 #define NVSRAM4_SRAM_WRITE 0x02
132  // sram_cmd
134 
145 #define NVSRAM4_SRAM_RDRTC 0x13
146 #define NVSRAM4_SRAM_FAST_RDRTC 0x1D
147 #define NVSRAM4_SRAM_WRTC 0x12
148  // rtc_cmd
150 
161 #define NVSRAM4_SPEC_NV_STORE 0x3C
162 #define NVSRAM4_SPEC_NV_RECALL 0x60
163 #define NVSRAM4_SPEC_NV_ASENB 0x59
164 #define NVSRAM4_SPEC_NV_ASDISB 0x19
165  // nv_spc_cmd
167 
178 #define NVSRAM4_SPEC_SLEEP 0xB9
179 #define NVSRAM4_SPEC_WRSN 0xC2
180 #define NVSRAM4_SPEC_RDSN 0xC3
181 #define NVSRAM4_SPEC_FRDSN 0xC9
182 #define NVSRAM4_SPEC_RDID 0x9F
183 #define NVSRAM4_SPEC_FRDID 0x99
184  // spc_cmd
186 
197 #define NVSRAM4_STATUS_RDY 0x01
198 #define NVSRAM4_STATUS_WEN 0x02
199 #define NVSRAM4_STATUS_BP0 0x04
200 #define NVSRAM4_STATUS_BP1 0x08
201 #define NVSRAM4_STATUS_SNL 0x40
202 #define NVSRAM4_STATUS_WPEN 0x80
203  // status_reg_cont
205 
216 #define NVSRAM4_HOLD_ENABLE 0x00
217 #define NVSRAM4_HOLD_DISABLE 0x01
218 
219  // en_hold
221 
232 #define NVSRAM4_HSB_ENABLE 0x00
233 #define NVSRAM4_HSB_DISABLE 0x01
234 
235  // en_hw_store
237 
248 #define NVSRAM4_WP_ENABLE 0x00
249 #define NVSRAM4_WP_DISABLE 0x01
250 
251  // en_hw_store
253 
254  // nvsram4_set
256 
267 #define NVSRAM4_SUCCESS 0
268 #define NVSRAM4_ERROR -1
269 
270  // status
272 
287 #define NVSRAM4_MAP_MIKROBUS( cfg, mikrobus ) \
288  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
289  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
290  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
291  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
292  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_AN ); \
293  cfg.hsb = MIKROBUS( mikrobus, MIKROBUS_RST ); \
294  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
295  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
296  // nvsram4_map // nvsram4
299 
304 typedef struct
305 {
306  // Output pins
307 
308  digital_out_t hld;
309  digital_out_t hsb;
310  digital_out_t wp;
312  // Input pins
313 
314  digital_in_t int_pin;
316  // Modules
317 
318  spi_master_t spi;
320  pin_name_t chip_select;
322 } nvsram4_t;
323 
328 typedef struct
329 {
330  // Communication gpio pins
331 
332  pin_name_t miso;
333  pin_name_t mosi;
334  pin_name_t sck;
335  pin_name_t cs;
337  // Additional gpio pins
338 
339  pin_name_t hld;
340  pin_name_t hsb;
341  pin_name_t wp;
342  pin_name_t int_pin;
344  // static variable
345 
346  uint32_t spi_speed;
347  spi_master_mode_t spi_mode;
348  spi_master_chip_select_polarity_t cs_polarity;
350 } nvsram4_cfg_t;
351 
352 typedef struct
353 {
354 
355  uint8_t hours;
356  uint8_t min;
357  uint8_t sec;
358 
360 
361 typedef struct
362 {
363 
364  uint8_t year;
365  uint8_t month;
366  uint8_t day;
367  uint8_t day_of_week;
368 
369 
371 
372 typedef struct
373 {
374 
375  uint8_t day;
376  uint8_t hours;
377  uint8_t min;
378  uint8_t sec;
379 
381 
397 void nvsram4_cfg_setup ( nvsram4_cfg_t *cfg );
398 
413 err_t nvsram4_init ( nvsram4_t *ctx, nvsram4_cfg_t *cfg );
414 
425 void nvsram4_default_cfg ( nvsram4_t *ctx );
426 
444 err_t nvsram4_generic_write ( nvsram4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
445 
463 err_t nvsram4_generic_read ( nvsram4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
464 
478 err_t nvsram4_set_cmd ( nvsram4_t *ctx, uint8_t cmd );
479 
491 uint8_t nvsram4_read_status ( nvsram4_t *ctx );
492 
504 void nvsram4_write_status ( nvsram4_t *ctx, uint8_t st_data );
505 
524 err_t nvsram4_burst_read_memory ( nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_out, uint8_t n_bytes );
525 
544 err_t nvsram4_burst_write_memory ( nvsram4_t *ctx, uint32_t mem_addr, uint8_t *data_in, uint8_t n_bytes );
545 
559 uint32_t nvsram4_read_id ( nvsram4_t *ctx );
560 
575 uint8_t nvsram4_rtc_read_reg ( nvsram4_t *ctx, uint8_t rtc_reg );
576 
592 void nvsram4_rtc_write_reg ( nvsram4_t *ctx, uint8_t rtc_reg, uint8_t data_in );
593 
606 void nvsram4_enable_rtc_write ( nvsram4_t *ctx );
607 
621 
635 void nvsram4_get_rtc_time ( nvsram4_t *ctx, nvsram4_rtc_time_t *rtc_time );
636 
651 err_t nvsram4_set_rtc_time ( nvsram4_t *ctx, nvsram4_rtc_time_t rtc_time );
652 
666 void nvsram4_get_rtc_date ( nvsram4_t *ctx, nvsram4_rtc_date_t *rtc_date );
667 
682 err_t nvsram4_set_rtc_date ( nvsram4_t *ctx, nvsram4_rtc_date_t rtc_date );
683 
697 void nvsram4_get_rtc_alarm ( nvsram4_t *ctx, nvsram4_rtc_alarm_t *rtc_alarm );
698 
713 err_t nvsram4_set_rtc_alarm ( nvsram4_t *ctx, nvsram4_rtc_alarm_t rtc_alarm );
714 
732 err_t nvsram4_hold ( nvsram4_t *ctx, uint8_t en_hold );
733 
751 err_t nvsram4_hardware_store ( nvsram4_t *ctx, uint8_t en_hw_store );
752 
770 err_t nvsram4_hw_write_protection ( nvsram4_t *ctx, uint8_t en_wp );
771 
772 #ifdef __cplusplus
773 }
774 #endif
775 #endif // NVSRAM4_H
776  // nvsram4
778 
779 // ------------------------------------------------------------------------ END
nvsram4_init
err_t nvsram4_init(nvsram4_t *ctx, nvsram4_cfg_t *cfg)
nvSRAM 4 initialization function.
nvsram4_default_cfg
void nvsram4_default_cfg(nvsram4_t *ctx)
nvSRAM 4 default configuration function.
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_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:351
nvsram4_write_status
void nvsram4_write_status(nvsram4_t *ctx, uint8_t st_data)
nvSRAM 4 write status function.
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_read_status
uint8_t nvsram4_read_status(nvsram4_t *ctx)
nvSRAM 4 read status function.
nvsram4_cfg_t
nvSRAM 4 Click configuration object.
Definition: nvsram4.h:327
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:303
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_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_rtc_alarm_t
Definition: nvsram4.h:371
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_rtc_date_t
Definition: nvsram4.h:360
nvsram4_hw_write_protection
err_t nvsram4_hw_write_protection(nvsram4_t *ctx, uint8_t en_wp)
nvSRAM 4 write protection function.