Skip to content

Commit dd484ab

Browse files
committed
pbio/drv/sound/sound_ev3: Implement beep-only driver
1 parent 708f682 commit dd484ab

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

lib/pbio/drv/sound/sound_ev3.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,90 @@
77

88
#include <stdint.h>
99

10-
void pbdrv_sound_stop() {
10+
#include <pbdrv/gpio.h>
11+
12+
#include <tiam1808/ehrpwm.h>
13+
#include <tiam1808/hw/soc_AM1808.h>
14+
#include <tiam1808/hw/hw_syscfg0_AM1808.h>
15+
#include <tiam1808/hw/hw_types.h>
16+
#include <tiam1808/psc.h>
17+
18+
#include "../drv/gpio/gpio_ev3.h"
1119

20+
// The division factor of 40 gives decent coverage of the frequency range
21+
// 64 Hz - 24 kHz that the other drivers support.
22+
// (This is limited by the 16-bit period counter.)
23+
#define PWM_TIMEBASE_FREQ (SOC_EHRPWM_0_MODULE_FREQ / 40)
24+
25+
// Audio amplifier enable
26+
static const pbdrv_gpio_t pin_sound_en = PBDRV_GPIO_EV3_PIN(13, 3, 0, 6, 15);
27+
// Audio output pin
28+
#define SYSCFG_PINMUX3_PINMUX3_7_4_GPIO0_0 0
29+
static const pbdrv_gpio_t pin_audio = PBDRV_GPIO_EV3_PIN(3, 7, 4, 0, 0);
30+
31+
void pbdrv_sound_stop() {
32+
// Turn speaker amplifier off
33+
pbdrv_gpio_out_low(&pin_sound_en);
34+
// Clean up counter
35+
HWREGH(SOC_EHRPWM_0_REGS + EHRPWM_TBCTL) |= EHRPWM_TBCTL_CTRMODE_STOPFREEZE;
36+
EHRPWMWriteTBCount(SOC_EHRPWM_0_REGS, 0);
1237
}
1338

1439
void pbdrv_beep_start(uint32_t frequency, uint16_t sample_attenuator) {
40+
// This hardware doesn't implement volume control
41+
(void)sample_attenuator;
1542

43+
if (frequency < 64) {
44+
frequency = 64;
45+
}
46+
if (frequency > 24000) {
47+
frequency = 24000;
48+
}
49+
50+
uint32_t pwm_period = (PWM_TIMEBASE_FREQ + frequency / 2) / frequency;
51+
52+
// Program PWM to generate a square wave of this frequency
53+
EHRPWMLoadCMPB(SOC_EHRPWM_0_REGS, pwm_period / 2, true, 0, true);
54+
EHRPWMPWMOpPeriodSet(SOC_EHRPWM_0_REGS, pwm_period, EHRPWM_COUNT_UP, true);
55+
56+
// Turn speaker amplifier on
57+
pbdrv_gpio_out_high(&pin_sound_en);
1658
}
1759

1860
void pbdrv_sound_init() {
61+
// Turn on EPWM
62+
PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_EHRPWM, PSC_POWERDOMAIN_ALWAYS_ON, PSC_MDCTL_NEXT_ENABLE);
63+
64+
// The stop function performs various initializations
65+
pbdrv_sound_stop();
66+
67+
// Program a timebase division factor of /40
68+
// Don't use EHRPWMTimebaseClkConfig because its calculation algorithm
69+
// isn't very good and is also tricky to fix.
70+
HWREGH(SOC_EHRPWM_0_REGS + EHRPWM_TBCTL) = (HWREGH(SOC_EHRPWM_0_REGS + EHRPWM_TBCTL) & ~(EHRPWM_TBCTL_CLKDIV | EHRPWM_TBCTL_HSPCLKDIV)) |
71+
(EHRPWM_TBCTL_CLKDIV_DIVBY4 << EHRPWM_TBCTL_CLKDIV_SHIFT) |
72+
(EHRPWM_TBCTL_HSPCLKDIV_DIVBY10 << EHRPWM_TBCTL_HSPCLKDIV_SHIFT);
73+
// Program EPWM to generate the desired wave shape
74+
// Pulse goes high @ t=0
75+
// Pulse goes low @ t=CMPB
76+
EHRPWMConfigureAQActionOnB(
77+
SOC_EHRPWM_0_REGS,
78+
EHRPWM_AQCTLB_ZRO_EPWMXBHIGH,
79+
EHRPWM_AQCTLB_PRD_DONOTHING,
80+
EHRPWM_AQCTLB_CAU_DONOTHING,
81+
EHRPWM_AQCTLB_CAD_DONOTHING,
82+
EHRPWM_AQCTLB_CBU_EPWMXBLOW,
83+
EHRPWM_AQCTLB_CBD_DONOTHING,
84+
EHRPWM_AQSFRC_ACTSFB_DONOTHING
85+
);
86+
// Disable unused features
87+
EHRPWMDBOutput(SOC_EHRPWM_0_REGS, EHRPWM_DBCTL_OUT_MODE_BYPASS);
88+
EHRPWMChopperDisable(SOC_EHRPWM_0_REGS);
89+
EHRPWMTZTripEventDisable(SOC_EHRPWM_0_REGS, false);
90+
EHRPWMTZTripEventDisable(SOC_EHRPWM_0_REGS, true);
1991

92+
// Configure IO pin mode
93+
pbdrv_gpio_alt(&pin_audio, SYSCFG_PINMUX3_PINMUX3_7_4_EPWM0B);
2094
}
2195

2296
#endif // PBDRV_CONFIG_SOUND_EV3

0 commit comments

Comments
 (0)