i2c working

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDMTESIN2 by Seeker of Truth ,

Committer:
greenroshks
Date:
Tue Dec 16 07:44:54 2014 +0000
Revision:
10:ed6d3b8d1d56
Parent:
9:221d218f4690
working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
raizel_varun 9:221d218f4690 1 #include "mbed.h"
raizel_varun 9:221d218f4690 2 #include "rtos.h"
greenroshks 0:8b0d43fe6c05 3 #include "slave.h"
raizel_varun 9:221d218f4690 4
raizel_varun 9:221d218f4690 5 void write_to_master(char); //function to write data to master
raizel_varun 9:221d218f4690 6
raizel_varun 9:221d218f4690 7 I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave
raizel_varun 9:221d218f4690 8
raizel_varun 9:221d218f4690 9 Serial pcslave(USBTX, USBRX);
raizel_varun 9:221d218f4690 10 DigitalOut data_ready(D10);
raizel_varun 9:221d218f4690 11 int i2c_status=0; //read/write mode for i2c 0 : write2slave, 1 : write2master
raizel_varun 9:221d218f4690 12 int reset=0;
raizel_varun 9:221d218f4690 13 int temp;
raizel_varun 9:221d218f4690 14
raizel_varun 9:221d218f4690 15 typedef struct
raizel_varun 9:221d218f4690 16 {
greenroshks 10:ed6d3b8d1d56 17 char data[13]; // To avoid dynamic memory allocation
raizel_varun 9:221d218f4690 18 int length;
raizel_varun 9:221d218f4690 19 }i2c_data;
raizel_varun 9:221d218f4690 20
raizel_varun 9:221d218f4690 21
raizel_varun 9:221d218f4690 22
greenroshks 10:ed6d3b8d1d56 23
greenroshks 10:ed6d3b8d1d56 24
greenroshks 10:ed6d3b8d1d56 25
raizel_varun 9:221d218f4690 26 Mail<i2c_data,16> i2c_data_receive;
raizel_varun 9:221d218f4690 27 Mail<i2c_data,16> i2c_data_send;
greenroshks 0:8b0d43fe6c05 28
raizel_varun 9:221d218f4690 29
raizel_varun 9:221d218f4690 30 void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
raizel_varun 9:221d218f4690 31 {
raizel_varun 9:221d218f4690 32 int slave_status = 1;
raizel_varun 9:221d218f4690 33
raizel_varun 9:221d218f4690 34 while(slave_status)
raizel_varun 9:221d218f4690 35 {
raizel_varun 9:221d218f4690 36 slave.address(0x20);
raizel_varun 9:221d218f4690 37 if(slave.receive()==1)
greenroshks 0:8b0d43fe6c05 38 {
raizel_varun 9:221d218f4690 39 slave_status=slave.write(data,length);
raizel_varun 9:221d218f4690 40
raizel_varun 9:221d218f4690 41
raizel_varun 9:221d218f4690 42 }
raizel_varun 9:221d218f4690 43 else if(slave.receive()==3 || slave.receive()==2)
raizel_varun 9:221d218f4690 44 {
raizel_varun 9:221d218f4690 45 slave_status=slave.read(data,length);
greenroshks 0:8b0d43fe6c05 46 }
raizel_varun 9:221d218f4690 47
raizel_varun 9:221d218f4690 48 }
raizel_varun 9:221d218f4690 49 printf("\ndone\n\r");
raizel_varun 9:221d218f4690 50
raizel_varun 9:221d218f4690 51 }
greenroshks 10:ed6d3b8d1d56 52 char data_send[13],data_receive[13];
raizel_varun 9:221d218f4690 53 void T_I2C_BAE()
greenroshks 0:8b0d43fe6c05 54 {
raizel_varun 9:221d218f4690 55 //char data_send,data_receive;
raizel_varun 9:221d218f4690 56 //while(1)
raizel_varun 9:221d218f4690 57 //{
raizel_varun 9:221d218f4690 58 // Thread::signal_wait(0x1);
raizel_varun 9:221d218f4690 59 //i2c_status = temp;
raizel_varun 9:221d218f4690 60 //wait(0.5);
greenroshks 10:ed6d3b8d1d56 61 printf("\n entered thread %d\n\r",i2c_status);
raizel_varun 9:221d218f4690 62 if(i2c_status == 0 && reset !=1)
raizel_varun 9:221d218f4690 63 {
raizel_varun 9:221d218f4690 64
greenroshks 10:ed6d3b8d1d56 65 FUNC_I2C_WRITE2CDMS(data_receive);
raizel_varun 9:221d218f4690 66 i2c_data * i2c_data_r = i2c_data_receive.alloc();
greenroshks 10:ed6d3b8d1d56 67 strcpy(i2c_data_r->data,data_receive);
greenroshks 10:ed6d3b8d1d56 68 i2c_data_r->length = 13;
raizel_varun 9:221d218f4690 69 i2c_data_receive.put(i2c_data_r);
greenroshks 10:ed6d3b8d1d56 70 printf("\n Data received from CDMS is %s\n\r",data_receive);
raizel_varun 9:221d218f4690 71 i2c_data_receive.free(i2c_data_r); // This has to be done from a differen thread
raizel_varun 9:221d218f4690 72
raizel_varun 9:221d218f4690 73 }
raizel_varun 9:221d218f4690 74 else if(i2c_status ==1 && reset !=1)
raizel_varun 9:221d218f4690 75 {
raizel_varun 9:221d218f4690 76 osEvent evt = i2c_data_send.get();
raizel_varun 9:221d218f4690 77 if (evt.status == osEventMail)
raizel_varun 9:221d218f4690 78 {
raizel_varun 9:221d218f4690 79 i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
greenroshks 10:ed6d3b8d1d56 80 strcpy(data_send,i2c_data_s -> data);
greenroshks 10:ed6d3b8d1d56 81 FUNC_I2C_WRITE2CDMS(data_send,13);
greenroshks 10:ed6d3b8d1d56 82 printf("\nData sent to CDMS is %s\n\r",data_send);
raizel_varun 9:221d218f4690 83 i2c_data_send.free(i2c_data_s);
raizel_varun 9:221d218f4690 84 i2c_status = 0;
raizel_varun 9:221d218f4690 85 //temp = i2c_status;
greenroshks 10:ed6d3b8d1d56 86
raizel_varun 9:221d218f4690 87 }
raizel_varun 9:221d218f4690 88 }
raizel_varun 9:221d218f4690 89
raizel_varun 9:221d218f4690 90 //}
raizel_varun 9:221d218f4690 91 }
greenroshks 0:8b0d43fe6c05 92
raizel_varun 9:221d218f4690 93
raizel_varun 9:221d218f4690 94
raizel_varun 9:221d218f4690 95 void FUNC_INT()
raizel_varun 9:221d218f4690 96 {
raizel_varun 9:221d218f4690 97 reset = 0;
raizel_varun 9:221d218f4690 98
raizel_varun 9:221d218f4690 99
raizel_varun 9:221d218f4690 100 }
raizel_varun 9:221d218f4690 101
raizel_varun 9:221d218f4690 102 void FUNC_RESET()
raizel_varun 9:221d218f4690 103 {
raizel_varun 9:221d218f4690 104 reset = 1;
greenroshks 0:8b0d43fe6c05 105 }
raizel_varun 9:221d218f4690 106
raizel_varun 9:221d218f4690 107 void ir2master()
raizel_varun 9:221d218f4690 108 {
raizel_varun 9:221d218f4690 109
greenroshks 10:ed6d3b8d1d56 110 char data[13];
greenroshks 10:ed6d3b8d1d56 111 strcpy(data,"sakthi ");
greenroshks 10:ed6d3b8d1d56 112 strcat(data,"priya");
raizel_varun 9:221d218f4690 113 data_ready=0;
greenroshks 10:ed6d3b8d1d56 114 //data = pcslave.getc();
raizel_varun 9:221d218f4690 115 reset =0;
raizel_varun 9:221d218f4690 116 i2c_status=1;
raizel_varun 9:221d218f4690 117 i2c_data * i2c_data_s = i2c_data_send.alloc();
greenroshks 10:ed6d3b8d1d56 118 strcpy(i2c_data_s->data,data);
greenroshks 10:ed6d3b8d1d56 119 i2c_data_s->length = 13;
raizel_varun 9:221d218f4690 120 i2c_data_send.put(i2c_data_s);
raizel_varun 9:221d218f4690 121 data_ready=1;
raizel_varun 9:221d218f4690 122 //temp = i2c_status;
greenroshks 0:8b0d43fe6c05 123 }
raizel_varun 9:221d218f4690 124
raizel_varun 9:221d218f4690 125
raizel_varun 9:221d218f4690 126
raizel_varun 9:221d218f4690 127
raizel_varun 9:221d218f4690 128
raizel_varun 9:221d218f4690 129