/* a.out.h Copyright 1997, 1998, 1999, 2001 Red Hat, Inc. This file is part of Cygwin. This software is a copyrighted work licensed under the terms of the Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */ #ifndef _A_OUT_H_ #define _A_OUT_H_ #ifdef __cplusplus extern "C" { #endif #define COFF_IMAGE_WITH_PE #define COFF_LONG_SECTION_NAMES /*** coff information for Intel 386/486. */ /********************** FILE HEADER **********************/ struct external_filehdr { short f_magic; /* magic number */ short f_nscns; /* number of sections */ unsigned long f_timdat; /* time & date stamp */ unsigned long f_symptr; /* file pointer to symtab */ unsigned long f_nsyms; /* number of symtab entries */ short f_opthdr; /* sizeof(optional hdr) */ short f_flags; /* flags */ }; /* Bits for f_flags: * F_RELFLG relocation info stripped from file * F_EXEC file is executable (no unresolved external references) * F_LNNO line numbers stripped from file * F_LSYMS local symbols stripped from file * F_AR32WR file has byte ordering of an AR32WR machine (e.g. vax) */ #define F_RELFLG (0x0001) #define F_EXEC (0x0002) #define F_LNNO (0x0004) #define F_LSYMS (0x0008) #define I386MAGIC 0x14c #define I386PTXMAGIC 0x154 #define I386AIXMAGIC 0x175 /* This is Lynx's all-platform magic number for executables. */ #define LYNXCOFFMAGIC 0415 #define I386BADMAG(x) (((x).f_magic != I386MAGIC) \ && (x).f_magic != I386AIXMAGIC \ && (x).f_magic != I386PTXMAGIC \ && (x).f_magic != LYNXCOFFMAGIC) #define FILHDR struct external_filehdr #define FILHSZ 20 /********************** AOUT "OPTIONAL HEADER"= **********************/ typedef struct { unsigned short magic; /* type of file */ unsigned short vstamp; /* version stamp */ unsigned long tsize; /* text size in bytes, padded to FW bdry*/ unsigned long dsize; /* initialized data " " */ unsigned long bsize; /* uninitialized data " " */ unsigned long entry; /* entry pt. */ unsigned long text_start; /* base of text used for this file */ unsigned long data_start; /* base of data used for this file= */ } AOUTHDR; #define AOUTSZ 28 #define AOUTHDRSZ 28 #define OMAGIC 0404 /* object files, eg as output */ #define ZMAGIC 0413 /* demand load format, eg normal ld output */ #define STMAGIC 0401 /* target shlib */ #define SHMAGIC 0443 /* host shlib */ /* define some NT default values */ /* #define NT_IMAGE_BASE 0x400000 moved to internal.h */ #define NT_SECTION_ALIGNMENT 0x1000 #define NT_FILE_ALIGNMENT 0x200 #define NT_DEF_RESERVE 0x100000 #define NT_DEF_COMMIT 0x1000 /********************** SECTION HEADER **********************/ struct external_scnhdr { char s_name[8]; /* section name */ unsigned long s_paddr; /* physical address, offset of last addr in scn */ unsigned long s_vaddr; /* virtual address */ unsigned long s_size; /* section size */ unsigned long s_scnptr; /* file ptr to raw data for section */ unsigned long s_relptr; /* file ptr to relocation */ unsigned long s_lnnoptr; /* file ptr to line numbers */ unsigned short s_nreloc; /* number of relocation entries */ unsigned short s_nlnno; /* number of line number entries*/ unsigned long s_flags; /* flags */ }; #define SCNHDR struct external_scnhdr #define SCNHSZ 40 /* * names of "special" sections */ #define _TEXT ".text" #define _DATA ".data" #define _BSS ".bss" #define _COMMENT ".comment" #define _LIB ".lib" /********************** LINE NUMBERS **********************/ /* 1 line number entry for every "breakpointable" source line in a section. * Line numbers are grouped on a per function basis; first entry in a function * grouping will have l_lnno = 0 and in place of physical address will be the * symbol table index of the function name. */ struct external_lineno { union { unsigned long l_symndx; /* function name symbol index, iff l_lnno 0 */ unsigned long l_paddr; /* (physical) address of line number */ } l_addr; unsigned short l_lnno; /* line number */ }; #define LINENO struct external_lineno #define LINESZ 6 /********************** SYMBOLS **********************/ #define E_SYMNMLEN 8 /* # characters in a symbol name */ #define E_FILNMLEN 14 /* # characters in a file name */ #define E_DIMNUM 4 /* # array dimensions in auxiliary entry */ struct __attribute__((packed)) external_syment { union { char e_name[E_SYMNMLEN]; struct { unsigned long e_zeroes; unsigned long e_offset; } e; } e; unsigned long e_value; unsigned short e_scnum; unsigned short e_type; char e_sclass[1]; char e_numaux[1]; }; #define N_BTMASK (0xf) #define N_TMASK (0x30) #define N_BTSHFT (4) #define N_TSHIFT (2) union external_auxent { struct { unsigned long x_tagndx; /* str, un, or enum tag indx */ union { struct { unsigned short x_lnno; /* declaration line number */ unsigned short x_size; /* str/union/array size */ } x_lnsz; unsigned long x_fsize; /* size of function */ } x_misc; union { struct { /* if ISFCN, tag, or .bb */ unsigned long x_lnnoptr;/* ptr to fcn line # */ unsigned long x_endndx; /* entry ndx past block end */ } x_fcn; struct { /* if ISARY, up to 4 dimen. */ char x_dimen[E_DIMNUM][2]; } x_ary; } x_fcnary; unsigned short x_tvndx; /* tv index */ } x_sym; union { char x_fname[E_FILNMLEN]; struct { unsigned long x_zeroes; unsigned long x_offset; } x_n; } x_file; struct { unsigned long x_scnlen; /* section length */ unsigned short x_nreloc; /* # relocation entries */ unsigned short x_nlinno; /* # line numbers */ unsigned long x_checksum; /* section COMDAT checksum */ unsigned short x_associated;/* COMDAT associated section index */ char x_comdat[1]; /* COMDAT selection number */ } x_scn; struct { unsigned long x_tvfill; /* tv fill value */ unsigned short x_tvlen; /* length of .tv */ char x_tvran[2][2]; /* tv range */ } x_tv; /* info about .tv section (in auxent of symbol .tv)) */ }; #define SYMENT struct external_syment #define SYMESZ 18 #define AUXENT union external_auxent #define AUXESZ 18 #define _ETEXT "etext" /********************** RELOCATION DIRECTIVES **********************/ struct external_reloc { char r_vaddr[4]; char r_symndx[4]; char r_type[2]; }; #define RELOC struct external_reloc #define RELSZ 10 /* end of coff/i386.h */ /* PE COFF header information */ #ifndef _PE_H #define _PE_H /* NT specific file attributes */ #define IMAGE_FILE_RELOCS_STRIPPED 0x0001 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 #define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 #define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080 #define IMAGE_FILE_32BIT_MACHINE 0x0100 #define IMAGE_FILE_DEBUG_STRIPPED 0x0200 #define IMAGE_FILE_SYSTEM 0x1000 #define IMAGE_FILE_DLL 0x2000 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000 /* additional flags to be set for section headers to allow the NT loader to read and write to the section data (to replace the addresses of data in dlls for one thing); also to execute the section in .text's case= */ #define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 #define IMAGE_SCN_MEM_EXECUTE 0x20000000 #define IMAGE_SCN_MEM_READ 0x40000000 #define IMAGE_SCN_MEM_WRITE 0x80000000 /* * Section characteristics added for ppc-nt */ #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 /* Reserved. */ #define IMAGE_SCN_CNT_CODE 0x00000020 /* Section contains code. */ #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 /* Section contains initialized data. */ #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 /* Section contains uninitialized data. */ #define IMAGE_SCN_LNK_OTHER 0x00000100 /* Reserved. */ #define IMAGE_SCN_LNK_INFO 0x00000200 /* Section contains comments or some other type of information. */ #define IMAGE_SCN_LNK_REMOVE 0x00000800 /* Section contents will not become part of image. */ #define IMAGE_SCN_LNK_COMDAT 0x00001000 /* Section contents comdat. */ #define IMAGE_SCN_MEM_FARDATA 0x00008000 #define IMAGE_SCN_MEM_PURGEABLE 0x00020000 #define IMAGE_SCN_MEM_16BIT 0x00020000 #define IMAGE_SCN_MEM_LOCKED 0x00040000 #define IMAGE_SCN_MEM_PRELOAD 0x00080000 #define IMAGE_SCN_ALIGN_1BYTES 0x00100000 #define IMAGE_SCN_ALIGN_2BYTES 0x00200000 #define IMAGE_SCN_ALIGN_4BYTES 0x00300000 #define IMAGE_SCN_ALIGN_8BYTES 0x00400000 #define IMAGE_SCN_ALIGN_16BYTES 0x00500000 /* Default alignment if no others are specified. */ #define IMAGE_SCN_ALIGN_32BYTES 0x00600000 #define IMAGE_SCN_ALIGN_64BYTES 0x00700000 #define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 /* Section contains extended relocations. */ #define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 /* Section is not cachable. */ #define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 /* Section is not pageable. */ #define IMAGE_SCN_MEM_SHARED 0x10000000 /* Section is shareable. */ /* COMDAT selection codes. */ #define IMAGE_COMDAT_SELECT_NODUPLICATES (1) /* Warn if duplicates. */ #define IMAGE_COMDAT_SELECT_ANY (2) /* No warning. */ #define IMAGE_COMDAT_SELECT_SAME_SIZE (3) /* Warn if different size. */ #define IMAGE_COMDAT_SELECT_EXACT_MATCH (4) /* Warn if different. */ #define IMAGE_COMDAT_SELECT_ASSOCIATIVE (5) /* Base on other section. */ /* Magic values that are true for all dos/nt implementations */ #define DOSMAGIC 0x5a4d #define NT_SIGNATURE 0x00004550 /* NT allows long filenames, we want to accommodate this. This may break some of the bfd functions */ #undef FILNMLEN #define FILNMLEN 18 /* # characters in a file name */ #ifdef COFF_IMAGE_WITH_PE /* The filehdr is only weired in images */ #undef FILHDR struct external_PE_filehdr { /* DOS header fields */ unsigned short e_magic; /* Magic number, 0x5a4d */ unsigned short e_cblp; /* Bytes on last page of file, 0x90 */ unsigned short e_cp; /* Pages in file, 0x3 */ unsigned short e_crlc; /* Relocations, 0x0 */ unsigned short e_cparhdr; /* Size of header in paragraphs, 0x4 */ unsigned short e_minalloc; /* Minimum extra paragraphs needed, 0x0 */ unsigned short e_maxalloc; /* Maximum extra paragraphs needed, 0xFFFF */ unsigned short e_ss; /* Initial (relative) SS value, 0x0 */ unsigned short e_sp; /* Initial SP value, 0xb8 */ unsigned short e_csum; /* Checksum, 0x0 */ unsigned short e_ip; /* Initial IP value, 0x0 */ unsigned short e_cs; /* Initial (relative) CS value, 0x0 */ unsigned short e_lfarlc; /* File address of relocation table, 0x40 */ unsigned short e_ovno; /* Overlay number, 0x0 */ char e_res[4][2]; /* Reserved words, all 0x0 */ unsigned short e_oemid; /* OEM identifier (for e_oeminfo), 0x0 */ unsigned short e_oeminfo; /* OEM information; e_oemid specific, 0x0 */ char e_res2[10][2]; /* Reserved words, all 0x0 */ unsigned long e_lfanew; /* File address of new exe header, 0x80 */ char dos_message[16][4]; /* other stuff, always follow DOS header */ unsigned int nt_signature; /* required NT signature, 0x4550 */ /* From standard header */ unsigned short f_magic; /* magic number */ unsigned short f_nscns; /* number of sections */ unsigned long f_timdat; /* time & date stamp */ unsigned long f_symptr; /* file pointer to symtab */ unsigned long f_nsyms; /* number of symtab entries */ unsigned short f_opthdr; /* sizeof(optional hdr) */ unsigned short f_flags; /* flags */ }; #define FILHDR struct external_PE_filehdr #undef FILHSZ #define FILHSZ 152 #endif typedef struct { unsigned short magic; /* type of file */ unsigned short vstamp; /* version stamp */ unsigned long tsize; /* text size in bytes, padded to FW bdry*/ unsigned long dsize; /* initialized data " " */ unsigned long bsize; /* uninitialized data " " */ unsigned long entry; /* entry pt. */ unsigned long text_start; /* base of text used for this file */ unsigned long data_start; /* base of all data used for this file */ /* NT extra fields; see internal.h for descriptions */ unsigned long ImageBase; unsigned long SectionAlignment; unsigned long FileAlignment; unsigned short MajorOperatingSystemVersion; unsigned short MinorOperatingSystemVersion; unsigned short MajorImageVersion; unsigned short MinorImageVersion; unsigned short MajorSubsystemVersion; unsigned short MinorSubsystemVersion; char Reserved1[4]; unsigned long SizeOfImage; unsigned long SizeOfHeaders; unsigned long CheckSum; unsigned short Subsystem; unsigned short DllCharacteristics; unsigned long SizeOfStackReserve; unsigned long SizeOfStackCommit; unsigned long SizeOfHeapReserve; unsigned long SizeOfHeapCommit; unsigned long LoaderFlags; unsigned long NumberOfRvaAndSizes; /* IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; */ char DataDirectory[16][2][4]; /* 16 entries, 2 elements/entry, 4 chars */ } PEAOUTHDR; #undef AOUTSZ #define AOUTSZ (AOUTHDRSZ + 196) #undef E_FILNMLEN #define E_FILNMLEN 18 /* # characters in a file name */ #endif /* end of coff/pe.h */ #define DT_NON (0) /* no derived type */ #define DT_PTR (1) /* pointer */ #define DT_FCN (2) /* function */ #define DT_ARY (3) /* array */ #define ISPTR(x) (((x) & N_TMASK) == (DT_PTR << N_BTSHFT)) #define ISFCN(x) (((x) & N_TMASK) == (DT_FCN << N_BTSHFT)) #define ISARY(x) (((x) & N_TMASK) == (DT_ARY << N_BTSHFT)) #ifdef __cplusplus } #endif #endif /* _A_OUT_H_ */ /a> 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
/*
    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 <stdio.h>
#include <string.h>

#include "ch.h"
#include "hal.h"
#include "test.h"
#include "shell.h"
#include "evtimer.h"
#include "chprintf.h"

#include "ff.h"

/*===========================================================================*/
/* Card insertion monitor.                                                   */
/*===========================================================================*/

