2013-11-11 11:05:47 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#include "chprintf.h"
|
|
|
|
#include "shell.h"
|
|
|
|
|
|
|
|
#include "usbcfg.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Red LED blinker thread, times are in milliseconds.
|
|
|
|
*/
|
2013-11-11 12:14:43 +00:00
|
|
|
static THD_WORKING_AREA(waThread1, 128);
|
2013-11-11 11:05:47 +00:00
|
|
|
static msg_t Thread1(void *arg) {
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("blinker1");
|
|
|
|
while (TRUE) {
|
|
|
|
palClearPad(GPIOG, GPIOG_LED4_RED);
|
|
|
|
chThdSleepMilliseconds(500);
|
|
|
|
palSetPad(GPIOG, GPIOG_LED4_RED);
|
|
|
|
chThdSleepMilliseconds(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Green LED blinker thread, times are in milliseconds.
|
|
|
|
*/
|
2013-11-11 12:14:43 +00:00
|
|
|
static THD_WORKING_AREA(waThread2, 128);
|
2013-11-11 11:05:47 +00:00
|
|
|
static msg_t Thread2(void *arg) {
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("blinker2");
|
|
|
|
while (TRUE) {
|
|
|
|
palClearPad(GPIOG, GPIOG_LED3_GREEN);
|
|
|
|
chThdSleepMilliseconds(250);
|
|
|
|
palSetPad(GPIOG, GPIOG_LED3_GREEN);
|
|
|
|
chThdSleepMilliseconds(250);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Command line related. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/* Virtual serial port over USB.*/
|
|
|
|
SerialUSBDriver SDU1;
|
|
|
|
|
2013-11-11 12:14:43 +00:00
|
|
|
#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
|
|
|
|
#define TEST_WA_SIZE THD_WORKING_AREA_SIZE(256)
|
2013-11-11 11:05:47 +00:00
|
|
|
|
|
|
|
static void cmd_mem(BaseSequentialStream *chp, int argc, char *argv[]) {
|
|
|
|
size_t n, size;
|
|
|
|
|
|
|
|
(void)argv;
|
|
|
|
if (argc > 0) {
|
|
|
|
chprintf(chp, "Usage: mem\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = chHeapStatus(NULL, &size);
|
|
|
|
chprintf(chp, "core free memory : %u bytes\r\n", chCoreStatus());
|
|
|
|
chprintf(chp, "heap fragments : %u\r\n", n);
|
|
|
|
chprintf(chp, "heap free total : %u bytes\r\n", size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) {
|
2013-11-11 12:14:43 +00:00
|
|
|
static const char *states[] = {CH_STATE_NAMES};
|
|
|
|
thread_t *tp;
|
2013-11-11 11:05:47 +00:00
|
|
|
|
|
|
|
(void)argv;
|
|
|
|
if (argc > 0) {
|
|
|
|
chprintf(chp, "Usage: threads\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chprintf(chp, " addr stack prio refs state time\r\n");
|
|
|
|
tp = chRegFirstThread();
|
|
|
|
do {
|
2013-11-11 12:14:43 +00:00
|
|
|
chprintf(chp, "%.8lx %.8lx %4lu %4lu %9s\r\n",
|
2013-11-11 11:05:47 +00:00
|
|
|
(uint32_t)tp, (uint32_t)tp->p_ctx.r13,
|
|
|
|
(uint32_t)tp->p_prio, (uint32_t)(tp->p_refs - 1),
|
2013-11-11 12:14:43 +00:00
|
|
|
states[tp->p_state]);
|
2013-11-11 11:05:47 +00:00
|
|
|
tp = chRegNextThread(tp);
|
|
|
|
} while (tp != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) {
|
2013-11-11 12:14:43 +00:00
|
|
|
thread_t *tp;
|
2013-11-11 11:05:47 +00:00
|
|
|
|
|
|
|
(void)argv;
|
|
|
|
if (argc > 0) {
|
|
|
|
chprintf(chp, "Usage: test\r\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-11-11 12:14:43 +00:00
|
|
|
tp = chThdCreateFromHeap(NULL, TEST_WA_SIZE, chThdGetPriorityX(),
|
2013-11-11 11:05:47 +00:00
|
|
|
TestThread, chp);
|
|
|
|
if (tp == NULL) {
|
|
|
|
chprintf(chp, "out of memory\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chThdWait(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const ShellCommand commands[] = {
|
|
|
|
{"mem", cmd_mem},
|
|
|
|
{"threads", cmd_threads},
|
|
|
|
{"test", cmd_test},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const ShellConfig shell_cfg1 = {
|
|
|
|
(BaseSequentialStream *)&SDU1,
|
|
|
|
commands
|
|
|
|
};
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Initialization and main thread. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Application entry point.
|
|
|
|
*/
|
|
|
|
int main(void) {
|
2013-11-11 12:14:43 +00:00
|
|
|
thread_t *shelltp = NULL;
|
2013-11-11 11:05:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* System initializations.
|
|
|
|
* - HAL initialization, this also initializes the configured device drivers
|
|
|
|
* and performs the board-specific initializations.
|
|
|
|
* - Kernel initialization, the main() function becomes a thread and the
|
|
|
|
* RTOS is active.
|
|
|
|
*/
|
|
|
|
halInit();
|
|
|
|
chSysInit();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shell manager initialization.
|
|
|
|
*/
|
|
|
|
shellInit();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializes a serial-over-USB CDC driver.
|
|
|
|
*/
|
|
|
|
sduObjectInit(&SDU1);
|
|
|
|
sduStart(&SDU1, &serusbcfg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Activates the USB driver and then the USB bus pull-up on D+.
|
|
|
|
* Note, a delay is inserted in order to not have to disconnect the cable
|
|
|
|
* after a reset.
|
|
|
|
*/
|
|
|
|
usbDisconnectBus(serusbcfg.usbp);
|
|
|
|
chThdSleepMilliseconds(1000);
|
|
|
|
usbStart(serusbcfg.usbp, &usbcfg);
|
|
|
|
usbConnectBus(serusbcfg.usbp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creating the blinker threads.
|
|
|
|
*/
|
|
|
|
chThdCreateStatic(waThread1, sizeof(waThread1),
|
|
|
|
NORMALPRIO + 10, Thread1, NULL);
|
|
|
|
chThdCreateStatic(waThread2, sizeof(waThread2),
|
|
|
|
NORMALPRIO + 10, Thread2, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normal main() thread activity, in this demo it just performs
|
|
|
|
* a shell respawn upon its termination.
|
|
|
|
*/
|
|
|
|
while (TRUE) {
|
|
|
|
if (!shelltp) {
|
|
|
|
if (SDU1.config->usbp->state == USB_ACTIVE) {
|
|
|
|
/* Spawns a new shell.*/
|
|
|
|
shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* If the previous shell exited.*/
|
2013-11-11 12:14:43 +00:00
|
|
|
if (chThdTerminatedX(shelltp)) {
|
2013-11-11 11:05:47 +00:00
|
|
|
/* Recovers memory of the previous shell.*/
|
|
|
|
chThdRelease(shelltp);
|
|
|
|
shelltp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chThdSleepMilliseconds(500);
|
|
|
|
}
|
|
|
|
}
|