Electoday 2025

ไมโครคอนโทรลเลอร์ => ARM Processors => Topic started by: tha on December 16, 2020, 01:16:43 PM

Title: STM32F1 SPI
Post by: tha on December 16, 2020, 01:16:43 PM
25 Serial peripheral interface (SPI)
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.

25.1 SPI introduction
ใน high-density, XL-density และ connectivity line devices, the SPI interface จัดให้มี two main functions, รองรับอย่างใดอย่างหนึ่ง the SPI protocol หรือ the I2S audio protocol. โดยค่าเริ่มต้น, คือ SPI function ที่ถูกเลือก. มันเป็นไปได้ที่จะสวิทช์ the interface จาก SPI ไปยัง I2S โดย software.
ใน Cat.1 และ Cat.2 devices, the I2S protocol ไม่มีให้ใช้งาน.

The serial peripheral interface (SPI) ยอมให้ half/ full-duplex, synchronous, serial communication กับ external devices. The interface สามารถถูกกำหนดค่าเป็น the master และในกรณีนี้มันจัดให้มี the communication clock (SCK) ไปยัง the external slave device. The interface ยังสามารถทำงานใน multimaster configuration.

มันอาจใช้เพื่อวัตถุประสงค์หลายประการ, รวมถึง simplex synchronous transfers บน two lines ด้วย a possible bidirectional data line หรือ reliable communication โดยใช้ CRC checking.

The I2S เป็น a synchronous serial communication interface อีกด้วย. มันสามารถจัดการกับ four different audio standards ประกอบด้วย the I2S Philips standard, the MSB- and LSB-justified standards, และ the PCM standard. มันสามารถทำงานเป็น a slave หรือ a master device ใน full-duplex mode (โดยใช้ 4 pins) หรือใน half-duplex mode (โดย 6 pins). Master clock สามารถถูกจัดให้มีโดย the interface ไปยัง an external slave component เมื่อ the I2S ถูกกำหนดค่าเป็น the communication master.

คำเตือน : เนื่องจากบาง SPI3/I2S3 pins ถูกใช้ร่วมกันกับ JTAG pins (SPI3_NSS/I2S3_WS ด้วย JTDI และ SPI3_SCK/I2S3_CK
             ด้วย JTDO), มันไม่ถูกควบคุมโดย the IO controller และถูกสงวนไว้สำหรับ JTAG usage (หลังจากแต่ละ Reset). สำหรับจุด
             มุ่งหมายนี้, ก่อนที่จะกำหนดค่า the SPI3/I2S3 pins, ผู้ใช้ต้องปิดการใช้งาน the JTAG และใช้ the SWD interface (เมื่อ
             ทำการดีบัก the application), หรือปิดการใช้งานทั้ง JTAG/SWD interfaces (สำหรับ standalone applications). สำหรับ
             ข้อมูกที่มากขึ้นบน the configuration ของ JTAG/SWD interface pins อ้างอิงถึง Section 9.3.5: JTAG/SWD
             alternate function remapping.
Title: Re: STM32F1 SPI
Post by: tha on December 17, 2020, 07:41:30 AM
25.2 SPI and I2S main features

25.2.1 SPI features
•    Full-duplex synchronous transfers บน three lines
•    Simplex synchronous transfers บน two lines พร้อมด้วยหรือปราศจาก a bidirectional data line
•    8- or 16-bit transfer frame format selection
•    Master or slave operation
•    Multimaster mode capability
•    8 master mode baud rate prescalers (fPCLK/2 max.)
•    Slave mode frequency (fPCLK/2 max.)
•    Faster communication สำหรับทั้ง master and slave
•    NSS management โดย hardware หรือ software สำหรับทั้ง master and slave: dynamic change of master/slave
      operations
•    Programmable clock polarity and phase
•    Programmable data order ด้วย MSB-first or LSB-first shifting
•    Dedicated transmission and reception flags with interrupt capability
•    SPI bus busy status flag
•    Hardware CRC feature for reliable communication:
      – CRC value สามารถถูกส่งถ่ายเป็น last byte ใน Tx mode
      – Automatic CRC error checking for last received byte
•    Master mode fault, overrun and CRC error flags with interrupt capability
•    1-byte transmission and reception buffer with DMA capability: Tx and Rx requests
Title: Re: STM32F1 SPI
Post by: tha on December 17, 2020, 09:13:10 AM
25.2.2 I2S features
•    Half-duplex communication (เพียง transmitter หรือ receiver)
•    Master or slave operations
•    8-bit programmable linear prescaler เพื่อถึง accurate audio sample frequencies (from
      8 kHz to 192 kHz)
•    Data format อาจจะ 16-bit, 24-bit or 32-bit
•    Packet frame ถูกคงที่เป็น 16-bit (16-bit data frame) หรือ 32-bit (16-bit, 24-bit, 32-bit data frame) by audio
      channel
•    Programmable clock polarity (steady state)
•    Underrun flag in slave transmission mode and Overrun flag in reception mode (master
      and slave)
•    16-bit register for transmission and reception with one data register for both channel
      sides
•    Supported I2S protocols:
      – I2S Phillps standard
      – MSB-justified standard (left-justified)
      – LSB-justified standard (right-justified)
      – PCM standard (with short and long frame synchronization on 16-bit channel frame
         or 16-bit data frame extended to 32-bit channel frame)
•    Data direction is always MSB first
•    DMA capability for transmission and reception (16-bit wide)
•    Master clock อาจถูกส่งออกเพื่อขับ an external audio component. Ratio ถูกคงที่ที่ 256 × FS (where FS is the audio
      sampling frequency)
•    In connectivity line devices, both I2S (I2S2 and I2S3) have a dedicated PLL (PLL3) to
     generate an even more accurate clock.
Title: Re: STM32F1 SPI
Post by: tha on December 17, 2020, 10:46:34 AM
25.3 SPI functional description

25.3.1 General description
The block diagram ของ the SPI ถูกแสดงใน Figure 238.

