Simple demo for exercising the board

Dependencies:   MAX11300 MAX4822 OneWire Terminal ds3231 mbed

Committer:
j3
Date:
Fri Jul 29 06:46:54 2016 +0000
Revision:
0:24b8a6f0a563
Child:
1:03c04017a728
working demo for exercising brd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:24b8a6f0a563 1 /**********************************************************************
j3 0:24b8a6f0a563 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:24b8a6f0a563 3 *
j3 0:24b8a6f0a563 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:24b8a6f0a563 5 * copy of this software and associated documentation files (the "Software"),
j3 0:24b8a6f0a563 6 * to deal in the Software without restriction, including without limitation
j3 0:24b8a6f0a563 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:24b8a6f0a563 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:24b8a6f0a563 9 * Software is furnished to do so, subject to the following conditions:
j3 0:24b8a6f0a563 10 *
j3 0:24b8a6f0a563 11 * The above copyright notice and this permission notice shall be included
j3 0:24b8a6f0a563 12 * in all copies or substantial portions of the Software.
j3 0:24b8a6f0a563 13 *
j3 0:24b8a6f0a563 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:24b8a6f0a563 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:24b8a6f0a563 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:24b8a6f0a563 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:24b8a6f0a563 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:24b8a6f0a563 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:24b8a6f0a563 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:24b8a6f0a563 21 *
j3 0:24b8a6f0a563 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:24b8a6f0a563 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:24b8a6f0a563 24 * Products, Inc. Branding Policy.
j3 0:24b8a6f0a563 25 *
j3 0:24b8a6f0a563 26 * The mere transfer of this software does not imply any licenses
j3 0:24b8a6f0a563 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:24b8a6f0a563 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:24b8a6f0a563 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:24b8a6f0a563 30 * ownership rights.
j3 0:24b8a6f0a563 31 **********************************************************************/
j3 0:24b8a6f0a563 32
j3 0:24b8a6f0a563 33 #include "mbed.h"
j3 0:24b8a6f0a563 34 #include "ds3231.h"
j3 0:24b8a6f0a563 35 #include "MAX4822.h"
j3 0:24b8a6f0a563 36 #include "MAX11300.h"
j3 0:24b8a6f0a563 37 #include "Terminal.h"
j3 0:24b8a6f0a563 38 #include "OneWire.h"
j3 0:24b8a6f0a563 39
j3 0:24b8a6f0a563 40 using namespace OneWire;
j3 0:24b8a6f0a563 41 using namespace RomCommands;
j3 0:24b8a6f0a563 42
j3 0:24b8a6f0a563 43 void set_rtc(Terminal & term, Ds3231 & rtc);
j3 0:24b8a6f0a563 44 void get_time_date(Terminal & term, Ds3231 & rtc);
j3 0:24b8a6f0a563 45 void search_ow_bus(Terminal & term, DS2484 & owm);
j3 0:24b8a6f0a563 46 void print_rom_id(Terminal & term, RomId & romId);
j3 0:24b8a6f0a563 47
j3 0:24b8a6f0a563 48 int main(void)
j3 0:24b8a6f0a563 49 {
j3 0:24b8a6f0a563 50 Terminal term(USBTX, USBRX);
j3 0:24b8a6f0a563 51 term.baud(57600);
j3 0:24b8a6f0a563 52 term.cls();
j3 0:24b8a6f0a563 53 term.home();
j3 0:24b8a6f0a563 54 term.printf("Starting Demo\n");
j3 0:24b8a6f0a563 55
j3 0:24b8a6f0a563 56 SPI spi_bus(D11, D12, D13);
j3 0:24b8a6f0a563 57 MAX4822 rly_drvr(spi_bus, D10);
j3 0:24b8a6f0a563 58 DigitalOut rly_drvr_set(D7, 1);
j3 0:24b8a6f0a563 59 DigitalOut rly_drvr_reset(D6, 1);
j3 0:24b8a6f0a563 60
j3 0:24b8a6f0a563 61 rly_drvr.reset_all_relays(rly_drvr_reset);
j3 0:24b8a6f0a563 62
j3 0:24b8a6f0a563 63 MAX11300 pixi(spi_bus, D8);
j3 0:24b8a6f0a563 64
j3 0:24b8a6f0a563 65 //set all analog outs to 0
j3 0:24b8a6f0a563 66 for(uint8_t idx = 0; idx < 9; idx++)
j3 0:24b8a6f0a563 67 {
j3 0:24b8a6f0a563 68 pixi.single_ended_dac_write(static_cast<MAX11300::MAX11300_Ports>(idx), 0);
j3 0:24b8a6f0a563 69 }
j3 0:24b8a6f0a563 70
j3 0:24b8a6f0a563 71 //ensure latching relays are reset
j3 0:24b8a6f0a563 72 for(uint8_t idx = 0; idx < 3; idx++)
j3 0:24b8a6f0a563 73 {
j3 0:24b8a6f0a563 74 pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(idx + 9), 1);
j3 0:24b8a6f0a563 75 wait_ms(100);
j3 0:24b8a6f0a563 76 pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(idx + 9), 0);
j3 0:24b8a6f0a563 77 }
j3 0:24b8a6f0a563 78
j3 0:24b8a6f0a563 79 I2C i2c_bus(D14, D15);
j3 0:24b8a6f0a563 80 i2c_bus.frequency(400000);
j3 0:24b8a6f0a563 81
j3 0:24b8a6f0a563 82 Ds3231 rtc(i2c_bus);
j3 0:24b8a6f0a563 83
j3 0:24b8a6f0a563 84 DS2484 owm(i2c_bus);
j3 0:24b8a6f0a563 85 OneWireMaster::CmdResult owm_result = owm.OWInitMaster();
j3 0:24b8a6f0a563 86 while(owm_result != OneWireMaster::Success)
j3 0:24b8a6f0a563 87 {
j3 0:24b8a6f0a563 88 wait_ms(100);
j3 0:24b8a6f0a563 89 term.printf("Failed to initialize OneWire master\n");
j3 0:24b8a6f0a563 90 owm_result = owm.OWInitMaster();
j3 0:24b8a6f0a563 91 }
j3 0:24b8a6f0a563 92 term.printf("OneWire master initialized\n\n");
j3 0:24b8a6f0a563 93
j3 0:24b8a6f0a563 94 int32_t user_entry = 0;
j3 0:24b8a6f0a563 95 int32_t relay = 0;
j3 0:24b8a6f0a563 96
j3 0:24b8a6f0a563 97 uint16_t analog_out, analog_val;
j3 0:24b8a6f0a563 98
j3 0:24b8a6f0a563 99 const int32_t QUIT = 14;
j3 0:24b8a6f0a563 100
j3 0:24b8a6f0a563 101 while(user_entry != QUIT)
j3 0:24b8a6f0a563 102 {
j3 0:24b8a6f0a563 103 term.printf("1. Set RTC\n");
j3 0:24b8a6f0a563 104 term.printf("2. Get Time/Date\n");
j3 0:24b8a6f0a563 105 term.printf("3. Search OneWire bus\n");
j3 0:24b8a6f0a563 106 term.printf("4. Set non-latching relay\n");
j3 0:24b8a6f0a563 107 term.printf("5. Reset non-latching relay\n");
j3 0:24b8a6f0a563 108 term.printf("6. Set all non-latching relays\n");
j3 0:24b8a6f0a563 109 term.printf("7. Reset all non-latching relays\n");
j3 0:24b8a6f0a563 110 term.printf("8. Set latching relay\n");
j3 0:24b8a6f0a563 111 term.printf("9. Reset latching relay\n");
j3 0:24b8a6f0a563 112 term.printf("10. Set 0-10 analog out\n");
j3 0:24b8a6f0a563 113 term.printf("11. Set 4-20 out\n");
j3 0:24b8a6f0a563 114 term.printf("12. Get 4-20 in\n");
j3 0:24b8a6f0a563 115 term.printf("%d. Clear Screen\n", (QUIT - 1));
j3 0:24b8a6f0a563 116 term.printf("%d. Quit\n\n", QUIT);
j3 0:24b8a6f0a563 117
j3 0:24b8a6f0a563 118 user_entry = term.get_int32("Please select an option above: ", 1, QUIT);
j3 0:24b8a6f0a563 119
j3 0:24b8a6f0a563 120
j3 0:24b8a6f0a563 121 //get non-latching relay
j3 0:24b8a6f0a563 122 if((user_entry == 4) || (user_entry == 5))
j3 0:24b8a6f0a563 123 {
j3 0:24b8a6f0a563 124 relay = term.get_int32("Select a relay from 1 to 8: ", 1, 8);
j3 0:24b8a6f0a563 125 }
j3 0:24b8a6f0a563 126
j3 0:24b8a6f0a563 127 //get latching relay
j3 0:24b8a6f0a563 128 if((user_entry == 8) || (user_entry == 9))
j3 0:24b8a6f0a563 129 {
j3 0:24b8a6f0a563 130 relay = (8 + term.get_int32("Select a relay from 1 to 3: ", 1, 3));
j3 0:24b8a6f0a563 131 }
j3 0:24b8a6f0a563 132
j3 0:24b8a6f0a563 133 switch(user_entry)
j3 0:24b8a6f0a563 134 {
j3 0:24b8a6f0a563 135 case 1:
j3 0:24b8a6f0a563 136 set_rtc(term, rtc);
j3 0:24b8a6f0a563 137 break;
j3 0:24b8a6f0a563 138
j3 0:24b8a6f0a563 139 case 2:
j3 0:24b8a6f0a563 140 get_time_date(term, rtc);
j3 0:24b8a6f0a563 141 break;
j3 0:24b8a6f0a563 142
j3 0:24b8a6f0a563 143 case 3:
j3 0:24b8a6f0a563 144 search_ow_bus(term, owm);
j3 0:24b8a6f0a563 145 break;
j3 0:24b8a6f0a563 146
j3 0:24b8a6f0a563 147 case 4:
j3 0:24b8a6f0a563 148 rly_drvr.set_relay(static_cast<MAX4822::RelayChannel>(relay));
j3 0:24b8a6f0a563 149 break;
j3 0:24b8a6f0a563 150
j3 0:24b8a6f0a563 151 case 5:
j3 0:24b8a6f0a563 152 rly_drvr.reset_relay(static_cast<MAX4822::RelayChannel>(relay));
j3 0:24b8a6f0a563 153 break;
j3 0:24b8a6f0a563 154
j3 0:24b8a6f0a563 155 case 6:
j3 0:24b8a6f0a563 156 rly_drvr.set_all_relays(rly_drvr_set);
j3 0:24b8a6f0a563 157 term.printf("\n");
j3 0:24b8a6f0a563 158 break;
j3 0:24b8a6f0a563 159
j3 0:24b8a6f0a563 160 case 7:
j3 0:24b8a6f0a563 161 rly_drvr.reset_all_relays(rly_drvr_reset);
j3 0:24b8a6f0a563 162 term.printf("\n");
j3 0:24b8a6f0a563 163 break;
j3 0:24b8a6f0a563 164
j3 0:24b8a6f0a563 165 case 8:
j3 0:24b8a6f0a563 166 pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(relay), 1);
j3 0:24b8a6f0a563 167 break;
j3 0:24b8a6f0a563 168
j3 0:24b8a6f0a563 169 case 9:
j3 0:24b8a6f0a563 170 pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(relay), 0);
j3 0:24b8a6f0a563 171 break;
j3 0:24b8a6f0a563 172
j3 0:24b8a6f0a563 173 case 10:
j3 0:24b8a6f0a563 174 analog_out = term.get_int32("Please select a 0-10V output; 0-7: ", 0, 7);
j3 0:24b8a6f0a563 175 analog_val = term.get_int32("please select a value from 0-4095: ", 0, 4095);
j3 0:24b8a6f0a563 176 pixi.single_ended_dac_write(static_cast<MAX11300::MAX11300_Ports>(analog_out), analog_val);
j3 0:24b8a6f0a563 177 break;
j3 0:24b8a6f0a563 178
j3 0:24b8a6f0a563 179 case 11:
j3 0:24b8a6f0a563 180 analog_val = term.get_int32("please select a value from 0-4095: ", 0, 4095);
j3 0:24b8a6f0a563 181 pixi.single_ended_dac_write(MAX11300::PORT8, analog_val);
j3 0:24b8a6f0a563 182 break;
j3 0:24b8a6f0a563 183
j3 0:24b8a6f0a563 184 case 12:
j3 0:24b8a6f0a563 185 pixi.single_ended_adc_read(MAX11300::PORT12, analog_val);
j3 0:24b8a6f0a563 186 term.printf("4-20 in = %d\n", analog_val);
j3 0:24b8a6f0a563 187 break;
j3 0:24b8a6f0a563 188
j3 0:24b8a6f0a563 189 case (QUIT - 1):
j3 0:24b8a6f0a563 190 term.cls();
j3 0:24b8a6f0a563 191 term.home();
j3 0:24b8a6f0a563 192 break;
j3 0:24b8a6f0a563 193
j3 0:24b8a6f0a563 194 case QUIT:
j3 0:24b8a6f0a563 195 term.printf("\nEnding Program\n");
j3 0:24b8a6f0a563 196 break;
j3 0:24b8a6f0a563 197
j3 0:24b8a6f0a563 198 default:
j3 0:24b8a6f0a563 199 mbed_die();
j3 0:24b8a6f0a563 200 break;
j3 0:24b8a6f0a563 201 }
j3 0:24b8a6f0a563 202 }
j3 0:24b8a6f0a563 203 }
j3 0:24b8a6f0a563 204
j3 0:24b8a6f0a563 205 //*********************************************************************
j3 0:24b8a6f0a563 206 void set_rtc(Terminal & term, Ds3231 & rtc)
j3 0:24b8a6f0a563 207 {
j3 0:24b8a6f0a563 208 //default, use bit masks in ds3231.h for desired operation
j3 0:24b8a6f0a563 209 ds3231_cntl_stat_t rtc_control_status = {0,0};
j3 0:24b8a6f0a563 210 ds3231_time_t rtc_time;
j3 0:24b8a6f0a563 211 ds3231_calendar_t rtc_calendar;
j3 0:24b8a6f0a563 212
j3 0:24b8a6f0a563 213 rtc.set_cntl_stat_reg(rtc_control_status);
j3 0:24b8a6f0a563 214
j3 0:24b8a6f0a563 215 //get day from user
j3 0:24b8a6f0a563 216 rtc_calendar.day = term.get_int32("\nPlease enter day of week, 1 for Sunday (1-7): ", 1, 7);
j3 0:24b8a6f0a563 217
j3 0:24b8a6f0a563 218 //get day of month from user
j3 0:24b8a6f0a563 219 rtc_calendar.date = term.get_int32("\nPlease enter day of month (1-31): ", 1, 31);
j3 0:24b8a6f0a563 220
j3 0:24b8a6f0a563 221 //get month from user
j3 0:24b8a6f0a563 222 rtc_calendar.month = term.get_int32("\nPlease enter the month, 1 for January (1-12): ", 1, 12);
j3 0:24b8a6f0a563 223
j3 0:24b8a6f0a563 224 //get year from user
j3 0:24b8a6f0a563 225 rtc_calendar.year = term.get_int32("\nPlease enter the year (0-99): ", 0, 99);
j3 0:24b8a6f0a563 226
j3 0:24b8a6f0a563 227 //Get time mode
j3 0:24b8a6f0a563 228 rtc_time.mode = term.get_int32("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1);
j3 0:24b8a6f0a563 229
j3 0:24b8a6f0a563 230 if(rtc_time.mode)
j3 0:24b8a6f0a563 231 {
j3 0:24b8a6f0a563 232 //Get AM/PM status
j3 0:24b8a6f0a563 233 rtc_time.am_pm = term.get_int32("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1);
j3 0:24b8a6f0a563 234 //Get hour from user
j3 0:24b8a6f0a563 235 rtc_time.hours = term.get_int32("\nPlease enter the hour (1-12): ", 1, 12);
j3 0:24b8a6f0a563 236 }
j3 0:24b8a6f0a563 237 else
j3 0:24b8a6f0a563 238 {
j3 0:24b8a6f0a563 239 //Get hour from user
j3 0:24b8a6f0a563 240 rtc_time.hours = term.get_int32("\nPlease enter the hour (0-23): ", 0, 23);
j3 0:24b8a6f0a563 241 }
j3 0:24b8a6f0a563 242
j3 0:24b8a6f0a563 243 //Get minutes from user
j3 0:24b8a6f0a563 244 rtc_time.minutes = term.get_int32("\nPlease enter the minute (0-59): ", 0, 59);
j3 0:24b8a6f0a563 245
j3 0:24b8a6f0a563 246
j3 0:24b8a6f0a563 247 //Get seconds from user
j3 0:24b8a6f0a563 248 rtc_time.seconds = term.get_int32("\nPlease enter the second (0-59): ", 0, 59);
j3 0:24b8a6f0a563 249
j3 0:24b8a6f0a563 250 term.printf("\n");
j3 0:24b8a6f0a563 251
j3 0:24b8a6f0a563 252 rtc.set_time(rtc_time);
j3 0:24b8a6f0a563 253 rtc.set_calendar(rtc_calendar);
j3 0:24b8a6f0a563 254 }
j3 0:24b8a6f0a563 255
j3 0:24b8a6f0a563 256 //*********************************************************************
j3 0:24b8a6f0a563 257 void get_time_date(Terminal & term, Ds3231 & rtc)
j3 0:24b8a6f0a563 258 {
j3 0:24b8a6f0a563 259 time_t epoch_time;
j3 0:24b8a6f0a563 260
j3 0:24b8a6f0a563 261 epoch_time = rtc.get_epoch();
j3 0:24b8a6f0a563 262 term.printf("\n%s\n", ctime(&epoch_time));
j3 0:24b8a6f0a563 263 }
j3 0:24b8a6f0a563 264
j3 0:24b8a6f0a563 265 //*********************************************************************
j3 0:24b8a6f0a563 266 void search_ow_bus(Terminal & term, DS2484 & owm)
j3 0:24b8a6f0a563 267 {
j3 0:24b8a6f0a563 268 OneWireMaster::CmdResult result = owm.OWReset();
j3 0:24b8a6f0a563 269 SearchState search_state;
j3 0:24b8a6f0a563 270
j3 0:24b8a6f0a563 271 if(result == OneWireMaster::Success)
j3 0:24b8a6f0a563 272 {
j3 0:24b8a6f0a563 273 term.printf("\nOWReset success, starting search\n");
j3 0:24b8a6f0a563 274
j3 0:24b8a6f0a563 275 result = OWFirst(owm, search_state);
j3 0:24b8a6f0a563 276 if(result == OneWireMaster::Success)
j3 0:24b8a6f0a563 277 {
j3 0:24b8a6f0a563 278 while(result == OneWireMaster::Success)
j3 0:24b8a6f0a563 279 {
j3 0:24b8a6f0a563 280 //print current devices rom id
j3 0:24b8a6f0a563 281 print_rom_id(term, search_state.romId);
j3 0:24b8a6f0a563 282
j3 0:24b8a6f0a563 283 //find the next device if any
j3 0:24b8a6f0a563 284 result = OWNext(owm, search_state);
j3 0:24b8a6f0a563 285 }
j3 0:24b8a6f0a563 286 term.printf("\n");
j3 0:24b8a6f0a563 287 }
j3 0:24b8a6f0a563 288 else
j3 0:24b8a6f0a563 289 {
j3 0:24b8a6f0a563 290 term.printf("\nSearch failed\n");
j3 0:24b8a6f0a563 291 term.printf("\nError code = %d\n", result);
j3 0:24b8a6f0a563 292 }
j3 0:24b8a6f0a563 293 }
j3 0:24b8a6f0a563 294 else
j3 0:24b8a6f0a563 295 {
j3 0:24b8a6f0a563 296 term.printf("\nFailed to find any 1-wire devices on bus\n");
j3 0:24b8a6f0a563 297 term.printf("\nError code = %d\n", result);
j3 0:24b8a6f0a563 298 }
j3 0:24b8a6f0a563 299 }
j3 0:24b8a6f0a563 300
j3 0:24b8a6f0a563 301 //*********************************************************************
j3 0:24b8a6f0a563 302 void print_rom_id(Terminal & term, RomId & romId)
j3 0:24b8a6f0a563 303 {
j3 0:24b8a6f0a563 304 //print the rom number
j3 0:24b8a6f0a563 305 term.printf("\n");
j3 0:24b8a6f0a563 306 int8_t idx = (RomId::byteLen - 1);
j3 0:24b8a6f0a563 307 do
j3 0:24b8a6f0a563 308 {
j3 0:24b8a6f0a563 309 if(romId[idx] < 16)
j3 0:24b8a6f0a563 310 {
j3 0:24b8a6f0a563 311 term.printf("0x0%x ", romId[idx--]);
j3 0:24b8a6f0a563 312 }
j3 0:24b8a6f0a563 313 else
j3 0:24b8a6f0a563 314 {
j3 0:24b8a6f0a563 315 term.printf("0x%2x ", romId[idx--]);
j3 0:24b8a6f0a563 316 }
j3 0:24b8a6f0a563 317 }
j3 0:24b8a6f0a563 318 while(idx >= 0);
j3 0:24b8a6f0a563 319 term.printf("\n");
j3 0:24b8a6f0a563 320 }