diff --git a/cores/AirMCU/wiring_analog.c b/cores/AirMCU/wiring_analog.c index c03e082..198aa40 100644 --- a/cores/AirMCU/wiring_analog.c +++ b/cores/AirMCU/wiring_analog.c @@ -18,15 +18,17 @@ #include "Arduino.h" #include "PinConfigured.h" +#include "airyyxx_ll_adc.h" #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -#if (defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)) ||\ +#if (defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)) || \ (defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)) -//This is the list of the IOs configured -uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0}; + // This is the list of the IOs configured + uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0}; #endif #if !defined(ADC_RESOLUTION_16B) @@ -36,72 +38,85 @@ uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0}; #endif #define MAX_PWM_RESOLUTION 16 -static int _readResolution = ADC_RESOLUTION; -static int _internalReadResolution = + static int _readResolution = ADC_RESOLUTION; + static int _internalReadResolution = #if ADC_RESOLUTION > MAX_ADC_RESOLUTION - MAX_ADC_RESOLUTION + MAX_ADC_RESOLUTION #else #ifdef ADC_RESOLUTION_12B #if ADC_RESOLUTION <= 6 && defined(ADC_RESOLUTION_6B) - 6 + 6 #elif ADC_RESOLUTION <= 8 - 8 + 8 #elif ADC_RESOLUTION <= 10 - 10 + 10 #elif ADC_RESOLUTION <= 12 - 12 + 12 #elif ADC_RESOLUTION <= 14 && defined(ADC_RESOLUTION_14B) - 14 + 14 #elif defined(ADC_RESOLUTION_16B) - 16 + 16 #endif -#else /* ADC_RESOLUTION_12B */ - 12 +#else /* ADC_RESOLUTION_12B */ + 12 #endif /* ADC_RESOLUTION_12B */ #endif /* ADC_RESOLUTION > MAX_ADC_RESOLUTION */ - ; + ; -static int _writeResolution = PWM_RESOLUTION; -static int _internalWriteResolution = + static int _writeResolution = PWM_RESOLUTION; + static int _internalWriteResolution = #if PWM_RESOLUTION > MAX_PWM_RESOLUTION - MAX_PWM_RESOLUTION + MAX_PWM_RESOLUTION #else - PWM_RESOLUTION + PWM_RESOLUTION #endif - ; + ; -static uint32_t _writeFreq = PWM_FREQUENCY; + static uint32_t _writeFreq = PWM_FREQUENCY; -void analogReadResolution(int res) -{ - if ((res > 0) && (res <= 32)) { - _readResolution = res; - _internalReadResolution = _readResolution; - if (_readResolution > MAX_ADC_RESOLUTION) { - _internalReadResolution = MAX_ADC_RESOLUTION; - } else { + void analogReadResolution(int res) + { + if ((res > 0) && (res <= 32)) + { + _readResolution = res; + _internalReadResolution = _readResolution; + if (_readResolution > MAX_ADC_RESOLUTION) + { + _internalReadResolution = MAX_ADC_RESOLUTION; + } + else + { #ifdef ADC_RESOLUTION_12B #ifdef ADC_RESOLUTION_6B - if (_internalReadResolution <= 6) { - _internalReadResolution = 6; - } else + if (_internalReadResolution <= 6) + { + _internalReadResolution = 6; + } + else #endif - if (_internalReadResolution <= 8) { + if (_internalReadResolution <= 8) + { _internalReadResolution = 8; - } else if (_internalReadResolution <= 10) { + } + else if (_internalReadResolution <= 10) + { _internalReadResolution = 10; - } else if (_internalReadResolution <= 12) { + } + else if (_internalReadResolution <= 12) + { _internalReadResolution = 12; } #ifdef ADC_RESOLUTION_14B - else if (_internalReadResolution <= 14) { + else if (_internalReadResolution <= 14) + { _internalReadResolution = 14; } #endif #ifdef ADC_RESOLUTION_16B - else if (_internalReadResolution <= 16) { + else if (_internalReadResolution <= 16) + { _internalReadResolution = 16; } #endif @@ -109,115 +124,191 @@ void analogReadResolution(int res) /* AIR32F1xx have no ADC_RESOLUTION_xB */ _internalReadResolution = 12; #endif + } + } + else + { + Error_Handler(); } - } else { - Error_Handler(); } -} -void analogWriteResolution(int res) -{ - if ((res > 0) && (res <= 32)) { - _writeResolution = res; - if (_writeResolution > MAX_PWM_RESOLUTION) { - _internalWriteResolution = MAX_PWM_RESOLUTION; - } else { - _internalWriteResolution = _writeResolution; + void analogWriteResolution(int res) + { + if ((res > 0) && (res <= 32)) + { + _writeResolution = res; + if (_writeResolution > MAX_PWM_RESOLUTION) + { + _internalWriteResolution = MAX_PWM_RESOLUTION; + } + else + { + _internalWriteResolution = _writeResolution; + } + } + else + { + Error_Handler(); } - } else { - Error_Handler(); } -} -void analogWriteFrequency(uint32_t freq) -{ - _writeFreq = freq; -} + void analogWriteFrequency(uint32_t freq) + { + _writeFreq = freq; + } -static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to) -{ - if (from != to) { - if (from > to) { - value = (value < (uint32_t)(1 << (from - to))) ? 0 : ((value + 1) >> (from - to)) - 1; - } else { - if (value != 0) { - value = ((value + 1) << (to - from)) - 1; + static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to) + { + if (from != to) + { + if (from > to) + { + value = (value < (uint32_t)(1 << (from - to))) ? 0 : ((value + 1) >> (from - to)) - 1; + } + else + { + if (value != 0) + { + value = ((value + 1) << (to - from)) - 1; + } } } + return value; } - return value; -} -void analogReference(eAnalogReference ulMode) -{ - UNUSED(ulMode); -} + void analogReference(eAnalogReference ulMode) + { + UNUSED(ulMode); + } -// Perform the read operation on the selected analog pin. -// the initialization of the analog PIN is done through this function -uint32_t analogRead(uint32_t ulPin) -{ - uint32_t value = 0; + // Perform the read operation on the selected analog pin. + // the initialization of the analog PIN is done through this function + uint32_t analogRead(uint32_t ulPin) + { + uint32_t value = 0; #if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY) - PinName p = analogInputToPinName(ulPin); - if (p != NC) { - value = adc_read_value(p, _internalReadResolution); - value = mapResolution(value, _internalReadResolution, _readResolution); - } + PinName p = analogInputToPinName(ulPin); + if (p != NC) + { + value = adc_read_value(p, _internalReadResolution); + value = mapResolution(value, _internalReadResolution, _readResolution); + } #else UNUSED(ulPin); #endif - return value; -} + return value; + } + uint32_t analogReadMillivolts(uint32_t ulPin) + { + uint32_t value = 0; + uint32_t VRef = analogReadVref(); + value = analogReadVoltage(VRef, ulPin); + return value; + } -void analogOutputInit(void) -{ -} + int32_t analogReadVref() + { +#ifdef __LL_ADC_CALC_VREFANALOG_VOLTAGE +#ifdef AIR32U5xx + return (__LL_ADC_CALC_VREFANALOG_VOLTAGE(ADC1, analogRead(AVREF), LL_ADC_RESOLUTION)); +#else + return (__LL_ADC_CALC_VREFANALOG_VOLTAGE(analogRead(AVREF), LL_ADC_RESOLUTION)); +#endif +#else + return (VREFINT * ADC_RANGE / analogRead(AVREF)); // ADC sample to mV +#endif + } -// Right now, PWM output only works on the pins with -// hardware support. These are defined in the appropriate -// variant.cpp file. For the rest of the pins, we default -// to digital output. -void analogWrite(uint32_t ulPin, uint32_t ulValue) -{ + int32_t analogReadTempSensor() + { +#ifdef ATEMP + int32_t VRef = analogReadVref(); +#ifdef __LL_ADC_CALC_TEMPERATURE +#ifdef AIR32U5xx + return (__LL_ADC_CALC_TEMPERATURE(ADC1, VRef, analogRead(ATEMP), LL_ADC_RESOLUTION)); +#else + return (__LL_ADC_CALC_TEMPERATURE(VRef, analogRead(ATEMP), LL_ADC_RESOLUTION)); +#endif +#elif defined(__LL_ADC_CALC_TEMPERATURE_TYP_PARAMS) + return (__LL_ADC_CALC_TEMPERATURE_TYP_PARAMS(AVG_SLOPE, VTEMP, CALX_TEMP, VRef, analogRead(ATEMP), LL_ADC_RESOLUTION)); +#else + return 0; +#endif + +#else +#warning "No temperature sensor on this device" + return 0; + +#endif + } + + int32_t analogReadVoltage(int32_t VRef, uint32_t pin) + { +#ifdef AIR32U5xx + return (__LL_ADC_CALC_DATA_TO_VOLTAGE(ADC1, VRef, analogRead(pin), LL_ADC_RESOLUTION)); +#else + return (__LL_ADC_CALC_DATA_TO_VOLTAGE(VRef, analogRead(pin), LL_ADC_RESOLUTION)); +#endif + } + + void analogOutputInit(void) + { + } + + // Right now, PWM output only works on the pins with + // hardware support. These are defined in the appropriate + // variant.cpp file. For the rest of the pins, we default + // to digital output. + void analogWrite(uint32_t ulPin, uint32_t ulValue) + { #if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY) - uint8_t do_init = 0; + uint8_t do_init = 0; #endif - PinName p = digitalPinToPinName(ulPin); - if (p != NC) { + PinName p = digitalPinToPinName(ulPin); + if (p != NC) + { #if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY) - if (pin_in_pinmap(p, PinMap_DAC)) { - if (is_pin_configured(p, g_anOutputPinConfigured) == false) { - do_init = 1; - set_pin_configured(p, g_anOutputPinConfigured); + if (pin_in_pinmap(p, PinMap_DAC)) + { + if (is_pin_configured(p, g_anOutputPinConfigured) == false) + { + do_init = 1; + set_pin_configured(p, g_anOutputPinConfigured); + } + ulValue = mapResolution(ulValue, _writeResolution, DACC_RESOLUTION); + dac_write_value(p, ulValue, do_init); } - ulValue = mapResolution(ulValue, _writeResolution, DACC_RESOLUTION); - dac_write_value(p, ulValue, do_init); - } else -#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY + else +#endif // HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY #if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY) - if (pin_in_pinmap(p, PinMap_TIM)) { - if (is_pin_configured(p, g_anOutputPinConfigured) == false) { + if (pin_in_pinmap(p, PinMap_TIM)) + { + if (is_pin_configured(p, g_anOutputPinConfigured) == false) + { set_pin_configured(p, g_anOutputPinConfigured); } ulValue = mapResolution(ulValue, _writeResolution, _internalWriteResolution); pwm_start(p, _writeFreq, ulValue, _internalWriteResolution); - } else + } + else #endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */ { - //DIGITAL PIN ONLY - // Defaults to digital write + // DIGITAL PIN ONLY + // Defaults to digital write pinMode(ulPin, OUTPUT); ulValue = mapResolution(ulValue, _writeResolution, 8); - if (ulValue < 128) { + if (ulValue < 128) + { digitalWrite(ulPin, LOW); - } else { + } + else + { digitalWrite(ulPin, HIGH); } } + } } -} #ifdef __cplusplus } diff --git a/cores/AirMCU/wiring_analog.h b/cores/AirMCU/wiring_analog.h index dc3de9d..82c2e5f 100644 --- a/cores/AirMCU/wiring_analog.h +++ b/cores/AirMCU/wiring_analog.h @@ -19,6 +19,12 @@ #ifndef _WIRING_ANALOG_ #define _WIRING_ANALOG_ +#if defined(AIR001xx) || defined(AIR32F1xx) +/* Analog read resolution */ +#define LL_ADC_RESOLUTION LL_ADC_RESOLUTION_12B +#define ADC_RANGE 4096 +#endif + #ifdef __cplusplus extern "C" { #endif @@ -56,6 +62,38 @@ extern void analogWrite(uint32_t ulPin, uint32_t ulValue) ; */ extern uint32_t analogRead(uint32_t ulPin) ; +/** + * @brief Read the value from the specified analog pin in millivolts. + * + * @param ulPin + * @return uint32_t + */ +extern uint32_t analogReadMillivolts(uint32_t ulPin); + +/** + * @brief Read the value from the specified analog pin in volts. + * + * @return int32_t + */ +extern int32_t analogReadVref(); + +/** + * @brief Read the value from the specified analog pin in volts. + * + * @param VRef + * @return int32_t + */ +extern int32_t analogReadTempSensor(); + +/** + * @brief Read the value from the specified analog pin in volts. + * + * @param VRef + * @param pin + * @return int32_t + */ +extern int32_t analogReadVoltage(int32_t VRef, uint32_t pin); + /* * \brief Set the resolution of analogRead return values. Default is 10 bits (range from 0 to 1023). *