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