Beispiel #1
0
void receiver_therad(void *arg)
{
    chRegSetThreadName("USB receiver");
    struct io_dev_s *dev = (struct io_dev_s *)arg;

    static char response_buffer[100];
    service_call_handler.service_table = service_calls;
    service_call_handler.response_buffer = response_buffer;
    service_call_handler.response_buffer_sz = sizeof(response_buffer);
    service_call_handler.send_cb = io_dev_send;
    service_call_handler.send_cb_arg = dev;

    serial_datagram_rcv_handler_t rcv;
    static char datagram_buf[64];
    serial_datagram_rcv_handler_init(
        &rcv,
        datagram_buf,
        sizeof(datagram_buf),
        (serial_datagram_cb_t)msg_dispatcher,
        messages);

    while (1) {
        static uint8_t buf[32];
        int len = chnReadTimeout(dev->channel, buf, sizeof(buf), MS2ST(1));
        if (len == 0) {
            continue;
        }
        serial_datagram_receive(&rcv, buf, len);
    }
}
Beispiel #2
0
void PExReceive(void) {
  if (!AckPending) {
    unsigned char received;
    while (chnReadTimeout(&BDU1, &received, 1, TIME_IMMEDIATE)) {
      PExReceiveByte(received);
    }
  }
}
Beispiel #3
0
int sr5ReadDataTimeout(ts_channel_s *tsChannel, uint8_t * buffer, int size, int timeout) {
#if TS_UART_DMA_MODE || defined(__DOXYGEN__)
	UNUSED(tsChannel);
	return (int)iqReadTimeout(&tsUartDma.fifoRxQueue, (uint8_t * )buffer, (size_t)size, timeout);
#else /* TS_UART_DMA_MODE */
	if (tsChannel->channel == NULL)
		return 0;
	return chnReadTimeout(tsChannel->channel, (uint8_t * )buffer, size, timeout);
#endif /* TS_UART_DMA_MODE */
}
Beispiel #4
0
void virtser_task(void) {
  uint8_t numBytesReceived = 0;
  uint8_t buffer[16];
  do {
    numBytesReceived = chnReadTimeout(&drivers.serial_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
    for (int i=0;i<numBytesReceived;i++) {
      virtser_recv(buffer[i]);
    }
  } while (numBytesReceived > 0);
}
Beispiel #5
0
void raw_hid_task(void) {
  uint8_t buffer[RAW_EPSIZE];
  size_t size = 0;
  do {
    size_t size = chnReadTimeout(&drivers.raw_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
    if (size > 0) {
        raw_hid_receive(buffer, size);
    }
  } while(size > 0);
}
Beispiel #6
0
void console_task(void) {
  uint8_t buffer[CONSOLE_EPSIZE];
  size_t size = 0;
  do {
    size_t size = chnReadTimeout(&drivers.console_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
    if (size > 0) {
        console_receive(buffer, size);
    }
  } while(size > 0);
}
Beispiel #7
0
    static msg_t ThreadUSBDMidi(void *arg) {
  (void)arg;
#if CH_USE_REGISTRY
  chRegSetThreadName("usbdmidi");
#endif
  uint8_t r[4];
  while (1) {
    chnReadTimeout(&MDU1, &r[0], 4, TIME_INFINITE);
    MidiInMsgHandler(MIDI_DEVICE_USB_DEVICE, ((r[0] & 0xF0) >> 4) + 1, r[1],
                     r[2], r[3]);
  }
}
Beispiel #8
0
void bbio_spi_sniff(t_hydra_console *con)
{
	uint8_t cs_state, data, rx_data[2];
	mode_config_proto_t* proto = &con->mode->proto;
	bsp_status_t status;

	proto->dev_mode = DEV_SPI_SLAVE;
	status = bsp_spi_init(proto->dev_num, proto);
	status = bsp_spi_init(proto->dev_num+1, proto);

	if(status == BSP_OK) {
		cprint(con, "\x01", 1);
	} else {
		cprint(con, "\x00", 1);
		proto->dev_mode = DEV_SPI_MASTER;
		status = bsp_spi_init(proto->dev_num, proto);
		status = bsp_spi_deinit(proto->dev_num+1);
		return;
	}
	cs_state = 1;
	while(!USER_BUTTON || chnReadTimeout(con->sdu, &data, 1,1)) {
		if (cs_state == 0 && bsp_spi_get_cs(proto->dev_num)) {
			cprint(con, "]", 1);
			cs_state = 1;
		} else if (cs_state == 1 && !(bsp_spi_get_cs(proto->dev_num))) {
			cprint(con, "[", 1);
			cs_state = 0;
		}
		if(bsp_spi_rxne(proto->dev_num)){
			bsp_spi_read_u8(proto->dev_num,	&rx_data[0], 1);

			if(bsp_spi_rxne(proto->dev_num+1)){
				bsp_spi_read_u8(proto->dev_num+1, &rx_data[1], 1);
			} else {
				rx_data[1] = 0;
			}

			cprintf(con, "\\%c%c", rx_data[0], rx_data[1]);
		}
	}
	proto->dev_mode = DEV_SPI_MASTER;
	status = bsp_spi_init(proto->dev_num, proto);
	status = bsp_spi_deinit(proto->dev_num+1);
}
msg_t bridge_thread (void *arg)
{
t_hydra_console *con;
con = arg;
chRegSetThreadName("UART bridge");
chThdSleepMilliseconds(10);
uint8_t rx_data;
uint8_t tx_data;
mode_config_proto_t* proto = &con->mode->proto;

    while (!USER_BUTTON) {
        if(bsp_uart_rxne(proto->dev_num)) {
            bsp_uart_read_u8(proto->dev_num, &rx_data ,1);
            chSequentialStreamWrite(con->sdu, &rx_data, 1);
        }
        if(chnReadTimeout(con->sdu, &tx_data, 1, 1)) {
            bsp_uart_write_u8(proto->dev_num, &tx_data, 1);
            //chSequentialStreamWrite(con->sdu, &tx_data, 1);
        }
    }
    return (msg_t)1;
}
Beispiel #10
0
static void bridge(t_hydra_console *con)
{
	uint8_t tx_data[UART_BRIDGE_BUFF_SIZE];
	uint8_t bytes_read;
	//uint8_t bytes_read;
	mode_config_proto_t* proto = &con->mode->proto;

	cprintf(con, "Interrupt by pressing user button.\r\n");
	cprint(con, "\r\n", 2);

	thread_t *bthread = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, "bridge_thread",
						LOWPRIO, bridge_thread, con);
	while(!USER_BUTTON) {
		bytes_read = chnReadTimeout(con->sdu, tx_data,
					    UART_BRIDGE_BUFF_SIZE, US2ST(100));
		if(bytes_read > 0) {
			bsp_uart_write_u8(proto->dev_num, tx_data, bytes_read);
		}
	}
	chThdTerminate(bthread);
	chThdWait(bthread);
}
Beispiel #11
0
bool recv_midi_packet(MIDI_EventPacket_t* const event) {
  size_t size = chnReadTimeout(&drivers.midi_driver.driver, (uint8_t*)event, sizeof(MIDI_EventPacket_t), TIME_IMMEDIATE);
  return size == sizeof(MIDI_EventPacket_t);
}
Beispiel #12
0
static msg_t receiverThread(void *arg) {
  int i;
  UNUSED (arg);
  chRegSetThreadName("receiver");
  
  while (TRUE) {
    chMtxLock(&nrfMutex);
    size_t s = chnReadTimeout(&nrf24l01.channels[0], serialInBuf, 32, MS2ST(10));
    chMtxUnlock(&nrfMutex);

    if (s) {
      if(!wait_for_ack){
	//	serialOutBuf[0]= serialInBuf[0];
	serialInBuf[8]= 1;

	for(i=0; i<5; i++){
	  target_addr[i] = serialInBuf[2+i];
	} 
	nrf24l01SetTXAddress(&nrf24l01, target_addr);
	chMtxLock(&nrfMutex);
	chnWriteTimeout(&nrf24l01.channels[0], serialInBuf, 32, MS2ST(100));
	chMtxUnlock(&nrfMutex);

	if(wait_for_ack){
	  break;
	}
    
	for(i=0;i<5; i++){
	  rec_list[iter][i] = serialInBuf[2+i];
	  } 
	rec_list[iter][5] = serialInBuf[10];
	
	for(i=0; i<(serialInBuf[10]);i++){
	  rec_list[iter][6+i] = serialInBuf[12+i];
	}
	
	iter = (iter+1)%10;
	
	
	for (i=0;i<(int)s;i++) {
	  chprintf((BaseSequentialStream*)&SD1, "%c ", serialInBuf[i]);
	}
	chprintf((BaseSequentialStream*)&SD1, "\n\r", s);
    
	
      }


      if(wait_for_ack){
	if((serialInBuf[0] = msg_seq) && (serialInBuf[8]==1)){
	  wait_for_ack = 0;
	  chprintf((BaseSequentialStream*)&SD1, "Acknowledged.\n\r");
	  
	}
	//else keep trying to send. 
      }
    
    }
    chSchDoYieldS();
  }
  return 0;
}
Beispiel #13
0
/**
 * @return true if legacy command was processed, false otherwise
 */
bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) {
	if (command == TS_HELLO_COMMAND || command == TS_HELLO_COMMAND_DEPRECATED) {
		scheduleMsg(&tsLogger, "Got naked Query command");
		handleQueryCommand(tsChannel, TS_PLAIN);
		return true;
	} else if (command == 't' || command == 'T') {
		handleTestCommand(tsChannel);
		return true;
	} else if (command == TS_PAGE_COMMAND) {
		int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&pageIn, sizeof(pageIn), TS_READ_TIMEOUT);
		if (recieved != sizeof(pageIn)) {
			tunerStudioError("ERROR: not enough for PAGE");
			return true;
		}
		handlePageSelectCommand(tsChannel, TS_PLAIN, pageIn);
		return true;
	} else if (command == TS_BURN_COMMAND) {
		scheduleMsg(&tsLogger, "Got naked BURN");
		uint16_t page;
		int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&page, sizeof(page), TS_READ_TIMEOUT);
		if (recieved != sizeof(page)) {
			tunerStudioError("ERROR: Not enough for plain burn");
			return true;
		}
		handleBurnCommand(tsChannel, TS_PLAIN, page);
		return true;
	} else if (command == TS_CHUNK_WRITE_COMMAND) {
		scheduleMsg(&tsLogger, "Got naked CHUNK_WRITE");
		int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&writeChunkRequest, sizeof(writeChunkRequest),
				TS_READ_TIMEOUT);
		if (recieved != sizeof(writeChunkRequest)) {
			scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write header: %d", recieved);
			tsState.errorCounter++;
			return true;
		}
		recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&tsChannel->crcReadBuffer, writeChunkRequest.count,
				TS_READ_TIMEOUT);
		if (recieved != writeChunkRequest.count) {
			scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write content: %d while expecting %d", recieved,
					writeChunkRequest.count);
			tsState.errorCounter++;
			return true;
		}
		currentPageId = writeChunkRequest.page;

		handleWriteChunkCommand(tsChannel, TS_PLAIN, writeChunkRequest.offset, writeChunkRequest.count,
				(uint8_t *) &tsChannel->crcReadBuffer);
		return true;
	} else if (command == TS_READ_COMMAND) {
		//scheduleMsg(logger, "Got naked READ PAGE???");
		int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&readRequest, sizeof(readRequest),
				TS_READ_TIMEOUT);
		if (recieved != sizeof(readRequest)) {
			tunerStudioError("Not enough for plain read header");
			return true;
		}
		handlePageReadCommand(tsChannel, TS_PLAIN, readRequest.page, readRequest.offset, readRequest.count);
		return true;
	} else if (command == TS_OUTPUT_COMMAND) {
		//scheduleMsg(logger, "Got naked Channels???");
		handleOutputChannelsCommand(tsChannel, TS_PLAIN);
		return true;
	} else if (command == TS_LEGACY_HELLO_COMMAND) {
		tunerStudioDebug("ignoring LEGACY_HELLO_COMMAND");
		return true;
	} else if (command == TS_COMMAND_F) {
		tunerStudioDebug("not ignoring F");
		tunerStudioWriteData(tsChannel, (const uint8_t *) PROTOCOL, strlen(PROTOCOL));
		return true;
	} else {
		return false;
	}
}
Beispiel #14
0
void runBinaryProtocolLoop(ts_channel_s *tsChannel, bool_t isConsoleRedirect) {
	int wasReady = false;
	while (true) {
		int isReady = ts_serial_ready(isConsoleRedirect);
		if (!isReady) {
			chThdSleepMilliseconds(10);
			wasReady = false;
			continue;
		}

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

		tsState.tsCounter++;

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

		recieved = chnReadTimeout(tsChannel->channel, &secondByte, 1, TS_READ_TIMEOUT);
		if (recieved != 1) {
			tunerStudioError("ERROR: no second");
			continue;
		}
//		scheduleMsg(logger, "Got secondByte=%x=[%c]", secondByte, secondByte);

		uint32_t incomingPacketSize = firstByte * 256 + secondByte;

		if (incomingPacketSize == BINARY_SWITCH_TAG) {
			// we are here if we get a binary switch request while already in binary mode. We will just ignore it.
			tunerStudioWriteData(tsChannel, (const uint8_t *) &BINARY_RESPONSE, 2);
			continue;
		}

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

		recieved = chnReadTimeout(tsChannel->channel, (uint8_t* )tsChannel->crcReadBuffer, 1, TS_READ_TIMEOUT);
		if (recieved != 1) {
			tunerStudioError("ERROR: did not receive command");
			continue;
		}

		char command = tsChannel->crcReadBuffer[0];
		if (!isKnownCommand(command)) {
			scheduleMsg(&tsLogger, "unexpected command %x", command);
			sendErrorCode(tsChannel);
			continue;
		}

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

		recieved = chnReadTimeout(tsChannel->channel, (uint8_t * ) (tsChannel->crcReadBuffer + 1),
				incomingPacketSize + CRC_VALUE_SIZE - 1, TS_READ_TIMEOUT);
		int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1;
		if (recieved != expectedSize) {
			scheduleMsg(&tsLogger, "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*) (tsChannel->crcReadBuffer + incomingPacketSize);

		expectedCrc = SWAP_UINT32(expectedCrc);

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

			scheduleMsg(&tsLogger, "TunerStudio: command %c actual CRC %x/expected %x", tsChannel->crcReadBuffer[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(tsChannel, tsChannel->crcReadBuffer, incomingPacketSize);
		if (!success)
			print("got unexpected TunerStudio command %x:%c\r\n", command, command);

	}
}
Beispiel #15
0
CCM_FUNC static THD_FUNCTION(ThreadSDU, arg)
{
  (void)arg;
  uint8_t in_buffer[SERIAL_BUFFERS_SIZE];
  uint8_t out_buffer[SERIAL_BUFFERS_SIZE];
  //uint8_t buffer_check[SERIAL_BUFFERS_SIZE/2];
  size_t in, out;
  thread_t* th_shell = NULL;
  chRegSetThreadName("SDU");

  while(USBD1.state != USB_READY) chThdSleepMilliseconds(10);
  while(SDU1.state != SDU_READY) chThdSleepMilliseconds(10);
  while(SD3.state != SD_READY) chThdSleepMilliseconds(10);

  // Enable K-line transceiver
  palSetPad(PORT_KLINE_CS, PAD_KLINE_CS);

  shellInit();

  while (TRUE) {

    if (settings.serialMode == SERIAL_MODE_SHELL)  {
        if (th_shell == NULL || chThdTerminatedX(th_shell)) {
            th_shell = shellCreateStatic(&shell_cfg1, waThreadShell, sizeof(waThreadShell), NORMALPRIO +1);
        }
        chThdSleepMilliseconds(10);
        continue;
    }

    if (settings.serialMode != SERIAL_MODE_KLINE) {
        chThdSleepMilliseconds(10);
        continue;
    }

    /* In case we stop it to change baudrate */
    while (SD3.state != SD_READY) chThdSleepMilliseconds(10);

    if (doKLineInit && 0)
    {
      sdStop(&SD3);
      klineInit(false);
      //fiveBaudInit(&SD3);
      //sdReadTimeout(&SD3, buffer_check, 1, MS2ST(5)); // noise
      doKLineInit = false;
      sdStart(&SD3, &uart1Cfg);
    }

    in = chnReadTimeout(&SDU1, in_buffer, sizeof(in_buffer), TIME_IMMEDIATE);
    out = sdReadTimeout(&SD3, out_buffer, sizeof(out_buffer), TIME_IMMEDIATE);

    while (in == 0 && out == 0) {

        chThdSleepMilliseconds(1);
        in = chnReadTimeout(&SDU1, in_buffer, sizeof(in_buffer), TIME_IMMEDIATE);
        out = sdReadTimeout(&SD3, out_buffer, sizeof(out_buffer), TIME_IMMEDIATE);
    }

    if (in > 0)
    {
      sdWriteTimeout(&SD3, in_buffer, in, MS2ST(10));
    }

    if (out > 0)
    {
      chnWriteTimeout(&SDU1, out_buffer, out, MS2ST(10));
    }

  }
  return;
}
Beispiel #16
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
}