/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT 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.
ChibiOS/RT 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/>.
---
A special exception to the GPL can be applied should you wish to distribute
a combined work that includes ChibiOS/RT, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
#include <stdio.h>
#include <string.h>
#include "ch.h"
#include "hal.h"
//#include "test.h"
#include "shell.h"
#include "evtimer.h"
#include "chprintf.h"
#include "usbcfg.h"
#include "ap_math.h"
#include "hardware.h"
#include "useAdc.h"
#include "spiInterface.h"
#include "SSD1306.h"
#include "timer2.h"
static MUTEX_DECL(mutexDisplay);
/*===========================================================================*/
/* Command line related. */
/*===========================================================================*/
#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
#define TEST_WA_SIZE THD_WORKING_AREA_SIZE(256)
static void cmd_mem(BaseChannel *chp, int argc, char *argv[]) {
size_t n, size , largest;
(void) argv;
if (argc > 0) {
chprintf(chp, "Usage: mem\r\n");
return;
}
n = chHeapStatus(NULL, &size, &largest);
chprintf(chp, "core free memory : %u bytes\r\n", chCoreStatus());
chprintf(chp, "heap fragments : %u \r\n",n);
chprintf(chp, "largest fragment : %u \r\n",largest);
chprintf(chp, "heap free total : %u bytes\r\n", size);
}
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) {
shellUsage(chp, "threads");
return;
}
chprintf(chp, "stklimit stack addr refs prio state name\r\n" SHELL_NEWLINE_STR);
tp = chRegFirstThread();
do {
#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE)
uint32_t stklimit = (uint32_t)tp->wabase;
#else
uint32_t stklimit = 0U;
#endif
chprintf(chp, "%08lx %08lx %08lx %4lu %4lu %9s %12s" SHELL_NEWLINE_STR,
stklimit, (uint32_t)tp->ctx.sp, (uint32_t)tp,
(uint32_t)tp->refs - 1, (uint32_t)tp->prio, states[tp->state],
tp->name == NULL ? "" : tp->name);
tp = chRegNextThread(tp);
} while (tp != NULL);
}
static void cmd_test(BaseChannel *chp, int argc, char *argv[]) {
thread_t *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 = { (BaseChannel *) &SDU1, commands };
/*
* Red LEDs blinker thread, times are in milliseconds.
*/
uint16_t sampleVal;
static THD_WORKING_AREA(waThread1, 512);
static msg_t Thread1(void *arg) {
(void) arg;
chRegSetThreadName("PLL ");
while (TRUE) {
sampleVal = getNextPulse();
chThdSleep(1000);
}
return 0;
}
/*
* USB Bulk thread, times are in milliseconds.
*/
static THD_WORKING_AREA(waThread2, 512);
static msg_t Thread2(void *arg) {
chThdSleep(TIME_INFINITE);
}
/*
* Application entry point.
*/
int main(void) {
thread_t *shelltp = NULL;
// struct EventListener el0, el1;
/*
* 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();
/*
* 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(1500);
// usbStart(serusbcfg.usbp, &usbcfg);
// usbConnectBus(serusbcfg.usbp);
// Ignition timing code
initTimer2();
useAdc();
/*
* Activates the serial driver 2 using the driver default configuration.
*/
// sdStart(&SD2, NULL);
/*
* Shell manager initialization.
*/
shellInit();
ap_init();
/*
* Creates the PLL thread
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/* start the SPI hardware for display */
ssd1306spiInit();
ssd1306_begin( SSD1306_SWITCHCAPVCC, 0) ;
int spd = 800;
int adv = 0;
setRPM(spd);
while(1)
{
adcSample();
adv = getAdc(0) * 1200 / 4096 - 300;
/* initialise the display */
chMtxLock(&mutexDisplay);
clearDisplay();
font_gotoxy(0,0);
print_digits(64, 16, 4, 1, adv);
print_digits(64, 32, 4,-1, spd);
print_large_string("RPM:",0,32,4);
print_large_string("ADV:",0,16,4);
font_puts("count");
font_sig_digits(12, 0,1,-1, sampleVal);
display();
chMtxUnlock(&mutexDisplay);
//
// invertDisplay(x & 32);
chThdSleep(chTimeMS2I(10));
}
// shell thread code . dead at present
// /*
// * Normal main() thread activity, spawning shells.
// */
// while (true) {
// if (SDU1.config->usbp->state == USB_ACTIVE) {
//o thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
// "shell", NORMALPRIO + 1,
// shellThread, (void *)&shell_cfg1);
// chThdWait(shelltp); /* Waiting termination. */
// }
// chThdSleepMilliseconds(1000);
// }
}