Beispiel #1
0
/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
int main (void)
{
  USBD_API_INIT_PARAM_T usb_param;
	ErrorCode_t ret;
  USB_CORE_DESCS_T desc;

  SystemCoreClockUpdate();

  /* Setup UART for 115.2K, 8 data bits, no parity, 1 stop bit */
  UARTInit(115200);	
  UARTSend((uint8_t *)"\r\nLPC11Uxx USB ROMAPI example>", 31);

  /* get USB API table pointer */
  pUsbApi = (USBD_API_T*)((*(ROM **)(0x1FFF1FF8))->pUSBD);  

  /* enable clocks and pinmux for usb */
  USB_pin_clk_init();

  /* initilize call back structures */
  memset((void*)&usb_param, 0, sizeof(USBD_API_INIT_PARAM_T));
  usb_param.usb_reg_base = LPC_USB_BASE;
  usb_param.mem_base = 0x10001000;
  usb_param.mem_size = 0x1000;
  usb_param.max_num_ep = 2;
  usb_param.USB_Configure_Event = USB_Configure_Event;

/* Initialize Descriptor pointers */
  memset((void*)&desc, 0, sizeof(USB_CORE_DESCS_T));
  desc.device_desc = (uint8_t *)&USB_DeviceDescriptor[0];
  desc.string_desc = (uint8_t *)&USB_StringDescriptor[0];
  desc.full_speed_desc = (uint8_t *)&USB_FsConfigDescriptor[0];
  desc.high_speed_desc = (uint8_t *)&USB_FsConfigDescriptor[0];
  
  /* USB Initialization */
  ret = pUsbApi->hw->Init(&hUsb, &desc, &usb_param);  
  if (ret == LPC_OK) 
  {
    ret = usb_hid_init(hUsb, (USB_INTERFACE_DESCRIPTOR *)&USB_FsConfigDescriptor[sizeof(USB_CONFIGURATION_DESCRIPTOR)], 
			&usb_param.mem_base, &usb_param.mem_size);
    if (ret == LPC_OK) {
      NVIC_EnableIRQ(USB_IRQn); //  enable USB interrrupts 
      /* now connect */
      pUsbApi->hw->Connect(hUsb, 1);	
    }
  }                       
  else {    
    UARTSend((uint8_t *)"\r\nhwUSB_Init error!!!", 21);
  }

	while (1);
}
Beispiel #2
0
void usb_init(void) {

	USBD_API_INIT_PARAM_T usb_param;
	USBD_CDC_INIT_PARAM_T cdc_param;
	USB_CORE_DESCS_T desc;
	USBD_HANDLE_T hUsb, hCdc;
	ErrorCode_t ret = LPC_OK;
	uint32_t ep_indx;
	

	/* get USB API table pointer */
	pUsbApi = (USBD_API_T*)((*(ROM **)(0x1FFF1FF8))->pUSBD);

	/* enable clocks and pinmux */
	USB_pin_clk_init();

	/* initilize call back structures */
	memset((void*)&usb_param, 0, sizeof(USBD_API_INIT_PARAM_T));
	usb_param.usb_reg_base = LPC_USB_BASE;
	usb_param.mem_base = 0x10001000;
	usb_param.mem_size = 0x1000;
	usb_param.max_num_ep = 3;

	/* init CDC params */
	memset((void*)&cdc_param, 0, sizeof(USBD_CDC_INIT_PARAM_T));
	memset((void*)&g_vCOM, 0, sizeof(VCOM_DATA_T));

	/* user defined functions */
	//#if defined(UART_BRIDGE)
	//  cdc_param.SetLineCode = VCOM_SetLineCode; 
	//  usb_param.USB_SOF_Event = VCOM_sof_event; 
	//#endif
	cdc_param.SendBreak = VCOM_SendBreak;

	/* Initialize Descriptor pointers */
	memset((void*)&desc, 0, sizeof(USB_CORE_DESCS_T));
	desc.device_desc = (uint8_t *)&VCOM_DeviceDescriptor[0];
	desc.string_desc = (uint8_t *)&VCOM_StringDescriptor[0];
	desc.full_speed_desc = (uint8_t *)&VCOM_ConfigDescriptor[0];
	desc.high_speed_desc = (uint8_t *)&VCOM_ConfigDescriptor[0];

	/* USB Initialization */
	ret = pUsbApi->hw->Init(&hUsb, &desc, &usb_param);  
	if (ret == LPC_OK) {

		// init CDC params
		cdc_param.mem_base = usb_param.mem_base;
		cdc_param.mem_size = usb_param.mem_size;
		cdc_param.cif_intf_desc = (uint8_t *)&VCOM_ConfigDescriptor[USB_CONFIGUARTION_DESC_SIZE];
		cdc_param.dif_intf_desc = (uint8_t *)&VCOM_ConfigDescriptor[USB_CONFIGUARTION_DESC_SIZE + \
								  USB_INTERFACE_DESC_SIZE + 0x0013 + USB_ENDPOINT_DESC_SIZE ];

		ret = pUsbApi->cdc->init(hUsb, &cdc_param, &hCdc);

		if (ret == LPC_OK) {
			/* store USB handle */
			g_vCOM.hUsb = hUsb;
			g_vCOM.hCdc = hCdc;
			g_vCOM.send_fn = VCOM_usb_send;

			/* allocate transfer buffers */
			g_vCOM.rxBuf = (uint8_t*)(cdc_param.mem_base + (0 * USB_HS_MAX_BULK_PACKET));
			g_vCOM.txBuf = (uint8_t*)(cdc_param.mem_base + (1 * USB_HS_MAX_BULK_PACKET));
			cdc_param.mem_size -= (4 * USB_HS_MAX_BULK_PACKET);

			/* register endpoint interrupt handler */
			ep_indx = (((USB_CDC_EP_BULK_IN & 0x0F) << 1) + 1);
			ret = pUsbApi->core->RegisterEpHandler (hUsb, ep_indx, VCOM_bulk_in_hdlr, &g_vCOM);
			if (ret == LPC_OK) {
				/* register endpoint interrupt handler */
				ep_indx = ((USB_CDC_EP_BULK_OUT & 0x0F) << 1);
				ret = pUsbApi->core->RegisterEpHandler (hUsb, ep_indx, VCOM_bulk_out_hdlr, &g_vCOM);
				if (ret == LPC_OK) {

					/* enable IRQ */
					NVIC_EnableIRQ(USB_IRQ_IRQn); //  enable USB0 interrrupts 
					/* USB Connect */
					pUsbApi->hw->Connect(hUsb, 1);
				}
			}
		}    
	}

}
int main(void)
{
	unsigned char *p = &_vcom.string_descriptor[sizeof VCOM_StringDescriptor];
	int n;
	static unsigned short q;

	SystemInit();
	SystemCoreClockUpdate();
	lava_lmp_pin_init();
	USB_pin_clk_init();

	/* synthesize custom string descriptor using serial from EEPROM */
	memcpy(&vcom->string_descriptor, VCOM_StringDescriptor,
						  sizeof VCOM_StringDescriptor);
	lava_lmp_eeprom(EEPROM_RESERVED_OFFSET, EEPROM_READ, p,
			USB_SERIAL_NUMBER_CHARS * 2);

	if (*p == 0xff || *p == 0x00)
		memcpy(p, VCOM_UnsetSerial, sizeof(VCOM_UnsetSerial));

	for (n = 0; n < USB_SERIAL_NUMBER_CHARS; n++)
		ascii_serial[n] = p[n * 2];
	ascii_serial[USB_SERIAL_NUMBER_CHARS] = '\0';

	usbapi = (USBD_API_T *)((*(ROM **)(0x1FFF1FF8))->pUSBD);
	if (usbapi->hw->Init(&vcom->hUsb, (USB_CORE_DESCS_T *)&desc, &usb_param))
		goto spin;
	if (usbapi->cdc->init(vcom->hUsb, &cdc_param, &vcom->hCdc))
		goto spin;
	usbapi->core->RegisterEpHandler(vcom->hUsb,
			((USB_CDC_EP_BULK_IN & 0xf) << 1) + 1, VCOM_in, vcom);
	usbapi->core->RegisterEpHandler(vcom->hUsb,
			(USB_CDC_EP_BULK_OUT & 0xf) << 1, VCOM_out, vcom);

	vcom->rx_next_read = 0;
	vcom->rx_next_write = 0;

	NVIC_EnableIRQ(USB_IRQn); /* enable USB0 IRQ */
	usbapi->hw->Connect(vcom->hUsb, 1); /* USB Connect */

	/* foreground code feeds board-specific JSON parser */

	while (1) {
		if (!vcom->rxlen[vcom->rx_next_read]) {
			if (!vcom->pend_rx) {
				if (flash_led && !(q++ & 0x7fff)) {
					if (q & 0x8000)
						LPC_GPIO->SET[0] = 1 << 2;
					else
						LPC_GPIO->CLR[0] = 1 << 2;
				}


				if (idle_ok)
					lmp_json_callback_board(NULL, -1);
				continue;
			}
			NVIC_DisableIRQ(USB_IRQn);
			vcom->rxlen[vcom->rx_next_write] = usbapi->hw->ReadEP(
				vcom->hUsb, USB_CDC_EP_BULK_OUT, vcom->rxBuf[vcom->rx_next_write]);
			if (vcom->rx_next_write == RX_BUFS - 1)
				vcom->rx_next_write = 0;
			else
				vcom->rx_next_write++;
			vcom->pend_rx = 0;
			NVIC_EnableIRQ(USB_IRQn);
		}
		lmp_parse(vcom->rxBuf[vcom->rx_next_read], vcom->rxlen[vcom->rx_next_read]);
		NVIC_DisableIRQ(USB_IRQn);
		vcom->rxlen[vcom->rx_next_read] = 0;
		if (vcom->rx_next_read == RX_BUFS - 1)
			vcom->rx_next_read = 0;
		else
			vcom->rx_next_read++;
		NVIC_EnableIRQ(USB_IRQn);
	}

spin:
	while (1) {
		LPC_GPIO->SET[1] = 1 << 20;
		LPC_GPIO->CLR[1] = 1 << 20;
	}
}
Beispiel #4
0
/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
int main (void)
{
  USBD_API_INIT_PARAM_T usb_param;
  USB_CORE_DESCS_T desc;
  ErrorCode_t ret;
  USB_INTERFACE_DESCRIPTOR* pIntfDesc;
  USB_COMMON_DESCRIPTOR *pD;
  uint32_t next_desc_adr, total_len = 0; 

  SystemCoreClockUpdate();

  /* Setup UART for 115.2K, 8 data bits, no parity, 1 stop bit */
  UARTInit(115200);	
  UARTSend((uint8_t *)"\r\nLPC11Uxx USB ROMAPI example>", 31);

  /* get USB API table pointer */
  pUsbApi = (USBD_API_T*)((*(ROM **)(0x1FFF1FF8))->pUSBD);

  /* enable clocks and pinmux for usb0 */
  USB_pin_clk_init();

  /* initilize call back structures */
  memset((void*)&usb_param, 0, sizeof(USBD_API_INIT_PARAM_T));
  usb_param.usb_reg_base = LPC_USB_BASE;
  usb_param.mem_base = 0x10000800;
  usb_param.mem_size = 0x0800;
  usb_param.max_num_ep = 4;
  usb_param.USB_Configure_Event = USB_Configure_Event;

/* Initialize Descriptor pointers */
  memset((void*)&desc, 0, sizeof(USB_CORE_DESCS_T));
  desc.device_desc = (uint8_t *)&USB_DeviceDescriptor[0];
  desc.string_desc = (uint8_t *)&USB_StringDescriptor[0];
  desc.full_speed_desc = (uint8_t *)&USB_FsConfigDescriptor[0];
  desc.high_speed_desc = (uint8_t *)&USB_FsConfigDescriptor[0];

  /* USB Initialization */
  ret = pUsbApi->hw->Init(&hUsb, &desc, &usb_param);  
  if (ret == LPC_OK) 
  {

    pD = (USB_COMMON_DESCRIPTOR *)desc.high_speed_desc;
    next_desc_adr = (uint32_t)desc.high_speed_desc;

    while (pD->bLength) {
      if (pD->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE) {
        
        pIntfDesc = (USB_INTERFACE_DESCRIPTOR*)pD;
        
        switch(pIntfDesc->bInterfaceClass) {
          case USB_DEVICE_CLASS_STORAGE:
            ret = usb_msc_mem_init(hUsb, pIntfDesc, &usb_param.mem_base, &usb_param.mem_size);
            if (ret != LPC_OK)
              UARTSend((uint8_t *)"\r\n usb_msc_mem_init error!!!", 31);
            break;
          case USB_DEVICE_CLASS_APP:
            ret = usb_dfu_init(hUsb, pIntfDesc, &usb_param.mem_base, &usb_param.mem_size);
            if (ret != LPC_OK)
              UARTSend((uint8_t *)"\r\n usb_dfu_init error!!!", 27);
            break;
          case USB_DEVICE_CLASS_HUMAN_INTERFACE:
            ret = usb_hid_init(hUsb, pIntfDesc, &usb_param.mem_base, &usb_param.mem_size);
            if (ret != LPC_OK)
              UARTSend((uint8_t *)"\r\n usb_hid_init error!!!", 27);
            break;
        }
        if (ret != LPC_OK)
          break; /* break while loop no point proceeding further */
      }
      pIntfDesc = 0;
      total_len += pD->bLength;
      next_desc_adr = (uint32_t)pD + pD->bLength;
      pD = (USB_COMMON_DESCRIPTOR*)next_desc_adr;
    }

    if (total_len != ((USB_CONFIGURATION_DESCRIPTOR *)desc.high_speed_desc)->wTotalLength)
      UARTSend((uint8_t *)"\r\nBadly formed config descriptor!!!", 38);  

    if (ret == LPC_OK) {
      NVIC_EnableIRQ(USB_IRQn); //  enable USB interrrupts 
      /* now connect */
      pUsbApi->hw->Connect(hUsb, 1);	
    }

  }                       
  else {
    UARTSend("\r\nhwUSB_Init error!!!", 21);
  }

	while (1) 
	{                           				
	  u32Milliseconds = 100;
      if (dfu_detach_sig) {
        /* disconnect */
        pUsbApi->hw->Connect(hUsb, 0);
        /* for current test wrap-around condition is overlooked */
        while (u32Milliseconds < 98);
        /* connect the device back */
        pUsbApi->hw->Connect(hUsb, 1);
        dfu_detach_sig = 0;
      }		
	  /* Wait... */
	  while(u32Milliseconds);
	}
}
Beispiel #5
0
ErrorCode_t CDC_Init(SFPStream *stream, uint32_t guid[4]) {
	USBD_API_INIT_PARAM_T usb_param;
	USB_CORE_DESCS_T desc;
	USBD_HANDLE_T hUsb;
	ErrorCode_t ret = LPC_OK;
	uint32_t ep_indx;

	CDC_GenerateSerialDescriptor(guid);

	/* get USB API table pointer */
	pUsbApi = (USBD_API_T*) ((*(ROM **) (0x1FFF1FF8))->pUSBD);

	/* enable clocks and pinmux for usb0 */
	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;
	usb_param.mem_base = 0x20004000;
	usb_param.mem_size = 0x800;
	usb_param.max_num_ep = 5;

	/* Initialize Descriptor pointers */
	memset((void*) &desc, 0, sizeof(USB_CORE_DESCS_T));
	desc.device_desc = (uint8_t *) &UPER_DeviceDescriptor[0];
	desc.string_desc = (uint8_t *) &UPER_StringDescriptor[0];
	desc.full_speed_desc = (uint8_t *) &UPER_ConfigDescriptor[0];
	desc.high_speed_desc = (uint8_t *) &UPER_ConfigDescriptor[0];
	//desc.device_qualifier = (uint8_t*) &VCOM_DeviceQualifier[0];

	/* USB Initialization */
	//uint32_t usbMemSize = pUsbApi->hw->GetMemSize(&usb_param);
	ret = pUsbApi->hw->Init(&hUsb, &desc, &usb_param);

	if (ret != LPC_OK)
		return ret;

	/* Initialize private data */
	pUsbHandle = hUsb;

	CDC_SFP_rxPending = 0;
	CDC_SFP_rxBufferReadPos = 0;
	CDC_SFP_rxBufferWritePos = 0;

	CDC_SFP_txReady = 1;
	CDC_SFP_txBufferReadPos = 0;
	CDC_SFP_txBufferWritePos = 0;

	/* register SFP endpoint interrupt handler */
	ep_indx = (((USB_CDC_SFP_EP_BULK_IN & 0x0F) << 1) + 1);
	ret = pUsbApi->core->RegisterEpHandler(hUsb, ep_indx, CDC_SFP_bulk_in_hdlr, NULL);

	if (ret != LPC_OK)
		return ret;

	/* register SFP endpoint interrupt handler */
	ep_indx = ((USB_CDC_SFP_EP_BULK_OUT & 0x0F) << 1);
	ret = pUsbApi->core->RegisterEpHandler(hUsb, ep_indx, CDC_SFP_bulk_out_hdlr, NULL);

	if (ret != LPC_OK)
		return ret;

	/* register EP0 handler */
	ret = pUsbApi->core->RegisterClassHandler(hUsb, EP0_hdlr, NULL);
	if (ret != LPC_OK)
		return ret;

	/* enable IRQ */
	NVIC_SetPriority(USB_IRQn, 0); // give highest priority to USB
	NVIC_EnableIRQ(USB_IRQn); //  enable USB0 interrrupts

	/* USB Connect */
	pUsbApi->hw->Connect(hUsb, 1);

	stream->available = CDC_Stream_available;
	stream->read 	  = CDC_Stream_read;
	stream->readByte  = CDC_Stream_readByte;
	stream->write	  = CDC_Stream_write;

	return LPC_OK;
}