DS3232M RTC demo program. Tested on the K64F (this example) and the KL25Z, but should work on any platform by changing the SDA and SCL pins. USB serial baud rate is 230400, tested on TeraTerm. Demo includes setting the DS3232's time, turning on and off the 32KHz and 1Hz outputs and accessing the DS3232's temperature. Also included are DS3232 user RAM access tests and user RAM CRC data calculations. Since user RAM is battery backed up, CRC routines are included to insure that the data stored in user RAM is intact.
Dependencies: ds3232m mbed mbed-rtos
Revision 4:df12939cba66, committed 2016-03-11
- Comitter:
- loopsva
- Date:
- Fri Mar 11 18:19:40 2016 +0000
- Parent:
- 3:f0eb506d4add
- Commit message:
- Updated support for v6 of ds3232m library
Changed in this revision
diff -r f0eb506d4add -r df12939cba66 ds3232m.lib --- a/ds3232m.lib Thu Dec 25 21:43:17 2014 +0000 +++ b/ds3232m.lib Fri Mar 11 18:19:40 2016 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/loopsva/code/ds3232m/#a9a8027a7cb2 +http://developer.mbed.org/users/loopsva/code/ds3232m/#968b8efe3ca0
diff -r f0eb506d4add -r df12939cba66 main.cpp --- a/main.cpp Thu Dec 25 21:43:17 2014 +0000 +++ b/main.cpp Fri Mar 11 18:19:40 2016 +0000 @@ -1,15 +1,16 @@ #include "mbed.h" -//#include "rtos.h" //mbed rtos - uncomment to make RTOS available +#include "rtos.h" //mbed rtos - uncomment to make RTOS available #include "ds3232m.h" //Maxim RTC -RawSerial pc(USBTX, USBRX); //onsole interface - #ifdef RTOS_H -Mutex MutexI2C0; //lock/unlock I2C requests +Mutex MutexI2cWait_1; //lock/unlock I2C requests +RawSerial pc(USBTX, USBRX); //console interface for RTOS +#else +Serial pc(USBTX, USBRX); //console interface #endif //RTOS_H -#define SDA0 PTE25 -#define SCL0 PTE24 +#define SDA0 D14 //assuming arduino pins +#define SCL0 D15 ds3232m rtc(SDA0, SCL0, 400000); //Maxim real time clock //--------------------------------------------------------------------------------------------------------------------------------------// @@ -34,14 +35,29 @@ char inchar = 0; //RX input character //--------------------------------------------------------------------------------------------------------------------------------------// +// various Mutex lock/unlock definitions + +void lockI2C() { +#ifdef RTOS_H + MutexI2cWait_1.lock(); +#endif //RTOS_H +} + +void unlockI2C() { +#ifdef RTOS_H + MutexI2cWait_1.unlock(); +#endif //RTOS_H +} + +//--------------------------------------------------------------------------------------------------------------------------------------// // This function is called when a character goes into the RX buffer. -#define DOUPONELINE "\033[1A" -#define BS 0x08 //ascii backspace -#define CR 0x0d //ascii CR -#define LF 0x0a //ascii LF -#define ticC 0x03 //ascii control C -#define DEGC_DEGF_FLOAT 9.0 / 5.0 + 32.0 +#define DOUPONELINE "\033[1A" +#define BS 0x08 //ascii backspace +#define CR 0x0d //ascii CR +#define LF 0x0a //ascii LF +#define ticC 0x03 //ascii control C +#define DEGC_DEGF_FLOAT 9.0 / 5.0 + 32.0 void PcRxChar(char inchar) { if(inchar == CR) pc.printf(" %c %c", BS, BS); //NOTE: Bug in K64F has issue with CR and/or LF, these 2 lines are needed @@ -76,6 +92,7 @@ // Read received chars from USB UART interrupt void PcRxIRQ(void){ +/* #if defined(TARGET_KL25Z) NVIC_DisableIRQ(UART0_IRQn); // n=0, 1 or 2 Disable Rx interrupt on kl25z #endif @@ -85,10 +102,12 @@ #if defined(TARGET_LPC1768) LPC_UART0->IER = 0; //Disable Rx interrupt on mbed1768 #endif +*/ while (pc.readable()) { - inchar = pc.getc(); //<<<<< broken here!!!! //read data from USB + inchar = pc.getc(); //read data from USB PcRxChar(inchar); //go process char } +/* #if defined(TARGET_KL25Z) NVIC_EnableIRQ(UART0_IRQn); // n=0, 1 or 2 Re-enable Rx interrupt on kl25z #endif @@ -98,6 +117,7 @@ #if defined(TARGET_LPC1768) LPC_UART0->IER = 1; //RE-enable Rx interrupt on mbed1768 #endif +*/ } //--------------------------------------------------------------------------------------------------------------------------------------// @@ -197,8 +217,10 @@ // Load the local mbed-RTC from that external DS3232 RTC void loadRTC() { + lockI2C(); //get data from DS3232 rtc.getTime(rtc_m); + unlockI2C(); t.tm_sec = rtc_m.sec; t.tm_min = rtc_m.min; t.tm_hour = rtc_m.hour; @@ -218,12 +240,14 @@ void Get3232Temp() { pc.printf("%c", BS); - if(rtc.startTempCycle() == true) { + lockI2C(); + if(rtc.startTempCycle(rtc_m) == true) { + unlockI2C(); pc.printf(" - DS3232M start converstion ok \r\n"); } else { + unlockI2C(); pc.printf(" - DS3232M start converstion BUSY!!! \r\n"); - } - + } //wait for DS3232 temperature conversion to finish int i = 80; for(i = 80; i > 0; i--) { @@ -232,14 +256,22 @@ #else wait_ms(20); #endif - if((rtc.checkTempBusy()) == true) break; + lockI2C(); + if((rtc.checkTempBusy(rtc_m)) == true) break; + unlockI2C(); } //timed out or display temperature if(i > 0) { - float ds3232tempC = rtc.getTemperature(); + float ds3232tempC = rtc.getTemperature(rtc_m); + unlockI2C(); +#if (defined(TARGET_K64F) || defined(TARGET_DISCO_F746NG)) + pc.printf(" - DS3232M Temperature: %.2fC %.1fF\r\n", (double)ds3232tempC, (double)ds3232tempC * DEGC_DEGF_FLOAT); +#else pc.printf(" - DS3232M Temperature: %.2fC %.1fF\r\n", ds3232tempC, ds3232tempC * DEGC_DEGF_FLOAT); +#endif } else { + unlockI2C(); pc.printf("*** DS3232M Temperature timeout!!!!\r\n"); } } @@ -257,19 +289,19 @@ #endif if(pcRxQty != 0) { pc.printf("\r\n"); - if(pcRxBuffer[0] == 'A') rtc.set32KhzOutput(true, false); //turn on 32KHz output - if(pcRxBuffer[0] == 'a') rtc.set32KhzOutput(false, false); //turn off 32KHz output - if(pcRxBuffer[0] == 'B') rtc.set1hzOutput(true, false); //turn on 1Hz output - if(pcRxBuffer[0] == 'b') rtc.set1hzOutput(false, false); //turn off 1Hz output + if(pcRxBuffer[0] == 'A') rtc.set32KhzOutput(rtc_m, true, false); //turn on 32KHz output + if(pcRxBuffer[0] == 'a') rtc.set32KhzOutput(rtc_m, false, false); //turn off 32KHz output + if(pcRxBuffer[0] == 'B') rtc.set1hzOutput(rtc_m, true, false); //turn on 1Hz output + if(pcRxBuffer[0] == 'b') rtc.set1hzOutput(rtc_m,false, false); //turn off 1Hz output if(pcRxBuffer[0] == 'c') { allowDisplayTime = true; SetRTC(); //change DS3232 time allowDisplayTime = false; pcRxBuffer[0] = 'c'; } - uint16_t errorValue = 0; + uint8_t errorValue = 0; if(pcRxBuffer[0] == 'g') { //get and display stored data string - errorValue = rtc.getUserRAM(pcRxBuffer, 128, 126); + errorValue = rtc.getUserRAM(pcRxBuffer, rtc_m, 128, 124); if(errorValue) { pc.printf("'Get' user RAM error: %d\r\n", errorValue); } else if(pcRxBuffer[0] == NULL) { @@ -292,7 +324,9 @@ wait_ms(20); #endif } while((pcRxLine == false) && (pcRxEOB == false)); - errorValue = rtc.putUserRAM(pcRxBuffer, 128, pcRxQty + 2); + lockI2C(); + errorValue = rtc.putUserRAM(pcRxBuffer, rtc_m, 128, pcRxQty + 2); + unlockI2C(); if(errorValue) pc.printf("'Put' user RAM error: %d\r\n", errorValue); allowDisplayTime = false; pcRxBuffer[0] = 's'; @@ -300,8 +334,10 @@ } if(pcRxBuffer[0] == 't') Get3232Temp(); //display the DS3232's temperature if(pcRxBuffer[0] == 'z') { - rtc.LoadRTCRam(); - pc.printf("CRC16 value: 0x%04X\r\n", rtc.calculateCRC16(rtc.RTCbuffer, 0x14, 0xea)); + lockI2C(); + rtc.LoadRTCRam(rtc_m); + unlockI2C(); + pc.printf("CRC16 value: 0x%04X\r\n", rtc.calculateCRC16(pcRxBuffer, rtc_m, 0x14, 0xea)); } if((pcRxBuffer[0] != 'a') && (pcRxBuffer[0] != 'A') && (pcRxBuffer[0] != 'b') && (pcRxBuffer[0] != 'B') && (pcRxBuffer[0] != 'c') && (pcRxBuffer[0] != 'g')
diff -r f0eb506d4add -r df12939cba66 mbed-rtos.lib --- a/mbed-rtos.lib Thu Dec 25 21:43:17 2014 +0000 +++ b/mbed-rtos.lib Fri Mar 11 18:19:40 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#13a25134ac60 +http://mbed.org/users/mbed_official/code/mbed-rtos/#dfc27975e193
diff -r f0eb506d4add -r df12939cba66 mbed.bld --- a/mbed.bld Thu Dec 25 21:43:17 2014 +0000 +++ b/mbed.bld Fri Mar 11 18:19:40 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb \ No newline at end of file