modify to nucleo STM32F411
Fork of MX28 by
Utilities.cpp@4:30da84384307, 2015-08-25 (annotated)
- Committer:
- soulx
- Date:
- Tue Aug 25 08:51:58 2015 +0000
- Revision:
- 4:30da84384307
- Parent:
- 0:ea5b951002cf
modify for Nucleo STM32F411
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GIPetrou | 0:ea5b951002cf | 1 | /* Copyright (c) 2012 Georgios Petrou, MIT License |
GIPetrou | 0:ea5b951002cf | 2 | * |
GIPetrou | 0:ea5b951002cf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
GIPetrou | 0:ea5b951002cf | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
GIPetrou | 0:ea5b951002cf | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
GIPetrou | 0:ea5b951002cf | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
GIPetrou | 0:ea5b951002cf | 7 | * furnished to do so, subject to the following conditions: |
GIPetrou | 0:ea5b951002cf | 8 | * |
GIPetrou | 0:ea5b951002cf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
GIPetrou | 0:ea5b951002cf | 10 | * substantial portions of the Software. |
GIPetrou | 0:ea5b951002cf | 11 | * |
GIPetrou | 0:ea5b951002cf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
GIPetrou | 0:ea5b951002cf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
GIPetrou | 0:ea5b951002cf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
GIPetrou | 0:ea5b951002cf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
GIPetrou | 0:ea5b951002cf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
GIPetrou | 0:ea5b951002cf | 17 | */ |
GIPetrou | 0:ea5b951002cf | 18 | |
GIPetrou | 0:ea5b951002cf | 19 | #include "Utilities.h" |
GIPetrou | 0:ea5b951002cf | 20 | |
GIPetrou | 0:ea5b951002cf | 21 | uint8_t Utilities::GetCheckSum(const uint8_t *data, uint8_t length) |
GIPetrou | 0:ea5b951002cf | 22 | { |
GIPetrou | 0:ea5b951002cf | 23 | uint8_t checkSum = 0; |
GIPetrou | 0:ea5b951002cf | 24 | |
GIPetrou | 0:ea5b951002cf | 25 | for(int i = 0; i < length; i++) |
GIPetrou | 0:ea5b951002cf | 26 | checkSum += data[i]; |
GIPetrou | 0:ea5b951002cf | 27 | |
GIPetrou | 0:ea5b951002cf | 28 | checkSum = ~checkSum; |
GIPetrou | 0:ea5b951002cf | 29 | |
GIPetrou | 0:ea5b951002cf | 30 | return checkSum; |
GIPetrou | 0:ea5b951002cf | 31 | } |
GIPetrou | 0:ea5b951002cf | 32 | |
GIPetrou | 0:ea5b951002cf | 33 | void Utilities::ConvertUInt16ToUInt8Array(uint16_t value, uint8_t* data) |
GIPetrou | 0:ea5b951002cf | 34 | { |
GIPetrou | 0:ea5b951002cf | 35 | data[0] = value; |
GIPetrou | 0:ea5b951002cf | 36 | data[1] = value >> 8; |
GIPetrou | 0:ea5b951002cf | 37 | } |
GIPetrou | 0:ea5b951002cf | 38 | |
GIPetrou | 0:ea5b951002cf | 39 | void Utilities::ConvertInt16ToUInt8Array(int16_t value, uint8_t* data) |
GIPetrou | 0:ea5b951002cf | 40 | { |
GIPetrou | 0:ea5b951002cf | 41 | data[0] = value; |
GIPetrou | 0:ea5b951002cf | 42 | data[1] = value >> 8; |
GIPetrou | 0:ea5b951002cf | 43 | } |
GIPetrou | 0:ea5b951002cf | 44 | |
GIPetrou | 0:ea5b951002cf | 45 | void Utilities::ConvertUInt32ToUInt8Array(uint32_t value, uint8_t* data) |
GIPetrou | 0:ea5b951002cf | 46 | { |
GIPetrou | 0:ea5b951002cf | 47 | data[0] = value; |
GIPetrou | 0:ea5b951002cf | 48 | data[1] = value >> 8; |
GIPetrou | 0:ea5b951002cf | 49 | data[2] = value >> 16; |
GIPetrou | 0:ea5b951002cf | 50 | data[3] = value >> 24; |
GIPetrou | 0:ea5b951002cf | 51 | } |
GIPetrou | 0:ea5b951002cf | 52 | |
GIPetrou | 0:ea5b951002cf | 53 | void Utilities::ConvertInt32ToUInt8Array(int32_t value, uint8_t* data) |
GIPetrou | 0:ea5b951002cf | 54 | { |
GIPetrou | 0:ea5b951002cf | 55 | data[0] = value; |
GIPetrou | 0:ea5b951002cf | 56 | data[1] = value >> 8; |
GIPetrou | 0:ea5b951002cf | 57 | data[2] = value >> 16; |
GIPetrou | 0:ea5b951002cf | 58 | data[3] = value >> 24; |
GIPetrou | 0:ea5b951002cf | 59 | } |
GIPetrou | 0:ea5b951002cf | 60 | |
GIPetrou | 0:ea5b951002cf | 61 | uint16_t Utilities::ConvertUInt8ArrayToUInt16(uint8_t* data) |
GIPetrou | 0:ea5b951002cf | 62 | { |
GIPetrou | 0:ea5b951002cf | 63 | return (((uint16_t)data[1]) << 8) | ((uint16_t)data[0]); |
GIPetrou | 0:ea5b951002cf | 64 | } |
GIPetrou | 0:ea5b951002cf | 65 | |
GIPetrou | 0:ea5b951002cf | 66 | int16_t Utilities::ConvertUInt8ArrayToInt16(uint8_t* data) |
GIPetrou | 0:ea5b951002cf | 67 | { |
GIPetrou | 0:ea5b951002cf | 68 | return (((int16_t)data[1]) << 8) | ((int16_t)data[0]); |
GIPetrou | 0:ea5b951002cf | 69 | } |
GIPetrou | 0:ea5b951002cf | 70 | |
GIPetrou | 0:ea5b951002cf | 71 | int32_t Utilities::ConvertUInt8ArrayToInt32(uint8_t* data) |
GIPetrou | 0:ea5b951002cf | 72 | { |
GIPetrou | 0:ea5b951002cf | 73 | return (((int32_t)data[3]) << 24) | (((int32_t)data[2]) << 16) | (((int32_t)data[1]) << 8) | ((int32_t)data[0]); |
GIPetrou | 0:ea5b951002cf | 74 | } |
GIPetrou | 0:ea5b951002cf | 75 |