Steven Kay
/
MCP9803_NUCLEO_Driver
Initial I2C Working
MCP9803/MCP9803.cpp@3:c6aad2355a40, 2017-03-30 (annotated)
- Committer:
- sk398
- Date:
- Thu Mar 30 06:27:48 2017 +0000
- Revision:
- 3:c6aad2355a40
- Parent:
- 2:832cb4376d2a
Working Version. By commenting out the required class decleration, the software is portable between the F042K6 and F446RE development boards
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:fbf82bf637bb | 1 | /******************************************************************************************** |
sk398 | 0:fbf82bf637bb | 2 | |
sk398 | 0:fbf82bf637bb | 3 | Filename: MCP9803.cpp |
sk398 | 0:fbf82bf637bb | 4 | |
sk398 | 0:fbf82bf637bb | 5 | Original Author: Steven Kay |
sk398 | 0:fbf82bf637bb | 6 | |
sk398 | 0:fbf82bf637bb | 7 | Development Group: Autonomous Systems Group, RAL Space |
sk398 | 0:fbf82bf637bb | 8 | |
sk398 | 0:fbf82bf637bb | 9 | Original Creation Date: April 2017 |
sk398 | 0:fbf82bf637bb | 10 | |
sk398 | 0:fbf82bf637bb | 11 | Description: <Desc> |
sk398 | 0:fbf82bf637bb | 12 | |
sk398 | 0:fbf82bf637bb | 13 | Revision History: Version 1.0 - Initial Release |
sk398 | 0:fbf82bf637bb | 14 | |
sk398 | 0:fbf82bf637bb | 15 | *********************************************************************************************/ |
sk398 | 0:fbf82bf637bb | 16 | |
sk398 | 0:fbf82bf637bb | 17 | #include "MCP9803.h" |
sk398 | 0:fbf82bf637bb | 18 | |
sk398 | 0:fbf82bf637bb | 19 | MCP9803::MCP9803(PinName sda, PinName scl, int Address, int frequency) |
sk398 | 0:fbf82bf637bb | 20 | { |
sk398 | 0:fbf82bf637bb | 21 | _I2C = new I2C(sda,scl); |
sk398 | 0:fbf82bf637bb | 22 | |
sk398 | 0:fbf82bf637bb | 23 | _I2C -> frequency(frequency); |
sk398 | 0:fbf82bf637bb | 24 | |
sk398 | 0:fbf82bf637bb | 25 | _I2C -> start(); |
sk398 | 0:fbf82bf637bb | 26 | _I2C -> stop(); |
sk398 | 0:fbf82bf637bb | 27 | |
sk398 | 0:fbf82bf637bb | 28 | chipAddress = Address; |
sk398 | 1:444546e8cd20 | 29 | |
sk398 | 1:444546e8cd20 | 30 | inBuffer = (char *)malloc(1); |
sk398 | 0:fbf82bf637bb | 31 | } |
sk398 | 0:fbf82bf637bb | 32 | |
sk398 | 2:832cb4376d2a | 33 | |
sk398 | 2:832cb4376d2a | 34 | |
sk398 | 2:832cb4376d2a | 35 | int MCP9803::ConfigSensor( int shutdown, int comp_int, int alert_polarity, |
sk398 | 2:832cb4376d2a | 36 | int fault_guide, int adc_res, int one_shot) |
sk398 | 0:fbf82bf637bb | 37 | { |
sk398 | 2:832cb4376d2a | 38 | // Assign passed values to struct members |
sk398 | 2:832cb4376d2a | 39 | CONFIG_REG_VALUE.CONFIG_BITS.SHUTDOWN_BIT = shutdown; |
sk398 | 2:832cb4376d2a | 40 | CONFIG_REG_VALUE.CONFIG_BITS.COMP_INT_BIT = comp_int; |
sk398 | 2:832cb4376d2a | 41 | CONFIG_REG_VALUE.CONFIG_BITS.ALERT_POLARITY_BIT = alert_polarity; |
sk398 | 2:832cb4376d2a | 42 | CONFIG_REG_VALUE.CONFIG_BITS.FAULT_QUEUE = fault_guide; |
sk398 | 2:832cb4376d2a | 43 | CONFIG_REG_VALUE.CONFIG_BITS.ADC_RES = adc_res; |
sk398 | 2:832cb4376d2a | 44 | CONFIG_REG_VALUE.CONFIG_BITS.ONE_SHOT = one_shot; |
sk398 | 2:832cb4376d2a | 45 | |
sk398 | 2:832cb4376d2a | 46 | // Generate Command to send to MCP9803 |
sk398 | 2:832cb4376d2a | 47 | // <Pointer to Config Register, CONFIG Register Value> |
sk398 | 2:832cb4376d2a | 48 | char dataOut[] = {CONFIG_REG_POINT, CONFIG_REG_VALUE.CONFIG_VALUE}; |
sk398 | 2:832cb4376d2a | 49 | |
sk398 | 2:832cb4376d2a | 50 | // Write Bytes to MCP9803 |
sk398 | 2:832cb4376d2a | 51 | int writeReturn = I2C_Write(dataOut,CONFIG_CMD_LENGTH); |
sk398 | 2:832cb4376d2a | 52 | |
sk398 | 2:832cb4376d2a | 53 | // Check on return status of the Write command |
sk398 | 2:832cb4376d2a | 54 | if(writeReturn != 0) |
sk398 | 2:832cb4376d2a | 55 | { |
sk398 | 2:832cb4376d2a | 56 | // Failure |
sk398 | 2:832cb4376d2a | 57 | return FAILURE; |
sk398 | 2:832cb4376d2a | 58 | } |
sk398 | 2:832cb4376d2a | 59 | else |
sk398 | 2:832cb4376d2a | 60 | { |
sk398 | 2:832cb4376d2a | 61 | // Success |
sk398 | 2:832cb4376d2a | 62 | return SUCCESS; |
sk398 | 2:832cb4376d2a | 63 | } |
sk398 | 2:832cb4376d2a | 64 | } |
sk398 | 2:832cb4376d2a | 65 | |
sk398 | 2:832cb4376d2a | 66 | |
sk398 | 2:832cb4376d2a | 67 | int MCP9803::RawTempValue() |
sk398 | 2:832cb4376d2a | 68 | { |
sk398 | 2:832cb4376d2a | 69 | // Set MCP9803 Register Pointer to point to Temperature Register |
sk398 | 2:832cb4376d2a | 70 | I2C_Write((char *)TEMP_REG_POINT,1); |
sk398 | 2:832cb4376d2a | 71 | |
sk398 | 2:832cb4376d2a | 72 | // Read TEMP_DATA_LENGTH bytes into temp buffer |
sk398 | 2:832cb4376d2a | 73 | char *tempInBuffer = I2C_Read(TEMP_DATA_LENGTH); |
sk398 | 0:fbf82bf637bb | 74 | |
sk398 | 2:832cb4376d2a | 75 | // Convert tempInBuffer to rawData |
sk398 | 2:832cb4376d2a | 76 | // Shift first Byte (MSB) up by 8, OR with second Byte (LSB) |
sk398 | 2:832cb4376d2a | 77 | // Then shift Result down by 4 |
sk398 | 2:832cb4376d2a | 78 | int rawData = ((*(tempInBuffer)<<8|*(tempInBuffer+1))>>4); |
sk398 | 2:832cb4376d2a | 79 | |
sk398 | 2:832cb4376d2a | 80 | // |
sk398 | 2:832cb4376d2a | 81 | return rawData; |
sk398 | 2:832cb4376d2a | 82 | } |
sk398 | 2:832cb4376d2a | 83 | |
sk398 | 2:832cb4376d2a | 84 | |
sk398 | 2:832cb4376d2a | 85 | |
sk398 | 2:832cb4376d2a | 86 | float MCP9803::FormattedTempValue(int format) |
sk398 | 2:832cb4376d2a | 87 | { |
sk398 | 2:832cb4376d2a | 88 | // Get Raw Temp Value |
sk398 | 2:832cb4376d2a | 89 | int tempData = RawTempValue(); |
sk398 | 0:fbf82bf637bb | 90 | |
sk398 | 2:832cb4376d2a | 91 | if(tempData == READ_TEMP_FAIL_VALUE) |
sk398 | 2:832cb4376d2a | 92 | { |
sk398 | 2:832cb4376d2a | 93 | return READ_TEMP_FAIL_ERROR; |
sk398 | 2:832cb4376d2a | 94 | } |
sk398 | 2:832cb4376d2a | 95 | |
sk398 | 2:832cb4376d2a | 96 | // Check and convert if 2's Complement |
sk398 | 2:832cb4376d2a | 97 | if(tempData > 2047) |
sk398 | 2:832cb4376d2a | 98 | { |
sk398 | 2:832cb4376d2a | 99 | tempData -= 4096; |
sk398 | 2:832cb4376d2a | 100 | } |
sk398 | 2:832cb4376d2a | 101 | |
sk398 | 2:832cb4376d2a | 102 | // Return formatted data based on format |
sk398 | 2:832cb4376d2a | 103 | switch(format) |
sk398 | 2:832cb4376d2a | 104 | { |
sk398 | 2:832cb4376d2a | 105 | // Return celcius value of temperature |
sk398 | 2:832cb4376d2a | 106 | case CELCIUS: |
sk398 | 2:832cb4376d2a | 107 | return (RAW_TO_C*tempData); |
sk398 | 2:832cb4376d2a | 108 | |
sk398 | 2:832cb4376d2a | 109 | // Return farenheit value of temperature |
sk398 | 2:832cb4376d2a | 110 | case FARENHEIT: |
sk398 | 2:832cb4376d2a | 111 | return ((RAW_TO_C*tempData)*C_F_1)+C_F_2; |
sk398 | 2:832cb4376d2a | 112 | |
sk398 | 2:832cb4376d2a | 113 | // Return Kelvin value of temperature |
sk398 | 2:832cb4376d2a | 114 | case KELVIN: |
sk398 | 2:832cb4376d2a | 115 | return ((RAW_TO_C*tempData)+C_TO_F); |
sk398 | 2:832cb4376d2a | 116 | |
sk398 | 2:832cb4376d2a | 117 | // If input is invalid, return value outwith range |
sk398 | 2:832cb4376d2a | 118 | // of other format options |
sk398 | 2:832cb4376d2a | 119 | default: |
sk398 | 2:832cb4376d2a | 120 | return FORMAT_FAIL; |
sk398 | 2:832cb4376d2a | 121 | } |
sk398 | 0:fbf82bf637bb | 122 | } |
sk398 | 0:fbf82bf637bb | 123 | |
sk398 | 0:fbf82bf637bb | 124 | |
sk398 | 0:fbf82bf637bb | 125 | |
sk398 | 0:fbf82bf637bb | 126 | int MCP9803::I2C_Write(char *dataOut,int dataLen) |
sk398 | 0:fbf82bf637bb | 127 | { |
sk398 | 2:832cb4376d2a | 128 | // Define Char Buffer of length dataLen |
sk398 | 0:fbf82bf637bb | 129 | char outBuffer[dataLen]; |
sk398 | 0:fbf82bf637bb | 130 | |
sk398 | 2:832cb4376d2a | 131 | // Populate Buffer with dataOut |
sk398 | 0:fbf82bf637bb | 132 | for(int i=0 ; i<dataLen ; i++) |
sk398 | 0:fbf82bf637bb | 133 | { |
sk398 | 0:fbf82bf637bb | 134 | outBuffer[i] = *(dataOut+i); |
sk398 | 0:fbf82bf637bb | 135 | } |
sk398 | 0:fbf82bf637bb | 136 | |
sk398 | 2:832cb4376d2a | 137 | // Write out to I2C Bus |
sk398 | 0:fbf82bf637bb | 138 | int sendStatus = _I2C -> write(chipAddress,outBuffer,dataLen,0); |
sk398 | 0:fbf82bf637bb | 139 | |
sk398 | 2:832cb4376d2a | 140 | // Return success value of the Write |
sk398 | 2:832cb4376d2a | 141 | // Returns 0 for Success, 1 for failure |
sk398 | 0:fbf82bf637bb | 142 | return sendStatus; |
sk398 | 0:fbf82bf637bb | 143 | } |
sk398 | 0:fbf82bf637bb | 144 | |
sk398 | 0:fbf82bf637bb | 145 | |
sk398 | 0:fbf82bf637bb | 146 | |
sk398 | 0:fbf82bf637bb | 147 | char *MCP9803::I2C_Read(int dataLen) |
sk398 | 0:fbf82bf637bb | 148 | { |
sk398 | 2:832cb4376d2a | 149 | // Reallocate memory to dataLen length expected for inBuffer |
sk398 | 1:444546e8cd20 | 150 | setBufferSize(dataLen); |
sk398 | 0:fbf82bf637bb | 151 | |
sk398 | 2:832cb4376d2a | 152 | // Read from the I2C Bus |
sk398 | 0:fbf82bf637bb | 153 | int receiveStatus = _I2C -> read(chipAddress,inBuffer,dataLen,0); |
sk398 | 1:444546e8cd20 | 154 | |
sk398 | 2:832cb4376d2a | 155 | // Check success value |
sk398 | 2:832cb4376d2a | 156 | // If receiveStatus != 0, indicates failure |
sk398 | 0:fbf82bf637bb | 157 | if(receiveStatus != 0) |
sk398 | 0:fbf82bf637bb | 158 | { |
sk398 | 2:832cb4376d2a | 159 | // Generate unique command to indicate failure, based on number of |
sk398 | 2:832cb4376d2a | 160 | // expected incoming bytes |
sk398 | 0:fbf82bf637bb | 161 | for(int i = 0; i < dataLen; i++) |
sk398 | 0:fbf82bf637bb | 162 | { |
sk398 | 2:832cb4376d2a | 163 | // For even indexes of inBuffer |
sk398 | 1:444546e8cd20 | 164 | if(i % 2 == 0) |
sk398 | 1:444546e8cd20 | 165 | { |
sk398 | 2:832cb4376d2a | 166 | inBuffer[i] = FAIL_EVEN_VALUE; |
sk398 | 1:444546e8cd20 | 167 | } |
sk398 | 2:832cb4376d2a | 168 | // For odd indexes of inBuffer |
sk398 | 1:444546e8cd20 | 169 | else |
sk398 | 1:444546e8cd20 | 170 | { |
sk398 | 2:832cb4376d2a | 171 | inBuffer[i] = FAIL_ODD_VALUE; |
sk398 | 1:444546e8cd20 | 172 | } |
sk398 | 0:fbf82bf637bb | 173 | } |
sk398 | 2:832cb4376d2a | 174 | // Return unique error command |
sk398 | 1:444546e8cd20 | 175 | return inBuffer; |
sk398 | 0:fbf82bf637bb | 176 | } |
sk398 | 0:fbf82bf637bb | 177 | else |
sk398 | 0:fbf82bf637bb | 178 | { |
sk398 | 2:832cb4376d2a | 179 | // Return Buffer contents based on successful read |
sk398 | 0:fbf82bf637bb | 180 | return inBuffer; |
sk398 | 0:fbf82bf637bb | 181 | } |
sk398 | 0:fbf82bf637bb | 182 | } |
sk398 | 0:fbf82bf637bb | 183 | |
sk398 | 1:444546e8cd20 | 184 | void MCP9803::setBufferSize(int dataLen) |
sk398 | 1:444546e8cd20 | 185 | { |
sk398 | 2:832cb4376d2a | 186 | // Reallocate memory for the inBuffer to account for differing lengths of |
sk398 | 2:832cb4376d2a | 187 | // data which can be read in |
sk398 | 1:444546e8cd20 | 188 | inBuffer = (char *)realloc(inBuffer, dataLen); |
sk398 | 1:444546e8cd20 | 189 | } |
sk398 | 0:fbf82bf637bb | 190 | |
sk398 | 0:fbf82bf637bb | 191 | /******************************************************************************************** |
sk398 | 0:fbf82bf637bb | 192 | |
sk398 | 0:fbf82bf637bb | 193 | Method: LinearMotor::LinearMotor |
sk398 | 0:fbf82bf637bb | 194 | |
sk398 | 0:fbf82bf637bb | 195 | Description: Class initialiser sets up the Linear Motor's pins |
sk398 | 0:fbf82bf637bb | 196 | |
sk398 | 0:fbf82bf637bb | 197 | Method Visibility: Public |
sk398 | 0:fbf82bf637bb | 198 | |
sk398 | 0:fbf82bf637bb | 199 | Input Arguments |
sk398 | 0:fbf82bf637bb | 200 | Name Type Description |
sk398 | 0:fbf82bf637bb | 201 | ----------------------------------------------------------------------------------------- |
sk398 | 0:fbf82bf637bb | 202 | pwmPin Int The integer number of the connected hardware pin for |
sk398 | 0:fbf82bf637bb | 203 | the PWM output to the linear motor |
sk398 | 0:fbf82bf637bb | 204 | |
sk398 | 0:fbf82bf637bb | 205 | sensorPin Int The integer number of the connected hardware pin for |
sk398 | 0:fbf82bf637bb | 206 | the Analog Sensor feedback from the Linear Motor |
sk398 | 0:fbf82bf637bb | 207 | |
sk398 | 0:fbf82bf637bb | 208 | Output Arguments |
sk398 | 0:fbf82bf637bb | 209 | ---------------------------------------------------------------------------------------- |
sk398 | 0:fbf82bf637bb | 210 | Name Type Description |
sk398 | 0:fbf82bf637bb | 211 | N/A |
sk398 | 0:fbf82bf637bb | 212 | |
sk398 | 0:fbf82bf637bb | 213 | *********************************************************************************************/ |