/*
 * File:        k10_i2c_demo.c
 * Purpose:     Demo Code for TWR-K10X256
 *
 */

#include "common.h"
#include "hal_i2c.h"
#include "hal_dev_mma8451.h"

//Defines for GPIO
#define GPIO_PIN_MASK            0x1Fu
#define GPIO_PIN(x)              (((1)<<(x & GPIO_PIN_MASK)))

void init_gpio(void);
void deinit_i2c_demo(void);
void delay(void);

void i2c_demo(int a,char** b)
{
    unsigned char tmp;
    signed short resultx, resulty, resultz;
    
    printf("****Kinetis K10 I2C Demo!****\n");
    printf("****Press any key to exit...****\n");
    
    //Initialize the switch input and LED output on TWR-K40X256
    init_gpio();
    
    //Set PTC10 to 0 (turns on blue LED)
    GPIOC_PDOR&=~GPIO_PDOR_PDO(GPIO_PIN(10));
    
    //Initialize I2C modules
    hal_i2c_init();
    
    //Configure MMA8451 sensor
    tmp = hal_dev_mma8451_read_reg(0x2a);
    hal_dev_mma8451_write_reg(0x2a,tmp|0x01);
    
    while(1)
    {
        if((hal_dev_mma8451_read_reg(0x00)&0xf) != 0)
        {
            resultx   = hal_dev_mma8451_read_reg(0x01)<<8;
            resultx  |= hal_dev_mma8451_read_reg(0x02);
            resultx >>= 2;

            resulty   = hal_dev_mma8451_read_reg(0x03)<<8;
            resulty  |= hal_dev_mma8451_read_reg(0x04);
            resulty >>= 2;

            resultz   = hal_dev_mma8451_read_reg(0x05)<<8;
            resultz  |= hal_dev_mma8451_read_reg(0x06);
            resultz >>= 2;
        }
        else
        {
            continue;
        }
      
        //Read x-axis register
        printf("%5d", resultx);
        printf("   %5d", resulty);
        printf("   %5d\r", resultz);
        
        //If tilted, turn on orange LED and turn on all LCD segments
        if(resultx>4000 && resultx<8000)
        {
          GPIOC_PDOR&=~GPIO_PDOR_PDO(GPIO_PIN(7)); //Set PTC7 to 0 (turns on LED)
        }
        //If not tilted, turn off orange LED and turn off LCD segments except FSL segment
        else
        {
          GPIOC_PDOR|=GPIO_PDOR_PDO(GPIO_PIN(7)); //Set PTC7 to 1 (turns off LED)
        }
        
        //Look at status of SW6 or SW7
        if((GPIOC_PDIR&GPIO_PDIR_PDI(GPIO_PIN(5)))==0 || (GPIOC_PDIR&GPIO_PDIR_PDI(GPIO_PIN(13)))==0)  //if either pressed...
        {
          GPIOC_PDOR&=~GPIO_PDOR_PDO(GPIO_PIN(8)); //Set PTC8 to 0 (turns on yellow LED)
        }
        else
        {
          GPIOC_PDOR|=GPIO_PDOR_PDO(GPIO_PIN(8)); //Set PTC8 to 1 (turns off yellow LED)
        }
        
        //Updated SLCD display with RTC value
        //update_lcd();
        
        //Toggle the green LED on PTC9
        GPIOC_PTOR|=GPIO_PDOR_PDO(GPIO_PIN(9));
        if(char_present())
        {// if pressing any key, exit this demo
            break;
        }
        delay();
    }
    deinit_i2c_demo();
    in_char();
    printf("\r\n");
}

/*
 * Initialize GPIO for Tower switches and LED's
 *
 *   PTC5 - SW6
 *   PTC13 - SW7
 *
 *   PTC10 - Blue LED (D17)
 *   PTC9 - Green LED (D14)
 *   PTC8 - Yellow LED (D15)
 *   PTC7 - Orange LED (D16)
 */
void init_gpio(void)
{
    /* Turn on all port clocks */
    SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
    
    //Set PTC10, PTC7, PTC8, and PTC9 to GPIO
    PORTC_PCR10=(0|PORT_PCR_MUX(1));
    PORTC_PCR7=(0|PORT_PCR_MUX(1));
    PORTC_PCR8=(0|PORT_PCR_MUX(1));
    PORTC_PCR9=(0|PORT_PCR_MUX(1));
    
    //Set PTC10, PTC7, PTC8, PTC9 as an output
    GPIOC_PDDR=GPIO_PDDR_PDD(GPIO_PIN(7) | GPIO_PIN(8) | GPIO_PIN(9) | GPIO_PIN(10));	
}

/*
 * Function to provide a short delay
 */
void delay(void)
{
  unsigned int i, n;
  for(i=0;i<30000;i++)
  {
    for(n=0;n<200;n++)
    {
      asm("nop");
    }
  }
}

void deinit_i2c_demo(void)
{
    hal_i2c_deinit();
    GPIOC_PDOR &= ~0x780; 
    GPIOC_PDDR &= ~0x780;  // disable PTC7,8,9    
}
