for frequency correction testing

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Committer:
chaithanyarss
Date:
Wed Jun 29 13:59:21 2016 +0000
Revision:
187:2c7263530c57
Parent:
115:2e1144a41d76
Child:
206:fba4aeebf004
SD card finalized code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aniruddhv 52:0bd68655c651 1 void FCTN_CDMS_INIT_RTC()
aniruddhv 52:0bd68655c651 2 {
ee12b079 93:4d76de54a699 3 SPI_mutex.lock();
aniruddhv 52:0bd68655c651 4 gCS_RTC=1;
aniruddhv 52:0bd68655c651 5 spi.format(8,0);
aniruddhv 52:0bd68655c651 6 spi.frequency(1000000);
aniruddhv 52:0bd68655c651 7 //clearing the halt bit
aniruddhv 52:0bd68655c651 8 gCS_RTC=1;
aniruddhv 52:0bd68655c651 9 gCS_RTC=0;
aniruddhv 52:0bd68655c651 10 spi.write(0x8C);
aniruddhv 52:0bd68655c651 11 spi.write(0x00);
aniruddhv 52:0bd68655c651 12
aniruddhv 52:0bd68655c651 13 //clearing the OF bit
aniruddhv 52:0bd68655c651 14 gCS_RTC=1;
aniruddhv 52:0bd68655c651 15 gCS_RTC=0;
aniruddhv 52:0bd68655c651 16 spi.write(0x8F);
aniruddhv 52:0bd68655c651 17 spi.write(0x00);
aniruddhv 52:0bd68655c651 18
aniruddhv 52:0bd68655c651 19 //century bits
aniruddhv 52:0bd68655c651 20 gCS_RTC=1;
aniruddhv 52:0bd68655c651 21 gCS_RTC=0;
aniruddhv 52:0bd68655c651 22 spi.write(0x80|0x03);
aniruddhv 52:0bd68655c651 23 spi.write(0x00);
aniruddhv 52:0bd68655c651 24
aniruddhv 52:0bd68655c651 25 //Kick starting the oscillator
aniruddhv 52:0bd68655c651 26 gCS_RTC=1;
aniruddhv 52:0bd68655c651 27 gCS_RTC=0;
aniruddhv 52:0bd68655c651 28 spi.write(0x81); //register address with write flag
aniruddhv 52:0bd68655c651 29 spi.write(0x80);//enabling stop bit in the seconds register
aniruddhv 52:0bd68655c651 30
aniruddhv 52:0bd68655c651 31 gCS_RTC=1;
aniruddhv 52:0bd68655c651 32 gCS_RTC=0;
aniruddhv 52:0bd68655c651 33 spi.write(0x81);
aniruddhv 52:0bd68655c651 34 spi.write(0x00);//disabling the stop bit to restart the oscillator
aniruddhv 52:0bd68655c651 35
aniruddhv 52:0bd68655c651 36
aniruddhv 52:0bd68655c651 37 gCS_RTC=1;
aniruddhv 52:0bd68655c651 38 gCS_RTC=0;
aniruddhv 52:0bd68655c651 39 spi.write(0x80);
aniruddhv 52:0bd68655c651 40 spi.write(0x01); // set milliseconds value to 00
aniruddhv 52:0bd68655c651 41 gCS_RTC=1;
aniruddhv 52:0bd68655c651 42
aniruddhv 52:0bd68655c651 43 gCS_RTC=0;
aniruddhv 52:0bd68655c651 44 spi.write(0x81);
aniruddhv 52:0bd68655c651 45 spi.write(0x01); //set seconds value to 00
aniruddhv 52:0bd68655c651 46 gCS_RTC=1;
aniruddhv 52:0bd68655c651 47
aniruddhv 52:0bd68655c651 48 gCS_RTC=0;
aniruddhv 52:0bd68655c651 49 spi.write(0x82);
aniruddhv 52:0bd68655c651 50 spi.write(0x01);//set minutes value to 00
aniruddhv 52:0bd68655c651 51 gCS_RTC=1;
aniruddhv 52:0bd68655c651 52
aniruddhv 52:0bd68655c651 53 gCS_RTC=0;
aniruddhv 52:0bd68655c651 54 spi.write(0x83);
aniruddhv 52:0bd68655c651 55 spi.write(0x23); //set the hours to 01
aniruddhv 52:0bd68655c651 56 gCS_RTC=1;
aniruddhv 52:0bd68655c651 57
aniruddhv 52:0bd68655c651 58 gCS_RTC=0;
aniruddhv 52:0bd68655c651 59 spi.write(0x84);
aniruddhv 52:0bd68655c651 60 spi.write(0x01); //set day of the week to 01
aniruddhv 52:0bd68655c651 61 gCS_RTC=1;
aniruddhv 52:0bd68655c651 62
aniruddhv 52:0bd68655c651 63 gCS_RTC=0;
aniruddhv 52:0bd68655c651 64 spi.write(0x85);
aniruddhv 52:0bd68655c651 65 spi.write(0x31); //set date of the month to 01
aniruddhv 52:0bd68655c651 66 gCS_RTC=1;
aniruddhv 52:0bd68655c651 67
aniruddhv 52:0bd68655c651 68 gCS_RTC=0;
aniruddhv 52:0bd68655c651 69 spi.write(0x86);
aniruddhv 52:0bd68655c651 70 spi.write(0x12); //set month to 01
aniruddhv 52:0bd68655c651 71 gCS_RTC=1;
aniruddhv 52:0bd68655c651 72
aniruddhv 52:0bd68655c651 73 gCS_RTC=0;
aniruddhv 52:0bd68655c651 74 spi.write(0x87);
aniruddhv 52:0bd68655c651 75 spi.write(0x01); //set year to 00(2000)
aniruddhv 52:0bd68655c651 76 gCS_RTC=1;
chaithanyarss 187:2c7263530c57 77 gPC.puts("\r\nrtc initalised\r\n");
ee12b079 93:4d76de54a699 78 SPI_mutex.unlock();
aniruddhv 52:0bd68655c651 79 }
aniruddhv 52:0bd68655c651 80
aniruddhv 52:0bd68655c651 81 uint64_t FCTN_CDMS_RD_RTC()
aniruddhv 52:0bd68655c651 82 {
chaithanyarss 187:2c7263530c57 83 SPI_mutex.lock();
chaithanyarss 187:2c7263530c57 84 uint8_t response;
chaithanyarss 187:2c7263530c57 85
chaithanyarss 187:2c7263530c57 86 gCS_RTC=1;
chaithanyarss 187:2c7263530c57 87 gCS_RTC=0;
chaithanyarss 187:2c7263530c57 88 spi.write(0x0F);
chaithanyarss 187:2c7263530c57 89 response = (spi.write(0x00))&0x04;
chaithanyarss 187:2c7263530c57 90 /* if(response =! 0x00)
chaithanyarss 187:2c7263530c57 91 {
chaithanyarss 187:2c7263530c57 92 time = 0x0000000000000000;
chaithanyarss 187:2c7263530c57 93 return time;
chaithanyarss 187:2c7263530c57 94 }*/
chaithanyarss 187:2c7263530c57 95
chaithanyarss 187:2c7263530c57 96 gCS_RTC=1;
chaithanyarss 187:2c7263530c57 97 gCS_RTC=0;
chaithanyarss 187:2c7263530c57 98 spi.write(0x00); //reading centiseconds register
chaithanyarss 187:2c7263530c57 99 response = spi.write(0x00); // read the value by sending dummy byte
chaithanyarss 187:2c7263530c57 100 uint8_t centiseconds = ((response&0xF0)>>4)*10+(response&0x0F)*1;
chaithanyarss 187:2c7263530c57 101
chaithanyarss 187:2c7263530c57 102 response =spi.write(0x00);
chaithanyarss 187:2c7263530c57 103 uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1;
chaithanyarss 187:2c7263530c57 104
chaithanyarss 187:2c7263530c57 105 response =spi.write(0x00);
chaithanyarss 187:2c7263530c57 106 uint8_t minutes = ((response&0x70)>>4)*10+(response&0x0F)*1;
chaithanyarss 187:2c7263530c57 107
chaithanyarss 187:2c7263530c57 108 response=spi.write(0x00);
chaithanyarss 187:2c7263530c57 109 uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1;
chaithanyarss 187:2c7263530c57 110
chaithanyarss 187:2c7263530c57 111 uint8_t day =spi.write(0x00);
chaithanyarss 187:2c7263530c57 112
chaithanyarss 187:2c7263530c57 113 response =spi.write(0x00);
chaithanyarss 187:2c7263530c57 114 uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1;
chaithanyarss 187:2c7263530c57 115
chaithanyarss 187:2c7263530c57 116 response =spi.write(0x00);
chaithanyarss 187:2c7263530c57 117 uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1;
chaithanyarss 187:2c7263530c57 118
chaithanyarss 187:2c7263530c57 119 response =spi.write(0x00);
chaithanyarss 187:2c7263530c57 120 uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1;
chaithanyarss 187:2c7263530c57 121 year = (year == 17)?0x00:(year == 18)?0x01:(year == 19)?0x02:(year == 20)?0x03:0x00;
chaithanyarss 187:2c7263530c57 122 gCS_RTC=1;
chaithanyarss 187:2c7263530c57 123
chaithanyarss 187:2c7263530c57 124 uint64_t time;
chaithanyarss 187:2c7263530c57 125 time = 0;
chaithanyarss 187:2c7263530c57 126 time = time|(((uint64_t)(centiseconds&0x7F)));
chaithanyarss 187:2c7263530c57 127 time = time|(((uint64_t)(seconds&0x3F))<<7);
chaithanyarss 187:2c7263530c57 128 time = time|(((uint64_t)(minutes&0x3F))<<13);
chaithanyarss 187:2c7263530c57 129 time = time|(((uint64_t)(hours&0x1F))<<19);
chaithanyarss 187:2c7263530c57 130 time = time|(((uint64_t)(day&0x1F))<<24);
chaithanyarss 187:2c7263530c57 131 time = time|(((uint64_t)(month&0x07))<<29);
chaithanyarss 187:2c7263530c57 132 time = time|(((uint64_t)(year&0x03))<<33);
chaithanyarss 187:2c7263530c57 133 time = (time&0x00000007FFFFFFFF);
chaithanyarss 187:2c7263530c57 134
chaithanyarss 187:2c7263530c57 135 SPI_mutex.unlock();
chaithanyarss 187:2c7263530c57 136 return time;
aniruddhv 52:0bd68655c651 137 }