Denwis La / Mbed OS mDot_Send_Data

Dependencies:   libmDot-dev-mbed5-deprecated ISL29011

Fork of mdot-examples by 3mdeb

Committer:
SDesign2018
Date:
Sat Dec 09 02:37:51 2017 +0000
Revision:
11:dce6c1b255fd
Parent:
10:db20118b7d32
Child:
12:bdf16bf682ec
Simplified the ADXL372 initialize function; Enabled low pass filter for more consistent data but still very high

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SDesign2018 5:c9ab5062cfc3 1 #include <stdlib.h>
SDesign2018 6:3b41238872a8 2 #include <iostream>
SDesign2018 5:c9ab5062cfc3 3 #include <string.h>
SDesign2018 5:c9ab5062cfc3 4 #include <mbed.h>
SDesign2018 4:b0ce6385d008 5 #include "dot_util.h"
SDesign2018 4:b0ce6385d008 6 #include "RadioEvent.h"
SDesign2018 5:c9ab5062cfc3 7 #include "itoa.h"
SDesign2018 5:c9ab5062cfc3 8
SDesign2018 4:b0ce6385d008 9
SDesign2018 4:b0ce6385d008 10 /////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 11 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
SDesign2018 4:b0ce6385d008 12 // * Because these example programs can be used for both mDot and xDot //
SDesign2018 4:b0ce6385d008 13 // devices, the LoRa stack is not included. The libmDot library should //
SDesign2018 4:b0ce6385d008 14 // be imported if building for mDot devices. The libxDot library //
SDesign2018 4:b0ce6385d008 15 // should be imported if building for xDot devices. //
SDesign2018 4:b0ce6385d008 16 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
SDesign2018 4:b0ce6385d008 17 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
SDesign2018 4:b0ce6385d008 18 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
SDesign2018 4:b0ce6385d008 19 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
SDesign2018 4:b0ce6385d008 20 /////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 21
SDesign2018 4:b0ce6385d008 22 /////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 23 // * these options must match between the two devices in //
SDesign2018 4:b0ce6385d008 24 // order for communication to be successful
SDesign2018 4:b0ce6385d008 25 //-------------------MDOT variables------------------------//
SDesign2018 4:b0ce6385d008 26 /////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 27 static uint8_t network_address[] = { 0x00, 0x11, 0x22, 0x33 };
SDesign2018 4:b0ce6385d008 28 static uint8_t network_session_key[] = { 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33 };
SDesign2018 4:b0ce6385d008 29 static uint8_t data_session_key[] = { 0x33, 0x22, 0x11, 0x00, 0x33, 0x22, 0x11, 0x00, 0x33, 0x22, 0x11, 0x00, 0x33, 0x22, 0x11, 0x00 };
SDesign2018 4:b0ce6385d008 30
SDesign2018 4:b0ce6385d008 31 mDot* dot = NULL;
SDesign2018 4:b0ce6385d008 32 lora::ChannelPlan* plan = NULL;
SDesign2018 4:b0ce6385d008 33 //--------------End of MDOT variables-------------------//
SDesign2018 4:b0ce6385d008 34
SDesign2018 4:b0ce6385d008 35 Serial pc(USBTX, USBRX);
SDesign2018 4:b0ce6385d008 36
SDesign2018 4:b0ce6385d008 37 // ADXL372 Slave SPI
SDesign2018 4:b0ce6385d008 38 DigitalOut MOSI(D11);
SDesign2018 4:b0ce6385d008 39 DigitalIn MISO(D12);
SDesign2018 4:b0ce6385d008 40 DigitalOut SPI_CLK(D13);
SDesign2018 4:b0ce6385d008 41 DigitalOut SPI_CS(D10);
SDesign2018 4:b0ce6385d008 42 //InterruptIn INT1();
SDesign2018 4:b0ce6385d008 43 //InterruptIn INT2();
SDesign2018 11:dce6c1b255fd 44
SDesign2018 4:b0ce6385d008 45
SDesign2018 4:b0ce6385d008 46 // ADXL372 Slave I2C
SDesign2018 4:b0ce6385d008 47 I2C ADXL372(I2C_SDA, I2C_SCL); // (D14,D15) (MISO, CS)
SDesign2018 4:b0ce6385d008 48
SDesign2018 4:b0ce6385d008 49 // ADT7410 Temperature
SDesign2018 4:b0ce6385d008 50 I2C ADT7410(I2C_SDA, I2C_SCL); // Attempt at making I2C connection to slaves (D14,D15)
SDesign2018 4:b0ce6385d008 51 InterruptIn ADT7410_Int(D2); // Allow this pin for ADT7410 Interrupt critical temperature notice
SDesign2018 4:b0ce6385d008 52
SDesign2018 4:b0ce6385d008 53 // DS7505s Temperature
SDesign2018 4:b0ce6385d008 54 I2C DS7505(I2C_SDA, I2C_SCL); // Attempt at making I2C connection to slaves (D14,D15)
SDesign2018 4:b0ce6385d008 55
SDesign2018 4:b0ce6385d008 56 // Create reocurring interrupt function that could be used to periodically take temperatures
SDesign2018 4:b0ce6385d008 57 // Not working right now due to some mutex initialize error
SDesign2018 4:b0ce6385d008 58 // Suspect that it is due to it having be a RTOS task thing
SDesign2018 4:b0ce6385d008 59 // Should probably go back to using an in processor timer interrupt instead of mbed
SDesign2018 4:b0ce6385d008 60 Ticker interruptEverything;
SDesign2018 4:b0ce6385d008 61
SDesign2018 4:b0ce6385d008 62 const int ADT7410_Address_7BIT = 0x49; // A0 set HIGH and A1 set LOW
SDesign2018 4:b0ce6385d008 63 const int ADT7410_Address_8BIT = ADT7410_Address_7BIT << 1; // Shift 1 bit to left for R/~W bit, and basic I2C format
SDesign2018 4:b0ce6385d008 64
SDesign2018 4:b0ce6385d008 65 const int ADXL372_Address_7bit = 0x1D; // Address for the I2C if MISO pulled low, 0x53 if pulled high
SDesign2018 4:b0ce6385d008 66 const int ADXL372_Address_8bit = ADXL372_Address_7bit << 1; // Same
SDesign2018 4:b0ce6385d008 67
SDesign2018 4:b0ce6385d008 68 const int DS7505s_Address_7bit = 0x48; // A0 set LOR, A1 set LOW, A2 set LOW
SDesign2018 4:b0ce6385d008 69 const int DS7505s_Address_8bit = DS7505s_Address_7bit << 1; // Same
SDesign2018 4:b0ce6385d008 70
SDesign2018 4:b0ce6385d008 71
SDesign2018 4:b0ce6385d008 72 //-------------------All prototype functions-----------------------//
SDesign2018 4:b0ce6385d008 73 void ADXL372Initialize(void);
SDesign2018 4:b0ce6385d008 74
SDesign2018 4:b0ce6385d008 75 int accelerometerI2CWrite(int hexAddress, int hexData);
SDesign2018 4:b0ce6385d008 76 char * accelerometerI2CRead(int hexAddress);
SDesign2018 4:b0ce6385d008 77 void ADXL372Reset(void);
SDesign2018 10:db20118b7d32 78 void I2CSelfTest(void);
SDesign2018 4:b0ce6385d008 79 void BitBangSPIWrite(const unsigned char regAddr, const unsigned char regData);
SDesign2018 5:c9ab5062cfc3 80 uint8_t BitBangSPIRead (const unsigned char regAddr);
SDesign2018 4:b0ce6385d008 81
SDesign2018 4:b0ce6385d008 82 int ADT7410Write(unsigned char registerAddress, unsigned char data);
SDesign2018 4:b0ce6385d008 83 char * ADT7410Read(int hex);
SDesign2018 4:b0ce6385d008 84
SDesign2018 4:b0ce6385d008 85 int DS7505sWrite(unsigned char registerAddress, unsigned char data);
SDesign2018 4:b0ce6385d008 86 char * DS7505sRead(int hex);
SDesign2018 4:b0ce6385d008 87
SDesign2018 4:b0ce6385d008 88 unsigned char * twosComplementConversion(unsigned char *value);
SDesign2018 6:3b41238872a8 89 void flushSerialBuffer(void);
SDesign2018 4:b0ce6385d008 90 //---------------------End of prototype functions-----------------------------//
SDesign2018 4:b0ce6385d008 91
SDesign2018 4:b0ce6385d008 92 void printMenu(){
SDesign2018 4:b0ce6385d008 93 pc.printf("Please eneter a debug option: \n\r"
SDesign2018 5:c9ab5062cfc3 94 "1: I2C Read converted values from Accelerometer ADXL372\n\r"
SDesign2018 4:b0ce6385d008 95 "2: Read converted values from Temperature ADT7410\n\r"
SDesign2018 4:b0ce6385d008 96 "3: Read raw values from Accelerometer ADXL372\n\r"
SDesign2018 4:b0ce6385d008 97 "4: Read raw values from Temperature ADT7410\n\r"
SDesign2018 9:df0c4e8a3097 98 "5: Initialize Accelerometer with I2C\n\r"
SDesign2018 10:db20118b7d32 99 "6: Reset Accelerometer with I2C\n\r"
SDesign2018 10:db20118b7d32 100 "7: Perform self test with I2C\n\r"
SDesign2018 10:db20118b7d32 101 "8: Send Temperature data\n\r"
SDesign2018 10:db20118b7d32 102 "9: SPI Read values from Accelerometer ADXL372\n\r"
SDesign2018 10:db20118b7d32 103 "a: SPI reset for ADXL372\n\r"
SDesign2018 10:db20118b7d32 104 "b: Initialize Accelerometer with SPI\n\r"
SDesign2018 10:db20118b7d32 105 "c: Check data in status register with SPI\n\r"
SDesign2018 10:db20118b7d32 106 "d: Perform self test with SPI\n\r");
SDesign2018 4:b0ce6385d008 107 }
SDesign2018 4:b0ce6385d008 108
SDesign2018 4:b0ce6385d008 109
SDesign2018 4:b0ce6385d008 110 /*******************************************************************************
SDesign2018 4:b0ce6385d008 111 * Function to be called by the ticker interrupt
SDesign2018 4:b0ce6385d008 112 * Read temperatures and send it
SDesign2018 4:b0ce6385d008 113 *
SDesign2018 4:b0ce6385d008 114 *
SDesign2018 4:b0ce6385d008 115 ******************************************************************************/
SDesign2018 4:b0ce6385d008 116 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 117 void interruptReadTemperature(void){
SDesign2018 4:b0ce6385d008 118 std::vector<uint8_t> tx_data;
SDesign2018 4:b0ce6385d008 119 uint16_t temperatures;
SDesign2018 4:b0ce6385d008 120 char data[2] = {0, 0};
SDesign2018 4:b0ce6385d008 121 char cmd[1];
SDesign2018 4:b0ce6385d008 122 cmd[0] = 0x00;
SDesign2018 4:b0ce6385d008 123 //pc.printf("Register Addres is: %x \n\r", cmd[0]);
SDesign2018 4:b0ce6385d008 124 if(ADT7410.write(ADT7410_Address_8BIT, cmd,1) == 0){
SDesign2018 4:b0ce6385d008 125 if(ADT7410.read(ADT7410_Address_8BIT, data, 2) == 0){
SDesign2018 4:b0ce6385d008 126 temperatures = ((data[0] << 8) | data[1]) >> 3;
SDesign2018 4:b0ce6385d008 127 tx_data.push_back((temperatures >> 8) & 0xFF);
SDesign2018 4:b0ce6385d008 128 tx_data.push_back(temperatures & 0xFF);
SDesign2018 4:b0ce6385d008 129 logInfo("light: %lu [0x%04X]", temperatures, temperatures);
SDesign2018 4:b0ce6385d008 130 send_data(tx_data);
SDesign2018 4:b0ce6385d008 131 //return (data[0] >> 8 | data[1])>>3; // Explained here: https://stackoverflow.com/a/141576 SOOO GOOOOODDD
SDesign2018 4:b0ce6385d008 132
SDesign2018 4:b0ce6385d008 133 }
SDesign2018 4:b0ce6385d008 134 }
SDesign2018 4:b0ce6385d008 135 }
SDesign2018 4:b0ce6385d008 136 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 137
SDesign2018 11:dce6c1b255fd 138
SDesign2018 11:dce6c1b255fd 139
SDesign2018 11:dce6c1b255fd 140 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 11:dce6c1b255fd 141 /* _
SDesign2018 11:dce6c1b255fd 142 ____ ___ ____ _(_)___
SDesign2018 11:dce6c1b255fd 143 / __ `__ \/ __ `/ / __ \
SDesign2018 11:dce6c1b255fd 144 / / / / / / /_/ / / / / /
SDesign2018 11:dce6c1b255fd 145 /_/ /_/ /_/\__,_/_/_/ /_/
SDesign2018 11:dce6c1b255fd 146
SDesign2018 11:dce6c1b255fd 147 *//////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 148 int main() {
SDesign2018 4:b0ce6385d008 149 // Custom event handler for automatically displaying RX data
SDesign2018 5:c9ab5062cfc3 150 //interruptEverything.attach(&interruptReadTemperature, 7.0);
SDesign2018 4:b0ce6385d008 151 RadioEvent events;
SDesign2018 4:b0ce6385d008 152 uint32_t tx_frequency;
SDesign2018 4:b0ce6385d008 153 uint8_t tx_datarate;
SDesign2018 4:b0ce6385d008 154 uint8_t tx_power;
SDesign2018 4:b0ce6385d008 155 uint8_t frequency_band;
SDesign2018 4:b0ce6385d008 156
SDesign2018 4:b0ce6385d008 157 // Points to the returned char pointer from called functions
SDesign2018 4:b0ce6385d008 158 char * rawTempValues; // Could change to uint8_t, same for other char pointers
SDesign2018 4:b0ce6385d008 159
SDesign2018 4:b0ce6385d008 160 // Save converted values here
SDesign2018 4:b0ce6385d008 161 uint16_t convertedTempValue; // Data values must be uint16_t for conversion and send prep
SDesign2018 4:b0ce6385d008 162 char *accelValues;
SDesign2018 4:b0ce6385d008 163 uint16_t XData;
SDesign2018 4:b0ce6385d008 164 uint16_t YData;
SDesign2018 4:b0ce6385d008 165 uint16_t ZData;
SDesign2018 4:b0ce6385d008 166 int regAddress;
SDesign2018 4:b0ce6385d008 167
SDesign2018 4:b0ce6385d008 168 // Change baud rate in serial terminal to this value
SDesign2018 4:b0ce6385d008 169 pc.baud(115200);
SDesign2018 4:b0ce6385d008 170
SDesign2018 4:b0ce6385d008 171
SDesign2018 4:b0ce6385d008 172 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
SDesign2018 4:b0ce6385d008 173
SDesign2018 4:b0ce6385d008 174 // Sometimes when calling this, it creates error: type specifier expected
SDesign2018 4:b0ce6385d008 175 // Even with identical include files I would get this in another workspace.
SDesign2018 4:b0ce6385d008 176 plan = new lora::ChannelPlan_US915();
SDesign2018 4:b0ce6385d008 177
SDesign2018 4:b0ce6385d008 178 logInfo("Now asserting");
SDesign2018 4:b0ce6385d008 179 assert(plan);
SDesign2018 4:b0ce6385d008 180
SDesign2018 4:b0ce6385d008 181 // Careful when using this. The production ready libmdot-mbed5 has a void constructor
SDesign2018 4:b0ce6385d008 182 // Therefore, can only use the libmDot-dev-mbed5 version, for now.
SDesign2018 4:b0ce6385d008 183 dot = mDot::getInstance(plan);
SDesign2018 4:b0ce6385d008 184 assert(dot);
SDesign2018 4:b0ce6385d008 185
SDesign2018 4:b0ce6385d008 186 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
SDesign2018 4:b0ce6385d008 187
SDesign2018 4:b0ce6385d008 188 // start from a well-known state
SDesign2018 4:b0ce6385d008 189 logInfo("defaulting Dot configuration");
SDesign2018 4:b0ce6385d008 190 dot->resetConfig();
SDesign2018 4:b0ce6385d008 191
SDesign2018 4:b0ce6385d008 192 // make sure library logging is turned on
SDesign2018 4:b0ce6385d008 193 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
SDesign2018 4:b0ce6385d008 194
SDesign2018 4:b0ce6385d008 195 // attach the custom events handler
SDesign2018 4:b0ce6385d008 196 dot->setEvents(&events);
SDesign2018 4:b0ce6385d008 197
SDesign2018 4:b0ce6385d008 198 // update configuration if necessary
SDesign2018 4:b0ce6385d008 199 if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
SDesign2018 4:b0ce6385d008 200 logInfo("changing network join mode to PEER_TO_PEER");
SDesign2018 4:b0ce6385d008 201 if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
SDesign2018 4:b0ce6385d008 202 logError("failed to set network join mode to PEER_TO_PEER");
SDesign2018 4:b0ce6385d008 203 }
SDesign2018 4:b0ce6385d008 204 }
SDesign2018 4:b0ce6385d008 205
SDesign2018 4:b0ce6385d008 206 /*
SDesign2018 4:b0ce6385d008 207 * Get the Frequency and then choose transfer frequency, datarate, and power accordingly
SDesign2018 4:b0ce6385d008 208 *
SDesign2018 4:b0ce6385d008 209 */
SDesign2018 4:b0ce6385d008 210 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 211 frequency_band = dot->getFrequencyBand();
SDesign2018 4:b0ce6385d008 212 switch (frequency_band) {
SDesign2018 4:b0ce6385d008 213 case lora::ChannelPlan::EU868_OLD:
SDesign2018 4:b0ce6385d008 214 case lora::ChannelPlan::EU868:
SDesign2018 4:b0ce6385d008 215 // 250kHz channels achieve higher throughput
SDesign2018 4:b0ce6385d008 216 // DR_6 : SF7 @ 250kHz
SDesign2018 4:b0ce6385d008 217 // DR_0 - DR_5 (125kHz channels) available but much slower
SDesign2018 4:b0ce6385d008 218 tx_frequency = 869850000;
SDesign2018 4:b0ce6385d008 219 tx_datarate = lora::DR_6;
SDesign2018 4:b0ce6385d008 220 // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
SDesign2018 4:b0ce6385d008 221 tx_power = 4;
SDesign2018 4:b0ce6385d008 222 break;
SDesign2018 4:b0ce6385d008 223
SDesign2018 4:b0ce6385d008 224 case lora::ChannelPlan::US915_OLD:
SDesign2018 4:b0ce6385d008 225 case lora::ChannelPlan::US915:
SDesign2018 4:b0ce6385d008 226 case lora::ChannelPlan::AU915_OLD:
SDesign2018 4:b0ce6385d008 227 case lora::ChannelPlan::AU915:
SDesign2018 4:b0ce6385d008 228 // 500kHz channels achieve highest throughput
SDesign2018 4:b0ce6385d008 229 // DR_8 : SF12 @ 500kHz
SDesign2018 4:b0ce6385d008 230 // DR_9 : SF11 @ 500kHz
SDesign2018 4:b0ce6385d008 231 // DR_10 : SF10 @ 500kHz
SDesign2018 4:b0ce6385d008 232 // DR_11 : SF9 @ 500kHz
SDesign2018 4:b0ce6385d008 233 // DR_12 : SF8 @ 500kHz
SDesign2018 4:b0ce6385d008 234 // DR_13 : SF7 @ 500kHz
SDesign2018 4:b0ce6385d008 235 // DR_0 - DR_3 (125kHz channels) available but much slower
SDesign2018 4:b0ce6385d008 236 tx_frequency = 915500000;
SDesign2018 4:b0ce6385d008 237 tx_datarate = lora::DR_13;
SDesign2018 4:b0ce6385d008 238 // 915 bands have no duty cycle restrictions, set tx power to max
SDesign2018 4:b0ce6385d008 239 tx_power = 20;
SDesign2018 4:b0ce6385d008 240 break;
SDesign2018 4:b0ce6385d008 241
SDesign2018 4:b0ce6385d008 242 case lora::ChannelPlan::AS923:
SDesign2018 4:b0ce6385d008 243 case lora::ChannelPlan::AS923_JAPAN:
SDesign2018 4:b0ce6385d008 244 // 250kHz channels achieve higher throughput
SDesign2018 4:b0ce6385d008 245 // DR_6 : SF7 @ 250kHz
SDesign2018 4:b0ce6385d008 246 // DR_0 - DR_5 (125kHz channels) available but much slower
SDesign2018 4:b0ce6385d008 247 tx_frequency = 924800000;
SDesign2018 4:b0ce6385d008 248 tx_datarate = lora::DR_6;
SDesign2018 4:b0ce6385d008 249 tx_power = 16;
SDesign2018 4:b0ce6385d008 250 break;
SDesign2018 4:b0ce6385d008 251
SDesign2018 4:b0ce6385d008 252 case lora::ChannelPlan::KR920:
SDesign2018 4:b0ce6385d008 253 // DR_5 : SF7 @ 125kHz
SDesign2018 4:b0ce6385d008 254 tx_frequency = 922700000;
SDesign2018 4:b0ce6385d008 255 tx_datarate = lora::DR_5;
SDesign2018 4:b0ce6385d008 256 tx_power = 14;
SDesign2018 4:b0ce6385d008 257 break;
SDesign2018 4:b0ce6385d008 258
SDesign2018 4:b0ce6385d008 259 default:
SDesign2018 4:b0ce6385d008 260 while (true) {
SDesign2018 4:b0ce6385d008 261 logFatal("no known channel plan in use - extra configuration is needed!");
SDesign2018 4:b0ce6385d008 262 wait(5);
SDesign2018 4:b0ce6385d008 263 }
SDesign2018 4:b0ce6385d008 264 break;
SDesign2018 4:b0ce6385d008 265 }
SDesign2018 4:b0ce6385d008 266
SDesign2018 4:b0ce6385d008 267 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 268
SDesign2018 4:b0ce6385d008 269 // in PEER_TO_PEER mode there is no join request/response transaction
SDesign2018 4:b0ce6385d008 270 // as long as both Dots are configured correctly, they should be able to communicate
SDesign2018 4:b0ce6385d008 271 update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);
SDesign2018 4:b0ce6385d008 272
SDesign2018 4:b0ce6385d008 273 // save changes to configuration
SDesign2018 4:b0ce6385d008 274 logInfo("saving configuration");
SDesign2018 4:b0ce6385d008 275 if (!dot->saveConfig()) {
SDesign2018 4:b0ce6385d008 276 logError("failed to save configuration");
SDesign2018 4:b0ce6385d008 277 }
SDesign2018 4:b0ce6385d008 278
SDesign2018 4:b0ce6385d008 279 // Display configuration
SDesign2018 4:b0ce6385d008 280 // It's gonna output a lot of information onto the Serial Terminal
SDesign2018 4:b0ce6385d008 281 display_config();
SDesign2018 4:b0ce6385d008 282
SDesign2018 4:b0ce6385d008 283 /*
SDesign2018 4:b0ce6385d008 284 *
SDesign2018 4:b0ce6385d008 285 * From here on is my own code
SDesign2018 4:b0ce6385d008 286 * Can add more options to choose from
SDesign2018 4:b0ce6385d008 287 *
SDesign2018 4:b0ce6385d008 288 */
SDesign2018 4:b0ce6385d008 289 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 290 printMenu();
SDesign2018 6:3b41238872a8 291 flushSerialBuffer();
SDesign2018 4:b0ce6385d008 292 pc.printf("\n\rChoose what you want to do: \n\r");
SDesign2018 4:b0ce6385d008 293
SDesign2018 4:b0ce6385d008 294 char userInput = pc.getc();
SDesign2018 4:b0ce6385d008 295 while(1){
SDesign2018 4:b0ce6385d008 296 // Create a vector of uint8_t elements to be sent later
SDesign2018 4:b0ce6385d008 297 std::vector<uint8_t> tx_data;
SDesign2018 4:b0ce6385d008 298
SDesign2018 4:b0ce6385d008 299 // Checks if a character has been pressed;
SDesign2018 4:b0ce6385d008 300 // Works right now for 1 digit numbers :(
SDesign2018 4:b0ce6385d008 301 // Change to work with larger inputs
SDesign2018 4:b0ce6385d008 302 if(pc.readable())
SDesign2018 4:b0ce6385d008 303 {
SDesign2018 4:b0ce6385d008 304 userInput = pc.getc();
SDesign2018 4:b0ce6385d008 305 switch(userInput){
SDesign2018 4:b0ce6385d008 306 case 49: // 1
SDesign2018 4:b0ce6385d008 307 pc.printf("Reading converted values from accelerometer\n\r");
SDesign2018 4:b0ce6385d008 308 for(int i = 0; i < 15; ++i){
SDesign2018 4:b0ce6385d008 309 regAddress = 0x08; // This is the register address for XData
SDesign2018 4:b0ce6385d008 310 accelValues = accelerometerI2CRead(regAddress);
SDesign2018 5:c9ab5062cfc3 311 XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short int, remove last 4 flag bits
SDesign2018 4:b0ce6385d008 312 YData = ((*(accelValues + 2) << 8) | *(accelValues + 3)) >> 4;
SDesign2018 4:b0ce6385d008 313 ZData = ((*(accelValues + 4) << 8) | *(accelValues + 5)) >> 4;
SDesign2018 4:b0ce6385d008 314 pc.printf("\n %d: X: 0x%x | Y: 0x%x | Z: 0x%x \n\r", i+1, XData, YData, ZData);
SDesign2018 4:b0ce6385d008 315 wait(0.2);
SDesign2018 4:b0ce6385d008 316 }
SDesign2018 4:b0ce6385d008 317 break;
SDesign2018 4:b0ce6385d008 318 case 50: // 2
SDesign2018 4:b0ce6385d008 319 pc.printf("Reading converted values from temperature\n\r");
SDesign2018 4:b0ce6385d008 320 for(int i = 0; i < 10; ++i){
SDesign2018 4:b0ce6385d008 321 regAddress = 0x00;
SDesign2018 4:b0ce6385d008 322 rawTempValues = ADT7410Read(regAddress);
SDesign2018 4:b0ce6385d008 323 convertedTempValue = ((*(rawTempValues + 0) << 8) | *(rawTempValues + 1)) >> 3; // Combine the two bytes into
SDesign2018 5:c9ab5062cfc3 324 // a short int variable(16 bits), remove last 3 bits
SDesign2018 4:b0ce6385d008 325
SDesign2018 4:b0ce6385d008 326 pc.printf("\n %d: Temperature is: 0x%x \n\r", i+1, convertedTempValue);
SDesign2018 4:b0ce6385d008 327 }
SDesign2018 4:b0ce6385d008 328
SDesign2018 4:b0ce6385d008 329 break;
SDesign2018 4:b0ce6385d008 330 case 51: // 3
SDesign2018 4:b0ce6385d008 331 pc.printf("Reading raw values from accelerometer\n\r");
SDesign2018 4:b0ce6385d008 332 for(int i = 0; i < 15; ++i){
SDesign2018 4:b0ce6385d008 333 regAddress = 0x08;
SDesign2018 4:b0ce6385d008 334 accelValues = accelerometerI2CRead(regAddress);
SDesign2018 5:c9ab5062cfc3 335 XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short int(16 bits), remove last 4 flag bits
SDesign2018 4:b0ce6385d008 336 YData = ((*(accelValues + 2) << 8) | *(accelValues + 3)) >> 4;
SDesign2018 4:b0ce6385d008 337 ZData = ((*(accelValues + 4) << 8) | *(accelValues + 5)) >> 4;
SDesign2018 4:b0ce6385d008 338 pc.printf("\n %d: X:: H: %x | L: %x | Y:: H: %x | L: %x | Z: H: %x | L: %x \n\r", i+1, *(accelValues + 0), *(accelValues + 1), *(accelValues + 2), *(accelValues + 3), *(accelValues + 4), *(accelValues + 5));
SDesign2018 4:b0ce6385d008 339 wait(0.2);
SDesign2018 4:b0ce6385d008 340 }
SDesign2018 4:b0ce6385d008 341 break;
SDesign2018 4:b0ce6385d008 342 case 52: // 4
SDesign2018 4:b0ce6385d008 343 pc.printf("Reading raw values from temperature\n\r");
SDesign2018 4:b0ce6385d008 344 for(int i = 0; i < 10; ++i){
SDesign2018 4:b0ce6385d008 345 regAddress = 0x00;
SDesign2018 4:b0ce6385d008 346 rawTempValues = ADT7410Read(regAddress);
SDesign2018 4:b0ce6385d008 347 pc.printf("\n %d: Temperature is: HIGH BYTE: %x | LOW BYTE: %x \n\r", i+1, *(rawTempValues + 0), *(rawTempValues + 1));
SDesign2018 4:b0ce6385d008 348 }
SDesign2018 4:b0ce6385d008 349 break;
SDesign2018 4:b0ce6385d008 350 case 53: // 5
SDesign2018 4:b0ce6385d008 351 ADXL372Initialize();
SDesign2018 4:b0ce6385d008 352 break;
SDesign2018 4:b0ce6385d008 353 case 54: // 6
SDesign2018 4:b0ce6385d008 354 ADXL372Reset();
SDesign2018 4:b0ce6385d008 355 break;
SDesign2018 4:b0ce6385d008 356
SDesign2018 10:db20118b7d32 357 case 55: // 7: perform self test with I2C
SDesign2018 10:db20118b7d32 358 I2CSelfTest();
SDesign2018 10:db20118b7d32 359 break;
SDesign2018 10:db20118b7d32 360 case 56: // 8: Read Temperature raws
SDesign2018 4:b0ce6385d008 361 regAddress = 0x00;
SDesign2018 4:b0ce6385d008 362 rawTempValues = ADT7410Read(regAddress);
SDesign2018 4:b0ce6385d008 363 convertedTempValue = ((*(rawTempValues + 0) << 8) | *(rawTempValues + 1)) >> 3; // Combine the two bytes
SDesign2018 4:b0ce6385d008 364
SDesign2018 4:b0ce6385d008 365 tx_data.push_back((convertedTempValue >> 8) & 0xFF);
SDesign2018 4:b0ce6385d008 366 tx_data.push_back(convertedTempValue & 0xFF);
SDesign2018 4:b0ce6385d008 367 logInfo("light: %lu [0x%04X]", convertedTempValue, convertedTempValue);
SDesign2018 4:b0ce6385d008 368 send_data(tx_data);
SDesign2018 5:c9ab5062cfc3 369 break;
SDesign2018 4:b0ce6385d008 370
SDesign2018 10:db20118b7d32 371 case 57: // 9: Read Accelerometer SPI
SDesign2018 5:c9ab5062cfc3 372 uint8_t MSB;
SDesign2018 5:c9ab5062cfc3 373 uint8_t LSB;
SDesign2018 5:c9ab5062cfc3 374
SDesign2018 5:c9ab5062cfc3 375
SDesign2018 5:c9ab5062cfc3 376 for(int i = 0; i < 15; ++i){
SDesign2018 5:c9ab5062cfc3 377
SDesign2018 5:c9ab5062cfc3 378 MSB = BitBangSPIRead(0x08); // XData MSB
SDesign2018 5:c9ab5062cfc3 379 LSB = BitBangSPIRead(0x09); // XData LSB
SDesign2018 5:c9ab5062cfc3 380 XData = ((MSB << 8) | LSB) >> 4;
SDesign2018 5:c9ab5062cfc3 381
SDesign2018 5:c9ab5062cfc3 382 MSB = BitBangSPIRead(0x0A); // YData MSB
SDesign2018 5:c9ab5062cfc3 383 LSB = BitBangSPIRead(0x0B); // YData LSB
SDesign2018 5:c9ab5062cfc3 384 YData = ((MSB << 8) | LSB) >> 4;
SDesign2018 5:c9ab5062cfc3 385
SDesign2018 5:c9ab5062cfc3 386 MSB = BitBangSPIRead(0x0C); // ZData MSB
SDesign2018 5:c9ab5062cfc3 387 LSB = BitBangSPIRead(0x0D); // ZData LSB
SDesign2018 5:c9ab5062cfc3 388 ZData = ((MSB << 8 ) | LSB) >> 4;
SDesign2018 5:c9ab5062cfc3 389
SDesign2018 5:c9ab5062cfc3 390 pc.printf("\n %d: X: 0x%x | Y: 0x%x | Z: 0x%x \n\r", i+1, XData, YData, ZData);
SDesign2018 5:c9ab5062cfc3 391 wait(0.2);
SDesign2018 5:c9ab5062cfc3 392 }
SDesign2018 5:c9ab5062cfc3 393 break;
SDesign2018 10:db20118b7d32 394 case 97: // a: Reset Accelerometer with SPI
SDesign2018 6:3b41238872a8 395 int intRegister;
SDesign2018 7:5f2b3d1a9b0b 396 char input[4];
SDesign2018 5:c9ab5062cfc3 397 char passRegister[1];
SDesign2018 6:3b41238872a8 398 int intData;
SDesign2018 5:c9ab5062cfc3 399 char passData[1];
SDesign2018 8:efab0e415826 400 BitBangSPIWrite(0x41, 0x52);
SDesign2018 8:efab0e415826 401 pc.printf("Done \n\r");
SDesign2018 6:3b41238872a8 402 break;
SDesign2018 10:db20118b7d32 403 case 98: // b: Initialize Accelerometer with SPI
SDesign2018 9:df0c4e8a3097 404 BitBangSPIWrite(0x24, 0x01); // Turn on X
SDesign2018 9:df0c4e8a3097 405 BitBangSPIWrite(0x26, 0x01); // Turn on Y
SDesign2018 9:df0c4e8a3097 406 BitBangSPIWrite(0x28, 0x01); // Turn on Z
SDesign2018 9:df0c4e8a3097 407 pc.printf("Done\n\r");
SDesign2018 9:df0c4e8a3097 408 break;
SDesign2018 10:db20118b7d32 409 case 99: // c: Check status with SPI
SDesign2018 9:df0c4e8a3097 410 uint8_t statusData = BitBangSPIRead(0x04);
SDesign2018 9:df0c4e8a3097 411
SDesign2018 9:df0c4e8a3097 412 pc.printf("0x%x in status\n\r", statusData);
SDesign2018 9:df0c4e8a3097 413 break;
SDesign2018 10:db20118b7d32 414 case 100: // d: Perform self-test
SDesign2018 9:df0c4e8a3097 415 uint8_t testResult;
SDesign2018 9:df0c4e8a3097 416 BitBangSPIWrite(0x3F, 0x03); // put to fullbandwidth measurement mode and lpf enaled(0)
SDesign2018 9:df0c4e8a3097 417
SDesign2018 9:df0c4e8a3097 418 BitBangSPIWrite(0x40, 0x01); // start self test
SDesign2018 9:df0c4e8a3097 419 testResult = BitBangSPIRead(0x40);
SDesign2018 9:df0c4e8a3097 420 while(!(BitBangSPIRead(0x40) & 0x02)){}
SDesign2018 9:df0c4e8a3097 421 wait(0.4);
SDesign2018 9:df0c4e8a3097 422
SDesign2018 9:df0c4e8a3097 423 testResult = BitBangSPIRead(0x40);
SDesign2018 9:df0c4e8a3097 424
SDesign2018 9:df0c4e8a3097 425 if(testResult & 0x04)
SDesign2018 9:df0c4e8a3097 426 {
SDesign2018 9:df0c4e8a3097 427 pc.printf("It passed \n\r");
SDesign2018 9:df0c4e8a3097 428 }
SDesign2018 9:df0c4e8a3097 429 pc.printf("0x%x\n\r", testResult);
SDesign2018 9:df0c4e8a3097 430 break;
SDesign2018 10:db20118b7d32 431
SDesign2018 4:b0ce6385d008 432 default:
SDesign2018 4:b0ce6385d008 433 printMenu();
SDesign2018 4:b0ce6385d008 434 break;
SDesign2018 4:b0ce6385d008 435 }
SDesign2018 4:b0ce6385d008 436 }
SDesign2018 4:b0ce6385d008 437
SDesign2018 4:b0ce6385d008 438 }
SDesign2018 4:b0ce6385d008 439
SDesign2018 4:b0ce6385d008 440 return 0;
SDesign2018 4:b0ce6385d008 441 }
SDesign2018 4:b0ce6385d008 442 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 443
SDesign2018 4:b0ce6385d008 444
SDesign2018 4:b0ce6385d008 445 /*******************************************************************************
SDesign2018 4:b0ce6385d008 446 *
SDesign2018 4:b0ce6385d008 447 * I2C function for the the ADXL372 accelerometer for a write sequence
SDesign2018 4:b0ce6385d008 448 * Param:
SDesign2018 4:b0ce6385d008 449 * hexAddress: Pass the hexadecimal value for what register you want
SDesign2018 4:b0ce6385d008 450 * hexData: Pass the hexadecimal value for what data you want to send
SDesign2018 4:b0ce6385d008 451 * i.e. hexadecimal represenatation of certain bits
SDesign2018 4:b0ce6385d008 452 * Returns:
SDesign2018 4:b0ce6385d008 453 * 1: Write was a complete success
SDesign2018 4:b0ce6385d008 454 * 2: Writing data to register failed
SDesign2018 4:b0ce6385d008 455 * 3: Writing to register Address failed
SDesign2018 4:b0ce6385d008 456 ******************************************************************************/
SDesign2018 4:b0ce6385d008 457 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 458 int accelerometerI2CWrite(int hexAddress, int hexData){
SDesign2018 4:b0ce6385d008 459 //--------- One full writing cycle for ADXL372 for X enable ------------------//
SDesign2018 4:b0ce6385d008 460 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 461 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 462 * '2' - timeout
SDesign2018 4:b0ce6385d008 463 */
SDesign2018 4:b0ce6385d008 464 int flag;
SDesign2018 4:b0ce6385d008 465 int registerAddress = hexAddress;
SDesign2018 4:b0ce6385d008 466 int data = hexData;
SDesign2018 4:b0ce6385d008 467
SDesign2018 4:b0ce6385d008 468 ADXL372.start();
SDesign2018 4:b0ce6385d008 469 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 470 if(flag == 1)
SDesign2018 4:b0ce6385d008 471 {
SDesign2018 11:dce6c1b255fd 472 //pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 473 wait(0.1);
SDesign2018 4:b0ce6385d008 474 flag = ADXL372.write(registerAddress);
SDesign2018 4:b0ce6385d008 475 if(flag == 1)
SDesign2018 4:b0ce6385d008 476 {
SDesign2018 11:dce6c1b255fd 477 //pc.printf("Write to register 0x%x address success\n\r", registerAddress);
SDesign2018 4:b0ce6385d008 478 flag = ADXL372.write(data);
SDesign2018 4:b0ce6385d008 479 if(flag == 1)
SDesign2018 4:b0ce6385d008 480 {
SDesign2018 4:b0ce6385d008 481 pc.printf("Writing data 0x%x to register address success\n\r", data);
SDesign2018 4:b0ce6385d008 482 ADXL372.stop();
SDesign2018 4:b0ce6385d008 483 return 1;
SDesign2018 4:b0ce6385d008 484 }else {ADXL372.stop(); return 2;}
SDesign2018 4:b0ce6385d008 485 }else {ADXL372.stop(); return 3;}
SDesign2018 4:b0ce6385d008 486 }else ADXL372.stop();
SDesign2018 4:b0ce6385d008 487
SDesign2018 4:b0ce6385d008 488 return 0;
SDesign2018 4:b0ce6385d008 489 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 490 }
SDesign2018 4:b0ce6385d008 491 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 492
SDesign2018 4:b0ce6385d008 493
SDesign2018 4:b0ce6385d008 494 /*******************************************************************************
SDesign2018 4:b0ce6385d008 495 * I2C read sequence for the accelerometer
SDesign2018 4:b0ce6385d008 496 * Param:
SDesign2018 4:b0ce6385d008 497 * hexAddress: pass the hexadecimal representation of desired Register address
SDesign2018 4:b0ce6385d008 498 * Return:
SDesign2018 4:b0ce6385d008 499 * Char pointer to the array of read data.
SDesign2018 4:b0ce6385d008 500 *
SDesign2018 4:b0ce6385d008 501 * Right now it works only for the XData, YData, ZData because I wrote it to read
SDesign2018 4:b0ce6385d008 502 * 6 bytes(6 registers).
SDesign2018 4:b0ce6385d008 503 * Should change it to be 1 byte at a time
SDesign2018 4:b0ce6385d008 504 ******************************************************************************/
SDesign2018 4:b0ce6385d008 505 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 506 char * accelerometerI2CRead(int hexAddress){
SDesign2018 4:b0ce6385d008 507 char accelData[6];
SDesign2018 4:b0ce6385d008 508 char registerAddress[1];
SDesign2018 4:b0ce6385d008 509 registerAddress[0] = hexAddress;
SDesign2018 4:b0ce6385d008 510
SDesign2018 4:b0ce6385d008 511 // Perform mbed's way sending a start bit, then device address[r/~w], and then the register address
SDesign2018 4:b0ce6385d008 512 // Also if it succeeds, continue to the next operation
SDesign2018 4:b0ce6385d008 513 if(ADXL372.write(ADXL372_Address_8bit, registerAddress, 1) == 0){
SDesign2018 4:b0ce6385d008 514
SDesign2018 4:b0ce6385d008 515 // If previous sequence works, get 6 bytes into char array accelData
SDesign2018 4:b0ce6385d008 516 // Char array because it uses 1 byte(8bits)
SDesign2018 4:b0ce6385d008 517 // Should probably change it to uint8_t type
SDesign2018 4:b0ce6385d008 518 if(ADXL372.read(ADXL372_Address_8bit, accelData, 6) == 0){
SDesign2018 4:b0ce6385d008 519 return accelData;
SDesign2018 4:b0ce6385d008 520 }else pc.printf("Failed to read\n\r");
SDesign2018 4:b0ce6385d008 521 }else pc.printf("Failed to write\n\r");
SDesign2018 4:b0ce6385d008 522 return 0; // Only if it fails
SDesign2018 4:b0ce6385d008 523 }
SDesign2018 4:b0ce6385d008 524 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 525
SDesign2018 4:b0ce6385d008 526
SDesign2018 4:b0ce6385d008 527
SDesign2018 4:b0ce6385d008 528 /*******************************************************************************
SDesign2018 4:b0ce6385d008 529 * Initializes whatever settings you want for the accelerometer
SDesign2018 4:b0ce6385d008 530 * Can change it to use the previous I2C write function instead of all this mess
SDesign2018 4:b0ce6385d008 531 *
SDesign2018 4:b0ce6385d008 532 ******************************************************************************/
SDesign2018 4:b0ce6385d008 533 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 534 void ADXL372Initialize(void){
SDesign2018 11:dce6c1b255fd 535 accelerometerI2CWrite(0x3F, 0x8F); // Enable I2C highspeed,Low Pass, High pass and full bandwidth measurement mode
SDesign2018 11:dce6c1b255fd 536 accelerometerI2CWrite(0x24, 0x01); // X used for activity threshold
SDesign2018 11:dce6c1b255fd 537 accelerometerI2CWrite(0x26, 0x01); // Y used for activity threshold
SDesign2018 11:dce6c1b255fd 538 accelerometerI2CWrite(0x28, 0x01); // Z used for activity threshold
SDesign2018 4:b0ce6385d008 539 }
SDesign2018 4:b0ce6385d008 540 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 541
SDesign2018 4:b0ce6385d008 542
SDesign2018 4:b0ce6385d008 543
SDesign2018 4:b0ce6385d008 544 /*******************************************************************************
SDesign2018 4:b0ce6385d008 545 * BitBangSPIWrite for ADXL372 if you wire it up as SPI
SDesign2018 4:b0ce6385d008 546 *
SDesign2018 4:b0ce6385d008 547 * Sends the 6-0bits of desired register with LSB of transmission bit R/!W
SDesign2018 4:b0ce6385d008 548 *
SDesign2018 4:b0ce6385d008 549 ******************************************************************************/
SDesign2018 4:b0ce6385d008 550 ///////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 551 void BitBangSPIWrite(const unsigned char regAddr, const unsigned char regData)
SDesign2018 4:b0ce6385d008 552 {
SDesign2018 4:b0ce6385d008 553 unsigned char SPICount; // Counter used to clock out the data
SDesign2018 4:b0ce6385d008 554
SDesign2018 4:b0ce6385d008 555 unsigned char SPIData; // Define a data structure for the SPI data
SDesign2018 4:b0ce6385d008 556 SPI_CS = 0; // Make sure we start with active-low CS high
SDesign2018 4:b0ce6385d008 557 SPI_CLK = 0; // and CK low
SDesign2018 4:b0ce6385d008 558
SDesign2018 4:b0ce6385d008 559 SPIData = regAddr; // Preload the data to be sent with Address
SDesign2018 4:b0ce6385d008 560 SPI_CS = 1; // Set active-low CS low to start the SPI cycle
SDesign2018 4:b0ce6385d008 561
SDesign2018 4:b0ce6385d008 562 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out the Address byte
SDesign2018 4:b0ce6385d008 563 {
SDesign2018 4:b0ce6385d008 564 if (SPIData & 0x80) // Check for a 1 at MSB
SDesign2018 4:b0ce6385d008 565 MOSI = 1; // and set the MOSI line appropriately
SDesign2018 4:b0ce6385d008 566 else
SDesign2018 4:b0ce6385d008 567 MOSI = 0;
SDesign2018 4:b0ce6385d008 568 SPI_CLK = 1; // Toggle the clock line
SDesign2018 4:b0ce6385d008 569 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 570 SPIData <<= 1; // Rotate to get the next bit
SDesign2018 4:b0ce6385d008 571 } // and loop back to send the next bit
SDesign2018 4:b0ce6385d008 572
SDesign2018 4:b0ce6385d008 573 // Repeat for the Data byte
SDesign2018 4:b0ce6385d008 574 SPIData = regData; // Preload the data to be sent with Data
SDesign2018 4:b0ce6385d008 575 for (SPICount = 0; SPICount < 8; SPICount++)
SDesign2018 4:b0ce6385d008 576 {
SDesign2018 4:b0ce6385d008 577 if (SPIData & 0x80)
SDesign2018 4:b0ce6385d008 578 MOSI = 1;
SDesign2018 4:b0ce6385d008 579 else
SDesign2018 4:b0ce6385d008 580 MOSI = 0;
SDesign2018 4:b0ce6385d008 581 SPI_CLK = 1;
SDesign2018 4:b0ce6385d008 582 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 583 SPIData <<= 1; // After each bit, move next bit one to left
SDesign2018 4:b0ce6385d008 584 }
SDesign2018 4:b0ce6385d008 585 SPI_CS = 0;
SDesign2018 4:b0ce6385d008 586 MOSI = 0;
SDesign2018 4:b0ce6385d008 587 }
SDesign2018 4:b0ce6385d008 588 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 589
SDesign2018 4:b0ce6385d008 590
SDesign2018 4:b0ce6385d008 591
SDesign2018 4:b0ce6385d008 592 /*******************************************************************************
SDesign2018 4:b0ce6385d008 593 * BitBangSPIRead for ADXL372 if you wire it up as SPI
SDesign2018 4:b0ce6385d008 594 *
SDesign2018 4:b0ce6385d008 595 *
SDesign2018 4:b0ce6385d008 596 *
SDesign2018 4:b0ce6385d008 597 ******************************************************************************/
SDesign2018 4:b0ce6385d008 598 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 5:c9ab5062cfc3 599 uint8_t BitBangSPIRead (uint8_t regAddr)
SDesign2018 4:b0ce6385d008 600 {
SDesign2018 4:b0ce6385d008 601
SDesign2018 4:b0ce6385d008 602 unsigned char SPICount; // Counter used to clock out the data
SDesign2018 4:b0ce6385d008 603
SDesign2018 5:c9ab5062cfc3 604 uint8_t SPIData;
SDesign2018 4:b0ce6385d008 605
SDesign2018 4:b0ce6385d008 606 SPI_CS = 0; // Make sure we start with active-low CS high
SDesign2018 4:b0ce6385d008 607 SPI_CLK = 0; // and CK low
SDesign2018 4:b0ce6385d008 608 SPIData = regAddr; // Preload the data to be sent with Address and Data
SDesign2018 4:b0ce6385d008 609
SDesign2018 4:b0ce6385d008 610 SPI_CS = 1; // Set active-low CS low to start the SPI cycle
SDesign2018 4:b0ce6385d008 611 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out the Address and Data
SDesign2018 4:b0ce6385d008 612 {
SDesign2018 4:b0ce6385d008 613 if (SPIData & 0x80)
SDesign2018 4:b0ce6385d008 614 MOSI = 1;
SDesign2018 4:b0ce6385d008 615 else
SDesign2018 4:b0ce6385d008 616 MOSI = 0;
SDesign2018 4:b0ce6385d008 617 SPI_CLK = 1;
SDesign2018 4:b0ce6385d008 618 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 619 SPIData <<= 1;
SDesign2018 4:b0ce6385d008 620 }// and loop back to send the next bit
SDesign2018 4:b0ce6385d008 621
SDesign2018 4:b0ce6385d008 622 MOSI = 0; // Reset the MOSI data line
SDesign2018 4:b0ce6385d008 623
SDesign2018 4:b0ce6385d008 624 SPIData = 0;
SDesign2018 4:b0ce6385d008 625 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock in the data to be read
SDesign2018 4:b0ce6385d008 626 {
SDesign2018 4:b0ce6385d008 627 SPIData <<=1; // Rotate the data, keep previous bit value
SDesign2018 4:b0ce6385d008 628 SPI_CLK = 1; // Raise the clock to clock the data out of the MAX7456
SDesign2018 4:b0ce6385d008 629 SPIData += SPI_MISO; // Read the data one bit at a time, starting from MISO MSB
SDesign2018 4:b0ce6385d008 630 SPI_CLK = 0; // Drop the clock ready for the next bit
SDesign2018 4:b0ce6385d008 631 }// and loop back for next bit
SDesign2018 4:b0ce6385d008 632 SPI_CS = 0; // Raise CS
SDesign2018 4:b0ce6385d008 633
SDesign2018 5:c9ab5062cfc3 634 return ((uint8_t)SPIData); // Finally return the read data
SDesign2018 4:b0ce6385d008 635 }
SDesign2018 4:b0ce6385d008 636
SDesign2018 4:b0ce6385d008 637 /*******************************************************************************
SDesign2018 4:b0ce6385d008 638 * Not really used at the moment
SDesign2018 4:b0ce6385d008 639 * Not really needed. But keep just in case because I don't want to rewrite it
SDesign2018 4:b0ce6385d008 640 ******************************************************************************/
SDesign2018 4:b0ce6385d008 641 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 642 unsigned char twosComplementConversion(unsigned char value)
SDesign2018 4:b0ce6385d008 643 {
SDesign2018 4:b0ce6385d008 644 /*
SDesign2018 4:b0ce6385d008 645 * Go from bit 0 to bit 7 and invert them
SDesign2018 4:b0ce6385d008 646 * Then finally add 1
SDesign2018 4:b0ce6385d008 647 */
SDesign2018 4:b0ce6385d008 648 char mask = value & 0x80;
SDesign2018 4:b0ce6385d008 649 if(mask == 0x80){ // Check for sign
SDesign2018 4:b0ce6385d008 650 value = ~value + 1;
SDesign2018 4:b0ce6385d008 651 return value;
SDesign2018 4:b0ce6385d008 652 }
SDesign2018 4:b0ce6385d008 653 return value;
SDesign2018 4:b0ce6385d008 654 }
SDesign2018 4:b0ce6385d008 655 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 656
SDesign2018 4:b0ce6385d008 657
SDesign2018 4:b0ce6385d008 658 /*******************************************************************************
SDesign2018 4:b0ce6385d008 659 * Performs one byte write I2C protocol
SDesign2018 4:b0ce6385d008 660 * PARAM:
SDesign2018 4:b0ce6385d008 661 * registerAddress: register you want access to in device, one byte char hex format
SDesign2018 4:b0ce6385d008 662 * data: one byte data that you want to write to device register
SDesign2018 4:b0ce6385d008 663 * Return:
SDesign2018 4:b0ce6385d008 664 * 0: failure at writing i2c address
SDesign2018 4:b0ce6385d008 665 * 1: successful write
SDesign2018 4:b0ce6385d008 666 * 2: failure at writing data
SDesign2018 4:b0ce6385d008 667 * 3: failure at writing register address
SDesign2018 4:b0ce6385d008 668 ******************************************************************************/
SDesign2018 4:b0ce6385d008 669 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 670 int ADT7410Write(unsigned char registerAddress, unsigned char data){
SDesign2018 4:b0ce6385d008 671 int flag;
SDesign2018 4:b0ce6385d008 672 ADT7410.start();
SDesign2018 4:b0ce6385d008 673 flag = ADT7410.write(ADT7410_Address_8BIT);
SDesign2018 4:b0ce6385d008 674 if(flag == 1)
SDesign2018 4:b0ce6385d008 675 {
SDesign2018 4:b0ce6385d008 676 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 677 wait(0.1);
SDesign2018 4:b0ce6385d008 678 flag = ADT7410.write(registerAddress);
SDesign2018 4:b0ce6385d008 679 if(flag == 1)
SDesign2018 4:b0ce6385d008 680 {
SDesign2018 4:b0ce6385d008 681 pc.printf("Write to register 0x%x address success\n\r", registerAddress);
SDesign2018 4:b0ce6385d008 682 flag = ADT7410.write(data);
SDesign2018 4:b0ce6385d008 683 if(flag == 1)
SDesign2018 4:b0ce6385d008 684 {
SDesign2018 4:b0ce6385d008 685 pc.printf("Writing data 0x%x to register address success\n\r", data);
SDesign2018 4:b0ce6385d008 686 ADT7410.stop();
SDesign2018 4:b0ce6385d008 687 return 1;
SDesign2018 4:b0ce6385d008 688 }else {ADT7410.stop(); return 2;}
SDesign2018 4:b0ce6385d008 689 }else {ADT7410.stop(); return 3;}
SDesign2018 4:b0ce6385d008 690 }else ADT7410.stop();
SDesign2018 4:b0ce6385d008 691
SDesign2018 4:b0ce6385d008 692 return 0;
SDesign2018 4:b0ce6385d008 693 }
SDesign2018 4:b0ce6385d008 694 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 695
SDesign2018 4:b0ce6385d008 696 /*******************************************************************************
SDesign2018 4:b0ce6385d008 697 * I2C Read function for ADT7410 Temperature sensor
SDesign2018 4:b0ce6385d008 698 * Param:
SDesign2018 4:b0ce6385d008 699 * hex: hexadecimal representation for desired register
SDesign2018 4:b0ce6385d008 700 * Return:
SDesign2018 4:b0ce6385d008 701 * Char pointer to the array of data values.
SDesign2018 4:b0ce6385d008 702 * Could also change from a char pointer to a uint8_t pointer.
SDesign2018 4:b0ce6385d008 703 *
SDesign2018 4:b0ce6385d008 704 ******************************************************************************/
SDesign2018 4:b0ce6385d008 705 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 706 char * ADT7410Read(int hex){
SDesign2018 4:b0ce6385d008 707 //short int convertedVal;
SDesign2018 4:b0ce6385d008 708 char data[2] = {0, 0};
SDesign2018 4:b0ce6385d008 709 char cmd[1];
SDesign2018 4:b0ce6385d008 710 cmd[0] = hex;
SDesign2018 4:b0ce6385d008 711 //pc.printf("Register Addres is: %x \n\r", cmd[0]);
SDesign2018 4:b0ce6385d008 712 if(ADT7410.write(ADT7410_Address_8BIT, cmd,1) == 0){
SDesign2018 4:b0ce6385d008 713 if(ADT7410.read(ADT7410_Address_8BIT, data, 2) == 0){
SDesign2018 4:b0ce6385d008 714
SDesign2018 4:b0ce6385d008 715 return data;
SDesign2018 4:b0ce6385d008 716 //return (data[0] << 8 | data[1])>>3; // Explained here: https://stackoverflow.com/a/141576 SOOO GREAT
SDesign2018 4:b0ce6385d008 717
SDesign2018 4:b0ce6385d008 718 }else {pc.printf("Failed to read \n\r"); return data;}
SDesign2018 4:b0ce6385d008 719 }else {pc.printf("Failed to write \n\r"); return data;}
SDesign2018 4:b0ce6385d008 720
SDesign2018 4:b0ce6385d008 721 }
SDesign2018 4:b0ce6385d008 722 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 723
SDesign2018 4:b0ce6385d008 724
SDesign2018 4:b0ce6385d008 725 /*******************************************************************************
SDesign2018 4:b0ce6385d008 726 * ADXL372 reset function
SDesign2018 4:b0ce6385d008 727 * Resets all registers and settings back to default
SDesign2018 4:b0ce6385d008 728 * Basically the same as the previous ADXL372 I2C write function
SDesign2018 4:b0ce6385d008 729 *
SDesign2018 4:b0ce6385d008 730 ******************************************************************************/
SDesign2018 4:b0ce6385d008 731 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 732 void ADXL372Reset(void){
SDesign2018 4:b0ce6385d008 733 int flag;
SDesign2018 4:b0ce6385d008 734 //--------- One full writing cycle for ADXL372 for Z Enable ------------------//
SDesign2018 4:b0ce6385d008 735 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 736 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 737 * '2' - timeout
SDesign2018 4:b0ce6385d008 738 */
SDesign2018 4:b0ce6385d008 739 ADXL372.start();
SDesign2018 4:b0ce6385d008 740 flag = ADXL372.write(ADXL372_Address_8bit | 0);
SDesign2018 4:b0ce6385d008 741 if(flag == 1)
SDesign2018 4:b0ce6385d008 742 {
SDesign2018 4:b0ce6385d008 743 //pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 744
SDesign2018 4:b0ce6385d008 745 flag = ADXL372.write(0x41);
SDesign2018 4:b0ce6385d008 746 if(flag == 1)
SDesign2018 4:b0ce6385d008 747 {
SDesign2018 4:b0ce6385d008 748 //pc.printf("Write to 0x41 register address success\n\r");
SDesign2018 4:b0ce6385d008 749 flag = ADXL372.write(0x52); // Set bit 0
SDesign2018 4:b0ce6385d008 750 if(flag == 1)
SDesign2018 4:b0ce6385d008 751 {
SDesign2018 4:b0ce6385d008 752 pc.printf("Everything has been reset\n\r");
SDesign2018 4:b0ce6385d008 753 ADXL372.stop();
SDesign2018 4:b0ce6385d008 754 }
SDesign2018 4:b0ce6385d008 755 }
SDesign2018 4:b0ce6385d008 756 }
SDesign2018 4:b0ce6385d008 757 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 758 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 759 }
SDesign2018 4:b0ce6385d008 760 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 6:3b41238872a8 761
SDesign2018 10:db20118b7d32 762 /*
SDesign2018 10:db20118b7d32 763 *
SDesign2018 10:db20118b7d32 764 * Self-test to see if the accelerometer is working as intended
SDesign2018 10:db20118b7d32 765 * Wait 300 ms.
SDesign2018 10:db20118b7d32 766 * Check bit 2 for a 1 for success. Bit 1 for completion of self-test.
SDesign2018 10:db20118b7d32 767 * Returns whole register
SDesign2018 10:db20118b7d32 768 */
SDesign2018 10:db20118b7d32 769 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 10:db20118b7d32 770 void I2CSelfTest(void){
SDesign2018 10:db20118b7d32 771 char *result;
SDesign2018 10:db20118b7d32 772 uint8_t check;
SDesign2018 10:db20118b7d32 773 accelerometerI2CWrite(0x3F, 0x03);
SDesign2018 10:db20118b7d32 774 accelerometerI2CWrite(0x40, 0x01);
SDesign2018 10:db20118b7d32 775 wait(0.3);
SDesign2018 10:db20118b7d32 776 result = accelerometerI2CRead(0x40);
SDesign2018 10:db20118b7d32 777 check = result[0];
SDesign2018 10:db20118b7d32 778 if(check & 0x04){
SDesign2018 10:db20118b7d32 779 pc.printf("Passed\n\r");
SDesign2018 10:db20118b7d32 780 }else {pc.printf("FAILED\n\r");}
SDesign2018 10:db20118b7d32 781 }
SDesign2018 10:db20118b7d32 782 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 10:db20118b7d32 783
SDesign2018 6:3b41238872a8 784 // Used to clear serial buffer in beginning
SDesign2018 6:3b41238872a8 785 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 6:3b41238872a8 786 void flushSerialBuffer(void) { char char1 = 0; while (pc.readable()) { char1 = pc.getc(); } return; }