1use stm32f1xx_hal::{
3 pac::{self},
4 prelude::*,
5};
6
7use crate::{SettingClock, SettingDMA};
8
9pub fn init(dma_set: SettingDMA, dp: pac::Peripherals) -> stm32f1xx_hal::dma::dma1::Channels {
11 let mut rcc = dp.RCC.constrain();
12 let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
15 gpiob.pb0.into_floating_input(&mut gpiob.crl);
17
18 let timer = dp.TIM3;
20 let add_apbenr = unsafe { &(*stm32f1xx_hal::pac::RCC::ptr()).apb1enr };
22 add_apbenr.modify(|_, w| w.tim3en().set_bit());
23
24 timer.ccmr2_input().write(|w| w.cc3s().ti3());
26 timer.ccer.write(|w| w.cc3p().set_bit().cc3e().set_bit());
28 unsafe {
29 timer.dmar.write(|w| w.dmab().bits(0x81));
30 }
31 timer.cr1.write(|w| w.cen().set_bit());
33 timer.dier.write(|w| w.cc3de().set_bit());
35
36 let mut dma = dp.DMA1.split(&mut rcc.ahb);
38
39 dma.2.set_peripheral_address(dma_set.add_periph, false);
41 dma.2.set_memory_address(dma_set.add_mem, true);
43 dma.2.set_transfer_length(dma_set.nb_data.into()); unsafe {
46 dma.2.ch().cr.modify(|_, w| {
48 w.dir() .clear_bit()
50 .circ() .clear_bit()
52 .psize() .bits(0x01)
54 .msize() .bits(0x01)
56 .pl() .low()
58 });
59 }
60 dma.2.listen(stm32f1xx_hal::dma::Event::TransferComplete);
62 dma.2.start();
64
65 dma
66}
67
68pub fn time_stock(mut tab_time: [u16; 19], dma: SettingDMA) -> [u16; 19] {
70 for i in 0..19 {
71 unsafe {
72 tab_time[(i as usize)] = *((dma.add_mem + (i * 0x02) as u32) as *mut u16);
73 }
74 }
75 tab_time
76}
77
78pub fn synchro(tab_time: [u16; 19], mut ind: usize, status_trame: &mut bool) -> usize {
80 let mut diff = 0;
81
82 while !(1350..=1360).contains(&diff) {
83 if ind < 11 {
84 ind += 1;
86 diff = diff_calcul(tab_time[ind - 1], tab_time[ind]);
87 } else {
88 *status_trame = false; break;
90 }
91 }
92
93 ind
94}
95
96pub fn diff_calcul(x: u16, y: u16) -> u16 {
99 if x > y {
100 65535 - x + y
102 } else {
103 y - x
104 }
105}
106
107pub fn convert_data(
109 timer: SettingClock,
110 tab_time: [u16; 19],
111 mut ind: usize,
112 status_trame: &mut bool,
113) -> [u8; 8] {
114 let mut tab_value: [u8; 8] = [0; 8];
115 for k in 0..8 {
116 let data = (diff_calcul(tab_time[ind], tab_time[ind + 1]) as f32 * timer.period) as u8;
117 if !(36..=81).contains(&data) {
118 *status_trame = false;
120 } else {
121 tab_value[k] = (data - 36) / 3; ind += 1;
123 }
124 }
125 tab_value
126}
127
128pub fn check(tab_value: [u8; 8]) -> bool {
130 let mut checksum: u8 = 5;
131 let crclookup: [u8; 16] = [0, 13, 7, 10, 14, 3, 9, 4, 1, 12, 6, 11, 15, 2, 8, 5];
132
133 for a in tab_value.iter().take(7).skip(1) {
134 checksum = crclookup[(checksum as usize)];
137 checksum ^= a;
138 }
139 checksum = crclookup[(checksum as usize)];
140
141 tab_value[7] == checksum
142}
143
144pub fn stop_dma(mut dma: stm32f1xx_hal::dma::dma1::Channels) -> stm32f1xx_hal::dma::dma1::Channels {
146 let add_ifcr = unsafe { &(*stm32f1xx_hal::pac::DMA1::ptr()).ifcr };
147 add_ifcr.write(|w| w.cgif2().set_bit()); dma.2.stop();
149 dma
150}
151
152pub fn start_dma(
154 dma_set: SettingDMA,
155 mut dma: stm32f1xx_hal::dma::dma1::Channels,
156) -> stm32f1xx_hal::dma::dma1::Channels {
157 dma.2.set_transfer_length(dma_set.nb_data.into());
158 dma.2.listen(stm32f1xx_hal::dma::Event::TransferComplete);
159 dma.2.start();
160
161 dma
162}