nvsram3  2.0.0.0
nvsram3.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 NVSRAM3_H
29 #define NVSRAM3_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_i2c_master.h"
48 
70 #define NVSRAM3_CONTROL_REG_MEM_CTRL 0x00
71 #define NVSRAM3_CONTROL_REG_SERIAL_NUMBER 0x01
72 #define NVSRAM3_CONTROL_REG_DEVICE_ID 0x09
73 #define NVSRAM3_CONTROL_REG_CMD 0xAA
74 
80 #define NVSRAM3_RTC_REG_FLAGS 0x00
81 #define NVSRAM3_RTC_REG_TIME_KEEPING_CENTURIES 0x01
82 #define NVSRAM3_RTC_REG_ALARM_SECONDS 0x02
83 #define NVSRAM3_RTC_REG_ALARM_MINUTES 0x03
84 #define NVSRAM3_RTC_REG_ALARM_HOURS 0x04
85 #define NVSRAM3_RTC_REG_ALARM_DAY 0x05
86 #define NVSRAM3_RTC_REG_INTERRUPT_STATUS_CONTROL 0x06
87 #define NVSRAM3_RTC_REG_WATCHDOG_TIMER 0x07
88 #define NVSRAM3_RTC_REG_CALIBRATION_CONTROL 0x08
89 #define NVSRAM3_RTC_REG_TIME_KEEPING_SECONDS 0x09
90 #define NVSRAM3_RTC_REG_TIME_KEEPING_MINUTES 0x0A
91 #define NVSRAM3_RTC_REG_TIME_KEEPING_HOURS 0x0B
92 #define NVSRAM3_RTC_REG_TIME_KEEPING_DAY 0x0C
93 #define NVSRAM3_RTC_REG_TIME_KEEPING_DATE 0x0D
94 #define NVSRAM3_RTC_REG_TIME_KEEPING_MONTHS 0x0E
95 #define NVSRAM3_RTC_REG_TIME_KEEPING_YEARS 0x0F
96  // nvsram3_reg
98 
113 #define NVSRAM3_SERIAL_NUMBER_LOCK 0x40
114 #define NVSRAM3_SERIAL_NUMBER_UNLOCK 0x00
115 #define NVSRAM3_BLOCK_PROTECT_QUARTER 0x04
116 #define NVSRAM3_BLOCK_PROTECT_HALF 0x08
117 #define NVSRAM3_BLOCK_PROTECT_FULL 0x0C
118 
119 #define NVSRAM3_STORE_COMMAND 0x3C
120 #define NVSRAM3_RECALL_COMMAND 0x60
121 #define NVSRAM3_ASENB_COMMAND 0x59
122 #define NVSRAM3_ASDISB_COMMAND 0x19
123 #define NVSRAM3_SLEEP_COMMAND 0xB9
124 
129 #define NVSRAM3_RTC_WRITE_ENABLE 0x02
130 #define NVSRAM3_RTC_WRITE_DISABLE 0x00
131 #define NVSRAM3_RTC_READ_ENABLE 0x01
132 #define NVSRAM3_RTC_READ_DISABLE 0x00
133 
139 #define NVSRAM3_MEMORY_DEV_ADDR_0 0x50
140 #define NVSRAM3_MEMORY_DEV_ADDR_1 0x52
141 #define NVSRAM3_MEMORY_DEV_ADDR_2 0x54
142 #define NVSRAM3_MEMORY_DEV_ADDR_3 0x56
143 
144 #define NVSRAM3_RTC_DEV_ADDR_0 0x68
145 #define NVSRAM3_RTC_DEV_ADDR_1 0x6A
146 #define NVSRAM3_RTC_DEV_ADDR_2 0x6C
147 #define NVSRAM3_RTC_DEV_ADDR_3 0x6E
148 
149 #define NVSRAM3_CONTROL_DEV_ADDR_0 0x18
150 #define NVSRAM3_CONTROL_DEV_ADDR_1 0x1A
151 #define NVSRAM3_CONTROL_DEV_ADDR_2 0x1C
152 #define NVSRAM3_CONTROL_DEV_ADDR_3 0x1E
153 
159 #define NVSRAM3_HSB_ENABLE 0x00
160 #define NVSRAM3_HSB_DISABLE 0x01
161 
162 #define NVSRAM3_WP_ENABLE 0x00
163 #define NVSRAM3_WP_DISABLE 0x01
164  // nvsram3_set
166 
177 #define NVSRAM3_SUCCESS 0
178 #define NVSRAM3_ERROR -1
179 
180  // status
182 
197 #define NVSRAM3_MAP_MIKROBUS( cfg, mikrobus ) \
198  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
199  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
200  cfg.hsb = MIKROBUS( mikrobus, MIKROBUS_RST ); \
201  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
202  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
203  // nvsram3_map // nvsram3
206 
211 typedef struct
212 {
213  // Output pins
214 
215  digital_out_t hsb;
216  digital_out_t wp;
218  // Input pins
219 
220  digital_in_t int_pin;
222  // Modules
223 
224  i2c_master_t i2c;
226  // I2C slave address
227 
228  uint8_t slave_address;
230 } nvsram3_t;
231 
236 typedef struct
237 {
238  pin_name_t scl;
239  pin_name_t sda;
241  pin_name_t hsb;
242  pin_name_t wp;
243  pin_name_t int_pin;
245  uint32_t i2c_speed;
246  uint8_t i2c_address;
248 } nvsram3_cfg_t;
249 
250 typedef struct
251 {
252 
253  uint8_t hours;
254  uint8_t min;
255  uint8_t sec;
256 
258 
259 typedef struct
260 {
261 
262  uint16_t year;
263  uint8_t month;
264  uint8_t day;
265  uint8_t day_of_week;
267 
268 typedef struct
269 {
270 
271  uint8_t day;
272  uint8_t hours;
273  uint8_t min;
274  uint8_t sec;
275 
277 
296 
313 err_t nvsram3_init ( nvsram3_t *ctx, nvsram3_cfg_t *cfg );
314 
331 
349 err_t nvsram3_generic_write ( nvsram3_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
350 
368 err_t nvsram3_generic_read ( nvsram3_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
369 
382 err_t nvsram3_send_cmd ( nvsram3_t *ctx, uint8_t cmd );
383 
399 err_t nvsram3_memory_write ( nvsram3_t *ctx, uint32_t mem_addr, uint8_t *data_in, uint8_t n_bytes );
400 
416 err_t nvsram3_memory_read ( nvsram3_t *ctx, uint32_t mem_addr, uint8_t *data_out, uint8_t n_bytes );
417 
428 
442 err_t nvsram3_rtc_read_reg ( nvsram3_t *ctx, uint8_t rtc_reg, uint8_t *data_out );
443 
457 err_t nvsram3_rtc_write_reg ( nvsram3_t *ctx, uint8_t rtc_reg, uint8_t data_in ) ;
458 
471 
485 
498 
512 
525 
539 
555 err_t nvsram3_hardware_store ( nvsram3_t *ctx, uint8_t state );
556 
572 err_t nvsram3_hw_write_protection ( nvsram3_t *ctx, uint8_t state );
573 
574 #ifdef __cplusplus
575 }
576 #endif
577 #endif // NVSRAM3_H
578  // nvsram3
580 
581 // ------------------------------------------------------------------------ END
nvsram3_set_rtc_alarm
err_t nvsram3_set_rtc_alarm(nvsram3_t *ctx, nvsram3_rtc_alarm_t rtc_alarm)
nvSRAM 3 set RTC alarm function.
nvsram3_hw_write_protection
err_t nvsram3_hw_write_protection(nvsram3_t *ctx, uint8_t state)
nvSRAM 3 write protection function.
nvsram3_send_cmd
err_t nvsram3_send_cmd(nvsram3_t *ctx, uint8_t cmd)
nvSRAM 3 send command function.
nvsram3_cfg_t::sda
pin_name_t sda
Definition: nvsram3.h:239
nvsram3_get_rtc_time
void nvsram3_get_rtc_time(nvsram3_t *ctx, nvsram3_rtc_time_t *rtc_time)
nvSRAM 3 get RTC time function.
nvsram3_init
err_t nvsram3_init(nvsram3_t *ctx, nvsram3_cfg_t *cfg)
nvSRAM 3 initialization function.
nvsram3_rtc_alarm_t::min
uint8_t min
Definition: nvsram3.h:273
nvsram3_default_cfg
err_t nvsram3_default_cfg(nvsram3_t *ctx)
nvSRAM 3 default configuration function.
nvsram3_t::wp
digital_out_t wp
Definition: nvsram3.h:216
nvsram3_rtc_time_t::min
uint8_t min
Definition: nvsram3.h:254
nvsram3_rtc_read_reg
err_t nvsram3_rtc_read_reg(nvsram3_t *ctx, uint8_t rtc_reg, uint8_t *data_out)
nvSRAM 3 RTC read register function.
nvsram3_memory_write
err_t nvsram3_memory_write(nvsram3_t *ctx, uint32_t mem_addr, uint8_t *data_in, uint8_t n_bytes)
nvSRAM 3 write memory function.
nvsram3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: nvsram3.h:245
nvsram3_rtc_time_t
Definition: nvsram3.h:251
nvsram3_rtc_write_reg
err_t nvsram3_rtc_write_reg(nvsram3_t *ctx, uint8_t rtc_reg, uint8_t data_in)
nvSRAM 3 RTC write register function.
nvsram3_rtc_date_t::day
uint8_t day
Definition: nvsram3.h:264
nvsram3_t
nvSRAM 3 Click context object.
Definition: nvsram3.h:212
nvsram3_rtc_alarm_t
Definition: nvsram3.h:269
nvsram3_t::hsb
digital_out_t hsb
Definition: nvsram3.h:215
nvsram3_t::slave_address
uint8_t slave_address
Definition: nvsram3.h:228
nvsram3_memory_read
err_t nvsram3_memory_read(nvsram3_t *ctx, uint32_t mem_addr, uint8_t *data_out, uint8_t n_bytes)
nvSRAM 3 read memory function.
nvsram3_rtc_alarm_t::hours
uint8_t hours
Definition: nvsram3.h:272
nvsram3_rtc_date_t::year
uint16_t year
Definition: nvsram3.h:262
nvsram3_get_rtc_date
void nvsram3_get_rtc_date(nvsram3_t *ctx, nvsram3_rtc_date_t *rtc_date)
nvSRAM 3 get RTC date function.
nvsram3_set_rtc_date
err_t nvsram3_set_rtc_date(nvsram3_t *ctx, nvsram3_rtc_date_t rtc_date)
nvSRAM 3 set RTC date function.
nvsram3_rtc_date_t::day_of_week
uint8_t day_of_week
Definition: nvsram3.h:265
nvsram3_cfg_setup
void nvsram3_cfg_setup(nvsram3_cfg_t *cfg)
nvSRAM 3 configuration object setup function.
nvsram3_rtc_time_t::sec
uint8_t sec
Definition: nvsram3.h:255
nvsram3_cfg_t
nvSRAM 3 Click configuration object.
Definition: nvsram3.h:237
nvsram3_set_rtc_time
err_t nvsram3_set_rtc_time(nvsram3_t *ctx, nvsram3_rtc_time_t rtc_time)
nvSRAM 3 set RTC time function.
nvsram3_cfg_t::scl
pin_name_t scl
Definition: nvsram3.h:238
nvsram3_get_device_id
uint32_t nvsram3_get_device_id(nvsram3_t *ctx)
nvSRAM 3 get device ID function.
nvsram3_rtc_date_t::month
uint8_t month
Definition: nvsram3.h:263
nvsram3_cfg_t::i2c_address
uint8_t i2c_address
Definition: nvsram3.h:246
nvsram3_cfg_t::int_pin
pin_name_t int_pin
Definition: nvsram3.h:243
nvsram3_generic_write
err_t nvsram3_generic_write(nvsram3_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
nvSRAM 3 I2C writing function.
nvsram3_cfg_t::wp
pin_name_t wp
Definition: nvsram3.h:242
nvsram3_rtc_time_t::hours
uint8_t hours
Definition: nvsram3.h:253
nvsram3_t::i2c
i2c_master_t i2c
Definition: nvsram3.h:224
nvsram3_t::int_pin
digital_in_t int_pin
Definition: nvsram3.h:220
nvsram3_get_rtc_alarm
void nvsram3_get_rtc_alarm(nvsram3_t *ctx, nvsram3_rtc_alarm_t *rtc_alarm)
nvSRAM 3 get RTC alarm function.
nvsram3_rtc_alarm_t::sec
uint8_t sec
Definition: nvsram3.h:274
nvsram3_hardware_store
err_t nvsram3_hardware_store(nvsram3_t *ctx, uint8_t state)
nvSRAM 3 hardware store function.
nvsram3_rtc_alarm_t::day
uint8_t day
Definition: nvsram3.h:271
nvsram3_rtc_date_t
Definition: nvsram3.h:260
nvsram3_generic_read
err_t nvsram3_generic_read(nvsram3_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
nvSRAM 3 I2C reading function.
nvsram3_cfg_t::hsb
pin_name_t hsb
Definition: nvsram3.h:241