Rev 21 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 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 | |||
| 7 | mjames | 28 | #include <stdio.h> |
| 2 | mjames | 29 | #include <string.h> |
| 30 | |||
| 31 | #include "ch.h" |
||
| 32 | #include "hal.h" |
||
| 7 | mjames | 33 | //#include "test.h" |
| 2 | mjames | 34 | #include "shell.h" |
| 35 | #include "evtimer.h" |
||
| 36 | #include "chprintf.h" |
||
| 37 | |||
| 16 | mjames | 38 | |
| 39 | |||
| 22 | mjames | 40 | #if (HAL_USE_SERIAL_ == TRUE) |
| 41 | #include "cfg.h" |
||
| 15 | mjames | 42 | #endif |
| 2 | mjames | 43 | |
| 7 | mjames | 44 | |
| 5 | mjames | 45 | #include "ap_math.h" |
| 2 | mjames | 46 | #include "hardware.h" |
| 6 | mjames | 47 | #include "useAdc.h" |
| 3 | mjames | 48 | #include "spiInterface.h" |
| 49 | #include "SSD1306.h" |
||
| 16 | mjames | 50 | #include "font.h" |
| 2 | mjames | 51 | |
| 6 | mjames | 52 | #include "timer2.h" |
| 2 | mjames | 53 | |
| 15 | mjames | 54 | |
| 16 | mjames | 55 | #include "shellCmds.h" |
| 15 | mjames | 56 | |
| 57 | |||
| 6 | mjames | 58 | static MUTEX_DECL(mutexDisplay); |
| 59 | |||
| 2 | mjames | 60 | /*===========================================================================*/ |
| 61 | /* Command line related. */ |
||
| 62 | /*===========================================================================*/ |
||
| 63 | |||
| 7 | mjames | 64 | #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048) |
| 65 | #define TEST_WA_SIZE THD_WORKING_AREA_SIZE(256) |
||
| 2 | mjames | 66 | |
| 67 | |||
| 68 | |||
| 15 | mjames | 69 | static const ShellConfig shell_cfg1 = { |
| 22 | mjames | 70 | #if (HAL_USE_SERIAL_ == TRUE) |
| 15 | mjames | 71 | (BaseSequentialStream *)&SDU1, |
| 72 | #else |
||
| 22 | mjames | 73 | (BaseSequentialStream *)&SD2, |
| 15 | mjames | 74 | #endif |
| 16 | mjames | 75 | shellCommands |
| 15 | mjames | 76 | }; |
| 77 | //////// |
||
| 78 | // end of shell stuff |
||
| 79 | |||
| 13 | mjames | 80 | uint16_t sampIndex; |
| 6 | mjames | 81 | |
| 8 | mjames | 82 | static THD_WORKING_AREA(waThread1, 512); |
| 2 | mjames | 83 | static msg_t Thread1(void *arg) { |
| 84 | |||
| 85 | (void) arg; |
||
| 6 | mjames | 86 | chRegSetThreadName("PLL "); |
| 2 | mjames | 87 | while (TRUE) { |
| 19 | mjames | 88 | processPhase (); |
| 89 | chThdSleep(10); |
||
| 11 | mjames | 90 | |
| 2 | mjames | 91 | } |
| 92 | return 0; |
||
| 93 | } |
||
| 94 | |||
| 95 | /* |
||
| 15 | mjames | 96 | * Command Shell Thread |
| 7 | mjames | 97 | */ |
| 8 | mjames | 98 | static THD_WORKING_AREA(waThread2, 512); |
| 7 | mjames | 99 | static msg_t Thread2(void *arg) { |
| 15 | mjames | 100 | thread_t *shelltp = NULL; |
| 16 | mjames | 101 | |
| 102 | chRegSetThreadName("Shell "); |
||
| 103 | /* |
||
| 104 | * in this demo it just performs |
||
| 15 | mjames | 105 | * a shell respawn upon its termination. |
| 106 | */ |
||
| 107 | while (true) { |
||
| 108 | if (!shelltp) { |
||
| 22 | mjames | 109 | #if (HAL_USE_SERIAL_ == TRUE) |
| 110 | if (SDU1.config->p->state == _ACTIVE) { |
||
| 15 | mjames | 111 | /* Spawns a new shell.*/ |
| 112 | shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1); |
||
| 113 | } |
||
| 114 | #else |
||
| 115 | shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1); |
||
| 116 | #endif |
||
| 117 | } |
||
| 118 | else { |
||
| 119 | /* If the previous shell exited.*/ |
||
| 120 | if (chThdTerminatedX(shelltp)) { |
||
| 121 | /* Recovers memory of the previous shell.*/ |
||
| 122 | chThdRelease(shelltp); |
||
| 123 | shelltp = NULL; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | chThdSleepMilliseconds(500); |
||
| 127 | } |
||
| 7 | mjames | 128 | |
| 15 | mjames | 129 | return MSG_OK; |
| 7 | mjames | 130 | } |
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /* |
||
| 2 | mjames | 136 | * Application entry point. |
| 137 | */ |
||
| 138 | int main(void) { |
||
| 139 | // struct EventListener el0, el1; |
||
| 140 | |||
| 141 | /* |
||
| 142 | * System initializations. |
||
| 143 | * - HAL initialization, this also initializes the configured device drivers |
||
| 144 | * and performs the board-specific initializations. |
||
| 145 | * - Kernel initialization, the main() function becomes a thread and the |
||
| 146 | * RTOS is active. |
||
| 147 | */ |
||
| 148 | halInit(); |
||
| 149 | chSysInit(); |
||
| 150 | |||
| 6 | mjames | 151 | |
| 7 | mjames | 152 | |
| 22 | mjames | 153 | #if (HAL_USE_SERIAL_ == TRUE) |
| 15 | mjames | 154 | /* |
| 22 | mjames | 155 | * Initializes a serial-over- CDC driver. |
| 15 | mjames | 156 | */ |
| 157 | sduObjectInit(&SDU1); |
||
| 22 | mjames | 158 | sduStart(&SDU1, &sercfg); |
| 15 | mjames | 159 | |
| 22 | mjames | 160 | #if HAL_USE__DUAL_CDC == TRUE |
| 18 | mjames | 161 | sduObjectInit(&SDU2); |
| 22 | mjames | 162 | sduStart(&SDU2, &sercfg2); |
| 18 | mjames | 163 | #endif |
| 164 | |||
| 15 | mjames | 165 | /* |
| 22 | mjames | 166 | * Activates the driver and then the bus pull-up on D+. |
| 15 | mjames | 167 | * Note, a delay is inserted in order to not have to disconnect the cable |
| 168 | * after a reset. |
||
| 169 | */ |
||
| 22 | mjames | 170 | DisconnectBus(sercfg.p); |
| 15 | mjames | 171 | chThdSleepMilliseconds(1000); |
| 22 | mjames | 172 | Start(sercfg.p, &cfg); |
| 173 | ConnectBus(sercfg.p); |
||
| 15 | mjames | 174 | #else |
| 175 | /* |
||
| 176 | * Initializes serial port. |
||
| 177 | */ |
||
| 178 | sdStart(&SD2, NULL); |
||
| 22 | mjames | 179 | #endif /* HAL_USE_SERIAL_ */ |
| 15 | mjames | 180 | |
| 7 | mjames | 181 | // Ignition timing code |
| 21 | mjames | 182 | initTimers(); |
| 6 | mjames | 183 | |
| 8 | mjames | 184 | useAdc(); |
| 6 | mjames | 185 | |
| 2 | mjames | 186 | /* |
| 187 | * Activates the serial driver 2 using the driver default configuration. |
||
| 188 | */ |
||
| 8 | mjames | 189 | // sdStart(&SD2, NULL); |
| 2 | mjames | 190 | |
| 191 | /* |
||
| 192 | * Shell manager initialization. |
||
| 193 | */ |
||
| 194 | shellInit(); |
||
| 195 | |||
| 5 | mjames | 196 | ap_init(); |
| 3 | mjames | 197 | |
| 5 | mjames | 198 | |
| 199 | /* |
||
| 6 | mjames | 200 | * Creates the PLL thread |
| 5 | mjames | 201 | */ |
| 202 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); |
||
| 203 | |||
| 15 | mjames | 204 | |
| 205 | |||
| 206 | chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL); |
||
| 207 | |||
| 3 | mjames | 208 | /* start the SPI hardware for display */ |
| 209 | ssd1306spiInit(); |
||
| 210 | |||
| 211 | ssd1306_begin( SSD1306_SWITCHCAPVCC, 0) ; |
||
| 6 | mjames | 212 | int spd = 800; |
| 3 | mjames | 213 | |
| 6 | mjames | 214 | int adv = 0; |
| 215 | setRPM(spd); |
||
| 10 | mjames | 216 | int avgSmp = 0; |
| 6 | mjames | 217 | while(1) |
| 3 | mjames | 218 | { |
| 14 | mjames | 219 | |
| 220 | // read the dial |
||
| 10 | mjames | 221 | adcSample(); |
| 222 | int smp = getAdc(0) ; |
||
| 20 | mjames | 223 | avgSmp += (smp-avgSmp)/20; |
| 10 | mjames | 224 | adv = avgSmp * 1200 / 4096 - 300; |
| 6 | mjames | 225 | |
| 20 | mjames | 226 | setAdvance(adv); |
| 6 | mjames | 227 | |
| 20 | mjames | 228 | |
| 3 | mjames | 229 | /* initialise the display */ |
| 6 | mjames | 230 | chMtxLock(&mutexDisplay); |
| 3 | mjames | 231 | clearDisplay(); |
| 232 | |||
| 5 | mjames | 233 | font_gotoxy(0,0); |
| 3 | mjames | 234 | |
| 6 | mjames | 235 | print_digits(64, 16, 4, 1, adv); |
| 3 | mjames | 236 | |
| 11 | mjames | 237 | print_digits(64, 33, 4,-1, getRPM()); |
| 9 | mjames | 238 | print_scaled_string("RPM:",0,33,4,256); |
| 239 | print_scaled_string("ADV:",0,16,4,256); |
||
| 3 | mjames | 240 | |
| 9 | mjames | 241 | font_gotoxy(0,0); |
| 242 | font_puts("count:"); |
||
| 13 | mjames | 243 | font_sig_digits(64, 0,1,-1, getCount()); |
| 3 | mjames | 244 | |
| 11 | mjames | 245 | font_sig_digits(64,52,4,-1, getDelta()); |
| 6 | mjames | 246 | |
| 11 | mjames | 247 | |
| 3 | mjames | 248 | display(); |
| 6 | mjames | 249 | |
| 22 | mjames | 250 | |
| 7 | mjames | 251 | chMtxUnlock(&mutexDisplay); |
| 6 | mjames | 252 | // |
| 5 | mjames | 253 | // invertDisplay(x & 32); |
| 3 | mjames | 254 | |
| 7 | mjames | 255 | chThdSleep(chTimeMS2I(10)); |
| 3 | mjames | 256 | |
| 257 | } |
||
| 258 | |||
| 7 | mjames | 259 | // shell thread code . dead at present |
| 260 | // /* |
||
| 261 | // * Normal main() thread activity, spawning shells. |
||
| 262 | // */ |
||
| 263 | // while (true) { |
||
| 22 | mjames | 264 | // if (SDU1.config->p->state == _ACTIVE) { |
| 8 | mjames | 265 | //o thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, |
| 7 | mjames | 266 | // "shell", NORMALPRIO + 1, |
| 267 | // shellThread, (void *)&shell_cfg1); |
||
| 268 | // chThdWait(shelltp); /* Waiting termination. */ |
||
| 269 | // } |
||
| 270 | // chThdSleepMilliseconds(1000); |
||
| 271 | // } |
||
| 272 | |||
| 2 | mjames | 273 | } |