publishing to check changes in cdms code
Dependencies: FreescaleIAP SimpleDMA mbed-rtos mbed
Fork of CDMS_CODE_samp_23SEP_DMA_flag by
Diff: cdms_rtc.h
- Revision:
- 224:3ac1f91e0ebc
- Parent:
- 209:63e9c8f8b5d2
- Child:
- 240:946a9342a95f
- Child:
- 248:407ab7e337cd
diff -r f4acf895b598 -r 3ac1f91e0ebc cdms_rtc.h --- a/cdms_rtc.h Sat Jul 02 15:28:21 2016 +0000 +++ b/cdms_rtc.h Mon Jul 04 14:31:30 2016 +0000 @@ -1,123 +1,147 @@ + void FCTN_CDMS_INIT_RTC() -{ +{ + wait_ms(4000); SPI_mutex.lock(); gCS_RTC=1; spi.format(8,0); spi.frequency(1000000); - //clearing the halt bit + + //Kick starting the oscillator + gCS_RTC=0; + spi.write(0x81); //register address with write flag + spi.write(0x80);//enabling stop bit in the seconds register + gCS_RTC=1; + + //clearing the halt bit gCS_RTC=1; gCS_RTC=0; spi.write(0x8C); spi.write(0x00); - - //clearing the OF bit - gCS_RTC=1; + gCS_RTC=1; + + //clearing the OF bit gCS_RTC=0; spi.write(0x8F); spi.write(0x00); - + gCS_RTC=1; + //century bits - gCS_RTC=1; gCS_RTC=0; spi.write(0x80|0x03); spi.write(0x00); + gCS_RTC=1; - //Kick starting the oscillator - gCS_RTC=1; - gCS_RTC=0; - spi.write(0x81); //register address with write flag - spi.write(0x80); //enabling stop bit in the seconds register - - gCS_RTC=1; + gCS_RTC=0; spi.write(0x81); - spi.write(0x00); //disabling the stop bit to restart the oscillator - - + spi.write(0x00);//disabling the stop bit to restart the oscillator gCS_RTC=1; + gCS_RTC=0; - spi.write(0x80); + spi.write(0x80); spi.write(0x01); // set milliseconds value to 00 gCS_RTC=1; - + gCS_RTC=0; - spi.write(0x81); + spi.write(0x81); spi.write(0x01); //set seconds value to 00 gCS_RTC=1; - + gCS_RTC=0; - spi.write(0x82); + spi.write(0x82); spi.write(0x01);//set minutes value to 00 gCS_RTC=1; - + gCS_RTC=0; - spi.write(0x83); + spi.write(0x83); spi.write(0x23); //set the hours to 01 gCS_RTC=1; - + gCS_RTC=0; - spi.write(0x84); + spi.write(0x84); spi.write(0x01); //set day of the week to 01 gCS_RTC=1; - + gCS_RTC=0; - spi.write(0x85); + spi.write(0x85); spi.write(0x31); //set date of the month to 01 gCS_RTC=1; - + gCS_RTC=0; - spi.write(0x86); + spi.write(0x86); spi.write(0x12); //set month to 01 gCS_RTC=1; - + gCS_RTC=0; - spi.write(0x87); + spi.write(0x87); spi.write(0x01); //set year to 00(2000) gCS_RTC=1; - gPC.puts("\r\nrtc initalised\r\n"); + gPC.puts("\n\r rtc initalised \n"); SPI_mutex.unlock(); } - + uint64_t FCTN_CDMS_RD_RTC() -{ - SPI_mutex.lock(); +{ + SPI_mutex.lock(); uint8_t response; - - gCS_RTC=1; + uint64_t time = 0; + + gCS_RTC = 0; + spi.write(0x0F); + response = spi.write(0x00); + //gPC.printf("0x%02X",response); + gCS_RTC = 1; + gCS_RTC=0; - spi.write(0x0F); - response = (spi.write(0x00))&0x04; + spi.write(0x00); //reading milliseconds register + response = spi.write(0x00); // read the value by sending dummy byte + uint8_t centiseconds = (uint8_t(response&0xF0)>>4)*10+uint8_t(response&0x0F)*1; gCS_RTC=1; gCS_RTC=0; - spi.write(0x00); //reading centiseconds register - response = spi.write(0x00); // read the value by sending dummy byte - uint8_t centiseconds = ((response&0xF0)>>4)*10+(response&0x0F)*1; - - response =spi.write(0x00); - uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1; - - response =spi.write(0x00); - uint8_t minutes = ((response&0x70)>>4)*10+(response&0x0F)*1; - - response=spi.write(0x00); - uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1; - - uint8_t day =spi.write(0x00); - - response =spi.write(0x00); - uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1; - - response =spi.write(0x00); - uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1; - + spi.write(0x01); //reading seconds register + response =spi.write(0x01); + uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1; + + gCS_RTC=1; + gCS_RTC=0; + spi.write(0x02); //reading minutes register + response =spi.write(0x01); + uint8_t minutes = ((response&0xF0)>>4)*10+(response&0x0F)*1; + + gCS_RTC=1; + gCS_RTC=0; + spi.write(0x03); //reading hours register + response=spi.write(0x01); + uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1; + + gCS_RTC=1; + gCS_RTC=0; + spi.write(0x04); //reading day's register + uint8_t day =spi.write(0x01); + + gCS_RTC=1; + gCS_RTC=0; + spi.write(0x05); //reading date register + response =spi.write(0x01); + uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1; + + gCS_RTC=1; + gCS_RTC=0; + spi.write(0x06); //reading month registe + response =spi.write(0x01); + uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1; + + gCS_RTC=1; + gCS_RTC=0; response =spi.write(0x00); uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1; - year = (year == 17)?0x00:(year == 18)?0x01:(year == 19)?0x02:(year == 20)?0x03:0x00; + year = (year == 16)?0x00:(year == 17)?0x01:(year == 18)?0x02:(year == 19)?0x03:0x00; gCS_RTC=1; - - uint64_t time; - time = 0; + + uint8_t Time_stamp[8] = {year, month, date, day, hours, minutes, seconds, centiseconds}; + time = time|(((uint64_t)(centiseconds&0x7F))); time = time|(((uint64_t)(seconds&0x3F))<<7); time = time|(((uint64_t)(minutes&0x3F))<<13); @@ -126,8 +150,6 @@ time = time|(((uint64_t)(month&0x07))<<29); time = time|(((uint64_t)(year&0x03))<<33); time = (time&0x00000007FFFFFFFF); - SPI_mutex.unlock(); return time; } -