A method, device and medium for playing audio based on SPI controller
By combining the SPI controller and the DMA controller, the high cost and resource occupation problems of the audio playback solution in the payment cloud speaker are solved, and low-cost and efficient audio playback is achieved, which is suitable for most MCU chip platforms.
Patent Information
- Application Number
- CN202311812571.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-26
- Publication Date
- 2025-09-23
- Estimated Expiration
- 2043-12-26
AI Technical Summary
Existing audio playback solutions in payment cloud speakers have problems such as high cost, high resource usage, and noise interference. In particular, when using PWM signals to play 16kHz sampling frequency, 16-bit PCM audio files, it results in high CPU resource usage and possible noise generation.
The method of playing audio using the SPI controller is to sample the audio data at the set sampling frequency through the analog-to-digital converter, convert it into PCM format, and convert the value of the audio file into PWM data. The transmission rate of the SPI controller is configured to be 24MHz, and the DMA controller and buffer memory are used to realize the playback of audio files.
It reduces product cost and power consumption, releases CPU resources, ensures audio playback quality and speed, improves product competitiveness in the market, and is suitable for most MCU chip platforms.
Smart Images

Figure CN117765985B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of audio playback, and in particular to a method, device and medium for playing audio based on an SPI controller. Background Art
[0002] The payment cloud speaker is an intelligent terminal that integrates payment collection, broadcasting, marketing and services. It broadcasts payment information by connecting to the Internet. It has the characteristics of loud voice, long standby time, faster broadcasting and simple operation. It is widely used in convenience stores, fast food restaurants, supermarkets, vegetable markets and other scenarios.
[0003] Audio playback is a key feature of payment cloud speaker products. Existing technologies primarily employ two approaches: one uses a built-in or external audio module (AD converter and DA converter) for playback. The AD converter samples the continuous analog audio signal at a specific sampling frequency and quantizes it into a discrete digital audio signal. During playback, the DA converter converts the digital audio signal into an analog audio signal at the original sampling frequency and outputs it. This solution relies on an audio module, resulting in high costs, and some chips lack a built-in DA converter. Another approach uses PWM (pulse width modulation) signals for audio playback, which requires high-frequency and high-priority interrupts, preventing the CPU from responding to other tasks.
[0004] Audio files usually use A / D conversion to convert continuous voltage signals into PCM through Pulse Code Modulation (PCM). Sampling and quantization are required. The main indicators are as follows:
[0005] Sampling Rate: The number of samples collected per unit time, i.e., the reciprocal of the sampling period, which refers to the time interval between two samples. The higher the sampling rate, the better the sound quality, but also the greater the bandwidth occupied.
[0006] Sampling bit count: The number of binary bits representing a sample, that is, the number of bits used to represent each sample point. Computer audio quantization depths are typically 4, 8, 16, or 32 bits. For example, with an 8-bit sampling depth, each sample point can represent 256 different sample values. The number of sampling bits affects sound quality. The more sampling bits, the closer the quantized waveform is to the original waveform, resulting in higher sound quality, but also requires more storage space.
[0007] The audio files played by the Payment Cloud Speaker are usually 16-bit PCM files, with a sampling frequency of 16kHz, a bit depth of 16 bits, mono, raw data, and 16-bit PCM encoding.
[0008] Playing audio means using the DAC to output the corresponding data at the original sampling rate. Using PWM to play a 16kHz sampling frequency, 16-bit PCM audio file requires starting a 16kHz timer and 80kHz PWM. The PWM duty cycle is changed in the timer interrupt at a frequency of 16kHz, thereby changing the output voltage value to achieve audio playback at a 16kHz sampling rate. This solution is widely used in low-cost products, but it has two problems:
[0009] 1. When changing the PWM duty cycle, you need to restart the PWM clock. Turning off the PWM clock first and then turning it on will cause an interval delay when switching the duty cycle. This may be a chip problem, which will cause obvious noise to be heard.
[0010] 2. It takes up a lot of resources. During audio playback, it is necessary to enter an interrupt at a frequency of 16kHz to modify the duty cycle. To ensure playback quality, the interrupt priority needs to be set high, and the CPU cannot handle other tasks. Summary of the Invention
[0011] In view of this, an object of the present invention is to provide a method for playing audio based on an SPI controller.
[0012] In order to achieve the above technical objectives, the technical solution adopted by the present invention is:
[0013] The present invention provides a method for playing audio based on an SPI controller, comprising the following steps:
[0014] Step 1: Sample audio data at a set sampling frequency;
[0015] Step 2: Convert the audio data into a PCM audio file.
[0016] Step 3: Convert the value of the audio file into PWM data;
[0017] Step 4: Configure the transmission rate of the SPI controller according to the sampling frequency of the audio file;
[0018] Step 5: Convert the PWM data into SPI data;
[0019] Step 6: Use multiple buffer memories to write corresponding SPI data, and send the SPI data to the SPI controller in turn through the DMA controller;
[0020] Step 7: The SPI controller continuously sends the SPI data at the configured transmission rate to achieve playback of the audio file.
[0021] Furthermore, the step 1 is specifically as follows: a continuous audio analog signal is sampled and quantized into a discrete audio digital signal by an analog-to-digital converter at a set sampling frequency fs, wherein the audio digital signal is the original audio data; the set sampling frequency fs is 16kHz.
[0022] Furthermore, the step 2 specifically includes:
[0023] Step 21: Analyze the format of the audio data;
[0024] Step 22: Determine whether the format of the audio data is PCM format. If so, go directly to step 3; otherwise, use an audio encoding tool to convert the non-PCM format audio data into an audio file in PCM format; the audio encoding tool uses Adobe Audition software.
[0025] Furthermore, the step 3 specifically includes:
[0026] Step 31: setting the number of bits of the audio file and determining a value range according to the number of bits of the audio file;
[0027] Step 32: Determine the number of bytes required by the SPI controller based on the value range, ensuring that each bit of each byte corresponds to each number in the value range;
[0028] Step 33: Determine whether the audio file needs to be compressed based on the size of the audio file. If so, convert the value of the audio digital signal into PWM data corresponding to the duty cycle of the analog PWM according to the compression ratio; otherwise, directly use the value of the audio digital signal as the PWM data.
[0029] Furthermore, the step 31 is specifically as follows:
[0030] The number of bits of the audio file is 16, and the value of the audio digital signal is represented by 16-bit binary. The value range of the audio digital signal is 0 to 65535, with a total of 65536 values. The audio digital signal is also divided into 65536 layers in terms of amplitude.
[0031] The step 32 is specifically as follows:
[0032] Sending one cycle of PWM data requires 65536 bits, that is, the SPI controller needs to use 8192 bytes to ensure that each bit of each byte corresponds exactly to each bit in the value range;
[0033] The step 33 is specifically as follows:
[0034] Step 331: compress the 16-bit audio file to 9 bits. The value range corresponding to 9 bits is 0 to 511. Sending one cycle of PWM data requires 512 bits, that is, 64 bytes.
[0035] Step 332: Define the value of the audio digital signal as x, and the converted value as y, and then obtain the conversion formula as: y=x×(512 / 65536)=x / 128;
[0036] Step 333: Use the converted value y as PWM data.
[0037] Furthermore, the step 4 is specifically as follows:
[0038] When the number of bits of the audio file is 16 bits and the sampling frequency fs is 16 kHz, the transmission rate of the SPI controller is set to 24 MHz.
[0039] Furthermore, the step 5 specifically includes:
[0040] Step 51: One cycle of the PWM data is 512 bits. The SPI controller determines the value to be sent for each bit in each byte based on the PWM data. The value to be sent for each bit is 0 or 1. If the specific value of the PWM data is m, 0≤m≤512, then the value to be sent for the first m bits of the 512 bits in one cycle is 1, and the value to be sent for the last 512-m bits is 0.
[0041] Step 52: Each bit in the 512 bits of a cycle is set to 0 or 1 according to the value to be sent, thereby obtaining 512 bits of SPI data.
[0042] Furthermore, the step 6 specifically includes:
[0043] Step 61: Initialize at least two buffer memories, each buffer memory being used to store one cycle of SPI data.
[0044] Step 62: Configure the size of the buffer memory to 188 bytes * 20 to ensure that the writing speed of the buffer memory is greater than the sending speed of the DMA controller;
[0045] Step 63: After the first buffer memory completes writing the corresponding SPI data, the SPI data is sent to the SPI controller via the DMA controller; while the DMA controller is sending the SPI data in the first buffer memory, the second buffer memory is written, and after the second buffer memory completes writing the corresponding SPI data, the SPI data is sent to the SPI controller via the DMA controller; while the DMA controller is sending the SPI data in the second buffer memory, the first buffer memory is written again to implement cyclic sending.
[0046] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the method for playing audio based on the SPI controller as described above is implemented.
[0047] The present invention also provides a computer-readable storage medium having a computer program stored thereon. When the program is executed by a processor, the method for playing audio based on the SPI controller as described above is implemented.
[0048] By adopting the above technical solution, the present invention has the following beneficial effects compared with the prior art:
[0049] 1. Similar to the PWM audio playback solution, the output voltage is varied by varying the duty cycle at a 16kHz frequency based on the audio data. Using SPI to play PCM audio files significantly reduces product cost and power consumption by eliminating codec chips, while maintaining audio playback quality and speed without sacrificing product quality. This significantly enhances product competitiveness in the fiercely competitive cloud audio market. This invention is highly adaptable and compatible with most commercially available MCU chip platforms.
[0050] SPI uses a serial shift register to shift bytes one by one through the MOSI signal line and transmits them to the slave device. Each bit can only send 0 or 1. In order to make it send a PWM duty cycle signal, it is necessary to control the number of 0 and 1 it sends per unit time, and the transmission rate of SPI data must match the sampling frequency of the audio file. When using a 16-bit audio file, sending a PWM requires 64 bytes. The audio sampling rate is 16kHz, 16kHz*64*8bit=8.192M. The playback frequency must be more than twice the sampling frequency, so the transmission rate of the SPI controller is set to 24MHz. The same PWM data is sent three times in a cycle. In order to make the transmission rate of the SPI controller closer to the audio playback rate, the same PWM data is sent as a full 64kb for the first time, and the first 62 bytes of the array are sent for the second and third times. This method is closest to the actual playback rate.
[0051] 2. To free up some CPU resources when playing audio files, a DMA controller is used. The DMA controller sends data without CPU intervention, freeing up CPU resources for other operations. Two buffer memories are set up, each with a size of 188 bytes * 20, ensuring that the write speed of the buffer memories is greater than the send speed of the DMA controller. When the DMA controller is sending to the first buffer memory, it can write to the second buffer memory, and when sending to the second buffer memory, it can write to the first buffer memory, achieving a circular transmission. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0053] Figure 1 This is a flowchart of an execution method for playing audio based on an SPI controller provided by an embodiment of the present invention.
[0054] Figure 2 is a schematic diagram of an electronic device provided by an embodiment of the present invention.
[0055] Figure 3 It is a schematic diagram of a computer-readable storage medium provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0056] The present invention will be described in further detail below with reference to the accompanying drawings and examples. It is particularly noted that the following examples are intended only to illustrate the present invention and are not intended to limit the scope of the present invention. Similarly, the following examples are only some embodiments of the present invention and are not intended to be exhaustive. All other embodiments obtained by those of ordinary skill in the art without creative effort are intended to fall within the scope of protection of the present invention.
[0057] The overall design key points of the present invention are: using SPI to implement PWM function, selecting 16-bit PCM format audio files, setting the transmission rate of the SPI controller to 24MHz, and configuring the DMA controller with two buffer memories of size 188 bytes * 20, about 3.7kb.
[0058] See Figure 1 The present invention provides a method for playing audio based on an SPI controller, comprising the following steps:
[0059] Step 1: Sample audio data at a set sampling frequency;
[0060] In this embodiment, step 1 specifically involves sampling a continuous analog audio signal at a set sampling frequency fs using an analog-to-digital converter and quantizing it into a discrete digital audio signal, where the digital audio signal is the original audio data. The set sampling frequency fs is 16 kHz. The audio data is sampled to provide a data source for subsequent playback. Using a 16 kHz sampling frequency improves sampling efficiency and performance.
[0061] Step 2: Convert the audio data into a PCM audio file (PCM file, PCM (Pulse Code Modulation) is a digital audio coding technology used to convert analog audio signals into digital form for storage or transmission);
[0062] In this embodiment, step 2 specifically includes:
[0063] Step 21: Analyze the format of the audio data;
[0064] Step 22: Determine whether the audio data is in PCM format. If so, proceed directly to step 3. Otherwise, use an audio encoding tool to convert the audio data in a non-PCM format (such as MP3, WAV, FLAC, etc.) into a PCM audio file. The audio encoding tool uses Adobe Audition software. All different formats are unified into PCM files to facilitate subsequent unified processing.
[0065] Step 3, converting the value of the audio file into PWM data (PWM (Pulse Width Modulation) is a modulation technology used to control signals, voltages or currents in electronic devices. It simulates analog signals by changing the pulse width of the signal and is commonly used in digital-to-analog conversion (DAC) or analog-to-digital conversion (ADC) in electronic systems. The basic principle of PWM technology is to control the average value of the output signal by adjusting the duty cycle of the pulse. The duty cycle of the pulse refers to the ratio of the high-level time of the pulse to the cycle time in one cycle. By changing the duty cycle, different output effects can be achieved). In the present invention, the data in the PCM file is used as the duty cycle data of PWM to adjust the size of the output signal, thereby changing the amplitude of the speaker;
[0066] In this embodiment, step 3 specifically includes:
[0067] Step 31: setting the number of bits of the audio file and determining a value range according to the number of bits of the audio file;
[0068] In this embodiment, step 31 is specifically as follows:
[0069] If the number of bits of the audio file is 16, the value of the audio digital signal is represented by 16-bit binary. The value range of the audio digital signal is 0 to 65535, with a total of 65536 values. The amplitude of the audio digital signal is also divided into 65536 layers. If 16 bits is selected, the effect will be better.
[0070] Step 32: Determine the number of bytes required by the SPI controller based on the value range, ensuring that each bit of each byte corresponds to each number in the value range;
[0071] In this embodiment, step 32 is specifically as follows:
[0072] Sending one cycle of PWM data requires 65536 bits, that is, the SPI controller needs to use 8192 bytes to ensure that each bit of each byte corresponds exactly to each bit in the value range;
[0073] Step 33: Determine whether the audio file needs to be compressed based on the size of the audio file. If so, convert the value of the audio digital signal into PWM data corresponding to the duty cycle of the analog PWM according to the compression ratio; otherwise, directly use the value of the audio digital signal as the PWM data.
[0074] In this embodiment, step 33 is specifically as follows:
[0075] Step 331: compress the 16-bit audio file to 9 bits. The value range corresponding to 9 bits is 0 to 511. Sending one cycle of PWM data requires 512 bits, or 64 bytes. Since 16 bits are used, the amount of data is too large to be fully simulated, so the data needs to be compressed. Experience shows that the effect of 8 bits is not very good. Therefore, in order to ensure the effect and control the data amount, it is chosen to compress it to 9 bits for processing.
[0076] Step 332: Define the value of the audio digital signal as x, and the converted value as y, then the conversion formula is: y = x × (512 / 65536) = x / 128; the converted value will be smaller, making subsequent processing easier;
[0077] Step 333: Use the converted value y as PWM data. Assume that the value x of the audio digital signal is 291 and the converted value y is 2. Therefore, the first two bits in one cycle (512 bits) are set to 1 and the rest are set to 0. In this way, the output waveform has the first two bits at a high level and the last 510 bits at a low level in one cycle (512 bits), thereby simulating the PWM duty cycle.
[0078] Step 4: Configure the transmission rate of the SPI controller according to the sampling frequency of the audio file;
[0079] In this embodiment, step 4 is specifically as follows:
[0080] When the number of bits of the audio file is 16 bits and the sampling frequency fs is 16 kHz, the transmission rate of the SPI controller is set to 24 MHz.
[0081] If you use an SPI controller to simulate PWM audio output, you must consider adjusting the period and duty cycle. The SPI controller uses a serial shift register to shift each byte out to the slave device via the MOSI signal line. Each bit can only be 0 or 1. To ensure that the SPI controller generates a PWM duty cycle signal, you must control the number of 0s and 1s it transmits per unit time. Furthermore, the SPI controller's transmission rate must match the sampling frequency of the audio file.
[0082] The SPI controller of the embedded platform used in the present invention is obtained by dividing 192 MHz by 2 / 4 / 8 / 16, and the transmission rate of the SPI controller can be adjusted to 3 / 6 / 12 / 24M.
[0083] There is a close relationship between the SPI clock frequency and the transmission rate. The clock frequency determines the data transmission rate, that is, the number of bits that can be transmitted per second.
[0084] The transmission rate (or data transmission rate) is usually expressed in bits per second (bps). The relationship between it and the clock frequency can be calculated using the following formula:
[0085] Transmission rate = clock frequency / division factor;
[0086] Therefore, by controlling the SPI controller's clock frequency and the frequency division factor, the transfer rate of the SPI controller interface can be adjusted. If the SPI controller's clock frequency is 192MHz and the frequency division factor is 8, the transfer rate is 24Mbps (megabits per second). This means that in this case, the SPI controller can transfer 25 million bits per second.
[0087] The SPI controller analog output PWM transmission rate formula is as follows:
[0088] SPI controller transmission rate (bps) = PWM frequency * PWM duty cycle gear;
[0089] If the audio is played at a sampling rate of 16kHz, that is, the PWM frequency is 16kHz, the theoretical transmission rate of the SPI controller is 16kHz*512=81920000bps=8.192Mbps;
[0090] When playing voice at a 16kHz sampling rate, the sound can be played, but there is a 16kHz harmonic that can be heard by the human ear. Therefore, a higher frequency PWM is required. The maximum PWM frequency should be at least twice the maximum frequency of the human ear (20Khz), but the data should still be updated at 16kHz. The PWM frequency should be at least twice the sampling frequency (not including twice).
[0091] Therefore, the PWM frequency is set to three times of 16kHz, and the same PWM duty cycle is output three times, that is, 48kHz. The transmission rate of the SPI controller is 48kHz*512, which is set to 24.576MHz.
[0092] However, the SPI controller of the present invention does not support the 24.576MHz rate. The closest is 24MHz, with a PWM gear of 1500, which is approximately 188 bytes (1500 / 8). As mentioned above, a 48kHz PWM frequency is obtained by outputting the same 16kHz PWM cycle three times, that is, the same 64-byte data is sent three times, totaling 192 bytes (1536 bits). In order to make the transmission rate of the SPI controller closer to the audio playback rate, it is necessary to discard 4 bytes of data. The same PWM data is sent the full 64 bytes the first time, and the first 62 bytes are sent the second and third times. This method is closest to the actual playback rate.
[0093] The actual playback frequency is (64+62+62)*16kHz*8=24.064MHz. It takes 188 bytes to send a complete PWM data.
[0094] Step 5: Convert the PWM data into SPI data;
[0095] In this embodiment, step 5 specifically includes:
[0096] Step 51: One cycle of the PWM data is 512 bits. The SPI controller determines the value to be sent for each bit in each byte based on the PWM data. The value to be sent for each bit is 0 or 1. If the specific value of the PWM data is m, 0≤m≤512, then the value to be sent for the first m bits of the 512 bits in one cycle is 1, and the value to be sent for the last 512-m bits is 0.
[0097] For example, if the PWM data is 2, the first two bits in one cycle (512 bits) are set to 1, and the rest are set to 0. In this way, the output waveform has the first two bits as high level and the last 510 bits as low level in one cycle (512 bits), thus simulating the PWM duty cycle.
[0098] Step 52: Each bit in the 512 bits of a cycle is set to 0 or 1 according to the value to be sent, thereby obtaining 512 bits of SPI data.
[0099] Step 6: Use multiple buffer memories to write corresponding SPI data, and send the SPI data to the SPI controller in turn through the DMA controller;
[0100] In this embodiment, step 6 specifically includes:
[0101] Step 61: Initialize at least two buffer memories, each buffer memory being used to store one cycle of SPI data.
[0102] Step 62: Configure the size of the buffer memory to be 188 bytes * 20 to ensure that the writing speed of the buffer memory is greater than the sending speed of the DMA controller; it is necessary to test the time to fill a buffer memory with data to ensure that the time required for the buffer memory to write data is much less than the time required for the DMA controller to send. When a buffer memory is set to 188 bytes * 20, which is approximately 3.7 kb, the time required to write data is approximately 616.62 us. The actual time required to trigger the DMA controller transmission completion interrupt after completing the sending of a buffer memory is 1.28 ms. In this way, the time required for the buffer memory to write data is much less than the time required for the DMA controller to send; before starting the SPI controller to send data for the first time, fill two buffer memories first, set the block transfer length of the DMA controller to 188 * 20, and after the block transfer is completed, trigger the DMA controller transmission completion interrupt to write the next buffer memory to ensure that the DMA controller can transmit data normally.
[0103] Step 63: After the first buffer memory has finished writing the corresponding SPI data, the SPI data is sent to the SPI controller via the DMA controller. While the DMA controller is sending the SPI data in the first buffer memory, it writes to the second buffer memory. After the second buffer memory has finished writing the corresponding SPI data, it sends the SPI data to the SPI controller via the DMA controller. While the DMA controller is sending the SPI data in the second buffer memory, it again writes to the first buffer memory, thereby implementing cyclic transmission. Using a DMA controller enables data transmission between the buffer memory and the SPI controller, freeing up some CPU resources when playing audio files. The DMA controller does not require CPU intervention when sending data, freeing up CPU resources for other operations. Because the time required for writing data to the two buffer memories is much shorter than the time required for sending data by the DMA controller, cyclic transmission is implemented by switching between the two buffer memories for writing operations, thereby improving transmission efficiency.
[0104] Step 7: The SPI controller continuously transmits SPI data at the configured transmission rate to enable audio file playback. Specifically, the SPI controller uses a serial shift register to continuously transmit SPI data bit by bit via the MOSI signal line at the configured transmission rate to enable audio file playback. Playing PCM audio files using SPI can significantly reduce product cost and power consumption by saving codec chips, while ensuring audio playback quality and speed without compromising product quality. This significantly improves product competitiveness in the fiercely competitive payment cloud audio market. This invention is highly applicable and compatible with the vast majority of MCU chip platforms on the market.
[0105] like Figure 2 As shown, an embodiment of the present invention further provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned method for playing audio based on the SPI controller when executing the program.
[0106] like Figure 3 As shown, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which implements the above-mentioned method for playing audio based on the SPI controller when the program is executed by a processor.
[0107] In addition, the functional units in various embodiments of the present invention may be integrated into a single processing unit, each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0108] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) or a processor to execute all or part of the steps of the various embodiments of the present invention. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0109] The above descriptions are only some embodiments of the present invention and do not limit the scope of protection of the present invention. Any equivalent device or equivalent process transformation made by using the contents of the description and drawings of the present invention, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A method for playing audio based on an SPI controller, characterized in that: The steps include: Step 1: Sample audio data at a set sampling frequency; Step 2: Convert the audio data into a PCM audio file. Step 3: Convert the value of the audio file into PWM data; Step 4: Configure the transmission rate of the SPI controller according to the sampling frequency of the audio file. Specifically, when the number of bits of the audio file is 16 bits and the sampling frequency fs is 16 kHz, set the transmission rate of the SPI controller to 24 MHz. The same PWM data is sent three times in a loop. The first time, the full 64 KB of the same PWM data is sent. The second and third times, the first 62 bytes of the array are sent. The formula for the analog output PWM transmission rate of the SPI controller is as follows: The transmission rate of the SPI controller = PWM frequency * PWM duty cycle; Step 5: Convert the PWM data into SPI data; Step 6: Use multiple buffer memories to write corresponding SPI data, and send the SPI data to the SPI controller in turn through the DMA controller; Step 7: The SPI controller continuously sends the SPI data at the configured transmission rate to achieve playback of the audio file; the SPI controller uses the serial shift register to continuously send the SPI data one bit at a time through the MOSI signal line at the configured transmission rate.
2. A method for playing audio based on an SPI controller as claimed in claim 1, characterized in that, The step 1 is specifically as follows: a continuous audio analog signal is sampled and quantized into a discrete audio digital signal by an analog-to-digital converter at a set sampling frequency fs, wherein the audio digital signal is the original audio data; the set sampling frequency fs is 16 kHz.
3. A method for playing audio based on an SPI controller as claimed in claim 1, characterized in that, The step 2 specifically includes: Step 21: Analyze the format of the audio data; Step 22: Determine whether the format of the audio data is PCM format. If so, go directly to step 3; otherwise, use an audio encoding tool to convert the non-PCM format audio data into an audio file in PCM format; the audio encoding tool uses Adobe Audition software.
4. A method for playing audio based on an SPI controller as claimed in claim 2, characterized in that, The step 3 specifically includes: Step 31: setting the number of bits of the audio file and determining a value range according to the number of bits of the audio file; Step 32: Determine the number of bytes required by the SPI controller based on the value range, ensuring that each bit of each byte corresponds to each number in the value range; Step 33: Determine whether the audio file needs to be compressed based on the size of the audio file. If so, convert the value of the audio digital signal into PWM data corresponding to the duty cycle of the analog PWM according to the compression ratio; otherwise, directly use the value of the audio digital signal as the PWM data.
5. A method for playing audio based on an SPI controller as claimed in claim 4, characterized in that, The step 31 is specifically as follows: The number of bits of the audio file is 16, and the value of the audio digital signal is represented by 16-bit binary. The value range of the audio digital signal is 0-65535, with a total of 65536 values. The audio digital signal is also divided into 65536 layers in terms of amplitude. The step 32 is specifically as follows: Sending one cycle of PWM data requires 65536 bits, that is, the SPI controller needs to use 8192 bytes to ensure that each bit of each byte corresponds exactly to each bit in the value range; The step 33 is specifically as follows: Step 331: compress the 16-bit audio file to 9 bits. The value range corresponding to 9 bits is 0-511. Sending one cycle of PWM data requires 512 bits, that is, 64 bytes. Step 332: Define the value of the audio digital signal as x, and the converted value as y, then the conversion formula is: y = x × (512 / 65536) = x / 128; Step 333: Use the converted value y as PWM data.
6. A method for playing audio based on an SPI controller as claimed in claim 1, characterized in that: The step 5 specifically includes: Step 51: One cycle of the PWM data is 512 bits. The SPI controller determines the value to be sent for each bit in each byte based on the PWM data. The value to be sent for each bit is 0 or 1. If the specific value of the PWM data is m, 0≤m≤512, then the value to be sent for the first m bits of the 512 bits in one cycle is 1, and the value to be sent for the last 512-m bits is 0. Step 52: Each bit in the 512 bits of a cycle is set to 0 or 1 according to the value to be sent, thereby obtaining 512 bits of SPI data.
7. A method for playing audio based on an SPI controller as claimed in claim 6, characterized in that: The step 6 specifically includes: Step 61: Initialize at least two buffer memories, each buffer memory being used to store one cycle of SPI data. Step 62: Configure the size of the buffer memory to 188 bytes * 20 to ensure that the writing speed of the buffer memory is greater than the sending speed of the DMA controller; Step 63: After the first buffer memory completes writing the corresponding SPI data, the SPI data is sent to the SPI controller via the DMA controller; while the DMA controller is sending the SPI data in the first buffer memory, the second buffer memory is written, and after the second buffer memory completes writing the corresponding SPI data, the SPI data is sent to the SPI controller via the DMA controller; while the DMA controller is sending the SPI data in the second buffer memory, the first buffer memory is written again to implement cyclic sending.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the method for playing audio based on an SPI controller as described in any one of claims 1 to 7 is implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method for playing audio based on an SPI controller as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Method and device for driving digital speaker based on code conversion
CN102684701A
PWM (Pulse Width Modulation) signal generation method, chip and electronic equipment
CN114416610A