STM32F1 USB

Started by tha, April 01, 2021, 07:19:59 AM

Previous topic - Next topic

tha

23 Universal serial bus full-speed device interface (USB)

Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.


Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.


High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.


XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.


Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.

ส่วนนี้ใช้กับ the STM32F103xx performance line และ STM32F102xx USB access line families เท่านั้น.


23.1 USB introduction

The USB peripheral จัดให้มีใช้ an interface ระหว่าง a full-speed USB 2.0 bus และ the APB1 bus.
USB แขวนไว้/ดำเนินการต่อถูกรองรับซึ่งยอมให้หยุด the device clocks เพื่อ low-power consumption

23.2 USB main features

•    USB specification version 2.0 full-speed compliant
•    จำนวนที่กำหนดค่าได้ของ endpoints จาก 1 ถึง 8
•    Cyclic redundancy check (CRC) การสร้าง / การตรวจสอบ , Non-return-to-zero Inverted (NRZI) การเข้ารหัส / ถอดรหัส
      และการบรรจุบิต
•    รองรับ Isochronous transfers
•    รองรับ Double-buffered bulk/isochronous endpoint
•    USB การทำงานแขวนไว้/ดำเนินการต่อ
•    Frame locked clock pulse generation

ใน low, medium, high และ XL-density devices, the USB และ CAN แชร์ a dedicated 512-byte SRAM memory สำหรับ data transmission and reception, และดังนั้นพวกมันไม่สามารถถูกใช้ได้พร้อมกัน (the shared RAM ถูกเข้าถึงผ่านทาง CAN and USB อย่างกีดกัน). The USB and CAN สามารถถูกใช้ใน the same application แต่ไม่ในเวลาเดียวกัน.

23.3 USB functional description

Figure 220 แสดง the block diagram ของ the USB peripheral.



The USB peripheral จัดให้มี an USB compliant connection ระหว่าง the host PC และ the function ที่มีให้ใช้โดย the microcontroller. Data transfer ระหว่าง the host PC และ the system memory เกิดขึ้นผ่านทาง a dedicated packet buffer memory ถูกเข้าถึงโดยตรงโดย the USB peripheral. ขนาดของ this dedicated buffer memory ต้องสอดคล้องกับจำนวนของ endpoints ที่ใช้และ the maximum packet size. dedicated memory นี้มีขนาดถึง 512 bytes และมากถึง 16 mono-directional หรือ 8 bidirectional endpoints สามารถถูกใช้.The USB peripheral เชื่อมต่อกับ the USB host, การตรวจจับ token packets, การรับมือ data transmission/reception, และการประมวลผล handshake packets ตามที่ถูกต้องการโดย the USB standard. Transaction formatting ถูกดำเนินการโดย the hardware, รวมถึง CRC generation และการตรวจสอบ.

tha

แต่ละ endpoint ที่เกี่ยวข้องกับ a buffer description block แสดงให้เห็นที่ๆ the endpoint related memory area ตั้งอยู่, มันใหญ่เท่าใดหรือ bytes จำนวนเท่าใดที่ต้องถูกส่ง. เมื่อ a token สำหรับ a valid function/endpoint pair ถูกรับรู้จาก the USB peripheral, the related data transfer (ถ้าต้องการและถ้า the endpoint ถูกกำหนดค่า) จะเกิดขึ้น. The data ที่บัฟเฟอร์โดย the USB peripheral จะถูกโหลดใน an internal 16 bit register และ memory เข้าถึงไปยัง the dedicated buffer ถูกดำเนินการ. เมื่อ the data ทั้งหมดถูกส่งถ่ายแล้ว, ถ้าจำเป็น, the proper handshake packet over the USB ถูกสร้างหรือคาดว่าจะสอดคล้องกับ the direction of the transfer

tha

ที่จุดสิ้นสุดของ the transaction, an endpoint-specific interrupt ถูกสร้างขึ้น, การอ่าน status registers และ/หรือการใช้ different interrupt response routines. The microcontroller สามารถกำหนด :
•    the endpoint ที่จะถูกบริการ
•    ประเภทของ transaction ที่จะเกิดขึ้น, ถ้า errors (อย่างเช่น bit stuffing, format, CRC, protocol, missing ACK,
      over/underrun) เกิดขึ้น.

