Blame view

RIOT/drivers/include/ds1307.h 4.06 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  /*
   * Copyright (C) 2017 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup    drivers_ds1307  DS1307 RTC
   * @ingroup     drivers_sensors
   * @brief       Device drive interface for the DS1307 real-time clock
   * @{
   *
   * @file
   * @brief       DS1307 definitions
   *
   * @author  Martine Lenders <m.lenders@fu-berlin.de>
   */
  #ifndef DS1307_H
  #define DS1307_H
  
  #include <stdbool.h>
  #include <time.h>
  
  #include "nvram.h"
  #include "periph/i2c.h"
  #include "periph/gpio.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @brief   I2C address of DS1307 RTC
   */
  #ifndef DS1307_I2C_ADDRESS
  #define DS1307_I2C_ADDRESS      (0x68)
  #endif
  
  /**
   * @brief   Maximum I2C bus speed to use with the device
   */
  #ifndef DS1307_I2C_MAX_CLK
  #define DS1307_I2C_MAX_CLK      (I2C_SPEED_FAST)
  #endif
  
  /**
   * @brief   Maximum size in byte of on-chip NVRAM
   */
  #define DS1307_NVRAM_MAX_SIZE   (56U)
  
  /**
   * @brief   Device descriptor for DS1307 devices
   */
  typedef struct {
      i2c_t i2c;                  /**< I2C bus the device is connected to */
      nvram_t nvram;              /**< on-chip NVRAM (see nvram.h) */
  } ds1307_t;
  
  /**
   * @brief   Set of configuration parameters for DS1307 devices
   */
  typedef struct {
      i2c_t i2c;                  /**< I2C bus the device is connected to */
      i2c_speed_t clk;            /**< clock speed to use on the I2C bus */
  } ds1307_params_t;
  
  /**
   * @brief   Modes for the DS1307 square wave / output driver
   */
  typedef enum {
      DS1307_SQW_MODE_0 = 0x00,       /**< OUT: 0 */
      DS1307_SQW_MODE_1000HZ = 0x10,  /**< SQW: 1kHz */
      DS1307_SQW_MODE_4096HZ = 0x11,  /**< SQW: 4.096 kHz */
      DS1307_SQW_MODE_8192HZ = 0x12,  /**< SQW: 8.192 kHz */
      DS1307_SQW_MODE_32768HZ = 0x13, /**< SQW: 32.768 kHz */
      DS1307_SQW_MODE_1 = 0x80,       /**< OUT: 1 */
  } ds1307_sqw_mode_t;
  
  /**
   * @brief   Initialize the given DS1307 device
   *
   * @param[out] dev      device descriptor of the targeted device
   * @param[in] params    device configuration (i2c bus, address and bus clock)
   *
   * @return      0 on success
   * @return      < 0 if unable to speak to the device
   */
  int ds1307_init(ds1307_t *dev, const ds1307_params_t *params);
  
  /**
   * @brief   Set RTC to a given time.
   *
   * @param[in] dev       device descriptor of the targeted device
   * @param[in] time      pointer to the struct holding the time to set.
   *
   * @return      0 on success
   * @return      < 0 if unable to speak to the device
   */
  int ds1307_set_time(const ds1307_t *dev, const struct tm *time);
  
  /**
   * @brief   Get current RTC time.
   *
   * @param[in] dev       device descriptor of the targeted device
   * @param[out] time     pointer to the struct to write the time to.
   *
   * @return      0 on success
   * @return      < 0 if unable to speak to the device
   */
  int ds1307_get_time(const ds1307_t *dev, struct tm *time);
  
  /**
   * @brief   Halt clock
   *
   * @note    Can be reversed using @ref ds1307_set_time()
   *
   * @param[in] dev       device descriptor of the targeted device
   *
   * @return      0 on success
   * @return      < 0 if unable to speak to the device
   */
  int ds1307_halt(const ds1307_t *dev);
  
  /**
   * @brief   Set mode of square wave / output driver
   *
   * @note    To get the actual output of the driver, attach the pin via GPIO
   *
   * @param[in] dev       device descriptor of the targeted device
   * @param[in] mode      mode for the square wave / output driver
   *
   * @return      0 on success
   * @return      < 0 if unable to speak to the device
   */
  int ds1307_set_sqw_mode(const ds1307_t *dev, ds1307_sqw_mode_t mode);
  
  /**
   * @brief   Get current mode of square wave / output driver
   *
   * @note    To get the actual output of the driver, attach the pin via GPIO
   *
   * @param[in] dev       device descriptor of the targeted device
   *
   * @return  current mode of the square wave / output driver
   *          (see ds1307_sqw_mode_t)
   * @return  < 0 if unable to speak to the device
   */
  int ds1307_get_sqw_mode(const ds1307_t *dev);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* DS1307_H */
  /** @} */