furi_hal_infrared.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. FURI_CRITICAL_ENTER();
  121. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  122. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  123. FURI_CRITICAL_EXIT();
  124. hal_gpio_init_ex(
  125. &gpio_infrared_rx, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
  126. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  127. TIM_InitStruct.Prescaler = 64 - 1;
  128. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  129. TIM_InitStruct.Autoreload = 0x7FFFFFFE;
  130. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  131. LL_TIM_Init(TIM2, &TIM_InitStruct);
  132. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  133. LL_TIM_DisableARRPreload(TIM2);
  134. LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_TI1FP1);
  135. LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_RESET);
  136. LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  137. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
  138. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_FALLING);
  139. LL_TIM_DisableIT_TRIG(TIM2);
  140. LL_TIM_DisableDMAReq_TRIG(TIM2);
  141. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  142. LL_TIM_EnableMasterSlaveMode(TIM2);
  143. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
  144. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  145. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
  146. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
  147. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_INDIRECTTI);
  148. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  149. furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_infrared_tim_rx_isr);
  150. furi_hal_infrared_state = InfraredStateAsyncRx;
  151. LL_TIM_EnableIT_CC1(TIM2);
  152. LL_TIM_EnableIT_CC2(TIM2);
  153. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
  154. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  155. LL_TIM_SetCounter(TIM2, 0);
  156. LL_TIM_EnableCounter(TIM2);
  157. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  158. NVIC_EnableIRQ(TIM2_IRQn);
  159. }
  160. void furi_hal_infrared_async_rx_stop(void) {
  161. furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
  162. LL_TIM_DeInit(TIM2);
  163. furi_hal_interrupt_set_timer_isr(TIM2, NULL);
  164. LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2);
  165. furi_hal_infrared_state = InfraredStateIdle;
  166. }
  167. void furi_hal_infrared_async_rx_set_timeout(uint32_t timeout_us) {
  168. furi_assert(LL_APB1_GRP1_IsEnabledClock(LL_APB1_GRP1_PERIPH_TIM2));
  169. LL_TIM_OC_SetCompareCH3(TIM2, timeout_us);
  170. LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_ACTIVE);
  171. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH3);
  172. LL_TIM_EnableIT_CC3(TIM2);
  173. }
  174. bool furi_hal_infrared_is_busy(void) {
  175. return furi_hal_infrared_state != InfraredStateIdle;
  176. }
  177. void furi_hal_infrared_async_rx_set_capture_isr_callback(
  178. FuriHalInfraredRxCaptureCallback callback,
  179. void* ctx) {
  180. infrared_tim_rx.capture_callback = callback;
  181. infrared_tim_rx.capture_context = ctx;
  182. }
  183. void furi_hal_infrared_async_rx_set_timeout_isr_callback(
  184. FuriHalInfraredRxTimeoutCallback callback,
  185. void* ctx) {
  186. infrared_tim_rx.timeout_callback = callback;
  187. infrared_tim_rx.timeout_context = ctx;
  188. }
  189. static void furi_hal_infrared_tx_dma_terminate(void) {
  190. LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  191. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  192. LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_2);
  193. furi_assert(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress);
  194. LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  195. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);
  196. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
  197. LL_TIM_DisableCounter(TIM1);
  198. osStatus_t status = osSemaphoreRelease(infrared_tim_tx.stop_semaphore);
  199. furi_check(status == osOK);
  200. furi_hal_infrared_state = InfraredStateAsyncTxStopped;
  201. }
  202. static uint8_t furi_hal_infrared_get_current_dma_tx_buffer(void) {
  203. uint8_t buf_num = 0;
  204. uint32_t buffer_adr = LL_DMA_GetMemoryAddress(DMA1, LL_DMA_CHANNEL_2);
  205. if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[0].data) {
  206. buf_num = 0;
  207. } else if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[1].data) {
  208. buf_num = 1;
  209. } else {
  210. furi_assert(0);
  211. }
  212. return buf_num;
  213. }
  214. static void furi_hal_infrared_tx_dma_polarity_isr() {
  215. if(LL_DMA_IsActiveFlag_TE1(DMA1)) {
  216. LL_DMA_ClearFlag_TE1(DMA1);
  217. furi_crash(NULL);
  218. }
  219. if(LL_DMA_IsActiveFlag_TC1(DMA1) && LL_DMA_IsEnabledIT_TC(DMA1, LL_DMA_CHANNEL_1)) {
  220. LL_DMA_ClearFlag_TC1(DMA1);
  221. furi_check(
  222. (furi_hal_infrared_state == InfraredStateAsyncTx) ||
  223. (furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
  224. (furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress));
  225. /* actually TC2 is processed and buffer is next buffer */
  226. uint8_t next_buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
  227. furi_hal_infrared_tx_dma_set_polarity(next_buf_num, 0);
  228. }
  229. }
  230. static void furi_hal_infrared_tx_dma_isr() {
  231. if(LL_DMA_IsActiveFlag_TE2(DMA1)) {
  232. LL_DMA_ClearFlag_TE2(DMA1);
  233. furi_crash(NULL);
  234. }
  235. if(LL_DMA_IsActiveFlag_HT2(DMA1) && LL_DMA_IsEnabledIT_HT(DMA1, LL_DMA_CHANNEL_2)) {
  236. LL_DMA_ClearFlag_HT2(DMA1);
  237. uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
  238. uint8_t next_buf_num = !buf_num;
  239. if(infrared_tim_tx.buffer[buf_num].last_packet_end) {
  240. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  241. } else if(
  242. !infrared_tim_tx.buffer[buf_num].packet_end ||
  243. (furi_hal_infrared_state == InfraredStateAsyncTx)) {
  244. furi_hal_infrared_tx_fill_buffer(next_buf_num, 0);
  245. if(infrared_tim_tx.buffer[next_buf_num].last_packet_end) {
  246. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  247. }
  248. } else if(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) {
  249. /* fallthrough */
  250. } else {
  251. furi_crash(NULL);
  252. }
  253. }
  254. if(LL_DMA_IsActiveFlag_TC2(DMA1) && LL_DMA_IsEnabledIT_TC(DMA1, LL_DMA_CHANNEL_2)) {
  255. LL_DMA_ClearFlag_TC2(DMA1);
  256. furi_check(
  257. (furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) ||
  258. (furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
  259. (furi_hal_infrared_state == InfraredStateAsyncTx));
  260. uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
  261. uint8_t next_buf_num = !buf_num;
  262. if(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) {
  263. furi_hal_infrared_tx_dma_terminate();
  264. } else if(
  265. infrared_tim_tx.buffer[buf_num].last_packet_end ||
  266. (infrared_tim_tx.buffer[buf_num].packet_end &&
  267. (furi_hal_infrared_state == InfraredStateAsyncTxStopReq))) {
  268. furi_hal_infrared_state = InfraredStateAsyncTxStopInProgress;
  269. furi_hal_infrared_tx_fill_buffer_last(next_buf_num);
  270. furi_hal_infrared_tx_dma_set_buffer(next_buf_num);
  271. } else {
  272. /* if it's not end of the packet - continue receiving */
  273. furi_hal_infrared_tx_dma_set_buffer(next_buf_num);
  274. }
  275. if(infrared_tim_tx.signal_sent_callback && infrared_tim_tx.buffer[buf_num].packet_end &&
  276. (furi_hal_infrared_state != InfraredStateAsyncTxStopped)) {
  277. infrared_tim_tx.signal_sent_callback(infrared_tim_tx.signal_sent_context);
  278. }
  279. }
  280. }
  281. static void furi_hal_infrared_configure_tim_pwm_tx(uint32_t freq, float duty_cycle) {
  282. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
  283. /* LL_DBGMCU_APB2_GRP1_FreezePeriph(LL_DBGMCU_APB2_GRP1_TIM1_STOP); */
  284. LL_TIM_DisableCounter(TIM1);
  285. LL_TIM_SetRepetitionCounter(TIM1, 0);
  286. LL_TIM_SetCounter(TIM1, 0);
  287. LL_TIM_SetPrescaler(TIM1, 0);
  288. LL_TIM_SetCounterMode(TIM1, LL_TIM_COUNTERMODE_UP);
  289. LL_TIM_EnableARRPreload(TIM1);
  290. LL_TIM_SetAutoReload(
  291. TIM1, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM1), freq));
  292. #if INFRARED_TX_DEBUG == 1
  293. LL_TIM_OC_SetCompareCH1(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
  294. LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
  295. /* LL_TIM_OCMODE_PWM2 set by DMA */
  296. LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_INACTIVE);
  297. LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH1N, LL_TIM_OCPOLARITY_HIGH);
  298. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
  299. LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N);
  300. LL_TIM_DisableIT_CC1(TIM1);
  301. #else
  302. LL_TIM_OC_SetCompareCH3(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
  303. LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH3);
  304. /* LL_TIM_OCMODE_PWM2 set by DMA */
  305. LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_FORCED_INACTIVE);
  306. LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH3N, LL_TIM_OCPOLARITY_HIGH);
  307. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH3);
  308. LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH3N);
  309. LL_TIM_DisableIT_CC3(TIM1);
  310. #endif
  311. LL_TIM_DisableMasterSlaveMode(TIM1);
  312. LL_TIM_EnableAllOutputs(TIM1);
  313. LL_TIM_DisableIT_UPDATE(TIM1);
  314. LL_TIM_EnableDMAReq_UPDATE(TIM1);
  315. NVIC_SetPriority(TIM1_UP_TIM16_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  316. NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
  317. }
  318. static void furi_hal_infrared_configure_tim_cmgr2_dma_tx(void) {
  319. LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  320. LL_DMA_InitTypeDef dma_config = {0};
  321. #if INFRARED_TX_DEBUG == 1
  322. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR1);
  323. #else
  324. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR2);
  325. #endif
  326. dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
  327. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  328. dma_config.Mode = LL_DMA_MODE_NORMAL;
  329. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  330. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  331. /* fill word to have other bits set to 0 */
  332. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
  333. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  334. dma_config.NbData = 0;
  335. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  336. dma_config.Priority = LL_DMA_PRIORITY_VERYHIGH;
  337. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &dma_config);
  338. furi_hal_interrupt_set_dma_channel_isr(
  339. DMA1, LL_DMA_CHANNEL_1, furi_hal_infrared_tx_dma_polarity_isr);
  340. LL_DMA_ClearFlag_TE1(DMA1);
  341. LL_DMA_ClearFlag_TC1(DMA1);
  342. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
  343. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  344. NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 4, 0));
  345. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  346. }
  347. static void furi_hal_infrared_configure_tim_rcr_dma_tx(void) {
  348. LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  349. LL_DMA_InitTypeDef dma_config = {0};
  350. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->RCR);
  351. dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
  352. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  353. dma_config.Mode = LL_DMA_MODE_NORMAL;
  354. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  355. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  356. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_HALFWORD;
  357. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD;
  358. dma_config.NbData = 0;
  359. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  360. dma_config.Priority = LL_DMA_PRIORITY_MEDIUM;
  361. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_2, &dma_config);
  362. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, furi_hal_infrared_tx_dma_isr);
  363. LL_DMA_ClearFlag_TC2(DMA1);
  364. LL_DMA_ClearFlag_HT2(DMA1);
  365. LL_DMA_ClearFlag_TE2(DMA1);
  366. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_2);
  367. LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  368. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_2);
  369. NVIC_SetPriority(DMA1_Channel2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  370. NVIC_EnableIRQ(DMA1_Channel2_IRQn);
  371. }
  372. static void furi_hal_infrared_tx_fill_buffer_last(uint8_t buf_num) {
  373. furi_assert(buf_num < 2);
  374. furi_assert(furi_hal_infrared_state != InfraredStateAsyncRx);
  375. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  376. furi_assert(infrared_tim_tx.data_callback);
  377. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  378. furi_assert(buffer->data != NULL);
  379. (void)buffer->data;
  380. furi_assert(buffer->polarity != NULL);
  381. (void)buffer->polarity;
  382. infrared_tim_tx.buffer[buf_num].data[0] = 0; // 1 pulse
  383. infrared_tim_tx.buffer[buf_num].polarity[0] = INFRARED_TX_CCMR_LOW;
  384. infrared_tim_tx.buffer[buf_num].data[1] = 0; // 1 pulse
  385. infrared_tim_tx.buffer[buf_num].polarity[1] = INFRARED_TX_CCMR_LOW;
  386. infrared_tim_tx.buffer[buf_num].size = 2;
  387. infrared_tim_tx.buffer[buf_num].last_packet_end = true;
  388. infrared_tim_tx.buffer[buf_num].packet_end = true;
  389. }
  390. static void furi_hal_infrared_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
  391. furi_assert(buf_num < 2);
  392. furi_assert(furi_hal_infrared_state != InfraredStateAsyncRx);
  393. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  394. furi_assert(infrared_tim_tx.data_callback);
  395. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  396. furi_assert(buffer->data != NULL);
  397. furi_assert(buffer->polarity != NULL);
  398. FuriHalInfraredTxGetDataState status = FuriHalInfraredTxGetDataStateOk;
  399. uint32_t duration = 0;
  400. bool level = 0;
  401. size_t* size = &buffer->size;
  402. size_t polarity_counter = 0;
  403. while(polarity_shift--) {
  404. buffer->polarity[polarity_counter++] = INFRARED_TX_CCMR_LOW;
  405. }
  406. for(*size = 0; (*size < INFRARED_TIM_TX_DMA_BUFFER_SIZE) &&
  407. (status == FuriHalInfraredTxGetDataStateOk);) {
  408. if(infrared_tim_tx.tx_timing_rest_duration > 0) {
  409. if(infrared_tim_tx.tx_timing_rest_duration > 0xFFFF) {
  410. buffer->data[*size] = 0xFFFF;
  411. status = FuriHalInfraredTxGetDataStateOk;
  412. } else {
  413. buffer->data[*size] = infrared_tim_tx.tx_timing_rest_duration;
  414. status = infrared_tim_tx.tx_timing_rest_status;
  415. }
  416. infrared_tim_tx.tx_timing_rest_duration -= buffer->data[*size];
  417. buffer->polarity[polarity_counter] = infrared_tim_tx.tx_timing_rest_level ?
  418. INFRARED_TX_CCMR_HIGH :
  419. INFRARED_TX_CCMR_LOW;
  420. ++(*size);
  421. ++polarity_counter;
  422. continue;
  423. }
  424. status = infrared_tim_tx.data_callback(infrared_tim_tx.data_context, &duration, &level);
  425. uint32_t num_of_impulses = roundf(duration / infrared_tim_tx.cycle_duration);
  426. if(num_of_impulses == 0) {
  427. if((*size == 0) && (status == FuriHalInfraredTxGetDataStateDone)) {
  428. /* if this is one sample in current buffer, but we
  429. * have more to send - continue
  430. */
  431. status = FuriHalInfraredTxGetDataStateOk;
  432. }
  433. } else if((num_of_impulses - 1) > 0xFFFF) {
  434. infrared_tim_tx.tx_timing_rest_duration = num_of_impulses - 1;
  435. infrared_tim_tx.tx_timing_rest_status = status;
  436. infrared_tim_tx.tx_timing_rest_level = level;
  437. status = FuriHalInfraredTxGetDataStateOk;
  438. } else {
  439. buffer->polarity[polarity_counter] = level ? INFRARED_TX_CCMR_HIGH :
  440. INFRARED_TX_CCMR_LOW;
  441. buffer->data[*size] = num_of_impulses - 1;
  442. ++(*size);
  443. ++polarity_counter;
  444. }
  445. }
  446. buffer->last_packet_end = (status == FuriHalInfraredTxGetDataStateLastDone);
  447. buffer->packet_end = buffer->last_packet_end || (status == FuriHalInfraredTxGetDataStateDone);
  448. if(*size == 0) {
  449. buffer->data[0] = 0; // 1 pulse
  450. buffer->polarity[0] = INFRARED_TX_CCMR_LOW;
  451. buffer->size = 1;
  452. }
  453. }
  454. static void furi_hal_infrared_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
  455. furi_assert(buf_num < 2);
  456. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  457. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  458. furi_assert(buffer->polarity != NULL);
  459. FURI_CRITICAL_ENTER();
  460. bool channel_enabled = LL_DMA_IsEnabledChannel(DMA1, LL_DMA_CHANNEL_1);
  461. if(channel_enabled) {
  462. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
  463. }
  464. LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_1, (uint32_t)buffer->polarity);
  465. LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, buffer->size + polarity_shift);
  466. if(channel_enabled) {
  467. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  468. }
  469. FURI_CRITICAL_EXIT();
  470. }
  471. static void furi_hal_infrared_tx_dma_set_buffer(uint8_t buf_num) {
  472. furi_assert(buf_num < 2);
  473. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  474. InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
  475. furi_assert(buffer->data != NULL);
  476. /* non-circular mode requires disabled channel before setup */
  477. FURI_CRITICAL_ENTER();
  478. bool channel_enabled = LL_DMA_IsEnabledChannel(DMA1, LL_DMA_CHANNEL_2);
  479. if(channel_enabled) {
  480. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);
  481. }
  482. LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_2, (uint32_t)buffer->data);
  483. LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_2, buffer->size);
  484. if(channel_enabled) {
  485. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
  486. }
  487. FURI_CRITICAL_EXIT();
  488. }
  489. static void furi_hal_infrared_async_tx_free_resources(void) {
  490. furi_assert(
  491. (furi_hal_infrared_state == InfraredStateIdle) ||
  492. (furi_hal_infrared_state == InfraredStateAsyncTxStopped));
  493. osStatus_t status;
  494. hal_gpio_init(&gpio_infrared_tx, GpioModeOutputOpenDrain, GpioPullDown, GpioSpeedLow);
  495. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_1, NULL);
  496. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, NULL);
  497. LL_TIM_DeInit(TIM1);
  498. LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_TIM1);
  499. LL_C2_AHB1_GRP1_DisableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  500. status = osSemaphoreDelete(infrared_tim_tx.stop_semaphore);
  501. furi_check(status == osOK);
  502. free(infrared_tim_tx.buffer[0].data);
  503. free(infrared_tim_tx.buffer[1].data);
  504. free(infrared_tim_tx.buffer[0].polarity);
  505. free(infrared_tim_tx.buffer[1].polarity);
  506. infrared_tim_tx.buffer[0].data = NULL;
  507. infrared_tim_tx.buffer[1].data = NULL;
  508. infrared_tim_tx.buffer[0].polarity = NULL;
  509. infrared_tim_tx.buffer[1].polarity = NULL;
  510. }
  511. void furi_hal_infrared_async_tx_start(uint32_t freq, float duty_cycle) {
  512. if((duty_cycle > 1) || (duty_cycle <= 0) || (freq > INFRARED_MAX_FREQUENCY) ||
  513. (freq < INFRARED_MIN_FREQUENCY) || (infrared_tim_tx.data_callback == NULL)) {
  514. furi_crash(NULL);
  515. }
  516. furi_assert(furi_hal_infrared_state == InfraredStateIdle);
  517. furi_assert(infrared_tim_tx.buffer[0].data == NULL);
  518. furi_assert(infrared_tim_tx.buffer[1].data == NULL);
  519. furi_assert(infrared_tim_tx.buffer[0].polarity == NULL);
  520. furi_assert(infrared_tim_tx.buffer[1].polarity == NULL);
  521. size_t alloc_size_data = INFRARED_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
  522. infrared_tim_tx.buffer[0].data = malloc(alloc_size_data);
  523. infrared_tim_tx.buffer[1].data = malloc(alloc_size_data);
  524. size_t alloc_size_polarity =
  525. (INFRARED_TIM_TX_DMA_BUFFER_SIZE + INFRARED_POLARITY_SHIFT) * sizeof(uint8_t);
  526. infrared_tim_tx.buffer[0].polarity = malloc(alloc_size_polarity);
  527. infrared_tim_tx.buffer[1].polarity = malloc(alloc_size_polarity);
  528. infrared_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
  529. infrared_tim_tx.cycle_duration = 1000000.0 / freq;
  530. infrared_tim_tx.tx_timing_rest_duration = 0;
  531. furi_hal_infrared_tx_fill_buffer(0, INFRARED_POLARITY_SHIFT);
  532. furi_hal_infrared_configure_tim_pwm_tx(freq, duty_cycle);
  533. furi_hal_infrared_configure_tim_cmgr2_dma_tx();
  534. furi_hal_infrared_configure_tim_rcr_dma_tx();
  535. furi_hal_infrared_tx_dma_set_polarity(0, INFRARED_POLARITY_SHIFT);
  536. furi_hal_infrared_tx_dma_set_buffer(0);
  537. furi_hal_infrared_state = InfraredStateAsyncTx;
  538. LL_TIM_ClearFlag_UPDATE(TIM1);
  539. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  540. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
  541. delay_us(5);
  542. LL_TIM_GenerateEvent_UPDATE(TIM1); /* DMA -> TIMx_RCR */
  543. delay_us(5);
  544. LL_GPIO_ResetOutputPin(
  545. gpio_infrared_tx.port, gpio_infrared_tx.pin); /* when disable it prevents false pulse */
  546. hal_gpio_init_ex(
  547. &gpio_infrared_tx, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1);
  548. FURI_CRITICAL_ENTER();
  549. LL_TIM_GenerateEvent_UPDATE(TIM1); /* TIMx_RCR -> Repetition counter */
  550. LL_TIM_EnableCounter(TIM1);
  551. FURI_CRITICAL_EXIT();
  552. }
  553. void furi_hal_infrared_async_tx_wait_termination(void) {
  554. furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
  555. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  556. osStatus_t status;
  557. status = osSemaphoreAcquire(infrared_tim_tx.stop_semaphore, osWaitForever);
  558. furi_check(status == osOK);
  559. furi_hal_infrared_async_tx_free_resources();
  560. furi_hal_infrared_state = InfraredStateIdle;
  561. }
  562. void furi_hal_infrared_async_tx_stop(void) {
  563. furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
  564. furi_assert(furi_hal_infrared_state < InfraredStateMAX);
  565. FURI_CRITICAL_ENTER();
  566. if(furi_hal_infrared_state == InfraredStateAsyncTx)
  567. furi_hal_infrared_state = InfraredStateAsyncTxStopReq;
  568. FURI_CRITICAL_EXIT();
  569. furi_hal_infrared_async_tx_wait_termination();
  570. }
  571. void furi_hal_infrared_async_tx_set_data_isr_callback(
  572. FuriHalInfraredTxGetDataISRCallback callback,
  573. void* context) {
  574. furi_assert(furi_hal_infrared_state == InfraredStateIdle);
  575. infrared_tim_tx.data_callback = callback;
  576. infrared_tim_tx.data_context = context;
  577. }
  578. void furi_hal_infrared_async_tx_set_signal_sent_isr_callback(
  579. FuriHalInfraredTxSignalSentISRCallback callback,
  580. void* context) {
  581. infrared_tim_tx.signal_sent_callback = callback;
  582. infrared_tim_tx.signal_sent_context = context;
  583. }