@@ -5,12 +5,97 @@ It contains functions to interact with the ADC in order to implement async ADC r
55*/
66
77#include < analog.h>
8- #include < analog.cpp>
8+ // #include <analog.cpp>
99#include < IRQManager.h>
1010
1111
12+ /* * VERBATIM from Arduino's analog.cpp
13+ */
14+ #define MAX_ADC_CHANNELS 29
15+ static uint16_t analog_values_by_channels[MAX_ADC_CHANNELS] = {0 };
16+ static void ADC_irq_cbk (adc_callback_args_t * cb_data);
17+ static ADC_Container adc (0 ,ADC_irq_cbk);
18+ static ADC_Container adc1 (1 ,ADC_irq_cbk);
1219
20+ static ADCIrqCbk_f scan_complete_cbk = nullptr ;
21+ static ADCIrqCbk_f scan_complete_b_cbk = nullptr ;
22+ static ADCIrqCbk_f window_compare_a_cbk = nullptr ;
23+ static ADCIrqCbk_f window_compare_b_cbk = nullptr ;
1324
25+ static void readAllGroupA (ADC_Container *_adc) {
26+ for (int i = 0 ; i < MAX_ADC_CHANNELS; i++) {
27+ if (_adc->channel_cfg .scan_mask & (1 << i)) {
28+ // is the channel active -> yes, read it
29+ R_ADC_Read (&(_adc->ctrl ), (adc_channel_t )i, analog_values_by_channels + i);
30+ }
31+ }
32+ }
33+
34+ static void readAllGroupB (ADC_Container *_adc) {
35+ for (int i = 0 ; i < MAX_ADC_CHANNELS; i++) {
36+ if (_adc->channel_cfg .scan_mask_group_b & (1 << i)) {
37+ // is the channel active -> yes, read it
38+ R_ADC_Read (&(_adc->ctrl ), (adc_channel_t )i, analog_values_by_channels + i);
39+ }
40+ }
41+ }
42+
43+
44+ static void ADC_irq_cbk (adc_callback_args_t * cb_data) {
45+ if (cb_data->event == ADC_EVENT_SCAN_COMPLETE) {
46+ if (scan_complete_cbk != nullptr ) {
47+ if (cb_data->unit == 0 ) {
48+ readAllGroupA (&adc);
49+ }
50+ else if (cb_data->unit == 1 ) {
51+ readAllGroupA (&adc1);
52+ }
53+ scan_complete_cbk (cb_data->unit );
54+ }
55+ }
56+ else if (cb_data->event == ADC_EVENT_SCAN_COMPLETE_GROUP_B) {
57+ if (scan_complete_b_cbk != nullptr ) {
58+ if (cb_data->unit == 0 ) {
59+ readAllGroupB (&adc);
60+ }
61+ else if (cb_data->unit == 1 ) {
62+ readAllGroupB (&adc1);
63+ }
64+ scan_complete_b_cbk (cb_data->unit );
65+ }
66+ }
67+ else if (cb_data->event == ADC_EVENT_WINDOW_COMPARE_A) {
68+ if (window_compare_a_cbk != nullptr ) {
69+ window_compare_a_cbk (cb_data->unit );
70+ }
71+ }
72+ else if (cb_data->event == ADC_EVENT_WINDOW_COMPARE_B) {
73+ if (window_compare_b_cbk != nullptr ) {
74+ window_compare_b_cbk (cb_data->unit );
75+ }
76+ }
77+ }
78+
79+ /* -------------------------------------------------------------------------- */
80+ static ADC_Container *get_ADC_container_ptr (int32_t pin, uint16_t &cfg) {
81+ /* -------------------------------------------------------------------------- */
82+ ADC_Container *rv = nullptr ;
83+ auto cfg_adc = getPinCfgs (pin, PIN_CFG_REQ_ADC);
84+ if (cfg_adc[0 ] > 0 ) {
85+ if (IS_ADC1 (cfg_adc[0 ])) {
86+ rv = &adc1;
87+ }
88+ else {
89+ rv = &adc;
90+ }
91+ }
92+ cfg = cfg_adc[0 ];
93+ return rv;
94+
95+ }
96+
97+ /* END of verbatim
98+ */
1499// ////////////////// ADC //////////////
15100
16101void startScan (int pin)
0 commit comments