2020-10-22 12:54:09 +00:00
|
|
|
/***************************************************************************
|
2025-04-04 17:06:57 +00:00
|
|
|
* Copyright (C) 2020 - 2025 by Federico Amedeo Izzo IU2NUO, *
|
2022-06-02 07:56:05 +00:00
|
|
|
* Niccolò Izzo IU2KIN *
|
|
|
|
|
* Silvano Seva IU2KWO *
|
2020-10-22 12:54:09 +00:00
|
|
|
* *
|
|
|
|
|
* 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/> *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
2023-07-21 06:35:39 +00:00
|
|
|
#include <peripherals/gpio.h>
|
2020-12-24 15:24:35 +00:00
|
|
|
#include <interfaces/nvmem.h>
|
|
|
|
|
#include <interfaces/platform.h>
|
2025-08-13 16:37:29 +00:00
|
|
|
#include <interfaces/delays.h>
|
2020-12-04 09:45:03 +00:00
|
|
|
#include <hwconfig.h>
|
2021-02-01 20:27:31 +00:00
|
|
|
#include <string.h>
|
2024-11-10 10:57:53 +00:00
|
|
|
#include <adc_stm32.h>
|
2020-12-04 09:45:03 +00:00
|
|
|
#include <calibInfo_MDx.h>
|
2020-12-13 10:43:58 +00:00
|
|
|
#include <toneGenerator_MDx.h>
|
2023-07-21 06:23:41 +00:00
|
|
|
#include <peripherals/rtc.h>
|
2021-04-12 17:10:13 +00:00
|
|
|
#include <interfaces/audio.h>
|
2025-08-13 16:37:29 +00:00
|
|
|
#include <gps_stm32.h>
|
|
|
|
|
#include <gps.h>
|
2020-12-04 09:45:03 +00:00
|
|
|
|
2023-07-13 06:49:25 +00:00
|
|
|
static hwInfo_t hwInfo;
|
2020-10-22 12:54:09 +00:00
|
|
|
|
|
|
|
|
void platform_init()
|
|
|
|
|
{
|
2020-10-24 08:40:40 +00:00
|
|
|
/* Configure GPIOs */
|
|
|
|
|
gpio_setMode(GREEN_LED, OUTPUT);
|
|
|
|
|
gpio_setMode(RED_LED, OUTPUT);
|
|
|
|
|
|
2020-10-24 17:24:57 +00:00
|
|
|
gpio_setMode(CH_SELECTOR_0, INPUT);
|
|
|
|
|
gpio_setMode(CH_SELECTOR_1, INPUT);
|
|
|
|
|
gpio_setMode(CH_SELECTOR_2, INPUT);
|
|
|
|
|
gpio_setMode(CH_SELECTOR_3, INPUT);
|
|
|
|
|
|
2021-11-20 07:36:22 +00:00
|
|
|
gpio_setMode(PTT_SW, INPUT_PULL_UP);
|
|
|
|
|
gpio_setMode(PTT_EXT, INPUT_PULL_UP);
|
2020-10-24 17:24:57 +00:00
|
|
|
|
2024-11-10 10:57:53 +00:00
|
|
|
gpio_setMode(AIN_VBAT, ANALOG);
|
|
|
|
|
gpio_setMode(AIN_VOLUME, ANALOG);
|
|
|
|
|
gpio_setMode(AIN_MIC, ANALOG);
|
|
|
|
|
gpio_setMode(AIN_RSSI, ANALOG);
|
|
|
|
|
|
2022-03-05 08:09:25 +00:00
|
|
|
#ifndef RUNNING_TESTSUITE
|
2021-02-19 10:51:05 +00:00
|
|
|
gpio_setMode(PWR_SW, OUTPUT);
|
2021-11-09 18:28:23 +00:00
|
|
|
gpio_setPin(PWR_SW);
|
2022-01-03 13:49:25 +00:00
|
|
|
#endif
|
2021-02-19 10:51:05 +00:00
|
|
|
|
2024-11-10 10:57:53 +00:00
|
|
|
/* Initialise ADC1, for vbat, RSSI, ... */
|
|
|
|
|
adcStm32_init(&adc1);
|
2020-10-24 08:40:40 +00:00
|
|
|
|
2021-02-01 20:27:31 +00:00
|
|
|
memset(&hwInfo, 0x00, sizeof(hwInfo));
|
|
|
|
|
|
2020-12-31 16:55:04 +00:00
|
|
|
nvm_init(); /* Initialise non volatile memory manager */
|
2022-08-11 15:02:04 +00:00
|
|
|
nvm_readHwInfo(&hwInfo); /* Load hardware information data */
|
2020-12-31 16:55:04 +00:00
|
|
|
toneGen_init(); /* Initialise tone generator */
|
|
|
|
|
rtc_init(); /* Initialise RTC */
|
2021-04-12 17:10:13 +00:00
|
|
|
audio_init(); /* Initialise audio management module */
|
2020-10-22 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void platform_terminate()
|
|
|
|
|
{
|
2020-10-24 17:24:57 +00:00
|
|
|
/* Shut down LEDs */
|
2020-10-24 08:40:40 +00:00
|
|
|
gpio_clearPin(GREEN_LED);
|
|
|
|
|
gpio_clearPin(RED_LED);
|
|
|
|
|
|
2020-12-31 16:55:04 +00:00
|
|
|
/* Shut down all the modules */
|
2024-11-10 10:57:53 +00:00
|
|
|
adcStm32_terminate(&adc1);
|
2025-08-13 16:37:29 +00:00
|
|
|
gpsStm32_terminate();
|
2020-12-04 09:45:03 +00:00
|
|
|
nvm_terminate();
|
2020-12-31 16:55:04 +00:00
|
|
|
toneGen_terminate();
|
2021-04-12 17:10:13 +00:00
|
|
|
audio_terminate();
|
2021-02-19 10:51:05 +00:00
|
|
|
|
|
|
|
|
/* Finally, remove power supply */
|
2021-02-19 10:58:43 +00:00
|
|
|
gpio_clearPin(PWR_SW);
|
2020-10-24 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-14 10:47:21 +00:00
|
|
|
uint16_t platform_getVbat()
|
2020-10-24 08:40:40 +00:00
|
|
|
{
|
2020-10-26 12:14:52 +00:00
|
|
|
/*
|
|
|
|
|
* Battery voltage is measured through an 1:3 voltage divider and
|
2024-11-10 10:57:53 +00:00
|
|
|
* adc1_getMeasurement returns a value in uV.
|
2020-10-26 12:14:52 +00:00
|
|
|
*/
|
2024-11-10 10:57:53 +00:00
|
|
|
uint32_t vbat = adc_getVoltage(&adc1, ADC_VBAT_CH) * 3;
|
|
|
|
|
return vbat / 1000;
|
2020-10-24 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-13 19:57:32 +00:00
|
|
|
uint8_t platform_getMicLevel()
|
2020-10-24 08:40:40 +00:00
|
|
|
{
|
2021-08-13 19:57:32 +00:00
|
|
|
/* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */
|
2024-11-10 10:57:53 +00:00
|
|
|
return adc_getRawSample(&adc1, ADC_VOX_CH) >> 4;
|
2020-10-24 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-13 19:57:32 +00:00
|
|
|
uint8_t platform_getVolumeLevel()
|
2020-10-24 08:40:40 +00:00
|
|
|
{
|
2021-08-13 19:57:32 +00:00
|
|
|
/*
|
2023-09-08 14:02:53 +00:00
|
|
|
* Volume level corresponds to an analog signal in the range 0 - 1650mV.
|
|
|
|
|
* Potentiometer has pseudo-logarithmic law, well described with two straight
|
|
|
|
|
* lines with a breakpoint around 270mV.
|
|
|
|
|
* Output value has range 0 - 255 with breakpoint at 150.
|
2021-08-13 19:57:32 +00:00
|
|
|
*/
|
2024-11-10 10:57:53 +00:00
|
|
|
uint32_t value = adc_getVoltage(&adc1, ADC_VOL_CH) / 1000;
|
2023-09-08 14:02:53 +00:00
|
|
|
uint32_t output;
|
|
|
|
|
|
|
|
|
|
if(value <= 270)
|
|
|
|
|
{
|
|
|
|
|
// First line: offset zero, slope 0.556
|
|
|
|
|
output = value;
|
|
|
|
|
output = (output * 556) / 1000;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Second line: offset 270, slope 0.076
|
|
|
|
|
output = value - 270;
|
|
|
|
|
output = (output * 76) / 1000;
|
|
|
|
|
output += 150;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(output > 255)
|
|
|
|
|
output = 255;
|
|
|
|
|
|
|
|
|
|
return output;
|
2020-10-24 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-03 07:28:27 +00:00
|
|
|
int8_t platform_getChSelector()
|
2020-10-24 08:40:40 +00:00
|
|
|
{
|
2020-10-24 17:24:57 +00:00
|
|
|
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];
|
2020-10-24 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-24 14:10:22 +00:00
|
|
|
bool platform_getPttStatus()
|
|
|
|
|
{
|
2020-10-24 17:24:57 +00:00
|
|
|
/* PTT line has a pullup resistor with PTT switch closing to ground */
|
2021-07-10 19:52:11 +00:00
|
|
|
uint8_t intPttStatus = gpio_readPin(PTT_SW);
|
|
|
|
|
uint8_t extPttStatus = gpio_readPin(PTT_EXT);
|
|
|
|
|
return ((intPttStatus == 0) || (extPttStatus == 0)) ? true : false;
|
2020-10-24 14:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-11 10:38:20 +00:00
|
|
|
bool platform_pwrButtonStatus()
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* When power knob is set to off, battery voltage measurement returns 0V.
|
2021-04-11 16:18:58 +00:00
|
|
|
* Here we set the threshold to 1V since, with knob in off position, there
|
|
|
|
|
* is always a bit of noise in the ADC measurement making the returned
|
|
|
|
|
* voltage not to be exactly zero.
|
2021-04-11 10:38:20 +00:00
|
|
|
*/
|
2021-11-09 18:28:23 +00:00
|
|
|
return (platform_getVbat() > 1000) ? true : false;
|
2021-04-11 10:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-24 08:40:40 +00:00
|
|
|
void platform_ledOn(led_t led)
|
|
|
|
|
{
|
|
|
|
|
switch(led)
|
|
|
|
|
{
|
|
|
|
|
case GREEN:
|
|
|
|
|
gpio_setPin(GREEN_LED);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RED:
|
|
|
|
|
gpio_setPin(RED_LED);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void platform_ledOff(led_t led)
|
|
|
|
|
{
|
|
|
|
|
switch(led)
|
|
|
|
|
{
|
|
|
|
|
case GREEN:
|
|
|
|
|
gpio_clearPin(GREEN_LED);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RED:
|
|
|
|
|
gpio_clearPin(RED_LED);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void platform_beepStart(uint16_t freq)
|
|
|
|
|
{
|
2022-09-10 00:24:45 +00:00
|
|
|
// calculate appropriate volume.
|
|
|
|
|
uint8_t vol = platform_getVolumeLevel();
|
|
|
|
|
// Since beeps have been requested, we do not want to have 0 volume.
|
|
|
|
|
// We also do not want the volume to be excessive.
|
|
|
|
|
if (vol < 10)
|
|
|
|
|
vol = 5;
|
|
|
|
|
if (vol > 176)
|
|
|
|
|
vol = 176;
|
|
|
|
|
toneGen_beepOn((float)freq, vol, 0);
|
2020-10-24 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void platform_beepStop()
|
|
|
|
|
{
|
2022-09-09 12:23:11 +00:00
|
|
|
toneGen_beepOff();
|
2020-10-22 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 05:58:29 +00:00
|
|
|
datetime_t platform_getCurrentTime()
|
|
|
|
|
{
|
|
|
|
|
return rtc_getTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void platform_setTime(datetime_t t)
|
|
|
|
|
{
|
|
|
|
|
rtc_setTime(t);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 20:27:31 +00:00
|
|
|
const hwInfo_t *platform_getHwInfo()
|
|
|
|
|
{
|
|
|
|
|
return &hwInfo;
|
|
|
|
|
}
|
2025-08-13 16:37:29 +00:00
|
|
|
|
|
|
|
|
const struct gpsDevice *platform_initGps()
|
|
|
|
|
{
|
|
|
|
|
const struct gpsDevice *dev = NULL;
|
|
|
|
|
|
|
|
|
|
// Turn on the GPS and check if there is voltage on the RXD pin
|
|
|
|
|
gpio_setMode(GPS_DATA, INPUT_PULL_DOWN);
|
|
|
|
|
gpio_setMode(GPS_EN, OUTPUT);
|
|
|
|
|
gpio_setPin(GPS_EN);
|
|
|
|
|
|
|
|
|
|
for(size_t i = 0; i < 50; i++) {
|
|
|
|
|
if(gpio_readPin(GPS_DATA) != 0) {
|
|
|
|
|
dev = &gps;
|
|
|
|
|
gpsStm32_init(9600);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sleepFor(0, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gpio_clearPin(GPS_EN);
|
|
|
|
|
gpio_setMode(GPS_DATA, ALTERNATE | ALTERNATE_FUNC(7));
|
|
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
|
}
|