Home

Using the SmartDMA Controller with USB on the

image

Contents

1. outpw UCEPDEF1 UEPDEF1 EP NUM C UEPDEF1 EP CFG 0 UEPDEF1 EP INT 0 UEPDEF1 EP ASET 0 UEPDEF1 EP DIR OUT UEPDEF1 EP TYPE BULK Definition register 2 64 byte FIFO packet size and 64 byte packets outpw UCEPDEF2 UEPDEF2 FIFO SIZE 64 UEPDEF2 EP MX PCT 604 Definition register 3 No packet status in the OUT direction and a stop interrupt is generated on SHRT PKT BUFF ERR or OTHER ERR outpw UCEDEF3 UEPDEF3 MODE SDMA UEPCTL FULL PKT UEPCTL BUF ERR UEPCTL OTHER ERR Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note A 3 AMD Set endpoint D as a BULK IN endpoint with buffer per IRP SmartDMA Definition register 1 Set the endpoint number to C the configuration interface and alternate setting to 0 the direction to IN and the type to BULK outpw UDEPDEF1 UEPDEF1 EP NUM D UEPDEF1 EP CFG 0 UEPDEF1 EP INT 0 UEPDEF1 EP ASET 0 D UEPDEF1 EP DIR IN UEPDEF1 EP TYPE BULK Definition register 2 32 byte FIFO packet size and 64 byte packets outpw UDEPDEF2 UEPDEF2 FIFO SIZE 32 UEPDEF2 EP
2. HandleErrorD Now that we ve handled the interrupt clear the act req bit turn interrupts back on and resume outpw UDEPCTL inpw UDEPCTL amp UEPCTL ACT REQ outpw UIMASK1 uimask outpw SD3CON sdcon k for actions on the A endpoint if status amp UISTAT A EP ACT WORD a status uimask sdcon Stop the DMA while we figure out the interrupt source uimask inpw UIMASK1 sdcon inpw SD2CON outpw UIMASK1 uimask amp UISTAT1 A EP ACT outpw SD2CON sdcon amp SDCON RXST a status inpw UAEPCTL if a status amp UEPCTL BUF ERR a status UEPCTL OTHER ERR HandleErrA Now that we ve handled the interrupt clear the act reg bit turn interrupts back on and resume outpw UAEPCTL inpw UAEPCTL 4 UEPCTL ACT REQ outpw UIMASK1 uimask outpw SD2CON sdcon if status amp UISTAT A STATINT A 6 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note AMD The SmartDMA places each new packet in its own buffer so there s no need to stop on that We can keep receiving data while we handle each packet HandleReceivedPacketA else if status amp UISTAT C EP ACT WORD c_status uimask sdcon Stop the DMA while we figure out the interrupt source uimask inpw UIMASK1 sdcon inpw SD3CON outpw UIMASK1 uim
3. outpw EOI RR KKK KK kok ke e kok NAM P UsbProcessI ESCRIPTION This functi interrupt D E USB ACKCkCk ckCk ck ckCck ck K KA kCk ck K KZ ck X AZ A X AZ ck kk A X X ck kk ck k ck ck k ck ck k ck ck k ck ck k ck ck k ck k k k kk KK KK nt Determine interrupt cause and finish interrupt on loops checking to see what condition caused the USB until all sources are handled Depending on the interrupt will source it endpoint in CkCkCkck ck ck ck ck ck ck ck ck ck kk void UsbProcessIn First de volatile WO while sta Ch if s Then chec else else usually shut down a SDMA channel and mask out an terrupt while it uses a subfunction to handle the source OKCKCKCKCKCKCkCKCkCkCkCkCkCKCkCkCk kok k kok Kk Koko Ck kCk Ck kc k Ck kck ck kck I I OR ke ke ke t termine which endpoint is causing the interrupt RD status inpw UISTAT1 tus 0 54 eck for transmit errors first tatus amp UISTAT1 D EP ACT WORD uimask sdcon Stop the DMA while we figure out the interrupt source inpw UIMASK1 inpw SD3CON ky uimask sdcon outpw UIMASK1 uimask amp UISTAT1 D EP ACT outpw SD3CON sdcon amp SDCON TXST Since this is a transmit line we ve programmed SmartDMA to interrupt after completely sendi The USB only interrupts if there s been an error ng a packet x
4. void ConfigureUSB Turn off USB interrupts outpw UIMASK1 0 outpw UIMASK2 0 Set endpoint A as a BULK OUT endpoint with buffer per packet IRP SmartDMA Definition register 1 Set the endpoint number to B the configuration interface and alternate setting to 0 the direction to IN and the type to BULK outpw UAEPDEF1 UEPDEF1 EP NUM A UEPDEF1 EP CFG 0 UEPDEF1 EP INT 0 UEPDEF1 EP ASET 0 UEPDEF1 EP DIR OUT UEPDEF1 EP TYPE BULK Definition register 2 64 byte FIFO packet size and 64 byte packets outpw UAEPDEF2 UEPDEF2 FIFO SIZE 16 UEPDEF2 EP MX PCT 64 Definition register 3 SmartDMA with packet status and an interrupt is generated on FULL PKT or SHRT PKT and a stop interrupt on BUFF ERR or OTHER ERR The bits are shifted 8 to the left to move them from the stop positions to the status positions outpw UAEDEF3 UEPDEF3 MODE SDMA STAT UEPCTL FULL PKT UEPCTL SHOR PKT 8 UEPCTL BUF ERR UEPCTL OTHER ERR Gl I Set endpoint C as a BULK OUT endpoint with buffer per IRP SmartDMA Definition register 1 Set the endpoint number to C the configuration interface and alternate setting to 0 the direction to OUT and the type to BULK
5. MX PCT 604 Definition register 3 No packet status in the OUT direction and a stop interrupt is generated on SHRT PKT BUFF ERR or OTHER ERR outpw UDEDEF3 UEPDEF3 MODE SDMA UEPCTL BUF ERR UEPCTL OTHER ERR Set up the USB interrupt channel and vector unsigned long far VectorTable Address of handler offset value VectorTable unsigned long far ITYPE_USB lt lt 2 VectorTable unsigned long USBHandleInt I outpw CH2CON CHCON SRC INTERNAL CHCON LTM CHCON PR6 Turn on the receive interrupts outpw UIMASK1 UIMASK1 C EP ACT UIMASK1 A EP STATINT UIMASK1 A EP ACT UIMASK1 OTHER INT UIMASK1 CNT EP NEW UIMASK1 CNT EP ACT Ok k k kok k kok kok ok kok k ok k kok k kok k kok ok kok k kok k kok k kok kok Koko kok k kCkCk Ck kCk I ck K kok kck ck ko kk NAME ConfigureSDMA Configures SmartDMA channels 2 and 3 DESCRIPTION Configures the SmartDMA channels to work with endpoints A C and D KOKCKCKCKCKCKCkCk kOkCk kCKCk kCkCk kok k Ck kCK Ck kCKCk kCKCk KCk k kCk Ck kCk CK Ck kCK Ck kc k k kok k kc k k kok kck ck kck ck k kk kk kk ke e ke amp void ConfigureSDMA DWORD address WORD i Set up SDMA Channel 2 SDMA Control register Receive Set Own medium priority USB as requesting source outpw SD2CON SDCON RXSO SDCON P MED SDCON D GetLinearAddress ring buffer a amp address outpw
6. buffer c c current Low address t the error condition is Ky if error_cause amp UEPCTL_BUF_ERR BUFF_ERR indicates an overflow in the current packet Part of the previous packet might be in the buffer so we need to pull that out and store it in the data buffer Then we need to point the DMA to the start of the current buffer flush the buffer and continue if length PACKET SIZE int bytes_left i char data_buf data_buf_c c_current Determine the number of bytes left from the previous packet bytes_left PACKET_SIZE length PACKET SIZE Read the bytes left from the previous packet for i 0 i lt bytes left i data buf length i inpw UCEPDAT Update the length to point to past the end of the packet that was just read from the data FIFO length length amp PACKET SIZE PACKET SIZE Point the data to the start of the interrupted packet ring_buffer_c c_current Low_address length amp PACKET_SIZE The buffer length has to be decreased by the amount of data already received ring_buffer_c c_current Size length amp PACKET_SIZE outpw UCEPCTL inpw UCEPCTL UEPCTL NOT FLUSH UEPCTL BUF ERR else if error_cause amp UEPCTL_OTHER_ERR OTHER_ERR indicates the most recently received
7. data from the FIFO and write it to memory data buf length i inpw UAEPDAT Update the message count and clear the error bit ring buffer a a current Message count packet size ring buffer ala current Config WORD address gt gt 16 amp 0x000f 0x0200 We have now received a packet Invoke the packet handler routine HandleReceivedPacketA Clear the error bits and flush the FIFO outpw UAEPCTL inpw UAEPCTL amp UEPCTL_NOT_FLUSH UEPCTL_BUF_ERR UEPCTL OTHER ERR KOK KR KKK I k kok kok k oko k kok ok oko k kok k kok k oko k oko A I k kok k kck I OI I kk NAME HandleReceivedPacketA Handle a successful receive on the A endpoint DESCRIPTION This function handles packets on the A endpoint by dropping them to the floor and resetting the most recently used buffer descriptor KOKCKCKCKCKCKCkCkCkOkCk kCKCk kok k kCkCk Ck kCK Ck kCkCkCkCKCkCKCk Ck kCk kk kk Ck kk Ck kc k Ck kk k kc k k kc k ck kck ck kck ck kok ck X k ok ck ke kk void HandleReceivedPacketA DWORD address Get the original address of this data buffer Get LinearAddress data buf ala current amp address Reset the buffer ring buffer a a current Size PACKET SIZE 1 amp Ox7FFF ring buffer a a current Low address WORD address amp Oxffff ring buffer ala current Config WORD add
8. interrupt vector OxC to point to the USB interrupt handler 4 Set interrupt channel 2 for an internal level triggered source and then enable it with the CH2CON register Setting Up the SmartDMA Channel 1 Create and initialize a buffer of descriptors in memory a Allocate an array of data buffer descriptors and a data buffer in memory b Configure the buffer descriptors by setting the LADR and HADR bytes to point to the data buffer and then set the OWN bit for any buffers that are available for transfer c Set the STP and ENP bits at the start and end respectively of any buffer chains If each SmartDMA buffer contains all the data for a transfer set both the STP and ENP bits in each buffer 2 Program the SmartDMA control register SDx CON a Clear the start bit xXST and set the DSEL bit This stops the channel and makes USB the re questing source b Clear the xXSO bit so that the hardware always returns the descriptor to software control when the hardware transfers BCNT bytes c For IN transmit endpoints B or D set the TTCI bit to cause an interrupt after all the data is trans ferred d For OUT receive endpoints A or C do not set any other bits 3 Write the descriptor ring address into the appropri ate Ring Address High SDxxRAH and Ring Ad dress Low SDxxRCAL registers 4 Set the Current Buffer Descriptor SDxCBD regis ter to point to the first available buffer 5 Turn on t
9. the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note A 1 AMD access to it endpoint D has no data buffers it will use either data buf c or data buf a volatile SDMADescriptorFormat far ring buffer d DESCRIPTORS These are the rings of format descriptors Each descriptor contains the status for a buffer Must be volatile since the hardware has access to it WORD a current Number of the buffer currently being used by this WORD c current SmartDMA channel or the number of the most recently WORD d current Used channel void main void ConfigureUSB ConfigureSDMA Loop forever for k k KR k k kok kok k kok k kok k oko kok k kok k kok k kok k kok k kkkkk kkk kkk kkk kkk kk kkk k kkk ck K kok Koko ko kk NAME GetDiff Return the absolute difference between two numbers DESCRIPTION Configures endpoint KKK kok kCkCk kCKCk kCk kok Ck kCK Ck kCKCk kCKCk KCk k kCkCkCkCkCK Ck A Ck kok kok k kok kck ck k kk ck kk kk kk WORD GetDiff WORD numl WORD num2 if numl gt num2 return numl num2 else return num2 numl KOK k kk KK kok ok kok kok k oko k kok k kok k kok k kok k kok k kok Koko Koko A kkk Ck kok ck kok ck I ck K kok kkk k kk NAME GetLinearAddress Get the linear address of a pointer DESCRIPTION KOKCKCKCKCKCKCkCkCkCkCk kCKCk kCk Ck kok k Ck kCK Ck kCKCk kCk K KCk k kCk Ck kok kCkCK Ck
10. Message count 0 c current 0 GetLinearAddress ring buffer d amp address outpw SD3TRCAL WORD address amp Oxfff0 SDTRCAL TRC 16 outpw SD3TRAH WORD address gt gt 16 amp 0x000f Set up the transmit ring descriptors to not point anywhere Leave them under software control so we can adjust them after we receiv data for i 0 i lt DESCRIPTORS i ring buffer d il Low address 0 ring buffer d i Low address 0x0300 ring buffer c i Size 0 ring buffer c il Message count 0 Set up the SDMA interrupt channels and vectors unsigned long far VectorTable Address of handler offset value VectorTable unsigned long far ITYPE_SDMA3 lt lt 2 VectorTable unsigned long SDMAHandleInt outpw CH7CON CHCON PR6 KOK k kk k k kok kok ok kok k ok k kok k kok k Koko Koko k kok k Koko k Koko k Koko Koko kok k Koko k kok k kok ck kok ck Koko k K kok kkk ke kk NAM UsbHandleInt USB Interrupt handler DESCRIPTION Calls the USB interrupt processor and cleans up th nd of interrupt register afterwards KOKCKCKCKCKCKCkCk kCkCk Koko Ck kok k Ck kCK Ck kCKCk kCKCkCKCk Ck kok k kk K kk kk Ck kk k kc k k kk ck kck ck kck ck kck ck ckok ck kok ke ke ke e ke void interrupt UsbHandleInt UsbProcessInt Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note A 5 AMD EOITYP
11. OKCKCKCKCkCKCkCk kCkCk kCkCk kCkCk kCkCK Ck kCK kok k kok K kCk k kCkCk kCkCK K kCK Ck kc k k kk kok Ck kc k ck kck ck I OR ck kk void HandleOtherInterrupt This function would handle Control endpoint messages and other standard USB interrupts For a detailed example of a full featured USB interrupt handler study CodeKits CK0005xx or CK0012xx available on the EPD website KOK KR KKK kok kok KK I I I A Koko Koko Koko kok k Koko k kok k kok k kok kck ck K kk kkk kkk NAME SDMAHandleInt SmartDMA interrupt DESCRIPTION This function should be activated when SDMA channel 3 generates a transmit terminal count interrupt It calls the interrupt processor and writes to the end of the interrupt register KOKCKCKCKCkCKCkCk kCkCk kCk Ck kCkCk kCkCk Ck kCK Ck kCK Ck kCk Ck kCk Ck kCk Ck kCk CK kCkCK Ck kCk kok k kk k kc k Ck kck ck kck ck kok ck ckck ck kok ck ke kk void interrupt SDMAHandleInt SDMAProcessInt outpw EOI EOITYPE SDMA3 KOK k kk k k kok kok ok kok k kok k oko k kok k kok ok Koko k kok k oko k kk kkk kkk kok k Ck kCkCk kCk A I ck ckck ck kkk k kk NAME SDMAProcessInt Handle a successful transmit on the D endpoint DESCRIPTION This function is called when an entire buffer is transmitted It A 12 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note AMD masks the USB interrupt to avoid being repeate
12. SD2RRCAL WORD address amp Oxfff0 SDRRCAL RRC 16 outpw SD2RRAH WORD address gt gt 16 amp 0x000f 7 1 USB for i 0 i lt DESCRIPTORS i GetLinearAddress data_buf_a i amp address ring_buffer_a i Low_address WORD address amp Oxffff ring_buffer_a i Low_address SDBUF_OWN 0x0300 WORD address gt gt 16 amp 0x000f ring buffer a i Size A BUF SIZE 1 amp Ox7FFF ring buffer a il Message count 0 a_current 0 A 4 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note AMD Set up SDMA Channel 3 SDMA Control register Transmit terminal count interrupt medium priority USB as requesting source outpw SD3CON SDCON TTCI SDCON TXSO SDCON RXSO SDCON P MED SDCON DEL USB GetLinearAddress ring buffer c amp address outpw SD3RRCAL WORD address amp Oxfff0 SDRRCAL RRC 16 outpw SD3RRAH WORD address gt gt 16 amp 0x000f Set up the receive ring descriptor to point to the available data buffers for i 0 i lt DESCRIPTORS itt GetLinearAddress data buf c i amp address ring buffer c i Low address WORD address amp Oxffff ring buffer c il Low address SDBUF OWN 0x0300 WORD address gt gt 16 amp 0x000f ring buffer c i Size C BUF SIZE 1 amp Ox7FFF ring buffer c il
13. UAEPCTL if status amp UEPCTL OTHER ERR The packet was rejected Discard it and wait for the host to retransmit else ring buffer a a current Size A_BUF_SIZE 1 amp Ox7FFF ring buffer a a current Low address low address ring buffer a a current Message count 0 ring buffer a a current Config WORD address gt gt 16 amp 0x000f SDBUF OWN 0x0200 A 8 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note AMD WORD length packet size i char data buf data buf ala current Was the previous packet full length or short We know that full length packets have a total of packet size bytes in them and we can read the PACKET SIZE register to get the size of shorter packets if status amp UEPCTL SHOR PKT packet size inpw UAEPSIZ else packet size PACKET SIZE length GetDiff inpw SD2CRAD ring buffer a a current Low address if length packet size If the amount of data written from the FIFO is longer than the amount of data originally in the FIFO we have a real problem printf errorMn return We need to read the unread data from the FIFO It starts at the length offset into the FIFO and continues to the packet size for i length i lt packet_size i Read the
14. Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note by Mark Langsdorf AMD 1 This application note explains how using the SmartDMA controller with USB enhances the functionality of an Am186 CC microcontroller This application note assumes that the reader is knowledgeable about the Am186CC microcontroller and USB INTRODUCTION The SmartDMA controller provides a powerful and effi cient way to transfer data between memory and the USB data FIFOs on the Am186CC microcontroller Correctly implemented the SmartDMA controller en ables full utilization of the 12 Mbit s full speed bus bandwidth of the 1 0 1 1 and 2 0 USB specifications The most efficient implementation of the SmartDMA controller is the buffer per IRP I O request packet which requires complex software handling either at the end of each buffer or for error recovery This application note discusses the differences be tween buffer per packet and buffer per IRP SmartDMA channels and explains why a system can use either one Also included are an example of how to set up a buffer per IRP BULK interface and an example of error recovery routines for the buffer per IRP BULK inter face The procedures and code in this application note must be combined with EPD CodeKits CK0005xx or CK0012xx to implement a fully functioning USB system using the SmartDMA controller To access the Code Kits refer to the following link on the AMD web s
15. andleInt bProcessInt ndleErrorD void HandleErrorA S void Us a a void HandleReceivedPacketA a a a void H void HandleErrorC ndleReceivedPacketC void HandleOtherInterrupt void SDMAHandleInt void SDMAProcessInt typedef struct SmartDMABufferDescriptorFormat void H WORD Low_address Low order bits of address field WORD Config Status and configuration WORD Size Receive buffer size in bytes WORD Message_count Frame length in bytes SDMADescriptorFormat define DESCRIPTORS 16 Number of descriptors in ring buffer define PACKET_SIZE 64 Size of a USB packet define A BUF SIZE PACKET SIZE Size of endpoint A s buffers define C BUF SIZE 512 Size of endpoint C s buffers char data buf a DESCRIPTORS A BUF SIZE Data buffers for endpoint A volatile SDMADescriptorFormat far ring buffer a DESCRIPTORS These are the rings of format descriptors Each descriptor contains the S a status for a buffer Must be volatile since the hardware has ccess to it char data buf c DESCRIPTORS C BUF SIZE Data buffers for endpoint C volatile SDMADescriptorFormat far ring buffer c DESCRIPTORS These are the rings of format descriptors Each descriptor contains the status for a buffer Must be volatile since the hardware has k k Using
16. ask amp UISTAT1_C_EP_ACT outpw SD3CON sdcon amp SDCON RXST c status inpw UCEPCTL There can either be an error condition or a short packet condition on this endpoint Short packet means the host is done sending the IRP and we need to handle the packet if c status amp UEPCTL BUF ERR c status amp UEPCTL OTHER ERR HandleErrC else if c status amp UEPCTL SHOR PKT HandleReceivedPacketC Now that we ve handled the interrupt clear the act req bit turn interrupts back on and resume outpw UCEPCTL inpw UCEPCTL amp UEPCTL ACT REQ outpw UIMASK1 uimask outpw SD3CON sdcon else Must be a control endpoint or general USB status interrupt HandleOtherInterrupt k k KR k kk kok k kok k kok k oko k kok k kok K kok k kok k kok k Koko k KCKCK Koko kok k Koko Koko k kok ck kok ck kok ck K kok Koko ko kk NAM DESCRIPTION void Handlel m HandleErrorD Handle an error on the D endpoint This function handles an error on the D endpoint If there is a BUFF Gl R underrun error it retransmits the current packet If there is an OTHER NAK error it retransmits the previous packet OK kCkCk kCKCk kCk ck kCkCk kCkCK Ck kCKCk kCKCk kCk k kok k kk k kCk CK Ck kok kk k kk kk kck ck kok ck ckok ck kok ck ke kk ErrorD WORD current address current ri
17. ations intended to support or sustain life or in any other application in which the failure of AMD s product could create a situation where personal injury death or severe property or environmental damage may occur AMD reserves the right to discontinue or make changes to its products at any time without notice A 14 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note
18. dly called on buffer nderrun and turns off the SDMA transmit channel It then checks O See if there are any other buffers that need to be transmitted f there are it points the CBD at the buffer and turns the channel nd interrupts back on The transmit buffers are prepared by the C channel receive function HandleReceivedPacketC KOCKCKCKCKCKCkCk kCkCk kCkCk kCkCk kok Ck kCK Ck kCKCk kCkCk kok Ck kCk Ck A Ck kc k k kk k kc k k kc kck kck ck kck ck kok ck ckok ck kok ck ke ke e ke x void SDMAProcessInt WORD found cbd Check for interrupt source if inpw SD3STAT amp SDSTAT TTC 0 TTC is the only interrupt we should get printf unknown interrupt n return else Clear the interrupt outpw SD3STAT 0 Turn off USB interrupts and SmartDMA transmission outpw UIMASK1 inpw UIMASK1 amp UISTAT1 D EP ACT outpw SD3CON inpw SD3CON amp SDCON TXST Check for any additional buffers that need to be transmitted These buffers have already been prepared by the receiver function cbd inpw SD3CBD amp Ox00ff found FindBuffer ring buffer d cbd SDBUF OWN if found Point the CBD to the found buffer outpw SD3CBD inpw SD3CBD found 1 Turn on SmartDMA transmission outpw SD3CON inpw SD3CON SDCON TXST Clear the USB of any errors outpw UDEPCTL inpw UDEPCTL amp UEPCTL OTHER ERR Turn on USB
19. e moved into the FIFO this might involve back ing up more than one packet size The following is the recovery process 1 Read the SmartDMA channel current transfer ad dress and compare it to the data buffer address The difference is the amount of data already trans mitted 2 Reduce the amount of data transferred by one packet size and then perform an AND operation to the amount transferred with the packet size The re sult is the offset from the start of the data buffer to the start of the NAK response packet 3 Add the packet start offset to the buffer base ad dress and store the sum in the Address word of the buffer descriptor The buffer descriptor now points to the start of the packet that needs to be resent AMD 4 Adjust the Byte Count word to adjust for the short ened buffer length 5 Turn the SmartDMA channel back on The host will send a new IN token and the packet is resent Handling USB BUF ERR on an OUT Endpoint with a SmartDMA Channel The BUF ERR bit in the xEPCTL register is set when the SmartDMA channel is unable to move data from the USB FIFO fast enough The result is the FIFO contains part of the packet which the host has acknowledged and cannot resend and contains part of an overrun packet which the host can resend The error handler must retrieve the rest of the acknowledged packet and update the SmartDMA pointers so that the host can re send the bad packet 1 Read the SmartDMA current r
20. each interrupt With buffer sizes of approxi mately one kilobyte the Am186CC microcontroller can send data at the full bus rate of 12 Mbit s The buffer per IRP method of data transfer is faster and more efficient than buffer per packet because buffer per IRP minimizes the overhead Both methods generate an interrupt after each buffer and the interrupts for each method take the same amount of time to complete The SmartDMA controller cannot transfer data while the processor is handling an interrupt so data transfer is faster if fewer interrupts occur Because the buffer per IRP method generates only one interrupt per IRP and an IRP can contain several packets only one interrupt occurs per several packets which is considerably less than with the buffer per packet method that generates one interrupt per packet Therefore the buffer per IRP method of data transfer is faster and more efficient SETTING UP THE USB AND SmartDMA CONTROLLER The following steps describe how to set up the USB endpoints and the SmartDMA controller on the Am186CC microcontroller Publication 23112 Rev A Amendment 0 Issue Date November 1999 AMD Setting Up the USB Endpoint 1 Only the USB data endpoints can be used with the SmartDMA controller Endpoint A must be config ured for OUT data on SmartDMA Channel 2 re ceive while Endpoint B must be configured for IN data on the SmartDMA Channel 2 transmit End point C must be configured fo
21. eceive address and compare it to the data buffer address The differ ence is the amount of data already received 2 If more than a packet size of data is received per form a modulo on the received amount with the packet size This value is the amount of the ac knowledged packet that is written to memory In a loop the rest of the data must be read from the USB FIFO and written into memory 3 Update the SmartDMA channel descriptor to point to the start of the packet that was in transmit when the error occurred If data was read from the FIFO in step 2 then the pointer does this already 4 Turn the SmartDMA channel back on The host re sends the interrupted packet Handling USB OTHER ERR on an OUT Endpoint with a SmartDMA Channel The OTHER ERR bit is set when the device receives a bad packet The device immediately issues a NAK to the packet and the host automatically resends it The error handler needs to rewind the SmartDMA channel so that the re sent packet overwrites the bad packet data This is done as follows 1 Read the SmartDMA channel current receive ad dress and compare it to the data buffer address The difference is the amount of data already re ceived 2 Reduce the amount of data received by a packet size This is the offset of the start of the bad packet 3 Addthe offset to the start of the SmartDMA channel data buffer and assign the sum to the Address word of the ring descriptor 4 Adjust the Byte Co
22. h to PACKET_SIZE just to be safe length PACKET_SIZE length PACKET SIZE Back up a packet length Bach packet is PACKET SIZE bytes so chopping off the lower bits will give us the offset of the packet start Add that to the beginning of the buffer ring buffer d current ring Low address length amp PACKET SIZE The buffer length has to be decreased by the amount of data already transmitted ring buffer d current ring Size length amp PACKET SIZE Flush the FIFO and clear the error bit outpw UDEPCTL inpw UAEPCTL amp UEPCTL NOT FLUSH UEPCTL OTHER ERR printf unknown interrupt source KOK RK KK kCkCk KCkCk KCkCk kok k oko k kok k Koko Koko k kok k I A A kok k Ck kCkCk kCk kkk k kkk kkk ckckck ko kk NAM DESCRIPTION HandleErrorA Handle an error on the A endpoint This function recovers the data for any buffer with partially written data in it and discards any buffer with bad data in it Kk kk I kCkCk kok k kCkCKCkCkCK Ck kCKCk kCkCk KCk kok Ck kCkCK Ck kCK Ck kCk Ck kk k kk Ck kk k kck ck k kk kk kok ck k kk void HandleErrorA ring buffer a a current Config amp SDBUF_OWN DWORD address WORD low address status GetLinearAddress data buf a a current amp address low addres WORD address amp Oxffff status inpw
23. he SmartDMA channel 6 For receive endpoints turn on the USB ACT REQ interrupt mask 7 For transmit endpoints do not turn on the USB in terrupt mask or the SmartDMA channel interrupt mask until data is available Turning on the USB interrupt mask or the Smart DMA channel interrupt mask before data is avail able can cause unnecessary buffer error interrupts to occur when the FIFO underruns ERROR HANDLING Error Handling in Buffer Per Packet Mode In buffer per packet mode error handling is relatively easy for IN endpoints and OTHER ERRs on OUT end points Each packet is in a separate buffer If an error occurs the bad buffer returns to software control Soft ware clears the error bits in the buffer descriptor resets the addresses to point to the start of the data buffer and retransfers the data Handling a USB BUF ERR on an OUT endpoint is complicated The process is similar to the process required for BUF ERRs on Buffer per IRP mode as described below Error Handling in Buffer Per IRP Mode Error handling is more complex in buffer per IRP mode There are four separate error direction combina tions and each condition is discussed below 2 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note Handling USB BUF ERR on an IN Endpoint With a SmartDMA Channel The BUF ERR bit is set in the USB endpoint control register XEPCTL when a buffer underun occurs the endpoint requests da
24. interrupts outpw UIMASK1 inpw UIMASK1 UISTAT1 D EP ACT Gl I Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note A 13 AMD Trademarks AMD the AMD logo and combinations thereof Am186 and SmartDMA are trademarks of Advanced Micro Devices Inc Product names used in this publication are for identification purposes only and may be trademarks of their respective companies Disclaimer The contents of this document are provided in connection with Advanced Micro Devices Inc AMD products AMD makes no representations or warranties with respect to the accuracy or completeness of the contents of this publication and reserves the right to make changes to specifi cations and product descriptions at any time without notice No license whether express implied arising by estoppel or otherwise to any intel lectual property rights is granted by this publication Except as set forth in AMD s Standard Terms and Conditions of Sale AMD assumes no liability whatsoever and disclaims any express or implied warranty relating to its products including but not limited to the implied warranty of merchantability fitness for a particular purpose or infringement of any intellectual property right AMD s products are not designed intended authorized or warranted for use as components in systems intended for surgical implant into the body or in other applic
25. ite www amd com products Ipd codekits downloads html For more information about the SmartDMA controller and USB refer to the Am186 CC CH CU Microcon trollers User s Manual order 21914 and the Am186 CC CH CU Microcontrollers Register Set Manual order 21916 SmartDMA CONTROLLER MODES When used with a USB the SmartDMA controller on the Am186CC microcontroller can operate in two modes buffer per packet also known as store status and buffer per IRP also Known as no store status These two modes describe the memory buffer that is pointed to by the descriptor in the SmartDMA channel descriptor ring Buffer Per Packet In buffer per packet mode each USB packet has its own descriptor When a packet is transferred the de Copyright 1999 Advanced Micro Devices Inc All rights reserved scriptor is returned to software control by the hardware and the SmartDMA channel pair Current Buffer De scriptor SDxCBD register is automatically updated to point to the next available buffer If an error occurs a NAK is issued to terminate the transaction and soft ware must intervene in some cases to reset the SmartDMA channel Buffer lengths are limited to the size of USB packets Buffer Per IRP In buffer per IRP mode several USB data packets can share the same descriptor The advantage of buffer per IRP is that there is no limit to the size of each buffer So several hundred bytes or more can be transferred between
26. kc k kok k kc k kok kck ck I OR ck ke ke WORD GetLinearAddress void far fptr DWORD address DWORD seg offset seg unsigned DWORD fptr gt gt 16 offset unsigned DWORD fptr amp Oxffff seg lt lt 4 address seg offset return 0 KOR KKK KK kok kok k KK I I kok I A A Ck k kc k kck I OR kok ck ko kk NAME FindBuffer Search for an unused or used buffer DESCRIPTION Kk kok Ck kCk k kCk Ck kok k Ck kCK Ck kCk Ck kCk Ck kCk k kCk Ck k k CK Ck kCK Ck kCk Ck kk k kk k kk kck ck kck ck kck ck ckok ck ckok ck ke ke e ke WORD FindBuffer SDMADescriptorFormat ring buffer WORD active WORD cbd WORD found i A 2 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note AMD found 0 for i cbd i lt DESCRIPTORS amp amp found i if ring buffer i i Config amp SDBUF OWN active found i 1 for i 0 i lt cbd amp amp found i if ring buffer i i Config amp SDBUF OWN active found i 1 return found KOK KR KKK kok kok ok kok k ok k kok k I k kok k A A kCk Ck kCKCk kCk Ck kok k kok k Koko k K I I kk NAME ConfigureUSB Configures USB endpoint A C and D DESCRIPTION Configures each endpoint s definition registers KOKCKCKCKCKCKCkCk kCkCk kCKCk kCk Ck kCkCK Ck kCK Ck kCKCkCkCKCk KCk k kCkCk kCk Ck kCkCk Ck kc k k kok k kk k kc k ck kck ck k kk kk kk kk
27. ng length WORD error cause inpw UDEPCTL current ring inpw SD3CBD amp Ox00ff current address inpw SD3TCAD length GetDiff current address ring buffer d current ring Low address First determine the source of the error if error cause amp UEPCTL BUF ERR BUFF_ERR indicates an error in the current packet We need to point the SDMA to the start of the current packet and resend it Bach packet is PACKET_SIZE bytes so chopping off the lower bits will give us the offset of the packet start Add that to the beginning of the buffer ring buffer d current ring Low address length amp PACKET SIZE 1 The buffer length has decreased by the amount of data Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note A 7 AMD already transmitted ring buffer d current ring Size length amp PACKET SIZE 1 Flush the FIFO and clear the error bit outpw UDEPCTL inpw UAEPCTL amp UEPCTL NOT FLUSH UEPCTL BUF ERR else if error_cause amp UEPCTL_OTHER_ERR OTHER ERR indicates the previous packet was NAK ed so we need to point if lengt the SDMA to the previous packet and resend it h lt PACKET_SIZE No previous packet We shouldn t get this but set the lengt
28. packet was NAK ed so we need to rewind the SDMA so that it will overwrite the previous packet xy Lf lengt h gt PACKET SIZ E Point the write pointer to length the start of the NAK d packet PACKET SIZE ring buffer c c current Low address length amp PACKET SIZE A 10 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note AMD The buffer length has to be decreased by the amount of data already received ring buffer c c current Size length amp PACKET SIZE outpw UCEPCTL inpw UCEPCTL amp UEPCTL NOT FLUSH UEPCTL OTHER ERR printf unknown interrupt source k k kk k k kk kok k kok k ok k kok k kok k kok Koko k kok k A IA Koko KK Koko k KC k kok ck kok ck kk ck K kok kkk k kk NAME HandleReceivedPacketC Handle a successful receive on the C endpoint DESCRIPTION An entire buffer has been received so we need to queue it up for transmit on endpoint D The first step is to find an unused buffer descriptor in the endpoint D ring The handler will continue to be called until a buffer is available We need to set the D descriptor to point to the start of the current C data buffer If the D channel is not currently transmitting it is turned on so it can transmit at the next request of the host Once the buffer has been t
29. r OUT on Channel 3 receive and Endpoint D must be IN on Channel 3 transmit Depending on the application the appro priate endpoint should be chosen 2 The endpoint should be disabled and then defined a Definition register 1 xEPDEF1 controls the endpoint number the USB configuration inter face setting as set by the host during configu ration direction IN or OUT and the type which can be bulk interrupt or isochronous b Definition register 2 XEPDEF2 controls the FIFO size and the maximum packet size The FIFO size is only relevant for IN buffers Unless the NOT ZERO or NOT LAST BYTE bits of the endpoint control register are cleared the USB controller responds to host requests with NAK responses if the FIFO is not completely full c Definition register 3 xEPDEF3 controls the SmartDMA channel mode and operation For a buffer per IRP transfer set the transfer mode to 100b clear all of the IMSK bits and set the BUFF ERR SMSK and OTH ERR SMSK bits These actions cause the endpoint to stop on any error generating an interrupt in the process If the endpoint operates in the OUT direction the SHRT PKT SMSK bit should also be set This action causes the endpoint to stop at the end of the IRP generating an interrupt For buffer per packet transfer set the mode to 101b and set the IMSK bits These actions cause the endpoint to interrupt when an error occurs or a complete packet is received or sent 3 Set
30. ransferred to D s control we need to find another receive buffer KOKCKCKCKCKCKCkCk kCkCk Koko Ck kCkCk kCkCK Ck kCKCkCkCKCk kCk k kCk Ck k kk K kCk Ck kc k Ck kk k kc k k kckck kck ck kok ck kok ck OR ck ke ke void HandleReceivedPacketC Short packets indicate the end of an IRP Process the buffer by passing it to the transmit channel for loopback DWORD address WORD cbd found First find an unused buffer descriptor on endpoint D cbd inpw SD3CBD s Ox00ff found FindBuffer ring buffer d cbd 0 if found Point the D descriptor to the recently received buffer WORD length Get LinearAddress data buf c c current amp address ring buffer d found 1 Low address WORD address amp Oxffff ring buffer d found 1 Config WORD address gt gt 16 amp 0x000f 0x0200 The difference between the data buffer start and the location lso use the 4th word of the descriptor if we were writing tatus which we aren t length GetDiff inpw SD3CRAD ring buffer d found 1 Low address buffer d found 1 Size 0x8000 length 1 buffer d found 1 Config SDBUF OWN Check to see if the SDMA transmitter is active If it isn t make it active and turn on the interrupts Otherwise let th SDMA transfer interrupt handle the buffer SD3CONO amp SDCON TXST 0 T of the last SDMA write is the length of the buffer We co
31. ress gt gt 16 amp 0x000f SDBUF OWN 0x0200 a current outpw SD2CBD amp Oxff00 gt gt 8 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note A 9 AMD KOR RK RK kok kok k kok k ok k oko k kok k kok Koko k kok kk kkk kkk k kkk kkk k Ck kCKCk kCk A ck kck ck kkk ko kk NAME D Handle ESCRIPTION This func If a buffer overrun caused the error a previously ACK ed packet from the FIFO and wri t then updates the SDMA descriptor to point to af data from memory I ErrorC Handle an error on the C endpoint tion recovers from a buffer error on the C endpoi the function pulls a Dt ny tes it to ter the end of the newly written packet and prepares for the host to retransmit the NAK ed packet he function rewinds the SDMA descriptor to point The host will retransmit normally packe wr of the NAK ed packet t If the error was caused by the device NAK ing a to the start KOKCKCKCKCKCKCkCk kCkCk kok k kCkCk kok k Ck kCK Ck kCKCk kCKCk kCk Ck kCk Ck kCkCK kk Ck kc k Ck kok k kc k k kc k ck kck ck kck ck kok ck OR ck kk void Handlel DWORD address WORD error_cause Get LinearAddress da low_address length ErrorC WORD low address inpw UD inpw SD3RCAD EPCT GetDiff low address Check to see wha L ta buf c c current length bytes left amp address ring
32. ta but the SmartDMA controller either cannot distribute the data quickly enough or has no available data This error always occurs when an IN endpoint is activated with SmartDMA channel and there is no data even if the SmartDMA channel is not turned on The interrupt should be masked until data is available Assuming data is available when this error occurs perform the following steps 1 Read the SmartDMA channel current transfer ad dress and compare it to the data buffer address The difference is the amount of data already trans mitted 2 Find the offset from the data buffer base address to the start of the bad packet by removing the low order bits those that are smaller than the packet size from the amount of data transferred 3 Add the packet start offset to the buffer base ad dress and store the sum in the Address word of the buffer descriptor The buffer descriptor now points to the start of the packet that needs to be resent 4 Adjust the byte count word to adjust for the short ened buffer length 5 Turn the SmartDMA channel back on The host sends a new IN token and the packet is re sent Handling USB OTHER ERR on an IN Endpoint with a SmartDMA Channel The OTHER ERR bit in the xEPCTL register is set when the host responds with a NAK to the most re cently transmitted packet The SmartDMA channel transmit pointer must be backed up to the start of the NAK response packet Because part of a new packet might b
33. uld a S Point the CBD to the found buffer outpw SD3CBD inpw SD3CBD found 1 Turn on SmartDMA transmission outpw SD3CON inpw SD3CON SDCON TXST Clear the USB of any errors Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note A 11 AMD outpw UDEPCTL inpw UDEPCTL amp UEPCTL OTHER ERR Turn on USB interrupts outpw UIMASK1 inpw UIMASK1 UISTAT1 D P ACT r Find the next empty receive buffer descriptor found FindBuffer ring_buffer_c c_current SDBUF_OWN if found Return the current buffer to the available pool ring buffer c c current Size C_BUF_SIZE 1 amp Ox7 FFF ring buffer c c current Low address WORD address amp Oxffff ring buffer c c current Config WORD address gt gt 16 amp 0x000f SDBUF OWN 0x0200 Point the SDMA to this buffer so that the next OUT token will write to it c current found 1 outpw SD3CBD inpw SD3CDB c current 8 Clear the short packet bit now that the interrupt is fully handled outpw UCEPCTL inpw UCEPCTL amp UEPCTL SHOR PKT KOR KKK kk kok kok ok kok k ok k oko k I A I RI kkk kk kkk kkk NAME HandleOtherInterrupt Handle non DMA interrupts DESCRIPTION This is a placeholder function for full featured USB interrupt handler K
34. unt field to account for the short ened buffer 5 Restart the SmartDMA channel and wait for the host to resend Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note 3 AMD EXAMPLE CODE Refer to Appendix A for an example of code that shows error recovery procedures for USB with the SmartDMA controller The code is not a fully functioning USB driver and requires a HandleOtherlnt function to process transactions e g initialization configuration over the control endpoint 4 Using the SmartDMA Controller with USB on the Am186 CC Microcontroller Application Note Appendix A AMD Example Code The example code is a simple loopback on endpoints C and D Data can also be sent to the device on endpoint A but the data is discarded This code provides an example of error handling procedures for USB with the SmartDMA controller The code is not a fully functional USB driver This code must be combined with EPD CodeKits CK0005xx or CK0012xx to implement a fully functioning USB system using the SmartDMA controller To access the CodekKits refer to the following link on the AMD web site www amd com products Ipd codekits downloads html finclude Aml86CC h finclude usb h WORD GetDiff WORD numl WORD num2 WORD GetLinearAddress void far fptr DWORD address WORD FindBuffer SDMADescriptorFormat ring buffer WORD active WORD cbd void ConfigureUSB void ConfigureSDMA void UsbH

Download Pdf Manuals

image

Related Search

Related Contents

Manual - Nintendo of Europe  Philips 69190/30/PH  Manuel de l`utilisateur  【88240】U・SA・HA・NA フェルトメーカー  Sanyo Cadnica KR-CH(3.0) User's Manual  

Copyright © All rights reserved.
Failed to retrieve file