furi-hal-irda.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #include "furi-hal-irda.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 IRDA_TIM_TX_DMA_BUFFER_SIZE 200
  18. #define IRDA_POLARITY_SHIFT 1
  19. #define IRDA_TX_CCMR_HIGH (TIM_CCMR2_OC3PE | LL_TIM_OCMODE_PWM2) /* Mark time - enable PWM2 mode */
  20. #define IRDA_TX_CCMR_LOW (TIM_CCMR2_OC3PE | LL_TIM_OCMODE_FORCED_INACTIVE) /* Space time - force low */
  21. typedef struct{
  22. FuriHalIrdaRxCaptureCallback capture_callback;
  23. void *capture_context;
  24. FuriHalIrdaRxTimeoutCallback timeout_callback;
  25. void *timeout_context;
  26. } IrdaTimRx;
  27. typedef struct{
  28. uint8_t* polarity;
  29. uint16_t* data;
  30. size_t size;
  31. bool packet_end;
  32. bool last_packet_end;
  33. } IrdaTxBuf;
  34. typedef struct {
  35. float cycle_duration;
  36. FuriHalIrdaTxGetDataISRCallback data_callback;
  37. FuriHalIrdaTxSignalSentISRCallback signal_sent_callback;
  38. void* data_context;
  39. void* signal_sent_context;
  40. IrdaTxBuf buffer[2];
  41. osSemaphoreId_t stop_semaphore;
  42. } IrdaTimTx;
  43. typedef enum {
  44. IrdaStateIdle, /** Furi Hal Irda is ready to start RX or TX */
  45. IrdaStateAsyncRx, /** Async RX started */
  46. IrdaStateAsyncTx, /** Async TX started, DMA and timer is on */
  47. IrdaStateAsyncTxStopReq, /** Async TX started, async stop request received */
  48. IrdaStateAsyncTxStopInProgress, /** Async TX started, stop request is processed and we wait for last data to be sent */
  49. IrdaStateAsyncTxStopped, /** Async TX complete, cleanup needed */
  50. IrdaStateMAX,
  51. } IrdaState;
  52. static volatile IrdaState furi_hal_irda_state = IrdaStateIdle;
  53. static IrdaTimTx irda_tim_tx;
  54. static IrdaTimRx irda_tim_rx;
  55. static bool furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift);
  56. static void furi_hal_irda_async_tx_free_resources(void);
  57. static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift);
  58. static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num);
  59. static void furi_hal_irda_tx_fill_buffer_last(uint8_t buf_num);
  60. static uint8_t furi_hal_irda_get_current_dma_tx_buffer(void);
  61. static void furi_hal_irda_tx_dma_polarity_isr();
  62. static void furi_hal_irda_tx_dma_isr();
  63. static void furi_hal_irda_tim_rx_isr() {
  64. /* Timeout */
  65. if(LL_TIM_IsActiveFlag_CC3(TIM2)) {
  66. LL_TIM_ClearFlag_CC3(TIM2);
  67. furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
  68. /* Timers CNT register starts to counting from 0 to ARR, but it is
  69. * reseted when Channel 1 catches interrupt. It is not reseted by
  70. * channel 2, though, so we have to distract it's values (see TimerIRQSourceCCI1 ISR).
  71. * This can cause false timeout: when time is over, but we started
  72. * receiving new signal few microseconds ago, because CNT register
  73. * is reseted once per period, not per sample. */
  74. if (LL_GPIO_IsInputPinSet(gpio_irda_rx.port, gpio_irda_rx.pin) != 0) {
  75. if (irda_tim_rx.timeout_callback)
  76. irda_tim_rx.timeout_callback(irda_tim_rx.timeout_context);
  77. }
  78. }
  79. /* Rising Edge */
  80. if(LL_TIM_IsActiveFlag_CC1(TIM2)) {
  81. LL_TIM_ClearFlag_CC1(TIM2);
  82. furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
  83. if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
  84. /* Low pin level is a Mark state of IRDA signal. Invert level for further processing. */
  85. uint32_t duration = LL_TIM_IC_GetCaptureCH1(TIM2) - LL_TIM_IC_GetCaptureCH2(TIM2);
  86. if (irda_tim_rx.capture_callback)
  87. irda_tim_rx.capture_callback(irda_tim_rx.capture_context, 1, duration);
  88. } else {
  89. furi_assert(0);
  90. }
  91. }
  92. /* Falling Edge */
  93. if(LL_TIM_IsActiveFlag_CC2(TIM2)) {
  94. LL_TIM_ClearFlag_CC2(TIM2);
  95. furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
  96. if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
  97. /* High pin level is a Space state of IRDA signal. Invert level for further processing. */
  98. uint32_t duration = LL_TIM_IC_GetCaptureCH2(TIM2);
  99. if (irda_tim_rx.capture_callback)
  100. irda_tim_rx.capture_callback(irda_tim_rx.capture_context, 0, duration);
  101. } else {
  102. furi_assert(0);
  103. }
  104. }
  105. }
  106. void furi_hal_irda_async_rx_start(void) {
  107. furi_assert(furi_hal_irda_state == IrdaStateIdle);
  108. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  109. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  110. hal_gpio_init_ex(&gpio_irda_rx, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
  111. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  112. TIM_InitStruct.Prescaler = 64 - 1;
  113. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  114. TIM_InitStruct.Autoreload = 0x7FFFFFFE;
  115. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  116. LL_TIM_Init(TIM2, &TIM_InitStruct);
  117. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  118. LL_TIM_DisableARRPreload(TIM2);
  119. LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_TI1FP1);
  120. LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_RESET);
  121. LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  122. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
  123. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_FALLING);
  124. LL_TIM_DisableIT_TRIG(TIM2);
  125. LL_TIM_DisableDMAReq_TRIG(TIM2);
  126. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  127. LL_TIM_EnableMasterSlaveMode(TIM2);
  128. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
  129. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  130. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
  131. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
  132. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_INDIRECTTI);
  133. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  134. furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_irda_tim_rx_isr);
  135. furi_hal_irda_state = IrdaStateAsyncRx;
  136. LL_TIM_EnableIT_CC1(TIM2);
  137. LL_TIM_EnableIT_CC2(TIM2);
  138. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
  139. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  140. LL_TIM_SetCounter(TIM2, 0);
  141. LL_TIM_EnableCounter(TIM2);
  142. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  143. NVIC_EnableIRQ(TIM2_IRQn);
  144. }
  145. void furi_hal_irda_async_rx_stop(void) {
  146. furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
  147. LL_TIM_DeInit(TIM2);
  148. furi_hal_interrupt_set_timer_isr(TIM2, NULL);
  149. LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2);
  150. furi_hal_irda_state = IrdaStateIdle;
  151. }
  152. void furi_hal_irda_async_rx_set_timeout(uint32_t timeout_us) {
  153. furi_assert(LL_APB1_GRP1_IsEnabledClock(LL_APB1_GRP1_PERIPH_TIM2));
  154. LL_TIM_OC_SetCompareCH3(TIM2, timeout_us);
  155. LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_ACTIVE);
  156. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH3);
  157. LL_TIM_EnableIT_CC3(TIM2);
  158. }
  159. bool furi_hal_irda_is_busy(void) {
  160. return furi_hal_irda_state != IrdaStateIdle;
  161. }
  162. void furi_hal_irda_async_rx_set_capture_isr_callback(FuriHalIrdaRxCaptureCallback callback, void *ctx) {
  163. irda_tim_rx.capture_callback = callback;
  164. irda_tim_rx.capture_context = ctx;
  165. }
  166. void furi_hal_irda_async_rx_set_timeout_isr_callback(FuriHalIrdaRxTimeoutCallback callback, void *ctx) {
  167. irda_tim_rx.timeout_callback = callback;
  168. irda_tim_rx.timeout_context = ctx;
  169. }
  170. static void furi_hal_irda_tx_dma_terminate(void) {
  171. LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  172. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  173. LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_2);
  174. furi_assert(furi_hal_irda_state == IrdaStateAsyncTxStopInProgress);
  175. LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  176. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);
  177. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
  178. LL_TIM_DisableCounter(TIM1);
  179. osStatus_t status = osSemaphoreRelease(irda_tim_tx.stop_semaphore);
  180. furi_check(status == osOK);
  181. furi_hal_irda_state = IrdaStateAsyncTxStopped;
  182. }
  183. static uint8_t furi_hal_irda_get_current_dma_tx_buffer(void) {
  184. uint8_t buf_num = 0;
  185. uint32_t buffer_adr = LL_DMA_GetMemoryAddress(DMA1, LL_DMA_CHANNEL_2);
  186. if (buffer_adr == (uint32_t) irda_tim_tx.buffer[0].data) {
  187. buf_num = 0;
  188. } else if (buffer_adr == (uint32_t) irda_tim_tx.buffer[1].data) {
  189. buf_num = 1;
  190. } else {
  191. furi_assert(0);
  192. }
  193. return buf_num;
  194. }
  195. static void furi_hal_irda_tx_dma_polarity_isr() {
  196. if (LL_DMA_IsActiveFlag_TE1(DMA1)) {
  197. LL_DMA_ClearFlag_TE1(DMA1);
  198. furi_check(0);
  199. }
  200. if (LL_DMA_IsActiveFlag_TC1(DMA1) && LL_DMA_IsEnabledIT_TC(DMA1, LL_DMA_CHANNEL_1)) {
  201. LL_DMA_ClearFlag_TC1(DMA1);
  202. furi_check((furi_hal_irda_state == IrdaStateAsyncTx)
  203. || (furi_hal_irda_state == IrdaStateAsyncTxStopReq)
  204. || (furi_hal_irda_state == IrdaStateAsyncTxStopInProgress));
  205. /* actually TC2 is processed and buffer is next buffer */
  206. uint8_t next_buf_num = furi_hal_irda_get_current_dma_tx_buffer();
  207. furi_hal_irda_tx_dma_set_polarity(next_buf_num, 0);
  208. }
  209. }
  210. static void furi_hal_irda_tx_dma_isr() {
  211. if (LL_DMA_IsActiveFlag_TE2(DMA1)) {
  212. LL_DMA_ClearFlag_TE2(DMA1);
  213. furi_check(0);
  214. }
  215. if (LL_DMA_IsActiveFlag_HT2(DMA1) && LL_DMA_IsEnabledIT_HT(DMA1, LL_DMA_CHANNEL_2)) {
  216. LL_DMA_ClearFlag_HT2(DMA1);
  217. uint8_t buf_num = furi_hal_irda_get_current_dma_tx_buffer();
  218. uint8_t next_buf_num = !buf_num;
  219. if (irda_tim_tx.buffer[buf_num].last_packet_end) {
  220. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  221. } else if (!irda_tim_tx.buffer[buf_num].packet_end || (furi_hal_irda_state == IrdaStateAsyncTx)) {
  222. bool result = furi_hal_irda_tx_fill_buffer(next_buf_num, 0);
  223. if (irda_tim_tx.buffer[next_buf_num].last_packet_end) {
  224. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  225. }
  226. if (!result) {
  227. furi_assert(0);
  228. furi_hal_irda_state = IrdaStateAsyncTxStopReq;
  229. }
  230. } else if (furi_hal_irda_state == IrdaStateAsyncTxStopReq) {
  231. /* fallthrough */
  232. } else {
  233. furi_check(0);
  234. }
  235. }
  236. if (LL_DMA_IsActiveFlag_TC2(DMA1) && LL_DMA_IsEnabledIT_TC(DMA1, LL_DMA_CHANNEL_2)) {
  237. LL_DMA_ClearFlag_TC2(DMA1);
  238. furi_check((furi_hal_irda_state == IrdaStateAsyncTxStopInProgress)
  239. || (furi_hal_irda_state == IrdaStateAsyncTxStopReq)
  240. || (furi_hal_irda_state == IrdaStateAsyncTx));
  241. uint8_t buf_num = furi_hal_irda_get_current_dma_tx_buffer();
  242. uint8_t next_buf_num = !buf_num;
  243. if (furi_hal_irda_state == IrdaStateAsyncTxStopInProgress) {
  244. furi_hal_irda_tx_dma_terminate();
  245. } else if (irda_tim_tx.buffer[buf_num].last_packet_end
  246. || (irda_tim_tx.buffer[buf_num].packet_end && (furi_hal_irda_state == IrdaStateAsyncTxStopReq))) {
  247. furi_hal_irda_state = IrdaStateAsyncTxStopInProgress;
  248. furi_hal_irda_tx_fill_buffer_last(next_buf_num);
  249. furi_hal_irda_tx_dma_set_buffer(next_buf_num);
  250. } else {
  251. /* if it's not end of the packet - continue receiving */
  252. furi_hal_irda_tx_dma_set_buffer(next_buf_num);
  253. }
  254. if (irda_tim_tx.signal_sent_callback) {
  255. irda_tim_tx.signal_sent_callback(irda_tim_tx.signal_sent_context);
  256. }
  257. }
  258. }
  259. static void furi_hal_irda_configure_tim_pwm_tx(uint32_t freq, float duty_cycle)
  260. {
  261. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
  262. /* LL_DBGMCU_APB2_GRP1_FreezePeriph(LL_DBGMCU_APB2_GRP1_TIM1_STOP); */
  263. LL_TIM_DisableCounter(TIM1);
  264. LL_TIM_SetRepetitionCounter(TIM1, 0);
  265. LL_TIM_SetCounter(TIM1, 0);
  266. LL_TIM_SetPrescaler(TIM1, 0);
  267. LL_TIM_SetCounterMode(TIM1, LL_TIM_COUNTERMODE_UP);
  268. LL_TIM_EnableARRPreload(TIM1);
  269. LL_TIM_SetAutoReload(TIM1, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM1), freq));
  270. LL_TIM_OC_SetCompareCH3(TIM1, ( (LL_TIM_GetAutoReload(TIM1) + 1 ) * (1 - duty_cycle)));
  271. LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH3);
  272. /* LL_TIM_OCMODE_PWM2 set by DMA */
  273. LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_FORCED_INACTIVE);
  274. LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH3N, LL_TIM_OCPOLARITY_HIGH);
  275. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH3);
  276. LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH3N);
  277. LL_TIM_DisableIT_CC3(TIM1);
  278. LL_TIM_DisableMasterSlaveMode(TIM1);
  279. LL_TIM_EnableAllOutputs(TIM1);
  280. LL_TIM_DisableIT_UPDATE(TIM1);
  281. LL_TIM_EnableDMAReq_UPDATE(TIM1);
  282. NVIC_SetPriority(TIM1_UP_TIM16_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  283. NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
  284. }
  285. static void furi_hal_irda_configure_tim_cmgr2_dma_tx(void) {
  286. LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  287. LL_DMA_InitTypeDef dma_config = {0};
  288. dma_config.PeriphOrM2MSrcAddress = (uint32_t)&(TIM1->CCMR2);
  289. dma_config.MemoryOrM2MDstAddress = (uint32_t) NULL;
  290. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  291. dma_config.Mode = LL_DMA_MODE_NORMAL;
  292. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  293. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  294. /* fill word to have other bits set to 0 */
  295. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
  296. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  297. dma_config.NbData = 0;
  298. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  299. dma_config.Priority = LL_DMA_PRIORITY_VERYHIGH;
  300. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &dma_config);
  301. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_1, furi_hal_irda_tx_dma_polarity_isr);
  302. LL_DMA_ClearFlag_TE1(DMA1);
  303. LL_DMA_ClearFlag_TC1(DMA1);
  304. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
  305. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  306. NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 4, 0));
  307. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  308. }
  309. static void furi_hal_irda_configure_tim_rcr_dma_tx(void) {
  310. LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  311. LL_DMA_InitTypeDef dma_config = {0};
  312. dma_config.PeriphOrM2MSrcAddress = (uint32_t)&(TIM1->RCR);
  313. dma_config.MemoryOrM2MDstAddress = (uint32_t) NULL;
  314. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  315. dma_config.Mode = LL_DMA_MODE_NORMAL;
  316. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  317. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  318. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_HALFWORD;
  319. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD;
  320. dma_config.NbData = 0;
  321. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  322. dma_config.Priority = LL_DMA_PRIORITY_MEDIUM;
  323. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_2, &dma_config);
  324. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, furi_hal_irda_tx_dma_isr);
  325. LL_DMA_ClearFlag_TC2(DMA1);
  326. LL_DMA_ClearFlag_HT2(DMA1);
  327. LL_DMA_ClearFlag_TE2(DMA1);
  328. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_2);
  329. LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  330. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_2);
  331. NVIC_SetPriority(DMA1_Channel2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  332. NVIC_EnableIRQ(DMA1_Channel2_IRQn);
  333. }
  334. static void furi_hal_irda_tx_fill_buffer_last(uint8_t buf_num) {
  335. furi_assert(buf_num < 2);
  336. furi_assert(furi_hal_irda_state != IrdaStateAsyncRx);
  337. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  338. furi_assert(irda_tim_tx.data_callback);
  339. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  340. furi_assert(buffer->data != NULL);
  341. furi_assert(buffer->polarity != NULL);
  342. irda_tim_tx.buffer[buf_num].data[0] = 0; // 1 pulse
  343. irda_tim_tx.buffer[buf_num].polarity[0] = IRDA_TX_CCMR_LOW;
  344. irda_tim_tx.buffer[buf_num].data[1] = 0; // 1 pulse
  345. irda_tim_tx.buffer[buf_num].polarity[1] = IRDA_TX_CCMR_LOW;
  346. irda_tim_tx.buffer[buf_num].size = 2;
  347. irda_tim_tx.buffer[buf_num].last_packet_end = true;
  348. irda_tim_tx.buffer[buf_num].packet_end = true;
  349. }
  350. static bool furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
  351. furi_assert(buf_num < 2);
  352. furi_assert(furi_hal_irda_state != IrdaStateAsyncRx);
  353. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  354. furi_assert(irda_tim_tx.data_callback);
  355. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  356. furi_assert(buffer->data != NULL);
  357. furi_assert(buffer->polarity != NULL);
  358. FuriHalIrdaTxGetDataState status = FuriHalIrdaTxGetDataStateOk;
  359. uint32_t duration = 0;
  360. bool level = 0;
  361. size_t *size = &buffer->size;
  362. size_t polarity_counter = 0;
  363. while (polarity_shift--) {
  364. buffer->polarity[polarity_counter++] = IRDA_TX_CCMR_LOW;
  365. }
  366. for (*size = 0; (*size < IRDA_TIM_TX_DMA_BUFFER_SIZE) && (status == FuriHalIrdaTxGetDataStateOk); ++(*size), ++polarity_counter) {
  367. status = irda_tim_tx.data_callback(irda_tim_tx.data_context, &duration, &level);
  368. if (status == FuriHalIrdaTxGetDataStateError) {
  369. furi_assert(0);
  370. break;
  371. }
  372. uint32_t num_of_impulses = roundf(duration / irda_tim_tx.cycle_duration);
  373. if ((buffer->data[*size] + num_of_impulses - 1) > 0xFFFF) {
  374. furi_assert(0);
  375. status = FuriHalIrdaTxGetDataStateError;
  376. break;
  377. }
  378. buffer->polarity[polarity_counter] = level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
  379. buffer->data[*size] = num_of_impulses - 1;
  380. }
  381. buffer->last_packet_end = (status == FuriHalIrdaTxGetDataStateLastDone);
  382. buffer->packet_end = buffer->last_packet_end || (status == FuriHalIrdaTxGetDataStateDone);
  383. return status != FuriHalIrdaTxGetDataStateError;
  384. }
  385. static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
  386. furi_assert(buf_num < 2);
  387. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  388. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  389. furi_assert(buffer->polarity != NULL);
  390. __disable_irq();
  391. bool channel_enabled = LL_DMA_IsEnabledChannel(DMA1, LL_DMA_CHANNEL_1);
  392. if (channel_enabled) {
  393. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
  394. }
  395. LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_1, (uint32_t) buffer->polarity);
  396. LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, buffer->size + polarity_shift);
  397. if (channel_enabled) {
  398. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  399. }
  400. __enable_irq();
  401. }
  402. static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num) {
  403. furi_assert(buf_num < 2);
  404. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  405. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  406. furi_assert(buffer->data != NULL);
  407. /* non-circular mode requires disabled channel before setup */
  408. __disable_irq();
  409. bool channel_enabled = LL_DMA_IsEnabledChannel(DMA1, LL_DMA_CHANNEL_2);
  410. if (channel_enabled) {
  411. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);
  412. }
  413. LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_2, (uint32_t)buffer->data);
  414. LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_2, buffer->size);
  415. if (channel_enabled) {
  416. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
  417. }
  418. __enable_irq();
  419. }
  420. static void furi_hal_irda_async_tx_free_resources(void) {
  421. furi_assert((furi_hal_irda_state == IrdaStateIdle) || (furi_hal_irda_state == IrdaStateAsyncTxStopped));
  422. osStatus_t status;
  423. hal_gpio_init(&gpio_irda_tx, GpioModeOutputOpenDrain, GpioPullDown, GpioSpeedLow);
  424. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_1, NULL);
  425. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, NULL);
  426. LL_TIM_DeInit(TIM1);
  427. LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_TIM1);
  428. LL_C2_AHB1_GRP1_DisableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  429. status = osSemaphoreDelete(irda_tim_tx.stop_semaphore);
  430. furi_check(status == osOK);
  431. free(irda_tim_tx.buffer[0].data);
  432. free(irda_tim_tx.buffer[1].data);
  433. free(irda_tim_tx.buffer[0].polarity);
  434. free(irda_tim_tx.buffer[1].polarity);
  435. irda_tim_tx.buffer[0].data = NULL;
  436. irda_tim_tx.buffer[1].data = NULL;
  437. irda_tim_tx.buffer[0].polarity = NULL;
  438. irda_tim_tx.buffer[1].polarity = NULL;
  439. }
  440. bool furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
  441. if ((duty_cycle > 1) || (duty_cycle < 0) || (freq > 40000) || (freq < 10000) || (irda_tim_tx.data_callback == NULL)) {
  442. furi_assert(0);
  443. return false;
  444. }
  445. furi_assert(furi_hal_irda_state == IrdaStateIdle);
  446. furi_assert(irda_tim_tx.buffer[0].data == NULL);
  447. furi_assert(irda_tim_tx.buffer[1].data == NULL);
  448. furi_assert(irda_tim_tx.buffer[0].polarity == NULL);
  449. furi_assert(irda_tim_tx.buffer[1].polarity == NULL);
  450. size_t alloc_size_data = IRDA_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
  451. irda_tim_tx.buffer[0].data = furi_alloc(alloc_size_data);
  452. irda_tim_tx.buffer[1].data = furi_alloc(alloc_size_data);
  453. size_t alloc_size_polarity = (IRDA_TIM_TX_DMA_BUFFER_SIZE + IRDA_POLARITY_SHIFT) * sizeof(uint8_t);
  454. irda_tim_tx.buffer[0].polarity = furi_alloc(alloc_size_polarity);
  455. irda_tim_tx.buffer[1].polarity = furi_alloc(alloc_size_polarity);
  456. irda_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
  457. irda_tim_tx.cycle_duration = 1000000.0 / freq;
  458. bool result = furi_hal_irda_tx_fill_buffer(0, IRDA_POLARITY_SHIFT);
  459. if (result) {
  460. furi_hal_irda_configure_tim_pwm_tx(freq, duty_cycle);
  461. furi_hal_irda_configure_tim_cmgr2_dma_tx();
  462. furi_hal_irda_configure_tim_rcr_dma_tx();
  463. furi_hal_irda_tx_dma_set_polarity(0, IRDA_POLARITY_SHIFT);
  464. furi_hal_irda_tx_dma_set_buffer(0);
  465. furi_hal_irda_state = IrdaStateAsyncTx;
  466. LL_TIM_ClearFlag_UPDATE(TIM1);
  467. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  468. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
  469. delay_us(5);
  470. LL_TIM_GenerateEvent_UPDATE(TIM1); /* DMA -> TIMx_RCR */
  471. delay_us(5);
  472. LL_GPIO_ResetOutputPin(gpio_irda_tx.port, gpio_irda_tx.pin); /* when disable it prevents false pulse */
  473. hal_gpio_init_ex(&gpio_irda_tx, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1);
  474. __disable_irq();
  475. LL_TIM_GenerateEvent_UPDATE(TIM1); /* TIMx_RCR -> Repetition counter */
  476. LL_TIM_EnableCounter(TIM1);
  477. __enable_irq();
  478. } else {
  479. furi_hal_irda_async_tx_free_resources();
  480. }
  481. return result;
  482. }
  483. void furi_hal_irda_async_tx_wait_termination(void) {
  484. furi_assert(furi_hal_irda_state >= IrdaStateAsyncTx);
  485. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  486. osStatus_t status;
  487. status = osSemaphoreAcquire(irda_tim_tx.stop_semaphore, osWaitForever);
  488. furi_check(status == osOK);
  489. furi_hal_irda_async_tx_free_resources();
  490. furi_hal_irda_state = IrdaStateIdle;
  491. }
  492. void furi_hal_irda_async_tx_stop(void) {
  493. furi_assert(furi_hal_irda_state >= IrdaStateAsyncTx);
  494. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  495. __disable_irq();
  496. if (furi_hal_irda_state == IrdaStateAsyncTx)
  497. furi_hal_irda_state = IrdaStateAsyncTxStopReq;
  498. __enable_irq();
  499. furi_hal_irda_async_tx_wait_termination();
  500. }
  501. void furi_hal_irda_async_tx_set_data_isr_callback(FuriHalIrdaTxGetDataISRCallback callback, void* context) {
  502. furi_assert(furi_hal_irda_state == IrdaStateIdle);
  503. irda_tim_tx.data_callback = callback;
  504. irda_tim_tx.data_context = context;
  505. }
  506. void furi_hal_irda_async_tx_set_signal_sent_isr_callback(FuriHalIrdaTxSignalSentISRCallback callback, void* context) {
  507. irda_tim_tx.signal_sent_callback = callback;
  508. irda_tim_tx.signal_sent_context = context;
  509. }