ds3231.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. use field_offset::offset_of;
  2. use stm32l4xx_hal::prelude::{
  3. _embedded_hal_blocking_i2c_Write, _embedded_hal_blocking_i2c_WriteRead,
  4. };
  5. use tock_registers::{
  6. interfaces::{ReadWriteable, Readable, Writeable},
  7. register_bitfields, register_structs,
  8. registers::InMemoryRegister,
  9. };
  10. register_bitfields![
  11. u8,
  12. T_SECOND [
  13. Value OFFSET(0) NUMBITS(7) [],
  14. ],
  15. T_MINUTE [
  16. Value OFFSET(0) NUMBITS(7) [],
  17. ],
  18. T_HOUR [
  19. Value OFFSET(0) NUMBITS(6) [],
  20. n24 OFFSET(6) NUMBITS(1) [],
  21. ],
  22. T_WEEKDAY [
  23. Value OFFSET(0) NUMBITS(3) [],
  24. ],
  25. T_DAY [
  26. Value OFFSET(0) NUMBITS(6) [],
  27. ],
  28. T_MONTH [
  29. Value OFFSET(0) NUMBITS(5) [],
  30. Century OFFSET(7) NUMBITS(1) [],
  31. ],
  32. T_YEAR [
  33. Value OFFSET(0) NUMBITS(8) [],
  34. ],
  35. A_SECOND [
  36. Value OFFSET(0) NUMBITS(7) [],
  37. M1 OFFSET(7) NUMBITS(1) [],
  38. ],
  39. A_MINUTE [
  40. Value OFFSET(0) NUMBITS(7) [],
  41. M2 OFFSET(7) NUMBITS(1) [],
  42. ],
  43. A_HOUR [
  44. Value OFFSET(0) NUMBITS(6) [],
  45. n24 OFFSET(6) NUMBITS(1) [],
  46. M3 OFFSET(7) NUMBITS(1) [],
  47. ],
  48. A_DAY_DATE [
  49. Value OFFSET(0) NUMBITS(6) [],
  50. nDT OFFSET(6) NUMBITS(1) [],
  51. M4 OFFSET(7) NUMBITS(1) [],
  52. ],
  53. CONTROL_1 [
  54. A1IE 0,
  55. A2IE 1,
  56. INTCN 2,
  57. RS1 3,
  58. RS2 4,
  59. CONV 5,
  60. BBSQW 6,
  61. nEOSC 7,
  62. ],
  63. CONTROL_2 [
  64. A1F 0,
  65. A2F 1,
  66. BSY 2,
  67. EN32KHZ 3,
  68. OSF 7,
  69. ],
  70. AGING_OFFSET [
  71. Value OFFSET(0) NUMBITS(7) [],
  72. Sign OFFSET(7) NUMBITS(1) [],
  73. ],
  74. TEMP_MSB [
  75. Value OFFSET(0) NUMBITS(7) [],
  76. Sign OFFSET(7) NUMBITS(1) [],
  77. ],
  78. TEMP_LSB [
  79. Value OFFSET(6) NUMBITS(2) [],
  80. ],
  81. ];
  82. register_structs! {
  83. Ds3231Registers {
  84. (0x00 => second: InMemoryRegister<u8, T_SECOND::Register>),
  85. (0x01 => minute: InMemoryRegister<u8, T_MINUTE::Register>),
  86. (0x02 => hour: InMemoryRegister<u8, T_HOUR::Register>),
  87. (0x03 => weekday: InMemoryRegister<u8, T_WEEKDAY::Register>),
  88. (0x04 => day: InMemoryRegister<u8, T_DAY::Register>),
  89. (0x05 => month: InMemoryRegister<u8, T_MONTH::Register>),
  90. (0x06 => year: InMemoryRegister<u8, T_YEAR::Register>),
  91. (0x07 => a1_second: InMemoryRegister<u8, A_SECOND::Register>),
  92. (0x08 => a1_minute: InMemoryRegister<u8, A_MINUTE::Register>),
  93. (0x09 => a1_hour: InMemoryRegister<u8, A_HOUR::Register>),
  94. (0x0A => a1_day_date: InMemoryRegister<u8, A_DAY_DATE::Register>),
  95. (0x0B => a2_minute: InMemoryRegister<u8, A_MINUTE::Register>),
  96. (0x0C => a2_hour: InMemoryRegister<u8, A_HOUR::Register>),
  97. (0x0D => a2_day_date: InMemoryRegister<u8, A_DAY_DATE::Register>),
  98. (0x0E => control_1: InMemoryRegister<u8, CONTROL_1::Register>),
  99. (0x0F => control_2: InMemoryRegister<u8, CONTROL_2::Register>),
  100. (0x10 => aging_offset: InMemoryRegister<u8, AGING_OFFSET::Register>),
  101. (0x11 => temp_msb: InMemoryRegister<u8, TEMP_MSB::Register>),
  102. (0x12 => temp_lsb: InMemoryRegister<u8, TEMP_LSB::Register>),
  103. (0x13 => @END),
  104. }
  105. }
  106. pub enum Weekday {
  107. Sunday = 0,
  108. Monday = 1,
  109. Tuesday = 2,
  110. Wednesday = 3,
  111. Thursday = 4,
  112. Friday = 5,
  113. Saturday = 6,
  114. }
  115. impl From<u8> for Weekday {
  116. fn from(value: u8) -> Self {
  117. match value {
  118. 0 => Weekday::Sunday,
  119. 1 => Weekday::Monday,
  120. 2 => Weekday::Tuesday,
  121. 3 => Weekday::Wednesday,
  122. 4 => Weekday::Thursday,
  123. 5 => Weekday::Friday,
  124. 6 => Weekday::Saturday,
  125. _ => panic!(),
  126. }
  127. }
  128. }
  129. impl Ds3231Registers {
  130. const fn default() -> Self {
  131. Self {
  132. second: InMemoryRegister::new(0x00),
  133. minute: InMemoryRegister::new(0x00),
  134. hour: InMemoryRegister::new(0x00),
  135. weekday: InMemoryRegister::new(0x00),
  136. day: InMemoryRegister::new(0x00),
  137. month: InMemoryRegister::new(0x00),
  138. year: InMemoryRegister::new(0x00),
  139. a1_second: InMemoryRegister::new(0x00),
  140. a1_minute: InMemoryRegister::new(0x00),
  141. a1_hour: InMemoryRegister::new(0x00),
  142. a1_day_date: InMemoryRegister::new(0x00),
  143. a2_minute: InMemoryRegister::new(0x00),
  144. a2_hour: InMemoryRegister::new(0x00),
  145. a2_day_date: InMemoryRegister::new(0x00),
  146. control_1: InMemoryRegister::new(0x00),
  147. control_2: InMemoryRegister::new(0x00),
  148. aging_offset: InMemoryRegister::new(0x00),
  149. temp_msb: InMemoryRegister::new(0x00),
  150. temp_lsb: InMemoryRegister::new(0x00),
  151. }
  152. }
  153. fn i2c_write_ctrl(&self, address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
  154. let mut buffer: [u8; 3] = [0; 3];
  155. buffer[0] = offset_of!(Ds3231Registers => control_1).get_byte_offset() as u8;
  156. buffer[1] = self.control_1.get();
  157. buffer[2] = self.control_2.get();
  158. match i2c.write(address, &buffer) {
  159. Ok(_) => (),
  160. Err(_) => panic!(),
  161. }
  162. }
  163. fn i2c_write_time(&self, address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
  164. let mut buffer: [u8; 4] = [0; 4];
  165. buffer[0] = offset_of!(Ds3231Registers => second).get_byte_offset() as u8;
  166. buffer[1] = self.second.get();
  167. buffer[2] = self.minute.get();
  168. buffer[3] = self.hour.get();
  169. match i2c.write(address, &buffer) {
  170. Ok(_) => (),
  171. Err(_) => panic!(),
  172. }
  173. }
  174. fn i2c_write_date(&self, address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
  175. let mut buffer: [u8; 5] = [0; 5];
  176. buffer[0] = offset_of!(Ds3231Registers => weekday).get_byte_offset() as u8;
  177. buffer[1] = self.weekday.get();
  178. buffer[2] = self.day.get();
  179. buffer[3] = self.month.get();
  180. buffer[4] = self.year.get();
  181. match i2c.write(address, &buffer) {
  182. Ok(_) => (),
  183. Err(_) => panic!(),
  184. }
  185. }
  186. // Returns the second, minute, and hour from the RTC
  187. fn i2c_read_time(&mut self, address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_WriteRead) {
  188. let mut buffer: [u8; 3] = [0; 3];
  189. match i2c.write_read(
  190. address,
  191. &[offset_of!(Ds3231Registers => second).get_byte_offset() as u8],
  192. &mut buffer,
  193. ) {
  194. Ok(_) => (),
  195. Err(_) => panic!(),
  196. }
  197. }
  198. // Returns the weekday, day, month, year, and century from the RTC
  199. fn i2c_read_date(&mut self, address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_WriteRead) {
  200. let mut buffer: [u8; 4] = [0; 4];
  201. match i2c.write_read(
  202. address,
  203. &[offset_of!(Ds3231Registers => weekday).get_byte_offset() as u8],
  204. &mut buffer,
  205. ) {
  206. Ok(_) => (),
  207. Err(_) => panic!(),
  208. }
  209. }
  210. }
  211. pub fn init(address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
  212. let regs = Ds3231Registers::default();
  213. regs.control_1.modify(
  214. CONTROL_1::nEOSC::CLEAR // Enable internal oscillator on VBAT
  215. + CONTROL_1::BBSQW::CLEAR // Disable outputs on VBAT
  216. + CONTROL_1::RS1::CLEAR // Set square wave output to 1Hz
  217. + CONTROL_1::RS2::CLEAR // Set square wave output to 1Hz
  218. + CONTROL_1::INTCN::CLEAR // Enable square wave output
  219. + CONTROL_1::A1IE::CLEAR // Disable alarm 1
  220. + CONTROL_1::A2IE::CLEAR, // Disable alarm 2
  221. );
  222. // Disable 32kHz output
  223. regs.control_2.modify(CONTROL_2::EN32KHZ::CLEAR);
  224. regs.i2c_write_ctrl(address, i2c);
  225. }
  226. pub fn set_time(
  227. address: u8,
  228. i2c: &mut impl _embedded_hal_blocking_i2c_Write,
  229. second: u32,
  230. minute: u32,
  231. hour: u32,
  232. ) {
  233. let regs = Ds3231Registers::default();
  234. regs.second
  235. .write(T_SECOND::Value.val(decimal_to_bcd(second)));
  236. regs.minute
  237. .write(T_MINUTE::Value.val(decimal_to_bcd(minute)));
  238. regs.hour.write(T_HOUR::Value.val(decimal_to_bcd(hour)));
  239. regs.i2c_write_time(address, i2c);
  240. }
  241. pub fn set_date(
  242. address: u8,
  243. i2c: &mut impl _embedded_hal_blocking_i2c_Write,
  244. weekday: Weekday,
  245. day: u32,
  246. month: u32,
  247. year: u32,
  248. century: u32,
  249. ) {
  250. let regs = Ds3231Registers::default();
  251. regs.weekday
  252. .write(T_WEEKDAY::Value.val(decimal_to_bcd(weekday as u32)));
  253. regs.day.write(T_DAY::Value.val(decimal_to_bcd(day)));
  254. regs.month.modify(
  255. T_MONTH::Value.val(decimal_to_bcd(month)) + T_MONTH::Century.val(decimal_to_bcd(century)),
  256. );
  257. regs.year.write(T_YEAR::Value.val(decimal_to_bcd(year)));
  258. regs.i2c_write_time(address, i2c);
  259. }
  260. pub fn get_time(
  261. address: u8,
  262. i2c: &mut impl _embedded_hal_blocking_i2c_WriteRead,
  263. ) -> (u32, u32, u32) {
  264. let mut regs = Ds3231Registers::default();
  265. regs.i2c_read_time(address, i2c);
  266. let second = bcd_to_decimal(regs.second.read(T_SECOND::Value));
  267. let minute = bcd_to_decimal(regs.minute.read(T_MINUTE::Value));
  268. let hour = bcd_to_decimal(regs.hour.read(T_HOUR::Value));
  269. (second, minute, hour)
  270. }
  271. pub fn get_date(
  272. address: u8,
  273. i2c: &mut impl _embedded_hal_blocking_i2c_WriteRead,
  274. ) -> (Weekday, u32, u32, u32, u32) {
  275. let mut regs = Ds3231Registers::default();
  276. regs.i2c_read_date(address, i2c);
  277. let weekday: Weekday = regs.weekday.read(T_WEEKDAY::Value).into();
  278. let day = bcd_to_decimal(regs.day.read(T_DAY::Value));
  279. let month = bcd_to_decimal(regs.month.read(T_MONTH::Value));
  280. let year = bcd_to_decimal(regs.year.read(T_YEAR::Value));
  281. let century = bcd_to_decimal(regs.month.read(T_MONTH::Century));
  282. (weekday, day, month, year, century)
  283. }
  284. #[inline]
  285. fn bcd_to_decimal(value: u8) -> u32 {
  286. (((value >> 4) * 10) + (value & 0xF)) as u32
  287. }
  288. #[inline]
  289. fn decimal_to_bcd(value: u32) -> u8 {
  290. (((value / 10) << 4) | (value % 10)) as u8
  291. }
  292. pub fn in_dst(weekday: Weekday, day: u32, month: u32, hour_24: u32) -> bool {
  293. let prev_sunday: i32 = day as i32 - weekday as i32;
  294. match month {
  295. 0..=2 | 12.. => false,
  296. 4..=10 => true,
  297. 3 => match prev_sunday {
  298. ..8 => false,
  299. 15.. => true,
  300. d if d == day as i32 => hour_24 >= 2,
  301. _ => true,
  302. },
  303. 11 => match prev_sunday {
  304. ..=0 => true,
  305. 8.. => false,
  306. d if d == day as i32 => hour_24 < 2,
  307. _ => false,
  308. },
  309. }
  310. }
  311. #[cfg(test)]
  312. mod test {
  313. use super::*;
  314. #[test]
  315. fn dst_test() {
  316. // 2020 - begins Mar 8th @ 2AM (Sunday), ends Nov 1st @ 2AM (Sunday)
  317. assert!(in_dst(Weekday::Sunday, 8, 3, 1) == false);
  318. assert!(in_dst(Weekday::Sunday, 8, 3, 2) == true);
  319. assert!(in_dst(Weekday::Sunday, 8, 3, 3) == true);
  320. assert!(in_dst(Weekday::Sunday, 1, 11, 1) == true);
  321. assert!(in_dst(Weekday::Sunday, 1, 11, 2) == false);
  322. assert!(in_dst(Weekday::Sunday, 1, 11, 3) == false);
  323. // 2021 - begins Mar 14th @ 2AM (Sunday), ends Nov 7th @ 2AM (Sunday)
  324. assert!(in_dst(Weekday::Sunday, 14, 3, 1) == false);
  325. assert!(in_dst(Weekday::Sunday, 14, 3, 2) == true);
  326. assert!(in_dst(Weekday::Sunday, 14, 3, 3) == true);
  327. assert!(in_dst(Weekday::Sunday, 7, 11, 1) == true);
  328. assert!(in_dst(Weekday::Sunday, 7, 11, 2) == false);
  329. assert!(in_dst(Weekday::Sunday, 7, 11, 3) == false);
  330. // 2022 - begins Mar 13th @ 2AM (Sunday), ends Nov 6th @ 2AM (Sunday)
  331. assert!(in_dst(Weekday::Sunday, 13, 3, 1) == false);
  332. assert!(in_dst(Weekday::Sunday, 13, 3, 2) == true);
  333. assert!(in_dst(Weekday::Sunday, 13, 3, 3) == true);
  334. assert!(in_dst(Weekday::Sunday, 6, 11, 1) == true);
  335. assert!(in_dst(Weekday::Sunday, 6, 11, 2) == false);
  336. assert!(in_dst(Weekday::Sunday, 6, 11, 3) == false);
  337. // 2023 - begins Mar 12th @ 2AM (Sunday), ends Nov 5th @ 2AM (Sunday)
  338. assert!(in_dst(Weekday::Sunday, 12, 3, 1) == false);
  339. assert!(in_dst(Weekday::Sunday, 12, 3, 2) == true);
  340. assert!(in_dst(Weekday::Sunday, 12, 3, 3) == true);
  341. assert!(in_dst(Weekday::Sunday, 5, 11, 1) == true);
  342. assert!(in_dst(Weekday::Sunday, 5, 11, 2) == false);
  343. assert!(in_dst(Weekday::Sunday, 5, 11, 3) == false);
  344. // Sanity check other dates in 2021
  345. assert!(in_dst(Weekday::Friday, 1, 1, 0) == false);
  346. assert!(in_dst(Weekday::Monday, 1, 2, 0) == false);
  347. assert!(in_dst(Weekday::Monday, 1, 3, 0) == false);
  348. assert!(in_dst(Weekday::Thursday, 1, 4, 0) == true);
  349. assert!(in_dst(Weekday::Saturday, 1, 5, 0) == true);
  350. assert!(in_dst(Weekday::Tuesday, 1, 6, 0) == true);
  351. assert!(in_dst(Weekday::Thursday, 1, 7, 0) == true);
  352. assert!(in_dst(Weekday::Sunday, 1, 8, 0) == true);
  353. assert!(in_dst(Weekday::Wednesday, 1, 9, 0) == true);
  354. assert!(in_dst(Weekday::Friday, 1, 10, 0) == true);
  355. assert!(in_dst(Weekday::Monday, 1, 11, 0) == true);
  356. assert!(in_dst(Weekday::Wednesday, 1, 12, 0) == false);
  357. }
  358. }