-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrtc.rs
More file actions
266 lines (246 loc) · 8.55 KB
/
Copy pathrtc.rs
File metadata and controls
266 lines (246 loc) · 8.55 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! RTC support for the Neotron Pico BIOS
//!
//! We could have one of two RTCs fitted. This module abstracts that away.
// -----------------------------------------------------------------------------
// Licence Statement
// -----------------------------------------------------------------------------
// Copyright (c) Jonathan 'theJPster' Pallant and the Neotron Developers, 2023
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Sub-modules
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Imports
// -----------------------------------------------------------------------------
pub use ds1307::{DateTimeAccess, NaiveDateTime};
// -----------------------------------------------------------------------------
// Types
// -----------------------------------------------------------------------------
/// The ways this module can fail
pub enum Error<E> {
Bus(E),
DriverBug,
NoRtcFound,
}
/// The kinds of RTC we support.
pub enum Rtc {
Ds1307,
Mcp7940n,
None,
}
impl Rtc {
/// Create a new RTC object.
///
/// The `bus` is dropped at the end of the function, so use a `&mut`
/// reference, or a disposable shared-bus proxy.
pub fn new<T, E>(bus: T) -> Rtc
where
T: embedded_hal::blocking::i2c::WriteRead<Error = E>
+ embedded_hal::blocking::i2c::Write<Error = E>,
{
let mut mcp7940n = mcp794xx::Mcp794xx::new_mcp7940n(bus);
match mcp7940n.is_oscillator_running() {
Ok(true) => {
defmt::info!("MCP7940N found and ticking");
return Rtc::Mcp7940n;
}
Ok(false) => {
defmt::info!("MCP7940N found, and has started ticking");
let _ = mcp7940n.enable_external_oscillator();
let _ = mcp7940n.enable();
return Rtc::Mcp7940n;
}
Err(_e) => defmt::info!("MCP7940N not found"),
}
let bus = mcp7940n.destroy();
let mut ds1307 = ds1307::Ds1307::new(bus);
match ds1307.running() {
Ok(true) => {
defmt::info!("DS1307 found and ticking");
return Rtc::Ds1307;
}
Ok(false) => {
defmt::info!("DS1307 found, and has started ticking");
let _ = ds1307.set_running();
return Rtc::Ds1307;
}
Err(_e) => defmt::info!("DS1307 not found"),
}
Rtc::None
}
/// Get the current wall-time.
///
/// We don't care about timezones - this is basic Gregorian naive wall-clock date/time.
///
/// You get `Err()` if the RTC doesn't respond or wasn't found on start-up.
pub fn get_time<T, E>(&mut self, bus: T) -> Result<NaiveDateTime, Error<E>>
where
T: embedded_hal::blocking::i2c::WriteRead<Error = E>
+ embedded_hal::blocking::i2c::Write<Error = E>,
{
match self {
Self::Ds1307 => {
let mut driver = ds1307::Ds1307::new(bus);
driver.datetime().map_err(|e| match e {
ds1307::Error::I2C(bus_error) => Error::Bus(bus_error),
ds1307::Error::InvalidInputData => Error::DriverBug,
})
}
Self::Mcp7940n => {
let mut driver = mcp794xx::Mcp794xx::new_mcp7940n(bus);
driver.datetime().map_err(|e| match e {
mcp794xx::Error::Comm(bus_error) => Error::Bus(bus_error),
mcp794xx::Error::InvalidInputData => Error::DriverBug,
})
}
Self::None => Err(Error::NoRtcFound),
}
}
/// Set the current wall-time
///
/// We don't care about timezones - this is basic Gregorian naive wall-clock date/time.
///
/// You get `Err()` if the RTC doesn't respond or wasn't found on start-up.
pub fn set_time<T, E>(&mut self, bus: T, new_time: NaiveDateTime) -> Result<(), Error<E>>
where
T: embedded_hal::blocking::i2c::WriteRead<Error = E>
+ embedded_hal::blocking::i2c::Write<Error = E>,
{
match self {
Self::Ds1307 => {
let mut driver = ds1307::Ds1307::new(bus);
driver.set_datetime(&new_time).map_err(|e| match e {
ds1307::Error::I2C(bus_error) => Error::Bus(bus_error),
ds1307::Error::InvalidInputData => Error::DriverBug,
})
}
Self::Mcp7940n => {
let mut driver = mcp794xx::Mcp794xx::new_mcp7940n(bus);
driver.set_datetime(&new_time).map_err(|e| match e {
mcp794xx::Error::Comm(bus_error) => Error::Bus(bus_error),
mcp794xx::Error::InvalidInputData => Error::DriverBug,
})
}
Self::None => Err(Error::NoRtcFound),
}
}
/// Store configuration in the RTC's RAM
pub fn configuration_set<T, E>(&mut self, bus: T, data: &[u8]) -> Result<(), Error<E>>
where
T: embedded_hal::blocking::i2c::WriteRead<Error = E>
+ embedded_hal::blocking::i2c::Write<Error = E>,
{
match self {
Self::Ds1307 => {
let mut driver = ds1307::Ds1307::new(bus);
driver.write_ram(1, data).map_err(|e| match e {
ds1307::Error::I2C(bus_error) => Error::Bus(bus_error),
ds1307::Error::InvalidInputData => Error::DriverBug,
})?;
driver
.write_ram(0, &[data.len() as u8])
.map_err(|e| match e {
ds1307::Error::I2C(bus_error) => Error::Bus(bus_error),
ds1307::Error::InvalidInputData => Error::DriverBug,
})
}
Self::Mcp7940n => {
let mut driver = mcp794xx::Mcp794xx::new_mcp7940n(bus);
driver.write_sram_data(1, data).map_err(|e| match e {
mcp794xx::Error::Comm(bus_error) => Error::Bus(bus_error),
mcp794xx::Error::InvalidInputData => Error::DriverBug,
})?;
driver
.write_sram_byte(0, data.len() as u8)
.map_err(|e| match e {
mcp794xx::Error::Comm(bus_error) => Error::Bus(bus_error),
mcp794xx::Error::InvalidInputData => Error::DriverBug,
})
}
Self::None => Err(Error::NoRtcFound),
}
}
/// Get configuration from the RTC's RAM
pub fn configuration_get<T, E>(&mut self, bus: T, mut data: &mut [u8]) -> Result<u8, Error<E>>
where
T: embedded_hal::blocking::i2c::WriteRead<Error = E>
+ embedded_hal::blocking::i2c::Write<Error = E>,
{
match self {
Self::Ds1307 => {
let mut driver = ds1307::Ds1307::new(bus);
if data.len() > 55 {
// This chip can only read 56 bytes (inc our count byte)
data = &mut data[0..55];
}
let mut count = [0u8; 1];
driver.read_ram(0, &mut count).map_err(|e| match e {
ds1307::Error::I2C(bus_error) => Error::Bus(bus_error),
ds1307::Error::InvalidInputData => Error::DriverBug,
})?;
driver.read_ram(1, data).map_err(|e| match e {
ds1307::Error::I2C(bus_error) => Error::Bus(bus_error),
ds1307::Error::InvalidInputData => Error::DriverBug,
})?;
if usize::from(count[0]) <= data.len() {
Ok(count[0])
} else {
Ok(0)
}
}
Self::Mcp7940n => {
if data.len() > 63 {
// This chip can only read 64 bytes (inc our count byte)
data = &mut data[0..63];
}
let mut driver = mcp794xx::Mcp794xx::new_mcp7940n(bus);
let count = driver.read_sram_byte(0).map_err(|e| match e {
mcp794xx::Error::Comm(bus_error) => Error::Bus(bus_error),
mcp794xx::Error::InvalidInputData => Error::DriverBug,
})?;
driver.read_sram_data(1, data).map_err(|e| match e {
mcp794xx::Error::Comm(bus_error) => Error::Bus(bus_error),
mcp794xx::Error::InvalidInputData => Error::DriverBug,
})?;
if usize::from(count) <= data.len() {
Ok(count)
} else {
Ok(0)
}
}
Self::None => Err(Error::NoRtcFound),
}
}
/// Get the name of the RTC we found
///
/// Returns `None` if no RTC was found
pub fn get_kind(&self) -> Option<&'static str> {
match self {
Self::Ds1307 => Some("DS1307"),
Self::Mcp7940n => Some("MCP7940N"),
Self::None => None,
}
}
}
// -----------------------------------------------------------------------------
// Static and Const Data
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// End of file
// -----------------------------------------------------------------------------