Subversion Repositories ChibiGauge

Rev

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 "useAdc.h"
  46.  
  47.  
  48. #include "shellCmds.h"
  49.  
  50.  
  51. static MUTEX_DECL(mutexDisplay);
  52.  
  53. /*===========================================================================*/
  54. /* Command line related.                                                     */
  55. /*===========================================================================*/
  56.  
  57. #define SHELL_WA_SIZE   THD_WORKING_AREA_SIZE(2048)
  58. #define TEST_WA_SIZE    THD_WORKING_AREA_SIZE(256)
  59.  
  60.  
  61.  
  62. static const ShellConfig shell_cfg1 = {
  63. #if (HAL_USE_SERIAL_USB == TRUE)
  64.   (BaseSequentialStream *)&SDU1,
  65. #else
  66.   (BaseSequentialStream *)&SD1,
  67. #endif
  68.   shellCommands
  69. };
  70. ////////
  71. // end of shell stuff
  72.  
  73. uint16_t sampIndex;
  74.  
  75. void setDriveA(uint8_t bit)
  76. {
  77.         if(bit)
  78.         {
  79.             palSetPad(GPIOA, GPIOA_A1);
  80.            palClearPad(GPIOA, GPIOA_A2);
  81.         }
  82.         else
  83.         {
  84.                 palClearPad(GPIOA, GPIOA_A1);
  85.                 palSetPad(GPIOA, GPIOA_A2);
  86.         }
  87. }
  88.  
  89. void setDriveB(uint8_t bit)
  90. {
  91.         if(bit)
  92.         {
  93.            palSetPad(GPIOA, GPIOA_B1);
  94.            palClearPad(GPIOA, GPIOA_B2);
  95.         }
  96.         else
  97.         {
  98.                 palClearPad(GPIOA, GPIOA_B1);
  99.                 palSetPad(GPIOA, GPIOA_B2);
  100.         }
  101. }
  102.  
  103.  
  104. volatile int origin = 0;;
  105. volatile int target = 630;
  106. volatile int count = 0;
  107.  
  108. static THD_WORKING_AREA(waThread1, 512);
  109. static msg_t Thread1(void *arg) {
  110.  
  111.         (void) arg;
  112.         unsigned const fast=10 ;
  113.         unsigned const slow = 40;
  114.         unsigned const range = slow-fast;
  115.      unsigned  del = fast;
  116.      int step  = 1;
  117.    chRegSetThreadName("Step");
  118.         while (TRUE) {
  119.  
  120.                 switch  (count % 4)
  121.                 {
  122.                 case 0: setDriveA(1); setDriveB(0); break;
  123.                 case 1: setDriveA(1); setDriveB(1); break;
  124.                 case 2: setDriveA(0); setDriveB(1); break;
  125.                 case 3: setDriveA(0); setDriveB(0); break;
  126.                 }
  127.                 chThdSleep(del);
  128.                 count = count + step;
  129.  
  130.                 // all this calculates minimum distance from
  131.                 // target or origin
  132.         int d1= count-origin;
  133.         if(d1<0) d1 = -d1;
  134.         int d2= count-target;
  135.         if(d2<0) d2 = -d2;
  136.         // finally, minimum distance
  137.         int dist  = d1<d2 ? d1 : d2;
  138.  
  139.  
  140.         del = fast ;
  141.         if(dist < range) // inside lower bound of distance
  142.         {
  143.                 del = slow-dist;
  144.         }
  145.  
  146.                 if(count < target ) { step = 1; }
  147.                 if(count > target ) { step =-1; }
  148.                 if(count == target) { step = 0; }
  149.  
  150.         }
  151.         return 0;
  152. }
  153.  
  154. /*
  155.  * Command Shell Thread
  156.  */
  157. static THD_WORKING_AREA(waThread2, 512);
  158. static msg_t Thread2(void *arg) {
  159.         thread_t *shelltp = NULL;
  160.  
  161.         chRegSetThreadName("Shell ");
  162. /*
  163.            * in this demo it just performs
  164.            * a shell respawn upon its termination.
  165.            */
  166.           while (true) {
  167.             if (!shelltp) {
  168.         #if (HAL_USE_SERIAL_USB == TRUE)
  169.               if (SDU1.config->usbp->state == USB_ACTIVE) {
  170.                 /* Spawns a new shell.*/
  171.                 shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
  172.               }
  173.         #else
  174.                 shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
  175.         #endif
  176.             }
  177.             else {
  178.               /* If the previous shell exited.*/
  179.               if (chThdTerminatedX(shelltp)) {
  180.                 /* Recovers memory of the previous shell.*/
  181.                 chThdRelease(shelltp);
  182.                 shelltp = NULL;
  183.               }
  184.             }
  185.             chThdSleepMilliseconds(500);
  186.           }
  187.  
  188.         return MSG_OK;
  189. }
  190.  
  191.  
  192.  
  193.  
  194. /*
  195.  * Application entry point.
  196.  */
  197. int main(void) {
  198. //  struct EventListener el0, el1;
  199.  
  200.         /*
  201.          * System initializations.
  202.          * - HAL initialization, this also initializes the configured device drivers
  203.          *   and performs the board-specific initializations.
  204.          * - Kernel initialization, the main() function becomes a thread and the
  205.          *   RTOS is active.
  206.          */
  207.         halInit();
  208.         chSysInit();
  209.  
  210. #if (HAL_USE_SERIAL_USB == TRUE)
  211.   /*
  212.    * Initializes a serial-over-USB CDC driver.
  213.    */
  214.   sduObjectInit(&SDU1);
  215.   sduStart(&SDU1, &serusbcfg);
  216.  
  217. #if  HAL_USE_USB_DUAL_CDC == TRUE
  218.   sduObjectInit(&SDU2);
  219.   sduStart(&SDU2, &serusbcfg2);
  220. #endif
  221.  
  222.   /*
  223.    * Activates the USB driver and then the USB bus pull-up on D+.
  224.    * Note, a delay is inserted in order to not have to disconnect the cable
  225.    * after a reset.
  226.    */
  227.   usbDisconnectBus(serusbcfg.usbp);
  228.   chThdSleepMilliseconds(1000);
  229.   usbStart(serusbcfg.usbp, &usbcfg);
  230.   usbConnectBus(serusbcfg.usbp);
  231. #else
  232.   /*
  233.    * Initializes serial port.
  234.    */
  235.   sdStart(&SD2, NULL);
  236. #endif /* HAL_USE_SERIAL_USB */
  237.  
  238.  
  239.    useAdc();
  240.  
  241.         /*
  242.          * Shell manager initialization.
  243.          */
  244.         shellInit();
  245.  
  246.  
  247.          /*
  248.          * Creates the PLL thread
  249.          */
  250.         chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  251.  
  252.  
  253.  
  254.         chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
  255.  
  256.         /* start the SPI hardware for display */
  257.         while(1)
  258. {
  259.  
  260.         // read the dial
  261.         adcSample();
  262.  
  263.  
  264.  
  265.         origin = count;
  266.  
  267.     target = getAdc(0)*630L/4096L;
  268.  
  269.  
  270.  
  271.          chThdSleep(chTimeMS2I(100));
  272.  
  273. }
  274.  
  275.  
  276. }
  277.