Skip to content

Commit

Permalink
Added beeps, fixed squelch, more robust audio subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej committed Oct 9, 2024
1 parent 5f15570 commit d5a0288
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 27 deletions.
2 changes: 1 addition & 1 deletion openrtx/src/core/openrtx.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void openrtx_init()
gfx_init(); // Initialize display and graphics driver
kbd_init(); // Initialize keyboard driver
ui_init(); // Initialize user interface
// vp_init(); // Initialize voice prompts
vp_init(); // Initialize voice prompts
#ifdef CONFIG_SCREEN_CONTRAST
display_setContrast(state.settings.contrast);
#endif
Expand Down
2 changes: 1 addition & 1 deletion openrtx/src/core/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void *ui_threadFunc(void *arg)
ui_saveState(); // Save local state copy
pthread_mutex_unlock(&state_mutex); // Unlock r/w access to radio state

// vp_tick(); // continue playing voice prompts in progress if any.
vp_tick(); // continue playing voice prompts in progress if any.

// If synchronization needed take mutex and update RTX configuration
if(sync_rtx)
Expand Down
6 changes: 3 additions & 3 deletions openrtx/src/core/voicePrompts.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ static bool beep_tick()

void vp_init()
{
return;

#ifdef PLATFORM_A36PLUS
#ifdef VP_USE_FILESYSTEM
if(vpFile == NULL)
vpFile = fopen("voiceprompts.vpc", "r");
Expand All @@ -373,7 +374,7 @@ void vp_init()
{
loadVpToC();
}

#endif
if (vpDataLoaded)
{
// If the hash key is down, set vpLevel to high, if beep or less.
Expand Down Expand Up @@ -564,7 +565,6 @@ void vp_play()

void vp_tick()
{
return;
if (platform_getPttStatus() && (voicePromptActive || (currentBeepDuration > 0)))
{
vp_stop();
Expand Down
4 changes: 2 additions & 2 deletions openrtx/src/rtx/rtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void rtx_task()
// uint8_t height = (rssi + 160) / 2;
// Macro for log2, not using the math library
#define log2(x) (31 - __builtin_clz(x))
uint8_t height = ((rssi + 160)*log2(22-rssi>>1))>>3;
uint8_t height = ((rssi + 160)*log2(22 - (rssi>>1) )) >> 3;
state.spectrum_data[i] = height;
// set peak value
if(rssi > state.spectrum_peakRssi)
Expand All @@ -185,7 +185,7 @@ void rtx_task()
break;
}
rssi = radio_getRssi();
height = ((rssi + 160)*log2(22-rssi>>1))>>3;
height = ((rssi + 160)*log2(22 - (rssi>>1) )) >> 3;
state.spectrum_data[i] = height;
// give the UI a chance to refresh
state.spectrum_shouldRefresh = true;
Expand Down
1 change: 1 addition & 0 deletions openrtx/src/ui/default/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,7 @@ void ui_updateFSM(bool *sync_rtx)
// Cycle over the available modulations
state.settings.rx_modulation = !state.settings.rx_modulation;
bk4819_set_modulation(state.settings.rx_modulation); // required for instant feedback
radio_updateConfiguration();
}
break;
case R_STEP:
Expand Down
14 changes: 12 additions & 2 deletions platform/drivers/audio/audio_A36Plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <interfaces/audio.h>
#include <peripherals/gpio.h>
#include <bk4819.h>

const struct audioDevice outputDevices[] =
{
Expand Down Expand Up @@ -54,9 +55,14 @@ void audio_connect(const enum AudioSource source, const enum AudioSink sink)
* speaker power amplifier to hear analog fm audio.
*/
if (source == SOURCE_RTX && sink == SINK_SPK)
gpio_setPin(MIC_SPK_EN); // open speaker
{
radio_enableAfOutput();
}
gpio_setPin(MIC_SPK_EN); // open speaker

}


void audio_disconnect(const enum AudioSource source, const enum AudioSink sink)
{
/*
Expand All @@ -65,7 +71,11 @@ void audio_disconnect(const enum AudioSource source, const enum AudioSink sink)
* speaker power amplifier to squelch noise.
*/
if (source == SOURCE_RTX && sink == SINK_SPK)
gpio_clearPin(MIC_SPK_EN); // open microphone
{
radio_disableAfOutput();
//gpio_clearPin(MIC_SPK_EN); // open microphone
}

}

bool audio_checkPathCompatibility(const enum AudioSource p1Source,
Expand Down
4 changes: 2 additions & 2 deletions platform/drivers/baseband/bk4819.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,10 @@ typedef enum
// } bk4819_RX_ACG_GAIN;

static void spi_write_byte(uint8_t data);
static void spi_write_half_word(uint16_t data);
static uint16_t spi_read_half_word(void);

uint16_t ReadRegister(unsigned char reg);
static void WriteRegister(bk4819_reg_t reg, uint16_t data);
void WriteRegister(bk4819_reg_t reg, uint16_t data);

/**
* @brief Get interrupt
Expand Down Expand Up @@ -358,6 +357,7 @@ uint8_t bk4819_get_vox(void);
*/
void bk4819_set_Squelch(uint8_t RTSO, uint8_t RTSC, uint8_t ETSO, uint8_t ETSC, uint8_t GTSO, uint8_t GTSC);

void BK4819_SetAF(uint8_t AF);

/**
* @brief Get RSSI value 0.5dB/step
Expand Down
11 changes: 5 additions & 6 deletions platform/drivers/baseband/radio_A36Plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ void radio_init(const rtxStatus_t* rtxState)


// gpio_clearPin(BK1080_EN);
gpio_clearPin(MIC_SPK_EN);
bk4819_init();
BK4819_SetAF(0);

//bk4819_enable_freq_scan(BK4819_SCAN_FRE_TIME_2);
// bk4819_enable_vox(0, 0x10, 0x30, 0x30);
Expand Down Expand Up @@ -137,12 +137,12 @@ bool radio_checkRxDigitalSquelch()

void radio_enableAfOutput()
{
gpio_setPin(MIC_SPK_EN);
bk4819_set_modulation(config->modulation);
}

void radio_disableAfOutput()
{
gpio_clearPin(MIC_SPK_EN);
BK4819_SetAF(0);
}

void radio_checkVOX(){
Expand Down Expand Up @@ -184,7 +184,6 @@ void radio_enableRx()
if (config->rxToneEn){
bk4819_enable_rx_ctcss(config->rxTone / 10);
}
bk4819_set_modulation(config->modulation);
bk4819_rx_on();
radioStatus = RX;
}
Expand Down Expand Up @@ -234,8 +233,8 @@ void radio_updateConfiguration()
// config->txDisable = 0;
// Set squelch
int squelch = -127 + (config->sqlLevel * 66) / 15;
bk4819_set_Squelch((squelch / 2 + 160) * 2,
((squelch - 2) / 2 + 160) * 2,
bk4819_set_Squelch(((squelch + 160) * 2),
((squelch - 3 + 160) * 2),
0x5f, 0x5e, 0x20, 0x08
);
// Set BK4819 PA Gain tuning according to TX power and frequency
Expand Down
8 changes: 0 additions & 8 deletions platform/mcu/GD32F330/drivers/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@
#define MIC_EN_GPIO_PIN GPIO_PIN_12
#define MIC_EN_GPIO_RCU RCU_GPIOA

static void LCD_gpio_init(void);
static void key_gpio_init(void);
static void usart_gpio_init(void);
static void eeprom_gpio_init(void);
static void bk4819_gpio_init(void);
static void bk1080_gpio_init(void);
static void misc_gpio_init(void);

void gpio_config(void);

#endif
70 changes: 68 additions & 2 deletions platform/targets/A36Plus/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,82 @@ void platform_ledOff(led_t led)
}
}

// TIMER0 interrupt handler
void TIMER0_IRQHandler()
{
if (timer_interrupt_flag_get(TIMER0, TIMER_INT_UP) != RESET)
{
// Clear the interrupt flag
timer_interrupt_flag_clear(TIMER0, TIMER_INT_UP);
// Change the duty cycle
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, rand() % 256);
}
}

void platform_beepStart(uint16_t freq)
{
// BK4819_PlayTone(freq, true);
// Enable necessary peripherals
rcu_periph_clock_enable(RCU_GPIOA);
// Disable UART to make sure it doesn't interfere with the speaker
//usart_deinit(USART0);
rcu_periph_clock_disable(RCU_USART0);
rcu_periph_clock_enable(RCU_TIMER0);

// Configure GPIO pin A11 for alternate function (TIMER1_CH4)
gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_11);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);


// Configure Timer for PWM
timer_oc_parameter_struct timer_ocinitpara;
timer_parameter_struct timer_initpara;

// Deinitialize TIMER0
timer_deinit(TIMER0);

// Initialize TIMER0 parameters
timer_initpara.prescaler = 84; // Prescaler to get 1us per tick (assuming 84MHz system clock)
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
// period = 1 / (freq * 1us) - 1
timer_initpara.period = (uint32_t)(1.0f / ((float)freq * 0.00000101f));
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER0, &timer_initpara);

// Initialize TIMER0 Channel 3 parameters
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
//timer_ocinitpara.outputnstate = TIMER_CCXN_ENABLE;
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
//timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_LOW;
timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
//timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH;
timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocinitpara);
timer_primary_output_config(TIMER0, ENABLE);

// Set PWM mode and duty cycle
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 160); // 50% duty cycle
timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
timer_auto_reload_shadow_enable(TIMER0);

// Enable TIMER0
timer_enable(TIMER0);

// Enable the TIMER0 interrupt
timer_interrupt_enable(TIMER0, TIMER_INT_UP);
}



void platform_beepStop()
{
return;
// Disable the speaker and stop the timer
timer_disable(TIMER0);
rcu_periph_clock_enable(RCU_USART0);
}


// Helper function to convert BCD to normal numbers
static uint8_t bcd2dec(uint8_t bcd)
{
Expand Down

0 comments on commit d5a0288

Please sign in to comment.