Example for using the MAX1472 RF Transmitter for low power data transmission.
Dependencies: mbed-dev2 max32630fthr USBDevice
ForwardErrCorr.cpp@4:2e3db197b7e2, 2020-09-04 (annotated)
- Committer:
- tlyp
- Date:
- Fri Sep 04 20:41:34 2020 +0000
- Revision:
- 4:2e3db197b7e2
- Parent:
- 2:33b3b46a9c0d
Cleaned up header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tlyp | 2:33b3b46a9c0d | 1 | /******************************************************************************* |
tlyp | 2:33b3b46a9c0d | 2 | * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved. |
tlyp | 2:33b3b46a9c0d | 3 | * |
tlyp | 2:33b3b46a9c0d | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
tlyp | 2:33b3b46a9c0d | 5 | * copy of this software and associated documentation files (the "Software"), |
tlyp | 2:33b3b46a9c0d | 6 | * to deal in the Software without restriction, including without limitation |
tlyp | 2:33b3b46a9c0d | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
tlyp | 2:33b3b46a9c0d | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
tlyp | 2:33b3b46a9c0d | 9 | * Software is furnished to do so, subject to the following conditions: |
tlyp | 2:33b3b46a9c0d | 10 | * |
tlyp | 2:33b3b46a9c0d | 11 | * The above copyright notice and this permission notice shall be included |
tlyp | 2:33b3b46a9c0d | 12 | * in all copies or substantial portions of the Software. |
tlyp | 2:33b3b46a9c0d | 13 | * |
tlyp | 2:33b3b46a9c0d | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
tlyp | 2:33b3b46a9c0d | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
tlyp | 2:33b3b46a9c0d | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
tlyp | 2:33b3b46a9c0d | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
tlyp | 2:33b3b46a9c0d | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
tlyp | 2:33b3b46a9c0d | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
tlyp | 2:33b3b46a9c0d | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
tlyp | 2:33b3b46a9c0d | 21 | * |
tlyp | 2:33b3b46a9c0d | 22 | * Except as contained in this notice, the name of Maxim Integrated |
tlyp | 2:33b3b46a9c0d | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
tlyp | 2:33b3b46a9c0d | 24 | * Products, Inc. Branding Policy. |
tlyp | 2:33b3b46a9c0d | 25 | * |
tlyp | 2:33b3b46a9c0d | 26 | * The mere transfer of this software does not imply any licenses |
tlyp | 2:33b3b46a9c0d | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
tlyp | 2:33b3b46a9c0d | 28 | * trademarks, maskwork rights, or any other form of intellectual |
tlyp | 2:33b3b46a9c0d | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
tlyp | 2:33b3b46a9c0d | 30 | * ownership rights. |
tlyp | 2:33b3b46a9c0d | 31 | ******************************************************************************* |
tlyp | 2:33b3b46a9c0d | 32 | */ |
tlyp | 2:33b3b46a9c0d | 33 | |
tlyp | 2:33b3b46a9c0d | 34 | #include "ForwardErrCorr.h" |
tlyp | 2:33b3b46a9c0d | 35 | |
tlyp | 2:33b3b46a9c0d | 36 | Translator::Translator(char *SymmetricKey, char *TransTable): |
tlyp | 2:33b3b46a9c0d | 37 | m_SymmetricKey(SymmetricKey),m_TransTable(TransTable) |
tlyp | 2:33b3b46a9c0d | 38 | { |
tlyp | 2:33b3b46a9c0d | 39 | } |
tlyp | 2:33b3b46a9c0d | 40 | |
tlyp | 2:33b3b46a9c0d | 41 | //****************************************************************************** |
tlyp | 2:33b3b46a9c0d | 42 | Translator::~Translator(void){ |
tlyp | 2:33b3b46a9c0d | 43 | } |
tlyp | 2:33b3b46a9c0d | 44 | |
tlyp | 2:33b3b46a9c0d | 45 | //****************************************************************************** |
tlyp | 2:33b3b46a9c0d | 46 | uint32_t Translator::Decrypt(char *tes, uint16_t *Output){ |
tlyp | 2:33b3b46a9c0d | 47 | int8_t ValOut,ValIn; |
tlyp | 2:33b3b46a9c0d | 48 | int key = 0; |
tlyp | 2:33b3b46a9c0d | 49 | Output[0] = 0; |
tlyp | 2:33b3b46a9c0d | 50 | printf("Recieved Data -> Decrypted Data:\r\n"); |
tlyp | 2:33b3b46a9c0d | 51 | for(int i = 2;i<10;i++){ |
tlyp | 2:33b3b46a9c0d | 52 | ValIn = tes[i]; |
tlyp | 2:33b3b46a9c0d | 53 | ValIn = ValIn ^ m_SymmetricKey[key]; //Unencrypt data |
tlyp | 2:33b3b46a9c0d | 54 | printf("%c -> %i\r\n",ValIn,tes[i]); |
tlyp | 2:33b3b46a9c0d | 55 | key++; |
tlyp | 2:33b3b46a9c0d | 56 | ValOut = 5; |
tlyp | 2:33b3b46a9c0d | 57 | for (int j=0;j<4;j++){ |
tlyp | 2:33b3b46a9c0d | 58 | if (ValIn == m_TransTable[j]){ |
tlyp | 2:33b3b46a9c0d | 59 | ValOut = j; |
tlyp | 2:33b3b46a9c0d | 60 | } |
tlyp | 2:33b3b46a9c0d | 61 | } |
tlyp | 2:33b3b46a9c0d | 62 | if (ValOut == 5){ |
tlyp | 2:33b3b46a9c0d | 63 | ValOut = ChkHam(ValIn); |
tlyp | 2:33b3b46a9c0d | 64 | } |
tlyp | 2:33b3b46a9c0d | 65 | if (ValOut == 4){ |
tlyp | 2:33b3b46a9c0d | 66 | printf("Transmission error -- Hamming Distance Collision\r\n"); |
tlyp | 2:33b3b46a9c0d | 67 | return (1); |
tlyp | 2:33b3b46a9c0d | 68 | } |
tlyp | 2:33b3b46a9c0d | 69 | Output[0] = (Output[0] << 2) + ValOut; |
tlyp | 2:33b3b46a9c0d | 70 | printf("Output: %d\r\n",Output[0]); |
tlyp | 2:33b3b46a9c0d | 71 | } |
tlyp | 2:33b3b46a9c0d | 72 | return(0); |
tlyp | 2:33b3b46a9c0d | 73 | } |
tlyp | 2:33b3b46a9c0d | 74 | |
tlyp | 2:33b3b46a9c0d | 75 | //****************************************************************************** |
tlyp | 2:33b3b46a9c0d | 76 | int Translator::ChkHam(char ChkVal){ |
tlyp | 2:33b3b46a9c0d | 77 | char trial = ChkVal & 0x1F; |
tlyp | 2:33b3b46a9c0d | 78 | bool dupe = 0; |
tlyp | 2:33b3b46a9c0d | 79 | int index, temp; |
tlyp | 2:33b3b46a9c0d | 80 | int min = 5; |
tlyp | 2:33b3b46a9c0d | 81 | for (int k = 0; k<4;k++){ |
tlyp | 2:33b3b46a9c0d | 82 | temp = HamDist(trial,m_TransTable[k]); |
tlyp | 2:33b3b46a9c0d | 83 | if (temp == 1){ |
tlyp | 2:33b3b46a9c0d | 84 | return (k); |
tlyp | 2:33b3b46a9c0d | 85 | } |
tlyp | 2:33b3b46a9c0d | 86 | else if (temp < min){ |
tlyp | 2:33b3b46a9c0d | 87 | min = temp; |
tlyp | 2:33b3b46a9c0d | 88 | index = k; |
tlyp | 2:33b3b46a9c0d | 89 | dupe = 0; |
tlyp | 2:33b3b46a9c0d | 90 | } |
tlyp | 2:33b3b46a9c0d | 91 | else if (temp == min){ |
tlyp | 2:33b3b46a9c0d | 92 | dupe = 1; |
tlyp | 2:33b3b46a9c0d | 93 | } |
tlyp | 2:33b3b46a9c0d | 94 | } |
tlyp | 2:33b3b46a9c0d | 95 | if (dupe == 1){ |
tlyp | 2:33b3b46a9c0d | 96 | return(4); |
tlyp | 2:33b3b46a9c0d | 97 | } |
tlyp | 2:33b3b46a9c0d | 98 | else{ |
tlyp | 2:33b3b46a9c0d | 99 | return (index); |
tlyp | 2:33b3b46a9c0d | 100 | } |
tlyp | 2:33b3b46a9c0d | 101 | } |
tlyp | 2:33b3b46a9c0d | 102 | |
tlyp | 2:33b3b46a9c0d | 103 | //****************************************************************************** |
tlyp | 2:33b3b46a9c0d | 104 | int Translator::HamDist(char ChkVal, char TableVal){ |
tlyp | 2:33b3b46a9c0d | 105 | int count=0; |
tlyp | 2:33b3b46a9c0d | 106 | char tes1,tes2; |
tlyp | 2:33b3b46a9c0d | 107 | for (int j =0;j<5;j++){ |
tlyp | 2:33b3b46a9c0d | 108 | tes1 = ChkVal >> j; |
tlyp | 2:33b3b46a9c0d | 109 | tes2 = TableVal >> j; |
tlyp | 2:33b3b46a9c0d | 110 | char temp = tes1 ^ tes2; |
tlyp | 2:33b3b46a9c0d | 111 | count += temp&1; |
tlyp | 2:33b3b46a9c0d | 112 | } |
tlyp | 2:33b3b46a9c0d | 113 | return (count); |
tlyp | 2:33b3b46a9c0d | 114 | } |
tlyp | 2:33b3b46a9c0d | 115 | |
tlyp | 2:33b3b46a9c0d | 116 | //****************************************************************************** |
tlyp | 2:33b3b46a9c0d | 117 | uint32_t Translator::Encrypt(uint16_t tempData,char *EncryptedData){ |
tlyp | 2:33b3b46a9c0d | 118 | char data; |
tlyp | 2:33b3b46a9c0d | 119 | int z=0; |
tlyp | 2:33b3b46a9c0d | 120 | printf("FEC Encoded Data:\r\n"); |
tlyp | 2:33b3b46a9c0d | 121 | for (int i=14;i>=0;i-=2){ |
tlyp | 2:33b3b46a9c0d | 122 | data = ((tempData >> i)&0x03); |
tlyp | 2:33b3b46a9c0d | 123 | data = m_TransTable[data]; |
tlyp | 2:33b3b46a9c0d | 124 | printf("%d ",data); |
tlyp | 2:33b3b46a9c0d | 125 | data = (data ^ m_SymmetricKey[z]); |
tlyp | 2:33b3b46a9c0d | 126 | EncryptedData[z]=data; |
tlyp | 2:33b3b46a9c0d | 127 | z++; |
tlyp | 2:33b3b46a9c0d | 128 | } |
tlyp | 2:33b3b46a9c0d | 129 | printf("\r\n"); |
tlyp | 2:33b3b46a9c0d | 130 | return(0); |
tlyp | 2:33b3b46a9c0d | 131 | } |
tlyp | 2:33b3b46a9c0d | 132 | |
tlyp | 2:33b3b46a9c0d | 133 | //****************************************************************************** |
tlyp | 2:33b3b46a9c0d | 134 | uint32_t Translator::Encrypt(char tempData, char *EncryptedData){ |
tlyp | 2:33b3b46a9c0d | 135 | char data; |
tlyp | 2:33b3b46a9c0d | 136 | int z =0; |
tlyp | 2:33b3b46a9c0d | 137 | for (int i=6;i>=0;i-=2){ |
tlyp | 2:33b3b46a9c0d | 138 | data = ((tempData >> i)&0x03); |
tlyp | 2:33b3b46a9c0d | 139 | data = m_TransTable[data]; |
tlyp | 2:33b3b46a9c0d | 140 | data = (data ^ m_SymmetricKey[z]); |
tlyp | 2:33b3b46a9c0d | 141 | EncryptedData[z] = data; |
tlyp | 2:33b3b46a9c0d | 142 | z++; |
tlyp | 2:33b3b46a9c0d | 143 | } |
tlyp | 2:33b3b46a9c0d | 144 | return(0); |
tlyp | 2:33b3b46a9c0d | 145 | } |