furi_hal_infrared.c 27 KB

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