From ae7d26b6591885784015a7be0960193d7c97f44d Mon Sep 17 00:00:00 2001
From: "Michael D. M. Dryden" <mdryden@chem.utoronto.ca>
Date: Wed, 27 Jan 2016 19:38:40 -0500
Subject: [PATCH] Added module to control PMT shutter. Added temporary commands
 to main.c to control.

---
 DSTAT/src/experiment.c |  2 +-
 DSTAT/src/experiment.h |  1 +
 DSTAT/src/main.c       | 22 ++++++++++++-
 DSTAT/src/shutter.c    | 71 ++++++++++++++++++++++++++++++++++++++++++
 DSTAT/src/shutter.h    | 24 ++++++++++++++
 5 files changed, 118 insertions(+), 2 deletions(-)
 create mode 100644 DSTAT/src/shutter.c
 create mode 100644 DSTAT/src/shutter.h

diff --git a/DSTAT/src/experiment.c b/DSTAT/src/experiment.c
index 3da1d45..458cbc7 100644
--- a/DSTAT/src/experiment.c
+++ b/DSTAT/src/experiment.c
@@ -38,7 +38,7 @@ volatile uint16_t tcf0period = 0;
 uint32_t skip_samples = 0;
 
 //Private function declarations
-uint16_t set_timer_period(uint32_t period, volatile void *tc);
+//uint16_t set_timer_period(uint32_t period, volatile void *tc);
 static void precond_rtc_callback(uint32_t time);
 static void porte_int0_lsv(void);
 static void tcf0_ovf_callback(void);
diff --git a/DSTAT/src/experiment.h b/DSTAT/src/experiment.h
index 7c171ae..5c47469 100644
--- a/DSTAT/src/experiment.h
+++ b/DSTAT/src/experiment.h
@@ -76,6 +76,7 @@ extern uint16_t g_gain;
 extern uint8_t g_short;
 extern uint8_t autogain_enable;
 
+uint16_t set_timer_period(uint32_t period, volatile void *tc);
 void experiment_handler(char command);
 void pot_init(void);
 void pot_set_gain(void);
diff --git a/DSTAT/src/main.c b/DSTAT/src/main.c
index c7e5c68..d219490 100644
--- a/DSTAT/src/main.c
+++ b/DSTAT/src/main.c
@@ -9,6 +9,7 @@
 #include "asf.h"
 #include "settings.h"
 #include "tcs.h"
+#include "shutter.h"
 #include <string.h>
 #include <math.h>
 #include <stdint.h>
@@ -23,6 +24,8 @@ int8_t command_handler(char command){
 	 * Calls functions in 
 	 * @param command Command character input.
 	 */
+    
+    double p1;
 
 	switch (command){
         case 'E': //Experiment options
@@ -44,6 +47,23 @@ int8_t command_handler(char command){
                 printf("T%u.%u.%u.%u\n\r", tcs_data[0], tcs_data[1], tcs_data[2], tcs_data[3]);
             }
             break;
+        
+        case 'Z': //Test shutter
+            scanf("%lg",&p1);
+            shutter_cont(p1);
+            break;
+        
+        case 'z':
+            shutter_cont_stop();
+            break;
+        
+        case '1':
+            shutter_close();
+            break;
+            
+        case '2':
+            shutter_open();
+            break;
             
 		case 'V': //check version
 			printf("V%u.%u\n\r", BOARD_VER_MAJOR, BOARD_VER_MINOR);
@@ -110,7 +130,7 @@ int main(void){
 	}
     
     tcs_init();
-
+    shutter_init();
 	
 	program_loop:
 		while(getchar() != '!');
diff --git a/DSTAT/src/shutter.c b/DSTAT/src/shutter.c
new file mode 100644
index 0000000..1c8a4ac
--- /dev/null
+++ b/DSTAT/src/shutter.c
@@ -0,0 +1,71 @@
+//
+//  shutter.c
+//  dstat-firmware
+//
+//  Created by Michael Dryden on 2016-01-27.
+//  Copyright © 2016 wheeler lab. All rights reserved.
+//
+
+#include "shutter.h"
+#include <stdint.h>
+#include <tc.h>
+#include <math.h>
+
+void shutter_init(void){
+    ioport_set_pin_dir(SHUTTER_PIN, IOPORT_DIR_OUTPUT);
+    ioport_set_pin_level(SHUTTER_PIN, 0);
+}
+
+void shutter_open(void){
+    ioport_set_pin_level(SHUTTER_PIN, 1);
+}
+
+void shutter_close(void){
+    ioport_set_pin_level(SHUTTER_PIN, 0);
+}
+
+uint8_t shutter_cont(double freq){
+    tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_OFF_gc);
+    if (freq > 30 || freq < 0.23842) {
+        return 1;
+    }
+
+    else{
+        tc_enable(&SHUTTER_TC);
+        tc_set_wgm(&SHUTTER_TC, TC_WG_FRQ);
+        tc_enable_cc_channels(&SHUTTER_TC, TC_CCAEN);
+        
+        tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_DIV64_gc);
+
+        uint16_t temp_div = ceil((1/(2*freq))*F_CPU/65536);
+        uint16_t divider = 0;
+        
+        if (temp_div <= 64){
+            tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV64_gc);
+            divider = 64;
+        }
+        else if (temp_div <= 256){
+            tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV256_gc);
+            divider = 256;
+        }
+        else if (temp_div <= 1024){
+            tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV1024_gc);
+            divider = 1024;
+        }
+        else{
+            printf("#ERR: Frequency/ADC rate is too low\n\r");
+            return 0;
+        }
+
+        SHUTTER_TC.CCA = ((uint16_t)((F_CPU/divider)/(2*freq)))-1; //f=1/(2*(CCA+1)*f_clk)
+    
+        return 0;
+    }
+}
+
+void shutter_cont_stop(void){
+    tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_OFF_gc);
+    tc_set_wgm(&SHUTTER_TC, TC_WG_NORMAL);
+    tc_disable(&SHUTTER_TC);
+    ioport_set_pin_level(SHUTTER_PIN, 0);
+}
\ No newline at end of file
diff --git a/DSTAT/src/shutter.h b/DSTAT/src/shutter.h
new file mode 100644
index 0000000..c1ba5dd
--- /dev/null
+++ b/DSTAT/src/shutter.h
@@ -0,0 +1,24 @@
+//
+//  shutter.h
+//  dstat-firmware
+//
+//  Created by Michael Dryden on 2016-01-27.
+//  Copyright © 2016 wheeler lab. All rights reserved.
+//
+
+#ifndef shutter_h
+#define shutter_h
+
+#include <stdio.h>
+#include <ioport.h>
+
+#define SHUTTER_PIN     IOPORT_CREATE_PIN(PORTE,0)
+#define SHUTTER_TC      TCE0
+
+void shutter_init(void);
+void shutter_open(void);
+void shutter_close(void);
+uint8_t shutter_cont(double freq);
+void shutter_cont_stop(void);
+
+#endif /* shutter_h */
-- 
GitLab