2012-06-17 14:23:28 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#include "lwipthread.h"
|
|
|
|
|
2012-06-17 15:59:49 +00:00
|
|
|
#include "usbshell.h"
|
2012-06-17 14:23:28 +00:00
|
|
|
#include "web/web.h"
|
|
|
|
|
2012-06-28 13:54:19 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Card insertion monitor. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#define POLLING_INTERVAL 10
|
|
|
|
#define POLLING_DELAY 10
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Card monitor timer.
|
|
|
|
*/
|
|
|
|
static VirtualTimer tmr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Debounce counter.
|
|
|
|
*/
|
|
|
|
static unsigned cnt;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Card event sources.
|
|
|
|
*/
|
|
|
|
static EventSource inserted_event, removed_event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Insertion monitor timer callback function.
|
|
|
|
*
|
|
|
|
* @param[in] p pointer to the @p BaseBlockDevice object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void tmrfunc(void *p) {
|
|
|
|
BaseBlockDevice *bbdp = p;
|
|
|
|
|
|
|
|
/* The presence check is performed only while the driver is not in a
|
|
|
|
transfer state because it is often performed by changing the mode of
|
|
|
|
the pin connected to the CS/D3 contact of the card, this could disturb
|
|
|
|
the transfer.*/
|
|
|
|
blkstate_t state = blkGetDriverState(bbdp);
|
|
|
|
chSysLockFromIsr();
|
|
|
|
if ((state != BLK_READING) && (state != BLK_WRITING)) {
|
|
|
|
/* Safe to perform the check.*/
|
|
|
|
if (cnt > 0) {
|
|
|
|
if (blkIsInserted(bbdp)) {
|
|
|
|
if (--cnt == 0) {
|
|
|
|
chEvtBroadcastI(&inserted_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cnt = POLLING_INTERVAL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!blkIsInserted(bbdp)) {
|
|
|
|
cnt = POLLING_INTERVAL;
|
|
|
|
chEvtBroadcastI(&removed_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, bbdp);
|
|
|
|
chSysUnlockFromIsr();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Polling monitor start.
|
|
|
|
*
|
|
|
|
* @param[in] p pointer to an object implementing @p BaseBlockDevice
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void tmr_init(void *p) {
|
|
|
|
|
|
|
|
chEvtInit(&inserted_event);
|
|
|
|
chEvtInit(&removed_event);
|
|
|
|
chSysLock();
|
|
|
|
cnt = POLLING_INTERVAL;
|
|
|
|
chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, p);
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Main and generic code. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Card insertion event.
|
|
|
|
*/
|
|
|
|
static void InsertHandler(eventid_t id) {
|
|
|
|
|
|
|
|
(void)id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Card removal event.
|
|
|
|
*/
|
|
|
|
static void RemoveHandler(eventid_t id) {
|
|
|
|
|
|
|
|
(void)id;
|
|
|
|
}
|
|
|
|
|
2012-06-17 14:23:28 +00:00
|
|
|
/*
|
|
|
|
* Green LED blinker thread, times are in milliseconds.
|
|
|
|
*/
|
|
|
|
static WORKING_AREA(waThread1, 128);
|
|
|
|
static msg_t Thread1(void *arg) {
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("blinker");
|
|
|
|
while (TRUE) {
|
2012-06-28 18:37:56 +00:00
|
|
|
palTogglePad(GPIOC, GPIOC_LED);
|
|
|
|
chThdSleepMilliseconds(cnt ? 500 : 125);
|
2012-06-17 14:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Application entry point.
|
|
|
|
*/
|
|
|
|
int main(void) {
|
2012-06-28 13:54:19 +00:00
|
|
|
static const evhandler_t evhndl[] = {
|
|
|
|
InsertHandler,
|
|
|
|
RemoveHandler
|
|
|
|
};
|
|
|
|
struct EventListener el0, el1;
|
2012-06-17 14:23:28 +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();
|
|
|
|
|
|
|
|
/*
|
2012-06-17 15:59:49 +00:00
|
|
|
* Activates the shell on the USB-CDC.
|
|
|
|
*/
|
|
|
|
usInit();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Activates the serial driver 6 using the driver default configuration.
|
2012-06-17 14:23:28 +00:00
|
|
|
*/
|
|
|
|
sdStart(&SD6, NULL);
|
|
|
|
|
2012-06-28 13:54:19 +00:00
|
|
|
/*
|
|
|
|
* Activates the card insertion monitor.
|
|
|
|
*/
|
|
|
|
tmr_init(&SDCD1);
|
|
|
|
|
2012-06-17 14:23:28 +00:00
|
|
|
/*
|
|
|
|
* Creates the blinker thread.
|
|
|
|
*/
|
|
|
|
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates the LWIP threads (it changes priority internally).
|
|
|
|
*/
|
|
|
|
chThdCreateStatic(wa_lwip_thread, LWIP_THREAD_STACK_SIZE, NORMALPRIO + 1,
|
|
|
|
lwip_thread, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates the HTTP thread (it changes priority internally).
|
|
|
|
*/
|
|
|
|
chThdCreateStatic(wa_http_server, sizeof(wa_http_server), NORMALPRIO + 1,
|
|
|
|
http_server, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normal main() thread activity, in this demo it does nothing except
|
2012-06-28 13:54:19 +00:00
|
|
|
* sleeping in a loop and listen for events.
|
2012-06-17 14:23:28 +00:00
|
|
|
*/
|
2012-06-28 13:54:19 +00:00
|
|
|
chEvtRegister(&inserted_event, &el0, 0);
|
|
|
|
chEvtRegister(&removed_event, &el1, 1);
|
2012-06-17 14:23:28 +00:00
|
|
|
while (TRUE) {
|
2012-06-17 15:59:49 +00:00
|
|
|
usStateCheck();
|
|
|
|
if (palReadPad(GPIOA, GPIOA_BUTTON_WKUP) != 0) {
|
|
|
|
}
|
2012-06-28 13:54:19 +00:00
|
|
|
chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS));
|
2012-06-17 14:23:28 +00:00
|
|
|
}
|
|
|
|
}
|