summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-06-03 20:47:37 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-06-03 20:47:37 +0200
commit0bc2a81c0aab3c89b534415d6f07d07e392260ce (patch)
tree5995a09ea9212df38490b330fc878060b6bb9294 /include
parentdcfb42c972f904482514ef194003018a02c8c260 (diff)
downloadradio-controller-0bc2a81c0aab3c89b534415d6f07d07e392260ce.tar.gz
radio-controller-0bc2a81c0aab3c89b534415d6f07d07e392260ce.tar.bz2
radio-controller-0bc2a81c0aab3c89b534415d6f07d07e392260ce.tar.xz
radio-controller-0bc2a81c0aab3c89b534415d6f07d07e392260ce.zip
o Adding support for reading IR codes.
Diffstat (limited to 'include')
-rw-r--r--include/misc.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/include/misc.h b/include/misc.h
new file mode 100644
index 0000000..aacba74
--- /dev/null
+++ b/include/misc.h
@@ -0,0 +1,61 @@
+#pragma once
+
+#include <chrono>
+#include <stm32f1xx_hal_tim.h>
+#include <stm32f103x6.h>
+
+template<unsigned Fcpu, typename SecondsPerTimerTick>
+struct from_f_cpu_and_time_per_timer_tick {
+ template<std::intmax_t Num, std::intmax_t Den>
+ using r = std::ratio<Num, Den>;
+
+ using f_cpu = r<Fcpu, 1>;
+ using seconds_per_cpu_tick = r<1, f_cpu::num>;
+ using seconds_per_timer_tick = SecondsPerTimerTick;
+ using timer_frequency = r<SecondsPerTimerTick::den, SecondsPerTimerTick::num>;
+
+// static constexpr std::intmax_t prescaler = std::ratio_divive<f_cpu, timer_frequency>::value;
+ using prescaler_t = std::ratio_divide<f_cpu, timer_frequency>;
+ static_assert(prescaler_t::den == 1, "Could not represent the prescaler as an integer");
+ static constexpr std::intmax_t prescaler = prescaler_t::num;
+
+ // us_per_timer_tick = seconds_per_timer_tick * 1'000'000
+ using us_per_timer_tick_t = typename std::ratio_multiply<seconds_per_timer_tick, r<1'000'000, 1>>::type;
+
+ static constexpr uint16_t us_per_timer_tick = static_cast<uint16_t>(us_per_timer_tick_t::num);
+
+ template<typename T>
+ __always_inline constexpr
+ static T to_us(unsigned int count)
+ {
+ return T(count * us_per_timer_tick);
+ }
+};
+
+using values = from_f_cpu_and_time_per_timer_tick<72'000'000, std::ratio_multiply<std::ratio<5, 1>, std::micro>>;
+
+//static_assert(values::timer_frequency::num == 200'000, "timer_frequency::num");
+static_assert(values::timer_frequency::den == 1, "timer_frequency::den");
+//static_assert(std::is_same<
+// values::timer_frequency,
+// std::ratio<200'000, 1>>::value, "Timer frequency is not 200kHz");
+static_assert(values::prescaler == 360, "Timer frequency is not 200kHz");
+static_assert(values::prescaler - 1 == RADIO_RX_TIMER_PRESCALER, "Bad prescaler from STM32CubeMX");
+static_assert(values::us_per_timer_tick == 5, "bad us_per_timer_tick");
+
+static inline
+void debug_pin(int count) {
+ for(int i = 0; i < count; i++) {
+ HAL_GPIO_WritePin(DEBUG_PIN_GPIO_Port, DEBUG_PIN_Pin, GPIO_PIN_SET);
+ __NOP();
+ __NOP();
+ __NOP();
+ HAL_GPIO_WritePin(DEBUG_PIN_GPIO_Port, DEBUG_PIN_Pin, GPIO_PIN_RESET);
+ __NOP();
+ __NOP();
+ __NOP();
+ }
+ __NOP();
+ __NOP();
+ __NOP();
+}