furi_hal_infrared.c 26 KB

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