การรองรับพิเศษถูกเสนอถึง Isochronous transfers และ high throughput bulk transfers, การจัดให้มีใช้ a double buffer usage, ซึ่งยอมให้มี an available buffer เสมอสำหรับ the USB peripheral ในขณะที่ the microcontroller ใช้อีกอันหนึ่ง.

The unit สามารถถูกวางใน low-power mode (SUSPEND mode), โดยการเขียนใน the control register, เมื่อไรก็ตามที่จำเป็น. ที่เวลานี้, static power dissipation ทั้งหมดถูกหลีกเลี่ยง, และ the USB clock สามารถถูกทำให้ช้าลงหรือหยุด. การตรวจจับกิจกรรมที่ the USB inputs, ในขณะใน low-power mode, จะปลุก the device ขึ้นแบบ asynchronously. A special interrupt source สามารถถูกต่อโดยตรงถึง a wakeup line เพื่อยอมให้ the system รีสตาร์ทโดยทันที the normal clock generation และ/หรือรองรับ direct clock start/stop.


tha

23.3.1 Description of USB blocks

The USB peripheral จัดให้มีใช้คุณลักษณะทั้งหมดที่เกี่ยวกับ USB interfacing, ซึ่งรวมถึง blocks ต่อไปนี้ :
•    Serial Interface Engine (SIE) : หน้าที่ของ block นี้รวมถึง : synchronization pattern recognition, bit-stuffing,
      CRC generation and checking, PID verification/generation, and handshake evaluation. มันต้องอินเตอร์เฟสกับ the
      USB transceivers และใช้ the virtual buffers ที่จัดให้มีโดย the packet buffer interface สำหรับ local data storage,
      unit นี้สร้าง signals ที่สอดคล้องกับ  USB peripheral events อีกด้วย, อย่างเช่น Start of Frame (SOF), USB_Reset, Data
      errors etc. และไปยัง Endpoint related events อย่าง end of transmission หรือ correct reception of a packet;
      signals เหล่านี้ดังนั้นถูกใช้เพื่อสร้าง interrupts
•    Timer : block นี้สร้าง a start-of-frame locked clock pulse และตรวจจับ a global suspend (จาก the host) เมื่อ
      ไม่มี traffic ถูกรับมาเป็นเวลา 3 ms.
•    Packet Buffer Interface : block นี้จัดการ the local memory ที่จัดให้มีใช้ a set of buffers ใน a flexible way, ทั้ง
      สำหรับ transmission และ reception. มันสามารถเลือก the proper buffer สอดคล้องกับการร้องขอที่มาจาก the SIE และให้
      ตำแหน่งพวกมันใน the memory addresses ที่ถูกชี้โดย the Endpoint registers. มันเพิ่ม the address หลังจากแต่ละ
      exchanged word จนกระทั่ง the end of packet, ติดตามจำนวนของ exchanged bytes และป้องกันไม่ให้ the buffer ทำงาน
      เกิน the maximum capacity.
•    Endpoint-Related Registers : แต่ละ endpoint มี an associated register ที่มี the endpoint type และ status
      ปัจจุบันของมัน. สำหรับ mono-directional/single-buffer endpoints, a single register สามารถถูกใช้เพื่อจัดให้มีใช้ two
      distinct endpoints. จำนวนของ registers คือ 8, ยอมให้สูงถึง 16 mono-directional/single-buffer หรือสูงถึง 7 double-
      buffer endpoints(a) ในการผสมใดก็ได้. ตัวอย่างเช่น the USB peripheral สามารถถูกโปรแกรมให้มี 4 double buffer
      endpoints และ 8 single-buffer/mono-directional endpoints
•    Control Registers: เหล่านี้คือ the registers ที่มี information เกี่ยวกับ the status ของ the whole USB peripheral
      และถูกใช้เพื่อบังคับบาง USB events, อย่างเช่น resume และ power-down.
•    Interrupt Registers : เหล่านี้บรรจุ the Interrupt masks และบันทึกของ the events. พวกมันสามารถถูกใช้เพื่อสอบถาม
      an interrupt reason, the interrupt status หรือเพื่อเคลียร์ the status of a pending interrupt.

