/*
 Author: Obrtac Tomas  
 Mail: obrtac.tomas@gmail.com
 2017
 */

#include "I2Ccommands.h"
#include <xc.h>


void Setting_I2C()
{
    TRISBbits.TRISB4 = 1;
    TRISBbits.TRISB5 = 1;
    SSPCON1bits.SSPM = 0b1000; //Set MCU as master
    SSPCON1bits.SSPEN = 1; //Enable MSSP
    SSPSTATbits.SMP = 1; //Disable slew rate
    SSPADD = (FOSC / (4 * BAUD)) - 1; // Setting desired frequency
}

void Setting_I2C2()
{
    TRISDbits.TRISD0 = 1;
    TRISDbits.TRISD1 = 1;
    SSP2CON1bits.SSPM = 0b1000; //Set MCU as master
    SSP2CON1bits.SSPEN = 1; //Enable MSSP
    SSP2STATbits.SMP = 1; //Disable slew rate
    SSP2ADD = (FOSC / (4 * BAUD)) - 1; // Setting desired frequency
}


void Send_I2C_Data(unsigned char databyte)
{
    PIR1bits.SSP1IF=0; // clear SSP interrupt bit
    SSPBUF = databyte;
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void Send_I2C2_Data(unsigned char databyte)
{
    PIR3bits.SSP2IF=0; // clear SSP interrupt bit
    SSP2BUF = databyte;
    while(!PIR3bits.SSP2IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void Send_I2C_StartBit(void)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
    SSPCON2bits.SEN=1;          // send start bit
    while(!PIR1bits.SSP1IF);    // Wait for the SSPIF bit to go back high before we load the data buffer
}

void Send_I2C2_StartBit(void)
{
    PIR3bits.SSP2IF=0;          // clear SSP interrupt bit
    SSP2CON2bits.SEN=1;          // send start bit
    while(!PIR3bits.SSP2IF);    // Wait for the SSPIF bit to go back high before we load the data buffer
}

void Send_I2C_StopBit(void)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
    SSPCON2bits.PEN=1;          // send stop bit
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void Send_I2C2_StopBit(void)
{
    PIR3bits.SSP2IF=0;          // clear SSP interrupt bit
    SSP2CON2bits.PEN=1;          // send stop bit
    while(!PIR3bits.SSP2IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void WaitMSSP()
{
   while(!PIR1bits.SSPIF); //If no interrupt, wait
   PIR1bits.SSPIF = 0; //Clear interrupt flag, continue
}

void WaitMSSP2()
{
   while(!PIR3bits.SSP2IF); //If no interrupt, wait
   PIR3bits.SSP2IF = 0; //Clear interrupt flag, continue
}

unsigned int Read_I2C_Data(void)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
    SSPCON2bits.RCEN=1;         // set the receive enable bit to initiate a read of 8 bits from the serial eeprom
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete    
    return (SSPBUF);            // Data from eeprom is now in the SSPBUF so return that value
}

unsigned int Read_I2C2_Data(void)
{
    PIR3bits.SSP2IF=0;          // clear SSP interrupt bit
    SSP2CON2bits.RCEN=1;         // set the receive enable bit to initiate a read of 8 bits from the serial eeprom
    while(!PIR3bits.SSP2IF);    // Wait for interrupt flag to go high indicating transmission is complete    
    return (SSP2BUF);            // Data from eeprom is now in the SSPBUF so return that value
}

void Send_I2C_ACK(void)
{
   PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
   SSPCON2bits.ACKDT=0;        // clear the Acknowledge Data Bit - this means we are sending an Acknowledge or 'ACK'
   SSPCON2bits.ACKEN=1;        // set the ACK enable bit to initiate transmission of the ACK bit to the serial eeprom
   while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void Send_I2C2_ACK(void)
{
   PIR3bits.SSP2IF=0;          // clear SSP interrupt bit
   SSP2CON2bits.ACKDT=0;        // clear the Acknowledge Data Bit - this means we are sending an Acknowledge or 'ACK'
   SSP2CON2bits.ACKEN=1;        // set the ACK enable bit to initiate transmission of the ACK bit to the serial eeprom
   while(!PIR3bits.SSP2IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void Send_I2C_NAK(void)
{
    PIR1bits.SSP1IF=0;           // clear SSP interrupt bit
    SSPCON2bits.ACKDT=1;        // set the Acknowledge Data Bit- this means we are sending a No-Ack or 'NAK'
    SSPCON2bits.ACKEN=1;        // set the ACK enable bit to initiate transmission of the ACK bit to the serial eeprom
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void Send_I2C2_NAK(void)
{
    PIR3bits.SSP2IF=0;           // clear SSP interrupt bit
    SSP2CON2bits.ACKDT=1;        // set the Acknowledge Data Bit- this means we are sending a No-Ack or 'NAK'
    SSP2CON2bits.ACKEN=1;        // set the ACK enable bit to initiate transmission of the ACK bit to the serial eeprom
    while(!PIR3bits.SSP2IF);    // Wait for interrupt flag to go high indicating transmission is complete
}

void Port_I2C2_UP(void)
{
    TRISDbits.TRISD0 = 1;
    TRISDbits.TRISD1 = 1;
}