furi-hal-irda.c 26 KB

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