A collection of Analog Devices drivers for the mbed platform

For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all

Committer:
Adrian Suciu
Date:
Tue May 17 14:21:17 2016 +0300
Revision:
19:fb92949e59c9
Child:
20:9790e53d6e26
Added ADXL362 driver & reference project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Adrian Suciu 19:fb92949e59c9 1 /**
Adrian Suciu 19:fb92949e59c9 2 * @file main.cpp
Adrian Suciu 19:fb92949e59c9 3 * @brief Main file for the ADXL362-example project
Adrian Suciu 19:fb92949e59c9 4 * @author Analog Devices Inc.
Adrian Suciu 19:fb92949e59c9 5 *
Adrian Suciu 19:fb92949e59c9 6 * For support please go to:
Adrian Suciu 19:fb92949e59c9 7 * Github: https://github.com/analogdevicesinc/mbed-adi
Adrian Suciu 19:fb92949e59c9 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
Adrian Suciu 19:fb92949e59c9 9 * Product: www.analog.com/EVAL-ADXL362-ARDZ
Adrian Suciu 19:fb92949e59c9 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
Adrian Suciu 19:fb92949e59c9 11
Adrian Suciu 19:fb92949e59c9 12 ********************************************************************************
Adrian Suciu 19:fb92949e59c9 13 * Copyright 2016(c) Analog Devices, Inc.
Adrian Suciu 19:fb92949e59c9 14 *
Adrian Suciu 19:fb92949e59c9 15 * All rights reserved.
Adrian Suciu 19:fb92949e59c9 16 *
Adrian Suciu 19:fb92949e59c9 17 * Redistribution and use in source and binary forms, with or without
Adrian Suciu 19:fb92949e59c9 18 * modification, are permitted provided that the following conditions are met:
Adrian Suciu 19:fb92949e59c9 19 * - Redistributions of source code must retain the above copyright
Adrian Suciu 19:fb92949e59c9 20 * notice, this list of conditions and the following disclaimer.
Adrian Suciu 19:fb92949e59c9 21 * - Redistributions in binary form must reproduce the above copyright
Adrian Suciu 19:fb92949e59c9 22 * notice, this list of conditions and the following disclaimer in
Adrian Suciu 19:fb92949e59c9 23 * the documentation and/or other materials provided with the
Adrian Suciu 19:fb92949e59c9 24 * distribution.
Adrian Suciu 19:fb92949e59c9 25 * - Neither the name of Analog Devices, Inc. nor the names of its
Adrian Suciu 19:fb92949e59c9 26 * contributors may be used to endorse or promote products derived
Adrian Suciu 19:fb92949e59c9 27 * from this software without specific prior written permission.
Adrian Suciu 19:fb92949e59c9 28 * - The use of this software may or may not infringe the patent rights
Adrian Suciu 19:fb92949e59c9 29 * of one or more patent holders. This license does not release you
Adrian Suciu 19:fb92949e59c9 30 * from the requirement that you obtain separate licenses from these
Adrian Suciu 19:fb92949e59c9 31 * patent holders to use this software.
Adrian Suciu 19:fb92949e59c9 32 * - Use of the software either in source or binary form, must be run
Adrian Suciu 19:fb92949e59c9 33 * on or directly connected to an Analog Devices Inc. component.
Adrian Suciu 19:fb92949e59c9 34 *
Adrian Suciu 19:fb92949e59c9 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
Adrian Suciu 19:fb92949e59c9 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
Adrian Suciu 19:fb92949e59c9 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Adrian Suciu 19:fb92949e59c9 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
Adrian Suciu 19:fb92949e59c9 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Adrian Suciu 19:fb92949e59c9 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
Adrian Suciu 19:fb92949e59c9 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Adrian Suciu 19:fb92949e59c9 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Adrian Suciu 19:fb92949e59c9 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Adrian Suciu 19:fb92949e59c9 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Adrian Suciu 19:fb92949e59c9 45 *
Adrian Suciu 19:fb92949e59c9 46 ********************************************************************************/
Adrian Suciu 19:fb92949e59c9 47 #include "mbed.h"
Adrian Suciu 19:fb92949e59c9 48 #include "ADXL362.h"
Adrian Suciu 19:fb92949e59c9 49 #include "lcd.h"
Adrian Suciu 19:fb92949e59c9 50
Adrian Suciu 19:fb92949e59c9 51 Serial pc(USBTX, USBRX); ///< Serial interface to the pc
Adrian Suciu 19:fb92949e59c9 52 Lcd lcd;
Adrian Suciu 19:fb92949e59c9 53 ADXL362 adxl362(D9);
Adrian Suciu 19:fb92949e59c9 54
Adrian Suciu 19:fb92949e59c9 55 static const uint16_t INACT_VAL = 50;
Adrian Suciu 19:fb92949e59c9 56 static const uint16_t INACT_TIMER = 10 * 100;
Adrian Suciu 19:fb92949e59c9 57 static const uint16_t ACT_VAL = 50;
Adrian Suciu 19:fb92949e59c9 58 static const uint8_t ACT_TIMER = 100;
Adrian Suciu 19:fb92949e59c9 59 static const uint16_t SCAN_SENSOR_TIME = 500;
Adrian Suciu 19:fb92949e59c9 60
Adrian Suciu 19:fb92949e59c9 61 void flush_serial_buffer(void)
Adrian Suciu 19:fb92949e59c9 62 {
Adrian Suciu 19:fb92949e59c9 63 while (pc.readable())
Adrian Suciu 19:fb92949e59c9 64 pc.getc();
Adrian Suciu 19:fb92949e59c9 65 return;
Adrian Suciu 19:fb92949e59c9 66 }
Adrian Suciu 19:fb92949e59c9 67
Adrian Suciu 19:fb92949e59c9 68 int main()
Adrian Suciu 19:fb92949e59c9 69 {
Adrian Suciu 19:fb92949e59c9 70 uint8_t ui8s[22];
Adrian Suciu 19:fb92949e59c9 71 uint8_t ui8xu = 0;
Adrian Suciu 19:fb92949e59c9 72 uint8_t ui8xd = 0;
Adrian Suciu 19:fb92949e59c9 73 uint8_t ui8yu = 0;
Adrian Suciu 19:fb92949e59c9 74 uint8_t ui8yd = 0;
Adrian Suciu 19:fb92949e59c9 75 uint8_t ui8all = 0;
Adrian Suciu 19:fb92949e59c9 76
Adrian Suciu 19:fb92949e59c9 77 lcd.init();
Adrian Suciu 19:fb92949e59c9 78 adxl362.reset();
Adrian Suciu 19:fb92949e59c9 79 wait_us(500);
Adrian Suciu 19:fb92949e59c9 80 adxl362.set_activity_threshold(ACT_VAL);
Adrian Suciu 19:fb92949e59c9 81 adxl362.set_activity_time(ACT_TIMER / 10);
Adrian Suciu 19:fb92949e59c9 82
Adrian Suciu 19:fb92949e59c9 83 adxl362.set_inactivity_threshold(INACT_VAL);
Adrian Suciu 19:fb92949e59c9 84 adxl362.set_inactivity_time(INACT_TIMER);
Adrian Suciu 19:fb92949e59c9 85 adxl362.set_act_inact_ctl_reg(0x3f);
Adrian Suciu 19:fb92949e59c9 86 #if(ADXL_INT_SEL == INTACC_PIN_1)
Adrian Suciu 19:fb92949e59c9 87 adxl362.set_polling_interrupt1_pin(D2, 0x40);
Adrian Suciu 19:fb92949e59c9 88 #elif(ADXL_INT_SEL == INTACC_PIN_2)
Adrian Suciu 19:fb92949e59c9 89 adxl362.set_polling_interrupt2_pin(D2, 0x40); /* Map the awake status to INT2 pin */
Adrian Suciu 19:fb92949e59c9 90 #endif
Adrian Suciu 19:fb92949e59c9 91 // interrupt ?
Adrian Suciu 19:fb92949e59c9 92
Adrian Suciu 19:fb92949e59c9 93 adxl362.set_mode(ADXL362::MEASUREMENT);
Adrian Suciu 19:fb92949e59c9 94
Adrian Suciu 19:fb92949e59c9 95 uint8_t ui8awake = 0;
Adrian Suciu 19:fb92949e59c9 96
Adrian Suciu 19:fb92949e59c9 97 /* Infinite loop */
Adrian Suciu 19:fb92949e59c9 98 while (1) {
Adrian Suciu 19:fb92949e59c9 99 #if(ADXL_INT_SEL == INTACC_PIN_1)
Adrian Suciu 19:fb92949e59c9 100 if (adxl362.get_int1()) {
Adrian Suciu 19:fb92949e59c9 101 #elif(ADXL_INT_SEL == INTACC_PIN_2)
Adrian Suciu 19:fb92949e59c9 102 if(adxl362.get_int2()) {
Adrian Suciu 19:fb92949e59c9 103 #endif
Adrian Suciu 19:fb92949e59c9 104 if (ui8awake == 0) {
Adrian Suciu 19:fb92949e59c9 105 ui8awake = 1;
Adrian Suciu 19:fb92949e59c9 106
Adrian Suciu 19:fb92949e59c9 107 /* Set BLLCD pin - turn on LCD backlight */
Adrian Suciu 19:fb92949e59c9 108 lcd.bl_enable();
Adrian Suciu 19:fb92949e59c9 109
Adrian Suciu 19:fb92949e59c9 110 lcd.display_string(0, 60, (int8_t *) "[mG]");
Adrian Suciu 19:fb92949e59c9 111 lcd.display_string(1, 60, (int8_t *) "[mG]");
Adrian Suciu 19:fb92949e59c9 112 lcd.display_string(2, 60, (int8_t *) "[mG]");
Adrian Suciu 19:fb92949e59c9 113
Adrian Suciu 19:fb92949e59c9 114 #if TEMP_ADC == 1
Adrian Suciu 19:fb92949e59c9 115 lcd.display_string(3, 60, (int8_t *)"[ADC]");
Adrian Suciu 19:fb92949e59c9 116 #else
Adrian Suciu 19:fb92949e59c9 117 lcd.display_string(3, 60, (int8_t *) "[C]");
Adrian Suciu 19:fb92949e59c9 118 #endif
Adrian Suciu 19:fb92949e59c9 119 ui8xu = 0;
Adrian Suciu 19:fb92949e59c9 120 ui8xd = 0;
Adrian Suciu 19:fb92949e59c9 121 ui8yu = 0;
Adrian Suciu 19:fb92949e59c9 122 ui8yd = 0;
Adrian Suciu 19:fb92949e59c9 123 ui8all = 0;
Adrian Suciu 19:fb92949e59c9 124
Adrian Suciu 19:fb92949e59c9 125 lcd.display_symbol(0, Lcd::UP_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 126 lcd.display_symbol(1, Lcd::LEFT_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 127 lcd.display_symbol(1, Lcd::RIGHT_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 128 lcd.display_symbol(2, Lcd::DOWN_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 129 lcd.display_symbol(1, Lcd::CENTER_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 130 }
Adrian Suciu 19:fb92949e59c9 131
Adrian Suciu 19:fb92949e59c9 132 } else {
Adrian Suciu 19:fb92949e59c9 133 if (ui8awake == 1) {
Adrian Suciu 19:fb92949e59c9 134 ui8awake = 0;
Adrian Suciu 19:fb92949e59c9 135
Adrian Suciu 19:fb92949e59c9 136 /* Clear BLLCD pin - turn off LCD backlight */
Adrian Suciu 19:fb92949e59c9 137 lcd.bl_disable();
Adrian Suciu 19:fb92949e59c9 138
Adrian Suciu 19:fb92949e59c9 139 /* Clear screen */
Adrian Suciu 19:fb92949e59c9 140 lcd.fill_pages(0, 4, 0x00);
Adrian Suciu 19:fb92949e59c9 141 }
Adrian Suciu 19:fb92949e59c9 142 }
Adrian Suciu 19:fb92949e59c9 143
Adrian Suciu 19:fb92949e59c9 144 if (ui8awake == 1) {
Adrian Suciu 19:fb92949e59c9 145 wait_ms(100);
Adrian Suciu 19:fb92949e59c9 146 uint64_t acc = adxl362.scan();
Adrian Suciu 19:fb92949e59c9 147 int16_t i16SensorX = static_cast<uint16_t>((acc & 0xffff000000000000) >> 48);
Adrian Suciu 19:fb92949e59c9 148 int16_t i16SensorY = static_cast<uint16_t>((acc & 0x0000ffff00000000) >> 32);
Adrian Suciu 19:fb92949e59c9 149 int16_t i16SensorZ = static_cast<uint16_t>((acc & 0x00000000ffff0000) >> 16);
Adrian Suciu 19:fb92949e59c9 150 int16_t i16SensorT = static_cast<uint16_t> (acc & 0x000000000000ffff);
Adrian Suciu 19:fb92949e59c9 151
Adrian Suciu 19:fb92949e59c9 152 sprintf((char *) ui8s, "x = % 5d", i16SensorX);
Adrian Suciu 19:fb92949e59c9 153 lcd.display_string(0, 0, (int8_t *) ui8s);
Adrian Suciu 19:fb92949e59c9 154
Adrian Suciu 19:fb92949e59c9 155 sprintf((char *) ui8s, "y = % 5d", i16SensorY);
Adrian Suciu 19:fb92949e59c9 156 lcd.display_string(1, 0, (int8_t *) ui8s);
Adrian Suciu 19:fb92949e59c9 157
Adrian Suciu 19:fb92949e59c9 158 sprintf((char *) ui8s, "z = % 5d", i16SensorZ);
Adrian Suciu 19:fb92949e59c9 159 lcd.display_string(2, 0, (int8_t *) ui8s);
Adrian Suciu 19:fb92949e59c9 160
Adrian Suciu 19:fb92949e59c9 161 #if TEMP_ADC == 1
Adrian Suciu 19:fb92949e59c9 162 sprintf((char *)ui8s, "t = % 5d", i16SensorT);
Adrian Suciu 19:fb92949e59c9 163 lcd.display_string(3, 0, (int8_t *)ui8s);
Adrian Suciu 19:fb92949e59c9 164 #else
Adrian Suciu 19:fb92949e59c9 165 float f32temp = ((float) i16SensorT + Lcd::ACC_TEMP_BIAS)
Adrian Suciu 19:fb92949e59c9 166 / (1 / Lcd::ACC_TEMP_SENSITIVITY); // -34.625
Adrian Suciu 19:fb92949e59c9 167 sprintf((char *) ui8s, "t = % 4.1f", f32temp);
Adrian Suciu 19:fb92949e59c9 168 lcd.display_string(3, 0, (int8_t *) ui8s);
Adrian Suciu 19:fb92949e59c9 169 #endif
Adrian Suciu 19:fb92949e59c9 170
Adrian Suciu 19:fb92949e59c9 171 if (i16SensorY > Lcd::ACC_LIMIT) {
Adrian Suciu 19:fb92949e59c9 172 if (ui8xu == 0) {
Adrian Suciu 19:fb92949e59c9 173 ui8xu = 1;
Adrian Suciu 19:fb92949e59c9 174 lcd.display_symbol(0, Lcd::UP_X, 8, Lcd::pui8Rec8x8);
Adrian Suciu 19:fb92949e59c9 175 }
Adrian Suciu 19:fb92949e59c9 176
Adrian Suciu 19:fb92949e59c9 177 } else {
Adrian Suciu 19:fb92949e59c9 178 if (ui8xu == 1) {
Adrian Suciu 19:fb92949e59c9 179 ui8xu = 0;
Adrian Suciu 19:fb92949e59c9 180 lcd.display_symbol(0, Lcd::UP_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 181 }
Adrian Suciu 19:fb92949e59c9 182 }
Adrian Suciu 19:fb92949e59c9 183
Adrian Suciu 19:fb92949e59c9 184 if (i16SensorY < -Lcd::ACC_LIMIT) {
Adrian Suciu 19:fb92949e59c9 185 if (ui8xd == 0) {
Adrian Suciu 19:fb92949e59c9 186 ui8xd = 1;
Adrian Suciu 19:fb92949e59c9 187 lcd.display_symbol(2, Lcd::DOWN_X, 8, Lcd::pui8Rec8x8);
Adrian Suciu 19:fb92949e59c9 188 }
Adrian Suciu 19:fb92949e59c9 189
Adrian Suciu 19:fb92949e59c9 190 } else {
Adrian Suciu 19:fb92949e59c9 191 if (ui8xd == 1) {
Adrian Suciu 19:fb92949e59c9 192 ui8xd = 0;
Adrian Suciu 19:fb92949e59c9 193 lcd.display_symbol(2, Lcd::DOWN_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 194 }
Adrian Suciu 19:fb92949e59c9 195 }
Adrian Suciu 19:fb92949e59c9 196
Adrian Suciu 19:fb92949e59c9 197 if (i16SensorX > Lcd::ACC_LIMIT) {
Adrian Suciu 19:fb92949e59c9 198 if (ui8yu == 0) {
Adrian Suciu 19:fb92949e59c9 199 ui8yu = 1;
Adrian Suciu 19:fb92949e59c9 200 lcd.display_symbol(1, Lcd::RIGHT_X, 8, Lcd::pui8Rec8x8);
Adrian Suciu 19:fb92949e59c9 201 }
Adrian Suciu 19:fb92949e59c9 202
Adrian Suciu 19:fb92949e59c9 203 } else {
Adrian Suciu 19:fb92949e59c9 204 if (ui8yu == 1) {
Adrian Suciu 19:fb92949e59c9 205 ui8yu = 0;
Adrian Suciu 19:fb92949e59c9 206 lcd.display_symbol(1, Lcd::RIGHT_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 207 }
Adrian Suciu 19:fb92949e59c9 208 }
Adrian Suciu 19:fb92949e59c9 209
Adrian Suciu 19:fb92949e59c9 210 if (i16SensorX < -Lcd::ACC_LIMIT) {
Adrian Suciu 19:fb92949e59c9 211 if (ui8yd == 0) {
Adrian Suciu 19:fb92949e59c9 212 ui8yd = 1;
Adrian Suciu 19:fb92949e59c9 213 lcd.display_symbol(1, Lcd::LEFT_X, 8, Lcd::pui8Rec8x8);
Adrian Suciu 19:fb92949e59c9 214 }
Adrian Suciu 19:fb92949e59c9 215
Adrian Suciu 19:fb92949e59c9 216 } else {
Adrian Suciu 19:fb92949e59c9 217 if (ui8yd == 1) {
Adrian Suciu 19:fb92949e59c9 218 ui8yd = 0;
Adrian Suciu 19:fb92949e59c9 219 lcd.display_symbol(1, Lcd::LEFT_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 220 }
Adrian Suciu 19:fb92949e59c9 221 }
Adrian Suciu 19:fb92949e59c9 222
Adrian Suciu 19:fb92949e59c9 223 if ((ui8xu == 0) && (ui8xd == 0) && (ui8yu == 0) && (ui8yd == 0)) {
Adrian Suciu 19:fb92949e59c9 224 if (ui8all == 0) {
Adrian Suciu 19:fb92949e59c9 225 ui8all = 1;
Adrian Suciu 19:fb92949e59c9 226 lcd.display_symbol(1, Lcd::CENTER_X, 8, Lcd::pui8Rec8x8);
Adrian Suciu 19:fb92949e59c9 227 }
Adrian Suciu 19:fb92949e59c9 228
Adrian Suciu 19:fb92949e59c9 229 } else {
Adrian Suciu 19:fb92949e59c9 230 if (ui8all == 1) {
Adrian Suciu 19:fb92949e59c9 231 ui8all = 0;
Adrian Suciu 19:fb92949e59c9 232 lcd.display_symbol(1, Lcd::CENTER_X, 8, Lcd::pui8RecInv8x8);
Adrian Suciu 19:fb92949e59c9 233 }
Adrian Suciu 19:fb92949e59c9 234 }
Adrian Suciu 19:fb92949e59c9 235 }
Adrian Suciu 19:fb92949e59c9 236 }
Adrian Suciu 19:fb92949e59c9 237
Adrian Suciu 19:fb92949e59c9 238 }
Adrian Suciu 19:fb92949e59c9 239