ADC  8.0
Analog to Digital Conversor library for the Teensy 3.1/3.2 microprocessor
AnalogBufferDMA.h
1 /* Teensy 3.x, LC, 4.0 ADC library
2  * https://github.com/pedvide/ADC
3  * Copyright (c) 2019 Pedro Villanueva
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #ifndef ANALOGBUFFERDMA_H
27 #define ANALOGBUFFERDMA_H
28 
29 #include "DMAChannel.h"
30 #include "ADC.h"
31 // lets wrap some of our Dmasettings stuff into helper class
32 class AnalogBufferDMA {
33  // keep our settings and the like:
34 public: // At least temporary to play with dma settings.
35 #ifndef KINETISL
36  DMASetting _dmasettings_adc[2];
37 #endif
38  DMAChannel _dmachannel_adc;
39 
40  static AnalogBufferDMA *_activeObjectPerADC[2];
41  static void adc_0_dmaISR();
42  static void adc_1_dmaISR();
43  void processADC_DMAISR();
44 public:
45 
46  AnalogBufferDMA(volatile uint16_t *buffer1, uint16_t buffer1_count,
47  volatile uint16_t *buffer2 = nullptr, uint16_t buffer2_count = 0) :
48  _buffer1(buffer1), _buffer1_count(buffer1_count), _buffer2(buffer2), _buffer2_count(buffer2_count) {};
49 
50  void init(ADC *adc, int8_t adc_num = -1);
51 
52  void stopOnCompletion(bool stop_on_complete);
53  inline bool stopOnCompletion(void) {return _stop_on_completion;}
54  bool clearCompletion();
55  inline volatile uint16_t *bufferLastISRFilled() {return (!_buffer2 || (_interrupt_count & 1))? _buffer1 : _buffer2;}
56  inline uint16_t bufferCountLastISRFilled() {return (!_buffer2 || (_interrupt_count & 1))? _buffer1_count : _buffer2_count;}
57  inline uint32_t interruptCount() {return _interrupt_count;}
58  inline uint32_t interruptDeltaTime() {return _interrupt_delta_time;}
59  inline bool interrupted() {return _interrupt_delta_time != 0;}
60  inline void clearInterrupt() {_interrupt_delta_time = 0;}
61  inline void userData(uint32_t new_data) {_user_data = new_data;}
62  inline uint32_t userData(void) {return _user_data;}
63 protected:
64 
65  volatile uint32_t _interrupt_count = 0;
66  volatile uint32_t _interrupt_delta_time;
67  volatile uint32_t _last_isr_time;
68 
69  volatile uint16_t *_buffer1;
70  uint16_t _buffer1_count;
71  volatile uint16_t *_buffer2;
72  uint16_t _buffer2_count;
73  uint32_t _user_data = 0;
74  bool _stop_on_completion = false;
75 };
76 
77 #endif
ADC
Definition: ADC.h:68