![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
while loop removed ( master cant send)
Fork of slave_working_obama by
main.cpp@8:04b912dc2443, 2014-12-13 (annotated)
- Committer:
- sakthipriya
- Date:
- Sat Dec 13 11:22:34 2014 +0000
- Revision:
- 8:04b912dc2443
- Parent:
- 7:debeb7070a54
- Child:
- 9:baf5de5828c9
i2c working
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); |
sakthipriya | 8:04b912dc2443 | 9 | InterruptIn master_reset(D11); |
greenroshks | 5:ce0bb4174ca4 | 10 | DigitalOut data_ready(D10); |
greenroshks | 5:ce0bb4174ca4 | 11 | int i2c_status=0; //read/write mode for i2c 0 : write2slave, 1 : write2master |
sakthipriya | 8:04b912dc2443 | 12 | int reset=0; |
sakthipriya | 8:04b912dc2443 | 13 | int temp; |
sakthipriya | 8:04b912dc2443 | 14 | |
greenroshks | 4:133b49d51b18 | 15 | |
greenroshks | 4:133b49d51b18 | 16 | typedef struct |
greenroshks | 2:458f9e603814 | 17 | { |
greenroshks | 4:133b49d51b18 | 18 | char data; // To avoid dynamic memory allocation |
greenroshks | 4:133b49d51b18 | 19 | int length; |
greenroshks | 4:133b49d51b18 | 20 | }i2c_data; |
viswachaitanya | 0:24e80ff2c7b1 | 21 | |
greenroshks | 4:133b49d51b18 | 22 | Mail<i2c_data,16> i2c_data_receive; |
greenroshks | 4:133b49d51b18 | 23 | Mail<i2c_data,16> i2c_data_send; |
raizel_varun | 6:8aea752c6eda | 24 | |
sakthipriya | 8:04b912dc2443 | 25 | |
greenroshks | 5:ce0bb4174ca4 | 26 | void FUNC_I2C_WRITE2CDMS(char *data, int length=1) |
greenroshks | 4:133b49d51b18 | 27 | { |
raizel_varun | 6:8aea752c6eda | 28 | int slave_status = 1; |
raizel_varun | 6:8aea752c6eda | 29 | |
greenroshks | 2:458f9e603814 | 30 | while(slave_status) |
greenroshks | 2:458f9e603814 | 31 | { |
viswachaitanya | 0:24e80ff2c7b1 | 32 | slave.address(0x20); |
greenroshks | 2:458f9e603814 | 33 | if(slave.receive()==1) |
greenroshks | 2:458f9e603814 | 34 | { |
greenroshks | 5:ce0bb4174ca4 | 35 | slave_status=slave.write(data,length); |
sakthipriya | 7:debeb7070a54 | 36 | |
greenroshks | 2:458f9e603814 | 37 | |
viswachaitanya | 0:24e80ff2c7b1 | 38 | } |
sakthipriya | 8:04b912dc2443 | 39 | else if(slave.receive()==3 || slave.receive()==2) |
sakthipriya | 8:04b912dc2443 | 40 | { |
sakthipriya | 8:04b912dc2443 | 41 | slave_status=slave.read(data,length); |
sakthipriya | 8:04b912dc2443 | 42 | } |
sakthipriya | 8:04b912dc2443 | 43 | |
greenroshks | 2:458f9e603814 | 44 | } |
sakthipriya | 8:04b912dc2443 | 45 | printf("\ndone\n\r"); |
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); |
sakthipriya | 8:04b912dc2443 | 55 | //i2c_status = temp; |
sakthipriya | 8:04b912dc2443 | 56 | //wait(0.5); |
sakthipriya | 8:04b912dc2443 | 57 | printf("\n entered thread\n\r"); |
sakthipriya | 8:04b912dc2443 | 58 | if(i2c_status == 0 && reset !=1) |
greenroshks | 5:ce0bb4174ca4 | 59 | { |
greenroshks | 5:ce0bb4174ca4 | 60 | |
greenroshks | 5:ce0bb4174ca4 | 61 | FUNC_I2C_WRITE2CDMS(&data_receive); |
greenroshks | 5:ce0bb4174ca4 | 62 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 63 | i2c_data_r->data = data_receive; |
greenroshks | 5:ce0bb4174ca4 | 64 | i2c_data_r->length = 1; |
greenroshks | 5:ce0bb4174ca4 | 65 | i2c_data_receive.put(i2c_data_r); |
sakthipriya | 8:04b912dc2443 | 66 | printf("\n Data received from CDMS is %c\n\r",data_receive); |
sakthipriya | 8:04b912dc2443 | 67 | i2c_data_receive.free(i2c_data_r); // This has to be done from a differen thread |
raizel_varun | 6:8aea752c6eda | 68 | |
greenroshks | 5:ce0bb4174ca4 | 69 | } |
sakthipriya | 8:04b912dc2443 | 70 | else if(i2c_status ==1 && reset !=1) |
greenroshks | 5:ce0bb4174ca4 | 71 | { |
greenroshks | 5:ce0bb4174ca4 | 72 | osEvent evt = i2c_data_send.get(); |
greenroshks | 5:ce0bb4174ca4 | 73 | if (evt.status == osEventMail) |
greenroshks | 5:ce0bb4174ca4 | 74 | { |
greenroshks | 5:ce0bb4174ca4 | 75 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
greenroshks | 5:ce0bb4174ca4 | 76 | data_send = i2c_data_s -> data; |
greenroshks | 5:ce0bb4174ca4 | 77 | FUNC_I2C_WRITE2CDMS(&data_send); |
sakthipriya | 8:04b912dc2443 | 78 | printf("\nData sent to CDMS is %c\n\r",data_send); |
greenroshks | 5:ce0bb4174ca4 | 79 | i2c_data_send.free(i2c_data_s); |
greenroshks | 5:ce0bb4174ca4 | 80 | i2c_status = 0; |
sakthipriya | 8:04b912dc2443 | 81 | //temp = i2c_status; |
greenroshks | 5:ce0bb4174ca4 | 82 | } |
raizel_varun | 6:8aea752c6eda | 83 | } |
raizel_varun | 6:8aea752c6eda | 84 | |
greenroshks | 4:133b49d51b18 | 85 | } |
greenroshks | 2:458f9e603814 | 86 | } |
greenroshks | 4:133b49d51b18 | 87 | |
greenroshks | 4:133b49d51b18 | 88 | void FUNC_INT() |
greenroshks | 4:133b49d51b18 | 89 | { |
sakthipriya | 8:04b912dc2443 | 90 | reset = 0; |
greenroshks | 4:133b49d51b18 | 91 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 7:debeb7070a54 | 92 | |
greenroshks | 4:133b49d51b18 | 93 | } |
sakthipriya | 7:debeb7070a54 | 94 | |
sakthipriya | 8:04b912dc2443 | 95 | void FUNC_RESET() |
sakthipriya | 8:04b912dc2443 | 96 | { |
sakthipriya | 8:04b912dc2443 | 97 | reset = 1; |
sakthipriya | 8:04b912dc2443 | 98 | } |
sakthipriya | 8:04b912dc2443 | 99 | |
sakthipriya | 7:debeb7070a54 | 100 | void ir2master() |
sakthipriya | 7:debeb7070a54 | 101 | { |
sakthipriya | 8:04b912dc2443 | 102 | |
sakthipriya | 8:04b912dc2443 | 103 | char data='a'; |
sakthipriya | 7:debeb7070a54 | 104 | data_ready=0; |
sakthipriya | 7:debeb7070a54 | 105 | data = pc.getc(); |
sakthipriya | 8:04b912dc2443 | 106 | reset =0; |
sakthipriya | 7:debeb7070a54 | 107 | i2c_status=1; |
sakthipriya | 7:debeb7070a54 | 108 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 7:debeb7070a54 | 109 | i2c_data_s->data = data; |
sakthipriya | 7:debeb7070a54 | 110 | i2c_data_s->length = 1; |
sakthipriya | 7:debeb7070a54 | 111 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 7:debeb7070a54 | 112 | data_ready=1; |
sakthipriya | 8:04b912dc2443 | 113 | //temp = i2c_status; |
sakthipriya | 7:debeb7070a54 | 114 | } |
sakthipriya | 7:debeb7070a54 | 115 | |
greenroshks | 4:133b49d51b18 | 116 | |
greenroshks | 4:133b49d51b18 | 117 | |
greenroshks | 4:133b49d51b18 | 118 | |
raizel_varun | 6:8aea752c6eda | 119 | int main() |
raizel_varun | 6:8aea752c6eda | 120 | { |
sakthipriya | 8:04b912dc2443 | 121 | printf("\n slave started\n\r"); |
raizel_varun | 6:8aea752c6eda | 122 | |
greenroshks | 4:133b49d51b18 | 123 | ptr_t_i2c = new Thread(T_I2C_BAE); |
sakthipriya | 8:04b912dc2443 | 124 | master_reset.fall(&FUNC_RESET); |
greenroshks | 4:133b49d51b18 | 125 | interrupt.rise(&FUNC_INT); |
greenroshks | 2:458f9e603814 | 126 | while(1) |
greenroshks | 2:458f9e603814 | 127 | { |
sakthipriya | 7:debeb7070a54 | 128 | ir2master(); |
greenroshks | 5:ce0bb4174ca4 | 129 | |
greenroshks | 2:458f9e603814 | 130 | } |
raizel_varun | 6:8aea752c6eda | 131 | |
raizel_varun | 6:8aea752c6eda | 132 | } |
raizel_varun | 6:8aea752c6eda | 133 | |
raizel_varun | 6:8aea752c6eda | 134 | |
raizel_varun | 6:8aea752c6eda | 135 | |
raizel_varun | 6:8aea752c6eda | 136 |