#define SDC_POLLING_INTERVAL            10
#define SDC_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   Inserion monitor function.
 *
 * @param[in] sdcp      pointer to the @p SDCDriver object
 *
 * @notapi
 */
bool_t sdc_lld_is_card_inserted(SDCDriver *sdcp) {

  (void)sdcp;
  return !palReadPad(GPIOF, GPIOF_SD_DETECT);
}

/**
 * @brief   Protection detection.
 * @note    Not supported.
 *
 * @param[in] sdcp      pointer to the @p SDCDriver object
 *
 * @notapi
 */
bool_t sdc_lld_is_write_protected(SDCDriver *sdcp) {

  (void)sdcp;
  return FALSE;
}

/**
 * @brief   Inserion monitor timer callback function.
 *
 * @param[in] p         pointer to the @p SDCDriver object
 *
 * @notapi
 */
static void tmrfunc(void *p) {
  SDCDriver *sdcp = p;

  if (cnt > 0) {
    if (sdcIsCardInserted(sdcp)) {
      if (--cnt == 0) {
        chEvtBroadcastI(&inserted_event);
      }
    }
    else
      cnt = SDC_POLLING_INTERVAL;
  }
  else {
    if (!sdcIsCardInserted(sdcp)) {
      cnt = SDC_POLLING_INTERVAL;
      chEvtBroadcastI(&removed_event);
    }
  }
  chVTSetI(&tmr, MS2ST(SDC_POLLING_DELAY), tmrfunc, sdcp);
}

