Add I2CSlave to ov580 master.

Dependencies:   MorseGenerator2

main.cpp

Committer:
claytonk
Date:
2018-10-19
Revision:
1:ffffa383ba7e
Parent:
0:605fe136b7ea
Child:
2:afc300c4f8e4

File content as of revision 1:ffffa383ba7e:

#include "mbed.h"

/*
FEATURE REQUIREMENTS

- I2C write occurs on

-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

-Flood/Dot tagging indicated on GPIO3
    - 0 Flood
    - 1 DOT

-Fault indication on GPIO4 to indicate to app to reboot
    - 0 OK
    - 1 Fault/Reboot

I2C write commands
Reg:        Information             Condition
0x00        Emitter enable          0x00        Emitters off
                                    0x01        Dot on
                                    0x10        Flood on
                                    0x11        Flood / Dot on

0x01        Dot fault               Read from LM36011

0x02        Flood Fault             Read from LM36011

0x03        Kill VCSEL Fault        Define this

*/

// define pins for I2C
#define dot_sda p2
#define dot_scl p4
#define flood_sda p28
#define flood_scl p3
#define ov580_sda p30
#define ov580_scl p31
#define strobe_dot p23
#define strobe_flood p24
#define pwm_1_pin p29
#define pwm_0_pin p5

// define LED pins
#define ledRed p12
#define ledGreen p17
#define ledBlue p13

// initialize LEDs
DigitalOut red(ledRed,1);
DigitalOut green(ledGreen,1);
DigitalOut blue(ledBlue,1);

// Initialize outputs
DigitalIn pwm_1(pwm_1_pin,PullNone);

// 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);

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;

// register settings
static uint8_t enable_ir = 0x05;
static uint8_t disable_ir = 0x20;
static uint8_t enable_flash_timeout = 0x01;

// 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 = 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

//size_t write_size 2;
bool err;
bool on = false;
bool stacked_error = false;
bool in_app = false;
uint64_t stacked_counter = 0;
bool flip = false;
bool dot_on = false;
bool flood_on = false;
bool pulsed = false;

char lmInit[2] = {enable_reg,enable_ir};
char lmOff[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};

//create interupts
InterruptIn strobe_0(p23);
InterruptIn strobe_1(p24);

// i2c declarations
I2C flood_I2C(flood_sda,flood_scl);
I2C dot_I2C(dot_sda,dot_scl);
I2CSlave ov_I2C(ov580_sda,ov580_scl);

// functions
void lightsOn()
{
    // set on true if stacked error isn't present
    if(in_app) {

        if(!pulsed && dot_on) {
            // set driver current
            dot_I2C.write(LM36011_addr, lmInit, 2, false);
            wait_us(2);
        } else if (pulsed && flip) {
            dot_I2C.write(LM36011_addr, lmInit, 2, false);
            wait_us(2);
        } else {
            dot_I2C.write(LM36011_addr, lmOff, 2, false);
            wait_us(2);
        }

        if(!pulsed && flood_on) {
            flood_I2C.write(LM36011_addr, lmInit, 2, false);
            wait_us(2);
        } else if (pulsed && !flip) {
            flood_I2C.write(LM36011_addr, lmInit, 2, false);
            wait_us(2);
        } else {
            flood_I2C.write(LM36011_addr, lmOff, 2, false);
            wait_us(2);
        }
    }
}

void stack_check()
{
    // check strobe 1 for high, and if high set stacked_error
    stacked_error = strobe_1.read();
    if(stacked_error) {
        stacked_counter++;
    } else {
        in_app = false;
        stacked_counter = 0;
    }
    if(stacked_counter > 30) {
        in_app = true;
    }
    flood_on = pwm_0;
    dot_on = pwm_1;
    if(flood_on && dot_on) {
        pulsed = true;

    } else {
        pulsed = false;
    }

    flip = !flip;
}

// main() runs in its own thread in the OS
int main()
{

    // set I2C Frequency to 400kHz
    flood_I2C.frequency(400000);
    dot_I2C.frequency(400000);

    // 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);    

    // set interrupts
    strobe_0.fall(lightsOn);
    strobe_0.rise(stack_check);

    while (true) {

        green = !green;
        wait(0.5);
    }
}