Esempio n. 1
0
static bool handlePlainCommand(uint8_t command) {
	if (command == TS_HELLO_COMMAND) {
		scheduleMsg(&logger, "Got naked Query command");
		handleQueryCommand(TS_PLAIN);
		return true;
	} else if (command == 't' || command == 'T') {
		handleTestCommand();
		return true;
	} else if (command == TS_PAGE_COMMAND) {
		int recieved = chSequentialStreamRead(getTsSerialDevice(), (uint8_t *)&pageIn, sizeof(pageIn));
		// todo: validate 'recieved' value
		handlePageSelectCommand(TS_PLAIN, pageIn);
		return true;
	} else if (command == TS_READ_COMMAND) {
		//scheduleMsg(&logger, "Got naked READ PAGE???");
		int recieved = chSequentialStreamRead(getTsSerialDevice(), (uint8_t *)&readRequest, sizeof(readRequest));
		if (recieved != sizeof(readRequest)) {
			// todo: handler error
			return true;
		}
		handlePageReadCommand(TS_PLAIN, readRequest.page, readRequest.offset, readRequest.count);
		return true;
	} else if (command == TS_OUTPUT_COMMAND) {
		//scheduleMsg(&logger, "Got naked Channels???");
		handleOutputChannelsCommand(TS_PLAIN);
		return true;
	} else if (command == 'F') {
		tunerStudioDebug("not ignoring F");
		tunerStudioWriteData((const uint8_t *) PROTOCOL, strlen(PROTOCOL));
		return true;
	} else {
		return false;
	}
}
Esempio n. 2
0
static THD_FUNCTION(serial_read_thread, arg) {
	(void)arg;

	chRegSetThreadName("USB-Serial read");

	uint8_t buffer[128];
	int i;
	int len;
	int had_data = 0;

	for(;;) {
		len = chSequentialStreamRead(&SDU1, (uint8_t*) buffer, 1);

		for (i = 0;i < len;i++) {
			serial_rx_buffer[serial_rx_write_pos++] = buffer[i];

			if (serial_rx_write_pos == SERIAL_RX_BUFFER_SIZE) {
				serial_rx_write_pos = 0;
			}

			had_data = 1;
		}

		if (had_data) {
			chEvtSignal(process_tp, (eventmask_t) 1);
			had_data = 0;
		}
	}
}
Esempio n. 3
0
/**
 * @brief   Reads a whole line from the input channel.
 *
 * @param[in] chp       pointer to a @p BaseSequentialStream object
 * @param[in] line      pointer to the line buffer
 * @param[in] size      buffer maximum length
 * @return              The operation status.
 * @retval true         the channel was reset or CTRL-D pressed.
 * @retval false        operation successful.
 *
 * @api
 */
bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size) {
  char *p = line;

  while (true) {
    char c;

    if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
      return true;
    if (c == 4) {
      chprintf(chp, "^D");
      return true;
    }
    if ((c == 8) || (c == 127)) {
      if (p != line) {
        chSequentialStreamPut(chp, c);
        chSequentialStreamPut(chp, 0x20);
        chSequentialStreamPut(chp, c);
        p--;
      }
      continue;
    }
    if (c == '\r') {
      chprintf(chp, "\r\n");
      *p = 0;
      return false;
    }
    if (c < 0x20)
      continue;
    if (p < line + size - 1) {
      chSequentialStreamPut(chp, c);
      *p++ = (char)c;
    }
  }
}
Esempio n. 4
0
static msg_t Thread1(void *arg) {
  (void)arg;
  chRegSetThreadName("blinker");

  BaseSequentialStream *usb_cdc = (BaseSequentialStream *)&SDU1;
  char c;

  while (TRUE) {
    if (chSequentialStreamRead(usb_cdc, (uint8_t *)&c, 1) != 0) {
      chprintf(usb_cdc, "You entered hex char 0x%X\r\n", c);
    }
  }
  return (0);
}
Esempio n. 5
0
void bbio_mode_spi(t_hydra_console *con)
{
	uint8_t bbio_subcommand;
	uint16_t to_rx, to_tx, i;
	uint8_t *tx_data = (uint8_t *)g_sbuf;
	uint8_t *rx_data = (uint8_t *)g_sbuf+4096;
	uint8_t data;
	bsp_status_t status;
	mode_config_proto_t* proto = &con->mode->proto;

	bbio_spi_init_proto_default(con);
	bsp_spi_init(proto->dev_num, proto);

	while (!USER_BUTTON) {
		if(chSequentialStreamRead(con->sdu, &bbio_subcommand, 1) == 1) {
			switch(bbio_subcommand) {
			case BBIO_RESET:
				bsp_spi_deinit(proto->dev_num);
				return;
			case BBIO_SPI_CS_LOW:
				bsp_spi_select(proto->dev_num);
				cprint(con, "\x01", 1);
				break;
			case BBIO_SPI_CS_HIGH:
				bsp_spi_unselect(proto->dev_num);
				cprint(con, "\x01", 1);
				break;
			case BBIO_SPI_SNIFF_ALL:
			case BBIO_SPI_SNIFF_CS_LOW:
			case BBIO_SPI_SNIFF_CS_HIGH:
				bbio_spi_sniff(con);
				break;
			case BBIO_SPI_WRITE_READ:
			case BBIO_SPI_WRITE_READ_NCS:
				chSequentialStreamRead(con->sdu, rx_data, 4);
				to_tx = (rx_data[0] << 8) + rx_data[1];
				to_rx = (rx_data[2] << 8) + rx_data[3];
				if ((to_tx > 4096) || (to_rx > 4096)) {
					cprint(con, "\x00", 1);
					break;
				}
				chSequentialStreamRead(con->sdu, tx_data, to_tx);

				if(bbio_subcommand == BBIO_SPI_WRITE_READ) {
					bsp_spi_select(proto->dev_num);
				}
				bsp_spi_write_u8(proto->dev_num, tx_data,
				                 to_tx);
				i=0;
				while(i<to_rx) {
					if((to_rx-i) >= 255) {
						bsp_spi_read_u8(proto->dev_num,
						                rx_data+i,
						                255);
					} else {
						bsp_spi_read_u8(proto->dev_num,
						                rx_data+i,
						                to_rx-i);
					}
					i+=255;
				}
				if(bbio_subcommand == BBIO_SPI_WRITE_READ) {
					bsp_spi_unselect(proto->dev_num);
				}
				i=0;
				cprint(con, "\x01", 1);
				while(i < to_rx) {
					cprintf(con, "%c", rx_data[i]);
					i++;
				}
				break;
			default:
				if ((bbio_subcommand & BBIO_SPI_BULK_TRANSFER) == BBIO_SPI_BULK_TRANSFER) {
					// data contains the number of bytes to
					// write
					data = (bbio_subcommand & 0b1111) + 1;

					chSequentialStreamRead(con->sdu, tx_data, data);
					bsp_spi_write_read_u8(proto->dev_num,
					                      tx_data,
					                      rx_data,
					                      data);
					cprint(con, "\x01", 1);
					i=0;
					while(i < data) {
						cprintf(con, "%c", rx_data[i]);
						i++;
					}
				} else if ((bbio_subcommand & BBIO_SPI_SET_SPEED) == BBIO_SPI_SET_SPEED) {
					proto->dev_speed = bbio_subcommand & 0b111;
					status = bsp_spi_init(proto->dev_num, proto);
					if(status == BSP_OK) {
						cprint(con, "\x01", 1);
					} else {
						cprint(con, "\x00", 1);
					}
				} else if ((bbio_subcommand & BBIO_SPI_CONFIG) == BBIO_SPI_CONFIG) {
					proto->dev_polarity = (bbio_subcommand & 0b100)?1:0;
					proto->dev_phase = (bbio_subcommand & 0b10)?1:0;
					status = bsp_spi_init(proto->dev_num, proto);
					if(status == BSP_OK) {
						cprint(con, "\x01", 1);
					} else {
						cprint(con, "\x00", 1);
					}
				} else if ((bbio_subcommand & BBIO_SPI_CONFIG_PERIPH) == BBIO_SPI_CONFIG_PERIPH) {
					cprint(con, "\x01", 1);
				}

			}
		}
	}
}
Esempio n. 6
0
	static size_t ImageBaseFileStreamRead(struct gdispImageIO *pio, void *buf, size_t len) {
		if (pio->fd == (void *)-1) return 0;
		len = chSequentialStreamRead(((BaseFileStream *)pio->fd), (uint8_t *)buf, len);
		pio->pos += len;
		return len;
	}
