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