samp Srinivasan / Mbed 2 deprecated CDMS_CODE_FROM13JAN2017

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cdms_rtc.h Source File

cdms_rtc.h

00001 
00002 void FCTN_CDMS_INIT_RTC()
00003 {   
00004     if(EN_RTC == 0)
00005     return;
00006     wait_ms(4000);
00007     SPI_mutex.lock();
00008     gCS_RTC=1;
00009     spi.format(8,0);
00010     spi.frequency(1000000);
00011     
00012     //Kick starting the oscillator 
00013     gCS_RTC=0;
00014     spi.write(0x81); //register address with write flag
00015     spi.write(0x80);//enabling stop bit in the seconds register
00016     gCS_RTC=1;
00017     
00018     gCS_RTC=0;
00019     spi.write(0x81);
00020     spi.write(0x00);//disabling the stop bit to restart the oscillator 
00021     gCS_RTC=1;
00022         
00023     //clearing the halt bit 
00024     gCS_RTC=1;
00025     gCS_RTC=0;
00026     spi.write(0x8C);
00027     spi.write(0x00);
00028     gCS_RTC=1; 
00029      
00030     //clearing the OF bit 
00031     gCS_RTC=0;
00032     spi.write(0x8F);
00033     spi.write(0x00);
00034     gCS_RTC=1; 
00035      
00036     //century bits
00037     gCS_RTC=0;
00038     spi.write(0x80|0x03);
00039     spi.write(0x00);
00040     gCS_RTC=1;
00041     
00042     gCS_RTC=0;
00043     spi.write(0x80); 
00044     spi.write(0x00); // set milliseconds value to 00
00045     gCS_RTC=1;
00046  
00047     gCS_RTC=0;
00048     spi.write(0x81); 
00049     spi.write(0x00); //set seconds value to 00
00050     gCS_RTC=1;
00051  
00052     gCS_RTC=0;
00053     spi.write(0x82); 
00054     spi.write(0x00);//set minutes value to 00
00055     gCS_RTC=1;
00056  
00057     gCS_RTC=0;
00058     spi.write(0x83); 
00059     spi.write(0x00); //set the hours to 01
00060     gCS_RTC=1;
00061     
00062     gCS_RTC=0;
00063     spi.write(0x84); 
00064     spi.write(0x01); //set day of the week to 01
00065     gCS_RTC=1;
00066     
00067     gCS_RTC=0;
00068     spi.write(0x85); 
00069     spi.write(0x01); //set date of the month to 01
00070     gCS_RTC=1;
00071     
00072     gCS_RTC=0;
00073     spi.write(0x86); 
00074     spi.write(0x01); //set month to 01
00075     gCS_RTC=1;
00076     
00077     gCS_RTC=0;
00078     spi.write(0x87); 
00079     spi.write(0x00); //set year to 00(2000)
00080     gCS_RTC=1;
00081     gPC.puts("\n\r rtc initalised \n");
00082     RTC_INIT_STATUS = 1;
00083     FCTN_CDMS_WR_FLASH(5,1);
00084     SPI_mutex.unlock();
00085 }
00086  
00087 uint64_t FCTN_CDMS_RD_RTC()
00088 {   
00089     if(EN_RTC == 0)
00090     return 0;
00091     SPI_mutex.lock(); 
00092     uint8_t response;
00093     uint64_t time = 0;
00094     
00095     gCS_RTC = 0;
00096     spi.write(0x0F);
00097     response = spi.write(0x00);
00098     //gPC.printf("0x%02X",response);
00099     gCS_RTC = 1;
00100     
00101     gCS_RTC=0;
00102     spi.write(0x00); //reading milliseconds register
00103     response = spi.write(0x00); // read the value by sending dummy byte
00104     uint8_t centiseconds =  (uint8_t(response&0xF0)>>4)*10+uint8_t(response&0x0F)*1;
00105     
00106     gCS_RTC=1;
00107     gCS_RTC=0;
00108     spi.write(0x01); //reading seconds register
00109     response =spi.write(0x01);
00110     uint8_t seconds =  ((response&0x70)>>4)*10+(response&0x0F)*1;
00111     
00112     gCS_RTC=1;
00113     gCS_RTC=0;
00114     spi.write(0x02); //reading minutes register
00115     response =spi.write(0x01);
00116     uint8_t minutes =  ((response&0xF0)>>4)*10+(response&0x0F)*1;
00117     
00118     gCS_RTC=1;
00119     gCS_RTC=0;
00120     spi.write(0x03); //reading hours register
00121     response=spi.write(0x01);
00122     uint8_t hours =  ((response&0x30)>>4)*10+(response&0x0F)*1;
00123     
00124     gCS_RTC=1;
00125     gCS_RTC=0;
00126     spi.write(0x04); //reading day's  register
00127     uint8_t day =spi.write(0x01);
00128     
00129     gCS_RTC=1;
00130     gCS_RTC=0;
00131     spi.write(0x05); //reading date register
00132     response =spi.write(0x01);
00133     uint8_t date =  ((response&0x30)>>4)*10+(response&0x0F)*1;
00134     
00135     gCS_RTC=1;
00136     gCS_RTC=0;
00137     spi.write(0x06); //reading month registe
00138     response =spi.write(0x01);
00139     uint8_t month =  ((response&0x10)>>4)*10+(response&0x0F)*1;
00140     
00141     gCS_RTC=1;
00142     gCS_RTC=0;
00143     response =spi.write(0x00);
00144     uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1;
00145     year = (year == 16)?0x00:(year == 17)?0x01:(year == 18)?0x02:(year == 19)?0x03:0x00;
00146     gCS_RTC=1;
00147     
00148     uint8_t Time_stamp[8] = {year, month, date, day, hours, minutes, seconds, centiseconds};
00149     
00150     time = time|(((uint64_t)(centiseconds&0x7F)));
00151     time = time|(((uint64_t)(seconds&0x3F))<<7);
00152     time = time|(((uint64_t)(minutes&0x3F))<<13);
00153     time = time|(((uint64_t)(hours&0x1F))<<19);
00154     time = time|(((uint64_t)(day&0x1F))<<24);
00155     time = time|(((uint64_t)(month&0x07))<<29);
00156     time = time|(((uint64_t)(year&0x03))<<33);
00157     time = (time&0x00000007FFFFFFFF);
00158     SPI_mutex.unlock();
00159     return time;
00160 }