working version

Dependencies:   mbed mbed-rtos SimpleDMA FreescaleIAP eeprom

Fork of CDMS_CODE_FM_28JAN2017 by samp Srinivasan

Committer:
samp1234
Date:
Sun Apr 03 15:35:53 2022 +0000
Revision:
357:f3d48d62e00e
Parent:
356:197c93dc2012
First commit test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chaithanyarss 224:3ac1f91e0ebc 1
aniruddhv 52:0bd68655c651 2 void FCTN_CDMS_INIT_RTC()
chaithanyarss 224:3ac1f91e0ebc 3 {
ee12b079 289:9bd62b69874c 4 if(EN_RTC == 0)
ee12b079 289:9bd62b69874c 5 return;
chaithanyarss 224:3ac1f91e0ebc 6 wait_ms(4000);
ee12b079 93:4d76de54a699 7 SPI_mutex.lock();
aniruddhv 52:0bd68655c651 8 gCS_RTC=1;
aniruddhv 52:0bd68655c651 9 spi.format(8,0);
aniruddhv 52:0bd68655c651 10 spi.frequency(1000000);
chaithanyarss 224:3ac1f91e0ebc 11
chaithanyarss 224:3ac1f91e0ebc 12 //Kick starting the oscillator
chaithanyarss 224:3ac1f91e0ebc 13 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 14 spi.write(0x81); //register address with write flag
chaithanyarss 224:3ac1f91e0ebc 15 spi.write(0x80);//enabling stop bit in the seconds register
chaithanyarss 224:3ac1f91e0ebc 16 gCS_RTC=1;
chaithanyarss 257:7d404e9dc9e2 17
chaithanyarss 257:7d404e9dc9e2 18 gCS_RTC=0;
chaithanyarss 257:7d404e9dc9e2 19 spi.write(0x81);
chaithanyarss 257:7d404e9dc9e2 20 spi.write(0x00);//disabling the stop bit to restart the oscillator
chaithanyarss 257:7d404e9dc9e2 21 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 22
chaithanyarss 224:3ac1f91e0ebc 23 //clearing the halt bit
aniruddhv 52:0bd68655c651 24 gCS_RTC=1;
aniruddhv 52:0bd68655c651 25 gCS_RTC=0;
aniruddhv 52:0bd68655c651 26 spi.write(0x8C);
aniruddhv 52:0bd68655c651 27 spi.write(0x00);
chaithanyarss 224:3ac1f91e0ebc 28 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 29
chaithanyarss 224:3ac1f91e0ebc 30 //clearing the OF bit
aniruddhv 52:0bd68655c651 31 gCS_RTC=0;
aniruddhv 52:0bd68655c651 32 spi.write(0x8F);
aniruddhv 52:0bd68655c651 33 spi.write(0x00);
chaithanyarss 224:3ac1f91e0ebc 34 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 35
aniruddhv 52:0bd68655c651 36 //century bits
aniruddhv 52:0bd68655c651 37 gCS_RTC=0;
aniruddhv 52:0bd68655c651 38 spi.write(0x80|0x03);
aniruddhv 52:0bd68655c651 39 spi.write(0x00);
chaithanyarss 224:3ac1f91e0ebc 40 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 41
ee12b079 209:63e9c8f8b5d2 42 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 43 spi.write(0x80);
ee12b079 245:da9d1bd999da 44 spi.write(0x00); // set milliseconds value to 00
ee12b079 209:63e9c8f8b5d2 45 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 46
ee12b079 209:63e9c8f8b5d2 47 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 48 spi.write(0x81);
ee12b079 245:da9d1bd999da 49 spi.write(0x00); //set seconds value to 00
ee12b079 209:63e9c8f8b5d2 50 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 51
ee12b079 209:63e9c8f8b5d2 52 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 53 spi.write(0x82);
ee12b079 245:da9d1bd999da 54 spi.write(0x00);//set minutes value to 00
ee12b079 209:63e9c8f8b5d2 55 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 56
ee12b079 209:63e9c8f8b5d2 57 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 58 spi.write(0x83);
ee12b079 245:da9d1bd999da 59 spi.write(0x00); //set the hours to 01
ee12b079 209:63e9c8f8b5d2 60 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 61
ee12b079 209:63e9c8f8b5d2 62 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 63 spi.write(0x84);
ee12b079 209:63e9c8f8b5d2 64 spi.write(0x01); //set day of the week to 01
ee12b079 209:63e9c8f8b5d2 65 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 66
ee12b079 209:63e9c8f8b5d2 67 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 68 spi.write(0x85);
ee12b079 245:da9d1bd999da 69 spi.write(0x01); //set date of the month to 01
ee12b079 209:63e9c8f8b5d2 70 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 71
ee12b079 209:63e9c8f8b5d2 72 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 73 spi.write(0x86);
ee12b079 245:da9d1bd999da 74 spi.write(0x01); //set month to 01
ee12b079 209:63e9c8f8b5d2 75 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 76
ee12b079 209:63e9c8f8b5d2 77 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 78 spi.write(0x87);
samp1234 356:197c93dc2012 79 spi.write(0x16); //set year to 00(2000)
ee12b079 209:63e9c8f8b5d2 80 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 81 gPC.puts("\n\r rtc initalised \n");
ee12b079 301:701ac67649b7 82 RTC_INIT_STATUS = 1;
samp1234 357:f3d48d62e00e 83 //FCTN_CDMS_WR_FLASH(5,1);
samp1234 357:f3d48d62e00e 84 WRITE_TO_EEPROM(5,1);
ee12b079 209:63e9c8f8b5d2 85 SPI_mutex.unlock();
ee12b079 209:63e9c8f8b5d2 86 }
chaithanyarss 224:3ac1f91e0ebc 87
ee12b079 209:63e9c8f8b5d2 88 uint64_t FCTN_CDMS_RD_RTC()
chaithanyarss 224:3ac1f91e0ebc 89 {
samp1234 356:197c93dc2012 90 if(EN_RTC == 0)
ee12b079 289:9bd62b69874c 91 return 0;
chaithanyarss 224:3ac1f91e0ebc 92 SPI_mutex.lock();
ee12b079 209:63e9c8f8b5d2 93 uint8_t response;
chaithanyarss 224:3ac1f91e0ebc 94 uint64_t time = 0;
chaithanyarss 224:3ac1f91e0ebc 95
chaithanyarss 224:3ac1f91e0ebc 96 gCS_RTC = 0;
chaithanyarss 224:3ac1f91e0ebc 97 spi.write(0x0F);
chaithanyarss 224:3ac1f91e0ebc 98 response = spi.write(0x00);
chaithanyarss 224:3ac1f91e0ebc 99 //gPC.printf("0x%02X",response);
chaithanyarss 224:3ac1f91e0ebc 100 gCS_RTC = 1;
chaithanyarss 224:3ac1f91e0ebc 101
ee12b079 209:63e9c8f8b5d2 102 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 103 spi.write(0x00); //reading milliseconds register
chaithanyarss 224:3ac1f91e0ebc 104 response = spi.write(0x00); // read the value by sending dummy byte
chaithanyarss 224:3ac1f91e0ebc 105 uint8_t centiseconds = (uint8_t(response&0xF0)>>4)*10+uint8_t(response&0x0F)*1;
aniruddhv 52:0bd68655c651 106
aniruddhv 52:0bd68655c651 107 gCS_RTC=1;
aniruddhv 52:0bd68655c651 108 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 109 spi.write(0x01); //reading seconds register
chaithanyarss 224:3ac1f91e0ebc 110 response =spi.write(0x01);
chaithanyarss 224:3ac1f91e0ebc 111 uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1;
chaithanyarss 224:3ac1f91e0ebc 112
chaithanyarss 224:3ac1f91e0ebc 113 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 114 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 115 spi.write(0x02); //reading minutes register
chaithanyarss 224:3ac1f91e0ebc 116 response =spi.write(0x01);
chaithanyarss 224:3ac1f91e0ebc 117 uint8_t minutes = ((response&0xF0)>>4)*10+(response&0x0F)*1;
chaithanyarss 224:3ac1f91e0ebc 118
chaithanyarss 224:3ac1f91e0ebc 119 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 120 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 121 spi.write(0x03); //reading hours register
chaithanyarss 224:3ac1f91e0ebc 122 response=spi.write(0x01);
chaithanyarss 224:3ac1f91e0ebc 123 uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1;
chaithanyarss 224:3ac1f91e0ebc 124
chaithanyarss 224:3ac1f91e0ebc 125 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 126 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 127 spi.write(0x04); //reading day's register
chaithanyarss 224:3ac1f91e0ebc 128 uint8_t day =spi.write(0x01);
chaithanyarss 224:3ac1f91e0ebc 129
chaithanyarss 224:3ac1f91e0ebc 130 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 131 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 132 spi.write(0x05); //reading date register
chaithanyarss 224:3ac1f91e0ebc 133 response =spi.write(0x01);
chaithanyarss 224:3ac1f91e0ebc 134 uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1;
chaithanyarss 224:3ac1f91e0ebc 135
chaithanyarss 224:3ac1f91e0ebc 136 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 137 gCS_RTC=0;
chaithanyarss 224:3ac1f91e0ebc 138 spi.write(0x06); //reading month registe
chaithanyarss 224:3ac1f91e0ebc 139 response =spi.write(0x01);
chaithanyarss 224:3ac1f91e0ebc 140 uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1;
chaithanyarss 224:3ac1f91e0ebc 141
chaithanyarss 224:3ac1f91e0ebc 142 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 143 gCS_RTC=0;
samp1234 356:197c93dc2012 144 spi.write(0x07);
samp1234 356:197c93dc2012 145 response = spi.write(0x01);
ee12b079 209:63e9c8f8b5d2 146 uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1;
chaithanyarss 224:3ac1f91e0ebc 147 year = (year == 16)?0x00:(year == 17)?0x01:(year == 18)?0x02:(year == 19)?0x03:0x00;
aniruddhv 52:0bd68655c651 148 gCS_RTC=1;
chaithanyarss 224:3ac1f91e0ebc 149
chaithanyarss 224:3ac1f91e0ebc 150
aniruddhv 52:0bd68655c651 151 time = time|(((uint64_t)(centiseconds&0x7F)));
aniruddhv 52:0bd68655c651 152 time = time|(((uint64_t)(seconds&0x3F))<<7);
aniruddhv 52:0bd68655c651 153 time = time|(((uint64_t)(minutes&0x3F))<<13);
aniruddhv 52:0bd68655c651 154 time = time|(((uint64_t)(hours&0x1F))<<19);
samp1234 356:197c93dc2012 155 time = time|(((uint64_t)(date&0x1F))<<24);
samp1234 356:197c93dc2012 156 time = time|(((uint64_t)(month&0x0F))<<29);
aniruddhv 52:0bd68655c651 157 time = time|(((uint64_t)(year&0x03))<<33);
aniruddhv 52:0bd68655c651 158 time = (time&0x00000007FFFFFFFF);
ee12b079 209:63e9c8f8b5d2 159 SPI_mutex.unlock();
ee12b079 209:63e9c8f8b5d2 160 return time;
ee12b079 209:63e9c8f8b5d2 161 }