Add I2CSlave to ov580 master.

Dependencies:   MorseGenerator2

Revision:
3:6b14862f5f51
Parent:
2:afc300c4f8e4
Child:
4:fc0c93cb0197
--- a/main.cpp	Fri Oct 19 16:45:09 2018 +0000
+++ b/main.cpp	Sun Oct 21 22:24:52 2018 +0000
@@ -3,11 +3,15 @@
 /*
 FEATURE REQUIREMENTS
 
-- I2C write occurs on
+-   I2C write occurs on falling edge of strobe pulse to prevent unknown states
+
+-   Stack check occurs on leading edge of strobe pulse to ensure strobes are
+    set correctly
 
--Reoccuring enable from app that will prevent the emitters from remaining
-on if the app crashes and a turn off command isn't sent
+-   Reoccuring enable from app that will prevent the emitters from remaining
+    on if the app crashes and a turn off command isn't sent
     - Can be a counter on the emitter enable > threshold
+    - Better if it's a counter based on reads from the fault register
 
 -Flood/Dot tagging indicated on GPIO3
     - 0 Flood
@@ -26,12 +30,28 @@
 
 0x01        Dot fault               Read from LM36011
 
-0x02        Flood Fault             Read from LM36011
+0x02        Dot Current             0x00 - level_dot_max
+
+0x03        Flood Fault             Read from LM36011
+
+0x04        Flood Current           0x00 - level_flood_max
 
-0x03        Kill VCSEL Fault        Define this
+0x05        Fault register          bit[0]      KILL_VCSEL fault
+                                    bit[1]      dot fault
+                                    bit[2]      flood fault
+                                    bit[3]      unstacked fault
+                                    bit[4]      ?not in app fault?
+                                    bit[5-7]    unused
+
+
+0x06        Version number          0x70        = version 7.0
 
 */
 
+/*          DEFINES         */
+
+#define version_number 0x70
+
 // define pins for I2C
 #define dot_sda p2
 #define dot_scl p4
@@ -39,8 +59,12 @@
 #define flood_scl p3
 #define ov580_sda p30
 #define ov580_scl p31
+
+// define strobe pins
 #define strobe_dot p23
 #define strobe_flood p24
+
+// define pmw pins
 #define pwm_1_pin p29
 #define pwm_0_pin p5
 
@@ -49,30 +73,33 @@
 #define ledGreen p17
 #define ledBlue p13
 
+
+/*          I/O            */
 // initialize LEDs
 DigitalOut red(ledRed,1);
 DigitalOut green(ledGreen,1);
 DigitalOut blue(ledBlue,1);
 
 // Initialize outputs
-DigitalIn pwm_1(pwm_1_pin,PullNone);
+DigitalOut pwm_0_output(pwm_0_pin,0);          // GPIO 3
+DigitalOut pwm_1_output(pwm_1_pin,0);          // GPIO 4
 
 // Initialize inputs
 DigitalIn strobe0(p23,PullNone);
 DigitalIn strobe1(p24,PullNone);
-DigitalIn pwm_0(pwm_0_pin,PullNone);
 DigitalIn vcselFault(p25,PullNone);
-DigitalIn killVcsel(p26,PullNone);
+//DigitalIn killVcsel(p26,PullNone);  // vcselFault is 1V8 instead of 3V3
 
+/*          REGISTERS       */
 static uint8_t LM36011_addr = 0x64 << 1;  //0xC8
 
 // register names
 static uint8_t enable_reg = 0x01;
 static uint8_t configuration_reg = 0x02;
 static uint8_t brightness_reg = 0x03;
-static uint8_t torch_reg = 0x04;
-static uint8_t flags_reg = 0x05;
-static uint8_t device_id_reset_reg = 0x06;
+//static uint8_t torch_reg = 0x04;
+//static uint8_t flags_reg = 0x05;
+//static uint8_t device_id_reset_reg = 0x06;
 
 // register settings
 static uint8_t enable_ir = 0x05;
@@ -81,64 +108,108 @@
 
 // level settings
 static uint8_t level_flood_max = 0x66;      // = 1.2 A
