Repository for CDMS code
Dependencies: SimpleDMA mbed-rtos mbed eeprom
Fork of COM_MNG_TMTC_SIMPLE by
cdms_rtc.h@52:0bd68655c651, 2016-01-14 (annotated)
- Committer:
- aniruddhv
- Date:
- Thu Jan 14 06:16:42 2016 +0000
- Revision:
- 52:0bd68655c651
- Child:
- 93:4d76de54a699
Merged with Shubham's code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aniruddhv | 52:0bd68655c651 | 1 | void FCTN_CDMS_INIT_RTC() |
aniruddhv | 52:0bd68655c651 | 2 | { |
aniruddhv | 52:0bd68655c651 | 3 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 4 | spi.format(8,0); |
aniruddhv | 52:0bd68655c651 | 5 | spi.frequency(1000000); |
aniruddhv | 52:0bd68655c651 | 6 | //clearing the halt bit |
aniruddhv | 52:0bd68655c651 | 7 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 8 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 9 | spi.write(0x8C); |
aniruddhv | 52:0bd68655c651 | 10 | spi.write(0x00); |
aniruddhv | 52:0bd68655c651 | 11 | |
aniruddhv | 52:0bd68655c651 | 12 | //clearing the OF bit |
aniruddhv | 52:0bd68655c651 | 13 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 14 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 15 | spi.write(0x8F); |
aniruddhv | 52:0bd68655c651 | 16 | spi.write(0x00); |
aniruddhv | 52:0bd68655c651 | 17 | |
aniruddhv | 52:0bd68655c651 | 18 | //century bits |
aniruddhv | 52:0bd68655c651 | 19 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 20 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 21 | spi.write(0x80|0x03); |
aniruddhv | 52:0bd68655c651 | 22 | spi.write(0x00); |
aniruddhv | 52:0bd68655c651 | 23 | |
aniruddhv | 52:0bd68655c651 | 24 | //Kick starting the oscillator |
aniruddhv | 52:0bd68655c651 | 25 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 26 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 27 | spi.write(0x81); //register address with write flag |
aniruddhv | 52:0bd68655c651 | 28 | spi.write(0x80);//enabling stop bit in the seconds register |
aniruddhv | 52:0bd68655c651 | 29 | |
aniruddhv | 52:0bd68655c651 | 30 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 31 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 32 | spi.write(0x81); |
aniruddhv | 52:0bd68655c651 | 33 | spi.write(0x00);//disabling the stop bit to restart the oscillator |
aniruddhv | 52:0bd68655c651 | 34 | |
aniruddhv | 52:0bd68655c651 | 35 | |
aniruddhv | 52:0bd68655c651 | 36 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 37 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 38 | spi.write(0x80); |
aniruddhv | 52:0bd68655c651 | 39 | spi.write(0x01); // set milliseconds value to 00 |
aniruddhv | 52:0bd68655c651 | 40 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 41 | |
aniruddhv | 52:0bd68655c651 | 42 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 43 | spi.write(0x81); |
aniruddhv | 52:0bd68655c651 | 44 | spi.write(0x01); //set seconds value to 00 |
aniruddhv | 52:0bd68655c651 | 45 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 46 | |
aniruddhv | 52:0bd68655c651 | 47 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 48 | spi.write(0x82); |
aniruddhv | 52:0bd68655c651 | 49 | spi.write(0x01);//set minutes value to 00 |
aniruddhv | 52:0bd68655c651 | 50 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 51 | |
aniruddhv | 52:0bd68655c651 | 52 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 53 | spi.write(0x83); |
aniruddhv | 52:0bd68655c651 | 54 | spi.write(0x23); //set the hours to 01 |
aniruddhv | 52:0bd68655c651 | 55 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 56 | |
aniruddhv | 52:0bd68655c651 | 57 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 58 | spi.write(0x84); |
aniruddhv | 52:0bd68655c651 | 59 | spi.write(0x01); //set day of the week to 01 |
aniruddhv | 52:0bd68655c651 | 60 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 61 | |
aniruddhv | 52:0bd68655c651 | 62 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 63 | spi.write(0x85); |
aniruddhv | 52:0bd68655c651 | 64 | spi.write(0x31); //set date of the month to 01 |
aniruddhv | 52:0bd68655c651 | 65 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 66 | |
aniruddhv | 52:0bd68655c651 | 67 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 68 | spi.write(0x86); |
aniruddhv | 52:0bd68655c651 | 69 | spi.write(0x12); //set month to 01 |
aniruddhv | 52:0bd68655c651 | 70 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 71 | |
aniruddhv | 52:0bd68655c651 | 72 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 73 | spi.write(0x87); |
aniruddhv | 52:0bd68655c651 | 74 | spi.write(0x01); //set year to 00(2000) |
aniruddhv | 52:0bd68655c651 | 75 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 76 | printf("\n\r rtc initalised \n"); |
aniruddhv | 52:0bd68655c651 | 77 | } |
aniruddhv | 52:0bd68655c651 | 78 | |
aniruddhv | 52:0bd68655c651 | 79 | uint64_t FCTN_CDMS_RD_RTC() |
aniruddhv | 52:0bd68655c651 | 80 | { |
aniruddhv | 52:0bd68655c651 | 81 | uint8_t response; |
aniruddhv | 52:0bd68655c651 | 82 | printf("\n\r Entered rtc\n"); |
aniruddhv | 52:0bd68655c651 | 83 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 84 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 85 | spi.write(0x00); //reading milliseconds register |
aniruddhv | 52:0bd68655c651 | 86 | response = spi.write(0x00); // read the value by sending dummy byte |
aniruddhv | 52:0bd68655c651 | 87 | uint8_t centiseconds = (uint8_t(response&0xF0)>>4)*10+uint8_t(response&0x0F)*1; |
aniruddhv | 52:0bd68655c651 | 88 | |
aniruddhv | 52:0bd68655c651 | 89 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 90 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 91 | spi.write(0x01); //reading seconds register |
aniruddhv | 52:0bd68655c651 | 92 | response =spi.write(0x01); |
aniruddhv | 52:0bd68655c651 | 93 | uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1; |
aniruddhv | 52:0bd68655c651 | 94 | |
aniruddhv | 52:0bd68655c651 | 95 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 96 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 97 | spi.write(0x02); //reading minutes register |
aniruddhv | 52:0bd68655c651 | 98 | response =spi.write(0x01); |
aniruddhv | 52:0bd68655c651 | 99 | uint8_t minutes = ((response&0xF0)>>4)*10+(response&0x0F)*1; |
aniruddhv | 52:0bd68655c651 | 100 | |
aniruddhv | 52:0bd68655c651 | 101 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 102 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 103 | spi.write(0x03); //reading hours register |
aniruddhv | 52:0bd68655c651 | 104 | response=spi.write(0x01); |
aniruddhv | 52:0bd68655c651 | 105 | uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1; |
aniruddhv | 52:0bd68655c651 | 106 | |
aniruddhv | 52:0bd68655c651 | 107 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 108 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 109 | spi.write(0x04); //reading day's register |
aniruddhv | 52:0bd68655c651 | 110 | uint8_t day =spi.write(0x01); |
aniruddhv | 52:0bd68655c651 | 111 | |
aniruddhv | 52:0bd68655c651 | 112 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 113 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 114 | spi.write(0x05); //reading date register |
aniruddhv | 52:0bd68655c651 | 115 | response =spi.write(0x01); |
aniruddhv | 52:0bd68655c651 | 116 | uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1; |
aniruddhv | 52:0bd68655c651 | 117 | |
aniruddhv | 52:0bd68655c651 | 118 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 119 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 120 | spi.write(0x06); //reading month registe |
aniruddhv | 52:0bd68655c651 | 121 | response =spi.write(0x01); |
aniruddhv | 52:0bd68655c651 | 122 | uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1; |
aniruddhv | 52:0bd68655c651 | 123 | |
aniruddhv | 52:0bd68655c651 | 124 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 125 | gCS_RTC=0; |
aniruddhv | 52:0bd68655c651 | 126 | spi.write(0x07); //reading year's registe |
aniruddhv | 52:0bd68655c651 | 127 | response =spi.write(0x01); |
aniruddhv | 52:0bd68655c651 | 128 | uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1; |
aniruddhv | 52:0bd68655c651 | 129 | gCS_RTC=1; |
aniruddhv | 52:0bd68655c651 | 130 | //sprintf(Time_stamp,"%02d%02d%02d%02d%02d%02d%02d%02d",year, month, date, day, hours, minutes, seconds, milliseconds ); |
aniruddhv | 52:0bd68655c651 | 131 | uint8_t Time_stamp[8] = {year, month, date, day, hours, minutes, seconds, centiseconds}; |
aniruddhv | 52:0bd68655c651 | 132 | for(int i= 0; i<8;i++) |
aniruddhv | 52:0bd68655c651 | 133 | printf("%d\t",Time_stamp[i]); |
aniruddhv | 52:0bd68655c651 | 134 | printf("read\r\n"); |
aniruddhv | 52:0bd68655c651 | 135 | uint64_t time; |
aniruddhv | 52:0bd68655c651 | 136 | time = 0; |
aniruddhv | 52:0bd68655c651 | 137 | time = time|(((uint64_t)(centiseconds&0x7F))); |
aniruddhv | 52:0bd68655c651 | 138 | time = time|(((uint64_t)(seconds&0x3F))<<7); |
aniruddhv | 52:0bd68655c651 | 139 | time = time|(((uint64_t)(minutes&0x3F))<<13); |
aniruddhv | 52:0bd68655c651 | 140 | time = time|(((uint64_t)(hours&0x1F))<<19); |
aniruddhv | 52:0bd68655c651 | 141 | time = time|(((uint64_t)(day&0x1F))<<24); |
aniruddhv | 52:0bd68655c651 | 142 | time = time|(((uint64_t)(month&0x07))<<29); |
aniruddhv | 52:0bd68655c651 | 143 | time = time|(((uint64_t)(year&0x03))<<33); |
aniruddhv | 52:0bd68655c651 | 144 | time = (time&0x00000007FFFFFFFF); |
aniruddhv | 52:0bd68655c651 | 145 | //printf("\n\r%x%x", (int)(time >> 32), (int)(time)); |
aniruddhv | 52:0bd68655c651 | 146 | printf("\n\r0x%016llx\n\r", time); |
aniruddhv | 52:0bd68655c651 | 147 | return time; |
aniruddhv | 52:0bd68655c651 | 148 | } |