
Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.
Dependencies: C12832 MQTT LM75B MMA7660
Dependents: MFT_IoT_demo_USB400 IBM_RFID
EthernetInterface/lwip-sys/arch/checksum.c@6:37b6d0d56190, 2014-08-20 (annotated)
- Committer:
- samdanbury
- Date:
- Wed Aug 20 12:45:14 2014 +0000
- Revision:
- 6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | /* Copyright (C) 2013 - Adam Green (https://github.com/adamgreen) |
samdanbury | 6:37b6d0d56190 | 2 | |
samdanbury | 6:37b6d0d56190 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
samdanbury | 6:37b6d0d56190 | 4 | you may not use this file except in compliance with the License. |
samdanbury | 6:37b6d0d56190 | 5 | You may obtain a copy of the License at |
samdanbury | 6:37b6d0d56190 | 6 | |
samdanbury | 6:37b6d0d56190 | 7 | http://www.apache.org/licenses/LICENSE-2.0 |
samdanbury | 6:37b6d0d56190 | 8 | |
samdanbury | 6:37b6d0d56190 | 9 | Unless required by applicable law or agreed to in writing, software |
samdanbury | 6:37b6d0d56190 | 10 | distributed under the License is distributed on an "AS IS" BASIS, |
samdanbury | 6:37b6d0d56190 | 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
samdanbury | 6:37b6d0d56190 | 12 | See the License for the specific language governing permissions and |
samdanbury | 6:37b6d0d56190 | 13 | limitations under the License. |
samdanbury | 6:37b6d0d56190 | 14 | */ |
samdanbury | 6:37b6d0d56190 | 15 | #if defined(TOOLCHAIN_GCC) && defined(__thumb2__) |
samdanbury | 6:37b6d0d56190 | 16 | |
samdanbury | 6:37b6d0d56190 | 17 | |
samdanbury | 6:37b6d0d56190 | 18 | /* This is a hand written Thumb-2 assembly language version of the |
samdanbury | 6:37b6d0d56190 | 19 | algorithm 3 version of lwip_standard_chksum in lwIP's inet_chksum.c. It |
samdanbury | 6:37b6d0d56190 | 20 | performs the checksumming 32-bits at a time and even unrolls the loop to |
samdanbury | 6:37b6d0d56190 | 21 | perform two of these 32-bit adds per loop iteration. |
samdanbury | 6:37b6d0d56190 | 22 | |
samdanbury | 6:37b6d0d56190 | 23 | Returns: |
samdanbury | 6:37b6d0d56190 | 24 | 16-bit 1's complement summation (not inversed). |
samdanbury | 6:37b6d0d56190 | 25 | |
samdanbury | 6:37b6d0d56190 | 26 | NOTE: This function does return a uint16_t from the assembly language code |
samdanbury | 6:37b6d0d56190 | 27 | but is marked as void so that GCC doesn't issue warning because it |
samdanbury | 6:37b6d0d56190 | 28 | doesn't know about this low level return. |
samdanbury | 6:37b6d0d56190 | 29 | */ |
samdanbury | 6:37b6d0d56190 | 30 | __attribute__((naked)) void /*uint16_t*/ thumb2_checksum(const void* pData, int length) |
samdanbury | 6:37b6d0d56190 | 31 | { |
samdanbury | 6:37b6d0d56190 | 32 | __asm ( |
samdanbury | 6:37b6d0d56190 | 33 | ".syntax unified\n" |
samdanbury | 6:37b6d0d56190 | 34 | ".thumb\n" |
samdanbury | 6:37b6d0d56190 | 35 | |
samdanbury | 6:37b6d0d56190 | 36 | // Push non-volatile registers we use on stack. Push link register too to |
samdanbury | 6:37b6d0d56190 | 37 | // keep stack 8-byte aligned and allow single pop to restore and return. |
samdanbury | 6:37b6d0d56190 | 38 | " push {r4, lr}\n" |
samdanbury | 6:37b6d0d56190 | 39 | // Initialize sum, r2, to 0. |
samdanbury | 6:37b6d0d56190 | 40 | " movs r2, #0\n" |
samdanbury | 6:37b6d0d56190 | 41 | // Remember whether pData was at odd address in r3. This is used later to |
samdanbury | 6:37b6d0d56190 | 42 | // know if it needs to swap the result since the summation will be done at |
samdanbury | 6:37b6d0d56190 | 43 | // an offset of 1, rather than 0. |
samdanbury | 6:37b6d0d56190 | 44 | " ands r3, r0, #1\n" |
samdanbury | 6:37b6d0d56190 | 45 | // Need to 2-byte align? If not skip ahead. |
samdanbury | 6:37b6d0d56190 | 46 | " beq 1$\n" |
samdanbury | 6:37b6d0d56190 | 47 | // We can return if there are no bytes to sum. |
samdanbury | 6:37b6d0d56190 | 48 | " cbz r1, 9$\n" |
samdanbury | 6:37b6d0d56190 | 49 | |
samdanbury | 6:37b6d0d56190 | 50 | // 2-byte align. |
samdanbury | 6:37b6d0d56190 | 51 | // Place the first data byte in odd summation location since it needs to be |
samdanbury | 6:37b6d0d56190 | 52 | // swapped later. It's ok to overwrite r2 here as it only had a value of 0 |
samdanbury | 6:37b6d0d56190 | 53 | // up until now. Advance r0 pointer and decrement r1 length as we go. |
samdanbury | 6:37b6d0d56190 | 54 | " ldrb r2, [r0], #1\n" |
samdanbury | 6:37b6d0d56190 | 55 | " lsls r2, r2, #8\n" |
samdanbury | 6:37b6d0d56190 | 56 | " subs r1, r1, #1\n" |
samdanbury | 6:37b6d0d56190 | 57 | |
samdanbury | 6:37b6d0d56190 | 58 | // Need to 4-byte align? If not skip ahead. |
samdanbury | 6:37b6d0d56190 | 59 | "1$:\n" |
samdanbury | 6:37b6d0d56190 | 60 | " ands r4, r0, #3\n" |
samdanbury | 6:37b6d0d56190 | 61 | " beq 2$\n" |
samdanbury | 6:37b6d0d56190 | 62 | // Have more than 1 byte left to align? If not skip ahead to take care of |
samdanbury | 6:37b6d0d56190 | 63 | // trailing byte. |
samdanbury | 6:37b6d0d56190 | 64 | " cmp r1, #2\n" |
samdanbury | 6:37b6d0d56190 | 65 | " blt 7$\n" |
samdanbury | 6:37b6d0d56190 | 66 | |
samdanbury | 6:37b6d0d56190 | 67 | // 4-byte align. |
samdanbury | 6:37b6d0d56190 | 68 | " ldrh r4, [r0], #2\n" |
samdanbury | 6:37b6d0d56190 | 69 | " adds r2, r2, r4\n" |
samdanbury | 6:37b6d0d56190 | 70 | " subs r1, r1, #2\n" |
samdanbury | 6:37b6d0d56190 | 71 | |
samdanbury | 6:37b6d0d56190 | 72 | // Main summing loop which sums up data 2 words at a time. |
samdanbury | 6:37b6d0d56190 | 73 | // Make sure that we have more than 7 bytes left to sum. |
samdanbury | 6:37b6d0d56190 | 74 | "2$:\n" |
samdanbury | 6:37b6d0d56190 | 75 | " cmp r1, #8\n" |
samdanbury | 6:37b6d0d56190 | 76 | " blt 3$\n" |
samdanbury | 6:37b6d0d56190 | 77 | // Sum next two words. Applying previous upper 16-bit carry to |
samdanbury | 6:37b6d0d56190 | 78 | // lower 16-bits. |
samdanbury | 6:37b6d0d56190 | 79 | " ldr r4, [r0], #4\n" |
samdanbury | 6:37b6d0d56190 | 80 | " adds r2, r4\n" |
samdanbury | 6:37b6d0d56190 | 81 | " adc r2, r2, #0\n" |
samdanbury | 6:37b6d0d56190 | 82 | " ldr r4, [r0], #4\n" |
samdanbury | 6:37b6d0d56190 | 83 | " adds r2, r4\n" |
samdanbury | 6:37b6d0d56190 | 84 | " adc r2, r2, #0\n" |
samdanbury | 6:37b6d0d56190 | 85 | " subs r1, r1, #8\n" |
samdanbury | 6:37b6d0d56190 | 86 | " b 2$\n" |
samdanbury | 6:37b6d0d56190 | 87 | |
samdanbury | 6:37b6d0d56190 | 88 | // Sum up any remaining half-words. |
samdanbury | 6:37b6d0d56190 | 89 | "3$:\n" |
samdanbury | 6:37b6d0d56190 | 90 | // Make sure that we have more than 1 byte left to sum. |
samdanbury | 6:37b6d0d56190 | 91 | " cmp r1, #2\n" |
samdanbury | 6:37b6d0d56190 | 92 | " blt 7$\n" |
samdanbury | 6:37b6d0d56190 | 93 | // Sum up next half word, continue to apply carry. |
samdanbury | 6:37b6d0d56190 | 94 | " ldrh r4, [r0], #2\n" |
samdanbury | 6:37b6d0d56190 | 95 | " adds r2, r4\n" |
samdanbury | 6:37b6d0d56190 | 96 | " adc r2, r2, #0\n" |
samdanbury | 6:37b6d0d56190 | 97 | " subs r1, r1, #2\n" |
samdanbury | 6:37b6d0d56190 | 98 | " b 3$\n" |
samdanbury | 6:37b6d0d56190 | 99 | |
samdanbury | 6:37b6d0d56190 | 100 | // Handle trailing byte, if it exists |
samdanbury | 6:37b6d0d56190 | 101 | "7$:\n" |
samdanbury | 6:37b6d0d56190 | 102 | " cbz r1, 8$\n" |
samdanbury | 6:37b6d0d56190 | 103 | " ldrb r4, [r0]\n" |
samdanbury | 6:37b6d0d56190 | 104 | " adds r2, r4\n" |
samdanbury | 6:37b6d0d56190 | 105 | " adc r2, r2, #0\n" |
samdanbury | 6:37b6d0d56190 | 106 | |
samdanbury | 6:37b6d0d56190 | 107 | // Fold 32-bit checksum into 16-bit checksum. |
samdanbury | 6:37b6d0d56190 | 108 | "8$:\n" |
samdanbury | 6:37b6d0d56190 | 109 | " ubfx r4, r2, #16, #16\n" |
samdanbury | 6:37b6d0d56190 | 110 | " ubfx r2, r2, #0, #16\n" |
samdanbury | 6:37b6d0d56190 | 111 | " adds r2, r4\n" |
samdanbury | 6:37b6d0d56190 | 112 | " ubfx r4, r2, #16, #16\n" |
samdanbury | 6:37b6d0d56190 | 113 | " ubfx r2, r2, #0, #16\n" |
samdanbury | 6:37b6d0d56190 | 114 | " adds r2, r4\n" |
samdanbury | 6:37b6d0d56190 | 115 | |
samdanbury | 6:37b6d0d56190 | 116 | // Swap bytes if started at odd address |
samdanbury | 6:37b6d0d56190 | 117 | " cbz r3, 9$\n" |
samdanbury | 6:37b6d0d56190 | 118 | " rev16 r2, r2\n" |
samdanbury | 6:37b6d0d56190 | 119 | |
samdanbury | 6:37b6d0d56190 | 120 | // Return final sum. |
samdanbury | 6:37b6d0d56190 | 121 | "9$: mov r0, r2\n" |
samdanbury | 6:37b6d0d56190 | 122 | " pop {r4, pc}\n" |
samdanbury | 6:37b6d0d56190 | 123 | ); |
samdanbury | 6:37b6d0d56190 | 124 | } |
samdanbury | 6:37b6d0d56190 | 125 | |
samdanbury | 6:37b6d0d56190 | 126 | #endif |