Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
[code] Working on shell
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoussin committed May 1, 2017
1 parent b114f8c commit 0467b07
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 41 deletions.
1 change: 1 addition & 0 deletions code/Motolink.files
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ app/prot_obd.h
app/prot_yamaha.h
app/sensors.c
app/sensors.h
app/shell_cmds.c
app/tables.c
app/tables.h
binsize.py
Expand Down
3 changes: 2 additions & 1 deletion code/app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ CSRC = $(STARTUPSRC) \
innovate.c \
opamp.c \
tables.c \
canbus.c
canbus.c \
shell_cmds.c

# C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
Expand Down
2 changes: 1 addition & 1 deletion code/app/chconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
* @note Requires @p CH_CFG_USE_WAITEXIT.
* @note Requires @p CH_CFG_USE_HEAP and/or @p CH_CFG_USE_MEMPOOLS.
*/
#define CH_CFG_USE_DYNAMIC TRUE
#define CH_CFG_USE_DYNAMIC FALSE

/** @} */

Expand Down
56 changes: 17 additions & 39 deletions code/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
/* Macros */
/*===========================================================================*/

#define SHELL_SD SDU1
#define STDOUT_SD SHELL_SD
#define STDIN_SD SHELL_SD
#define SHELL_SD SDU1

/* Check if tp was the previous thread */
#define RUNNING(tp) (uint16_t)((tp == chThdGetSelfX()->p_next) << 15)
Expand All @@ -45,16 +43,16 @@
/* Thread pointers. */
/*===========================================================================*/

uint16_t irq_pct = 0;
static bool dbg_can = false;
const char *irq_name = "Interrupts";
thread_t *shelltp = NULL;

/*===========================================================================*/
/* Structs / Vars */
/*===========================================================================*/

uint16_t irq_pct = 0;
const char *irq_name = "Interrupts";
static virtual_timer_t vt_freqin;
extern bool dbg_can;
extern const ShellCommand sh_commands[];

/*===========================================================================*/
/* CallBacks */
Expand All @@ -79,34 +77,9 @@ CCM_FUNC void freqinVTHandler(void *arg)
chSysUnlockFromISR();
}

static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) {
static const char *states[] = {CH_STATE_NAMES};
thread_t *tp;

(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: threads\r\n");
return;
}
chprintf(chp, " addr stack prio refs state\r\n");
tp = chRegFirstThread();
do {
chprintf(chp, "%08lx %08lx %4lu %4lu %9s %lu\r\n",
(uint32_t)tp, (uint32_t)tp->p_ctx.r13,
(uint32_t)tp->p_prio, (uint32_t)(tp->p_refs - 1),
states[tp->p_state]);
tp = chRegNextThread(tp);
} while (tp != NULL);
}

static const ShellCommand commands[] = {
{"threads", cmd_threads},
{NULL, NULL}
};

static const ShellConfig shell_cfg1 = {
(BaseSequentialStream *)&SDU1,
commands
(BaseSequentialStream *)&SHELL_SD,
sh_commands
};

