diff --git a/Inc/AdcArtery.h b/Inc/AdcArtery.h index 05be90b..bd85603 100644 --- a/Inc/AdcArtery.h +++ b/Inc/AdcArtery.h @@ -14,8 +14,12 @@ typedef struct { uint8_t ADC_Channel; int32_t offset; - int32_t mux; - int32_t div; + double mux; + double div; + +#ifdef ACCESS_ADC + osMutexId_t access; +#endif } tAdcArtery; @@ -23,8 +27,8 @@ tAdcArtery ADC_Initial( adc_type *ADCx, adc_channel_select_type ADC_Channel, int32_t offset, - int32_t mux, - int32_t div + double mux, + double div ); tAdcIO vAdcGetIo(tAdcArtery *env); diff --git a/Src/AdcArtery.c b/Src/AdcArtery.c index 4703a9b..95aa51d 100644 --- a/Src/AdcArtery.c +++ b/Src/AdcArtery.c @@ -8,8 +8,8 @@ tAdcArtery ADC_Initial( adc_type *ADCx, adc_channel_select_type ADC_Channel, int32_t offset, - int32_t mux, - int32_t div + double mux, + double div ) { adc_common_config_type adc_common_struct; @@ -78,6 +78,18 @@ tAdcArtery ADC_Initial( adc_calibration_start(ADCx); while (adc_calibration_status_get(ADCx)); +#ifdef ACCESS_ADC + + tAdcArtery adc = { + .ADCx = ADCx, + .ADC_Channel = ADC_Channel, + .offset = offset, + .div = div, + .mux = mux, + .access = osMutexNew(NULL) + }; + +#else tAdcArtery adc = { .ADCx = ADCx, @@ -87,10 +99,44 @@ tAdcArtery ADC_Initial( .mux = mux }; +#endif + return adc; } static uint16_t vAdcGet(tAdcArtery *env, uint32_t timeout) { + +#ifdef ACCESS_ADC + + if (osMutexAcquire(env->access, 1200) == osOK) { + + uint32_t endMs = SystemGetMs() + timeout; + uint16_t data; + + adc_ordinary_software_trigger_enable(env->ADCx, TRUE); + + while (adc_flag_get(env->ADCx, ADC_OCCE_FLAG) == RESET) { + if (SystemGetMs() > endMs) { + //todo error + osMutexRelease(env->access); + return 0; + } + } + + data = adc_ordinary_conversion_data_get(env->ADCx); + + adc_flag_clear(env->ADCx, ADC_OCCO_FLAG); + + int64_t projected = (int64_t) ((double )(data + env->offset) * env->mux / env->div); + + osMutexRelease(env->access); + + return projected; + } else { + return 0; + } + +#else uint32_t endMs = SystemGetMs() + timeout; uint16_t data; @@ -109,6 +155,7 @@ static uint16_t vAdcGet(tAdcArtery *env, uint32_t timeout) { int64_t projected = ((data + env->offset) * env->mux / env->div); return projected; +#endif } tAdcIO vAdcGetIo(tAdcArtery *env) {