static size_t chstream_read(void *ip, void *data, size_t size) {
	return chSequentialStreamRead((BaseSequentialStream *) ip, data, size) ;
}
Esempio n. 8
0
static msg_t tsThreadEntryPoint(void *arg) {
	(void) arg;
	chRegSetThreadName("tunerstudio thread");

	int wasReady = false;
	while (true) {
		int isReady = ts_serail_ready();
		if (!isReady) {
			chThdSleepMilliseconds(10);
			wasReady = false;
			continue;
		}

		if (!wasReady) {
			wasReady = TRUE;
//			scheduleSimpleMsg(&logger, "ts channel is now ready ", hTimeNow());
		}

		tsCounter++;

		int recieved = chSequentialStreamRead(getTsSerialDevice(), &firstByte, 1);
		if (recieved != 1) {
			tunerStudioError("ERROR: no command");
			continue;
		}
//		scheduleMsg(&logger, "Got first=%x=[%c]", firstByte, firstByte);
		if (handlePlainCommand(firstByte))
			continue;

		recieved = chSequentialStreamRead(getTsSerialDevice(), &secondByte, 1);
		if (recieved != 1) {
			tunerStudioError("ERROR: no second");
			continue;
		}
//		scheduleMsg(&logger, "Got secondByte=%x=[%c]", secondByte, secondByte);

		uint32_t incomingPacketSize = firstByte * 256 + secondByte;

		if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(crcIoBuffer) - CRC_WRAPPING_SIZE)) {
			scheduleMsg(&logger, "TunerStudio: invalid size: %d", incomingPacketSize);
			tunerStudioError("ERROR: size");
			sendErrorCode();
			continue;
		}

		recieved = chnReadTimeout(getTsSerialDevice(), crcIoBuffer, 1, MS2ST(TS_READ_TIMEOUT));
		if (recieved != 1) {
			tunerStudioError("ERROR: did not receive command");
			continue;
		}

		char command = crcIoBuffer[0];
		if (!isKnownCommand(command)) {
			scheduleMsg(&logger, "unexpected command %x", command);
			sendErrorCode();
			continue;
		}

//		scheduleMsg(&logger, "TunerStudio: reading %d+4 bytes(s)", incomingPacketSize);

		recieved = chnReadTimeout(getTsSerialDevice(), (uint8_t * ) (crcIoBuffer + 1), incomingPacketSize + CRC_VALUE_SIZE - 1,
				MS2ST(TS_READ_TIMEOUT));
		int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1;
		if (recieved != expectedSize) {
			scheduleMsg(&logger, "got ONLY %d for packet size %d/%d for command %c", recieved, incomingPacketSize,
					expectedSize, command);
			tunerStudioError("ERROR: not enough");
			continue;
		}

		uint32_t expectedCrc = *(uint32_t*) (crcIoBuffer + incomingPacketSize);

		expectedCrc = SWAP_UINT32(expectedCrc);

		uint32_t actualCrc = crc32(crcIoBuffer, incomingPacketSize);
		if (actualCrc != expectedCrc) {
			scheduleMsg(&logger, "TunerStudio: CRC %x %x %x %x", crcIoBuffer[incomingPacketSize + 0],
					crcIoBuffer[incomingPacketSize + 1], crcIoBuffer[incomingPacketSize + 2],
					crcIoBuffer[incomingPacketSize + 3]);

			scheduleMsg(&logger, "TunerStudio: command %c actual CRC %x/expected %x", crcIoBuffer[0], actualCrc,
					expectedCrc);
			tunerStudioError("ERROR: CRC issue");
			continue;
		}

//		scheduleMsg(&logger, "TunerStudio: P00-07 %x %x %x %x %x %x %x %x", crcIoBuffer[0], crcIoBuffer[1],
//				crcIoBuffer[2], crcIoBuffer[3], crcIoBuffer[4], crcIoBuffer[5], crcIoBuffer[6], crcIoBuffer[7]);

		int success = tunerStudioHandleCrcCommand(crcIoBuffer, incomingPacketSize);
		if (!success)
			print("got unexpected TunerStudio command %x:%c\r\n", command, command);

	}
#if defined __GNUC__
	return 0;
#endif
}