/** * Some configuration changes require full firmware reset. * Once day we will write graceful shutdown, but that would be one day. */ static void scheduleReboot(void) { scheduleMsg(&sharedLogger, "Rebooting in 5 seconds..."); lockAnyContext(); chVTSetI(&resetTimer, MS2ST(5000), (vtfunc_t) rebootNow, NULL); unlockAnyContext(); }
static msg_t tsThreadEntryPoint(void *arg) { (void) arg; chRegSetThreadName("tunerstudio thread"); int wasReady = false; while (true) { int isReady = ts_serail_ready(); if (!isReady) { chThdSleepMilliseconds(10); wasReady = false; continue; } if (!wasReady) { wasReady = TRUE; // scheduleSimpleMsg(&logger, "ts channel is now ready ", hTimeNow()); } tsCounter++; int recieved = chSequentialStreamRead(getTsSerialDevice(), &firstByte, 1); if (recieved != 1) { tunerStudioError("ERROR: no command"); continue; } // scheduleMsg(&logger, "Got first=%x=[%c]", firstByte, firstByte); if (handlePlainCommand(firstByte)) continue; recieved = chSequentialStreamRead(getTsSerialDevice(), &secondByte, 1); if (recieved != 1) { tunerStudioError("ERROR: no second"); continue; } // scheduleMsg(&logger, "Got secondByte=%x=[%c]", secondByte, secondByte); uint32_t incomingPacketSize = firstByte * 256 + secondByte; if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(crcIoBuffer) - CRC_WRAPPING_SIZE)) { scheduleMsg(&logger, "TunerStudio: invalid size: %d", incomingPacketSize); tunerStudioError("ERROR: size"); sendErrorCode(); continue; } recieved = chnReadTimeout(getTsSerialDevice(), crcIoBuffer, 1, MS2ST(TS_READ_TIMEOUT)); if (recieved != 1) { tunerStudioError("ERROR: did not receive command"); continue; } char command = crcIoBuffer[0]; if (!isKnownCommand(command)) { scheduleMsg(&logger, "unexpected command %x", command); sendErrorCode(); continue; } // scheduleMsg(&logger, "TunerStudio: reading %d+4 bytes(s)", incomingPacketSize); recieved = chnReadTimeout(getTsSerialDevice(), (uint8_t * ) (crcIoBuffer + 1), incomingPacketSize + CRC_VALUE_SIZE - 1, MS2ST(TS_READ_TIMEOUT)); int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1; if (recieved != expectedSize) { scheduleMsg(&logger, "got ONLY %d for packet size %d/%d for command %c", recieved, incomingPacketSize, expectedSize, command); tunerStudioError("ERROR: not enough"); continue; } uint32_t expectedCrc = *(uint32_t*) (crcIoBuffer + incomingPacketSize); expectedCrc = SWAP_UINT32(expectedCrc); uint32_t actualCrc = crc32(crcIoBuffer, incomingPacketSize); if (actualCrc != expectedCrc) { scheduleMsg(&logger, "TunerStudio: CRC %x %x %x %x", crcIoBuffer[incomingPacketSize + 0], crcIoBuffer[incomingPacketSize + 1], crcIoBuffer[incomingPacketSize + 2], crcIoBuffer[incomingPacketSize + 3]); scheduleMsg(&logger, "TunerStudio: command %c actual CRC %x/expected %x", crcIoBuffer[0], actualCrc, expectedCrc); tunerStudioError("ERROR: CRC issue"); continue; } // scheduleMsg(&logger, "TunerStudio: P00-07 %x %x %x %x %x %x %x %x", crcIoBuffer[0], crcIoBuffer[1], // crcIoBuffer[2], crcIoBuffer[3], crcIoBuffer[4], crcIoBuffer[5], crcIoBuffer[6], crcIoBuffer[7]); int success = tunerStudioHandleCrcCommand(crcIoBuffer, incomingPacketSize); if (!success) print("got unexpected TunerStudio command %x:%c\r\n", command, command); } #if defined __GNUC__ return 0; #endif }
static void periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { engine->periodicFastCallback(); chVTSetAny(&periodicFastTimer, MS2ST(20), (vtfunc_t) &periodicFastCallback, engine); }
/* * Application entry point. */ int main(void) { unsigned i; static uint8_t patterns1[4096], patterns2[4096], buf1[4096], buf2[4096]; /* System initializations. - HAL initialization, this also initializes the configured device drivers and performs the board-specific initializations. - Kernel initialization, the main() function becomes a thread and the RTOS is active.*/ halInit(); chSysInit(); /* Creates the blinker thread.*/ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 10, Thread1, NULL); /* Activates the ADC1 driver and the thermal sensor.*/ adcStart(&ADCD1, NULL); adcSTM32EnableTSVREFE(); /* Starts an ADC continuous conversion.*/ adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH); /* Activating SPI drivers.*/ spiStart(&SPID1, &hs_spicfg); spiStart(&SPID2, &hs_spicfg); spiStart(&SPID3, &hs_spicfg); /* Starting SPI threads instances.*/ chThdCreateStatic(waSPI1, sizeof(waSPI1), NORMALPRIO + 1, spi_thread, &SPID1); chThdCreateStatic(waSPI2, sizeof(waSPI2), NORMALPRIO + 1, spi_thread, &SPID2); chThdCreateStatic(waSPI3, sizeof(waSPI3), NORMALPRIO + 1, spi_thread, &SPID3); /* Allocating two DMA2 streams for memory copy operations.*/ if (dmaStreamAllocate(STM32_DMA2_STREAM6, 0, NULL, NULL)) chSysHalt(); if (dmaStreamAllocate(STM32_DMA2_STREAM7, 0, NULL, NULL)) chSysHalt(); for (i = 0; i < sizeof (patterns1); i++) patterns1[i] = (uint8_t)i; for (i = 0; i < sizeof (patterns2); i++) patterns2[i] = (uint8_t)(i ^ 0xAA); /* Normal main() thread activity, it does continues memory copy operations using 2 DMA streams at the lowest priority.*/ while (TRUE) { VirtualTimer vt; /* Starts a VT working as watchdog to catch a malfunction in the DMA driver.*/ chSysLock(); chVTSetI(&vt, MS2ST(10), tmo, NULL); chSysUnlock(); /* Copy pattern 1.*/ dmaStartMemCopy(STM32_DMA2_STREAM6, STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE, patterns1, buf1, sizeof (patterns1)); dmaStartMemCopy(STM32_DMA2_STREAM7, STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE, patterns1, buf2, sizeof (patterns1)); dmaWaitCompletion(STM32_DMA2_STREAM6); dmaWaitCompletion(STM32_DMA2_STREAM7); if (memcmp(patterns1, buf1, sizeof (patterns1))) chSysHalt(); if (memcmp(patterns1, buf2, sizeof (patterns1))) chSysHalt(); /* Copy pattern 2.*/ dmaStartMemCopy(STM32_DMA2_STREAM6, STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE, patterns2, buf1, sizeof (patterns2)); dmaStartMemCopy(STM32_DMA2_STREAM7, STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE, patterns2, buf2, sizeof (patterns2)); dmaWaitCompletion(STM32_DMA2_STREAM6); dmaWaitCompletion(STM32_DMA2_STREAM7); if (memcmp(patterns2, buf1, sizeof (patterns2))) chSysHalt(); if (memcmp(patterns2, buf2, sizeof (patterns2))) chSysHalt(); /* Stops the watchdog.*/ chSysLock(); if (chVTIsArmedI(&vt)) chVTResetI(&vt); chSysUnlock(); chThdSleepMilliseconds(2); } return 0; }
/* * Application entry point. */ int main(void) { static const evhandler_t evhndl[] = { InsertHandler, RemoveHandler, ShellHandler }; event_listener_t el0, el1, el2; /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. * - lwIP subsystem initialization using the default configuration. */ halInit(); chSysInit(); lwipInit(NULL); /* * Initializes a serial-over-USB CDC driver. */ sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg); /* * Activates the USB driver and then the USB bus pull-up on D+. * Note, a delay is inserted in order to not have to disconnect the cable * after a reset. */ usbDisconnectBus(serusbcfg.usbp); chThdSleepMilliseconds(1500); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); /* * Shell manager initialization. */ shellInit(); /* * Activates the serial driver 6 and SDC driver 1 using default * configuration. */ sdStart(&SD6, NULL); sdcStart(&SDCD1, NULL); /* * Activates the card insertion monitor. */ tmr_init(&SDCD1); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Creates the HTTP thread (it changes priority internally). */ chThdCreateStatic(wa_http_server, sizeof(wa_http_server), NORMALPRIO + 1, http_server, NULL); /* * Normal main() thread activity, handling SD card events and shell * start/exit. */ chEvtRegister(&inserted_event, &el0, 0); chEvtRegister(&removed_event, &el1, 1); chEvtRegister(&shell_terminated, &el2, 2); while (true) { if (!shelltp && (SDU1.config->usbp->state == USB_ACTIVE)) { shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO + 1, shellThread, (void *)&shell_cfg1); } chEvtDispatch(evhndl, chEvtWaitOneTimeout(ALL_EVENTS, MS2ST(500))); } }
static void control_thread(void* arg) { (void)arg; chRegSetThreadName("motor"); event_listener_t listener; chEvtRegisterMask(&_setpoint_update_event, &listener, ALL_EVENTS); uint64_t timestamp_hnsec = motor_rtctl_timestamp_hnsec(); while (1) { /* * Control loop period adapts to comm period. */ const uint32_t comm_period = motor_rtctl_get_comm_period_hnsec(); unsigned control_period_ms = IDLE_CONTROL_PERIOD_MSEC; if (comm_period > 0) { control_period_ms = comm_period / HNSEC_PER_MSEC; } if (control_period_ms < 1) { control_period_ms = 1; } else if (control_period_ms > IDLE_CONTROL_PERIOD_MSEC) { control_period_ms = IDLE_CONTROL_PERIOD_MSEC; } /* * Thread priority - maximum if the motor is running, normal otherwise */ const tprio_t desired_thread_priority = (comm_period > 0) ? HIGHPRIO : NORMALPRIO; if (desired_thread_priority != chThdGetPriorityX()) { const tprio_t old = chThdSetPriority(desired_thread_priority); printf("Motor: Priority changed: %i --> %i\n", (int)old, (int)desired_thread_priority); } /* * The event must be set only when the mutex is unlocked. * Otherwise this thread will take control, stumble upon the locked mutex, return the control * to the thread that holds the mutex, unlock the mutex, then proceed. */ chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(control_period_ms)); chMtxLock(&_mutex); const uint64_t new_timestamp_hnsec = motor_rtctl_timestamp_hnsec(); const uint32_t dt_hnsec = new_timestamp_hnsec - timestamp_hnsec; const float dt = dt_hnsec / (float)HNSEC_PER_SEC; timestamp_hnsec = new_timestamp_hnsec; assert(dt > 0); update_filters(dt); update_setpoint_ttl(dt_hnsec / HNSEC_PER_MSEC); update_control(comm_period, dt); poll_beep(); chMtxUnlock(&_mutex); watchdogReset(_watchdog_id); } abort(); }