Test program for DS1683, Maxim Total-Elapsed-Time and Event Recorder with Alarm.
Dependencies: DS1683 mbed vt100
testDS1683.cpp@3:7043500e6f93, 2017-04-12 (annotated)
- Committer:
- Rhyme
- Date:
- Wed Apr 12 07:24:02 2017 +0000
- Revision:
- 3:7043500e6f93
- Parent:
- 2:746993c2ea4a
- Child:
- 4:8fac856f8a67
Support for MAX32600 is under construction
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:fa3939708086 | 1 | #include "mbed.h" |
Rhyme | 0:fa3939708086 | 2 | #include "vt100.h" |
Rhyme | 0:fa3939708086 | 3 | #include "MSS.h" |
Rhyme | 0:fa3939708086 | 4 | #include "DS1683.h" |
Rhyme | 0:fa3939708086 | 5 | |
Rhyme | 0:fa3939708086 | 6 | #define MSU_DS1683_ADDRESS 0x6B |
Rhyme | 0:fa3939708086 | 7 | |
Rhyme | 0:fa3939708086 | 8 | vt100 *tty = 0 ; |
Rhyme | 0:fa3939708086 | 9 | DS1683 *ds1683 = 0 ; |
Rhyme | 0:fa3939708086 | 10 | int ds_alarm_flag = 0 ; |
Rhyme | 3:7043500e6f93 | 11 | DigitalOut ledD1(PIN_SCK, 1) ; |
Rhyme | 3:7043500e6f93 | 12 | DigitalOut ledD2(PIN_MISO, 1) ; |
Rhyme | 3:7043500e6f93 | 13 | DigitalOut ledD3(PIN_MOSI, 1) ; |
Rhyme | 3:7043500e6f93 | 14 | DigitalOut ledD4(PIN_CS0, 1) ; |
Rhyme | 0:fa3939708086 | 15 | |
Rhyme | 0:fa3939708086 | 16 | void doHelp(void) |
Rhyme | 0:fa3939708086 | 17 | { |
Rhyme | 0:fa3939708086 | 18 | printf("DS1683 test program %s\n", __DATE__) ; |
Rhyme | 1:c0fdefcb18ae | 19 | printf("event <count> : event count test\n") ; |
Rhyme | 1:c0fdefcb18ae | 20 | printf("time <sec> : elapsed time count test\n") ; |
Rhyme | 1:c0fdefcb18ae | 21 | printf("clear : clear event and etc registers\n") ; |
Rhyme | 1:c0fdefcb18ae | 22 | printf("mem : dump memory\n") ; |
Rhyme | 0:fa3939708086 | 23 | printf("write <addr> <value> : write byte data to address\n") ; |
Rhyme | 1:c0fdefcb18ae | 24 | printf("help : print this\n") ; |
Rhyme | 0:fa3939708086 | 25 | printf("\n") ; |
Rhyme | 0:fa3939708086 | 26 | } |
Rhyme | 0:fa3939708086 | 27 | |
Rhyme | 0:fa3939708086 | 28 | void ds_alarm(void) |
Rhyme | 0:fa3939708086 | 29 | { |
Rhyme | 0:fa3939708086 | 30 | ds_alarm_flag = 1 ; |
Rhyme | 0:fa3939708086 | 31 | } |
Rhyme | 0:fa3939708086 | 32 | |
Rhyme | 0:fa3939708086 | 33 | void dump_mem(void) |
Rhyme | 0:fa3939708086 | 34 | { |
Rhyme | 1:c0fdefcb18ae | 35 | int i ; |
Rhyme | 0:fa3939708086 | 36 | uint8_t data[0x30] ; |
Rhyme | 0:fa3939708086 | 37 | if (ds1683) { |
Rhyme | 1:c0fdefcb18ae | 38 | ds1683->read(0, data, 0x30) ; |
Rhyme | 2:746993c2ea4a | 39 | #if 0 |
Rhyme | 1:c0fdefcb18ae | 40 | printf(" : ") ; |
Rhyme | 1:c0fdefcb18ae | 41 | for (i = 0 ; i < 0x10 ; i++ ) { |
Rhyme | 1:c0fdefcb18ae | 42 | printf("%02X ", i) ; |
Rhyme | 1:c0fdefcb18ae | 43 | } |
Rhyme | 1:c0fdefcb18ae | 44 | printf("\n") ; |
Rhyme | 1:c0fdefcb18ae | 45 | for (i = 0 ; i < 0x30 ; i++ ) { |
Rhyme | 0:fa3939708086 | 46 | if ((i % 0x10)==0) { |
Rhyme | 0:fa3939708086 | 47 | printf("%02X : ", i) ; |
Rhyme | 0:fa3939708086 | 48 | } |
Rhyme | 0:fa3939708086 | 49 | printf("%02X ", data[i]) ; |
Rhyme | 0:fa3939708086 | 50 | if (((i+1) % 0x10)==0) { |
Rhyme | 0:fa3939708086 | 51 | printf("\n") ; |
Rhyme | 0:fa3939708086 | 52 | } |
Rhyme | 0:fa3939708086 | 53 | } |
Rhyme | 0:fa3939708086 | 54 | printf("\n") ; |
Rhyme | 2:746993c2ea4a | 55 | #else |
Rhyme | 2:746993c2ea4a | 56 | printf("EVENT PIN: ") ; |
Rhyme | 2:746993c2ea4a | 57 | if (data[1] & 0x04) { |
Rhyme | 2:746993c2ea4a | 58 | printf("ON") ; |
Rhyme | 2:746993c2ea4a | 59 | } else { |
Rhyme | 2:746993c2ea4a | 60 | printf("OFF") ; |
Rhyme | 2:746993c2ea4a | 61 | } |
Rhyme | 2:746993c2ea4a | 62 | printf("\n") ; |
Rhyme | 2:746993c2ea4a | 63 | printf("EVENT Alarm: ") ; |
Rhyme | 2:746993c2ea4a | 64 | if (data[1] & 0x02) { |
Rhyme | 2:746993c2ea4a | 65 | printf("[ON] ") ; |
Rhyme | 2:746993c2ea4a | 66 | } else { |
Rhyme | 2:746993c2ea4a | 67 | printf("[OFF] ") ; |
Rhyme | 2:746993c2ea4a | 68 | } |
Rhyme | 2:746993c2ea4a | 69 | printf("Event Counter: %02X%02X / ", data[9], data[8]) ; |
Rhyme | 2:746993c2ea4a | 70 | printf("%02X%02X\n", data[0x11], data[0x10]) ; |
Rhyme | 2:746993c2ea4a | 71 | printf("ETC Alarm: ") ; |
Rhyme | 2:746993c2ea4a | 72 | if (data[1] & 0x01) { |
Rhyme | 2:746993c2ea4a | 73 | printf("[ON] ") ; |
Rhyme | 2:746993c2ea4a | 74 | } else { |
Rhyme | 2:746993c2ea4a | 75 | printf("[OFF] ") ; |
Rhyme | 2:746993c2ea4a | 76 | } |
Rhyme | 2:746993c2ea4a | 77 | printf("ETC Counter: %02X%02X%02X%02X / ", |
Rhyme | 2:746993c2ea4a | 78 | data[0x0D], data[0x0C], data[0x0B], data[0x0A]) ; |
Rhyme | 2:746993c2ea4a | 79 | printf("%02X%02X%02X%02X\n", |
Rhyme | 2:746993c2ea4a | 80 | data[0x15], data[0x14], data[0x13], data[0x12]) ; |
Rhyme | 2:746993c2ea4a | 81 | printf("\n") ; |
Rhyme | 2:746993c2ea4a | 82 | #endif |
Rhyme | 0:fa3939708086 | 83 | } |
Rhyme | 0:fa3939708086 | 84 | } |
Rhyme | 0:fa3939708086 | 85 | |
Rhyme | 0:fa3939708086 | 86 | void init_hardware(void) |
Rhyme | 0:fa3939708086 | 87 | { |
Rhyme | 3:7043500e6f93 | 88 | #if defined (TARGET_MAX32600MBED) |
Rhyme | 3:7043500e6f93 | 89 | tty = new vt100(57600) ; |
Rhyme | 3:7043500e6f93 | 90 | DigitalIn tmpIn(P2_5, PullUp) ; |
Rhyme | 3:7043500e6f93 | 91 | DigitalOut tmpOut(P1_2) ; |
Rhyme | 3:7043500e6f93 | 92 | #else |
Rhyme | 0:fa3939708086 | 93 | tty = new vt100() ; |
Rhyme | 0:fa3939708086 | 94 | DigitalIn tmpIn(PIN_INT0, PullUp) ; |
Rhyme | 0:fa3939708086 | 95 | DigitalOut tmpOut(PIN_AN0) ; |
Rhyme | 3:7043500e6f93 | 96 | #endif |
Rhyme | 3:7043500e6f93 | 97 | tty->cls() ; |
Rhyme | 3:7043500e6f93 | 98 | |
Rhyme | 0:fa3939708086 | 99 | |
Rhyme | 0:fa3939708086 | 100 | ds1683 = new DS1683( |
Rhyme | 0:fa3939708086 | 101 | PIN_SDA, /* I2C SDA */ |
Rhyme | 0:fa3939708086 | 102 | PIN_SCL, /* I2C SCL */ |
Rhyme | 3:7043500e6f93 | 103 | // PIN_AN0, /* event GPIO output */ |
Rhyme | 3:7043500e6f93 | 104 | P1_2, /* MAX32600 only */ |
Rhyme | 3:7043500e6f93 | 105 | // PIN_INT0, /* alarm interrupt */ |
Rhyme | 3:7043500e6f93 | 106 | P2_5, |
Rhyme | 0:fa3939708086 | 107 | MSU_DS1683_ADDRESS ) ; /* 0x6B as 7bit address */ |
Rhyme | 0:fa3939708086 | 108 | ds1683->enterPW(0xFFFFFFFF) ; |
Rhyme | 0:fa3939708086 | 109 | } |
Rhyme | 0:fa3939708086 | 110 | |
Rhyme | 0:fa3939708086 | 111 | /* test Elapsed Time Counter */ |
Rhyme | 0:fa3939708086 | 112 | void doETC(int sec) |
Rhyme | 0:fa3939708086 | 113 | { |
Rhyme | 0:fa3939708086 | 114 | int loop = 0 ; |
Rhyme | 0:fa3939708086 | 115 | int time_out ; |
Rhyme | 0:fa3939708086 | 116 | ds_alarm_flag = 0 ; |
Rhyme | 0:fa3939708086 | 117 | ds1683->event = 0 ; /* set event pin inactive */ |
Rhyme | 1:c0fdefcb18ae | 118 | ds1683->clearETC() ; /* clear elapsed time counter */ |
Rhyme | 1:c0fdefcb18ae | 119 | ds1683->setETCAlarm(sec * 4) ; /* etc alarm limit sec x 250msdiv(4) */ |
Rhyme | 0:fa3939708086 | 120 | time_out = sec + 5 ; |
Rhyme | 0:fa3939708086 | 121 | ds1683->enableETCAlarm() ; |
Rhyme | 0:fa3939708086 | 122 | |
Rhyme | 1:c0fdefcb18ae | 123 | printf("ETC (Elapsed Time Count) Test for %d sec\n", sec) ; |
Rhyme | 0:fa3939708086 | 124 | ds1683->event = 1 ; /* activate event pin */ |
Rhyme | 0:fa3939708086 | 125 | while(ds_alarm_flag == 0) { |
Rhyme | 1:c0fdefcb18ae | 126 | ledD3 = 0 ; |
Rhyme | 1:c0fdefcb18ae | 127 | wait(0.5) ; |
Rhyme | 1:c0fdefcb18ae | 128 | ledD3 = 1 ; |
Rhyme | 1:c0fdefcb18ae | 129 | wait(0.5) ; |
Rhyme | 0:fa3939708086 | 130 | printf("%d sec\n", loop++) ; fflush(stdout) ; |
Rhyme | 0:fa3939708086 | 131 | |
Rhyme | 0:fa3939708086 | 132 | if (ds1683->getStatus() & 0x01) { /* ETC_AF_BIT */ |
Rhyme | 0:fa3939708086 | 133 | printf("ETC alarm detected\n") ; |
Rhyme | 1:c0fdefcb18ae | 134 | ledD1 = 0 ; /* LED1 ON */ |
Rhyme | 0:fa3939708086 | 135 | break ; |
Rhyme | 0:fa3939708086 | 136 | } else if (loop > time_out) { |
Rhyme | 0:fa3939708086 | 137 | printf("ETC Test Time Out\n") ; |
Rhyme | 0:fa3939708086 | 138 | break ; |
Rhyme | 0:fa3939708086 | 139 | } |
Rhyme | 0:fa3939708086 | 140 | } |
Rhyme | 0:fa3939708086 | 141 | ds1683->event = 0 ; |
Rhyme | 0:fa3939708086 | 142 | ds_alarm_flag = 0 ; |
Rhyme | 0:fa3939708086 | 143 | } |
Rhyme | 0:fa3939708086 | 144 | |
Rhyme | 0:fa3939708086 | 145 | /* test Event Counter */ |
Rhyme | 0:fa3939708086 | 146 | void doEvent(int num) |
Rhyme | 0:fa3939708086 | 147 | { |
Rhyme | 0:fa3939708086 | 148 | int loop = 0 ; |
Rhyme | 0:fa3939708086 | 149 | int time_out ; |
Rhyme | 0:fa3939708086 | 150 | ds_alarm_flag = 0 ; |
Rhyme | 0:fa3939708086 | 151 | ds1683->disableEventAlarm() ; |
Rhyme | 0:fa3939708086 | 152 | ds1683->clearEvent() ; |
Rhyme | 0:fa3939708086 | 153 | ds1683->clearAlarm() ; |
Rhyme | 0:fa3939708086 | 154 | ds1683->setEventAlarm(num) ; |
Rhyme | 0:fa3939708086 | 155 | time_out = num + 5 ; |
Rhyme | 1:c0fdefcb18ae | 156 | printf("Event count test for %d events\n", num) ; |
Rhyme | 0:fa3939708086 | 157 | ds1683->enableEventAlarm() ; |
Rhyme | 0:fa3939708086 | 158 | ds1683->event.write(1) ; |
Rhyme | 0:fa3939708086 | 159 | while(ds_alarm_flag == 0) { |
Rhyme | 1:c0fdefcb18ae | 160 | ledD4 = 0 ; /* LED ON */ |
Rhyme | 0:fa3939708086 | 161 | ds1683->event.write(0) ; |
Rhyme | 1:c0fdefcb18ae | 162 | printf("%d ", loop++) ; fflush(stdout) ; |
Rhyme | 1:c0fdefcb18ae | 163 | wait(0.4) ; |
Rhyme | 0:fa3939708086 | 164 | ds1683->event.write(1) ; |
Rhyme | 1:c0fdefcb18ae | 165 | ledD4 = 1 ; /* LED OFF */ |
Rhyme | 0:fa3939708086 | 166 | if (ds1683->getStatus() & 0x02) { /* EVENT_AF_BIT */ |
Rhyme | 1:c0fdefcb18ae | 167 | printf("\nEvent count alarm detected\n") ; |
Rhyme | 1:c0fdefcb18ae | 168 | ledD2 = 0 ; /* LED2 ON */ |
Rhyme | 0:fa3939708086 | 169 | break ; |
Rhyme | 0:fa3939708086 | 170 | } else if (loop > time_out) { |
Rhyme | 1:c0fdefcb18ae | 171 | printf("\nEvent count test time out\n") ; |
Rhyme | 0:fa3939708086 | 172 | break ; |
Rhyme | 0:fa3939708086 | 173 | } |
Rhyme | 0:fa3939708086 | 174 | wait(0.1) ; |
Rhyme | 0:fa3939708086 | 175 | } |
Rhyme | 0:fa3939708086 | 176 | ds1683->event = 0 ; |
Rhyme | 0:fa3939708086 | 177 | ds_alarm_flag = 0 ; |
Rhyme | 0:fa3939708086 | 178 | } |
Rhyme | 0:fa3939708086 | 179 | |
Rhyme | 0:fa3939708086 | 180 | void doCommand(char *cmd) |
Rhyme | 0:fa3939708086 | 181 | { |
Rhyme | 0:fa3939708086 | 182 | int count = 0 ; |
Rhyme | 0:fa3939708086 | 183 | int addr ; |
Rhyme | 0:fa3939708086 | 184 | int data ; |
Rhyme | 0:fa3939708086 | 185 | |
Rhyme | 0:fa3939708086 | 186 | switch(*cmd) { |
Rhyme | 0:fa3939708086 | 187 | case 'e': case 'E': /* event */ |
Rhyme | 0:fa3939708086 | 188 | scanf("%d", &count) ; |
Rhyme | 0:fa3939708086 | 189 | doEvent(count) ; |
Rhyme | 0:fa3939708086 | 190 | break ; |
Rhyme | 0:fa3939708086 | 191 | case 't': case 'T': /* ETC */ |
Rhyme | 0:fa3939708086 | 192 | scanf("%d", &count) ; |
Rhyme | 0:fa3939708086 | 193 | doETC(count) ; |
Rhyme | 0:fa3939708086 | 194 | break ; |
Rhyme | 0:fa3939708086 | 195 | case 'm': case 'M': /* dump Memory */ |
Rhyme | 0:fa3939708086 | 196 | dump_mem() ; |
Rhyme | 0:fa3939708086 | 197 | break ; |
Rhyme | 1:c0fdefcb18ae | 198 | case 'c': case 'C': /* clear registers */ |
Rhyme | 1:c0fdefcb18ae | 199 | ds1683->clearRegs() ; |
Rhyme | 1:c0fdefcb18ae | 200 | break ; |
Rhyme | 0:fa3939708086 | 201 | case 'w': case 'W': /* write byte */ |
Rhyme | 0:fa3939708086 | 202 | scanf("%x %x",&addr, &data) ; |
Rhyme | 0:fa3939708086 | 203 | ds1683->writeReg8(addr, data) ; |
Rhyme | 0:fa3939708086 | 204 | break ; |
Rhyme | 0:fa3939708086 | 205 | case 'p': case 'P': /* event pin */ |
Rhyme | 0:fa3939708086 | 206 | scanf("%d", &data) ; |
Rhyme | 0:fa3939708086 | 207 | if (data) { |
Rhyme | 0:fa3939708086 | 208 | ds1683->event = 1 ; |
Rhyme | 1:c0fdefcb18ae | 209 | ledD4 = 0 ; |
Rhyme | 0:fa3939708086 | 210 | } else { |
Rhyme | 0:fa3939708086 | 211 | ds1683->event = 0 ; |
Rhyme | 1:c0fdefcb18ae | 212 | ledD4 = 1 ; |
Rhyme | 0:fa3939708086 | 213 | } |
Rhyme | 0:fa3939708086 | 214 | break ; |
Rhyme | 0:fa3939708086 | 215 | default: |
Rhyme | 0:fa3939708086 | 216 | doHelp() ; |
Rhyme | 0:fa3939708086 | 217 | break ; |
Rhyme | 0:fa3939708086 | 218 | } |
Rhyme | 0:fa3939708086 | 219 | } |
Rhyme | 0:fa3939708086 | 220 | |
Rhyme | 1:c0fdefcb18ae | 221 | void doLEDs(void) |
Rhyme | 1:c0fdefcb18ae | 222 | { |
Rhyme | 1:c0fdefcb18ae | 223 | uint8_t status ; |
Rhyme | 1:c0fdefcb18ae | 224 | status = ds1683->getStatus() ; |
Rhyme | 1:c0fdefcb18ae | 225 | if (status & 0x01) { |
Rhyme | 1:c0fdefcb18ae | 226 | ledD1 = 0 ; |
Rhyme | 1:c0fdefcb18ae | 227 | } else { |
Rhyme | 1:c0fdefcb18ae | 228 | ledD1 = 1 ; |
Rhyme | 1:c0fdefcb18ae | 229 | } |
Rhyme | 1:c0fdefcb18ae | 230 | if (status & 0x02) { |
Rhyme | 1:c0fdefcb18ae | 231 | ledD2 = 0 ; |
Rhyme | 1:c0fdefcb18ae | 232 | } else { |
Rhyme | 1:c0fdefcb18ae | 233 | ledD2 = 1 ; |
Rhyme | 1:c0fdefcb18ae | 234 | } |
Rhyme | 1:c0fdefcb18ae | 235 | } |
Rhyme | 0:fa3939708086 | 236 | |
Rhyme | 1:c0fdefcb18ae | 237 | void prompt(void) |
Rhyme | 1:c0fdefcb18ae | 238 | { |
Rhyme | 1:c0fdefcb18ae | 239 | printf("> ") ; |
Rhyme | 1:c0fdefcb18ae | 240 | fflush(stdout) ; |
Rhyme | 1:c0fdefcb18ae | 241 | wait(0.01) ; |
Rhyme | 1:c0fdefcb18ae | 242 | doLEDs() ; |
Rhyme | 1:c0fdefcb18ae | 243 | } |
Rhyme | 0:fa3939708086 | 244 | |
Rhyme | 0:fa3939708086 | 245 | int main() { |
Rhyme | 0:fa3939708086 | 246 | char cmd[32] ; |
Rhyme | 1:c0fdefcb18ae | 247 | |
Rhyme | 0:fa3939708086 | 248 | init_hardware() ; |
Rhyme | 0:fa3939708086 | 249 | |
Rhyme | 0:fa3939708086 | 250 | doHelp() ; |
Rhyme | 0:fa3939708086 | 251 | prompt() ; |
Rhyme | 0:fa3939708086 | 252 | while(1) { |
Rhyme | 0:fa3939708086 | 253 | scanf("%s", cmd) ; |
Rhyme | 0:fa3939708086 | 254 | doCommand(cmd) ; |
Rhyme | 0:fa3939708086 | 255 | prompt() ; |
Rhyme | 0:fa3939708086 | 256 | } |
Rhyme | 0:fa3939708086 | 257 | } |