-//static uint8_t level_dot_max = 0x5F;      // = 1.03 A
+static uint8_t level_dot_max = 0x5F;      // = 1.03 A
 //static uint8_t level_dot_max = 0x3F;      // = 0.75 A
 //static uint8_t level_dot_max = 0x3C;      // = 0.70 A
 //static uint8_t level_dot_max = 0x15       //0.257 A
-static uint8_t level_dot_max = 0x1F;        // =  352mA
+//static uint8_t level_dot_max = 0x1F;        // =  352mA
 
-char lmInit[2] = {enable_reg,enable_ir};
-char lmOff[2] = {enable_reg,disable_ir};
+char lm_on[2] = {enable_reg,enable_ir};
+char lm_off[2] = {enable_reg,disable_ir};
 char lmSafety[2] = {configuration_reg,enable_flash_timeout};
 
 char flashBrightness_dot[2] = {brightness_reg,level_dot_max};
 char flashBrightness_flood[2] = {brightness_reg,level_flood_max};
 
+/*          INTERRUPTS      */
 //create interupts
-InterruptIn strobe_0(p23);
-InterruptIn strobe_1(p24);
+InterruptIn int_strobe_dot(strobe_dot);
+//InterruptIn int_strobe_flood(strobe_flood);  // only need one interrupt in
 
+/*          I2C             */
 // i2c declarations
 I2C flood_I2C(flood_sda,flood_scl);
 I2C dot_I2C(dot_sda,dot_scl);
 I2CSlave ov_I2C(ov580_sda,ov580_scl);
 
-// functions
+/*          VARIABLES       */
+bool stacked = false;
+bool emitter_status_dot = false;
+int timeout = 20;
+char rcv_buffer[3] = {0,0,0};
+
+/*          FUNCTIONS       */
 void lightsOn()
 {
- 
+    pwm_0_output = emitter_status_dot;
 }
 
 void stack_check()
 {
-
+    stacked = strobe1.read();
+    emitter_status_dot = !emitter_status_dot;
 }
 
 // main() runs in its own thread in the OS
 int main()
 {
+    wait(10);
 
     // set I2C Frequency to 400kHz
     flood_I2C.frequency(400000);
     dot_I2C.frequency(400000);
+    ov_I2C.frequency(400000);
+
+    ov_I2C.address(0x10);
 
     // write safety
     flood_I2C.write(LM36011_addr,lmSafety,2,false);
     dot_I2C.write(LM36011_addr,lmSafety,2,false);
-    
+
     // write brightness
     flood_I2C.write(LM36011_addr,flashBrightness_flood,2,false);
-    dot_I2C.write(LM36011_addr,flashBrightness_dot,2,false);    
+    dot_I2C.write(LM36011_addr,flashBrightness_dot,2,false);
+    //dot_I2C.write(LM36011_addr,lm_on,2,false);
 
     // set interrupts
-    strobe_0.rise(stack_check);
-    strobe_0.fall(lightsOn);
+    int_strobe_dot.rise(stack_check);
+    int_strobe_dot.fall(lightsOn);
+
+    char msg[2] = {1,1};
 
 
     while (true) {
+        int i = ov_I2C.receive();
 
-        green = !green;
-        wait(0.5);
+        switch(i) {
+            case I2CSlave::ReadAddressed:
+                ov_I2C.write(msg, 2);
+                break;
+            case I2CSlave::WriteGeneral:
+                ov_I2C.read(rcv_buffer, 3);
+                break;
+            case I2CSlave::WriteAddressed:
+                ov_I2C.read(rcv_buffer, 3);
+                /*
+                if ((int)rcv_buffer[0] == 1 && (int)rcv_buffer[1] == 1) {
+                    green = 0;
+                    red = 1;
+                } else {
+                    red = 0;
+                    green = 1;
+                }
+                */
+                green = 0;
+                dot_I2C.write(LM36011_addr,lm_on,2,false);
+                break;
+        }
+
+        // clear buffer
+        for (int k = 0; k < 3; k++) rcv_buffer[i]=0;
+
+        //green = !emitter_status_dot;
+        //red = emitter_status_dot;
+
     }
 }