![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
master working
Fork of master_working_1_2_reset_test by
main.cpp@0:58b0c597a58c, 2014-12-13 (annotated)
- Committer:
- sakthipriya
- Date:
- Sat Dec 13 11:24:36 2014 +0000
- Revision:
- 0:58b0c597a58c
- Child:
- 1:e5c939440756
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 | 0:58b0c597a58c | 20 | |
sakthipriya | 0:58b0c597a58c | 21 | Thread * ptr_t_i2c; |
sakthipriya | 0:58b0c597a58c | 22 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 0:58b0c597a58c | 23 | { |
sakthipriya | 0:58b0c597a58c | 24 | //printf("press backspace for master to start \n\r"); |
sakthipriya | 0:58b0c597a58c | 25 | //while(1){ |
sakthipriya | 0:58b0c597a58c | 26 | //interrupt = 0; |
sakthipriya | 0:58b0c597a58c | 27 | // if(pc.getc()=='z'){ |
sakthipriya | 0:58b0c597a58c | 28 | wait(0.5); |
sakthipriya | 0:58b0c597a58c | 29 | interrupt = 1; |
sakthipriya | 0:58b0c597a58c | 30 | printf("\nMaster executed\n"); |
sakthipriya | 0:58b0c597a58c | 31 | |
sakthipriya | 0:58b0c597a58c | 32 | |
sakthipriya | 0:58b0c597a58c | 33 | bool ack0 =true; |
sakthipriya | 0:58b0c597a58c | 34 | bool loopvariable0 =true; |
sakthipriya | 0:58b0c597a58c | 35 | //char *data = new char; |
sakthipriya | 0:58b0c597a58c | 36 | //*data = 'b'; |
sakthipriya | 0:58b0c597a58c | 37 | |
sakthipriya | 0:58b0c597a58c | 38 | |
sakthipriya | 0:58b0c597a58c | 39 | |
sakthipriya | 0:58b0c597a58c | 40 | master.frequency(100000); //set clock frequency |
sakthipriya | 0:58b0c597a58c | 41 | //master.start(); |
sakthipriya | 0:58b0c597a58c | 42 | /*while(write2slave) |
sakthipriya | 0:58b0c597a58c | 43 | { *writedata = pc.getc(); |
sakthipriya | 0:58b0c597a58c | 44 | wait(0.5); |
sakthipriya | 0:58b0c597a58c | 45 | |
sakthipriya | 0:58b0c597a58c | 46 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:58b0c597a58c | 47 | master.start(); //initiating the data transfer |
sakthipriya | 0:58b0c597a58c | 48 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:58b0c597a58c | 49 | if(master_status_write==0) |
sakthipriya | 0:58b0c597a58c | 50 | { |
sakthipriya | 0:58b0c597a58c | 51 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:58b0c597a58c | 52 | write2slave=false; |
sakthipriya | 0:58b0c597a58c | 53 | } |
sakthipriya | 0:58b0c597a58c | 54 | |
sakthipriya | 0:58b0c597a58c | 55 | |
sakthipriya | 0:58b0c597a58c | 56 | }*/ |
sakthipriya | 0:58b0c597a58c | 57 | |
sakthipriya | 0:58b0c597a58c | 58 | while(ack0) |
sakthipriya | 0:58b0c597a58c | 59 | { |
sakthipriya | 0:58b0c597a58c | 60 | //printf("2 master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:58b0c597a58c | 61 | |
sakthipriya | 0:58b0c597a58c | 62 | master.start(); //initiating the data transfer |
sakthipriya | 0:58b0c597a58c | 63 | ack0 = master.read(addr,data,length); |
sakthipriya | 0:58b0c597a58c | 64 | } |
sakthipriya | 0:58b0c597a58c | 65 | if(!ack0) |
sakthipriya | 0:58b0c597a58c | 66 | { |
sakthipriya | 0:58b0c597a58c | 67 | printf("\n master has read %c from slave\n\r",*data); |
sakthipriya | 0:58b0c597a58c | 68 | loopvariable0=false; |
sakthipriya | 0:58b0c597a58c | 69 | } |
sakthipriya | 0:58b0c597a58c | 70 | //master.stop(); |
sakthipriya | 0:58b0c597a58c | 71 | |
sakthipriya | 0:58b0c597a58c | 72 | //printf("done\n\r"); |
sakthipriya | 0:58b0c597a58c | 73 | //delete data; |
sakthipriya | 0:58b0c597a58c | 74 | interrupt=0; |
sakthipriya | 0:58b0c597a58c | 75 | } |
sakthipriya | 0:58b0c597a58c | 76 | |
sakthipriya | 0:58b0c597a58c | 77 | void T_I2C_MASTER_FSLAVE(void const *args) |
sakthipriya | 0:58b0c597a58c | 78 | { |
sakthipriya | 0:58b0c597a58c | 79 | char data_receive; |
sakthipriya | 0:58b0c597a58c | 80 | while(1) |
sakthipriya | 0:58b0c597a58c | 81 | { |
sakthipriya | 0:58b0c597a58c | 82 | Thread::signal_wait(0x1); |
sakthipriya | 0:58b0c597a58c | 83 | FUNC_I2C_MASTER_FSLAVE(&data_receive,1); |
sakthipriya | 0:58b0c597a58c | 84 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
sakthipriya | 0:58b0c597a58c | 85 | i2c_data_r->data = data_receive; |
sakthipriya | 0:58b0c597a58c | 86 | i2c_data_r->length = 1; |
sakthipriya | 0:58b0c597a58c | 87 | i2c_data_receive.put(i2c_data_r); |
sakthipriya | 0:58b0c597a58c | 88 | printf("\n Data received from CDMS is %c\n\r",data_receive); |
sakthipriya | 0:58b0c597a58c | 89 | i2c_data_receive.free(i2c_data_r); |
sakthipriya | 0:58b0c597a58c | 90 | } |
sakthipriya | 0:58b0c597a58c | 91 | } |
sakthipriya | 0:58b0c597a58c | 92 | |
sakthipriya | 0:58b0c597a58c | 93 | void FUNC_INT() |
sakthipriya | 0:58b0c597a58c | 94 | { |
sakthipriya | 0:58b0c597a58c | 95 | |
sakthipriya | 0:58b0c597a58c | 96 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 0:58b0c597a58c | 97 | |
sakthipriya | 0:58b0c597a58c | 98 | } |
sakthipriya | 0:58b0c597a58c | 99 | |
sakthipriya | 0:58b0c597a58c | 100 | |
sakthipriya | 0:58b0c597a58c | 101 | |
sakthipriya | 0:58b0c597a58c | 102 | |
sakthipriya | 0:58b0c597a58c | 103 | int main() |
sakthipriya | 0:58b0c597a58c | 104 | { |
sakthipriya | 0:58b0c597a58c | 105 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
sakthipriya | 0:58b0c597a58c | 106 | |
sakthipriya | 0:58b0c597a58c | 107 | data_ready.rise(&FUNC_INT); |
sakthipriya | 0:58b0c597a58c | 108 | printf("\nstarted master\n"); |
sakthipriya | 0:58b0c597a58c | 109 | bool write2slave; |
sakthipriya | 0:58b0c597a58c | 110 | char *writedata = new char; |
sakthipriya | 0:58b0c597a58c | 111 | |
sakthipriya | 0:58b0c597a58c | 112 | |
sakthipriya | 0:58b0c597a58c | 113 | bool master_status_write = true; |
sakthipriya | 0:58b0c597a58c | 114 | while(1) |
sakthipriya | 0:58b0c597a58c | 115 | { write2slave=true; |
sakthipriya | 0:58b0c597a58c | 116 | interrupt = 0; |
sakthipriya | 0:58b0c597a58c | 117 | |
sakthipriya | 0:58b0c597a58c | 118 | *writedata = pc.getc(); |
sakthipriya | 0:58b0c597a58c | 119 | |
sakthipriya | 0:58b0c597a58c | 120 | interrupt = 1; |
sakthipriya | 0:58b0c597a58c | 121 | master.frequency(100000); //set clock frequency |
sakthipriya | 0:58b0c597a58c | 122 | while(write2slave) |
sakthipriya | 0:58b0c597a58c | 123 | { |
sakthipriya | 0:58b0c597a58c | 124 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:58b0c597a58c | 125 | master.start(); //initiating the data transfer |
sakthipriya | 0:58b0c597a58c | 126 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:58b0c597a58c | 127 | if(master_status_write==0) |
sakthipriya | 0:58b0c597a58c | 128 | { |
sakthipriya | 0:58b0c597a58c | 129 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:58b0c597a58c | 130 | write2slave=false; |
sakthipriya | 0:58b0c597a58c | 131 | } |
sakthipriya | 0:58b0c597a58c | 132 | } |
sakthipriya | 0:58b0c597a58c | 133 | |
sakthipriya | 0:58b0c597a58c | 134 | } |
sakthipriya | 0:58b0c597a58c | 135 | } |