Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed-rtos mbed
Fork of CDMS_BAE by
Diff: i2c.cpp
- Revision:
- 7:60e940b7a36b
- Child:
- 8:4acac3bf5e9b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/i2c.cpp Thu Feb 05 23:19:46 2015 +0000
@@ -0,0 +1,157 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "i2c.h"
+DigitalOut interrupt(D9);
+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;
+Timer t;
+Timer t1;
+Timer t2;
+Timer t3;
+
+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)
+{
+
+
+ bool ack0 =true;
+
+ interrupt = 1;
+ t1.start();
+ //wait_ms(20);
+ ack0 = master.read(addr|1,data,length);
+ t1.stop();
+
+
+ if(!ack0)
+ {
+ printf("\n master has read %s from slave\n\r",data);
+
+ }
+ //master.stop();
+
+ printf("\n%d\n\r",t1.read_us());
+ t1.reset();
+ storedata(data);
+ interrupt=0;
+
+}
+
+typedef struct {
+ char Voltage[9];
+ char Current[5];
+ char Temperature[2];
+ char PanelTemperature[3];//read by the 4 thermistors on solar panels
+ char BatteryTemperature; //to be populated
+ char faultpoll; //polled faults
+ char faultir; //interrupted faults
+ char power_mode; //power modes
+ char AngularSpeed[3];
+ char Bnewvalue[3];
+
+ //float magnetometer,gyro=>to be addes
+} hk_data;
+hk_data decode_data;
+
+/*void TC_DECODE(char *data_hk) //getting the structure back from hk data sent by bae
+{
+ for(int i=0;i<=7;i++)
+ {
+ decode_data.Voltage[i] = data_hk[i];
+ decode_data.Voltage[8] = '\0';
+ }
+ for(int i=0;i<=3;i++)
+ {
+ decode_data.Current[i] = data_hk[8+i];
+ decode_data.Current[4] = '\0';
+ }
+ decode_data.Temperature[0] = data_hk[12];
+ decode_data.Temperature[1] = '\0';
+ for(int i=0;i<=1;i++)
+ {
+ decode_data.PanelTemperature[i] = data_hk[13+i];
+ decode_data.PanelTemperature[2] = '\0';
+ }
+ decode_data.BatteryTemperature = data_hk[15];
+ decode_data.faultpoll = data_hk[16];
+ decode_data.faultir = data_hk[17];
+ decode_data.power_mode = data_hk[18];
+ for(int i=0;i<=1;i++)
+ {
+ decode_data.AngularSpeed[i] = data_hk[19+i];
+ decode_data.AngularSpeed[2] = '\0';
+ }
+ for(int i=0;i<=1;i++)
+ {
+ decode_data.Bnewvalue[i] = data_hk[21+i];
+ decode_data.Bnewvalue[2] = '\0';
+ }
+ printf("\n voltage %s\n\r",decode_data.Voltage);
+ printf("\n current %s\n\r",decode_data.Current);
+ printf("\n faultpoll %c\n\r",decode_data.faultpoll);
+}*/
+
+
+
+char writedata;
+bool write2slave;
+bool master_status_write;
+void FUNC_MASTER_WRITE()
+{ //wait(1);
+ write2slave=true;
+
+
+
+ char data = pc.getc();
+ interrupt = 1;
+ t.start();
+ t3.start();
+ wait_ms(20);
+ 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);
+ 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;
+ t.stop();
+ //t3.start();
+ master_status_write = (bool) master.write(addr|0x00,&writedata,1);
+ t3.stop();
+ if(master_status_write==0)
+ {
+ printf("master has written %c to slave\n\r",writedata);
+ write2slave=false;
+ }
+ i2c_data_send.free(i2c_data_s);
+ printf("\n%d\n",t.read_us());
+ t.reset();
+ printf("\n%d\n",t3.read_us());
+ t3.reset();
+ }
+ interrupt = 0;
+}
+
+
+
+
\ No newline at end of file
