Rev 14 | Rev 16 | Go to most recent revision | 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 | |||
15 | mjames | 38 | #if (HAL_USE_SERIAL_USB == TRUE) |
7 | mjames | 39 | #include "usbcfg.h" |
15 | mjames | 40 | #endif |
2 | mjames | 41 | |
7 | mjames | 42 | |
5 | mjames | 43 | #include "ap_math.h" |
2 | mjames | 44 | #include "hardware.h" |
6 | mjames | 45 | #include "useAdc.h" |
3 | mjames | 46 | #include "spiInterface.h" |
47 | #include "SSD1306.h" |
||
2 | mjames | 48 | |
6 | mjames | 49 | #include "timer2.h" |
2 | mjames | 50 | |
15 | mjames | 51 | |
52 | |||
53 | |||
6 | mjames | 54 | static MUTEX_DECL(mutexDisplay); |
55 | |||
2 | mjames | 56 | /*===========================================================================*/ |
57 | /* Command line related. */ |
||
58 | /*===========================================================================*/ |
||
59 | |||
7 | mjames | 60 | #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048) |
61 | #define TEST_WA_SIZE THD_WORKING_AREA_SIZE(256) |
||
2 | mjames | 62 | |
63 | static void cmd_mem(BaseChannel *chp, int argc, char *argv[]) { |
||
7 | mjames | 64 | size_t n, size , largest; |
2 | mjames | 65 | |
66 | (void) argv; |
||
67 | if (argc > 0) { |
||
68 | chprintf(chp, "Usage: mem\r\n"); |
||
69 | return; |
||
70 | } |
||
7 | mjames | 71 | n = chHeapStatus(NULL, &size, &largest); |
15 | mjames | 72 | chprintf(chp, "core free memory : %u bytes\r\n", chCoreGetStatusX()); |
7 | mjames | 73 | chprintf(chp, "heap fragments : %u \r\n",n); |
74 | chprintf(chp, "largest fragment : %u \r\n",largest); |
||
2 | mjames | 75 | chprintf(chp, "heap free total : %u bytes\r\n", size); |
76 | } |
||
77 | |||
7 | mjames | 78 | static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) { |
79 | static const char *states[] = {CH_STATE_NAMES}; |
||
80 | thread_t *tp; |
||
2 | mjames | 81 | |
7 | mjames | 82 | (void)argv; |
83 | if (argc > 0) { |
||
84 | shellUsage(chp, "threads"); |
||
85 | return; |
||
86 | } |
||
87 | chprintf(chp, "stklimit stack addr refs prio state name\r\n" SHELL_NEWLINE_STR); |
||
88 | tp = chRegFirstThread(); |
||
89 | do { |
||
90 | #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE) |
||
91 | uint32_t stklimit = (uint32_t)tp->wabase; |
||
92 | #else |
||
93 | uint32_t stklimit = 0U; |
||
94 | #endif |
||
95 | chprintf(chp, "%08lx %08lx %08lx %4lu %4lu %9s %12s" SHELL_NEWLINE_STR, |
||
96 | stklimit, (uint32_t)tp->ctx.sp, (uint32_t)tp, |
||
97 | (uint32_t)tp->refs - 1, (uint32_t)tp->prio, states[tp->state], |
||
98 | tp->name == NULL ? "" : tp->name); |
||
99 | tp = chRegNextThread(tp); |
||
100 | } while (tp != NULL); |
||
2 | mjames | 101 | } |
102 | |||
103 | static void cmd_test(BaseChannel *chp, int argc, char *argv[]) { |
||
7 | mjames | 104 | thread_t *tp; |
2 | mjames | 105 | |
106 | (void) argv; |
||
107 | if (argc > 0) { |
||
108 | chprintf(chp, "Usage: test\r\n"); |
||
109 | return; |
||
110 | } |
||
7 | mjames | 111 | //tp = chThdCreateFromHeap(NULL, TEST_WA_SIZE, chThdGetPriority(), TestThread, |
112 | // chp); |
||
2 | mjames | 113 | if (tp == NULL) { |
114 | chprintf(chp, "out of memory\r\n"); |
||
115 | return; |
||
116 | } |
||
117 | chThdWait(tp); |
||
118 | } |
||
119 | |||
120 | static const ShellCommand commands[] = { { "mem", cmd_mem }, { "threads", |
||
121 | cmd_threads }, { "test", cmd_test }, { NULL, NULL } }; |
||
122 | |||
123 | |||
15 | mjames | 124 | |
125 | |||
126 | |||
127 | |||
128 | static const ShellConfig shell_cfg1 = { |
||
129 | #if (HAL_USE_SERIAL_USB == TRUE) |
||
130 | (BaseSequentialStream *)&SDU1, |
||
131 | #else |
||
132 | (BaseSequentialStream *)&SD1, |
||
133 | #endif |
||
134 | commands |
||
135 | }; |
||
136 | //////// |
||
137 | // end of shell stuff |
||
138 | |||
13 | mjames | 139 | uint16_t sampIndex; |
6 | mjames | 140 | |
8 | mjames | 141 | static THD_WORKING_AREA(waThread1, 512); |
2 | mjames | 142 | static msg_t Thread1(void *arg) { |
143 | |||
144 | (void) arg; |
||
6 | mjames | 145 | chRegSetThreadName("PLL "); |
2 | mjames | 146 | while (TRUE) { |
13 | mjames | 147 | sampIndex = getNextRefPulseIndex(); |
14 | mjames | 148 | if(slowPulse() == 0) |
13 | mjames | 149 | processNextPulse(sampIndex); |
11 | mjames | 150 | |
2 | mjames | 151 | } |
152 | return 0; |
||
153 | } |
||
154 | |||
155 | /* |
||
15 | mjames | 156 | * Command Shell Thread |
7 | mjames | 157 | */ |
8 | mjames | 158 | static THD_WORKING_AREA(waThread2, 512); |
7 | mjames | 159 | static msg_t Thread2(void *arg) { |
15 | mjames | 160 | thread_t *shelltp = NULL; |
161 | /* |
||
162 | * Normal main() thread activity, in this demo it just performs |
||
163 | * a shell respawn upon its termination. |
||
164 | */ |
||
165 | while (true) { |
||
166 | if (!shelltp) { |
||
167 | #if (HAL_USE_SERIAL_USB == TRUE) |
||
168 | if (SDU1.config->usbp->state == USB_ACTIVE) { |
||
169 | /* Spawns a new shell.*/ |
||
170 | shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1); |
||
171 | } |
||
172 | #else |
||
173 | shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1); |
||
174 | #endif |
||
175 | } |
||
176 | else { |
||
177 | /* If the previous shell exited.*/ |
||
178 | if (chThdTerminatedX(shelltp)) { |
||
179 | /* Recovers memory of the previous shell.*/ |
||
180 | chThdRelease(shelltp); |
||
181 | shelltp = NULL; |
||
182 | } |
||
183 | } |
||
184 | chThdSleepMilliseconds(500); |
||
185 | } |
||
7 | mjames | 186 | |
15 | mjames | 187 | return MSG_OK; |
7 | mjames | 188 | } |
189 | |||
190 | |||
191 | |||
192 | |||
193 | /* |
||
2 | mjames | 194 | * Application entry point. |
195 | */ |
||
196 | int main(void) { |
||
197 | // struct EventListener el0, el1; |
||
198 | |||
199 | /* |
||
200 | * System initializations. |
||
201 | * - HAL initialization, this also initializes the configured device drivers |
||
202 | * and performs the board-specific initializations. |
||
203 | * - Kernel initialization, the main() function becomes a thread and the |
||
204 | * RTOS is active. |
||
205 | */ |
||
206 | halInit(); |
||
207 | chSysInit(); |
||
208 | |||
6 | mjames | 209 | |
7 | mjames | 210 | |
15 | mjames | 211 | #if (HAL_USE_SERIAL_USB == TRUE) |
212 | /* |
||
213 | * Initializes a serial-over-USB CDC driver. |
||
214 | */ |
||
215 | sduObjectInit(&SDU1); |
||
216 | sduStart(&SDU1, &serusbcfg); |
||
217 | |||
218 | /* |
||
219 | * Activates the USB driver and then the USB bus pull-up on D+. |
||
220 | * Note, a delay is inserted in order to not have to disconnect the cable |
||
221 | * after a reset. |
||
222 | */ |
||
223 | usbDisconnectBus(serusbcfg.usbp); |
||
224 | chThdSleepMilliseconds(1000); |
||
225 | usbStart(serusbcfg.usbp, &usbcfg); |
||
226 | usbConnectBus(serusbcfg.usbp); |
||
227 | #else |
||
228 | /* |
||
229 | * Initializes serial port. |
||
230 | */ |
||
231 | sdStart(&SD2, NULL); |
||
232 | #endif /* HAL_USE_SERIAL_USB */ |
||
233 | |||
7 | mjames | 234 | // Ignition timing code |
6 | mjames | 235 | initTimer2(); |
236 | |||
8 | mjames | 237 | useAdc(); |
6 | mjames | 238 | |
2 | mjames | 239 | /* |
240 | * Activates the serial driver 2 using the driver default configuration. |
||
241 | */ |
||
8 | mjames | 242 | // sdStart(&SD2, NULL); |
2 | mjames | 243 | |
244 | /* |
||
245 | * Shell manager initialization. |
||
246 | */ |
||
247 | shellInit(); |
||
248 | |||
5 | mjames | 249 | ap_init(); |
3 | mjames | 250 | |
5 | mjames | 251 | |
252 | /* |
||
6 | mjames | 253 | * Creates the PLL thread |
5 | mjames | 254 | */ |
255 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); |
||
256 | |||
15 | mjames | 257 | |
258 | |||
259 | chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL); |
||
260 | |||
3 | mjames | 261 | /* start the SPI hardware for display */ |
262 | ssd1306spiInit(); |
||
263 | |||
264 | ssd1306_begin( SSD1306_SWITCHCAPVCC, 0) ; |
||
6 | mjames | 265 | int spd = 800; |
3 | mjames | 266 | |
6 | mjames | 267 | int adv = 0; |
268 | setRPM(spd); |
||
10 | mjames | 269 | int avgSmp = 0; |
6 | mjames | 270 | while(1) |
3 | mjames | 271 | { |
14 | mjames | 272 | |
273 | // read the dial |
||
10 | mjames | 274 | adcSample(); |
275 | int smp = getAdc(0) ; |
||
276 | avgSmp += (smp-avgSmp)/4; |
||
277 | adv = avgSmp * 1200 / 4096 - 300; |
||
6 | mjames | 278 | |
279 | |||
3 | mjames | 280 | /* initialise the display */ |
6 | mjames | 281 | chMtxLock(&mutexDisplay); |
3 | mjames | 282 | clearDisplay(); |
283 | |||
5 | mjames | 284 | font_gotoxy(0,0); |
3 | mjames | 285 | |
6 | mjames | 286 | print_digits(64, 16, 4, 1, adv); |
3 | mjames | 287 | |
11 | mjames | 288 | print_digits(64, 33, 4,-1, getRPM()); |
9 | mjames | 289 | print_scaled_string("RPM:",0,33,4,256); |
290 | print_scaled_string("ADV:",0,16,4,256); |
||
3 | mjames | 291 | |
9 | mjames | 292 | font_gotoxy(0,0); |
293 | font_puts("count:"); |
||
13 | mjames | 294 | font_sig_digits(64, 0,1,-1, getCount()); |
3 | mjames | 295 | |
11 | mjames | 296 | font_sig_digits(64,52,4,-1, getDelta()); |
6 | mjames | 297 | |
11 | mjames | 298 | |
3 | mjames | 299 | display(); |
6 | mjames | 300 | |
7 | mjames | 301 | chMtxUnlock(&mutexDisplay); |
6 | mjames | 302 | // |
5 | mjames | 303 | // invertDisplay(x & 32); |
3 | mjames | 304 | |
7 | mjames | 305 | chThdSleep(chTimeMS2I(10)); |
3 | mjames | 306 | |
307 | } |
||
308 | |||
7 | mjames | 309 | // shell thread code . dead at present |
310 | // /* |
||
311 | // * Normal main() thread activity, spawning shells. |
||
312 | // */ |
||
313 | // while (true) { |
||
314 | // if (SDU1.config->usbp->state == USB_ACTIVE) { |
||
8 | mjames | 315 | //o thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, |
7 | mjames | 316 | // "shell", NORMALPRIO + 1, |
317 | // shellThread, (void *)&shell_cfg1); |
||
318 | // chThdWait(shelltp); /* Waiting termination. */ |
||
319 | // } |
||
320 | // chThdSleepMilliseconds(1000); |
||
321 | // } |
||
322 | |||
2 | mjames | 323 | } |