diff --git a/meson.build b/meson.build index f141016c..5f6e8e01 100644 --- a/meson.build +++ b/meson.build @@ -120,6 +120,45 @@ md380_inc = inc + ['platform/mcu/CMSIS/Include', 'rtos/uC-OS3/Ports/ARM-Cortex-M/ARMv7-M', 'rtos/uC-CPU/ARM-Cortex-M/ARMv7-M'] + +## TYT MD-UV380 +mduv380_src = src + ['openrtx/src/main.c', + 'openrtx/src/bootstrap.c', + 'platform/mcu/STM32F4xx/boot/startup.c', + 'platform/mcu/STM32F4xx/boot/libc_integration.c', + 'platform/mcu/STM32F4xx/drivers/usb/usb_bsp.c', + 'platform/mcu/STM32F4xx/drivers/usb/usb_core.c', + 'platform/mcu/STM32F4xx/drivers/usb/usb_dcd.c', + 'platform/mcu/STM32F4xx/drivers/usb/usb_dcd_int.c', + 'platform/mcu/STM32F4xx/drivers/usb/usbd_desc.c', + 'platform/mcu/STM32F4xx/drivers/usb/usbd_core.c', + 'platform/mcu/STM32F4xx/drivers/usb/usbd_ioreq.c', + 'platform/mcu/STM32F4xx/drivers/usb/usbd_req.c', + 'platform/mcu/STM32F4xx/drivers/usb/usbd_usr.c', + 'platform/mcu/STM32F4xx/drivers/gpio.c', + 'platform/mcu/STM32F4xx/drivers/usb_vcom.c', + 'platform/mcu/STM32F4xx/drivers/delays.c', + 'platform/mcu/CMSIS/Device/ST/STM32F4xx/Source/system_stm32f4xx.c', + 'platform/drivers/display/HX83XX_MDxx380.c', + 'platform/targets/MD-UV380/platform.c', + 'platform/targets/MD-UV380/adc1.c', + 'openrtx/src/graphics/graphics_rgb565.c', + 'rtos/uC-OS3/Ports/ARM-Cortex-M/ARMv7-M/os_cpu_c.c', + 'rtos/uC-OS3/Ports/ARM-Cortex-M/ARMv7-M/os_cpu_a.s', + 'rtos/uC-CPU/ARM-Cortex-M/ARMv7-M/cpu_c.c', + 'rtos/uC-CPU/ARM-Cortex-M/ARMv7-M/cpu_a.s'] + +mduv380_def = def + {'STM32F40_41xxx': ''} + +mduv380_inc = inc + ['platform/mcu/CMSIS/Include', + 'platform/mcu/CMSIS/Device/ST/STM32F4xx/Include', + 'platform/mcu/STM32F4xx', + 'platform/mcu/STM32F4xx/drivers', + 'platform/mcu/STM32F4xx/drivers/usb', + 'platform/targets/MD-UV380', + 'rtos/uC-OS3/Ports/ARM-Cortex-M/ARMv7-M', + 'rtos/uC-CPU/ARM-Cortex-M/ARMv7-M'] + ## ## Compilation defines ## @@ -141,6 +180,15 @@ foreach k, v : md380_def endif endforeach +mduv380_args = [] +foreach k, v : mduv380_def + if v == '' + mduv380_args += '-D@0@'.format(k) + else + mduv380_args += '-D@0@=@1@'.format(k, v) + endif +endforeach + linux_opts = {'sources': linux_src, 'c_args': linux_args, 'include_directories': linux_inc, @@ -151,6 +199,11 @@ md380_opts = {'sources': md380_src, 'c_args': md380_args, 'include_directories': md380_inc} + +mduv380_opts = {'sources': mduv380_src, + 'c_args': mduv380_args, + 'include_directories': mduv380_inc} + ## ## Targets ## @@ -173,7 +226,7 @@ targets = [ 'load_addr': '0x0800C000'}, {'name': 'mduv380g', - 'opts': md380_opts, + 'opts': mduv380_opts, 'flashable': true, 'wrap': 'UV3X0', 'load_addr': '0x0800C000'}, diff --git a/platform/targets/MD-UV380/adc1.c b/platform/targets/MD-UV380/adc1.c new file mode 100644 index 00000000..6a5f2a02 --- /dev/null +++ b/platform/targets/MD-UV380/adc1.c @@ -0,0 +1,105 @@ +/*************************************************************************** + * Copyright (C) 2020 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 * + ***************************************************************************/ + +#include +#include "hwconfig.h" +#include "adc1.h" + +uint16_t measurements[4]; + +void adc1_init() +{ + RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; + RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN; + + /* + * Configure GPIOs to analog input mode: + * - PA0: volume potentiometer level + * - PA1: battery voltage + * - PA3: vox level + * - PB0: RSSI level + */ + gpio_setMode(AIN_VOLUME, INPUT_ANALOG); + gpio_setMode(AIN_VBAT, INPUT_ANALOG); + gpio_setMode(AIN_MIC, INPUT_ANALOG); + gpio_setMode(AIN_RSSI, INPUT_ANALOG); + + /* + * ADC clock is APB2 frequency divided by 8, giving 10.5MHz. + * We set the sample time of each channel to 480 ADC cycles and we have to + * scan four channels: given that a conversion takes 12 cycles, we have a + * total conversion time of ~187us. + */ + ADC->CCR |= ADC_CCR_ADCPRE; + ADC1->SMPR2 = ADC_SMPR2_SMP0 + | ADC_SMPR2_SMP1 + | ADC_SMPR2_SMP3 + | ADC_SMPR2_SMP8; + + /* + * No overrun interrupt, 12-bit resolution, no analog watchdog, no + * discontinuous mode, enable scan mode, no end of conversion interrupts, + * enable continuous conversion (free-running). + */ + ADC1->CR1 |= ADC_CR1_SCAN; + ADC1->CR2 |= ADC_CR2_DMA + | ADC_CR2_DDS + | ADC_CR2_CONT + | ADC_CR2_ADON; + + /* Scan sequence config. */ + ADC1->SQR1 = 3 << 20; /* Four channels to be converted */ + ADC1->SQR3 |= (1 << 0) /* CH1, battery voltage on PA1 */ + | (8 << 5) /* CH8, RSSI value on PB0 */ + | (3 << 10) /* CH3, vox level on PA3 */ + | (0 << 15); /* CH0, volume potentiometer level on PA0 */ + + /* DMA2 Stream 0 configuration: + * - channel 0: ADC1 + * - low priority + * - half-word transfer, both memory and peripheral + * - increment memory + * - circular mode + * - peripheral-to-memory transfer + * - no interrupts + */ + DMA2_Stream0->PAR = ((uint32_t) &(ADC1->DR)); + DMA2_Stream0->M0AR = ((uint32_t) &measurements); + DMA2_Stream0->NDTR = 4; + DMA2_Stream0->CR = DMA_SxCR_MSIZE_0 + | DMA_SxCR_PSIZE_0 + | DMA_SxCR_MINC + | DMA_SxCR_CIRC + | DMA_SxCR_EN; + + /* Finally, start conversion */ + ADC1->CR2 |= ADC_CR2_SWSTART; +} + +void adc1_terminate() +{ + DMA2_Stream0->CR &= ~DMA_SxCR_EN; + ADC1->CR2 &= ADC_CR2_ADON; + RCC->APB2ENR &= ~RCC_APB2ENR_ADC1EN; +} + +float adc1_getMeasurement(uint8_t ch) +{ + if(ch > 3) return 0.0f; + float value = ((float) measurements[ch]); + return (value * 3300.0f)/4096.0f; +} diff --git a/platform/targets/MD-UV380/adc1.h b/platform/targets/MD-UV380/adc1.h new file mode 100644 index 00000000..017d2182 --- /dev/null +++ b/platform/targets/MD-UV380/adc1.h @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright (C) 2020 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 * + ***************************************************************************/ + +#ifndef ADC1_H +#define ADC1_H + +#include + +/** + * Driver for ADC1, used to continuously sample the following channels: + * - ADC1_CH0 (PA0): output value of the volume potentiometer; + * - ADC1_CH1 (PA1): battery voltage through 1:3 resistor divider; + * - ADC1_CH3 (PA3): vox level; + * - ADC1_CH8 (PB0): RSSI level; + */ + +/** + * Initialise and start ADC1 and DMA2 Stream 0. + * + * ADC is configured in free-running mode with 1:8 prescaler and a sample time + * for each channel of 480 cycles. This gives a sampling frequency, for each + * channel, of ~5.3kHz. + * + * DMA2 Stream 0 is used to transfer data from ADC1 data register to an internal + * buffer, from which is fetched by application code using adc1_getMeasurement(). + */ +void adc1_init(); + +/** + * Turn off ADC1 (also gating off its clock) and disable DMA2 Stream 0. + * DMA2 clock is kept active. + */ +void adc1_terminate(); + +/** + * Get current measurement of a given channel, mapped as below: + * - channel 0: battery voltage + * - channel 1: RSSI level + * - channel 2: vox level + * - channel 3: volume level + * + * NOTE: the mapping above DOES NOT correspond to the physical ADC channel + * mapping! + * + * @param ch: channel number, between 0 and 3. + * @return current value of the specified channel in mV. + */ +float adc1_getMeasurement(uint8_t ch); + +#endif /* ADC1_H */ diff --git a/platform/targets/MD-UV380/hwconfig.h b/platform/targets/MD-UV380/hwconfig.h index e0c552ef..1b6f143b 100644 --- a/platform/targets/MD-UV380/hwconfig.h +++ b/platform/targets/MD-UV380/hwconfig.h @@ -26,7 +26,7 @@ #define SCREEN_WIDTH 160 #define SCREEN_HEIGHT 128 -/* Defines for GPIO control, really ugly but useful. */ +/* Display */ #define LCD_D0 GPIOD,14 #define LCD_D1 GPIOD,15 #define LCD_D2 GPIOD,0 @@ -40,9 +40,27 @@ #define LCD_CS GPIOD,6 #define LCD_RS GPIOD,12 #define LCD_RST GPIOD,13 - #define LCD_BKLIGHT GPIOD,8 +/* Signalling LEDs */ +#define GREEN_LED GPIOE,0 +#define RED_LED GPIOE,1 + +/* Analog inputs */ +#define AIN_VOLUME GPIOA,0 +#define AIN_VBAT GPIOA,1 +#define AIN_MIC GPIOA,3 +#define AIN_RSSI GPIOB,0 + +/* Channel selection rotary encoder */ +#define CH_SELECTOR_0 GPIOE,14 +#define CH_SELECTOR_1 GPIOE,15 +#define CH_SELECTOR_2 GPIOB,10 +#define CH_SELECTOR_3 GPIOB,11 + +/* Push-to-talk switch */ +#define PTT_SW GPIOE,11 + /* * To enable pwm for display backlight dimming uncomment this directive. * diff --git a/platform/targets/MD-UV380/platform.c b/platform/targets/MD-UV380/platform.c index ce7af598..e9f386e8 100644 --- a/platform/targets/MD-UV380/platform.c +++ b/platform/targets/MD-UV380/platform.c @@ -1,6 +1,6 @@ /*************************************************************************** * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * - * Niccolò Izzo IU2KIN * + * Niccolò Izzo IU2KIN, * * Silvano Seva IU2KWO * * * * This program is free software; you can redistribute it and/or modify * @@ -21,6 +21,7 @@ #include #include #include "hwconfig.h" +#include "adc1.h" #ifdef ENABLE_BKLIGHT_DIMMING void TIM1_TRG_COM_TIM11_IRQHandler() @@ -52,6 +53,20 @@ void platform_init() gpio_setMode(LCD_BKLIGHT, OUTPUT); gpio_clearPin(LCD_BKLIGHT); + gpio_setMode(CH_SELECTOR_0, INPUT); + gpio_setMode(CH_SELECTOR_1, INPUT); + gpio_setMode(CH_SELECTOR_2, INPUT); + gpio_setMode(CH_SELECTOR_3, INPUT); + + gpio_setMode(PTT_SW, INPUT); + + /* + * Initialise ADC1, for vbat, RSSI, ... + * Configuration of corresponding GPIOs in analog input mode is done inside + * the driver. + */ + adc1_init(); + #ifdef ENABLE_BKLIGHT_DIMMING /* * Configure TIM11 for backlight PWM: Fpwm = 256Hz, 8 bit of resolution @@ -85,37 +100,52 @@ void platform_terminate() /* Shut down backlight */ gpio_clearPin(LCD_BKLIGHT); - gpio_clearPin(GREEN_LED); - gpio_clearPin(RED_LED); - #ifdef ENABLE_BKLIGHT_DIMMING RCC->APB2ENR &= ~RCC_APB2ENR_TIM11EN; #endif + + /* Shut down LEDs */ + gpio_clearPin(GREEN_LED); + gpio_clearPin(RED_LED); + + /* Shut down timer */ + RCC->APB2ENR &= ~RCC_APB2ENR_TIM8EN; + + /* Shut down ADC */ + adc1_terminate(); + } float platform_getVbat() { - return 0.0f; + return adc1_getMeasurement(0); } float platform_getMicLevel() { - return 0.0f; + return adc1_getMeasurement(2); } float platform_getVolumeLevel() { - return 0.0f; + return adc1_getMeasurement(3); } uint8_t platform_getChSelector() { - return 0.0f; + static const uint8_t rsPositions[] = { 11, 14, 10, 15, 6, 3, 7, 2, 12, 13, + 9, 16, 5, 4, 8, 1 }; + int pos = gpio_readPin(CH_SELECTOR_0) + | (gpio_readPin(CH_SELECTOR_1) << 1) + | (gpio_readPin(CH_SELECTOR_2) << 2) + | (gpio_readPin(CH_SELECTOR_3) << 3); + return rsPositions[pos]; } bool platform_getPttStatus() { - return false; + /* PTT line has a pullup resistor with PTT switch closing to ground */ + return (gpio_readPin(PTT_SW) == 0) ? true : false; } void platform_ledOn(led_t led) @@ -154,12 +184,13 @@ void platform_ledOff(led_t led) void platform_beepStart(uint16_t freq) { + /* TODO */ (void) freq; } void platform_beepStop() { - + /* TODO */ } void platform_setBacklightLevel(uint8_t level)