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