46
46
* Typedefs for internal stuctures, enums
47
47
*/
48
48
typedef enum {
49
- e_no_intr = 0 ,
50
- e_tx_intr = 1 ,
51
- e_txthr_intr = 2 ,
52
- e_rx_intr = 4 ,
49
+ E_NO_INTR = 0 ,
50
+ E_TX_INTR = 1 ,
51
+ E_TXTHR_INTR = 2 ,
52
+ E_RX_INTR = 4 ,
53
53
} intr_mode_t ;
54
54
55
55
typedef enum {
56
- e_inactive = 0 ,
57
- e_first_half = 1 ,
58
- e_last_data = 2 ,
59
- e_end_trans = 4 ,
60
- e_set_conti = 8 ,
56
+ E_INACTIVE = 0 ,
57
+ E_FIRST_HALF = 1 ,
58
+ E_LAST_DATA = 2 ,
59
+ E_END_TRANS = 4 ,
60
+ E_SET_CONTI = 8 ,
61
61
} transaction_state_t ;
62
62
63
63
struct rmt_obj_s
@@ -68,8 +68,8 @@ struct rmt_obj_s
68
68
int channel ;
69
69
bool tx_not_rx ;
70
70
int buffers ;
71
- int remaining_to_send ;
72
- uint32_t * remaining_ptr ;
71
+ int data_size ;
72
+ uint32_t * data_ptr ;
73
73
intr_mode_t intr_mode ;
74
74
transaction_state_t tx_state ;
75
75
rmt_rx_data_cb_t cb ;
@@ -84,14 +84,14 @@ static xSemaphoreHandle g_rmt_objlocks[MAX_CHANNELS] = {
84
84
};
85
85
86
86
static rmt_obj_t g_rmt_objects [MAX_CHANNELS ] = {
87
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
88
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
89
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
90
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
91
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
92
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
93
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
94
- { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , e_no_intr , e_inactive , NULL , false},
87
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
88
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
89
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
90
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
91
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
92
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
93
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
94
+ { false, NULL , 0 , 0 , 0 , 0 , 0 , NULL , E_NO_INTR , E_INACTIVE , NULL , false},
95
95
};
96
96
97
97
/**
@@ -187,19 +187,23 @@ bool rmtDeinit(rmt_obj_t *rmt)
187
187
}
188
188
189
189
size_t from = rmt -> channel ;
190
+ size_t to = rmt -> buffers + rmt -> channel ;
191
+ size_t i ;
190
192
191
193
#if !CONFIG_DISABLE_HAL_LOCKS
192
194
if (g_rmt_objlocks [from ] != NULL ) {
193
195
vSemaphoreDelete (g_rmt_objlocks [from ]);
194
196
}
195
197
#endif
196
198
197
- size_t to = rmt -> buffers + rmt -> channel ;
198
- size_t i ;
199
+ if (g_rmt_objects [from ].data_alloc ) {
200
+ free (g_rmt_objects [from ].data_ptr );
201
+ }
199
202
200
203
for (i = from ; i < to ; i ++ ) {
201
204
g_rmt_objects [i ].allocated = false;
202
205
}
206
+
203
207
g_rmt_objects [from ].channel = 0 ;
204
208
g_rmt_objects [from ].buffers = 0 ;
205
209
@@ -213,7 +217,7 @@ bool rmtWrite(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
213
217
}
214
218
215
219
int channel = rmt -> channel ;
216
- int allocated_size = 64 * rmt -> buffers ;
220
+ int allocated_size = MAX_DATA_PER_CHANNEL * rmt -> buffers ;
217
221
218
222
if (size > allocated_size ) {
219
223
@@ -224,10 +228,10 @@ bool rmtWrite(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
224
228
esp_intr_alloc (ETS_RMT_INTR_SOURCE , (int )ESP_INTR_FLAG_IRAM , _rmt_isr , NULL , & intr_handle );
225
229
}
226
230
227
- rmt -> remaining_to_send = size - MAX_DATA_PER_ITTERATION ;
228
- rmt -> remaining_ptr = ((uint32_t * )data ) + MAX_DATA_PER_ITTERATION ;
229
- rmt -> intr_mode = e_tx_intr | e_txthr_intr ;
230
- rmt -> tx_state = e_set_conti | e_first_half ;
231
+ rmt -> data_size = size - MAX_DATA_PER_ITTERATION ;
232
+ rmt -> data_ptr = ((uint32_t * )data ) + MAX_DATA_PER_ITTERATION ;
233
+ rmt -> intr_mode = E_TX_INTR | E_TXTHR_INTR ;
234
+ rmt -> tx_state = E_SET_CONTI | E_FIRST_HALF ;
231
235
232
236
// init the tx limit for interruption
233
237
RMT .tx_lim_ch [channel ].limit = half_tx_nr ;
@@ -243,9 +247,9 @@ bool rmtWrite(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
243
247
RMTMEM .chan [channel ].data32 [MAX_DATA_PER_ITTERATION ].val = 0 ;
244
248
245
249
// clear and enable both Tx completed and half tx event
246
- RMT .int_clr .val | = _INT_TX_END (channel );
247
- RMT .int_clr .val | = _INT_THR_EVNT (channel );
248
- RMT .int_clr .val | = _INT_ERROR (channel );
250
+ RMT .int_clr .val = _INT_TX_END (channel );
251
+ RMT .int_clr .val = _INT_THR_EVNT (channel );
252
+ RMT .int_clr .val = _INT_ERROR (channel );
249
253
250
254
RMT .int_ena .val |= _INT_TX_END (channel );
251
255
RMT .int_ena .val |= _INT_THR_EVNT (channel );
@@ -261,7 +265,6 @@ bool rmtWrite(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
261
265
}
262
266
}
263
267
264
-
265
268
bool rmtReadData (rmt_obj_t * rmt , uint32_t * data , size_t size )
266
269
{
267
270
if (!rmt ) {
@@ -289,7 +292,7 @@ bool rmtBeginReceive(rmt_obj_t* rmt)
289
292
}
290
293
int channel = rmt -> channel ;
291
294
292
- RMT .int_clr .val | = _INT_ERROR (channel );
295
+ RMT .int_clr .val = _INT_ERROR (channel );
293
296
RMT .int_ena .val |= _INT_ERROR (channel );
294
297
295
298
RMT .conf_ch [channel ].conf1 .mem_owner = 1 ;
@@ -308,7 +311,7 @@ bool rmtReceiveCompleted(rmt_obj_t* rmt)
308
311
309
312
if (RMT .int_raw .val & _INT_RX_END (channel )) {
310
313
// RX end flag
311
- RMT .int_clr .val | = _INT_RX_END (channel );
314
+ RMT .int_clr .val = _INT_RX_END (channel );
312
315
return true;
313
316
} else {
314
317
return false;
@@ -323,13 +326,13 @@ bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb)
323
326
int channel = rmt -> channel ;
324
327
325
328
RMT_MUTEX_LOCK (channel );
326
- rmt -> intr_mode = e_rx_intr ;
327
- rmt -> tx_state = e_first_half ;
329
+ rmt -> intr_mode = E_RX_INTR ;
330
+ rmt -> tx_state = E_FIRST_HALF ;
328
331
rmt -> cb = cb ;
329
332
// allocate internally two buffers which would alternate
330
333
if (!rmt -> data_alloc ) {
331
- rmt -> remaining_ptr = (uint32_t * )malloc (2 * MAX_DATA_PER_CHANNEL * (rmt -> buffers )* sizeof (uint32_t ));
332
- rmt -> remaining_to_send = MAX_DATA_PER_CHANNEL * rmt -> buffers ;
334
+ rmt -> data_ptr = (uint32_t * )malloc (2 * MAX_DATA_PER_CHANNEL * (rmt -> buffers )* sizeof (uint32_t ));
335
+ rmt -> data_size = MAX_DATA_PER_CHANNEL * rmt -> buffers ;
333
336
rmt -> data_alloc = true;
334
337
}
335
338
@@ -366,12 +369,12 @@ bool rmtReadAsync(rmt_obj_t* rmt, rmt_data_t* data, size_t size, void* eventFlag
366
369
}
367
370
368
371
if (data && size > 0 ) {
369
- rmt -> remaining_ptr = (uint32_t * )data ;
370
- rmt -> remaining_to_send = size ;
372
+ rmt -> data_ptr = (uint32_t * )data ;
373
+ rmt -> data_size = size ;
371
374
}
372
375
373
376
RMT_MUTEX_LOCK (channel );
374
- rmt -> intr_mode = e_rx_intr ;
377
+ rmt -> intr_mode = E_RX_INTR ;
375
378
376
379
RMT .conf_ch [channel ].conf1 .mem_owner = 1 ;
377
380
@@ -596,28 +599,28 @@ static void IRAM_ATTR _rmt_isr(void* arg)
596
599
RMT .int_clr .val = _INT_RX_END (ch ); // TODO: replace clear interrupts
597
600
RMT .int_ena .val &= ~_INT_RX_END (ch );
598
601
599
- if ((g_rmt_objects [ch ].intr_mode )& e_rx_intr ) {
602
+ if ((g_rmt_objects [ch ].intr_mode ) & E_RX_INTR ) {
600
603
if (g_rmt_objects [ch ].events ) {
601
604
xEventGroupSetBits (g_rmt_objects [ch ].events , RMT_FLAG_RX_DONE );
602
605
}
603
- if (g_rmt_objects [ch ].remaining_ptr && g_rmt_objects [ch ].remaining_to_send > 0 ) {
606
+ if (g_rmt_objects [ch ].data_ptr && g_rmt_objects [ch ].data_size > 0 ) {
604
607
size_t i ;
605
- uint32_t * data = g_rmt_objects [ch ].remaining_ptr ;
608
+ uint32_t * data = g_rmt_objects [ch ].data_ptr ;
606
609
if (g_rmt_objects [ch ].cb ) {
607
- if (g_rmt_objects [ch ].tx_state & e_first_half ) {
608
- g_rmt_objects [ch ].tx_state &= ~e_first_half ;
610
+ if (g_rmt_objects [ch ].tx_state & E_FIRST_HALF ) {
611
+ g_rmt_objects [ch ].tx_state &= ~E_FIRST_HALF ;
609
612
} else {
610
- g_rmt_objects [ch ].tx_state |= e_first_half ;
613
+ g_rmt_objects [ch ].tx_state |= E_FIRST_HALF ;
611
614
data += MAX_DATA_PER_CHANNEL * (g_rmt_objects [ch ].buffers );
612
615
}
613
616
}
614
- for (i = 0 ; i < g_rmt_objects [ch ].remaining_to_send ; i ++ ) {
617
+ for (i = 0 ; i < g_rmt_objects [ch ].data_size ; i ++ ) {
615
618
* data ++ = RMTMEM .chan [ch ].data32 [i ].val ;
616
619
}
617
620
// configured callback
618
621
if (g_rmt_objects [ch ].cb ) {
619
622
// actually received data ptr
620
- uint32_t * data = g_rmt_objects [ch ].remaining_ptr ;
623
+ uint32_t * data = g_rmt_objects [ch ].data_ptr ;
621
624
(g_rmt_objects [ch ].cb )(data , _rmt_get_mem_len (ch ));
622
625
623
626
// restart the reception
@@ -627,7 +630,7 @@ static void IRAM_ATTR _rmt_isr(void* arg)
627
630
RMT .int_ena .val |= _INT_RX_END (ch );
628
631
} else {
629
632
// if not callback provide, expect only one Rx
630
- g_rmt_objects [ch ].intr_mode &= ~e_rx_intr ;
633
+ g_rmt_objects [ch ].intr_mode &= ~E_RX_INTR ;
631
634
}
632
635
}
633
636
} else {
@@ -639,7 +642,7 @@ static void IRAM_ATTR _rmt_isr(void* arg)
639
642
640
643
}
641
644
642
- if (intr_val & _INT_ERROR (ch )) {
645
+ if (intr_val & _INT_ERROR (ch )) {
643
646
digitalWrite (2 , 1 );
644
647
// clear the flag
645
648
RMT .int_clr .val = _INT_ERROR (ch );
@@ -656,79 +659,79 @@ static void IRAM_ATTR _rmt_isr(void* arg)
656
659
RMT .conf_ch [ch ].conf1 .mem_wr_rst = 0 ;
657
660
}
658
661
659
- if (intr_val & _INT_TX_END (ch )) {
662
+ if (intr_val & _INT_TX_END (ch )) {
660
663
661
664
RMT .int_clr .val = _INT_TX_END (ch );
662
665
663
- if (g_rmt_objects [ch ].tx_state & e_last_data ) {
664
- g_rmt_objects [ch ].tx_state = e_end_trans ;
666
+ if (g_rmt_objects [ch ].tx_state & E_LAST_DATA ) {
667
+ g_rmt_objects [ch ].tx_state = E_END_TRANS ;
665
668
RMT .conf_ch [ch ].conf1 .tx_conti_mode = 0 ;
666
669
int half_tx_nr = MAX_DATA_PER_ITTERATION /2 ;
667
670
int i ;
668
- if (g_rmt_objects [ch ].tx_state & e_first_half ) {
671
+ if (g_rmt_objects [ch ].tx_state & E_FIRST_HALF ) {
669
672
for (i = 0 ; i < half_tx_nr ; i ++ ) {
670
673
RMTMEM .chan [ch ].data32 [i ].val = 0x000F000F ;
671
674
}
672
675
RMTMEM .chan [ch ].data32 [i ].val = 0 ;
673
- g_rmt_objects [ch ].tx_state &= ~e_first_half ;
676
+ g_rmt_objects [ch ].tx_state &= ~E_FIRST_HALF ;
674
677
} else {
675
678
for (i = 0 ; i < half_tx_nr ; i ++ ) {
676
679
RMTMEM .chan [ch ].data32 [half_tx_nr + i ].val = 0x000F000F ;
677
680
}
678
681
RMTMEM .chan [ch ].data32 [i ].val = 0 ;
679
- g_rmt_objects [ch ].tx_state |= e_first_half ;
682
+ g_rmt_objects [ch ].tx_state |= E_FIRST_HALF ;
680
683
}
681
684
682
- } else if (g_rmt_objects [ch ].tx_state & e_end_trans ) {
685
+ } else if (g_rmt_objects [ch ].tx_state & E_END_TRANS ) {
683
686
RMT .conf_ch [ch ].conf1 .tx_conti_mode = 0 ;
684
687
RMT .int_ena .val &= ~_INT_TX_END (ch );
685
688
RMT .int_ena .val &= ~_INT_THR_EVNT (ch );
686
- g_rmt_objects [ch ].intr_mode = e_no_intr ;
687
- g_rmt_objects [ch ].tx_state = e_inactive ;
689
+ g_rmt_objects [ch ].intr_mode = E_NO_INTR ;
690
+ g_rmt_objects [ch ].tx_state = E_INACTIVE ;
688
691
}
689
692
}
690
693
691
- if (intr_val & _INT_THR_EVNT (ch )) {
694
+ if (intr_val & _INT_THR_EVNT (ch )) {
692
695
// clear the flag
693
- RMT .int_clr .val | _INT_THR_EVNT (ch );
696
+ RMT .int_clr .val = _INT_THR_EVNT (ch );
694
697
695
698
// initial setup of continuous mode
696
- if (g_rmt_objects [ch ].tx_state & e_set_conti ) {
699
+ if (g_rmt_objects [ch ].tx_state & E_SET_CONTI ) {
697
700
RMT .conf_ch [ch ].conf1 .tx_conti_mode = 1 ;
698
- g_rmt_objects [ch ].intr_mode &= ~e_set_conti ;
701
+ g_rmt_objects [ch ].intr_mode &= ~E_SET_CONTI ;
699
702
}
700
703
701
704
// check if still any data to be sent
702
- uint32_t * data = g_rmt_objects [ch ].remaining_ptr ;
705
+ uint32_t * data = g_rmt_objects [ch ].data_ptr ;
703
706
if (data )
704
707
{
705
- int remaining_size = g_rmt_objects [ch ].remaining_to_send ;
708
+ int remaining_size = g_rmt_objects [ch ].data_size ;
706
709
int half_tx_nr = MAX_DATA_PER_ITTERATION /2 ;
707
710
int i ;
708
711
709
712
// will the remaining data occupy the entire halfbuffer
710
713
if (remaining_size > half_tx_nr ) {
711
- if (g_rmt_objects [ch ].tx_state & e_first_half ) {
714
+ if (g_rmt_objects [ch ].tx_state & E_FIRST_HALF ) {
712
715
// ets_printf("first\n");
713
716
RMTMEM .chan [ch ].data32 [0 ].val = data [0 ] - 1 ;
714
717
for (i = 1 ; i < half_tx_nr ; i ++ ) {
715
718
RMTMEM .chan [ch ].data32 [i ].val = data [i ];
716
719
}
717
- g_rmt_objects [ch ].tx_state &= ~e_first_half ;
720
+ g_rmt_objects [ch ].tx_state &= ~E_FIRST_HALF ;
718
721
} else {
719
722
// ets_printf("second\n");
720
723
for (i = 0 ; i < half_tx_nr ; i ++ ) {
721
724
RMTMEM .chan [ch ].data32 [half_tx_nr + i ].val = data [i ];
722
725
}
723
- g_rmt_objects [ch ].tx_state |= e_first_half ;
726
+ g_rmt_objects [ch ].tx_state |= E_FIRST_HALF ;
724
727
}
725
- g_rmt_objects [ch ].remaining_to_send -= half_tx_nr ;
726
- g_rmt_objects [ch ].remaining_ptr += half_tx_nr ;
728
+ g_rmt_objects [ch ].data_size -= half_tx_nr ;
729
+ g_rmt_objects [ch ].data_ptr += half_tx_nr ;
727
730
} else {
728
731
// less remaining data than buffer size -> fill in with fake (inactive) pulses
729
- ets_printf ("last chunk..." );
730
- if (g_rmt_objects [ch ].tx_state & e_first_half ) {
731
- ets_printf ("first\n" );
732
+ // ets_printf("last chunk...");
733
+ if (g_rmt_objects [ch ].tx_state & E_FIRST_HALF ) {
734
+ // ets_printf("first\n");
732
735
RMTMEM .chan [ch ].data32 [0 ].val = data [0 ] - 1 ;
733
736
for (i = 1 ; i < half_tx_nr ; i ++ ) {
734
737
if (i < remaining_size ) {
@@ -737,27 +740,27 @@ static void IRAM_ATTR _rmt_isr(void* arg)
737
740
RMTMEM .chan [ch ].data32 [i ].val = 0x000F000F ;
738
741
}
739
742
}
740
- g_rmt_objects [ch ].tx_state &= ~e_first_half ;
743
+ g_rmt_objects [ch ].tx_state &= ~E_FIRST_HALF ;
741
744
} else {
742
- ets_printf ("second\n" );
745
+ // ets_printf("second\n");
743
746
for (i = 0 ; i < half_tx_nr ; i ++ ) {
744
747
if (i < remaining_size ) {
745
748
RMTMEM .chan [ch ].data32 [half_tx_nr + i ].val = data [i ];
746
749
} else {
747
750
RMTMEM .chan [ch ].data32 [half_tx_nr + i ].val = 0x000F000F ;
748
751
}
749
752
}
750
- g_rmt_objects [ch ].tx_state |= e_first_half ;
753
+ g_rmt_objects [ch ].tx_state |= E_FIRST_HALF ;
751
754
}
752
755
RMTMEM .chan [ch ].data32 [MAX_DATA_PER_ITTERATION ].val = 0 ;
753
756
// mark
754
- g_rmt_objects [ch ].remaining_ptr = NULL ;
757
+ g_rmt_objects [ch ].data_ptr = NULL ;
755
758
}
756
759
} else {
757
760
// no data left, just copy the fake (inactive) pulses
758
- if ( (!(g_rmt_objects [ch ].tx_state & e_last_data )) &&
759
- (!(g_rmt_objects [ch ].tx_state & e_end_trans )) ) {
760
- g_rmt_objects [ch ].tx_state |= e_last_data ;
761
+ if ( (!(g_rmt_objects [ch ].tx_state & E_LAST_DATA )) &&
762
+ (!(g_rmt_objects [ch ].tx_state & E_END_TRANS )) ) {
763
+ g_rmt_objects [ch ].tx_state |= E_END_TRANS ;
761
764
} else {
762
765
// ...do_nothing
763
766
}
0 commit comments