RTC on M0 and class design without rtc and target
Quote:
Example for training purposes only!!!
RTC/I2C data for target LPC11U24 Humer M0-Board and no data for others!!!
See RTC WIKI
rtc.cpp@0:397b5462e6d7, 2018-11-15 (annotated)
- Committer:
- fpucher
- Date:
- Thu Nov 15 06:55:25 2018 +0000
- Revision:
- 0:397b5462e6d7
ifdef target
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fpucher | 0:397b5462e6d7 | 1 | /*********************************** |
fpucher | 0:397b5462e6d7 | 2 | name: rtc.cpp Version: 0.2 |
fpucher | 0:397b5462e6d7 | 3 | author: PE HTL BULME |
fpucher | 0:397b5462e6d7 | 4 | email: pe@bulme.at |
fpucher | 0:397b5462e6d7 | 5 | description: |
fpucher | 0:397b5462e6d7 | 6 | Implementation portion of class rtc |
fpucher | 0:397b5462e6d7 | 7 | real time clock on himbed0 |
fpucher | 0:397b5462e6d7 | 8 | |
fpucher | 0:397b5462e6d7 | 9 | ***********************************/ |
fpucher | 0:397b5462e6d7 | 10 | |
fpucher | 0:397b5462e6d7 | 11 | #include "mbed.h" |
fpucher | 0:397b5462e6d7 | 12 | #include "const.h" |
fpucher | 0:397b5462e6d7 | 13 | #include "rtc.h" |
fpucher | 0:397b5462e6d7 | 14 | |
fpucher | 0:397b5462e6d7 | 15 | RTC::~RTC() |
fpucher | 0:397b5462e6d7 | 16 | {} |
fpucher | 0:397b5462e6d7 | 17 | |
fpucher | 0:397b5462e6d7 | 18 | #ifdef TARGET_LPC11XX |
fpucher | 0:397b5462e6d7 | 19 | // Default constructor |
fpucher | 0:397b5462e6d7 | 20 | RTC::RTC() : i2c(p28, p27) // HIMBED M0 |
fpucher | 0:397b5462e6d7 | 21 | { |
fpucher | 0:397b5462e6d7 | 22 | i2c.frequency(40000); // I2C Frequenz 40kHz |
fpucher | 0:397b5462e6d7 | 23 | char init1[2] = {0x6, 0x00}; |
fpucher | 0:397b5462e6d7 | 24 | char init2[2] = {0x7, 0xff}; |
fpucher | 0:397b5462e6d7 | 25 | i2c.write(0x40, init1, 2); |
fpucher | 0:397b5462e6d7 | 26 | i2c.write(0x40, init2, 2); |
fpucher | 0:397b5462e6d7 | 27 | |
fpucher | 0:397b5462e6d7 | 28 | } |
fpucher | 0:397b5462e6d7 | 29 | |
fpucher | 0:397b5462e6d7 | 30 | // Param. constructor with pins sda and scl |
fpucher | 0:397b5462e6d7 | 31 | RTC::RTC(PinName sda, PinName scl) : i2c(sda, scl) //_sda(sda), _scl(scl) // no I2C default constructor |
fpucher | 0:397b5462e6d7 | 32 | { |
fpucher | 0:397b5462e6d7 | 33 | i2c.frequency(40000); // I2C Frequenz 40kHz |
fpucher | 0:397b5462e6d7 | 34 | char init1[2] = {0x6, 0x00}; |
fpucher | 0:397b5462e6d7 | 35 | char init2[2] = {0x7, 0xff}; |
fpucher | 0:397b5462e6d7 | 36 | i2c.write(0x40, init1, 2); |
fpucher | 0:397b5462e6d7 | 37 | i2c.write(0x40, init2, 2); |
fpucher | 0:397b5462e6d7 | 38 | } |
fpucher | 0:397b5462e6d7 | 39 | |
fpucher | 0:397b5462e6d7 | 40 | char RTC::rtc_read(char address) |
fpucher | 0:397b5462e6d7 | 41 | { |
fpucher | 0:397b5462e6d7 | 42 | char value; |
fpucher | 0:397b5462e6d7 | 43 | i2c.start(); |
fpucher | 0:397b5462e6d7 | 44 | //i2c.write(RTC8563_ADR_RD, &address, true); |
fpucher | 0:397b5462e6d7 | 45 | i2c.write(RTC8563_ADR_WR); |
fpucher | 0:397b5462e6d7 | 46 | i2c.write(address); |
fpucher | 0:397b5462e6d7 | 47 | i2c.start(); |
fpucher | 0:397b5462e6d7 | 48 | i2c.write(RTC8563_ADR_RD); |
fpucher | 0:397b5462e6d7 | 49 | value = i2c.read(0); |
fpucher | 0:397b5462e6d7 | 50 | i2c.stop(); |
fpucher | 0:397b5462e6d7 | 51 | return value; |
fpucher | 0:397b5462e6d7 | 52 | } |
fpucher | 0:397b5462e6d7 | 53 | void RTC::rtc_write(char address, char value) |
fpucher | 0:397b5462e6d7 | 54 | { |
fpucher | 0:397b5462e6d7 | 55 | i2c.start(); |
fpucher | 0:397b5462e6d7 | 56 | //i2c.write(RTC8563_ADR_WR, &address, value, true); |
fpucher | 0:397b5462e6d7 | 57 | i2c.write(RTC8563_ADR_WR); |
fpucher | 0:397b5462e6d7 | 58 | i2c.write(address); |
fpucher | 0:397b5462e6d7 | 59 | i2c.write(value); |
fpucher | 0:397b5462e6d7 | 60 | i2c.stop(); |
fpucher | 0:397b5462e6d7 | 61 | } |
fpucher | 0:397b5462e6d7 | 62 | |
fpucher | 0:397b5462e6d7 | 63 | void RTC::rtc_init() |
fpucher | 0:397b5462e6d7 | 64 | { |
fpucher | 0:397b5462e6d7 | 65 | // Format |
fpucher | 0:397b5462e6d7 | 66 | // 2015/03/31 |
fpucher | 0:397b5462e6d7 | 67 | // 22:10:00 |
fpucher | 0:397b5462e6d7 | 68 | |
fpucher | 0:397b5462e6d7 | 69 | week_val = 0x01; // Tu |
fpucher | 0:397b5462e6d7 | 70 | rtc_write(CONTROL1, 0x20); // stop |
fpucher | 0:397b5462e6d7 | 71 | rtc_write(CONTROL2, 0x00); |
fpucher | 0:397b5462e6d7 | 72 | rtc_write(YEARS, (0x15)); |
fpucher | 0:397b5462e6d7 | 73 | rtc_write(MONTHS, (0x03)); |
fpucher | 0:397b5462e6d7 | 74 | rtc_write(DAYS, (0x31)); |
fpucher | 0:397b5462e6d7 | 75 | rtc_write(HOURS, (0x22)); |
fpucher | 0:397b5462e6d7 | 76 | rtc_write(MINUTES, (0x10)); |
fpucher | 0:397b5462e6d7 | 77 | rtc_write(SECONDS, (0x00)); |
fpucher | 0:397b5462e6d7 | 78 | rtc_write(WEEKDAYS, week_val); |
fpucher | 0:397b5462e6d7 | 79 | rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz |
fpucher | 0:397b5462e6d7 | 80 | rtc_write(TIMER_CINTROL, 0x00); |
fpucher | 0:397b5462e6d7 | 81 | rtc_write(CONTROL1, 0x00); // start |
fpucher | 0:397b5462e6d7 | 82 | } |
fpucher | 0:397b5462e6d7 | 83 | |
fpucher | 0:397b5462e6d7 | 84 | void RTC::rtc_alarm() // Setting up RTC alarm |
fpucher | 0:397b5462e6d7 | 85 | { |
fpucher | 0:397b5462e6d7 | 86 | rtc_write(CONTROL1, 0x20); // stop |
fpucher | 0:397b5462e6d7 | 87 | rtc_write(CONTROL2, 0x02); // AIE - alarm interrupt enabled |
fpucher | 0:397b5462e6d7 | 88 | rtc_write(DAY_ALARM, (0x80) ); |
fpucher | 0:397b5462e6d7 | 89 | rtc_write(HOUR_ALARM, (0x97)); |
fpucher | 0:397b5462e6d7 | 90 | rtc_write(MINUTE_ALARM, (0x02)); |
fpucher | 0:397b5462e6d7 | 91 | rtc_write(WEEKDAY_ALARM, (0x80)); |
fpucher | 0:397b5462e6d7 | 92 | rtc_write(CONTROL1, 0x00); // start |
fpucher | 0:397b5462e6d7 | 93 | } |
fpucher | 0:397b5462e6d7 | 94 | #else |
fpucher | 0:397b5462e6d7 | 95 | |
fpucher | 0:397b5462e6d7 | 96 | // Default constructor |
fpucher | 0:397b5462e6d7 | 97 | RTC::RTC() |
fpucher | 0:397b5462e6d7 | 98 | {} |
fpucher | 0:397b5462e6d7 | 99 | RTC::RTC(PinName sda, PinName scl) // : i2c(sda, scl) //_sda(sda), _scl(scl) // no I2C default constructor |
fpucher | 0:397b5462e6d7 | 100 | {} |
fpucher | 0:397b5462e6d7 | 101 | |
fpucher | 0:397b5462e6d7 | 102 | char RTC::rtc_read(char address) { |
fpucher | 0:397b5462e6d7 | 103 | return 0x21; |
fpucher | 0:397b5462e6d7 | 104 | } |
fpucher | 0:397b5462e6d7 | 105 | |
fpucher | 0:397b5462e6d7 | 106 | void RTC::rtc_write(char address, char value) |
fpucher | 0:397b5462e6d7 | 107 | { } |
fpucher | 0:397b5462e6d7 | 108 | |
fpucher | 0:397b5462e6d7 | 109 | void RTC::rtc_init() |
fpucher | 0:397b5462e6d7 | 110 | { |
fpucher | 0:397b5462e6d7 | 111 | // Format |
fpucher | 0:397b5462e6d7 | 112 | // 2015/03/31 |
fpucher | 0:397b5462e6d7 | 113 | // 22:10:00 |
fpucher | 0:397b5462e6d7 | 114 | } |
fpucher | 0:397b5462e6d7 | 115 | |
fpucher | 0:397b5462e6d7 | 116 | void RTC::rtc_alarm() // Setting up RTC alarm |
fpucher | 0:397b5462e6d7 | 117 | {} |
fpucher | 0:397b5462e6d7 | 118 | #endif |
fpucher | 0:397b5462e6d7 | 119 | |
fpucher | 0:397b5462e6d7 | 120 | //-----------------INTERNAL USE ONLY ---------------------------- |
fpucher | 0:397b5462e6d7 | 121 | void RTC::error() |
fpucher | 0:397b5462e6d7 | 122 | { |
fpucher | 0:397b5462e6d7 | 123 | } |