Eval-TempSense-ARDZ application code
Dependencies: platform_drivers ADT73xx-ADT74xx
main.cpp@1:af9efbb9e9c5, 2019-08-28 (annotated)
- Committer:
- EndaKilgarriff
- Date:
- Wed Aug 28 15:38:19 2019 +0000
- Revision:
- 1:af9efbb9e9c5
- Parent:
- 0:782471fb58f7
Inital Commit for Eval-TempSense-ARDZ
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EndaKilgarriff | 1:af9efbb9e9c5 | 1 | /*************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 2 | * @file main.cpp |
EndaKilgarriff | 1:af9efbb9e9c5 | 3 | * @brief Main application code for EVAL-TempSense-ARDZ mbed example program |
EndaKilgarriff | 1:af9efbb9e9c5 | 4 | * @author ssmith (sean.smith@analog.com) |
EndaKilgarriff | 1:af9efbb9e9c5 | 5 | ****************************************************************************** |
EndaKilgarriff | 1:af9efbb9e9c5 | 6 | * Copyright (c) 2019 Analog Devices, Inc. |
EndaKilgarriff | 1:af9efbb9e9c5 | 7 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 8 | * All rights reserved. |
EndaKilgarriff | 1:af9efbb9e9c5 | 9 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 10 | * Redistribution and use in source and binary forms, with or without |
EndaKilgarriff | 1:af9efbb9e9c5 | 11 | * modification, are permitted provided that the following conditions are met: |
EndaKilgarriff | 1:af9efbb9e9c5 | 12 | * - Redistributions of source code must retain the above copyright notice, |
EndaKilgarriff | 1:af9efbb9e9c5 | 13 | * this list of conditions and the following disclaimer. |
EndaKilgarriff | 1:af9efbb9e9c5 | 14 | * - Redistributions in binary form must reproduce the above copyright notice, |
EndaKilgarriff | 1:af9efbb9e9c5 | 15 | * this list of conditions and the following disclaimer in the documentation |
EndaKilgarriff | 1:af9efbb9e9c5 | 16 | * and/or other materials provided with the distribution. |
EndaKilgarriff | 1:af9efbb9e9c5 | 17 | * - Modified versions of the software must be conspicuously marked as such. |
EndaKilgarriff | 1:af9efbb9e9c5 | 18 | * - This software is licensed solely and exclusively for use with |
EndaKilgarriff | 1:af9efbb9e9c5 | 19 | * processors/products manufactured by or for Analog Devices, Inc. |
EndaKilgarriff | 1:af9efbb9e9c5 | 20 | * - This software may not be combined or merged with other code in any manner |
EndaKilgarriff | 1:af9efbb9e9c5 | 21 | * that would cause the software to become subject to terms and |
EndaKilgarriff | 1:af9efbb9e9c5 | 22 | * conditions which differ from those listed here. |
EndaKilgarriff | 1:af9efbb9e9c5 | 23 | * - Neither the name of Analog Devices, Inc. nor the names of its |
EndaKilgarriff | 1:af9efbb9e9c5 | 24 | * contributors may be used to endorse or promote products derived |
EndaKilgarriff | 1:af9efbb9e9c5 | 25 | * from this software without specific prior written permission. |
EndaKilgarriff | 1:af9efbb9e9c5 | 26 | * - The use of this software may or may not infringe the patent rights |
EndaKilgarriff | 1:af9efbb9e9c5 | 27 | * of one or more patent holders. This license does not release you from |
EndaKilgarriff | 1:af9efbb9e9c5 | 28 | * the requirement that you obtain separate licenses from these patent |
EndaKilgarriff | 1:af9efbb9e9c5 | 29 | * holders to use this software. |
EndaKilgarriff | 1:af9efbb9e9c5 | 30 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 31 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" |
EndaKilgarriff | 1:af9efbb9e9c5 | 32 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
EndaKilgarriff | 1:af9efbb9e9c5 | 33 | * NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A |
EndaKilgarriff | 1:af9efbb9e9c5 | 34 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, |
EndaKilgarriff | 1:af9efbb9e9c5 | 35 | * INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
EndaKilgarriff | 1:af9efbb9e9c5 | 36 | * SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
EndaKilgarriff | 1:af9efbb9e9c5 | 37 | * (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF |
EndaKilgarriff | 1:af9efbb9e9c5 | 38 | * INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE |
EndaKilgarriff | 1:af9efbb9e9c5 | 39 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
EndaKilgarriff | 1:af9efbb9e9c5 | 40 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
EndaKilgarriff | 1:af9efbb9e9c5 | 41 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
EndaKilgarriff | 1:af9efbb9e9c5 | 42 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
EndaKilgarriff | 1:af9efbb9e9c5 | 43 | * POSSIBILITY OF SUCH DAMAGE. |
EndaKilgarriff | 1:af9efbb9e9c5 | 44 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 45 | * 20180927-7CBSD SLA |
EndaKilgarriff | 1:af9efbb9e9c5 | 46 | *****************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 47 | #include <cstdio> |
EndaKilgarriff | 1:af9efbb9e9c5 | 48 | #include <mbed.h> |
EndaKilgarriff | 1:af9efbb9e9c5 | 49 | #include "app_config.h" |
EndaKilgarriff | 1:af9efbb9e9c5 | 50 | #include <cmath> |
EndaKilgarriff | 1:af9efbb9e9c5 | 51 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 52 | #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) |
EndaKilgarriff | 1:af9efbb9e9c5 | 53 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 54 | #define TEMP_MIN -40 |
EndaKilgarriff | 1:af9efbb9e9c5 | 55 | #define TEMP_MAX 150 |
EndaKilgarriff | 1:af9efbb9e9c5 | 56 | #define MAX_HYST_TEMP 15 |
EndaKilgarriff | 1:af9efbb9e9c5 | 57 | #define MIN_HYST_TEMP 0 |
EndaKilgarriff | 1:af9efbb9e9c5 | 58 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 59 | #define RESET_DELAY 500 |
EndaKilgarriff | 1:af9efbb9e9c5 | 60 | #define WAIT_MENU_TIME 1 |
EndaKilgarriff | 1:af9efbb9e9c5 | 61 | #define NOT_USED 0 |
EndaKilgarriff | 1:af9efbb9e9c5 | 62 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 63 | static void print_title(void); |
EndaKilgarriff | 1:af9efbb9e9c5 | 64 | static void print_prompt(void); |
EndaKilgarriff | 1:af9efbb9e9c5 | 65 | static uint8_t select_device(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 66 | static void print_active_device(int ext_int_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 67 | static int get_menu_select(uint8_t *menu_select); |
EndaKilgarriff | 1:af9efbb9e9c5 | 68 | static uint8_t read_temperature(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 69 | static uint8_t set_resolution(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 70 | static uint8_t set_op_mode(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 71 | static uint8_t bunch_of_temps(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 72 | static uint8_t readback_reg(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 73 | static uint8_t reset_interface(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 74 | static uint8_t write_to_setpoint_reg(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 75 | static uint8_t set_fault_queue(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 76 | static uint8_t set_ct_int_config(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 77 | static void microcontroller_reset(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 78 | static int input_check(int input_val, |
EndaKilgarriff | 1:af9efbb9e9c5 | 79 | int lowest_accepted_val, |
EndaKilgarriff | 1:af9efbb9e9c5 | 80 | int highest_accepted_val, |
EndaKilgarriff | 1:af9efbb9e9c5 | 81 | int invalid_check); |
EndaKilgarriff | 1:af9efbb9e9c5 | 82 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 83 | #ifdef __cplusplus |
EndaKilgarriff | 1:af9efbb9e9c5 | 84 | extern "C" |
EndaKilgarriff | 1:af9efbb9e9c5 | 85 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 86 | #endif |
EndaKilgarriff | 1:af9efbb9e9c5 | 87 | #include "adt7420.h" |
EndaKilgarriff | 1:af9efbb9e9c5 | 88 | #ifdef __cplusplus |
EndaKilgarriff | 1:af9efbb9e9c5 | 89 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 90 | #endif // __cplusplus |
EndaKilgarriff | 1:af9efbb9e9c5 | 91 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 92 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 93 | /******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 94 | /************************** Variables Declarations ****************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 95 | /******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 96 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 97 | /** |
EndaKilgarriff | 1:af9efbb9e9c5 | 98 | The following definitions are a requirement for the platform_driver |
EndaKilgarriff | 1:af9efbb9e9c5 | 99 | Pin are changed if required in the app_config.h file |
EndaKilgarriff | 1:af9efbb9e9c5 | 100 | ***/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 101 | PinName slave_selects[MAX_SLAVE_SELECTS] = { SPI_CS, SPI_CSE }; |
EndaKilgarriff | 1:af9efbb9e9c5 | 102 | mbed::SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); |
EndaKilgarriff | 1:af9efbb9e9c5 | 103 | mbed::I2C i2c(I2C_SDA, I2C_SCL); |
EndaKilgarriff | 1:af9efbb9e9c5 | 104 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 105 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 106 | i2c_init_param i2c_params = { |
EndaKilgarriff | 1:af9efbb9e9c5 | 107 | GENERIC_I2C, // i2c type |
EndaKilgarriff | 1:af9efbb9e9c5 | 108 | NOT_USED, // i2c id |
EndaKilgarriff | 1:af9efbb9e9c5 | 109 | 100000, // i2c max speed (hz) |
EndaKilgarriff | 1:af9efbb9e9c5 | 110 | INTERNAL_I2C, // i2c slave address (defined in app_config.h) |
EndaKilgarriff | 1:af9efbb9e9c5 | 111 | }; |
EndaKilgarriff | 1:af9efbb9e9c5 | 112 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 113 | spi_init_param spi_params = { |
EndaKilgarriff | 1:af9efbb9e9c5 | 114 | MBED, //For future ADI use |
EndaKilgarriff | 1:af9efbb9e9c5 | 115 | GENERIC_SPI, //For future ADI use |
EndaKilgarriff | 1:af9efbb9e9c5 | 116 | NOT_USED, //id |
EndaKilgarriff | 1:af9efbb9e9c5 | 117 | 1000000, //SPI frequency (Hz) |
EndaKilgarriff | 1:af9efbb9e9c5 | 118 | SPI_MODE_3, //CPOL/CPHA settings for your device |
EndaKilgarriff | 1:af9efbb9e9c5 | 119 | INTERNAL_SPI, //CS index for PinName (defined in app_config.h) |
EndaKilgarriff | 1:af9efbb9e9c5 | 120 | }; |
EndaKilgarriff | 1:af9efbb9e9c5 | 121 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 122 | adt7420_init_param init_params = { |
EndaKilgarriff | 1:af9efbb9e9c5 | 123 | i2c_params, // I2C parameters |
EndaKilgarriff | 1:af9efbb9e9c5 | 124 | spi_params, // SPI parameters |
EndaKilgarriff | 1:af9efbb9e9c5 | 125 | NOT_USED, //Resolution setting |
EndaKilgarriff | 1:af9efbb9e9c5 | 126 | ACTIVE_DEVICE, //Set this in app_config.h |
EndaKilgarriff | 1:af9efbb9e9c5 | 127 | }; |
EndaKilgarriff | 1:af9efbb9e9c5 | 128 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 129 | adt7420_dev *device; |
EndaKilgarriff | 1:af9efbb9e9c5 | 130 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 131 | registers_e registers; |
EndaKilgarriff | 1:af9efbb9e9c5 | 132 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 133 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 134 | int32_t connected = -1; |
EndaKilgarriff | 1:af9efbb9e9c5 | 135 | uint8_t device_id = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 136 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 137 | //Configure and instantiate the UART - PB10/11 |
EndaKilgarriff | 1:af9efbb9e9c5 | 138 | Serial pc(USBTX, USBRX, 115200); |
EndaKilgarriff | 1:af9efbb9e9c5 | 139 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 140 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 141 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 142 | int main() |
EndaKilgarriff | 1:af9efbb9e9c5 | 143 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 144 | uint8_t menu_select = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 145 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 146 | print_title(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 147 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 148 | device_id = select_device(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 149 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 150 | connected = adt7420_init(&device, init_params); |
EndaKilgarriff | 1:af9efbb9e9c5 | 151 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 152 | if (connected != SUCCESS) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 153 | pc.printf("\n\n Connection to device failed :(\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 154 | pc.printf(" ...Restarting application... \n\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 155 | wait(WAIT_MENU_TIME); |
EndaKilgarriff | 1:af9efbb9e9c5 | 156 | microcontroller_reset(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 157 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 158 | pc.printf("\n\n Connection to device succeeded!\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 159 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 160 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 161 | while (connected == SUCCESS) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 162 | menu_select = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 163 | print_active_device(device_id); |
EndaKilgarriff | 1:af9efbb9e9c5 | 164 | print_prompt(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 165 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 166 | if (get_menu_select(&menu_select) == FAILURE) |
EndaKilgarriff | 1:af9efbb9e9c5 | 167 | pc.printf("\n***** Returning to main menu *****\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 168 | else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 169 | switch (menu_select) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 170 | case 1: |
EndaKilgarriff | 1:af9efbb9e9c5 | 171 | read_temperature(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 172 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 173 | case 2: |
EndaKilgarriff | 1:af9efbb9e9c5 | 174 | set_resolution(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 175 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 176 | case 3: |
EndaKilgarriff | 1:af9efbb9e9c5 | 177 | set_op_mode(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 178 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 179 | case 4: |
EndaKilgarriff | 1:af9efbb9e9c5 | 180 | bunch_of_temps(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 181 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 182 | case 5: |
EndaKilgarriff | 1:af9efbb9e9c5 | 183 | readback_reg(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 184 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 185 | case 6: |
EndaKilgarriff | 1:af9efbb9e9c5 | 186 | reset_interface(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 187 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 188 | case 7: |
EndaKilgarriff | 1:af9efbb9e9c5 | 189 | write_to_setpoint_reg(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 190 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 191 | case 8: |
EndaKilgarriff | 1:af9efbb9e9c5 | 192 | set_fault_queue(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 193 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 194 | case 9: |
EndaKilgarriff | 1:af9efbb9e9c5 | 195 | set_ct_int_config(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 196 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 197 | case 10: |
EndaKilgarriff | 1:af9efbb9e9c5 | 198 | /*Restore device registers to default values before restarting microcontroller*/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 199 | reset_interface(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 200 | microcontroller_reset(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 201 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 202 | default: |
EndaKilgarriff | 1:af9efbb9e9c5 | 203 | pc.printf("Invalid option"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 204 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 205 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 206 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 207 | wait(WAIT_MENU_TIME); //wait 1 second |
EndaKilgarriff | 1:af9efbb9e9c5 | 208 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 209 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 210 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 211 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 212 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 213 | * @brief Prints the title block. |
EndaKilgarriff | 1:af9efbb9e9c5 | 214 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 215 | void print_title() |
EndaKilgarriff | 1:af9efbb9e9c5 | 216 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 217 | pc.printf("*****************************************************************\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 218 | pc.printf("* EVAL-TempeSense-ARDZ Demonstration Program -- (mbed) *\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 219 | pc.printf("* *\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 220 | pc.printf("* This program demonstrates communication with the ADT7xx *\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 221 | pc.printf("* High-Accuracy digital temperature sensor family *\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 222 | pc.printf("* It will also work with both SPI & I2C versions *\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 223 | pc.printf("* *\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 224 | pc.printf("* Set the baud rate to 115200 select the newline terminator. *\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 225 | pc.printf("*****************************************************************\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 226 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 227 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 228 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 229 | * @brief Prints the "main menu" prompt to the console. |
EndaKilgarriff | 1:af9efbb9e9c5 | 230 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 231 | void print_prompt() |
EndaKilgarriff | 1:af9efbb9e9c5 | 232 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 233 | pc.printf("\n\nCommand Summary:\n\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 234 | pc.printf(" 1 -Read temperature\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 235 | pc.printf(" 2 -Set resolution\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 236 | pc.printf(" 3 -Set operation mode\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 237 | pc.printf(" 4 -Poll temperature\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 238 | pc.printf(" 5 -Read a register\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 239 | pc.printf(" 6 -Reset the interface\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 240 | pc.printf(" 7 -Write to a setpoint register\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 241 | pc.printf(" 8 -Set Fault Queue configuration\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 242 | pc.printf(" 9 -Set CT/INT polarity and mode\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 243 | pc.printf(" 10 -Full System Reset\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 244 | pc.printf("\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 245 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 246 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 247 | static int get_menu_select(uint8_t *menu_select) |
EndaKilgarriff | 1:af9efbb9e9c5 | 248 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 249 | int invalid_check = pc.scanf("%d",(int *) menu_select); |
EndaKilgarriff | 1:af9efbb9e9c5 | 250 | return input_check(*menu_select, 1, 10, invalid_check); |
EndaKilgarriff | 1:af9efbb9e9c5 | 251 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 252 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 253 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 254 | * @brief - Select the serial interface (SPI/I2C) and device |
EndaKilgarriff | 1:af9efbb9e9c5 | 255 | * based on the part family. |
EndaKilgarriff | 1:af9efbb9e9c5 | 256 | * - Only one device and interface can be active. |
EndaKilgarriff | 1:af9efbb9e9c5 | 257 | * Example: ADT7320 - SPI (Internal or Remote device) |
EndaKilgarriff | 1:af9efbb9e9c5 | 258 | * ADT7420 - I2C (Internal or Remote device) |
EndaKilgarriff | 1:af9efbb9e9c5 | 259 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 260 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 261 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 262 | * @return new_dev - Return device selected |
EndaKilgarriff | 1:af9efbb9e9c5 | 263 | * Example: 1 - Internal(Main PCB) |
EndaKilgarriff | 1:af9efbb9e9c5 | 264 | * 2 - Remote (External PCB) |
EndaKilgarriff | 1:af9efbb9e9c5 | 265 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 266 | uint8_t select_device() |
EndaKilgarriff | 1:af9efbb9e9c5 | 267 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 268 | pc.printf("Please select interface by choosing a device:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 269 | pc.printf(" 1- ADT7320 (SPI)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 270 | pc.printf(" 2- ADT7420 (I2C)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 271 | pc.printf(" Select an option: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 272 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 273 | int invalid_check, new_interface = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 274 | invalid_check = pc.scanf("%d", &new_interface); |
EndaKilgarriff | 1:af9efbb9e9c5 | 275 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 276 | //Prompts for user input while correct interface is not selected |
EndaKilgarriff | 1:af9efbb9e9c5 | 277 | while (input_check(new_interface, 1, 2, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 278 | pc.printf("Please select interface by choosing a device:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 279 | pc.printf(" 1- ADT7320 (SPI)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 280 | pc.printf(" 2- ADT7420 (I2C)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 281 | pc.printf(" Select an option: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 282 | invalid_check = pc.scanf("%d", &new_interface); |
EndaKilgarriff | 1:af9efbb9e9c5 | 283 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 284 | pc.printf("%d", new_interface); |
EndaKilgarriff | 1:af9efbb9e9c5 | 285 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 286 | switch (new_interface) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 287 | case 1: |
EndaKilgarriff | 1:af9efbb9e9c5 | 288 | pc.printf(" ADT7320 sensor selected!\n\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 289 | init_params.act_device = ID_ADT7320; |
EndaKilgarriff | 1:af9efbb9e9c5 | 290 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 291 | case 2: |
EndaKilgarriff | 1:af9efbb9e9c5 | 292 | pc.printf(" ADT7420 sensor selected!\n\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 293 | init_params.act_device = ID_ADT7420; |
EndaKilgarriff | 1:af9efbb9e9c5 | 294 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 295 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 296 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 297 | pc.printf("Available devices:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 298 | pc.printf(" 1- Internal (Main PCB)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 299 | pc.printf(" 2- Remote (External PCB)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 300 | pc.printf(" Select an option: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 301 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 302 | int new_dev = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 303 | invalid_check = pc.scanf("%d", &new_dev); |
EndaKilgarriff | 1:af9efbb9e9c5 | 304 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 305 | //Prompts for user input while correct device is not selected |
EndaKilgarriff | 1:af9efbb9e9c5 | 306 | while (input_check(new_dev, 1, 2, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 307 | pc.printf("Device select:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 308 | pc.printf(" 1- Internal (Main PCB)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 309 | pc.printf(" 2- Remote (External PCB)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 310 | pc.printf(" Select an option: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 311 | invalid_check = pc.scanf("%d", &new_dev); |
EndaKilgarriff | 1:af9efbb9e9c5 | 312 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 313 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 314 | pc.printf("%d", new_dev); |
EndaKilgarriff | 1:af9efbb9e9c5 | 315 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 316 | switch (new_dev) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 317 | case 1: |
EndaKilgarriff | 1:af9efbb9e9c5 | 318 | pc.printf(" Internal sensor selected!"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 319 | init_params.mbed_i2c_init.slave_address = INTERNAL_I2C; |
EndaKilgarriff | 1:af9efbb9e9c5 | 320 | init_params.mbed_spi_init.chip_select = INTERNAL_SPI; |
EndaKilgarriff | 1:af9efbb9e9c5 | 321 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 322 | case 2: |
EndaKilgarriff | 1:af9efbb9e9c5 | 323 | pc.printf(" External sensor selected!"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 324 | init_params.mbed_i2c_init.slave_address = EXTERNAL_I2C; |
EndaKilgarriff | 1:af9efbb9e9c5 | 325 | init_params.mbed_spi_init.chip_select = EXTERNAL_SPI; |
EndaKilgarriff | 1:af9efbb9e9c5 | 326 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 327 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 328 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 329 | return new_dev; |
EndaKilgarriff | 1:af9efbb9e9c5 | 330 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 331 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 332 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 333 | * @brief - Reads and prints the temperature in Celsius Degree |
EndaKilgarriff | 1:af9efbb9e9c5 | 334 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 335 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 336 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 337 | * @return - Return SUCCESS flag - currently unused. |
EndaKilgarriff | 1:af9efbb9e9c5 | 338 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 339 | static uint8_t read_temperature() |
EndaKilgarriff | 1:af9efbb9e9c5 | 340 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 341 | float temp = adt7420_get_temperature(device); |
EndaKilgarriff | 1:af9efbb9e9c5 | 342 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 343 | pc.printf("Current temperature:%.3f C", temp); |
EndaKilgarriff | 1:af9efbb9e9c5 | 344 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 345 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 346 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 347 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 348 | * @brief -Set the device resolution for 13 or 16 bits |
EndaKilgarriff | 1:af9efbb9e9c5 | 349 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 350 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 351 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 352 | * @return - Return SUCCESS/FAILURE flags |
EndaKilgarriff | 1:af9efbb9e9c5 | 353 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 354 | static uint8_t set_resolution() |
EndaKilgarriff | 1:af9efbb9e9c5 | 355 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 356 | pc.printf(" Available resolutions:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 357 | pc.printf(" 1- 13-bit\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 358 | pc.printf(" 2- 16-bit\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 359 | pc.printf(" Select an option: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 360 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 361 | int new_res, invalid_check = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 362 | invalid_check = pc.scanf("%d", &new_res); |
EndaKilgarriff | 1:af9efbb9e9c5 | 363 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 364 | if (input_check(new_res, 1, 2, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 365 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 366 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 367 | pc.printf("%d\n", new_res); |
EndaKilgarriff | 1:af9efbb9e9c5 | 368 | new_res = (new_res == 1) ? 0 : 1; |
EndaKilgarriff | 1:af9efbb9e9c5 | 369 | adt7420_set_resolution(device, new_res); |
EndaKilgarriff | 1:af9efbb9e9c5 | 370 | pc.printf("Set resolution to %d-bit", (13 + 3 * new_res)); |
EndaKilgarriff | 1:af9efbb9e9c5 | 371 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 372 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 373 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 374 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 375 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 376 | * @brief - Set the device operation mode |
EndaKilgarriff | 1:af9efbb9e9c5 | 377 | * (Continuous conversion, One-shot, SPS, Shutdown). |
EndaKilgarriff | 1:af9efbb9e9c5 | 378 | * - Consult datasheet for more information. |
EndaKilgarriff | 1:af9efbb9e9c5 | 379 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 380 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 381 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 382 | * @return - Return SUCCESS/FAILURE flags |
EndaKilgarriff | 1:af9efbb9e9c5 | 383 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 384 | static uint8_t set_op_mode() |
EndaKilgarriff | 1:af9efbb9e9c5 | 385 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 386 | pc.printf(" Available operation modes:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 387 | pc.printf(" 1- Continuous conversion mode (default)\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 388 | pc.printf(" 2- One-shot mode\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 389 | pc.printf(" 3- 1 SPS mode\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 390 | pc.printf(" 4- Shutdown\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 391 | pc.printf(" Select a mode: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 392 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 393 | int new_mode, invalid_check = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 394 | invalid_check = pc.scanf("%d", &new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 395 | if (input_check(new_mode, 1, 4, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 396 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 397 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 398 | pc.printf("%d\n", new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 399 | switch (new_mode) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 400 | case 1: |
EndaKilgarriff | 1:af9efbb9e9c5 | 401 | adt7420_set_operation_mode(device, ADT7420_OP_MODE_CONT_CONV); |
EndaKilgarriff | 1:af9efbb9e9c5 | 402 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 403 | case 2: |
EndaKilgarriff | 1:af9efbb9e9c5 | 404 | /*When One shot mode is set completes one conversion and immediately goes to shutdown mode*/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 405 | adt7420_set_operation_mode(device, ADT7420_OP_MODE_ONE_SHOT); |
EndaKilgarriff | 1:af9efbb9e9c5 | 406 | pc.printf("\n One Shot mode enabled, device will enter shutdown mode once a conversion is complete.\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 407 | pc.printf(" See page 10 in datasheet for details.\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 408 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 409 | case 3: |
EndaKilgarriff | 1:af9efbb9e9c5 | 410 | adt7420_set_operation_mode(device, ADT7420_OP_MODE_1_SPS); |
EndaKilgarriff | 1:af9efbb9e9c5 | 411 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 412 | case 4: |
EndaKilgarriff | 1:af9efbb9e9c5 | 413 | adt7420_set_operation_mode(device, ADT7420_OP_MODE_SHUTDOWN); |
EndaKilgarriff | 1:af9efbb9e9c5 | 414 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 415 | default: |
EndaKilgarriff | 1:af9efbb9e9c5 | 416 | pc.printf("Invalid option\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 417 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 418 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 419 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 420 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 421 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 422 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 423 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 424 | * @brief - Prints poll of temperature based on the frequency of readings and |
EndaKilgarriff | 1:af9efbb9e9c5 | 425 | * number of samples. |
EndaKilgarriff | 1:af9efbb9e9c5 | 426 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 427 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 428 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 429 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 430 | * @return - Return SUCCESS/FAILURE flags |
EndaKilgarriff | 1:af9efbb9e9c5 | 431 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 432 | static uint8_t bunch_of_temps() |
EndaKilgarriff | 1:af9efbb9e9c5 | 433 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 434 | pc.printf(" Enter number of desired samples: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 435 | int num_samples, invalid_check = 1; |
EndaKilgarriff | 1:af9efbb9e9c5 | 436 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 437 | invalid_check = pc.scanf("%d", &num_samples); |
EndaKilgarriff | 1:af9efbb9e9c5 | 438 | if (input_check(num_samples, 1, 2000000, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 439 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 440 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 441 | pc.printf("%d\n", num_samples); |
EndaKilgarriff | 1:af9efbb9e9c5 | 442 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 443 | pc.printf(" Enter a desired frequency in samples/sec (max 10): "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 444 | int sample_freq = 1; |
EndaKilgarriff | 1:af9efbb9e9c5 | 445 | invalid_check = pc.scanf("%d", &sample_freq); |
EndaKilgarriff | 1:af9efbb9e9c5 | 446 | if (input_check(sample_freq, 1, 10, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 447 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 448 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 449 | sample_freq = constrain(sample_freq, 1, 10); |
EndaKilgarriff | 1:af9efbb9e9c5 | 450 | pc.printf("%d", sample_freq); |
EndaKilgarriff | 1:af9efbb9e9c5 | 451 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 452 | uint32_t delay_sec = 1000000 / sample_freq; |
EndaKilgarriff | 1:af9efbb9e9c5 | 453 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 454 | pc.printf(" Gathering %d seconds of samples\n", num_samples/sample_freq); |
EndaKilgarriff | 1:af9efbb9e9c5 | 455 | pc.printf("Press enter to continue and then press again to quit\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 456 | getchar(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 457 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 458 | for (int i = 0; i < num_samples; i++) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 459 | if(pc.readable()) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 460 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 461 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 462 | pc.printf(" Sample:%d: Temperature:", i + 1); |
EndaKilgarriff | 1:af9efbb9e9c5 | 463 | float temp = adt7420_get_temperature(device); |
EndaKilgarriff | 1:af9efbb9e9c5 | 464 | pc.printf("%.4f\n", temp); |
EndaKilgarriff | 1:af9efbb9e9c5 | 465 | wait_us(delay_sec); |
EndaKilgarriff | 1:af9efbb9e9c5 | 466 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 467 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 468 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 469 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 470 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 471 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 472 | * @brief - Reads back data store in device registers |
EndaKilgarriff | 1:af9efbb9e9c5 | 473 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 474 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 475 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 476 | * @return - Return SUCCESS/FAILURE flags |
EndaKilgarriff | 1:af9efbb9e9c5 | 477 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 478 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 479 | static uint8_t readback_reg() |
EndaKilgarriff | 1:af9efbb9e9c5 | 480 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 481 | pc.printf(" Available registers:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 482 | pc.printf(" 1- Status\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 483 | pc.printf(" 2- Configuration\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 484 | pc.printf(" 3- Temperature\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 485 | pc.printf(" 4- ID\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 486 | pc.printf(" 5- Critical Temperature setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 487 | pc.printf(" 6- Hysteresis Temperature setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 488 | pc.printf(" 7- Temperature high setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 489 | pc.printf(" 8- Temperature low setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 490 | pc.printf(" Select a mode: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 491 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 492 | uint16_t read_value = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 493 | int new_mode, invalid_check = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 494 | invalid_check = pc.scanf("%d", &new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 495 | if (input_check(new_mode, 1, 8, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 496 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 497 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 498 | pc.printf("%d\n", new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 499 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 500 | switch (new_mode) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 501 | case 1: |
EndaKilgarriff | 1:af9efbb9e9c5 | 502 | read_value = get_register_value(device, REG_STATUS); |
EndaKilgarriff | 1:af9efbb9e9c5 | 503 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 504 | case 2: |
EndaKilgarriff | 1:af9efbb9e9c5 | 505 | read_value = get_register_value(device, REG_CONFIG); |
EndaKilgarriff | 1:af9efbb9e9c5 | 506 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 507 | case 3: |
EndaKilgarriff | 1:af9efbb9e9c5 | 508 | read_value = get_register_value(device, REG_TEMP); |
EndaKilgarriff | 1:af9efbb9e9c5 | 509 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 510 | case 4: |
EndaKilgarriff | 1:af9efbb9e9c5 | 511 | read_value = get_register_value(device, REG_ID); |
EndaKilgarriff | 1:af9efbb9e9c5 | 512 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 513 | case 5: |
EndaKilgarriff | 1:af9efbb9e9c5 | 514 | read_value = get_register_value(device, REG_T_CRIT); |
EndaKilgarriff | 1:af9efbb9e9c5 | 515 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 516 | case 6: |
EndaKilgarriff | 1:af9efbb9e9c5 | 517 | read_value = get_register_value(device, REG_HIST); |
EndaKilgarriff | 1:af9efbb9e9c5 | 518 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 519 | case 7: |
EndaKilgarriff | 1:af9efbb9e9c5 | 520 | read_value = get_register_value(device, REG_T_HIGH); |
EndaKilgarriff | 1:af9efbb9e9c5 | 521 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 522 | case 8: |
EndaKilgarriff | 1:af9efbb9e9c5 | 523 | read_value = get_register_value(device, REG_T_LOW); |
EndaKilgarriff | 1:af9efbb9e9c5 | 524 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 525 | default: |
EndaKilgarriff | 1:af9efbb9e9c5 | 526 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 527 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 528 | pc.printf("Read value: 0x%x\n", read_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 529 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 530 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 531 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 532 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 533 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 534 | * @brief - Resets device interface (SPI/I2C) (power-on reset) |
EndaKilgarriff | 1:af9efbb9e9c5 | 535 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 536 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 537 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 538 | * @return - Return SUCCESS flag - currently unused. |
EndaKilgarriff | 1:af9efbb9e9c5 | 539 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 540 | static uint8_t reset_interface() |
EndaKilgarriff | 1:af9efbb9e9c5 | 541 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 542 | pc.printf(" Resetting interface...\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 543 | adt7420_reset_interface(device); |
EndaKilgarriff | 1:af9efbb9e9c5 | 544 | wait_us(RESET_DELAY); |
EndaKilgarriff | 1:af9efbb9e9c5 | 545 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 546 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 547 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 548 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 549 | * @brief - Write to setpoint registers THIGH, TLOW, TCRIT and THYST. |
EndaKilgarriff | 1:af9efbb9e9c5 | 550 | - Values entered in Celsius and rounded to a near integer value. |
EndaKilgarriff | 1:af9efbb9e9c5 | 551 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 552 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 553 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 554 | * @return - Return SUCCESS/FAILURE flags |
EndaKilgarriff | 1:af9efbb9e9c5 | 555 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 556 | static uint8_t write_to_setpoint_reg() |
EndaKilgarriff | 1:af9efbb9e9c5 | 557 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 558 | pc.printf(" Available registers:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 559 | pc.printf(" 1- Critical setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 560 | pc.printf(" 2- Hystersis setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 561 | pc.printf(" 3- Temperature high setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 562 | pc.printf(" 4- Temperature low setpoint\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 563 | pc.printf(" Select a mode: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 564 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 565 | int new_mode, invalid_check = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 566 | invalid_check = pc.scanf("%d", &new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 567 | if (input_check(new_mode, 1, 4, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 568 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 569 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 570 | pc.printf("%d\n", new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 571 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 572 | float temp_c; |
EndaKilgarriff | 1:af9efbb9e9c5 | 573 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 574 | if(new_mode == 2) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 575 | pc.printf("Enter value to write (0 to 15) Celsius:"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 576 | invalid_check = pc.scanf("%f", &temp_c); |
EndaKilgarriff | 1:af9efbb9e9c5 | 577 | if(input_check(temp_c, MIN_HYST_TEMP, MAX_HYST_TEMP, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 578 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 579 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 580 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 581 | pc.printf("Enter value to write (in Celsius):"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 582 | invalid_check = pc.scanf("%f", &temp_c); |
EndaKilgarriff | 1:af9efbb9e9c5 | 583 | if(input_check(temp_c, TEMP_MIN, TEMP_MAX, invalid_check) == FAILURE){ |
EndaKilgarriff | 1:af9efbb9e9c5 | 584 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 585 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 586 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 587 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 588 | pc.printf(" %.2f", temp_c); |
EndaKilgarriff | 1:af9efbb9e9c5 | 589 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 590 | int16_t write_value; |
EndaKilgarriff | 1:af9efbb9e9c5 | 591 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 592 | if(new_mode == 2) |
EndaKilgarriff | 1:af9efbb9e9c5 | 593 | write_value = round(temp_c); |
EndaKilgarriff | 1:af9efbb9e9c5 | 594 | else |
EndaKilgarriff | 1:af9efbb9e9c5 | 595 | write_value = round(128 * temp_c); |
EndaKilgarriff | 1:af9efbb9e9c5 | 596 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 597 | switch (new_mode) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 598 | case 1: |
EndaKilgarriff | 1:af9efbb9e9c5 | 599 | if (adt7420_wr_setpoint_reg(device, REG_T_CRIT, write_value) == SUCCESS) |
EndaKilgarriff | 1:af9efbb9e9c5 | 600 | pc.printf("\n0x%x successfuly written\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 601 | else |
EndaKilgarriff | 1:af9efbb9e9c5 | 602 | pc.printf("\n0x%x NOT successfuly written\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 603 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 604 | case 2: |
EndaKilgarriff | 1:af9efbb9e9c5 | 605 | if (adt7420_wr_setpoint_reg(device, REG_HIST, write_value) == SUCCESS) |
EndaKilgarriff | 1:af9efbb9e9c5 | 606 | pc.printf("\n0x%x successfuly written (bits 7:4 are fixed at 0)\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 607 | else |
EndaKilgarriff | 1:af9efbb9e9c5 | 608 | pc.printf("\n0x%x NOT successfuly written\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 609 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 610 | case 3: |
EndaKilgarriff | 1:af9efbb9e9c5 | 611 | if (adt7420_wr_setpoint_reg(device, REG_T_HIGH, write_value) == SUCCESS) |
EndaKilgarriff | 1:af9efbb9e9c5 | 612 | pc.printf("\n0x%x successfuly written\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 613 | else |
EndaKilgarriff | 1:af9efbb9e9c5 | 614 | pc.printf("\n0x%x NOT successfuly written\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 615 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 616 | case 4: |
EndaKilgarriff | 1:af9efbb9e9c5 | 617 | if (adt7420_wr_setpoint_reg(device, REG_T_LOW, write_value) == SUCCESS) |
EndaKilgarriff | 1:af9efbb9e9c5 | 618 | pc.printf("\n0x%x successfuly written\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 619 | else |
EndaKilgarriff | 1:af9efbb9e9c5 | 620 | pc.printf("\n0x%x NOT successfuly written\n", write_value); |
EndaKilgarriff | 1:af9efbb9e9c5 | 621 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 622 | default: |
EndaKilgarriff | 1:af9efbb9e9c5 | 623 | pc.printf("Invalid selection - try again.\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 624 | wait(2); |
EndaKilgarriff | 1:af9efbb9e9c5 | 625 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 626 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 627 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 628 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 629 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 630 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 631 | * @brief - Set the number of undertemperature/overtemperature faults |
EndaKilgarriff | 1:af9efbb9e9c5 | 632 | * that can occur before setting the INT and CT output pins |
EndaKilgarriff | 1:af9efbb9e9c5 | 633 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 634 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 635 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 636 | * @return - Return SUCCESS/FAILURE flags |
EndaKilgarriff | 1:af9efbb9e9c5 | 637 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 638 | static uint8_t set_fault_queue() |
EndaKilgarriff | 1:af9efbb9e9c5 | 639 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 640 | pc.printf(" Available fault queue options:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 641 | pc.printf(" 1- 1 fault (default) \n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 642 | pc.printf(" 2- 2 faults\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 643 | pc.printf(" 3- 3 faults\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 644 | pc.printf(" 4- 4 faults\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 645 | pc.printf(" Select a mode: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 646 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 647 | int new_fault, invalid_check = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 648 | invalid_check = pc.scanf("%d", &new_fault); |
EndaKilgarriff | 1:af9efbb9e9c5 | 649 | if (input_check(new_fault, 1, 4, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 650 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 651 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 652 | pc.printf("%d\n", new_fault); |
EndaKilgarriff | 1:af9efbb9e9c5 | 653 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 654 | switch (new_fault) |
EndaKilgarriff | 1:af9efbb9e9c5 | 655 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 656 | case 1: |
EndaKilgarriff | 1:af9efbb9e9c5 | 657 | adt7420_set_fault_queue(device, ADT7420_FAULT_QUEUE_1_FAULT); |
EndaKilgarriff | 1:af9efbb9e9c5 | 658 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 659 | case 2: |
EndaKilgarriff | 1:af9efbb9e9c5 | 660 | adt7420_set_fault_queue(device, ADT7420_FAULT_QUEUE_2_FAULTS); |
EndaKilgarriff | 1:af9efbb9e9c5 | 661 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 662 | case 3: |
EndaKilgarriff | 1:af9efbb9e9c5 | 663 | adt7420_set_fault_queue(device, ADT7420_FAULT_QUEUE_3_FAULTS); |
EndaKilgarriff | 1:af9efbb9e9c5 | 664 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 665 | case 4: |
EndaKilgarriff | 1:af9efbb9e9c5 | 666 | adt7420_set_fault_queue(device, ADT7420_FAULT_QUEUE_4_FAULTS); |
EndaKilgarriff | 1:af9efbb9e9c5 | 667 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 668 | default: |
EndaKilgarriff | 1:af9efbb9e9c5 | 669 | pc.printf("Invalid option\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 670 | break; |
EndaKilgarriff | 1:af9efbb9e9c5 | 671 | } return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 672 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 673 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 674 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 675 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 676 | * @brief - Set INT/CT Outputs pins to Comparator or Interrupt mode |
EndaKilgarriff | 1:af9efbb9e9c5 | 677 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 678 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 679 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 680 | * @return - Return SUCCESS/FAILURE flags |
EndaKilgarriff | 1:af9efbb9e9c5 | 681 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 682 | static uint8_t set_ct_int_config() |
EndaKilgarriff | 1:af9efbb9e9c5 | 683 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 684 | int new_polarity = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 685 | int new_mode, invalid_check = 0; |
EndaKilgarriff | 1:af9efbb9e9c5 | 686 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 687 | pc.printf(" Choose INT/CT mode:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 688 | pc.printf(" 1- Interrupt (default) \n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 689 | pc.printf(" 2- Comparator \n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 690 | pc.printf(" Select a mode: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 691 | invalid_check = pc.scanf("%d", &new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 692 | if (input_check(new_mode, 1, 2, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 693 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 694 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 695 | pc.printf("%d\n", new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 696 | new_mode = (new_mode == 1) ? 0 : 1; |
EndaKilgarriff | 1:af9efbb9e9c5 | 697 | adt7420_set_ct_int_mode(device, new_mode); |
EndaKilgarriff | 1:af9efbb9e9c5 | 698 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 699 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 700 | pc.printf("\n Set output polarity for Critical and Over/Under Temperature pin:\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 701 | pc.printf(" (Feature available only for internal sensors).\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 702 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 703 | if(init_params.mbed_i2c_init.slave_address == INTERNAL_I2C || |
EndaKilgarriff | 1:af9efbb9e9c5 | 704 | init_params.mbed_spi_init.chip_select == INTERNAL_SPI) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 705 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 706 | pc.printf(" 1- Active Low (default) \n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 707 | pc.printf(" 2- Active High\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 708 | pc.printf(" Select a mode: "); |
EndaKilgarriff | 1:af9efbb9e9c5 | 709 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 710 | invalid_check = pc.scanf("%d", &new_polarity); |
EndaKilgarriff | 1:af9efbb9e9c5 | 711 | if (input_check(new_polarity, 1, 2, invalid_check) == FAILURE) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 712 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 713 | } else { |
EndaKilgarriff | 1:af9efbb9e9c5 | 714 | pc.printf("%d\n", new_polarity); |
EndaKilgarriff | 1:af9efbb9e9c5 | 715 | new_polarity = (new_polarity == 1) ? 0 : 1; |
EndaKilgarriff | 1:af9efbb9e9c5 | 716 | adt7420_set_ct_int_polarity(device, new_polarity); |
EndaKilgarriff | 1:af9efbb9e9c5 | 717 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 718 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 719 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 720 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 721 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 722 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 723 | * @brief Reset microcontroller |
EndaKilgarriff | 1:af9efbb9e9c5 | 724 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 725 | * @param None |
EndaKilgarriff | 1:af9efbb9e9c5 | 726 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 727 | * @return None. |
EndaKilgarriff | 1:af9efbb9e9c5 | 728 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 729 | static void microcontroller_reset() |
EndaKilgarriff | 1:af9efbb9e9c5 | 730 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 731 | NVIC_SystemReset(); |
EndaKilgarriff | 1:af9efbb9e9c5 | 732 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 733 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 734 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 735 | * @brief Prints the active device every time the main menu is redrawn |
EndaKilgarriff | 1:af9efbb9e9c5 | 736 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 737 | * @param external_internal_selection - External or Internal Chip Selected |
EndaKilgarriff | 1:af9efbb9e9c5 | 738 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 739 | * @return None. |
EndaKilgarriff | 1:af9efbb9e9c5 | 740 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 741 | static void print_active_device(int external_internal_selection) |
EndaKilgarriff | 1:af9efbb9e9c5 | 742 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 743 | const char* devices[7] = { "\n\n Active Device: ADT7410 I2C" , |
EndaKilgarriff | 1:af9efbb9e9c5 | 744 | "\n\n Active Device: ADT7420 I2C", |
EndaKilgarriff | 1:af9efbb9e9c5 | 745 | "\n\n Active Device: ADT7422 I2C", |
EndaKilgarriff | 1:af9efbb9e9c5 | 746 | "\n\n Active Device: ADT7310 SPI", |
EndaKilgarriff | 1:af9efbb9e9c5 | 747 | "\n\n Active Device: ADT7311 SPI", |
EndaKilgarriff | 1:af9efbb9e9c5 | 748 | "\n\n Active Device: ADT7312 SPI", |
EndaKilgarriff | 1:af9efbb9e9c5 | 749 | "\n\n Active Device: ADT7320 SPI"}; |
EndaKilgarriff | 1:af9efbb9e9c5 | 750 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 751 | const char* external_internal_print[] = {" - Internal Chip \n", |
EndaKilgarriff | 1:af9efbb9e9c5 | 752 | " - External Chip \n" }; |
EndaKilgarriff | 1:af9efbb9e9c5 | 753 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 754 | pc.printf("%s %s", devices[init_params.act_device], external_internal_print[external_internal_selection - 1]); |
EndaKilgarriff | 1:af9efbb9e9c5 | 755 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 756 | |
EndaKilgarriff | 1:af9efbb9e9c5 | 757 | /***************************************************************************//** |
EndaKilgarriff | 1:af9efbb9e9c5 | 758 | * @brief Checks is an input is a digit and within valid range |
EndaKilgarriff | 1:af9efbb9e9c5 | 759 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 760 | * @param input_val - Value inputed by user |
EndaKilgarriff | 1:af9efbb9e9c5 | 761 | * @param lowest_accepted_val - Lowest acceptable value |
EndaKilgarriff | 1:af9efbb9e9c5 | 762 | * @param highest_accepted_val - Highest acceptable value |
EndaKilgarriff | 1:af9efbb9e9c5 | 763 | * @param invalid_check - Checks if unexpected type of data was entered in scanf |
EndaKilgarriff | 1:af9efbb9e9c5 | 764 | * |
EndaKilgarriff | 1:af9efbb9e9c5 | 765 | * @return SUCCESS/FAILURE Flags. |
EndaKilgarriff | 1:af9efbb9e9c5 | 766 | *******************************************************************************/ |
EndaKilgarriff | 1:af9efbb9e9c5 | 767 | static int input_check(int input_val, |
EndaKilgarriff | 1:af9efbb9e9c5 | 768 | int lowest_accepted_val, |
EndaKilgarriff | 1:af9efbb9e9c5 | 769 | int highest_accepted_val, |
EndaKilgarriff | 1:af9efbb9e9c5 | 770 | int invalid_check) |
EndaKilgarriff | 1:af9efbb9e9c5 | 771 | { |
EndaKilgarriff | 1:af9efbb9e9c5 | 772 | if(invalid_check == 0 || input_val < lowest_accepted_val || input_val > highest_accepted_val) { |
EndaKilgarriff | 1:af9efbb9e9c5 | 773 | pc.printf("\n\n***** Invalid entry: No changes made *****\n"); |
EndaKilgarriff | 1:af9efbb9e9c5 | 774 | wait(WAIT_MENU_TIME); |
EndaKilgarriff | 1:af9efbb9e9c5 | 775 | return FAILURE; |
EndaKilgarriff | 1:af9efbb9e9c5 | 776 | } |
EndaKilgarriff | 1:af9efbb9e9c5 | 777 | return SUCCESS; |
EndaKilgarriff | 1:af9efbb9e9c5 | 778 | } |