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