![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
vr1.1
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of CDMS_RTOS_v1_1 by
i2c.cpp@0:d6e3d1e21484, 2015-06-15 (annotated)
- Committer:
- cholletisaik777
- Date:
- Mon Jun 15 12:16:59 2015 +0000
- Revision:
- 0:d6e3d1e21484
- Child:
- 3:0c3f7c3fabc9
CDMS-RTOSv0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cholletisaik777 | 0:d6e3d1e21484 | 1 | #include "mbed.h" |
cholletisaik777 | 0:d6e3d1e21484 | 2 | //#include "pin_config.h" |
cholletisaik777 | 0:d6e3d1e21484 | 3 | #include "i2c.h" |
cholletisaik777 | 0:d6e3d1e21484 | 4 | |
cholletisaik777 | 0:d6e3d1e21484 | 5 | Serial gc(USBTX,USBRX); |
cholletisaik777 | 0:d6e3d1e21484 | 6 | InterruptIn irpt_4m_slv(D12); //I2c interrupt from CDMS |
cholletisaik777 | 0:d6e3d1e21484 | 7 | DigitalOut irpt_2_slv(D11); //I2C interrupt to CDMS |
cholletisaik777 | 0:d6e3d1e21484 | 8 | I2C master(PTE0,PTE1); |
cholletisaik777 | 0:d6e3d1e21484 | 9 | |
cholletisaik777 | 0:d6e3d1e21484 | 10 | |
cholletisaik777 | 0:d6e3d1e21484 | 11 | |
cholletisaik777 | 0:d6e3d1e21484 | 12 | const int addr = 0x20; //slave address |
cholletisaik777 | 0:d6e3d1e21484 | 13 | Timer t; |
cholletisaik777 | 0:d6e3d1e21484 | 14 | |
cholletisaik777 | 0:d6e3d1e21484 | 15 | |
cholletisaik777 | 0:d6e3d1e21484 | 16 | void FCTN_I2C_READ(char *data, int length ) |
cholletisaik777 | 0:d6e3d1e21484 | 17 | { |
cholletisaik777 | 0:d6e3d1e21484 | 18 | bool read_ack = true; |
cholletisaik777 | 0:d6e3d1e21484 | 19 | irpt_2_slv = 0; |
cholletisaik777 | 0:d6e3d1e21484 | 20 | t.start(); |
cholletisaik777 | 0:d6e3d1e21484 | 21 | read_ack=master.read(addr|1,data,length); |
cholletisaik777 | 0:d6e3d1e21484 | 22 | t.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 23 | if(read_ack == 0) |
cholletisaik777 | 0:d6e3d1e21484 | 24 | printf("\r\n\rData received from BAE is %s \r\n",data); |
cholletisaik777 | 0:d6e3d1e21484 | 25 | else |
cholletisaik777 | 0:d6e3d1e21484 | 26 | { |
cholletisaik777 | 0:d6e3d1e21484 | 27 | printf("\r\n \r data not received \r\n"); |
cholletisaik777 | 0:d6e3d1e21484 | 28 | master.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 29 | } |
cholletisaik777 | 0:d6e3d1e21484 | 30 | irpt_2_slv = 1; |
cholletisaik777 | 0:d6e3d1e21484 | 31 | printf("\r\n\r %d \r\n",t.read_us()); |
cholletisaik777 | 0:d6e3d1e21484 | 32 | t.reset(); |
cholletisaik777 | 0:d6e3d1e21484 | 33 | } |
cholletisaik777 | 0:d6e3d1e21484 | 34 | |
cholletisaik777 | 0:d6e3d1e21484 | 35 | void FCTN_I2C_WRITE(char *data,int length) |
cholletisaik777 | 0:d6e3d1e21484 | 36 | { |
cholletisaik777 | 0:d6e3d1e21484 | 37 | bool write_ack = true; |
cholletisaik777 | 0:d6e3d1e21484 | 38 | irpt_2_slv = 0; |
cholletisaik777 | 0:d6e3d1e21484 | 39 | t.start(); |
cholletisaik777 | 0:d6e3d1e21484 | 40 | write_ack=master.write(addr|0x00,data,length); |
cholletisaik777 | 0:d6e3d1e21484 | 41 | t.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 42 | if(write_ack == 0) |
cholletisaik777 | 0:d6e3d1e21484 | 43 | printf("\r\n\rData sent to BAE is %s \r\n",data); |
cholletisaik777 | 0:d6e3d1e21484 | 44 | else |
cholletisaik777 | 0:d6e3d1e21484 | 45 | { |
cholletisaik777 | 0:d6e3d1e21484 | 46 | printf("\r\n\r data not sent \r\n"); |
cholletisaik777 | 0:d6e3d1e21484 | 47 | master.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 48 | } |
cholletisaik777 | 0:d6e3d1e21484 | 49 | irpt_2_slv = 1; |
cholletisaik777 | 0:d6e3d1e21484 | 50 | printf("\r\n\r %d \r\n",t.read_us()); |
cholletisaik777 | 0:d6e3d1e21484 | 51 | t.reset(); |
cholletisaik777 | 0:d6e3d1e21484 | 52 | } |
cholletisaik777 | 0:d6e3d1e21484 | 53 | |
cholletisaik777 | 0:d6e3d1e21484 | 54 | bool check = 1; |
cholletisaik777 | 0:d6e3d1e21484 | 55 | void FCTN_MASTER_I2C(char i2c_data_type , char* i2c_data) |
cholletisaik777 | 0:d6e3d1e21484 | 56 | { |
cholletisaik777 | 0:d6e3d1e21484 | 57 | FCTN_I2C_WRITE(&i2c_data_type,1); |
cholletisaik777 | 0:d6e3d1e21484 | 58 | wait_ms(10); |
cholletisaik777 | 0:d6e3d1e21484 | 59 | switch(i2c_data_type) |
cholletisaik777 | 0:d6e3d1e21484 | 60 | { |
cholletisaik777 | 0:d6e3d1e21484 | 61 | case 's': |
cholletisaik777 | 0:d6e3d1e21484 | 62 | FCTN_I2C_WRITE(i2c_data,11); |
cholletisaik777 | 0:d6e3d1e21484 | 63 | break; |
cholletisaik777 | 0:d6e3d1e21484 | 64 | |
cholletisaik777 | 0:d6e3d1e21484 | 65 | case 'l': |
cholletisaik777 | 0:d6e3d1e21484 | 66 | FCTN_I2C_WRITE(i2c_data,135); |
cholletisaik777 | 0:d6e3d1e21484 | 67 | break; |
cholletisaik777 | 0:d6e3d1e21484 | 68 | |
cholletisaik777 | 0:d6e3d1e21484 | 69 | case 'h': |
cholletisaik777 | 0:d6e3d1e21484 | 70 | FCTN_I2C_READ(i2c_data,25); |
cholletisaik777 | 0:d6e3d1e21484 | 71 | break; |
cholletisaik777 | 0:d6e3d1e21484 | 72 | |
cholletisaik777 | 0:d6e3d1e21484 | 73 | default: |
cholletisaik777 | 0:d6e3d1e21484 | 74 | printf("\r\n\r invalid command \r\n"); |
cholletisaik777 | 0:d6e3d1e21484 | 75 | } |
cholletisaik777 | 0:d6e3d1e21484 | 76 | } |