/**
 * @brief   Polling monitor start.
 *
 * @param[in] sdcp      pointer to the @p SDCDriver object
 *
 * @notapi
 */
static void tmr_init(SDCDriver *sdcp) {

  chEvtInit(&inserted_event);
  chEvtInit(&removed_event);
  chSysLock();
  cnt = SDC_POLLING_INTERVAL;
  chVTSetI(&tmr, MS2ST(SDC_POLLING_DELAY), tmrfunc, sdcp);
  chSysUnlock();
}

/*===========================================================================*/
/* FatFs related.                                                            */
/*===========================================================================*/

/**
 * @brief FS object.
 */
FATFS SDC_FS;

/* FS mounted and ready.*/
static bool_t fs_ready = FALSE;

/* Generic large buffer.*/
uint8_t fbuff[1024];

static FRESULT scan_files(BaseChannel *chp, char *path) {
  FRESULT res;
  FILINFO fno;
  DIR dir;
  int i;
  char *fn;

  res = f_opendir(&dir, path);
  if (res == FR_OK) {
    i = strlen(path);
    for (;;) {
      res = f_readdir(&dir, &fno);
      if (res != FR_OK || fno.fname[0] == 0)
        break;
      if (fno.fname[0] == '.')
        continue;
      fn = fno.fname;
      if (fno.fattrib & AM_DIR) {
        path[i++] = '/';
        strcpy(&path[i], fn);
        res = scan_files(chp, path);
        if (res != FR_OK)
          break;
        path[i] = 0;
      }
      else {
        chprintf(chp, "%s/%s\r\n", path, fn);
      }
    }
  }
  return res;
}

