Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP Server Demo does not work with compiler optimisations enabled #1

Open
edpgcooper opened this issue Feb 16, 2022 · 1 comment
Open

Comments

@edpgcooper
Copy link

edpgcooper commented Feb 16, 2022

Setting the XC8 optimisation level above level 1 seems to prevent the stack from operating correctly. I've seen this in my custom MCC project and in this demo project, I've tried various XC8 compilers from 2.20 in various modes. What's causing this? I would like to use optimised code as my ISRs take much longer to service without.

This problem probably also affects the other two demo's though I've not had the chance to test this.

@edpgcooper edpgcooper changed the title TCP Server Demo will not work with compiler optimisation enabled. TCP Server Demo does not work with compiler optimisations enabled Feb 22, 2022
@edpgcooper
Copy link
Author

edpgcooper commented Mar 10, 2022

The issue is with the byteReverse32(a) macro in TCPIPLibrary/network.h

For whatever reason it doesn’t produce the correct result with the compiler optimistion, it would byte reverse 192.168.11.246 into 246.11.0.192 this would then cause the ARP packets to be corrupted so the system was unable to resolve IP addresses – and therefore communicate over Ethernet.

I decided to get rid of the macros and just use standard functions instead, I inserted the following functions into Network.c,

uint32_t byteReverse32(uint32_t value) 
{
    return (((value & (uint32_t)0x000000FF) << 24) |
            ((value & (uint32_t)0x0000FF00) <<  8) |
            ((value & (uint32_t)0x00FF0000) >>  8) |
            ((value & (uint32_t)0xFF000000) >> 24));
}

uint16_t byteSwap16(uint16_t value)
{
    return (((value & (uint16_t)0x00FF) << 8) |
            ((value & (uint16_t)0xFF00) >> 8));
}

uint32_t byteReverse24(uint32_t value)
{
    return (((value & (uint32_t)0x000000FF) << 16) |
            ((value & (uint32_t)0x0000FF00)) |
            ((value & (uint32_t)0x00FF0000) >> 16));
}

Then updated network.h as follows

uint32_t byteReverse32(uint32_t value);
uint16_t byteSwap16(uint16_t value);
uint32_t byteReverse24(uint32_t value);

// host to network & network to host macros
#ifndef htons
#define htons(a) byteSwap16(a)
#endif
#ifndef ntohs
#define ntohs(a) byteSwap16(a)
#endif
#ifndef htonl
#define htonl(a) byteReverse32(a)
#endif
#ifndef ntohl
#define ntohl(a) byteReverse32(a)
#endif

#define convert_hton24(a)  byteReverse24(a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant