![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
widout tc reset working
Fork of CDMS_tc by
main.cpp@1:24c54d213671, 2014-12-20 (annotated)
- Committer:
- sakthipriya
- Date:
- Sat Dec 20 06:05:33 2014 +0000
- Revision:
- 1:24c54d213671
- Parent:
- 0:252ce694a71e
master working without tc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:252ce694a71e | 1 | #include "mbed.h" |
sakthipriya | 0:252ce694a71e | 2 | #include "rtos.h" |
sakthipriya | 0:252ce694a71e | 3 | |
sakthipriya | 0:252ce694a71e | 4 | const int addr = 0x20; //slave address |
sakthipriya | 0:252ce694a71e | 5 | I2C master (D14,D15); //configure pins p27,p28 as I2C master |
sakthipriya | 0:252ce694a71e | 6 | Serial pc (USBTX,USBRX); |
sakthipriya | 0:252ce694a71e | 7 | DigitalOut interrupt(D9); |
sakthipriya | 0:252ce694a71e | 8 | InterruptIn data_ready(D10); |
sakthipriya | 0:252ce694a71e | 9 | |
sakthipriya | 0:252ce694a71e | 10 | int reset; |
sakthipriya | 0:252ce694a71e | 11 | |
sakthipriya | 0:252ce694a71e | 12 | typedef struct |
sakthipriya | 0:252ce694a71e | 13 | { |
sakthipriya | 0:252ce694a71e | 14 | char data; // To avoid dynamic memory allocation |
sakthipriya | 0:252ce694a71e | 15 | int length; |
sakthipriya | 0:252ce694a71e | 16 | }i2c_data; |
sakthipriya | 0:252ce694a71e | 17 | |
sakthipriya | 0:252ce694a71e | 18 | //Mail<i2c_data,16> i2c_data_receive; |
sakthipriya | 0:252ce694a71e | 19 | Mail<i2c_data,16> i2c_data_send; |
sakthipriya | 0:252ce694a71e | 20 | |
sakthipriya | 0:252ce694a71e | 21 | Thread * ptr_t_i2c; |
sakthipriya | 0:252ce694a71e | 22 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 0:252ce694a71e | 23 | { |
sakthipriya | 0:252ce694a71e | 24 | //printf("press backspace for master to start \n\r"); |
sakthipriya | 0:252ce694a71e | 25 | //while(1){ |
sakthipriya | 0:252ce694a71e | 26 | //interrupt = 0; |
sakthipriya | 0:252ce694a71e | 27 | // if(pc.getc()=='z'){ |
sakthipriya | 1:24c54d213671 | 28 | //wait(0.5); |
sakthipriya | 0:252ce694a71e | 29 | interrupt = 1; |
sakthipriya | 0:252ce694a71e | 30 | printf("\nMaster executed\n"); |
sakthipriya | 0:252ce694a71e | 31 | |
sakthipriya | 0:252ce694a71e | 32 | |
sakthipriya | 0:252ce694a71e | 33 | bool ack0 =true; |
sakthipriya | 0:252ce694a71e | 34 | bool loopvariable0 =true; |
sakthipriya | 0:252ce694a71e | 35 | //char *data = new char; |
sakthipriya | 0:252ce694a71e | 36 | //*data = 'b'; |
sakthipriya | 0:252ce694a71e | 37 | |
sakthipriya | 0:252ce694a71e | 38 | |
sakthipriya | 0:252ce694a71e | 39 | |
sakthipriya | 0:252ce694a71e | 40 | master.frequency(100000); //set clock frequency |
sakthipriya | 0:252ce694a71e | 41 | //master.start(); |
sakthipriya | 0:252ce694a71e | 42 | /*while(write2slave) |
sakthipriya | 0:252ce694a71e | 43 | { *writedata = pc.getc(); |
sakthipriya | 0:252ce694a71e | 44 | wait(0.5); |
sakthipriya | 0:252ce694a71e | 45 | |
sakthipriya | 0:252ce694a71e | 46 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:252ce694a71e | 47 | master.start(); //initiating the data transfer |
sakthipriya | 0:252ce694a71e | 48 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:252ce694a71e | 49 | if(master_status_write==0) |
sakthipriya | 0:252ce694a71e | 50 | { |
sakthipriya | 0:252ce694a71e | 51 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:252ce694a71e | 52 | write2slave=false; |
sakthipriya | 0:252ce694a71e | 53 | } |
sakthipriya | 0:252ce694a71e | 54 | |
sakthipriya | 0:252ce694a71e | 55 | |
sakthipriya | 0:252ce694a71e | 56 | }*/ |
sakthipriya | 0:252ce694a71e | 57 | |
sakthipriya | 1:24c54d213671 | 58 | while(ack0 && data_ready ==1 ) |
sakthipriya | 0:252ce694a71e | 59 | { |
sakthipriya | 0:252ce694a71e | 60 | //printf("2 master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:252ce694a71e | 61 | |
sakthipriya | 0:252ce694a71e | 62 | master.start(); //initiating the data transfer |
sakthipriya | 0:252ce694a71e | 63 | ack0 = master.read(addr,data,length); |
sakthipriya | 0:252ce694a71e | 64 | } |
sakthipriya | 0:252ce694a71e | 65 | if(!ack0) |
sakthipriya | 0:252ce694a71e | 66 | { |
sakthipriya | 0:252ce694a71e | 67 | printf("\n master has read %s from slave\n\r",data); |
sakthipriya | 0:252ce694a71e | 68 | loopvariable0=false; |
sakthipriya | 0:252ce694a71e | 69 | } |
sakthipriya | 0:252ce694a71e | 70 | //master.stop(); |
sakthipriya | 0:252ce694a71e | 71 | |
sakthipriya | 0:252ce694a71e | 72 | //printf("done\n\r"); |
sakthipriya | 0:252ce694a71e | 73 | //delete data; |
sakthipriya | 0:252ce694a71e | 74 | interrupt=0; |
sakthipriya | 1:24c54d213671 | 75 | printf("\n blahblah\n\r"); |
sakthipriya | 0:252ce694a71e | 76 | } |
sakthipriya | 0:252ce694a71e | 77 | |
sakthipriya | 0:252ce694a71e | 78 | void T_I2C_MASTER_FSLAVE(void const *args) |
sakthipriya | 0:252ce694a71e | 79 | { |
sakthipriya | 0:252ce694a71e | 80 | char data_receive[25]; |
sakthipriya | 0:252ce694a71e | 81 | while(1) |
sakthipriya | 0:252ce694a71e | 82 | { |
sakthipriya | 0:252ce694a71e | 83 | Thread::signal_wait(0x1); |
sakthipriya | 1:24c54d213671 | 84 | |
sakthipriya | 1:24c54d213671 | 85 | |
sakthipriya | 0:252ce694a71e | 86 | FUNC_I2C_MASTER_FSLAVE(data_receive,25); |
sakthipriya | 0:252ce694a71e | 87 | /*i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
sakthipriya | 0:252ce694a71e | 88 | strcpy(i2c_data_r->data , data_receive); |
sakthipriya | 0:252ce694a71e | 89 | i2c_data_r->length = 25; |
sakthipriya | 0:252ce694a71e | 90 | i2c_data_receive.put(i2c_data_r);*/ |
sakthipriya | 0:252ce694a71e | 91 | printf("\n Data received from slave is %s\n\r",data_receive); |
sakthipriya | 1:24c54d213671 | 92 | |
sakthipriya | 0:252ce694a71e | 93 | } |
sakthipriya | 0:252ce694a71e | 94 | } |
sakthipriya | 0:252ce694a71e | 95 | |
sakthipriya | 0:252ce694a71e | 96 | void FUNC_INT() |
sakthipriya | 0:252ce694a71e | 97 | { |
sakthipriya | 0:252ce694a71e | 98 | |
sakthipriya | 0:252ce694a71e | 99 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 0:252ce694a71e | 100 | |
sakthipriya | 0:252ce694a71e | 101 | } |
sakthipriya | 0:252ce694a71e | 102 | |
sakthipriya | 0:252ce694a71e | 103 | char writedata; |
sakthipriya | 0:252ce694a71e | 104 | bool write2slave; |
sakthipriya | 0:252ce694a71e | 105 | bool master_status_write; |
sakthipriya | 0:252ce694a71e | 106 | void FUNC_MASTER_WRITE() |
sakthipriya | 0:252ce694a71e | 107 | { //wait(1); |
sakthipriya | 0:252ce694a71e | 108 | write2slave=true; |
sakthipriya | 0:252ce694a71e | 109 | |
sakthipriya | 0:252ce694a71e | 110 | |
sakthipriya | 0:252ce694a71e | 111 | |
sakthipriya | 0:252ce694a71e | 112 | char data = pc.getc(); |
sakthipriya | 0:252ce694a71e | 113 | interrupt = 1; |
sakthipriya | 0:252ce694a71e | 114 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 0:252ce694a71e | 115 | i2c_data_s->data = data; |
sakthipriya | 0:252ce694a71e | 116 | i2c_data_s->length = 1; |
sakthipriya | 0:252ce694a71e | 117 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 0:252ce694a71e | 118 | master_status_write = true; |
sakthipriya | 0:252ce694a71e | 119 | |
sakthipriya | 0:252ce694a71e | 120 | // interrupt = 1; |
sakthipriya | 0:252ce694a71e | 121 | |
sakthipriya | 0:252ce694a71e | 122 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 0:252ce694a71e | 123 | if (evt.status == osEventMail) |
sakthipriya | 0:252ce694a71e | 124 | { |
sakthipriya | 0:252ce694a71e | 125 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
sakthipriya | 0:252ce694a71e | 126 | writedata = i2c_data_s -> data; |
sakthipriya | 0:252ce694a71e | 127 | master.frequency(100000); //set clock frequency |
sakthipriya | 1:24c54d213671 | 128 | while(write2slave && interrupt ==1 ) |
sakthipriya | 0:252ce694a71e | 129 | { |
sakthipriya | 0:252ce694a71e | 130 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:252ce694a71e | 131 | master.start(); //initiating the data transfer |
sakthipriya | 0:252ce694a71e | 132 | master_status_write = (bool) master.write(addr|0x00,&writedata,1); |
sakthipriya | 0:252ce694a71e | 133 | if(master_status_write==0) |
sakthipriya | 0:252ce694a71e | 134 | { |
sakthipriya | 0:252ce694a71e | 135 | printf("master has written %c to slave\n\r",writedata); |
sakthipriya | 0:252ce694a71e | 136 | write2slave=false; |
sakthipriya | 0:252ce694a71e | 137 | } |
sakthipriya | 0:252ce694a71e | 138 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 0:252ce694a71e | 139 | } |
sakthipriya | 0:252ce694a71e | 140 | } |
sakthipriya | 0:252ce694a71e | 141 | interrupt = 0; |
sakthipriya | 0:252ce694a71e | 142 | } |
sakthipriya | 0:252ce694a71e | 143 | |
sakthipriya | 0:252ce694a71e | 144 | |
sakthipriya | 0:252ce694a71e | 145 | |
sakthipriya | 0:252ce694a71e | 146 | int main() |
sakthipriya | 0:252ce694a71e | 147 | { |
sakthipriya | 1:24c54d213671 | 148 | interrupt=0; |
sakthipriya | 0:252ce694a71e | 149 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
sakthipriya | 0:252ce694a71e | 150 | data_ready.rise(&FUNC_INT); |
sakthipriya | 0:252ce694a71e | 151 | printf("\nstarted master\n"); |
sakthipriya | 0:252ce694a71e | 152 | while(1) |
sakthipriya | 0:252ce694a71e | 153 | { //Thread::wait(9000); |
sakthipriya | 0:252ce694a71e | 154 | //interrupt = 1; |
sakthipriya | 0:252ce694a71e | 155 | |
sakthipriya | 0:252ce694a71e | 156 | FUNC_MASTER_WRITE(); |
sakthipriya | 0:252ce694a71e | 157 | //interrupt = 0; |
sakthipriya | 0:252ce694a71e | 158 | ; |
sakthipriya | 0:252ce694a71e | 159 | } |
sakthipriya | 0:252ce694a71e | 160 | } |