/*===========================================================================*/
/* Command line related.                                                     */
/*===========================================================================*/

#define SHELL_WA_SIZE   THD_WA_SIZE(2048)
#define TEST_WA_SIZE    THD_WA_SIZE(256)

static void cmd_mem(BaseChannel *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(BaseChannel *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(BaseChannel *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 void cmd_tree(BaseChannel *chp, int argc, char *argv[]) {
  FRESULT err;
  uint32_t clusters;
  FATFS *fsp;

  (void)argv;
  if (argc > 0) {
    chprintf(chp, "Usage: tree\r\n");
    return;
  }
  if (!fs_ready) {
    chprintf(chp, "File System not mounted\r\n");
    return;
  }
  err = f_getfree("/", &clusters, &fsp);
  if (err != FR_OK) {
    chprintf(chp, "FS: f_getfree() failed\r\n");
    return;
  }
  chprintf(chp,
           "FS: %lu free clusters, %lu sectors per cluster, %lu bytes free\r\n",
           clusters, (uint32_t)SDC_FS.csize,
           clusters * (uint32_t)SDC_FS.csize * (uint32_t)SDC_BLOCK_SIZE);
  fbuff[0] = 0;
  scan_files(chp, (char *)fbuff);
}

static const ShellCommand commands[] = {
  {"mem", cmd_mem},
  {"threads", cmd_threads},
  {"test", cmd_test},
  {"tree", cmd_tree},
  {NULL, NULL}
};

static const ShellConfig shell_cfg1 = {
  (BaseChannel *)&SD1,
  commands
};

/*===========================================================================*/
/* Main and generic code.                                                    */
/*===========================================================================*/

/*
 * SD card insertion event.
 */
static void InsertHandler(eventid_t id) {
  FRESULT err;

  (void)id;
  /*
   * On insertion SDC initialization and FS mount.
   */
  if (sdcConnect(&SDCD1))
    return;

  err = f_mount(0, &SDC_FS);
  if (err != FR_OK) {
    sdcDisconnect(&SDCD1);
    return;
  }
  fs_ready = TRUE;
}

/*
 * SD card removal event.
 */
static void RemoveHandler(eventid_t id) {

  (void)id;
  if (sdcGetDriverState(&SDCD1) == SDC_ACTIVE)
    sdcDisconnect(&SDCD1);
  fs_ready = FALSE;
}

/*
 * Red 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(GPIOF, GPIOF_LED4);
    palSetPad(GPIOF, GPIOF_LED1);
    chThdSleepMilliseconds(250);
    palClearPad(GPIOF, GPIOF_LED1);
    palSetPad(GPIOF, GPIOF_LED2);
    chThdSleepMilliseconds(250);
    palClearPad(GPIOF, GPIOF_LED2);
    palSetPad(GPIOF, GPIOF_LED3);
    chThdSleepMilliseconds(250);
    palClearPad(GPIOF, GPIOF_LED3);
    palSetPad(GPIOF, GPIOF_LED4);
    chThdSleepMilliseconds(250);
  }
}

/*
 * Application entry point.
 */
int main(void) {
  static const evhandler_t evhndl[] = {
    InsertHandler,
    RemoveHandler
  };
  Thread *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();

  /*
   * Activates the serial driver 1 and SDC driver 1 using default
   * configuration.
   */
  sdStart(&SD1, NULL);
  sdcStart(&SDCD1, NULL);

  /*
   * Shell manager initialization.
   */
  shellInit();

  /*
   * Activates the card insertion monitor.
   */
  tmr_init(&SDCD1);

  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, 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) {
    if (!shelltp)
      shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO);
    else if (chThdTerminated(shelltp)) {
      chThdRelease(shelltp);    /* Recovers memory of the previous shell.   */
      shelltp = NULL;           /* Triggers spawning of a new shell.        */
    }
    chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS));
  }
}