ds3231.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. use field_offset::offset_of;
  2. use stm32l4xx_hal::prelude::{_embedded_hal_blocking_i2c_Read, _embedded_hal_blocking_i2c_Write};
  3. use tock_registers::{
  4. interfaces::{ReadWriteable, Readable},
  5. register_bitfields, register_structs,
  6. registers::InMemoryRegister,
  7. };
  8. register_bitfields![
  9. u8,
  10. T_SECOND [
  11. Value OFFSET(0) NUMBITS(7) [],
  12. ],
  13. T_MINUTE [
  14. Value OFFSET(0) NUMBITS(7) [],
  15. ],
  16. T_HOUR [
  17. Value OFFSET(0) NUMBITS(6) [],
  18. n24 OFFSET(6) NUMBITS(1) [],
  19. ],
  20. T_DAY [
  21. Value OFFSET(0) NUMBITS(3) [],
  22. ],
  23. T_DATE [
  24. Value OFFSET(0) NUMBITS(6) [],
  25. ],
  26. T_MONTH [
  27. Value OFFSET(0) NUMBITS(5) [],
  28. Century OFFSET(7) NUMBITS(1) [],
  29. ],
  30. T_YEAR [
  31. Value OFFSET(0) NUMBITS(8) [],
  32. ],
  33. A_SECOND [
  34. Value OFFSET(0) NUMBITS(7) [],
  35. M1 OFFSET(7) NUMBITS(1) [],
  36. ],
  37. A_MINUTE [
  38. Value OFFSET(0) NUMBITS(7) [],
  39. M2 OFFSET(7) NUMBITS(1) [],
  40. ],
  41. A_HOUR [
  42. Value OFFSET(0) NUMBITS(6) [],
  43. n24 OFFSET(6) NUMBITS(1) [],
  44. M3 OFFSET(7) NUMBITS(1) [],
  45. ],
  46. A_DAY_DATE [
  47. Value OFFSET(0) NUMBITS(6) [],
  48. nDT OFFSET(6) NUMBITS(1) [],
  49. M4 OFFSET(7) NUMBITS(1) [],
  50. ],
  51. CONTROL_1 [
  52. A1IE 0,
  53. A2IE 1,
  54. INTCN 2,
  55. RS1 3,
  56. RS2 4,
  57. CONV 5,
  58. BBSQW 6,
  59. nEOSC 7,
  60. ],
  61. CONTROL_2 [
  62. A1F 0,
  63. A2F 1,
  64. BSY 2,
  65. EN32KHZ 3,
  66. OSF 7,
  67. ],
  68. AGING_OFFSET [
  69. Value OFFSET(0) NUMBITS(7) [],
  70. Sign OFFSET(7) NUMBITS(1) [],
  71. ],
  72. TEMP_MSB [
  73. Value OFFSET(0) NUMBITS(7) [],
  74. Sign OFFSET(7) NUMBITS(1) [],
  75. ],
  76. TEMP_LSB [
  77. Value OFFSET(6) NUMBITS(2) [],
  78. ],
  79. ];
  80. register_structs! {
  81. Ds3231Registers {
  82. (0x00 => t_second: InMemoryRegister<u8, T_SECOND::Register>),
  83. (0x01 => t_minute: InMemoryRegister<u8, T_MINUTE::Register>),
  84. (0x02 => t_hour: InMemoryRegister<u8, T_HOUR::Register>),
  85. (0x03 => t_weekday: InMemoryRegister<u8, T_DAY::Register>),
  86. (0x04 => t_day: InMemoryRegister<u8, T_DATE::Register>),
  87. (0x05 => t_month: InMemoryRegister<u8, T_MONTH::Register>),
  88. (0x06 => t_year: InMemoryRegister<u8, T_YEAR::Register>),
  89. (0x07 => a1_second: InMemoryRegister<u8, A_SECOND::Register>),
  90. (0x08 => a1_minute: InMemoryRegister<u8, A_MINUTE::Register>),
  91. (0x09 => a1_hour: InMemoryRegister<u8, A_HOUR::Register>),
  92. (0x0A => a1_day_date: InMemoryRegister<u8, A_DAY_DATE::Register>),
  93. (0x0B => a2_minute: InMemoryRegister<u8, A_MINUTE::Register>),
  94. (0x0C => a2_hour: InMemoryRegister<u8, A_HOUR::Register>),
  95. (0x0D => a2_day_date: InMemoryRegister<u8, A_DAY_DATE::Register>),
  96. (0x0E => control_1: InMemoryRegister<u8, CONTROL_1::Register>),
  97. (0x0F => control_2: InMemoryRegister<u8, CONTROL_2::Register>),
  98. (0x10 => aging_offset: InMemoryRegister<u8, AGING_OFFSET::Register>),
  99. (0x11 => temp_msb: InMemoryRegister<u8, TEMP_MSB::Register>),
  100. (0x12 => temp_lsb: InMemoryRegister<u8, TEMP_LSB::Register>),
  101. (0x13 => @END),
  102. }
  103. }
  104. pub enum Weekday {
  105. SUNDAY = 0,
  106. MONDAY = 1,
  107. TUESDAY = 2,
  108. WEDNESDAY = 3,
  109. THURSDAY = 4,
  110. FRIDAY = 5,
  111. SATURDAY = 6,
  112. }
  113. impl Ds3231Registers {
  114. fn i2c_read_regs(&mut self, i2c: &mut impl _embedded_hal_blocking_i2c_Read) {}
  115. fn set_time(second: &u32, minute: &u32, hour: &u32) {}
  116. fn set_date(weekday: &Weekday, day: &u32, month: &u32, year: &u32, century: &u32) {}
  117. fn get_time(second: &mut u32, minute: &mut u32, hour: &mut u32) {}
  118. fn get_date(
  119. weekday: &mut Weekday,
  120. day: &mut u32,
  121. month: &mut u32,
  122. year: &mut u32,
  123. century: &mut u32,
  124. ) {
  125. }
  126. }
  127. pub fn in_dst(weekday: Weekday, day: u32, month: u32, hour_24: u32) -> bool {
  128. let prev_sunday: i32 = day as i32 - weekday as i32;
  129. match month {
  130. 0..=2 | 12.. => false,
  131. 4..=10 => true,
  132. 3 => match prev_sunday {
  133. ..8 => false,
  134. 15.. => true,
  135. d if d == day as i32 => hour_24 >= 2,
  136. _ => true,
  137. },
  138. 11 => match prev_sunday {
  139. ..=0 => true,
  140. 8.. => false,
  141. d if d == day as i32 => hour_24 < 2,
  142. _ => false,
  143. },
  144. }
  145. }
  146. #[cfg(test)]
  147. mod test {
  148. use super::*;
  149. #[test]
  150. fn dst_test() {
  151. // 2020 - begins Mar 8th @ 2AM (Sunday), ends Nov 1st @ 2AM (Sunday)
  152. assert!(in_dst(Weekday::SUNDAY, 8, 3, 1) == false);
  153. assert!(in_dst(Weekday::SUNDAY, 8, 3, 2) == true);
  154. assert!(in_dst(Weekday::SUNDAY, 8, 3, 3) == true);
  155. assert!(in_dst(Weekday::SUNDAY, 1, 11, 1) == true);
  156. assert!(in_dst(Weekday::SUNDAY, 1, 11, 2) == false);
  157. assert!(in_dst(Weekday::SUNDAY, 1, 11, 3) == false);
  158. // 2021 - begins Mar 14th @ 2AM (Sunday), ends Nov 7th @ 2AM (Sunday)
  159. assert!(in_dst(Weekday::SUNDAY, 14, 3, 1) == false);
  160. assert!(in_dst(Weekday::SUNDAY, 14, 3, 2) == true);
  161. assert!(in_dst(Weekday::SUNDAY, 14, 3, 3) == true);
  162. assert!(in_dst(Weekday::SUNDAY, 7, 11, 1) == true);
  163. assert!(in_dst(Weekday::SUNDAY, 7, 11, 2) == false);
  164. assert!(in_dst(Weekday::SUNDAY, 7, 11, 3) == false);
  165. // 2022 - begins Mar 13th @ 2AM (Sunday), ends Nov 6th @ 2AM (Sunday)
  166. assert!(in_dst(Weekday::SUNDAY, 13, 3, 1) == false);
  167. assert!(in_dst(Weekday::SUNDAY, 13, 3, 2) == true);
  168. assert!(in_dst(Weekday::SUNDAY, 13, 3, 3) == true);
  169. assert!(in_dst(Weekday::SUNDAY, 6, 11, 1) == true);
  170. assert!(in_dst(Weekday::SUNDAY, 6, 11, 2) == false);
  171. assert!(in_dst(Weekday::SUNDAY, 6, 11, 3) == false);
  172. // 2023 - begins Mar 12th @ 2AM (Sunday), ends Nov 5th @ 2AM (Sunday)
  173. assert!(in_dst(Weekday::SUNDAY, 12, 3, 1) == false);
  174. assert!(in_dst(Weekday::SUNDAY, 12, 3, 2) == true);
  175. assert!(in_dst(Weekday::SUNDAY, 12, 3, 3) == true);
  176. assert!(in_dst(Weekday::SUNDAY, 5, 11, 1) == true);
  177. assert!(in_dst(Weekday::SUNDAY, 5, 11, 2) == false);
  178. assert!(in_dst(Weekday::SUNDAY, 5, 11, 3) == false);
  179. // Sanity check other dates in 2021
  180. assert!(in_dst(Weekday::FRIDAY, 1, 1, 0) == false);
  181. assert!(in_dst(Weekday::MONDAY, 1, 2, 0) == false);
  182. assert!(in_dst(Weekday::MONDAY, 1, 3, 0) == false);
  183. assert!(in_dst(Weekday::THURSDAY, 1, 4, 0) == true);
  184. assert!(in_dst(Weekday::SATURDAY, 1, 5, 0) == true);
  185. assert!(in_dst(Weekday::TUESDAY, 1, 6, 0) == true);
  186. assert!(in_dst(Weekday::THURSDAY, 1, 7, 0) == true);
  187. assert!(in_dst(Weekday::SUNDAY, 1, 8, 0) == true);
  188. assert!(in_dst(Weekday::WEDNESDAY, 1, 9, 0) == true);
  189. assert!(in_dst(Weekday::FRIDAY, 1, 10, 0) == true);
  190. assert!(in_dst(Weekday::MONDAY, 1, 11, 0) == true);
  191. assert!(in_dst(Weekday::WEDNESDAY, 1, 12, 0) == false);
  192. }
  193. }