i2c slave working version 1.0
Fork of freeslave_write2master_working by
main.cpp@5:ce0bb4174ca4, 2014-12-10 (annotated)
- Committer:
- greenroshks
- Date:
- Wed Dec 10 16:38:29 2014 +0000
- Revision:
- 5:ce0bb4174ca4
- Parent:
- 4:133b49d51b18
- Child:
- 6:8aea752c6eda
i2c slave completed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:24e80ff2c7b1 | 1 | #include "mbed.h" |
greenroshks | 4:133b49d51b18 | 2 | #include "rtos.h" |
viswachaitanya | 0:24e80ff2c7b1 | 3 | |
viswachaitanya | 0:24e80ff2c7b1 | 4 | void write_to_master(char); //function to write data to master |
greenroshks | 4:133b49d51b18 | 5 | Thread * ptr_t_i2c; |
viswachaitanya | 0:24e80ff2c7b1 | 6 | I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave |
viswachaitanya | 0:24e80ff2c7b1 | 7 | Serial pc (USBTX,USBRX); |
greenroshks | 2:458f9e603814 | 8 | InterruptIn interrupt(D9); |
greenroshks | 5:ce0bb4174ca4 | 9 | DigitalOut data_ready(D10); |
greenroshks | 5:ce0bb4174ca4 | 10 | int i2c_status=0; //read/write mode for i2c 0 : write2slave, 1 : write2master |
greenroshks | 4:133b49d51b18 | 11 | |
greenroshks | 4:133b49d51b18 | 12 | typedef struct |
greenroshks | 2:458f9e603814 | 13 | { |
greenroshks | 4:133b49d51b18 | 14 | char data; // To avoid dynamic memory allocation |
greenroshks | 4:133b49d51b18 | 15 | int length; |
greenroshks | 4:133b49d51b18 | 16 | }i2c_data; |
viswachaitanya | 0:24e80ff2c7b1 | 17 | |
greenroshks | 4:133b49d51b18 | 18 | Mail<i2c_data,16> i2c_data_receive; |
greenroshks | 4:133b49d51b18 | 19 | Mail<i2c_data,16> i2c_data_send; |
greenroshks | 4:133b49d51b18 | 20 | |
greenroshks | 5:ce0bb4174ca4 | 21 | void FUNC_I2C_WRITE2CDMS(char *data, int length=1) |
greenroshks | 4:133b49d51b18 | 22 | { |
greenroshks | 4:133b49d51b18 | 23 | |
greenroshks | 2:458f9e603814 | 24 | int slave_status = 1; |
greenroshks | 2:458f9e603814 | 25 | while(slave_status) |
greenroshks | 2:458f9e603814 | 26 | { |
viswachaitanya | 0:24e80ff2c7b1 | 27 | slave.address(0x20); |
greenroshks | 2:458f9e603814 | 28 | if(slave.receive()==1) |
greenroshks | 2:458f9e603814 | 29 | { |
greenroshks | 5:ce0bb4174ca4 | 30 | slave_status=slave.write(data,length); |
greenroshks | 3:5e68dd4bbb70 | 31 | // printf("\n In the while loop\n"); |
greenroshks | 2:458f9e603814 | 32 | |
greenroshks | 4:133b49d51b18 | 33 | /*if(!slave_status) |
greenroshks | 2:458f9e603814 | 34 | { |
greenroshks | 3:5e68dd4bbb70 | 35 | printf("slave wrote %c to master\n\r",*data); |
greenroshks | 2:458f9e603814 | 36 | |
greenroshks | 3:5e68dd4bbb70 | 37 | }*/ |
greenroshks | 2:458f9e603814 | 38 | |
viswachaitanya | 0:24e80ff2c7b1 | 39 | } |
greenroshks | 3:5e68dd4bbb70 | 40 | else |
greenroshks | 3:5e68dd4bbb70 | 41 | if(slave.receive()==3 || slave.receive()==2) |
greenroshks | 3:5e68dd4bbb70 | 42 | { |
greenroshks | 4:133b49d51b18 | 43 | //i2c_data *i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 44 | slave_status=slave.read(data,length); |
greenroshks | 3:5e68dd4bbb70 | 45 | /*if(!slave_status) |
greenroshks | 3:5e68dd4bbb70 | 46 | { |
greenroshks | 3:5e68dd4bbb70 | 47 | printf("1 read %c from master\n\r",*data); |
greenroshks | 3:5e68dd4bbb70 | 48 | }*/ |
greenroshks | 3:5e68dd4bbb70 | 49 | } |
greenroshks | 2:458f9e603814 | 50 | //slave.stop(); |
greenroshks | 2:458f9e603814 | 51 | } |
viswachaitanya | 0:24e80ff2c7b1 | 52 | printf("2 done"); |
greenroshks | 4:133b49d51b18 | 53 | |
greenroshks | 2:458f9e603814 | 54 | } |
greenroshks | 2:458f9e603814 | 55 | |
greenroshks | 4:133b49d51b18 | 56 | void T_I2C_BAE(void const * args) |
greenroshks | 2:458f9e603814 | 57 | { |
greenroshks | 4:133b49d51b18 | 58 | char data_send,data_receive; |
greenroshks | 5:ce0bb4174ca4 | 59 | //data_send = 'a'; |
greenroshks | 4:133b49d51b18 | 60 | while(1) |
greenroshks | 4:133b49d51b18 | 61 | { |
greenroshks | 4:133b49d51b18 | 62 | Thread::signal_wait(0x1); |
greenroshks | 5:ce0bb4174ca4 | 63 | if(i2c_status == 0) |
greenroshks | 5:ce0bb4174ca4 | 64 | { |
greenroshks | 5:ce0bb4174ca4 | 65 | |
greenroshks | 5:ce0bb4174ca4 | 66 | FUNC_I2C_WRITE2CDMS(&data_receive); |
greenroshks | 5:ce0bb4174ca4 | 67 | //printf("\n Data received from CDMS is %c\n",data_receive); |
greenroshks | 5:ce0bb4174ca4 | 68 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 69 | i2c_data_r->data = data_receive; |
greenroshks | 5:ce0bb4174ca4 | 70 | i2c_data_r->length = 1; |
greenroshks | 5:ce0bb4174ca4 | 71 | i2c_data_receive.put(i2c_data_r); |
greenroshks | 5:ce0bb4174ca4 | 72 | } |
greenroshks | 5:ce0bb4174ca4 | 73 | else if(i2c_status ==1 ) |
greenroshks | 5:ce0bb4174ca4 | 74 | { |
greenroshks | 5:ce0bb4174ca4 | 75 | osEvent evt = i2c_data_send.get(); |
greenroshks | 5:ce0bb4174ca4 | 76 | if (evt.status == osEventMail) |
greenroshks | 5:ce0bb4174ca4 | 77 | { |
greenroshks | 5:ce0bb4174ca4 | 78 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
greenroshks | 5:ce0bb4174ca4 | 79 | //printf("\nData read from CDMS is %c\n",i2c_data_r->data); |
greenroshks | 5:ce0bb4174ca4 | 80 | data_send = i2c_data_s -> data; |
greenroshks | 5:ce0bb4174ca4 | 81 | FUNC_I2C_WRITE2CDMS(&data_send); |
greenroshks | 5:ce0bb4174ca4 | 82 | printf("\nData sent to CDMS is %c\n",data_send); |
greenroshks | 5:ce0bb4174ca4 | 83 | i2c_data_send.free(i2c_data_s); |
greenroshks | 5:ce0bb4174ca4 | 84 | i2c_status = 0; |
greenroshks | 5:ce0bb4174ca4 | 85 | //delete i2c_data_r; |
greenroshks | 5:ce0bb4174ca4 | 86 | } |
greenroshks | 5:ce0bb4174ca4 | 87 | } |
greenroshks | 4:133b49d51b18 | 88 | } |
greenroshks | 2:458f9e603814 | 89 | } |
greenroshks | 4:133b49d51b18 | 90 | |
greenroshks | 4:133b49d51b18 | 91 | void FUNC_INT() |
greenroshks | 4:133b49d51b18 | 92 | { |
greenroshks | 4:133b49d51b18 | 93 | // char * data = new char; |
greenroshks | 4:133b49d51b18 | 94 | //*data = 'a'; |
greenroshks | 4:133b49d51b18 | 95 | ptr_t_i2c->signal_set(0x1); |
greenroshks | 4:133b49d51b18 | 96 | //delete data; |
greenroshks | 4:133b49d51b18 | 97 | } |
greenroshks | 4:133b49d51b18 | 98 | |
greenroshks | 4:133b49d51b18 | 99 | |
greenroshks | 4:133b49d51b18 | 100 | |
greenroshks | 2:458f9e603814 | 101 | void main() |
greenroshks | 2:458f9e603814 | 102 | { |
greenroshks | 2:458f9e603814 | 103 | printf("\nStarted slave... waiting for master\n"); |
greenroshks | 4:133b49d51b18 | 104 | ptr_t_i2c = new Thread(T_I2C_BAE); |
greenroshks | 4:133b49d51b18 | 105 | char data='a'; |
greenroshks | 4:133b49d51b18 | 106 | interrupt.rise(&FUNC_INT); |
greenroshks | 2:458f9e603814 | 107 | while(1) |
greenroshks | 2:458f9e603814 | 108 | { |
greenroshks | 2:458f9e603814 | 109 | //*(data_send)=pc.getc(); |
greenroshks | 2:458f9e603814 | 110 | |
greenroshks | 4:133b49d51b18 | 111 | //FUNC_I2C_WRITE2CDMS(&data,1); |
greenroshks | 4:133b49d51b18 | 112 | //printf("\n The data send or received by slave is %c\n",data); |
greenroshks | 4:133b49d51b18 | 113 | //(data)++; |
greenroshks | 2:458f9e603814 | 114 | //wait(1); |
greenroshks | 5:ce0bb4174ca4 | 115 | //Remove the comments for receiving data |
greenroshks | 5:ce0bb4174ca4 | 116 | /* osEvent evt = i2c_data_receive.get(); |
greenroshks | 4:133b49d51b18 | 117 | if (evt.status == osEventMail) |
greenroshks | 4:133b49d51b18 | 118 | { |
greenroshks | 4:133b49d51b18 | 119 | i2c_data *i2c_data_r = (i2c_data*)evt.value.p; |
greenroshks | 4:133b49d51b18 | 120 | printf("\nData read from CDMS is %c\n",i2c_data_r->data); |
greenroshks | 4:133b49d51b18 | 121 | |
greenroshks | 4:133b49d51b18 | 122 | i2c_data_receive.free(i2c_data_r); |
greenroshks | 4:133b49d51b18 | 123 | //delete i2c_data_r; |
greenroshks | 5:ce0bb4174ca4 | 124 | }*/ |
greenroshks | 5:ce0bb4174ca4 | 125 | //put in comments for receiving data |
greenroshks | 5:ce0bb4174ca4 | 126 | data_ready=0; |
greenroshks | 5:ce0bb4174ca4 | 127 | data = pc.getc(); |
greenroshks | 5:ce0bb4174ca4 | 128 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 129 | i2c_data_s->data = data; |
greenroshks | 5:ce0bb4174ca4 | 130 | i2c_data_s->length = 1; |
greenroshks | 5:ce0bb4174ca4 | 131 | i2c_data_send.put(i2c_data_s); |
greenroshks | 5:ce0bb4174ca4 | 132 | data_ready=1; |
greenroshks | 5:ce0bb4174ca4 | 133 | i2c_status=1; |
greenroshks | 5:ce0bb4174ca4 | 134 | |
greenroshks | 2:458f9e603814 | 135 | } |
greenroshks | 2:458f9e603814 | 136 | } |