![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
check commmit messgae
Fork of master_working_1_2 by
main.cpp
- Committer:
- greenroshks
- Date:
- 2014-12-19
- Revision:
- 3:7a895e0fdd45
- Parent:
- 2:e80af3c366aa
File content as of revision 3:7a895e0fdd45:
#include "mbed.h" #include "rtos.h" const int addr = 0x20; //slave address I2C master (D14,D15); //configure pins p27,p28 as I2C master Serial pc (USBTX,USBRX); DigitalOut interrupt(D9); InterruptIn data_ready(D10); int reset; typedef struct { char data; // To avoid dynamic memory allocation int length; }i2c_data; Mail<i2c_data,16> i2c_data_receive; Mail<i2c_data,16> i2c_data_send; Thread * ptr_t_i2c; void FUNC_I2C_MASTER_FSLAVE(char * data,int length) { //printf("press backspace for master to start \n\r"); //while(1){ //interrupt = 0; // if(pc.getc()=='z'){ wait(0.5); interrupt = 1; printf("\nMaster executed\n"); bool ack0 =true; bool loopvariable0 =true; //char *data = new char; //*data = 'b'; master.frequency(100000); //set clock frequency //master.start(); /*while(write2slave) { *writedata = pc.getc(); wait(0.5); printf("master clk freq setup and addressing slave\n\r"); master.start(); //initiating the data transfer master_status_write = (bool) master.write(addr|0x00,writedata,1); if(master_status_write==0) { printf("master has written %c to slave\n\r",*writedata); write2slave=false; } }*/ //wait(0.5); while(ack0) { //printf("2 master clk freq setup and addressing slave\n\r"); master.start(); //initiating the data transfer ack0 = master.read(addr,data,length); } if(!ack0 && data_ready ==1) { printf("\n master has read %c from slave\n\r",*data); loopvariable0=false; } //master.stop(); //printf("done\n\r"); //delete data; interrupt=0; } void T_I2C_MASTER_FSLAVE(void const *args) { char data_receive; while(1) { Thread::signal_wait(0x1); FUNC_I2C_MASTER_FSLAVE(&data_receive,1); i2c_data * i2c_data_r = i2c_data_receive.alloc(); i2c_data_r->data = data_receive; i2c_data_r->length = 1; i2c_data_receive.put(i2c_data_r); printf("\n Data received from slave is %c\n\r",data_receive); i2c_data_receive.free(i2c_data_r); } } void FUNC_INT() { ptr_t_i2c->signal_set(0x1); } char *writedata = new char; void FUNC_MASTER_WRITE() { bool write2slave=true; interrupt = 0; char data = pc.getc(); i2c_data * i2c_data_s = i2c_data_send.alloc(); i2c_data_s->data = data; i2c_data_s->length = 1; i2c_data_send.put(i2c_data_s); bool master_status_write = true; interrupt = 1; osEvent evt = i2c_data_send.get(); if (evt.status == osEventMail) { i2c_data *i2c_data_s = (i2c_data*)evt.value.p; *writedata = i2c_data_s -> data; master.frequency(100000); //set clock frequency while(write2slave && interrupt ==1) { printf("master clk freq setup and addressing slave\n\r"); master.start(); //initiating the data transfer master_status_write = (bool) master.write(addr|0x00,writedata,1); if(master_status_write==0) { printf("master has written %c to slave\n\r",*writedata); write2slave=false; } i2c_data_send.free(i2c_data_s); } } } int main() { ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); data_ready.rise(&FUNC_INT); printf("\nstarted master\n"); while(1) { FUNC_MASTER_WRITE(); } }