37 volatile uint16_t adcc_usage=0;
52 volatile uint16_t ADC_Module_PowerUp(
void)
54 volatile uint16_t fres=1;
66 #elif defined (_ADC1MD) 90 volatile uint16_t ADC_Module_PowerDown(
void)
92 volatile uint16_t fres=1;
108 #elif defined (_ADC1MD) 133 volatile uint16_t ADC_Module_Initialize(
volatile HSADC_ADMODCFG_t adc_cfg )
135 volatile uint16_t fres = 1;
138 fres &= ADC_Module_PowerUp();
141 fres &= ADC_Module_Reset();
148 _ADON = ADCON1_ADON_DISABLED;
149 _ADSIDL = adc_cfg.config.adsidl;
151 _REFSEL = adc_cfg.refcfg.refsel;
152 _REFCIE = adc_cfg.refcfg.refcie;
153 _REFERCIE = adc_cfg.refcfg.refercie;
154 _WARMTIME = adc_cfg.config.warmtime;
156 _FORM = adc_cfg.config.form;
157 _EIEN = adc_cfg.config.eien;
158 ADCON2Lbits.PTGEN = adc_cfg.config.ptgen;
160 _CNVCHSEL = adc_cfg.swtrig.cnvchsel;
161 _SUSPEND = adc_cfg.swtrig.suspend;
162 _SUSPCIE = adc_cfg.swtrig.suspcie;
164 ADCON3Hbits.CLKSEL = adc_cfg.cores.clksel;
165 ADCON3Hbits.CLKDIV = adc_cfg.cores.clkdiv;
167 _SHRADCS = adc_cfg.cores.shared_core.adcs;
168 _SHRRES = adc_cfg.cores.shared_core.res;
169 _SHRSAMC = adc_cfg.cores.shared_core.samc;
170 _SHREISEL = adc_cfg.cores.shared_core.eisel;
172 #if defined (ADCORE0L) 173 ADCORE0Hbits.ADCS = adc_cfg.cores.core0.adcs;
174 ADCORE0Hbits.RES = adc_cfg.cores.core0.res;
175 ADCORE0Hbits.EISEL = adc_cfg.cores.core0.eisel;
176 ADCORE0Lbits.SAMC = adc_cfg.cores.core0.samc;
177 ADCON4Lbits.SAMC0EN = adc_cfg.cores.core0.samc_en;
179 #if defined (ADCORE1L) 180 ADCORE1Hbits.ADCS = adc_cfg.cores.core1.adcs;
181 ADCORE1Hbits.RES = adc_cfg.cores.core1.res;
182 ADCORE1Hbits.EISEL = adc_cfg.cores.core1.eisel;
183 ADCORE1Lbits.SAMC = adc_cfg.cores.core1.samc;
184 ADCON4Lbits.SAMC0EN = adc_cfg.cores.core1.samc_en;
186 #if defined (ADCORE2L) 187 ADCORE2Hbits.ADCS = adc_cfg.cores.core2.adcs;
188 ADCORE2Hbits.RES = adc_cfg.cores.core2.res;
189 ADCORE2Hbits.EISEL = adc_cfg.cores.core2.eisel;
190 ADCORE2Lbits.SAMC = adc_cfg.cores.core2.samc;
191 ADCON4Lbits.SAMC0EN = adc_cfg.cores.core2.samc_en;
213 volatile uint16_t ADC_ADInput_Initialize(
volatile HSADC_ADCANCFG_t adin_cfg ) {
215 volatile uint16_t fres = 1;
218 if(adin_cfg.ad_input > ADC_ANINPUT_COUNT) {
return(0); }
221 adcc_usage |= (0x0001 << adin_cfg.config.core_index);
225 fres &= ADC_ADInput_SetMode(adin_cfg);
228 fres &= ADC_ADInput_SetInterrupt(adin_cfg);
231 fres &= ADC_ADInput_SetTriggerMode(adin_cfg);
234 fres &= ADC_ADInput_SetTriggerSource(adin_cfg);
253 volatile uint16_t ADC_Module_Enable(
void)
255 volatile uint16_t fres=1;
256 volatile uint16_t i=0;
257 volatile uint16_t adcore_check=0;
263 for (i=0; i<ADC_CORE_COUNT; i++) {
264 adcore_check = (adcc_usage & (0x0001 << i));
266 { fres &= ADC_Core_PowerUp(i); }
285 volatile uint16_t ADC_Module_Disable(
void)
288 ADCON1Lbits.ADON = ADC_OFF;
289 return(1 - ADCON1Lbits.ADON);
305 volatile uint16_t ADC_Module_Reset(
void) {
311 ADCON1Lbits.ADON = ADC_OFF;
313 ADCON1L = REG_ADCON1L_RESET;
314 ADCON1H = REG_ADCON1H_RESET;
315 ADCON2L = REG_ADCON2L_RESET;
316 ADCON2H = REG_ADCON2H_RESET;
317 ADCON3L = REG_ADCON3L_RESET;
318 ADCON3H = REG_ADCON3H_RESET;
319 #if defined (ADCON4L) 320 ADCON4L = REG_ADCON4L_RESET;
322 #if defined (ADCON4H) 323 ADCON4H = REG_ADCON4H_RESET;
325 ADCON5L = REG_ADCON5L_RESET;
326 ADCON5H = REG_ADCON5H_RESET;
329 ADMOD0L = REG_ADMOD0L_RESET;
330 ADMOD0H = REG_ADMOD0H_RESET;
331 ADMOD1L = REG_ADMOD1L_RESET;
333 ADMOD1H = REG_ADMOD1H_RESET;
335 ADIEL = REG_ADIEL_RESET;
336 ADIEH = REG_ADIEH_RESET;
337 ADSTATL = REG_ADSTATL_RESET;
338 ADSTATH = REG_ADSTATH_RESET;
339 ADEIEL = REG_ADEIEL_RESET;
340 ADEIEH = REG_ADEIEH_RESET;
341 ADEISTATL = REG_ADEISTATL_RESET;
342 ADEISTATH = REG_ADEISTATH_RESET;
344 ADLVLTRGL = REG_ADLVLTRGL_RESET;
345 ADLVLTRGH = REG_ADLVLTRGH_RESET;
372 volatile uint16_t ADC_Core_CheckReady(
void)
374 volatile uint16_t fres = 1;
375 volatile uint16_t timeout = 0;
376 volatile uint16_t rdy_compare = 0;
377 volatile uint16_t reg_buf = 0;
384 reg_buf = (ADCON5L & 0x00FF);
385 rdy_compare = ((reg_buf << 8) | reg_buf);
387 while( (ADCON5L != rdy_compare) && (timeout++ < 0xFFFE) );
388 if(timeout == 0xFFFF) { fres = 0; }
415 volatile uint16_t ADC_Core_PowerUp(
volatile uint16_t index)
417 volatile uint16_t fres=1;
418 volatile uint16_t *regptr;
419 volatile uint16_t reg_buf=0;
420 volatile uint16_t timeout=0;
424 if (index >= ADC_CORE_COUNT)
return(0);
426 regptr = (
volatile uint16_t *)&ADCON5L;
429 if(index == ADC_SHARED_CORE_INDEX) {
432 reg_buf = (REG_ADCON5L_SHRPWR_ON & REG_ADCON5L_VALID_DATA_WRITE_MSK);
437 reg_buf = ((0x0001 << index) & REG_ADCON5L_VALID_DATA_WRITE_MSK);
440 if(!(*regptr & reg_buf))
441 { *regptr |= reg_buf; }
442 fres &= (
volatile bool)((*regptr & reg_buf) == reg_buf);
446 while(!(ADCON5L & reg_buf) && (timeout++ < 0xFFFE) );
447 if(timeout == 0xFFFF)
return(0);
450 regptr = (
volatile uint16_t *)&ADCON3H;
453 if(index == ADC_SHARED_CORE_INDEX) {
456 reg_buf = (REG_ADCON3H_SHREN_ENABLED & REG_ADCON3H_VALID_DATA_WRITE_MSK);
461 reg_buf = ((0x0001 << index) & REG_ADCON3H_VALID_DATA_WRITE_MSK);
464 if(!(*regptr & reg_buf))
465 { *regptr |= reg_buf; }
466 fres &= (
volatile bool)((*regptr & reg_buf) == reg_buf);
491 volatile uint16_t ADC_ADInput_SetTriggerSource(
volatile HSADC_ADCANCFG_t adin_cfg)
493 volatile uint16_t fres = 1;
494 volatile uint8_t *regptr;
497 if (adin_cfg.ad_input >= ADC_ANINPUT_COUNT)
return(0);
500 regptr = (
volatile uint8_t *)&ADTRIG0L;
501 regptr += (
volatile uint8_t)adin_cfg.ad_input;
502 *regptr = (
volatile uint8_t)adin_cfg.config.trigger_source;
505 fres &= (
volatile bool)((*regptr == (
volatile uint8_t)adin_cfg.config.trigger_source));
527 volatile uint16_t ADC_ADInput_SetInterrupt(
volatile HSADC_ADCANCFG_t adin_cfg)
529 volatile uint16_t fres = 1;
530 volatile uint16_t ad_idx = 0;
531 volatile uint16_t reg_val = 0;
535 if (adin_cfg.ad_input >= ADC_ANINPUT_COUNT)
538 ad_idx = (
volatile uint16_t)adin_cfg.ad_input;
543 reg_val = (
volatile uint16_t)adin_cfg.config.early_interrupt_enable;
545 ADEIEL |= (reg_val & REG_ADEIEL_VALID_DATA_MSK);
546 fres &= (
volatile bool)((ADEIEL & reg_val) == reg_val);
549 reg_val = (
volatile uint16_t)adin_cfg.config.interrupt_enable;
551 ADIEL |= (reg_val & REG_ADIEL_VALID_DATA_MSK);
552 fres &= (
volatile bool)((ADIEL & reg_val) == reg_val);
561 reg_val = (
volatile uint16_t)adin_cfg.config.early_interrupt_enable;
563 reg_val &= REG_ADEIEH_VALID_DATA_MSK;
565 fres &= (
volatile bool)((ADEIEH & reg_val) == reg_val);
568 reg_val = (
volatile uint16_t)adin_cfg.config.interrupt_enable;
570 ADIEH |= (reg_val & REG_ADIEH_VALID_DATA_MSK);
571 fres &= (
volatile bool)((ADIEH & reg_val) == reg_val);
602 volatile uint16_t ADC_ADInput_SetTriggerMode(
volatile HSADC_ADCANCFG_t adin_cfg)
604 volatile uint16_t fres = 1;
605 volatile uint16_t regval = 0;
606 volatile uint16_t ad_idx = 0;
609 if (adin_cfg.ad_input >= ADC_ANINPUT_COUNT)
return(0);
611 ad_idx = adin_cfg.ad_input;
616 regval = (((
volatile uint16_t)adin_cfg.config.trigger_mode) << ad_idx);
617 ADLVLTRGL |= (regval & REG_ADSTATL_VALID_DATA_MSK);
618 fres &= (
volatile bool)((ADLVLTRGL & regval) == regval);
624 regval = (((
volatile uint16_t)adin_cfg.config.trigger_mode) << ad_idx);
625 ADLVLTRGH |= (regval & REG_ADSTATH_VALID_DATA_MSK);
626 fres &= (
volatile bool)((ADLVLTRGH & regval) == regval);
663 volatile uint16_t ADC_ADInput_SetMode(
volatile HSADC_ADCANCFG_t adin_cfg)
665 volatile uint16_t fres = 1;
666 volatile uint16_t ad_idx=0;
667 volatile uint16_t regval = 0;
670 if (adin_cfg.ad_input >= ADC_ANINPUT_COUNT)
673 ad_idx = adin_cfg.ad_input;
676 regval = (
volatile uint16_t)adin_cfg.config.input_mode;
678 regval |= (
volatile uint16_t)adin_cfg.config.data_mode;
681 if (adin_cfg.ad_input<8) {
685 ADMOD0L |= (regval & REG_ADMOD0L_VALID_DATA_MSK);
686 fres &= (
volatile bool)((ADMOD0L & regval) == regval);
688 else if (adin_cfg.ad_input<16) {
693 ADMOD0H |= (regval & REG_ADMOD0H_VALID_DATA_MSK);
694 fres &= (
volatile bool)((ADMOD0H & regval) == regval);
696 else if (adin_cfg.ad_input<24) {
701 ADMOD1L |= (regval & REG_ADMOD1L_VALID_DATA_MSK);
702 fres &= (
volatile bool)((ADMOD1L & regval) == regval);
706 #if defined (ADMOD1H) 711 ADMOD1H |= (regval & REG_ADMOD1H_VALID_DATA_MSK);
712 fres &= (
volatile bool)((ADMOD1H & regval) == regval);
743 volatile uint16_t ADC_ADComp_Initialize(
volatile uint16_t index,
volatile HSADC_ADCMP_CONFIG_t adcmp_cfg)
746 volatile uint16_t fres = 1;
747 volatile uint16_t *regptr16;
748 volatile uint16_t reg_offset = 0;
749 volatile uint16_t reg_value = 0;
752 if (index >= (ADC_ADCMP_COUNT-1))
return(0);
755 reg_offset = (index) * ((
volatile uint16_t)&ADCMP1CON - (
volatile uint16_t)&ADCMP0CON);
756 regptr16 = (
volatile uint16_t *)&ADCMP0CON + reg_offset;
757 *regptr16 = (adcmp_cfg.ADCMPxCON.value & REG_ADCMPxCON_VALID_DATA_WR_MSK);
758 fres &= ((*regptr16 & REG_ADCMPxCON_VALID_DATA_RD_MSK) == (adcmp_cfg.ADCMPxCON.value & REG_ADCMPxCON_VALID_DATA_WR_MSK));
761 reg_offset = (index) * ((
volatile uint16_t)&ADCMP1LO - (
volatile uint16_t)&ADCMP0LO);
762 regptr16 = (
volatile uint16_t *)&ADCMP0LO + reg_offset;
763 *regptr16 = (adcmp_cfg.ADCMPxLO & REG_ADCMPxLO_VALID_DATA_MSK);
764 fres &= ((*regptr16 & REG_ADCMPxLO_VALID_DATA_MSK) == (adcmp_cfg.ADCMPxLO & REG_ADCMPxLO_VALID_DATA_MSK));
767 reg_offset = (index) * ((
volatile uint16_t)&ADCMP1HI - (
volatile uint16_t)&ADCMP0HI);
768 regptr16 = (
volatile uint16_t *)&ADCMP0HI + reg_offset;
769 *regptr16 = (adcmp_cfg.ADCMPxHI & REG_ADCMPxHI_VALID_DATA_MSK);
770 fres &= ((*regptr16 & REG_ADCMPxHI_VALID_DATA_MSK) == (adcmp_cfg.ADCMPxHI & REG_ADCMPxHI_VALID_DATA_MSK));
774 reg_value = adcmp_cfg.ADCMPxCON.bits.chnl;
776 if (reg_value < 16) {
779 reg_offset = (index) * ((
volatile uint16_t)&ADCMP1ENL - (
volatile uint16_t)&ADCMP0ENL);
780 regptr16 = (
volatile uint16_t *)&ADCMP0ENL + reg_offset;
781 *regptr16 = ((1 << reg_value) & REG_ADCMPxENH_VALID_DATA_MSK);
784 else if (reg_value < 32) {
790 reg_offset = (index) * ((
volatile uint16_t)&ADCMP1ENH - (
volatile uint16_t)&ADCMP0ENH);
791 regptr16 = (
volatile uint16_t *)&ADCMP0ENH + reg_offset;
792 *regptr16 = ((1 << reg_value) & REG_ADCMPxENH_VALID_DATA_MSK);
800 fres &= (
volatile bool)(*regptr16 & (1 << reg_value));
827 volatile uint16_t ADC_ADFilter_Initialize(
volatile uint16_t index,
volatile HSADC_ADFLT_CONFIG_t adflt_cfg) {
829 volatile uint16_t fres = 1;
830 volatile uint16_t *regptr16;
831 volatile uint16_t reg_offset = 0;
835 if (index >= (ADC_ADFL_COUNT-1))
839 reg_offset = (index) * ((
volatile uint16_t)&ADFL1CON - (
volatile uint16_t)&ADFL0CON);
840 regptr16 = (
volatile uint16_t *)&ADFL0CON + reg_offset;
841 *regptr16 = (adflt_cfg.ADFLxCON.value & REG_ADFLxCON_VALID_DATA_WR_MSK);
842 fres &= ((*regptr16 & REG_ADFLxCON_VALID_DATA_RD_MSK) == (adflt_cfg.ADFLxCON.value & REG_ADFLxCON_VALID_DATA_WR_MSK));
845 reg_offset = (index) * ((
volatile uint16_t)&ADFL1DAT - (
volatile uint16_t)&ADFL0DAT);
846 regptr16 = (
volatile uint16_t *)&ADFL0DAT + reg_offset;
847 *regptr16 = (adflt_cfg.ADFLxDAT & REG_ADFLxDAT_VALID_DATA_MSK);
848 fres &= ((*regptr16 & REG_ADFLxDAT_VALID_DATA_MSK) == (adflt_cfg.ADFLxDAT & REG_ADFLxDAT_VALID_DATA_MSK));