Skip to content

Commit 42753c9

Browse files
committed
Added Moisture lib + FRAM change
1 parent 7bc20d6 commit 42753c9

File tree

10 files changed

+335
-2
lines changed

10 files changed

+335
-2
lines changed

microNode/libraries/FRAM/fram.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ uint8_t FRAMread(uint16_t address){
4242
}
4343

4444

45-
void FRAMreadblock(uint16_t startAddress, uint16_t number, uint8_t buffer[]){
45+
void FRAMreadblock(uint16_t startAddress, uint8_t buffer[], uint16_t number){
4646
Wire.beginTransmission(pageAddress(startAddress));
4747
Wire.write(wordAddress(startAddress));
4848
Wire.endTransmission(false);

microNode/libraries/FRAM/fram.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void FRAMwriteblock(uint16_t startAddress, uint8_t data[], uint16_t length);
1515

1616
uint8_t FRAMread(uint16_t address);
1717

18-
void FRAMreadblock(uint16_t startAddress, uint16_t number, uint8_t buffer[]);
18+
void FRAMreadblock(uint16_t startAddress, uint8_t buffer[], uint16_t number);
1919

2020

2121
void FRAMpack(uint16_t address, void* data, uint8_t len);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdint.h>
2+
#include <moisture.h>
3+
4+
#define COUNT_NUMBER 1000
5+
6+
#define CAP_DRIVE_PIN 10
7+
8+
9+
void setup(){
10+
Serial.begin(9600);
11+
while(!Serial);
12+
init_AC(CAP_DRIVE_PIN);
13+
14+
}
15+
16+
void loop(){
17+
18+
uint32_t reading = takeSoilReading(COUNT_NUMBER);
19+
20+
Serial.println(reading);
21+
22+
delay(10);
23+
}
24+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "Arduino.h"
2+
#include <stdint.h>
3+
4+
#include "fastTimer.h"
5+
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 syncTC() __attribute__ ((always_inline, unused));
13+
static void syncTC(){
14+
while(TC5->COUNT16.STATUS.reg & TC_STATUS_SYNCBUSY);
15+
}
16+
17+
18+
#define TIMER_CLK 37500
19+
#define TIMER_PRESCALE 4
20+
21+
22+
uint8_t timeout_TC(){
23+
if(TC5->COUNT16.INTFLAG.bit.OVF){
24+
TC5->COUNT16.INTFLAG.bit.OVF = 1;
25+
return 1;
26+
}
27+
return 0;
28+
}
29+
30+
void init_TC(){
31+
syncGCLK();
32+
GCLK->CLKCTRL.reg = 0x411C; //enable GGCLK for AC_ana, clkgen1 = 32 kHz crystal
33+
syncGCLK();
34+
35+
TC5->COUNT16.CTRLA.reg = TC_CTRLA_SWRST;
36+
syncTC();
37+
while (TC5->COUNT16.CTRLA.bit.SWRST);
38+
39+
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
40+
41+
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV4 | TC_CTRLA_ENABLE;
42+
43+
TC5->COUNT16.INTENSET.bit.OVF = 1;
44+
syncTC();
45+
46+
}
47+
48+
49+
void reset_TC(){
50+
TC5->COUNT16.COUNT.reg = 0;
51+
TC5->COUNT16.INTENSET.bit.OVF = 1;
52+
syncTC();
53+
}
54+
55+
56+
uint32_t read_TC(){
57+
return TC5->COUNT16.COUNT.reg;
58+
}
59+
60+
61+
void disable_TC(){
62+
TC5->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
63+
syncTC();
64+
}
65+
66+
67+
uint32_t micros_to_ticks(uint32_t micros){
68+
return (micros * TIMER_CLK * TIMER_PRESCALE) / 1e6;
69+
}
70+
71+
72+
uint32_t ticks_to_micros(uint32_t ticks){
73+
return (ticks * TIMER_PRESCALE * 1e6) / TIMER_CLK;
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdint.h>
2+
#include "Arduino.h"
3+
4+
5+
void init_TC();
6+
7+
void reset_TC();
8+
9+
uint32_t read_TC();
10+
11+
uint8_t timeout_TC();
12+
13+
void disable_TC();
14+
15+
uint32_t micros_to_ticks(uint32_t micros);
16+
17+
uint32_t ticks_to_micros(uint32_t ticks);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#######################################
2+
# Syntax Coloring Map FRAM
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
10+
#######################################
11+
# Methods and Functions (KEYWORD2)
12+
#######################################
13+
init_AC KEYWORD2
14+
disableAC KEYWORD2
15+
takeSoilReading KEYWORD2
16+
17+
18+
#######################################
19+
# Constants (LITERAL1)
20+
#######################################
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdint.h>
2+
#include "Arduino.h"
3+
4+
// datasheet pg 896 for all AC registers
5+
void init_AC(uint8_t outPin);
6+
7+
void disableAC();
8+
9+
uint32_t takeSoilReading(uint16_t counts);
10+

package_loranodes_index.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,46 @@
312312
"version": "4.0.0-atmel"
313313
}
314314
]
315+
},
316+
{
317+
"version": "1.0.5",
318+
"boards": [
319+
{
320+
"name": "microNode"
321+
}
322+
],
323+
"archiveFileName": "microNode-1.0.5.tar.bz2",
324+
"checksum": "SHA-256:fe8396b9e3038800d52eab8d3380351cab442d9477ac1df0148f750a69fb0754",
325+
"url": "https://github.com/loranodes/arduino-core/raw/1.0.5/microNode-1.0.5.tar.bz2",
326+
"size": 1181620,
327+
"architecture": "samd",
328+
"name": "LoRaNodes M0 Boards",
329+
"category": "LoRaNodes",
330+
"help": {
331+
"online": "https://github.com/loranodes"
332+
},
333+
"toolsDependencies": [
334+
{
335+
"name": "arm-none-eabi-gcc",
336+
"packager": "arduino",
337+
"version": "4.8.3-2014q1"
338+
},
339+
{
340+
"name": "bossac",
341+
"packager": "arduino",
342+
"version": "1.6.1-arduino"
343+
},
344+
{
345+
"name": "openocd",
346+
"packager": "arduino",
347+
"version": "0.9.0-arduino"
348+
},
349+
{
350+
"name": "CMSIS",
351+
"packager": "arduino",
352+
"version": "4.0.0-atmel"
353+
}
354+
]
315355
}
316356
]
317357
}

0 commit comments

Comments
 (0)