1、1Implementing a Custom USB Device Controller Driver in ZephyrMOHAMMED BILLOOZephyr Developer Summit 20232THE SPEAKEREmbedded Software ConsultantDesign WorkMedical devicesScientific InstrumentsAutomotiveDefenseConsumer ElectronicsExperience/ExpertiseRTOS-based systems ZephyrEmbedded Linux/The Yocto P
2、rojectQtMohammed Billoo(mabmab-) SPEAKERTraining/WorkshopsVirtualOn-site/In-personwww.mab-BIOS FOOD Newslettermabmab-4AgendaProject OverviewSystem ArchitectureProblem StatementBackground(Very brief)USB PrimerInitial ApproachChallenges FacedTroubleshooting Tools/StepsResolutionNext StepsRetrospective
3、Q&AMAB Labs,LLC 2023 All Rights Reserved5PROJECT OVERVIEW6System ArchitectureEnclustra XU5 BoardZynq Ultrascale+MPSoC FPGAProduction readyUART ConsoleEthernet7System ArchitectureARM(hard block)RISC-V CoreRISC-V MemoryXilinx USB Controller(hard block)Xilinx UART Controller(hard block)ConsoleDebugging
4、8Existing SystemHard ARM CoreUART ConsoleEthernetUSB CDC9Existing SystemFreeRTOS-based Implementations“Final”Blinky USB CDC NetworkDebugging Access to RISC-V Memory Network10USB CDC in FreeRTOSTinyUSBUSB Host/Device StackTargeted for Embedded Systems Memory Safe No dynamic allocations Thread Safe In
5、terrupts deferredCross-platformOpen-source11TinyUSBCustom ImplementationCustom Implementation12TinyUSBOS Abstraction Layer(OSAL)RTOS-specific implementationsosal.h13TinyUSBOS Abstraction Layer(OSAL)RTOS-specific implementationsosal_freertos.h14TinyUSBMCU-specific Implementationtusb_option.h15TinyUSB
6、MCU-specific Implementationtusb_option.h16TinyUSBMCU-specific ImplementationCustom addition17Existing SystemTinyUSB customizationtusb_config.h18Existing SystemApplicationEchoTask to receive data over USB CDC and transmit the same dataMCU portInterrupt handlersWrite to appropriate Xilinx registers in
7、 response to TUSB function calls19Problem StatementImplement USB device and CDC driver leveraging Zephyrs USB stackAssume Xilinx USB device controllerMAB Labs,LLC 2023 All Rights Reserved20(BRIEF)USB OVERVIEW21Device ConnectionDevice plugged into host busHost detects additionHost interrogates device
8、 Identify type and“services”it provides Load appropriate driverUSB transfer modesControlInterruptBulkIsochronous22USB TransactionToken packetHeaderSpecifies whats nextData packetOptionalPayloadStatus PacketACK(or NAK)transactionError correction23USB TransactionALL TRANSACTIONS INITIATED BY HOST!Lang
9、uage is from hosts perspective24USB PacketFieldsSync:Known pattern for clock synchronizationPacket ID(PID):Packet typeAddress:Specifies destination device on busEndpoint:Target endpoint at destinationToken PacketsIN:Host wishes to read data from deviceOUT:Host wishes to write data to deviceSetup:Hos
10、t wishes to begin control transfer25USB PacketHandshakeACK Positive acknowledgment that packet has been receivedNAK Device busy/no data presentSTALL Device requires intervention from host26USB TransactionTOKEN PACKETDATA PACKETHANDSHAKE PACKET27USB EndpointsData source/sinkDevice driver(host)EP1 OUT
11、Interrupt:“Have data on EP1 OUT”Process data in EP1 OUT bufferPlace data in EP1 IN bufferEP1 IN28Zero Length PacketsZLPHost sends OUT token to send data to EP1 OUT bufferHost sends IN token to check receipt Zero Length(Data)Packet If device has successfully received packet,responds with ACK Device p
12、rocesses data in EP1 OUT Otherwise,responds with NAK29Zero Length PacketsZLPHost waiting for response to EP1 OUT transmissionHost sends IN token,reads data from EP1 IN bufferHost sends OUT token Determine if device is ready for next transaction Zero Length(Data)Packet If device ready,response with A
13、CK Otherwise,device responds with NAK Host tries again later30USB EndpointsAll devices must support endpoint 0Receives all control and status requests during enumerationAllows host to identify Functionality provided by USB device Other endpoints available31USB EnumerationDevice Descriptor RequestUsu
14、ally,the first step during enumerationSETUP TOKENDATA=Device Descriptor RequestIN TOKENDATA=DescriptorsOUT TOKENDATA=ZLP(transaction status)MAB Labs,LLC 2023 All Rights Reserved32INITIAL APPROACH33Initial StrategyImplement USB Device callbacksLow level APIHigh level API34Initial StrategyUse Windows
15、for CDC host driverPlug In DeviceUse TeraTerm to check if characters are being echoedUse printks to test functionality35Initial StrategyDone?36TroubleshootingNeeded to independently track USB transactionsTool?37Troubleshooting38TroubleshootingUse ILAInternal Logic AnalyzerXilinx debugging toolCaptur
16、e signals in the FPGAAbsolute last resort39Initial ResultsDetermined that data wasnt being correctly transferred to the FPGAAlways sending NAKsHost would try again but would ultimately give upUnderlying Xilinx registers were not configured correctly Use ARM to interrogate Xilinx registersMAB Labs,LL
17、C 2023 All Rights Reserved40RESOLUTION41ResolutionRunning out of timeDecided to port TinyUSB to ZephyrCompletely sidestep Zephyr USB device stackCreated a new OS port for ZephyrCreated OSAL for ZephyrCreated Zephyr threads toManage transactions from FPGAManage CDC echo functionality42ResolutionWorke
18、d!43ResolutionDone?Yes,for nowMAB Labs,LLC 2023 All Rights Reserved44NEXT STEPS45RetrospectiveShould have taken time to intelligently port driverNave at the timeZephyr USB device stack different from TinyUSBNot as simple as simply copying functions overBut,happy customerOn June 21,asked for availabi
19、lity on more potential Zephyr work46Next StepsTwo optionsPort TinyUSB to ZephyrProbably unnecessary Already have a stackCorrectly incorporate Xilinx USB device driver into Zephyr stackCan use ARM core Dont have RISC-V IPCan use Zynq development board Dont have access to EnclustraPush upstream/mainline!MAB Labs,LLC 2023 All Rights Reserved47Q&Amabmab-