static THD_FUNCTION(ThreadGFXEvent, arg) { (void)arg; chRegSetThreadName("GFXEvent"); GEvent* pe; static RTCDateTime timespec; struct tm timeinfo; struct tm tim; struct tm *canary; static time_t unix_time; while (true) { pe = geventEventWait(&gl, TIME_INFINITE); switch (pe->type) { case GEVENT_GWIN_BUTTON: if (((GEventGWinButton*)pe)->gwin == ghBtnSettings) { createSettingsFrame(); gwinShow(ghFrame1); updateGraph = false; updateSettingTime = true; } else if (((GEventGWinButton*)pe)->gwin == ghBtnSave) { timeinfo.tm_hour = atoi(gwinListGetSelectedText(ghListHour)); timeinfo.tm_min = atoi(gwinListGetSelectedText(ghListMin)); timeinfo.tm_sec = 0; timeinfo.tm_mday = atoi(gwinListGetSelectedText(ghListDay)); timeinfo.tm_mon = gwinListGetSelected(ghListMonth); timeinfo.tm_year = atoi(gwinListGetSelectedText(ghListYear))-1900; unix_time = mktime(&timeinfo); canary = localtime_r(&unix_time, &tim); osalDbgCheck(&tim == canary); rtcConvertStructTmToDateTime(&tim, 0, ×pec); rtcSetTime(RTC_DRIVER, ×pec); } else if (((GEventGWinButton*)pe)->gwin == ghBtnTime) { updateGraph = false; gwinShow(ghListFreq); } break; case GEVENT_GWIN_CHECKBOX: if (((GEventGWinCheckbox*)pe)->gwin == ghCheckBoxHR) { if (((GEventGWinCheckbox*)pe)->isChecked) use12HR = true; else use12HR = false; } break; case GEVENT_GWIN_CLOSE: updateGraph = true; updateSettingTime = false; switch (activeGraph) { case 0: drawGraphLines(ghContainerGraphC); break; case 1: drawGraphLines(ghContainerGraphV); break; case 2: drawGraphLines(ghContainerGraphW); break; case 3: drawGraphLines(ghContainerGraphCV_1); drawGraphLines(ghContainerGraphCV_2); break; default: break; } break; case GEVENT_GWIN_RADIO: activeGraph = atoi(gwinGetText(((GEventGWinRadio *)pe)->gwin)); setActiveGraph(activeGraph); break; case GEVENT_GWIN_LIST: if (((GEventGWinList*)pe)->gwin == ghListFreq) { char str[5]; uint8_t val = atoi(gwinListGetSelectedText(ghListFreq)); GRAPH_Refresh = ((1.0/val)*1000); snprintf(str, 5, "%d%s", val, "Hz"); gwinSetText(ghBtnTime, str, true); chThdSleepMilliseconds(10); gwinHide(ghListFreq); updateGraph = true; switch (activeGraph) { case 0: drawGraphLines(ghContainerGraphC); break; case 1: drawGraphLines(ghContainerGraphV); break; case 2:palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); palClearPad(GPIOA, 5); drawGraphLines(ghContainerGraphW); break; case 3: drawGraphLines(ghContainerGraphCV_1); drawGraphLines(ghContainerGraphCV_2); break; default: break; } } break; default: break; } chThdSleepMilliseconds(500); } }
THD_FUNCTION(scMS5611Thread, arg) { systime_t sleep_until; systime_t interval_st = MS2ST(ms5611_interval_ms); (void)arg; chRegSetThreadName(__func__); SC_LOG_PRINTF("d: ms5611 init\r\n"); // We need to give a bit of time to the chip until we can ask it to shut down // i2c not ready initially? (This is inherited from the LSM9DS0 driver) chThdSleepMilliseconds(20); ms5611_reset(); chThdSleepMilliseconds(100); ms5611_reset(); chThdSleepMilliseconds(100); ms5611_reset(); chThdSleepMilliseconds(100); ms5611_read_prom(); uint8_t ref_crc = ms5611_prom[MS5611_PROM_SIZE - 1] & 0x0F; uint8_t calc_crc = ms5611_calc_crc(); if (ref_crc != calc_crc) { SC_LOG_PRINTF("e: ms5611 crc failure: 0x%x vs 0x%x\r\n", ref_crc, calc_crc); } sleep_until = chVTGetSystemTime() + interval_st; // Loop initiating measurements and waiting for results while (!chThdShouldTerminateX()) { systime_t now = chVTGetSystemTime(); systime_t wait_time = sleep_until - now; uint32_t temp; uint32_t pres; msg_t msg; // Using a semaphore allows to cancel the sleep outside the thread if (chBSemWaitTimeout(&ms5611_wait_sem, wait_time) != MSG_TIMEOUT) { continue; } sleep_until += interval_st; now = chVTGetSystemTime(); temp = ms5611_read(true); if (chThdShouldTerminateX()) { break; } pres = ms5611_read(false); chMtxLock(&data_mtx); ms5611_temp = temp; ms5611_pres = pres; ms5611_time_st = now; chMtxUnlock(&data_mtx); // Notify main app about new measurements being available msg = sc_event_msg_create_type(SC_EVENT_TYPE_MS5611_AVAILABLE); sc_event_msg_post(msg, SC_EVENT_MSG_POST_FROM_NORMAL); } }
static THD_FUNCTION(Thread1, arg) { (void)arg; chRegSetThreadName("blinker"); while (true) { unsigned i; for (i = 0; i < 4; i++) { palClearPad(PORT_D, PD_LED1); chThdSleepMilliseconds(100); palClearPad(PORT_D, PD_LED2); chThdSleepMilliseconds(100); palClearPad(PORT_D, PD_LED3); chThdSleepMilliseconds(100); palClearPad(PORT_D, PD_LED4); chThdSleepMilliseconds(100); palSetPad(PORT_D, PD_LED1); chThdSleepMilliseconds(100); palSetPad(PORT_D, PD_LED2); chThdSleepMilliseconds(100); palSetPad(PORT_D, PD_LED3); chThdSleepMilliseconds(100); palSetPad(PORT_D, PD_LED4); chThdSleepMilliseconds(300); } for (i = 0; i < 4; i++) { palTogglePort(PORT_D, PAL_PORT_BIT(PD_LED1) | PAL_PORT_BIT(PD_LED2) | PAL_PORT_BIT(PD_LED3) | PAL_PORT_BIT(PD_LED4)); chThdSleepMilliseconds(500); palTogglePort(PORT_D, PAL_PORT_BIT(PD_LED1) | PAL_PORT_BIT(PD_LED2) | PAL_PORT_BIT(PD_LED3) | PAL_PORT_BIT(PD_LED4)); chThdSleepMilliseconds(500); } for (i = 0; i < 4; i++) { palTogglePad(PORT_D, PD_LED1); chThdSleepMilliseconds(250); palTogglePad(PORT_D, PD_LED1); palTogglePad(PORT_D, PD_LED2); chThdSleepMilliseconds(250); palTogglePad(PORT_D, PD_LED2); palTogglePad(PORT_D, PD_LED3); chThdSleepMilliseconds(250); palTogglePad(PORT_D, PD_LED3); palTogglePad(PORT_D, PD_LED4); chThdSleepMilliseconds(250); palTogglePad(PORT_D, PD_LED4); } for (i = 0; i < 4; i++) { palClearPort(PORT_D, PAL_PORT_BIT(PD_LED1) | PAL_PORT_BIT(PD_LED3)); palSetPort(PORT_D, PAL_PORT_BIT(PD_LED2) | PAL_PORT_BIT(PD_LED4)); chThdSleepMilliseconds(500); palClearPort(PORT_D, PAL_PORT_BIT(PD_LED2) | PAL_PORT_BIT(PD_LED4)); palSetPort(PORT_D, PAL_PORT_BIT(PD_LED1) | PAL_PORT_BIT(PD_LED3)); chThdSleepMilliseconds(500); } palSetPort(PORT_D, PAL_PORT_BIT(PD_LED1) | PAL_PORT_BIT(PD_LED2) | PAL_PORT_BIT(PD_LED3) | PAL_PORT_BIT(PD_LED4)); } }
/* * Application entry point. */ int main(void) { /* * 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(); /* * Initializes the PWM driver 1 and ICU driver 1. * GPIOD10 is the PWM output. * GPIOA0 is the ICU input. * The two pins have to be externally connected together. */ icuStart(&ICUD1, &icucfg); icuEnable(&ICUD1); /* Sets A0 alternative function.*/ SIU.PCR[0].R = 0b0100010100000100; pwmStart(&PWMD1, &pwmcfg); /* Sets D10 alternative function.*/ SIU.PCR[58].R = 0b0100010100000100; chThdSleepMilliseconds(2000); /* * Starts the PWM channel 0 using 75% duty cycle. */ pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 50% duty cycle. */ pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 25% duty cycle. */ pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500)); chThdSleepMilliseconds(5000); /* * Changes PWM period and the PWM channel 0 to 50% duty cycle. */ pwmChangePeriod(&PWMD1, 25000); pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000)); chThdSleepMilliseconds(5000); /* * Disables channel 0 and stops the drivers. */ pwmDisableChannel(&PWMD1, 0); pwmStop(&PWMD1); icuDisable(&ICUD1); icuStop(&ICUD1); palClearPad(PORT_D, PD_LED3); palClearPad(PORT_D, PD_LED4); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { chThdSleepMilliseconds(500); } return 0; }
/** * @brief Test execution thread function. * * @param[in] p pointer to a @p BaseChannel object for test output * @return A failure boolean value. */ msg_t TestThread(void *p) { int i, j; chp = p; test_println(""); test_println("*** ChibiOS/RT test suite"); test_println("***"); test_print("*** Kernel: "); test_println(CH_KERNEL_VERSION); test_print("*** Compiled: "); test_println(__DATE__ " - " __TIME__); #ifdef CH_COMPILER_NAME test_print("*** Compiler: "); test_println(CH_COMPILER_NAME); #endif test_print("*** Architecture: "); test_println(CH_ARCHITECTURE_NAME); #ifdef CH_CORE_VARIANT_NAME test_print("*** Core Variant: "); test_println(CH_CORE_VARIANT_NAME); #endif #ifdef CH_PORT_INFO test_print("*** Port Info: "); test_println(CH_PORT_INFO); #endif #ifdef PLATFORM_NAME test_print("*** Platform: "); test_println(PLATFORM_NAME); #endif #ifdef BOARD_NAME test_print("*** Test Board: "); test_println(BOARD_NAME); #endif test_println(""); global_fail = FALSE; i = 0; while (patterns[i]) { j = 0; while (patterns[i][j]) { print_line(); test_print("--- Test Case "); test_printn(i + 1); test_print("."); test_printn(j + 1); test_print(" ("); test_print(patterns[i][j]->name); test_println(")"); #if DELAY_BETWEEN_TESTS > 0 chThdSleepMilliseconds(DELAY_BETWEEN_TESTS); #endif execute_test(patterns[i][j]); if (local_fail) { test_print("--- Result: FAILURE (#"); test_printn(failpoint); test_print(" ["); print_tokens(); test_println("])"); } else test_println("--- Result: SUCCESS"); j++; } i++; } print_line(); test_println(""); test_print("Final result: "); if (global_fail) test_println("FAILURE"); else test_println("SUCCESS"); return (msg_t)global_fail; }
/* * Application entry point. */ int main(void) { thread_t *shelltp1 = NULL; thread_t *shelltp2 = NULL; /* * 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(); /* * Initializes a serial-over-USB CDC driver. */ sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg1); sduObjectInit(&SDU2); sduStart(&SDU2, &serusbcfg2); /* * 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(serusbcfg1.usbp); chThdSleepMilliseconds(1500); usbStart(serusbcfg1.usbp, &usbcfg); usbConnectBus(serusbcfg1.usbp); /* * Shell manager initialization. */ shellInit(); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state. */ while (true) { if (!shelltp1 && (SDU1.config->usbp->state == USB_ACTIVE)) shelltp1 = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminatedX(shelltp1)) { chThdRelease(shelltp1); /* Recovers memory of the previous shell. */ shelltp1 = NULL; /* Triggers spawning of a new shell. */ } if (!shelltp2 && (SDU2.config->usbp->state == USB_ACTIVE)) shelltp2 = shellCreate(&shell_cfg2, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminatedX(shelltp2)) { chThdRelease(shelltp2); /* Recovers memory of the previous shell. */ shelltp2 = NULL; /* Triggers spawning of a new shell. */ } chThdSleepMilliseconds(1000); } }
/* * 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. */ halInit(); chSysInit(); /* * 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 card insertion monitor. */ tmr_init(&SDCD1); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, 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))); } }
/* * Application entry point. */ int main(void) { unsigned i; gptcnt_t interval, threshold, worst; /* * 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(); /* * Prepares the Serial driver 1 and GPT drivers 2 and 3. */ sdStart(&SD1, NULL); /* Default is 38400-8-N-1.*/ palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7)); palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7)); gptStart(&GPTD2, &gpt2cfg); gptStart(&GPTD3, &gpt3cfg); /* * Initializes the mailboxes and creates the worker threads. */ for (i = 0; i < NUM_THREADS; i++) { chMBInit(&mb[i], b[i], MAILBOX_SIZE); chThdCreateStatic(waWorkerThread[i], sizeof waWorkerThread[i], NORMALPRIO - 20, WorkerThread, (void *)i); } /* * Test procedure. */ println(""); println("*** ChibiOS/RT IRQ-STORM long duration test"); println("***"); print("*** Kernel: "); println(CH_KERNEL_VERSION); print("*** Compiled: "); println(__DATE__ " - " __TIME__); #ifdef CH_COMPILER_NAME print("*** Compiler: "); println(CH_COMPILER_NAME); #endif print("*** Architecture: "); println(CH_ARCHITECTURE_NAME); #ifdef CH_CORE_VARIANT_NAME print("*** Core Variant: "); println(CH_CORE_VARIANT_NAME); #endif #ifdef CH_PORT_INFO print("*** Port Info: "); println(CH_PORT_INFO); #endif #ifdef PLATFORM_NAME print("*** Platform: "); println(PLATFORM_NAME); #endif #ifdef BOARD_NAME print("*** Test Board: "); println(BOARD_NAME); #endif println("***"); print("*** System Clock: "); printn(STM32_SYSCLK); println(""); print("*** Iterations: "); printn(ITERATIONS); println(""); print("*** Randomize: "); printn(RANDOMIZE); println(""); print("*** Threads: "); printn(NUM_THREADS); println(""); print("*** Mailbox size: "); printn(MAILBOX_SIZE); println(""); println(""); worst = 0; for (i = 1; i <= ITERATIONS; i++){ print("Iteration "); printn(i); println(""); saturated = FALSE; threshold = 0; for (interval = 2000; interval >= 20; interval -= interval / 10) { gptStartContinuous(&GPTD2, interval - 1); /* Slightly out of phase.*/ gptStartContinuous(&GPTD3, interval + 1); /* Slightly out of phase.*/ chThdSleepMilliseconds(1000); gptStopTimer(&GPTD2); gptStopTimer(&GPTD3); if (!saturated) print("."); else { print("#"); if (threshold == 0) threshold = interval; } } /* Gives the worker threads a chance to empty the mailboxes before next cycle.*/ chThdSleepMilliseconds(20); println(""); print("Saturated at "); printn(threshold); println(" uS"); println(""); if (threshold > worst) worst = threshold; } gptStopTimer(&GPTD2); gptStopTimer(&GPTD3); print("Worst case at "); printn(worst); println(" uS"); println(""); println("Test Complete"); /* * Normal main() thread activity, nothing in this test. */ while (TRUE) { chThdSleepMilliseconds(5000); } return 0; }
/* * Application entry point. */ int main(void) { Thread *shelltp = NULL; /* * 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(); /* * Shell manager initialization. */ shellInit(); /* * 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(1000); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); /* * Activates the serial driver 2 using the driver default configuration. * PA2(TX) and PA3(RX) are routed to USART2. */ sdStart(&SD2, NULL); palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7)); palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7)); /* * Initializes the SPI driver 1 in order to access the MEMS. The signals * are already initialized in the board file. */ spiStart(&SPID1, &spi1cfg); /* * Initializes the SPI driver 2. The SPI2 signals are routed as follow: * PB12 - NSS. * PB13 - SCK. * PB14 - MISO. * PB15 - MOSI. */ spiStart(&SPID2, &spi2cfg); palSetPad(GPIOB, 12); palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); /* NSS. */ palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* SCK. */ palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5)); /* MISO. */ palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* MOSI. */ /* * Initializes the PWM driver 4, routes the TIM4 outputs to the board LEDs. */ pwmStart(&PWMD4, &pwmcfg); palSetPadMode(GPIOD, GPIOD_LED4, PAL_MODE_ALTERNATE(2)); /* Green. */ palSetPadMode(GPIOD, GPIOD_LED3, PAL_MODE_ALTERNATE(2)); /* Orange. */ palSetPadMode(GPIOD, GPIOD_LED5, PAL_MODE_ALTERNATE(2)); /* Red. */ palSetPadMode(GPIOD, GPIOD_LED6, PAL_MODE_ALTERNATE(2)); /* Blue. */ /* * Creates the example thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 10, Thread1, NULL); /* * Normal main() thread activity, in this demo it just performs * a shell respawn upon its termination. */ while (TRUE) { if (!shelltp) { if (SDU1.config->usbp->state == USB_ACTIVE) { /* Spawns a new shell.*/ shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); } } else { /* If the previous shell exited.*/ if (chThdTerminated(shelltp)) { /* Recovers memory of the previous shell.*/ chThdRelease(shelltp); shelltp = NULL; } } chThdSleepMilliseconds(500); } }
static msg_t thdUsbStorage(void *arg) { (void) arg; // unused chRegSetThreadName("UsbStorage:polling"); uint antiBounce=5; EventListener connected; // Should use EXTI interrupt instead of active polling, // but in the chibios_opencm3 implementation, since EXTI is // used via libopencm3, ISR are routed on pprz/opencm3 and cannot // be used concurrently by chibios api // Should be fixed when using chibios-rt branch while (!chThdShouldTerminate() && antiBounce) { const bool_t usbConnected = palReadPad (GPIOA, GPIOA_OTG_FS_VBUS); if (usbConnected) antiBounce--; else antiBounce=5; chThdSleepMilliseconds(20); } isRunning = true; chRegSetThreadName("UsbStorage:connected"); /* Stop the logs*/ chibios_logFinish (true); /* connect sdcard sdc interface sdio */ if (sdioConnect () == false) chThdExit (RDY_TIMEOUT); /* initialize the USB mass storage driver */ msdInit(&UMSD1); /* start the USB mass storage service */ msdStart(&UMSD1, &msdConfig); /* start the USB driver */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); usbStart(&USBD1, &usbConfig); usbConnectBus(&USBD1); /* wait for a real usb storage connexion before shutting down autopilot */ chEvtRegisterMask(&UMSD1.evt_connected, &connected, EVENT_MASK(1)); chEvtWaitOne(EVENT_MASK(1)); /* stop autopilot */ if (pprzThdPtr != NULL) { chThdTerminate (pprzThdPtr); chThdWait (pprzThdPtr); pprzThdPtr = NULL; } /* wait until usb-storage is unmount and usb cable is unplugged*/ while (!chThdShouldTerminate() && palReadPad (GPIOA, GPIOA_OTG_FS_VBUS)) { chThdSleepMilliseconds(10); } /* then close open descriptors and reboot autopilot */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(500); msdStop(&UMSD1); sdioDisconnect (); MCU_RESTART(); return RDY_OK; }
static void sleepMilliseconds(uint32_t ms) { chThdSleepMilliseconds(ms); }
THD_FUNCTION(Thread1, arg) { (void)arg; /* * Activate the serial driver 0 using the driver default configuration. */ sdStart(&SD0, NULL); while (chnGetTimeout(&SD0, TIME_INFINITE)) { chnWrite(&SD0, (const uint8_t *)start_msg, strlen(start_msg)); chThdSleepMilliseconds(2000); /* Test 1 - use DMA engine to execute a word-wise memory-to-memory copy. */ chnWrite(&SD0, (const uint8_t *)test_1_msg, strlen(test_1_msg)); strcpy(instring, "After DMA test \r\n"); strcpy(outstring, "Before DMA test \r\n"); if (strcmp("Before DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } request = &test_1_req; chSysLock(); dmaRequestS(request, TIME_INFINITE); chSysUnlock(); if (strcmp("After DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } /* Test 2 - use DMA engine to execute a byte-wise memory-to-memory copy. */ chnWrite(&SD0, (const uint8_t *)test_2_msg, strlen(test_2_msg)); strcpy(instring, "After DMA test \r\n"); strcpy(outstring, "Before DMA test \r\n"); if (strcmp("Before DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } request = &test_2_req; chSysLock(); dmaRequestS(request, TIME_INFINITE); chSysUnlock(); if (strcmp("After DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } /* Test 3 - use DMA engine to execute a word-wise memory-to-memory set. */ chnWrite(&SD0, (const uint8_t *)test_3_msg, strlen(test_3_msg)); strcpy(instring, "After DMA test \r\n"); strcpy(outstring, "Before DMA test \r\n"); if (strcmp("Before DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } request = &test_3_req; chSysLock(); dmaRequestS(request, TIME_INFINITE); chSysUnlock(); if (strcmp("AAAAAAAAAAAAAAAA\r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } /* Test 4 - use DMA engine to execute a word-wise memory-to-memory copy, * then call a callback. */ chnWrite(&SD0, (const uint8_t *)test_4_msg, strlen(test_4_msg)); strcpy(instring, "After DMA test \r\n"); strcpy(outstring, "Before DMA test \r\n"); cb_arg = 1; if (strcmp("Before DMA test \r\n", outstring) || (cb_arg != 1)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } request = &test_4_req; chSysLock(); dmaRequestS(request, TIME_INFINITE); chSysUnlock(); if (strcmp("After DMA test \r\n", outstring) || cb_arg) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } /* Test 5 - use exclusive DMA channel 0 to execute a word-wise * memory-to-memory copy. */ chnWrite(&SD0, (const uint8_t *)test_5_msg, strlen(test_5_msg)); strcpy(instring, "After DMA test \r\n"); strcpy(outstring, "Before DMA test \r\n"); if (strcmp("Before DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } request = &test_5_req; chSysLock(); dmaAcquireI(&ch, 0); chSysUnlock(); dmaTransfer(&ch, request); if (strcmp("After DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } /* Test 6 - Attempt to claim DMA channel 0, fail, release it, attempt to * claim it again */ chnWrite(&SD0, (const uint8_t *)test_6_msg, strlen(test_6_msg)); chSysLock(); result = dmaAcquireI(&ch, 0); chSysUnlock(); if (!result) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } dmaRelease(&ch); chSysLock(); result = dmaAcquireI(&ch, 0); chSysUnlock(); if (result) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } dmaRelease(&ch); /* Test 7 - use exclusive DMA channel 1 to execute a word-wise * memory-to-memory copy. */ chnWrite(&SD0, (const uint8_t *)test_7_msg, strlen(test_7_msg)); strcpy(instring, "After DMA test \r\n"); strcpy(outstring, "Before DMA test \r\n"); if (strcmp("Before DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } request = &test_5_req; chSysLock(); dmaAcquireI(&ch, 1); chSysUnlock(); dmaTransfer(&ch, request); if (strcmp("After DMA test \r\n", outstring)) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } dmaRelease(&ch); /* Test 8 - Claim all 3 DMA channels, attempt dmaRequest, fail */ chnWrite(&SD0, (const uint8_t *)test_8_msg, strlen(test_8_msg)); chSysLock(); result = dmaAcquireI(&ch, 0); chSysUnlock(); if (result) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } chSysLock(); result = dmaAcquireI(&ch1, 1); chSysUnlock(); if (result) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } chSysLock(); result = dmaAcquireI(&ch2, 2); chSysUnlock(); if (result) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } chSysLock(); result_i = dmaRequestS(request, TIME_IMMEDIATE); chSysUnlock(); if (result_i > 0) { chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string)); } else { chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string)); } dmaRelease(&ch); dmaRelease(&ch1); dmaRelease(&ch2); } }
/* * Application entry point. */ int main(void) { /* * 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(); /* * Activates the serial driver 2 and SDC driver 1 using default * configuration. */ sdStart(&SD6, NULL); BaseSequentialStream *chp = (BaseSequentialStream *)&SD6; chprintf(chp, "running main()\r\n"); chThdSleepMilliseconds(50); #if STM32_USB_USE_OTG2 USBDriver *usb_driver = &USBD2; #else USBDriver *usb_driver = &USBD1; #endif /* * Activates the card insertion monitor. */ init_sd(); chprintf(chp, "done starting SDC\r\n"); const bool_t sdcConnectStatus = sdcConnect(&SDCD1); if( sdcConnectStatus != CH_SUCCESS ) { chprintf(chp, "failed to connect to SD Card, sdcConnectStatus = %u\r\n", sdcConnectStatus); for(;;) { chThdSleepMilliseconds(3000); } } chprintf(chp, "setting up MSD\r\n"); const usb_msd_driver_state_t msd_driver_state = msdInit(usb_driver, (BaseBlockDevice*)&SDCD1, &UMSD1, USB_MS_DATA_EP, USB_MSD_INTERFACE_NUMBER); if( msd_driver_state != USB_MSD_DRIVER_OK ) { chprintf(chp, "Error initing USB MSD, %d %s\r\n", msd_driver_state, usb_msd_driver_state_t_to_str(msd_driver_state)); } UMSD1.chp = chp; chprintf(chp, "Initializing SDU1...\r\n"); serusbcfg.usbp = usb_driver; sduObjectInit(&SDU1); /*Disconnect the USB Bus*/ usbDisconnectBus(usb_driver); chThdSleepMilliseconds(200); /*Start the useful functions*/ sduStart(&SDU1, &serusbcfg); msdStart(&UMSD1); usbStart(usb_driver, &msd_usb_config); /*Connect the USB Bus*/ usbConnectBus(usb_driver); /* * Creates the blinker thread. */ chprintf(chp, "starting blinker thread\r\n"); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); while (TRUE) { palTogglePad(GPIOC, GPIOC_LED); chThdSleepMilliseconds(500); } }
int main(void) { Thread * pub_tp = NULL; Thread * sub_tp = NULL; halInit(); chSysInit(); /* * Initializes a serial-over-USB CDC driver. */ #if USE_USB_SERIAL sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg); #endif /* * 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. */ #if USE_USB_SERIAL usbDisconnectBus(serusbcfg.usbp); chThdSleepMilliseconds(500); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); #endif /* Start the serial driver. */ #if !USE_USB_SERIAL sdStart(&SD3, NULL); #endif for (;;) { #if USE_USB_SERIAL if (!pub_tp && (SDU1.config->usbp->state == USB_ACTIVE)) { #else if (!pub_tp) { #endif pub_tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(2048), NORMALPRIO, rosserial_pub_thread, NULL); } else if (chThdTerminated(pub_tp)) { chThdRelease(pub_tp); pub_tp = NULL; } chThdSleepMilliseconds(123); #if USE_USB_SERIAL if (!sub_tp && (SDU1.config->usbp->state == USB_ACTIVE)) { #else if (!sub_tp) { #endif sub_tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(2048), NORMALPRIO, rosserial_sub_thread, NULL); } else if (chThdTerminated(sub_tp)) { chThdRelease(sub_tp); sub_tp = NULL; } chThdSleepMilliseconds(500); } return CH_SUCCESS; } }
/* * Application entry point. */ int main(void) { /* Initialization of all the imported components in the order specified in the application wizard. The function is generated automatically.*/ componentsInit(); palClearPad(PORT11, P11_LED4); /* * Initializes the PWM driver 8 and ICU driver 1. * PIN80 is the PWM output. * PIN63 is the ICU input. * The two pins have to be externally connected together. */ /* Sets PIN63 alternative function.*/ SIU.PCR[179].R = 0b0000010100001100; /* Sets PIN80 alternative function.*/ SIU.PCR[202].R = 0b0000011000001100; icuStart(&ICUD1, &icucfg); icuEnable(&ICUD1); pwmStart(&PWMD8, &pwmcfg); chThdSleepMilliseconds(2000); /* * Starts the PWM channel 0 using 75% duty cycle. */ pwmEnableChannel(&PWMD8, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD8, 7500)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 50% duty cycle. */ pwmEnableChannel(&PWMD8, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD8, 5000)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 25% duty cycle. */ pwmEnableChannel(&PWMD8, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD8, 2500)); chThdSleepMilliseconds(5000); /* * Changes PWM period and the PWM channel 0 to 50% duty cycle. */ pwmChangePeriod(&PWMD8, 25000); pwmEnableChannel(&PWMD8, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD8, 5000)); chThdSleepMilliseconds(5000); /* * Disables PWM channel 0 and stops the drivers. */ pwmDisableChannel(&PWMD8, 0); pwmStop(&PWMD8); /* * Disables and stops the ICU drivers. */ icuDisable(&ICUD1); icuStop(&ICUD1); palClearPad(PORT11, P11_LED3); palClearPad(PORT11, P11_LED4); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { chThdSleepMilliseconds(500); } return 0; }
static THD_FUNCTION(Thread1, arg) { (void)arg; /* * Activates the serial driver 1 using the driver default configuration. */ sdStart(&SD1, NULL); while (true) { unsigned i; for (i = 0; i < 4; i++) { palClearPad(PORT_E, PE_LED1); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED2); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED3); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED4); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED1); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED2); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED3); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED4); chThdSleepMilliseconds(300); } for (i = 0; i < 4; i++) { palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); } for (i = 0; i < 4; i++) { palTogglePad(PORT_E, PE_LED1); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED1); palTogglePad(PORT_E, PE_LED2); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED2); palTogglePad(PORT_E, PE_LED3); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED3); palTogglePad(PORT_E, PE_LED4); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED4); } for (i = 0; i < 4; i++) { palClearPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3)); palSetPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); palClearPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4)); palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3)); chThdSleepMilliseconds(500); } palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); } }
int main(void) { static Thread *shelltp = NULL; static const evhandler_t evhndl_main[] = { extdetail_WKUP_button_handler }; struct EventListener el0; /* * 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(); extdetail_init(); palSetPad(GPIOC, GPIOC_LED); palSetPad(GPIOA, GPIOA_SPI1_SCK); palSetPad(GPIOA, GPIOA_SPI1_NSS); /* * SPI1 I/O pins setup. */ palSetPadMode(adis_connections.spi_sck_port, adis_connections.spi_sck_pad, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(adis_connections.spi_miso_port, adis_connections.spi_miso_pad, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST| PAL_STM32_PUDR_FLOATING); palSetPadMode(adis_connections.spi_mosi_port, adis_connections.spi_mosi_pad, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST ); palSetPadMode(adis_connections.spi_cs_port, adis_connections.spi_cs_pad, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); palSetPad(GPIOA, GPIOA_SPI1_SCK); palSetPad(GPIOA, GPIOA_SPI1_NSS); /*! * Initializes a serial-over-USB CDC driver. */ sduObjectInit(&SDU_PSAS); sduStart(&SDU_PSAS, &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(1000); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); shellInit(); iwdg_begin(); /*! * Activates the serial driver 6 and SDC driver 1 using default * configuration. */ sdStart(&SD6, NULL); spiStart(&SPID1, &adis_spicfg); /* Set transfer parameters. */ chThdSleepMilliseconds(300); adis_init(); adis_reset(); /*! Activates the EXT driver 1. */ extStart(&EXTD1, &extcfg); chThdCreateStatic(waThread_blinker, sizeof(waThread_blinker), NORMALPRIO, Thread_blinker, NULL); chThdCreateStatic(waThread_adis_dio1, sizeof(waThread_adis_dio1), NORMALPRIO, Thread_adis_dio1, NULL); chThdCreateStatic(waThread_adis_newdata, sizeof(waThread_adis_newdata), NORMALPRIO, Thread_adis_newdata, NULL); chThdCreateStatic(waThread_indwatchdog, sizeof(waThread_indwatchdog), NORMALPRIO, Thread_indwatchdog, NULL); chEvtRegister(&extdetail_wkup_event, &el0, 0); while (TRUE) { if (!shelltp && (SDU_PSAS.config->usbp->state == USB_ACTIVE)) shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminated(shelltp)) { chThdRelease(shelltp); /* Recovers memory of the previous shell. */ shelltp = NULL; /* Triggers spawning of a new shell. */ } chEvtDispatch(evhndl_main, chEvtWaitOneTimeout((eventmask_t)1, MS2ST(500))); } }
void Hip9011Hardware::sendSyncCommand(unsigned char command) { SPI_SYNCHRONOUS(command); chThdSleepMilliseconds(10); }
/* * Application entry point. */ int main(void) { size_t start = 1100; size_t end = 1150; volatile int32_t adc_its = 0; volatile int32_t spi_its = 0; volatile int32_t uart_its = 0; volatile int32_t adc_its_idle = 0; volatile int32_t spi_its_idle = 0; volatile int32_t uart_its_idle = 0; volatile uint32_t idle_thread_cnt = 0; #if EMCNAND_USE_KILL_TEST size_t kill = 8000; #endif /* * 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(); emcStart(&EMCD1, &emccfg); #if !STM32_EMC_EMCNAND_USE_FSMC_INT extStart(&EXTD1, &extcfg); #endif emcnandStart(&EMCNANDD1, &nandcfg); chThdSleepMilliseconds(4000); chThdCreateStatic(fsmcIdleThreadWA, sizeof(fsmcIdleThreadWA), NORMALPRIO - 20, fsmcIdleThread, NULL); nand_wp_release(); dma_storm_adc_start(); dma_storm_uart_start(); dma_storm_spi_start(); T = chVTGetSystemTimeX(); general_test(&EMCNANDD1, start, end, 1); T = chVTGetSystemTimeX() - T; adc_its = dma_storm_adc_stop(); uart_its = dma_storm_uart_stop(); spi_its = dma_storm_spi_stop(); chSysLock(); idle_thread_cnt = IdleCnt; IdleCnt = 0; chSysUnlock(); dma_storm_adc_start(); dma_storm_uart_start(); dma_storm_spi_start(); chThdSleep(T); adc_its_idle = dma_storm_adc_stop(); uart_its_idle = dma_storm_uart_stop(); spi_its_idle = dma_storm_spi_stop(); osalDbgCheck(idle_thread_cnt > (IdleCnt / 4)); osalDbgCheck(abs(adc_its - adc_its_idle) < (adc_its_idle / 20)); osalDbgCheck(abs(uart_its - uart_its_idle) < (uart_its_idle / 20)); osalDbgCheck(abs(spi_its - spi_its_idle) < (spi_its_idle / 10)); ecc_test(&EMCNANDD1, end); #if EMCNAND_USE_KILL_TEST kill_block(&EMCNANDD1, kill); #endif nand_wp_assert(); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { chThdSleepMilliseconds(500); } }
/*! \brief Reset the ADIS */ void adis_reset() { palClearPad(adis_connections.reset_port, adis_connections.reset_pad); chThdSleepMilliseconds(ADIS_RESET_MSECS); palSetPad(adis_connections.reset_port, adis_connections.reset_pad); chThdSleepMilliseconds(ADIS_RESET_MSECS); }
/** * @brief IRQ storm execution. * * @param[in] cfg pointer to the test configuration structure * * @api */ void irq_storm_execute(const irq_storm_config_t *cfg) { unsigned i; gptcnt_t interval, threshold, worst; /* Global configuration pointer.*/ config = cfg; /* Starting timers using the stored configurations.*/ gptStart(cfg->gpt1p, cfg->gptcfg1p); gptStart(cfg->gpt2p, cfg->gptcfg2p); /* * Initializes the mailboxes and creates the worker threads. */ for (i = 0; i < IRQ_STORM_CFG_NUM_THREADS; i++) { chMBObjectInit(&mb[i], b[i], IRQ_STORM_CFG_MAILBOX_SIZE); threads[i] = chThdCreateStatic(irq_storm_thread_wa[i], sizeof irq_storm_thread_wa[i], IRQ_STORM_CFG_THREADS_PRIORITY, irq_storm_thread, (void *)i); } /* Printing environment information.*/ chprintf(cfg->out, ""); chprintf(cfg->out, "\r\n*** ChibiOS/RT IRQ-STORM long duration test\r\n***\r\n"); chprintf(cfg->out, "*** Kernel: %s\r\n", CH_KERNEL_VERSION); chprintf(cfg->out, "*** Compiled: %s\r\n", __DATE__ " - " __TIME__); #ifdef PORT_COMPILER_NAME chprintf(cfg->out, "*** Compiler: %s\r\n", PORT_COMPILER_NAME); #endif chprintf(cfg->out, "*** Architecture: %s\r\n", PORT_ARCHITECTURE_NAME); #ifdef PORT_CORE_VARIANT_NAME chprintf(cfg->out, "*** Core Variant: %s\r\n", PORT_CORE_VARIANT_NAME); #endif chprintf(cfg->out, "*** System Clock: %d\r\n", cfg->sysclk); #ifdef PORT_INFO chprintf(cfg->out, "*** Port Info: %s\r\n", PORT_INFO); #endif #ifdef PLATFORM_NAME chprintf(cfg->out, "*** Platform: %s\r\n", PLATFORM_NAME); #endif #ifdef BOARD_NAME chprintf(cfg->out, "*** Test Board: %s\r\n", BOARD_NAME); #endif chprintf(cfg->out, "***\r\n"); chprintf(cfg->out, "*** Iterations: %d\r\n", IRQ_STORM_CFG_ITERATIONS); chprintf(cfg->out, "*** Randomize: %d\r\n", IRQ_STORM_CFG_RANDOMIZE); chprintf(cfg->out, "*** Threads: %d\r\n", IRQ_STORM_CFG_NUM_THREADS); chprintf(cfg->out, "*** Mailbox size: %d\r\n\r\n", IRQ_STORM_CFG_MAILBOX_SIZE); /* Test loop.*/ worst = 0; for (i = 1; i <= IRQ_STORM_CFG_ITERATIONS; i++){ chprintf(cfg->out, "Iteration %d\r\n", i); saturated = false; threshold = 0; /* Timer intervals starting at 2mS then decreased by 10% after each cycle.*/ for (interval = 2000; interval >= 2; interval -= (interval + 9) / 10) { /* Timers programmed slightly out of phase each other.*/ gptStartContinuous(cfg->gpt1p, interval - 1); /* Slightly out of phase.*/ gptStartContinuous(cfg->gpt2p, interval + 1); /* Slightly out of phase.*/ /* Storming for one second.*/ chThdSleepMilliseconds(1000); /* Timers stopped.*/ gptStopTimer(cfg->gpt1p); gptStopTimer(cfg->gpt2p); /* Did the storm saturate the threads chain?*/ if (!saturated) chprintf(cfg->out, "."); else { chprintf(cfg->out, "#"); if (threshold == 0) threshold = interval; break; } } /* Gives threads a chance to empty the mailboxes before next cycle.*/ chThdSleepMilliseconds(20); chprintf(cfg->out, "\r\nSaturated at %d uS\r\n\r\n", threshold); if (threshold > worst) worst = threshold; } gptStopTimer(cfg->gpt1p); gptStopTimer(cfg->gpt2p); chprintf(cfg->out, "Worst case at %d uS\r\n", worst); chprintf(cfg->out, "\r\nTest Complete\r\n"); /* Terminating threads and cleaning up.*/ for (i = 0; i < IRQ_STORM_CFG_NUM_THREADS; i++) { chThdTerminate(threads[i]); chThdWait(threads[i]); threads[i] = NULL; } }
/* * Application entry point. */ int main(void) { /* * 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(); /* * Initializes the PWM driver 2 and ICU driver 3. * GPIOA15 is the PWM output. * GPIOC6 is the ICU input. * The two pins have to be externally connected together. */ pwmStart(&PWMD2, &pwmcfg); palSetPadMode(GPIOA, 15, PAL_MODE_ALTERNATE(1)); icuStart(&ICUD3, &icucfg); palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(2)); icuEnable(&ICUD3); chThdSleepMilliseconds(2000); /* * Starts the PWM channel 0 using 75% duty cycle. */ pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 7500)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 50% duty cycle. */ pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 25% duty cycle. */ pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 2500)); chThdSleepMilliseconds(5000); /* * Changes PWM period to half second the duty cycle becomes 50% * implicitly. */ pwmChangePeriod(&PWMD2, 5000); chThdSleepMilliseconds(5000); /* * Disables channel 0 and stops the drivers. */ pwmDisableChannel(&PWMD2, 0); pwmStop(&PWMD2); icuDisable(&ICUD3); icuStop(&ICUD3); palClearPad(GPIOD, GPIOD_LED4); palClearPad(GPIOD, GPIOD_LED5); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { chThdSleepMilliseconds(500); } return 0; }
static THD_FUNCTION(Thread1, arg) { (void)arg; /* * Activates the serial driver 1 using the driver default configuration. */ sdStart(&SD1, NULL); while (TRUE) { unsigned i; chnWriteTimeout(&SD1, (uint8_t *)"Hello World!\r\n", 14, TIME_INFINITE); for (i = 0; i < 4; i++) { palClearPad(PORT_E, PE_LED1); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED2); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED3); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED4); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED1); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED2); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED3); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED4); chThdSleepMilliseconds(300); } for (i = 0; i < 4; i++) { palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); } for (i = 0; i < 4; i++) { palTogglePad(PORT_E, PE_LED1); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED1); palTogglePad(PORT_E, PE_LED2); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED2); palTogglePad(PORT_E, PE_LED3); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED3); palTogglePad(PORT_E, PE_LED4); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED4); } for (i = 0; i < 4; i++) { palClearPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3)); palSetPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); palClearPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4)); palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3)); chThdSleepMilliseconds(500); } palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); } }
/* * Application entry point. */ int main(void) { /* * 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(); /* * 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); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL); /* * L3GD20 Object Initialization */ l3gd20ObjectInit(&L3GD20D1); /* * Activates the L3GD20 driver. */ l3gd20Start(&L3GD20D1, &l3gd20cfg); while(!palReadLine(LINE_BUTTON)){ chprintf(chp, "Press BTN to calibrate gyroscope...\r\n"); chThdSleepMilliseconds(150); #if CHPRINTF_USE_ANSI_CODE chprintf(chp, "\033[2J\033[1;1H"); #endif } chprintf(chp, "Calibrating Gyroscope sampling bias...\r\n"); chprintf(chp, "Keep it in the rest position while red LED is on\r\n"); chThdSleepMilliseconds(3000); palSetLine(LINE_LED10_RED); chThdSleepMilliseconds(1000); gyroscopeSampleBias(&L3GD20D1); palClearLine(LINE_LED10_RED); #if CHPRINTF_USE_ANSI_CODE chprintf(chp, "\033[2J\033[1;1H"); #endif while (TRUE) { palToggleLine(LINE_LED10_RED); gyroscopeReadRaw(&L3GD20D1, rawdata); for(i = 0; i < L3GD20_NUMBER_OF_AXES; i++) chprintf(chp, "RAW-%c:%d\r\n", axesID[i], rawdata[i]); gyroscopeReadCooked(&L3GD20D1, cookeddata); for(i = 0; i < L3GD20_NUMBER_OF_AXES; i++) chprintf(chp, "COOKED-%c:%.3f\r\n", axesID[i], cookeddata[i]); gyroscopeGetTemp(&L3GD20D1, &temperature); chprintf(chp, "TEMP:%.1f C°\r\n", temperature); chThdSleepMilliseconds(150); #if CHPRINTF_USE_ANSI_CODE chprintf(chp, "\033[2J\033[1;1H"); #endif } l3gd20Stop(&L3GD20D1); }
static msg_t stThread(StepperMotor *motor) { chRegSetThreadName("stepper"); // try to get saved stepper position (-1 for no data) motor->currentPosition = loadStepperPos(); // first wait until at least 1 slowADC sampling is complete waitForSlowAdc(); // now check if stepper motor re-initialization is requested - if the throttle pedal is pressed at startup bool forceStepperParking = !engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE) && getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) > STEPPER_PARKING_TPS; if (boardConfiguration->stepperForceParkingEveryRestart) forceStepperParking = true; scheduleMsg(logger, "Stepper: savedStepperPos=%d forceStepperParking=%d (tps=%.2f)", motor->currentPosition, (forceStepperParking ? 1 : 0), getTPS(PASS_ENGINE_PARAMETER_SIGNATURE)); if (motor->currentPosition < 0 || forceStepperParking) { // reset saved value saveStepperPos(-1); /** * let's park the motor in a known position to begin with * * I believe it's safer to retract the valve for parking - at least on a bench I've seen valves * disassembling themselves while pushing too far out. * * Add extra steps to compensate step skipping by some old motors. */ int numParkingSteps = (int)efiRound((1.0f + (float)boardConfiguration->stepperParkingExtraSteps / PERCENT_MULT) * motor->totalSteps, 1.0f); for (int i = 0; i < numParkingSteps; i++) { motor->pulse(); } // set & save zero stepper position after the parking completion motor->currentPosition = 0; saveStepperPos(motor->currentPosition); } else { // The initial target position should correspond to the saved stepper position. // Idle thread starts later and sets a new target position. motor->setTargetPosition(motor->currentPosition); } while (true) { int targetPosition = motor->getTargetPosition(); int currentPosition = motor->currentPosition; if (targetPosition == currentPosition) { chThdSleepMilliseconds(motor->reactionTime); continue; } bool isIncrementing = targetPosition > currentPosition; motor->setDirection(isIncrementing); if (isIncrementing) { motor->currentPosition++; } else { motor->currentPosition--; } motor->pulse(); // save position to backup RTC register saveStepperPos(motor->currentPosition); } // let's part the motor in a known position to begin with // for (int i = 0; i < ST_COUNT / 2; i++) { // motor->pulse(); // } return 0; }
/* * Application entry point. */ int main(void) { /* * 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. */ uint32_t tempR, tempP, tempY; float Roll,Pitch,Yaw; halInit(); chSysInit(); i2c_setup(); palSetPadMode(IOPORT1, 8, PAL_MODE_INPUT); // PA8 - RADIO INPUT palSetPadMode(IOPORT2, 13, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); /* SCK. */ palSetPadMode(IOPORT2, 14, PAL_MODE_STM32_ALTERNATE_PUSHPULL); /* MISO.*/ palSetPadMode(IOPORT2, 15, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); /* MOSI.*/ palSetPadMode(IOPORT2, 12, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); palSetPadMode(IOPORT3, 11, PAL_MODE_OUTPUT_PUSHPULL); chMtxInit(&mtx_imu); chMtxInit(&mutex_motors); palClearPad(IOPORT3,10); //palClearPad(IOPORT3,11); /* * Activates the USB driver and then the USB bus pull-up on D+. */ sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg); usbConnectBus(serusbcfg.usbp); //palClearPad(GPIOC, GPIOC_USB_DISC); icuStart(&ICUD1, &icucfg); icuEnable(&ICUD1); chThdSleepSeconds(1); chThdCreateStatic(waThread1, sizeof(waThread1), HIGHPRIO, Thread1, NULL); //chThdCreateStatic(waMotorsThread, sizeof(waMotorsThread), NORMALPRIO, MotorsThread, NULL); spiAcquireBus(&SPID2); /* Acquire ownership of the bus. */ spiStart(&SPID2, &ls_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID2); KP = 1; KI = 1; KD = 0; while (TRUE) { while(!(SPID2.spi->SR & SPI_SR_RXNE)); spiReceive(&SPID2,24,rxbuf_16bit); // Floating point calculation of Roll/Pitch/Yaw inside the microcontroller. Decomment next 6 lines if you want to implement // the control Law. tempR = (uint32_t)((rxbuf_16bit[0] << 31) | (rxbuf_16bit[1] << 23) | (rxbuf_16bit[2] << 11) | rxbuf_16bit[3]); tempP = (uint32_t)((rxbuf_16bit[4] << 31) | (rxbuf_16bit[5] << 23) | (rxbuf_16bit[6] << 11) | rxbuf_16bit[7]); tempY = (uint32_t)((rxbuf_16bit[8] << 31) | (rxbuf_16bit[9] << 23) | (rxbuf_16bit[10] << 11) | rxbuf_16bit[11]); Roll = (*(float*)&tempR); Pitch = (*(float*)&tempP); Yaw = (*(float*)&tempY); // CONTROL LAW HERE //++ (ROLL,PITCH,YAW ERRORS) -----> [CONTROLLER] -----> (MOTORS INPUT) // PID Control Law roll_error = 0 - Roll; pitch_error = 0 - Pitch; yaw_error = 0 - Yaw; roll_I = roll_I + roll_error*0.01; pitch_I = pitch_I + pitch_error*0.01; roll_D = (roll_error - roll_prev_error)/0.01; pitch_D = (pitch_error - pitch_prev_error)/0.01; roll_controller_output = (int8_t)(KP*roll_error + KI*roll_I + KD*roll_D); pitch_controller_output = (int8_t)(KP*pitch_error + KI*pitch_I + KD*pitch_D); //roll_controller_output = (int8_t)roll_error; //pitch_controller_output = (int8_t)pitch_error; roll_prev_error = roll_error; pitch_prev_error = pitch_error; //------------------------------------------------------------------- blctrl20_set_velocity(); palSetPad(IOPORT3,11); /* SPI FLOATING POINT TEST PACKET (sending 5.6F) rxbuf_16bit[0] = 0; rxbuf_16bit[1] = 129; rxbuf_16bit[2] = 1638; rxbuf_16bit[3] = 819; rxbuf_16bit[4] = 0; rxbuf_16bit[5] = 129; rxbuf_16bit[6] = 1638; rxbuf_16bit[7] = 819; rxbuf_16bit[8] = 1; rxbuf_16bit[9] = 129; rxbuf_16bit[10] = 1638; rxbuf_16bit[11] = 819; */ if(SDU1.config->usbp->state==USB_ACTIVE) { // chprintf((BaseChannel *)&SDU1,"S:%6d:%6d:%6d:%6d:%6d:%6d:%6d:%6d:%6d:%6d:%6d:%6d:E\r\n",rxbuf_16bit[0],rxbuf_16bit[1],rxbuf_16bit[2],rxbuf_16bit[3],rxbuf_16bit[4],rxbuf_16bit[5],rxbuf_16bit[6],rxbuf_16bit[7],rxbuf_16bit[8],rxbuf_16bit[9],rxbuf_16bit[10],rxbuf_16bit[11]); chprintf((BaseChannel *)&SDU1, "S:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:%6D:E\r\n",rxbuf_16bit[0],rxbuf_16bit[1],rxbuf_16bit[2],rxbuf_16bit[3],rxbuf_16bit[4],rxbuf_16bit[5],rxbuf_16bit[6],rxbuf_16bit[7],rxbuf_16bit[8],rxbuf_16bit[9],rxbuf_16bit[10],rxbuf_16bit[11],(int8_t)Roll,(int8_t)Pitch,(int8_t)Yaw,icu_ch[3],icu_ch[4],roll_controller_output,pitch_controller_output,yaw_controller_output); } chThdSleepMilliseconds(10); } spiUnselect(&SPID2); spiReleaseBus(&SPID2); /* Ownership release. */ }
//Wireless static void cmd_nrf(BaseSequentialStream *chp, int argc, char *argv[]) { if(*(argv[0]) == 'c'){ channel = strtol(argv[1], NULL, 0); nrf24l01SetChannel(&nrf24l01, channel); chprintf((BaseSequentialStream*)&SD1, "Channel set to %d.\n\r", channel); } else if(*(argv[0]) == 'a'){ if(strlen(argv[1]) == 5){ int i = 0; uint8_t add[5]; for(i; i<5; i++){ addr[i] = *(argv[1]+i); } serialOutBuf[0] = msg_seq; serialOutBuf[2] = addr[0]; serialOutBuf[3] = addr[1]; serialOutBuf[4] = addr[2]; serialOutBuf[5] = addr[3]; serialOutBuf[6] = addr[4]; nrf24l01SetRXAddress(&nrf24l01, 0, addr); chprintf((BaseSequentialStream*)&SD1, "Personal Address set to %s.\n\r", addr); //addr = add; }else { chprintf((BaseSequentialStream*)&SD1, "Not an appropriate length address."); } } else if(*(argv[0]) == 't'){ int i=0; for(i; i<5; i++){ target_addr[i] = *(argv[1] + i); } chprintf((BaseSequentialStream*)&SD1, "%s.\n\r", target_addr); //chprintf((BaseSequentialStream*)&SD1, "Sending..."); nrf24l01SetTXAddress(&nrf24l01, target_addr); int msg_size = strlen(argv[2]); serialOutBuf[0] = msg_seq; serialOutBuf[7] = ','; serialOutBuf[8] = msg_type; serialOutBuf[9] = ','; serialOutBuf[10] = msg_size; serialOutBuf[11] = ','; for(i=0;i<(msg_size);i++){ serialOutBuf[i+12] = *(argv[2] + i); } wait_for_ack = 1; for(i=0; i<5; i++){ chMtxLock(&nrfMutex); chnWriteTimeout(&nrf24l01.channels[0], serialOutBuf, 32, MS2ST(100)); chMtxUnlock(&nrfMutex); chThdSleepMilliseconds(200); if (!wait_for_ack) break; chprintf((BaseSequentialStream*)&SD1, "failed to send %d\n\r", i); } msg_seq = msg_seq ^ 1; } else if(*(argv[0]) == 'r'){ if (*(argv[1]) == 'l'){ int i,j; for(i=0; i<iter; i++){ chprintf((BaseSequentialStream*)&SD1, "%d. <", i+1); for(j=0; j<5; j++){ chprintf((BaseSequentialStream*)&SD1, "%c", rec_list[(iter-1)-i][j]); } chprintf((BaseSequentialStream*)&SD1, ">, <%d> \n\r", rec_list[(iter-1)-i][5]); } }else{ int j; fifo = (fifo+1)%10; if(fifo != iter){ for(j=0; j<5; j++){ chprintf((BaseSequentialStream*)&SD1, "%c", rec_list[fifo][j]); } chprintf((BaseSequentialStream*)&SD1, ": " ); for(j=0; j<(serialInBuf[10]);j++){ chprintf((BaseSequentialStream*)&SD1, "%c ", serialInBuf[12+j]); } chprintf((BaseSequentialStream*)&SD1, "\n\r"); } for(j=0; j<26; j++){ rec_list[fifo][j] = 0; } } } }
void Lcd_t::Init() { // ==== Backlight: Timer15 Ch2 ==== // Setup pin PinSetupAlterFuncOutput(LCD_GPIO, LCD_BCKLT, omPushPull, ps50MHz); // Remap Timer15 to PB14 & PB15 AFIO->MAPR2 |= 0x00000001; // Setup timer15 rccEnableAPB2(RCC_APB2ENR_TIM15EN, false); TIM15->CR1 = 0x01; // Enable timer TIM15->CR2 = 0; TIM15->PSC = 0; // Do not divide input freq TIM15->ARR = 100; // Autoreload register: full brightness=100 TIM15->BDTR = 0xC000; // Main output Enable TIM15->CCMR1 = 0x6000; // PWM mode1 on Ch2 enabled TIM15->CCER = 0x0010; // Output2 enabled, polarity not inverted BackligthValue = 0; // ==== GPIOs ==== // Configure LCD_XRES, LCD_XCS, LCD_SCLK & LCD_SDA as Push-Pull output InitGpios(); // ========================= Init LCD ====================================== SCLK_Lo(); XCS_Hi(); // Reset display XRES_Lo(); chThdSleepMilliseconds(9); XRES_Hi(); WriteCmd(0xAF); // display ON // Reset display again XRES_Lo(); chThdSleepMilliseconds(7); XRES_Hi(); chThdSleepMilliseconds(7); // Initial commands WriteCmd(0xAF); // display ON WriteCmd(0xA4); // Set normal display mode WriteCmd(0x2F); // Charge pump on WriteCmd(0x40); // Set start row address = 0 // WriteCmd(0xC8); // mirror Y axis // WriteCmd(0xA1); // Mirror X axis // Set x=0, y=0 WriteCmd(0xB3); // Y axis initialization WriteCmd(0x10); // X axis initialisation1 WriteCmd(0x08); // X axis initialisation2 Cls(); // clear LCD buffer draw_mode = OVERWRITE; // ====================== Switch to USART + DMA ============================ #ifdef ENABLE_DMAUSART_MODE PinSetupAlterFuncOutput(LCD_GPIO, LCD_SCLK, omPushPull, ps50MHz); PinSetupAlterFuncOutput(LCD_GPIO, LCD_SDA, omPushPull, ps50MHz); // Workaround hardware bug with disabled CK3 when SPI2 is enabled SPI2->CR2 |= SPI_CR2_SSOE; // ==== USART init ==== rccEnableUSART3(false); // Usart clock: enabled, idle low, first edge, enable last bit pulse // Usart itself LCD_USART->BRR = Clk.APB1FreqHz / 100000; LCD_USART->CR1 = USART_CR1_TE | /* Transmitter enabled */ \ USART_CR1_M; /* 9 bit */ LCD_USART->CR2 = USART_CR2_CLKEN | \ USART_CR2_LBCL; LCD_USART->CR3 = USART_CR3_DMAT; // Enable DMA at transmitter LCD_USART->CR1 |= USART_CR1_UE; // Enable USART // ==== DMA ==== rccEnableDMA1(); dmaStreamAllocate(LCD_DMA, IRQ_PRIO_MEDIUM, LcdDmaCompIrq, NULL); dmaStreamSetPeripheral(LCD_DMA, &USART3->DR); dmaStreamSetMemory0(LCD_DMA, (uint32_t)&IBuf[0]); dmaStreamSetMode (LCD_DMA, LCD_DMA_MODE); // Start transmission XCS_Lo(); // DMA_Cmd(DMA1_Channel2, ENABLE); // Enable USARTy DMA TX Channel #else for(int i=0; i < 864; i++) WriteData(0x00); // Clear all screen #endif Backlight(0); chThdCreateStatic(waLcdThread, sizeof(waLcdThread), NORMALPRIO, (tfunc_t)LcdThread, NULL); }
/* * Application entry point. */ int main(void) { color_t color = Black; uint16_t pen = 0; Thread *shelltp = NULL; /* * 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(); gdispInit(); ginputGetMouse(0); gdispSetOrientation(GDISP_ROTATE_90); drawScreen(); /* * 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(1000); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); /* * Shell manager initialization. */ shellInit(); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state. */ while (TRUE) { if (!shelltp && (SDU1.config->usbp->state == USB_ACTIVE)) shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminated(shelltp)) { chThdRelease(shelltp); /* Recovers memory of the previous shell. */ shelltp = NULL; /* Triggers spawning of a new shell. */ } ginputGetMouseStatus(0, &ev); if (!(ev.current_buttons & GINPUT_MOUSE_BTN_LEFT)) { chThdSleepMicroseconds(500); // Senza questo sleep l'USB non parte continue; } /* inside color box ? */ if(ev.y >= OFFSET && ev.y <= COLOR_SIZE) { if(GET_COLOR(0)) color = Black; else if(GET_COLOR(1)) color = Red; else if(GET_COLOR(2)) color = Yellow; else if(GET_COLOR(3)) color = Green; else if(GET_COLOR(4)) color = Blue; else if(GET_COLOR(5)) color = White; /* inside pen box ? */ } else if(ev.x >= OFFSET && ev.x <= PEN_SIZE) { if(GET_PEN(1)) pen = 0; else if(GET_PEN(2)) pen = 1; else if(GET_PEN(3)) pen = 2; else if(GET_PEN(4)) pen = 3; else if(GET_PEN(5)) pen = 4; /* inside drawing area ? */ } else if(DRAW_AREA(ev.x, ev.y)) { if(pen == 0) gdispDrawPixel(ev.x, ev.y, color); else gdispFillCircle(ev.x, ev.y, pen, color); } } }
/** * @brief Performs the initialization procedure on the inserted card. * @details This function should be invoked when a card is inserted and * brings the driver in the @p MMC_READY state where it is possible * to perform read and write operations. * @note It is possible to invoke this function from the insertion event * handler. * * @param[in] mmcp pointer to the @p MMCDriver object * * @return The operation status. * @retval HAL_SUCCESS the operation succeeded and the driver is now * in the @p MMC_READY state. * @retval HAL_FAILED the operation failed. * * @api */ bool mmcConnect(MMCDriver *mmcp) { unsigned i; uint8_t r3[4]; osalDbgCheck(mmcp != NULL); osalDbgAssert((mmcp->state == BLK_ACTIVE) || (mmcp->state == BLK_READY), "invalid state"); /* Connection procedure in progress.*/ mmcp->state = BLK_CONNECTING; mmcp->block_addresses = FALSE; /* Slow clock mode and 128 clock pulses.*/ spiStart(mmcp->config->spip, mmcp->config->lscfg); spiIgnore(mmcp->config->spip, 16); /* SPI mode selection.*/ i = 0; while (TRUE) { if (send_command_R1(mmcp, MMCSD_CMD_GO_IDLE_STATE, 0) == 0x01) break; if (++i >= MMC_CMD0_RETRY) goto failed; chThdSleepMilliseconds(10); } /* Try to detect if this is a high capacity card and switch to block addresses if possible. This method is based on "How to support SDC Ver2 and high capacity cards" by ElmChan.*/ if (send_command_R3(mmcp, MMCSD_CMD_SEND_IF_COND, MMCSD_CMD8_PATTERN, r3) != 0x05) { /* Switch to SDHC mode.*/ i = 0; while (TRUE) { if ((send_command_R1(mmcp, MMCSD_CMD_APP_CMD, 0) == 0x01) && (send_command_R3(mmcp, MMCSD_CMD_APP_OP_COND, 0x400001aa, r3) == 0x00)) break; if (++i >= MMC_ACMD41_RETRY) goto failed; chThdSleepMilliseconds(10); } /* Execute dedicated read on OCR register */ send_command_R3(mmcp, MMCSD_CMD_READ_OCR, 0, r3); /* Check if CCS is set in response. Card operates in block mode if set.*/ if (r3[0] & 0x40) mmcp->block_addresses = TRUE; } /* Initialization.*/ i = 0; while (TRUE) { uint8_t b = send_command_R1(mmcp, MMCSD_CMD_INIT, 0); if (b == 0x00) break; if (b != 0x01) goto failed; if (++i >= MMC_CMD1_RETRY) goto failed; chThdSleepMilliseconds(10); } /* Initialization complete, full speed.*/ spiStart(mmcp->config->spip, mmcp->config->hscfg); /* Setting block size.*/ if (send_command_R1(mmcp, MMCSD_CMD_SET_BLOCKLEN, MMCSD_BLOCK_SIZE) != 0x00) goto failed; /* Determine capacity.*/ if (read_CxD(mmcp, MMCSD_CMD_SEND_CSD, mmcp->csd)) goto failed; mmcp->capacity = mmcsdGetCapacity(mmcp->csd); if (mmcp->capacity == 0) goto failed; if (read_CxD(mmcp, MMCSD_CMD_SEND_CID, mmcp->cid)) goto failed; mmcp->state = BLK_READY; return HAL_SUCCESS; /* Connection failed, state reset to BLK_ACTIVE.*/ failed: spiStop(mmcp->config->spip); mmcp->state = BLK_ACTIVE; return HAL_FAILED; }