1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-12-21 12:29:51 +00:00

sound_pwm

This commit is contained in:
Stephen Crane 2018-09-11 16:06:52 +01:00
parent 14d3ad3e69
commit 9cfa09eade
3 changed files with 70 additions and 2 deletions

View File

@ -14,8 +14,8 @@
#define TFT_RST 26
// PS/2 keyboard
#define KBD_DATA 14
#define KBD_IRQ 0
#define KBD_DATA 34
#define KBD_IRQ 35
// SPI-RAM
#undef SPIRAM_CS
@ -26,6 +26,8 @@
// sound
#define DAC_SOUND 25
#define PWM_SOUND 25
#define PWM_DUTY 20 // 20/1024 -> volume
bool hardware_reset();
void hardware_init(class CPU &);

54
sound_pwm.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "sound_pwm.h"
#include "hardware.h"
#if defined(ESP32)
#include <driver/ledc.h>
#define CHANNEL LEDC_CHANNEL_0
#define TIMER LEDC_TIMER_0
#define SPEED_MODE LEDC_HIGH_SPEED_MODE
void PWM::begin(unsigned gpio) {
ledc_timer_config_t timer_conf;
timer_conf.duty_resolution = LEDC_TIMER_10_BIT;
timer_conf.freq_hz = 440;
timer_conf.speed_mode = SPEED_MODE;
timer_conf.timer_num = TIMER;
ESP_ERROR_CHECK(::ledc_timer_config(&timer_conf));
ledc_channel_config_t ledc_conf;
ledc_conf.channel = CHANNEL;
ledc_conf.duty = 0;
ledc_conf.gpio_num = gpio;
ledc_conf.intr_type = LEDC_INTR_DISABLE;
ledc_conf.speed_mode = SPEED_MODE;
ledc_conf.timer_sel = TIMER;
ESP_ERROR_CHECK(::ledc_channel_config(&ledc_conf));
}
void PWM::set_duty(unsigned duty) {
ESP_ERROR_CHECK(::ledc_set_duty(SPEED_MODE, CHANNEL, duty));
ESP_ERROR_CHECK(::ledc_update_duty(SPEED_MODE, CHANNEL));
}
void PWM::stop() {
set_duty(0);
}
void PWM::set_freq(unsigned freq) {
ESP_ERROR_CHECK(::ledc_set_freq(SPEED_MODE, TIMER, freq));
}
#else
void PWM::begin(unsigned gpio) {
}
void PWM::start(unsigned duty) {
}
void PWM::stop() {
}
void PWM::set_freq(unsigned freq) {
}
#endif

12
sound_pwm.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __SOUND_PWM_H__
#define __SOUND_PWM_H__
class PWM {
public:
void begin(unsigned gpio);
void set_duty(unsigned duty);
void stop();
void set_freq(unsigned freq);
};
#endif