a. Endpoint 0 ถูกใช้เสมอสำหรับ control transfer ใน single-buffer mode.

tha

The USB peripheral ถูกต่อถึง the APB1 bus ผ่านทาง an APB1 interface, บรรจุ blocks ต่อไปนี้:
•    Packet Memory: นี้คือ the local memory ที่ในทางกายภาพบรรจุ the Packet Buffers. มันสามารถถูกใช้โดย the
      Packet Buffer interface, ซึ่งสร้าง the data structure และสามารถถูกเข้าถึงโดยตรงโดย the application software. ขนาด
      ของ the Packet Memory คือ 512 bytes, มีโครงสร้างเป็น 256 words คุณ 16 bits.
•    Arbiter: block นี้ยอมรับ memory requests ที่มาจาก the APB1 bus และจาก the USB interface. มันแก้ปัญหาความ
      ขัดแย้งโดยการให้ priority กับการเข้าถึง APB1, ในขณะที่สงวนครึ่งหนึ่งของ the memory bandwidth ไว้เสมอเพื่อเสร็จสมบูรณ์
      USB transfers ทั้งหมด. time-duplex scheme นี้จัดให้มีใช้ a virtual dual-port SRAM ที่ยอมให้เข้าถึง memory , ในขณะที่
      an USB transaction กำลังเกิดขึ้น. Multiword APB1 transfers ที่มีความยาวเท่าใดก็ได้ถูกยอมให้โดยแบบแผนนี้อีกด้วย.
•    Register Mapper: block นี้รวบรวม the various byte-wide และ bit-wide registers ของ the USB peripheral ใน
      a structured 16-bit wide word set ที่ให้ที่อยู่โดย the APB1.
•    APB1 Wrapper: นี้จัดให้มี an interface ไปยัง the APB1 สำหรับ the memory และ register. มันยังแม็ป the whole
      USB peripheral ใน the APB1 address space.
•    Interrupt Mapper: block นี้ถูกใช้เพื่อเลือกอย่างไรที่ the possible USB events สามารถสร้าง interrupts และแม็ปพวก
      มันถึง three different lines ของ the NVIC:
      –    USB low-priority interrupt (Channel 20): ถูกทริกโดย USB events ทั้งหมด (Correct transfer, USB reset, etc.).
            The firmware ต้องตรวจสอบ the interrupt source ก่อนให้บริการ the interrupt.
      –    USB high-priority interrupt (Channel 19): ถูกทริกเฉพาะโดย a correct transfer event สำหรับ isochronous และ
            double-buffer bulk transfer เพื่อให้ถึง the highest possible transfer rate.
      –    USB wakeup interrupt (Channel 42): ถูกทริกโดย the wakeup event จาก the USB Suspend mode.

tha

23.4.2 System and power-on reset

เมื่อ system and power-on reset, การทำงานอันดับแรก the application software ควรดำเนินการคือจัดให้มี all required clock signals ไปยัง the USB peripheral และจากนั้นจึงยกเลิกการยืนยัน reset signal ของมันเพื่อให้สามารถเข้าถึง registers ของมัน. ลำดับการเริ่มต้นทั้งหมดถูกอธิบายต่อจากนี้.

ตามที่ a first step application software จำเป็นเพื่อทำให้ทำงาน register macrocell clock และยกเลิกการยืนยัน macrocell specific reset signal โดยใช้ related control bits ที่จัดให้มีโดย device clock management logic.

หลังจากนั้น, the analog part ของ the device ที่เกี่ยวข้องกับ the USB transceiver ต้องถูก switched on โดยใช้ the PDWN bit ใน CNTR register, ซึ่งต้องการการจัดการพิเศษ. บิตนี้มีไว้เพื่อเปิด the internal voltage references ที่จ่ายไฟ the port transceiver. วงจรนี้มี a defined startup time (tSTARTUP ที่ระบุใน the datasheet) ในระหว่างที่พฤติกรรมของ the USB transceiver ไม่ได้ถูกกำหนด. ดังนั้นจึงจำเป็นต้องรอเวลานี้, หลังจากการเซ็ต the PDWN bit ใน the CNTR register, ก่อนการเอาออก the reset condition บน the USB part (โดยการเคลียร์ the FRES bit ใน the CNTR register). การเคลียร์ the ISTR register ดังนั้นจะเอาออก any spurious pending interrupt ก่อน macrocell operation อื่นใดๆจะถูกเปิดการใช้งาน.

