|
| 1 | +#include <stdint.h> |
| 2 | +#include "Arduino.h" |
| 3 | + |
| 4 | +#include "moisture.h" |
| 5 | +#include "fastTimer.h" |
| 6 | + |
| 7 | +static __inline__ void syncGCLK() __attribute__ ((always_inline, unused)); |
| 8 | +static void syncGCLK(){ |
| 9 | + while(GCLK->STATUS.bit.SYNCBUSY == 1); |
| 10 | +} |
| 11 | + |
| 12 | +static __inline__ void ACsync() __attribute__ ((always_inline, unused)); |
| 13 | +static void ACsync(){ |
| 14 | + while(AC->STATUSB.bit.SYNCBUSY == 1); |
| 15 | +} |
| 16 | + |
| 17 | +#define LOWER_TH 10 |
| 18 | +#define UPPER_TH 50 |
| 19 | + |
| 20 | +volatile uint32_t cycles = 0; |
| 21 | + |
| 22 | +uint32_t start = 0; |
| 23 | +uint32_t finish = 0; |
| 24 | + |
| 25 | +uint8_t pin; |
| 26 | + |
| 27 | +void AC_Handler(){ |
| 28 | + cycles += 1; |
| 29 | + |
| 30 | + uint8_t ACinterrupt = REG_AC_INTFLAG; // copy out flag reg |
| 31 | + AC->INTFLAG.bit.COMP0=1; //Reset int flags |
| 32 | + |
| 33 | + if(ACinterrupt & 0x1){ |
| 34 | + digitalWrite(10, HIGH); |
| 35 | + } |
| 36 | + else if(ACinterrupt & 0x2){ |
| 37 | + digitalWrite(10, LOW); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// datasheet pg 896 for all AC registers |
| 42 | +void init_AC(uint8_t outPin){ |
| 43 | + pin = outPin; |
| 44 | + |
| 45 | + pinMode(pin, OUTPUT); |
| 46 | + digitalWrite(pin, LOW); |
| 47 | + |
| 48 | + // Setup the clock sources for the ADC analog and digitalWrite Pg 95 |
| 49 | + syncGCLK(); |
| 50 | + GCLK->CLKCTRL.reg = 0x4120; //enable GGCLK for AC_ana, clkgen1 = 32 kHz crystal |
| 51 | + syncGCLK(); |
| 52 | + GCLK->CLKCTRL.reg = 0x401F; //enable GGCLK for AC_dig, clkgen0 = 48 MHz PLL |
| 53 | + syncGCLK(); |
| 54 | + |
| 55 | + // Enable the AC clock in the power manager reg |
| 56 | +// PM->APBCMASK.bit.AC = 1; ? doesnt like this |
| 57 | + REG_PM_APBCMASK |= PM_APBCMASK_AC; |
| 58 | + |
| 59 | + // Disable comparator |
| 60 | + AC->CTRLA.bit.ENABLE=0; |
| 61 | + |
| 62 | + ACsync(); |
| 63 | + |
| 64 | + AC->COMPCTRL[0].bit.HYST=1; //Enable hysteresis Pg 909 |
| 65 | + AC->COMPCTRL[0].bit.OUT=0; //Output is not routed to a pin Pg 909 |
| 66 | + |
| 67 | + AC->COMPCTRL[0].bit.MUXPOS=0x1; //pin 0 as positive input. Pg 910 |
| 68 | + AC->COMPCTRL[0].bit.MUXNEG=0x5; //Vscale negative input Pg 910 |
| 69 | + |
| 70 | + AC->COMPCTRL[0].bit.INTSEL=0x2; //Set interrupt mode rising=0x1 falling=0x2 Pg 911 |
| 71 | + |
| 72 | + AC->COMPCTRL[0].bit.SPEED=1; //AC speed high=1, low=0 Pg 911 |
| 73 | + AC->COMPCTRL[0].bit.SINGLE=0; //Continuous measurement Pg 911 |
| 74 | + |
| 75 | + ACsync(); |
| 76 | + |
| 77 | + AC->COMPCTRL[1].bit.HYST=1; //Enable hysteresis Pg 909 |
| 78 | + AC->COMPCTRL[1].bit.OUT=0; //Output is not routed to a pin Pg 909 |
| 79 | + |
| 80 | + AC->COMPCTRL[1].bit.MUXPOS=0x1; //pin 0 as positive input. Pg 910 |
| 81 | + AC->COMPCTRL[1].bit.MUXNEG=0x5; //Vscale negative input Pg 910 |
| 82 | + |
| 83 | + AC->COMPCTRL[1].bit.INTSEL=0x1; //Set interrupt mode rising=0x1 falling=0x2 Pg 911 |
| 84 | + |
| 85 | + AC->COMPCTRL[1].bit.SPEED=1; //AC speed high=1, low=0 Pg 911 |
| 86 | + AC->COMPCTRL[1].bit.SINGLE=0; //Continuous measurement Pg 911 |
| 87 | + |
| 88 | + ACsync(); |
| 89 | + |
| 90 | + // Enable AC interrupts |
| 91 | + AC->INTENSET.bit.COMP0=1; //Enable AC0 interrupt Pg 902 |
| 92 | + AC->INTENSET.bit.COMP1=1; //Enable AC1 interrupt Pg 902 |
| 93 | + |
| 94 | + //set AC Vscale voltages |
| 95 | + // Vscale = (Vdd*(value + 1)) / 64 |
| 96 | + REG_AC_SCALER0 = LOWER_TH; |
| 97 | + REG_AC_SCALER1 = UPPER_TH; |
| 98 | + |
| 99 | + |
| 100 | + // Enable interrupt vector |
| 101 | + NVIC_EnableIRQ(AC_IRQn); |
| 102 | + |
| 103 | + ACsync(); |
| 104 | + AC->COMPCTRL[0].bit.ENABLE=1; //Enable AC 0 Pg 908 |
| 105 | + ACsync(); |
| 106 | + AC->COMPCTRL[1].bit.ENABLE=1; //Enable AC 1 Pg 908 |
| 107 | + ACsync(); |
| 108 | + |
| 109 | + AC->CTRLA.bit.ENABLE=1; //Enable AC unit Pg 898 |
| 110 | + ACsync(); |
| 111 | + AC->INTFLAG.bit.COMP0=1; //Reset int flags |
| 112 | + ACsync(); |
| 113 | + AC->INTFLAG.bit.COMP1=1; //Reset int flags |
| 114 | + |
| 115 | + digitalWrite(pin, HIGH); |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +void disableAC(){ |
| 120 | + ACsync(); |
| 121 | + AC->CTRLA.bit.ENABLE=0; //disable AC Pg 898 |
| 122 | + ACsync(); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +uint32_t takeSoilReading(uint16_t counts){ |
| 127 | + |
| 128 | + init_TC(); |
| 129 | + reset_TC(); |
| 130 | + cycles = 0; |
| 131 | + |
| 132 | + while(cycles < counts){ |
| 133 | + if(timeout_TC()){ |
| 134 | + Serial.println("true"); |
| 135 | + return 0; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + uint32_t time = read_TC(); |
| 140 | + |
| 141 | + disable_TC(); |
| 142 | + |
| 143 | + uint32_t period = ticks_to_micros(time) / counts; |
| 144 | + // uint32_t cap = ((double(dur))*1000000) / double(539227); |
| 145 | + |
| 146 | + return period; |
| 147 | + |
| 148 | +} |
0 commit comments