Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem max32630fthr USBDevice
Fork of FTHR_SD_Demo by
main.cpp@10:148da21c297e, 2018-07-02 (annotated)
- Committer:
- cyberjoey
- Date:
- Mon Jul 02 16:00:52 2018 +0000
- Revision:
- 10:148da21c297e
- Parent:
- 9:36ba3626aab7
- Child:
- 11:94a7379c0db8
fixed serial com issue on idle by modifying revisions of mbed-os, USBDevice, and other libs
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| switches | 0:60a522ae2e35 | 1 | #include "mbed.h" |
| switches | 1:90313362ec11 | 2 | #include "SDFileSystem.h" |
| cyberjoey | 9:36ba3626aab7 | 3 | #include "max32630fthr.h" |
| cyberjoey | 9:36ba3626aab7 | 4 | #include "USBSerial.h" |
| switches | 0:60a522ae2e35 | 5 | |
| cyberjoey | 9:36ba3626aab7 | 6 | |
| cyberjoey | 9:36ba3626aab7 | 7 | #define I2C_ADDRESS 0x30 // DS2484 I2C address |
| cyberjoey | 9:36ba3626aab7 | 8 | #define DS2484_ADD 0x30 // DS2484 I2C write address |
| cyberjoey | 9:36ba3626aab7 | 9 | |
| cyberjoey | 9:36ba3626aab7 | 10 | |
| cyberjoey | 9:36ba3626aab7 | 11 | #define PERIOD 10 // Logging period in seconds |
| cyberjoey | 9:36ba3626aab7 | 12 | |
| cyberjoey | 9:36ba3626aab7 | 13 | //DS2484 Status register bits |
| cyberjoey | 9:36ba3626aab7 | 14 | #define OWB 0x01 // status reg, One Wire Busy |
| cyberjoey | 9:36ba3626aab7 | 15 | #define PPD 0x02 // status reg, Presence Pulse Detected |
| cyberjoey | 9:36ba3626aab7 | 16 | #define SD 0x04 // status reg, Short Detected |
| cyberjoey | 9:36ba3626aab7 | 17 | #define LL 0x08 // status reg, Logic Level |
| cyberjoey | 9:36ba3626aab7 | 18 | #define RST 0x10 // status reg, Device Reset (DS2484 Reset) |
| cyberjoey | 9:36ba3626aab7 | 19 | #define SBR 0x20 // status reg, Single Bit Result |
| cyberjoey | 9:36ba3626aab7 | 20 | #define TSB 0x40 // status reg, Triplet Second Bit |
| cyberjoey | 9:36ba3626aab7 | 21 | #define DIR 0x80 // status reg, Branch Direction Taken |
| cyberjoey | 9:36ba3626aab7 | 22 | //DS2484 Command bytes |
| cyberjoey | 9:36ba3626aab7 | 23 | #define RESET 0xF0 // DS2484 Reset |
| cyberjoey | 9:36ba3626aab7 | 24 | #define SRP 0xE1 // DS2484 Set Read Pointer |
| cyberjoey | 9:36ba3626aab7 | 25 | #define WDC 0xD2 // DS2484 Write Device Configuration |
| cyberjoey | 9:36ba3626aab7 | 26 | #define ACP 0xC3 // DS2484 Adjust one wire Control Port |
| cyberjoey | 9:36ba3626aab7 | 27 | #define OW_RESET 0xB4 // DS2484 One Wire Reset |
| cyberjoey | 9:36ba3626aab7 | 28 | #define OW_SB 0x87 // One Wire Single Bit (write or read) |
| cyberjoey | 9:36ba3626aab7 | 29 | #define OWWB 0xA5 // One Wire Write Byte |
| cyberjoey | 9:36ba3626aab7 | 30 | #define OWRB 0x96 // One Wire Read Byte |
| cyberjoey | 9:36ba3626aab7 | 31 | #define OWT 0x78 // One Wire Triplet (read 2 bits, write 1 bit) |
| cyberjoey | 9:36ba3626aab7 | 32 | //One Wire Commands |
| cyberjoey | 9:36ba3626aab7 | 33 | #define OW_SEARCH 0xF0 // Search Rom |
| cyberjoey | 9:36ba3626aab7 | 34 | #define OW_READ 0x33 // Read Rom |
| cyberjoey | 9:36ba3626aab7 | 35 | #define OW_MATCH 0x55 // Match Rom |
| cyberjoey | 9:36ba3626aab7 | 36 | #define OW_SKIP 0xCC // Skip Rom |
| cyberjoey | 9:36ba3626aab7 | 37 | #define OW_OD_MATCH 0x69 // Match Rom |
| cyberjoey | 9:36ba3626aab7 | 38 | #define OW_OD_SKIP 0x3C // Skip Rom |
| cyberjoey | 9:36ba3626aab7 | 39 | #define OW_ALARM 0xEC // Alarm Search |
| cyberjoey | 9:36ba3626aab7 | 40 | // OT07 OW commands |
| cyberjoey | 9:36ba3626aab7 | 41 | #define OT07_OW_CONVERT 0x44 //OT07 OW convert temperature command |
| cyberjoey | 9:36ba3626aab7 | 42 | #define OT07_OW_WRITE 0xCC // OT07 OW Write Register command |
| cyberjoey | 9:36ba3626aab7 | 43 | #define OT07_OW_READ 0x33 // OT07 OW Read Register command |
| cyberjoey | 9:36ba3626aab7 | 44 | #define OT07_OW_RESET 0x82 // OT07 OW Soft Reset command |
| switches | 3:247c750f9f3d | 45 | |
| cyberjoey | 9:36ba3626aab7 | 46 | //OT07 Registers |
| cyberjoey | 9:36ba3626aab7 | 47 | #define OT07_STATUS 0x00 // OT07 status regiter |
| cyberjoey | 9:36ba3626aab7 | 48 | #define OT07_INT_EN 0x01 // OT07 Interrupt Enable |
| cyberjoey | 9:36ba3626aab7 | 49 | #define OT07_FIFO_W 0x04 // OT07 FIFO Write Pointer |
| cyberjoey | 9:36ba3626aab7 | 50 | #define OT07_FIFO_R 0x05 // OT07 FIFO Read Pointer |
| cyberjoey | 9:36ba3626aab7 | 51 | #define OT07_FIFO_OF 0x06 // OT07 FIFO Overflow Counter |
| cyberjoey | 9:36ba3626aab7 | 52 | #define OT07_FIFO_COUNT 0x07 // OT07 FIFO Data Count |
| cyberjoey | 9:36ba3626aab7 | 53 | #define OT07_FIFO_DATA 0x08 // OT07 FIFO Data |
| cyberjoey | 9:36ba3626aab7 | 54 | #define OT07_FIFO_CNFG1 0x09 // OT07 FIFO Configuration 1 (FIFO_A_FULL) |
| cyberjoey | 9:36ba3626aab7 | 55 | #define OT07_FIFO_CNFG2 0x0A // OT07 FIFO Configuration 2 |
| cyberjoey | 9:36ba3626aab7 | 56 | #define OT07_SYS 0x0C // OT07 System Configuration |
| cyberjoey | 9:36ba3626aab7 | 57 | #define OT07_ALARM_HIGH_MSB 0x10 // OT07 Alarm High MSB |
| cyberjoey | 9:36ba3626aab7 | 58 | #define OT07_ALARM_HIGH_LSB 0x11 // OT07 Alarm High LSB |
| cyberjoey | 9:36ba3626aab7 | 59 | #define OT07_ALARM_LOW_MSB 0x12 // OT07 Alarm Low MSB |
| cyberjoey | 9:36ba3626aab7 | 60 | #define OT07_ALARM_LOW_LSB 0x13 // OT07 Alarm LOW LSB |
| cyberjoey | 9:36ba3626aab7 | 61 | #define OT07_ADC_BITS 0x14 // OT07 Temp Seneor Setup (ADC_RES[7:6]) |
| cyberjoey | 9:36ba3626aab7 | 62 | #define OT07_GPIO_SETUP 0x20 // OT07 GPIO Setup, sets GPIO modes |
| cyberjoey | 9:36ba3626aab7 | 63 | #define Ot07_GPIO_CTRL 0x21 // OT07 GPIO control |
| cyberjoey | 9:36ba3626aab7 | 64 | #define OT07_ROM_ID 0x30 // OT07 ROM_ID address of LSB? |
| cyberjoey | 9:36ba3626aab7 | 65 | |
| cyberjoey | 9:36ba3626aab7 | 66 | |
| cyberjoey | 9:36ba3626aab7 | 67 | #define MAX_DEVICES 64 // Maximum number of rom devices allowed |
| cyberjoey | 9:36ba3626aab7 | 68 | #define ID_LENGTH 8 // Rom ID length in bytes |
| cyberjoey | 9:36ba3626aab7 | 69 | |
| cyberjoey | 9:36ba3626aab7 | 70 | |
| cyberjoey | 9:36ba3626aab7 | 71 | #define BS 8 // ASCII Back Space |
| cyberjoey | 9:36ba3626aab7 | 72 | #define CR 13 // ASCII Carriage Return |
| cyberjoey | 9:36ba3626aab7 | 73 | |
| cyberjoey | 10:148da21c297e | 74 | const char* settings_file = "/sd/settings.txt"; |
| cyberjoey | 10:148da21c297e | 75 | const char* log_file = "/sd/MAX30207Log.csv"; |
| cyberjoey | 10:148da21c297e | 76 | |
| cyberjoey | 9:36ba3626aab7 | 77 | //global variable |
| cyberjoey | 9:36ba3626aab7 | 78 | |
| cyberjoey | 9:36ba3626aab7 | 79 | //******************** init Feather Boared Hardware *********************** |
| cyberjoey | 9:36ba3626aab7 | 80 | |
| cyberjoey | 9:36ba3626aab7 | 81 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
| cyberjoey | 9:36ba3626aab7 | 82 | |
| cyberjoey | 9:36ba3626aab7 | 83 | //microSD logging file system setup |
| cyberjoey | 9:36ba3626aab7 | 84 | SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); // mosi, miso, sclk, cs |
| cyberjoey | 9:36ba3626aab7 | 85 | |
| cyberjoey | 9:36ba3626aab7 | 86 | //SD card insertion detection pin |
| cyberjoey | 9:36ba3626aab7 | 87 | DigitalIn SDDetect(P2_2, PullUp); |
| cyberjoey | 9:36ba3626aab7 | 88 | |
| cyberjoey | 9:36ba3626aab7 | 89 | |
| cyberjoey | 9:36ba3626aab7 | 90 | // Virtual serial port over USB |
| cyberjoey | 9:36ba3626aab7 | 91 | //USBSerial pc; |
| cyberjoey | 9:36ba3626aab7 | 92 | USBSerial pc(0x0B6A, 0x0042, 0x0001, true); |
| cyberjoey | 9:36ba3626aab7 | 93 | |
| cyberjoey | 9:36ba3626aab7 | 94 | // I2C setup |
| cyberjoey | 9:36ba3626aab7 | 95 | I2C i2c(P3_4,P3_5); // P3_4 -> I2C1_SDA, P3_5-> I2C1_SCL |
| cyberjoey | 9:36ba3626aab7 | 96 | |
| cyberjoey | 9:36ba3626aab7 | 97 | //Timer setup |
| cyberjoey | 9:36ba3626aab7 | 98 | Ticker timer_1; // timer for blinking led heartbeat |
| cyberjoey | 9:36ba3626aab7 | 99 | |
| cyberjoey | 9:36ba3626aab7 | 100 | bool tick_flag; // used for counting seconds |
| cyberjoey | 9:36ba3626aab7 | 101 | bool log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 102 | bool led_toggle_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 103 | bool button_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 104 | bool error_flag; |
| cyberjoey | 9:36ba3626aab7 | 105 | int error_ticks; |
| cyberjoey | 9:36ba3626aab7 | 106 | |
| cyberjoey | 9:36ba3626aab7 | 107 | //LED blink setup |
| switches | 1:90313362ec11 | 108 | DigitalOut rLED(LED1); |
| switches | 1:90313362ec11 | 109 | DigitalOut gLED(LED2); |
| cyberjoey | 9:36ba3626aab7 | 110 | DigitalOut bLED(LED3); |
| switches | 0:60a522ae2e35 | 111 | |
| cyberjoey | 9:36ba3626aab7 | 112 | InterruptIn button(SW1); |
| cyberjoey | 9:36ba3626aab7 | 113 | |
| cyberjoey | 9:36ba3626aab7 | 114 | |
| cyberjoey | 9:36ba3626aab7 | 115 | void LED_blink_callback(){ // LED Heart beat |
| cyberjoey | 9:36ba3626aab7 | 116 | |
| cyberjoey | 9:36ba3626aab7 | 117 | led_toggle_flag = !led_toggle_flag; |
| cyberjoey | 9:36ba3626aab7 | 118 | |
| cyberjoey | 9:36ba3626aab7 | 119 | if(log_flag) //if logging |
| cyberjoey | 9:36ba3626aab7 | 120 | { |
| cyberjoey | 9:36ba3626aab7 | 121 | |
| cyberjoey | 9:36ba3626aab7 | 122 | if(led_toggle_flag) |
| cyberjoey | 9:36ba3626aab7 | 123 | { |
| cyberjoey | 9:36ba3626aab7 | 124 | //toggle red led |
| cyberjoey | 9:36ba3626aab7 | 125 | rLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 126 | } |
| cyberjoey | 9:36ba3626aab7 | 127 | else |
| cyberjoey | 9:36ba3626aab7 | 128 | { |
| cyberjoey | 9:36ba3626aab7 | 129 | rLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 130 | gLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 131 | bLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 132 | } |
| cyberjoey | 9:36ba3626aab7 | 133 | } |
| cyberjoey | 9:36ba3626aab7 | 134 | else if(error_flag) //if error (no sd card) |
| cyberjoey | 9:36ba3626aab7 | 135 | { |
| cyberjoey | 9:36ba3626aab7 | 136 | if(led_toggle_flag) |
| cyberjoey | 9:36ba3626aab7 | 137 | { |
| cyberjoey | 9:36ba3626aab7 | 138 | //toggle red led |
| cyberjoey | 9:36ba3626aab7 | 139 | rLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 140 | gLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 141 | } |
| cyberjoey | 9:36ba3626aab7 | 142 | else |
| cyberjoey | 9:36ba3626aab7 | 143 | { |
| cyberjoey | 9:36ba3626aab7 | 144 | rLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 145 | gLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 146 | bLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 147 | } |
| cyberjoey | 9:36ba3626aab7 | 148 | error_ticks--; |
| cyberjoey | 9:36ba3626aab7 | 149 | if(error_ticks <= 0) |
| cyberjoey | 9:36ba3626aab7 | 150 | error_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 151 | } |
| cyberjoey | 9:36ba3626aab7 | 152 | else |
| cyberjoey | 9:36ba3626aab7 | 153 | { |
| cyberjoey | 9:36ba3626aab7 | 154 | if(led_toggle_flag) |
| cyberjoey | 9:36ba3626aab7 | 155 | { |
| cyberjoey | 9:36ba3626aab7 | 156 | //toggle teal leds |
| cyberjoey | 9:36ba3626aab7 | 157 | gLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 158 | bLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 159 | } |
| cyberjoey | 9:36ba3626aab7 | 160 | else |
| cyberjoey | 9:36ba3626aab7 | 161 | { |
| cyberjoey | 9:36ba3626aab7 | 162 | rLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 163 | gLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 164 | bLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 165 | } |
| cyberjoey | 9:36ba3626aab7 | 166 | } |
| cyberjoey | 9:36ba3626aab7 | 167 | |
| cyberjoey | 9:36ba3626aab7 | 168 | tick_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 169 | } |
| cyberjoey | 9:36ba3626aab7 | 170 | |
| cyberjoey | 9:36ba3626aab7 | 171 | void btn_pressed() //button pressed isr |
| cyberjoey | 9:36ba3626aab7 | 172 | { |
| cyberjoey | 9:36ba3626aab7 | 173 | button_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 174 | } |
| cyberjoey | 9:36ba3626aab7 | 175 | |
| cyberjoey | 9:36ba3626aab7 | 176 | /* |
| cyberjoey | 9:36ba3626aab7 | 177 | void take_data_flag_callback(){ // set data sample time flag |
| cyberjoey | 9:36ba3626aab7 | 178 | |
| cyberjoey | 9:36ba3626aab7 | 179 | take_data_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 180 | } |
| cyberjoey | 9:36ba3626aab7 | 181 | */ |
| cyberjoey | 9:36ba3626aab7 | 182 | |
| cyberjoey | 9:36ba3626aab7 | 183 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 184 | // Define CRC-8 Table |
| cyberjoey | 9:36ba3626aab7 | 185 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 186 | |
| cyberjoey | 9:36ba3626aab7 | 187 | static unsigned char crc_table[] = { |
| cyberjoey | 9:36ba3626aab7 | 188 | 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, |
| cyberjoey | 9:36ba3626aab7 | 189 | 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, |
| cyberjoey | 9:36ba3626aab7 | 190 | 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, |
| cyberjoey | 9:36ba3626aab7 | 191 | 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, |
| cyberjoey | 9:36ba3626aab7 | 192 | 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, |
| cyberjoey | 9:36ba3626aab7 | 193 | 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, |
| cyberjoey | 9:36ba3626aab7 | 194 | 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, |
| cyberjoey | 9:36ba3626aab7 | 195 | 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, |
| cyberjoey | 9:36ba3626aab7 | 196 | 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, |
| cyberjoey | 9:36ba3626aab7 | 197 | 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, |
| cyberjoey | 9:36ba3626aab7 | 198 | 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, |
| cyberjoey | 9:36ba3626aab7 | 199 | 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, |
| cyberjoey | 9:36ba3626aab7 | 200 | 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, |
| cyberjoey | 9:36ba3626aab7 | 201 | 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, |
| cyberjoey | 9:36ba3626aab7 | 202 | 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, |
| cyberjoey | 9:36ba3626aab7 | 203 | 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; |
| cyberjoey | 9:36ba3626aab7 | 204 | |
| cyberjoey | 9:36ba3626aab7 | 205 | |
| cyberjoey | 9:36ba3626aab7 | 206 | |
| cyberjoey | 9:36ba3626aab7 | 207 | // ----------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 208 | // Calculate the CRC8 of the byte value provided with the current total |
| cyberjoey | 9:36ba3626aab7 | 209 | // calc_crc8(unsigned char, unsigned char) crc8 total, byte to add |
| cyberjoey | 9:36ba3626aab7 | 210 | // pass crc8 total = 0 to start new crc sum. |
| cyberjoey | 9:36ba3626aab7 | 211 | |
| cyberjoey | 9:36ba3626aab7 | 212 | // Returns new crc8 total |
| cyberjoey | 9:36ba3626aab7 | 213 | // ----------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 214 | |
| cyberjoey | 9:36ba3626aab7 | 215 | unsigned char calc_crc8(unsigned char crc8, unsigned char value){ |
| cyberjoey | 9:36ba3626aab7 | 216 | |
| cyberjoey | 9:36ba3626aab7 | 217 | return crc_table[crc8 ^ value]; |
| cyberjoey | 9:36ba3626aab7 | 218 | } |
| cyberjoey | 9:36ba3626aab7 | 219 | |
| cyberjoey | 9:36ba3626aab7 | 220 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 221 | // OW_reset() |
| cyberjoey | 9:36ba3626aab7 | 222 | // returns 0 no device present, 1 device present |
| cyberjoey | 9:36ba3626aab7 | 223 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 224 | |
| cyberjoey | 9:36ba3626aab7 | 225 | int OW_reset(){ |
| cyberjoey | 9:36ba3626aab7 | 226 | char data[2]; // define as minimun array size so 'data' is a pointer |
| cyberjoey | 9:36ba3626aab7 | 227 | data[0] = OW_RESET; // One Wire Reset 0xB4 |
| cyberjoey | 9:36ba3626aab7 | 228 | i2c.write(DS2484_ADD,data,1,0); // write reset command to DS2484 |
| cyberjoey | 9:36ba3626aab7 | 229 | wait_ms(1); |
| cyberjoey | 9:36ba3626aab7 | 230 | do{ // poll OW-busy |
| cyberjoey | 9:36ba3626aab7 | 231 | i2c.read(DS2484_ADD,data,1); // read back status byte from DS2484 |
| cyberjoey | 9:36ba3626aab7 | 232 | }while(OWB & data[0]); |
| cyberjoey | 9:36ba3626aab7 | 233 | if(PPD & data[0])return 1; // Presence Pulse Detected bit true? |
| cyberjoey | 9:36ba3626aab7 | 234 | return 0; // else |
| cyberjoey | 9:36ba3626aab7 | 235 | } // end OW_reset() |
| cyberjoey | 9:36ba3626aab7 | 236 | |
| cyberjoey | 9:36ba3626aab7 | 237 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 238 | // OW_write_byte(char) takes char to send over one wire |
| cyberjoey | 9:36ba3626aab7 | 239 | // returns DS2484 status byte |
| cyberjoey | 9:36ba3626aab7 | 240 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 241 | |
| cyberjoey | 9:36ba3626aab7 | 242 | char OW_write_byte(char byte){ |
| cyberjoey | 9:36ba3626aab7 | 243 | |
| cyberjoey | 9:36ba3626aab7 | 244 | char data[2]; |
| cyberjoey | 9:36ba3626aab7 | 245 | data[0] = 0xA5; |
| cyberjoey | 9:36ba3626aab7 | 246 | data[1] = byte; |
| cyberjoey | 9:36ba3626aab7 | 247 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 248 | i2c.read(DS2484_ADD,data,1); // read status byte |
| cyberjoey | 9:36ba3626aab7 | 249 | |
| cyberjoey | 9:36ba3626aab7 | 250 | // db.printf("[%02X]",byte); |
| cyberjoey | 9:36ba3626aab7 | 251 | wait_us(700); // assumes normal speed OW write |
| cyberjoey | 9:36ba3626aab7 | 252 | return data[0]; // return DS2484 Status byte |
| cyberjoey | 9:36ba3626aab7 | 253 | |
| cyberjoey | 9:36ba3626aab7 | 254 | }// end OW_write_byte() |
| cyberjoey | 9:36ba3626aab7 | 255 | |
| cyberjoey | 9:36ba3626aab7 | 256 | |
| cyberjoey | 9:36ba3626aab7 | 257 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 258 | // OW_read_byte(char *, int) char* (save data pointer) |
| cyberjoey | 9:36ba3626aab7 | 259 | // int (number of bytes to read) |
| cyberjoey | 9:36ba3626aab7 | 260 | // returns 0x00 |
| cyberjoey | 9:36ba3626aab7 | 261 | // |
| cyberjoey | 9:36ba3626aab7 | 262 | // assumes OW_reset, OW_match rom, and OW device register read address already sent. |
| cyberjoey | 9:36ba3626aab7 | 263 | // |
| cyberjoey | 9:36ba3626aab7 | 264 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 265 | |
| cyberjoey | 9:36ba3626aab7 | 266 | char OW_read_byte(char* a, int n){ |
| cyberjoey | 9:36ba3626aab7 | 267 | |
| cyberjoey | 9:36ba3626aab7 | 268 | char data[2]; |
| cyberjoey | 9:36ba3626aab7 | 269 | int i; |
| cyberjoey | 9:36ba3626aab7 | 270 | |
| cyberjoey | 9:36ba3626aab7 | 271 | //read n bytes of data |
| cyberjoey | 9:36ba3626aab7 | 272 | i=0; |
| cyberjoey | 9:36ba3626aab7 | 273 | do{ |
| cyberjoey | 9:36ba3626aab7 | 274 | data[0] = OWRB; // send 0x96; DS2484 command, OW read byte |
| cyberjoey | 9:36ba3626aab7 | 275 | i2c.write(DS2484_ADD,data,1,0); |
| cyberjoey | 9:36ba3626aab7 | 276 | wait_us(600); |
| cyberjoey | 9:36ba3626aab7 | 277 | do{ |
| cyberjoey | 9:36ba3626aab7 | 278 | i2c.read(DS2484_ADD,data,1); // read status byte and test OW-busy? |
| cyberjoey | 9:36ba3626aab7 | 279 | }while(data[0]&0x01 == 1); // status bit[0] set --> OW busy |
| cyberjoey | 9:36ba3626aab7 | 280 | |
| cyberjoey | 9:36ba3626aab7 | 281 | //get byte from DS2484 |
| cyberjoey | 9:36ba3626aab7 | 282 | data[0] = SRP; // send 0xE1, set read pointer |
| cyberjoey | 9:36ba3626aab7 | 283 | data[1] = 0xE1; // set read pointer address 0xE1 read OW data |
| cyberjoey | 9:36ba3626aab7 | 284 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 285 | i2c.read(DS2484_ADD,data,1); // read bytes from OW register |
| cyberjoey | 9:36ba3626aab7 | 286 | |
| cyberjoey | 9:36ba3626aab7 | 287 | a[i] = data[0]; |
| cyberjoey | 9:36ba3626aab7 | 288 | |
| cyberjoey | 9:36ba3626aab7 | 289 | i++; |
| cyberjoey | 9:36ba3626aab7 | 290 | //pc.printf("[%02X] ",data[0]); |
| cyberjoey | 9:36ba3626aab7 | 291 | }while(i<n); |
| cyberjoey | 9:36ba3626aab7 | 292 | //for(i=0;i<n;i++) db.printf("OW_read_byte - [%2d][%02X]\r\n",i,a[i]); |
| cyberjoey | 9:36ba3626aab7 | 293 | return 0x00; |
| cyberjoey | 9:36ba3626aab7 | 294 | }// end OW_read_byte() |
| cyberjoey | 9:36ba3626aab7 | 295 | |
| cyberjoey | 9:36ba3626aab7 | 296 | |
| cyberjoey | 9:36ba3626aab7 | 297 | |
| cyberjoey | 9:36ba3626aab7 | 298 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 299 | // OW_match_rom(char *) pointer to device_id byte array |
| cyberjoey | 9:36ba3626aab7 | 300 | // returns 0x00 |
| cyberjoey | 9:36ba3626aab7 | 301 | // assumes OW_reset alread sent |
| cyberjoey | 9:36ba3626aab7 | 302 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 303 | |
| cyberjoey | 9:36ba3626aab7 | 304 | char OW_match_rom(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 305 | |
| cyberjoey | 9:36ba3626aab7 | 306 | int i; |
| cyberjoey | 9:36ba3626aab7 | 307 | //Match rom |
| cyberjoey | 9:36ba3626aab7 | 308 | OW_write_byte(OW_MATCH); // send 0x55, match rom command |
| cyberjoey | 9:36ba3626aab7 | 309 | //send rom code for device |
| cyberjoey | 9:36ba3626aab7 | 310 | for(i=0;i<8;i++){ |
| cyberjoey | 9:36ba3626aab7 | 311 | OW_write_byte(device_id[i]); //I2C write ow byte |
| cyberjoey | 9:36ba3626aab7 | 312 | } |
| cyberjoey | 9:36ba3626aab7 | 313 | return 0x00; |
| cyberjoey | 9:36ba3626aab7 | 314 | |
| cyberjoey | 9:36ba3626aab7 | 315 | }// end OW_macth_rom |
| cyberjoey | 9:36ba3626aab7 | 316 | |
| cyberjoey | 9:36ba3626aab7 | 317 | |
| cyberjoey | 9:36ba3626aab7 | 318 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 319 | // search_rom(char *) pointer to start of 2D array rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 320 | // returns number of devices found |
| cyberjoey | 9:36ba3626aab7 | 321 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 322 | |
| cyberjoey | 9:36ba3626aab7 | 323 | |
| cyberjoey | 9:36ba3626aab7 | 324 | int search_rom(char rom_id_list[MAX_DEVICES][ID_LENGTH]){ // searches for all device on OW bus, returns # found |
| cyberjoey | 9:36ba3626aab7 | 325 | |
| cyberjoey | 9:36ba3626aab7 | 326 | int bit_num; |
| cyberjoey | 9:36ba3626aab7 | 327 | int byte_num; |
| cyberjoey | 9:36ba3626aab7 | 328 | int last_zero; |
| cyberjoey | 9:36ba3626aab7 | 329 | int last_discrep; |
| cyberjoey | 9:36ba3626aab7 | 330 | int search_dir; |
| cyberjoey | 9:36ba3626aab7 | 331 | int rom_count; |
| cyberjoey | 9:36ba3626aab7 | 332 | char byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 333 | int last_device; |
| cyberjoey | 9:36ba3626aab7 | 334 | int i; |
| cyberjoey | 9:36ba3626aab7 | 335 | |
| cyberjoey | 9:36ba3626aab7 | 336 | char rom_id[8]; //used in rom search |
| cyberjoey | 9:36ba3626aab7 | 337 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 338 | char crc; |
| cyberjoey | 9:36ba3626aab7 | 339 | |
| cyberjoey | 9:36ba3626aab7 | 340 | //init for first search only |
| cyberjoey | 9:36ba3626aab7 | 341 | last_device = 0; |
| cyberjoey | 9:36ba3626aab7 | 342 | last_discrep = 0; |
| cyberjoey | 9:36ba3626aab7 | 343 | rom_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 344 | |
| cyberjoey | 9:36ba3626aab7 | 345 | for(i=0;i<8;i++)rom_id[i] = 0x00; //clear rom_id |
| cyberjoey | 9:36ba3626aab7 | 346 | |
| cyberjoey | 9:36ba3626aab7 | 347 | do{ // loop for each rom search (end when last_device = 1) |
| cyberjoey | 9:36ba3626aab7 | 348 | //db.printf("\r\nsearch next rom [%2d]",rom_count); |
| cyberjoey | 9:36ba3626aab7 | 349 | |
| cyberjoey | 9:36ba3626aab7 | 350 | //init variable for each search |
| cyberjoey | 9:36ba3626aab7 | 351 | bit_num = 1; |
| cyberjoey | 9:36ba3626aab7 | 352 | byte_num = 0; |
| cyberjoey | 9:36ba3626aab7 | 353 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 354 | last_zero = 0; |
| cyberjoey | 9:36ba3626aab7 | 355 | |
| cyberjoey | 9:36ba3626aab7 | 356 | |
| cyberjoey | 9:36ba3626aab7 | 357 | //OW reset |
| cyberjoey | 9:36ba3626aab7 | 358 | if(OW_reset()){ //returns 1 if at least one device present |
| cyberjoey | 9:36ba3626aab7 | 359 | //test if last device found |
| cyberjoey | 9:36ba3626aab7 | 360 | if(last_device!=1){ // more devices to find |
| cyberjoey | 9:36ba3626aab7 | 361 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 362 | OW_write_byte(OW_SEARCH); //send 0xF0 over one wire to init search rom |
| cyberjoey | 9:36ba3626aab7 | 363 | |
| cyberjoey | 9:36ba3626aab7 | 364 | do{ |
| cyberjoey | 9:36ba3626aab7 | 365 | //determine search direction |
| cyberjoey | 9:36ba3626aab7 | 366 | if(bit_num < last_discrep){ //before last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 367 | if((rom_id[byte_num] & byte_mask) > 0){// use last rom path |
| cyberjoey | 9:36ba3626aab7 | 368 | search_dir = 1; // last path was 1 |
| cyberjoey | 9:36ba3626aab7 | 369 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 370 | search_dir = 0; // last path was 0 |
| cyberjoey | 9:36ba3626aab7 | 371 | } |
| cyberjoey | 9:36ba3626aab7 | 372 | }else{ // at or past last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 373 | if(bit_num == last_discrep){ // at last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 374 | search_dir = 1; //been here before so use 1 |
| cyberjoey | 9:36ba3626aab7 | 375 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 376 | search_dir = 0; //past last discrepancy so use 0 |
| cyberjoey | 9:36ba3626aab7 | 377 | } |
| cyberjoey | 9:36ba3626aab7 | 378 | } |
| cyberjoey | 9:36ba3626aab7 | 379 | |
| cyberjoey | 9:36ba3626aab7 | 380 | //write direction bit and get reply |
| cyberjoey | 9:36ba3626aab7 | 381 | data[0] = 0x78; |
| cyberjoey | 9:36ba3626aab7 | 382 | data[1] = search_dir << 7; //sets bit 7 of status reg to search_dir |
| cyberjoey | 9:36ba3626aab7 | 383 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 384 | |
| cyberjoey | 9:36ba3626aab7 | 385 | wait_us(250); |
| cyberjoey | 9:36ba3626aab7 | 386 | |
| cyberjoey | 9:36ba3626aab7 | 387 | //read in rom_id bits |
| cyberjoey | 9:36ba3626aab7 | 388 | i2c.read(DS2484_ADD,data,1); // read status byte |
| cyberjoey | 9:36ba3626aab7 | 389 | |
| cyberjoey | 9:36ba3626aab7 | 390 | //(todo) check for no device error here |
| cyberjoey | 9:36ba3626aab7 | 391 | |
| cyberjoey | 9:36ba3626aab7 | 392 | //get search direction used by triplet command |
| cyberjoey | 9:36ba3626aab7 | 393 | |
| cyberjoey | 9:36ba3626aab7 | 394 | if(data[0]&0x80){ //true --> DIR bit = 1 |
| cyberjoey | 9:36ba3626aab7 | 395 | search_dir = 1; |
| cyberjoey | 9:36ba3626aab7 | 396 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 397 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 398 | } |
| cyberjoey | 9:36ba3626aab7 | 399 | |
| cyberjoey | 9:36ba3626aab7 | 400 | //test for discrepancy (both 0 and 1 present at current bit number)(TSB = 0 (1's present) and SBR = 0 (0's present)) |
| cyberjoey | 9:36ba3626aab7 | 401 | if(!(data[0] & 0x60)){ // true --> discrepancy exist, both TSB and SBR = 0 |
| cyberjoey | 9:36ba3626aab7 | 402 | if(search_dir == 0)last_zero = bit_num; |
| cyberjoey | 9:36ba3626aab7 | 403 | } |
| cyberjoey | 9:36ba3626aab7 | 404 | if(search_dir == 1){ // write search dir to rom bit |
| cyberjoey | 9:36ba3626aab7 | 405 | rom_id[byte_num] |= byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 406 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 407 | rom_id[byte_num] &= ~byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 408 | } |
| cyberjoey | 9:36ba3626aab7 | 409 | |
| cyberjoey | 9:36ba3626aab7 | 410 | //increment bit_num and byte_num if needed |
| cyberjoey | 9:36ba3626aab7 | 411 | bit_num++; |
| cyberjoey | 9:36ba3626aab7 | 412 | byte_mask <<= 1; //rotate 1 bit left |
| cyberjoey | 9:36ba3626aab7 | 413 | if(byte_mask == 0){ //if bit shifts out of byte set byte to 1 |
| cyberjoey | 9:36ba3626aab7 | 414 | byte_num++; //also increnent byt num |
| cyberjoey | 9:36ba3626aab7 | 415 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 416 | } |
| cyberjoey | 9:36ba3626aab7 | 417 | // pc.printf("sdx[%d] lz[%2d] byte[%d] mask[%02X] bn[%d] ROM[%02X %02X %02X]\n\r",search_dir,last_zero,byte_num,byte_mask, byte_num, rom_id[2], rom_id[1], rom_id[0]); |
| cyberjoey | 9:36ba3626aab7 | 418 | |
| cyberjoey | 9:36ba3626aab7 | 419 | |
| cyberjoey | 9:36ba3626aab7 | 420 | }while(bit_num<65); //bit nun started at 1 so end after 64 |
| cyberjoey | 9:36ba3626aab7 | 421 | last_discrep = last_zero; |
| cyberjoey | 9:36ba3626aab7 | 422 | if(last_discrep == 0)last_device = 1; |
| cyberjoey | 9:36ba3626aab7 | 423 | //copy rom_id into rom_id_list and calc crc of first 7 bytes |
| cyberjoey | 9:36ba3626aab7 | 424 | crc = 0x00; // reset crc8 total to 0 |
| cyberjoey | 9:36ba3626aab7 | 425 | //db.printf(" -- rom id[ "); |
| cyberjoey | 9:36ba3626aab7 | 426 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 427 | rom_id_list[rom_count][i] = rom_id[i] ; |
| cyberjoey | 9:36ba3626aab7 | 428 | //db.printf("%02X ",rom_id[i]); |
| cyberjoey | 9:36ba3626aab7 | 429 | } |
| cyberjoey | 9:36ba3626aab7 | 430 | //db.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 431 | //db.printf("last_device[%2d]\r\n",last_device); |
| cyberjoey | 9:36ba3626aab7 | 432 | //db.printf("last_zero[%2d]\r\n",last_zero); |
| cyberjoey | 9:36ba3626aab7 | 433 | //db.printf("last_discrep[%2d]\r\n",last_discrep); |
| cyberjoey | 9:36ba3626aab7 | 434 | |
| cyberjoey | 9:36ba3626aab7 | 435 | //clac_crc of rom ID |
| cyberjoey | 9:36ba3626aab7 | 436 | for (i=0;i<7;i++){ |
| cyberjoey | 9:36ba3626aab7 | 437 | crc = calc_crc8(crc, rom_id[i]); |
| cyberjoey | 9:36ba3626aab7 | 438 | } |
| cyberjoey | 9:36ba3626aab7 | 439 | //db.printf("crc8 = [%02X]\r\n",crc); |
| cyberjoey | 9:36ba3626aab7 | 440 | |
| cyberjoey | 9:36ba3626aab7 | 441 | |
| cyberjoey | 9:36ba3626aab7 | 442 | rom_count++; |
| cyberjoey | 9:36ba3626aab7 | 443 | }//if(last_device..) |
| cyberjoey | 9:36ba3626aab7 | 444 | }else{//if prescent -- if no device present rom count is 0 |
| cyberjoey | 9:36ba3626aab7 | 445 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 446 | } |
| cyberjoey | 9:36ba3626aab7 | 447 | }while(last_device == 0); |
| cyberjoey | 9:36ba3626aab7 | 448 | return rom_count; |
| cyberjoey | 9:36ba3626aab7 | 449 | }// end search_rom() |
| cyberjoey | 9:36ba3626aab7 | 450 | |
| cyberjoey | 9:36ba3626aab7 | 451 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 452 | // alarm_search(char *) pointer to start of 2D array rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 453 | // returns number of devices found with alarm triggered |
| cyberjoey | 9:36ba3626aab7 | 454 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 455 | |
| cyberjoey | 9:36ba3626aab7 | 456 | int alarm_search(char rom_id_list[MAX_DEVICES][ID_LENGTH]){ // searches for all devices with alarm triggered on OW bus, returns # found |
| switches | 0:60a522ae2e35 | 457 | |
| cyberjoey | 9:36ba3626aab7 | 458 | int bit_num; |
| cyberjoey | 9:36ba3626aab7 | 459 | int byte_num; |
| cyberjoey | 9:36ba3626aab7 | 460 | int last_zero; |
| cyberjoey | 9:36ba3626aab7 | 461 | int last_discrep; |
| cyberjoey | 9:36ba3626aab7 | 462 | int search_dir; |
| cyberjoey | 9:36ba3626aab7 | 463 | int rom_count; |
| cyberjoey | 9:36ba3626aab7 | 464 | char byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 465 | int last_device; |
| cyberjoey | 9:36ba3626aab7 | 466 | int i; |
| cyberjoey | 9:36ba3626aab7 | 467 | |
| cyberjoey | 9:36ba3626aab7 | 468 | char rom_id[8]; //used in rom search |
| cyberjoey | 9:36ba3626aab7 | 469 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 470 | char crc; |
| cyberjoey | 9:36ba3626aab7 | 471 | |
| cyberjoey | 9:36ba3626aab7 | 472 | //init for first search only |
| cyberjoey | 9:36ba3626aab7 | 473 | last_device = 0; |
| cyberjoey | 9:36ba3626aab7 | 474 | last_discrep = 0; |
| cyberjoey | 9:36ba3626aab7 | 475 | rom_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 476 | |
| cyberjoey | 9:36ba3626aab7 | 477 | for(i=0;i<8;i++)rom_id[i] = 0x00; //clear rom_id |
| cyberjoey | 9:36ba3626aab7 | 478 | |
| cyberjoey | 9:36ba3626aab7 | 479 | do{ // loop for each rom search (end when last_device = 1) |
| cyberjoey | 9:36ba3626aab7 | 480 | |
| cyberjoey | 9:36ba3626aab7 | 481 | //init variable for each search |
| cyberjoey | 9:36ba3626aab7 | 482 | bit_num = 1; |
| cyberjoey | 9:36ba3626aab7 | 483 | byte_num = 0; |
| cyberjoey | 9:36ba3626aab7 | 484 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 485 | last_zero = 0; |
| cyberjoey | 9:36ba3626aab7 | 486 | |
| cyberjoey | 9:36ba3626aab7 | 487 | |
| cyberjoey | 9:36ba3626aab7 | 488 | //OW reset |
| cyberjoey | 9:36ba3626aab7 | 489 | if(OW_reset()){ //returns 1 if at least one device present |
| cyberjoey | 9:36ba3626aab7 | 490 | //test if last device found |
| cyberjoey | 9:36ba3626aab7 | 491 | if(last_device!=1){ // more devices to find |
| cyberjoey | 9:36ba3626aab7 | 492 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 493 | OW_write_byte(OW_ALARM); //send 0xEC over one wire to init alarm search |
| cyberjoey | 9:36ba3626aab7 | 494 | |
| cyberjoey | 9:36ba3626aab7 | 495 | do{ |
| cyberjoey | 9:36ba3626aab7 | 496 | //determine search direction |
| cyberjoey | 9:36ba3626aab7 | 497 | if(bit_num < last_discrep){ //before last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 498 | if((rom_id[byte_num] & byte_mask) > 0){// use last rom path |
| cyberjoey | 9:36ba3626aab7 | 499 | search_dir = 1; // last path was 1 |
| cyberjoey | 9:36ba3626aab7 | 500 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 501 | search_dir = 0; // last path was 0 |
| cyberjoey | 9:36ba3626aab7 | 502 | } |
| cyberjoey | 9:36ba3626aab7 | 503 | }else{ // at or past last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 504 | if(bit_num == last_discrep){ // at last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 505 | search_dir = 1; //been here before so use 1 |
| cyberjoey | 9:36ba3626aab7 | 506 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 507 | search_dir = 0; //past last discrepancy so use 0 |
| cyberjoey | 9:36ba3626aab7 | 508 | } |
| cyberjoey | 9:36ba3626aab7 | 509 | } |
| cyberjoey | 9:36ba3626aab7 | 510 | |
| cyberjoey | 9:36ba3626aab7 | 511 | //write direction bit and get reply |
| cyberjoey | 9:36ba3626aab7 | 512 | data[0] = 0x78; |
| cyberjoey | 9:36ba3626aab7 | 513 | data[1] = search_dir << 7; //sets bit 7 of status reg to search_dir |
| cyberjoey | 9:36ba3626aab7 | 514 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 515 | |
| cyberjoey | 9:36ba3626aab7 | 516 | wait_us(250); |
| cyberjoey | 9:36ba3626aab7 | 517 | |
| cyberjoey | 9:36ba3626aab7 | 518 | //read in rom_id bits |
| cyberjoey | 9:36ba3626aab7 | 519 | i2c.read(DS2484_ADD,data,1); // read status byte |
| cyberjoey | 9:36ba3626aab7 | 520 | |
| cyberjoey | 9:36ba3626aab7 | 521 | //(todo) check for no device error here |
| cyberjoey | 9:36ba3626aab7 | 522 | |
| cyberjoey | 9:36ba3626aab7 | 523 | //get search direction used by triplet command |
| cyberjoey | 9:36ba3626aab7 | 524 | |
| cyberjoey | 9:36ba3626aab7 | 525 | if(data[0]&0x80){ //true --> DIR bit = 1 |
| cyberjoey | 9:36ba3626aab7 | 526 | search_dir = 1; |
| cyberjoey | 9:36ba3626aab7 | 527 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 528 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 529 | } |
| cyberjoey | 9:36ba3626aab7 | 530 | |
| cyberjoey | 9:36ba3626aab7 | 531 | //test for discrepancy (both 0 and 1 present at current bit number)(TSB = 0 (1's present) and SBR = 0 (0's present)) |
| cyberjoey | 9:36ba3626aab7 | 532 | if(!(data[0] & 0x60)){ // true --> discrepancy exist, both TSB and SBR = 0 |
| cyberjoey | 9:36ba3626aab7 | 533 | if(search_dir == 0)last_zero = bit_num; |
| cyberjoey | 9:36ba3626aab7 | 534 | } |
| cyberjoey | 9:36ba3626aab7 | 535 | if(search_dir == 1){ // write search dir to rom bit |
| cyberjoey | 9:36ba3626aab7 | 536 | rom_id[byte_num] |= byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 537 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 538 | rom_id[byte_num] &= ~byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 539 | } |
| cyberjoey | 9:36ba3626aab7 | 540 | |
| cyberjoey | 9:36ba3626aab7 | 541 | //increment bit_num and byte_num if needed |
| cyberjoey | 9:36ba3626aab7 | 542 | bit_num++; |
| cyberjoey | 9:36ba3626aab7 | 543 | byte_mask <<= 1; //rotate 1 bit left |
| cyberjoey | 9:36ba3626aab7 | 544 | if(byte_mask == 0){ //if bit shifts out of byte set byte to 1 |
| cyberjoey | 9:36ba3626aab7 | 545 | byte_num++; //also increnent byt num |
| cyberjoey | 9:36ba3626aab7 | 546 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 547 | } |
| cyberjoey | 9:36ba3626aab7 | 548 | |
| cyberjoey | 9:36ba3626aab7 | 549 | |
| cyberjoey | 9:36ba3626aab7 | 550 | }while(bit_num<65); //bit nun started at 1 so end after 64 |
| cyberjoey | 9:36ba3626aab7 | 551 | last_discrep = last_zero; |
| cyberjoey | 9:36ba3626aab7 | 552 | if(last_discrep == 0)last_device = 1; |
| cyberjoey | 9:36ba3626aab7 | 553 | //copy rom_id into rom_id_list and calc crc of first 7 bytes |
| cyberjoey | 9:36ba3626aab7 | 554 | crc = 0x00; // reset crc8 total to 0 |
| cyberjoey | 9:36ba3626aab7 | 555 | //db.printf(" -- rom id[ "); |
| cyberjoey | 9:36ba3626aab7 | 556 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 557 | rom_id_list[rom_count][i] = rom_id[i]; |
| cyberjoey | 9:36ba3626aab7 | 558 | } |
| cyberjoey | 9:36ba3626aab7 | 559 | |
| cyberjoey | 9:36ba3626aab7 | 560 | //clac_crc of rom ID |
| cyberjoey | 9:36ba3626aab7 | 561 | for (i=0;i<7;i++){ |
| cyberjoey | 9:36ba3626aab7 | 562 | crc = calc_crc8(crc, rom_id[i]); |
| cyberjoey | 9:36ba3626aab7 | 563 | } |
| cyberjoey | 9:36ba3626aab7 | 564 | |
| cyberjoey | 9:36ba3626aab7 | 565 | rom_count++; |
| cyberjoey | 9:36ba3626aab7 | 566 | }//if(last_device..) |
| cyberjoey | 9:36ba3626aab7 | 567 | }else{//if prescent -- if no device present rom count is 0 |
| cyberjoey | 9:36ba3626aab7 | 568 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 569 | } |
| cyberjoey | 9:36ba3626aab7 | 570 | }while(last_device == 0); |
| cyberjoey | 9:36ba3626aab7 | 571 | |
| cyberjoey | 9:36ba3626aab7 | 572 | //check if rom_id from alarm search is all 1s (this means there was a presence pulse but no alarm is triggered) |
| cyberjoey | 9:36ba3626aab7 | 573 | bool bad_rom_id = true; |
| cyberjoey | 9:36ba3626aab7 | 574 | for(int i = 0; i < 7; i++) |
| cyberjoey | 9:36ba3626aab7 | 575 | { |
| cyberjoey | 9:36ba3626aab7 | 576 | if(rom_id_list[0][i] != 0xFF) |
| cyberjoey | 9:36ba3626aab7 | 577 | bad_rom_id = false; //found a byte in the rom id that isn't all 1s |
| cyberjoey | 9:36ba3626aab7 | 578 | } |
| cyberjoey | 9:36ba3626aab7 | 579 | if(bad_rom_id) |
| cyberjoey | 9:36ba3626aab7 | 580 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 581 | |
| cyberjoey | 9:36ba3626aab7 | 582 | return rom_count; |
| cyberjoey | 9:36ba3626aab7 | 583 | }// end alarm_search() |
| cyberjoey | 9:36ba3626aab7 | 584 | |
| cyberjoey | 9:36ba3626aab7 | 585 | |
| cyberjoey | 9:36ba3626aab7 | 586 | // ******************** write_OW_register() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 587 | // write_OW_register(char*, int, int, char*) |
| cyberjoey | 9:36ba3626aab7 | 588 | // char* pointer to rom_id |
| cyberjoey | 9:36ba3626aab7 | 589 | // int start address |
| cyberjoey | 9:36ba3626aab7 | 590 | // int number of bytes to write |
| cyberjoey | 9:36ba3626aab7 | 591 | // char* pointer to write data location |
| cyberjoey | 9:36ba3626aab7 | 592 | // returns 0 if CRC match data |
| cyberjoey | 9:36ba3626aab7 | 593 | // returns -1 if CRC does not match data |
| cyberjoey | 9:36ba3626aab7 | 594 | // |
| cyberjoey | 9:36ba3626aab7 | 595 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 596 | |
| cyberjoey | 9:36ba3626aab7 | 597 | |
| cyberjoey | 9:36ba3626aab7 | 598 | |
| cyberjoey | 9:36ba3626aab7 | 599 | int write_OW_register(char *device_ID, int start_add, int n, char *data){ |
| cyberjoey | 9:36ba3626aab7 | 600 | int i; |
| cyberjoey | 9:36ba3626aab7 | 601 | //select device |
| cyberjoey | 9:36ba3626aab7 | 602 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 603 | OW_match_rom(device_ID); |
| cyberjoey | 9:36ba3626aab7 | 604 | //set start address |
| cyberjoey | 9:36ba3626aab7 | 605 | OW_write_byte(OT07_OW_WRITE); // send 0xCC, OW write register command for OT07 |
| cyberjoey | 9:36ba3626aab7 | 606 | OW_write_byte(start_add); // send write register start address |
| cyberjoey | 9:36ba3626aab7 | 607 | OW_write_byte(n-1); // send length of bytes to write (00-> one byte) |
| cyberjoey | 9:36ba3626aab7 | 608 | for(i=0;i<n;i++){ // send n bytes of data |
| cyberjoey | 9:36ba3626aab7 | 609 | OW_write_byte(data[i]); |
| cyberjoey | 9:36ba3626aab7 | 610 | } |
| cyberjoey | 9:36ba3626aab7 | 611 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 612 | |
| cyberjoey | 9:36ba3626aab7 | 613 | // --- todo ---- |
| cyberjoey | 9:36ba3626aab7 | 614 | // calculate CRC of |
| cyberjoey | 9:36ba3626aab7 | 615 | // Command + length + n bytes sent |
| cyberjoey | 9:36ba3626aab7 | 616 | // compare with 2 bytes read |
| cyberjoey | 9:36ba3626aab7 | 617 | |
| cyberjoey | 9:36ba3626aab7 | 618 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 619 | |
| cyberjoey | 9:36ba3626aab7 | 620 | }//end write_OW_register |
| cyberjoey | 9:36ba3626aab7 | 621 | |
| cyberjoey | 9:36ba3626aab7 | 622 | |
| cyberjoey | 9:36ba3626aab7 | 623 | |
| cyberjoey | 9:36ba3626aab7 | 624 | |
| cyberjoey | 9:36ba3626aab7 | 625 | // ******************** read_OW_register() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 626 | // read_OW_register(char*, int, int, char*) |
| cyberjoey | 9:36ba3626aab7 | 627 | // char* pointer to rom_id |
| cyberjoey | 9:36ba3626aab7 | 628 | // int start address |
| cyberjoey | 9:36ba3626aab7 | 629 | // int number of bytes to read, not including CRC bytes |
| cyberjoey | 9:36ba3626aab7 | 630 | // char* pointer to save data location |
| cyberjoey | 9:36ba3626aab7 | 631 | // returns 0 if CRC match data |
| cyberjoey | 9:36ba3626aab7 | 632 | // returns -1 if CRC does not match data |
| cyberjoey | 9:36ba3626aab7 | 633 | // |
| cyberjoey | 9:36ba3626aab7 | 634 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 635 | |
| cyberjoey | 9:36ba3626aab7 | 636 | |
| cyberjoey | 9:36ba3626aab7 | 637 | |
| cyberjoey | 9:36ba3626aab7 | 638 | int read_OW_register(char *device_ID, int start_add, int n, char *data){ |
| cyberjoey | 9:36ba3626aab7 | 639 | |
| cyberjoey | 9:36ba3626aab7 | 640 | //int i; |
| cyberjoey | 9:36ba3626aab7 | 641 | //select device |
| cyberjoey | 9:36ba3626aab7 | 642 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 643 | OW_match_rom(device_ID); |
| cyberjoey | 9:36ba3626aab7 | 644 | //set start address |
| cyberjoey | 9:36ba3626aab7 | 645 | OW_write_byte(OT07_OW_READ); // send 0x33, OW read register command for OT07 |
| cyberjoey | 9:36ba3626aab7 | 646 | OW_write_byte(start_add); // send read register start address |
| cyberjoey | 9:36ba3626aab7 | 647 | OW_write_byte(n-1); // send length of bytes to read (0 -> 1 byte) |
| cyberjoey | 9:36ba3626aab7 | 648 | OW_read_byte(data,n+2); // read n bytes plus 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 649 | |
| cyberjoey | 9:36ba3626aab7 | 650 | // debug output |
| cyberjoey | 9:36ba3626aab7 | 651 | //for(i=0;i<n+2;i++) db.printf("read_OW_register - [%2d][%02X]\r\n",i,data[i]); |
| cyberjoey | 9:36ba3626aab7 | 652 | |
| cyberjoey | 9:36ba3626aab7 | 653 | |
| cyberjoey | 9:36ba3626aab7 | 654 | // --- todo ---- |
| cyberjoey | 9:36ba3626aab7 | 655 | // calculate CRC of |
| cyberjoey | 9:36ba3626aab7 | 656 | // Command + length + n bytes |
| cyberjoey | 9:36ba3626aab7 | 657 | // compare with last 2 bytes read |
| cyberjoey | 9:36ba3626aab7 | 658 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 659 | |
| cyberjoey | 9:36ba3626aab7 | 660 | }//end read_OW_register |
| cyberjoey | 9:36ba3626aab7 | 661 | |
| cyberjoey | 9:36ba3626aab7 | 662 | int set_test_mode(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 663 | char data[4]; |
| cyberjoey | 9:36ba3626aab7 | 664 | |
| cyberjoey | 9:36ba3626aab7 | 665 | // enter test mode |
| cyberjoey | 9:36ba3626aab7 | 666 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 667 | OW_match_rom(device_id); // match ROM |
| cyberjoey | 9:36ba3626aab7 | 668 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 669 | OW_write_byte(0xFF); // device register address |
| cyberjoey | 9:36ba3626aab7 | 670 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 671 | OW_write_byte(0x54); // first half of test mode pass code |
| cyberjoey | 9:36ba3626aab7 | 672 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 673 | |
| cyberjoey | 9:36ba3626aab7 | 674 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 675 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 676 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 677 | OW_write_byte(0xFF); // device register address |
| cyberjoey | 9:36ba3626aab7 | 678 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 679 | OW_write_byte(0x4D); // second half of test mode pass code |
| cyberjoey | 9:36ba3626aab7 | 680 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 681 | |
| cyberjoey | 9:36ba3626aab7 | 682 | // set ADC_ENABLED |
| cyberjoey | 9:36ba3626aab7 | 683 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 684 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 685 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 686 | OW_write_byte(0x81); // device register address |
| cyberjoey | 9:36ba3626aab7 | 687 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 688 | OW_write_byte(0x04); // ADC_ENABLED code |
| cyberjoey | 9:36ba3626aab7 | 689 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 690 | |
| cyberjoey | 9:36ba3626aab7 | 691 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 692 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 693 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 694 | OW_write_byte(0x98); // device register address |
| cyberjoey | 9:36ba3626aab7 | 695 | OW_write_byte(0x05); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 696 | OW_write_byte(0x40); // add 98 data 40 |
| cyberjoey | 9:36ba3626aab7 | 697 | OW_write_byte(0xD4); // add 99 data D4 |
| cyberjoey | 9:36ba3626aab7 | 698 | OW_write_byte(0xE0); // add 9A data E0 |
| cyberjoey | 9:36ba3626aab7 | 699 | OW_write_byte(0xB3); // add 9B data B3 |
| cyberjoey | 9:36ba3626aab7 | 700 | OW_write_byte(0x09); // add 9C data 09 |
| cyberjoey | 9:36ba3626aab7 | 701 | OW_write_byte(0xBA); // add 9D data BA |
| cyberjoey | 9:36ba3626aab7 | 702 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 703 | |
| cyberjoey | 9:36ba3626aab7 | 704 | |
| cyberjoey | 9:36ba3626aab7 | 705 | //OTP copy |
| cyberjoey | 9:36ba3626aab7 | 706 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 707 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 708 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 709 | OW_write_byte(0x80); // device register address |
| cyberjoey | 9:36ba3626aab7 | 710 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 711 | OW_write_byte(0x82); // |
| cyberjoey | 9:36ba3626aab7 | 712 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 713 | |
| cyberjoey | 9:36ba3626aab7 | 714 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 715 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 716 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 717 | OW_write_byte(0x80); // device register address |
| cyberjoey | 9:36ba3626aab7 | 718 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 719 | OW_write_byte(0x02); // |
| cyberjoey | 9:36ba3626aab7 | 720 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 721 | |
| cyberjoey | 9:36ba3626aab7 | 722 | |
| cyberjoey | 9:36ba3626aab7 | 723 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 724 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 725 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 726 | OW_write_byte(0xFF); // device register address |
| cyberjoey | 9:36ba3626aab7 | 727 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 728 | OW_write_byte(0x00); // exit Test mode |
| cyberjoey | 9:36ba3626aab7 | 729 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 730 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 731 | }// end set_test_mode() |
| cyberjoey | 9:36ba3626aab7 | 732 | |
| cyberjoey | 9:36ba3626aab7 | 733 | |
| cyberjoey | 9:36ba3626aab7 | 734 | |
| cyberjoey | 9:36ba3626aab7 | 735 | // ******************** convert_temperature() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 736 | // convert_temperature() |
| cyberjoey | 9:36ba3626aab7 | 737 | // returns 0 |
| cyberjoey | 9:36ba3626aab7 | 738 | // dose not block for 0.75 seconds. |
| cyberjoey | 9:36ba3626aab7 | 739 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 740 | |
| cyberjoey | 9:36ba3626aab7 | 741 | int convert_temperature(){ // convert sent to all OW devices |
| cyberjoey | 9:36ba3626aab7 | 742 | |
| cyberjoey | 9:36ba3626aab7 | 743 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 744 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 745 | |
| cyberjoey | 9:36ba3626aab7 | 746 | OW_write_byte(OW_SKIP); // send 0xCC, skip rom command |
| cyberjoey | 9:36ba3626aab7 | 747 | |
| cyberjoey | 9:36ba3626aab7 | 748 | // activate strong pullup |
| cyberjoey | 9:36ba3626aab7 | 749 | data[0] = WDC; // send 0xD2 Write Device Configuration |
| cyberjoey | 9:36ba3626aab7 | 750 | data[1] = 0xA5; // 1010 0101 b strong and active pullup on |
| cyberjoey | 9:36ba3626aab7 | 751 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 752 | |
| cyberjoey | 9:36ba3626aab7 | 753 | //convert command |
| cyberjoey | 9:36ba3626aab7 | 754 | OW_write_byte(OT07_OW_CONVERT); // send 0x44, Convert Temperature |
| cyberjoey | 9:36ba3626aab7 | 755 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 756 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 757 | } |
| cyberjoey | 9:36ba3626aab7 | 758 | |
| cyberjoey | 9:36ba3626aab7 | 759 | |
| cyberjoey | 9:36ba3626aab7 | 760 | // ******************** convert_temperature() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 761 | // convert_temperature(char *) takes 8 byte rom_id as input |
| cyberjoey | 9:36ba3626aab7 | 762 | // returns 0 |
| cyberjoey | 9:36ba3626aab7 | 763 | // dose not block for 0.75 seconds. |
| cyberjoey | 9:36ba3626aab7 | 764 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 765 | |
| cyberjoey | 9:36ba3626aab7 | 766 | int convert_temperature(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 767 | |
| cyberjoey | 9:36ba3626aab7 | 768 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 769 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 770 | OW_match_rom(device_id); // send device id |
| cyberjoey | 9:36ba3626aab7 | 771 | |
| cyberjoey | 9:36ba3626aab7 | 772 | // activate strong pullup |
| cyberjoey | 9:36ba3626aab7 | 773 | data[0] = WDC; // send 0xD2 Write Device Configuration |
| cyberjoey | 9:36ba3626aab7 | 774 | data[1] = 0xA5; // 1010 0101 b strong and active pullup on |
| cyberjoey | 9:36ba3626aab7 | 775 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 776 | |
| cyberjoey | 9:36ba3626aab7 | 777 | //convert command |
| cyberjoey | 9:36ba3626aab7 | 778 | OW_write_byte(OT07_OW_CONVERT); // send 0x44, Convert Temperature |
| cyberjoey | 9:36ba3626aab7 | 779 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 780 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 781 | } |
| cyberjoey | 9:36ba3626aab7 | 782 | |
| cyberjoey | 9:36ba3626aab7 | 783 | //************************ get_temperature() ********************************* |
| cyberjoey | 9:36ba3626aab7 | 784 | // get_temperature(char *) takes 8 byte rom_id as input |
| cyberjoey | 9:36ba3626aab7 | 785 | // returns double temperature in oC |
| cyberjoey | 9:36ba3626aab7 | 786 | //****************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 787 | |
| cyberjoey | 9:36ba3626aab7 | 788 | double get_temperature(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 789 | |
| cyberjoey | 9:36ba3626aab7 | 790 | char t[4]; |
| cyberjoey | 9:36ba3626aab7 | 791 | double T; |
| cyberjoey | 9:36ba3626aab7 | 792 | int count; |
| cyberjoey | 9:36ba3626aab7 | 793 | // char data[2]; |
| cyberjoey | 9:36ba3626aab7 | 794 | |
| cyberjoey | 9:36ba3626aab7 | 795 | read_OW_register(device_id,OT07_FIFO_DATA,0x02,t); // Read temperature from FIFO, 2 bytes |
| cyberjoey | 9:36ba3626aab7 | 796 | //db.printf("get_temperature -- FIFO[%02X %02X] CRC[%02X %02X]\r\n",t[0],t[1],t[2],t[3]); |
| cyberjoey | 9:36ba3626aab7 | 797 | //calculate temperture from data |
| cyberjoey | 9:36ba3626aab7 | 798 | count = (int)(t[0]*256 + t[1]); |
| cyberjoey | 9:36ba3626aab7 | 799 | if (count >= 32768)count = count - 65536; // 2s comp |
| cyberjoey | 9:36ba3626aab7 | 800 | T = (double)count*0.005; |
| cyberjoey | 9:36ba3626aab7 | 801 | |
| cyberjoey | 9:36ba3626aab7 | 802 | //db.printf("T = %f count[%5d]\r\n", T,count); |
| cyberjoey | 9:36ba3626aab7 | 803 | return T; |
| cyberjoey | 9:36ba3626aab7 | 804 | }// end get_temperature() |
| cyberjoey | 9:36ba3626aab7 | 805 | |
| cyberjoey | 9:36ba3626aab7 | 806 | |
| cyberjoey | 10:148da21c297e | 807 | void write_settings_file(int interval, bool device_logged[MAX_DEVICES]) |
| cyberjoey | 10:148da21c297e | 808 | { |
| cyberjoey | 10:148da21c297e | 809 | FILE *fp = fopen(settings_file, "w"); |
| cyberjoey | 10:148da21c297e | 810 | if (fp != NULL) |
| cyberjoey | 10:148da21c297e | 811 | { |
| cyberjoey | 10:148da21c297e | 812 | fprintf(fp, "i %d\r\n", interval); |
| cyberjoey | 10:148da21c297e | 813 | |
| cyberjoey | 10:148da21c297e | 814 | fprintf(fp, "d"); |
| cyberjoey | 10:148da21c297e | 815 | |
| cyberjoey | 10:148da21c297e | 816 | for(int i = 0; i < MAX_DEVICES; i++) |
| cyberjoey | 10:148da21c297e | 817 | { |
| cyberjoey | 10:148da21c297e | 818 | if(device_logged[i] == true) |
| cyberjoey | 10:148da21c297e | 819 | { |
| cyberjoey | 10:148da21c297e | 820 | fprintf(fp," %d", i); |
| cyberjoey | 10:148da21c297e | 821 | } |
| cyberjoey | 10:148da21c297e | 822 | } |
| cyberjoey | 10:148da21c297e | 823 | fprintf(fp,"\r\n"); |
| cyberjoey | 10:148da21c297e | 824 | |
| cyberjoey | 10:148da21c297e | 825 | fclose(fp); |
| cyberjoey | 10:148da21c297e | 826 | } |
| cyberjoey | 10:148da21c297e | 827 | return; |
| cyberjoey | 10:148da21c297e | 828 | } |
| cyberjoey | 10:148da21c297e | 829 | |
| cyberjoey | 10:148da21c297e | 830 | bool print_settings_file() |
| cyberjoey | 10:148da21c297e | 831 | { |
| cyberjoey | 10:148da21c297e | 832 | FILE *fp = fopen(settings_file, "r"); |
| cyberjoey | 10:148da21c297e | 833 | if (fp != NULL) |
| cyberjoey | 10:148da21c297e | 834 | { |
| cyberjoey | 10:148da21c297e | 835 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 836 | |
| cyberjoey | 10:148da21c297e | 837 | // Read contents from file |
| cyberjoey | 10:148da21c297e | 838 | char c = fgetc(fp); |
| cyberjoey | 10:148da21c297e | 839 | while (!feof(fp)) |
| cyberjoey | 10:148da21c297e | 840 | { |
| cyberjoey | 10:148da21c297e | 841 | pc.printf("%c", c); |
| cyberjoey | 10:148da21c297e | 842 | c = fgetc(fp); |
| cyberjoey | 10:148da21c297e | 843 | } |
| cyberjoey | 10:148da21c297e | 844 | |
| cyberjoey | 10:148da21c297e | 845 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 846 | |
| cyberjoey | 10:148da21c297e | 847 | fclose(fp); |
| cyberjoey | 10:148da21c297e | 848 | } |
| cyberjoey | 10:148da21c297e | 849 | else |
| cyberjoey | 10:148da21c297e | 850 | { |
| cyberjoey | 10:148da21c297e | 851 | return false; |
| cyberjoey | 10:148da21c297e | 852 | } |
| cyberjoey | 10:148da21c297e | 853 | return true; |
| cyberjoey | 10:148da21c297e | 854 | } |
| cyberjoey | 10:148da21c297e | 855 | |
| cyberjoey | 10:148da21c297e | 856 | bool print_log_file() |
| cyberjoey | 10:148da21c297e | 857 | { |
| cyberjoey | 10:148da21c297e | 858 | FILE *fp = fopen(log_file, "r"); |
| cyberjoey | 10:148da21c297e | 859 | if (fp != NULL) |
| cyberjoey | 10:148da21c297e | 860 | { |
| cyberjoey | 10:148da21c297e | 861 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 862 | |
| cyberjoey | 10:148da21c297e | 863 | // Read contents from file |
| cyberjoey | 10:148da21c297e | 864 | char c = fgetc(fp); |
| cyberjoey | 10:148da21c297e | 865 | while (!feof(fp)) |
| cyberjoey | 10:148da21c297e | 866 | { |
| cyberjoey | 10:148da21c297e | 867 | pc.printf("%c", c); |
| cyberjoey | 10:148da21c297e | 868 | c = fgetc(fp); |
| cyberjoey | 10:148da21c297e | 869 | } |
| cyberjoey | 10:148da21c297e | 870 | |
| cyberjoey | 10:148da21c297e | 871 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 872 | |
| cyberjoey | 10:148da21c297e | 873 | fclose(fp); |
| cyberjoey | 10:148da21c297e | 874 | } |
| cyberjoey | 10:148da21c297e | 875 | else |
| cyberjoey | 10:148da21c297e | 876 | { |
| cyberjoey | 10:148da21c297e | 877 | return false; |
| cyberjoey | 10:148da21c297e | 878 | } |
| cyberjoey | 10:148da21c297e | 879 | return true; |
| cyberjoey | 10:148da21c297e | 880 | } |
| cyberjoey | 10:148da21c297e | 881 | |
| cyberjoey | 10:148da21c297e | 882 | |
| cyberjoey | 9:36ba3626aab7 | 883 | //****************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 884 | // main() |
| cyberjoey | 9:36ba3626aab7 | 885 | //****************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 886 | |
| cyberjoey | 9:36ba3626aab7 | 887 | |
| switches | 0:60a522ae2e35 | 888 | int main() |
| switches | 0:60a522ae2e35 | 889 | { |
| cyberjoey | 9:36ba3626aab7 | 890 | |
| cyberjoey | 9:36ba3626aab7 | 891 | char data[130]; |
| cyberjoey | 9:36ba3626aab7 | 892 | int device_count = 0; // number of OW devices found by search_rom() |
| cyberjoey | 9:36ba3626aab7 | 893 | int i; |
| cyberjoey | 9:36ba3626aab7 | 894 | int j; |
| cyberjoey | 9:36ba3626aab7 | 895 | int k; |
| cyberjoey | 9:36ba3626aab7 | 896 | |
| cyberjoey | 9:36ba3626aab7 | 897 | |
| cyberjoey | 9:36ba3626aab7 | 898 | char device_id[ID_LENGTH]; //8 byte rom id of current slected device |
| cyberjoey | 9:36ba3626aab7 | 899 | char rom_id_list[MAX_DEVICES][ID_LENGTH]; //List of rom id for each device on OW bus |
| cyberjoey | 9:36ba3626aab7 | 900 | |
| cyberjoey | 9:36ba3626aab7 | 901 | double T[MAX_DEVICES]; |
| cyberjoey | 9:36ba3626aab7 | 902 | |
| cyberjoey | 10:148da21c297e | 903 | bool device_logged[MAX_DEVICES]; |
| cyberjoey | 10:148da21c297e | 904 | |
| cyberjoey | 9:36ba3626aab7 | 905 | // i/o variables |
| cyberjoey | 9:36ba3626aab7 | 906 | char rx_buff[128]; // comport input buffer |
| cyberjoey | 9:36ba3626aab7 | 907 | int rx_index; // rx_buffer pointer |
| cyberjoey | 9:36ba3626aab7 | 908 | char c; // command type character |
| cyberjoey | 9:36ba3626aab7 | 909 | int n; // argument count |
| cyberjoey | 9:36ba3626aab7 | 910 | int arg1; // argumnet 1 |
| cyberjoey | 9:36ba3626aab7 | 911 | int arg2; // argument 2 |
| cyberjoey | 9:36ba3626aab7 | 912 | int device; // device argument |
| cyberjoey | 9:36ba3626aab7 | 913 | int num_bytes; |
| cyberjoey | 9:36ba3626aab7 | 914 | |
| cyberjoey | 9:36ba3626aab7 | 915 | int time_count; |
| cyberjoey | 9:36ba3626aab7 | 916 | int log_interval = PERIOD; |
| cyberjoey | 9:36ba3626aab7 | 917 | int time_to_sample; |
| cyberjoey | 9:36ba3626aab7 | 918 | |
| cyberjoey | 9:36ba3626aab7 | 919 | //************* init ticker timer callbacks **************** |
| cyberjoey | 9:36ba3626aab7 | 920 | timer_1.attach(&LED_blink_callback,1.0); //start ticker, once per sec. |
| cyberjoey | 9:36ba3626aab7 | 921 | |
| cyberjoey | 9:36ba3626aab7 | 922 | |
| cyberjoey | 9:36ba3626aab7 | 923 | i2c.frequency(400000); //set I2C clock to 100kHz |
| cyberjoey | 9:36ba3626aab7 | 924 | |
| cyberjoey | 9:36ba3626aab7 | 925 | //wait(1); //wait 1 sec for USB serial port to init. |
| cyberjoey | 9:36ba3626aab7 | 926 | |
| cyberjoey | 9:36ba3626aab7 | 927 | //db.printf("--- OT07 - Base Logger Firmware uisng DS2484 bridge V0.2 ---\r\ndaplink serial port\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 928 | //pc.printf("--- OT07 - Base Logger Firmware uisng DS2484 bridge V0.2 ---\r\nmicro USB serial port\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 929 | |
| cyberjoey | 9:36ba3626aab7 | 930 | rLED = LED_OFF; |
| switches | 6:96d8d823e292 | 931 | gLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 932 | bLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 933 | |
| cyberjoey | 9:36ba3626aab7 | 934 | |
| cyberjoey | 9:36ba3626aab7 | 935 | // reset DS2484 |
| cyberjoey | 9:36ba3626aab7 | 936 | data[0] = 0xE1; |
| cyberjoey | 9:36ba3626aab7 | 937 | data[1] = 0xF0; |
| cyberjoey | 9:36ba3626aab7 | 938 | i2c.write(DS2484_ADD,data,1,1); |
| cyberjoey | 9:36ba3626aab7 | 939 | i2c.read(DS2484_ADD,data,1); |
| cyberjoey | 9:36ba3626aab7 | 940 | |
| cyberjoey | 9:36ba3626aab7 | 941 | //db.printf("DS2484 Reset --> status[%02X]\r\n",data[0]); |
| cyberjoey | 9:36ba3626aab7 | 942 | |
| cyberjoey | 9:36ba3626aab7 | 943 | // ****************** search for all OW devices on bus ***************** |
| cyberjoey | 9:36ba3626aab7 | 944 | |
| cyberjoey | 9:36ba3626aab7 | 945 | device_count = search_rom(rom_id_list); |
| cyberjoey | 10:148da21c297e | 946 | |
| cyberjoey | 9:36ba3626aab7 | 947 | |
| cyberjoey | 9:36ba3626aab7 | 948 | // print out rom codes found |
| cyberjoey | 9:36ba3626aab7 | 949 | /*pc.printf("\r\nsearch rom\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 950 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 951 | //pc.printf("device[%02X] rom id[ ",j); |
| cyberjoey | 9:36ba3626aab7 | 952 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 953 | pc.printf("%02X",rom_id_list[j][i]); |
| cyberjoey | 9:36ba3626aab7 | 954 | } |
| cyberjoey | 9:36ba3626aab7 | 955 | pc.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 956 | } |
| cyberjoey | 9:36ba3626aab7 | 957 | pc.printf("\r\n");*/ |
| cyberjoey | 9:36ba3626aab7 | 958 | |
| cyberjoey | 9:36ba3626aab7 | 959 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 960 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 961 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 962 | } |
| cyberjoey | 9:36ba3626aab7 | 963 | set_test_mode(device_id); |
| cyberjoey | 9:36ba3626aab7 | 964 | //db.printf("device[%2X] in test mode\r\n",j); |
| cyberjoey | 9:36ba3626aab7 | 965 | } |
| cyberjoey | 9:36ba3626aab7 | 966 | |
| cyberjoey | 9:36ba3626aab7 | 967 | |
| cyberjoey | 9:36ba3626aab7 | 968 | rx_index = 0; //character buffer index for input from PC |
| cyberjoey | 9:36ba3626aab7 | 969 | |
| cyberjoey | 9:36ba3626aab7 | 970 | button.fall(&btn_pressed); |
| cyberjoey | 9:36ba3626aab7 | 971 | |
| cyberjoey | 9:36ba3626aab7 | 972 | //db.printf("start Main loop\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 973 | //db.printf("tf[%d]\r\n",tick_flag); |
| cyberjoey | 9:36ba3626aab7 | 974 | while(1) { // start main loop, take data if logging, check for input, repeat |
| cyberjoey | 9:36ba3626aab7 | 975 | |
| cyberjoey | 9:36ba3626aab7 | 976 | if(button_flag == true) |
| cyberjoey | 9:36ba3626aab7 | 977 | { |
| cyberjoey | 9:36ba3626aab7 | 978 | if(log_flag == false){ //start logging |
| cyberjoey | 9:36ba3626aab7 | 979 | |
| cyberjoey | 9:36ba3626aab7 | 980 | if(SDDetect)//if SD card not detected |
| cyberjoey | 9:36ba3626aab7 | 981 | { |
| cyberjoey | 9:36ba3626aab7 | 982 | error_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 983 | error_ticks = 6; |
| cyberjoey | 9:36ba3626aab7 | 984 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 985 | } |
| cyberjoey | 9:36ba3626aab7 | 986 | else |
| cyberjoey | 9:36ba3626aab7 | 987 | { |
| cyberjoey | 10:148da21c297e | 988 | FILE *fp = fopen(log_file, "a"); |
| cyberjoey | 9:36ba3626aab7 | 989 | if (fp != NULL) |
| cyberjoey | 9:36ba3626aab7 | 990 | { |
| cyberjoey | 9:36ba3626aab7 | 991 | fprintf(fp, "Time(s)"); |
| cyberjoey | 9:36ba3626aab7 | 992 | |
| cyberjoey | 9:36ba3626aab7 | 993 | for(j=0;j<device_count;j++) |
| cyberjoey | 9:36ba3626aab7 | 994 | { |
| cyberjoey | 9:36ba3626aab7 | 995 | fprintf(fp,", Device %d Temperature (C)",j); |
| cyberjoey | 9:36ba3626aab7 | 996 | } |
| cyberjoey | 9:36ba3626aab7 | 997 | fprintf(fp,"\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 998 | |
| cyberjoey | 9:36ba3626aab7 | 999 | fclose(fp); |
| cyberjoey | 9:36ba3626aab7 | 1000 | } |
| cyberjoey | 9:36ba3626aab7 | 1001 | |
| cyberjoey | 9:36ba3626aab7 | 1002 | time_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 1003 | |
| cyberjoey | 9:36ba3626aab7 | 1004 | time_to_sample = 0; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1005 | tick_flag = true; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1006 | log_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 1007 | } |
| cyberjoey | 9:36ba3626aab7 | 1008 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 1009 | //pc.printf("<stop logging>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1010 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1011 | } |
| cyberjoey | 9:36ba3626aab7 | 1012 | button_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1013 | } |
| switches | 1:90313362ec11 | 1014 | |
| cyberjoey | 9:36ba3626aab7 | 1015 | while(tick_flag == false){ //check for input while waiting for next tick |
| cyberjoey | 9:36ba3626aab7 | 1016 | |
| cyberjoey | 9:36ba3626aab7 | 1017 | // ---------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 1018 | // test for charater input for USB com port |
| cyberjoey | 9:36ba3626aab7 | 1019 | // ---------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 1020 | |
| cyberjoey | 9:36ba3626aab7 | 1021 | //test if PC sent some charaters |
| cyberjoey | 9:36ba3626aab7 | 1022 | while(pc.readable()){ //characters in buffer, get them |
| cyberjoey | 9:36ba3626aab7 | 1023 | rx_buff[rx_index] = pc.getc(); |
| cyberjoey | 9:36ba3626aab7 | 1024 | //pc.putc(rx_buff[rx_index]); //echo character |
| cyberjoey | 9:36ba3626aab7 | 1025 | //pc.printf("<[%02x] %c i[%d]>",rx_buff[i],rx_buff[i],i); //echo charater |
| cyberjoey | 9:36ba3626aab7 | 1026 | |
| cyberjoey | 9:36ba3626aab7 | 1027 | |
| cyberjoey | 9:36ba3626aab7 | 1028 | if(rx_buff[rx_index] == CR){ |
| cyberjoey | 9:36ba3626aab7 | 1029 | //db.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1030 | pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1031 | rx_buff[++rx_index] = 0; |
| cyberjoey | 9:36ba3626aab7 | 1032 | //db.printf("%s\r\n",rx_buff); |
| cyberjoey | 9:36ba3626aab7 | 1033 | //pc.printf("%s\r\n",rx_buff); |
| cyberjoey | 9:36ba3626aab7 | 1034 | rx_index = -1; // because i++ at end of while give i=0 on next loop |
| cyberjoey | 9:36ba3626aab7 | 1035 | device = 0; |
| cyberjoey | 9:36ba3626aab7 | 1036 | arg1 = 0; |
| cyberjoey | 9:36ba3626aab7 | 1037 | arg2 = 0; |
| cyberjoey | 9:36ba3626aab7 | 1038 | |
| cyberjoey | 9:36ba3626aab7 | 1039 | n = sscanf(rx_buff, " %c %d %x %x", &c, & device, &arg1, &arg2); |
| cyberjoey | 9:36ba3626aab7 | 1040 | |
| cyberjoey | 9:36ba3626aab7 | 1041 | //db.printf("c[%c] d[%d] a1[%x] a2[%x] n[%d]\r\n",c,device,arg1,arg2,n); //echo values read in |
| cyberjoey | 9:36ba3626aab7 | 1042 | //process input |
| cyberjoey | 9:36ba3626aab7 | 1043 | if(n > 0){//got input so process it |
| cyberjoey | 9:36ba3626aab7 | 1044 | switch(c){ |
| cyberjoey | 9:36ba3626aab7 | 1045 | case 'a': |
| cyberjoey | 9:36ba3626aab7 | 1046 | case 'A': // alarm search |
| cyberjoey | 9:36ba3626aab7 | 1047 | // ****************** search for all OW devices with alarm triggered on bus ***************** |
| cyberjoey | 9:36ba3626aab7 | 1048 | |
| cyberjoey | 9:36ba3626aab7 | 1049 | device_count = alarm_search(rom_id_list); |
| cyberjoey | 9:36ba3626aab7 | 1050 | |
| cyberjoey | 9:36ba3626aab7 | 1051 | pc.printf("%d devices:\r\n", device_count); |
| cyberjoey | 9:36ba3626aab7 | 1052 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1053 | pc.printf("device[%02X] rom id[",j); |
| cyberjoey | 9:36ba3626aab7 | 1054 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 1055 | pc.printf("%02X",rom_id_list[j][i]); |
| cyberjoey | 9:36ba3626aab7 | 1056 | } |
| cyberjoey | 9:36ba3626aab7 | 1057 | pc.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1058 | } |
| cyberjoey | 9:36ba3626aab7 | 1059 | break; |
| cyberjoey | 10:148da21c297e | 1060 | case 'd': |
| cyberjoey | 10:148da21c297e | 1061 | case 'D': |
| cyberjoey | 10:148da21c297e | 1062 | if(n==1) // if no device number is given |
| cyberjoey | 10:148da21c297e | 1063 | { |
| cyberjoey | 10:148da21c297e | 1064 | //clear device_logged array |
| cyberjoey | 10:148da21c297e | 1065 | for(int i = 0; i < MAX_DEVICES; i++) |
| cyberjoey | 10:148da21c297e | 1066 | { |
| cyberjoey | 10:148da21c297e | 1067 | device_logged[i] = false; |
| cyberjoey | 10:148da21c297e | 1068 | } |
| cyberjoey | 10:148da21c297e | 1069 | |
| cyberjoey | 10:148da21c297e | 1070 | write_settings_file(log_interval, device_logged); |
| cyberjoey | 10:148da21c297e | 1071 | } |
| cyberjoey | 10:148da21c297e | 1072 | if(n == 2) |
| cyberjoey | 10:148da21c297e | 1073 | { |
| cyberjoey | 10:148da21c297e | 1074 | device_logged[device] = true; |
| cyberjoey | 10:148da21c297e | 1075 | write_settings_file(log_interval, device_logged); |
| cyberjoey | 10:148da21c297e | 1076 | } |
| cyberjoey | 10:148da21c297e | 1077 | break; |
| cyberjoey | 9:36ba3626aab7 | 1078 | case 'f': |
| cyberjoey | 9:36ba3626aab7 | 1079 | case 'F': //f is for "flash" for microSD |
| cyberjoey | 9:36ba3626aab7 | 1080 | if(!SDDetect) |
| cyberjoey | 9:36ba3626aab7 | 1081 | pc.printf("y\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1082 | else |
| cyberjoey | 9:36ba3626aab7 | 1083 | pc.printf("n\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1084 | break; |
| cyberjoey | 10:148da21c297e | 1085 | case 'g': |
| cyberjoey | 10:148da21c297e | 1086 | case 'G': |
| cyberjoey | 10:148da21c297e | 1087 | //0 means get config, 1 means get log |
| cyberjoey | 10:148da21c297e | 1088 | if(n == 2) |
| cyberjoey | 10:148da21c297e | 1089 | { |
| cyberjoey | 10:148da21c297e | 1090 | bool fileExists = false; |
| cyberjoey | 10:148da21c297e | 1091 | if(device == 0)//get config |
| cyberjoey | 10:148da21c297e | 1092 | { |
| cyberjoey | 10:148da21c297e | 1093 | fileExists = print_settings_file(); |
| cyberjoey | 10:148da21c297e | 1094 | } |
| cyberjoey | 10:148da21c297e | 1095 | if(device == 1)//get log |
| cyberjoey | 10:148da21c297e | 1096 | { |
| cyberjoey | 10:148da21c297e | 1097 | fileExists = print_log_file(); |
| cyberjoey | 10:148da21c297e | 1098 | } |
| cyberjoey | 10:148da21c297e | 1099 | if(!fileExists) |
| cyberjoey | 10:148da21c297e | 1100 | { |
| cyberjoey | 10:148da21c297e | 1101 | pc.printf("no_file\r\n"); |
| cyberjoey | 10:148da21c297e | 1102 | } |
| cyberjoey | 10:148da21c297e | 1103 | } |
| cyberjoey | 10:148da21c297e | 1104 | break; |
| cyberjoey | 9:36ba3626aab7 | 1105 | case 'i': |
| cyberjoey | 9:36ba3626aab7 | 1106 | case 'I': //Set Logging sample intreval in seconds |
| cyberjoey | 9:36ba3626aab7 | 1107 | |
| cyberjoey | 9:36ba3626aab7 | 1108 | log_interval = device; |
| cyberjoey | 9:36ba3626aab7 | 1109 | if(log_interval < 1)log_interval = 1; |
| cyberjoey | 10:148da21c297e | 1110 | if(log_interval > 60)log_interval = 60; |
| cyberjoey | 10:148da21c297e | 1111 | write_settings_file(log_interval, device_logged); |
| cyberjoey | 9:36ba3626aab7 | 1112 | break; |
| cyberjoey | 9:36ba3626aab7 | 1113 | case 'l': |
| cyberjoey | 9:36ba3626aab7 | 1114 | case 'L': // Toggle logging |
| cyberjoey | 9:36ba3626aab7 | 1115 | |
| cyberjoey | 9:36ba3626aab7 | 1116 | if(log_flag == false){ //start logging |
| cyberjoey | 9:36ba3626aab7 | 1117 | if(SDDetect)//if SD card not detected |
| cyberjoey | 9:36ba3626aab7 | 1118 | { |
| cyberjoey | 9:36ba3626aab7 | 1119 | error_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 1120 | error_ticks = 6; |
| cyberjoey | 9:36ba3626aab7 | 1121 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1122 | } |
| cyberjoey | 9:36ba3626aab7 | 1123 | else |
| cyberjoey | 9:36ba3626aab7 | 1124 | { |
| cyberjoey | 10:148da21c297e | 1125 | FILE *fp = fopen(log_file, "a"); |
| cyberjoey | 9:36ba3626aab7 | 1126 | if (fp != NULL) |
| cyberjoey | 9:36ba3626aab7 | 1127 | { |
| cyberjoey | 9:36ba3626aab7 | 1128 | fprintf(fp, "Time(s)"); |
| cyberjoey | 9:36ba3626aab7 | 1129 | |
| cyberjoey | 9:36ba3626aab7 | 1130 | for(j=0;j<device_count;j++) |
| cyberjoey | 9:36ba3626aab7 | 1131 | { |
| cyberjoey | 9:36ba3626aab7 | 1132 | fprintf(fp,", Device %d Temperature (C)",j); |
| cyberjoey | 9:36ba3626aab7 | 1133 | } |
| cyberjoey | 9:36ba3626aab7 | 1134 | fprintf(fp,"\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1135 | |
| cyberjoey | 9:36ba3626aab7 | 1136 | fclose(fp); |
| cyberjoey | 9:36ba3626aab7 | 1137 | } |
| cyberjoey | 9:36ba3626aab7 | 1138 | |
| cyberjoey | 9:36ba3626aab7 | 1139 | time_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 1140 | |
| cyberjoey | 9:36ba3626aab7 | 1141 | time_to_sample = 0; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1142 | tick_flag = true; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1143 | log_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 1144 | } |
| cyberjoey | 9:36ba3626aab7 | 1145 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 1146 | //pc.printf("<stop logging>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1147 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1148 | } |
| cyberjoey | 9:36ba3626aab7 | 1149 | break; |
| cyberjoey | 9:36ba3626aab7 | 1150 | case 'P': |
| cyberjoey | 9:36ba3626aab7 | 1151 | case 'p': // power down One Wire buss |
| cyberjoey | 9:36ba3626aab7 | 1152 | if(n == 2){ |
| cyberjoey | 9:36ba3626aab7 | 1153 | if (device == 0){ // power down mode ON. |
| cyberjoey | 9:36ba3626aab7 | 1154 | data[0] = WDC; // 0xD2 Write Device Config |
| cyberjoey | 9:36ba3626aab7 | 1155 | data[1] = 0xD2; // 1101 0010 set 1WS = 0, SPU = 0, PDN = 1, APU = 0 |
| cyberjoey | 9:36ba3626aab7 | 1156 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 1157 | pc.printf("<Power down DQ>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1158 | }else{ // power down mode OFF |
| cyberjoey | 9:36ba3626aab7 | 1159 | data[0] = WDC; // 0xD2 Write Device Config |
| cyberjoey | 9:36ba3626aab7 | 1160 | data[1] = 0xE1; // 1110 0001 set 1WS = 0, SPU = 0, PDN = 0, APU = 1 |
| cyberjoey | 9:36ba3626aab7 | 1161 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 1162 | pc.printf("<Power up DQ>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1163 | } |
| cyberjoey | 9:36ba3626aab7 | 1164 | } |
| cyberjoey | 9:36ba3626aab7 | 1165 | break; |
| cyberjoey | 9:36ba3626aab7 | 1166 | case 'r': |
| cyberjoey | 9:36ba3626aab7 | 1167 | case 'R': //read register "r device radd.start radd.end" |
| cyberjoey | 9:36ba3626aab7 | 1168 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1169 | device_id[k] = rom_id_list[device][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1170 | } |
| cyberjoey | 9:36ba3626aab7 | 1171 | if(n==3){ //read single register from selected device |
| cyberjoey | 9:36ba3626aab7 | 1172 | read_OW_register(device_id,arg1,1,data); |
| cyberjoey | 9:36ba3626aab7 | 1173 | pc.printf("device[%02X] add[%02X] data[%02X] crc[%02X %02X]\r\n",device,arg1,data[0],data[1],data[2]); |
| cyberjoey | 9:36ba3626aab7 | 1174 | } |
| cyberjoey | 9:36ba3626aab7 | 1175 | if(n==4){ //read a range of regesters from selected device |
| cyberjoey | 9:36ba3626aab7 | 1176 | num_bytes = arg2-arg1 + 1; // calculate number of bytes to read |
| cyberjoey | 9:36ba3626aab7 | 1177 | if (num_bytes < 1) num_bytes = 1; // if arg2 <= arg 1 just read arg1 address. |
| cyberjoey | 9:36ba3626aab7 | 1178 | read_OW_register(device_id,arg1,num_bytes,data); |
| cyberjoey | 9:36ba3626aab7 | 1179 | for(i=0;i<num_bytes;i++){ |
| cyberjoey | 9:36ba3626aab7 | 1180 | pc.printf("\r\ndevice[%02X] add[%02X] data[%02X]",device,arg1+i,data[i]); |
| cyberjoey | 9:36ba3626aab7 | 1181 | } |
| cyberjoey | 9:36ba3626aab7 | 1182 | pc.printf(" crc[%02X %02X]\r\n",data[i],data[i+1]); |
| cyberjoey | 9:36ba3626aab7 | 1183 | } |
| cyberjoey | 9:36ba3626aab7 | 1184 | break; |
| cyberjoey | 9:36ba3626aab7 | 1185 | |
| cyberjoey | 9:36ba3626aab7 | 1186 | case 's': |
| cyberjoey | 9:36ba3626aab7 | 1187 | case 'S': // search rom |
| cyberjoey | 9:36ba3626aab7 | 1188 | // ****************** search for all OW devices on bus ***************** |
| cyberjoey | 9:36ba3626aab7 | 1189 | |
| cyberjoey | 9:36ba3626aab7 | 1190 | device_count = search_rom(rom_id_list); |
| cyberjoey | 9:36ba3626aab7 | 1191 | |
| cyberjoey | 9:36ba3626aab7 | 1192 | // print out rom codes found |
| cyberjoey | 9:36ba3626aab7 | 1193 | //pc.printf("\r\nsearch rom\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1194 | pc.printf("%d devices:\r\n", device_count); |
| cyberjoey | 9:36ba3626aab7 | 1195 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1196 | pc.printf("device[%02X] rom id[",j); |
| cyberjoey | 9:36ba3626aab7 | 1197 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 1198 | pc.printf("%02X",rom_id_list[j][i]); |
| cyberjoey | 9:36ba3626aab7 | 1199 | } |
| cyberjoey | 9:36ba3626aab7 | 1200 | pc.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1201 | } |
| cyberjoey | 9:36ba3626aab7 | 1202 | //pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1203 | break; |
| cyberjoey | 9:36ba3626aab7 | 1204 | |
| cyberjoey | 9:36ba3626aab7 | 1205 | case 'T': |
| cyberjoey | 9:36ba3626aab7 | 1206 | case 't': |
| cyberjoey | 9:36ba3626aab7 | 1207 | if(n == 2){//get temperatures from selected device |
| cyberjoey | 9:36ba3626aab7 | 1208 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1209 | device_id[k] = rom_id_list[device][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1210 | } |
| cyberjoey | 9:36ba3626aab7 | 1211 | convert_temperature(device_id); //send OW convert selected device |
| cyberjoey | 9:36ba3626aab7 | 1212 | wait(0.02); //wait 20 ms for convert temperature to complete |
| cyberjoey | 9:36ba3626aab7 | 1213 | T[0] = get_temperature(device_id); |
| cyberjoey | 9:36ba3626aab7 | 1214 | pc.printf("device[%02X] temperature[%.3f]\r\n",device,T[0]); |
| cyberjoey | 9:36ba3626aab7 | 1215 | //pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1216 | } |
| cyberjoey | 9:36ba3626aab7 | 1217 | if(n == 3){ // "t 1 5" get temperature for devices 1 thru 5 |
| cyberjoey | 9:36ba3626aab7 | 1218 | |
| cyberjoey | 9:36ba3626aab7 | 1219 | convert_temperature(); //send OW convert to all devices |
| cyberjoey | 9:36ba3626aab7 | 1220 | wait(0.02); //wait 750 ms for convert temperature to complete |
| cyberjoey | 9:36ba3626aab7 | 1221 | for(j=device;j<=arg1;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1222 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1223 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1224 | } |
| cyberjoey | 9:36ba3626aab7 | 1225 | pc.printf("device[%02X] temperature[%.3f]\r\n",j,get_temperature(device_id)); |
| cyberjoey | 9:36ba3626aab7 | 1226 | } |
| cyberjoey | 9:36ba3626aab7 | 1227 | //pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1228 | } |
| cyberjoey | 9:36ba3626aab7 | 1229 | break; |
| cyberjoey | 9:36ba3626aab7 | 1230 | |
| cyberjoey | 9:36ba3626aab7 | 1231 | case 'w': |
| cyberjoey | 9:36ba3626aab7 | 1232 | case 'W': //write register "w device w.addr data" |
| cyberjoey | 9:36ba3626aab7 | 1233 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1234 | device_id[k] = rom_id_list[device][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1235 | } |
| cyberjoey | 9:36ba3626aab7 | 1236 | data[0] = arg2; |
| cyberjoey | 9:36ba3626aab7 | 1237 | write_OW_register(device_id, arg1, 1, data); |
| cyberjoey | 9:36ba3626aab7 | 1238 | |
| cyberjoey | 9:36ba3626aab7 | 1239 | pc.printf("write -- device[%02X] add[%02X] data[%02X] ",device,arg1,arg2); |
| cyberjoey | 9:36ba3626aab7 | 1240 | |
| cyberjoey | 9:36ba3626aab7 | 1241 | // get CRC |
| cyberjoey | 9:36ba3626aab7 | 1242 | //OW_read_byte(data,2); |
| cyberjoey | 9:36ba3626aab7 | 1243 | pc.printf("crc[%02X %02X]\r\n",data[0],data[1]); |
| cyberjoey | 9:36ba3626aab7 | 1244 | |
| cyberjoey | 9:36ba3626aab7 | 1245 | break; |
| cyberjoey | 9:36ba3626aab7 | 1246 | |
| cyberjoey | 9:36ba3626aab7 | 1247 | case 'x': |
| cyberjoey | 9:36ba3626aab7 | 1248 | case 'X': // experimental modes |
| cyberjoey | 9:36ba3626aab7 | 1249 | // ****************** set up ADC enabled in test mode***************** |
| cyberjoey | 9:36ba3626aab7 | 1250 | pc.printf("<set ADC_ENABLED in test mode>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1251 | |
| cyberjoey | 9:36ba3626aab7 | 1252 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1253 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1254 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1255 | } |
| cyberjoey | 9:36ba3626aab7 | 1256 | set_test_mode(device_id); |
| cyberjoey | 9:36ba3626aab7 | 1257 | //db.printf("device[%2X] in test mode\r\n",j); |
| cyberjoey | 9:36ba3626aab7 | 1258 | } |
| cyberjoey | 9:36ba3626aab7 | 1259 | break; |
| cyberjoey | 9:36ba3626aab7 | 1260 | |
| cyberjoey | 9:36ba3626aab7 | 1261 | |
| cyberjoey | 9:36ba3626aab7 | 1262 | }//end switch(c) |
| cyberjoey | 9:36ba3626aab7 | 1263 | }//if(n>0) |
| cyberjoey | 9:36ba3626aab7 | 1264 | }//end if(CR) |
| cyberjoey | 9:36ba3626aab7 | 1265 | if(rx_buff[rx_index] == BS){//backspace received, back up buffer pointer |
| cyberjoey | 9:36ba3626aab7 | 1266 | if(rx_index>0)rx_index--;//remove last char from buffer if not at start. |
| cyberjoey | 9:36ba3626aab7 | 1267 | }else rx_index++; |
| cyberjoey | 9:36ba3626aab7 | 1268 | }//end while(pc.redable()) |
| cyberjoey | 9:36ba3626aab7 | 1269 | wait(0.1); // slow down polling |
| switches | 0:60a522ae2e35 | 1270 | |
| cyberjoey | 9:36ba3626aab7 | 1271 | }// end (while tick == false) |
| cyberjoey | 9:36ba3626aab7 | 1272 | tick_flag = false; // set to false for next time |
| cyberjoey | 9:36ba3626aab7 | 1273 | |
| cyberjoey | 9:36ba3626aab7 | 1274 | // only reached when tick_flag = true otherwise stuck in pc.readable() loop |
| cyberjoey | 9:36ba3626aab7 | 1275 | if(log_flag == true){ |
| cyberjoey | 9:36ba3626aab7 | 1276 | //pc.printf("tc{%5d] tts[%5d]\n\r",time_count,time_to_sample); |
| cyberjoey | 9:36ba3626aab7 | 1277 | if(time_count >= time_to_sample){ //take next sample |
| cyberjoey | 9:36ba3626aab7 | 1278 | time_to_sample += log_interval; // calculate time for next sample |
| cyberjoey | 9:36ba3626aab7 | 1279 | |
| cyberjoey | 9:36ba3626aab7 | 1280 | // start conversion |
| cyberjoey | 9:36ba3626aab7 | 1281 | convert_temperature(); |
| cyberjoey | 9:36ba3626aab7 | 1282 | wait(0.02); //wait 20ms for convert to complete |
| cyberjoey | 9:36ba3626aab7 | 1283 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1284 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1285 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1286 | } |
| cyberjoey | 9:36ba3626aab7 | 1287 | T[j] = get_temperature(device_id); |
| cyberjoey | 9:36ba3626aab7 | 1288 | } |
| cyberjoey | 9:36ba3626aab7 | 1289 | /* |
| cyberjoey | 9:36ba3626aab7 | 1290 | // print out time count and temperatures |
| cyberjoey | 9:36ba3626aab7 | 1291 | pc.printf("%5d",time_count); |
| cyberjoey | 9:36ba3626aab7 | 1292 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1293 | pc.printf(", %7.3f",T[j]); |
| cyberjoey | 9:36ba3626aab7 | 1294 | } |
| cyberjoey | 9:36ba3626aab7 | 1295 | pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1296 | */ |
| cyberjoey | 10:148da21c297e | 1297 | FILE *fp = fopen(log_file, "a"); |
| cyberjoey | 9:36ba3626aab7 | 1298 | if (fp != NULL) |
| cyberjoey | 9:36ba3626aab7 | 1299 | { |
| cyberjoey | 9:36ba3626aab7 | 1300 | //fprintf(fp, "\n"); |
| cyberjoey | 10:148da21c297e | 1301 | fprintf(fp, "%d",time_count); |
| cyberjoey | 9:36ba3626aab7 | 1302 | for(j=0;j<device_count;j++) |
| cyberjoey | 9:36ba3626aab7 | 1303 | { |
| cyberjoey | 9:36ba3626aab7 | 1304 | fprintf(fp,", %7.3f",T[j]); |
| cyberjoey | 9:36ba3626aab7 | 1305 | } |
| cyberjoey | 9:36ba3626aab7 | 1306 | fprintf(fp,"\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1307 | |
| cyberjoey | 9:36ba3626aab7 | 1308 | fclose(fp); |
| cyberjoey | 9:36ba3626aab7 | 1309 | } |
| cyberjoey | 9:36ba3626aab7 | 1310 | }// end if(time_count >= time_to_sample) |
| cyberjoey | 9:36ba3626aab7 | 1311 | time_count ++; // |
| cyberjoey | 9:36ba3626aab7 | 1312 | |
| cyberjoey | 9:36ba3626aab7 | 1313 | }// end if(log_flag) |
| cyberjoey | 9:36ba3626aab7 | 1314 | |
| cyberjoey | 9:36ba3626aab7 | 1315 | }//end while(1) |
| switches | 0:60a522ae2e35 | 1316 | } |
| switches | 0:60a522ae2e35 | 1317 |
