![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
integrating vr 1.0
Dependencies: SDFileSystem mbed-rtos mbed-src mbed
main.cpp@0:d6ce3d144be5, 2015-05-13 (annotated)
- Committer:
- sakthipriya
- Date:
- Wed May 13 06:21:49 2015 +0000
- Revision:
- 0:d6ce3d144be5
integrating vr 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:d6ce3d144be5 | 1 | #include "mbed.h" |
sakthipriya | 0:d6ce3d144be5 | 2 | #include "rtos.h" |
sakthipriya | 0:d6ce3d144be5 | 3 | #include "pin_config.h" |
sakthipriya | 0:d6ce3d144be5 | 4 | #include "SDFileSystem.h" |
sakthipriya | 0:d6ce3d144be5 | 5 | #include "string.h" |
sakthipriya | 0:d6ce3d144be5 | 6 | |
sakthipriya | 0:d6ce3d144be5 | 7 | const int addr = 0x20; //slave address |
sakthipriya | 0:d6ce3d144be5 | 8 | I2C master (PIN32,PIN31); //configure pins p27,p28 as I2C master |
sakthipriya | 0:d6ce3d144be5 | 9 | //I2C master (PIN32,PIN31); |
sakthipriya | 0:d6ce3d144be5 | 10 | Serial pc (USBTX,USBRX); |
sakthipriya | 0:d6ce3d144be5 | 11 | DigitalOut interrupt(PIN4); |
sakthipriya | 0:d6ce3d144be5 | 12 | InterruptIn data_ready(PIN39); |
sakthipriya | 0:d6ce3d144be5 | 13 | //DigitalOut interrupt(D9); |
sakthipriya | 0:d6ce3d144be5 | 14 | //InterruptIn data_ready(D10); |
sakthipriya | 0:d6ce3d144be5 | 15 | int reset; |
sakthipriya | 0:d6ce3d144be5 | 16 | Timer t; |
sakthipriya | 0:d6ce3d144be5 | 17 | Timer t1; |
sakthipriya | 0:d6ce3d144be5 | 18 | Timer t2; |
sakthipriya | 0:d6ce3d144be5 | 19 | Timer t3; |
sakthipriya | 0:d6ce3d144be5 | 20 | |
sakthipriya | 0:d6ce3d144be5 | 21 | void store_data(); |
sakthipriya | 0:d6ce3d144be5 | 22 | |
sakthipriya | 0:d6ce3d144be5 | 23 | typedef struct |
sakthipriya | 0:d6ce3d144be5 | 24 | { |
sakthipriya | 0:d6ce3d144be5 | 25 | char data; // To avoid dynamic memory allocation |
sakthipriya | 0:d6ce3d144be5 | 26 | int length; |
sakthipriya | 0:d6ce3d144be5 | 27 | }i2c_data; |
sakthipriya | 0:d6ce3d144be5 | 28 | |
sakthipriya | 0:d6ce3d144be5 | 29 | |
sakthipriya | 0:d6ce3d144be5 | 30 | |
sakthipriya | 0:d6ce3d144be5 | 31 | //Mail<i2c_data,16> i2c_data_receive; |
sakthipriya | 0:d6ce3d144be5 | 32 | Mail<i2c_data,16> i2c_data_send; |
sakthipriya | 0:d6ce3d144be5 | 33 | |
sakthipriya | 0:d6ce3d144be5 | 34 | Thread * ptr_t_i2c; |
sakthipriya | 0:d6ce3d144be5 | 35 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 0:d6ce3d144be5 | 36 | { |
sakthipriya | 0:d6ce3d144be5 | 37 | |
sakthipriya | 0:d6ce3d144be5 | 38 | |
sakthipriya | 0:d6ce3d144be5 | 39 | bool ack0 =true; |
sakthipriya | 0:d6ce3d144be5 | 40 | |
sakthipriya | 0:d6ce3d144be5 | 41 | interrupt = 1; |
sakthipriya | 0:d6ce3d144be5 | 42 | t1.start(); |
sakthipriya | 0:d6ce3d144be5 | 43 | //wait_ms(20); |
sakthipriya | 0:d6ce3d144be5 | 44 | ack0 = master.read(addr|1,data,length); |
sakthipriya | 0:d6ce3d144be5 | 45 | t1.stop(); |
sakthipriya | 0:d6ce3d144be5 | 46 | |
sakthipriya | 0:d6ce3d144be5 | 47 | |
sakthipriya | 0:d6ce3d144be5 | 48 | if(!ack0) |
sakthipriya | 0:d6ce3d144be5 | 49 | { |
sakthipriya | 0:d6ce3d144be5 | 50 | printf("\n master has read %s from slave\n\r",data); |
sakthipriya | 0:d6ce3d144be5 | 51 | |
sakthipriya | 0:d6ce3d144be5 | 52 | } |
sakthipriya | 0:d6ce3d144be5 | 53 | //master.stop(); |
sakthipriya | 0:d6ce3d144be5 | 54 | |
sakthipriya | 0:d6ce3d144be5 | 55 | store_data(); |
sakthipriya | 0:d6ce3d144be5 | 56 | printf("\n%d\n\r",t1.read_us()); |
sakthipriya | 0:d6ce3d144be5 | 57 | t1.reset(); |
sakthipriya | 0:d6ce3d144be5 | 58 | |
sakthipriya | 0:d6ce3d144be5 | 59 | interrupt=0; |
sakthipriya | 0:d6ce3d144be5 | 60 | |
sakthipriya | 0:d6ce3d144be5 | 61 | } |
sakthipriya | 0:d6ce3d144be5 | 62 | |
sakthipriya | 0:d6ce3d144be5 | 63 | typedef struct { |
sakthipriya | 0:d6ce3d144be5 | 64 | char Voltage[9]; |
sakthipriya | 0:d6ce3d144be5 | 65 | char Current[5]; |
sakthipriya | 0:d6ce3d144be5 | 66 | char Temperature[2]; |
sakthipriya | 0:d6ce3d144be5 | 67 | char PanelTemperature[3];//read by the 4 thermistors on solar panels |
sakthipriya | 0:d6ce3d144be5 | 68 | char BatteryTemperature; //to be populated |
sakthipriya | 0:d6ce3d144be5 | 69 | char faultpoll; //polled faults |
sakthipriya | 0:d6ce3d144be5 | 70 | char faultir; //interrupted faults |
sakthipriya | 0:d6ce3d144be5 | 71 | char power_mode; //power modes |
sakthipriya | 0:d6ce3d144be5 | 72 | char AngularSpeed[3]; |
sakthipriya | 0:d6ce3d144be5 | 73 | char Bnewvalue[3]; |
sakthipriya | 0:d6ce3d144be5 | 74 | |
sakthipriya | 0:d6ce3d144be5 | 75 | //float magnetometer,gyro=>to be addes |
sakthipriya | 0:d6ce3d144be5 | 76 | } hk_data; |
sakthipriya | 0:d6ce3d144be5 | 77 | hk_data decode_data; |
sakthipriya | 0:d6ce3d144be5 | 78 | |
sakthipriya | 0:d6ce3d144be5 | 79 | /*void TC_DECODE(char *data_hk) //getting the structure back from hk data sent by bae |
sakthipriya | 0:d6ce3d144be5 | 80 | { |
sakthipriya | 0:d6ce3d144be5 | 81 | for(int i=0;i<=7;i++) |
sakthipriya | 0:d6ce3d144be5 | 82 | { |
sakthipriya | 0:d6ce3d144be5 | 83 | decode_data.Voltage[i] = data_hk[i]; |
sakthipriya | 0:d6ce3d144be5 | 84 | decode_data.Voltage[8] = '\0'; |
sakthipriya | 0:d6ce3d144be5 | 85 | } |
sakthipriya | 0:d6ce3d144be5 | 86 | for(int i=0;i<=3;i++) |
sakthipriya | 0:d6ce3d144be5 | 87 | { |
sakthipriya | 0:d6ce3d144be5 | 88 | decode_data.Current[i] = data_hk[8+i]; |
sakthipriya | 0:d6ce3d144be5 | 89 | decode_data.Current[4] = '\0'; |
sakthipriya | 0:d6ce3d144be5 | 90 | } |
sakthipriya | 0:d6ce3d144be5 | 91 | decode_data.Temperature[0] = data_hk[12]; |
sakthipriya | 0:d6ce3d144be5 | 92 | decode_data.Temperature[1] = '\0'; |
sakthipriya | 0:d6ce3d144be5 | 93 | for(int i=0;i<=1;i++) |
sakthipriya | 0:d6ce3d144be5 | 94 | { |
sakthipriya | 0:d6ce3d144be5 | 95 | decode_data.PanelTemperature[i] = data_hk[13+i]; |
sakthipriya | 0:d6ce3d144be5 | 96 | decode_data.PanelTemperature[2] = '\0'; |
sakthipriya | 0:d6ce3d144be5 | 97 | } |
sakthipriya | 0:d6ce3d144be5 | 98 | decode_data.BatteryTemperature = data_hk[15]; |
sakthipriya | 0:d6ce3d144be5 | 99 | decode_data.faultpoll = data_hk[16]; |
sakthipriya | 0:d6ce3d144be5 | 100 | decode_data.faultir = data_hk[17]; |
sakthipriya | 0:d6ce3d144be5 | 101 | decode_data.power_mode = data_hk[18]; |
sakthipriya | 0:d6ce3d144be5 | 102 | for(int i=0;i<=1;i++) |
sakthipriya | 0:d6ce3d144be5 | 103 | { |
sakthipriya | 0:d6ce3d144be5 | 104 | decode_data.AngularSpeed[i] = data_hk[19+i]; |
sakthipriya | 0:d6ce3d144be5 | 105 | decode_data.AngularSpeed[2] = '\0'; |
sakthipriya | 0:d6ce3d144be5 | 106 | } |
sakthipriya | 0:d6ce3d144be5 | 107 | for(int i=0;i<=1;i++) |
sakthipriya | 0:d6ce3d144be5 | 108 | { |
sakthipriya | 0:d6ce3d144be5 | 109 | decode_data.Bnewvalue[i] = data_hk[21+i]; |
sakthipriya | 0:d6ce3d144be5 | 110 | decode_data.Bnewvalue[2] = '\0'; |
sakthipriya | 0:d6ce3d144be5 | 111 | } |
sakthipriya | 0:d6ce3d144be5 | 112 | printf("\n voltage %s\n\r",decode_data.Voltage); |
sakthipriya | 0:d6ce3d144be5 | 113 | printf("\n current %s\n\r",decode_data.Current); |
sakthipriya | 0:d6ce3d144be5 | 114 | printf("\n faultpoll %c\n\r",decode_data.faultpoll); |
sakthipriya | 0:d6ce3d144be5 | 115 | }*/ |
sakthipriya | 0:d6ce3d144be5 | 116 | |
sakthipriya | 0:d6ce3d144be5 | 117 | |
sakthipriya | 0:d6ce3d144be5 | 118 | void T_I2C_MASTER_FSLAVE(void const *args) |
sakthipriya | 0:d6ce3d144be5 | 119 | { |
sakthipriya | 0:d6ce3d144be5 | 120 | char data_receive[25]; |
sakthipriya | 0:d6ce3d144be5 | 121 | //char data_receive; |
sakthipriya | 0:d6ce3d144be5 | 122 | while(1) |
sakthipriya | 0:d6ce3d144be5 | 123 | { |
sakthipriya | 0:d6ce3d144be5 | 124 | Thread::signal_wait(0x1); |
sakthipriya | 0:d6ce3d144be5 | 125 | |
sakthipriya | 0:d6ce3d144be5 | 126 | |
sakthipriya | 0:d6ce3d144be5 | 127 | FUNC_I2C_MASTER_FSLAVE(data_receive,25); |
sakthipriya | 0:d6ce3d144be5 | 128 | /*i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
sakthipriya | 0:d6ce3d144be5 | 129 | strcpy(i2c_data_r->data , data_receive); |
sakthipriya | 0:d6ce3d144be5 | 130 | i2c_data_r->length = 25; |
sakthipriya | 0:d6ce3d144be5 | 131 | i2c_data_receive.put(i2c_data_r);*/ |
sakthipriya | 0:d6ce3d144be5 | 132 | printf("\n Data received from slave is %s\n\r",data_receive); |
sakthipriya | 0:d6ce3d144be5 | 133 | //TC_DECODE(data_receive); |
sakthipriya | 0:d6ce3d144be5 | 134 | } |
sakthipriya | 0:d6ce3d144be5 | 135 | } |
sakthipriya | 0:d6ce3d144be5 | 136 | |
sakthipriya | 0:d6ce3d144be5 | 137 | void FUNC_INT() |
sakthipriya | 0:d6ce3d144be5 | 138 | { |
sakthipriya | 0:d6ce3d144be5 | 139 | |
sakthipriya | 0:d6ce3d144be5 | 140 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 0:d6ce3d144be5 | 141 | |
sakthipriya | 0:d6ce3d144be5 | 142 | } |
sakthipriya | 0:d6ce3d144be5 | 143 | |
sakthipriya | 0:d6ce3d144be5 | 144 | char writedata; |
sakthipriya | 0:d6ce3d144be5 | 145 | bool write2slave; |
sakthipriya | 0:d6ce3d144be5 | 146 | bool master_status_write; |
sakthipriya | 0:d6ce3d144be5 | 147 | void FUNC_MASTER_WRITE() |
sakthipriya | 0:d6ce3d144be5 | 148 | { //wait(1); |
sakthipriya | 0:d6ce3d144be5 | 149 | write2slave=true; |
sakthipriya | 0:d6ce3d144be5 | 150 | |
sakthipriya | 0:d6ce3d144be5 | 151 | |
sakthipriya | 0:d6ce3d144be5 | 152 | |
sakthipriya | 0:d6ce3d144be5 | 153 | char data = pc.getc(); |
sakthipriya | 0:d6ce3d144be5 | 154 | interrupt = 1; |
sakthipriya | 0:d6ce3d144be5 | 155 | t.start(); |
sakthipriya | 0:d6ce3d144be5 | 156 | t3.start(); |
sakthipriya | 0:d6ce3d144be5 | 157 | // wait_ms(20); |
sakthipriya | 0:d6ce3d144be5 | 158 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 0:d6ce3d144be5 | 159 | i2c_data_s->data = data; |
sakthipriya | 0:d6ce3d144be5 | 160 | i2c_data_s->length = 1; |
sakthipriya | 0:d6ce3d144be5 | 161 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 0:d6ce3d144be5 | 162 | master_status_write = true; |
sakthipriya | 0:d6ce3d144be5 | 163 | |
sakthipriya | 0:d6ce3d144be5 | 164 | // interrupt = 1; |
sakthipriya | 0:d6ce3d144be5 | 165 | |
sakthipriya | 0:d6ce3d144be5 | 166 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 0:d6ce3d144be5 | 167 | if (evt.status == osEventMail) |
sakthipriya | 0:d6ce3d144be5 | 168 | { |
sakthipriya | 0:d6ce3d144be5 | 169 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
sakthipriya | 0:d6ce3d144be5 | 170 | writedata = i2c_data_s -> data; |
sakthipriya | 0:d6ce3d144be5 | 171 | t.stop(); |
sakthipriya | 0:d6ce3d144be5 | 172 | //t3.start(); |
sakthipriya | 0:d6ce3d144be5 | 173 | master_status_write = (bool) master.write(addr|0x00,&writedata,1); |
sakthipriya | 0:d6ce3d144be5 | 174 | t3.stop(); |
sakthipriya | 0:d6ce3d144be5 | 175 | if(master_status_write==0) |
sakthipriya | 0:d6ce3d144be5 | 176 | { |
sakthipriya | 0:d6ce3d144be5 | 177 | printf("\n\r CDMS has written %c to PAYLOAD\n\r",writedata); |
sakthipriya | 0:d6ce3d144be5 | 178 | write2slave=false; |
sakthipriya | 0:d6ce3d144be5 | 179 | } |
sakthipriya | 0:d6ce3d144be5 | 180 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 0:d6ce3d144be5 | 181 | printf("\n%d",t.read_us()); |
sakthipriya | 0:d6ce3d144be5 | 182 | t.reset(); |
sakthipriya | 0:d6ce3d144be5 | 183 | printf("\n%d\n",t3.read_us()); |
sakthipriya | 0:d6ce3d144be5 | 184 | t3.reset(); |
sakthipriya | 0:d6ce3d144be5 | 185 | } |
sakthipriya | 0:d6ce3d144be5 | 186 | interrupt = 0; |
sakthipriya | 0:d6ce3d144be5 | 187 | } |
sakthipriya | 0:d6ce3d144be5 | 188 | |
sakthipriya | 0:d6ce3d144be5 | 189 | /********************************** Function Prototypes declaration ***********************************/ |
sakthipriya | 0:d6ce3d144be5 | 190 | int FUNC_CDMS_hex2int(int); // Need to convert the RTC time values to integers |
sakthipriya | 0:d6ce3d144be5 | 191 | void FUNC_CDMS_Gettime(void); // Function to get the time values from RTC registers |
sakthipriya | 0:d6ce3d144be5 | 192 | void FUNC_CDMS_init_values(void); // Function to initialize the registers in the RTC |
sakthipriya | 0:d6ce3d144be5 | 193 | |
sakthipriya | 0:d6ce3d144be5 | 194 | |
sakthipriya | 0:d6ce3d144be5 | 195 | /***************** Configure the SPI1 of the CDMS uc as the data bus for the RTC***********************/ |
sakthipriya | 0:d6ce3d144be5 | 196 | SPI spi(PTE1,PTE3, PTE2); // mosi, miso, sclk |
sakthipriya | 0:d6ce3d144be5 | 197 | DigitalOut cs(PTE29); // PTE29 is used for chip select |
sakthipriya | 0:d6ce3d144be5 | 198 | |
sakthipriya | 0:d6ce3d144be5 | 199 | char ch; |
sakthipriya | 0:d6ce3d144be5 | 200 | SDFileSystem sd(PTE1, PTE3, PTE2, PTE22, "sd"); // the pinout on the mbed Cool Components workshop board |
sakthipriya | 0:d6ce3d144be5 | 201 | //Serial pc(USBTX, USBRX); // tx, rx |
sakthipriya | 0:d6ce3d144be5 | 202 | char time_stamp[15]; |
sakthipriya | 0:d6ce3d144be5 | 203 | |
sakthipriya | 0:d6ce3d144be5 | 204 | /***********************Initialization function of the RTC********************************************/ |
sakthipriya | 0:d6ce3d144be5 | 205 | void FUNC_CDMS_init_values(void) |
sakthipriya | 0:d6ce3d144be5 | 206 | { |
sakthipriya | 0:d6ce3d144be5 | 207 | cs=0; |
sakthipriya | 0:d6ce3d144be5 | 208 | spi.format(8,3); // Set the data bit with to be of 8 bits, |
sakthipriya | 0:d6ce3d144be5 | 209 | // data tx mode is 3 |
sakthipriya | 0:d6ce3d144be5 | 210 | spi.frequency(1000000); // Set Data rate to be 1 MHz |
sakthipriya | 0:d6ce3d144be5 | 211 | |
sakthipriya | 0:d6ce3d144be5 | 212 | cs = 0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 213 | spi.write(0x80); // Send the address of the Seconds register 0x80 |
sakthipriya | 0:d6ce3d144be5 | 214 | spi.write(0x00); // Set seconds value to 0 |
sakthipriya | 0:d6ce3d144be5 | 215 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 216 | |
sakthipriya | 0:d6ce3d144be5 | 217 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 218 | spi.write(0x81); // Send the address of the Minutes register 0x81 |
sakthipriya | 0:d6ce3d144be5 | 219 | spi.write(0x00); // Set minutes value to 0 |
sakthipriya | 0:d6ce3d144be5 | 220 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 221 | |
sakthipriya | 0:d6ce3d144be5 | 222 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 223 | spi.write(0x82); // Send the address of the Hours register 0x82 |
sakthipriya | 0:d6ce3d144be5 | 224 | spi.write(0x00); // Set hours value to 0 |
sakthipriya | 0:d6ce3d144be5 | 225 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 226 | |
sakthipriya | 0:d6ce3d144be5 | 227 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 228 | spi.write(0x83); // Send the address of the Day register 0x83 |
sakthipriya | 0:d6ce3d144be5 | 229 | spi.write(0x01); // Set the day to 01 |
sakthipriya | 0:d6ce3d144be5 | 230 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 231 | |
sakthipriya | 0:d6ce3d144be5 | 232 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 233 | spi.write(0x84); // Send the address of the date register 0x84 |
sakthipriya | 0:d6ce3d144be5 | 234 | spi.write(0x01); // Set date of the month to 01 |
sakthipriya | 0:d6ce3d144be5 | 235 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 236 | |
sakthipriya | 0:d6ce3d144be5 | 237 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 238 | spi.write(0x85); // Send the address of the Month register 0x80 |
sakthipriya | 0:d6ce3d144be5 | 239 | spi.write(0x01); // Set month to 01 |
sakthipriya | 0:d6ce3d144be5 | 240 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 241 | |
sakthipriya | 0:d6ce3d144be5 | 242 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 243 | spi.write(0x86); // Send the address of the year register 0x80 |
sakthipriya | 0:d6ce3d144be5 | 244 | spi.write(0x00); // Set year to 00(2000) |
sakthipriya | 0:d6ce3d144be5 | 245 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 246 | }// End of INIT Function |
sakthipriya | 0:d6ce3d144be5 | 247 | |
sakthipriya | 0:d6ce3d144be5 | 248 | |
sakthipriya | 0:d6ce3d144be5 | 249 | /*********************************Function to read the RTC registers*********************************/ |
sakthipriya | 0:d6ce3d144be5 | 250 | void FUNC_CDMS_Gettime() |
sakthipriya | 0:d6ce3d144be5 | 251 | { |
sakthipriya | 0:d6ce3d144be5 | 252 | |
sakthipriya | 0:d6ce3d144be5 | 253 | spi.format(8,3); // Set the data bit with to be of 8 bits, |
sakthipriya | 0:d6ce3d144be5 | 254 | // data tx mode is 3 |
sakthipriya | 0:d6ce3d144be5 | 255 | spi.frequency(1000000); // Set Data rate to be 1 MHz |
sakthipriya | 0:d6ce3d144be5 | 256 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 257 | |
sakthipriya | 0:d6ce3d144be5 | 258 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 259 | spi.write(0x00); // Sending address of seconds register |
sakthipriya | 0:d6ce3d144be5 | 260 | int seconds = spi.write(0x00); // Read the value by sending dummy byte |
sakthipriya | 0:d6ce3d144be5 | 261 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 262 | |
sakthipriya | 0:d6ce3d144be5 | 263 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 264 | spi.write(0x01); // Sending address of Minutes register |
sakthipriya | 0:d6ce3d144be5 | 265 | int minutes =spi.write(0x01); // Read the value by sending dummy byte |
sakthipriya | 0:d6ce3d144be5 | 266 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 267 | |
sakthipriya | 0:d6ce3d144be5 | 268 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 269 | spi.write(0x02); // Sending address of hours register |
sakthipriya | 0:d6ce3d144be5 | 270 | int hours =spi.write(0x01); // Read the value by sending dummy byte |
sakthipriya | 0:d6ce3d144be5 | 271 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 272 | |
sakthipriya | 0:d6ce3d144be5 | 273 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 274 | spi.write(0x03); // Sending address of day register |
sakthipriya | 0:d6ce3d144be5 | 275 | int day =spi.write(0x01); // Read the value by sending dummy byte |
sakthipriya | 0:d6ce3d144be5 | 276 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 277 | |
sakthipriya | 0:d6ce3d144be5 | 278 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 279 | spi.write(0x04); // Sending address of date register |
sakthipriya | 0:d6ce3d144be5 | 280 | int date =spi.write(0x01); // Read the value by sending dummy byte |
sakthipriya | 0:d6ce3d144be5 | 281 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 282 | |
sakthipriya | 0:d6ce3d144be5 | 283 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 284 | spi.write(0x05); // Sending address of month register |
sakthipriya | 0:d6ce3d144be5 | 285 | int month =spi.write(0x01); // Read the value by sending dummy byte |
sakthipriya | 0:d6ce3d144be5 | 286 | cs=1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 287 | |
sakthipriya | 0:d6ce3d144be5 | 288 | cs=0; // Set chip select pin to be 0: Activate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 289 | spi.write(0x06); // Sending address of year register |
sakthipriya | 0:d6ce3d144be5 | 290 | int year =spi.write(0x01); // Read the value by sending dummy byte |
sakthipriya | 0:d6ce3d144be5 | 291 | cs = 1; // Set chip select pin to be 1: DeActivate RTC chip |
sakthipriya | 0:d6ce3d144be5 | 292 | |
sakthipriya | 0:d6ce3d144be5 | 293 | // RTC sends in BCD format.. SO we convert the values generated by RTC to integers |
sakthipriya | 0:d6ce3d144be5 | 294 | year = FUNC_CDMS_hex2int(year); |
sakthipriya | 0:d6ce3d144be5 | 295 | month = FUNC_CDMS_hex2int(month); |
sakthipriya | 0:d6ce3d144be5 | 296 | date = FUNC_CDMS_hex2int(date); |
sakthipriya | 0:d6ce3d144be5 | 297 | day = FUNC_CDMS_hex2int(day); |
sakthipriya | 0:d6ce3d144be5 | 298 | hours = FUNC_CDMS_hex2int(hours); |
sakthipriya | 0:d6ce3d144be5 | 299 | minutes = FUNC_CDMS_hex2int(minutes); |
sakthipriya | 0:d6ce3d144be5 | 300 | seconds = FUNC_CDMS_hex2int(seconds); |
sakthipriya | 0:d6ce3d144be5 | 301 | |
sakthipriya | 0:d6ce3d144be5 | 302 | // Print the obtained Time stamp |
sakthipriya | 0:d6ce3d144be5 | 303 | //printf("The time is :%d %d %d %d %d %d %d \n\r",seconds,minutes,hours,day,date,month,year); |
sakthipriya | 0:d6ce3d144be5 | 304 | sprintf(time_stamp,"%02d%02d%02d%02d%02d%02d",year,month,date,hours,minutes,seconds); |
sakthipriya | 0:d6ce3d144be5 | 305 | printf(" \n\r Received HK data from BAE"); |
sakthipriya | 0:d6ce3d144be5 | 306 | printf("\n HK_data stored in %s.txt",time_stamp); |
sakthipriya | 0:d6ce3d144be5 | 307 | //puts(time_stamp); |
sakthipriya | 0:d6ce3d144be5 | 308 | //printf(".txt"); |
sakthipriya | 0:d6ce3d144be5 | 309 | }//End of Read Function |
sakthipriya | 0:d6ce3d144be5 | 310 | |
sakthipriya | 0:d6ce3d144be5 | 311 | /**************************Function to convert Hex values to Int values*****************************/ |
sakthipriya | 0:d6ce3d144be5 | 312 | int FUNC_CDMS_hex2int(int a) |
sakthipriya | 0:d6ce3d144be5 | 313 | { |
sakthipriya | 0:d6ce3d144be5 | 314 | a=(a/16)*10+(a%16); //function to convert hex type to int type |
sakthipriya | 0:d6ce3d144be5 | 315 | return a; |
sakthipriya | 0:d6ce3d144be5 | 316 | }// End of convert function |
sakthipriya | 0:d6ce3d144be5 | 317 | |
sakthipriya | 0:d6ce3d144be5 | 318 | |
sakthipriya | 0:d6ce3d144be5 | 319 | /******************************************END OF RTC CODE ****************************************/ |
sakthipriya | 0:d6ce3d144be5 | 320 | |
sakthipriya | 0:d6ce3d144be5 | 321 | char count = 10; |
sakthipriya | 0:d6ce3d144be5 | 322 | void store_data() |
sakthipriya | 0:d6ce3d144be5 | 323 | { |
sakthipriya | 0:d6ce3d144be5 | 324 | FUNC_CDMS_Gettime(); |
sakthipriya | 0:d6ce3d144be5 | 325 | |
sakthipriya | 0:d6ce3d144be5 | 326 | char hk_data[25]; |
sakthipriya | 0:d6ce3d144be5 | 327 | count++; |
sakthipriya | 0:d6ce3d144be5 | 328 | strcpy(hk_data,"hk_Data "); |
sakthipriya | 0:d6ce3d144be5 | 329 | strcat(hk_data,"!@@#"); |
sakthipriya | 0:d6ce3d144be5 | 330 | hk_data[10] = count; |
sakthipriya | 0:d6ce3d144be5 | 331 | //storedata(hk_data); |
sakthipriya | 0:d6ce3d144be5 | 332 | mkdir("/sd/hk", 0777); |
sakthipriya | 0:d6ce3d144be5 | 333 | char add[20]; |
sakthipriya | 0:d6ce3d144be5 | 334 | strcpy(add,"/sd/hk/"); |
sakthipriya | 0:d6ce3d144be5 | 335 | strcat(add,time_stamp); |
sakthipriya | 0:d6ce3d144be5 | 336 | strcat(add,".txt"); |
sakthipriya | 0:d6ce3d144be5 | 337 | |
sakthipriya | 0:d6ce3d144be5 | 338 | /*FILE *fp = fopen(add, "w"); |
sakthipriya | 0:d6ce3d144be5 | 339 | if(fp == NULL) { |
sakthipriya | 0:d6ce3d144be5 | 340 | error("Could not open file for write\n"); |
sakthipriya | 0:d6ce3d144be5 | 341 | } |
sakthipriya | 0:d6ce3d144be5 | 342 | else |
sakthipriya | 0:d6ce3d144be5 | 343 | { |
sakthipriya | 0:d6ce3d144be5 | 344 | |
sakthipriya | 0:d6ce3d144be5 | 345 | fprintf(fp, "%s ",hk_data); |
sakthipriya | 0:d6ce3d144be5 | 346 | fclose(fp); |
sakthipriya | 0:d6ce3d144be5 | 347 | printf("\n File stored in SD card\n");*/ |
sakthipriya | 0:d6ce3d144be5 | 348 | //printf("\n Reading from the file .... \n Data is %s\n",hk_data); |
sakthipriya | 0:d6ce3d144be5 | 349 | |
sakthipriya | 0:d6ce3d144be5 | 350 | |
sakthipriya | 0:d6ce3d144be5 | 351 | } |
sakthipriya | 0:d6ce3d144be5 | 352 | |
sakthipriya | 0:d6ce3d144be5 | 353 | |
sakthipriya | 0:d6ce3d144be5 | 354 | |
sakthipriya | 0:d6ce3d144be5 | 355 | //void create_file() |
sakthipriya | 0:d6ce3d144be5 | 356 | //{ |
sakthipriya | 0:d6ce3d144be5 | 357 | |
sakthipriya | 0:d6ce3d144be5 | 358 | /* |
sakthipriya | 0:d6ce3d144be5 | 359 | fprintf(fp, "Hello fun SD Card World!\n"); |
sakthipriya | 0:d6ce3d144be5 | 360 | fclose(fp); |
sakthipriya | 0:d6ce3d144be5 | 361 | |
sakthipriya | 0:d6ce3d144be5 | 362 | fp = fopen("/sd/mydir/sdtest.txt", "r"); |
sakthipriya | 0:d6ce3d144be5 | 363 | if(fp == NULL) { |
sakthipriya | 0:d6ce3d144be5 | 364 | error("Could not open file for write\n"); |
sakthipriya | 0:d6ce3d144be5 | 365 | } |
sakthipriya | 0:d6ce3d144be5 | 366 | //fprintf(fp, "Hello fun SD Card World!"); |
sakthipriya | 0:d6ce3d144be5 | 367 | // fprintf("The contents of %s file are :\n", "/sd/mydir/sdtest.txt"); |
sakthipriya | 0:d6ce3d144be5 | 368 | while( ( ch = fgetc(fp) ) != '\n' ) |
sakthipriya | 0:d6ce3d144be5 | 369 | { |
sakthipriya | 0:d6ce3d144be5 | 370 | pc.printf("%c",ch); |
sakthipriya | 0:d6ce3d144be5 | 371 | } |
sakthipriya | 0:d6ce3d144be5 | 372 | //fprintf(fp); |
sakthipriya | 0:d6ce3d144be5 | 373 | fclose(fp); |
sakthipriya | 0:d6ce3d144be5 | 374 | |
sakthipriya | 0:d6ce3d144be5 | 375 | printf("Jai Mata Di..Goodbye World!\n");*/ |
sakthipriya | 0:d6ce3d144be5 | 376 | //} |
sakthipriya | 0:d6ce3d144be5 | 377 | |
sakthipriya | 0:d6ce3d144be5 | 378 | //Ticker t; |
sakthipriya | 0:d6ce3d144be5 | 379 | |
sakthipriya | 0:d6ce3d144be5 | 380 | |
sakthipriya | 0:d6ce3d144be5 | 381 | int main() |
sakthipriya | 0:d6ce3d144be5 | 382 | { |
sakthipriya | 0:d6ce3d144be5 | 383 | interrupt=0; |
sakthipriya | 0:d6ce3d144be5 | 384 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
sakthipriya | 0:d6ce3d144be5 | 385 | data_ready.rise(&FUNC_INT); |
sakthipriya | 0:d6ce3d144be5 | 386 | printf("\n\rIITMSAT CDMS EXECUTED\n"); |
sakthipriya | 0:d6ce3d144be5 | 387 | master.frequency(100000); |
sakthipriya | 0:d6ce3d144be5 | 388 | |
sakthipriya | 0:d6ce3d144be5 | 389 | FUNC_CDMS_init_values(); |
sakthipriya | 0:d6ce3d144be5 | 390 | // t.attach(&store_data, 20.0); |
sakthipriya | 0:d6ce3d144be5 | 391 | while(1) |
sakthipriya | 0:d6ce3d144be5 | 392 | { //Thread::wait(9000); |
sakthipriya | 0:d6ce3d144be5 | 393 | //interrupt = 1; |
sakthipriya | 0:d6ce3d144be5 | 394 | |
sakthipriya | 0:d6ce3d144be5 | 395 | FUNC_MASTER_WRITE(); |
sakthipriya | 0:d6ce3d144be5 | 396 | //interrupt = 0; |
sakthipriya | 0:d6ce3d144be5 | 397 | ; |
sakthipriya | 0:d6ce3d144be5 | 398 | } |
sakthipriya | 0:d6ce3d144be5 | 399 | } |