Subversion Repositories chibiosIgnition

Rev

Rev 15 | Rev 18 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
  3.  2011,2012 Giovanni Di Sirio.
  4.  
  5.  This file is part of ChibiOS/RT.
  6.  
  7.  ChibiOS/RT is free software; you can redistribute it and/or modify
  8.  it under the terms of the GNU General Public License as published by
  9.  the Free Software Foundation; either version 3 of the License, or
  10.  (at your option) any later version.
  11.  
  12.  ChibiOS/RT is distributed in the hope that it will be useful,
  13.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  GNU General Public License for more details.
  16.  
  17.  You should have received a copy of the GNU General Public License
  18.  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20.  ---
  21.  
  22.  A special exception to the GPL can be applied should you wish to distribute
  23.  a combined work that includes ChibiOS/RT, without being obliged to provide
  24.  the source code for any proprietary components. See the file exception.txt
  25.  for full details of how and when the exception can be applied.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #include "ch.h"
  32. #include "hal.h"
  33. //#include "test.h"
  34. #include "shell.h"
  35. #include "evtimer.h"
  36. #include "chprintf.h"
  37.  
  38.  
  39.  
  40. #if (HAL_USE_SERIAL_USB == TRUE)
  41. #include "usbcfg.h"
  42. #endif
  43.  
  44.  
  45. #include "ap_math.h"
  46. #include "hardware.h"
  47. #include "useAdc.h"
  48. #include "spiInterface.h"
  49. #include "SSD1306.h"
  50. #include "font.h"
  51.  
  52. #include "timer2.h"
  53.  
  54.  
  55. #include "shellCmds.h"
  56.  
  57.  
  58. static MUTEX_DECL(mutexDisplay);
  59.  
  60. /*===========================================================================*/
  61. /* Command line related.                                                     */
  62. /*===========================================================================*/
  63.  
  64. #define SHELL_WA_SIZE   THD_WORKING_AREA_SIZE(2048)
  65. #define TEST_WA_SIZE    THD_WORKING_AREA_SIZE(256)
  66.  
  67.  
  68.  
  69. static const ShellConfig shell_cfg1 = {
  70. #if (HAL_USE_SERIAL_USB == TRUE)
  71.   (BaseSequentialStream *)&SDU1,
  72. #else
  73.   (BaseSequentialStream *)&SD1,
  74. #endif
  75.   shellCommands
  76. };
  77. ////////
  78. // end of shell stuff
  79.  
  80. uint16_t sampIndex;
  81.  
  82. static THD_WORKING_AREA(waThread1, 512);
  83. static msg_t Thread1(void *arg) {
  84.  
  85.         (void) arg;
  86.         chRegSetThreadName("PLL ");
  87.         while (TRUE) {
  88.                 sampIndex = getNextRefPulseIndex();
  89. if(slowPulse() == 0)
  90.                 processNextPulse(sampIndex);
  91.  
  92.         }
  93.         return 0;
  94. }
  95.  
  96. /*
  97.  * Command Shell Thread
  98.  */
  99. static THD_WORKING_AREA(waThread2, 512);
  100. static msg_t Thread2(void *arg) {
  101.         thread_t *shelltp = NULL;
  102.  
  103.         chRegSetThreadName("Shell ");
  104. /*
  105.            * in this demo it just performs
  106.            * a shell respawn upon its termination.
  107.            */
  108.           while (true) {
  109.             if (!shelltp) {
  110.         #if (HAL_USE_SERIAL_USB == TRUE)
  111.               if (SDU1.config->usbp->state == USB_ACTIVE) {
  112.                 /* Spawns a new shell.*/
  113.                 shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
  114.               }
  115.         #else
  116.                 shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
  117.         #endif
  118.             }
  119.             else {
  120.               /* If the previous shell exited.*/
  121.               if (chThdTerminatedX(shelltp)) {
  122.                 /* Recovers memory of the previous shell.*/
  123.                 chThdRelease(shelltp);
  124.                 shelltp = NULL;
  125.               }
  126.             }
  127.             chThdSleepMilliseconds(500);
  128.           }
  129.  
  130.         return MSG_OK;
  131. }
  132.  
  133.  
  134.  
  135.  
  136. /*
  137.  * Application entry point.
  138.  */
  139. int main(void) {
  140. //  struct EventListener el0, el1;
  141.  
  142.         /*
  143.          * System initializations.
  144.          * - HAL initialization, this also initializes the configured device drivers
  145.          *   and performs the board-specific initializations.
  146.          * - Kernel initialization, the main() function becomes a thread and the
  147.          *   RTOS is active.
  148.          */
  149.         halInit();
  150.         chSysInit();
  151.  
  152.  
  153.  
  154. #if (HAL_USE_SERIAL_USB == TRUE)
  155.   /*
  156.    * Initializes a serial-over-USB CDC driver.
  157.    */
  158.   sduObjectInit(&SDU1);
  159.   sduStart(&SDU1, &serusbcfg);
  160.  
  161.   /*
  162.    * Activates the USB driver and then the USB bus pull-up on D+.
  163.    * Note, a delay is inserted in order to not have to disconnect the cable
  164.    * after a reset.
  165.    */
  166.   usbDisconnectBus(serusbcfg.usbp);
  167.   chThdSleepMilliseconds(1000);
  168.   usbStart(serusbcfg.usbp, &usbcfg);
  169.   usbConnectBus(serusbcfg.usbp);
  170. #else
  171.   /*
  172.    * Initializes serial port.
  173.    */
  174.   sdStart(&SD2, NULL);
  175. #endif /* HAL_USE_SERIAL_USB */
  176.  
  177.           // Ignition timing code
  178.         initTimer2();
  179.  
  180.    useAdc();
  181.  
  182.         /*
  183.          * Activates the serial driver 2 using the driver default configuration.
  184.          */
  185. //      sdStart(&SD2, NULL);
  186.  
  187.         /*
  188.          * Shell manager initialization.
  189.          */
  190.         shellInit();
  191.  
  192.         ap_init();
  193.  
  194.  
  195.          /*
  196.          * Creates the PLL thread
  197.          */
  198.         chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  199.  
  200.  
  201.  
  202.         chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
  203.  
  204.         /* start the SPI hardware for display */
  205.         ssd1306spiInit();
  206.  
  207.         ssd1306_begin( SSD1306_SWITCHCAPVCC, 0) ;
  208.         int spd  = 800;
  209.  
  210.     int adv = 0;
  211.         setRPM(spd);
  212.     int avgSmp = 0;
  213.         while(1)
  214. {
  215.  
  216.         // read the dial
  217.         adcSample();
  218.     int smp =  getAdc(0) ;
  219.     avgSmp += (smp-avgSmp)/4;
  220.         adv = avgSmp * 1200 / 4096 - 300;
  221.  
  222.  
  223.         /* initialise the display */
  224.         chMtxLock(&mutexDisplay);
  225.         clearDisplay();
  226.  
  227.     font_gotoxy(0,0);
  228.  
  229.     print_digits(64, 16, 4, 1, adv);
  230.  
  231.     print_digits(64, 33, 4,-1, getRPM());
  232.     print_scaled_string("RPM:",0,33,4,256);
  233.     print_scaled_string("ADV:",0,16,4,256);
  234.  
  235.     font_gotoxy(0,0);
  236.     font_puts("count:");
  237.     font_sig_digits(64, 0,1,-1,  getCount());
  238.  
  239.     font_sig_digits(64,52,4,-1,  getDelta());
  240.  
  241.  
  242.          display();
  243.  
  244.          chMtxUnlock(&mutexDisplay);
  245.          //
  246. //       invertDisplay(x & 32);
  247.  
  248.          chThdSleep(chTimeMS2I(10));
  249.  
  250. }
  251.  
  252.  // shell thread  code . dead at present
  253. //        /*
  254. //         * Normal main() thread activity, spawning shells.
  255. //         */
  256. //        while (true) {
  257. //          if (SDU1.config->usbp->state == USB_ACTIVE) {
  258. //o           thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  259. //                                                    "shell", NORMALPRIO + 1,
  260. //                                                    shellThread, (void *)&shell_cfg1);
  261. //            chThdWait(shelltp);               /* Waiting termination.             */
  262. //          }
  263. //          chThdSleepMilliseconds(1000);
  264. //        }
  265.  
  266. }
  267.