furi_hal_infrared.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #include <furi_hal_infrared.h>
  2. #include <core/check.h>
  3. #include "stm32wbxx_ll_dma.h"
  4. #include "sys/_stdint.h"
  5. #include <furi_hal_interrupt.h>
  6. #include <furi_hal_resources.h>
  7. #include <stdint.h>
  8. #include <stm32wbxx_ll_tim.h>
  9. #include <stm32wbxx_ll_gpio.h>
  10. #include <stdio.h>
  11. #include <furi.h>
  12. #include <math.h>
  13. #define INFRARED_TX_DEBUG 0
  14. #if INFRARED_TX_DEBUG == 1
  15. #define gpio_infrared_tx gpio_infrared_tx_debug
  16. const GpioPin gpio_infrared_tx_debug = {.port = GPIOA, .pin = GPIO_PIN_7};
  17. #endif
  18. #define INFRARED_TIM_TX_DMA_BUFFER_SIZE 200
  19. #define INFRARED_POLARITY_SHIFT 1
  20. #define INFRARED_TX_CCMR_HIGH \
  21. (TIM_CCMR2_OC3PE | LL_TIM_OCMODE_PWM2) /* Mark time - enable PWM2 mode */
  22. #define INFRARED_TX_CCMR_LOW \
  23. (TIM_CCMR2_OC3PE | LL_TIM_OCMODE_FORCED_INACTIVE) /* Space time - force low */
  24. /* DMA Channels definition */
  25. #define IR_DMA DMA2
  26. #define IR_DMA_CH1_CHANNEL LL_DMA_CHANNEL_1
  27. #define IR_DMA_CH2_CHANNEL LL_DMA_CHANNEL_2
  28. #define IR_DMA_CH1_IRQ FuriHalInterruptIdDma2Ch1
  29. #define IR_DMA_CH2_IRQ FuriHalInterruptIdDma2Ch2
  30. #define IR_DMA_CH1_DEF IR_DMA, IR_DMA_CH1_CHANNEL
  31. #define IR_DMA_CH2_DEF IR_DMA, IR_DMA_CH2_CHANNEL
  32. typedef struct {
  33. FuriHalInfraredRxCaptureCallback capture_callback;
  34. void* capture_context;
  35. FuriHalInfraredRxTimeoutCallback timeout_callback;
  36. void* timeout_context;
  37. } InfraredTimRx;
  38. typedef struct {
  39. uint8_t* polarity;
  40. uint16_t* data;
  41. size_t size;
  42. bool packet_end;
  43. bool last_packet_end;
  44. } InfraredTxBuf;
  45. typedef struct {
  46. float cycle_duration;
  47. FuriHalInfraredTxGetDataISRCallback data_callback;
  48. FuriHalInfraredTxSignalSentISRCallback signal_sent_callback;
  49. void* data_context;
  50. void* signal_sent_context;
  51. InfraredTxBuf buffer[2];
  52. FuriSemaphore* stop_semaphore;
  53. uint32_t
  54. tx_timing_rest_duration; /** if timing is too long (> 0xFFFF), send it in few iterations */
  55. bool tx_timing_rest_level;
  56. FuriHalInfraredTxGetDataState tx_timing_rest_status;
  57. } InfraredTimTx;
  58. typedef enum {
  59. InfraredStateIdle, /** Furi Hal Infrared is ready to start RX or TX */
  60. InfraredStateAsyncRx, /** Async RX started */
  61. InfraredStateAsyncTx, /** Async TX started, DMA and timer is on */
  62. InfraredStateAsyncTxStopReq, /** Async TX started, async stop request received */
  63. InfraredStateAsyncTxStopInProgress, /** Async TX started, stop request is processed and we wait for last data to be sent */
  64. InfraredStateAsyncTxStopped, /** Async TX complete, cleanup needed */
  65. InfraredStateMAX,
  66. } InfraredState;
  67. static volatile InfraredState furi_hal_infrared_state = InfraredStateIdle;
  68. static InfraredTimTx infrared_tim_tx;
  69. static InfraredTimRx infrared_tim_rx;
  70. static void furi_hal_infrared_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift);
  71. static void furi_hal_infrared_async_tx_free_resources(void);
  72. static void furi_hal_infrared_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift);
  73. static void furi_hal_infrared_tx_dma_set_buffer(uint8_t buf_num);
  74. static void furi_hal_infrared_tx_fill_buffer_last(uint8_t buf_num);
  75. static uint8_t furi_hal_infrared_get_current_dma_tx_buffer(void);
  76. static void furi_hal_infrared_tx_dma_polarity_isr();
  77. static void furi_hal_infrared_tx_dma_isr();
  78. static void furi_hal_infrared_tim_rx_isr() {
  79. static uint32_t previous_captured_ch2 = 0;
  80. /* Timeout */
  81. if(LL_TIM_IsActiveFlag_CC3(TIM2)) {
  82. LL_TIM_ClearFlag_CC3(TIM2);
  83. furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
  84. /* Timers CNT register starts to counting from 0 to ARR, but it is
  85. * reseted when Channel 1 catches interrupt. It is not reseted by
  86. * channel 2, though, so we have to distract it's values (see TimerIRQSourceCCI1 ISR).
  87. * This can cause false timeout: when time is over, but we started
  88. * receiving new signal few microseconds ago, because CNT register
  89. * is reseted once per period, not per sample. */
  90. if(LL_GPIO_IsInputPinSet(gpio_infrared_rx.port, gpio_infrared_rx.pin) != 0) {
  91. if(infrared_tim_rx.timeout_callback)
  92. infrared_tim_rx.timeout_callback(infrared_tim_rx.timeout_context);
  93. }
  94. }
  95. /* Rising Edge */
  96. if(LL_TIM_IsActiveFlag_CC1(TIM2)) {
  97. LL_TIM_ClearFlag_CC1(TIM2);
  98. furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
  99. if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
  100. /* Low pin level is a Mark state of INFRARED signal. Invert level for further processing. */
  101. uint32_t duration = LL_TIM_IC_GetCaptureCH1(TIM2) - previous_captured_ch2;
  102. if(infrared_tim_rx.capture_callback)
  103. infrared_tim_rx.capture_callback(infrared_tim_rx.capture_context, 1, duration);
  104. } else {
  105. furi_assert(0);
  106. }
  107. }
  108. /* Falling Edge */
  109. if(LL_TIM_IsActiveFlag_CC2(TIM2)) {
  110. LL_TIM_ClearFlag_CC2(TIM2);
  111. furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
  112. if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
  113. /* High pin level is a Space state of INFRARED signal. Invert level for further processing. */
  114. uint32_t duration = LL_TIM_IC_GetCaptureCH2(TIM2);
  115. previous_captured_ch2 = duration;
  116. if(infrared_tim_rx.capture_callback)
  117. infrared_tim_rx.capture_callback(infrared_tim_rx.capture_context, 0, duration);
  118. } else {
  119. furi_assert(0);
  120. }
  121. }
  122. }
  123. void furi_hal_infrared_async_rx_start(void) {
  124. furi_assert(furi_hal_infrared_state == InfraredStateIdle);
  125. furi_hal_gpio_init_ex(
  126. &gpio_infrared_rx, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
  127. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  128. TIM_InitStruct.Prescaler = 64 - 1;
  129. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  130. TIM_InitStruct.Autoreload = 0x7FFFFFFE;
  131. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  132. LL_TIM_Init(TIM2, &TIM_InitStruct);
  133. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  134. LL_TIM_DisableARRPreload(TIM2);
  135. LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_TI1FP1);
  136. LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_RESET);
  137. LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  138. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
  139. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_FALLING);
  140. LL_TIM_DisableIT_TRIG(TIM2);
  141. LL_TIM_DisableDMAReq_TRIG(TIM2);
  142. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  143. LL_TIM_EnableMasterSlaveMode(TIM2);
  144. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
  145. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  146. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
  147. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
  148. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_INDIRECTTI);
  149. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  150. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, furi_hal_infrared_tim_rx_isr, NULL);
  151. furi_hal_infrared_state = InfraredStateAsyncRx;
  152. LL_TIM_EnableIT_CC1(TIM2);
  153. LL_TIM_EnableIT_CC2(TIM2);
  154. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
  155. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  156. LL_TIM_SetCounter(TIM2, 0);
  157. LL_TIM_EnableCounter(TIM2);
  158. }
  159. void furi_hal_infrared_async_rx_stop(void) {
  160. furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
  161. FURI_CRITICAL_ENTER();
  162. LL_TIM_DeInit(TIM2);
  163. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
  164. furi_hal_infrared_state = InfraredStateIdle;
  165. FURI_CRITICAL_EXIT();
  166. }
  167. void furi_hal_infrared_async_rx_set_timeout(uint32_t timeout_us) {
  168. LL_TIM_OC_SetCompareCH3(TIM2, timeout_us);
  169. LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_ACTIVE);
  170. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH3);
  171. LL_TIM_EnableIT_CC3(TIM2);
  172. }
  173. bool furi_hal_infrared_is_busy(void) {
  174. return furi_hal_infrared_state != InfraredStateIdle;
  175. }
  176. void furi_hal_infrared_async_rx_set_capture_isr_callback(
  177. FuriHalInfraredRxCaptureCallback callback,
  178. void* ctx) {
  179. infrared_tim_rx.capture_callback = callback;
  180. infrared_tim_rx.capture_context = ctx;
  181. }
  182. void furi_hal_infrared_async_rx_set_timeout_isr_callback(
  183. FuriHalInfraredRxTimeoutCallback callback,
  184. void* ctx) {
  185. infrared_tim_rx.timeout_callback = callback;
  186. infrared_tim_rx.timeout_context = ctx;
  187. }
  188. static void furi_hal_infrared_tx_dma_terminate(void) {
  189. LL_DMA_DisableIT_TC(IR_DMA_CH1_DEF);
  190. LL_DMA_DisableIT_HT(IR_DMA_CH2_DEF);
  191. LL_DMA_DisableIT_TC(IR_DMA_CH2_DEF);
  192. furi_assert(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress);
  193. LL_DMA_DisableIT_TC(IR_DMA_CH1_DEF);
  194. LL_DMA_DisableChannel(IR_DMA_CH2_DEF);
  195. LL_DMA_DisableChannel(IR_DMA_CH1_DEF);
  196. LL_TIM_DisableCounter(TIM1);
  197. FuriStatus status = furi_semaphore_release(infrared_tim_tx.stop_semaphore);
  198. furi_check(status == FuriStatusOk);
  199. furi_hal_infrared_state = InfraredStateAsyncTxStopped;
  200. }
  201. static uint8_t furi_hal_infrared_get_current_dma_tx_buffer(void) {
  202. uint8_t buf_num = 0;
  203. uint32_t buffer_adr = LL_DMA_GetMemoryAddress(IR_DMA_CH2_DEF);
  204. if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[0].data) {
  205. buf_num = 0;
  206. } else if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[1].data) {
  207. buf_num = 1;
  208. } else {
  209. furi_assert(0);
  210. }
  211. return buf_num;
  212. }
  213. static void furi_hal_infrared_tx_dma_polarity_isr() {
  214. #if IR_DMA_CH1_CHANNEL == LL_DMA_CHANNEL_1
  215. if(LL_DMA_IsActiveFlag_TE1(IR_DMA)) {
  216. LL_DMA_ClearFlag_TE1(IR_DMA);
  217. furi_crash(NULL);
  218. }
  219. if(LL_DMA_IsActiveFlag_TC1(IR_DMA) && LL_DMA_IsEnabledIT_TC(IR_DMA_CH1_DEF)) {
  220. LL_DMA_ClearFlag_TC1(IR_DMA);
  221. furi_check(
  222. (furi_hal_infrared_state == InfraredStateAsyncTx) ||
  223. (furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
  224. (furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress));
  225. /* actually TC2 is processed and buffer is next buffer */
  226. uint8_t next_buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
  227. furi_hal_infrared_tx_dma_set_polarity(next_buf_num, 0);
  228. }
  229. #else
  230. #error Update this code. Would you kindly?
  231. #endif
  232. }
  233. static void furi_hal_infrared_tx_dma_isr() {
  234. #if IR_DMA_CH2_CHANNEL == LL_DMA_CHANNEL_2
  235. if(LL_DMA_IsActiveFlag_TE2(IR_DMA)) {
  236. LL_DMA_ClearFlag_TE2(IR_DMA);
  237. furi_crash(NULL);
  238. }
  239. if(LL_DMA_IsActiveFlag_HT2(IR_DMA) && LL_DMA_IsEnabledIT_HT(IR_DMA_CH2_DEF)) {
  240. LL_DMA_ClearFlag_HT2(IR_DMA);
  241. uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
  242. uint8_t next_buf_num = !buf_num;
  243. if(infrared_tim_tx.buffer[buf_num].last_packet_end) {
  244. LL_DMA_DisableIT_HT(IR_DMA_CH2_DEF);
  245. } else if(
  246. !infrared_tim_tx.buffer[buf_num].packet_end ||
  247. (furi_hal_infrared_state == InfraredStateAsyncTx)) {
  248. furi_hal_infrared_tx_fill_buffer(next_buf_num, 0);
  249. if(infrared_tim_tx.buffer[next_buf_num].last_packet_end) {
  250. LL_DMA_DisableIT_HT(IR_DMA_CH2_DEF);
  251. }
  252. } else if(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) {
  253. /* fallthrough */
  254. } else {
  255. furi_crash(NULL);
  256. }
  257. }
  258. if(LL_DMA_IsActiveFlag_TC2(IR_DMA) && LL_DMA_IsEnabledIT_TC(IR_DMA_CH2_DEF)) {
  259. LL_DMA_ClearFlag_TC2(IR_DMA);
  260. furi_check(
  261. (furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) ||
  262. (furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
  263. (furi_hal_infrared_state == InfraredStateAsyncTx));
  264. uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
  265. uint8_t next_buf_num = !buf_num;
  266. if(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) {
  267. furi_hal_infrared_tx_dma_terminate();
  268. } else if(
  269. infrared_tim_tx.buffer[buf_num].last_packet_end ||
  270. (infrared_tim_tx.buffer[buf_num].packet_end &&
  271. (furi_hal_infrared_state == InfraredStateAsyncTxStopReq))) {
  272. furi_hal_infrared_state = InfraredStateAsyncTxStopInProgress;
  273. furi_hal_infrared_tx_fill_buffer_last(next_buf_num);
  274. furi_hal_infrared_tx_dma_set_buffer(next_buf_num);
  275. } else {
  276. /* if it's not end of the packet - continue receiving */
  277. furi_hal_infrared_tx_dma_set_buffer(next_buf_num);
  278. }
  279. if(infrared_tim_tx.signal_sent_callback && infrared_tim_tx.buffer[buf_num].packet_end &&
  280. (furi_hal_infrared_state != InfraredStateAsyncTxStopped)) {
  281. infrared_tim_tx.signal_sent_callback(infrared_tim_tx.signal_sent_context);
  282. }
  283. }
  284. #else
  285. #error Update this code. Would you kindly?
  286. #endif
  287. }
  288. static void furi_hal_infrared_configure_tim_pwm_tx(uint32_t freq, float duty_cycle) {
  289. /* LL_DBGMCU_APB2_GRP1_FreezePeriph(LL_DBGMCU_APB2_GRP1_TIM1_STOP); */
  290. LL_TIM_DisableCounter(TIM1);
  291. LL_TIM_SetRepetitionCounter(TIM1, 0);
  292. LL_TIM_SetCounter(TIM1, 0);
  293. LL_TIM_SetPrescaler(TIM1, 0);
  294. LL_TIM_SetCounterMode(TIM1, LL_TIM_COUNTERMODE_UP);
  295. LL_TIM_EnableARRPreload(TIM1);
  296. LL_TIM_SetAutoReload(
  297. TIM1, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM1), freq));
  298. #if INFRARED_TX_DEBUG == 1
  299. LL_TIM_OC_SetCompareCH1(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
  300. LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
  301. /* LL_TIM_OCMODE_PWM2 set by DMA */
  302. LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_INACTIVE);
  303. LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH1N, LL_TIM_OCPOLARITY_HIGH);
  304. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
  305. LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N);
  306. LL_TIM_DisableIT_CC1(TIM1);
  307. #else
  308. LL_TIM_OC_SetCompareCH3(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
  309. LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH3);
  310. /* LL_TIM_OCMODE_PWM2 set by DMA */
  311. LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_FORCED_INACTIVE);
  312. LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH3N, LL_TIM_OCPOLARITY_HIGH);
  313. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH3);
  314. LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH3N);
  315. LL_TIM_DisableIT_CC3(TIM1);
  316. #endif
  317. LL_TIM_DisableMasterSlaveMode(TIM1);
  318. LL_TIM_EnableAllOutputs(TIM1);
  319. LL_TIM_DisableIT_UPDATE(TIM1);
  320. LL_TIM_EnableDMAReq_UPDATE(TIM1);
  321. NVIC_SetPriority(TIM1_UP_TIM16_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  322. NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
  323. }
  324. static void furi_hal_infrared_configure_tim_cmgr2_dma_tx(void) {
  325. LL_DMA_InitTypeDef dma_config = {0};
  326. #if INFRARED_TX_DEBUG == 1
  327. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR1);
  328. #else
  329. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR2);
  330. #endif
  331. dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
  332. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  333. dma_config.Mode = LL_DMA_MODE_NORMAL;
  334. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  335. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  336. /* fill word to have other bits set to 0 */
  337. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
  338. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  339. dma_config.NbData = 0;
  340. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  341. dma_config.Priority = LL_DMA_PRIORITY_VERYHIGH;
  342. LL_DMA_Init(IR_DMA_CH1_DEF, &dma_config);
  343. #if IR_DMA_CH1_CHANNEL == LL_DMA_CHANNEL_1
  344. LL_DMA_ClearFlag_TE1(IR_DMA);
  345. LL_DMA_ClearFlag_TC1(IR_DMA);
  346. #else
  347. #error Update this code. Would you kindly?
  348. #endif
  349. LL_DMA_EnableIT_TE(IR_DMA_CH1_DEF);
  350. LL_DMA_EnableIT_TC(IR_DMA_CH1_DEF);
  351. furi_hal_interrupt_set_isr_ex(IR_DMA_CH1_IRQ, 4, furi_hal_infrared_tx_dma_polarity_isr, NULL);
  352. }
  353. static void furi_hal_infrared_configure_tim_rcr_dma_tx(void) {
  354. LL_DMA_InitTypeDef dma_config = {0};
  355. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->RCR);
  356. dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
  357. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  358. dma_config.Mode = LL_DMA_MODE_NORMAL;
  359. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  360. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  361. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_HALFWORD;
  362. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD;
  363. dma_config.NbData = 0;
  364. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  365. dma_config.Priority = LL_DMA_PRIORITY_MEDIUM;
  366. LL_DMA_Init(IR_DMA_CH2_DEF, &dma_config);
  367. #if IR_DMA_CH2_CHANNEL == LL_DMA_CHANNEL_2
  368. LL_DMA_ClearFlag_TC2(IR_DMA);
  369. LL_DMA_ClearFlag_HT2(IR_DMA);
  370. LL_DMA_ClearFlag_TE2(IR_DMA);
  371. #else
  372. #error Update this code. Would you kindly?
  373. #endif
  374. LL_DMA_EnableIT_TC(IR_DMA_CH2_DEF);
  375. LL_DMA_EnableIT_HT(IR_DMA_CH2_DEF);
  376. LL_DMA_EnableIT_TE(IR_DMA_CH2_DEF);
  377. furi_hal_interrupt_set_isr_ex(IR_DMA_CH2_IRQ, 5, furi_hal_infrared_tx_dma_isr, NULL);
  378. }
  379. static void furi_hal_infrared_tx_fill_buffer_last(uint8_t buf_num) {
  380. furi_assert(buf_num < 2);
  381. furi_assert(furi_hal_infrared_state != InfraredStateAsyncRx);
  382. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  383. furi_assert(infrared_tim_tx.data_callback);
  384. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  385. furi_assert(buffer->data != NULL);
  386. (void)buffer->data;
  387. furi_assert(buffer->polarity != NULL);
  388. (void)buffer->polarity;
  389. infrared_tim_tx.buffer[buf_num].data[0] = 0; // 1 pulse
  390. infrared_tim_tx.buffer[buf_num].polarity[0] = INFRARED_TX_CCMR_LOW;
  391. infrared_tim_tx.buffer[buf_num].data[1] = 0; // 1 pulse
  392. infrared_tim_tx.buffer[buf_num].polarity[1] = INFRARED_TX_CCMR_LOW;
  393. infrared_tim_tx.buffer[buf_num].size = 2;
  394. infrared_tim_tx.buffer[buf_num].last_packet_end = true;
  395. infrared_tim_tx.buffer[buf_num].packet_end = true;
  396. }
  397. static void furi_hal_infrared_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
  398. furi_assert(buf_num < 2);
  399. furi_assert(furi_hal_infrared_state != InfraredStateAsyncRx);
  400. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  401. furi_assert(infrared_tim_tx.data_callback);
  402. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  403. furi_assert(buffer->data != NULL);
  404. furi_assert(buffer->polarity != NULL);
  405. FuriHalInfraredTxGetDataState status = FuriHalInfraredTxGetDataStateOk;
  406. uint32_t duration = 0;
  407. bool level = 0;
  408. size_t* size = &buffer->size;
  409. size_t polarity_counter = 0;
  410. while(polarity_shift--) {
  411. buffer->polarity[polarity_counter++] = INFRARED_TX_CCMR_LOW;
  412. }
  413. for(*size = 0; (*size < INFRARED_TIM_TX_DMA_BUFFER_SIZE) &&
  414. (status == FuriHalInfraredTxGetDataStateOk);) {
  415. if(infrared_tim_tx.tx_timing_rest_duration > 0) {
  416. if(infrared_tim_tx.tx_timing_rest_duration > 0xFFFF) {
  417. buffer->data[*size] = 0xFFFF;
  418. status = FuriHalInfraredTxGetDataStateOk;
  419. } else {
  420. buffer->data[*size] = infrared_tim_tx.tx_timing_rest_duration;
  421. status = infrared_tim_tx.tx_timing_rest_status;
  422. }
  423. infrared_tim_tx.tx_timing_rest_duration -= buffer->data[*size];
  424. buffer->polarity[polarity_counter] = infrared_tim_tx.tx_timing_rest_level ?
  425. INFRARED_TX_CCMR_HIGH :
  426. INFRARED_TX_CCMR_LOW;
  427. ++(*size);
  428. ++polarity_counter;
  429. continue;
  430. }
  431. status = infrared_tim_tx.data_callback(infrared_tim_tx.data_context, &duration, &level);
  432. uint32_t num_of_impulses = roundf(duration / infrared_tim_tx.cycle_duration);
  433. if(num_of_impulses == 0) {
  434. if((*size == 0) && (status == FuriHalInfraredTxGetDataStateDone)) {
  435. /* if this is one sample in current buffer, but we
  436. * have more to send - continue
  437. */
  438. status = FuriHalInfraredTxGetDataStateOk;
  439. }
  440. } else if((num_of_impulses - 1) > 0xFFFF) {
  441. infrared_tim_tx.tx_timing_rest_duration = num_of_impulses - 1;
  442. infrared_tim_tx.tx_timing_rest_status = status;
  443. infrared_tim_tx.tx_timing_rest_level = level;
  444. status = FuriHalInfraredTxGetDataStateOk;
  445. } else {
  446. buffer->polarity[polarity_counter] = level ? INFRARED_TX_CCMR_HIGH :
  447. INFRARED_TX_CCMR_LOW;
  448. buffer->data[*size] = num_of_impulses - 1;
  449. ++(*size);
  450. ++polarity_counter;
  451. }
  452. }
  453. buffer->last_packet_end = (status == FuriHalInfraredTxGetDataStateLastDone);
  454. buffer->packet_end = buffer->last_packet_end || (status == FuriHalInfraredTxGetDataStateDone);
  455. if(*size == 0) {
  456. buffer->data[0] = 0; // 1 pulse
  457. buffer->polarity[0] = INFRARED_TX_CCMR_LOW;
  458. buffer->size = 1;
  459. }
  460. }
  461. static void furi_hal_infrared_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
  462. furi_assert(buf_num < 2);
  463. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  464. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  465. furi_assert(buffer->polarity != NULL);
  466. FURI_CRITICAL_ENTER();
  467. bool channel_enabled = LL_DMA_IsEnabledChannel(IR_DMA_CH1_DEF);
  468. if(channel_enabled) {
  469. LL_DMA_DisableChannel(IR_DMA_CH1_DEF);
  470. }
  471. LL_DMA_SetMemoryAddress(IR_DMA_CH1_DEF, (uint32_t)buffer->polarity);
  472. LL_DMA_SetDataLength(IR_DMA_CH1_DEF, buffer->size + polarity_shift);
  473. if(channel_enabled) {
  474. LL_DMA_EnableChannel(IR_DMA_CH1_DEF);
  475. }
  476. FURI_CRITICAL_EXIT();
  477. }
  478. static void furi_hal_infrared_tx_dma_set_buffer(uint8_t buf_num) {
  479. furi_assert(buf_num < 2);
  480. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  481. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  482. furi_assert(buffer->data != NULL);
  483. /* non-circular mode requires disabled channel before setup */
  484. FURI_CRITICAL_ENTER();
  485. bool channel_enabled = LL_DMA_IsEnabledChannel(IR_DMA_CH2_DEF);
  486. if(channel_enabled) {
  487. LL_DMA_DisableChannel(IR_DMA_CH2_DEF);
  488. }
  489. LL_DMA_SetMemoryAddress(IR_DMA_CH2_DEF, (uint32_t)buffer->data);
  490. LL_DMA_SetDataLength(IR_DMA_CH2_DEF, buffer->size);
  491. if(channel_enabled) {
  492. LL_DMA_EnableChannel(IR_DMA_CH2_DEF);
  493. }
  494. FURI_CRITICAL_EXIT();
  495. }
  496. static void furi_hal_infrared_async_tx_free_resources(void) {
  497. furi_assert(
  498. (furi_hal_infrared_state == InfraredStateIdle) ||
  499. (furi_hal_infrared_state == InfraredStateAsyncTxStopped));
  500. furi_hal_gpio_init(&gpio_infrared_tx, GpioModeOutputOpenDrain, GpioPullDown, GpioSpeedLow);
  501. furi_hal_interrupt_set_isr(IR_DMA_CH1_IRQ, NULL, NULL);
  502. furi_hal_interrupt_set_isr(IR_DMA_CH2_IRQ, NULL, NULL);
  503. LL_TIM_DeInit(TIM1);
  504. furi_semaphore_free(infrared_tim_tx.stop_semaphore);
  505. free(infrared_tim_tx.buffer[0].data);
  506. free(infrared_tim_tx.buffer[1].data);
  507. free(infrared_tim_tx.buffer[0].polarity);
  508. free(infrared_tim_tx.buffer[1].polarity);
  509. infrared_tim_tx.buffer[0].data = NULL;
  510. infrared_tim_tx.buffer[1].data = NULL;
  511. infrared_tim_tx.buffer[0].polarity = NULL;
  512. infrared_tim_tx.buffer[1].polarity = NULL;
  513. }
  514. void furi_hal_infrared_async_tx_start(uint32_t freq, float duty_cycle) {
  515. if((duty_cycle > 1) || (duty_cycle <= 0) || (freq > INFRARED_MAX_FREQUENCY) ||
  516. (freq < INFRARED_MIN_FREQUENCY) || (infrared_tim_tx.data_callback == NULL)) {
  517. furi_crash(NULL);
  518. }
  519. furi_assert(furi_hal_infrared_state == InfraredStateIdle);
  520. furi_assert(infrared_tim_tx.buffer[0].data == NULL);
  521. furi_assert(infrared_tim_tx.buffer[1].data == NULL);
  522. furi_assert(infrared_tim_tx.buffer[0].polarity == NULL);
  523. furi_assert(infrared_tim_tx.buffer[1].polarity == NULL);
  524. size_t alloc_size_data = INFRARED_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
  525. infrared_tim_tx.buffer[0].data = malloc(alloc_size_data);
  526. infrared_tim_tx.buffer[1].data = malloc(alloc_size_data);
  527. size_t alloc_size_polarity =
  528. (INFRARED_TIM_TX_DMA_BUFFER_SIZE + INFRARED_POLARITY_SHIFT) * sizeof(uint8_t);
  529. infrared_tim_tx.buffer[0].polarity = malloc(alloc_size_polarity);
  530. infrared_tim_tx.buffer[1].polarity = malloc(alloc_size_polarity);
  531. infrared_tim_tx.stop_semaphore = furi_semaphore_alloc(1, 0);
  532. infrared_tim_tx.cycle_duration = 1000000.0 / freq;
  533. infrared_tim_tx.tx_timing_rest_duration = 0;
  534. furi_hal_infrared_tx_fill_buffer(0, INFRARED_POLARITY_SHIFT);
  535. furi_hal_infrared_configure_tim_pwm_tx(freq, duty_cycle);
  536. furi_hal_infrared_configure_tim_cmgr2_dma_tx();
  537. furi_hal_infrared_configure_tim_rcr_dma_tx();
  538. furi_hal_infrared_tx_dma_set_polarity(0, INFRARED_POLARITY_SHIFT);
  539. furi_hal_infrared_tx_dma_set_buffer(0);
  540. furi_hal_infrared_state = InfraredStateAsyncTx;
  541. LL_TIM_ClearFlag_UPDATE(TIM1);
  542. LL_DMA_EnableChannel(IR_DMA_CH1_DEF);
  543. LL_DMA_EnableChannel(IR_DMA_CH2_DEF);
  544. furi_delay_us(5);
  545. LL_TIM_GenerateEvent_UPDATE(TIM1); /* DMA -> TIMx_RCR */
  546. furi_delay_us(5);
  547. LL_GPIO_ResetOutputPin(
  548. gpio_infrared_tx.port, gpio_infrared_tx.pin); /* when disable it prevents false pulse */
  549. furi_hal_gpio_init_ex(
  550. &gpio_infrared_tx, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1);
  551. FURI_CRITICAL_ENTER();
  552. LL_TIM_GenerateEvent_UPDATE(TIM1); /* TIMx_RCR -> Repetition counter */
  553. LL_TIM_EnableCounter(TIM1);
  554. FURI_CRITICAL_EXIT();
  555. }
  556. void furi_hal_infrared_async_tx_wait_termination(void) {
  557. furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
  558. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  559. FuriStatus status;
  560. status = furi_semaphore_acquire(infrared_tim_tx.stop_semaphore, FuriWaitForever);
  561. furi_check(status == FuriStatusOk);
  562. furi_hal_infrared_async_tx_free_resources();
  563. furi_hal_infrared_state = InfraredStateIdle;
  564. }
  565. void furi_hal_infrared_async_tx_stop(void) {
  566. furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
  567. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  568. FURI_CRITICAL_ENTER();
  569. if(furi_hal_infrared_state == InfraredStateAsyncTx)
  570. furi_hal_infrared_state = InfraredStateAsyncTxStopReq;
  571. FURI_CRITICAL_EXIT();
  572. furi_hal_infrared_async_tx_wait_termination();
  573. }
  574. void furi_hal_infrared_async_tx_set_data_isr_callback(
  575. FuriHalInfraredTxGetDataISRCallback callback,
  576. void* context) {
  577. furi_assert(furi_hal_infrared_state == InfraredStateIdle);
  578. infrared_tim_tx.data_callback = callback;
  579. infrared_tim_tx.data_context = context;
  580. }
  581. void furi_hal_infrared_async_tx_set_signal_sent_isr_callback(
  582. FuriHalInfraredTxSignalSentISRCallback callback,
  583. void* context) {
  584. infrared_tim_tx.signal_sent_callback = callback;
  585. infrared_tim_tx.signal_sent_context = context;
  586. }