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