2015-07-10 06:01:56 -07:00
/**
* @ file eficonsole . cpp
* @ brief Console package entry point code
*
*
* @ date Nov 15 , 2012
2020-01-13 18:57:43 -08:00
* @ author Andrey Belomutskiy , ( c ) 2012 - 2020
2015-07-10 06:01:56 -07:00
*
*
* This file is part of rusEfi - see http : //rusefi.com
*
* rusEfi is free software ; you can redistribute it and / or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation ; either
* version 3 of the License , or ( at your option ) any later version .
*
* rusEfi is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License along with this program .
* If not , see < http : //www.gnu.org/licenses/>.
*/
2021-08-03 19:05:01 -07:00
# include "pch.h"
2022-09-07 12:56:45 -07:00
2015-07-10 06:01:56 -07:00
# include "eficonsole.h"
# include "console_io.h"
2023-11-14 12:45:08 -08:00
# include "mpu_util.h"
2015-07-10 06:01:56 -07:00
2021-11-15 04:02:34 -08:00
static void testCritical ( ) {
2017-03-21 11:58:14 -07:00
chDbgCheck ( 0 ) ;
2015-07-10 06:01:56 -07:00
}
2021-11-15 04:02:34 -08:00
static void myerror ( ) {
2023-04-28 21:13:13 -07:00
firmwareError ( ObdCode : : CUSTOM_ERR_TEST_ERROR , " firmwareError: %d " , getRusEfiVersion ( ) ) ;
2015-07-10 06:01:56 -07:00
}
2023-11-14 12:45:08 -08:00
static void testHardFault ( ) {
causeHardFault ( ) ;
}
2024-04-15 10:51:13 -07:00
# if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
static void printUid ( ) {
uint32_t * uid = ( ( uint32_t * ) UID_BASE ) ;
2024-04-23 15:59:46 -07:00
engine - > outputChannels . deviceUid = crc8 ( ( const uint8_t * ) uid , 12 ) ;
2024-06-10 18:31:04 -07:00
efiPrintf ( " ********************** UID=%lx:%lx:%lx crc=%d ****************************** " , uid [ 0 ] , uid [ 1 ] , uid [ 2 ] , engine - > outputChannels . deviceUid ) ;
2024-04-15 10:51:13 -07:00
engineConfiguration - > device_uid [ 0 ] = uid [ 0 ] ;
engineConfiguration - > device_uid [ 1 ] = uid [ 1 ] ;
engineConfiguration - > device_uid [ 2 ] = uid [ 2 ] ;
}
# endif
2024-06-25 06:57:42 -07:00
/*
* I was a little bit surprised that we declare __attribute__ ( ( weak ) ) before returning type in a function definition .
* In the most sources this declaration is placed either before function name or after function parentheses , see :
* - https : //gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html#Function-Attributes
* - https : //en.wikipedia.org/wiki/Weak_symbol
* But it looks like our manner of __attribute__ ( ( weak ) ) declaration works at well , and I hope it will not cause
* problems in the future .
*/
2024-07-27 09:36:35 -07:00
PUBLIC_API_WEAK void boardSayHello ( ) {
2024-06-24 10:48:17 -07:00
}
2021-11-15 04:02:34 -08:00
static void sayHello ( ) {
2024-03-12 07:58:57 -07:00
efiPrintf ( PROTOCOL_HELLO_PREFIX " rusEFI LLC (c) 2012-2024. All rights reserved. " ) ;
2024-07-03 10:34:55 -07:00
efiPrintf ( PROTOCOL_HELLO_PREFIX " rusEFI v%d@%u now=%d " , getRusEfiVersion ( ) , /*do we have a working way to print 64 bit values?!*/ ( int ) SIGNATURE_HASH , ( int ) getTimeNowMs ( ) ) ;
2021-04-21 09:53:13 -07:00
efiPrintf ( PROTOCOL_HELLO_PREFIX " Chibios Kernel: %s " , CH_KERNEL_VERSION ) ;
efiPrintf ( PROTOCOL_HELLO_PREFIX " Compiled: " __DATE__ " - " __TIME__ " " ) ;
efiPrintf ( PROTOCOL_HELLO_PREFIX " COMPILER=%s " , __VERSION__ ) ;
2024-03-15 18:04:46 -07:00
# if EFI_USE_OPENBLT
2022-01-16 16:56:49 -08:00
efiPrintf ( PROTOCOL_HELLO_PREFIX " with OPENBLT " ) ;
# endif
2020-04-22 17:20:22 -07:00
2024-06-24 10:48:17 -07:00
boardSayHello ( ) ;
2024-03-15 21:26:36 -07:00
# if EFI_PROD_CODE && ENABLE_AUTO_DETECT_HSE
2021-07-14 12:20:38 -07:00
extern float hseFrequencyMhz ;
2021-07-19 15:04:59 -07:00
extern uint8_t autoDetectedRoundedMhz ;
efiPrintf ( PROTOCOL_HELLO_PREFIX " detected HSE clock %.2f MHz PLLM = %d " , hseFrequencyMhz , autoDetectedRoundedMhz ) ;
2021-07-14 09:42:09 -07:00
# endif /* ENABLE_AUTO_DETECT_HSE */
2022-06-28 23:33:14 -07:00
efiPrintf ( " hellenBoardId=%d " , engine - > engineState . hellenBoardId ) ;
2021-08-07 09:36:39 -07:00
# if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
2024-04-15 10:51:13 -07:00
printUid ( ) ;
2021-01-14 20:34:10 -08:00
2023-10-30 04:09:35 -07:00
# if defined(STM32F4) && !defined(AT32F4XX)
2022-03-27 16:36:54 -07:00
efiPrintf ( " can read 0x20000010 %d " , ramReadProbe ( ( const char * ) 0x20000010 ) ) ;
efiPrintf ( " can read 0x20020010 %d " , ramReadProbe ( ( const char * ) 0x20020010 ) ) ;
efiPrintf ( " can read 0x20070010 %d " , ramReadProbe ( ( const char * ) 0x20070010 ) ) ;
efiPrintf ( " isStm32F42x %s " , boolToString ( isStm32F42x ( ) ) ) ;
# endif // STM32F4
2021-10-07 22:29:40 -07:00
2022-12-16 15:24:39 -08:00
# ifndef MIN_FLASH_SIZE
2021-05-07 14:04:54 -07:00
# define MIN_FLASH_SIZE 1024
2022-12-16 15:24:39 -08:00
# endif // MIN_FLASH_SIZE
2021-05-07 14:04:54 -07:00
int flashSize = TM_ID_GetFlashSize ( ) ;
if ( flashSize < MIN_FLASH_SIZE ) {
2023-10-30 04:09:35 -07:00
// todo: bug, at the moment we report 1MB on dual-bank F7
2023-08-20 19:23:44 -07:00
criticalError ( " rusEFI expected at least %dK of flash " , MIN_FLASH_SIZE ) ;
2021-05-07 14:04:54 -07:00
}
2023-10-30 04:09:35 -07:00
# ifdef AT32F4XX
int mcuRevision = DBGMCU - > SERID & 0x07 ;
int mcuSerId = ( DBGMCU - > SERID > > 8 ) & 0xff ;
const char * partNumber , * package ;
uint32_t pnFlashSize ;
int ret = at32GetMcuType ( DBGMCU - > IDCODE , & partNumber , & package , & pnFlashSize ) ;
if ( ret = = 0 ) {
2024-06-10 20:33:36 -07:00
efiPrintf ( " MCU IDCODE %s in %s with %ld KB flash " ,
2023-10-30 04:09:35 -07:00
partNumber , package , pnFlashSize ) ;
} else {
2024-06-10 20:33:36 -07:00
efiPrintf ( " MCU IDCODE unknown 0x%lx " , DBGMCU - > IDCODE ) ;
2023-10-30 04:09:35 -07:00
}
efiPrintf ( " MCU SER_ID %s rev %c " ,
( mcuSerId = = 0x0d ) ? " AT32F435 " : ( ( mcuSerId = = 0x0e ) ? " AT32F437 " : " UNKNOWN " ) ,
' A ' + mcuRevision ) ;
efiPrintf ( " MCU F_SIZE %d KB " , flashSize ) ;
2023-11-03 09:10:24 -07:00
efiPrintf ( " MCU RAM %d KB " , at32GetRamSizeKb ( ) ) ;
2023-10-30 04:09:35 -07:00
# else
# define MCU_REVISION_MASK 0xfff
int mcuRevision = DBGMCU - > IDCODE & MCU_REVISION_MASK ;
2021-10-07 21:33:04 -07:00
efiPrintf ( " MCU rev=%x flashSize=%d " , mcuRevision , flashSize ) ;
2020-04-22 17:20:22 -07:00
# endif
2023-10-30 04:09:35 -07:00
# endif
2020-04-22 17:20:22 -07:00
2023-10-30 04:09:35 -07:00
# ifdef CH_CFG_ST_FREQUENCY
efiPrintf ( " CH_CFG_ST_FREQUENCY=%d " , CH_CFG_ST_FREQUENCY ) ;
2016-02-04 12:01:45 -08:00
# endif
2015-07-10 06:01:56 -07:00
2024-02-15 10:06:48 -08:00
# ifdef ENABLE_PERF_TRACE
efiPrintf ( " ENABLE_PERF_TRACE=%d " , ENABLE_PERF_TRACE ) ;
2015-07-10 06:01:56 -07:00
# endif
# ifdef STM32_ADCCLK
2021-04-21 09:53:13 -07:00
efiPrintf ( " STM32_ADCCLK=%d " , STM32_ADCCLK ) ;
efiPrintf ( " STM32_TIMCLK1=%d " , STM32_TIMCLK1 ) ;
efiPrintf ( " STM32_TIMCLK2=%d " , STM32_TIMCLK2 ) ;
2015-07-10 06:01:56 -07:00
# endif
# ifdef STM32_PCLK1
2021-04-21 09:53:13 -07:00
efiPrintf ( " STM32_PCLK1=%d " , STM32_PCLK1 ) ;
efiPrintf ( " STM32_PCLK2=%d " , STM32_PCLK2 ) ;
2015-07-10 06:01:56 -07:00
# endif
2021-04-21 09:53:13 -07:00
efiPrintf ( " PORT_IDLE_THREAD_STACK_SIZE=%d " , PORT_IDLE_THREAD_STACK_SIZE ) ;
2015-07-10 06:01:56 -07:00
2021-04-21 09:53:13 -07:00
efiPrintf ( " CH_DBG_ENABLE_ASSERTS=%d " , CH_DBG_ENABLE_ASSERTS ) ;
2016-02-04 12:01:45 -08:00
# ifdef CH_DBG_ENABLED
2021-04-21 09:53:13 -07:00
efiPrintf ( " CH_DBG_ENABLED=%d " , CH_DBG_ENABLED ) ;
2016-02-04 12:01:45 -08:00
# endif
2021-04-21 09:53:13 -07:00
efiPrintf ( " CH_DBG_SYSTEM_STATE_CHECK=%d " , CH_DBG_SYSTEM_STATE_CHECK ) ;
efiPrintf ( " CH_DBG_ENABLE_STACK_CHECK=%d " , CH_DBG_ENABLE_STACK_CHECK ) ;
2015-07-10 06:01:56 -07:00
2019-12-03 22:11:10 -08:00
# ifdef EFI_LOGIC_ANALYZER
2021-04-21 09:53:13 -07:00
efiPrintf ( " EFI_LOGIC_ANALYZER=%d " , EFI_LOGIC_ANALYZER ) ;
2015-07-10 06:01:56 -07:00
# endif
# ifdef EFI_TUNER_STUDIO
2021-04-21 09:53:13 -07:00
efiPrintf ( " EFI_TUNER_STUDIO=%d " , EFI_TUNER_STUDIO ) ;
2015-07-10 06:01:56 -07:00
# else
2021-04-21 09:53:13 -07:00
efiPrintf ( " EFI_TUNER_STUDIO=%d " , 0 ) ;
2015-07-10 06:01:56 -07:00
# endif
2019-04-12 19:10:57 -07:00
# if defined(EFI_SHAFT_POSITION_INPUT)
2021-04-21 09:53:13 -07:00
efiPrintf ( " EFI_SHAFT_POSITION_INPUT=%d " , EFI_SHAFT_POSITION_INPUT ) ;
2015-07-10 06:01:56 -07:00
# endif
# ifdef EFI_INTERNAL_ADC
2021-04-21 09:53:13 -07:00
efiPrintf ( " EFI_INTERNAL_ADC=%d " , EFI_INTERNAL_ADC ) ;
2015-07-10 06:01:56 -07:00
# endif
/**
* Time to finish output . This is needed to avoid mix - up of this methods output and console command confirmation
2024-03-12 07:58:57 -07:00
* this code here dates back to 2015. today in 2024 I have no idea what it does : (
2015-07-10 06:01:56 -07:00
*/
chThdSleepMilliseconds ( 5 ) ;
}
2019-11-06 16:53:09 -08:00
# if CH_DBG_THREADS_PROFILING && CH_DBG_FILL_THREADS
2020-08-01 18:31:23 -07:00
int CountFreeStackSpace ( const void * wabase ) {
2019-06-13 05:31:25 -07:00
const uint8_t * stackBase = reinterpret_cast < const uint8_t * > ( wabase ) ;
const uint8_t * stackUsage = stackBase ;
2020-08-01 18:31:23 -07:00
// thread stacks are filled with CH_DBG_STACK_FILL_VALUE
2019-06-13 05:31:25 -07:00
// find out where that ends - that's the last thing we needed on the stack
2020-08-01 18:31:23 -07:00
while ( * stackUsage = = CH_DBG_STACK_FILL_VALUE ) {
2019-06-13 05:31:25 -07:00
stackUsage + + ;
}
2020-08-01 18:31:23 -07:00
return ( int ) ( stackUsage - stackBase ) ;
2019-06-13 05:31:25 -07:00
}
2019-11-06 16:53:09 -08:00
# endif
2019-06-13 05:31:25 -07:00
2015-07-10 06:01:56 -07:00
/**
2019-06-13 05:31:25 -07:00
* This methods prints all threads , their stack usage , and their total times
2015-07-10 06:01:56 -07:00
*/
2021-11-15 04:02:34 -08:00
static void cmd_threads ( ) {
2019-06-13 05:31:25 -07:00
# if CH_DBG_THREADS_PROFILING && CH_DBG_FILL_THREADS
thread_t * tp = chRegFirstThread ( ) ;
2021-04-21 09:53:13 -07:00
efiPrintf ( " name \t wabase \t time \t free stack " ) ;
2019-06-13 05:31:25 -07:00
2020-08-01 18:31:23 -07:00
while ( tp ) {
int freeBytes = CountFreeStackSpace ( tp - > wabase ) ;
2024-06-10 00:51:14 -07:00
efiPrintf ( " %s \t %08x \t %lu \t %d " , tp - > name , ( unsigned int ) tp - > wabase , tp - > time , freeBytes ) ;
2019-06-13 05:31:25 -07:00
2021-10-04 05:19:58 -07:00
if ( freeBytes < 100 ) {
2023-08-20 19:23:44 -07:00
criticalError ( " Ran out of stack on thread %s, %d bytes remain " , tp - > name , freeBytes ) ;
2021-10-04 05:19:58 -07:00
}
2019-06-13 05:31:25 -07:00
tp = chRegNextThread ( tp ) ;
}
2020-08-01 18:31:23 -07:00
int isrSpace = CountFreeStackSpace ( reinterpret_cast < void * > ( 0x20000000 ) ) ;
2021-04-21 09:53:13 -07:00
efiPrintf ( " isr \t 0 \t 0 \t %d " , isrSpace ) ;
2019-06-13 05:31:25 -07:00
# else // CH_DBG_THREADS_PROFILING && CH_DBG_FILL_THREADS
2021-04-21 09:53:13 -07:00
efiPrintf ( " CH_DBG_THREADS_PROFILING && CH_DBG_FILL_THREADS is not enabled " ) ;
2019-06-13 05:31:25 -07:00
2015-07-10 06:01:56 -07:00
# endif
}
2023-08-08 02:30:53 -07:00
/**
* @ brief This is just a test function
*/
static void echo ( int value ) {
efiPrintf ( " got value: %d " , value ) ;
}
void checkStackAndHandleConsoleLine ( char * line ) {
assertStackVoid ( " console " , ObdCode : : STACK_USAGE_MISC , EXPECTED_REMAINING_STACK ) ;
handleConsoleLine ( line ) ;
}
2023-08-08 15:58:16 -07:00
void onCliCaseError ( const char * token ) {
firmwareError ( ObdCode : : CUSTOM_ERR_COMMAND_LOWER_CASE_EXPECTED , " lowerCase expected [%s] " , token ) ;
}
void onCliDuplicateError ( const char * token ) {
firmwareError ( ObdCode : : CUSTOM_SAME_TWICE , " Same action twice [%s] " , token ) ;
}
2023-08-08 20:02:20 -07:00
void onCliOverflowError ( ) {
firmwareError ( ObdCode : : CUSTOM_CONSOLE_TOO_MANY , " Too many console actions " ) ;
}
2021-04-20 11:09:41 -07:00
void initializeConsole ( ) {
initConsoleLogic ( ) ;
2015-07-10 06:01:56 -07:00
2021-04-20 11:09:41 -07:00
startConsole ( & handleConsoleLine ) ;
2015-07-10 06:01:56 -07:00
2024-04-15 10:51:13 -07:00
# if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
addConsoleAction ( " uid " , printUid ) ;
# endif
2015-07-10 06:01:56 -07:00
sayHello ( ) ;
2021-02-19 04:58:52 -08:00
addConsoleAction ( " test " , [ ] ( ) { /* do nothing */ } ) ;
2023-08-08 02:30:53 -07:00
addConsoleActionI ( " echo " , echo ) ;
2015-07-10 06:01:56 -07:00
addConsoleAction ( " hello " , sayHello ) ;
2024-07-30 08:12:01 -07:00
# if EFI_USE_OPENBLT
addConsoleAction ( " show_blt_version " , [ ] ( ) {
2024-07-30 09:32:13 -07:00
uint32_t bltBinVersion = getOpenBltVersion ( ) ;
efiPrintf ( " ********************** blt=%lx %s version " , bltBinVersion , bltBinVersion = = BLT_CURRENT_VERSION ? " CURRENT " : " UNEXPECTED " ) ;
2024-07-30 08:12:01 -07:00
} ) ;
# endif
2015-07-10 06:01:56 -07:00
# if EFI_HAS_RESET
addConsoleAction ( " reset " , scheduleReset ) ;
# endif
2020-03-28 18:28:32 -07:00
addConsoleAction ( " critical " , testCritical ) ;
2015-07-10 06:01:56 -07:00
addConsoleAction ( " error " , myerror ) ;
2023-11-14 12:45:08 -08:00
addConsoleAction ( " hard_fault " , testHardFault ) ;
2015-07-10 06:01:56 -07:00
addConsoleAction ( " threadsinfo " , cmd_threads ) ;
2023-11-24 15:16:13 -08:00
2023-12-14 07:54:21 -08:00
# if HAL_USE_WDG
2023-11-24 15:16:13 -08:00
addConsoleActionI ( " set_watchdog_timeout " , startWatchdog ) ;
addConsoleActionI ( " set_watchdog_reset " , setWatchdogResetPeriod ) ;
2023-12-14 07:54:21 -08:00
# endif
2015-07-10 06:01:56 -07:00
}