Skip to content

Commit

Permalink
Revise system headers / enforce c11 (#1695)
Browse files Browse the repository at this point in the history
!!! Breaking Change !!!
----
All Sming based applications used to have a user_config.h file inside of the application `include/` directory. If your application user_config.h is the same as the one coming from the Sming then make sure to delete it .

If you don't want to delete it most probably the compilation will break with similar error message:
```
include/user_config.h:26:40: fatal error: espinc/c_types_compatible.h: No such file or directory
  #include <espinc/c_types_compatible.h>
```

Replace ` #include <espinc/c_types_compatible.h>` with `include <c_types.h>` to fix this issue.
-----

This PR includes the following changes:
* Standardise data types
* Use `uint32_t` in preference to `uint32`, etc.
* Arduino libraries use a mixture so still need to maintain definitions for those
* Build `C` files within modules to c11 standard (previously defaulted to `gnu90`)
* Move `espinc\c_types_compatible.h` -> `c_types.h`
* Simplify and tidy Esp8266 system headers
* Rename `espinc/c_types_compatible.h` as `c_types.h`, revise to use `stdint` and `stdbool` definitions
* Place location for `c_types.h` and `mem.h` ahead of SDK version in makefiles
* Move system definitions from `user_config.h` into `esp_systemapi.h` (exclude LWIP)
* Don't #include `user_config.h` from `esp_systemapi.h`
* Move missing memory API definitions out of `esp_systemapi.h` and into `mem.h`
* Simplify other headers
* Fix samples - use <esp_systemapi.h> to ensure correct c_types.h gets used
* Move attribute definitions and macros out of ctypes.h
* Add `WEAK_ATTR` macro to allow selective weak symbol support
* Not a C/C++ standard feature and not consistently applied on all platforms, so avoid where possible.
  • Loading branch information
mikee47 authored and slaff committed May 21, 2019
1 parent 40ebc71 commit b68169b
Show file tree
Hide file tree
Showing 38 changed files with 267 additions and 321 deletions.
5 changes: 2 additions & 3 deletions Sming/Arch/Esp8266/Components/axtls-8266/axtls-8266.patch
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
diff -Nuar a/replacements/libc.c b/replacements/libc.c
--- a/replacements/libc.c 1970-01-01 01:00:00.000000000 +0100
+++ b/replacements/libc.c 2016-11-21 11:03:47.152184514 +0100
@@ -0,0 +1,29 @@
@@ -0,0 +1,28 @@
+/*
+ libc_replacements.c - replaces libc functions with functions
+ from Espressif SDK
Expand All @@ -21,8 +21,7 @@ diff -Nuar a/replacements/libc.c b/replacements/libc.c
+ Modified 03 April 2015 by Markus Sattler
+ */
+
+#include <stdint.h>
+#include "espinc/c_types_compatible.h"
+#include <c_types.h>
+#include <stdarg.h>
+
+extern int ets_putc(int);
Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Esp8266/Components/esp8266/crash_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern void __custom_crash_callback( struct rst_info * rst_info, uint32_t stack,
extern void custom_crash_callback( struct rst_info * rst_info, uint32_t stack, uint32_t stack_end ) __attribute__ ((weak, alias("__custom_crash_callback")));

void __wrap_system_restart_local() {
register uint32_t sp_reg asm("a1");
register uint32_t sp_reg __asm__("a1");
uint32_t sp = sp_reg;

struct rst_info rst_info = {0};
Expand Down
3 changes: 3 additions & 0 deletions Sming/Arch/Esp8266/Components/esp8266/esp_cplusplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "include/esp_cplusplus.h"
#include <stdlib.h>

extern void (*__init_array_start)();
extern void (*__init_array_end)();

////////////////////////////////////////////////////////////////////////

// Just do it! :)
Expand Down
40 changes: 40 additions & 0 deletions Sming/Arch/Esp8266/Components/esp8266/include/c_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2010 - 2011 Espressif System
*
*/

// Overrides c_types.h for all SDK versions

#ifndef _C_TYPES_H_
#define _C_TYPES_H_

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#define BOOL bool
#define TRUE true
#define FALSE false

typedef uint8_t uint8;
typedef uint8_t u8;
typedef int8_t sint8;
typedef int8_t int8;
typedef int8_t s8;
typedef uint16_t uint16;
typedef uint16_t u16;
typedef int16_t sint16;
typedef int16_t s16;
typedef uint32_t uint32;
typedef unsigned int u_int;
typedef uint32_t u32;
typedef int32_t sint32;
typedef int32_t s32;
typedef int32_t int32;
typedef int64_t sint64;
typedef uint64_t uint64;
typedef uint64_t u64;
typedef float real32;
typedef double real64;

#endif /* _C_TYPES_H_ */
25 changes: 25 additions & 0 deletions Sming/Arch/Esp8266/Components/esp8266/include/esp_attr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ESP8266 attribute definitions (previously in c_types.h)
#pragma once

#define IRAM_ATTR __attribute__((section(".iram.text")))
#define STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed))
#define STORE_ATTR __attribute__((aligned(4)))

#define DMEM_ATTR __attribute__((section(".bss")))
#define SHMEM_ATTR

#ifdef ICACHE_FLASH
#define ICACHE_FLASH_ATTR __attribute__((section(".irom0.text")))
#define ICACHE_RODATA_ATTR __attribute__((section(".irom.text")))
#else
#define ICACHE_FLASH_ATTR
#define ICACHE_RODATA_ATTR
#endif

#define STORE_ATTR __attribute__((aligned(4)))

#ifdef ENABLE_GDB
#define GDB_IRAM_ATTR IRAM_ATTR
#else
#define GDB_IRAM_ATTR
#endif
11 changes: 0 additions & 11 deletions Sming/Arch/Esp8266/Components/esp8266/include/esp_cplusplus.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,4 @@

#pragma once

#ifdef __cplusplus
extern "C" {
#endif
#include "esp_systemapi.h"
#ifdef __cplusplus
}
#endif

extern void (*__init_array_start)(void);
extern void (*__init_array_end)(void);

void cpp_core_initialize();
65 changes: 35 additions & 30 deletions Sming/Arch/Esp8266/Components/esp8266/include/esp_systemapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,52 @@

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

// ESP SDK config
#define LWIP_OPEN_SRC
#define USE_US_TIMER

// Default types
#define __CORRECT_ISO_CPP_STDLIB_H_PROTO
#include <limits.h>
#include "c_types.h"

// Remove buggy espconn
#define _NO_ESPCON_

/* Omitted from early SDK versions, in c_types.h in later versions (note: we don't use the SDK c_types.h) */
typedef enum {
OK = 0,
FAIL,
PENDING,
BUSY,
CANCEL,
} STATUS;

#include <sming_attr.h>
#include "esp_attr.h"
#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <os_type.h>
#include <user_interface.h>
#include <spi_flash.h>
#include <espconn.h>
#include "espinc/spi_register.h"

#include <stdarg.h>

#include <user_config.h>

#include "m_printf.h"
#include "debug_progmem.h"
#include "stringutil.h"

#define __ESP8266_EX__ // System definition ESP8266 SOC

#define IRAM_ATTR __attribute__((section(".iram.text")))
#define __forceinline __attribute__((always_inline)) inline
#define STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed))
#define STORE_ATTR __attribute__((aligned(4)))

/*
* Use this definition in the cases where a function or a variable is meant to be possibly unused. GCC will not produce a warning for it.
*/
#define SMING_UNUSED __attribute__((unused))
#define LOCAL static

/*
* Flags a compiler warning when Sming framework methods, functions or types are changed
*/
#define SMING_DEPRECATED __attribute__((deprecated))
#define BIT(nr) (1UL << (nr))

#ifdef ENABLE_GDB
#define GDB_IRAM_ATTR IRAM_ATTR
#else
#define GDB_IRAM_ATTR
#endif
#define REG_SET_BIT(_r, _b) (*(volatile uint32_t*)(_r) |= (_b))
#define REG_CLR_BIT(_r, _b) (*(volatile uint32_t*)(_r) &= ~(_b))

#undef assert
#ifdef SMING_RELEASE
Expand Down Expand Up @@ -100,12 +107,6 @@ extern int os_printf_plus(const char *format, ...) __attribute__ ((format (prin
extern int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
extern int ets_vsnprintf(char * s, size_t n, const char * format, va_list arg) __attribute__ ((format (printf, 3, 0)));

extern void *pvPortMalloc(size_t xWantedSize, const char *file, uint32 line);
extern void *pvPortZalloc(size_t xWantedSize, const char *file, uint32 line);
extern void pvPortFree(void *ptr);
extern void vPortFree(void *ptr, const char *file, uint32 line);
extern void *vPortMalloc(size_t xWantedSize);

extern void ets_intr_lock();
extern void ets_intr_unlock();

Expand All @@ -123,3 +124,7 @@ extern void xt_enable_interrupts();

extern void ets_isr_mask(unsigned intr);
extern void ets_isr_unmask(unsigned intr);

#ifdef __cplusplus
}
#endif

This file was deleted.

23 changes: 23 additions & 0 deletions Sming/Arch/Esp8266/Components/esp8266/include/mem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#ifndef SDK_INTERNAL

// Missing from early SDK versions
extern void *pvPortMalloc(size_t xWantedSize, const char *file, uint32 line);
extern void *pvPortZalloc(size_t xWantedSize, const char *file, uint32 line);
extern void pvPortFree(void *ptr);
extern void vPortFree(void *ptr, const char *file, uint32 line);
extern void *vPortMalloc(size_t xWantedSize);

#endif

#include_next <mem.h>

#ifdef __cplusplus
}
#endif

11 changes: 6 additions & 5 deletions Sming/Arch/Esp8266/Components/esp8266/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
#include "Platform/System.h"
#include <driver/uart.h>
#include <gdb/gdb_hooks.h>
#include <esp_cplusplus.h>

extern void init();

extern "C" void __attribute__((weak)) user_init(void)
extern "C" void WEAK_ATTR user_init(void)
{
// We want high resolution timing - see HardwareTimer class
system_timer_reinit();
Expand All @@ -40,7 +41,7 @@ extern "C" void __attribute__((weak)) user_init(void)
}

// For compatibility with SDK v1.1
extern "C" void __attribute__((weak)) user_rf_pre_init(void)
extern "C" void WEAK_ATTR user_rf_pre_init(void)
{
// RTC startup fix, author pvvx
volatile uint32 * ptr_reg_rtc_ram = (volatile uint32 *)0x60001000;
Expand All @@ -50,7 +51,7 @@ extern "C" void __attribute__((weak)) user_rf_pre_init(void)
}
}

extern "C" uint32 ICACHE_FLASH_ATTR __attribute__((weak)) user_rf_cal_sector_set(void)
extern "C" uint32 ICACHE_FLASH_ATTR WEAK_ATTR user_rf_cal_sector_set(void)
{
enum flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
Expand Down Expand Up @@ -88,7 +89,7 @@ extern "C" uint32 ICACHE_FLASH_ATTR __attribute__((weak)) user_rf_cal_sector_se

#if defined(ESP_SDK_VERSION_MAJOR) and ESP_SDK_VERSION_MAJOR>=3

extern "C" void ICACHE_FLASH_ATTR __attribute__((weak)) user_pre_init(void)
extern "C" void ICACHE_FLASH_ATTR WEAK_ATTR user_pre_init(void)
{
const uint32_t MAX_PROGRAM_SECTORS = 0x100000 / SPI_FLASH_SEC_SIZE; // 1MB addressable

Expand Down Expand Up @@ -128,7 +129,7 @@ extern "C" void ICACHE_FLASH_ATTR __attribute__((weak)) user_pre_init(void)
#endif /* defined(ESP_SDK_VERSION_MAJOR) and ESP_SDK_VERSION_MAJOR>=3 */

namespace std {
void __attribute__((weak)) __throw_bad_function_call()
void WEAK_ATTR __throw_bad_function_call()
{
while(1);
};
Expand Down
Loading

0 comments on commit b68169b

Please sign in to comment.