From 56c598da7e5a4ff2aa70b69af400c3a37a54047b Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Sat, 10 Apr 2021 14:12:15 +0200 Subject: [PATCH] Created low-level driver for backlight level management --- meson.build | 8 +- platform/drivers/backlight/backlight.h | 41 +++++ platform/drivers/backlight/backlight_GDx.c | 57 +++++++ platform/drivers/backlight/backlight_MDx.c | 168 +++++++++++++++++++++ platform/targets/DM-1801/platform.c | 35 ++--- platform/targets/GD-77/platform.c | 35 ++--- platform/targets/MD-3x0/platform.c | 43 ++---- platform/targets/MD-9600/platform.c | 47 ++---- platform/targets/MD-UV3x0/platform.c | 106 ++++--------- 9 files changed, 346 insertions(+), 194 deletions(-) create mode 100644 platform/drivers/backlight/backlight.h create mode 100644 platform/drivers/backlight/backlight_GDx.c create mode 100644 platform/drivers/backlight/backlight_MDx.c diff --git a/meson.build b/meson.build index 06246b18..54a4d36c 100644 --- a/meson.build +++ b/meson.build @@ -32,9 +32,9 @@ openrtx_inc = ['openrtx/include', 'platform/drivers/NVM', 'platform/drivers/GPS', 'platform/drivers/tones', - 'openrtx/include/fonts/adafruit', - 'platform/drivers/tones', - 'platform/drivers/baseband'] + 'platform/drivers/baseband', + 'platform/drivers/backlight', + 'openrtx/include/fonts/adafruit'] # Add to sources either the main executable or a platform test if get_option('test') != '' @@ -79,6 +79,7 @@ def = {'DONT_USE_CMSIS_INIT': ''} mdx_src = ['platform/drivers/ADC/ADC1_MDx.c', 'platform/drivers/GPS/GPS_MDx.cpp', 'platform/drivers/NVM/W25Qx.c', + 'platform/drivers/backlight/backlight_MDx.c', 'platform/drivers/tones/toneGenerator_MDx.cpp'] ## @@ -90,6 +91,7 @@ gdx_src = ['platform/drivers/NVM/W25Qx.c', 'platform/drivers/NVM/spiFlash_GDx.c', 'platform/drivers/NVM/nvmem_GDx.c', 'platform/drivers/ADC/ADC0_GDx.c', + 'platform/drivers/backlight/backlight_GDx.c', 'platform/drivers/baseband/radio_GDx.c', 'platform/drivers/baseband/AT1846S.c', 'platform/drivers/baseband/HR_C6000.c', diff --git a/platform/drivers/backlight/backlight.h b/platform/drivers/backlight/backlight.h new file mode 100644 index 00000000..3c2155ee --- /dev/null +++ b/platform/drivers/backlight/backlight.h @@ -0,0 +1,41 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * 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 BACKLIGHT_H +#define BACKLIGHT_H + +/** + * Low-level driver for backlight dimming control. + * This header file only provides the API for driver initialisation and shutdown, + * while effective setting of backlight level is provided by target-specific + * sources by implementating platform_setBacklightLevel(). + */ + +/** + * Initialise backlight driver. + */ +void backlight_init(); + +/** + * Terminate backlight driver. + */ +void backlight_terminate(); + +#endif /* BACKLIGHT_H */ diff --git a/platform/drivers/backlight/backlight_GDx.c b/platform/drivers/backlight/backlight_GDx.c new file mode 100644 index 00000000..7dde2254 --- /dev/null +++ b/platform/drivers/backlight/backlight_GDx.c @@ -0,0 +1,57 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * 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 +#include +#include "backlight.h" + +void backlight_init() +{ + /* + * Configure backlight PWM: 58.5kHz, 8 bit resolution + */ + SIM->SCGC6 |= SIM_SCGC6_FTM0(1); /* Enable clock */ + + FTM0->CONTROLS[3].CnSC = FTM_CnSC_MSB(1) + | FTM_CnSC_ELSB(1); /* Edge-aligned PWM, clear on match */ + FTM0->CONTROLS[3].CnV = 0; + + FTM0->MOD = 0xFF; /* Reload value */ + FTM0->SC = FTM_SC_PS(3) /* Prescaler divide by 8 */ + | FTM_SC_CLKS(1); /* Enable timer */ + + gpio_setMode(LCD_BKLIGHT, OUTPUT); + gpio_setAlternateFunction(LCD_BKLIGHT, 2); +} + +void backlight_terminate() +{ + gpio_clearPin(LCD_BKLIGHT); + SIM->SCGC6 &= ~SIM_SCGC6_FTM0(1); +} + +/* + * This function is defined in platform.h + */ +void platform_setBacklightLevel(uint8_t level) +{ + FTM0->CONTROLS[3].CnV = level; +} diff --git a/platform/drivers/backlight/backlight_MDx.c b/platform/drivers/backlight/backlight_MDx.c new file mode 100644 index 00000000..59a8e7b1 --- /dev/null +++ b/platform/drivers/backlight/backlight_MDx.c @@ -0,0 +1,168 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * 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 +#include +#include "backlight.h" + +#ifndef PLATFORM_MDUV3x0 /* MD-3x0 and MD-9600 */ + +void backlight_init() +{ + gpio_setMode(LCD_BKLIGHT, ALTERNATE); + gpio_setAlternateFunction(LCD_BKLIGHT, 3); + + /* + * Configure TIM8 for backlight PWM: Fpwm = 1kHz with 8 bit of resolution. + * APB2 freq. is 84MHz, but timer runs at twice this frequency. + * Then: PSC = 655 to have Ftick = 256.097kHz + * With ARR = 256, Fpwm is 1kHz; + * Backlight pin is connected to TIM8 CR1. + */ + RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; + __DSB(); + + TIM8->ARR = 255; + TIM8->PSC = 654; + TIM8->CNT = 0; + TIM8->CR1 |= TIM_CR1_ARPE; /* LCD backlight is on PC6, TIM8-CH1 */ + TIM8->CCMR1 |= TIM_CCMR1_OC1M_2 + | TIM_CCMR1_OC1M_1 + | TIM_CCMR1_OC1PE; + TIM8->CCER |= TIM_CCER_CC1E; + TIM8->BDTR |= TIM_BDTR_MOE; + TIM8->CCR1 = 0; + TIM8->EGR = TIM_EGR_UG; /* Update registers */ + TIM8->CR1 |= TIM_CR1_CEN; /* Start timer */ +} + +void backlight_terminate() +{ + /* Shut down backlight */ + gpio_setMode(LCD_BKLIGHT, OUTPUT); + gpio_clearPin(LCD_BKLIGHT); + + /* Shut down timer */ + RCC->APB2ENR &= ~RCC_APB2ENR_TIM8EN; + __DSB(); +} + +/* + * This function is defined in platform.h + */ +void platform_setBacklightLevel(uint8_t level) +{ + TIM8->CCR1 = level; +} + +#elif defined(ENABLE_BKLIGHT_DIMMING) /* MD-UV3x0 AND dimming enabled */ + +/* + * Interrupt-based software PWM for backlight dimming on MD-UV3x0. + * On this family of devices the GPIO for backlight control is not connected to + * any of the available timer compare output channels, thus making impossible to + * implement an hardware-based PWM. + * However, we provide a software-base backlight dimming for experimental purposes. + */ + +/* Name of interrupt handler is mangled for C++ compatibility */ +void _Z29TIM1_TRG_COM_TIM11_IRQHandlerv() +{ + if(TIM11->SR & TIM_SR_CC1IF) + { + gpio_clearPin(LCD_BKLIGHT); /* Clear pin on compare match */ + } + + if(TIM11->SR & TIM_SR_UIF) + { + gpio_setPin(LCD_BKLIGHT); /* Set pin on counter reload */ + } + + TIM11->SR = 0; +} + +void backlight_init() +{ + gpio_setMode(LCD_BKLIGHT, OUTPUT); + gpio_clearPin(LCD_BKLIGHT); + + /* + * Configure TIM11 for backlight PWM: Fpwm = 256Hz, 8 bit of resolution. + * APB2 freq. is 84MHz but timer runs at twice this frequency, then: + * PSC = 2564 to have Ftick = 65.52kHz + * With ARR = 256, Fpwm is 256Hz; + */ + RCC->APB2ENR |= RCC_APB2ENR_TIM11EN; + __DSB(); + + TIM11->ARR = 255; + TIM11->PSC = 2563; + TIM11->CNT = 0; + TIM11->CR1 |= TIM_CR1_ARPE; + TIM11->CCMR1 |= TIM_CCMR1_OC1M_2 + | TIM_CCMR1_OC1M_1 + | TIM_CCMR1_OC1PE; + TIM11->CCER |= TIM_CCER_CC1E; + TIM11->CCR1 = 0; + TIM11->EGR = TIM_EGR_UG; /* Update registers */ + TIM11->SR = 0; /* Clear interrupt flags */ + TIM11->DIER = TIM_DIER_CC1IE /* Interrupt on compare match */ + | TIM_DIER_UIE; /* Interrupt on counter reload */ + TIM11->CR1 |= TIM_CR1_CEN; /* Start timer */ + + NVIC_ClearPendingIRQ(TIM1_TRG_COM_TIM11_IRQn); + NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn,15); + NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn); +} + +void backlight_terminate() +{ + /* Shut down backlight */ + gpio_clearPin(LCD_BKLIGHT); + + /* Shut down timer */ + RCC->APB2ENR &= ~RCC_APB2ENR_TIM11EN; + __DSB(); +} + +/* + * This function is defined in platform.h + */ +void platform_setBacklightLevel(uint8_t level) +{ + /* + * Little workaround for the following nasty behaviour: if CCR1 value is + * zero, a waveform with 99% duty cycle is generated. This is because we are + * emulating pwm with interrupts. + */ + if(level > 1) + { + TIM11->CCR1 = level; + TIM11->CR1 |= TIM_CR1_CEN; + } + else + { + TIM11->CR1 &= ~TIM_CR1_CEN; + gpio_clearPin(LCD_BKLIGHT); + } +} + +#endif diff --git a/platform/targets/DM-1801/platform.c b/platform/targets/DM-1801/platform.c index b32949db..5c6ac2ae 100644 --- a/platform/targets/DM-1801/platform.c +++ b/platform/targets/DM-1801/platform.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "hwconfig.h" /* Mutex for concurrent access to ADC0 */ @@ -40,28 +41,14 @@ void platform_init() gpio_setMode(GREEN_LED, OUTPUT); gpio_setMode(RED_LED, OUTPUT); - gpio_setMode(LCD_BKLIGHT, OUTPUT); - gpio_clearPin(LCD_BKLIGHT); - gpio_setMode(PTT_SW, INPUT); gpio_setMode(PWR_SW, OUTPUT); /* - * Configure backlight PWM: 58.5kHz, 8 bit resolution + * Initialise backlight driver */ - SIM->SCGC6 |= SIM_SCGC6_FTM0(1); /* Enable clock */ - - FTM0->CONTROLS[3].CnSC = FTM_CnSC_MSB(1) - | FTM_CnSC_ELSB(1); /* Edge-aligned PWM, clear on match */ - FTM0->CONTROLS[3].CnV = 0; - - FTM0->MOD = 0xFF; /* Reload value */ - FTM0->SC = FTM_SC_PS(3) /* Prescaler divide by 8 */ - | FTM_SC_CLKS(1); /* Enable timer */ - - gpio_setMode(LCD_BKLIGHT, OUTPUT); - gpio_setAlternateFunction(LCD_BKLIGHT, 2); + backlight_init(); /* * Initialise ADC @@ -100,7 +87,9 @@ void platform_init() void platform_terminate() { - gpio_clearPin(LCD_BKLIGHT); + /* Shut down backlight */ + backlight_terminate(); + gpio_clearPin(RED_LED); gpio_clearPin(GREEN_LED); @@ -197,11 +186,6 @@ void platform_beepStop() /* TODO */ } -void platform_setBacklightLevel(uint8_t level) -{ - FTM0->CONTROLS[3].CnV = level; -} - const void *platform_getCalibrationData() { /* The first time this function is called, load calibration data from flash */ @@ -217,3 +201,10 @@ const hwInfo_t *platform_getHwInfo() { return &hwInfo; } + + +/* + * NOTE: implementation of this API function is provided in + * platform/drivers/backlight/backlight_GDx.c + */ +// void platform_setBacklightLevel(uint8_t level) diff --git a/platform/targets/GD-77/platform.c b/platform/targets/GD-77/platform.c index d190d3bd..f0d6ae67 100644 --- a/platform/targets/GD-77/platform.c +++ b/platform/targets/GD-77/platform.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "hwconfig.h" pthread_mutex_t adc_mutex; @@ -39,28 +40,14 @@ void platform_init() gpio_setMode(GREEN_LED, OUTPUT); gpio_setMode(RED_LED, OUTPUT); - gpio_setMode(LCD_BKLIGHT, OUTPUT); - gpio_clearPin(LCD_BKLIGHT); - gpio_setMode(PTT_SW, INPUT); gpio_setMode(PWR_SW, OUTPUT); /* - * Configure backlight PWM: 58.5kHz, 8 bit resolution + * Initialise backlight driver */ - SIM->SCGC6 |= SIM_SCGC6_FTM0(1); /* Enable clock */ - - FTM0->CONTROLS[3].CnSC = FTM_CnSC_MSB(1) - | FTM_CnSC_ELSB(1); /* Edge-aligned PWM, clear on match */ - FTM0->CONTROLS[3].CnV = 0; - - FTM0->MOD = 0xFF; /* Reload value */ - FTM0->SC = FTM_SC_PS(3) /* Prescaler divide by 8 */ - | FTM_SC_CLKS(1); /* Enable timer */ - - gpio_setMode(LCD_BKLIGHT, OUTPUT); - gpio_setAlternateFunction(LCD_BKLIGHT, 2); + backlight_init(); /* * Initialise ADC @@ -99,7 +86,9 @@ void platform_init() void platform_terminate() { - gpio_clearPin(LCD_BKLIGHT); + /* Shut down backlight */ + backlight_terminate(); + gpio_clearPin(RED_LED); gpio_clearPin(GREEN_LED); @@ -196,11 +185,6 @@ void platform_beepStop() /* TODO */ } -void platform_setBacklightLevel(uint8_t level) -{ - FTM0->CONTROLS[3].CnV = level; -} - const void *platform_getCalibrationData() { /* The first time this function is called, load calibration data from flash */ @@ -216,3 +200,10 @@ const hwInfo_t *platform_getHwInfo() { return &hwInfo; } + + +/* + * NOTE: implementation of this API function is provided in + * platform/drivers/backlight/backlight_GDx.c + */ +// void platform_setBacklightLevel(uint8_t level) diff --git a/platform/targets/MD-3x0/platform.c b/platform/targets/MD-3x0/platform.c index ac21bdb2..cb952bf1 100644 --- a/platform/targets/MD-3x0/platform.c +++ b/platform/targets/MD-3x0/platform.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -62,45 +63,18 @@ void platform_init() nvm_loadHwInfo(&hwInfo); /* Load hardware information data */ toneGen_init(); /* Initialise tone generator */ rtc_init(); /* Initialise RTC */ - - /* - * Configure TIM8 for backlight PWM: Fpwm = 100kHz with 8 bit of resolution. - * APB2 freq. is 84MHz, but timer runs at twice this frequency. - * Then: PSC = 655 to have Ftick = 256.097kHz - * With ARR = 256, Fpwm is 100kHz; - * Backlight pin is connected to TIM8 CR1. - */ - RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; - __DSB(); - - TIM8->ARR = 255; - TIM8->PSC = 654; - TIM8->CNT = 0; - TIM8->CR1 |= TIM_CR1_ARPE; /* LCD backlight is on PC6, TIM8-CH1 */ - TIM8->CCMR1 |= TIM_CCMR1_OC1M_2 - | TIM_CCMR1_OC1M_1 - | TIM_CCMR1_OC1PE; - TIM8->CCER |= TIM_CCER_CC1E; - TIM8->BDTR |= TIM_BDTR_MOE; - TIM8->CCR1 = 0; - TIM8->EGR = TIM_EGR_UG; /* Update registers */ - TIM8->CR1 |= TIM_CR1_CEN; /* Start timer */ + backlight_init(); /* Initialise backlight driver */ } void platform_terminate() { /* Shut down backlight */ - gpio_setMode(LCD_BKLIGHT, OUTPUT); - gpio_clearPin(LCD_BKLIGHT); + backlight_terminate(); /* Shut down LEDs */ gpio_clearPin(GREEN_LED); gpio_clearPin(RED_LED); - /* Shut down timer */ - RCC->APB2ENR &= ~RCC_APB2ENR_TIM8EN; - __DSB(); - /* Shut down all the modules */ adc1_terminate(); nvm_terminate(); @@ -193,11 +167,6 @@ void platform_beepStop() /* TODO */ } -void platform_setBacklightLevel(uint8_t level) -{ - TIM8->CCR1 = level; -} - const void *platform_getCalibrationData() { return ((const void *) &calibration); @@ -207,3 +176,9 @@ const hwInfo_t *platform_getHwInfo() { return &hwInfo; } + +/* + * NOTE: implementation of this API function is provided in + * platform/drivers/backlight/backlight_MDx.c + */ +// void platform_setBacklightLevel(uint8_t level) diff --git a/platform/targets/MD-9600/platform.c b/platform/targets/MD-9600/platform.c index 62e5d3cc..861f3d6e 100644 --- a/platform/targets/MD-9600/platform.c +++ b/platform/targets/MD-9600/platform.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -32,9 +33,6 @@ hwInfo_t hwInfo; void platform_init() { - gpio_setMode(LCD_BKLIGHT, ALTERNATE); - gpio_setAlternateFunction(LCD_BKLIGHT, 3); - gpio_setMode(CH_SELECTOR_0, INPUT_PULL_UP); gpio_setMode(CH_SELECTOR_1, INPUT_PULL_UP); @@ -77,40 +75,13 @@ void platform_init() nvm_init(); /* Initialise non volatile memory manager */ toneGen_init(); /* Initialise tone generator */ rtc_init(); /* Initialise RTC */ - - /* - * Configure TIM8 for backlight PWM: Fpwm = 100kHz with 8 bit of resolution. - * APB2 freq. is 84MHz, but timer runs at twice this frequency. - * Then: PSC = 655 to have Ftick = 256.097kHz - * With ARR = 256, Fpwm is 100kHz; - * Backlight pin is connected to TIM8 CR1. - */ - RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; - __DSB(); - - TIM8->ARR = 255; - TIM8->PSC = 654; - TIM8->CNT = 0; - TIM8->CR1 |= TIM_CR1_ARPE; /* LCD backlight is on PC6, TIM8-CH1 */ - TIM8->CCMR1 |= TIM_CCMR1_OC1M_2 - | TIM_CCMR1_OC1M_1 - | TIM_CCMR1_OC1PE; - TIM8->CCER |= TIM_CCER_CC1E; - TIM8->BDTR |= TIM_BDTR_MOE; - TIM8->CCR1 = 0; - TIM8->EGR = TIM_EGR_UG; /* Update registers */ - TIM8->CR1 |= TIM_CR1_CEN; /* Start timer */ + backlight_init(); /* Initialise backlight driver */ } void platform_terminate() { /* Shut down backlight */ - gpio_setMode(LCD_BKLIGHT, OUTPUT); - gpio_clearPin(LCD_BKLIGHT); - - /* Shut down timer */ - RCC->APB2ENR &= ~RCC_APB2ENR_TIM8EN; - __DSB(); + backlight_terminate(); /* Shut down all the modules */ adc1_terminate(); @@ -179,11 +150,6 @@ void platform_beepStop() /* TODO */ } -void platform_setBacklightLevel(uint8_t level) -{ - TIM8->CCR1 = level; -} - const void *platform_getCalibrationData() { return NULL; @@ -193,3 +159,10 @@ const hwInfo_t *platform_getHwInfo() { return &hwInfo; } + +/* + * NOTE: implementation of this API function is provided in + * platform/drivers/backlight/backlight_MDx.c + */ +// void platform_setBacklightLevel(uint8_t level) + diff --git a/platform/targets/MD-UV3x0/platform.c b/platform/targets/MD-UV3x0/platform.c index eff37d41..440e8f9e 100644 --- a/platform/targets/MD-UV3x0/platform.c +++ b/platform/targets/MD-UV3x0/platform.c @@ -27,27 +27,14 @@ #include #include +#ifdef ENABLE_BKLIGHT_DIMMING +#include +#endif + mduv3x0Calib_t calibration; hwInfo_t hwInfo; static int8_t knob_pos = 0; -#ifdef ENABLE_BKLIGHT_DIMMING -void _Z29TIM1_TRG_COM_TIM11_IRQHandlerv() -{ - if(TIM11->SR & TIM_SR_CC1IF) - { - gpio_clearPin(LCD_BKLIGHT); /* Clear pin on compare match */ - } - - if(TIM11->SR & TIM_SR_UIF) - { - gpio_setPin(LCD_BKLIGHT); /* Set pin on counter reload */ - } - - TIM11->SR = 0; -} -#endif - /* * Note that this interrupt handler currently assumes only the encoder will * ever cause this interrupt to fire @@ -90,9 +77,6 @@ void platform_init() gpio_setMode(GREEN_LED, OUTPUT); gpio_setMode(RED_LED, OUTPUT); - gpio_setMode(LCD_BKLIGHT, OUTPUT); - gpio_clearPin(LCD_BKLIGHT); - gpio_setMode(CH_SELECTOR_0, INPUT_PULL_UP); gpio_setMode(CH_SELECTOR_1, INPUT_PULL_UP); @@ -126,44 +110,20 @@ void platform_init() rtc_init(); /* Initialise RTC */ #ifdef ENABLE_BKLIGHT_DIMMING - /* - * Configure TIM11 for backlight PWM: Fpwm = 256Hz, 8 bit of resolution. - * APB2 freq. is 84MHz but timer runs at twice this frequency, then: - * PSC = 2564 to have Ftick = 65.52kHz - * With ARR = 256, Fpwm is 256Hz; - */ - RCC->APB2ENR |= RCC_APB2ENR_TIM11EN; - __DSB(); - - TIM11->ARR = 255; - TIM11->PSC = 2563; - TIM11->CNT = 0; - TIM11->CR1 |= TIM_CR1_ARPE; - TIM11->CCMR1 |= TIM_CCMR1_OC1M_2 - | TIM_CCMR1_OC1M_1 - | TIM_CCMR1_OC1PE; - TIM11->CCER |= TIM_CCER_CC1E; - TIM11->CCR1 = 0; - TIM11->EGR = TIM_EGR_UG; /* Update registers */ - TIM11->SR = 0; /* Clear interrupt flags */ - TIM11->DIER = TIM_DIER_CC1IE /* Interrupt on compare match */ - | TIM_DIER_UIE; /* Interrupt on counter reload */ - TIM11->CR1 |= TIM_CR1_CEN; /* Start timer */ - - NVIC_ClearPendingIRQ(TIM1_TRG_COM_TIM11_IRQn); - NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn,15); - NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn); + backlight_init(); /* Initialise backlight driver */ + #else + gpio_setMode(LCD_BKLIGHT, OUTPUT); + gpio_clearPin(LCD_BKLIGHT); #endif } void platform_terminate() { /* Shut down backlight */ - gpio_clearPin(LCD_BKLIGHT); - #ifdef ENABLE_BKLIGHT_DIMMING - RCC->APB2ENR &= ~RCC_APB2ENR_TIM11EN; - __DSB(); + backlight_terminate(); + #else + gpio_clearPin(LCD_BKLIGHT); #endif /* Shut down LEDs */ @@ -259,31 +219,6 @@ void platform_beepStop() /* TODO */ } -void platform_setBacklightLevel(uint8_t level) -{ - /* - * Little workaround for the following nasty behaviour: if CCR1 value is - * zero, a waveform with 99% duty cycle is generated. This is because we are - * emulating pwm with interrupts. - */ - if(level > 1) - { - #ifdef ENABLE_BKLIGHT_DIMMING - TIM11->CCR1 = level; - TIM11->CR1 |= TIM_CR1_CEN; - #else - gpio_setPin(LCD_BKLIGHT); - #endif - } - else - { - #ifdef ENABLE_BKLIGHT_DIMMING - TIM11->CR1 &= ~TIM_CR1_CEN; - #endif - gpio_clearPin(LCD_BKLIGHT); - } -} - const void *platform_getCalibrationData() { return ((const void *) &calibration); @@ -293,3 +228,22 @@ const hwInfo_t *platform_getHwInfo() { return &hwInfo; } + +/* + * NOTE: when backligth dimming is enabled, the implementation of this API + * function is provided in platform/drivers/backlight/backlight_MDx.c to avoid + * an useless function call. + */ +#ifndef ENABLE_BKLIGHT_DIMMING +void platform_setBacklightLevel(uint8_t level) +{ + if(level > 1) + { + gpio_setPin(LCD_BKLIGHT); + } + else + { + gpio_clearPin(LCD_BKLIGHT); + } +} +#endif