(https://i.imgur.com/g8WvToY.png)

ปกติแล้ว, the SPI ถูกต่อถึง external devices ผ่านทาง four pins:
•    MISO: Master In / Slave Out data. ขานี้สามารถถูกใช้เพื่อส่ง data ใน slave mode และรับ data ใน master mode.
•    MOSI: Master Out / Slave In data. ขานี้สามารถถูกใช้เพื่อส่ง data ใน master mode และรับ data ใน slave mode.
•    SCK: Serial Clock output สำหรับr SPI masters และ input สำหรับ SPI slaves.
•    NSS: Slave select. นี้เป็นขาทางเลือกเพื่อเลือก a slave device. ขานี้ทำงานเป็น a 'chip select' เพื่อให้ the SPI master
      ติดต่อกับ slaves ได้เฉพาะรายและเพื่อหลีกเลี่ยงความขัดแย้งบน the data lines. Slave NSS inputs สามารถถูกขับโดย standard
      IO ports บน the master device. The NSS pin อาจถูกใช้เป็น an output ได้อีกด้วยถ้าเปิดการใช้งาน (SSOE bit) และขับลง
      low ถ้า the SPI อยู่ใน master configuration. ในลักษณะนี้, ทุก NSS pins จาก devices ที่ต่อถึง the Master NSS pin เห็น a
      low level และกลายเป็น slaves เมื่อพวกมันถูกกำหนดค่าใน NSS hardware mode. เมื่อกำหนดค่าใน master mode ด้วย NSS
      ที่กำหนดค่าเป็น an input (MSTR=1 and SSOE=0) และถ้า NSS ถูกดึงลง low, the SPI จะเข้าสู่ the master mode fault
      state: the MSTR bit จะถูกเคลียร์โดยอัตโนมัติและ the device ถูกกำหนดค่าใน slave mode (อ้างอิงถึง Section 25.3.10).

ตัวอย่างพื้นฐานของ interconnections ระหว่าง a single master และ a single slave ถูกแสดงใน Figure 239.

(https://i.imgur.com/rQkm5TJ.png)

The MOSI pins ถูกต่อเข้าด้วยกันและ the MISO pins ถูกต่อเข้าด้วยกัน. ในวิธีนี้ data ถูกส่งถ่ายแบบอนุกรมระหว่าง master และ slave (most significant bit first).

The communication ถูกเริ่มต้นโดย the master เสมอ. เมื่อ the master device ส่ง data ไปยัง a slave device ทาง the MOSI pin, the slave device ตอบสนองทาง the MISO pin. นี้หมายถึง full-duplex communication ด้วยทั้ง data out และ data in จะประสานกันด้วย the same clock signal (ซึ่งถูกจัดให้มีโดย the master device ทาง the SCK pin).
Title: Re: STM32F1 SPI
Post by: tha on December 17, 2020, 12:07:10 PM
Slave select (NSS) pin management
Hardware or software slave select management สามารถถูกเซ็ตโดยใช้ the SSM bit ใน the SPI_CR1 register.
•    Software NSS management (SSM = 1)
      The slave select information ถูกขับภายในโดยค่าของ the SSI bit ใน the SPI_CR1 register. The external NSS pin ยัง
      คงเป็นอิสระสำหรับการใช้โดย application อื่น.
•    Hardware NSS management (SSM = 0)
      Two configurations ที่เป็นได้ขึ้นอยู่กับ the NSS output configuration (SSOE bit ใน register SPI_CR2).
      – NSS output enabled (SSM = 0, SSOE = 1)
         configuration นี้ถูกใช้ได้เพียงเมื่อ the device ทำงานใน master mode. The NSS signal ถูกขับลง low เมื่อ the master
         สตาร์ท the communication และจะเป็น low จนกระทั่งl the SPI ถูกปิดการใช้งาน.
      – NSS output disabled (SSM = 0, SSOE = 0)
         configuration นี้ยอมให้ multimaster capability สำหรับ devices ทำงานใน master mode. สำหรับ devices ที่เซ็ตเป็น
         slave, the NSS pin ทำงานเป็น a classical NSS input: the slave ถูกเลือกเมื่อ NSS เป็น low และไม่ถูกเลือกเมื่อ NSS
         high.
Title: Re: STM32F1 SPI
Post by: tha on December 18, 2020, 09:37:43 AM
Clock phase and clock polarity
Four possible timing relationships อาจถูกเลือกโดย software, โดยใช้ the CPOL และ CPHA bits ใน the SPI_CR1 register. The CPOL (clock polarity) bit ควบคุม the steady state value ของ the clock เมื่อไม่มี data ถูกส่งถ่าย. บิตนี้มีผลต่อทั้ง master และ slave modes. ถ้า CPOL ถูก reset, the SCK pin มี a low-level idle state. ถ้า CPOL ถูก set, the SCK pin มี a high-level idle state.

ถ้า the CPHA (clock phase) bit ถูก set, the second edge บน the SCK pin (falling edge ถ้า the CPOL bit ถูก reset, rising edge ถ้า the CPOL bit ถูก set) คือ the MSBit capture strobe. Data ถูก latched บนการเกิดขึ้นของ the second clock transition. ถ้า the CPHA bit ถูก reset, the first edge บน the SCK pin (falling edge ถ้า CPOL bit ถูก set, rising edge ถ้า CPOL bit ถูก reset) คือ the MSBit capture strobe. Data ถูก latched บนการเกิดขึ้นของ the first clock transition.

The combination ของ the CPOL (clock polarity) และ CPHA (clock phase) bits เลือก the data capture clock edge.

Figure 240, แสดง an SPI transfer ด้วย the four combinations ของ the CPHA และ CPOL bits. The diagram อาจถูกตีความได้ว่าเป็น a master หรือ slave timing diagram เมื่อ the SCK pin, the MISO pin, the MOSI pin ถูกต่อโดยตรงระหว่าง the master และ the slave device

Note: ก่อนการเปลี่ยน the CPOL/CPHA bits the SPI ต้องถูกปิดการใช้งานโดยการรีเซ็ต the SPE bit. Master และ slave
            ต้องถูกโปรแกรมด้วย the same timing mode.

            The idle state ของ SCK ต้องตรงกันกับ the polarity ที่เลือกใน the SPI_CR1 register (โดย pulling up SCK ถ้า
            CPOL=1 หรือ pulling down SCK ถ้า CPOL=0).

            The Data Frame Format (8- or 16-bit) ถูกเลือกผ่านทาง the DFF bit ใน SPI_CR1 register, และกำหนด the data
             length ในระหว่าง transmission/reception.

(https://i.imgur.com/Q8bFMyP.png)

Data frame format
Data สามารถถูก shifted out อย่างใดอย่างหนึ่ง MSB-first หรือ LSB-first ขึ้นอยู่กับค่าของ the LSBFIRST bit ใน the SPI_CR1 Register.

แต่ละ data frame เป็น 8 หรือ 16 bits long ขึ้นอยู่กับขนาดของ the data ที่โปรแกรมโดยใช้ the DFF bit ใน the SPI_CR1 register. The selected data frame format ใช้ได้สำหรับ transmission และ/หรือ reception.
Title: Re: STM32F1 SPI
Post by: tha on December 19, 2020, 07:06:53 AM
25.3.2 Configuring the SPI in slave mode
ใน the slave configuration, the serial clock ถูกรับบน the SCK pin จาก the master device. The value ที่เซ็ตใน the BR[2:0] bits ใน the SPI_CR1 register, ไม่มีผลต่อ the data transfer rate.

Note: ขอแนะนำให้เปิดการใช้งาน the SPI slave ก่อนที่ the master จะส่ง the clock. ถ้าไม่, undesired data transmission อาจ
         เกิดขึ้น. The data register ของ the slave จำเป็นที่จะพร้อมก่อน the first edge ของ the communication clock หรือก่อน
         the end ของ the ongoing communication. มันมีความจำเป็นต้องมี the polarity ของ the communication clock ที่เซ็ต
         เป็น the steady state value ก่อน the slave และ the master ถูก enabled.

ทำตามขั้นตอนด้านล่างเพื่อกำหนดค่า SPI ใน slave mode:

Procedure

1.    เซ็ต the DFF bit เพื่อกำหนด 8- หรือ 16-bit data frame format
2.    เลือก the CPOL and CPHA bits เพื่อกำหนดหนึ่งของ the four relationships ระหว่าง the data transfer และ the serial
       clock (ดู Figure 240). เพื่อ correct data transfer, the CPOL and CPHA bits ต้องถูกกำหนดค่าใน the same way ใน the
       slave device and the master device.
3.    The frame format (MSB-first หรือ LSB-first ขึ้นอยู่กับ the value ของ the LSBFIRST bit ใน the SPI_CR1 register)
       ต้องทำให้เหมือนกันกับ the master device.
4.    ใน Hardware mode (อ้างอิงถึง Slave select (NSS) pin management), the NSS pin ต้องถูกต่อถึง a low level signal
       ในระหว่าง the complete byte transmit sequence. ใน NSS software mode, เซ็ต the SSM bit และเคลียร์ the SSI bit
       ใน the SPI_CR1 register.

ในการกำหนดค่านี้ the MOSI pin เป็น a data input และ the MISO pin เป็น a data output.

Transmit sequence

The data byte ถูก parallel-loaded ลงใน the Tx buffer ในระหว่าง a write cycle.

The transmit sequence เริ่มเมื่อ the slave device รับ the clock signal และ the most significant bit ของ the data บน MOSI pin ของมัน. บิตที่เหลือ (the 7 bits ใน 8-bit data frame format, และ the 15 bits ใน 16-bit data frame format) ถูกโหลดลงใน the shift-register. The TXE flag ใน the SPI_SR register ถูกเซ็ตบน the transfer ของ data จาก the Tx Buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต.

Receive sequence

สำหรับ the receiver, เมื่อ data transfer เสร็จสมบูรณ์:
•    The Data ใน shift register ถูก transferred ไปยัง Rx Buffer และ the RXNE flag (SPI_SR register) ถูกเซ็ต
•    An Interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน the SPI_CR2 register

หลังจาก the last sampling clock edge the RXNE bit ถูกเซ็ต, a copy ของ the data byte ที่รับใน the shift register ถูกย้ายไปยัง the Rx buffer. เมื่อ the SPI_DR register ถูกอ่าน, the SPI peripheral จะส่งคืนค่า buffered นี้.

การเคลียร์ของ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register.
Title: Re: STM32F1 SPI
Post by: tha on December 19, 2020, 09:31:45 AM
25.3.3 Configuring the SPI in master mode

ใน the master configuration, the serial clock ถูกสร้างขึ้นบน the SCK pin.

Procedure

1.    เลือก the BR[2:0] bits เพื่อกำหนด the serial clock baud rate (ดู SPI_CR1 register).
2.    เลือก the CPOL and CPHA bits เพื่อกำหนดหนึ่งของ the four relationships ระหว่าง the data transfer และ the serial
       clock (ดู Figure 240).
3.    เซ็ต the DFF bit เพื่อกำหนด 8- หรือ 16-bit data frame format
4.    กำหนดค่า the LSBFIRST bit ใน the SPI_CR1 register เพื่อกำหนด the frame format.
5.    ถ้า the NSS pin ถูกต้องการใน input mode, ใน hardware mode, ต่อ the NSS pin ถึง a high-level signal ในระหว่าง the
       complete byte transmit sequence. ใน NSS software mode, เซ็ต the SSM และ SSI bits ใน the SPI_CR1 register.
       ถ้า the NSS pin ถูกต้องการใน output mode, the SSOE bit เพียงอย่างเดียวที่ควรถูกเซ็ต.
6.    The MSTR and SPE bits ต้องถูกเซ็ต (พวกมันยังคงเซ็ตเพียงถ้า the NSS pin ถูกต่อถึง a high-level signal).

ในการกำหนดค่านี้ the MOSI pin เป็น a data output และ the MISO pin เป็น a data input.

Transmit sequence

The transmit sequence เริ่มเมื่อ a byte ถูกเขียนใน the Tx Buffer.

The data byte ถูก parallel-loaded ลงใน the shift register (จาก the internal bus) ในระหว่าง the first bit transmission และจากนั้นเลื่อนออกไปตามลำดับไปที่ the MOSI pin MSB first หรือ LSB first ขึ้นอยู่กับ the LSBFIRST bit ใน the SPI_CR1 register. The TXE flag ถูกเซ็ตบน the transfer ของ data จาก the Tx Buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต.

Receive sequence

สำหรับ the receiver, เมื่อ data transfer เสร็จสมบูรณ์:
•    The Data ใน shift register ถูก transferred ไปยัง Rx Buffer และ the RXNE flag (SPI_SR register) ถูกเซ็ต
•    An Interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน the SPI_CR2 register

ที่ the last sampling clock edge the RXNE bit ถูกเซ็ต, a copy ของ the data byte ที่รับใน the shift register ถูกย้ายไปยัง the Rx buffer. เมื่อ the SPI_DR register ถูกอ่าน, the SPI peripheral จะส่งคืนค่า buffered นี้.

การเคลียร์ของ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register.

A continuous transmit stream สามารถถูกรักษาไว้ถ้า the next data ที่จะส่งถ่ายถูกใส่ลงใน the Tx buffer ทันใดนั้น the transmission ถูกสตาร์ท. โปรดจำเอาไว้ว่า TXE flag ควรเป็น '1 ก่อนความพยายามใดๆที่จะเขียน the Tx buffer ถูกทำ.

Note: เมื่อ a master กำลังสื่อสารกับ SPI slaves ซึ่งจำเป็นต้องยกเลิกการเลือกระหว่าง transmissions, the NSS pin ต้องถูกกำหนด
         ค่าเป็น GPIO หรือ GPIO อื่นต้องถูกใช้และสลับโดย software.
Title: Re: STM32F1 SPI
Post by: tha on December 20, 2020, 06:27:28 AM
25.3.4 Configuring the SPI for half-duplex communication

The SPI มีความสามารถของการทำงานใน half-duplex mode ใน 2 configurations.
•    1 clock และ 1 bidirectional data wire
•    1 clock และ 1 data wire (รับเพียงอย่างเดียวหรือส่งเพียงอย่างเดียว)

1 clock and 1 bidirectional data wire (BIDIMODE = 1)

โหมดนี้ถูกเปิดใช้งานโดยการเซ็ต the BIDIMODE bit ใน the SPI_CR1 register. ในโหมดนี้ SCK ถูกใช้สำหรับ the clock และ MOSI ใน master หรือ MISO ใน slave mode ถูกใช้สำหรับ data communication. The transfer direction (Input/Output) ถูกเลือกโดย the BIDIOE bit ใน the SPI_CR1 register. เมื่อบิตนี้เป็น 1, the data line เป็น output มิฉะนั้นมันจะเป็น input.

1 clock and 1 unidirectional data wire (BIDIMODE = 0)

ในโหมดนี้, the application สามรถใช้ the SPI อย่างใดอย่างหนึ่งใน transmit-only mode หรือใน receive-only mode.
•    Transmit-only mode ก็เหมือนกันกับ full-duplex mode (BIDIMODE=0, RXONLY=0): the data ถูกส่งไปบน the
      transmit pin (MOSI ใน master mode หรือ MISO ใน slave mode) และ the receive pin (MISO ใน master mode หรือ
      MOSI ใน slave mode) สามารถถูกใช้เป็น a general-purpose IO. ในกรณีนี้, the application เพียงต้องการเพิกเฉยต่อ the Rx
      buffer (ถ้า the data register ถูกอ่าน, มันไม่ได้บรรจุ the received value).
•    ใน receive-only mode, the application สามารถปิดการใช้งาน the SPI output function โดยการเซ็ต the RXONLY bit ใน
      the SPI_CR1 register. ในกรณีนี้, มันจะปล่อยเป็นอิสระ the transmit IO pin (MOSI ใน master mode หรือ MISO ใน slave
      mode), ดังนั้นมันสามารถถูกใช้เพื่อวัตถุประสงค์อื่น.

เพื่อสตาร์ท the communication ใน receive-only mode, กำหนดค่าและเปิดการใช้งาน the SPI:
•    ใน master mode, the communication สตาร์ททันทีและหยุดเมื่อ the SPE bit ถูกเคลียร์และ the current reception หยุด.
      ไม่มีความจำเป็นต้องอ่าน the BSY flag ในโหมดนี้. มันถูกเซ็ตเสมอเมื่อ an SPI communication กำลังดำเนินการอยู่.
•    ใน slave mode, the SPI ยังคงรับต่อไปตราบเท่าที่ the NSS ถูก pulled down (หรือ the SSI bit ถูกเคลียร์ใน NSS software
      mode) และ the SCK กำลังรันอยู่.
Title: Re: STM32F1 SPI
Post by: tha on December 20, 2020, 06:34:14 AM
25.3.5 Data transmission and reception procedures

Rx and Tx buffers

ใน reception, data ถูกรับและจากนั้นถูกเก็บลงใน an internal Rx buffer ขณะที่ใน transmission, data ในอันดับแรกถูกเก็บลงใน an internal Tx buffer ก่อนที่จะถูกส่ง.

A read access ของ the SPI_DR register จะคืนกลับ the Rx buffered value ในขณะที่ a write access ไปยัง the SPI_DR เก็บรักษา the written data ลงใน the Tx buffer.

Start sequence in master mode

•    ใน full-duplex (BIDIMODE=0 and RXONLY=0)
      – The sequence เริ่มเมื่อ data ถูกเขียนลงใน the SPI_DR register (Tx buffer).
      – The data จากนั้นถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission และจากนั้นเลื่อนออกแบบอนุกรมไปยัง the MOSI pin.
      – ในเวลาเดียวกัน, the received data บน the MISO pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift register และจากนั้น
         parallel loaded ลงใน the SPI_DR register (Rx buffer).
•    ใน unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
      – The sequence เริ่มทันทีที่ SPE=1
      – มีเพียง the receiver ถูกทำให้ทำงานและ the received data บน the MISO pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift
         register และจากนั้นถูกโหลดแบบขนานลงใน the SPI_DR register (Rx buffer).
•    ใน bidirectional mode, เมื่อทำการส่ง (BIDIMODE=1 and BIDIOE=1)
      – The sequence เริ่มเมื่อ data ถูกเขียนลงใน the SPI_DR register (Tx buffer).
      – The data จากนั้นถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission และจากนั้นเลื่อนออกแบบอนุกรมไปยัง the MOSI pin.
      – ไม่มี data ถูกรับ.
•    ใน bidirectional mode, เมื่อทำการรับ (BIDIMODE=1 and BIDIOE=0)
      – The sequence เริ่มทันทีที่ SPE=1 and BIDIOE=0.
      – The received data บน the MOSI pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift register และจากนั้นถูกโหลดแบบขนาน
         ลงใน the SPI_DR register (Rx buffer).
      – The transmitter ไม่ถูกทำให้ทำงานและไม่มี data ถูกเลื่อนออกแบบอนุกรมไปยัง the MOSI pin.
Title: Re: STM32F1 SPI
Post by: tha on December 20, 2020, 08:46:38 AM
Start sequence in slave mode

•    ใน full-duplex mode (BIDIMODE=0 and RXONLY=0)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ของ the data บน MOSI pin ของมัน.
         The 7 remaining bits ถูกโหลดลงใน the shift register.
      – ในเวลาเดียวกัน, the data ถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission, และจากนั้นถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin. The software ต้องมีการเขียน the data ที่จะถูกส่ง
         ก่อน the SPI master device เริ่นต้น the transfer.
•    ใน unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ของ the data บน MOSI pin ของมัน.
         The 7 remaining bits ถูกโหลดลงใน the shift register.
      – The transmitter ไม่ถูกทำให้ทำงานและไม่มี data ถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin.
•    ใน bidirectional mode, เมื่อทำการส่ง (BIDIMODE=1 and BIDIOE=1)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ใน the Tx buffer ถูกส่งไปบน the MISO
         pin.
      – The data จากนั้นถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission  และจากนั้นถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin. The software ต้องมีการเขียน the data ที่จะถูกส่ง
         ก่อน the SPI master device เริ่นต้น the transfer.
      – ไม่มี data ถูกรับ.
•    ใน bidirectional mode, เมื่อทำการรับ (BIDIMODE=1 and BIDIOE=0)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ของ the data บน MISO pin ของมัน.
      – The received data บน the MISO pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift register และจากนั้น parallel loaded
         ลงใน the SPI_DR register (Rx buffer).
      – The transmitter ไม่ถูกทำให้ทำงานและไม่มี data ถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin.
Title: Re: STM32F1 SPI
Post by: tha on December 21, 2020, 04:07:06 AM
Handling data transmission and reception

The TXE flag (Tx buffer ว่าง) ถูกเซ็ตเมื่อ the data ถูกส่งถ่ายจาก the Tx buffer ไปยัง the shift register. มันแสดงให้เห็นว่า the internal Tx buffer พร้อมที่จะถูกโหลดด้วย the next data. An interrupt สามารถถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต. การเคลียร์ the TXE bit ถูกกรทำโดยการเขียนไปยัง the SPI_DR register.

Note: The software ต้องแน่ใจว่า the TXE flag ถูกเซ็ตเป็น 1 ก่อนพยายามที่จะเขียนไปยัง the Tx buffer. มิฉะนั้น, มันจะเขียนทับ
         the data ก่อนหน้าที่เขียนไปยัง the Tx buffer.

The RXNE flag (Rx buffer ไม่ว่าง) ถูกเซ็ตบน the last sampling clock edge, เมื่อ the data ถูกส่งถ่ายจาก the shift register ไปยัง the Rx buffer. มันแสดงให้เห็นว่า data พร้อมที่จะถูกอ่านจาก the SPI_DR register. An interrupt สามารถถูกสร้างขึ้นถ้า the RXNEIE bit ใน the SPI_CR2 register ถูกเซ็ต. การเคลียร์ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register

สำหรับบาง configurations, the BSY flag สามารถถูกใช้ในระหว่าง the last data transfer เพื่อคอยจนกระทั่ง the completion ของ the transfer.

Full-duplex transmit และ receive procedure ใน master หรือ slave mode (BIDIMODE=0 และ RXONLY=0)

The software ต้องทำตามขั้นตอนนี้เพื่อ transmit และ receive data (ดู Figure 241 และ Figure 242):
1.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit เป็น 1.
2.    เขียน the first data item ที่จะถูกส่งลงใน the SPI_DR register (นี้เคลียร์ the TXE flag).
3.    คอยจนกระทั่ง TXE=1 และเขียน the second data item ที่จะถูกส่ง. จากนั้นคอยจนกระทั่ง RXNE=1 และอ่าน the SPI_DR จะได้
       the first received data item (นี้เคลียร์ the RXNE bit). ทำซ้ำการทำงานนี้สำหรับแต่ละ data item ที่จะถูก
       transmitted/received จนกระทั่ง the n–1 received data.
4.    คอยจนกระทั่ง RXNE=1 และอ่าน the last received data.
5.    คอยจนกระทั่ง TXE=1 และจากนั้นคอยจนกระทั่ง BSY=0 ก่อนปิดการใช้งาน the SPI.

ขั้นตอนนี้สามารถถูกนำไปใช้กับ dedicated interrupt subroutines ได้อีกด้วย เริ่มที่แต่ละ rising edges ของ the RXNE หรือ TXE flag

(https://i.imgur.com/Ur2cVf1.png)

(https://i.imgur.com/TQyiGJG.png)
Title: Re: STM32F1 SPI
Post by: tha on December 21, 2020, 04:14:12 AM
Transmit-only procedure (BIDIMODE=0 RXONLY=0)

ในโหมดนี้, the procedure สามารถถูกลดลงดังอธิบายข้างล่างและ the BSY bit สามารถถูกใช้เพื่อคอยจนกระทั่ง the completion ของ the transmission (ดู Figure 243 and Figure 244).
1.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit เป็น 1.
2.    เขียน the first data item ที่จะส่งลงใน the SPI_DR register (นี้เคลียร์ the TXE bit).
3.    คอยจนกระทั่ง TXE=1 และเขียน the next data item ที่จะถูกส่ง. ทำซ้ำขั้นตอนนี้สำหรับแต่ละ data item ที่จะถูกส่ง.
4.    หลังจากการเขียน the last data item ลงใน the SPI_DR register, คอยจนกระทั่ง TXE=1, จากนั้นคอยจนกระทั่ง BSY=0, นี้
       แสดงให้เห็นว่า the transmission ของ the last data เสร็จสมบูรณ์.

ขั้นตอนนี้สามารถถูกนำไปใช้กับ dedicated interrupt subroutines ได้อีกด้วย เริ่มที่แต่ละ rising edges ของ the TXE flag

Note: ในระหว่างการสื่อสารที่ไม่ต่อเนื่อง, จะมี a 2 APB clock period delay ระหว่าง the write operation ไปยัง SPI_DR และ the
         BSY bit เซ็ต. ด้วยเหตุนี้, ใน transmit-only mode, มันมีข้อบังคับให้คอยเป็นอันดับแรกจนกระทั่ง TXE ถูกเซ็ตและจากนั้นจน
         กระทั่ง BSY ถูกเคลียร์หลังจากการเขียน the last data.

         หลังจากการส่ง two data items ใน transmit-only mode, the OVR flag ถูกเซ็ต in the SPI_SR register เนื่องจาก the
         received data ไม่เคยถูกอ่าน.

(https://i.imgur.com/dZ0idoc.png)
Title: Re: STM32F1 SPI
Post by: tha on December 21, 2020, 05:22:30 AM
Bidirectional transmit procedure (BIDIMODE=1 and BIDIOE=1)

ในโหมดนี้, ขั้นตอนก็เหมือนกันกับขั้นตอนใน Transmit-only mode ยกเว้นที่ the BIDIMODE and BIDIOE bits ทั้งคู่ต้องถูกเซ็ตใน the SPI_CR2 register ก่อนการเปิดการใช้งาน the SPI.

Unidirectional receive-only procedure (BIDIMODE=0 and RXONLY=1)

ในโหมดนี้, ขั้นตอนสามารถถูกลดดังอธิบายข้างล่าง (ดู Figure 245):
1.    เซ็ต the RXONLY bit ใน the SPI_CR1 register.
2.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit เป็น 1:
          a) ใน master mode, นี้จะทำให้ทำงานโดยทันที the generation ของ the SCK clock, และ data ถูกรับแบบอนุกรมจนกระทั่งl
              the SPI ถูกปิดการใช้งาน (SPE=0).
          b) ใน slave mode, data ถูกรับเมื่อ the SPI master device ขับ NSS low และสร้าง the SCK clock.
3.    คอยจนกระทั่ง RXNE=1 และอ่าน the SPI_DR register เพื่อให้ได้ the received data (นี้เคลียร์ the RXNE bit). ทำซ้ำการ
       ทำงานนี้สำหรับแต่ละ data item ที่จะถูกรับ.

ขั้นตอนนี้สามารถถูกนำไปใช้กับ dedicated interrupt subroutines ได้อีกด้วย เริ่มที่แต่ละ rising edges ของ the RXNE flag

Note: ถ้ามีความจำเป็นที่จะปิดการใช้งาน the SPI หลังจาก the last transfer, ทำตามคำแนะนำที่อธิบายใน Section 25.3.8.

(https://i.imgur.com/9GmoJdi.png)
Title: Re: STM32F1 SPI
Post by: tha on December 22, 2020, 08:59:56 AM
Bidirectional receive procedure (BIDIMODE=1 and BIDIOE=0)

ในโหมดนี้, ขั้นตอนก็เหมือนกันกับขั้นตอน the Receive-only mode ยกเว้นที่ the BIDIMODE ต้องถูกเซ็ตและ the BIDIOE bit เคลียร์ใน the SPI_CR2 register ก่อนการเปิดการใช้งาน the SPI.

Continuous and discontinuous transfers

เมื่อส่ง data ใน master mode, ถ้า the software มีความเร็วเพียงพอที่จะตรวจพบแต่ละ rising edge ของ TXE (หรือ TXE interrupt) และเขียนทันทีไปยัง the SPI_DR register ก่อน the ongoing data transfer เสร็จสมบูรณ์, the communication ถูกพูดได้ว่า continuous. ในกรณีนี้, ไม่มีความไม่ต่อเนื่องในการสร้างของ the SPI clock ระหว่างแต่ละ data item และ the BSY bit ไม่เคยถูกเคลียร์ระหว่างแต่ละ data transfer.

ในทางตรงกันข้าม, ถ้า the software ไม่มีความเร็วเพียงพอ, นี้สามารถนำมาซึ่งบางความไม่ต่อเนื่องใน the communication. ในกรณีนี้, the BSY bit ถูกเคลียร์ระหว่างแต่ละ data transmission
(ดู Figure 246).

ใน Master receive-only mode (RXONLY=1), the communication จะต่อเนื่องเสมอและ the BSY flag ถูกอ่านได้เป็น 1 เสมอ.

ใน slave mode, ความต่อเนื่องของ the communication ถูกกำหนดโดย the SPI master device. ในกรณีใดๆ, ถึงแม้ว่า the communication จะต่อเนื่อง, the BSY flag จะ low ระหว่างแต่ละ transfer เป็นระยะเวลาขั้นต่ำของ one SPI clock cycle (ดู Figure 244).

(https://i.imgur.com/tTVxUEc.png)
Title: Re: STM32F1 SPI
Post by: tha on December 23, 2020, 06:01:12 AM
25.3.6 CRC calculation

A CRC calculator ถูกนำมาใช้สำหรับ communication reliability. CRC calculators ที่แยกกันถูกนำมาใช้สำหรับ transmitted data และ received data. The CRC ถูกคำนวนโดยใช้ a programmable polynomial อนุกรมบนแต่ละบิต. มันถูกคำนวนบน the sampling clock edge ที่กำหนดโดย the CPHA และ CPOL bits ใน the SPI_CR1 register.

Note: SPI มีสองชนิคของ of CRC calculation standard ซึ่งขึ้นโดยตรงกับ the data frame format ที่เลือกสำหรับ the
         transmission และ/หรือ reception: 8-bit data (CR8) และ 16-bit data (CRC16).

CRC calculation ถูกเปิดการใช้งานโดยการเซ็ต the CRCEN bit ใน the SPI_CR1 register. การกระทำนี้รีเซ็ต the CRC registers (SPI_RXCRCR และ SPI_TXCRCR). ใน full duplex หรือ transmitter only mode, เมื่อ the transfers ถูกจัดการโดย the software (CPU mode), มันมีความจำเป็นที่ต้องเขียน the bit CRCNEXT ทันทีหลังจาก the last data ที่จะถูกส่งถ่ายถูกเขียนไปยัง the SPI_DR. ที่ the end ของ last data transfer นี้, the SPI_TXCRCR value ถูกส่ง.

ใน receive only mode และเมื่อ the transfers ถูกจัดการโดย software (CPU mode),  มันมีความจำเป็นที่ต้องเขียน the CRCNEXT bit หลังจาก the second last data ถูกรับแล้ว. The CRC ถูกรับพอดีหลังจาก the last data reception และ the CRC check จากนั้นถูกดำเนินการ.

ที่ the end ของ data และ CRC transfers, the CRCERR flag ใน the SPI_SR register ถูกเซ็ตถ้า corruption เกิดขึ้นในระหว่าง the transfer

ถ้า data มีอยู่ใน the TX buffer, the CRC value ถูกส่งได้เพียงหลังจาก the transmission ของ the data byte. ในระหว่าง CRC transmission, the CRC calculator ถูก switched off และ the register value ยังคงไม่เปลี่ยนแปลง.

SPI communication โดยใช้ the CRC สามารถเป็นได้ผ่านทางขั้นตอนต่อไปนี้:
1.    โปรแกรม the CPOL, CPHA, LSBFirst, BR, SSM, SSI and MSTR values.
2.    โปรแกรม the polynomial ใน the SPI_CRCPR register.
3.    เปิดการใช้งาน the CRC calculation โดยการเซ็ต the CRCEN bit ใน the SPI_CR1 register. นี้จะเคลียร์ the SPI_RXCRCR
       and SPI_TXCRCR registers อีกด้วย.
4.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit ใน the SPI_CR1 register
5.    สตาร์ท the communication และค้ำจุน the communication จนกระทั่งหมดเว้นแต่ one byte หรือ halfword ถูก transmitted
      หรือ received แล้ว.
         – ใน full duplex หรือ transmitter-only mode, เมื่อ the transfers ถูกจัดการโดย software, เมื่อเขียน the last byte หรือ
            half word ไปยัง the Tx buffer, เซ็ต the CRCNEXT bit ใน the SPI_CR1 register เพื่อแสดงให้เห็นว่า the CRC จะถูก
            ส่งหลังจาก the transmission ของ the last byte.
         – ใน receiver only mode, เซ็ต the bit CRCNEXT แค่หลังจาก the reception ของที่สองก่อนถึง last data เพื่อเตรียม the
            SPI เข้าสู่ใน CRC Phase ที่ the end ของ the reception ของ the last data. CRC calculation ถูกแช่แข็งในระหว่าง the
            CRC transfer.
6.    หลังจาก the transfer ของ the last byte หรือ half word, the SPI เข้าสู่ the CRC transfer และ check phase. ใน full
       duplex mode หรือ receiver-only mode, the received CRC ถูกเปรียบเทียบกับ the SPI_RXCRCR value. ถ้า the value
       ไม่เท่ากัน, the CRCERR flag ใน SPI_SR ถูกเซ็ตและ an interrupt สามารถถูกสร้างขึ้นเมื่อ the ERRIE bit ใน the SPI_CR2
       register ถูกเซ็ต.

Note: เมื่อ the SPI อยู่ใน slave mode, ควรระมัดระวังการเปิดการใช้งาน CRC calculation เฉพาะเมื่อ the clock มีความเสถียร, นั่นคือ,
         เมื่ the clock อยู่ใน the steady state. ถ้าไม่, a wrong CRC calculation อาจถูกทำได้. ในความเป็นจริง, the CRC มีความไว
         ต่อ the SCK slave input clock ทันทีที่ CRCEN ถูกเซ็ต, และนี่, the value ของ the SPE bit เป็นอะไรก็ตาม.

        ด้วย high bitrate frequencies, โปรดระมัดระวังเมื่อทำการส่ง the CRC. ดังที่ the number ของ used CPU cycles ต้องทำให้
        ต่ำที่สุดเท่าที่เป็นได้ใน the CRC transfer phase, มันมีข้อห้ามที่จะเรียก software functions ใน the CRC transmission
        sequence เพื่อหลีกเลี่ยง errors ใน the last data และ CRC reception. ในความเป็นจริง, CRCNEXT bit ต้องถูกเขียนก่อน
        the end ของ the transmission/reception ของ the last data.

       สำหรับ high bit rate frequencies, แนะนำให้ใช้ the DMA mode เพื่อหลีกเลี่ยง the degradation ของ the SPI speed
       performance เนื่องจาก CPU accesses กระทบต่อ the SPI bandwidth.

       เมื่อ the devices ถูกกำหนดเป็น slaves และ the NSS hardware mode ถูกใช้, the NSS pin จำเป็นต้องให้เป็น low ระหว่าง
       the data phase และ the CRC phase.

เมื่อ the SPI ถูกกำหนดค่าใน slave mode พร้อมด้วย the CRC feature ถูกเปิดการใช้งาน, CRC calculation เกิดขึ้นแม้ว่า a high level ถูกใช้บน the NSS pin. นี้อาจเกิดขึ้นตัวอย่างเช่นในกรณีของ a multislave environment ที่ the communication master ระบุ slaves สลับกัน.


ระหว่าง a slave deselection (high level บน NSS) และ a new slave selection (low level บน NSS), the CRC value ควรถูกเคลียร์บนทั้ง master และ slave sides เพื่อประสานกันใหม่ the master และ slave สำหรับ CRC calculation ของมันตามลำดับ.

เพื่อเคลียร์ the CRC, ทำตามขั้นตอนข้างล่างนี้:
1. Disable SPI (SPE = 0)
2. Clear the CRCEN bit
3. Set the CRCEN bit
4. Enable the SPI (SPE = 1)
Title: Re: STM32F1 SPI
Post by: tha on December 23, 2020, 10:15:07 AM
25.3.7 Status flags
Four status flags ถูกจัดให้มีเพื่อให้ the application เฝ้าดู the state ของ the SPI bus ได้อย่างสมบูรณ์.

Tx buffer empty flag (TXE)
เมื่อมันถูกเซ็ต, flag นี้แสดงให้เห็นว่า the Tx buffer ว่างอยู่และ the next data ที่จะถูกส่งสามารถถูกโหลดลงใน the buffer. The TXE flag ถูกเคลียร์เมื่อเขียนไปยัง the SPI_DR register.

Rx buffer not empty (RXNE)
เมื่อเซ็ต, flag นี้แสดงให้เห็นว่ามี valid received data ใน the Rx buffer. มันถูกเคลียร์เมื่อ SPI_DR ถูกอ่าน.

BUSY flag
BSY flag นี้ถูกเซ็ตและเคลียร์โดย hardware (การเขียนไปยัง flag นี้จะไม่มีผลอะไร). The BSY flag แสดงให้เห็นว่า the state ของ the communication layer ของ the SPI.

เมื่อ BSY ถูกเซ็ต, มันแสดงให้เห็นว่า the SPI กำลังยุ่งอยู่กับการสื่อสาร. มีข้อยกเว้นอันหนึ่งใน master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0) ที่ the BSY flag เป็น low ในระหว่าง reception.

The BSY flag ถูกใช้ให้เป็นประโยชน์เพื่อตรวจจับ the end ของ a transfer ถ้า the software ต้องการปิดการใช้งาน the SPI และเข้าสู่ Halt mode (หรือปิดการใช้งาน the peripheral clock). นี้หลีกเลี่ยงการเสียหาย the last transfer. สำหรับการนี้, ขั้นตอนที่อธิบายด้านล่างนี้ต้องปฏิบัติตามอย่างเคร่งครัด.

The BSY flag ถูกใช้ให้เป็นประโยชน์เพื่อหลีกเลี่ยง write collisions ใน a multimaster system.

The BSY flag ถูกเซ็ตเมื่อ a transfer สตาร์ท, ด้วยมีข้อยกเว้นของ master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0).

มันถูกเคลียร์:
•    เมื่อ a transfer เสร็จ (ยกเว้นใน master mode ถ้า the communication มีความต่อเนื่อง)
•    เมื่อ the SPI ถูกปิดการใช้งาน
•    เมื่อ a master mode fault เกิดขึ้น (MODF=1)

เมื่อ communication ไม่ต่อเนื่อง, the BSY flag เป็น low ระหว่างแต่ละ communication

เมื่อ communication ต่อเนื่อง:
•    ใน master mode, the BSY flag เป็น high ในระหว่าง the transfers ทั้งหมด
•    ใน slave mode, the BSY flag เป็น low สำหรับ one SPI clock cycle ระหว่างแต่ละ transfer

Note: ไม่ควรใช้ the BSY flag เพื่อจัดการแต่ละ data transmission หรือ reception. มันดีกว่าที่จะใช้ the TXE and RXNE flags แทน.
Title: Re: STM32F1 SPI
Post by: tha on December 23, 2020, 11:24:40 AM
25.3.8 Disabling the SPI

เมื่อ a transfer สิ้นสุดลง, the application สามารถหยุด the communication โดยการปิดการใช้งาน the SPI peripheral. นี้ถูกทำโดยการเคลียร์ the SPE bit.

สำหรับบางการกำหนดค่า, การปิดการใช้งาน the SPI และเข้าสู่ the Halt mode ในขณะที่ a transfer กำลังดำเนินการอยู่สามารถเป็นเหตุให้ the current transfer เสียหายและ/หรือ the BSY flag อาจจะไม่น่าเชื่อถือได้.

เพื่อหลีกเลี่ยงผลกระทบเหล่านี้ขอแนะนำให้ปฏิบัติตามขั้นตอนต่อไปนี้เมื่อปิดการใช้งาน SPI:

In master or slave full-duplex mode (BIDIMODE=0, RXONLY=0)

1.    คอยจนกระทั่ง RXNE=1 เพื่อรับ the last data
2.    คอยจนกระทั่ง TXE=1
3.    จากนั้นคอยจนกระทั่ง BSY=0
4.    ปิดการใช้งาน the SPI (SPE=0) และ, ท้ายที่สุด, เข้าสู่ the Halt mode (หรือปิดการใช้งาน the peripheral clock)

In master or slave unidirectional transmit-only mode (BIDIMODE=0,
RXONLY=0) or bidirectional transmit mode (BIDIMODE=1, BIDIOE=1)


หลังจาก the last data ถูกเขียนลงใน the SPI_DR register:
1.    คอยจนกระทั่ง TXE=1
2.    จากนั้นคอยจนกระทั่ง BSY=0
3.    ปิดการใช้งาน the SPI (SPE=0) และ, ท้ายที่สุด, เข้าสู่ the Halt mode (หรือปิดการใช้งาน the peripheral clock)

In master unidirectional receive-only mode (MSTR=1, BIDIMODE=0,
RXONLY=1) or bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0)


กรณีนี้ต้องได้รับการจัดการโดยเฉพาะเพื่อให้แน่ใจว่า the SPI ไม่เริ่มต้น a new transfer:
1.    คอยสำหรับที่สองก่อนถึง last occurrence ของ RXNE=1 (n–1)
2.    จากนั้นคอยสำหรับ one SPI clock cycle (โดย a software loop) ก่อนปิดการใช้งาน the SPI (SPE=0)
3.    จากนั้นคอยสำหรับ the last RXNE=1 ก่อนเข้าสู่ the Halt mode (หรือปิดการใช้งาน the peripheral clock)

Note: In master bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0), the BSY flag เป็น low ในระหว่าง
           transfers.

In slave receive-only mode (MSTR=0, BIDIMODE=0, RXONLY=1) or
bidirectional receive mode (MSTR=0, BIDIMODE=1, BIDOE=0)


1.    คุณสามารถปิดการใช้งาน the SPI (write SPE=1) ที่เวลาใดก็ได้: the current transfer จะเสร็จสมบูรณ์ก่อน the SPI ถูกปิดการ
       ใช้งานจะมีผล
2.    จากนั้น, ถ้าคุณต้องการเข้าสู่ the Halt mode, คุณต้องอันดับแรกคอยจนกระทั่ง BSY = 0 ก่อนเข้าสู่ the Halt mode (หรือปิดการใช้
       งาน the peripheral clock)
Title: Re: STM32F1 SPI
Post by: tha on December 23, 2020, 04:12:46 PM
25.3.9 SPI communication using DMA (direct memory addressing)

ในการทำงานที่ maximum speed ของมัน, the SPI จำเป็นต้องป้อนด้วย the data สำหรับ transmission และ the data received บน the Rx buffer ควรถูกอ่านเพื่อหลีกเลี่ยง overrun. เพื่ออำนวยความสะดวก the transfers, the SPI มีคุณลักษณะ a DMA capability จัดให้มี a simple request/acknowledge protocol.

A DMA access ถูกร้องขอเมื่อ the enable bit ใน the SPI_CR2 register ถูกเปิดการใช้งาน. การร้องขอที่แยกกันต้องถูกปล่อยออกไปยัง the Tx และ Rx buffers (ดู Figure 247 และ Figure 248):
•    ใน transmission, a DMA request ถูกปล่อยออกแต่ละเวลาที่ TXE ถูกเซ็ตเป็น 1. The DMA จากนั้นเขียนไปยัง the SPI_DR
      register (นี้เคลียร์ the TXE flag).
•    ใน reception, a DMA request ถูกปล่อยออกแต่ละเวลาที่ RXNE ถูกเซ็ตเป็น 1. The DMA จากนั้นอ่าน the SPI_DR register (นี้
      เคลียร์ the RXNE flag).

เมื่อ the SPI ถูกใช้เฉพาะส่ง data, มันเป็นไปได้ที่จะเปิดการใช้งานเพียง the SPI Tx DMA channel. ในกรณีนี้, the OVR flag ถูกเซ็ตเพราะว่า the data received ไม่ถูกอ่าน.

เมื่อ the SPI ถูกใช้เฉพาะรับ data, มันเป็นไปได้ที่จะเปิดการใช้งานเพียง the SPI Rx DMA channel.

ใน transmission mode, เมื่อ the DMA เขียนl the data ทั้งหมดที่ถูกส่งหมดแล้ว (flag TCIF ถูกเซ็ตใน the DMA_ISR register), the BSY flag สามารถถูกเฝ้าดูเพื่อให้แน่ใจว่า the SPI communication เสร็จสมบูรณ์. นี้ถูกต้องการเพื่อหลีกเลี่ยงความเสียหายของ the last transmission ก่อนการปิดการใช้งาน the SPI หรือการเข้าสู่ the Stop mode. The software ต้องอันดับแรกคอยจนกระทั่ง TXE=1 และจากนั้นจนกระทั่ง BSY=0.

Note: ในระหว่างการสื่อสารที่ไม่ต่อเนื่อง, จะมี a 2 APB clock period delay ระหว่าง the write operation ไปยัง SPI_DR และ the
           BSY bit เซ็ต. ด้วยเหตุนี้, ใน transmit-only mode, มันมีข้อบังคับให้คอยเป็นอันดับแรกจนกระทั่ง TXE ถูกเซ็ตและจากนั้นจน
           กระทั่ง BSY=0 หลังจากการเขียน the last data.

(https://i.imgur.com/LHFHmyU.png)

DMA capability with CRC

เมื่อ SPI communication ถูกเปิดการใช้งานพร้อมด้วย CRC communication และ DMA mode, the transmission และ reception ของ the CRC ที่ the end ของ communication เป็นไปโดยอัตโนมัตินั่นคือปราศจากการใช้ the bit CRCNEXT. หลังจาก the CRC reception, the CRC ต้องถูกอ่านใน the SPI_DR register เพื่อเคลียร์ the RXNE flag.

ที่ the end ของ data และ CRC transfers, the CRCERR flag ใน SPI_SR ถูกเซ็ตถ้า corruption เกิดขึ้นในระหว่าง the transfer.
Title: Re: STM32F1 SPI
Post by: tha on December 24, 2020, 07:29:06 AM
25.3.10 Error flags

Master mode fault (MODF)

Master mode fault เกิดขึ้นเมื่อ the master device มี NSS pin ของมันถูกดึงลง low (ใน NSS hardware mode) หรือ SSI bit low (ใน NSS software mode), นี้เซ็ต the MODF bit โดยอัตโนมัติ.

Master mode fault มีผลต่อ the SPI peripheral ในวิธีต่อไปนี้:
•    The MODF bit ถูกเซ็ตและ an SPI interrupt ถูกสร้างขึ้นถ้า the ERRIE bit ถูกเซ็ต.
•    The SPE bit ถูกเคลียร์. นี้จะบล็อก output ทั้งหมดจาก the device และปิดการใช้งาน the SPI interface.
•    The MSTR bit ถูกเคลียร์, ดังนั้นบังคับให้ the device ลงใน slave mode

ใช้ the following software sequence เพื่อเคลียร์ the MODF bit:
1.    ทำการ a read or write access ไปยัง the SPI_SR register ในขณะที่ the MODF bit ถูกเซ็ต.
2.    จากนั้นเขียนถึง the SPI_CR1 register.

เพื่อหลีกเลี่ยง slave หลายตัวขัดแย้งกันใน a system ที่ประกอบด้วย MCUs หลายตัว, the NSS pin ต้องถูกดึงขึ้น high ในระหว่าง the MODF bit clearing sequence. The SPE and MSTR bits สามารถถูกเอากลับคืนสู่สถานะเดิมของมันหลังจาก clearing sequence นี้.

เพื่อความปลอดภัย, hardware ไม่ยอมให้มีการเซ็ตของ the SPE and MSTR bits ในขณะที่ the MODF bit ถูกเซ็ต.

ใน a slave device the MODF bit ไม่สามารถถูกเซ็ต. อย่างไรก็ตาม, ใน a multimaster configuration, the device สามารถอยู่ใน slave mode พร้อมด้วย MODF bit นี้เซ็ต. ในกรณีนี้, the MODF bit แสดงให้เห็นว่าอาจมี a multimaster conflict สำหรับ system control. An interrupt routine สามารถถูกใช้เพื่อกู้คืนอย่างหมดจดจากสถานะนี้โดยทำการ a reset หรือกลับคืนสู่สถานะเริ่มต้น.

Overrun condition

An overrun condition เกิดขึ้นเมื่อ the master device ส่ง data bytes และ the slave device ไม่ได้เคลียร์ the RXNE bit อันเป็นผลมาจากการส่ง the previous data byte.
เมื่อ an overrun condition เกิดขึ้น:
•    the OVR bit ถูกเซ็ตและ an interrupt ถูกสร้างขึ้นถ้า the ERRIE bit ถูกเซ็ต.

ในกรณีนี้, the receiver buffer contents จะไม่ถูกอัปเดตด้วย the newly received data จาก the master device. การอ่านจาก the SPI_DR register จะส่งกลับ byte นี้. ไบต์ที่ส่งในภายหลังอื่นๆทั้งหมดจะสูญหาย.

การเคลียร์ the OVR bit ถูกทำโดยการอ่านจาก the SPI_DR register ตามด้วยโดย a read access ไปยัง the SPI_SR register

CRC error

แฟล็กนี้ถูกใช้เพื่อตรวจสอบความถูกต้องของค่าที่ได้รับเมื่อ the CRCEN bit ใน the SPI_CR1 register ถูกเซ็ต. The CRCERR flag ใน the SPI_SR register ถูกเซ็ตถ้าค่าที่ได้รับใน the shift register ไม่เท่ากันกับ the receiver SPI_RXCRCR value.

(https://i.imgur.com/SondTeB.png)
Title: Re: STM32F1 SPI
Post by: tha on December 24, 2020, 02:50:43 PM
25.4 I2S functional description

The I2S audio protocol ไม่มีให้ใช้ใน low- and medium-density devices. ส่วนนี้เกี่ยวข้องเฉพาะ high-density, XL-density and connectivity line devices.

(https://i.imgur.com/cAbB1iP.png)

The SPI สามารถทำหน้าที่เป็น an audio I2S interface เมื่อ the I2S capability ถูกเปิดการใช้งาน (โดยการเซ็ต the I2SMOD bit ใน the SPI_I2SCFGR register). interface นี้ใช้เกือบ the same pins, flags และ interrupts เป็น the SPI.

The I2S ใช้ร่วม three common pins กับ the SPI:
•    SD: Serial Data (mapped บน the MOSI pin) เพื่อส่งหรือรับ the two time-multiplexed data channels (ใน half-
      duplex mode เท่านั้น).
•    WS: Word Select (mapped บน the NSS pin) คือ the data control signal เอ๊าพุทใน master mode และอินพุทใน slave
      mode.
•    CK: Serial Clock (mapped บน the SCK pin) คือ the serial clock เอ๊าพุทใน master mode และ serial clock อินพุทใน
      slave mode.

An additional pin สามารถถูกใช้เมื่อ a master clock output จำเป็นสำหรับบาง external audio devices:
•    MCK: Master Clock (mapped แยกต่างหาก) ถูกใช้, เมื่อ the I2S ถูกกำหนดค่าใน master mode (และเมื่อ the MCKOE bit
      ใน the SPI_I2SPR register ถูกเซ็ต), เพื่อเอ๊าพุท additional clock นี้สร้างขึ้นที่ a preconfigured frequency rate เท่ากันกับ
      256 × FS, ที่ FS คือ the audio sampling frequency

The I2S ใช้ clock generator ของตัวเองเพื่อผลิต the communication clock เมื่อมันถูกเซ็ตใน master mode. clock generator เป็น the source ของ the master clock output อีกด้วย. Two additional registers มีให้ใช้ใน I2S mode. รีจีสเตอร์หนึ่งถูกเชื่อมโยงถึง the clock generator configuration SPI_I2SPR และอีกรีจีสเตอร์หนึ่งคือ a generic I2S configuration register
SPI_I2SCFGR (audio standard, slave/master mode, data format, packet frame, clock polarity, etc.).

The SPI_CR1 register และทุก CRC registers ไม่ได้ถูกใช้ใน the I2S mode. ในทำนองเดียวกัน, the SSOE bit ใน the SPI_CR2 register และ the MODF และ CRCERR bits ใน the SPI_SR ไม่ได้ถูกใช้.

The I2S ใช้ the same SPI register สำหรับ data transfer (SPI_DR) ใน 16-bit wide mode.
Title: Re: STM32F1 SPI
Post by: tha on December 24, 2020, 04:39:30 PM
25.4.2 Supported audio protocols

The three-line bus ต้องจัดการเฉพาะ audio data โดยทั่วไป time-multiplexed บน two channels: the right channel และ the left channel. อย่างไรก็ตามมีเพียง one 16-bit register สำหรับ the transmission และ the reception. ดังนั้น, มันขึ้นอยู่กับ the software ที่เขียนลงใน the data register ค่าที่เพียงพอที่ตรงกันกับ the considered channel side, หรือเพื่ออ่าน the data จาก the data register และเพื่อชี้ตัว the corresponding channel โดยการเช็ค the CHSIDE bit ใน the SPI_SR register. Channel Left ถูกส่งเป็นอันดับแรกตามด้วย the channel right (CHSIDE ไม่มีความหมายสำหรับ the PCM protocol).

Four data และ packet frames มีให้ใช้งาน. Data อาจถูกส่งด้วยรูปแบบของ:
•    16-bit data บรรจุใน 16-bit frame
•    16-bit data packed in 32-bit frame
•    24-bit data packed in 32-bit frame
•    32-bit data packed in 32-bit frame

เมื่อใช้ 16-bit data ขยายบน 32-bit packet, the first 16 bits (MSB) เป็น the significant bits, the 16-bit LSB ถูกบังคับเป็น 0 โดยปราศจากความจำเป็นใดๆสำหรับ software action หรือ DMA request (เฉพาะ one read/write operation).

The 24-bit และ 32-bit data frames ต้องการ two CPU read หรือ write operations ไปยัง/จาก the SPI_DR หรือ two DMA operations ถ้า the DMA ถูกชอบสำหรับ the application. สำหรับ 24-bit data frame โดยเฉพาะ, the 8 nonsignificant bits ถูกขยายเป็น 32 bits ด้วย 0-bits (โดย hardware).

สำหรับทุก data formats และ communication standards, the most significant bit ถูกส่งเป็นอันดับแรกเสมอ (MSB first).

The I2S interface รองรับ four audio standards, กำหนดค่าได้โดยใช้ the I2SSTD[1:0] และ PCMSYNC bits ใน the SPI_I2SCFGR register
Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 09:10:45 AM
I2S Philips standard

สำหรับมาตรฐานนี้, the WS signal ถูกใช้เพื่อแสดงให้เห็นซึ่ง channel กำลังถูกส่ง. มันถูกทำให้ทำงาน one CK clock cycle ก่อน the first bit (MSB) มีให้ใช้งาน.

(https://i.imgur.com/EYZKBIw.png)

Data ถูก latched บน the falling edge ของ CK (สำหรับ the transmitter) และถูกอ่านบน the rising edge (สำหรับ the receiver). The WS signal ก็ถูก latched ด้วยบน the falling edge ของ CK.

(https://i.imgur.com/awWzNzu.png)
Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 09:35:22 AM
โหมดนี้ต้องการ two write หรือ read operations ไปยัง/จาก the SPI_DR.
• In transmission mode:
ถ้า 0x8EAA33 ต้องถูกส่ง (24-bit):

(https://i.imgur.com/G1Nyv17.png)

• ใน reception mode:
ถ้า data 0x8EAA33 ถูกรับ:

(https://i.imgur.com/68NR4xb.png)

(มีเพียง the 8MSB ถูกส่งเพื่อเทียบเคียงเป็น the 24 bits, 8 LSBs ไม่มีความหมายและสามารถเป็นอะไรก็ได้)
Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 10:19:41 AM
(https://i.imgur.com/7fUx7k1.png)

เมื่อ 16-bit data frame ขยายถึง 32-bit channel frame ถูกเลือกในระหว่าง the I2S configuration phase, เพียงหนึ่งการเข้าถึงไปยัง SPI_DR ถูกต้องการ. The 16 remaining bits ถูกบังคับโดย hardware เป็น 0x0000 เพื่อต่อ the data เป็น 32-bit format.

ถ้า the data ส่งหรือ the received data คือ 0x76A3 (0x76A30000 ขยายเป็น 32-bit), the operation แสดงใน Figure 255 ถูกต้องการ.

(https://i.imgur.com/wtXsgl8.png)

สำหรับ transmission, แต่ละเวลาที่ an MSB ถูกเขียนไปยัง SPI_DR, the TXE flag ถูกเซ็ตและ interrupt ของมัน, ถ้ายอมให้, ถูกสร้างขึ้นเพื่อโหลด SPI_DR ด้วย the new value เพื่อส่ง. นี้เกิดขึ้นถึงแม้ว่า 0x0000 ยังไม่ได้ถูกส่งเพราะมันถูกทำโดย hardware.

สำหรับ reception, the RXNE flag ถูกเซ็ตและ interrupt ของมัน, ถ้ายอมให้, ถูกสร้างขึ้นเมื่อ the first 16 MSB half-word ถูกรับ.

ด้วยวิธีนี้, เวลาที่มากขึ้นถูกจัดให้มีระหว่าง two write หรือ read operations, ซึ่งป้องกัน underrun หรือ overrun conditions (ขึ้นอยู่กับทิศทางของ the data transfer).
Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 01:26:57 PM
MSB justified standard

สำหรับมาตรฐานนี้, the WS signal ถูกสร้างขึ้นที่เวลาเดียวกันกับ the first data bit, ซึ่งคือ the MSBit.

(https://i.imgur.com/OUyreMG.png)

Data ถูก latched บน the falling edge ของ CK (สำหรับ the transmitter) และถูกอ่านบน the rising edge (สำหรับ the receiver).

(https://i.imgur.com/LKNfwAC.png)

(https://i.imgur.com/P0fBvzB.png)
Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 01:40:24 PM
LSB justified standard

มาตรฐานนี้ก็เหมือนกันกับ the MSB justified standard (ไม่แตกต่างสำหรับ the 16-bit และ 32-bit full-accuracy frame formats).

(https://i.imgur.com/v4FKfL0.png)
Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 02:04:06 PM
• In transmission mode:
ถ้า data 0x3478AE ต้องถูกส่ง, two write operations ไปยัง the SPI_DR register ถูกต้องการจาก software หรือโดย DMA. The operations ถูกแสดงไว้ข้างล่างนี้.

(https://i.imgur.com/tPx2aNU.png)

• In reception mode:
ถ้า data 0x3478AE ถูกรับ, two successive read operations จาก SPI_DR ถูกต้องการบนแต่ละ RXNE event.

(https://i.imgur.com/VUTQLEQ.png)

(เฉพาะ the 8 LSB ของ the half-word เท่านั้นที่มีความสำคัญ. A field ของ the 0x00 ถูกบังคับแทนลงใน ths 8MSBs.)

Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 02:35:52 PM
(https://i.imgur.com/WipEuQU.png)

เมื่อ 16-bit data frame ขยายถึง 32-bit channel frame ถูกเลือกในระหว่าง the I2S configuration phase, เพียงหนึ่ง access ไปยัง SPI_DR ถูกต้องการ. The 16 remaining bits ถูกบังคับโดย hardware เป็น 0x0000 เพื่อต่อ the data เป็น 32-bit format. ในกรณีนี้, มันตรงกันกับ the half-word MSB.

ถ้า the data ส่งหรือ the received data คือ 0x76A3 (0x0000 76A3 ขยายเป็น 32-bit), the operation แสดงใน Figure 264 ถูกต้องการ.

(https://i.imgur.com/ukkFjjV.png)

ใน transmission mode, เมื่อ TXE ถูกยืนยัน, the application ต้องเขียน the data ที่จะถูกส่ง (ในกรณีนี้ 0x76A3). The 0x000 field ถูกส่งเป็นอันดับแรก (การขยายบน 32-bit). TXE ถูกยืนยันอีกครั้งทันทีที่ the effective data (0x76A3) ถูกส่งไปบน SD.

ใน reception mode, RXNE ถูกยืนยันทันทีที่ the significant half-word ถูกรับ (และไม่ใช่ the 0x0000 field).

ด้วยวิธีนี้, เวลาที่มากขึ้นถูกจัดให้มีระหว่าง two write หรือ read operations, ซึ่งป้องกัน underrun หรือ overrun conditions
Title: Re: STM32F1 SPI
Post by: tha on December 25, 2020, 04:16:56 PM
PCM standard

สำหรับ the PCM standard, ไม่มีความจำเป็นต้องใช้ channel-side information. The two PCM modes (short และ long frame) มีให้ใช้งานและกำหนดค่าได้โดยใช้ the PCMSYNC bit ใน SPI_I2SCFGR.

(https://i.imgur.com/G029wa7.png)

สำหรับ long frame synchronization, the WS signal assertion time จะคงที่ 13 bits ใน master mode

สำหรับ short frame synchronization, the WS synchronization signal มีเพียง one cycle long.

(https://i.imgur.com/u6s449P.png)

Note: สำหรับทั้งสองโหมด (master and slave) และสำหรับทั้งสอง synchronizations (short และ long), จำนวนของบิตระหว่าง
           ข้อมูลสองชิ้นที่ติดตามกัน (และดังนั้น two synchronization signals) จำเป็นต้องถูกระบุ (DATLEN และ CHLEN bits ใน the
           SPI_I2SCFGR register) แม้ใน slave mode.
Title: Re: STM32F1 SPI
Post by: tha on December 26, 2020, 06:12:25 AM
25.4.3 Clock generator

The I2S bitrate กำหนด the dataflow บน the I2S data line และ the I2S clock signal frequency.

I2S bitrate = จำนวนของบิตต่อ channel x จำนวนของ channels x sampling audio frequency

สำหรับ a 16-bit audio, left และ right channel, the I2S bitrate ถูกคำนวณดังต่อไปนี้:
                     I2S bitrate = 16 x 2 x FS

มันจะเป็น: I2S bitrate = 32 x 2 x FS ถ้า the packet length คือ 32-bit wide.

(https://i.imgur.com/Bz6N4OW.png)

เมื่อ the master mode ถูกกำหนดค่า, a specific action จำเป็นต้องดำเนินการโปรแกรมอย่างเหมาะสม the linear divider เพื่อสื่อสารด้วย the desired audio frequency.

(https://i.imgur.com/HlvDA42.png)

Figure 268 แสดง the communication clock architecture. . The I2SxCLK source คือ the system clock (จัดให้มีโดย the HSI, the HSE or the PLL, และแหล่ง the AHB clock). สำหรับ connectivity line devices, The I2SxCLK source สามารถเป็นอย่างใดอย่างหนึ่ง SYSCLK หรือ the PLL3 VCO (2 × PLL3CLK) clock เพื่อให้สำเร็จ the maximum accuracy. การเลือกนี้ถูกทำ
โดยใช้ the I2S2SRC and I2S3SRC bits in the RCC_CFGR2 register.

The audio sampling frequency สามารถเป็น 96 kHz, 48 kHz, 44.1 kHz, 32 kHz, 22.05 kHz, 16 kHz, 11.025 kHz or 8 kHz (หรือค่าอื่นใดๆที่อยู่ในย่านนี้). เพื่อให้ถึง the desired frequency, the linear divider ต้องการถูกโปรแกรมให้สอดคล้องกับสูตรข้างล่าง:

เมื่อ the master clock ถูกสร้างขึ้น (MCKOE ใน the SPI_I2SPR register ถูกเซ็ต):
    FS = I2SxCLK / [(16*2)*((2*I2SDIV)+ODD)*8)] เมื่อ the channel frame คือ 16-bit wide
    FS = I2SxCLK / [(32*2)*((2*I2SDIV)+ODD)*4)] when the channel frame is 32-bit wide

เมื่อ the master clock ถูกปิดการใช้งาน (MCKOE bit ถูกเคลียร์):
    FS = I2SxCLK / [(16*2)*((2*I2SDIV)+ODD))] when the channel frame is 16-bit wide
    FS = I2SxCLK / [(32*2)*((2*I2SDIV)+ODD))] when the channel frame is 32-bit wide

Table 183, Table 184 and Table 185 จัดให้มีตัวอย่าง precision values สำหรับ different clock configurations.

Note: การกำหนดค่าอื่นๆสามารถเป็นได้ที่ยอมให้ optimum clock precision.

(https://i.imgur.com/fg73BiX.png)

(https://i.imgur.com/gHzzsHr.png)

(https://i.imgur.com/KusRdpu.png)

(https://i.imgur.com/btHZR1w.png)
Title: Re: STM32F1 SPI
Post by: tha on December 26, 2020, 08:07:29 AM
25.4.4 I2S master mode

The I2S สามารถถูกกำหนดค่าใน master mode สำหรับ transmission และ reception. นี้หมายความว่า the serial clock ถูกสร้างขึ้นบน the CK pin เช่นเดียวกันกับ the Word Select signal WS. Master clock (MCK) อาจถูกส่งออกหรือไม่, ขึ้นอยู่กับ the MCKOE bit ใน the SPI_I2SPR register.

Procedure

1.    เลือก the I2SDIV[7:0] bits ใน the SPI_I2SPR register เพื่อกำหนด the serial clock baud rate ให้ถึง the proper
       audio sample frequency. The ODD bit ใน the SPI_I2SPR register ก็ต้องถูกกำหนดด้วย.
2.    เลือก the CKPOL bit เพื่อกำหนด the steady level สำหรับ the communication clock. เซ็ต the MCKOE bit ใน the
       SPI_I2SPR register ถ้า the master clock MCK จำเป็นต้องถูกจัดให้มีไปยัง the external DAC/ADC audio component
       (the I2SDIV and ODD values ควรถูกคำนวณขึ้นอยู่กับสถานะของ the MCK output, สำหรับรายละเอียดที่มากขึ้นอ้างอิงถึง
       Section 25.4.3).
3.    เซ็ต the I2SMOD bit ใน SPI_I2SCFGR เพื่อทำให้ทำงาน the I2S functionalities และเลือก the I2S standard ผ่านทาง
       the I2SSTD[1:0] และ PCMSYNC bits, the data length ผ่านทาง the DATLEN[1:0] bits และจำนวนของบิตต่อ channel
       โดยการกำหนดค่า the CHLEN bit. เลือก the I2S master mode และ direction (Transmitter or Receiver) อีกด้วยผ่านทาง
       the I2SCFG[1:0] bits ใน the SPI_I2SCFGR register.
4.    ถ้าจำเป็น, เลือกทุก the potential interruption sources และ the DMA capabilities โดยการเขียน the SPI_CR2 register.
5.    The I2SE bit ใน SPI_I2SCFGR register ต้องถูกเซ็ต.

WS and CK ถูกกำหนดค่าใน output mode. MCK เป็น an output อีกด้วย, ถ้า the MCKOE bit ใน SPI_I2SPR ถูกเซ็ต.
Title: Re: STM32F1 SPI
Post by: tha on December 26, 2020, 09:42:41 AM
Transmission sequence

The transmission sequence เริ่มเมื่อ a half-word ถูกเขียนลงใน the Tx buffer.

สันนิษฐานว่า, the first data ถูกเขียนลงใน the Tx buffer ตรงกันกับ the channel Left data. เมื่อ data ถูกส่งถ่ายจาก the Tx buffer ไปยัง the shift register, TXE ถูกเซ็ตและ data ที่ตรงกันกับ the channel Right ต้องถูกเขียนลงใน the Tx buffer. The CHSIDE flag แสดงให้เห็นซึ่ง channel ที่ถูกส่ง. มันมีความหมายเมื่อ the TXE flag ถูกเซ็ต เพราะ the CHSIDE flag ถูกอัปเดตเมื่อ TXE ไปเป็น high.

A full frame ต้องถูกพิจารณาเป็น a Left channel data transmission ติดตามโดย a Right channel data transmission. มันเป็นไปไม่ได้ที่จะมี a partial frame ที่ the left channel เท่านั้นที่ถูกส่ง.

The data half-word ถูกโหลดแบบขนานลงใน the 16-bit shift register ในระหว่าง the first bit transmission, และจากนั้นถูกเลื่อนออก, แบบอนุกรม, ไปยัง the MOSI/SD pin, MSB first. The TXE flag ถูกเซ็ตหลังจากแต่ละ transfer จาก the Tx buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit in the SPI_CR2 register ถูกเซ็ต.

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the write operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2).

เพื่อให้แน่ใจว่า a continuous audio data transmission, มันมีข้อบังคับให้เขียน the SPI_DR ด้วย the next data ที่จะส่งก่อน the end ของ the current transmission.

เพื่อ switch off the I2S, โดยการเคลียร์ I2SE, มันมีข้อบังคับให้คอยสำหรับ TXE = 1 and BSY = 0.
Title: Re: STM32F1 SPI
Post by: tha on December 26, 2020, 12:08:14 PM
Reception sequence

โหมดการทำงานก็เหมือนกันกับสำหรับ the transmission mode ยกเว้นสำหรับข้อ 3 (อ้างอิงถึง the procedure ที่อธิบายใน Section 25.4.4), ที่ the configuration ควรถูกเซ็ต the master reception mode ผ่านทาง the I2SCFG[1:0] bits.

อย่างใดก็ตาม the data หรือ channel length, the audio data ถูกรับโดย 16-bit packets. นี้หมายความว่าแต่ละเวลาที่ the Rx buffer เต็ม, the RXNE flag ถูกเซ็ตและ an interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน SPI_CR2 register. ขึ้นอยู่กับ the data และ channel length configuration, the audio value ที่รับสำหรับ a right หรือ left channel อาจเป็นผลจากหนึ่งหรือสอง
receptions ลงใน the Rx buffer.

การเคลียร์ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register

CHSIDE ถูกอัปเดตหลังจากแต่ละ reception. มันมีความไวต่อ the WS signal ที่สร้างขึ้นโดย the I2S cell.

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the read operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2.

ถ้า data ถูกรับในขณะที่ดาต้าที่รับก่อนหน้ายังไม่ได้ถูกอ่าน, an overrun ถูกสร้างขึ้นและ the OVR flag ถูกเซ็ต. ถ้า the ERRIE bit ถูกเซ็ตใน the SPI_CR2 register, an interrupt ถูกสร้างเพื่อแสดง the error.

เพื่อปิด the I2S, การกระทำเฉพาะถูกต้องการเพื่อให้แน่ใจว่า the I2S เสร็จสมบูรณ์ the transfer cycle อย่างเหมาะสมโดยปราศจากการเริ่มต้น a new data transfer. The sequence ขึ้นอยู่กับ the configuration ของ the data and channel lengths, และบน the audio protocol mode ที่เลือก. ในกรณีของ:
•    16-bit data length ต่อขยายบน 32-bit channel length (DATLEN = 00 and CHLEN = 1) โดยใช้ the LSB justified
      mode (I2SSTD = 10)
      a)    คอยสำหรับลำดับที่สองก่อนถึงสุดท้าย RXNE = 1 (n – 1)
      b)    จากนั้นคอย 17 I2S clock cycles (โดยใช้ a software loop)
      c)    ปิดการใช้งาน the I2S (I2SE = 0)
•    16-bit data length ต่อขยายบน 32-bit channel length (DATLEN = 00 and CHLEN = 1) in MSB justified, I2S or
      PCM modes (I2SSTD = 00, I2SSTD = 01 or I2SSTD = 11,ตามลำดับ)
      a)    คอยสำหรับ the last RXNE
      b)    จากนั้นคอย 1 I2S clock cycle (โดยใช้ a software loop)
      c)    ปิดการใช้งาน the I2S (I2SE = 0)
•    สำหรับชุดการผสมอื่น ๆทั้งหมดของ DATLEN and CHLEN, อย่างใดก็ตาม the audio mode ที่เลือกผ่านทาง the I2SSTD bits,
      ดำเนินการตามลำดับต่อไปนี้เพื่อปิด the I2S:
      a)    คอยสำหรับลำดับที่สองก่อนถึงสุดท้าย RXNE = 1 (n – 1)
      b)    จากนั้นคอย one I2S clock cycle (โดยใช้ a software loop)
      c)    ปิดการใช้งาน the I2S (I2SE = 0)

Note: The BSY flag ต้องเป็น low ในระหว่าง transfers.
Title: Re: STM32F1 SPI
Post by: tha on December 26, 2020, 01:45:40 PM
25.4.5 I2S slave mode

ใน slave mode, the I2S สามารถถูกกำหนดค่าใน transmission or reception mode.The operating mode หลักๆจะทำตามกฎเดียวกันดังอธิบายสำหรับ the I2S master configuration. ใน slave mode, ไม่มี clock ที่ถูกสร้างขึ้นโดย the I2S interface. The clock and WS signals ถูกอินพุทจาก the external master ที่ต่อถึง the I2S interface. ไม่มีความจำเป็น, สำหรับผู้ใช้, ที่จะกำหนดค่า the clock.

The configuration steps ทำตามรายการข้างล่าง:
1.    เซ็ต the I2SMOD bit in the SPI_I2SCFGR register ให้ถึง the I2S functionalities และเลือก the I2S standard ผ่านทาง
       the I2SSTD[1:0] bits, the data length ผ่านทาง the DATLEN[1:0] bits และจำนวนของบิตต่อ channel สำหรับ the
       frame โดยการกำหนดค่า the CHLEN bit. เลือก the mode (transmission or reception) อีกด้วยสำหรับ the slave ผ่านทาง
       the I2SCFG[1:0] bits in SPI_I2SCFGR register.
2.    ถ้าจำเป็น, เลือกทุก the potential interruption sources และ the DMA capabilities โดยการเขียน the SPI_CR2 register.
3.    The I2SE bit in SPI_I2SCFGR register ต้องถูกเซ็ต.
Title: Re: STM32F1 SPI
Post by: tha on December 26, 2020, 05:03:47 PM
Transmission sequence

The transmission sequence เริ่มเมื่อ the external master device ส่ง the clock และเมื่อ the NSS_WS signal ร้องขอ the transfer ของ data. The slave ต้องถูก enabled ก่อน the external master สตาร์ท the communication. The I2S data register ต้องถูกโหลดก่อน the master เริ่มต้น the communication.

สำหรับ the I2S, MSB justified and LSB justified modes, the first data item ที่ถูกเขียนลงใน the data register ตรงกันกับ the data สำหรับ the left channel. เมื่อ the communication สตาร์ท, the data ถูกส่งถ่ายจาก the Tx buffer ไปยัง the shift register. The TXE flag จากนั้นถูกเซ็ตเพื่อร้องขอ the right channel data ให้ถูกเขียนลงใน the I2S data register.

The CHSIDE flag แสดงให้เห็นซึ่ง channel ที่ถูกส่ง. เปรียบเทียบกับ the master transmission mode, ใน slave mode, CHSIDE มีความไวต่อ the WS signal ที่มาจาก the external master. นี้หมายความว่า the slave จำเป็นต้องพร้อมส่ง the first data ก่อน the clock ถูกสร้างขึ้นโดย the master. WS assertion ตรงกันกับ left channel transmitted first.

Note: The I2SE ต้องถูกเขียนอย่างน้อย two PCLK cycles ก่อน the first clock of the master มาบน the CK line.

The data half-word ถูก parallel-loaded ลงใน the 16-bit shift register (จาก the internal bus) ในระหว่าง the first bit transmission, และจากนั้น shifted out แบบอนุกรมไปยัง the MOSI/SD pin MSB first. The TXE flag ถูกเซ็ตหลังจากแต่ละ transfer จาก the Tx buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต.

จำไว้ว่า the TXE flag ควรถูกเช็คว่าเป็น 1 ก่อนการพยายามที่จะเขียน the Tx buffer.

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the write operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2.

เพื่อความปลอดภัย a continuous audio data transmission, มันมีข้อบังคับให้เขียน the SPI_DR register ด้วย the next data ที่จะส่งก่อน the end of the current transmission. An underrun flag ถูเซ็ตและ an interrupt อาจถูกสร้างขึ้นถ้า the data ไม่ถูกเขียนลงใน the SPI_DR register ก่อน the first clock edge of the next data communication. นี้แสดงให้เห็นไปยัง the software ว่า the transferred data ผิดพลาด. ถ้า the ERRIE bit ถูกเซ็ตลงใน the SPI_CR2 register, an interrupt ถูกสร้างขึ้นเมื่อ the UDR flag in the SPI_SR register ไปเป็น high. ในกรณีนี้, มันมีข้อบังคับให้ปิด the I2S และให้รีสตาร์ท a data transfer โดยสตาร์ทจาก the left channel.

เพื่อปิด the I2S, โดยการเคลียร์ the I2SE bit, มันมีข้อบังคับให้คอยสำหรับ TXE = 1 and BSY = 0.
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 06:31:56 AM
Reception sequence.

โหมดการทำงานก็เหมือนกันกับสำหรับ the transmission mode ยกเว้นสำหรับข้อ 1 (อ้างอิงถึง the procedure ที่อธิบายใน Section 25.4.5), ที่ the configuration ควรเซ็ต the master reception mode ผโดยใช้ the I2SCFG[1:0] bits in the SPI_I2SCFGR register.

อย่างใดก็ตาม the data หรือ channel length, the audio data ถูกรับโดย 16-bit packets. นี้หมายความว่าแต่ละเวลาที่ the Rx buffer เต็ม, the RXNE flag ใน the SPI_SR register ถูกเซ็ตและ an interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน SPI_CR2 register. ขึ้นอยู่กับ the data และ channel length configuration, the audio value ที่รับสำหรับ a right หรือ left channel อาจเป็นผลจากหนึ่งหรือสอง receptions ลงใน the Rx buffer.

CHSIDE ถูกอัปเดตหลังจากแต่ละ data ถูกรับที่อ่านมาจาก SPI_DR. มันมีความไวต่อ the external WS line จัดการโดย the external master component

การเคลียร์ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the read operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2.

ถ้า data ถูกรับในขณะที่ดาต้าที่รับก่อนหน้านี้ยังไม่ได้ถูกอ่าน, an overrun ถูกสร้างขึ้นและ the OVR flag ถูกเซ็ต. ถ้า the ERRIE bit ถูกเซ็ตใน the SPI_CR2 register, an interrupt ถูกสร้างเพื่อแสดง the error.

เพื่อปิด the I2S in reception mode, I2SE ต้องถูกเคลียร์ทันทีหลังจากการรับ the last RXNE = 1.

Note: The external master components ควรมีความสามารถของ sending/receiving data in 16-bit or 32-bit packets ผ่าน
          an audio channel.
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 07:41:17 AM
25.4.6 Status flags

Three status flags ถูกจัดให้มีสำหรับ the application เพื่อการเฝ้ามองอย่างเต็มที่ the state of the I2S bus.

Busy flag (BSY)

BSY flag นี้ถูกเซ็ตและเคลียร์โดย hardware (การเขียนไปยัง flag นี้จะไม่มีผลอะไร). มันแสดงให้เห็นว่า the state ของ the communication layer ของ the I2S.

เมื่อ BSY ถูกเซ็ต, มันแสดงให้เห็นว่า the I2S กำลังยุ่งอยู่กับการสื่อสาร. มีข้อยกเว้นอันหนึ่งใน master receive mode (I2SCFG = 11) ที่ the BSY flag เป็น low ในระหว่าง reception.

The BSY flag ถูกใช้ให้เป็นประโยชน์เพื่อตรวจจับ the end ของ a transfer ถ้า the software ต้องการปิดการใช้งาน the  I2S. นี้หลีกเลี่ยงการเสียหาย the last transfer. สำหรับการนี้, ขั้นตอนที่อธิบายด้านล่างนี้ต้องปฏิบัติตามอย่างเคร่งครัด.

The BSY flag ถูกเซ็ตเมื่อ a transfer สตาร์ท, ยกเว้นเมื่อ the I2S is in master receiver mode..

The BSY flag ถูกเคลียร์:
•    เมื่อ a transfer เสร็จ (ยกเว้นใน master mode, ซึ่ง the communication ถูกนึกว่ามีความต่อเนื่อง)
•    เมื่อ the SPI ถูกปิดการใช้งาน

เมื่อ communication ต่อเนื่อง:
•    ใน master mode, the BSY flag เป็น high ในระหว่าง the transfers ทั้งหมด
•    ใน slave mode, the BSY flag เป็น low สำหรับ one I2S clock cycle ระหว่างแต่ละ transfer

Note: ไม่ควรใช้ the BSY flag เพื่อจัดการแต่ละ data transmission หรือ reception. มันดีกว่าที่จะใช้ the TXE and RXNE flags แทน.
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 08:33:37 AM
Tx buffer empty flag (TXE)

เมื่อเซ็ต, flag นี้แสดงให้เห็นว่า the Tx buffer ว่างอยู่และ the next data ที่จะถูกส่งสามารถถูกโหลดลงใน the buffer. The TXE flag ถูกรีเซ็ตเมื่อ the Tx buffer บรรจุดาต้าพร้อมแล้วที่จะถูกส่ง. มันถูกรีเซ็ตเมื่อ the I2S ถูก disabled อีกด้วย (I2SE bit ถูก reset).

Rx buffer not empty (RXNE)

เมื่อเซ็ต, flag นี้แสดงให้เห็นว่ามี valid received data ใน the Rx buffer. มันถูกเคลียร์เมื่อ SPI_DR ถูกอ่าน.
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 09:23:09 AM
Channel Side flag (CHSIDE)

ใน transmission mode, flag นี้ถูก refreshed เมื่อ TXE ไปเป็น high. มันแสดงให้เห็น the channel side ที่ the data จะส่งถ่ายบน SD ต้องเป็นของ. ในกรณีของ an underrun error event ใน slave transmission mode, flag ไม่น่าเชื่อถือและ I2S จำเป็นต้องถูก switched off และ switched on ก่อนเริ่มต้นใหม่ the communication.

ใน reception mode, flag นี้ถูก refreshed เมื่อ data ถูกรับลงใน SPI_DR. มันแสดงให้เห็นจากที่ channel side data ถูกรับแล้ว. จำไว้ว่าในกรณ๊ของ (เช่น OVR) flag นี้จะไม่มีความหมายและ the I2S ควรถูก reset โดยการปิดการใช้งานและจากนั้นเปิดการใช้งานมัน (พร้อมด้วยการกำหนดค่าถ้าจำเป็นต้องเปลี่ยน).

flag นี้ไม่มีความหมายใน the PCM standard (สำหรับทั้ง Short and Long frame modes).

เมื่อ the OVR or UDR flag in the SPI_SR ถูกเซ็ตและ the ERRIE bit in SPI_CR2 ถูกเซ็ตด้วย, an interrupt ถูกสร้างขึ้น. interrupt สามารถถูกเคลียร์โดยการอ่าน the SPI_SR status register (เมื่อ the interrupt source ถูกเคลียร์แล้ว).
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 10:23:36 AM
25.4.7 Error flags

มี two error flags สำหรับ the I2S cell.

Underrun flag (UDR)

ใน slave transmission mode, flag นี้ถูกเซ็ตเมื่อ the first clock สำหรับ data transmission ปรากฎในขณะที่ the software ยังไม่โหลดค่าใดๆลงใน SPI_DR. มันมีให้ใช้เมื่อ the I2SMOD bit ใน SPI_I2SCFGR ถูกเซ็ต. An interrupt อาจถูกสร้างขึ้นถ้า the ERRIE bit ใน SPI_CR2 ถูกเซ็ต.

The UDR bit ถูกเคลียร์โดย a read operation บน the SPI_SR register.

Overrun flag (OVR)

flag นี้ถูกเซ็ตเมื่อ data ถูกรับและ the previous data ยังไม่ได้ถูกอ่านจาก SPI_DR. เป็นผลให้, the incoming data สูญหาย. An interrupt อาจถูกสร้างขึ้นถ้า the ERRIE bit ถูกเซ็ตใน SPI_CR2.

ในกรณีนี้, the receive buffer contents ไม่ถูกอัปเดตด้วย the newly received data จาก the transmitter device. A read operation ไปยัง the SPI_DR register ส่งคืนกลับ the previouscorrectly received data. การส่ง half-words ที่ตามมาอื่นๆทั้งหมดจะสูญหาย.

การเคลียร์ the OVR bit ถูกทำโดย a read operation บน the SPI_DR register ติดตามโดย a read เข้าถึงไปยัง the SPI_SR register.
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 10:29:24 AM
25.4.8 I2S interrupts

Table 186 จัดให้มีรายการของ I2S interrupts.

(https://i.imgur.com/J9GnHnC.png)
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 10:34:52 AM
25.4.9 DMA features

DMA จริงๆแล้วก็ทำงานในวิธีเดียวกันกับสำหรับ the SPI mode. ไม่มีความแตกต่างบน the I2S. มีเพียง the CRC feature ที่ไม่มีให้ใช้ใน I2S mode เนื่องจากไม่มี data transfer protection system
Title: Re: STM32F1 SPI
Post by: tha on December 27, 2020, 03:22:07 PM
25.5 SPI and I2S registers

The peripheral registers ต้องถูกเข้าถึงโดย half-words (16 bits) or words (32 bits).

(https://i.imgur.com/M9QLufV.png)

Bit 15 BIDIMODE: Bidirectional data mode enable
     0: 2-line unidirectional data mode ถูกเลือก
     1: 1-line bidirectional data mode ถูกเลือก
Note: บิตนี้ไม่ถูกใช้ใน I2S mode

Bit 14 BIDIOE: Output enable in bidirectional mode
บิตนี้รวมกับ the BIDImode bit จะเลือก the direction of transfer in bidirectional mode
     0: Output ถูกปิดการใช้งาน (receive-only mode)
     1: Output ถูกเปิดการใช้งาน (transmit-only mode)
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.
           ใน master mode, the MOSI pin ถูกใช้ขณะที่ the MISO pin ถูกใช้ใน slave mode.

Bit 13 CRCEN: Hardware CRC calculation enable
     0: CRC calculation ถูกปิดการใช้งาน
     1: CRC calculation ถูกเปิดการใช้งาน
Note: บิตนี้ควรถูกเขียนเฉพาะเมือ่ SPI ถูกปิดการใช้งาน (SPE = '0') เพื่อการทำงานที่ถูกต้อง.
           มันไม่ถูกใช้ใน I2S mode.

Bit 12 CRCNEXT: CRC transfer next
     0: Data phase (ไม่มี CRC phase)
     1: Next transfer คือ CRC (CRC phase)
Note: เมื่อ the SPI ถูกกำหนดค่าใน full duplex หรือ transmitter only modes, CRCNEXT ต้องถูกเขียนทันทีที่ the last data ถูก
           เขียนไปยัง the SPI_DR register.
           เมื่อ the SPI ถูกกำหนดค่าใน receiver only mode, CRCNEXT ต้องถูกเซ็ตหลังจาก the second last data reception.
           บิตนี้ควรถูกเคลียร์เมื่อ the transfers ถูกจัดการโดย DMA.
           มันไม่ถูกใช้ใน I2S mode.

Bit 11 DFF: Data frame format
     0: 8-bit data frame format ถูกเลือกสำหรับ transmission/reception
     1: 16-bit data frame format ถูกเลือกสำหรับ transmission/reception
Note: บิตนี้ควรถูกเขียนเฉพาะเมือ่ SPI ถูกปิดการใช้งาน (SPE = '0') เพื่อการทำงานที่ถูกต้อง.
           มันไม่ถูกใช้ใน I2S mode.

Bit 10 RXONLY: Receive only
บิตนี้รวมกับ the BIDImode bit เลือก the direction of transfer in 2-line unidirectional mode. บิตนี้ยังมีประโยชน์ใน a multislave system ซึ่ง particular slave นี้ไม่ได้ถูกเข้าถึง, the output จาก the accessed slave ไม่ถูกทำให้เสียหาย.
     0: Full duplex (Transmit and receive)
     1: Output ถูกปิดการใช้งาน (Receive-only mode)
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.

Bit 9 SSM: Software slave management
เมื่อ the SSM bit ถูกเซ็ต, the NSS pin input ถูกแทนที่ด้วย the value จาก the SSI bit.
     0: Software slave management ถูกปิดการใช้งาน
     1: Software slave management ถูกเปิดการใช้งาน
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.

Bit 8 SSI: Internal slave select
บิตนี้มีผลเฉพาะเมื่อ the SSM bit ถูกเซ็ต. ค่าของบิตนี้ถูกบังคับลงบน the NSS pin และ the IO value ของ the NSS pin ถูกเพิกเฉย.
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.

Bit 7 LSBFIRST: Frame format
     0: MSB transmitted first
     1: LSB transmitted first
Note: บิตนี้ไม่ควรถูกเปลี่ยนเมื่อ communication กำลังดำเนินการ.
           มันไม่ถูกใช้ใน I2S mode.

Bit 6 SPE: SPI enable
     0: Peripheral ถูกปิดการใช้งาน
     1: Peripheral ถูกเปิดการใช้งาน
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.
           เมื่อปิดการใช้งาน the SPI, ทำตามขั้นตอนที่อธิบายใน Section 25.3.8.

Bits 5:3 BR[2:0]: Baud rate control
     000: fPCLK/2
     001: fPCLK/4
     010: fPCLK/8
     011: fPCLK/16
     100: fPCLK/32
     101: fPCLK/64
     110: fPCLK/128
     111: fPCLK/256
Note: บิตเหล่านี้ไม่ควรถูกเปลี่ยนเมื่อ communication กำลังดำเนินการ.
           พวกมันไม่ถูกใช้ใน I2S mode.

Bit 2 MSTR: Master selection
     0: Slave configuration
     1: Master configuration
Note: บิตนี้ไม่ควรถูกเปลี่ยนเมื่อ communication กำลังดำเนินการ.
           มันไม่ถูกใช้ใน I2S mode.

Bit1 CPOL: Clock polarity
     0: CK เป็น 0 เมื่อ idle
     1: CK เป็น 1 เมื่อ idle
Note: บิตนี้ไม่ควรถูกเปลี่ยนเมื่อ communication กำลังดำเนินการ.
           มันไม่ถูกใช้ใน I2S mode.

Bit 0 CPHA: Clock phase
     0: The first clock transition คือ the first data capture edge
     1: The second clock transition คือ the first data capture edge
Note: บิตนี้ไม่ควรถูกเปลี่ยนเมื่อ communication กำลังดำเนินการ.
           มันไม่ถูกใช้ใน I2S mode.
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 05:20:53 AM
(https://i.imgur.com/HqZILPT.png)

Bits 15:8 Reserved, must be kept at reset value.

Bit 7 TXEIE: Tx buffer empty interrupt enable
     0: TXE interrupt ถูกบัง
     1: TXE interrupt ไม่ถูกบัง. ถูกใช้เพื่อสร้าง an interrupt request เมื่อ the TXE flag ถูกเซ็ต.

Bit 6 RXNEIE: RX buffer not empty interrupt enable
     0: RXNE interrupt ถูกบัง
     1: RXNE interrupt ไม่ถูกบัง. ถูกใช้เพื่อสร้าง an interrupt request เมื่อ the RXNE flag ถูกเซ็ต.

Bit 5 ERRIE: Error interrupt enable
บิตนี้ควบคุมการสร้างขึ้นของ an interrupt เมื่อ an error condition เกิดขึ้น (CRCERR, OVR, MODF in SPI mode and UDR, OVR in I2S mode).
     0: Error interrupt ถูกบัง
     1: Error interrupt ถูกเปิดการใช้งาน

Bits 4:3 Reserved, must be kept at reset value.

Bit 2 SSOE: SS output enable
     0: SS output ถูกปิดการใช้งานใน master mode และ the cell สามารถทำงานใน multimaster configuration
     1: SS output ถูกเปิดการใช้งานใน master mode และเมื่อ the cell ถูกเปิดการใช้งาน. The cell ไม่สามารถทำงานใน a
          multimaster environment.
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.

Bit 1 TXDMAEN: Tx buffer DMA enable
เมื่อบิตนี้ถูกเซ็ต, the DMA request ถูกทำเมื่อไหร่ก็ตามที่ the TXE flag ถูกเซ็ต.
     0: Tx buffer DMA ถูกปิดการใช้งาน
     1: Tx buffer DMA ถูกเปิดการใช้งาน

Bit 0 RXDMAEN: Rx buffer DMA enable
เมื่อบิตนี้ถูกเซ็ต, the DMA request ถูกทำเมื่อไหร่ก็ตามที่ the RXNE flag ถูกเซ็ต.
     0: Rx buffer DMA ถูกปิดการใช้งาน
     1: Rx buffer DMA ถูกเปิดการใช้งาน
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 05:51:38 AM
(https://i.imgur.com/8YL6YDP.png)

Bits 15:8 Reserved, must be kept at reset value.

Bit 7 BSY: Busy flag
     0: SPI (or I2S) ไม่ยุ่งอยู่
     1: SPI (or I2S) ยุ่งอยู่ใน communication หรือ Tx buffer ไม่ว่าง
flag นี้เซ็ตและเคลียร์โดย hardware.
Note: BSY flag ต้องถูกใช้อย่างระมัดระวัง: อ้างอิงถึง Section 25.3.7 and Section 25.3.8.

Bit 6 OVR: Overrun flag
     0: ไม่มี overrun เกิดขึ้น
     1: Overrun เกิดขึ้น
flag นี้เซ็ตโดย hardware และรีเซ็ตโดย a software sequence. Refer to Section 25.4.7 for the software sequence.

Bit 5 MODF: Mode fault
     0: ไม่มี mode fault เกิดขึ้น
     1: Mode fault เกิดขึ้น
flag นี้เซ็ตโดย hardware และรีเซ็ตโดย a software sequence. Refer to Section 25.3.10 for the software sequence.
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.

Bit 4 CRCERR: CRC error flag
     0: CRC value ที่รับเท่ากันกับ the SPI_RXCRCR value
     1: CRC value ที่รับไม่เท่ากันกับ the SPI_RXCRCR value
Note: บิตนี้ไม่ถูกใช้ใน I2S mode.

Bit 3 UDR: Underrun flag
     0: ไม่มี underrun เกิดขึ้น
     1: Underrun เกิดขึ้น
flag นี้เซ็ตโดย hardware และรีเซ็ตโดย a software sequence. Refer to Section 25.4.7 for the software sequence.
Note: บิตนี้ไม่ถูกใช้ใน SPI mode.

Bit 2 CHSIDE: Channel side
     0: Channel Left ต้องถูกส่งหรือได้รับแล้ว
     1: Channel Right ต้องถูกส่งหรือได้รับแล้ว
Note: บิตนี้ไม่ถูกใช้สำหรับ SPI mode และไม่มีความหมายใน PCM mode.

Bit 1 TXE: Transmit buffer empty
     0: Tx buffer ไม่ว่าง
     1: Tx buffer ว่าง

Bit 0 RXNE: Receive buffer not empty
     0: Rx buffer ว่าง
     1: Rx buffer ไม่ว่าง
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 06:14:07 AM
(https://i.imgur.com/4w5w19n.png)

Bits 15:0 DR[15:0]: Data register
Data ถูกรับหรือถูกส่ง.
The data register ถูกแยกเป็น 2 buffers - หนึ่งสำหรับการเขียน (Transmit Buffer) และอีกอันหนึ่งสำหรับการอ่าน (Receive buffer). การเขียนไปยัง the data register จะเขียนลงใน the Tx buffer และการอ่านจาก the data register จะคืนกลับค่าที่อยู่ใน the Rx buffer.

Note: หมายเหตุเหล่านี้ใช้กับ SPI mode:
           ขึ้นอยู่กับ the data frame format selection bit (DFF in SPI_CR1 register), the data ส่งหรือรับเป็นอย่างใดอย่างหนึ่ง
           8-bit or 16-bit. การเลือกนี้ต้องถูกทำก่อนเปิดการใช้งาน the SPI เพื่ให้แน่ใจว่าการทำงานถูกต้อง.

           สำหรับ an 8-bit data frame, the buffers เป็น 8-bit และเฉพาะ the LSB of the register (SPI_DR[7:0]) ถูกใช้สำหรับ
           transmission/reception. เมื่อ in reception mode, the MSB ของ the register (SPI_DR[15:8]) ถูก forced ให้เป็น
           0.

           สำหรับ a 16-bit data frame, the buffers เป็น 16-bit และ the entire register, SPI_DR[15:0] ถูกใช้สำหรับ
           transmission/reception.
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 07:25:00 AM
(https://i.imgur.com/QFgQ9au.png)

Bits 15:0 CRCPOLY[15:0]: CRC polynomial register
register นี้บรรจุ the polynomial สำหรับ the CRC calculation.
The CRC polynomial (0007h) คือ the reset value ของ register นี้. polynomial อื่นๆสามารถถูกกำหนดค่าตามที่ต้องการ.
Note: บิตเหล่านี้ไม่ถูกใช้สำหรับ the I2S mode.
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 08:42:26 AM
(https://i.imgur.com/L0vMpeA.png)

Bits 15:0 RXCRC[15:0]: Rx CRC register
เมื่อ CRC calculation ถูกเปิดการใช้งาน, the RxCRC[15:0] bits บรรจุ the computed CRC value ของ the subsequently received bytes. register นี้ถูก reset เมื่อ the CRCEN bit ใน SPI_CR1 register ถูกเขียนให้เป็น 1. The CRC ถูกคำนวณแบบอนุกรมโดยใช้ the polynomial ที่โปรแกรมใน the SPI_CRCPR register

เฉพาะ the 8 LSB bits ถูกพิจารณาเมื่อ the data frame format ถูกเซ็ตเป็น 8-bit data (DFF bit of SPI_CR1 ถูกเคลียร์). CRC calculation ถูกทำบนฐานของ CRC8 standard ใดๆ.

The entire 16-bits ของ register นี้ถูกพิจารณาเมื่อ a 16-bit data frame format ถูกเลือก (DFF bit of the SPI_CR1 register ถูกเซ็ต). CRC calculation ถูกทำบนฐานของ CRC16 standard ใดๆ.

Note: การอ่านไปยัง register นี้เมื่อ the BSY Flag ถูกเซ็ตอาจส่งคืนกลับ an incorrect value.
           บิตเหล่านี้ไม่ถูกใช้สำหรับ the I2S mode.
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 08:50:11 AM
(https://i.imgur.com/AdvzQW7.png)

Bits 15:0 TXCRC[15:0]: Tx CRC register
เมื่อ CRC calculation ถูกเปิดการใช้งาน, the TxCRC[7:0] bits บรรจุ the computed CRC value ของ the subsequently received bytes. register นี้ถูก reset เมื่อ the CRCEN bit ใน SPI_CR1 register ถูกเขียนให้เป็น 1. The CRC ถูกคำนวณแบบอนุกรมโดยใช้ the polynomial ที่โปรแกรมใน the SPI_CRCPR register

เฉพาะ the 8 LSB bits ถูกพิจารณาเมื่อ the data frame format ถูกเซ็ตเป็น 8-bit data (DFF bit of SPI_CR1 ถูกเคลียร์). CRC calculation ถูกทำบนฐานของ CRC8 standard ใดๆ.

The entire 16-bits ของ register นี้ถูกพิจารณาเมื่อ a 16-bit data frame format ถูกเลือก (DFF bit of the SPI_CR1 register ถูกเซ็ต). CRC calculation ถูกทำบนฐานของ CRC16 standard ใดๆ.

Note: การอ่านไปยัง register นี้เมื่อ the BSY Flag ถูกเซ็ตอาจส่งคืนกลับ an incorrect value.
           บิตเหล่านี้ไม่ถูกใช้สำหรับ the I2S mode.
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 10:09:41 AM
(https://i.imgur.com/bAtmClZ.png)

Bits 15:12 Reserved, must be kept at reset value.

Bit 11 I2SMOD: I2S mode selection
     0: SPI mode ถูกเลือก
     1: I2S mode ถูกเลือก
Note: บิตนี้ควรถูกกำหนดค่าเมื่อ the SPI or I2S ปิดการใช้งาน

Bit 10 I2SE: I2S Enable
     0: I2S peripheral ถูกปิดการใช้งาน
     1: I2S peripheral ถูกเปิดการใช้งาน
Note: บิตนี้ไม่ถูกใช้ใน SPI mode.

Bits 9:8 I2SCFG: I2S configuration mode
     00: Slave - transmit
     01: Slave - receive
     10: Master - transmit
     11: Master - receive
Note: บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน
           มันไม่ถูกใช้ใน SPI mode.

Bit 7 PCMSYNC: PCM frame synchronization
     0: Short frame synchronization
     1: Long frame synchronization
Note: บิตนี้มีความหมายเพียงถ้า I2SSTD = 11 (PCM standard ถูกใช้)
           มันไม่ถูกใช้ใน SPI mode.

Bit 6 Reserved: forced at 0 by hardware

Bits 5:4 I2SSTD: I2S standard selection
     00: I2S Philips standard.
     01: MSB justified standard (left justified)
     10: LSB justified standard (right justified)
     11: PCM standard
สำหรับรายละเอียดที่มากขึ้นบน I2S standards, อ้างอิงถึง to Section 25.4.2. ไม่ถูกใช้ใน SPI mode.
Note: สำหรับการทำงานที่ถูกต้อง, บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน

Bit 3 CKPOL: Steady state clock polarity
     0: I2S clock steady state เป็น low level
     1: I2S clock steady state เป็น high level
Note: สำหรับการทำงานที่ถูกต้อง, บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน
           บิตนี้ไม่ถูกใช้ใน SPI mode.

Bits 2:1 DATLEN: Data length to be transferred
     00: 16-bit data length
     01: 24-bit data length
     10: 32-bit data length
     11: Not allowed
Note: สำหรับการทำงานที่ถูกต้อง, บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน
           บิตนี้ไม่ถูกใช้ใน SPI mode.

Bit 0 CHLEN: Channel length (number of bits per audio channel)
     0: 16-bit wide
     1: 32-bit wide
The bit write operation มีความหมายเพียงถ้า DATLEN = 00 มิฉะนั้น the channel length ถูกคงที่ไว้ที่ 32-bit โดย hardware ไม่ว่าจะใส่ค่าใดก็ตาม. ไม่ถูกใช้ใน SPI mode.
Note: สำหรับการทำงานที่ถูกต้อง, บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 10:57:43 AM
(https://i.imgur.com/z66Bo1G.png)

Bits 15:10 Reserved, must be kept at reset value.

Bit 9 MCKOE: Master clock output enable
     0: Master clock output ถูกปิดการใช้งาน
     1: Master clock output ถูกเปิดการใช้งาน
Note: บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน. มันถูกใช้เพียงเมื่อ the I2S อยู่ใน master mode.
           บิตนี้ไม่ถูกใช้ใน SPI mode.

Bit 8 ODD: Odd factor for the prescaler
     0: real divider value คือ = I2SDIV *2
     1: real divider value คือ = (I2SDIV * 2)+1
อ้างอิงถึง Section 25.4.3: Clock generator. ไม่ถูกใช้ใน SPI mode.
Note: บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน. มันถูกใช้เพียงเมื่อ the I2S อยู่ใน master mode.

Bits 7:0 I2SDIV: I2S Linear prescaler
     I2SDIV [7:0] = 0 หรือ I2SDIV [7:0] = 1 เป็นค่าต้องห้าม.
อ้างอิงถึง Section 25.4.3. ไม่ถูกใช้ใน SPI mode.
Note: บิตนี้ควรถูกกำหนดค่าเมื่อ the I2S ปิดการใช้งาน. มันถูกใช้เพียงเมื่อ the I2S อยู่ใน master mode.
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 11:00:42 AM
(https://i.imgur.com/SKUit0w.png)
Title: Re: STM32F1 SPI
Post by: tha on December 28, 2020, 01:25:56 PM
-