furi_hal_infrared.c 26 KB

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