![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
while loop removed ( master cant send)
Fork of slave_working_obama by
main.cpp@10:f2a8d9ac49d6, 2015-01-20 (annotated)
- Committer:
- sakthipriya
- Date:
- Tue Jan 20 16:12:18 2015 +0000
- Revision:
- 10:f2a8d9ac49d6
- Parent:
- 9:baf5de5828c9
- Child:
- 11:907c02e9fb00
slave debug
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; |
sakthipriya | 10:f2a8d9ac49d6 | 24 | |
sakthipriya | 8:04b912dc2443 | 25 | |
sakthipriya | 10:f2a8d9ac49d6 | 26 | int receive_status =0; |
greenroshks | 5:ce0bb4174ca4 | 27 | void FUNC_I2C_WRITE2CDMS(char *data, int length=1) |
greenroshks | 4:133b49d51b18 | 28 | { |
raizel_varun | 6:8aea752c6eda | 29 | int slave_status = 1; |
sakthipriya | 10:f2a8d9ac49d6 | 30 | // printf("\nData is %c\n\r",*data); |
sakthipriya | 10:f2a8d9ac49d6 | 31 | |
sakthipriya | 10:f2a8d9ac49d6 | 32 | slave.address(0x20); |
sakthipriya | 10:f2a8d9ac49d6 | 33 | unsigned char* add_reg = (unsigned char*)0x40067000; |
sakthipriya | 10:f2a8d9ac49d6 | 34 | unsigned char* data_reg = (unsigned char*)0x40067004; |
sakthipriya | 10:f2a8d9ac49d6 | 35 | unsigned char* status_reg = (unsigned char*)0x40067003; |
sakthipriya | 10:f2a8d9ac49d6 | 36 | unsigned char* c1_reg = (unsigned char*)0x40067002; |
sakthipriya | 10:f2a8d9ac49d6 | 37 | unsigned char* c2_reg = (unsigned char*)0x40067005; |
sakthipriya | 10:f2a8d9ac49d6 | 38 | unsigned char* freq_reg = (unsigned char*)0x40067001; |
greenroshks | 9:baf5de5828c9 | 39 | while(slave_status && interrupt ==1) |
sakthipriya | 10:f2a8d9ac49d6 | 40 | { |
sakthipriya | 10:f2a8d9ac49d6 | 41 | //wait(2); |
sakthipriya | 10:f2a8d9ac49d6 | 42 | receive_status = slave.receive(); |
sakthipriya | 10:f2a8d9ac49d6 | 43 | |
sakthipriya | 10:f2a8d9ac49d6 | 44 | if( receive_status==1) |
greenroshks | 2:458f9e603814 | 45 | { |
sakthipriya | 10:f2a8d9ac49d6 | 46 | // printf("\nslave receive b4: \n"); |
greenroshks | 5:ce0bb4174ca4 | 47 | slave_status=slave.write(data,length); |
sakthipriya | 10:f2a8d9ac49d6 | 48 | // printf("\nData sent to CDMS is %c\n\r",*data); |
sakthipriya | 10:f2a8d9ac49d6 | 49 | // printf("\n\r write"); |
sakthipriya | 10:f2a8d9ac49d6 | 50 | //printf("\nslave receive after: %d \n",slave.receive()); |
greenroshks | 2:458f9e603814 | 51 | |
viswachaitanya | 0:24e80ff2c7b1 | 52 | } |
sakthipriya | 10:f2a8d9ac49d6 | 53 | else if( receive_status==3 || receive_status==2) |
sakthipriya | 8:04b912dc2443 | 54 | { |
sakthipriya | 8:04b912dc2443 | 55 | slave_status=slave.read(data,length); |
sakthipriya | 10:f2a8d9ac49d6 | 56 | // printf("\n\r read"); |
sakthipriya | 8:04b912dc2443 | 57 | } |
sakthipriya | 10:f2a8d9ac49d6 | 58 | //printf("\n \r done while loop %d \n",slave.receive()); |
sakthipriya | 10:f2a8d9ac49d6 | 59 | //printf("\n\r ahoy drhghhhhhhhhhhh\n"); |
sakthipriya | 10:f2a8d9ac49d6 | 60 | printf("\n\r %d %d %d %d %d \n",*data_reg,*status_reg,*c1_reg,*c2_reg,*freq_reg); |
sakthipriya | 8:04b912dc2443 | 61 | |
greenroshks | 2:458f9e603814 | 62 | } |
sakthipriya | 8:04b912dc2443 | 63 | printf("\ndone\n\r"); |
greenroshks | 4:133b49d51b18 | 64 | |
greenroshks | 2:458f9e603814 | 65 | } |
greenroshks | 2:458f9e603814 | 66 | |
greenroshks | 4:133b49d51b18 | 67 | void T_I2C_BAE(void const * args) |
greenroshks | 2:458f9e603814 | 68 | { |
greenroshks | 4:133b49d51b18 | 69 | char data_send,data_receive; |
greenroshks | 4:133b49d51b18 | 70 | while(1) |
greenroshks | 4:133b49d51b18 | 71 | { |
greenroshks | 4:133b49d51b18 | 72 | Thread::signal_wait(0x1); |
sakthipriya | 8:04b912dc2443 | 73 | //i2c_status = temp; |
sakthipriya | 8:04b912dc2443 | 74 | //wait(0.5); |
sakthipriya | 8:04b912dc2443 | 75 | printf("\n entered thread\n\r"); |
sakthipriya | 8:04b912dc2443 | 76 | if(i2c_status == 0 && reset !=1) |
greenroshks | 5:ce0bb4174ca4 | 77 | { |
greenroshks | 5:ce0bb4174ca4 | 78 | |
greenroshks | 5:ce0bb4174ca4 | 79 | FUNC_I2C_WRITE2CDMS(&data_receive); |
greenroshks | 5:ce0bb4174ca4 | 80 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 5:ce0bb4174ca4 | 81 | i2c_data_r->data = data_receive; |
greenroshks | 5:ce0bb4174ca4 | 82 | i2c_data_r->length = 1; |
greenroshks | 5:ce0bb4174ca4 | 83 | i2c_data_receive.put(i2c_data_r); |
sakthipriya | 8:04b912dc2443 | 84 | printf("\n Data received from CDMS is %c\n\r",data_receive); |
sakthipriya | 8:04b912dc2443 | 85 | i2c_data_receive.free(i2c_data_r); // This has to be done from a differen thread |
raizel_varun | 6:8aea752c6eda | 86 | |
greenroshks | 5:ce0bb4174ca4 | 87 | } |
sakthipriya | 8:04b912dc2443 | 88 | else if(i2c_status ==1 && reset !=1) |
greenroshks | 5:ce0bb4174ca4 | 89 | { |
greenroshks | 5:ce0bb4174ca4 | 90 | osEvent evt = i2c_data_send.get(); |
greenroshks | 5:ce0bb4174ca4 | 91 | if (evt.status == osEventMail) |
greenroshks | 5:ce0bb4174ca4 | 92 | { |
greenroshks | 5:ce0bb4174ca4 | 93 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
greenroshks | 5:ce0bb4174ca4 | 94 | data_send = i2c_data_s -> data; |
sakthipriya | 10:f2a8d9ac49d6 | 95 | //printf("\n1.Data is %c\n\r",data_send); |
greenroshks | 5:ce0bb4174ca4 | 96 | FUNC_I2C_WRITE2CDMS(&data_send); |
sakthipriya | 8:04b912dc2443 | 97 | printf("\nData sent to CDMS is %c\n\r",data_send); |
greenroshks | 5:ce0bb4174ca4 | 98 | i2c_data_send.free(i2c_data_s); |
greenroshks | 5:ce0bb4174ca4 | 99 | i2c_status = 0; |
sakthipriya | 8:04b912dc2443 | 100 | //temp = i2c_status; |
greenroshks | 5:ce0bb4174ca4 | 101 | } |
raizel_varun | 6:8aea752c6eda | 102 | } |
greenroshks | 9:baf5de5828c9 | 103 | |
greenroshks | 9:baf5de5828c9 | 104 | data_ready=0; |
greenroshks | 4:133b49d51b18 | 105 | } |
greenroshks | 2:458f9e603814 | 106 | } |
greenroshks | 4:133b49d51b18 | 107 | |
greenroshks | 4:133b49d51b18 | 108 | void FUNC_INT() |
greenroshks | 4:133b49d51b18 | 109 | { |
sakthipriya | 8:04b912dc2443 | 110 | reset = 0; |
greenroshks | 4:133b49d51b18 | 111 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 7:debeb7070a54 | 112 | |
greenroshks | 4:133b49d51b18 | 113 | } |
sakthipriya | 7:debeb7070a54 | 114 | |
sakthipriya | 8:04b912dc2443 | 115 | void FUNC_RESET() |
sakthipriya | 8:04b912dc2443 | 116 | { |
sakthipriya | 8:04b912dc2443 | 117 | reset = 1; |
sakthipriya | 8:04b912dc2443 | 118 | } |
sakthipriya | 8:04b912dc2443 | 119 | |
sakthipriya | 7:debeb7070a54 | 120 | void ir2master() |
sakthipriya | 7:debeb7070a54 | 121 | { |
sakthipriya | 8:04b912dc2443 | 122 | |
sakthipriya | 8:04b912dc2443 | 123 | char data='a'; |
greenroshks | 9:baf5de5828c9 | 124 | //data_ready=0; |
sakthipriya | 7:debeb7070a54 | 125 | data = pc.getc(); |
greenroshks | 9:baf5de5828c9 | 126 | |
sakthipriya | 8:04b912dc2443 | 127 | reset =0; |
sakthipriya | 7:debeb7070a54 | 128 | i2c_status=1; |
sakthipriya | 7:debeb7070a54 | 129 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 7:debeb7070a54 | 130 | i2c_data_s->data = data; |
sakthipriya | 7:debeb7070a54 | 131 | i2c_data_s->length = 1; |
sakthipriya | 7:debeb7070a54 | 132 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 7:debeb7070a54 | 133 | data_ready=1; |
sakthipriya | 8:04b912dc2443 | 134 | //temp = i2c_status; |
sakthipriya | 7:debeb7070a54 | 135 | } |
sakthipriya | 7:debeb7070a54 | 136 | |
greenroshks | 4:133b49d51b18 | 137 | |
greenroshks | 4:133b49d51b18 | 138 | |
greenroshks | 4:133b49d51b18 | 139 | |
raizel_varun | 6:8aea752c6eda | 140 | int main() |
raizel_varun | 6:8aea752c6eda | 141 | { |
sakthipriya | 8:04b912dc2443 | 142 | printf("\n slave started\n\r"); |
raizel_varun | 6:8aea752c6eda | 143 | |
greenroshks | 4:133b49d51b18 | 144 | ptr_t_i2c = new Thread(T_I2C_BAE); |
sakthipriya | 8:04b912dc2443 | 145 | master_reset.fall(&FUNC_RESET); |
greenroshks | 4:133b49d51b18 | 146 | interrupt.rise(&FUNC_INT); |
greenroshks | 2:458f9e603814 | 147 | while(1) |
greenroshks | 2:458f9e603814 | 148 | { |
sakthipriya | 7:debeb7070a54 | 149 | ir2master(); |
greenroshks | 5:ce0bb4174ca4 | 150 | |
greenroshks | 2:458f9e603814 | 151 | } |
raizel_varun | 6:8aea752c6eda | 152 | |
raizel_varun | 6:8aea752c6eda | 153 | } |
raizel_varun | 6:8aea752c6eda | 154 | |
raizel_varun | 6:8aea752c6eda | 155 | |
raizel_varun | 6:8aea752c6eda | 156 | |
raizel_varun | 6:8aea752c6eda | 157 |