Example #1
0
int main(int argc, char **argv)
{
	uint8_t mcusr;
	char startupmsg[17];

 startmain:
	cli();
	mcusr = MCUSR;
	MCUSR = 0;
	TOGGLE_BEGIN();
	BSP_startmain();	/* Disables the watchdog timer. */
	serial_init();
	serial_send_rom(startup_message);
	serial_drain();
	SERIALSTR("*** Reset reason:");
	if (mcusr & (1 << WDRF)) SERIALSTR(" WD");
	if (mcusr & (1 << BORF)) SERIALSTR(" BO");
	if (mcusr & (1 << EXTRF)) SERIALSTR(" EXT");
	if (mcusr & (1 << PORF)) SERIALSTR(" PO");
	SERIALSTR("\r\n");
	twi_ctor();
	timekeeper_ctor();
	lcd_init();
	// Show the startup reason on the LCD.
	strcpy(startupmsg, "Startup: ----");
	if (mcusr & (1<<3)) startupmsg[9] = 'W';
	if (mcusr & (1<<2)) startupmsg[10] = 'B';
	if (mcusr & (1<<1)) startupmsg[11] = 'X';
	if (mcusr & (1<<0)) startupmsg[12] = 'P';
	lcd_line1(startupmsg);
	_delay_ms(500);
	buttons_ctor();
	alarm_ctor();
	timedisplay_ctor();
	timesetter_ctor();

	/* Drain the serial output just before the watchdog timer is
	   reenabled. */
	serial_drain();
	/* Turn off interrrupts until we have initialised the rest of the
	   hardware, and after QF_run() has properly initialised the active
	   objects.  Interrupts go back on in QF_onStartup() */
	QF_INT_LOCK();
	/* Initialize the BSP.  Enables the watchdog timer. */
	BSP_init();

	QF_run();

	goto startmain;
}
Example #2
0
static int buspirate_expect(struct programmer_t *pgm, char *send,
				char *expect, int wait_for_prompt)
{
	int got_it = 0;
	size_t expect_len = strlen(expect);
	char *rcvd;

	buspirate_send(pgm, send);
	while (1) {
		rcvd = buspirate_readline(pgm, NULL, 0);

		if (strncmp(rcvd, expect, expect_len) == 0) {
			if (! wait_for_prompt) {
				serial_drain(&pgm->fd, 0);
				return 1;
			} else {
				got_it = 1;
			}
		}

		if (buspirate_is_prompt(rcvd))
			break;
	}
	return got_it;
}
Example #3
0
static void
cliWriteConfig(void) {
    int16_t result;
    if (!HAS_FEATURE(PPSEN) && (cfg.flags & FLAG_PPSEN)) {
        cli_puts("WARNING: PPS output not available on this hardware\r\n");
        cfg.flags &= ~FLAG_PPSEN;
    }
    if ((cfg.flags & (FLAG_GPSEXT | FLAG_GPSOUT)) == (FLAG_GPSEXT | FLAG_GPSOUT)) {
        cli_puts("WARNING: gps_ext_in and gps_ext_out are mutually exclusive.\r\n");
        cfg.flags &= ~FLAG_GPSOUT;
    }
    /* Check for more than one ntpkey type */
    result = 0;
    if (cfg.flags & FLAG_NTPKEY_MD5) {
        result++;
    }
    if (cfg.flags & FLAG_NTPKEY_SHA1) {
        result++;
    }
    if (result > 1) {
        cli_puts("WARNING: More than one ntpkey type specified\r\n");
        cfg.flags &= ~(FLAG_NTPKEY_MD5 | FLAG_NTPKEY_SHA1);
    }
    cli_puts("Writing EEPROM...\r\n");
    result = eeprom_write_cfg();
    if (result == EERR_OK) {
        cli_puts("OK\r\n");
        serial_drain(cl_out);
        vTaskDelay(pdMS_TO_TICKS(1000));
        NVIC_SystemReset();
    } else {
        show_eeprom_error(result);
    }
}
Example #4
0
int birom16_call(struct birom16_state *state, uint16_t address) {
	uint8_t buffer[5];

	memset(buffer, 0x00, sizeof(buffer));

	buffer[0] = BIROM16_CMD_CALL;
	buffer[1] = (address & 0xFF000000) >> 24;
	buffer[2] = (address & 0x00FF0000) >> 16;
	buffer[3] = (address & 0x0000FF00) >> 8;
	buffer[4] = (address & 0x000000FF);

	serial_purge(state->serial);

	LOGD("Calling stage 2 at address 0x%04X.", address);

	unsigned int rc = serial_write(state->serial, buffer, sizeof(buffer));
	if (rc < sizeof(buffer)) {
		LOGE("Error writing to MCU.");
		return E_WRITE;
	}

	serial_drain(state->serial);

	//Expect single byte result of operation from MCU.
	rc = serial_read(state->serial, buffer, sizeof(buffer) - 1);
	if (buffer[0] != 0x31) {
		LOGE("Erroneous response from MCU.");
		return E_ERROR;
	}

	return E_NONE;
}
Example #5
0
int
serial_setup_console ()
{
  ProgramMode (0);
  Reset (1);
  /* >3 mSec reset pulse */
  usleep(3*1000);
  serial_drain ();
  Reset (0);
  FT_SetUSBParameters (handle, 64, 64);
  return 1;
}
Example #6
0
int birom16_write(struct birom16_state *state, uint16_t address, uint8_t *data, uint16_t size) {
	uint8_t buffer[4096];
	memset(buffer, 0x00, sizeof(buffer));

	serial_purge(state->serial);

	buffer[0] = BIROM16_CMD_WRITE;
	buffer[1] = (address & 0xFF00) >> 8;
	buffer[2] = (address & 0x00FF);
	buffer[3] = (size & 0xFF00) >> 8;
	buffer[4] = (size & 0x00FF);

	memcpy(buffer + 5, data, size);
	size += 5;

	uint8_t csum = checksum8(buffer, size);
	buffer[size++] = csum;

	LOGD("Writing %d bytes to address 0x%04X...", size, address);

	int rc = serial_write(state->serial, buffer, size);
	if (rc < size) {
		LOGE("Error writing to MCU.");
		return E_WRITE;
	}

	//Give MCU some breathing space.
	serial_drain(state->serial);

	//Expect single byte result of operation from MCU.
	uint8_t code = 0;
	uint8_t value = 0;
	double timeout = get_ticks() + 2;
	while (get_ticks() < timeout) {
		msleep(10);
		rc = serial_read(state->serial, &code, 1);
		if (rc < 0) {
			LOGE("Error reading from '%s'.", state->serial->address);
			return E_READ;
		}

		if (rc > 0) {
			if (code == BIROM16_RESP_ACK) {
				return E_NONE;
			}
		}
	}

	LOGE("Invalid MCU response: Code %d, value %d.", code, value);
	return E_MSGMALFORMED;
	//return E_NONE;
}
Example #7
0
/* ====== Programmer methods ======= */
static int buspirate_open(struct programmer_t *pgm, char * port)
{
	/* BusPirate runs at 115200 by default */
	if(pgm->baudrate == 0)
		pgm->baudrate = 115200;

	strcpy(pgm->port, port);
	serial_open(port, pgm->baudrate, &pgm->fd);

	/* drain any extraneous input */
	serial_drain(&pgm->fd, 0);

	return 0;
}
Example #8
0
int birom16_connect(struct birom16_state *state, uint8_t timeoutsec) {
	int rc;
	uint8_t buf[2];
	int tally = 0;

	serial_purge(state->serial);

	memset(&buf, 0x00, sizeof(buf));

	double timeout = get_ticks() + timeoutsec;
	while (get_ticks() < timeout) {
		msleep(100);

		buf[0] = BIROM16_CMD_PROBE;
		rc = serial_write(state->serial, buf, 1);
		if (rc < 1) {
			LOGE("Error writing to serial port! Aborting.");
			return rc;
		}

		serial_drain(state->serial);

		msleep(100);

		if ((tally % 10) == 0) {
			LOGI("Probing for BIROM %.0f...", ceil(timeout - get_ticks()));
		}
		++tally;

		memset(&buf, 0x00, sizeof(buf));
		while ((rc = serial_read(state->serial, buf, 1)) > 0) {
			if (buf[0] == BIROM16_RESP_PROBE) {
				return E_NONE;	//Returning zero to mean MCU is alive.
			}
		}

		if (rc < 0) {
			LOGE("Error reading from serial port! Aborting.");
			return rc;
		}

		if (buf[0] > 0 && buf[0] != BIROM16_RESP_PROBE) {
			LOGE("Malformed response from MCU (0x%X). Please power off board and try again.", buf[0]);
			return E_MSGMALFORMED;
		}
	}

	LOGE("ERROR - Time-out waiting for MCU.");
	return E_TIMEOUT;
}
Example #9
0
void QF_onStartup(void)
{
	Q_ASSERT( (SREG & (1<<7)) == 0 );

	Q_ASSERT(twi.ready);
	Q_ASSERT(timekeeper.ready);
	Q_ASSERT(buttons.ready);
	Q_ASSERT(alarm.ready);
	Q_ASSERT(timedisplay.ready);
	Q_ASSERT(timesetter.ready);

	serial_drain();

	BSP_QF_onStartup();
}
Example #10
0
void Comm::process_serial()
{
    if (! Serial.available()) {
        return;
    }

    byte addr = serial_get();
    byte pkt = serial_get();
    byte num = serial_get();

    bool okay = false;
    if (!addr) {                // handle by master
        if (m_handler) {
            okay = m_handler(num);
            serial_drain();
        }
        else {
            okay = drain(num);
        }
    }
    else {                      // send to slave
        okay = transmit(addr,num);
        serial_drain();
    }

    if (okay) {
        Serial.print("OK");
    }
    else {
        Serial.print("NO");
    }
    Serial.print(pkt);
    Serial.print(addr);
    Serial.print(num);
    Serial.print('\0');
}
Example #11
0
static void buspirate_enable(struct programmer_t *pgm)
{
	unsigned char *reset_str = "#\n";
	unsigned char *accept_str = "y\n";
	char *rcvd;
	int rc, print_banner = 0;

	/* Ensure configuration is self-consistant: */
	if (buspirate_verifyconfig(pgm)<0)
		exit(1);

	/* Attempt to start binary SPI mode unless explicitly told otherwise: */
	if (!buspirate_uses_ascii(pgm)) {
		fprintf(stderr, "Attempting to initiate BusPirate binary mode...\n");

		/* Send two CRs to ensure we're not in a sub-menu of the UI if we're in ASCII mode: */
		buspirate_send_bin(pgm, "\n\n", 2);

		/* Clear input buffer: */
		serial_drain(&pgm->fd, 0);

		/* Attempt to enter binary mode: */
		if (buspirate_start_spi_mode_bin(pgm) >= 0)
			return;
		else
			fprintf(stderr, "%s: Failed to start binary SPI mode, falling back to ASCII...\n", progname);
	}

	fprintf(stderr, "Attempting to initiate BusPirate ASCII mode...\n");

	/* Call buspirate_send_bin() instead of buspirate_send() 
	 * because we don't know if BP is in text or bin mode */
	rc = buspirate_send_bin(pgm, reset_str, strlen(reset_str));
	if (rc) {
		fprintf(stderr, "BusPirate is not responding. Serial port error: %d\n", rc);
		exit(1);
	}

	while(1) {
		rcvd = buspirate_readline_noexit(pgm, NULL, 0);
		if (! rcvd) {
			fprintf(stderr, "%s: Fatal: Programmer is not responding.\n", progname);
			exit(1);
		}
		if (strncmp(rcvd, "Are you sure?", 13) == 0) {
			buspirate_send_bin(pgm, accept_str, strlen(accept_str));
		}
		if (strncmp(rcvd, "RESET", 5) == 0) {
			print_banner = 1;
			continue;
		}
		if (buspirate_is_prompt(rcvd)) {
			puts("**");
			break;
		}
		if (print_banner)
			fprintf(stderr, "**  %s", rcvd);
	}

	if (!(pgm->flag & BP_FLAG_IN_BINMODE)) {
		fprintf(stderr, "BusPirate: using ASCII mode\n");
		if (buspirate_start_spi_mode_ascii(pgm) < 0) {
			fprintf(stderr, "%s: Failed to start ascii SPI mode\n", progname);
			exit(1);
		}
	}
}
Example #12
0
static int buspirate_start_spi_mode_bin(struct programmer_t *pgm)
{
	char buf[20] = { '\0' };

	/* == Switch to binmode - send 20x '\0' == */
	buspirate_send_bin(pgm, buf, sizeof(buf));

	/* Expecting 'BBIOx' reply */
	memset(buf, 0, sizeof(buf));
	buspirate_recv_bin(pgm, buf, 5);
	if (sscanf(buf, "BBIO%d", &PDATA(pgm)->binmode_version) != 1) {
		fprintf(stderr, "Binary mode not confirmed: '%s'\n", buf);
		buspirate_reset_from_binmode(pgm);
		return -1;
	}
	if (verbose)
		fprintf(stderr, "BusPirate binmode version: %d\n",
			PDATA(pgm)->binmode_version);

	pgm->flag |= BP_FLAG_IN_BINMODE;

	/* == Enter SPI mode == */
	buf[0] = 0x01;	/* Enter raw SPI mode */
	buspirate_send_bin(pgm, buf, 1);
	memset(buf, 0, sizeof(buf));
	buspirate_recv_bin(pgm, buf, 4);
	if (sscanf(buf, "SPI%d", &PDATA(pgm)->bin_spi_version) != 1) {
		fprintf(stderr, "SPI mode not confirmed: '%s'\n", buf);
		buspirate_reset_from_binmode(pgm);
		return -1;
	}
	if (verbose)
		fprintf(stderr, "BusPirate SPI version: %d\n",
			PDATA(pgm)->bin_spi_version);

	if (pgm->flag & BP_FLAG_NOPAGEDWRITE) {
		if (verbose)
			fprintf(stderr, "%s: Paged flash write disabled.\n", progname);
	} else {
		/* Check for write-then-read without !CS/CS and disable paged_write if absent: */
		strncpy(buf, "\x5\x0\x0\x0\x0", 5);
		buspirate_send_bin(pgm, buf, 5);
		buspirate_recv_bin(pgm, buf, 1);
		if (buf[0] != 0x01) {

			/* Disable paged write: */
			pgm->flag |= BP_FLAG_NOPAGEDWRITE;

			/* Return to SPI mode (0x00s have landed us back in binary bitbang mode): */
			buf[0] = 0x1;
			buspirate_send_bin(pgm, buf, 1);

			if (verbose)
				fprintf(stderr, "%s: Disabling paged flash write. (Need BusPirate firmware >=v5.10.)\n", progname);

			/* Flush serial buffer: */
			serial_drain(&pgm->fd, 0);
		} else {
			if (verbose)
				fprintf(stderr, "%s: Paged flash write enabled.\n", progname);
		}
	}

	/* 0b0100wxyz - Configure peripherals w=power, x=pull-ups/aux2, y=AUX, z=CS
	 * we want power (0x48) and all reset pins high. */
	PDATA(pgm)->current_peripherals_config  = 0x48 | PDATA(pgm)->reset;
	buspirate_expect_bin_byte(pgm, PDATA(pgm)->current_peripherals_config, 0x01);
	usleep(50000);	// sleep for 50ms after power up

	/* 01100xxx -  SPI speed
	 * xxx = 000=30kHz, 001=125kHz, 010=250kHz, 011=1MHz,
	 *       100=2MHz, 101=2.6MHz, 110=4MHz, 111=8MHz
	 * use 30kHz = 0x60 */
	buspirate_expect_bin_byte(pgm, 0x60 | PDATA(pgm)->spifreq, 0x01);

	/* 1000wxyz - SPI config, w=HiZ(0)/3.3v(1), x=CLK idle, y=CLK edge, z=SMP sample
	 * we want: 3.3V(1), idle low(0), data change on trailing edge (1),
	 *          sample in the middle of the pulse (0)
	 *       => 0b10001010 = 0x8a */
	buspirate_expect_bin_byte(pgm, 0x8A, 0x01);

	return 0;
}
Example #13
0
static device_status_t
suunto_d9_device_packet (device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size)
{
	suunto_d9_device_t *device = (suunto_d9_device_t *) abstract;

	if (device_is_cancelled (abstract))
		return DEVICE_STATUS_CANCELLED;

	// Clear RTS to send the command.
	serial_set_rts (device->port, 0);

	// Send the command to the dive computer.
	int n = serial_write (device->port, command, csize);
	if (n != csize) {
		WARNING ("Failed to send the command.");
		return EXITCODE (n);
	}

	// Wait until all data has been transmitted.
	serial_drain (device->port);

	// Receive the echo.
	unsigned char echo[128] = {0};
	assert (sizeof (echo) >= csize);
	n = serial_read (device->port, echo, csize);
	if (n != csize) {
		WARNING ("Failed to receive the echo.");
		return EXITCODE (n);
	}

	// Verify the echo.
	if (memcmp (command, echo, csize) != 0) {
		WARNING ("Unexpected echo.");
		return DEVICE_STATUS_PROTOCOL;
	}

	// Set RTS to receive the reply.
	serial_set_rts (device->port, 1);

	// Receive the answer of the dive computer.
	n = serial_read (device->port, answer, asize);
	if (n != asize) {
		WARNING ("Failed to receive the answer.");
		return EXITCODE (n);
	}

	// Verify the header of the package.
	if (answer[0] != command[0]) {
		WARNING ("Unexpected answer header.");
		return DEVICE_STATUS_PROTOCOL;
	}

	// Verify the size of the package.
	if (array_uint16_be (answer + 1) + 4 != asize) {
		WARNING ("Unexpected answer size.");
		return DEVICE_STATUS_PROTOCOL;
	}

	// Verify the parameters of the package.
	if (memcmp (command + 3, answer + 3, asize - size - 4) != 0) {
		WARNING ("Unexpected answer parameters.");
		return DEVICE_STATUS_PROTOCOL;
	}

	// Verify the checksum of the package.
	unsigned char crc = answer[asize - 1];
	unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00);
	if (crc != ccrc) {
		WARNING ("Unexpected answer CRC.");
		return DEVICE_STATUS_PROTOCOL;
	}

	return DEVICE_STATUS_SUCCESS;
}