cdms stnd alone isr i2c

Dependencies:   mbed-rtos mbed

Fork of TV_MASTER_I2C_RTOS by Team Fox

Committer:
sakthipriya
Date:
Sun Nov 01 12:48:34 2015 +0000
Revision:
0:abe5247de1be
Child:
1:963697dc5199
master i2c with rtos  working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sakthipriya 0:abe5247de1be 1 #include "mbed.h"
sakthipriya 0:abe5247de1be 2 #include "pin_config.h"
sakthipriya 0:abe5247de1be 3 #include "rtos.h"
sakthipriya 0:abe5247de1be 4 #define tm_len 1100
sakthipriya 0:abe5247de1be 5 #define tc_len 150
sakthipriya 0:abe5247de1be 6 #define hk_len 150
sakthipriya 0:abe5247de1be 7
sakthipriya 0:abe5247de1be 8 Serial pc(USBTX,USBRX);
sakthipriya 0:abe5247de1be 9 InterruptIn irpt_4m_slv(PIN39); //I2c interrupt from CDMS
sakthipriya 0:abe5247de1be 10 DigitalOut irpt_2_slv(PIN67); //I2C interrupt to CDMS
sakthipriya 0:abe5247de1be 11 I2C master(PIN32,PIN31);
sakthipriya 0:abe5247de1be 12 DigitalOut led1(PIN46);
sakthipriya 0:abe5247de1be 13 DigitalOut led2(PIN95);
sakthipriya 0:abe5247de1be 14 const int addr = 0x20; //slave address
sakthipriya 0:abe5247de1be 15 char hk_data[150];
sakthipriya 0:abe5247de1be 16 char telecommand[150];
sakthipriya 0:abe5247de1be 17 char telemetry[150];
sakthipriya 0:abe5247de1be 18 int length = 150;
sakthipriya 0:abe5247de1be 19 //Timer test;
sakthipriya 0:abe5247de1be 20 //Timer t;
sakthipriya 0:abe5247de1be 21 bool write_ack = true;
sakthipriya 0:abe5247de1be 22 bool read_ack = true;
sakthipriya 0:abe5247de1be 23 char cmd = 'r';
sakthipriya 0:abe5247de1be 24
sakthipriya 0:abe5247de1be 25 Thread *ptr_t_i2c;
sakthipriya 0:abe5247de1be 26
sakthipriya 0:abe5247de1be 27 void FCTN_I2C_READ(char*);
sakthipriya 0:abe5247de1be 28 void FCTN_I2C_WRITE(char*);
sakthipriya 0:abe5247de1be 29 void FCTN_ISR_I2C();
sakthipriya 0:abe5247de1be 30 int count = 0;
sakthipriya 0:abe5247de1be 31 void FCTN_MASTER_CMD()
sakthipriya 0:abe5247de1be 32 {
sakthipriya 0:abe5247de1be 33 switch(cmd)
sakthipriya 0:abe5247de1be 34 {
sakthipriya 0:abe5247de1be 35 case 'r':
sakthipriya 0:abe5247de1be 36 pc.printf("\n \r %d CDMS READ MODE\n",count);
sakthipriya 0:abe5247de1be 37 FCTN_I2C_READ(hk_data);
sakthipriya 0:abe5247de1be 38 cmd = 'w';
sakthipriya 0:abe5247de1be 39 count ++;
sakthipriya 0:abe5247de1be 40 break;
sakthipriya 0:abe5247de1be 41
sakthipriya 0:abe5247de1be 42 case 'w':
sakthipriya 0:abe5247de1be 43 pc.printf("\n \r %d CDMS WRITE MODE\n",count);
sakthipriya 0:abe5247de1be 44 FCTN_I2C_WRITE(telecommand);
sakthipriya 0:abe5247de1be 45 //strcat(telecommand,"1");
sakthipriya 0:abe5247de1be 46 cmd = 'r';
sakthipriya 0:abe5247de1be 47 count++;
sakthipriya 0:abe5247de1be 48 break;
sakthipriya 0:abe5247de1be 49 }
sakthipriya 0:abe5247de1be 50 }
sakthipriya 0:abe5247de1be 51
sakthipriya 0:abe5247de1be 52 void FCTN_I2C_WRITE(char *data)
sakthipriya 0:abe5247de1be 53 {
sakthipriya 0:abe5247de1be 54 irpt_2_slv = 1;
sakthipriya 0:abe5247de1be 55 //t.start();
sakthipriya 0:abe5247de1be 56 write_ack = master.write(addr|0x00,data,length);
sakthipriya 0:abe5247de1be 57 //t.stop();
sakthipriya 0:abe5247de1be 58 //if(write_ack == 0)
sakthipriya 0:abe5247de1be 59 //pc.printf("\n\r data not sent \n");
sakthipriya 0:abe5247de1be 60
sakthipriya 0:abe5247de1be 61 if (write_ack == 1)
sakthipriya 0:abe5247de1be 62 {
sakthipriya 0:abe5247de1be 63 pc.printf("\n\r data not sent \n");
sakthipriya 0:abe5247de1be 64 }
sakthipriya 0:abe5247de1be 65 irpt_2_slv = 0;
sakthipriya 0:abe5247de1be 66 //pc.printf("\n\r %d \n",t.read_us());
sakthipriya 0:abe5247de1be 67 //t.reset();
sakthipriya 0:abe5247de1be 68 }
sakthipriya 0:abe5247de1be 69
sakthipriya 0:abe5247de1be 70
sakthipriya 0:abe5247de1be 71 void FCTN_I2C_READ(char *data)
sakthipriya 0:abe5247de1be 72 {
sakthipriya 0:abe5247de1be 73 irpt_2_slv = 1;
sakthipriya 0:abe5247de1be 74 //t.start();
sakthipriya 0:abe5247de1be 75 read_ack = master.read(addr|1,data,length);
sakthipriya 0:abe5247de1be 76 //t.stop();
sakthipriya 0:abe5247de1be 77 //if(read_ack == 0)
sakthipriya 0:abe5247de1be 78 // pc.printf("\n\rData received from BAE %s \n",data);
sakthipriya 0:abe5247de1be 79 if (read_ack == 1)
sakthipriya 0:abe5247de1be 80 {
sakthipriya 0:abe5247de1be 81 pc.printf("\n \r data not received \n");
sakthipriya 0:abe5247de1be 82 }
sakthipriya 0:abe5247de1be 83 //if(read_ack == 1)
sakthipriya 0:abe5247de1be 84 //pc.printf("\n \r data not received \n");
sakthipriya 0:abe5247de1be 85
sakthipriya 0:abe5247de1be 86 irpt_2_slv = 0;
sakthipriya 0:abe5247de1be 87 //pc.printf("\n\r %d \n",t.read_us());
sakthipriya 0:abe5247de1be 88 //t.reset();
sakthipriya 0:abe5247de1be 89 }
sakthipriya 0:abe5247de1be 90
sakthipriya 0:abe5247de1be 91 void FCTN_ISR_I2C()
sakthipriya 0:abe5247de1be 92 {
sakthipriya 0:abe5247de1be 93 led2 = 1;
sakthipriya 0:abe5247de1be 94 //pc.printf("in isr");
sakthipriya 0:abe5247de1be 95 ptr_t_i2c->signal_set(0x1);
sakthipriya 0:abe5247de1be 96 //test.start();
sakthipriya 0:abe5247de1be 97 //wait(0.5);
sakthipriya 0:abe5247de1be 98 led2 = 0;
sakthipriya 0:abe5247de1be 99 }
sakthipriya 0:abe5247de1be 100
sakthipriya 0:abe5247de1be 101 void T_TM(void const * args)
sakthipriya 0:abe5247de1be 102 {
sakthipriya 0:abe5247de1be 103 while(1)
sakthipriya 0:abe5247de1be 104 {
sakthipriya 0:abe5247de1be 105 Thread::signal_wait(0x1);
sakthipriya 0:abe5247de1be 106 wait(2);
sakthipriya 0:abe5247de1be 107 //test.stop();
sakthipriya 0:abe5247de1be 108 led1 = 1;
sakthipriya 0:abe5247de1be 109 // wait(3);
sakthipriya 0:abe5247de1be 110 FCTN_I2C_READ(telemetry);
sakthipriya 0:abe5247de1be 111 led1 = 0;
sakthipriya 0:abe5247de1be 112 //pc.printf("\n \r %d",test.read_us());
sakthipriya 0:abe5247de1be 113 //test.reset();
sakthipriya 0:abe5247de1be 114 //irpt_2_slv = 1;
sakthipriya 0:abe5247de1be 115 // t.start();
sakthipriya 0:abe5247de1be 116 // read_ack = master.read(addr|1,telemetry,150);
sakthipriya 0:abe5247de1be 117 // t.stop();
sakthipriya 0:abe5247de1be 118 // if(read_ack == 0)
sakthipriya 0:abe5247de1be 119 // pc.printf("\n\rData received from BAE. %s \n",telemetry);
sakthipriya 0:abe5247de1be 120 // else
sakthipriya 0:abe5247de1be 121 // {
sakthipriya 0:abe5247de1be 122 // pc.printf("\n \r data not received \n");
sakthipriya 0:abe5247de1be 123 // }
sakthipriya 0:abe5247de1be 124 // irpt_2_slv = 0;
sakthipriya 0:abe5247de1be 125 // pc.printf("\n\r %d \n",t.read_us());
sakthipriya 0:abe5247de1be 126 // t.reset();
sakthipriya 0:abe5247de1be 127 // pc.printf("\n\r Telemetry Received \n");
sakthipriya 0:abe5247de1be 128 }
sakthipriya 0:abe5247de1be 129 }
sakthipriya 0:abe5247de1be 130
sakthipriya 0:abe5247de1be 131 int main()
sakthipriya 0:abe5247de1be 132 {
sakthipriya 0:abe5247de1be 133 pc.printf("\n\r MASTER ACTIVATED\n");
sakthipriya 0:abe5247de1be 134 master.frequency(100000);
sakthipriya 0:abe5247de1be 135 ptr_t_i2c = new Thread(T_TM);
sakthipriya 0:abe5247de1be 136 ptr_t_i2c->set_priority(osPriorityHigh);
sakthipriya 0:abe5247de1be 137 strcpy(telecommand," Telecommand1 : This is test telecommand. Verifying fuctionality. Yippee!!! It is working!!!!!!!!!!!!!");
sakthipriya 0:abe5247de1be 138 irpt_2_slv = 0;
sakthipriya 0:abe5247de1be 139 irpt_4m_slv.enable_irq();
sakthipriya 0:abe5247de1be 140 //irpt_4m_slv.mode(PullDown);
sakthipriya 0:abe5247de1be 141 irpt_4m_slv.rise(&FCTN_ISR_I2C);
sakthipriya 0:abe5247de1be 142 while(1)
sakthipriya 0:abe5247de1be 143 {
sakthipriya 0:abe5247de1be 144 Thread::wait(10000);
sakthipriya 0:abe5247de1be 145 //ptr_t_i2c->signal_set(0x1);
sakthipriya 0:abe5247de1be 146 pc.printf("\n\rTELEMETRY received from BAE is %s \n",telemetry);
sakthipriya 0:abe5247de1be 147 pc.printf("\n\rHK Data received from BAE is %s \n",hk_data);
sakthipriya 0:abe5247de1be 148 FCTN_MASTER_CMD();
sakthipriya 0:abe5247de1be 149 }
sakthipriya 0:abe5247de1be 150 }