furi-hal-irda.c 26 KB

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