From 0f8a2f67588298439a8171959327841e08a29231 Mon Sep 17 00:00:00 2001 From: cfif Date: Wed, 4 Dec 2024 13:10:49 +0300 Subject: [PATCH] Init --- Inc/AdcNation.h | 30 +++++++++++++++++ Src/AdcNation.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ modular.json | 22 +++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 Inc/AdcNation.h create mode 100644 Src/AdcNation.c create mode 100644 modular.json diff --git a/Inc/AdcNation.h b/Inc/AdcNation.h new file mode 100644 index 0000000..c018866 --- /dev/null +++ b/Inc/AdcNation.h @@ -0,0 +1,30 @@ +// +// Created by cfif on 17.11.22. +// + +#ifndef ADC_NATION_H +#define ADC_NATION_H + +#include "Adc.h" +#include "cmsis_os2.h" +#include "n32g45x.h" + +typedef struct { + ADC_Module *ADCx; + uint8_t ADC_Channel; + bool isCalibration; + + struct { + int32_t mux; + int32_t div; + int32_t offset; + } ratio; +} tAdcNation; + +tAdcNation ADC_Initial( + ADC_Module *ADCx, uint8_t ADC_Channel, int32_t mux, int32_t div, int32_t offset, uint32_t timeout +); + +tAdcIO vAdcGetIo(tAdcNation *env); + +#endif //ADC_NATION_H diff --git a/Src/AdcNation.c b/Src/AdcNation.c new file mode 100644 index 0000000..89ca8aa --- /dev/null +++ b/Src/AdcNation.c @@ -0,0 +1,86 @@ +// +// Created by cfif on 07.09.22. +// +#include +#include "AdcNation.h" + +tAdcNation ADC_Initial( + ADC_Module *ADCx, uint8_t ADC_Channel, int32_t mux, int32_t div, int32_t offset, uint32_t timeout +) { + + uint32_t endMs = SystemGetMs() + timeout; + + ADC_InitType ADC_InitStructure; + + // ADC configuration ------------------------------------------------------ + ADC_InitStructure.WorkMode = ADC_WORKMODE_INDEPENDENT; + ADC_InitStructure.MultiChEn = DISABLE; + ADC_InitStructure.ContinueConvEn = DISABLE; + ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE; + ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R; + ADC_InitStructure.ChsNumber = 1; + ADC_Init(ADCx, &ADC_InitStructure); + + // Enable ADC + ADC_Enable(ADCx, ENABLE); + + // Check ADC Ready + while (ADC_GetFlagStatusNew(ADCx, ADC_FLAG_RDY) == RESET); + + // Start ADC calibration + ADC_StartCalibration(ADCx); + + bool isCalibration = true; + // Check the end of ADC calibration + while (ADC_GetCalibrationStatus(ADCx)) { + if (SystemGetMs() > endMs) { + isCalibration = false; + } + } + + tAdcNation adc = { + .ADCx = ADCx, + .ADC_Channel = ADC_Channel, + .isCalibration = isCalibration, + .ratio = { + .mux = mux, + .div = div, + .offset = offset + } + }; + + return adc; +} + +static int32_t vAdcGet(tAdcNation *env, uint32_t timeout) { + uint32_t endMs = SystemGetMs() + timeout; + + uint16_t data; + + ADC_ConfigRegularChannel(env->ADCx, env->ADC_Channel, 1, ADC_SAMP_TIME_239CYCLES5); + + // Start ADC Software Conversion + ADC_EnableSoftwareStartConv(env->ADCx, ENABLE); + + while (ADC_GetFlagStatus(env->ADCx, ADC_FLAG_ENDC) == 0) { + if (SystemGetMs() > endMs) { + //todo error + return 0; + } + } + + ADC_ClearFlag(env->ADCx, ADC_FLAG_ENDC); + ADC_ClearFlag(env->ADCx, ADC_FLAG_STR); + data = ADC_GetDat(env->ADCx); + + data = (data * env->ratio.mux) / env->ratio.div + env->ratio.offset; + return data; +} + +tAdcIO vAdcGetIo(tAdcNation *env) { + tAdcIO io = { + .env = env, + .get = (AdcIOTransaction) vAdcGet, + }; + return io; +} \ No newline at end of file diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..5e86f41 --- /dev/null +++ b/modular.json @@ -0,0 +1,22 @@ +{ + "dep": [ + { + "type": "git", + "provider": "NAVIGATOR_UVEOS_NATION_TELIT", + "repo": "PeripheralDriver_NATION_N32G45X" + }, + { + "type": "git", + "provider": "NAVIGATOR_UVEOS_NATION_TELIT", + "repo": "Adc" + } + ], + "cmake": { + "inc_dirs": [ + "Inc" + ], + "srcs": [ + "Src/**.c" + ] + } +} \ No newline at end of file