/*
 * 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 1 using the driver default configuration.
	 */
	sdStart(&SERIAL_DRIVER, NULL);

	/*
	 * Activates the EXT driver.
	 */
	extStart(&EXTD1, &extcfg);

	/*
	 * Activates the I2C driver.
	 */
	i2cStart(&I2C_DRIVER, &i2c1cfg);

	/*
	 * Activates the SPI driver.
	 */
	spiStart(&SPI_DRIVER, &spi1cfg);

	/*
	 * Activates the RTCAN driver.
	 */
	rtcanInit();
	rtcanStart(&RTCAND1, &rtcan_config);

	/*
	 * Creates the blinker thread.
	 */
	chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

	chThdSleepMilliseconds(100);

	/*
	 * Creates the sensor threads.
	 */
	gyrotp = gyroRun(&SPI_DRIVER, NORMALPRIO);
	acctp = accRun(&I2C_DRIVER, NORMALPRIO);
	magtp = magRun(&I2C_DRIVER, NORMALPRIO);

	/*
	 * Creates the publisher threads.
	 */
	chThdCreateFromHeap(NULL, WA_SIZE_2K, NORMALPRIO + 1, PublisherRawThread,
			NULL);

	chThdSleepMilliseconds(100);

	remote_sub("IMURaw");

	/*
	 * Normal main() thread activity, in this demo it does nothing except
	 * sleeping in a loop and check the button state.
	 */
	while (TRUE) {
		chThdSleepMilliseconds(200);
	}
}
/*
 * Application entry point.
 */
int main(void) {
	RTCANConfig rtcan_config = {1000000, 100, 60};
	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();

	/*
	 * Activates the serial driver 1 using the driver default configuration.
	 */
	sdStart(&SERIAL_DRIVER, NULL);

	/*
	 * Shell manager initialization.
	 */
	shellInit();

	rtcanInit();
	rtcanStart (&RTCAND1, &rtcan_config);

	/*
	 * Creates the blinker thread.
	 */
	chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

	/*
	 * Creates the subscriber thread #1.
	 */
	chThdCreateFromHeap (NULL, WA_SIZE_512B, NORMALPRIO + 1, SubscriberThread1, NULL);

	/*
	 * Creates the subscriber thread #2.
	 */
	chThdCreateFromHeap (NULL, WA_SIZE_512B, NORMALPRIO + 1, SubscriberThread2, NULL);

	/*
	 * Creates the RX thread.
	 */
//	chThdCreateFromHeap (NULL, WA_SIZE_512B, NORMALPRIO + 2, RxThread, NULL);
	Middleware & mw = Middleware::instance();
	RemotePublisher rpub("led23", sizeof(LEDDataDebug));
	rpub.id((123 << 8) | 40);
	mw.advertise(&rpub);

	/*
	 * Normal main() thread activity, in this demo it does nothing except
	 * sleeping in a loop and check the button state.
	 */
	while (TRUE) {
		if (!shelltp)
			shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO - 1);
		else if (chThdTerminated(shelltp)) {
			chThdRelease(shelltp);
			shelltp = NULL;
		}
		chThdSleepMilliseconds(200);
	}
}