Beispiel #1
0
void eepromCLI()
{
	char c;

	sensorConfig_t sensorRam;

	systemConfig_t systemRam;

	uint8_t  eepromQuery = 'x';

	uint8_t  *p;

	uint8_t  *end;

	uint8_t  secondNibble;

	uint8_t  validQuery  = false;

	uint16_t i;

	uint32_t c1, c2;

    uint32_t size;

	uint32_t time;

	uint32_t charsEncountered;

	///////////////////////////////////////////////////////////////////////////////

    cliBusy = true;

    cliPortPrint("\nEntering EEPROM CLI....\n\n");

    while(true)
    {
        cliPortPrint("EEPROM CLI -> ");

        while ((cliPortAvailable() == false) && (validQuery == false));

        if (validQuery == false)
            eepromQuery = cliPortRead();

        cliPortPrint("\n");

        switch(eepromQuery)
        {
            ///////////////////////////

            case 'a': // config struct data
                c1 = sensorConfig.CRCAtEnd[0];

                c2 = crc32B((uint32_t*)(&sensorConfig),                  // CRC32B[sensorConfig]
                            (uint32_t*)(&sensorConfig.CRCAtEnd));

                cliPortPrintF("Sensor EEPROM structure information:\n");
                cliPortPrintF("Version          : %d\n", sensorConfig.version);
                cliPortPrintF("Size             : %d\n", sizeof(sensorConfig));
                cliPortPrintF("CRC on last read : %08X\n", c1);
                cliPortPrintF("Current CRC      : %08X\n", c2);

                if ( c1 != c2 )
                    cliPortPrintF("  CRCs differ. Current Sensor Config has not yet been saved.\n");

                cliPortPrintF("CRC Flags :\n");
                cliPortPrintF("  History Bad    : %s\n", sensorConfig.CRCFlags & CRC_HistoryBad ? "true" : "false" );

                validQuery = false;
                break;

            ///////////////////////////

            case 'b': // Write out to Console in Hex.  (RAM -> console)
                // we assume the flyer is not in the air, so that this is ok;

                cliPortPrintF("\n");

                cliPrintSensorEEPROM();

                cliPortPrintF("\n");

                if (crcCheckVal != crc32B((uint32_t*)(&sensorConfig),       // CRC32B[sensorConfig CRC32B[sensorConfig]]
                                          (uint32_t*)(&sensorConfig + 1)))
                {
                    cliPortPrint("NOTE: in-memory sensor config CRC invalid; there have probably been\n");
                    cliPortPrint("      changes to sensor config since the last write to flash/eeprom.\n");
                }

                validQuery = false;
                break;

            ///////////////////////////

            case 'c': // Read Sensor Config -> RAM
                cliPortPrint("Re-reading Sensor EEPROM.\n");
                readSensorEEPROM();

                validQuery = false;
                break;

            ///////////////////////////

            case 'd': // Read Console -> Sensor RAM
                charsEncountered = 0;

                secondNibble = 0;

                size = sizeof(sensorConfig);

                time = millis();

                p = (uint8_t*)&sensorRam;

                end = (uint8_t*)(&sensorRam + 1);

                ///////////////////////

                cliPortPrintF("Ready to read in sensor config. Expecting %d (0x%03X) bytes as %d\n", size, size, size * 2);

                cliPortPrintF("hexadecimal characters, optionally separated by [ \\n\\r_].\n");

                cliPortPrintF("Times out if no character is received for %dms\n", TIMEOUT);

                memset(p, 0, end - p);

                while (p < end)
                {
                    while (!cliPortAvailable() && millis() - time < TIMEOUT) {}
                    time = millis();

                    c = cliPortAvailable() ? cliPortRead() : '\0';

                    int8_t hex = parse_hex(c);

                    int ignore = c == ' ' || c == '\n' || c == '\r' || c == '_' ? true : false;

                    if (c != '\0') // assume the person isn't sending null chars
                        charsEncountered++;

                    if (ignore)
                        continue;

                    if (hex == -1)
                        break;

                    *p |= secondNibble ? hex : hex << 4;

                    p += secondNibble;

                    secondNibble ^= 1;
                }

                if (c == 0)
                {
                    cliPortPrintF("Did not receive enough hex chars! (got %d, expected %d)\n",
                        (p - (uint8_t*)&sensorRam) * 2 + secondNibble, size * 2);
                }
                else if (p < end || secondNibble)
                {
                    cliPortPrintF("Invalid character found at position %d: '%c' (0x%02x)",
                        charsEncountered, c, c);
                }
                // HJI else if (crcCheckVal != crc32B((uint32_t*)(&sensorConfig),       // CRC32B[sensorConfig CRC32B[sensorConfig]]
                // HJI                                (uint32_t*)(&sensorConfig + 1)))
                // HJI {
                // HJI     cliPortPrintF("CRC mismatch! Not writing to in-memory config.\n");
                // HJI     cliPortPrintF("Here's what was received:\n\n");
                // HJI     cliPrintSensorEEPROM();
                // HJI }
                else
                {
                    // check to see if the newly received sytem config
                    // actually differs from what's in-memory

                    for (i = 0; i < size; i++)
                        if (((uint8_t*)&sensorRam)[i] != ((uint8_t*)&sensorConfig)[i])
                            break;

                    if (i == size)
                    {
                        cliPortPrintF("NOTE: Uploaded Sensor Config was Identical to RAM Config.\n");
                    }
                    else
                    {
                        sensorConfig = sensorRam;
                        cliPortPrintF("Sensor RAM Config updated!\n");
                        cliPortPrintF("NOTE: Sensor Config not written to EEPROM; use 'w' to do so.\n");
                    }

                }

                // eat the next 100ms (or whatever Timeout is) of characters,
                // in case the person pasted too much by mistake or something

                time = millis();

                while (millis() - time < TIMEOUT)
                    if (cliPortAvailable())
                        cliPortRead();

                validQuery = false;
                break;

            ///////////////////////////

            case 'h': // Clear Bad Sensor History Flag
                cliPortPrintF("Clearing Bad Sensor History Flag.\n");
                sensorConfig.CRCFlags &= ~CRC_HistoryBad;
                validQuery = false;
                break;

            ///////////////////////////

            case 'v': // Reset Sensor EEPROM Parameters
                cliPortPrint( "\nSensor EEPROM Parameters Reset....(not rebooting)\n" );
                checkSensorEEPROM(true);
                validQuery = false;
                break;

            ///////////////////////////

            case 'w': // Write to Sensor EEPROM
                cliPortPrint("\nWriting Sensor EEPROM Parameters....\n");
                writeSensorEEPROM();

                validQuery = false;
                break;

            ///////////////////////////

            case 'x': // exit EEPROM CLI
                cliPortPrint("\nExiting EEPROM CLI....\n\n");
                cliBusy = false;
                return;
                break;

            ///////////////////////////

            case 'A': // config struct data
                c1 = systemConfig.CRCAtEnd[0];

                zeroPIDstates();

                c2 = crc32B((uint32_t*)(&systemConfig),                  // CRC32B[systemConfig]
                            (uint32_t*)(&systemConfig.CRCAtEnd));

                cliPortPrintF("System EEPROM structure information:\n");
                cliPortPrintF("Version          : %d\n", systemConfig.version);
                cliPortPrintF("Size             : %d\n", sizeof(systemConfig));
                cliPortPrintF("CRC on last read : %08X\n", c1);
                cliPortPrintF("Current CRC      : %08X\n", c2);

                if ( c1 != c2 )
                    cliPortPrintF("  CRCs differ. Current SystemConfig has not yet been saved.\n");

                cliPortPrintF("CRC Flags :\n");
                cliPortPrintF("  History Bad    : %s\n", systemConfig.CRCFlags & CRC_HistoryBad ? "true" : "false" );

                validQuery = false;
                break;

            ///////////////////////////

            case 'B': // Write out to Console in Hex.  (RAM -> console)
                // we assume the flyer is not in the air, so that this is ok;

                // these change randomly when not in flight and can mistakenly
                // make one think that the in-memory eeprom struct has changed

                zeroPIDstates();

                cliPortPrintF("\n");

                cliPrintSystemConfig();

                cliPortPrintF("\n");

                if (crcCheckVal != crc32B((uint32_t*)(&systemConfig),       // CRC32B[systemConfig CRC32B[systemConfig]]
                                          (uint32_t*)(&systemConfig + 1)))
                {
                    cliPortPrint("NOTE: in-memory system config CRC invalid; there have probably been\n");
                    cliPortPrint("      changes to system config since the last write to flash/eeprom.\n");
                }

                validQuery = false;
                break;

            ///////////////////////////

            case 'C': // Read System Config -> RAM
                cliPortPrint("Re-reading System EEPROM.\n");
                readSystemEEPROM();

                validQuery = false;
                break;

            ///////////////////////////

            case 'D': // Read Console -> System RAM
            	charsEncountered = 0;

            	secondNibble = 0;

            	size = sizeof(systemConfig);

                time = millis();

                p = (uint8_t*)&systemRam;

                end = (uint8_t*)(&systemRam + 1);

                ///////////////////////

                cliPortPrintF("Ready to read in system config. Expecting %d (0x%03X) bytes as %d\n", size, size, size * 2);

                cliPortPrintF("hexadecimal characters, optionally separated by [ \\n\\r_].\n");

                cliPortPrintF("Times out if no character is received for %dms\n", TIMEOUT);

                memset(p, 0, end - p);

                while (p < end)
                {
                    while (!cliPortAvailable() && millis() - time < TIMEOUT) {}
                    time = millis();

                    c = cliPortAvailable() ? cliPortRead() : '\0';

                    int8_t hex = parse_hex(c);

                    int ignore = c == ' ' || c == '\n' || c == '\r' || c == '_' ? true : false;

                    if (c != '\0') // assume the person isn't sending null chars
                        charsEncountered++;

                    if (ignore)
                        continue;

                    if (hex == -1)
                        break;

                    *p |= secondNibble ? hex : hex << 4;

                    p += secondNibble;

                    secondNibble ^= 1;
                }

                if (c == 0)
                {
                    cliPortPrintF("Did not receive enough hex chars! (got %d, expected %d)\n",
                        (p - (uint8_t*)&systemRam) * 2 + secondNibble, size * 2);
                }
                else if (p < end || secondNibble)
                {
                    cliPortPrintF("Invalid character found at position %d: '%c' (0x%02x)",
                        charsEncountered, c, c);
                }
                // HJI else if (crcCheckVal != crc32B((uint32_t*)(&systemConfig),       // CRC32B[systemConfig CRC32B[systemConfig]]
                // HJI                                (uint32_t*)(&systemConfig + 1)))
                // HJI {
                // HJI     cliPortPrintF("CRC mismatch! Not writing to in-memory config.\n");
                // HJI     cliPortPrintF("Here's what was received:\n\n");
                // HJI     cliPrintSystemConfig();
                // HJI }
                else
                {
                    // check to see if the newly received sytem config
                    // actually differs from what's in-memory

                    zeroPIDstates();

                    for (i = 0; i < size; i++)
                        if (((uint8_t*)&systemRam)[i] != ((uint8_t*)&systemConfig)[i])
                            break;

                    if (i == size)
                    {
                        cliPortPrintF("NOTE: Uploaded System Config was Identical to RAM Config.\n");
                    }
                    else
                    {
                        systemConfig = systemRam;
                        cliPortPrintF("System RAM Config updated!\n");
                        cliPortPrintF("NOTE: System Config not written to EEPROM; use 'W' to do so.\n");
                    }

                }

                // eat the next 100ms (or whatever Timeout is) of characters,
                // in case the person pasted too much by mistake or something

                time = millis();

                while (millis() - time < TIMEOUT)
                    if (cliPortAvailable())
                        cliPortRead();

                validQuery = false;
                break;

            ///////////////////////////

            case 'H': // Clear Bad System History Flag
                cliPortPrintF("Clearing Bad System History Flag.\n");
                systemConfig.CRCFlags &= ~CRC_HistoryBad;
                validQuery = false;
                break;

            ///////////////////////////

            case 'V': // Reset System EEPROM Parameters
                cliPortPrint( "\nSystem EEPROM Parameters Reset....(not rebooting)\n" );
                checkSystemEEPROM(true);

                validQuery = false;
                break;

            ///////////////////////////

            case 'W': // Write out to System EEPROM
                cliPortPrint("\nWriting System EEPROM Parameters....\n");
                writeSystemEEPROM();

                validQuery = false;
                break;

            ///////////////////////////

            case '?':
            //                0         1         2         3         4         5         6         7
            //                01234567890123456789012345678901234567890123456789012345678901234567890123456789
                cliPortPrintF("\n");
                cliPortPrintF("'a' Display Sensor Config Information     'A' Display System Config Information\n");
                cliPortPrintF("'b' Write Sensor Config -> Console        'B' Write System Config - > Console\n");
                cliPortPrintF("'c' Read Sensor Config -> RAM             'C' Read System Config -> RAM\n");
                cliPortPrintF("'d' Read Console -> Sensor RAM            'D' Read Console -> System RAM\n");
                cliPortPrintF("'h' Clear Sensor CRC Bad History flag     'H' Clear System CRC Bad History flag\n");
                cliPortPrintF("'v' Reset Sensor Config to Default        'V' Reset System Config to Default\n");
                cliPortPrintF("'w' Write Sensor Config -> EEPROM         'W' Write System Config -> EEPROM\n");
                cliPortPrintF("'x' Exit EEPROM CLI                       '?' Command Summary\n");
                cliPortPrintF("\n");
                break;

            ///////////////////////////
        }
    }
}
Beispiel #2
0
void systemInit(void)
{
	RCC_ClocksTypeDef rccClocks;

	///////////////////////////////////

	// Init cycle counter
    cycleCounterInit();

    // SysTick
    SysTick_Config(SystemCoreClock / 1000);

    // Turn on peripherial clocks
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12,    ENABLE);

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,     ENABLE);  // USART1, USART2
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,     ENABLE);  // ADC2

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,    ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,    ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,    ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,   ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,   ENABLE);  // PPM RX
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,   ENABLE);  // PWM ESC Out 1 & 2
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,   ENABLE);  // PWM ESC Out 5 & 6
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,   ENABLE);  // PWM Servo Out 1, 2, 3, & 4
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,   ENABLE);  // 500 Hz dt Counter
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,   ENABLE);  // 100 Hz dt Counter
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15,  ENABLE);  // PWM ESC Out 3 & 4
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16,  ENABLE);  // RangeFinder PWM
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17,  ENABLE);  // Spektrum Frame Sync

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  // Telemetry
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  // GPS
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  // Spektrum RX

    ///////////////////////////////////////////////////////////////////////////

    spiInit(SPI2);

    ///////////////////////////////////

    checkSensorEEPROM(false);
	checkSystemEEPROM(false);

	readSensorEEPROM();
	readSystemEEPROM();

	///////////////////////////////////

	if (systemConfig.receiverType == SPEKTRUM)
		checkSpektrumBind();

    ///////////////////////////////////

	checkResetType();

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  // 2 bits for pre-emption priority, 2 bits for subpriority

	///////////////////////////////////

	gpsPortClearBuffer       = &uart2ClearBuffer;
    gpsPortNumCharsAvailable = &uart2NumCharsAvailable;
    gpsPortPrintBinary       = &uart2PrintBinary;
    gpsPortRead              = &uart2Read;

    telemPortAvailable       = &uart1Available;
    telemPortPrint           = &uart1Print;
    telemPortPrintF          = &uart1PrintF;
    telemPortRead            = &uart1Read;

	///////////////////////////////////

	initMixer();

	usbInit();

	gpioInit();

	uart1Init();
    uart2Init();

    LED0_OFF;

    delay(10000);  // 10 seconds of 20 second delay for sensor stabilization

    checkUsbActive();

    ///////////////////////////////////

    #ifdef __VERSION__
        cliPortPrintF("\ngcc version " __VERSION__ "\n");
    #endif

    cliPortPrintF("\nFF32mini Firmware V%s, Build Date " __DATE__ " "__TIME__" \n", __FF32MINI_VERSION);

    if ((RCC->CR & RCC_CR_HSERDY) != RESET)
    {
        cliPortPrint("\nRunning on external HSE clock....\n");
    }
    else
    {
        cliPortPrint("\nERROR: Running on internal HSI clock....\n");
    }

    RCC_GetClocksFreq(&rccClocks);

    cliPortPrintF("\nHCLK->   %2d MHz\n",   rccClocks.HCLK_Frequency   / 1000000);
    cliPortPrintF(  "PCLK1->  %2d MHz\n",   rccClocks.PCLK1_Frequency  / 1000000);
    cliPortPrintF(  "PCLK2->  %2d MHz\n",   rccClocks.PCLK2_Frequency  / 1000000);
    cliPortPrintF(  "SYSCLK-> %2d MHz\n\n", rccClocks.SYSCLK_Frequency / 1000000);

    if (systemConfig.receiverType == PPM)
    	cliPortPrint("Using PPM Receiver....\n\n");
    else
    	cliPortPrint("Using Spektrum Satellite Receiver....\n\n");

    initUBLOX();

    delay(10000);  // Remaining 10 seconds of 20 second delay for sensor stabilization - probably not long enough..

    ///////////////////////////////////

	adcInit();
    aglInit();
    pwmServoInit();

    if (systemConfig.receiverType == SPEKTRUM)
        spektrumInit();
    else
        ppmRxInit();

    timingFunctionsInit();

    batteryInit();

    initFirstOrderFilter();
    initMavlink();
    initPID();

    LED0_ON;

    initMPU6000();
    initMag();
    initPressure();
}
Beispiel #3
0
void writeSensorEEPROM(void)
{
    uint16_t byteCount;
    uint8_t  pageIndex;

    uint8_t* start = (uint8_t*)(&sensorConfig);
	uint8_t* end   = (uint8_t*)(&sensorConfig + 1);

	///////////////////////////////////

    if (sensorConfig.CRCFlags & CRC_HistoryBad)
        evrPush(EVR_ConfigBadSensorHistory,0);

    sensorConfig.CRCAtEnd[0] = crc32B((uint32_t*)(&sensorConfig),                  // CRC32B[sensorConfig]
                                      (uint32_t*)(&sensorConfig.CRCAtEnd));

    ///////////////////////////////////

    setSPIdivisor(EEPROM_SPI, 2);  // 18 MHz SPI clock

    sensorConfigAddr.value = SENSOR_EEPROM_ADDR;

    ///////////////////////////////////

    // Sector Erase

    writeEnable();

    ENABLE_EEPROM;

	spiTransfer(EEPROM_SPI, SECTOR_ERASE_64KB);

	spiTransfer(EEPROM_SPI, sensorConfigAddr.bytes[2]);
	spiTransfer(EEPROM_SPI, sensorConfigAddr.bytes[1]);
	spiTransfer(EEPROM_SPI, sensorConfigAddr.bytes[0]);

	DISABLE_EEPROM;

	delayMicroseconds(2);

	eepromBusy();

    ///////////////////////////////////

    // Program Page(s)

	for (pageIndex = 0; pageIndex < ((sizeof(sensorConfig) / 256) + 1); pageIndex++)
    {
        writeEnable();

        ENABLE_EEPROM;

        spiTransfer(EEPROM_SPI, PAGE_PROGRAM_256_BYTES);

		spiTransfer(EEPROM_SPI, sensorConfigAddr.bytes[2]);
		spiTransfer(EEPROM_SPI, sensorConfigAddr.bytes[1]);
		spiTransfer(EEPROM_SPI, sensorConfigAddr.bytes[0]);

	    for (byteCount = 0; byteCount < 256; byteCount++)
	    {
			spiTransfer(EEPROM_SPI, *start++);

			if (start >= end)
			    break;
		}

		DISABLE_EEPROM;

		delayMicroseconds(2);

		eepromBusy();

		sensorConfigAddr.value += 0x0100;
    }

	readSensorEEPROM();
}