furi_hal_infrared.c 26 KB

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