aboutsummaryrefslogtreecommitdiffstats
path: root/demos/ARMCM4-STM32F407-DISCOVERY-MEMS/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'demos/ARMCM4-STM32F407-DISCOVERY-MEMS/main.c')
-rw-r--r--demos/ARMCM4-STM32F407-DISCOVERY-MEMS/main.c135
1 files changed, 124 insertions, 11 deletions
diff --git a/demos/ARMCM4-STM32F407-DISCOVERY-MEMS/main.c b/demos/ARMCM4-STM32F407-DISCOVERY-MEMS/main.c
index 05406ba58..7940e3a0a 100644
--- a/demos/ARMCM4-STM32F407-DISCOVERY-MEMS/main.c
+++ b/demos/ARMCM4-STM32F407-DISCOVERY-MEMS/main.c
@@ -21,14 +21,96 @@
#include "ch.h"
#include "hal.h"
#include "test.h"
+
+#include "chprintf.h"
+#include "shell.h"
#include "lis302dl.h"
+#include "usbcfg.h"
+
+/* Virtual serial port over USB.*/
+static SerialUSBDriver SDU1;
+
+/*===========================================================================*/
+/* Command line related. */
+/*===========================================================================*/
+
+#define SHELL_WA_SIZE THD_WA_SIZE(2048)
+#define TEST_WA_SIZE THD_WA_SIZE(256)
+
+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[]) {
+ static const char *states[] = {THD_STATE_NAMES};
+ Thread *tp;
+
+ (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 {
+ chprintf(chp, "%.8lx %.8lx %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], (uint32_t)tp->p_time);
+ tp = chRegNextThread(tp);
+ } while (tp != NULL);
+}
+
+static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) {
+ Thread *tp;
+
+ (void)argv;
+ if (argc > 0) {
+ chprintf(chp, "Usage: test\r\n");
+ return;
+ }
+ tp = chThdCreateFromHeap(NULL, TEST_WA_SIZE, chThdGetPriority(),
+ 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
+};
+
+/*===========================================================================*/
+/* Accelerometer related. */
+/*===========================================================================*/
+
/*
* PWM configuration structure.
* Cyclic callback enabled, channels 1 and 4 enabled without callbacks,
* the active state is a logic one.
*/
-static PWMConfig pwmcfg = {
+static const PWMConfig pwmcfg = {
100000, /* 100kHz PWM clock frequency. */
128, /* PWM period is 128 cycles. */
NULL,
@@ -68,16 +150,14 @@ static const SPIConfig spi2cfg = {
0
};
-/* Last accelerometer data.*/
-static int8_t xbuf[4], ybuf[4];
-
/*
* This is a periodic thread that does absolutely nothing except flashing
* a LED.
*/
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
- systime_t time;
+ static int8_t xbuf[4], ybuf[4]; /* Last accelerometer data.*/
+ systime_t time; /* Next deadline.*/
(void)arg;
chRegSetThreadName("reader");
@@ -138,10 +218,15 @@ static msg_t Thread1(void *arg) {
}
}
+/*===========================================================================*/
+/* Initialization and main thread. */
+/*===========================================================================*/
+
/*
* Application entry point.
*/
int main(void) {
+ Thread *shelltp = NULL;
/*
* System initializations.
@@ -154,6 +239,22 @@ int main(void) {
chSysInit();
/*
+ * 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);
+
+ /*
* Activates the serial driver 2 using the driver default configuration.
* PA2(TX) and PA3(RX) are routed to USART2.
*/
@@ -188,10 +289,10 @@ int main(void) {
* Initializes the PWM driver 4, routes the TIM4 outputs to the board LEDs.
*/
pwmStart(&PWMD4, &pwmcfg);
- palSetPadMode(GPIOD, GPIOD_LED4, PAL_MODE_ALTERNATE(2)); /* Green. */
- palSetPadMode(GPIOD, GPIOD_LED3, PAL_MODE_ALTERNATE(2)); /* Orange. */
- palSetPadMode(GPIOD, GPIOD_LED5, PAL_MODE_ALTERNATE(2)); /* Red. */
- palSetPadMode(GPIOD, GPIOD_LED6, PAL_MODE_ALTERNATE(2)); /* Blue. */
+ palSetPadMode(GPIOD, GPIOD_LED4, PAL_MODE_ALTERNATE(2)); /* Green. */
+ palSetPadMode(GPIOD, GPIOD_LED3, PAL_MODE_ALTERNATE(2)); /* Orange. */
+ palSetPadMode(GPIOD, GPIOD_LED5, PAL_MODE_ALTERNATE(2)); /* Red. */
+ palSetPadMode(GPIOD, GPIOD_LED6, PAL_MODE_ALTERNATE(2)); /* Blue. */
/*
* Creates the example thread.
@@ -206,8 +307,20 @@ int main(void) {
* driver 2.
*/
while (TRUE) {
- if (palReadPad(GPIOA, GPIOA_BUTTON))
- TestThread(&SD2);
+ 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 has been exited.*/
+ if (chThdTerminated(shelltp)) {
+ /* Recovers memory of the previous shell.*/
+ chThdRelease(shelltp);
+ shelltp = NULL;
+ }
+ }
chThdSleepMilliseconds(500);
}
}