pico-mdebug

mdebug firmware for pico / rp2040
git clone http://frotz.net/git/pico-mdebug.git
Log | Files | Refs | README

main.c (2108B)


      1 // Copyright 2021, Brian Swetland <swetland@frotz.net>
      2 // Licensed under the Apache License, Version 2.0.
      3 
      4 #include <stdio.h>
      5 #include <stdint.h>
      6 
      7 #include <pico/stdlib.h>
      8 #include <tusb.h>
      9 
     10 void rswd_init();
     11 uint32_t rswd_txn(uint32_t* rx, uint32_t rxc, uint32_t* tx);
     12 
     13 uint32_t rxbuf[8192 / 4];
     14 uint32_t txbuf[8192 / 4];
     15 
     16 uint32_t rxcount = 0;
     17 
     18 void mdebug_task(void) {
     19 	if (!tud_vendor_available()) {
     20 		return;
     21 	}
     22 
     23 	unsigned count = tud_vendor_read(rxbuf + rxcount, 64);
     24 	if (count == 0) {
     25 		return;
     26 	}
     27 	//printf("USB RX %u (%u)\n", count, rxcount);
     28 
     29 	if (count & 3) {
     30 		// bogus packet
     31 		rxcount = 0;
     32 		return;
     33 	}
     34 
     35 	rxcount += (count / 4);
     36 
     37 	if (count < 64) {
     38 		// short packet: end of txn
     39 		count = rswd_txn(rxbuf, rxcount, txbuf);
     40 		if (count) {
     41 			tud_vendor_write(txbuf, count * 4);
     42 		}
     43 		rxcount = 0;
     44 		return;
     45 	}
     46 
     47 	if (rxcount == (8192 / 4)) {
     48 		// overflow
     49 		rxcount = 0;
     50 		return;
     51 	}
     52 }
     53 
     54 int main() {
     55 	board_init();
     56 	setup_default_uart();
     57 	printf("Hello, world!\n");
     58 	rswd_init();
     59 	tusb_init();
     60 
     61 	for (;;) {
     62 		tud_task();
     63 		mdebug_task();
     64 	}
     65 
     66 	return 0;
     67 }
     68 
     69 
     70 static const tusb_desc_device_t dev_desc = {
     71 	.bLength = sizeof(tusb_desc_device_t),
     72 	.bDescriptorType = TUSB_DESC_DEVICE,
     73 	.bcdUSB = 0x0110,
     74 	.bDeviceClass = 0x00,
     75 	.bDeviceSubClass = 0x00,
     76 	.bDeviceProtocol = 0x00,
     77 	.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
     78 	.idVendor = 0x1209,
     79 	.idProduct = 0x5038,
     80 	.bcdDevice = 0x0100,
     81 	.iManufacturer = 0,
     82 	.iProduct = 0,
     83 	.iSerialNumber = 0,
     84 	.bNumConfigurations = 1,
     85 };
     86 
     87 const uint8_t* tud_descriptor_device_cb(void) {
     88 	return (const uint8_t*) &dev_desc;
     89 }
     90 
     91 #define IFC_MDEBUG 0
     92 #define IFC_TOTAL 1
     93 
     94 #define EP_MDEBUG_IN 0x81
     95 #define EP_MDEBUG_OUT 0x01
     96 
     97 #define CFG_LEN (TUD_CONFIG_DESC_LEN + TUD_VENDOR_DESC_LEN)
     98 
     99 static const uint8_t cfg_desc[] = {
    100 	TUD_CONFIG_DESCRIPTOR(1, IFC_TOTAL, 0, CFG_LEN,
    101 		TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
    102 	TUD_VENDOR_DESCRIPTOR(IFC_MDEBUG, 0, EP_MDEBUG_OUT, EP_MDEBUG_IN, 64),
    103 };
    104 
    105 const uint8_t* tud_descriptor_configuration_cb(uint8_t index) {
    106 	return cfg_desc;
    107 }
    108 
    109 const uint16_t* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
    110 	return NULL;
    111 }