示例#1
0
文件: init.c 项目: vivienJ/Firmware
__EXPORT int board_app_initialize(uintptr_t arg)
{
	/* Ensure the power is on 1 ms before we drive the GPIO pins */
	usleep(1000);

	if (OK == determin_hw_version(&hw_version, & hw_revision)) {
		switch (hw_version) {
		case HW_VER_FMUV2_STATE:
			break;

		case HW_VER_FMUV3_STATE:
			hw_type[1]++;
			hw_type[2] = '0';

			/* Has CAN2 transceiver Remove pull up */

			stm32_configgpio(GPIO_CAN2_RX);

			break;

		case HW_VER_FMUV2MINI_STATE:

			/* Detection for a Pixhack3 */

			stm32_configgpio(HW_VER_PA8);
			up_udelay(10);
			bool isph3 = stm32_gpioread(HW_VER_PA8);
			stm32_configgpio(HW_VER_PA8_INIT);


			if (isph3) {

				/* Pixhack3 looks like a FMuV3 Cube */

				hw_version = HW_VER_FMUV3_STATE;
				hw_type[1]++;
				hw_type[2] = '0';
				message("\nPixhack V3 detected, forcing to fmu-v3");

			} else {

				/* It is a mini */

				hw_type[2] = 'M';
			}

			break;

		default:

			/* questionable px4_fmu-v2 hardware, try forcing regular FMUv2 (not much else we can do) */

			message("\nbad version detected, forcing to fmu-v2");
			hw_version = HW_VER_FMUV2_STATE;
			break;
		}

		message("\nFMUv2 ver 0x%1X : Rev %x %s\n", hw_version, hw_revision, hw_type);
	}

	/* configure SPI interfaces */
	stm32_spiinitialize();

	px4_platform_init();

	/* configure the DMA allocator */

	if (board_dma_alloc_init() < 0) {
		message("DMA alloc FAILED");
	}

	/* set up the serial DMA polling */
	static struct hrt_call serial_dma_call;
	struct timespec ts;

	/*
	 * Poll at 1ms intervals for received bytes that have not triggered
	 * a DMA event.
	 */
	ts.tv_sec = 0;
	ts.tv_nsec = 1000000;

	hrt_call_every(&serial_dma_call,
		       ts_to_abstime(&ts),
		       ts_to_abstime(&ts),
		       (hrt_callout)stm32_serial_dma_poll,
		       NULL);

	/* initial LED state */
	drv_led_start();
	led_off(LED_AMBER);

	if (board_hardfault_init(2, true) != 0) {
		led_on(LED_AMBER);
	}

	/* Configure SPI-based devices */

	spi1 = stm32_spibus_initialize(PX4_SPI_BUS_SENSORS);

	if (!spi1) {
		message("[boot] FAILED to initialize SPI port %d\n", PX4_SPI_BUS_SENSORS);
		led_on(LED_AMBER);
		return -ENODEV;
	}

	/* Default SPI1 to 1MHz and de-assert the known chip selects. */
	SPI_SETFREQUENCY(spi1, 10000000);
	SPI_SETBITS(spi1, 8);
	SPI_SETMODE(spi1, SPIDEV_MODE3);
	up_udelay(20);

	/* Get the SPI port for the FRAM */

	spi2 = stm32_spibus_initialize(PX4_SPI_BUS_RAMTRON);

	if (!spi2) {
		message("[boot] FAILED to initialize SPI port %d\n", PX4_SPI_BUS_RAMTRON);
		led_on(LED_AMBER);
		return -ENODEV;
	}

	/* Default SPI2 to 37.5 MHz (40 MHz rounded to nearest valid divider, F4 max)
	 * and de-assert the known chip selects. */

	// XXX start with 10.4 MHz in FRAM usage and go up to 37.5 once validated
	SPI_SETFREQUENCY(spi2, 12 * 1000 * 1000);
	SPI_SETBITS(spi2, 8);
	SPI_SETMODE(spi2, SPIDEV_MODE3);

	spi4 = stm32_spibus_initialize(PX4_SPI_BUS_EXT);

	if (!spi4) {
		message("[boot] FAILED to initialize SPI port %d\n", PX4_SPI_BUS_EXT);
		led_on(LED_AMBER);
		return -ENODEV;
	}

	/* Default SPI4 to 1MHz and de-assert the known chip selects. */
	SPI_SETFREQUENCY(spi4, 10000000);
	SPI_SETBITS(spi4, 8);
	SPI_SETMODE(spi4, SPIDEV_MODE3);

#ifdef CONFIG_MMCSD
	/* First, get an instance of the SDIO interface */

	sdio = sdio_initialize(CONFIG_NSH_MMCSDSLOTNO);

	if (!sdio) {
		led_on(LED_AMBER);
		message("[boot] Failed to initialize SDIO slot %d\n", CONFIG_NSH_MMCSDSLOTNO);
		return -ENODEV;
	}

	/* Now bind the SDIO interface to the MMC/SD driver */
	int ret = mmcsd_slotinitialize(CONFIG_NSH_MMCSDMINOR, sdio);

	if (ret != OK) {
		led_on(LED_AMBER);
		message("[boot] Failed to bind SDIO to the MMC/SD driver: %d\n", ret);
		return ret;
	}

	/* Then let's guess and say that there is a card in the slot. There is no card detect GPIO. */
	sdio_mediachange(sdio, true);

#endif

	return OK;
}
示例#2
0
__EXPORT int board_app_initialize(uintptr_t arg)
{

#if defined(CONFIG_HAVE_CXX) && defined(CONFIG_HAVE_CXXINITIALIZE)

	/* run C++ ctors before we go any further */

	up_cxxinitialize();

#	if defined(CONFIG_EXAMPLES_NSH_CXXINITIALIZE)
#  		error CONFIG_EXAMPLES_NSH_CXXINITIALIZE Must not be defined! Use CONFIG_HAVE_CXX and CONFIG_HAVE_CXXINITIALIZE.
#	endif

#else
#  error platform is dependent on c++ both CONFIG_HAVE_CXX and CONFIG_HAVE_CXXINITIALIZE must be defined.
#endif

#if defined(BOARD_HAS_SIMPLE_HW_VERSIONING)

	if (OK == determin_hw_version(&hw_version, & hw_revision)) {
		switch (hw_version) {
		default:
		case 0x8:
			break;

		case 0xE:
			hw_type[1]++;
			hw_type[2] = '0';
			break;

		case 0xA:
			hw_type[2] = 'M';
			break;
		}

		PX4_INFO("Ver 0x%1X : Rev %x %s", hw_version, hw_revision, hw_type);
	}

#endif // BOARD_HAS_SIMPLE_HW_VERSIONING

	/* Bring up the Sensor power */

	stm32_gpiowrite(GPIO_VDD_3V3_SENSORS_EN, 1);

	/* Ensure the power is on 1 ms before we drive the GPIO pins */

	usleep(1000);

	/* Now it is ok to drive the pins high so configure SPI GPIO */

	stm32_spiinitialize();


	/* configure the high-resolution time/callout interface */

	hrt_init();

	param_init();

	/* configure the DMA allocator */

	if (board_dma_alloc_init() < 0) {
		message("DMA alloc FAILED");
	}

	/* configure CPU load estimation */
#ifdef CONFIG_SCHED_INSTRUMENTATION
	cpuload_initialize_once();
#endif

	/* set up the serial DMA polling */
	static struct hrt_call serial_dma_call;
	struct timespec ts;

	/*
	 * Poll at 1ms intervals for received bytes that have not triggered
	 * a DMA event.
	 */
	ts.tv_sec = 0;
	ts.tv_nsec = 1000000;

	hrt_call_every(&serial_dma_call,
		       ts_to_abstime(&ts),
		       ts_to_abstime(&ts),
		       (hrt_callout)stm32_serial_dma_poll,
		       NULL);

#if defined(CONFIG_STM32_BBSRAM)

	if (hardfault_check_status) {
		/* NB. the use of the console requires the hrt running
		 * to poll the DMA
		 */

		/* Using Battery Backed Up SRAM */

		int filesizes[CONFIG_STM32_BBSRAM_FILES + 1] = BSRAM_FILE_SIZES;

		stm32_bbsraminitialize(BBSRAM_PATH, filesizes);

#if defined(CONFIG_STM32_SAVE_CRASHDUMP)

		/* Panic Logging in Battery Backed Up Files */

		/*
		 * In an ideal world, if a fault happens in flight the
		 * system save it to BBSRAM will then reboot. Upon
		 * rebooting, the system will log the fault to disk, recover
		 * the flight state and continue to fly.  But if there is
		 * a fault on the bench or in the air that prohibit the recovery
		 * or committing the log to disk, the things are too broken to
		 * fly. So the question is:
		 *
		 * Did we have a hard fault and not make it far enough
		 * through the boot sequence to commit the fault data to
		 * the SD card?
		 */

		/* Do we have an uncommitted hard fault in BBSRAM?
		 *  - this will be reset after a successful commit to SD
		 */
		int hadCrash = hardfault_check_status("boot");

		if (hadCrash == OK) {

			message("[boot] There is a hard fault logged. Hold down the SPACE BAR," \
				" while booting to halt the system!\n");

			/* Yes. So add one to the boot count - this will be reset after a successful
			 * commit to SD
			 */

			int reboots = hardfault_increment_reboot("boot", false);

			/* Also end the misery for a user that holds for a key down on the console */

			int bytesWaiting;
			ioctl(fileno(stdin), FIONREAD, (unsigned long)((uintptr_t) &bytesWaiting));

			if (reboots > 2 || bytesWaiting != 0) {

				/* Since we can not commit the fault dump to disk. Display it
				 * to the console.
				 */

				hardfault_write("boot", fileno(stdout), HARDFAULT_DISPLAY_FORMAT, false);

				message("[boot] There were %d reboots with Hard fault that were not committed to disk - System halted %s\n",
					reboots,
					(bytesWaiting == 0 ? "" : " Due to Key Press\n"));


				/* For those of you with a debugger set a break point on up_assert and
				 * then set dbgContinue = 1 and go.
				 */

				/* Clear any key press that got us here */

				static volatile bool dbgContinue = false;
				int c = '>';

				while (!dbgContinue) {

					switch (c) {

					case EOF:


					case '\n':
					case '\r':
					case ' ':
						continue;

					default:

						putchar(c);
						putchar('\n');

						switch (c) {

						case 'D':
						case 'd':
							hardfault_write("boot", fileno(stdout), HARDFAULT_DISPLAY_FORMAT, false);
							break;

						case 'C':
						case 'c':
							hardfault_rearm("boot");
							hardfault_increment_reboot("boot", true);
							break;

						case 'B':
						case 'b':
							dbgContinue = true;
							break;

						default:
							break;
						} // Inner Switch

						message("\nEnter B - Continue booting\n" \
							"Enter C - Clear the fault log\n" \
							"Enter D - Dump fault log\n\n?>");
						fflush(stdout);

						if (!dbgContinue) {
							c = getchar();
						}

						break;

					} // outer switch
				} // for

			} // inner if
		} // outer if
	} // hardfault_check_status

#endif // CONFIG_STM32_SAVE_CRASHDUMP
#endif // CONFIG_STM32_BBSRAM

	/* initial LED state */
	drv_led_start();
	led_off(LED_AMBER);

	/* Configure SPI-based devices */

	spi1 = stm32_spibus_initialize(PX4_SPI_BUS_SENSORS);

	if (!spi1) {
		message("[boot] FAILED to initialize SPI port %d\n", PX4_SPI_BUS_SENSORS);
		board_autoled_on(LED_AMBER);
		return -ENODEV;
	}

	/* Default SPI1 to 1MHz and de-assert the known chip selects. */
	SPI_SETFREQUENCY(spi1, 10000000);
	SPI_SETBITS(spi1, 8);
	SPI_SETMODE(spi1, SPIDEV_MODE3);
	up_udelay(20);

	/* Get the SPI port for the FRAM */

	spi2 = stm32_spibus_initialize(PX4_SPI_BUS_RAMTRON);

	if (!spi2) {
		message("[boot] FAILED to initialize SPI port %d\n", PX4_SPI_BUS_RAMTRON);
		board_autoled_on(LED_AMBER);
		return -ENODEV;
	}

	/* Default SPI2 to 37.5 MHz (40 MHz rounded to nearest valid divider, F4 max)
	 * and de-assert the known chip selects. */

	// XXX start with 10.4 MHz in FRAM usage and go up to 37.5 once validated
	SPI_SETFREQUENCY(spi2, 12 * 1000 * 1000);
	SPI_SETBITS(spi2, 8);
	SPI_SETMODE(spi2, SPIDEV_MODE3);

	spi4 = stm32_spibus_initialize(PX4_SPI_BUS_EXT);

	if (!spi4) {
		message("[boot] FAILED to initialize SPI port %d\n", PX4_SPI_BUS_EXT);
		board_autoled_on(LED_AMBER);
		return -ENODEV;
	}

	/* Default SPI4 to 1MHz and de-assert the known chip selects. */
	SPI_SETFREQUENCY(spi4, 10000000);
	SPI_SETBITS(spi4, 8);
	SPI_SETMODE(spi4, SPIDEV_MODE3);

#ifdef CONFIG_MMCSD
	/* First, get an instance of the SDIO interface */

	sdio = sdio_initialize(CONFIG_NSH_MMCSDSLOTNO);

	if (!sdio) {
		message("[boot] Failed to initialize SDIO slot %d\n",
			CONFIG_NSH_MMCSDSLOTNO);
		return -ENODEV;
	}

	/* Now bind the SDIO interface to the MMC/SD driver */
	int ret = mmcsd_slotinitialize(CONFIG_NSH_MMCSDMINOR, sdio);

	if (ret != OK) {
		message("[boot] Failed to bind SDIO to the MMC/SD driver: %d\n", ret);
		return ret;
	}

	/* Then let's guess and say that there is a card in the slot. There is no card detect GPIO. */
	sdio_mediachange(sdio, true);

#endif

	return OK;
}