i2c slave working version 1.0
Fork of freeslave_write2master_working by
main.cpp@7:debeb7070a54, 2014-12-12 (annotated)
- Committer:
- sakthipriya
- Date:
- Fri Dec 12 12:16:48 2014 +0000
- Revision:
- 7:debeb7070a54
- Parent:
- 6:8aea752c6eda
i2c slave
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 |
sakthipriya | 7:debeb7070a54 | 11 | char data='a'; |
greenroshks | 4:133b49d51b18 | 12 | |
greenroshks | 4:133b49d51b18 | 13 | typedef struct |
greenroshks | 2:458f9e603814 | 14 | { |
greenroshks | 4:133b49d51b18 | 15 | char data; // To avoid dynamic memory allocation |
greenroshks | 4:133b49d51b18 | 16 | int length; |
greenroshks | 4:133b49d51b18 | 17 | }i2c_data; |
viswachaitanya | 0:24e80ff2c7b1 | 18 | |
greenroshks | 4:133b49d51b18 | 19 | Mail<i2c_data,16> i2c_data_receive; |
greenroshks | 4:133b49d51b18 | 20 | Mail<i2c_data,16> i2c_data_send; |
raizel_varun | 6:8aea752c6eda | 21 | |
raizel_varun | 6:8aea752c6eda | 22 | int alpha; |
greenroshks | 5:ce0bb4174ca4 | 23 | void FUNC_I2C_WRITE2CDMS(char *data, int length=1) |
greenroshks | 4:133b49d51b18 | 24 | { |
raizel_varun | 6:8aea752c6eda | 25 | int slave_status = 1; |
raizel_varun | 6:8aea752c6eda | 26 | |
greenroshks | 2:458f9e603814 | 27 | while(slave_status) |
greenroshks | 2:458f9e603814 | 28 | { |
viswachaitanya | 0:24e80ff2c7b1 | 29 | slave.address(0x20); |
greenroshks | 2:458f9e603814 | 30 | if(slave.receive()==1) |
greenroshks | 2:458f9e603814 | 31 | { |
greenroshks | 5:ce0bb4174ca4 | 32 | slave_status=slave.write(data,length); |
sakthipriya | 7:debeb7070a54 | 33 | |
greenroshks | 2:458f9e603814 | 34 | |
viswachaitanya | 0:24e80ff2c7b1 | 35 | } |
greenroshks | 3:5e68dd4bbb70 | 36 | else |
greenroshks | 3:5e68dd4bbb70 | 37 | if(slave.receive()==3 || slave.receive()==2) |
greenroshks | 3:5e68dd4bbb70 | 38 | { |
sakthipriya | 7:debeb7070a54 | 39 | |
greenroshks | 5:ce0bb4174ca4 | 40 | slave_status=slave.read(data,length); |
sakthipriya | 7:debeb7070a54 | 41 | |
greenroshks | 3:5e68dd4bbb70 | 42 | } |
sakthipriya | 7:debeb7070a54 | 43 | |
greenroshks | 2:458f9e603814 | 44 | } |
sakthipriya | 7:debeb7070a54 | 45 | printf("done"); |
greenroshks | 4:133b49d51b18 | 46 | |
greenroshks | 2:458f9e603814 | 47 | } |
greenroshks | 2:458f9e603814 | 48 | |
greenroshks | 4:133b49d51b18 | 49 | void T_I2C_BAE(void const * args) |
greenroshks | 2:458f9e603814 | 50 | { |
greenroshks | 4:133b49d51b18 | 51 | char data_send,data_receive; |
greenroshks | 4:133b49d51b18 | 52 | while(1) |
greenroshks | 4:133b49d51b18 | 53 | { |
greenroshks | 4:133b49d51b18 | 54 | Thread::signal_wait(0x1); |
greenroshks | 5:ce0bb4174ca4 | 55 | if(i2c_status == 0) |
greenroshks | 5:ce0bb4174ca4 | 56 | { |
greenroshks | 5:ce0bb4174ca4 | 57 | |
greenroshks | 5:ce0bb4174ca4 | 58 | FUNC_I2C_WRITE2CDMS(&data_receive); |
greenroshks | 5:ce0bb4174ca4 | 59 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 60 | i2c_data_r->data = data_receive; |
greenroshks | 5:ce0bb4174ca4 | 61 | i2c_data_r->length = 1; |
greenroshks | 5:ce0bb4174ca4 | 62 | i2c_data_receive.put(i2c_data_r); |
raizel_varun | 6:8aea752c6eda | 63 | printf("\n Data received from CDMS is %c\n",data_receive); |
sakthipriya | 7:debeb7070a54 | 64 | i2c_data_receive.free(i2c_data_r); |
raizel_varun | 6:8aea752c6eda | 65 | |
greenroshks | 5:ce0bb4174ca4 | 66 | } |
greenroshks | 5:ce0bb4174ca4 | 67 | else if(i2c_status ==1 ) |
greenroshks | 5:ce0bb4174ca4 | 68 | { |
greenroshks | 5:ce0bb4174ca4 | 69 | osEvent evt = i2c_data_send.get(); |
greenroshks | 5:ce0bb4174ca4 | 70 | if (evt.status == osEventMail) |
greenroshks | 5:ce0bb4174ca4 | 71 | { |
greenroshks | 5:ce0bb4174ca4 | 72 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
greenroshks | 5:ce0bb4174ca4 | 73 | data_send = i2c_data_s -> data; |
greenroshks | 5:ce0bb4174ca4 | 74 | FUNC_I2C_WRITE2CDMS(&data_send); |
greenroshks | 5:ce0bb4174ca4 | 75 | printf("\nData sent to CDMS is %c\n",data_send); |
greenroshks | 5:ce0bb4174ca4 | 76 | i2c_data_send.free(i2c_data_s); |
greenroshks | 5:ce0bb4174ca4 | 77 | i2c_status = 0; |
raizel_varun | 6:8aea752c6eda | 78 | |
sakthipriya | 7:debeb7070a54 | 79 | |
greenroshks | 5:ce0bb4174ca4 | 80 | } |
raizel_varun | 6:8aea752c6eda | 81 | |
raizel_varun | 6:8aea752c6eda | 82 | |
raizel_varun | 6:8aea752c6eda | 83 | |
raizel_varun | 6:8aea752c6eda | 84 | } |
raizel_varun | 6:8aea752c6eda | 85 | |
greenroshks | 4:133b49d51b18 | 86 | } |
greenroshks | 2:458f9e603814 | 87 | } |
greenroshks | 4:133b49d51b18 | 88 | |
greenroshks | 4:133b49d51b18 | 89 | void FUNC_INT() |
greenroshks | 4:133b49d51b18 | 90 | { |
sakthipriya | 7:debeb7070a54 | 91 | |
greenroshks | 4:133b49d51b18 | 92 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 7:debeb7070a54 | 93 | |
greenroshks | 4:133b49d51b18 | 94 | } |
sakthipriya | 7:debeb7070a54 | 95 | |
sakthipriya | 7:debeb7070a54 | 96 | void ir2master() |
sakthipriya | 7:debeb7070a54 | 97 | { |
sakthipriya | 7:debeb7070a54 | 98 | data_ready=0; |
sakthipriya | 7:debeb7070a54 | 99 | data = pc.getc(); |
sakthipriya | 7:debeb7070a54 | 100 | i2c_status=1; |
sakthipriya | 7:debeb7070a54 | 101 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 7:debeb7070a54 | 102 | i2c_data_s->data = data; |
sakthipriya | 7:debeb7070a54 | 103 | i2c_data_s->length = 1; |
sakthipriya | 7:debeb7070a54 | 104 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 7:debeb7070a54 | 105 | data_ready=1; |
sakthipriya | 7:debeb7070a54 | 106 | } |
sakthipriya | 7:debeb7070a54 | 107 | |
greenroshks | 4:133b49d51b18 | 108 | |
greenroshks | 4:133b49d51b18 | 109 | |
greenroshks | 4:133b49d51b18 | 110 | |
raizel_varun | 6:8aea752c6eda | 111 | int main() |
raizel_varun | 6:8aea752c6eda | 112 | { |
raizel_varun | 6:8aea752c6eda | 113 | |
raizel_varun | 6:8aea752c6eda | 114 | |
greenroshks | 4:133b49d51b18 | 115 | ptr_t_i2c = new Thread(T_I2C_BAE); |
greenroshks | 4:133b49d51b18 | 116 | interrupt.rise(&FUNC_INT); |
greenroshks | 2:458f9e603814 | 117 | while(1) |
greenroshks | 2:458f9e603814 | 118 | { |
sakthipriya | 7:debeb7070a54 | 119 | ir2master(); |
greenroshks | 5:ce0bb4174ca4 | 120 | |
greenroshks | 2:458f9e603814 | 121 | } |
raizel_varun | 6:8aea752c6eda | 122 | |
raizel_varun | 6:8aea752c6eda | 123 | } |
raizel_varun | 6:8aea752c6eda | 124 | |
raizel_varun | 6:8aea752c6eda | 125 | |
raizel_varun | 6:8aea752c6eda | 126 | |
raizel_varun | 6:8aea752c6eda | 127 |