![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
master working
Fork of master_working_1_2_reset_test by
main.cpp@5:cb35b461fa29, 2015-01-22 (annotated)
- Committer:
- sakthipriya
- Date:
- Thu Jan 22 14:34:49 2015 +0000
- Revision:
- 5:cb35b461fa29
- Parent:
- 4:7992ea5e41ed
master working
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); |
sakthipriya | 0:58b0c597a58c | 9 | |
sakthipriya | 0:58b0c597a58c | 10 | int reset; |
sakthipriya | 0:58b0c597a58c | 11 | |
sakthipriya | 0:58b0c597a58c | 12 | typedef struct |
sakthipriya | 0:58b0c597a58c | 13 | { |
sakthipriya | 0:58b0c597a58c | 14 | char data; // To avoid dynamic memory allocation |
sakthipriya | 0:58b0c597a58c | 15 | int length; |
sakthipriya | 0:58b0c597a58c | 16 | }i2c_data; |
sakthipriya | 0:58b0c597a58c | 17 | |
sakthipriya | 0:58b0c597a58c | 18 | Mail<i2c_data,16> i2c_data_receive; |
sakthipriya | 0:58b0c597a58c | 19 | Mail<i2c_data,16> i2c_data_send; |
sakthipriya | 4:7992ea5e41ed | 20 | unsigned char* data_reg = (unsigned char*)0x40067004; |
sakthipriya | 4:7992ea5e41ed | 21 | unsigned char* status_reg = (unsigned char*)0x40067003; |
sakthipriya | 4:7992ea5e41ed | 22 | unsigned char* c1_reg = (unsigned char*)0x40067002; |
sakthipriya | 4:7992ea5e41ed | 23 | unsigned char* c2_reg = (unsigned char*)0x40067005; |
sakthipriya | 4:7992ea5e41ed | 24 | unsigned char* freq_reg = (unsigned char*)0x40067001; |
sakthipriya | 0:58b0c597a58c | 25 | |
sakthipriya | 0:58b0c597a58c | 26 | Thread * ptr_t_i2c; |
sakthipriya | 0:58b0c597a58c | 27 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 0:58b0c597a58c | 28 | { |
sakthipriya | 0:58b0c597a58c | 29 | //printf("press backspace for master to start \n\r"); |
sakthipriya | 0:58b0c597a58c | 30 | //while(1){ |
sakthipriya | 0:58b0c597a58c | 31 | //interrupt = 0; |
sakthipriya | 0:58b0c597a58c | 32 | // if(pc.getc()=='z'){ |
sakthipriya | 0:58b0c597a58c | 33 | wait(0.5); |
sakthipriya | 0:58b0c597a58c | 34 | interrupt = 1; |
sakthipriya | 0:58b0c597a58c | 35 | printf("\nMaster executed\n"); |
sakthipriya | 0:58b0c597a58c | 36 | |
sakthipriya | 0:58b0c597a58c | 37 | |
sakthipriya | 0:58b0c597a58c | 38 | bool ack0 =true; |
sakthipriya | 0:58b0c597a58c | 39 | bool loopvariable0 =true; |
sakthipriya | 0:58b0c597a58c | 40 | //char *data = new char; |
sakthipriya | 0:58b0c597a58c | 41 | //*data = 'b'; |
sakthipriya | 0:58b0c597a58c | 42 | |
sakthipriya | 0:58b0c597a58c | 43 | |
sakthipriya | 0:58b0c597a58c | 44 | |
sakthipriya | 5:cb35b461fa29 | 45 | // master.frequency(100000); //set clock frequency |
sakthipriya | 5:cb35b461fa29 | 46 | master.start(); |
sakthipriya | 0:58b0c597a58c | 47 | /*while(write2slave) |
sakthipriya | 0:58b0c597a58c | 48 | { *writedata = pc.getc(); |
sakthipriya | 0:58b0c597a58c | 49 | wait(0.5); |
sakthipriya | 0:58b0c597a58c | 50 | |
sakthipriya | 0:58b0c597a58c | 51 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:58b0c597a58c | 52 | master.start(); //initiating the data transfer |
sakthipriya | 0:58b0c597a58c | 53 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:58b0c597a58c | 54 | if(master_status_write==0) |
sakthipriya | 0:58b0c597a58c | 55 | { |
sakthipriya | 0:58b0c597a58c | 56 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:58b0c597a58c | 57 | write2slave=false; |
sakthipriya | 0:58b0c597a58c | 58 | } |
sakthipriya | 0:58b0c597a58c | 59 | |
sakthipriya | 0:58b0c597a58c | 60 | |
sakthipriya | 0:58b0c597a58c | 61 | }*/ |
greenroshks | 3:7a895e0fdd45 | 62 | //wait(0.5); |
sakthipriya | 4:7992ea5e41ed | 63 | while(ack0 ) |
sakthipriya | 0:58b0c597a58c | 64 | { |
sakthipriya | 4:7992ea5e41ed | 65 | //printf("\n\r %x \n",addr); |
sakthipriya | 0:58b0c597a58c | 66 | |
sakthipriya | 4:7992ea5e41ed | 67 | //initiating the data transfer |
sakthipriya | 4:7992ea5e41ed | 68 | ack0 = master.read(addr|1,data,length); |
sakthipriya | 4:7992ea5e41ed | 69 | //printf("\nready to receive\n"); |
sakthipriya | 4:7992ea5e41ed | 70 | |
sakthipriya | 0:58b0c597a58c | 71 | } |
sakthipriya | 4:7992ea5e41ed | 72 | |
sakthipriya | 4:7992ea5e41ed | 73 | if(!ack0 ) |
sakthipriya | 0:58b0c597a58c | 74 | { |
sakthipriya | 0:58b0c597a58c | 75 | printf("\n master has read %c from slave\n\r",*data); |
sakthipriya | 5:cb35b461fa29 | 76 | //printf("\n \r %d %d %d %d %d\n",*data_reg,*status_reg,*c1_reg,*c2_reg,*freq_reg); |
sakthipriya | 0:58b0c597a58c | 77 | loopvariable0=false; |
sakthipriya | 0:58b0c597a58c | 78 | } |
sakthipriya | 0:58b0c597a58c | 79 | //master.stop(); |
sakthipriya | 0:58b0c597a58c | 80 | |
sakthipriya | 0:58b0c597a58c | 81 | //printf("done\n\r"); |
sakthipriya | 0:58b0c597a58c | 82 | //delete data; |
sakthipriya | 0:58b0c597a58c | 83 | interrupt=0; |
sakthipriya | 0:58b0c597a58c | 84 | } |
sakthipriya | 0:58b0c597a58c | 85 | |
sakthipriya | 0:58b0c597a58c | 86 | void T_I2C_MASTER_FSLAVE(void const *args) |
sakthipriya | 0:58b0c597a58c | 87 | { |
sakthipriya | 0:58b0c597a58c | 88 | char data_receive; |
sakthipriya | 0:58b0c597a58c | 89 | while(1) |
sakthipriya | 0:58b0c597a58c | 90 | { |
sakthipriya | 0:58b0c597a58c | 91 | Thread::signal_wait(0x1); |
sakthipriya | 0:58b0c597a58c | 92 | FUNC_I2C_MASTER_FSLAVE(&data_receive,1); |
sakthipriya | 0:58b0c597a58c | 93 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
sakthipriya | 0:58b0c597a58c | 94 | i2c_data_r->data = data_receive; |
sakthipriya | 0:58b0c597a58c | 95 | i2c_data_r->length = 1; |
sakthipriya | 0:58b0c597a58c | 96 | i2c_data_receive.put(i2c_data_r); |
sakthipriya | 1:e5c939440756 | 97 | printf("\n Data received from slave is %c\n\r",data_receive); |
sakthipriya | 0:58b0c597a58c | 98 | i2c_data_receive.free(i2c_data_r); |
sakthipriya | 0:58b0c597a58c | 99 | } |
sakthipriya | 0:58b0c597a58c | 100 | } |
sakthipriya | 0:58b0c597a58c | 101 | |
sakthipriya | 0:58b0c597a58c | 102 | void FUNC_INT() |
sakthipriya | 0:58b0c597a58c | 103 | { |
sakthipriya | 0:58b0c597a58c | 104 | |
sakthipriya | 0:58b0c597a58c | 105 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 0:58b0c597a58c | 106 | |
sakthipriya | 0:58b0c597a58c | 107 | } |
sakthipriya | 0:58b0c597a58c | 108 | |
sakthipriya | 1:e5c939440756 | 109 | char *writedata = new char; |
sakthipriya | 1:e5c939440756 | 110 | void FUNC_MASTER_WRITE() |
sakthipriya | 1:e5c939440756 | 111 | { |
sakthipriya | 1:e5c939440756 | 112 | bool write2slave=true; |
sakthipriya | 0:58b0c597a58c | 113 | |
sakthipriya | 0:58b0c597a58c | 114 | interrupt = 0; |
sakthipriya | 1:e5c939440756 | 115 | |
sakthipriya | 1:e5c939440756 | 116 | char data = pc.getc(); |
sakthipriya | 1:e5c939440756 | 117 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 1:e5c939440756 | 118 | i2c_data_s->data = data; |
sakthipriya | 1:e5c939440756 | 119 | i2c_data_s->length = 1; |
sakthipriya | 1:e5c939440756 | 120 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 1:e5c939440756 | 121 | bool master_status_write = true; |
sakthipriya | 0:58b0c597a58c | 122 | interrupt = 1; |
sakthipriya | 1:e5c939440756 | 123 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 1:e5c939440756 | 124 | if (evt.status == osEventMail) |
sakthipriya | 1:e5c939440756 | 125 | { |
sakthipriya | 1:e5c939440756 | 126 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
sakthipriya | 1:e5c939440756 | 127 | *writedata = i2c_data_s -> data; |
sakthipriya | 5:cb35b461fa29 | 128 | //master.frequency(100000); //set clock frequency |
greenroshks | 3:7a895e0fdd45 | 129 | while(write2slave && interrupt ==1) |
sakthipriya | 1:e5c939440756 | 130 | { |
sakthipriya | 0:58b0c597a58c | 131 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 4:7992ea5e41ed | 132 | //master.start(); //initiating the data transfer |
sakthipriya | 0:58b0c597a58c | 133 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:58b0c597a58c | 134 | if(master_status_write==0) |
sakthipriya | 0:58b0c597a58c | 135 | { |
sakthipriya | 0:58b0c597a58c | 136 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:58b0c597a58c | 137 | write2slave=false; |
sakthipriya | 0:58b0c597a58c | 138 | } |
sakthipriya | 2:e80af3c366aa | 139 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 5:cb35b461fa29 | 140 | //printf("\n \r %d %d %d %d %d\n",*data_reg,*status_reg,*c1_reg,*c2_reg,*freq_reg); |
sakthipriya | 1:e5c939440756 | 141 | } |
sakthipriya | 0:58b0c597a58c | 142 | } |
sakthipriya | 1:e5c939440756 | 143 | } |
sakthipriya | 1:e5c939440756 | 144 | |
sakthipriya | 1:e5c939440756 | 145 | |
sakthipriya | 1:e5c939440756 | 146 | |
sakthipriya | 1:e5c939440756 | 147 | int main() |
sakthipriya | 1:e5c939440756 | 148 | { |
sakthipriya | 4:7992ea5e41ed | 149 | int i= 0; |
sakthipriya | 5:cb35b461fa29 | 150 | master.frequency(100000); |
sakthipriya | 1:e5c939440756 | 151 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
sakthipriya | 1:e5c939440756 | 152 | data_ready.rise(&FUNC_INT); |
sakthipriya | 1:e5c939440756 | 153 | printf("\nstarted master\n"); |
sakthipriya | 4:7992ea5e41ed | 154 | //master.start(); |
sakthipriya | 1:e5c939440756 | 155 | while(1) |
sakthipriya | 1:e5c939440756 | 156 | { |
sakthipriya | 1:e5c939440756 | 157 | FUNC_MASTER_WRITE(); |
sakthipriya | 2:e80af3c366aa | 158 | |
sakthipriya | 0:58b0c597a58c | 159 | } |
sakthipriya | 0:58b0c597a58c | 160 | } |