while loop removed ( master cant send)
Fork of slave_working_obama by
main.cpp@6:8aea752c6eda, 2014-12-12 (annotated)
- Committer:
- raizel_varun
- Date:
- Fri Dec 12 08:51:42 2014 +0000
- Revision:
- 6:8aea752c6eda
- Parent:
- 5:ce0bb4174ca4
- Child:
- 7:debeb7070a54
alphaless
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; |
raizel_varun | 6:8aea752c6eda | 20 | |
raizel_varun | 6:8aea752c6eda | 21 | int alpha; |
greenroshks | 5:ce0bb4174ca4 | 22 | void FUNC_I2C_WRITE2CDMS(char *data, int length=1) |
greenroshks | 4:133b49d51b18 | 23 | { |
raizel_varun | 6:8aea752c6eda | 24 | int slave_status = 1; |
raizel_varun | 6:8aea752c6eda | 25 | |
greenroshks | 2:458f9e603814 | 26 | while(slave_status) |
greenroshks | 2:458f9e603814 | 27 | { |
viswachaitanya | 0:24e80ff2c7b1 | 28 | slave.address(0x20); |
greenroshks | 2:458f9e603814 | 29 | if(slave.receive()==1) |
greenroshks | 2:458f9e603814 | 30 | { |
greenroshks | 5:ce0bb4174ca4 | 31 | slave_status=slave.write(data,length); |
greenroshks | 3:5e68dd4bbb70 | 32 | // printf("\n In the while loop\n"); |
greenroshks | 2:458f9e603814 | 33 | |
greenroshks | 4:133b49d51b18 | 34 | /*if(!slave_status) |
greenroshks | 2:458f9e603814 | 35 | { |
greenroshks | 3:5e68dd4bbb70 | 36 | printf("slave wrote %c to master\n\r",*data); |
greenroshks | 2:458f9e603814 | 37 | |
greenroshks | 3:5e68dd4bbb70 | 38 | }*/ |
greenroshks | 2:458f9e603814 | 39 | |
viswachaitanya | 0:24e80ff2c7b1 | 40 | } |
greenroshks | 3:5e68dd4bbb70 | 41 | else |
greenroshks | 3:5e68dd4bbb70 | 42 | if(slave.receive()==3 || slave.receive()==2) |
greenroshks | 3:5e68dd4bbb70 | 43 | { |
greenroshks | 4:133b49d51b18 | 44 | //i2c_data *i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 45 | slave_status=slave.read(data,length); |
greenroshks | 3:5e68dd4bbb70 | 46 | /*if(!slave_status) |
greenroshks | 3:5e68dd4bbb70 | 47 | { |
greenroshks | 3:5e68dd4bbb70 | 48 | printf("1 read %c from master\n\r",*data); |
greenroshks | 3:5e68dd4bbb70 | 49 | }*/ |
greenroshks | 3:5e68dd4bbb70 | 50 | } |
greenroshks | 2:458f9e603814 | 51 | //slave.stop(); |
greenroshks | 2:458f9e603814 | 52 | } |
viswachaitanya | 0:24e80ff2c7b1 | 53 | printf("2 done"); |
greenroshks | 4:133b49d51b18 | 54 | |
greenroshks | 2:458f9e603814 | 55 | } |
greenroshks | 2:458f9e603814 | 56 | |
greenroshks | 4:133b49d51b18 | 57 | void T_I2C_BAE(void const * args) |
greenroshks | 2:458f9e603814 | 58 | { |
greenroshks | 4:133b49d51b18 | 59 | char data_send,data_receive; |
greenroshks | 5:ce0bb4174ca4 | 60 | //data_send = 'a'; |
greenroshks | 4:133b49d51b18 | 61 | while(1) |
greenroshks | 4:133b49d51b18 | 62 | { |
greenroshks | 4:133b49d51b18 | 63 | Thread::signal_wait(0x1); |
raizel_varun | 6:8aea752c6eda | 64 | printf("\nbdh\n"); |
raizel_varun | 6:8aea752c6eda | 65 | //i2c_status=alpha; |
raizel_varun | 6:8aea752c6eda | 66 | printf(" i2c status is %d " , i2c_status); |
greenroshks | 5:ce0bb4174ca4 | 67 | if(i2c_status == 0) |
greenroshks | 5:ce0bb4174ca4 | 68 | { |
greenroshks | 5:ce0bb4174ca4 | 69 | |
greenroshks | 5:ce0bb4174ca4 | 70 | FUNC_I2C_WRITE2CDMS(&data_receive); |
raizel_varun | 6:8aea752c6eda | 71 | printf("\n i2c=0\n\r"); |
raizel_varun | 6:8aea752c6eda | 72 | |
greenroshks | 5:ce0bb4174ca4 | 73 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 74 | i2c_data_r->data = data_receive; |
greenroshks | 5:ce0bb4174ca4 | 75 | i2c_data_r->length = 1; |
greenroshks | 5:ce0bb4174ca4 | 76 | i2c_data_receive.put(i2c_data_r); |
raizel_varun | 6:8aea752c6eda | 77 | printf("\n Data received from CDMS is %c\n",data_receive); |
raizel_varun | 6:8aea752c6eda | 78 | |
greenroshks | 5:ce0bb4174ca4 | 79 | } |
greenroshks | 5:ce0bb4174ca4 | 80 | else if(i2c_status ==1 ) |
greenroshks | 5:ce0bb4174ca4 | 81 | { |
greenroshks | 5:ce0bb4174ca4 | 82 | osEvent evt = i2c_data_send.get(); |
greenroshks | 5:ce0bb4174ca4 | 83 | if (evt.status == osEventMail) |
greenroshks | 5:ce0bb4174ca4 | 84 | { |
greenroshks | 5:ce0bb4174ca4 | 85 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
greenroshks | 5:ce0bb4174ca4 | 86 | //printf("\nData read from CDMS is %c\n",i2c_data_r->data); |
greenroshks | 5:ce0bb4174ca4 | 87 | data_send = i2c_data_s -> data; |
greenroshks | 5:ce0bb4174ca4 | 88 | FUNC_I2C_WRITE2CDMS(&data_send); |
greenroshks | 5:ce0bb4174ca4 | 89 | printf("\nData sent to CDMS is %c\n",data_send); |
greenroshks | 5:ce0bb4174ca4 | 90 | i2c_data_send.free(i2c_data_s); |
greenroshks | 5:ce0bb4174ca4 | 91 | i2c_status = 0; |
raizel_varun | 6:8aea752c6eda | 92 | |
greenroshks | 5:ce0bb4174ca4 | 93 | //delete i2c_data_r; |
greenroshks | 5:ce0bb4174ca4 | 94 | } |
raizel_varun | 6:8aea752c6eda | 95 | |
raizel_varun | 6:8aea752c6eda | 96 | |
raizel_varun | 6:8aea752c6eda | 97 | |
raizel_varun | 6:8aea752c6eda | 98 | } |
raizel_varun | 6:8aea752c6eda | 99 | |
greenroshks | 4:133b49d51b18 | 100 | } |
greenroshks | 2:458f9e603814 | 101 | } |
greenroshks | 4:133b49d51b18 | 102 | |
greenroshks | 4:133b49d51b18 | 103 | void FUNC_INT() |
greenroshks | 4:133b49d51b18 | 104 | { |
greenroshks | 4:133b49d51b18 | 105 | // char * data = new char; |
greenroshks | 4:133b49d51b18 | 106 | //*data = 'a'; |
raizel_varun | 6:8aea752c6eda | 107 | //printf("\blah\n"); |
greenroshks | 4:133b49d51b18 | 108 | ptr_t_i2c->signal_set(0x1); |
greenroshks | 4:133b49d51b18 | 109 | //delete data; |
raizel_varun | 6:8aea752c6eda | 110 | |
greenroshks | 4:133b49d51b18 | 111 | } |
greenroshks | 4:133b49d51b18 | 112 | |
greenroshks | 4:133b49d51b18 | 113 | |
greenroshks | 4:133b49d51b18 | 114 | |
raizel_varun | 6:8aea752c6eda | 115 | int main() |
raizel_varun | 6:8aea752c6eda | 116 | { |
raizel_varun | 6:8aea752c6eda | 117 | |
raizel_varun | 6:8aea752c6eda | 118 | |
greenroshks | 4:133b49d51b18 | 119 | ptr_t_i2c = new Thread(T_I2C_BAE); |
greenroshks | 4:133b49d51b18 | 120 | char data='a'; |
greenroshks | 4:133b49d51b18 | 121 | interrupt.rise(&FUNC_INT); |
greenroshks | 2:458f9e603814 | 122 | while(1) |
greenroshks | 2:458f9e603814 | 123 | { |
greenroshks | 2:458f9e603814 | 124 | //*(data_send)=pc.getc(); |
greenroshks | 2:458f9e603814 | 125 | |
greenroshks | 4:133b49d51b18 | 126 | //FUNC_I2C_WRITE2CDMS(&data,1); |
greenroshks | 4:133b49d51b18 | 127 | //printf("\n The data send or received by slave is %c\n",data); |
greenroshks | 4:133b49d51b18 | 128 | //(data)++; |
greenroshks | 2:458f9e603814 | 129 | //wait(1); |
greenroshks | 5:ce0bb4174ca4 | 130 | //Remove the comments for receiving data |
greenroshks | 5:ce0bb4174ca4 | 131 | /* osEvent evt = i2c_data_receive.get(); |
greenroshks | 4:133b49d51b18 | 132 | if (evt.status == osEventMail) |
greenroshks | 4:133b49d51b18 | 133 | { |
greenroshks | 4:133b49d51b18 | 134 | i2c_data *i2c_data_r = (i2c_data*)evt.value.p; |
greenroshks | 4:133b49d51b18 | 135 | printf("\nData read from CDMS is %c\n",i2c_data_r->data); |
greenroshks | 4:133b49d51b18 | 136 | |
greenroshks | 4:133b49d51b18 | 137 | i2c_data_receive.free(i2c_data_r); |
greenroshks | 4:133b49d51b18 | 138 | //delete i2c_data_r; |
greenroshks | 5:ce0bb4174ca4 | 139 | }*/ |
greenroshks | 5:ce0bb4174ca4 | 140 | //put in comments for receiving data |
greenroshks | 5:ce0bb4174ca4 | 141 | data_ready=0; |
greenroshks | 5:ce0bb4174ca4 | 142 | data = pc.getc(); |
raizel_varun | 6:8aea752c6eda | 143 | printf(" %c ", data); |
raizel_varun | 6:8aea752c6eda | 144 | i2c_status=1; |
raizel_varun | 6:8aea752c6eda | 145 | printf(" %d ", i2c_status ); |
greenroshks | 5:ce0bb4174ca4 | 146 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 147 | i2c_data_s->data = data; |
greenroshks | 5:ce0bb4174ca4 | 148 | i2c_data_s->length = 1; |
greenroshks | 5:ce0bb4174ca4 | 149 | i2c_data_send.put(i2c_data_s); |
raizel_varun | 6:8aea752c6eda | 150 | // wait(0.5); |
raizel_varun | 6:8aea752c6eda | 151 | alpha=i2c_status; |
greenroshks | 5:ce0bb4174ca4 | 152 | data_ready=1; |
raizel_varun | 6:8aea752c6eda | 153 | |
greenroshks | 5:ce0bb4174ca4 | 154 | |
greenroshks | 2:458f9e603814 | 155 | } |
raizel_varun | 6:8aea752c6eda | 156 | |
raizel_varun | 6:8aea752c6eda | 157 | } |
raizel_varun | 6:8aea752c6eda | 158 | |
raizel_varun | 6:8aea752c6eda | 159 | |
raizel_varun | 6:8aea752c6eda | 160 | |
raizel_varun | 6:8aea752c6eda | 161 |