Add MG_ARCH_FREERTOS_LWIP

This commit is contained in:
cpq 2021-05-28 23:49:26 +01:00
parent 4c0002e28a
commit c0a8546330
6 changed files with 92 additions and 30 deletions

View File

@ -203,7 +203,7 @@ option during build time, use the `-D OPTION` compiler flag:
```sh
$ cc app0.c mongoose.c # Use defaults!
$ cc app1.c mongoose.c -D MG_ENABLE_IPV6=1 # Build with IPv6 enabled
$ cc app2.c mongoose.c -D MG_ARCH=MG_ARCH_UNIX # Set UNIX architecture
$ cc app2.c mongoose.c -D MG_ARCH=MG_ARCH_FREERTOS_LWIP # Set architecture
$ cc app3.c mongoose.c -D MG_ENABLE_FS=0 -D MG_ENABLE_LOG=0 # Multiple options
```
@ -219,6 +219,7 @@ architecture is guessed during the build, so setting it is not usually required.
|MG_ARCH_WIN32 | Windows systems |
|MG_ARCH_ESP32 | Espressif's ESP32 |
|MG_ARCH_ESP8266 | Espressif's ESP8266 |
|MG_ARCH_FREERTOS_LWIP | All systems with FreeRTOS kernel and LwIP IP stack |
|MG_ARCH_FREERTOS_TCP | All systems with FreeRTOS kernel and FreeRTOS-Plus-TCP IP stack |
|MG_ARCH_CUSTOM | A custom architecture, discussed in the next section |
@ -264,34 +265,12 @@ systems, follow the guideline outlined below:
2. Create a file called `mongoose_custom.h`, with defines and includes that
are relevant to your platform. Mongoose uses `bool` type, `MG_DIRSEP` define,
and optionally other structures like `DIR *` depending on the functionality
you have enabled - see previous section. Below is an example for FreeRTOS+LWIP:
```c
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <FreeRTOS.h>
#include <task.h>
#include <lwip/sockets.h>
#define MG_INT64_FMT "%lld"
#define MG_DIRSEP '/'
```
And this one is for bare metal and custom TCP/IP stack:
you have enabled - see previous section. Below is an example:
```c
#include <stdbool.h> // For bool
#include <MySystem.h>
#include <stdarg.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#define MG_ENABLE_SOCKET 0 // Disable BSD socket API, implement your own

View File

@ -4198,7 +4198,7 @@ void mg_usleep(unsigned long usecs) {
Sleep(usecs / 1000);
#elif MG_ARCH == MG_ARCH_ESP8266
ets_delay_us(usecs);
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
vTaskDelay(pdMS_TO_TICKS(usecs / 1000));
#else
usleep((useconds_t) usecs);
@ -4212,7 +4212,7 @@ unsigned long mg_millis(void) {
return esp_timer_get_time() / 1000;
#elif MG_ARCH == MG_ARCH_ESP8266
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#else
struct timespec ts;

View File

@ -24,6 +24,7 @@
#define MG_ARCH_ESP32 3
#define MG_ARCH_ESP8266 4
#define MG_ARCH_FREERTOS_TCP 5
#define MG_ARCH_FREERTOS_LWIP 6
#if !defined(MG_ARCH)
#if defined(__unix__) || defined(__APPLE__)
@ -66,6 +67,7 @@
#if MG_ARCH == MG_ARCH_ESP32
#include <ctype.h>
@ -119,6 +121,41 @@
#endif
#if MG_ARCH == MG_ARCH_FREERTOS_LWIP
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/time.h>
#if MG_ENABLE_FS
#include <sys/stat.h>
#endif
#include <FreeRTOS.h>
#include <task.h>
#include <lwip/sockets.h>
#define MG_INT64_FMT "%lld"
#define MG_DIRSEP '/'
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(int cnt, size_t size) {
void *p = pvPortMalloc(cnt * size);
if (p != NULL) memset(p, 0, size);
return p;
}
#define calloc(a, b) mg_calloc((a), (b))
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
#define gmtime_r(a, b) gmtime(a)
#endif // MG_ARCH == MG_ARCH_FREERTOS_LWIP
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
#include <ctype.h>

View File

@ -6,6 +6,7 @@
#define MG_ARCH_ESP32 3
#define MG_ARCH_ESP8266 4
#define MG_ARCH_FREERTOS_TCP 5
#define MG_ARCH_FREERTOS_LWIP 6
#if !defined(MG_ARCH)
#if defined(__unix__) || defined(__APPLE__)
@ -43,6 +44,7 @@
#include "arch_esp32.h"
#include "arch_esp8266.h"
#include "arch_freertos_lwip.h"
#include "arch_freertos_tcp.h"
#include "arch_unix.h"
#include "arch_win32.h"

44
src/arch_freertos_lwip.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#if MG_ARCH == MG_ARCH_FREERTOS_LWIP
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#if defined(__GNUC__)
#include <sys/time.h>
#else
typedef long suseconds_t;
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
#endif
#if MG_ENABLE_FS
#include <sys/stat.h>
#endif
#include <FreeRTOS.h>
#include <task.h>
#include <lwip/sockets.h>
#define MG_INT64_FMT "%lld"
#define MG_DIRSEP '/'
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(int cnt, size_t size) {
void *p = pvPortMalloc(cnt * size);
if (p != NULL) memset(p, 0, size);
return p;
}
#define calloc(a, b) mg_calloc((a), (b))
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
#define gmtime_r(a, b) gmtime(a)
#endif // MG_ARCH == MG_ARCH_FREERTOS_LWIP

View File

@ -323,7 +323,7 @@ void mg_usleep(unsigned long usecs) {
Sleep(usecs / 1000);
#elif MG_ARCH == MG_ARCH_ESP8266
ets_delay_us(usecs);
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
vTaskDelay(pdMS_TO_TICKS(usecs / 1000));
#else
usleep((useconds_t) usecs);
@ -337,7 +337,7 @@ unsigned long mg_millis(void) {
return esp_timer_get_time() / 1000;
#elif MG_ARCH == MG_ARCH_ESP8266
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#else
struct timespec ts;