![](/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@15:2c8629da6ec9, 2015-07-16 (annotated)
- Committer:
- cholletisaik777
- Date:
- Thu Jul 16 09:42:13 2015 +0000
- Revision:
- 15:2c8629da6ec9
- Parent:
- 12:cb3ee1ac3638
changed pins
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cholletisaik777 | 0:d6e3d1e21484 | 1 | #include "mbed.h" |
cholletisaik777 | 15:2c8629da6ec9 | 2 | #include "pin_config.h" |
cholletisaik777 | 0:d6e3d1e21484 | 3 | #include "i2c.h" |
cholletisaik777 | 8:607ae92fa6af | 4 | #include "all_funcs.h" |
cholletisaik777 | 0:d6e3d1e21484 | 5 | |
cholletisaik777 | 0:d6e3d1e21484 | 6 | Serial gc(USBTX,USBRX); |
cholletisaik777 | 15:2c8629da6ec9 | 7 | InterruptIn irpt_4m_slv(PIN4); //I2c interrupt from CDMS |
cholletisaik777 | 15:2c8629da6ec9 | 8 | DigitalOut irpt_2_slv(PIN5); //I2C interrupt to CDMS |
cholletisaik777 | 15:2c8629da6ec9 | 9 | I2C master(PIN32,PIN31); |
cholletisaik777 | 0:d6e3d1e21484 | 10 | |
cholletisaik777 | 0:d6e3d1e21484 | 11 | |
cholletisaik777 | 0:d6e3d1e21484 | 12 | |
cholletisaik777 | 0:d6e3d1e21484 | 13 | const int addr = 0x20; //slave address |
cholletisaik777 | 0:d6e3d1e21484 | 14 | Timer t; |
cholletisaik777 | 0:d6e3d1e21484 | 15 | |
cholletisaik777 | 0:d6e3d1e21484 | 16 | |
cholletisaik777 | 12:cb3ee1ac3638 | 17 | int FCTN_I2C_READ(char *data, int length ) |
cholletisaik777 | 0:d6e3d1e21484 | 18 | { |
cholletisaik777 | 0:d6e3d1e21484 | 19 | bool read_ack = true; |
cholletisaik777 | 0:d6e3d1e21484 | 20 | irpt_2_slv = 0; |
cholletisaik777 | 0:d6e3d1e21484 | 21 | t.start(); |
cholletisaik777 | 0:d6e3d1e21484 | 22 | read_ack=master.read(addr|1,data,length); |
cholletisaik777 | 0:d6e3d1e21484 | 23 | t.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 24 | if(read_ack == 0) |
cholletisaik777 | 0:d6e3d1e21484 | 25 | printf("\r\n\rData received from BAE is %s \r\n",data); |
cholletisaik777 | 0:d6e3d1e21484 | 26 | else |
cholletisaik777 | 0:d6e3d1e21484 | 27 | { |
cholletisaik777 | 0:d6e3d1e21484 | 28 | printf("\r\n \r data not received \r\n"); |
cholletisaik777 | 0:d6e3d1e21484 | 29 | master.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 30 | } |
cholletisaik777 | 0:d6e3d1e21484 | 31 | irpt_2_slv = 1; |
cholletisaik777 | 0:d6e3d1e21484 | 32 | printf("\r\n\r %d \r\n",t.read_us()); |
cholletisaik777 | 0:d6e3d1e21484 | 33 | t.reset(); |
cholletisaik777 | 12:cb3ee1ac3638 | 34 | return read_ack; |
cholletisaik777 | 0:d6e3d1e21484 | 35 | } |
cholletisaik777 | 0:d6e3d1e21484 | 36 | |
cholletisaik777 | 12:cb3ee1ac3638 | 37 | int FCTN_I2C_WRITE(char *data,int length) |
cholletisaik777 | 0:d6e3d1e21484 | 38 | { |
cholletisaik777 | 0:d6e3d1e21484 | 39 | bool write_ack = true; |
cholletisaik777 | 0:d6e3d1e21484 | 40 | irpt_2_slv = 0; |
cholletisaik777 | 0:d6e3d1e21484 | 41 | t.start(); |
cholletisaik777 | 0:d6e3d1e21484 | 42 | write_ack=master.write(addr|0x00,data,length); |
cholletisaik777 | 0:d6e3d1e21484 | 43 | t.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 44 | if(write_ack == 0) |
cholletisaik777 | 0:d6e3d1e21484 | 45 | printf("\r\n\rData sent to BAE is %s \r\n",data); |
cholletisaik777 | 0:d6e3d1e21484 | 46 | else |
cholletisaik777 | 0:d6e3d1e21484 | 47 | { |
cholletisaik777 | 0:d6e3d1e21484 | 48 | printf("\r\n\r data not sent \r\n"); |
cholletisaik777 | 0:d6e3d1e21484 | 49 | master.stop(); |
cholletisaik777 | 0:d6e3d1e21484 | 50 | } |
cholletisaik777 | 0:d6e3d1e21484 | 51 | irpt_2_slv = 1; |
cholletisaik777 | 0:d6e3d1e21484 | 52 | printf("\r\n\r %d \r\n",t.read_us()); |
cholletisaik777 | 0:d6e3d1e21484 | 53 | t.reset(); |
cholletisaik777 | 12:cb3ee1ac3638 | 54 | return write_ack; |
cholletisaik777 | 0:d6e3d1e21484 | 55 | } |
cholletisaik777 | 0:d6e3d1e21484 | 56 | |
cholletisaik777 | 0:d6e3d1e21484 | 57 | bool check = 1; |
cholletisaik777 | 12:cb3ee1ac3638 | 58 | int FCTN_MASTER_I2C(char i2c_data_type , char* i2c_data) |
cholletisaik777 | 0:d6e3d1e21484 | 59 | { |
cholletisaik777 | 0:d6e3d1e21484 | 60 | FCTN_I2C_WRITE(&i2c_data_type,1); |
cholletisaik777 | 0:d6e3d1e21484 | 61 | wait_ms(10); |
cholletisaik777 | 0:d6e3d1e21484 | 62 | switch(i2c_data_type) |
cholletisaik777 | 0:d6e3d1e21484 | 63 | { |
cholletisaik777 | 0:d6e3d1e21484 | 64 | case 's': |
cholletisaik777 | 0:d6e3d1e21484 | 65 | FCTN_I2C_WRITE(i2c_data,11); |
cholletisaik777 | 0:d6e3d1e21484 | 66 | break; |
cholletisaik777 | 0:d6e3d1e21484 | 67 | |
cholletisaik777 | 0:d6e3d1e21484 | 68 | case 'l': |
cholletisaik777 | 0:d6e3d1e21484 | 69 | FCTN_I2C_WRITE(i2c_data,135); |
cholletisaik777 | 0:d6e3d1e21484 | 70 | break; |
cholletisaik777 | 0:d6e3d1e21484 | 71 | |
cholletisaik777 | 0:d6e3d1e21484 | 72 | case 'h': |
cholletisaik777 | 0:d6e3d1e21484 | 73 | FCTN_I2C_READ(i2c_data,25); |
cholletisaik777 | 0:d6e3d1e21484 | 74 | break; |
cholletisaik777 | 0:d6e3d1e21484 | 75 | |
cholletisaik777 | 0:d6e3d1e21484 | 76 | default: |
cholletisaik777 | 0:d6e3d1e21484 | 77 | printf("\r\n\r invalid command \r\n"); |
cholletisaik777 | 12:cb3ee1ac3638 | 78 | return 25; |
cholletisaik777 | 0:d6e3d1e21484 | 79 | } |
cholletisaik777 | 0:d6e3d1e21484 | 80 | } |