//Retarget the _write function to USB/Serial int WRITEFUNC(int iFileHandle, char *pcBuffer, int iLength) { if (vcom_connected()) { return vcom_write((uint8_t *) pcBuffer,(uint32_t) iLength); } return -1; }
static void receive() { static uint8_t rxbuf[256]; int rx = rflib_bfsk_get_packet(rxbuf, 255); if(rx > 0) { rxbuf[rx] = '\0'; lcdPrintln((char*)rxbuf); rflib_lcdDisplay(); /* also: write to USB-CDC */ if(vcom_connected()) vcom_write((uint8_t*)rxbuf, rx); } }
// TODO(zapta): make the standard printf working for USB out. void printf(const char *format, ...) { if (vcom_connected()) { // Assuming single thread, using static buffer. static char buf[100]; va_list ap; va_start(ap, format); int n = vsnprintf(buf, sizeof(buf), (const char *) format, ap); vcom_write((uint8_t*) buf, n); va_end(ap); } }
void USBInit(SemaphoreHandle_t usb_uart_connected_sem) { USBD_API_INIT_PARAM_T usb_param; USB_CORE_DESCS_T desc; ErrorCode_t ret = LPC_OK; /* Initialize board and chip */ SystemCoreClockUpdate(); Board_Init(); StopWatch_Init(); /* enable clocks and pinmux */ usb_pin_clk_init(); /* initialize call back structures */ memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T)); usb_param.usb_reg_base = LPC_USB_BASE + 0x200; usb_param.max_num_ep = 3; usb_param.mem_base = USB_STACK_MEM_BASE; usb_param.mem_size = USB_STACK_MEM_SIZE; /* Set the USB descriptors */ desc.device_desc = (uint8_t *) &USB_DeviceDescriptor[0]; desc.string_desc = (uint8_t *) &USB_StringDescriptor[0]; /* Note, to pass USBCV test full-speed only devices should have both descriptor arrays point to same location and device_qualifier set to 0. */ desc.high_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.full_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.device_qualifier = 0; /* USB Initialization */ ret = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* Init VCOM interface */ ret = vcom_init(g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* enable USB interrupts */ NVIC_EnableIRQ(USB_IRQn); /* now connect */ USBD_API->hw->Connect(g_hUsb, 1); } } /* Check if host has connected and opened the VCOM port */ while(!vcom_connected()) ; xSemaphoreGive(usb_uart_connected_sem); }
//# MENU CDC void cdc_menu(){ uint32_t prompt = 0, rdCnt = 0; static uint8_t g_rxBuff[256]; CDCenable(); lcdPrintln("CDC enabled."); lcdDisplay(); getInputWaitRelease(); while(getInputRaw()!=BTN_ENTER){ if(getInputRaw()==BTN_RIGHT){ lcdPrint("status:"); lcdPrint(IntToStr(g_vCOM.tx_flags,3,F_HEX)); lcdPrint(", "); lcdPrint("c="); lcdPrint(IntToStr(prompt,1,F_LONG)); lcdPrintln("."); lcdDisplay(); getInputWaitRelease(); }; if(getInputRaw()==BTN_LEFT){ vcom_write((uint8_t *)"Hello World!\r\n", 14); getInputWaitRelease(); }; if ((vcom_connected() != 0) && (prompt == 0)) { prompt = 1; } /* If VCOM port is opened echo whatever we receive back to host. */ if (prompt) { rdCnt = vcom_bread(&g_rxBuff[0], 256); if (rdCnt) { vcom_write((uint8_t*)"[", 1); while(g_vCOM.tx_flags & VCOM_TX_BUSY) __WFI(); // Wait for buffer emtpy vcom_write(&g_rxBuff[0], rdCnt); while(g_vCOM.tx_flags & VCOM_TX_BUSY) __WFI(); // Wait for buffer emtpy vcom_write((uint8_t*)"]", 1); } } /* Sleep until next IRQ happens */ __WFI(); }; lcdPrintln("disconnect"); lcdDisplay(); CDCdisable(); getInputWaitRelease(); }
//# MENU BPSK void bfsk_menu() { lcdClear(); lcdPrintln("ENTER to go back"); lcdPrintln("L/R/U/D to xmit"); rflib_lcdDisplay(); getInputWaitRelease(); cpu_clock_set(204); CDCenable(); rflib_init(); rflib_bfsk_init(); rflib_set_freq(FREQSTART); rflib_bfsk_receive(); while(1) { switch (getInputRaw()) { case BTN_UP: transmit("up"); break; case BTN_DOWN: transmit("down"); break; case BTN_RIGHT: transmit("right"); break; case BTN_LEFT: transmit("left"); break; case BTN_ENTER: goto stop; } if(vcom_connected()) { /* check if we got data from USB-CDC, transmit it, if so. */ uint8_t sendbuf[255]; uint32_t read = vcom_bread(sendbuf, 255); if(read > 0) rflib_bfsk_transmit(sendbuf, read, true); } receive(); } stop: rflib_shutdown(); return; }
/** * @brief main routine for blinky example * @return Function should not exit. */ int main(void) { USBD_API_INIT_PARAM_T usb_param; USB_CORE_DESCS_T desc; ErrorCode_t ret = LPC_OK; uint32_t prompt = 0, rdCnt = 0; SystemCoreClockUpdate(); /* Initialize board and chip */ Board_Init(); /* enable clocks */ Chip_USB_Init(); /* initialize USBD ROM API pointer. */ g_pUsbApi = (const USBD_API_T *) LPC_ROM_API->pUSBD; /* initialize call back structures */ memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T)); usb_param.usb_reg_base = LPC_USB0_BASE; /* WORKAROUND for artf44835 ROM driver BUG: Code clearing STALL bits in endpoint reset routine corrupts memory area next to the endpoint control data. For example When EP0, EP1_IN, EP1_OUT, EP2_IN are used we need to specify 3 here. But as a workaround for this issue specify 4. So that extra EPs control structure acts as padding buffer to avoid data corruption. Corruption of padding memory doesn’t affect the stack/program behaviour. */ usb_param.max_num_ep = 3 + 1; usb_param.mem_base = USB_STACK_MEM_BASE; usb_param.mem_size = USB_STACK_MEM_SIZE; /* Set the USB descriptors */ desc.device_desc = (uint8_t *) &USB_DeviceDescriptor[0]; desc.string_desc = (uint8_t *) &USB_StringDescriptor[0]; /* Note, to pass USBCV test full-speed only devices should have both descriptor arrays point to same location and device_qualifier set to 0. */ desc.high_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.full_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.device_qualifier = 0; /* USB Initialization */ ret = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* Init VCOM interface */ ret = vcom_init(g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* enable USB interrupts */ NVIC_EnableIRQ(USB0_IRQn); /* now connect */ USBD_API->hw->Connect(g_hUsb, 1); } } DEBUGSTR("USB CDC class based virtual Comm port example!\r\n"); while (1) { /* Check if host has connected and opened the VCOM port */ if ((vcom_connected() != 0) && (prompt == 0)) { vcom_write("Hello World!!\r\n", 15); prompt = 1; } /* If VCOM port is opened echo whatever we receive back to host. */ if (prompt) { rdCnt = vcom_bread(&g_rxBuff[0], 256); if (rdCnt) { vcom_write(&g_rxBuff[0], rdCnt); } } /* Sleep until next IRQ happens */ __WFI(); } }
/** * @brief main routine for blinky example * @return Function should not exit. */ int main(void) { USBD_API_INIT_PARAM_T usb_param; USB_CORE_DESCS_T desc; ErrorCode_t ret = LPC_OK; uint32_t prompt = 0; SystemCoreClockUpdate(); /* Initialize board and chip */ Board_Init(); Board_ADC_Init(); /* Initialize PWM Units */ handle0 = Chip_PWM_Init(0, 18, 100); handle1 = Chip_PWM_Init(0, 13, 100); /* enable clocks and pinmux */ Chip_USB_Init(); /* initialize USBD ROM API pointer. */ g_pUsbApi = (const USBD_API_T *) LPC_ROM_API->usbdApiBase; /* initialize call back structures */ memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T)); usb_param.usb_reg_base = LPC_USB0_BASE; /* WORKAROUND for artf44835 ROM driver BUG: Code clearing STALL bits in endpoint reset routine corrupts memory area next to the endpoint control data. For example When EP0, EP1_IN, EP1_OUT, EP2_IN are used we need to specify 3 here. But as a workaround for this issue specify 4. So that extra EPs control structure acts as padding buffer to avoid data corruption. Corruption of padding memory doesn’t affect the stack/program behaviour. */ usb_param.max_num_ep = 3 + 1; usb_param.mem_base = USB_STACK_MEM_BASE; usb_param.mem_size = USB_STACK_MEM_SIZE; /* Set the USB descriptors */ desc.device_desc = (uint8_t *) &USB_DeviceDescriptor[0]; desc.string_desc = (uint8_t *) &USB_StringDescriptor[0]; /* Note, to pass USBCV test full-speed only devices should have both descriptor arrays point to same location and device_qualifier set to 0. */ desc.high_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.full_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.device_qualifier = 0; /* USB Initialization */ ret = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* WORKAROUND for artf32219 ROM driver BUG: The mem_base parameter part of USB_param structure returned by Init() routine is not accurate causing memory allocation issues for further components. */ usb_param.mem_base = USB_STACK_MEM_BASE + (USB_STACK_MEM_SIZE - usb_param.mem_size); /* Initialize ADC */ Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup); Chip_ADC_EnableChannel(_LPC_ADC_ID, ADC_CH0, ENABLE); Chip_ADC_Int_SetChannelCmd(_LPC_ADC_ID, ADC_CH0, ENABLE); NVIC_SetPriority(_LPC_ADC_IRQ, 1); NVIC_EnableIRQ(_LPC_ADC_IRQ); /* Init VCOM interface */ ret = vcom_init(g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* enable USB interrupts */ NVIC_SetPriority(USB0_IRQn, 1); NVIC_EnableIRQ(USB0_IRQn); /* now connect */ USBD_API->hw->Connect(g_hUsb, 1); } } DEBUGSTR("USB CDC class based virtual Comm port example!\r\n"); /* Start BURST Mode (Continuously Convert and Interrupt) */ Chip_ADC_SetBurstCmd(_LPC_ADC_ID, ENABLE); Chip_RIT_Init(LPC_RITIMER); Chip_RIT_SetTimerInterval(LPC_RITIMER, CONTROL_INTERVAL); NVIC_EnableIRQ(RIT_IRQn); int read_mode = WAITING_HEADER; while (1) { /* Check if host has connected and opened the VCOM port */ if ((vcom_connected() != 0) && (prompt == 0)) { //vcom_write("Hello World!!\r\n", 15); prompt = 1; } if (prompt) { unsigned char c; if (vcom_bread(&c, 1) != 0) { switch (read_mode) { case WAITING_HEADER: if (c == PACKET_HEADER) { g_buffCounter = 0; read_mode = WAITING_FOOTER; } break; case WAITING_FOOTER: if (c == PACKET_FOOTER) { onReceivePacket(); read_mode = WAITING_HEADER; } else { g_rxBuff[g_buffCounter] = c; g_buffCounter++; } break; default: break; } } } /* Sleep until next IRQ happens */ //__WFI(); } }