furi_hal_irda.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 \
  26. (TIM_CCMR2_OC3PE | LL_TIM_OCMODE_FORCED_INACTIVE) /* Space time - force low */
  27. typedef struct {
  28. FuriHalIrdaRxCaptureCallback capture_callback;
  29. void* capture_context;
  30. FuriHalIrdaRxTimeoutCallback timeout_callback;
  31. void* timeout_context;
  32. } IrdaTimRx;
  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. } IrdaTxBuf;
  40. typedef struct {
  41. float cycle_duration;
  42. FuriHalIrdaTxGetDataISRCallback data_callback;
  43. FuriHalIrdaTxSignalSentISRCallback signal_sent_callback;
  44. void* data_context;
  45. void* signal_sent_context;
  46. IrdaTxBuf 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. FuriHalIrdaTxGetDataState tx_timing_rest_status;
  52. } IrdaTimTx;
  53. typedef enum {
  54. IrdaStateIdle, /** Furi Hal Irda is ready to start RX or TX */
  55. IrdaStateAsyncRx, /** Async RX started */
  56. IrdaStateAsyncTx, /** Async TX started, DMA and timer is on */
  57. IrdaStateAsyncTxStopReq, /** Async TX started, async stop request received */
  58. IrdaStateAsyncTxStopInProgress, /** Async TX started, stop request is processed and we wait for last data to be sent */
  59. IrdaStateAsyncTxStopped, /** Async TX complete, cleanup needed */
  60. IrdaStateMAX,
  61. } IrdaState;
  62. static volatile IrdaState furi_hal_irda_state = IrdaStateIdle;
  63. static IrdaTimTx irda_tim_tx;
  64. static IrdaTimRx irda_tim_rx;
  65. static void furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift);
  66. static void furi_hal_irda_async_tx_free_resources(void);
  67. static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift);
  68. static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num);
  69. static void furi_hal_irda_tx_fill_buffer_last(uint8_t buf_num);
  70. static uint8_t furi_hal_irda_get_current_dma_tx_buffer(void);
  71. static void furi_hal_irda_tx_dma_polarity_isr();
  72. static void furi_hal_irda_tx_dma_isr();
  73. static void furi_hal_irda_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_irda_state == IrdaStateAsyncRx);
  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_irda_rx.port, gpio_irda_rx.pin) != 0) {
  86. if(irda_tim_rx.timeout_callback)
  87. irda_tim_rx.timeout_callback(irda_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_irda_state == IrdaStateAsyncRx);
  94. if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
  95. /* Low pin level is a Mark state of IRDA signal. Invert level for further processing. */
  96. uint32_t duration = LL_TIM_IC_GetCaptureCH1(TIM2) - previous_captured_ch2;
  97. if(irda_tim_rx.capture_callback)
  98. irda_tim_rx.capture_callback(irda_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_irda_state == IrdaStateAsyncRx);
  107. if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
  108. /* High pin level is a Space state of IRDA signal. Invert level for further processing. */
  109. uint32_t duration = LL_TIM_IC_GetCaptureCH2(TIM2);
  110. previous_captured_ch2 = duration;
  111. if(irda_tim_rx.capture_callback)
  112. irda_tim_rx.capture_callback(irda_tim_rx.capture_context, 0, duration);
  113. } else {
  114. furi_assert(0);
  115. }
  116. }
  117. }
  118. void furi_hal_irda_async_rx_start(void) {
  119. furi_assert(furi_hal_irda_state == IrdaStateIdle);
  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_irda_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_irda_tim_rx_isr);
  150. furi_hal_irda_state = IrdaStateAsyncRx;
  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_irda_async_rx_stop(void) {
  161. furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
  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_irda_state = IrdaStateIdle;
  166. }
  167. void furi_hal_irda_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_irda_is_busy(void) {
  175. return furi_hal_irda_state != IrdaStateIdle;
  176. }
  177. void furi_hal_irda_async_rx_set_capture_isr_callback(
  178. FuriHalIrdaRxCaptureCallback callback,
  179. void* ctx) {
  180. irda_tim_rx.capture_callback = callback;
  181. irda_tim_rx.capture_context = ctx;
  182. }
  183. void furi_hal_irda_async_rx_set_timeout_isr_callback(
  184. FuriHalIrdaRxTimeoutCallback callback,
  185. void* ctx) {
  186. irda_tim_rx.timeout_callback = callback;
  187. irda_tim_rx.timeout_context = ctx;
  188. }
  189. static void furi_hal_irda_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_irda_state == IrdaStateAsyncTxStopInProgress);
  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(irda_tim_tx.stop_semaphore);
  199. furi_check(status == osOK);
  200. furi_hal_irda_state = IrdaStateAsyncTxStopped;
  201. }
  202. static uint8_t furi_hal_irda_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)irda_tim_tx.buffer[0].data) {
  206. buf_num = 0;
  207. } else if(buffer_adr == (uint32_t)irda_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_irda_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_irda_state == IrdaStateAsyncTx) ||
  223. (furi_hal_irda_state == IrdaStateAsyncTxStopReq) ||
  224. (furi_hal_irda_state == IrdaStateAsyncTxStopInProgress));
  225. /* actually TC2 is processed and buffer is next buffer */
  226. uint8_t next_buf_num = furi_hal_irda_get_current_dma_tx_buffer();
  227. furi_hal_irda_tx_dma_set_polarity(next_buf_num, 0);
  228. }
  229. }
  230. static void furi_hal_irda_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_irda_get_current_dma_tx_buffer();
  238. uint8_t next_buf_num = !buf_num;
  239. if(irda_tim_tx.buffer[buf_num].last_packet_end) {
  240. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  241. } else if(
  242. !irda_tim_tx.buffer[buf_num].packet_end || (furi_hal_irda_state == IrdaStateAsyncTx)) {
  243. furi_hal_irda_tx_fill_buffer(next_buf_num, 0);
  244. if(irda_tim_tx.buffer[next_buf_num].last_packet_end) {
  245. LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  246. }
  247. } else if(furi_hal_irda_state == IrdaStateAsyncTxStopReq) {
  248. /* fallthrough */
  249. } else {
  250. furi_crash(NULL);
  251. }
  252. }
  253. if(LL_DMA_IsActiveFlag_TC2(DMA1) && LL_DMA_IsEnabledIT_TC(DMA1, LL_DMA_CHANNEL_2)) {
  254. LL_DMA_ClearFlag_TC2(DMA1);
  255. furi_check(
  256. (furi_hal_irda_state == IrdaStateAsyncTxStopInProgress) ||
  257. (furi_hal_irda_state == IrdaStateAsyncTxStopReq) ||
  258. (furi_hal_irda_state == IrdaStateAsyncTx));
  259. uint8_t buf_num = furi_hal_irda_get_current_dma_tx_buffer();
  260. uint8_t next_buf_num = !buf_num;
  261. if(furi_hal_irda_state == IrdaStateAsyncTxStopInProgress) {
  262. furi_hal_irda_tx_dma_terminate();
  263. } else if(
  264. irda_tim_tx.buffer[buf_num].last_packet_end ||
  265. (irda_tim_tx.buffer[buf_num].packet_end &&
  266. (furi_hal_irda_state == IrdaStateAsyncTxStopReq))) {
  267. furi_hal_irda_state = IrdaStateAsyncTxStopInProgress;
  268. furi_hal_irda_tx_fill_buffer_last(next_buf_num);
  269. furi_hal_irda_tx_dma_set_buffer(next_buf_num);
  270. } else {
  271. /* if it's not end of the packet - continue receiving */
  272. furi_hal_irda_tx_dma_set_buffer(next_buf_num);
  273. }
  274. if(irda_tim_tx.signal_sent_callback && irda_tim_tx.buffer[buf_num].packet_end &&
  275. (furi_hal_irda_state != IrdaStateAsyncTxStopped)) {
  276. irda_tim_tx.signal_sent_callback(irda_tim_tx.signal_sent_context);
  277. }
  278. }
  279. }
  280. static void furi_hal_irda_configure_tim_pwm_tx(uint32_t freq, float duty_cycle) {
  281. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
  282. /* LL_DBGMCU_APB2_GRP1_FreezePeriph(LL_DBGMCU_APB2_GRP1_TIM1_STOP); */
  283. LL_TIM_DisableCounter(TIM1);
  284. LL_TIM_SetRepetitionCounter(TIM1, 0);
  285. LL_TIM_SetCounter(TIM1, 0);
  286. LL_TIM_SetPrescaler(TIM1, 0);
  287. LL_TIM_SetCounterMode(TIM1, LL_TIM_COUNTERMODE_UP);
  288. LL_TIM_EnableARRPreload(TIM1);
  289. LL_TIM_SetAutoReload(
  290. TIM1, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM1), freq));
  291. #if IRDA_TX_DEBUG == 1
  292. LL_TIM_OC_SetCompareCH1(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
  293. LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
  294. /* LL_TIM_OCMODE_PWM2 set by DMA */
  295. LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_INACTIVE);
  296. LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH1N, LL_TIM_OCPOLARITY_HIGH);
  297. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
  298. LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N);
  299. LL_TIM_DisableIT_CC1(TIM1);
  300. #else
  301. LL_TIM_OC_SetCompareCH3(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
  302. LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH3);
  303. /* LL_TIM_OCMODE_PWM2 set by DMA */
  304. LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_FORCED_INACTIVE);
  305. LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH3N, LL_TIM_OCPOLARITY_HIGH);
  306. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH3);
  307. LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH3N);
  308. LL_TIM_DisableIT_CC3(TIM1);
  309. #endif
  310. LL_TIM_DisableMasterSlaveMode(TIM1);
  311. LL_TIM_EnableAllOutputs(TIM1);
  312. LL_TIM_DisableIT_UPDATE(TIM1);
  313. LL_TIM_EnableDMAReq_UPDATE(TIM1);
  314. NVIC_SetPriority(TIM1_UP_TIM16_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  315. NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
  316. }
  317. static void furi_hal_irda_configure_tim_cmgr2_dma_tx(void) {
  318. LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  319. LL_DMA_InitTypeDef dma_config = {0};
  320. #if IRDA_TX_DEBUG == 1
  321. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR1);
  322. #else
  323. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR2);
  324. #endif
  325. dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
  326. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  327. dma_config.Mode = LL_DMA_MODE_NORMAL;
  328. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  329. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  330. /* fill word to have other bits set to 0 */
  331. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
  332. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  333. dma_config.NbData = 0;
  334. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  335. dma_config.Priority = LL_DMA_PRIORITY_VERYHIGH;
  336. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &dma_config);
  337. furi_hal_interrupt_set_dma_channel_isr(
  338. DMA1, LL_DMA_CHANNEL_1, furi_hal_irda_tx_dma_polarity_isr);
  339. LL_DMA_ClearFlag_TE1(DMA1);
  340. LL_DMA_ClearFlag_TC1(DMA1);
  341. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
  342. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  343. NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 4, 0));
  344. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  345. }
  346. static void furi_hal_irda_configure_tim_rcr_dma_tx(void) {
  347. LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  348. LL_DMA_InitTypeDef dma_config = {0};
  349. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->RCR);
  350. dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
  351. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  352. dma_config.Mode = LL_DMA_MODE_NORMAL;
  353. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  354. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  355. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_HALFWORD;
  356. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD;
  357. dma_config.NbData = 0;
  358. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
  359. dma_config.Priority = LL_DMA_PRIORITY_MEDIUM;
  360. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_2, &dma_config);
  361. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, furi_hal_irda_tx_dma_isr);
  362. LL_DMA_ClearFlag_TC2(DMA1);
  363. LL_DMA_ClearFlag_HT2(DMA1);
  364. LL_DMA_ClearFlag_TE2(DMA1);
  365. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_2);
  366. LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_2);
  367. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_2);
  368. NVIC_SetPriority(DMA1_Channel2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  369. NVIC_EnableIRQ(DMA1_Channel2_IRQn);
  370. }
  371. static void furi_hal_irda_tx_fill_buffer_last(uint8_t buf_num) {
  372. furi_assert(buf_num < 2);
  373. furi_assert(furi_hal_irda_state != IrdaStateAsyncRx);
  374. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  375. furi_assert(irda_tim_tx.data_callback);
  376. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  377. furi_assert(buffer->data != NULL);
  378. (void)buffer->data;
  379. furi_assert(buffer->polarity != NULL);
  380. (void)buffer->polarity;
  381. irda_tim_tx.buffer[buf_num].data[0] = 0; // 1 pulse
  382. irda_tim_tx.buffer[buf_num].polarity[0] = IRDA_TX_CCMR_LOW;
  383. irda_tim_tx.buffer[buf_num].data[1] = 0; // 1 pulse
  384. irda_tim_tx.buffer[buf_num].polarity[1] = IRDA_TX_CCMR_LOW;
  385. irda_tim_tx.buffer[buf_num].size = 2;
  386. irda_tim_tx.buffer[buf_num].last_packet_end = true;
  387. irda_tim_tx.buffer[buf_num].packet_end = true;
  388. }
  389. static void furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
  390. furi_assert(buf_num < 2);
  391. furi_assert(furi_hal_irda_state != IrdaStateAsyncRx);
  392. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  393. furi_assert(irda_tim_tx.data_callback);
  394. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  395. furi_assert(buffer->data != NULL);
  396. furi_assert(buffer->polarity != NULL);
  397. FuriHalIrdaTxGetDataState status = FuriHalIrdaTxGetDataStateOk;
  398. uint32_t duration = 0;
  399. bool level = 0;
  400. size_t* size = &buffer->size;
  401. size_t polarity_counter = 0;
  402. while(polarity_shift--) {
  403. buffer->polarity[polarity_counter++] = IRDA_TX_CCMR_LOW;
  404. }
  405. for(*size = 0;
  406. (*size < IRDA_TIM_TX_DMA_BUFFER_SIZE) && (status == FuriHalIrdaTxGetDataStateOk);) {
  407. if(irda_tim_tx.tx_timing_rest_duration > 0) {
  408. if(irda_tim_tx.tx_timing_rest_duration > 0xFFFF) {
  409. buffer->data[*size] = 0xFFFF;
  410. status = FuriHalIrdaTxGetDataStateOk;
  411. } else {
  412. buffer->data[*size] = irda_tim_tx.tx_timing_rest_duration;
  413. status = irda_tim_tx.tx_timing_rest_status;
  414. }
  415. irda_tim_tx.tx_timing_rest_duration -= buffer->data[*size];
  416. buffer->polarity[polarity_counter] =
  417. irda_tim_tx.tx_timing_rest_level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
  418. ++(*size);
  419. ++polarity_counter;
  420. continue;
  421. }
  422. status = irda_tim_tx.data_callback(irda_tim_tx.data_context, &duration, &level);
  423. uint32_t num_of_impulses = roundf(duration / irda_tim_tx.cycle_duration);
  424. if(num_of_impulses == 0) {
  425. if((*size == 0) && (status == FuriHalIrdaTxGetDataStateDone)) {
  426. /* if this is one sample in current buffer, but we
  427. * have more to send - continue
  428. */
  429. status = FuriHalIrdaTxGetDataStateOk;
  430. }
  431. } else if((num_of_impulses - 1) > 0xFFFF) {
  432. irda_tim_tx.tx_timing_rest_duration = num_of_impulses - 1;
  433. irda_tim_tx.tx_timing_rest_status = status;
  434. irda_tim_tx.tx_timing_rest_level = level;
  435. status = FuriHalIrdaTxGetDataStateOk;
  436. } else {
  437. buffer->polarity[polarity_counter] = level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
  438. buffer->data[*size] = num_of_impulses - 1;
  439. ++(*size);
  440. ++polarity_counter;
  441. }
  442. }
  443. buffer->last_packet_end = (status == FuriHalIrdaTxGetDataStateLastDone);
  444. buffer->packet_end = buffer->last_packet_end || (status == FuriHalIrdaTxGetDataStateDone);
  445. if(*size == 0) {
  446. buffer->data[0] = 0; // 1 pulse
  447. buffer->polarity[0] = IRDA_TX_CCMR_LOW;
  448. buffer->size = 1;
  449. }
  450. }
  451. static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
  452. furi_assert(buf_num < 2);
  453. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  454. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  455. furi_assert(buffer->polarity != NULL);
  456. FURI_CRITICAL_ENTER();
  457. bool channel_enabled = LL_DMA_IsEnabledChannel(DMA1, LL_DMA_CHANNEL_1);
  458. if(channel_enabled) {
  459. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
  460. }
  461. LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_1, (uint32_t)buffer->polarity);
  462. LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, buffer->size + polarity_shift);
  463. if(channel_enabled) {
  464. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  465. }
  466. FURI_CRITICAL_EXIT();
  467. }
  468. static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num) {
  469. furi_assert(buf_num < 2);
  470. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  471. IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
  472. furi_assert(buffer->data != NULL);
  473. /* non-circular mode requires disabled channel before setup */
  474. FURI_CRITICAL_ENTER();
  475. bool channel_enabled = LL_DMA_IsEnabledChannel(DMA1, LL_DMA_CHANNEL_2);
  476. if(channel_enabled) {
  477. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);
  478. }
  479. LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_2, (uint32_t)buffer->data);
  480. LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_2, buffer->size);
  481. if(channel_enabled) {
  482. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
  483. }
  484. FURI_CRITICAL_EXIT();
  485. }
  486. static void furi_hal_irda_async_tx_free_resources(void) {
  487. furi_assert(
  488. (furi_hal_irda_state == IrdaStateIdle) ||
  489. (furi_hal_irda_state == IrdaStateAsyncTxStopped));
  490. osStatus_t status;
  491. hal_gpio_init(&gpio_irda_tx, GpioModeOutputOpenDrain, GpioPullDown, GpioSpeedLow);
  492. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_1, NULL);
  493. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, NULL);
  494. LL_TIM_DeInit(TIM1);
  495. LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_TIM1);
  496. LL_C2_AHB1_GRP1_DisableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
  497. status = osSemaphoreDelete(irda_tim_tx.stop_semaphore);
  498. furi_check(status == osOK);
  499. free(irda_tim_tx.buffer[0].data);
  500. free(irda_tim_tx.buffer[1].data);
  501. free(irda_tim_tx.buffer[0].polarity);
  502. free(irda_tim_tx.buffer[1].polarity);
  503. irda_tim_tx.buffer[0].data = NULL;
  504. irda_tim_tx.buffer[1].data = NULL;
  505. irda_tim_tx.buffer[0].polarity = NULL;
  506. irda_tim_tx.buffer[1].polarity = NULL;
  507. }
  508. void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
  509. if((duty_cycle > 1) || (duty_cycle <= 0) || (freq > IRDA_MAX_FREQUENCY) ||
  510. (freq < IRDA_MIN_FREQUENCY) || (irda_tim_tx.data_callback == NULL)) {
  511. furi_crash(NULL);
  512. }
  513. furi_assert(furi_hal_irda_state == IrdaStateIdle);
  514. furi_assert(irda_tim_tx.buffer[0].data == NULL);
  515. furi_assert(irda_tim_tx.buffer[1].data == NULL);
  516. furi_assert(irda_tim_tx.buffer[0].polarity == NULL);
  517. furi_assert(irda_tim_tx.buffer[1].polarity == NULL);
  518. size_t alloc_size_data = IRDA_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
  519. irda_tim_tx.buffer[0].data = furi_alloc(alloc_size_data);
  520. irda_tim_tx.buffer[1].data = furi_alloc(alloc_size_data);
  521. size_t alloc_size_polarity =
  522. (IRDA_TIM_TX_DMA_BUFFER_SIZE + IRDA_POLARITY_SHIFT) * sizeof(uint8_t);
  523. irda_tim_tx.buffer[0].polarity = furi_alloc(alloc_size_polarity);
  524. irda_tim_tx.buffer[1].polarity = furi_alloc(alloc_size_polarity);
  525. irda_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
  526. irda_tim_tx.cycle_duration = 1000000.0 / freq;
  527. irda_tim_tx.tx_timing_rest_duration = 0;
  528. furi_hal_irda_tx_fill_buffer(0, IRDA_POLARITY_SHIFT);
  529. furi_hal_irda_configure_tim_pwm_tx(freq, duty_cycle);
  530. furi_hal_irda_configure_tim_cmgr2_dma_tx();
  531. furi_hal_irda_configure_tim_rcr_dma_tx();
  532. furi_hal_irda_tx_dma_set_polarity(0, IRDA_POLARITY_SHIFT);
  533. furi_hal_irda_tx_dma_set_buffer(0);
  534. furi_hal_irda_state = IrdaStateAsyncTx;
  535. LL_TIM_ClearFlag_UPDATE(TIM1);
  536. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  537. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
  538. delay_us(5);
  539. LL_TIM_GenerateEvent_UPDATE(TIM1); /* DMA -> TIMx_RCR */
  540. delay_us(5);
  541. LL_GPIO_ResetOutputPin(
  542. gpio_irda_tx.port, gpio_irda_tx.pin); /* when disable it prevents false pulse */
  543. hal_gpio_init_ex(
  544. &gpio_irda_tx, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1);
  545. FURI_CRITICAL_ENTER();
  546. LL_TIM_GenerateEvent_UPDATE(TIM1); /* TIMx_RCR -> Repetition counter */
  547. LL_TIM_EnableCounter(TIM1);
  548. FURI_CRITICAL_EXIT();
  549. }
  550. void furi_hal_irda_async_tx_wait_termination(void) {
  551. furi_assert(furi_hal_irda_state >= IrdaStateAsyncTx);
  552. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  553. osStatus_t status;
  554. status = osSemaphoreAcquire(irda_tim_tx.stop_semaphore, osWaitForever);
  555. furi_check(status == osOK);
  556. furi_hal_irda_async_tx_free_resources();
  557. furi_hal_irda_state = IrdaStateIdle;
  558. }
  559. void furi_hal_irda_async_tx_stop(void) {
  560. furi_assert(furi_hal_irda_state >= IrdaStateAsyncTx);
  561. furi_assert(furi_hal_irda_state < IrdaStateMAX);
  562. FURI_CRITICAL_ENTER();
  563. if(furi_hal_irda_state == IrdaStateAsyncTx) furi_hal_irda_state = IrdaStateAsyncTxStopReq;
  564. FURI_CRITICAL_EXIT();
  565. furi_hal_irda_async_tx_wait_termination();
  566. }
  567. void furi_hal_irda_async_tx_set_data_isr_callback(
  568. FuriHalIrdaTxGetDataISRCallback callback,
  569. void* context) {
  570. furi_assert(furi_hal_irda_state == IrdaStateIdle);
  571. irda_tim_tx.data_callback = callback;
  572. irda_tim_tx.data_context = context;
  573. }
  574. void furi_hal_irda_async_tx_set_signal_sent_isr_callback(
  575. FuriHalIrdaTxSignalSentISRCallback callback,
  576. void* context) {
  577. irda_tim_tx.signal_sent_callback = callback;
  578. irda_tim_tx.signal_sent_context = context;
  579. }