At system reset, the microcontroller ต้องเริ่มต้นทุก required registers และ the packet buffer description table, เพื่อทำให้ the USB peripheral สามารถ interrupts และ data transfers อย่างเหมาะสม. ทุก registers ที่ไม่เฉพาะเจาะจงกับ endpoint ใดๆต้องถูกเริ่มต้นให้สอดคล้องกับความต้องการของ application software (ทางเลือกของ interrupts ที่ถูกเปิดการใช้งาน, address ที่เลือกของ packet buffers, ฯลฯ). จากนั้น the process ดำเนินการต่อไปเหมือนกับสำหรับ the USB reset case (ดูย่อหน้าต่อไป).

tha

USB reset (RESET interrupt)

เมื่อ event นี้เกิดขึ้น, the USB peripheral ถูกใส่ใน the same conditions ที่มันถูกทิ้งโดย the system reset หลังจากการเริ่มต้นที่อธิบายในย่อหน้าก่อนหน้า : communication ถูกปิดการใช้งานในทุก endpoint registers (the USB peripheral จะไม่ตอบสนองต่อ packet ใดๆ). ตามที่ตอบสนองต่อ the USB reset event, the USB function ต้องถูกเปิดการใช้งาน, โดยมีเป็น USB address 0, จักให้มีใช้เฉพาะ the default control endpoint (endpoint address เป็น 0 ด้วย). นี้ถูกทำให้สำเร็จลงโดยการเซ็ต the Enable Function (EF) bit ของ the USB_DADDR register และการเตรียม the EP0R register และ related packet buffers ของมันสอดคล้องกัน. ในระหว่าง USB enumeration process, the host กำหนด a unique address ถึง device นี้, ซึ่งต้องถูกเขียนใน the ADD[6:0] bits ของ the USB_DADDR register, และกำหนดค่า endpoint ที่จำเป็นอื่นใดๆ.

เมื่อ a RESET interrupt ถูกรับ, the application software ตอบสนองโดยเปิดการใช้งานอีกครั้ง the default endpoint of USB function 0 ภายใน 10mS จากจุดสิ้นสุดของ reset sequence ซึ่งทริก the interrupt.

tha

Structure and usage of packet buffers

แต่ละ bidirectional endpoint อาจ receive หรือ transmit data จาก/ถึง the host. The received data ถูกเก็บใน a dedicated memory buffer ที่ถูกสงวนไว้สำหรับ endpoint นั้น, ในขณะที่ memory buffer อื่นบรรจุ the data ที่จะถูกส่งโดย the endpoint. เข้าถึงไปยัง memory นี้ถูกดำเนินการโดย the packet buffer interface block, ซึ่งจะส่ง a memory access request และรอสำหรับ acknowledgement ของมัน. เนื่องจาก the packet buffer memory ต้องถูกเข้าถึงโดย the microcontroller อีกด้วย, an arbitration logic จะดูแล the access conflicts, โดยใช้ครึ่งหนึ่ง  APB1 cycle สำหรับ microcontroller access และครึ่งหนึ่งที่เหลือสำหรับ the USB peripheral access. ด้วยวิธีนี้, ทั้งสอง the agents สามารถทำงานราวกับว่า the packet memory เป็น a dual-port SRAM, โดยไม่ต้องตระหนักถึง conflict ใดๆแม้ในขณะที่ the microcontroller กำลังดำเนินการ back-to-back accesses. The USB peripheral logic ใช้ a dedicated clock. ความถี่ของ dedicated clock นี้ถูกคงที่โดยความต้องการของ the USB standard ที่ 48 MHz, และนี้สามารถแตกต่างจาก the clock ที่ถูกใช้สำหรับ the interface ไปยัง the APB1 bus. Different clock configurations เป็นไปได้ที่ the APB1 clock frequency สามารถสูงกว่าหรือต่ำกว่า the USB peripheral.

Note: เนื่องจาก USB data rate และ packet memory interface requirements, the APB1 clock frequency ต้องใหญ่กว่า 8
           MHz เพื่อหลีกเลี่ยงปัญหา data overrun/underrun.