| /* |
| adc.h - Low level header for exposing ADC (Analog Digital Converter) |
| functionalities |
| Part of Grbl |
| |
| Copyright (c) 2019 Luigi Santivetti |
| |
| Grbl is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| Grbl is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with Grbl. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #ifndef adc_h |
| #define adc_h |
| |
| #define ADC_CHANNEL_00_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(0)) >> 0) |
| #define ADC_CHANNEL_01_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(1)) >> 1) |
| #define ADC_CHANNEL_02_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(2)) >> 2) |
| #define ADC_CHANNEL_03_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(3)) >> 3) |
| #define ADC_CHANNEL_04_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(4)) >> 4) |
| #define ADC_CHANNEL_05_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(5)) >> 5) |
| #define ADC_CHANNEL_06_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(6)) >> 6) |
| #define ADC_CHANNEL_07_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(7)) >> 7) |
| #define ADC_CHANNEL_08_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(8)) >> 8) |
| #define ADC_CHANNEL_09_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(9)) >> 9) |
| #define ADC_CHANNEL_10_ENABLED ((DEFAULT_ADC_CHANNELS_MASK & bit(10)) >> 10) |
| |
| #if DEFAULT_ADC_READINGS > 1 |
| // ADC ISR callbacks |
| #define ADC_ISR_PFN_SUM_ID 0 |
| #define ADC_ISR_PFN_AVR_ID 1 |
| #if DEFAULT_ADC_MODE == MODE0 |
| #define ADC_ISR_PFN_RST_ID 2 |
| #define ADC_ISR_APFN_COUNT 3 |
| #else |
| #define ADC_ISR_APFN_COUNT 2 |
| #endif |
| #endif |
| |
| // Number of enabled channels |
| #define ADC_CHANNELS_ENABLED_COUNT \ |
| (ADC_CHANNEL_00_ENABLED + ADC_CHANNEL_01_ENABLED + ADC_CHANNEL_02_ENABLED + \ |
| ADC_CHANNEL_03_ENABLED + ADC_CHANNEL_04_ENABLED + ADC_CHANNEL_05_ENABLED + \ |
| ADC_CHANNEL_06_ENABLED + ADC_CHANNEL_07_ENABLED + ADC_CHANNEL_08_ENABLED + \ |
| ADC_CHANNEL_09_ENABLED + ADC_CHANNEL_10_ENABLED) |
| |
| // Holding ADC readings |
| #if DEFAULT_ADC_RESOLUTION == 8 |
| typedef uint8_t adc_value; |
| #else |
| typedef uint16_t adc_value; |
| #endif |
| |
| typedef struct _adc_data adc_data; |
| |
| #ifdef ADC_CONFIG_SYSTEM_ALARM |
| #define ADC_INVALID_RANGE_CHANNEL(NUMBER) \ |
| ((!defined(DEFAULT_ADC_RANGE_CHANNEL_ ## NUMBER ## _MIN) || \ |
| !defined(DEFAULT_ADC_RANGE_CHANNEL_ ## NUMBER ## _MAX)) || \ |
| ((DEFAULT_ADC_RANGE_CHANNEL_ ## NUMBER ## _MIN >> DEFAULT_ADC_RESOLUTION) || \ |
| (DEFAULT_ADC_RANGE_CHANNEL_ ## NUMBER ## _MAX >> DEFAULT_ADC_RESOLUTION))) |
| |
| #if ADC_CHANNEL_00_ENABLED && ADC_INVALID_RANGE_CHANNEL(0) |
| #error "Invalid ADC00 range" |
| #endif |
| #if ADC_CHANNEL_01_ENABLED && ADC_INVALID_RANGE_CHANNEL(1) |
| #error "Invalid ADC01 range" |
| #endif |
| #if ADC_CHANNEL_02_ENABLED && ADC_INVALID_RANGE_CHANNEL(2) |
| #error "Invalid ADC02 range" |
| #endif |
| #if ADC_CHANNEL_03_ENABLED && ADC_INVALID_RANGE_CHANNEL(3) |
| #error "Invalid ADC03 range" |
| #endif |
| #if ADC_CHANNEL_04_ENABLED && ADC_INVALID_RANGE_CHANNEL(4) |
| #error "Invalid ADC04 range" |
| #endif |
| #if ADC_CHANNEL_05_ENABLED && ADC_INVALID_RANGE_CHANNEL(5) |
| #error "Invalid ADC05 range" |
| #endif |
| #if ADC_CHANNEL_06_ENABLED && ADC_INVALID_RANGE_CHANNEL(6) |
| #error "Invalid ADC06 range" |
| #endif |
| #if ADC_CHANNEL_07_ENABLED && ADC_INVALID_RANGE_CHANNEL(7) |
| #error "Invalid ADC07 range" |
| #endif |
| #if ADC_CHANNEL_08_ENABLED && ADC_INVALID_RANGE_CHANNEL(8) |
| #error "Invalid ADC08 range" |
| #endif |
| #if ADC_CHANNEL_09_ENABLED && ADC_INVALID_RANGE_CHANNEL(9) |
| #error "Invalid ADC09 range" |
| #endif |
| #if ADC_CHANNEL_10_ENABLED && ADC_INVALID_RANGE_CHANNEL(10) |
| #error "Invalid ADC10 range" |
| #endif |
| #endif |
| |
| typedef enum { |
| #if ADC_CHANNEL_00_ENABLED |
| ADC0, |
| #endif |
| #if ADC_CHANNEL_01_ENABLED |
| ADC1, |
| #endif |
| #if ADC_CHANNEL_02_ENABLED |
| ADC2, |
| #endif |
| #if ADC_CHANNEL_03_ENABLED |
| ADC3, |
| #endif |
| #if ADC_CHANNEL_04_ENABLED |
| ADC4, |
| #endif |
| #if ADC_CHANNEL_05_ENABLED |
| ADC5, |
| #endif |
| #if ADC_CHANNEL_06_ENABLED |
| ADC6, |
| #endif |
| #if ADC_CHANNEL_07_ENABLED |
| ADC7, |
| #endif |
| #if ADC_CHANNEL_08_ENABLED |
| ADC8, // Internal temperature |
| #endif |
| #if ADC_CHANNEL_09_ENABLED |
| ADC9, // Minimum voltage |
| #endif |
| #if ADC_CHANNEL_10_ENABLED |
| ADC10, // Bendgap voltage |
| #endif |
| } adc_channel; |
| |
| struct _adc_data { |
| #if DEFAULT_ADC_READINGS > 1 |
| // Array of function pointers called back from the ADC ISR and void if-else |
| void (* isr[ADC_ISR_APFN_COUNT])(adc_data *adc); |
| |
| // Store partial updates per channel |
| uint16_t sum; |
| |
| // Index tracking the current number of reading |
| uint8_t i_r; |
| #endif |
| |
| // Index for the current channel to be stored in the buffer |
| volatile adc_channel i_c; |
| |
| // Bitmask for the current status. The first 11 bit are reserved, where the |
| // i-bit set to 1 flags the i-th channel is drained and the buffer holds |
| // reliable data. The last 3 bits are for regular ADC ops signalling. |
| volatile uint16_t s; |
| |
| // Buffer holding readings per channel |
| struct { |
| volatile adc_value avr; |
| #ifdef ADC_CONFIG_SYSTEM_ALARM |
| adc_value min; |
| adc_value max; |
| #endif |
| } __attribute__ ((packed)) b[ADC_CHANNELS_ENABLED_COUNT]; |
| } __attribute__ ((packed)); |
| |
| // Setup the ADC with DEFAULTS, enable it and kick a conversion |
| void adc_init(adc_data *adc); |
| |
| // Teardown ADC, need to reinit afterwards |
| void adc_deinit(adc_data *adc); |
| |
| // Read unsigned ADC buffer value for channel |
| adc_value adc_read_unsigned(const adc_data *adc, const adc_channel c); |
| |
| #endif |