/* *****************************************************************************
 * main.c
 * Author: a0233209
 * Date: Jan 2020
 *
 *  * Description: Sample use of I2C drivers on MSP430FRxxxx to interface with
 * - an I/O Expander device. Code will configure the device pin as outputs and flash
 * - each LED in sequence on the first port register.
 *
 * Copyright (c) 2019 Texas Instruments Incorporated.  All rights reserved.
 * Software License Agreement
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of Texas Instruments Incorporated nor the names of
 * its contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/


#include <msp430.h> 
#include "USCI2C_master.h"

// Example slave: TCA9539
#define SLAVE_ADDR      0x21

#define SLAVE_INPUT0    0x00
#define SLAVE_INPUT1    0x01
#define SLAVE_OUTPUT0   0x02
#define SLAVE_OUTPUT1   0x03
#define SLAVE_CONFIG0   0x06
#define SLAVE_CONFIG1   0x07

#define BLINK_DELAY     80000

const unsigned char desired_config0 = 0x00; // port0 as outputs

void init_GPIO()
{
#if   defined (__MSP430FR6989__)
    P1SEL0 |= BIT7 | BIT6;              // FR6989 SDA, SCL pins
    P1OUT &= ~BIT4;                     // TCA9539 Slave reset pin
    P1DIR |= BIT4;
#elif defined (__MSP430FR4133__)
    P5SEL0 |= BIT2 | BIT3;              // FR4133 SDA, SCL pins
    P5OUT &= ~BIT1;                     // TCA9539 Slave reset pin
    P5DIR |= BIT1;
#endif

    PM5CTL0 &= ~LOCKLPM5;               // unlock power-on default high-impedance mode
}

void reset_slave()
{
#if   defined (__MSP430FR6989__)
    P1OUT &= ~BIT4;                     // TCA9539 Slave reset
    __delay_cycles(800000);
    P1OUT |= BIT4;
#elif defined (__MSP430FR4133__)
    P5DIR &= ~BIT1;                     // TCA9539 Slave reset
    __delay_cycles(800000);
    P5DIR |= BIT1;
#endif
}

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;           // hold watchdog

    init_GPIO();
    reset_slave();

    I2C_master_init();                  // initialize as I2C master
    I2C_set_slave_address(SLAVE_ADDR);

    unsigned char value = 1;
    unsigned char config;

    while(1)
    {
        // read slave config and reset if it doesn't match desired config
        config = I2C_register_read_char(SLAVE_CONFIG0);
        if(config != desired_config0)
            I2C_register_write(SLAVE_CONFIG0, desired_config0);

        // shift current slave output register
        // xor first two bits for TCA9539 EVM LEDs
        value = value << 1;
        if(value == 0)          // reset value if overflowed
            value = 1;

        I2C_register_write(SLAVE_OUTPUT0, value ^ 0x03);

        __delay_cycles(BLINK_DELAY);
    }
}

