Rev 5 | Rev 7 | 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 | |||
| 28 | #include <string.h> |
||
| 29 | |||
| 30 | #include "ch.h" |
||
| 31 | #include "hal.h" |
||
| 32 | #include "test.h" |
||
| 33 | #include "shell.h" |
||
| 34 | #include "evtimer.h" |
||
| 35 | #include "chprintf.h" |
||
| 36 | |||
| 37 | |||
| 5 | mjames | 38 | #include "ap_math.h" |
| 2 | mjames | 39 | #include "hardware.h" |
| 6 | mjames | 40 | #include "useAdc.h" |
| 3 | mjames | 41 | #include "spiInterface.h" |
| 42 | #include "SSD1306.h" |
||
| 2 | mjames | 43 | |
| 6 | mjames | 44 | #include "timer2.h" |
| 2 | mjames | 45 | |
| 6 | mjames | 46 | static MUTEX_DECL(mutexDisplay); |
| 47 | |||
| 2 | mjames | 48 | /*===========================================================================*/ |
| 49 | /* Command line related. */ |
||
| 50 | /*===========================================================================*/ |
||
| 51 | |||
| 52 | #define SHELL_WA_SIZE THD_WA_SIZE(2048) |
||
| 53 | #define TEST_WA_SIZE THD_WA_SIZE(256) |
||
| 54 | |||
| 55 | static void cmd_mem(BaseChannel *chp, int argc, char *argv[]) { |
||
| 56 | size_t n, size; |
||
| 57 | |||
| 58 | (void) argv; |
||
| 59 | if (argc > 0) { |
||
| 60 | chprintf(chp, "Usage: mem\r\n"); |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | n = chHeapStatus(NULL, &size); |
||
| 64 | chprintf(chp, "core free memory : %u bytes\r\n", chCoreStatus()); |
||
| 65 | chprintf(chp, "heap fragments : %u\r\n", n); |
||
| 66 | chprintf(chp, "heap free total : %u bytes\r\n", size); |
||
| 67 | } |
||
| 68 | |||
| 69 | static void cmd_threads(BaseChannel *chp, int argc, char *argv[]) { |
||
| 70 | static const char *states[] = { THD_STATE_NAMES }; |
||
| 71 | Thread *tp; |
||
| 72 | |||
| 73 | (void) argv; |
||
| 74 | if (argc > 0) { |
||
| 75 | chprintf(chp, "Usage: threads\r\n"); |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | chprintf(chp, " addr stack prio refs state time\r\n"); |
||
| 79 | tp = chRegFirstThread(); |
||
| 80 | do { |
||
| 81 | chprintf(chp, "%.8lx %.8lx %4lu %4lu %9s %lu\r\n", (uint32_t) tp, |
||
| 82 | (uint32_t) tp->p_ctx.r13, (uint32_t) tp->p_prio, |
||
| 83 | (uint32_t)(tp->p_refs - 1), states[tp->p_state], |
||
| 84 | (uint32_t) tp->p_time); |
||
| 85 | tp = chRegNextThread(tp); |
||
| 86 | } while (tp != NULL); |
||
| 87 | } |
||
| 88 | |||
| 89 | static void cmd_test(BaseChannel *chp, int argc, char *argv[]) { |
||
| 90 | Thread *tp; |
||
| 91 | |||
| 92 | (void) argv; |
||
| 93 | if (argc > 0) { |
||
| 94 | chprintf(chp, "Usage: test\r\n"); |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | tp = chThdCreateFromHeap(NULL, TEST_WA_SIZE, chThdGetPriority(), TestThread, |
||
| 98 | chp); |
||
| 99 | if (tp == NULL) { |
||
| 100 | chprintf(chp, "out of memory\r\n"); |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | chThdWait(tp); |
||
| 104 | } |
||
| 105 | |||
| 106 | static const ShellCommand commands[] = { { "mem", cmd_mem }, { "threads", |
||
| 107 | cmd_threads }, { "test", cmd_test }, { NULL, NULL } }; |
||
| 108 | |||
| 109 | static const ShellConfig shell_cfg1 = { (BaseChannel *) &SD2, commands }; |
||
| 110 | |||
| 111 | /* |
||
| 112 | * Red LEDs blinker thread, times are in milliseconds. |
||
| 113 | */ |
||
| 6 | mjames | 114 | uint16_t sampleVal; |
| 115 | |||
| 2 | mjames | 116 | static WORKING_AREA(waThread1, 128); |
| 117 | static msg_t Thread1(void *arg) { |
||
| 118 | |||
| 119 | (void) arg; |
||
| 6 | mjames | 120 | chRegSetThreadName("PLL "); |
| 2 | mjames | 121 | while (TRUE) { |
| 6 | mjames | 122 | sampleVal = getNextPulse(); |
| 123 | |||
| 124 | chThdSleep(1); |
||
| 2 | mjames | 125 | } |
| 126 | return 0; |
||
| 127 | } |
||
| 128 | |||
| 129 | /* |
||
| 130 | * Application entry point. |
||
| 131 | */ |
||
| 132 | int main(void) { |
||
| 133 | Thread *shelltp = NULL; |
||
| 134 | // struct EventListener el0, el1; |
||
| 135 | |||
| 136 | /* |
||
| 137 | * System initializations. |
||
| 138 | * - HAL initialization, this also initializes the configured device drivers |
||
| 139 | * and performs the board-specific initializations. |
||
| 140 | * - Kernel initialization, the main() function becomes a thread and the |
||
| 141 | * RTOS is active. |
||
| 142 | */ |
||
| 143 | halInit(); |
||
| 144 | chSysInit(); |
||
| 145 | |||
| 6 | mjames | 146 | |
| 147 | initTimer2(); |
||
| 148 | |||
| 149 | useAdc(); |
||
| 150 | |||
| 2 | mjames | 151 | /* |
| 152 | * Activates the serial driver 2 using the driver default configuration. |
||
| 153 | */ |
||
| 154 | sdStart(&SD2, NULL); |
||
| 155 | |||
| 156 | /* |
||
| 157 | * Shell manager initialization. |
||
| 158 | */ |
||
| 159 | shellInit(); |
||
| 160 | |||
| 5 | mjames | 161 | ap_init(); |
| 3 | mjames | 162 | |
| 5 | mjames | 163 | |
| 164 | /* |
||
| 6 | mjames | 165 | * Creates the PLL thread |
| 5 | mjames | 166 | */ |
| 167 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); |
||
| 168 | |||
| 3 | mjames | 169 | /* start the SPI hardware for display */ |
| 170 | ssd1306spiInit(); |
||
| 171 | |||
| 172 | ssd1306_begin( SSD1306_SWITCHCAPVCC, 0) ; |
||
| 6 | mjames | 173 | int spd = 800; |
| 3 | mjames | 174 | |
| 6 | mjames | 175 | int adv = 0; |
| 176 | setRPM(spd); |
||
| 177 | |||
| 178 | while(1) |
||
| 3 | mjames | 179 | { |
| 6 | mjames | 180 | spd = getRPM(); |
| 181 | |||
| 182 | adv = getAdc(0) * 1200 / 4096 - 300; |
||
| 183 | |||
| 184 | |||
| 3 | mjames | 185 | /* initialise the display */ |
| 6 | mjames | 186 | chMtxLock(&mutexDisplay); |
| 3 | mjames | 187 | clearDisplay(); |
| 188 | |||
| 5 | mjames | 189 | font_gotoxy(0,0); |
| 3 | mjames | 190 | |
| 6 | mjames | 191 | print_digits(64, 16, 4, 1, adv); |
| 3 | mjames | 192 | |
| 6 | mjames | 193 | print_digits(64, 32, 4,-1, spd); |
| 194 | print_large_string("RPM:",0,32,4); |
||
| 195 | print_large_string("ADV:",0,16,4); |
||
| 3 | mjames | 196 | |
| 6 | mjames | 197 | font_puts("count"); |
| 198 | font_sig_digits(12, 0,1,-1, sampleVal); |
||
| 3 | mjames | 199 | |
| 6 | mjames | 200 | |
| 3 | mjames | 201 | display(); |
| 6 | mjames | 202 | |
| 203 | chMtxUnlock(); |
||
| 204 | // |
||
| 5 | mjames | 205 | // invertDisplay(x & 32); |
| 3 | mjames | 206 | |
| 5 | mjames | 207 | chThdSleep(MS2ST(10)); |
| 3 | mjames | 208 | |
| 209 | } |
||
| 210 | |||
| 2 | mjames | 211 | |
| 212 | /* |
||
| 213 | * Normal main() thread activity, in this demo it does nothing except |
||
| 214 | * sleeping in a loop and listen for events. |
||
| 215 | */ |
||
| 216 | while (TRUE) { |
||
| 217 | if (!shelltp) |
||
| 218 | shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); |
||
| 219 | else if (chThdTerminated(shelltp)) { |
||
| 220 | chThdRelease(shelltp); /* Recovers memory of the previous shell. */ |
||
| 221 | shelltp = NULL; /* Triggers spawning of a new shell. */ |
||
| 222 | } |
||
| 223 | chThdSleep(MS2ST(100)); |
||
| 224 | // chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); |
||
| 225 | } |
||
| 226 | return 0; |
||
| 227 | } |