/* 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 . */ #include "ch.h" #include "hal.h" #include "test.h" #include "lwipthread.h" #include "usbshell.h" #include "web/web.h" /*===========================================================================*/ /* 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; } /* * 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) { palClearPad(GPIOC, GPIOC_LED); chThdSleepMilliseconds(500); palSetPad(GPIOC, GPIOC_LED); chThdSleepMilliseconds(500); } } /* * Application entry point. */ int main(void) { static const evhandler_t evhndl[] = { InsertHandler, RemoveHandler }; 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(); /* * Activates the shell on the USB-CDC. */ usInit(); /* * Activates the serial driver 6 using the driver default configuration. */ sdStart(&SD6, NULL); /* * Activates the card insertion monitor. */ tmr_init(&SDCD1); /* * 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 * sleeping in a loop and listen for events. */ chEvtRegister(&inserted_event, &el0, 0); chEvtRegister(&removed_event, &el1, 1); while (TRUE) { usStateCheck(); if (palReadPad(GPIOA, GPIOA_BUTTON_WKUP) != 0) { } chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); } }