Skip to content

Commit

Permalink
Merge remote-tracking branch 'Trriss/A36plus-try2' into A36Plus
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej committed Feb 13, 2025
2 parents 36aeac7 + d798604 commit 8261538
Show file tree
Hide file tree
Showing 87 changed files with 32,062 additions and 73 deletions.
6 changes: 6 additions & 0 deletions lib/printf/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@

#include <stdbool.h>
#include <stdint.h>
#ifdef GD32F330
#include <platform/mcu/GD32F330/drivers/USART0.h>
#endif
#ifdef GD32F30X_XD
#include <platform/mcu/GD32F303/drivers/USART0.h>
#endif


#include "printf.h"

Expand Down
107 changes: 106 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,38 @@ gd32f330_inc = ['platform/mcu/GD32F330',

gd32f330_def = {'GD32F330':''}

##
## GD32F303
##

gd32f303_src = [
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/system_gd32f30x.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_misc.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_gpio.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_usart.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_spi.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_rtc.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_adc.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_pmu.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_dma.c',
'platform/drivers/SPI/spi_gd32.c',
'platform/drivers/GPIO/gpio_gd32.c',
'platform/mcu/GD32F303/drivers/delays.cpp',
'platform/mcu/GD32F303/drivers/USART0.cpp',
'platform/mcu/GD32F303/boot/startup.cpp',
'platform/mcu/GD32F303/boot/bsp.cpp',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_timer.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_rcu.c',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Source/gd32f30x_spi.c',
'platform/mcu/GD32F303/boot/libc_integration.cpp']

gd32f303_inc = ['platform/mcu/GD32F303',
'platform/mcu/GD32F303/drivers',
'platform/mcu/CMSIS/Include',
'platform/mcu/CMSIS/Device/GD/GD32F30x/Include']

gd32f303_def = {'GD32F30X_XD':''}

##
## ----------------------- Platform specializations ----------------------------
##
Expand Down Expand Up @@ -550,6 +582,35 @@ a36plus_src += openrtx_src + gd32f330_src + miosix_cm4f_src + ui_src_default
a36plus_inc += openrtx_inc + gd32f330_inc + miosix_cm4f_inc
a36plus_def += openrtx_def + gd32f330_def + miosix_cm4f_def

##
## Talkpod A36Plus - Modified MCU GD32F303CGT6
##

a36plus_f303_src = ['platform/drivers/baseband/bk4819.c',
'platform/targets/A36Plus/hwconfig.c',
'platform/targets/A36Plus_f303/platform.c',
'platform/drivers/display/ST7735S_a36plus.c',
'platform/drivers/keyboard/keyboard_a36plus.c',
'platform/drivers/audio/audio_A36Plus.c',
'platform/drivers/CPS/cps_io_native_A36plus.c',
'platform/drivers/NVM/W25Qx.c',
'platform/drivers/NVM/nvmem_A36Plus.c',
#'platform/drivers/NVM/spiFlash_A36plus.c',
'platform/drivers/ADC/ADC0_A36plus_f303.c',
#'platform/drivers/baseband/bk1080.c',
#'openrtx/src/rtx/OpMode_WFM.cpp',
'platform/drivers/backlight/backlight_A36Plus_f303.c',
'platform/drivers/baseband/radio_A36Plus.cpp']

a36plus_f303_inc = ['platform/drivers/baseband',
'platform/targets/A36Plus_f303']

a36plus_f303_def = {'PLATFORM_A36PLUS': ''}

a36plus_f303_src += openrtx_src + gd32f303_src + miosix_cm4f_src + ui_src_default
a36plus_f303_inc += openrtx_inc + gd32f303_inc + miosix_cm4f_inc
a36plus_f303_def += openrtx_def + gd32f303_def + miosix_cm4f_def

##
## -------------------------- Compilation arguments ----------------------------
##
Expand Down Expand Up @@ -663,6 +724,15 @@ foreach k, v : a36plus_def
endif
endforeach

a36plus_f303_args = []
foreach k, v : a36plus_f303_def
if v == ''
a36plus_f303_args += '-D@0@'.format(k)
else
a36plus_f303_args += '-D@0@=@1@'.format(k, v)
endif
endforeach

linux_opts = {
'sources' : linux_default_src,
'include_directories': linux_inc,
Expand Down Expand Up @@ -770,6 +840,16 @@ a36plus_opts = {
'-Wl,--print-memory-usage'],
}

a36plus_f303_opts = {
'sources' : a36plus_f303_src,
'include_directories': a36plus_f303_inc,
'dependencies' : [codec2_dep],
'c_args' : a36plus_f303_args,
'cpp_args' : a36plus_f303_args,
'link_args' : ['-Wl,-T../platform/mcu/GD32F303/linker_script.ld',
'-Wl,--print-memory-usage'],
}

ttwrplus_opts = {
'sources' : ['CMakeLists.txt'],
'include_directories': [],
Expand Down Expand Up @@ -855,6 +935,13 @@ targets = [
'wrap' : ' ',
'load_addr': ' '
},
{
'name' : 'a36plus_f303',
'opts' : a36plus_f303_opts,
'flashable': true,
'wrap' : ' ',
'load_addr': ' '
},
{
'name' : 'ttwrplus',
'opts' : ttwrplus_opts,
Expand Down Expand Up @@ -1006,7 +1093,25 @@ foreach t : targets
custom_target(name+'_flash',
input : wrap,
output : name+'_flash',
command : [rtxflash, 'flash', 'A36Plus', '/dev/ttyUSB0', '@INPUT@'])
command : [rtxflash, 'flash', 'A36Plus', '/dev/ttyUSB0', '@INPUT@'])

# A36Plus f303 handling (GD32F303CGT6)
elif name == 'openrtx_a36plusf303'

# Wrap target for A36Plus_f303 for consistency, same output as bin target
# Wrapping is done by "rtxflash wrap A36Plus [input] [output.kdhx]
wrap = custom_target(name+'_wrap',
output : name+'_wrap.kdhx',
input : bin,
command : [rtxflash, 'wrap', 'A36Plus', '@INPUT@', '@OUTPUT@'])

# Flash target for A36Plus
# rtxflash flash A36Plus [serialport] [input.kdhx]
# FIXME: The serial port should be a parameter!
custom_target(name+'_flash',
input : wrap,
output : name+'_flash',
command : [rtxflash, 'flash', 'A36Plus', '/dev/ttyUSB0', '@INPUT@'])
else

wrap = custom_target(name+'_wrap',
Expand Down
11 changes: 11 additions & 0 deletions openrtx/src/ui/default/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1409,12 +1409,23 @@ void ui_updateFSM(bool *sync_rtx)
#if !defined(PLATFORM_TTWRPLUS)
if ((!state.emergency) && (!txOngoing) && (state.charge <= 0))
{
#ifdef GD32F30X_XD
// We have to rely on low battery voltage to detect the power switch being turned off
// since the power switch is not connected to the MCU anymore.
if (state.charge <= 0)
{
// shutdown
state.devStatus = SHUTDOWN;
}

#else
state.ui_screen = LOW_BAT;
if(event.type == EVENT_KBD && event.payload)
{
state.ui_screen = MAIN_VFO;
state.emergency = true;
}
#endif
return;
}
#endif // PLATFORM_TTWRPLUS
Expand Down
62 changes: 62 additions & 0 deletions platform/drivers/ADC/ADC0_A36plus_f303.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/***************************************************************************
* Copyright (C) 2020 - 2023 by Silvano Seva IU2KWO *
* and Niccolò Izzo IU2KIN *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/

#include "ADC0_A36plus.h"

#include <hwconfig.h>
#include "printf.h"

void adc0_init()
{
rcu_periph_clock_enable(RCU_ADC0);
adc_deinit(ADC0);
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
adc_resolution_config(ADC0, 0x2000000);
adc_data_alignment_config(ADC0, 0);
adc_channel_length_config(ADC0, 1, 1);
adc_regular_channel_config(ADC0, 0, 0, 5);
adc_external_trigger_source_config(ADC0, 1, 0xe0000);
adc_external_trigger_config(ADC0, 1, 1);
adc_enable(ADC0);
adc_calibration_enable(ADC0);
return;
}

void adc0_terminate()
{
return;
}

uint16_t adc0_getRawSample(uint8_t ch)
{
adc_regular_channel_config(ADC0, 0, 1, 5);
adc_software_trigger_enable(ADC0, 1);
char bVar1;
do
{
bVar1 = adc_flag_get(ADC0, ADC_FLAG_EOC);
} while (!bVar1);
adc_interrupt_flag_clear(ADC0, ADC_FLAG_EOC);
return adc_regular_data_read(ADC0);
}

uint32_t adc0_getMeasurement(uint8_t ch)
{
uint32_t sample = (uint32_t)(adc0_getRawSample(ch) * 0x1d5);
return sample/10;
}
4 changes: 2 additions & 2 deletions platform/drivers/CPS/cps_io_native_A36plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
#include "cps_data_A36plus.h"
#include "W25Qx.h"

extern const struct nvmDevice eflash;

static const uint32_t chDataBaseAddr = 0x0; /**< Base address of channel data */
static const uint32_t maxNumChannels = 512; /**< Maximum number of channels in memory */

extern const struct nvmDevice eflash;

static inline void W25Qx_readData(uint32_t addr, void *buf, size_t len)
{
nvm_devRead(&eflash, addr, buf, len);
Expand Down
9 changes: 8 additions & 1 deletion platform/drivers/GPIO/gpio_gd32.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
***************************************************************************/

#include <errno.h>
#include "gd32f3x0.h"

#ifdef GD32F330
#include <gd32f3x0.h>
#endif
#ifdef GD32F30X_XD
#include "gd32f30x.h"
#endif

#include "gpio_gd32.h"

void gpio_setMode(const void *port, const uint8_t pin, const uint16_t mode)
Expand Down
8 changes: 8 additions & 0 deletions platform/drivers/GPIO/gpio_gd32.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
#define GPIO_GD32_H

#include <peripherals/gpio.h>

#ifdef GD32F330
#include <gd32f3x0.h>
#endif
#ifdef GD32F30X_XD
#include "gd32f30x.h"
#endif



#ifdef __cplusplus
extern "C" {
Expand Down
50 changes: 39 additions & 11 deletions platform/drivers/NVM/nvmem_A36Plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ int W25Qx_writeData(uint32_t addr, const void *buf, size_t len)

static const struct W25QxCfg eflashCfg =
{
.spi = (const struct spiDevice *) &nvm_spi,
.cs = { &GpioA,4 }
.spi = (const struct spiDevice *) &nvm_spi0,
.cs = { FLASH_CS }
};

//W25Qx_DEVICE_DEFINE(eflash, NULL, 0x200000) // 2 MB, 16 Mbit
W25Qx_DEVICE_DEFINE(eflash, eflashCfg, 0x200000) // 2 MB, 16 Mbit

static const struct nvmDescriptor nvmDevices[] =
Expand Down Expand Up @@ -106,24 +104,54 @@ void nvm_dumpFlash()
}
}

//W25Qx_sleep();
//W25Qx_sleep(&eflash);
}
#endif

void nvm_init()
{
gpio_setMode(FLASH_CLK, ALTERNATE | ALTERNATE_FUNC(0));
gpio_setMode(FLASH_SDO, ALTERNATE | ALTERNATE_FUNC(0));
gpio_setMode(FLASH_SDI, ALTERNATE | ALTERNATE_FUNC(0));

spiGd32_init(&nvm_spi, 21000000, 0);
//taking from spiFlash_A36 plus .. should be working
#define FLASH_GPIO_PORT GPIOA
#define FLASH_GPIO_SCK_PIN GPIO_PIN_5
#define FLASH_GPIO_DIN_PIN GPIO_PIN_6
#define FLASH_GPIO_DOUT_PIN GPIO_PIN_7
#define FLASH_GPIO_CS_PIN GPIO_PIN_4

#ifdef GD32F330
gpio_af_set(FLASH_GPIO_PORT, GPIO_AF_0, FLASH_GPIO_SCK_PIN | FLASH_GPIO_DIN_PIN | FLASH_GPIO_DOUT_PIN);
gpio_mode_set(FLASH_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, FLASH_GPIO_SCK_PIN | FLASH_GPIO_DIN_PIN | FLASH_GPIO_DOUT_PIN);
gpio_output_options_set(FLASH_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, FLASH_GPIO_SCK_PIN | FLASH_GPIO_DIN_PIN | FLASH_GPIO_DOUT_PIN);

gpio_mode_set(FLASH_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, FLASH_GPIO_CS_PIN);
gpio_output_options_set(FLASH_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, FLASH_GPIO_CS_PIN);
#endif
#ifdef GD32F30X_XD
gpio_init(FLASH_GPIO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, FLASH_GPIO_SCK_PIN | FLASH_GPIO_DIN_PIN | FLASH_GPIO_DOUT_PIN);
gpio_init(FLASH_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, FLASH_GPIO_CS_PIN);
// SPI0_REMAP = 0
gpio_pin_remap_config(GPIO_SPI0_REMAP, DISABLE);
//gpio_bit_set(FLASH_GPIO_PORT, FLASH_GPIO_CS_PIN);
#endif

spi_parameter_struct spi_init_struct;

//For testing later if all works
// gpio_setMode(FLASH_CLK, ALTERNATE | ALTERNATE_FUNC(0));
// gpio_setMode(FLASH_SDO, ALTERNATE | ALTERNATE_FUNC(0));
// gpio_setMode(FLASH_SDI, ALTERNATE | ALTERNATE_FUNC(0));

spiGd32_init(&nvm_spi0, 21000000, 0);
W25Qx_init(&eflash);
}

void nvm_terminate()
{
W25Qx_terminate(&eflash);
spiGd32_terminate(&nvm_spi);
spiGd32_terminate(&nvm_spi0);

//

}

const struct nvmDescriptor *nvm_getDesc(const size_t index)
Expand All @@ -149,7 +177,7 @@ void nvm_readCalibData(void *buf)
addr += sizeof(PowerCalibration);
W25Qx_readData(addr, &calib->low, sizeof(PowerCalibration));

// W25Qx_sleep();
// W25Qx_sleep(&eflash);
}

void nvm_readHwInfo(hwInfo_t *info)
Expand Down
Loading

0 comments on commit 8261538

Please sign in to comment.