Add pwm sound sample code

This commit is contained in:
cuu 2025-03-29 16:42:05 +08:00
parent 188f2ca65b
commit c99316b917
6 changed files with 3547 additions and 3 deletions

View File

@ -14,7 +14,7 @@ include_directories(
add_subdirectory(i2ckbd)
add_subdirectory(lcdspi)
add_subdirectory(pwm_sound)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/rp2040-psram rp2040-psram)
add_executable(picocalc_helloworld
@ -47,6 +47,7 @@ target_link_libraries(picocalc_helloworld
i2ckbd
lcdspi
rp2040-psram
pwm_sound
)
pico_enable_stdio_usb(picocalc_helloworld 0)

View File

@ -6,10 +6,11 @@
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/clocks.h"
#include "i2ckbd.h"
#include "lcdspi.h"
#include "psram_spi.h"
#include "pwm_sound.h"
const uint LEDPIN = 25;
psram_spi_inst_t* async_spi_inst;
@ -174,9 +175,39 @@ int psram_test(psram_spi_inst_t*psram_spi){
lcd_print_string("PSRAM testing done\n");
}
#include "sample.h"
int wav_position = 0;
void pwm_interrupt_handler() {
int slice_l = pwm_gpio_to_slice_num(AUDIO_PIN_L);
int slice_r = pwm_gpio_to_slice_num(AUDIO_PIN_R);
pwm_clear_irq(slice_l);
pwm_clear_irq(slice_r);
if (wav_position < (WAV_DATA_LENGTH << 3) - 1) {
pwm_set_chan_level(pwm_gpio_to_slice_num(AUDIO_PIN_L), PWM_CHAN_A, WAV_DATA[wav_position >> 3]);
pwm_set_chan_level(pwm_gpio_to_slice_num(AUDIO_PIN_R), PWM_CHAN_B, WAV_DATA[wav_position >> 3]);
wav_position++;
} else {
wav_position = 0; // Stop
pwm_set_chan_level(slice_l, PWM_CHAN_A, 0);
pwm_set_chan_level(slice_r, PWM_CHAN_B, 0);
pwm_set_irq_enabled(slice_l, false);
pwm_set_irq_enabled(slice_r, false);
irq_remove_handler(PWM_IRQ_WRAP, pwm_interrupt_handler);
//
}
}
int main() {
stdio_init_all();
set_sys_clock_khz(133000, true);
init_i2c_kbd();
lcd_init();
@ -190,10 +221,13 @@ int main() {
sleep_ms(500);
gpio_put(LEDPIN, 0);
psram_spi_inst_t psram_spi = psram_spi_init_clkdiv(pio1, -1,1.0f,true);
init_pwm(pwm_interrupt_handler);
psram_spi_inst_t psram_spi = psram_spi_init_clkdiv(pio1, -1,1.0f,true);
psram_test(&psram_spi);
while (1) {
int c = lcd_getc(0);
if(c != -1 && c > 0) {
lcd_putc(0,c);

View File

@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)
project(pwm_sound
VERSION 0.0.1
DESCRIPTION "pwm_sound for rp2040."
)
add_library(pwm_sound INTERFACE)
target_sources(pwm_sound INTERFACE
pwm_sound.c
)
target_link_libraries(pwm_sound INTERFACE pico_stdlib hardware_pwm)
target_include_directories(pwm_sound INTERFACE ${CMAKE_CURRENT_LIST_DIR})

View File

@ -0,0 +1,33 @@
#include "pwm_sound.h"
void init_pwm(irq_handler_t my_handler) {
gpio_set_function(AUDIO_PIN_L, GPIO_FUNC_PWM);
gpio_set_function(AUDIO_PIN_R, GPIO_FUNC_PWM);
int slice_l = pwm_gpio_to_slice_num(AUDIO_PIN_L);
int slice_r = pwm_gpio_to_slice_num(AUDIO_PIN_R);
pwm_clear_irq(slice_l);
pwm_clear_irq(slice_r);
pwm_set_irq_enabled(slice_l, true);
pwm_set_irq_enabled(slice_r, true);
irq_set_exclusive_handler(PWM_IRQ_WRAP, my_handler);
irq_set_enabled(PWM_IRQ_WRAP, true);
pwm_config config = pwm_get_default_config();
pwm_config_set_clkdiv(&config, 6.05f); // 133MHz
pwm_config_set_wrap(&config, 250);
pwm_init(slice_l, &config, true);
pwm_init(slice_r, &config, true);
pwm_set_chan_level(slice_l, PWM_CHAN_A, 0);
pwm_set_chan_level(slice_r, PWM_CHAN_B, 0);
}

View File

@ -0,0 +1,15 @@
#ifndef PWM_SOUND_H
#define PWM_SOUND_H
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include <stdio.h>
#define PWM_CLOCK_KHZ 133000
#define AUDIO_PIN_L 26
#define AUDIO_PIN_R 27
void init_pwm(irq_handler_t);
#endif //PWM_SOUND_H

File diff suppressed because it is too large Load Diff