master working version 1.2

Dependencies:   mbed-rtos mbed

Fork of master_working_1_1 by sakthi priya amirtharaj

Committer:
sakthipriya
Date:
Mon Dec 15 05:51:25 2014 +0000
Revision:
2:e80af3c366aa
Parent:
1:e5c939440756
free queue

Who changed what in which revision?

UserRevisionLine numberNew 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 1:e5c939440756 88 printf("\n Data received from slave 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 1:e5c939440756 100 char *writedata = new char;
sakthipriya 1:e5c939440756 101 void FUNC_MASTER_WRITE()
sakthipriya 1:e5c939440756 102 {
sakthipriya 1:e5c939440756 103 bool write2slave=true;
sakthipriya 0:58b0c597a58c 104
sakthipriya 0:58b0c597a58c 105 interrupt = 0;
sakthipriya 1:e5c939440756 106
sakthipriya 1:e5c939440756 107 char data = pc.getc();
sakthipriya 1:e5c939440756 108 i2c_data * i2c_data_s = i2c_data_send.alloc();
sakthipriya 1:e5c939440756 109 i2c_data_s->data = data;
sakthipriya 1:e5c939440756 110 i2c_data_s->length = 1;
sakthipriya 1:e5c939440756 111 i2c_data_send.put(i2c_data_s);
sakthipriya 1:e5c939440756 112 bool master_status_write = true;
sakthipriya 0:58b0c597a58c 113 interrupt = 1;
sakthipriya 1:e5c939440756 114 osEvent evt = i2c_data_send.get();
sakthipriya 1:e5c939440756 115 if (evt.status == osEventMail)
sakthipriya 1:e5c939440756 116 {
sakthipriya 1:e5c939440756 117 i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
sakthipriya 1:e5c939440756 118 *writedata = i2c_data_s -> data;
sakthipriya 0:58b0c597a58c 119 master.frequency(100000); //set clock frequency
sakthipriya 0:58b0c597a58c 120 while(write2slave)
sakthipriya 1:e5c939440756 121 {
sakthipriya 0:58b0c597a58c 122 printf("master clk freq setup and addressing slave\n\r");
sakthipriya 0:58b0c597a58c 123 master.start(); //initiating the data transfer
sakthipriya 0:58b0c597a58c 124 master_status_write = (bool) master.write(addr|0x00,writedata,1);
sakthipriya 0:58b0c597a58c 125 if(master_status_write==0)
sakthipriya 0:58b0c597a58c 126 {
sakthipriya 0:58b0c597a58c 127 printf("master has written %c to slave\n\r",*writedata);
sakthipriya 0:58b0c597a58c 128 write2slave=false;
sakthipriya 0:58b0c597a58c 129 }
sakthipriya 2:e80af3c366aa 130 i2c_data_send.free(i2c_data_s);
sakthipriya 1:e5c939440756 131 }
sakthipriya 0:58b0c597a58c 132 }
sakthipriya 1:e5c939440756 133 }
sakthipriya 1:e5c939440756 134
sakthipriya 1:e5c939440756 135
sakthipriya 1:e5c939440756 136
sakthipriya 1:e5c939440756 137 int main()
sakthipriya 1:e5c939440756 138 {
sakthipriya 1:e5c939440756 139 ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE);
sakthipriya 1:e5c939440756 140 data_ready.rise(&FUNC_INT);
sakthipriya 1:e5c939440756 141 printf("\nstarted master\n");
sakthipriya 1:e5c939440756 142 while(1)
sakthipriya 1:e5c939440756 143 {
sakthipriya 1:e5c939440756 144 FUNC_MASTER_WRITE();
sakthipriya 2:e80af3c366aa 145
sakthipriya 0:58b0c597a58c 146 }
sakthipriya 0:58b0c597a58c 147 }