![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
hkdata send using string working
Fork of greenisthemaster by
main.cpp@5:29ec04694833, 2014-12-16 (annotated)
- Committer:
- raizel_varun
- Date:
- Tue Dec 16 11:05:44 2014 +0000
- Revision:
- 5:29ec04694833
- Parent:
- 4:cf8d2c7b65ad
BAE_vr 2.1.0 [hk_data in string] 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 | { |
raizel_varun | 5:29ec04694833 | 14 | char data[35]; // 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 | { |
greenroshks | 3:e1dc841fca6d | 67 | printf("\n master has read %s 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 | { |
raizel_varun | 5:29ec04694833 | 79 | char data_receive[35]; |
sakthipriya | 0:58b0c597a58c | 80 | while(1) |
sakthipriya | 0:58b0c597a58c | 81 | { |
sakthipriya | 0:58b0c597a58c | 82 | Thread::signal_wait(0x1); |
raizel_varun | 5:29ec04694833 | 83 | FUNC_I2C_MASTER_FSLAVE(data_receive,35); |
sakthipriya | 0:58b0c597a58c | 84 | i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
greenroshks | 3:e1dc841fca6d | 85 | strcpy(i2c_data_r->data , data_receive); |
raizel_varun | 5:29ec04694833 | 86 | i2c_data_r->length = 35; |
sakthipriya | 0:58b0c597a58c | 87 | i2c_data_receive.put(i2c_data_r); |
greenroshks | 3:e1dc841fca6d | 88 | printf("\n Data received from slave is %s\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 | |
raizel_varun | 5:29ec04694833 | 100 | char writedata[35]; |
raizel_varun | 4:cf8d2c7b65ad | 101 | bool write2slave; |
raizel_varun | 4:cf8d2c7b65ad | 102 | bool master_status_write; |
sakthipriya | 1:e5c939440756 | 103 | void FUNC_MASTER_WRITE() |
raizel_varun | 4:cf8d2c7b65ad | 104 | { //wait(1); |
raizel_varun | 4:cf8d2c7b65ad | 105 | write2slave=true; |
sakthipriya | 0:58b0c597a58c | 106 | |
raizel_varun | 4:cf8d2c7b65ad | 107 | interrupt = 1; |
sakthipriya | 1:e5c939440756 | 108 | |
raizel_varun | 5:29ec04694833 | 109 | char data[35]; |
raizel_varun | 4:cf8d2c7b65ad | 110 | strcpy(data,"sakthi"); |
raizel_varun | 4:cf8d2c7b65ad | 111 | strcat(data,"priya"); |
sakthipriya | 1:e5c939440756 | 112 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
greenroshks | 3:e1dc841fca6d | 113 | strcpy(i2c_data_s->data ,data); |
raizel_varun | 5:29ec04694833 | 114 | i2c_data_s->length = 35; |
sakthipriya | 1:e5c939440756 | 115 | i2c_data_send.put(i2c_data_s); |
raizel_varun | 4:cf8d2c7b65ad | 116 | master_status_write = true; |
raizel_varun | 4:cf8d2c7b65ad | 117 | |
raizel_varun | 4:cf8d2c7b65ad | 118 | // interrupt = 1; |
raizel_varun | 4:cf8d2c7b65ad | 119 | |
sakthipriya | 1:e5c939440756 | 120 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 1:e5c939440756 | 121 | if (evt.status == osEventMail) |
sakthipriya | 1:e5c939440756 | 122 | { |
sakthipriya | 1:e5c939440756 | 123 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
greenroshks | 3:e1dc841fca6d | 124 | strcpy(writedata ,i2c_data_s -> data); |
sakthipriya | 0:58b0c597a58c | 125 | master.frequency(100000); //set clock frequency |
sakthipriya | 0:58b0c597a58c | 126 | while(write2slave) |
sakthipriya | 1:e5c939440756 | 127 | { |
sakthipriya | 0:58b0c597a58c | 128 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:58b0c597a58c | 129 | master.start(); //initiating the data transfer |
raizel_varun | 5:29ec04694833 | 130 | master_status_write = (bool) master.write(addr|0x00,writedata,35); |
sakthipriya | 0:58b0c597a58c | 131 | if(master_status_write==0) |
sakthipriya | 0:58b0c597a58c | 132 | { |
greenroshks | 3:e1dc841fca6d | 133 | printf("master has written %s to slave\n\r",writedata); |
sakthipriya | 0:58b0c597a58c | 134 | write2slave=false; |
sakthipriya | 0:58b0c597a58c | 135 | } |
sakthipriya | 2:e80af3c366aa | 136 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 1:e5c939440756 | 137 | } |
sakthipriya | 0:58b0c597a58c | 138 | } |
raizel_varun | 4:cf8d2c7b65ad | 139 | interrupt = 0; |
sakthipriya | 1:e5c939440756 | 140 | } |
sakthipriya | 1:e5c939440756 | 141 | |
sakthipriya | 1:e5c939440756 | 142 | |
sakthipriya | 1:e5c939440756 | 143 | |
sakthipriya | 1:e5c939440756 | 144 | int main() |
sakthipriya | 1:e5c939440756 | 145 | { |
sakthipriya | 1:e5c939440756 | 146 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
sakthipriya | 1:e5c939440756 | 147 | data_ready.rise(&FUNC_INT); |
sakthipriya | 1:e5c939440756 | 148 | printf("\nstarted master\n"); |
sakthipriya | 1:e5c939440756 | 149 | while(1) |
raizel_varun | 4:cf8d2c7b65ad | 150 | { Thread::wait(8000); |
raizel_varun | 4:cf8d2c7b65ad | 151 | //interrupt = 1; |
raizel_varun | 4:cf8d2c7b65ad | 152 | |
raizel_varun | 4:cf8d2c7b65ad | 153 | FUNC_MASTER_WRITE(); |
raizel_varun | 4:cf8d2c7b65ad | 154 | //interrupt = 0; |
greenroshks | 3:e1dc841fca6d | 155 | ; |
sakthipriya | 0:58b0c597a58c | 156 | } |
sakthipriya | 0:58b0c597a58c | 157 | } |