Content originally posted in LPCWare by sarangkalbande on Tue Sep 17 02:45:17 MST 2013
Hi,
Thanks for the reply
Please find the code below
int main (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;
nfcInProgress = 0;
#ifdef UART
UART_Init(9600);
//Priority = 1;
//IsDataAvailable = 1;
#else
RS485_Init(BaudRate);
#endif
Timer_Init();
TimerOut=Timer_New(TIMEMS);
BuzzerTimer=Timer_New(TIMEMS);
Gpio_Init();
Buzzer_Init();
Relay_Init();
UART_BuffFlush();
Flush_SendDataBuff();
Flush_CardDataBuff();
phpalI14443p3a_Sw_DataParams_t I14443p3a;
phpalI14443p4_Sw_DataParams_t I14443p4;
phpalMifare_Sw_DataParams_t palMifare;
phKeyStore_Rc663_DataParams_t Rc663keyStore;
phbalReg_Stub_DataParams_t balReader;
phhalHw_Rc663_DataParams_t halReader;
phalMfc_Sw_DataParams_t alMfc;
phStatus_t status;
uint8_t bUid[10];
uint8_t bLength;
uint8_t bMoreCardsAvailable;
uint8_t bSak[1];
uint8_t bHalBufferReader[0x40];
Reset_RC663();
phbalReg_Stub_Init(&balReader, sizeof(phbalReg_Stub_DataParams_t));
PH_CHECK_SUCCESS_FCT(status,phhalHw_Rc663_Init(&halReader,sizeof(phhalHw_Rc663_DataParams_t),
&balReader, 0, bHalBufferReader, sizeof(bHalBufferReader),
bHalBufferReader, sizeof(bHalBufferReader)));
halReader.bBalConnectionType = PHHAL_HW_BAL_CONNECTION_SPI;
pHal = &halReader;
PH_CHECK_SUCCESS_FCT(status, phpalI14443p3a_Sw_Init(&I14443p3a,
sizeof(phpalI14443p3a_Sw_DataParams_t), pHal));
PH_CHECK_SUCCESS_FCT(status,phpalI14443p4_Sw_Init(&I14443p4,
sizeof(phpalI14443p4_Sw_DataParams_t), pHal));
PH_CHECK_SUCCESS_FCT(status,phpalMifare_Sw_Init(&palMifare,
sizeof(phpalMifare_Sw_DataParams_t), pHal, &I14443p4));
PH_CHECK_SUCCESS_FCT(status,phKeyStore_Rc663_Init(&Rc663keyStore,
sizeof(phKeyStore_Rc663_DataParams_t), pHal));
PH_CHECK_SUCCESS_FCT(status,phalMfc_Sw_Init(&alMfc,
sizeof(phalMfc_Sw_DataParams_t), &palMifare, NULL));
PH_CHECK_SUCCESS_FCT(status,phhalHw_FieldReset(pHal));
PH_CHECK_SUCCESS_FCT(status,phhalHw_ApplyProtocolSettings(pHal,
PHHAL_HW_CARDTYPE_ISO14443A));
#ifdef RS485
RS485_ReceiveEnable();
#endif
Buzzer_SetState(BUZ_OFF);
volatile uint32_t s,p;
//volatile uint32_t i;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
/* Port PIO0_17 is used for Buzzer control*/
LPC_IOCON->PIO0_17 |= 0x00;
LPC_GPIO->DIR[0] |= (1<<17);
SystemCoreClockUpdate ();
/* 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 = 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_IRQn); // enable USB0 interrrupts
// pUsbApi->hw->WriteEP(hUsb,USB_CDC_EP_BULK_IN,
// &buff[0], 5);
#if defined(UART_BRIDGE)
g_vCOM.send_fn = VCOM_uart_send;
// g_vCOM.send_fn = pUsbApi->hw->WriteEP(hUsb,USB_CDC_EP_BULK_IN,&buff[0], 5);
/* init UART for bridge */
init_uart1_bridge(&g_vCOM, 0);
/* enable IRQ */
NVIC_EnableIRQ(UART_IRQn); // enable Uart interrrupt
#endif
/* USB Connect */
pUsbApi->hw->Connect(hUsb, 1);
/* LPC_GPIO->SET[0] = (1<<17);
for(p=0;p<25;p++)
{
for(s=0xfffff;s>0;s--);
}
LPC_GPIO->CLR[0] = (1<<17);*/
// pUsbApi->hw->WriteEP(hUsb,USB_CDC_EP_BULK_IN,
// &buff[0], 5);
}
}
}
}
/* wait for ever in uart bridge mode */
while (1)
{
if (IsDataAvailable==0)
{
//nfcInProgress = 1;
status=phpalI14443p3a_ActivateCard(&I14443p3a, NULL, 0x00, bUid, &bLength,
bSak, &bMoreCardsAvailable);
if (status != PH_ERR_SUCCESS)
{
Buzzer_Off();
IsDataAvailable=0;
}
else
{
status = phalMfc_Authenticate(&alMfc, BLOCK_AUTHENTICATE, PHHAL_HW_MFC_KEYA,
0, 0, bUid, bLength);
//UART_SendChar(status);
//nfcInProgress = 0;
if (status != PH_ERR_SUCCESS)
{
Buzzer_Fail();
IsDataAvailable=0;
}
else
{
status = phalMfc_Read(&alMfc, 2, &CardDataBuffer[0]);
if (status != PH_ERR_SUCCESS)
{
Buzzer_Fail();
IsDataAvailable=0;
}
else
{
Buzzer_Pass();
// UART_Send(&CardDataBuffer[0],0x10);
pUsbApi->hw->WriteEP(hUsb,USB_CDC_EP_BULK_IN,
&CardDataBuffer[0], 0x10);
phpalI14443p3a_Sw_HaltA(&I14443p3a);
IsDataAvailable=0;
}
}
}
}
Buzzer_Handle();
}
}
In the above code whenever I connect USB cable to my device the main card reading logic always fails
and without usb cable it works fine and send data over serial but the problem arises only when the USB cable is connected
what could be the isuue?