while loop removed ( master to slave not working )
Fork of master_working_obama by
main.cpp@7:0d18080e16ce, 2015-01-26 (annotated)
- Committer:
- greenroshks
- Date:
- Mon Jan 26 11:11:59 2015 +0000
- Revision:
- 7:0d18080e16ce
- Parent:
- 6:efcbb48e0d03
with timeouts and timers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:58b0c597a58c | 1 | #include "mbed.h" |
sakthipriya | 0:58b0c597a58c | 2 | #include "rtos.h" |
sakthipriya | 0:58b0c597a58c | 3 | |
sakthipriya | 0:58b0c597a58c | 4 | const int addr = 0x20; //slave address |
sakthipriya | 0:58b0c597a58c | 5 | I2C master (D14,D15); //configure pins p27,p28 as I2C master |
sakthipriya | 0:58b0c597a58c | 6 | Serial pc (USBTX,USBRX); |
sakthipriya | 0:58b0c597a58c | 7 | DigitalOut interrupt(D9); |
sakthipriya | 0:58b0c597a58c | 8 | InterruptIn data_ready(D10); |
greenroshks | 6:efcbb48e0d03 | 9 | Timer t; |
greenroshks | 7:0d18080e16ce | 10 | Timer t2; |
sakthipriya | 0:58b0c597a58c | 11 | int reset; |
sakthipriya | 0:58b0c597a58c | 12 | |
sakthipriya | 0:58b0c597a58c | 13 | typedef struct |
sakthipriya | 0:58b0c597a58c | 14 | { |
sakthipriya | 0:58b0c597a58c | 15 | char data; // To avoid dynamic memory allocation |
sakthipriya | 0:58b0c597a58c | 16 | int length; |
sakthipriya | 0:58b0c597a58c | 17 | }i2c_data; |
sakthipriya | 0:58b0c597a58c | 18 | |
sakthipriya | 0:58b0c597a58c | 19 | Mail<i2c_data,16> i2c_data_receive; |
sakthipriya | 0:58b0c597a58c | 20 | Mail<i2c_data,16> i2c_data_send; |
sakthipriya | 4:7992ea5e41ed | 21 | unsigned char* data_reg = (unsigned char*)0x40067004; |
sakthipriya | 4:7992ea5e41ed | 22 | unsigned char* status_reg = (unsigned char*)0x40067003; |
sakthipriya | 4:7992ea5e41ed | 23 | unsigned char* c1_reg = (unsigned char*)0x40067002; |
sakthipriya | 4:7992ea5e41ed | 24 | unsigned char* c2_reg = (unsigned char*)0x40067005; |
sakthipriya | 4:7992ea5e41ed | 25 | unsigned char* freq_reg = (unsigned char*)0x40067001; |
sakthipriya | 0:58b0c597a58c | 26 | |
sakthipriya | 0:58b0c597a58c | 27 | Thread * ptr_t_i2c; |
sakthipriya | 0:58b0c597a58c | 28 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 0:58b0c597a58c | 29 | { |
sakthipriya | 0:58b0c597a58c | 30 | //printf("press backspace for master to start \n\r"); |
sakthipriya | 0:58b0c597a58c | 31 | //while(1){ |
sakthipriya | 0:58b0c597a58c | 32 | //interrupt = 0; |
sakthipriya | 0:58b0c597a58c | 33 | // if(pc.getc()=='z'){ |
greenroshks | 7:0d18080e16ce | 34 | |
greenroshks | 6:efcbb48e0d03 | 35 | |
sakthipriya | 0:58b0c597a58c | 36 | interrupt = 1; |
greenroshks | 7:0d18080e16ce | 37 | t2.start(); |
greenroshks | 6:efcbb48e0d03 | 38 | //printf("\nMaster executed\n"); |
sakthipriya | 0:58b0c597a58c | 39 | |
sakthipriya | 0:58b0c597a58c | 40 | |
sakthipriya | 0:58b0c597a58c | 41 | bool ack0 =true; |
sakthipriya | 0:58b0c597a58c | 42 | bool loopvariable0 =true; |
sakthipriya | 0:58b0c597a58c | 43 | //char *data = new char; |
sakthipriya | 0:58b0c597a58c | 44 | //*data = 'b'; |
sakthipriya | 0:58b0c597a58c | 45 | |
sakthipriya | 0:58b0c597a58c | 46 | |
sakthipriya | 0:58b0c597a58c | 47 | |
sakthipriya | 5:cb35b461fa29 | 48 | // master.frequency(100000); //set clock frequency |
greenroshks | 6:efcbb48e0d03 | 49 | // master.start(); |
sakthipriya | 0:58b0c597a58c | 50 | /*while(write2slave) |
sakthipriya | 0:58b0c597a58c | 51 | { *writedata = pc.getc(); |
sakthipriya | 0:58b0c597a58c | 52 | wait(0.5); |
sakthipriya | 0:58b0c597a58c | 53 | |
sakthipriya | 0:58b0c597a58c | 54 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:58b0c597a58c | 55 | master.start(); //initiating the data transfer |
sakthipriya | 0:58b0c597a58c | 56 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:58b0c597a58c | 57 | if(master_status_write==0) |
sakthipriya | 0:58b0c597a58c | 58 | { |
sakthipriya | 0:58b0c597a58c | 59 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:58b0c597a58c | 60 | write2slave=false; |
sakthipriya | 0:58b0c597a58c | 61 | } |
sakthipriya | 0:58b0c597a58c | 62 | |
sakthipriya | 0:58b0c597a58c | 63 | |
sakthipriya | 0:58b0c597a58c | 64 | }*/ |
greenroshks | 3:7a895e0fdd45 | 65 | //wait(0.5); |
greenroshks | 6:efcbb48e0d03 | 66 | //while(ack0 ) |
greenroshks | 6:efcbb48e0d03 | 67 | //{ |
sakthipriya | 4:7992ea5e41ed | 68 | //printf("\n\r %x \n",addr); |
sakthipriya | 0:58b0c597a58c | 69 | |
greenroshks | 7:0d18080e16ce | 70 | //t2.stop(); //initiating the data transfer |
greenroshks | 7:0d18080e16ce | 71 | //wait_ms(10); |
greenroshks | 7:0d18080e16ce | 72 | |
sakthipriya | 4:7992ea5e41ed | 73 | ack0 = master.read(addr|1,data,length); |
greenroshks | 7:0d18080e16ce | 74 | t2.stop(); |
greenroshks | 7:0d18080e16ce | 75 | //t2.stop(); |
greenroshks | 7:0d18080e16ce | 76 | |
sakthipriya | 4:7992ea5e41ed | 77 | //printf("\nready to receive\n"); |
sakthipriya | 4:7992ea5e41ed | 78 | |
greenroshks | 6:efcbb48e0d03 | 79 | //} |
sakthipriya | 4:7992ea5e41ed | 80 | |
sakthipriya | 4:7992ea5e41ed | 81 | if(!ack0 ) |
sakthipriya | 0:58b0c597a58c | 82 | { |
sakthipriya | 0:58b0c597a58c | 83 | printf("\n master has read %c from slave\n\r",*data); |
sakthipriya | 5:cb35b461fa29 | 84 | //printf("\n \r %d %d %d %d %d\n",*data_reg,*status_reg,*c1_reg,*c2_reg,*freq_reg); |
sakthipriya | 0:58b0c597a58c | 85 | loopvariable0=false; |
sakthipriya | 0:58b0c597a58c | 86 | } |
sakthipriya | 0:58b0c597a58c | 87 | //master.stop(); |
greenroshks | 7:0d18080e16ce | 88 | printf("\n%d\n\r",t2.read_us()); |
greenroshks | 7:0d18080e16ce | 89 | t2.reset(); |
sakthipriya | 0:58b0c597a58c | 90 | //printf("done\n\r"); |
sakthipriya | 0:58b0c597a58c | 91 | //delete data; |
sakthipriya | 0:58b0c597a58c | 92 | interrupt=0; |
greenroshks | 6:efcbb48e0d03 | 93 | |
greenroshks | 6:efcbb48e0d03 | 94 | |
greenroshks | 6:efcbb48e0d03 | 95 | |
sakthipriya | 0:58b0c597a58c | 96 | } |
sakthipriya | 0:58b0c597a58c | 97 | |
sakthipriya | 0:58b0c597a58c | 98 | void T_I2C_MASTER_FSLAVE(void const *args) |
sakthipriya | 0:58b0c597a58c | 99 | { |
sakthipriya | 0:58b0c597a58c | 100 | char data_receive; |
sakthipriya | 0:58b0c597a58c | 101 | while(1) |
sakthipriya | 0:58b0c597a58c | 102 | { |
sakthipriya | 0:58b0c597a58c | 103 | Thread::signal_wait(0x1); |
sakthipriya | 0:58b0c597a58c | 104 | FUNC_I2C_MASTER_FSLAVE(&data_receive,1); |
sakthipriya | 0:58b0c597a58c | 105 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
sakthipriya | 0:58b0c597a58c | 106 | i2c_data_r->data = data_receive; |
sakthipriya | 0:58b0c597a58c | 107 | i2c_data_r->length = 1; |
sakthipriya | 0:58b0c597a58c | 108 | i2c_data_receive.put(i2c_data_r); |
sakthipriya | 1:e5c939440756 | 109 | printf("\n Data received from slave is %c\n\r",data_receive); |
sakthipriya | 0:58b0c597a58c | 110 | i2c_data_receive.free(i2c_data_r); |
sakthipriya | 0:58b0c597a58c | 111 | } |
sakthipriya | 0:58b0c597a58c | 112 | } |
sakthipriya | 0:58b0c597a58c | 113 | |
sakthipriya | 0:58b0c597a58c | 114 | void FUNC_INT() |
sakthipriya | 0:58b0c597a58c | 115 | { |
sakthipriya | 0:58b0c597a58c | 116 | |
sakthipriya | 0:58b0c597a58c | 117 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 0:58b0c597a58c | 118 | |
sakthipriya | 0:58b0c597a58c | 119 | } |
sakthipriya | 0:58b0c597a58c | 120 | |
sakthipriya | 1:e5c939440756 | 121 | char *writedata = new char; |
sakthipriya | 1:e5c939440756 | 122 | void FUNC_MASTER_WRITE() |
sakthipriya | 1:e5c939440756 | 123 | { |
sakthipriya | 1:e5c939440756 | 124 | bool write2slave=true; |
sakthipriya | 0:58b0c597a58c | 125 | |
sakthipriya | 0:58b0c597a58c | 126 | interrupt = 0; |
sakthipriya | 1:e5c939440756 | 127 | |
sakthipriya | 1:e5c939440756 | 128 | char data = pc.getc(); |
greenroshks | 7:0d18080e16ce | 129 | |
sakthipriya | 1:e5c939440756 | 130 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 1:e5c939440756 | 131 | i2c_data_s->data = data; |
sakthipriya | 1:e5c939440756 | 132 | i2c_data_s->length = 1; |
sakthipriya | 1:e5c939440756 | 133 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 1:e5c939440756 | 134 | bool master_status_write = true; |
greenroshks | 6:efcbb48e0d03 | 135 | interrupt = 1; |
greenroshks | 7:0d18080e16ce | 136 | t.start(); |
greenroshks | 7:0d18080e16ce | 137 | //wait_ms(43); //to account for npt entering the inteupt = 1 conditin on slave due to the interruitp = 0 above |
sakthipriya | 1:e5c939440756 | 138 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 1:e5c939440756 | 139 | if (evt.status == osEventMail) |
sakthipriya | 1:e5c939440756 | 140 | { |
sakthipriya | 1:e5c939440756 | 141 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
sakthipriya | 1:e5c939440756 | 142 | *writedata = i2c_data_s -> data; |
sakthipriya | 5:cb35b461fa29 | 143 | //master.frequency(100000); //set clock frequency |
greenroshks | 7:0d18080e16ce | 144 | //while(write2slave) |
greenroshks | 7:0d18080e16ce | 145 | //{ |
greenroshks | 6:efcbb48e0d03 | 146 | printf("\nhere\n"); |
greenroshks | 7:0d18080e16ce | 147 | //master.start(); |
greenroshks | 7:0d18080e16ce | 148 | t.stop(); //initiating the data transfer |
sakthipriya | 0:58b0c597a58c | 149 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:58b0c597a58c | 150 | if(master_status_write==0) |
sakthipriya | 0:58b0c597a58c | 151 | { |
sakthipriya | 0:58b0c597a58c | 152 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:58b0c597a58c | 153 | write2slave=false; |
sakthipriya | 0:58b0c597a58c | 154 | } |
greenroshks | 7:0d18080e16ce | 155 | printf("\n%f\n",t.read()); |
greenroshks | 7:0d18080e16ce | 156 | t.reset(); |
sakthipriya | 2:e80af3c366aa | 157 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 5:cb35b461fa29 | 158 | //printf("\n \r %d %d %d %d %d\n",*data_reg,*status_reg,*c1_reg,*c2_reg,*freq_reg); |
greenroshks | 7:0d18080e16ce | 159 | //} |
sakthipriya | 0:58b0c597a58c | 160 | } |
sakthipriya | 1:e5c939440756 | 161 | } |
sakthipriya | 1:e5c939440756 | 162 | |
sakthipriya | 1:e5c939440756 | 163 | |
sakthipriya | 1:e5c939440756 | 164 | |
sakthipriya | 1:e5c939440756 | 165 | int main() |
sakthipriya | 1:e5c939440756 | 166 | { |
greenroshks | 6:efcbb48e0d03 | 167 | |
sakthipriya | 4:7992ea5e41ed | 168 | int i= 0; |
greenroshks | 6:efcbb48e0d03 | 169 | //master.start(); |
sakthipriya | 5:cb35b461fa29 | 170 | master.frequency(100000); |
sakthipriya | 1:e5c939440756 | 171 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
greenroshks | 6:efcbb48e0d03 | 172 | |
sakthipriya | 1:e5c939440756 | 173 | data_ready.rise(&FUNC_INT); |
sakthipriya | 1:e5c939440756 | 174 | printf("\nstarted master\n"); |
sakthipriya | 4:7992ea5e41ed | 175 | //master.start(); |
sakthipriya | 1:e5c939440756 | 176 | while(1) |
sakthipriya | 1:e5c939440756 | 177 | { |
sakthipriya | 1:e5c939440756 | 178 | FUNC_MASTER_WRITE(); |
sakthipriya | 2:e80af3c366aa | 179 | |
sakthipriya | 0:58b0c597a58c | 180 | } |
sakthipriya | 0:58b0c597a58c | 181 | } |