i2c slave working version 1.0

Dependencies:   mbed-rtos mbed

Fork of freeslave_write2master_working by Seeker of Truth ,

main.cpp

Committer:
sakthipriya
Date:
2014-12-12
Revision:
7:debeb7070a54
Parent:
6:8aea752c6eda

File content as of revision 7:debeb7070a54:

#include "mbed.h"
#include "rtos.h"

void write_to_master(char);                    //function to write data to master
Thread * ptr_t_i2c;
I2CSlave slave(D14,D15);                       //configuring pins p27, p28 as I2Cslave
Serial pc (USBTX,USBRX);
InterruptIn interrupt(D9);
DigitalOut data_ready(D10);
int i2c_status=0;                                 //read/write mode for i2c 0 : write2slave, 1 : write2master
char data='a';

typedef struct
{
    char data;          // To avoid dynamic memory allocation
    int length;
}i2c_data;

Mail<i2c_data,16> i2c_data_receive;
Mail<i2c_data,16> i2c_data_send;
 
 int alpha;
void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
{
       int slave_status = 1;
   
    while(slave_status)
    {                 
            slave.address(0x20);
            if(slave.receive()==1)
            {
                slave_status=slave.write(data,length);
               
                
            }
            else
                if(slave.receive()==3 || slave.receive()==2)
                {
                     
                     slave_status=slave.read(data,length);
                     
                }
           
    }
            printf("done");

}

void T_I2C_BAE(void const * args)
{
    char data_send,data_receive;
    while(1)
    {
        Thread::signal_wait(0x1);
        if(i2c_status == 0)
        {
            
            FUNC_I2C_WRITE2CDMS(&data_receive);
            i2c_data * i2c_data_r = i2c_data_receive.alloc();
            i2c_data_r->data = data_receive;
            i2c_data_r->length = 1;
            i2c_data_receive.put(i2c_data_r);
            printf("\n Data received from CDMS is %c\n",data_receive);
            i2c_data_receive.free(i2c_data_r);
            
        }
        else if(i2c_status ==1 )
        {
            osEvent evt = i2c_data_send.get();
            if (evt.status == osEventMail) 
            {
                i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
                data_send = i2c_data_s -> data;
                FUNC_I2C_WRITE2CDMS(&data_send);
                printf("\nData sent to CDMS is %c\n",data_send);
                i2c_data_send.free(i2c_data_s);
                i2c_status = 0;
               
               
            }
             
            
           
        }  
           
    }
}
        
void FUNC_INT()
{
  
   ptr_t_i2c->signal_set(0x1);
     
}

void ir2master()
{
        data_ready=0;
        data = pc.getc();
        i2c_status=1;
        i2c_data * i2c_data_s = i2c_data_send.alloc();
        i2c_data_s->data = data;
        i2c_data_s->length = 1;
        i2c_data_send.put(i2c_data_s); 
        data_ready=1;
}
      
   


int main()
{   
    
    
    ptr_t_i2c = new Thread(T_I2C_BAE);
    interrupt.rise(&FUNC_INT);
    while(1)
    {
      ir2master(); 
                
    }
     
}