shubham c / Mbed 2 deprecated SBC_TEST_CDMS_CODE

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