/*----------------------------------------------------------------------------- FUNCTION: phDal4Nfc_Reset PURPOSE: Reset the PN544, using the VEN pin -----------------------------------------------------------------------------*/ NFCSTATUS phDal4Nfc_Reset(long level) { NFCSTATUS retstatus = NFCSTATUS_SUCCESS; DAL_DEBUG("phDal4Nfc_Reset: VEN to %d",level); retstatus = gLinkFunc.reset(level); return retstatus; }
NFCSTATUS phDal4Nfc_ConfigRelease(void *pHwRef) { NFCSTATUS result = NFCSTATUS_SUCCESS; void * pThreadReturn; DAL_PRINT("phDal4Nfc_ConfigRelease "); if (gDalContext.hw_valid == TRUE) { /* Signal the read and write threads to exit. NOTE: there actually is no write thread! :) */ DAL_PRINT("Stop Reader Thread"); gReadWriteContext.nReadThreadAlive = 0; gReadWriteContext.nWriteThreadAlive = 0; /* Wake up the read thread so it can exit */ DAL_PRINT("Release Read Semaphore"); sem_post(&nfc_read_sem); DAL_DEBUG("phDal4Nfc_ConfigRelease - doing pthread_join(%d)", gReadWriteContext.nReadThread); if (pthread_join(gReadWriteContext.nReadThread, &pThreadReturn) != 0) { result = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED); DAL_PRINT("phDal4Nfc_ConfigRelease KO"); } /* Close the message queue */ #ifdef USE_MQ_MESSAGE_QUEUE mq_close(nDeferedCallMessageQueueId); #endif /* Shutdown NFC Chip */ phDal4Nfc_Reset(0); /* Close the link */ gLinkFunc.close(); if (gDalContext.pDev != NULL) { nfc_pn544_close(gDalContext.pDev); } /* Reset the Read Writer context to NULL */ memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext)); /* Reset the DAL context values to NULL */ memset((void *)&gDalContext,0,sizeof(gDalContext)); } gDalContext.hw_valid = FALSE; DAL_DEBUG("phDal4Nfc_ConfigRelease(): %04x\n", result); return result; }
/*----------------------------------------------------------------------------- FUNCTION: phDal4Nfc_Init PURPOSE: DAL Init function. -----------------------------------------------------------------------------*/ NFCSTATUS phDal4Nfc_Init(void *pContext, void *pHwRef ) { NFCSTATUS result = NFCSTATUS_SUCCESS; refresh_low_level_traces(); if ((NULL != pContext) && (NULL != pHwRef)) { pContext = pgDalContext; pgDalHwContext = (phHal_sHwReference_t *)pHwRef; if ( gDalContext.hw_valid == TRUE ) { /* The link has been opened from the application interface */ gLinkFunc.open_from_handle(pgDalHwContext); if (!gLinkFunc.is_opened()) { result = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE); } else { /* Clear link buffers */ gLinkFunc.flush(); } } else { static phDal4Nfc_sConfig_t hw_config; hw_config.deviceNode = NULL; result = phDal4Nfc_Config(&hw_config, pHwRef ); } } else /*Input parametrs invalid*/ { result = NFCSTATUS_INVALID_PARAMETER; } return result; }
NFCSTATUS phDal4Nfc_Shutdown( void *pContext, void *pHwRef) { NFCSTATUS result = NFCSTATUS_SUCCESS; void * pThreadReturn; // if (pContext == NULL) // return NFCSTATUS_INVALID_PARAMETER; if (gDalContext.hw_valid == TRUE) { /* Flush the link */ gLinkFunc.flush(); /* Close the message queue */ #ifdef USE_MQ_MESSAGE_QUEUE mq_close(nDeferedCallMessageQueueId); #endif } return result; }
/** * \ingroup grp_nfc_dal * * \brief DAL deferred callback function * Generic handler function called by a client thread when reading a message from the queue. * Will function will directly call the client function (same context). See phDal4Nfc_DeferredCall * * \param[in] params Parameter that will be passed to the client function. * */ void phDal4Nfc_DeferredCb (void *params) { int* pParam=NULL; int i; phNfc_sTransactionInfo_t TransactionInfo; pParam=(int*)params; switch(*pParam) { case PHDAL4NFC_READ_MESSAGE: DAL_PRINT(" Dal deferred read called \n"); TransactionInfo.buffer=gReadWriteContext.pReadBuffer; TransactionInfo.length=(uint16_t)gReadWriteContext.nNbOfBytesRead; TransactionInfo.status=NFCSTATUS_SUCCESS; gReadWriteContext.nReadBusy = FALSE; /* Reset flag so that another opertion can be issued.*/ gReadWriteContext.nWaitingOnRead = FALSE; if ((NULL != pgDalContext) && (NULL != pgDalContext->cb_if.receive_complete)) { pgDalContext->cb_if.receive_complete(pgDalContext->cb_if.pif_ctxt, pgDalHwContext,&TransactionInfo); } break; case PHDAL4NFC_WRITE_MESSAGE: DAL_PRINT(" Dal deferred write called \n"); #ifdef LOW_LEVEL_TRACES phOsalNfc_PrintData("Send buffer", (uint16_t)gReadWriteContext.nNbOfBytesToWrite, gReadWriteContext.pWriteBuffer); #endif /* DAL_DEBUG("dalMsg->transactInfo.length : %d\n", dalMsg->transactInfo.length); */ /* Make a Physical WRITE */ /* NOTE: need to usleep(3000) here if the write is for SWP */ usleep(500); /* NXP advise 500us sleep required between I2C writes */ gReadWriteContext.nNbOfBytesWritten = gLinkFunc.write(gReadWriteContext.pWriteBuffer, gReadWriteContext.nNbOfBytesToWrite); if (gReadWriteContext.nNbOfBytesWritten != gReadWriteContext.nNbOfBytesToWrite) { /* controller may be in standby. do it again! */ usleep(10000); /* wait 10 ms */ gReadWriteContext.nNbOfBytesWritten = gLinkFunc.write(gReadWriteContext.pWriteBuffer, gReadWriteContext.nNbOfBytesToWrite); } if (gReadWriteContext.nNbOfBytesWritten != gReadWriteContext.nNbOfBytesToWrite) { /* Report write failure or timeout */ DAL_PRINT(" Physical Write Error !!! \n"); TransactionInfo.length=(uint16_t)gReadWriteContext.nNbOfBytesWritten; TransactionInfo.status = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_BOARD_COMMUNICATION_ERROR); } else { DAL_PRINT(" Physical Write Success \n"); TransactionInfo.length=(uint16_t)gReadWriteContext.nNbOfBytesWritten; TransactionInfo.status=NFCSTATUS_SUCCESS; DAL_PRINT("WriteBuff[]={ "); for (i = 0; i < gReadWriteContext.nNbOfBytesWritten; i++) { DAL_DEBUG("0x%x ", gReadWriteContext.pWriteBuffer[i]); } DAL_PRINT("}\n"); // Free TempWriteBuffer if(gReadWriteContext.pTempWriteBuffer != NULL) { free(gReadWriteContext.pTempWriteBuffer); } } /* Reset Write context */ gReadWriteContext.nWriteBusy = FALSE; gReadWriteContext.nWaitingOnWrite = FALSE; /* call LLC callback */ if ((NULL != pgDalContext) && (NULL != pgDalContext->cb_if.send_complete)) { pgDalContext->cb_if.send_complete(pgDalContext->cb_if.pif_ctxt, pgDalHwContext,&TransactionInfo); } break; default: break; } }
int phDal4Nfc_ReaderThread(void * pArg) { char retvalue; NFCSTATUS result = NFCSTATUS_SUCCESS; uint8_t retry_cnt=0; void * memsetRet; static int MsgType= PHDAL4NFC_READ_MESSAGE; int * pmsgType=&MsgType; phDal4Nfc_Message_t sMsg; phOsalNfc_Message_t OsalMsg ; int i; int i2c_error_count; pthread_setname_np(pthread_self(), "reader"); /* Create the overlapped event. Must be closed before exiting to avoid a handle leak. This event is used READ API and the Reader thread*/ DAL_PRINT("RX Thread \n"); DAL_DEBUG("\nRX Thread nReadThreadAlive = %d",gReadWriteContext.nReadThreadAlive); DAL_DEBUG("\nRX Thread nWaitingOnRead = %d",gReadWriteContext.nWaitingOnRead); while(gReadWriteContext.nReadThreadAlive) /* Thread Loop */ { /* Check for the read request from user */ DAL_PRINT("RX Thread Sem Lock\n"); sem_wait(&nfc_read_sem); DAL_PRINT("RX Thread Sem UnLock\n"); if (!gReadWriteContext.nReadThreadAlive) { /* got the signal that we should exit. NOTE: we don't attempt to read below, since the read may block */ break; } /* Issue read operation.*/ i2c_error_count = 0; retry: gReadWriteContext.nNbOfBytesRead=0; DAL_DEBUG("RX Thread *New *** *****Request Length = %d",gReadWriteContext.nNbOfBytesToRead); memsetRet=memset(gReadWriteContext.pReadBuffer,0,gReadWriteContext.nNbOfBytesToRead); /* Wait for IRQ !!! */ gReadWriteContext.nNbOfBytesRead = gLinkFunc.read(gReadWriteContext.pReadBuffer, gReadWriteContext.nNbOfBytesToRead); /* TODO: Remove this hack * Reading the value 0x57 indicates a HW I2C error at I2C address 0x57 * (pn544). There should not be false positives because a read of length 1 * must be a HCI length read, and a length of 0x57 is impossible (max is 33). */ if(gReadWriteContext.nNbOfBytesToRead == 1 && gReadWriteContext.pReadBuffer[0] == 0x57) { i2c_error_count++; DAL_DEBUG("RX Thread Read 0x57 %d times\n", i2c_error_count); if (i2c_error_count < 5) { usleep(2000); goto retry; } DAL_PRINT("RX Thread NOTHING TO READ, RECOVER"); phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1); } else { i2c_error_count = 0; #ifdef LOW_LEVEL_TRACES phOsalNfc_PrintData("Received buffer", (uint16_t)gReadWriteContext.nNbOfBytesRead, gReadWriteContext.pReadBuffer); #endif DAL_DEBUG("RX Thread Read ok. nbToRead=%d\n", gReadWriteContext.nNbOfBytesToRead); DAL_DEBUG("RX Thread NbReallyRead=%d\n", gReadWriteContext.nNbOfBytesRead); DAL_PRINT("RX Thread ReadBuff[]={ "); for (i = 0; i < gReadWriteContext.nNbOfBytesRead; i++) { DAL_DEBUG("RX Thread 0x%x ", gReadWriteContext.pReadBuffer[i]); } DAL_PRINT("RX Thread }\n"); /* read completed immediately */ sMsg.eMsgType= PHDAL4NFC_READ_MESSAGE; /* Update the state */ phDal4Nfc_FillMsg(&sMsg,&OsalMsg); phDal4Nfc_DeferredCall((pphDal4Nfc_DeferFuncPointer_t)phDal4Nfc_DeferredCb,(void *)pmsgType); memsetRet=memset(&sMsg,0,sizeof(phDal4Nfc_Message_t)); memsetRet=memset(&OsalMsg,0,sizeof(phOsalNfc_Message_t)); } } /* End of thread Loop*/ DAL_PRINT("RX Thread exiting"); return TRUE; }
/*----------------------------------------------------------------------------- FUNCTION: phDal4Nfc_Config PURPOSE: Configure the serial port. -----------------------------------------------------------------------------*/ NFCSTATUS phDal4Nfc_Config(pphDal4Nfc_sConfig_t config,void **phwref) { NFCSTATUS retstatus = NFCSTATUS_SUCCESS; DAL_PRINT("phDal4Nfc_Config"); if ((config == NULL) || (phwref == NULL) || (config->nClientId == -1)) return NFCSTATUS_INVALID_PARAMETER; /* Register the link callbacks */ memset(&gLinkFunc, 0, sizeof(phDal4Nfc_link_cbk_interface_t)); switch(config->nLinkType) { case ENUM_DAL_LINK_TYPE_COM1: case ENUM_DAL_LINK_TYPE_COM2: case ENUM_DAL_LINK_TYPE_COM3: case ENUM_DAL_LINK_TYPE_COM4: case ENUM_DAL_LINK_TYPE_COM5: case ENUM_DAL_LINK_TYPE_USB: { DAL_PRINT("UART link Config"); /* Uart link interface */ gLinkFunc.init = phDal4Nfc_uart_initialize; gLinkFunc.open_from_handle = phDal4Nfc_uart_set_open_from_handle; gLinkFunc.is_opened = phDal4Nfc_uart_is_opened; gLinkFunc.flush = phDal4Nfc_uart_flush; gLinkFunc.close = phDal4Nfc_uart_close; gLinkFunc.open_and_configure = phDal4Nfc_uart_open_and_configure; gLinkFunc.read = phDal4Nfc_uart_read; gLinkFunc.write = phDal4Nfc_uart_write; gLinkFunc.download = phDal4Nfc_uart_download; gLinkFunc.reset = phDal4Nfc_uart_reset; } break; case ENUM_DAL_LINK_TYPE_I2C: { DAL_PRINT("I2C link Config"); /* i2c link interface */ gLinkFunc.init = phDal4Nfc_i2c_initialize; gLinkFunc.open_from_handle = phDal4Nfc_i2c_set_open_from_handle; gLinkFunc.is_opened = phDal4Nfc_i2c_is_opened; gLinkFunc.flush = phDal4Nfc_i2c_flush; gLinkFunc.close = phDal4Nfc_i2c_close; gLinkFunc.open_and_configure = phDal4Nfc_i2c_open_and_configure; gLinkFunc.read = phDal4Nfc_i2c_read; gLinkFunc.write = phDal4Nfc_i2c_write; gLinkFunc.reset = phDal4Nfc_i2c_reset; break; } default: { /* Shound not happen : Bad parameter */ return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_PARAMETER); } } gLinkFunc.init(); /* So that link interface can initialize its internal state */ retstatus = gLinkFunc.open_and_configure(config, phwref); if (retstatus != NFCSTATUS_SUCCESS) return retstatus; /* Iniatilize the DAL context */ (void)memset(&gDalContext,0,sizeof(phDal4Nfc_SContext_t)); pgDalContext = &gDalContext; /* Reset the Reader Thread values to NULL */ memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext)); gReadWriteContext.nReadThreadAlive = TRUE; gReadWriteContext.nWriteBusy = FALSE; gReadWriteContext.nWaitingOnWrite = FALSE; /* Prepare the message queue for the defered calls */ #ifdef USE_MQ_MESSAGE_QUEUE nDeferedCallMessageQueueId = mq_open(MQ_NAME_IDENTIFIER, O_CREAT|O_RDWR, 0666, &MQ_QUEUE_ATTRIBUTES); #else nDeferedCallMessageQueueId = config->nClientId; #endif /* Start Read and Write Threads */ if(NFCSTATUS_SUCCESS != phDal4Nfc_StartThreads()) { return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED); } gDalContext.hw_valid = TRUE; phDal4Nfc_Reset(1); phDal4Nfc_Reset(0); phDal4Nfc_Reset(1); return NFCSTATUS_SUCCESS; }
/*----------------------------------------------------------------------------- FUNCTION: phDal4Nfc_Config PURPOSE: Configure the serial port. -----------------------------------------------------------------------------*/ NFCSTATUS phDal4Nfc_Config(pphDal4Nfc_sConfig_t config,void **phwref) { NFCSTATUS retstatus = NFCSTATUS_SUCCESS; const hw_module_t* hw_module; nfc_pn544_device_t* pn544_dev; uint8_t num_eeprom_settings; uint8_t* eeprom_settings; int ret; /* Retrieve the hw module from the Android NFC HAL */ ret = hw_get_module(NFC_HARDWARE_MODULE_ID, &hw_module); if (ret) { ALOGE("hw_get_module() failed"); return NFCSTATUS_FAILED; } ret = nfc_pn544_open(hw_module, &pn544_dev); if (ret) { ALOGE("Could not open pn544 hw_module"); return NFCSTATUS_FAILED; } config->deviceNode = pn544_dev->device_node; if (config->deviceNode == NULL) { ALOGE("deviceNode NULL"); return NFCSTATUS_FAILED; } DAL_PRINT("phDal4Nfc_Config"); if ((config == NULL) || (phwref == NULL)) return NFCSTATUS_INVALID_PARAMETER; /* Register the link callbacks */ memset(&gLinkFunc, 0, sizeof(phDal4Nfc_link_cbk_interface_t)); switch(pn544_dev->linktype) { case PN544_LINK_TYPE_UART: case PN544_LINK_TYPE_USB: { DAL_PRINT("UART link Config"); /* Uart link interface */ gLinkFunc.init = phDal4Nfc_uart_initialize; gLinkFunc.open_from_handle = phDal4Nfc_uart_set_open_from_handle; gLinkFunc.is_opened = phDal4Nfc_uart_is_opened; gLinkFunc.flush = phDal4Nfc_uart_flush; gLinkFunc.close = phDal4Nfc_uart_close; gLinkFunc.open_and_configure = phDal4Nfc_uart_open_and_configure; gLinkFunc.read = phDal4Nfc_uart_read; gLinkFunc.write = phDal4Nfc_uart_write; gLinkFunc.reset = phDal4Nfc_uart_reset; } break; case PN544_LINK_TYPE_I2C: { DAL_PRINT("I2C link Config"); /* i2c link interface */ gLinkFunc.init = phDal4Nfc_i2c_initialize; gLinkFunc.open_from_handle = phDal4Nfc_i2c_set_open_from_handle; gLinkFunc.is_opened = phDal4Nfc_i2c_is_opened; gLinkFunc.flush = phDal4Nfc_i2c_flush; gLinkFunc.close = phDal4Nfc_i2c_close; gLinkFunc.open_and_configure = phDal4Nfc_i2c_open_and_configure; gLinkFunc.read = phDal4Nfc_i2c_read; gLinkFunc.write = phDal4Nfc_i2c_write; gLinkFunc.reset = phDal4Nfc_i2c_reset; break; } default: { /* Shound not happen : Bad parameter */ return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_PARAMETER); } } gLinkFunc.init(); /* So that link interface can initialize its internal state */ retstatus = gLinkFunc.open_and_configure(config, phwref); if (retstatus != NFCSTATUS_SUCCESS) return retstatus; /* Iniatilize the DAL context */ (void)memset(&gDalContext,0,sizeof(phDal4Nfc_SContext_t)); pgDalContext = &gDalContext; /* Reset the Reader Thread values to NULL */ memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext)); gReadWriteContext.nReadThreadAlive = TRUE; gReadWriteContext.nWriteBusy = FALSE; gReadWriteContext.nWaitingOnWrite = FALSE; /* Prepare the message queue for the defered calls */ #ifdef USE_MQ_MESSAGE_QUEUE nDeferedCallMessageQueueId = mq_open(MQ_NAME_IDENTIFIER, O_CREAT|O_RDWR, 0666, &MQ_QUEUE_ATTRIBUTES); #else nDeferedCallMessageQueueId = config->nClientId; #endif gDalContext.pDev = pn544_dev; /* Start Read and Write Threads */ if(NFCSTATUS_SUCCESS != phDal4Nfc_StartThreads()) { return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED); } gDalContext.hw_valid = TRUE; phDal4Nfc_Reset(1); phDal4Nfc_Reset(0); phDal4Nfc_Reset(1); return NFCSTATUS_SUCCESS; }