|
| 1 | +use critical_section::CriticalSection; |
| 2 | + |
| 3 | +use super::channel; |
| 4 | + |
| 5 | +/// Unit number |
| 6 | +#[derive(PartialEq, Eq, Copy, Clone, Debug)] |
| 7 | +pub enum Number { |
| 8 | + Unit0, |
| 9 | + Unit1, |
| 10 | + Unit2, |
| 11 | + Unit3, |
| 12 | + #[cfg(esp32)] |
| 13 | + Unit4, |
| 14 | + #[cfg(esp32)] |
| 15 | + Unit5, |
| 16 | + #[cfg(esp32)] |
| 17 | + Unit6, |
| 18 | + #[cfg(esp32)] |
| 19 | + Unit7, |
| 20 | +} |
| 21 | + |
| 22 | +/// Unit errors |
| 23 | +#[derive(Debug)] |
| 24 | +pub enum Error { |
| 25 | + /// Invalid filter threshold value |
| 26 | + InvalidFilterThresh, |
| 27 | + /// Invalid low limit - must be < 0 |
| 28 | + InvalidLowLimit, |
| 29 | + /// Invalid high limit - must be > 0 |
| 30 | + InvalidHighLimit, |
| 31 | +} |
| 32 | + |
| 33 | +/// the current status of the counter. |
| 34 | +#[derive(Copy, Clone, Debug, Default)] |
| 35 | +pub enum ZeroMode { |
| 36 | + /// pulse counter decreases from positive to 0. |
| 37 | + #[default] |
| 38 | + PosZero = 0, |
| 39 | + /// pulse counter increases from negative to 0 |
| 40 | + NegZero = 1, |
| 41 | + /// pulse counter is negative (not implemented?) |
| 42 | + Negitive = 2, |
| 43 | + /// pulse counter is positive (not implemented?) |
| 44 | + Positive = 3, |
| 45 | +} |
| 46 | + |
| 47 | +impl From<u8> for ZeroMode { |
| 48 | + fn from(value: u8) -> Self { |
| 49 | + match value { |
| 50 | + 0 => Self::PosZero, |
| 51 | + 1 => Self::NegZero, |
| 52 | + 2 => Self::Negitive, |
| 53 | + 3 => Self::Positive, |
| 54 | + _ => unreachable!(), // TODO: is this good enoough? should we use some default? |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Events |
| 60 | +#[derive(Copy, Clone, Debug, Default)] |
| 61 | +pub struct Events { |
| 62 | + pub low_limit: bool, |
| 63 | + pub high_limit: bool, |
| 64 | + pub thresh0: bool, |
| 65 | + pub thresh1: bool, |
| 66 | + pub zero: bool, |
| 67 | +} |
| 68 | + |
| 69 | +/// Unit configuration |
| 70 | +#[derive(Copy, Clone, Default)] |
| 71 | +pub struct Config { |
| 72 | + pub low_limit: i16, |
| 73 | + pub high_limit: i16, |
| 74 | + pub thresh0: i16, |
| 75 | + pub thresh1: i16, |
| 76 | + pub filter: Option<u16>, |
| 77 | +} |
| 78 | + |
| 79 | +pub struct Unit { |
| 80 | + number: Number, |
| 81 | +} |
| 82 | + |
| 83 | +impl Unit { |
| 84 | + /// return a new Unit |
| 85 | + pub(super) fn new(number: Number) -> Self { |
| 86 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 87 | + let conf0 = match number { |
| 88 | + Number::Unit0 => &pcnt.u0_conf0, |
| 89 | + Number::Unit1 => &pcnt.u1_conf0, |
| 90 | + Number::Unit2 => &pcnt.u2_conf0, |
| 91 | + Number::Unit3 => &pcnt.u3_conf0, |
| 92 | + #[cfg(esp32)] |
| 93 | + Number::Unit4 => &pcnt.u4_conf0, |
| 94 | + #[cfg(esp32)] |
| 95 | + Number::Unit5 => &pcnt.u5_conf0, |
| 96 | + #[cfg(esp32)] |
| 97 | + Number::Unit6 => &pcnt.u6_conf0, |
| 98 | + #[cfg(esp32)] |
| 99 | + Number::Unit7 => &pcnt.u7_conf0, |
| 100 | + }; |
| 101 | + // disable filter and all events |
| 102 | + conf0.modify(|_, w| unsafe { |
| 103 | + w.filter_en() |
| 104 | + .clear_bit() |
| 105 | + .filter_thres() |
| 106 | + .bits(0) |
| 107 | + .thr_l_lim_en() |
| 108 | + .clear_bit() |
| 109 | + .thr_h_lim_en() |
| 110 | + .clear_bit() |
| 111 | + .thr_thres0_en() |
| 112 | + .clear_bit() |
| 113 | + .thr_thres1_en() |
| 114 | + .clear_bit() |
| 115 | + .thr_zero_en() |
| 116 | + .clear_bit() |
| 117 | + }); |
| 118 | + Self { number } |
| 119 | + } |
| 120 | + |
| 121 | + pub fn configure(&mut self, config: Config) -> Result<(), Error> { |
| 122 | + // low limit must be >= or the limit is -32768 and when thats |
| 123 | + // hit the event status claims it was the high limit. |
| 124 | + // tested on an esp32s3 |
| 125 | + if config.low_limit >= 0 { |
| 126 | + return Err(Error::InvalidLowLimit); |
| 127 | + } |
| 128 | + if config.high_limit <= 0 { |
| 129 | + return Err(Error::InvalidHighLimit); |
| 130 | + } |
| 131 | + let (filter_en, filter) = match config.filter { |
| 132 | + Some(filter) => (true, filter), |
| 133 | + None => (false, 0), |
| 134 | + }; |
| 135 | + // filter must be less than 1024 |
| 136 | + if filter > 1023 { |
| 137 | + return Err(Error::InvalidFilterThresh); |
| 138 | + } |
| 139 | + |
| 140 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 141 | + let (conf0, conf1, conf2) = match self.number { |
| 142 | + Number::Unit0 => (&pcnt.u0_conf0, &pcnt.u0_conf1, &pcnt.u0_conf2), |
| 143 | + Number::Unit1 => (&pcnt.u1_conf0, &pcnt.u1_conf1, &pcnt.u1_conf2), |
| 144 | + Number::Unit2 => (&pcnt.u2_conf0, &pcnt.u2_conf1, &pcnt.u2_conf2), |
| 145 | + Number::Unit3 => (&pcnt.u3_conf0, &pcnt.u3_conf1, &pcnt.u3_conf2), |
| 146 | + #[cfg(esp32)] |
| 147 | + Number::Unit4 => (&pcnt.u4_conf0, &pcnt.u4_conf1, &pcnt.u4_conf2), |
| 148 | + #[cfg(esp32)] |
| 149 | + Number::Unit5 => (&pcnt.u5_conf0, &pcnt.u5_conf1, &pcnt.u5_conf2), |
| 150 | + #[cfg(esp32)] |
| 151 | + Number::Unit6 => (&pcnt.u6_conf0, &pcnt.u6_conf1, &pcnt.u6_conf2), |
| 152 | + #[cfg(esp32)] |
| 153 | + Number::Unit7 => (&pcnt.u7_conf0, &pcnt.u7_conf1, &pcnt.u7_conf2), |
| 154 | + }; |
| 155 | + conf2.write(|w| unsafe { |
| 156 | + w.cnt_l_lim() |
| 157 | + .bits(config.low_limit as u16) |
| 158 | + .cnt_h_lim() |
| 159 | + .bits(config.high_limit as u16) |
| 160 | + }); |
| 161 | + conf1.write(|w| unsafe { |
| 162 | + w.cnt_thres0() |
| 163 | + .bits(config.thresh0 as u16) |
| 164 | + .cnt_thres1() |
| 165 | + .bits(config.thresh1 as u16) |
| 166 | + }); |
| 167 | + conf0.modify(|_, w| unsafe { w.filter_thres().bits(filter).filter_en().bit(filter_en) }); |
| 168 | + self.pause(); |
| 169 | + self.clear(); |
| 170 | + Ok(()) |
| 171 | + } |
| 172 | + |
| 173 | + pub fn get_channel(&self, number: channel::Number) -> super::channel::Channel { |
| 174 | + super::channel::Channel::new(self.number, number) |
| 175 | + } |
| 176 | + |
| 177 | + pub fn clear(&self) { |
| 178 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 179 | + critical_section::with(|_cs| { |
| 180 | + match self.number { |
| 181 | + Number::Unit0 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u0().set_bit()), |
| 182 | + Number::Unit1 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u1().set_bit()), |
| 183 | + Number::Unit2 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u2().set_bit()), |
| 184 | + Number::Unit3 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u3().set_bit()), |
| 185 | + #[cfg(esp32)] |
| 186 | + Number::Unit4 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u4().set_bit()), |
| 187 | + #[cfg(esp32)] |
| 188 | + Number::Unit5 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u5().set_bit()), |
| 189 | + #[cfg(esp32)] |
| 190 | + Number::Unit6 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u6().set_bit()), |
| 191 | + #[cfg(esp32)] |
| 192 | + Number::Unit7 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u7().set_bit()), |
| 193 | + } |
| 194 | + // TODO: does this need a delay? (liebman / Jan 2 2023) |
| 195 | + match self.number { |
| 196 | + Number::Unit0 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u0().clear_bit()), |
| 197 | + Number::Unit1 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u1().clear_bit()), |
| 198 | + Number::Unit2 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u2().clear_bit()), |
| 199 | + Number::Unit3 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u3().clear_bit()), |
| 200 | + #[cfg(esp32)] |
| 201 | + Number::Unit4 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u4().clear_bit()), |
| 202 | + #[cfg(esp32)] |
| 203 | + Number::Unit5 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u5().clear_bit()), |
| 204 | + #[cfg(esp32)] |
| 205 | + Number::Unit6 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u6().clear_bit()), |
| 206 | + #[cfg(esp32)] |
| 207 | + Number::Unit7 => pcnt.ctrl.modify(|_, w| w.cnt_rst_u7().clear_bit()), |
| 208 | + } |
| 209 | + }); |
| 210 | + } |
| 211 | + |
| 212 | + /// Pause the counter |
| 213 | + pub fn pause(&self) { |
| 214 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 215 | + critical_section::with(|_cs| match self.number { |
| 216 | + Number::Unit0 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u0().set_bit()), |
| 217 | + Number::Unit1 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u1().set_bit()), |
| 218 | + Number::Unit2 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u2().set_bit()), |
| 219 | + Number::Unit3 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u3().set_bit()), |
| 220 | + #[cfg(esp32)] |
| 221 | + Number::Unit4 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u4().set_bit()), |
| 222 | + #[cfg(esp32)] |
| 223 | + Number::Unit5 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u5().set_bit()), |
| 224 | + #[cfg(esp32)] |
| 225 | + Number::Unit6 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u6().set_bit()), |
| 226 | + #[cfg(esp32)] |
| 227 | + Number::Unit7 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u7().set_bit()), |
| 228 | + }); |
| 229 | + } |
| 230 | + |
| 231 | + /// Resume the counter |
| 232 | + pub fn resume(&self) { |
| 233 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 234 | + critical_section::with(|_cs| match self.number { |
| 235 | + Number::Unit0 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u0().clear_bit()), |
| 236 | + Number::Unit1 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u1().clear_bit()), |
| 237 | + Number::Unit2 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u2().clear_bit()), |
| 238 | + Number::Unit3 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u3().clear_bit()), |
| 239 | + #[cfg(esp32)] |
| 240 | + Number::Unit4 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u4().clear_bit()), |
| 241 | + #[cfg(esp32)] |
| 242 | + Number::Unit5 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u5().clear_bit()), |
| 243 | + #[cfg(esp32)] |
| 244 | + Number::Unit6 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u6().clear_bit()), |
| 245 | + #[cfg(esp32)] |
| 246 | + Number::Unit7 => pcnt.ctrl.modify(|_, w| w.cnt_pause_u7().clear_bit()), |
| 247 | + }); |
| 248 | + } |
| 249 | + |
| 250 | + /// Enable which events generate interrupts on this unit. |
| 251 | + pub fn events(&self, events: Events) { |
| 252 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 253 | + let conf0 = match self.number { |
| 254 | + Number::Unit0 => &pcnt.u0_conf0, |
| 255 | + Number::Unit1 => &pcnt.u1_conf0, |
| 256 | + Number::Unit2 => &pcnt.u2_conf0, |
| 257 | + Number::Unit3 => &pcnt.u3_conf0, |
| 258 | + #[cfg(esp32)] |
| 259 | + Number::Unit4 => &pcnt.u4_conf0, |
| 260 | + #[cfg(esp32)] |
| 261 | + Number::Unit5 => &pcnt.u5_conf0, |
| 262 | + #[cfg(esp32)] |
| 263 | + Number::Unit6 => &pcnt.u6_conf0, |
| 264 | + #[cfg(esp32)] |
| 265 | + Number::Unit7 => &pcnt.u7_conf0, |
| 266 | + }; |
| 267 | + conf0.modify(|_, w| { |
| 268 | + w.thr_l_lim_en() |
| 269 | + .bit(events.low_limit) |
| 270 | + .thr_h_lim_en() |
| 271 | + .bit(events.high_limit) |
| 272 | + .thr_thres0_en() |
| 273 | + .bit(events.thresh0) |
| 274 | + .thr_thres1_en() |
| 275 | + .bit(events.thresh1) |
| 276 | + .thr_zero_en() |
| 277 | + .bit(events.zero) |
| 278 | + }); |
| 279 | + } |
| 280 | + |
| 281 | + /// Get the latest events for this unit. |
| 282 | + pub fn get_events(&self) -> Events { |
| 283 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 284 | + let status = pcnt.u_status[self.number as usize].read(); |
| 285 | + |
| 286 | + Events { |
| 287 | + low_limit: status.l_lim().bit(), |
| 288 | + high_limit: status.h_lim().bit(), |
| 289 | + thresh0: status.thres0().bit(), |
| 290 | + thresh1: status.thres1().bit(), |
| 291 | + zero: status.zero().bit(), |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + /// Get the mode of the last zero crossing |
| 296 | + pub fn get_zero_mode(&self) -> ZeroMode { |
| 297 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 298 | + pcnt.u_status[self.number as usize] |
| 299 | + .read() |
| 300 | + .zero_mode() |
| 301 | + .bits() |
| 302 | + .into() |
| 303 | + } |
| 304 | + |
| 305 | + /// Enable interrupts for this unit. |
| 306 | + pub fn listen(&self) { |
| 307 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 308 | + critical_section::with(|_cs| { |
| 309 | + pcnt.int_ena.modify(|_, w| match self.number { |
| 310 | + Number::Unit0 => w.cnt_thr_event_u0().set_bit(), |
| 311 | + Number::Unit1 => w.cnt_thr_event_u1().set_bit(), |
| 312 | + Number::Unit2 => w.cnt_thr_event_u2().set_bit(), |
| 313 | + Number::Unit3 => w.cnt_thr_event_u3().set_bit(), |
| 314 | + #[cfg(esp32)] |
| 315 | + Number::Unit4 => w.cnt_thr_event_u4().set_bit(), |
| 316 | + #[cfg(esp32)] |
| 317 | + Number::Unit5 => w.cnt_thr_event_u5().set_bit(), |
| 318 | + #[cfg(esp32)] |
| 319 | + Number::Unit6 => w.cnt_thr_event_u6().set_bit(), |
| 320 | + #[cfg(esp32)] |
| 321 | + Number::Unit7 => w.cnt_thr_event_u7().set_bit(), |
| 322 | + }) |
| 323 | + }); |
| 324 | + } |
| 325 | + |
| 326 | + /// Disable interrupts for this unit. |
| 327 | + pub fn unlisten(&self, _cs: CriticalSection) { |
| 328 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 329 | + critical_section::with(|_cs| { |
| 330 | + pcnt.int_ena.write(|w| match self.number { |
| 331 | + Number::Unit0 => w.cnt_thr_event_u0().clear_bit(), |
| 332 | + Number::Unit1 => w.cnt_thr_event_u1().clear_bit(), |
| 333 | + Number::Unit2 => w.cnt_thr_event_u2().clear_bit(), |
| 334 | + Number::Unit3 => w.cnt_thr_event_u3().clear_bit(), |
| 335 | + #[cfg(esp32)] |
| 336 | + Number::Unit4 => w.cnt_thr_event_u4().clear_bit(), |
| 337 | + #[cfg(esp32)] |
| 338 | + Number::Unit5 => w.cnt_thr_event_u5().clear_bit(), |
| 339 | + #[cfg(esp32)] |
| 340 | + Number::Unit6 => w.cnt_thr_event_u6().clear_bit(), |
| 341 | + #[cfg(esp32)] |
| 342 | + Number::Unit7 => w.cnt_thr_event_u7().clear_bit(), |
| 343 | + }) |
| 344 | + }); |
| 345 | + } |
| 346 | + |
| 347 | + /// Returns true if an interrupt is active for this unit. |
| 348 | + pub fn interrupt_set(&self) -> bool { |
| 349 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 350 | + match self.number { |
| 351 | + Number::Unit0 => pcnt.int_st.read().cnt_thr_event_u0().bit(), |
| 352 | + Number::Unit1 => pcnt.int_st.read().cnt_thr_event_u1().bit(), |
| 353 | + Number::Unit2 => pcnt.int_st.read().cnt_thr_event_u2().bit(), |
| 354 | + Number::Unit3 => pcnt.int_st.read().cnt_thr_event_u3().bit(), |
| 355 | + #[cfg(esp32)] |
| 356 | + Number::Unit4 => pcnt.int_st.read().cnt_thr_event_u4().bit(), |
| 357 | + #[cfg(esp32)] |
| 358 | + Number::Unit5 => pcnt.int_st.read().cnt_thr_event_u5().bit(), |
| 359 | + #[cfg(esp32)] |
| 360 | + Number::Unit6 => pcnt.int_st.read().cnt_thr_event_u6().bit(), |
| 361 | + #[cfg(esp32)] |
| 362 | + Number::Unit7 => pcnt.int_st.read().cnt_thr_event_u7().bit(), |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + /// Clear the interrupt bit for this unit. |
| 367 | + pub fn reset_interrupt(&self) { |
| 368 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 369 | + critical_section::with(|_cs| { |
| 370 | + pcnt.int_clr.write(|w| match self.number { |
| 371 | + Number::Unit0 => w.cnt_thr_event_u0().set_bit(), |
| 372 | + Number::Unit1 => w.cnt_thr_event_u1().set_bit(), |
| 373 | + Number::Unit2 => w.cnt_thr_event_u2().set_bit(), |
| 374 | + Number::Unit3 => w.cnt_thr_event_u3().set_bit(), |
| 375 | + #[cfg(esp32)] |
| 376 | + Number::Unit4 => w.cnt_thr_event_u4().set_bit(), |
| 377 | + #[cfg(esp32)] |
| 378 | + Number::Unit5 => w.cnt_thr_event_u5().set_bit(), |
| 379 | + #[cfg(esp32)] |
| 380 | + Number::Unit6 => w.cnt_thr_event_u6().set_bit(), |
| 381 | + #[cfg(esp32)] |
| 382 | + Number::Unit7 => w.cnt_thr_event_u7().set_bit(), |
| 383 | + }) |
| 384 | + }); |
| 385 | + } |
| 386 | + |
| 387 | + /// Get the current counter value. |
| 388 | + pub fn get_value(&self) -> i16 { |
| 389 | + let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; |
| 390 | + pcnt.u_cnt[self.number as usize].read().cnt().bits() as i16 |
| 391 | + } |
| 392 | +} |
0 commit comments