green rosh
/
BAE_vr2_1_1
The one with the new HK
Fork of BAE_vr2_1_1 by
slave.cpp@9:221d218f4690, 2014-12-15 (annotated)
- Committer:
- raizel_varun
- Date:
- Mon Dec 15 05:58:04 2014 +0000
- Revision:
- 9:221d218f4690
- Parent:
- 7:f06840d848e3
- Child:
- 10:ed6d3b8d1d56
I2C INTEGTRATED VR1.1
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
raizel_varun | 9:221d218f4690 | 17 | char data; // 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 | |
raizel_varun | 9:221d218f4690 | 23 | Mail<i2c_data,16> i2c_data_receive; |
raizel_varun | 9:221d218f4690 | 24 | Mail<i2c_data,16> i2c_data_send; |
greenroshks | 0:8b0d43fe6c05 | 25 | |
raizel_varun | 9:221d218f4690 | 26 | |
raizel_varun | 9:221d218f4690 | 27 | void FUNC_I2C_WRITE2CDMS(char *data, int length=1) |
raizel_varun | 9:221d218f4690 | 28 | { |
raizel_varun | 9:221d218f4690 | 29 | int slave_status = 1; |
raizel_varun | 9:221d218f4690 | 30 | |
raizel_varun | 9:221d218f4690 | 31 | while(slave_status) |
raizel_varun | 9:221d218f4690 | 32 | { |
raizel_varun | 9:221d218f4690 | 33 | slave.address(0x20); |
raizel_varun | 9:221d218f4690 | 34 | if(slave.receive()==1) |
greenroshks | 0:8b0d43fe6c05 | 35 | { |
raizel_varun | 9:221d218f4690 | 36 | slave_status=slave.write(data,length); |
raizel_varun | 9:221d218f4690 | 37 | |
raizel_varun | 9:221d218f4690 | 38 | |
raizel_varun | 9:221d218f4690 | 39 | } |
raizel_varun | 9:221d218f4690 | 40 | else if(slave.receive()==3 || slave.receive()==2) |
raizel_varun | 9:221d218f4690 | 41 | { |
raizel_varun | 9:221d218f4690 | 42 | slave_status=slave.read(data,length); |
greenroshks | 0:8b0d43fe6c05 | 43 | } |
raizel_varun | 9:221d218f4690 | 44 | |
raizel_varun | 9:221d218f4690 | 45 | } |
raizel_varun | 9:221d218f4690 | 46 | printf("\ndone\n\r"); |
raizel_varun | 9:221d218f4690 | 47 | |
raizel_varun | 9:221d218f4690 | 48 | } |
raizel_varun | 9:221d218f4690 | 49 | char data_send,data_receive; |
raizel_varun | 9:221d218f4690 | 50 | void T_I2C_BAE() |
greenroshks | 0:8b0d43fe6c05 | 51 | { |
raizel_varun | 9:221d218f4690 | 52 | //char data_send,data_receive; |
raizel_varun | 9:221d218f4690 | 53 | //while(1) |
raizel_varun | 9:221d218f4690 | 54 | //{ |
raizel_varun | 9:221d218f4690 | 55 | // Thread::signal_wait(0x1); |
raizel_varun | 9:221d218f4690 | 56 | //i2c_status = temp; |
raizel_varun | 9:221d218f4690 | 57 | //wait(0.5); |
raizel_varun | 9:221d218f4690 | 58 | printf("\n entered thread\n\r"); |
raizel_varun | 9:221d218f4690 | 59 | if(i2c_status == 0 && reset !=1) |
raizel_varun | 9:221d218f4690 | 60 | { |
raizel_varun | 9:221d218f4690 | 61 | |
raizel_varun | 9:221d218f4690 | 62 | FUNC_I2C_WRITE2CDMS(&data_receive); |
raizel_varun | 9:221d218f4690 | 63 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
raizel_varun | 9:221d218f4690 | 64 | i2c_data_r->data = data_receive; |
raizel_varun | 9:221d218f4690 | 65 | i2c_data_r->length = 1; |
raizel_varun | 9:221d218f4690 | 66 | i2c_data_receive.put(i2c_data_r); |
raizel_varun | 9:221d218f4690 | 67 | printf("\n Data received from CDMS is %c\n\r",data_receive); |
raizel_varun | 9:221d218f4690 | 68 | i2c_data_receive.free(i2c_data_r); // This has to be done from a differen thread |
raizel_varun | 9:221d218f4690 | 69 | |
raizel_varun | 9:221d218f4690 | 70 | } |
raizel_varun | 9:221d218f4690 | 71 | else if(i2c_status ==1 && reset !=1) |
raizel_varun | 9:221d218f4690 | 72 | { |
raizel_varun | 9:221d218f4690 | 73 | osEvent evt = i2c_data_send.get(); |
raizel_varun | 9:221d218f4690 | 74 | if (evt.status == osEventMail) |
raizel_varun | 9:221d218f4690 | 75 | { |
raizel_varun | 9:221d218f4690 | 76 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
raizel_varun | 9:221d218f4690 | 77 | data_send = i2c_data_s -> data; |
raizel_varun | 9:221d218f4690 | 78 | FUNC_I2C_WRITE2CDMS(&data_send); |
raizel_varun | 9:221d218f4690 | 79 | printf("\nData sent to CDMS is %c\n\r",data_send); |
raizel_varun | 9:221d218f4690 | 80 | i2c_data_send.free(i2c_data_s); |
raizel_varun | 9:221d218f4690 | 81 | i2c_status = 0; |
raizel_varun | 9:221d218f4690 | 82 | //temp = i2c_status; |
raizel_varun | 9:221d218f4690 | 83 | } |
raizel_varun | 9:221d218f4690 | 84 | } |
raizel_varun | 9:221d218f4690 | 85 | |
raizel_varun | 9:221d218f4690 | 86 | //} |
raizel_varun | 9:221d218f4690 | 87 | } |
greenroshks | 0:8b0d43fe6c05 | 88 | |
raizel_varun | 9:221d218f4690 | 89 | |
raizel_varun | 9:221d218f4690 | 90 | |
raizel_varun | 9:221d218f4690 | 91 | void FUNC_INT() |
raizel_varun | 9:221d218f4690 | 92 | { |
raizel_varun | 9:221d218f4690 | 93 | reset = 0; |
raizel_varun | 9:221d218f4690 | 94 | |
raizel_varun | 9:221d218f4690 | 95 | |
raizel_varun | 9:221d218f4690 | 96 | } |
raizel_varun | 9:221d218f4690 | 97 | |
raizel_varun | 9:221d218f4690 | 98 | void FUNC_RESET() |
raizel_varun | 9:221d218f4690 | 99 | { |
raizel_varun | 9:221d218f4690 | 100 | reset = 1; |
greenroshks | 0:8b0d43fe6c05 | 101 | } |
raizel_varun | 9:221d218f4690 | 102 | |
raizel_varun | 9:221d218f4690 | 103 | void ir2master() |
raizel_varun | 9:221d218f4690 | 104 | { |
raizel_varun | 9:221d218f4690 | 105 | |
raizel_varun | 9:221d218f4690 | 106 | char data='a'; |
raizel_varun | 9:221d218f4690 | 107 | data_ready=0; |
raizel_varun | 9:221d218f4690 | 108 | data = pcslave.getc(); |
raizel_varun | 9:221d218f4690 | 109 | reset =0; |
raizel_varun | 9:221d218f4690 | 110 | i2c_status=1; |
raizel_varun | 9:221d218f4690 | 111 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
raizel_varun | 9:221d218f4690 | 112 | i2c_data_s->data = data; |
raizel_varun | 9:221d218f4690 | 113 | i2c_data_s->length = 1; |
raizel_varun | 9:221d218f4690 | 114 | i2c_data_send.put(i2c_data_s); |
raizel_varun | 9:221d218f4690 | 115 | data_ready=1; |
raizel_varun | 9:221d218f4690 | 116 | //temp = i2c_status; |
greenroshks | 0:8b0d43fe6c05 | 117 | } |
raizel_varun | 9:221d218f4690 | 118 | |
raizel_varun | 9:221d218f4690 | 119 | |
raizel_varun | 9:221d218f4690 | 120 | |
raizel_varun | 9:221d218f4690 | 121 | |
raizel_varun | 9:221d218f4690 | 122 | |
raizel_varun | 9:221d218f4690 | 123 |