Commit a463d764 authored by Johny Mattsson's avatar Johny Mattsson
Browse files

WIP ESP32 IDF port.

Currently the UART driver break boot (or at least output).
parent b4f06819
#include <stdint.h>
#include <stdbool.h>
#include <rom.h>
static char space[]=" ";
static char blank[]=".. ";
static char brk[] = " | ";
static char addrfmt[]="\n%08x: ";
static char fmt[]="%02x ";
static char eol[]="\n";
static char onechar[]="%c";
void hexdump (char *src, uint32_t len, uint32_t base_addr)
{
bool nl = true;
char *p = (char *)base_addr;
while (len)
{
uint32_t line_addr = (unsigned)p & ~0xf;
union {
uint32_t dw[4];
char b[16];
} cache;
for (int i = 0; i < 4; ++i)
cache.dw[i] = ((uint32_t*)(p + (uint32_t)src - base_addr))[i];
if (nl)
{
ets_printf(addrfmt, line_addr);
nl = false;
}
uint32_t skips = ((unsigned)p > line_addr) ? ((unsigned)p - line_addr) : 0;
uint32_t skipe = (len < 16) ? 16-len : 16;
for (int i = 0; i < 16; ++i)
{
if (i < skips || i >= skipe)
ets_printf(blank);
else
{
ets_printf(fmt, cache.b[i]);
++p;
--len;
}
if (i == 7)
ets_printf(space);
}
ets_printf (brk);
for (int i = 0; i < 16; ++i)
{
char c = ' ';
if (i >= skips && i < skipe)
{
if (cache.b[i] >= 0x20 && cache.b[i] < 0x80)
c = cache.b[i];
else
c = '.';
}
ets_printf(onechar, c);
if (i == 7)
ets_printf(space);
}
nl = true;
}
ets_printf(eol);
}
#ifndef _RTOS_DBG_H_
#define _RTOS_DBG_H_
#include <stdint.h>
#define rtos_dbg_here() do{rtos_dbg_task_print(__FILE__,__LINE__);} while(0)
#define rtos_dbg_stack_here() do{rtos_dbg_stack_print(__FILE__,__LINE__);} while(0)
void rtos_dbg_task_print (const char *file, uint32_t line);
void rtos_dbg_stack_print (const char *file, uint32_t line);
#endif
#include "rtos_dbg.h"
#include <freertos/FreeRTOSConfig.h>
#include <stdio.h>
extern struct {
uint32_t *pxTopOfStack;
uint32_t x[5];
uint32_t y[5];
unsigned uxPriority;
uint32_t *pxStack;
signed char pcTaskName[configMAX_TASK_NAME_LEN];
} *pxCurrentTCB;
void rtos_dbg_task_print (const char *file, uint32_t line)
{
printf(">>dbg: %s:%d in RTOS task \"%s\": prio %d\n",
file,
line,
pxCurrentTCB->pcTaskName,
pxCurrentTCB->uxPriority
);
}
void rtos_dbg_stack_print (const char *file, uint32_t line)
{
uint32_t fill = 0xa5a5a5a5u;
uint32_t nwords = 0;
uint32_t *p = (uint32_t*)pxCurrentTCB->pxStack;
for (;p < pxCurrentTCB->pxTopOfStack && *p == fill; ++p)
++nwords;
printf(">>dbg: %s:%d in RTOS task \"%s\": %u stack untouched\n",
file, line, pxCurrentTCB->pcTaskName, nwords * 4);
}
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_main.c
*
* Description: entry file of user application
*
* Modification history:
* 2014/1/1, v1.0 create this file.
*******************************************************************************/
#include "lua.h"
#include "platform.h"
#include <string.h>
#include <stdlib.h>
#include "flash_fs.h"
#include "flash_api.h"
#include "sdkconfig.h"
#include "ets_sys.h"
#include "driver/uart.h"
#include "task/task.h"
#include "sections.h"
#include "mem.h"
#include "freertos/task.h"
#ifdef LUA_USE_MODULES_RTCTIME
#include "rtc/rtctime.h"
#endif
#define SIG_LUA 0
#define SIG_UARTINPUT 1
#ifdef __ESP32__
void system_soft_wdt_feed (void)
{
// FIXME this shouldn't go here, and it needs to tickle hardware watchdog
}
#endif
extern void call_user_start (void);
#if defined(__ESP8266__)
/* Note: the trampoline *must* be explicitly put into the .text segment, since
* by the time it is invoked the irom has not yet been mapped. This naturally
* also goes for anything the trampoline itself calls.
*/
void TEXT_SECTION_ATTR user_start_trampoline (void)
{
#ifdef LUA_USE_MODULES_RTCTIME
// Note: Keep this as close to call_user_start() as possible, since it
// is where the cpu clock actually gets bumped to 80MHz.
rtctime_early_startup ();
#endif
/* The first thing call_user_start() does is switch the interrupt vector
* base, which enables our 8/16bit load handler on the ESP8266. */
call_user_start ();
}
#endif
// +================== New task interface ==================+
static void start_lua(task_param_t param, task_prio_t prio) {
(void)param;
(void)prio;
char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
NODE_DBG("Task task_lua started.\n");
lua_main( 2, lua_argv );
}
static void handle_input(task_param_t flag, task_prio_t priority) {
(void)priority;
lua_handle_input (flag);
}
static task_handle_t input_task;
task_handle_t user_get_input_sig(void) {
return input_task;
}
bool user_process_input(bool force) {
return task_post_low(input_task, force);
}
void nodemcu_init(void)
{
NODE_ERR("\n");
// Initialize platform first for lua modules.
if( platform_init() != PLATFORM_OK )
{
// This should never happen
NODE_DBG("Can not init platform for modules.\n");
return;
}
// FIXME: this breaks horribly on the ESP32 at the moment; possibly all flash writes?
#if defined(FLASH_SAFE_API) && defined(__ESP8266__)
if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
NODE_ERR("Self adjust flash size.\n");
// Fit hardware real flash size.
flash_rom_set_size_byte(flash_safe_get_size_byte());
if( !fs_format() )
{
NODE_ERR( "\ni*** ERROR ***: unable to format. FS might be compromised.\n" );
NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
}
else{
NODE_ERR( "format done.\n" );
}
fs_unmount(); // mounted by format.
// Reboot to get SDK to use (or write) init data at new location
system_restart ();
// Don't post the start_lua task, we're about to reboot...
return;
}
#endif // defined(FLASH_SAFE_API)
#if defined ( BUILD_SPIFFS )
fs_mount();
// test_spiffs();
#endif
task_post_low(task_get_id(start_lua), 0);
}
static void nodemcu_main (void *param)
{
(void)param;
nodemcu_init ();
task_pump_messages ();
__builtin_unreachable ();
}
void user_init (void)
{
#ifdef LUA_USE_MODULES_RTCTIME
rtctime_late_startup ();
#endif
input_task = task_get_id (handle_input);
UART_ConfigTypeDef cfg;
cfg.baud_rate = CONFIG_CONSOLE_BIT_RATE;
cfg.data_bits = UART_WordLength_8b;
cfg.parity = USART_Parity_None;
cfg.stop_bits = USART_StopBits_1;
cfg.flow_ctrl = USART_HardwareFlowControl_None;
cfg.UART_RxFlowThresh = 120;
cfg.UART_InverseMask = UART_None_Inverse;
uart_init_uart0_console (&cfg, input_task);
#ifndef NODE_DEBUG
# ifndef __ESP32__
system_set_os_print(0);
# endif
#endif
// FIXME! Max supported RTOS stack size is 512 words (2k bytes), but
// NodeMCU currently uses more than that. The game is on to find these
// culprits, but this gcc doesn't do the -fstack-usage option :(
xTaskCreate (
nodemcu_main, "nodemcu", 1536, 0, configTIMER_TASK_PRIORITY +1, NULL);
}
#ifndef _SDK_OVERRIDES_C_TYPES_H
#define _SDK_OVERRIDES_C_TYPES_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#endif
#ifndef _SDK_OVERRIDES_EAGLE_SOC_
#define _SDK_OVERRIDES_EAGLE_SOC_
// TODO: Discover what this is now called in ESP-IDF...
#define ICACHE_RAM_ATTR
#endif
#ifndef _SDK_OVERRIDES_ETS_SYS_H_ #ifndef _SDK_OVERRIDES_ETS_SYS_H_
#define _SDK_OVERRIDES_ETS_SYS_H_ #define _SDK_OVERRIDES_ETS_SYS_H_
#include_next <ets_sys.h> #include_next <rom/ets_sys.h>
#include <freertos/xtensa_api.h> #include <freertos/xtensa_api.h>
#define ETS_UART_INTR_ATTACH(fn,arg) xt_set_interrupt_handler(ETS_UART_INUM, fn, arg) #define ETS_UART_INTR_ATTACH(fn,arg) xt_set_interrupt_handler(ETS_UART_INUM, fn, arg)
......
#ifndef _SDK_OVERRIDES_USER_INTERFACE_H_
#define _SDK_OVERRIDES_USER_INTERFACE_H_
#endif
Subproject commit b6b83c33bec18746657ae7cc35b7cff7d1abba6a
Subproject commit 8a83c7b706eecf80236a441b15bc2f80799ced78
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment