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:56:19 2017 +0000
Revision:
12:bdf16bf682ec
Parent:
11:dce6c1b255fd
Child:
13:e336881e0a3e
Tried to use twos complement function;

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