/*===========================================================================*/
Expand Down Expand Up @@ -273,13 +246,15 @@ CCM_FUNC static THD_FUNCTION(ThreadBDU, arg)
* USB Serial thread.
*/
THD_WORKING_AREA(waThreadSDU, 256);
THD_WORKING_AREA(waThreadShell, 512);
CCM_FUNC static THD_FUNCTION(ThreadSDU, arg)
{
(void)arg;
uint8_t in_buffer[SERIAL_BUFFERS_SIZE];
uint8_t out_buffer[SERIAL_BUFFERS_SIZE];
//uint8_t buffer_check[SERIAL_BUFFERS_SIZE/2];
size_t in, out;
thread_t* th_shell = NULL;
chRegSetThreadName("SDU");

while(USBD1.state != USB_READY) chThdSleepMilliseconds(10);
Expand All @@ -288,12 +263,17 @@ CCM_FUNC static THD_FUNCTION(ThreadSDU, arg)

// Enable K-line transceiver
palSetPad(PORT_KLINE_CS, PAD_KLINE_CS);

shellInit();

while (TRUE) {

while (settings.serialMode == SERIAL_MODE_SHELL && shelltp != NULL) {
chThdWait(shelltp);
if (settings.serialMode == SERIAL_MODE_SHELL) {
if (th_shell == NULL || chThdTerminatedX(th_shell)) {
th_shell = shellCreateStatic(&shell_cfg1, waThreadShell, sizeof(waThreadShell), NORMALPRIO +1);
}
chThdSleepMilliseconds(10);
continue;
}

if (settings.serialMode != SERIAL_MODE_KLINE) {
Expand Down Expand Up @@ -334,7 +314,6 @@ CCM_FUNC static THD_FUNCTION(ThreadSDU, arg)
chnWriteTimeout(&SDU1, out_buffer, out, MS2ST(10));
}


}
return;
}
Expand Down Expand Up @@ -702,7 +681,7 @@ static THD_FUNCTION(ThreadWdg, arg)
/*===========================================================================*/
/* Main Thread */
/*===========================================================================*/
THD_WORKING_AREA(waThreadShell, 512);

int main(void)
{
/*
Expand Down Expand Up @@ -760,7 +739,6 @@ int main(void)
chThdCreateStatic(waThreadSER2, sizeof(waThreadSER2), NORMALPRIO, ThreadSER2, NULL);
chThdCreateStatic(waThreadRecord, sizeof(waThreadRecord), NORMALPRIO+1, ThreadRecord, NULL);
chThdCreateStatic(waThreadWdg, sizeof(waThreadWdg), HIGHPRIO, ThreadWdg, NULL);
//shelltp = shellCreateStatic(&shell_cfg1, waThreadShell, sizeof(waThreadShell), NORMALPRIO + 1);

/* Create last as it uses pointers from above */
chThdCreateStatic(waThreadMonitor, sizeof(waThreadMonitor), NORMALPRIO+10, ThreadMonitor, NULL);
Expand Down
63 changes: 63 additions & 0 deletions code/app/shell_cmds.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "ch.h"
#include "hal.h"
#include "shell.h"
#include "chprintf.h"

bool dbg_can = false;

static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) {
static const char *states[] = {CH_STATE_NAMES};
thread_t *tp;

(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: threads\r\n");
return;
}
#if CH_CFG_USE_DYNAMIC
chprintf(chp, " addr stack prio refs state\r\n");
tp = chRegFirstThread();
do {
chprintf(chp, "%08lx %08lx %4lu %4lu %9s %lu\r\n",
(uint32_t)tp,
(uint32_t)tp->p_ctx.r13,
(uint32_t)tp->p_prio,
(uint32_t)(tp->p_refs - 1),
states[tp->p_state]);
tp = chRegNextThread(tp);
} while (tp != NULL);
#else
chprintf(chp, " addr stack prio state\r\n");
tp = chRegFirstThread();
do {
chprintf(chp, "%08lx %08lx %4lu %4lu %lu\r\n",
(uint32_t)tp,
(uint32_t)tp->p_ctx.r13,
(uint32_t)tp->p_prio,
states[tp->p_state]);
tp = chRegNextThread(tp);
} while (tp != NULL);
#endif
}

static void cmd_candbg(BaseSequentialStream *chp, int argc, char *argv[]) {

(void)argc;
(void)argv;
uint8_t in, buf;

chprintf(chp, "Press any key to quit\n");
chThdSleepMilliseconds(1000);
dbg_can = true;
in = chSequentialStreamRead(chp, &buf, 1);
while (in == 0) {
chThdSleepMilliseconds(10);
}
dbg_can = false;
}

const ShellCommand sh_commands[] = {
{"threads", cmd_threads},
{"candbg", cmd_candbg},
{NULL, NULL}
};

0 comments on commit 0467b07

Please sign in to comment.