Download FreeRTOS

Quality RTOS & Embedded Software

Simplifying Authenticated Cloud Connectivity for Any Device.
Designing an energy efficient and cloud-connected IoT solution with CoAP.
Introducing FreeRTOS Kernel version 11.0.0:
FreeRTOS Roadmap and Code Contribution process.
OPC-UA over TSN with FreeRTOS.

FreeRTOS_htons(), FreeRTOS_ntohs(), FreeRTOS_htonl() & FreeRTOS_ntohl()

[FreeRTOS-Plus-TCP API Reference]

uint16_t FreeRTOS_htons( uint16_t usValueToSwap );
uint16_t FreeRTOS_ntohs( uint16_t usValueToSwap );

uint32_t FreeRTOS_htonl( uint32_t ulValueToSwap );
uint32_t FreeRTOS_ntohl( uint32_t ulValueToSwap );

The Byte Order and Endian section of the Embedded Networking Basics and Glossary page provides an explanation of byte order considerations in IP networks.

The definition of ipconfigBYTE_ORDER in FreeRTOSIPConfig.h must be correct for the microcontroller on which FreeRTOS-Plus-TCP will run. If the microcontroller is big endian then ipconfigBYTE_ORDER must be set to pdFREERTOS_BIG_ENDIAN. If the microcontroller is little endian then ipconfigBYTE_ORDER must be set to pdFREERTOS_LITTLE_ENDIAN.


  • FreeRTOS_htons and FreeRTOS_ntohs() return the value of their 16-bit parameter with the high and low bytes swapped. For example, if the usValueToSwap parameter is 0x1122, then both macros return 0x2211.

  • FreeRTOS_htonl and FreeRTOS_ntohl() return the value of their 32-bit parameter with the byte order reversed. For example, if the ulValueToSwap parameter is 0x11223344, then both macros return 0x44332211.

If the microcontroller is big endian (and therefore ipconfigBYTE_ORDER set to pdFREERTOS_BIG_ENDIAN) then the byte order of the microcontroller and the byte order of the network already match, and all four byte swapping macros are defined to have no effect.

Byte swapping macros are primarily used when specifying the IP address and port number that make up a socket address.

Example usage:

The examples on the FreeRTOS_socket(), FreeRTOS_inet_addr() FreeRTOS_sendto() documentation pages demonstrate the use of FreeRTOS_htons().

The example on the FreeRTOS_recvfrom() documentation page demonstrates the use of FreeRTOS_ntohs().

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.