2017-05-13 08:21:47 -07:00
|
|
|
/*
|
|
|
|
Copyright (c) 2017 Daniel Fekete
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-04-16 06:40:46 -07:00
|
|
|
#include "stm32_gpio.h"
|
|
|
|
|
2017-04-17 03:37:05 -07:00
|
|
|
TIM_HandleTypeDef *handle;
|
2017-04-16 06:40:46 -07:00
|
|
|
|
|
|
|
static uint32_t counter;
|
2017-06-19 21:28:43 -07:00
|
|
|
static uint32_t waitCycles;
|
2017-06-19 21:38:11 -07:00
|
|
|
static uint8_t analogWriteResolutionBits = 8;
|
2017-06-19 21:28:43 -07:00
|
|
|
|
|
|
|
const uint32_t TIMER_MAX_CYCLES = UINT16_MAX;
|
|
|
|
|
|
|
|
const uint32_t PWM_FREQUENCY_HZ = 1000;
|
2017-04-16 06:40:46 -07:00
|
|
|
|
2017-05-21 23:04:06 -07:00
|
|
|
extern void pinMode(uint8_t, uint8_t);
|
|
|
|
|
2017-04-16 06:40:46 -07:00
|
|
|
#define min(a,b) ((a)<(b)?(a):(b))
|
|
|
|
|
|
|
|
stm32_pwm_disable_callback_func stm32_pwm_disable_callback = NULL;
|
|
|
|
|
2017-04-17 03:37:05 -07:00
|
|
|
void (*pwm_callback_func)();
|
|
|
|
|
|
|
|
void pwm_callback();
|
|
|
|
|
2017-04-16 06:40:46 -07:00
|
|
|
typedef struct {
|
|
|
|
GPIO_TypeDef *port;
|
2017-08-06 00:59:41 -07:00
|
|
|
void (*callback)();
|
2017-04-16 06:40:46 -07:00
|
|
|
uint32_t pin_mask;
|
2017-06-20 09:39:31 -07:00
|
|
|
uint32_t waveLengthCycles;
|
|
|
|
uint32_t dutyCycle;
|
|
|
|
int32_t counterCycles;
|
2017-04-16 06:40:46 -07:00
|
|
|
} stm32_pwm_type;
|
|
|
|
|
2017-04-17 12:01:24 -07:00
|
|
|
static stm32_pwm_type pwm_config[sizeof(variant_pin_list) / sizeof(variant_pin_list[0])];
|
2017-04-16 06:40:46 -07:00
|
|
|
|
|
|
|
void stm32_pwm_disable(GPIO_TypeDef *port, uint32_t pin);
|
|
|
|
|
2017-06-19 21:38:11 -07:00
|
|
|
void analogWriteResolution(int bits) {
|
|
|
|
analogWriteResolutionBits = bits;
|
|
|
|
}
|
|
|
|
|
2017-08-06 00:59:41 -07:00
|
|
|
static void initTimer() {
|
2017-04-17 03:37:05 -07:00
|
|
|
static TIM_HandleTypeDef staticHandle;
|
|
|
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
handle = &staticHandle;
|
|
|
|
pwm_callback_func = &pwm_callback;
|
|
|
|
|
2017-04-16 06:40:46 -07:00
|
|
|
stm32_pwm_disable_callback = &stm32_pwm_disable;
|
|
|
|
|
|
|
|
|
2017-05-09 05:03:31 -07:00
|
|
|
#ifdef TIM2 //99% of chips have TIM2
|
|
|
|
__HAL_RCC_TIM2_CLK_ENABLE();
|
|
|
|
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
|
|
|
|
HAL_NVIC_EnableIRQ(TIM2_IRQn);
|
|
|
|
|
|
|
|
handle->Instance = TIM2;
|
|
|
|
#else
|
|
|
|
__HAL_RCC_TIM3_CLK_ENABLE();
|
|
|
|
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
|
|
|
|
HAL_NVIC_EnableIRQ(TIM3_IRQn);
|
|
|
|
|
|
|
|
handle->Instance = TIM3;
|
|
|
|
#endif
|
|
|
|
|
2017-06-19 21:28:43 -07:00
|
|
|
handle->Init.Prescaler = 0;
|
2017-04-17 03:37:05 -07:00
|
|
|
handle->Init.CounterMode = TIM_COUNTERMODE_UP;
|
2017-06-19 21:28:43 -07:00
|
|
|
waitCycles = TIMER_MAX_CYCLES;
|
|
|
|
handle->Init.Period = waitCycles;
|
2017-04-17 03:37:05 -07:00
|
|
|
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
HAL_TIM_Base_Init(handle);
|
2017-04-16 06:40:46 -07:00
|
|
|
|
2017-04-17 03:37:05 -07:00
|
|
|
HAL_TIM_Base_Start_IT(handle);
|
2017-04-16 06:40:46 -07:00
|
|
|
}
|
2017-08-06 00:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void pwmWrite(uint8_t pin, int dutyCycle, int frequency, int durationMillis) {
|
|
|
|
initTimer();
|
2017-04-16 06:40:46 -07:00
|
|
|
|
2017-05-22 22:13:30 -07:00
|
|
|
for(size_t i=0; i<sizeof(pwm_config) / sizeof(pwm_config[0]); i++) {
|
2017-04-16 06:40:46 -07:00
|
|
|
if (pwm_config[i].port == NULL ||
|
2017-04-17 12:01:24 -07:00
|
|
|
(pwm_config[i].port == variant_pin_list[pin].port
|
|
|
|
&& pwm_config[i].pin_mask == variant_pin_list[pin].pin_mask)) {
|
2017-04-16 06:40:46 -07:00
|
|
|
|
|
|
|
if (pwm_config[i].port == NULL) {
|
|
|
|
pinMode(pin, OUTPUT);
|
|
|
|
}
|
|
|
|
|
2017-04-17 12:01:24 -07:00
|
|
|
pwm_config[i].port = variant_pin_list[pin].port;
|
|
|
|
pwm_config[i].pin_mask = variant_pin_list[pin].pin_mask;
|
2017-06-20 09:39:31 -07:00
|
|
|
pwm_config[i].waveLengthCycles = HAL_RCC_GetPCLK2Freq() / frequency;
|
2017-08-06 01:01:48 -07:00
|
|
|
pwm_config[i].dutyCycle = (uint64_t)pwm_config[i].waveLengthCycles * dutyCycle >> 16;
|
2017-06-20 09:39:31 -07:00
|
|
|
|
|
|
|
if (durationMillis > 0) {
|
|
|
|
pwm_config[i].counterCycles = HAL_RCC_GetPCLK2Freq() / 1000 * durationMillis;
|
|
|
|
}
|
|
|
|
|
2017-04-16 06:40:46 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 09:39:31 -07:00
|
|
|
extern void tone(uint8_t pin, unsigned int frequency, unsigned long durationMillis) {
|
2017-08-06 01:01:48 -07:00
|
|
|
pwmWrite(pin, 1 << 15, frequency, durationMillis);
|
2017-06-20 09:39:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void analogWrite(uint8_t pin, int value) {
|
2017-08-06 01:01:48 -07:00
|
|
|
pwmWrite(pin, ((uint32_t)value << 16) >> analogWriteResolutionBits, PWM_FREQUENCY_HZ, 0);
|
2017-06-20 09:39:31 -07:00
|
|
|
}
|
|
|
|
|
2017-08-06 00:59:41 -07:00
|
|
|
void stm32ScheduleMicros(uint32_t microseconds, void (*callback)()) {
|
|
|
|
initTimer();
|
|
|
|
|
|
|
|
for(size_t i=0; i<sizeof(pwm_config) / sizeof(pwm_config[0]); i++) {
|
|
|
|
if (pwm_config[i].port == NULL && pwm_config[i].callback == NULL) {
|
|
|
|
|
|
|
|
pwm_config[i].callback = callback;
|
|
|
|
|
|
|
|
pwm_config[i].waveLengthCycles = HAL_RCC_GetPCLK2Freq() * (uint64_t)microseconds / 1000000;
|
|
|
|
pwm_config[i].counterCycles = pwm_config[i].waveLengthCycles;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 06:40:46 -07:00
|
|
|
void stm32_pwm_disable(GPIO_TypeDef *port, uint32_t pin_mask) {
|
2017-05-22 22:13:30 -07:00
|
|
|
for(size_t i=0; i<sizeof(pwm_config) / sizeof(pwm_config[0]); i++) {
|
2017-04-16 06:40:46 -07:00
|
|
|
if (pwm_config[i].port == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwm_config[i].port == port && pwm_config[i].pin_mask == pin_mask) {
|
|
|
|
|
2017-05-22 22:13:30 -07:00
|
|
|
for(size_t j = i + 1; j < sizeof(pwm_config) / sizeof(pwm_config[0]); j++) {
|
2017-04-16 06:40:46 -07:00
|
|
|
if (pwm_config[j].port == NULL) {
|
|
|
|
pwm_config[i].port = pwm_config[j - 1].port;
|
|
|
|
pwm_config[i].pin_mask = pwm_config[j - 1].pin_mask;
|
|
|
|
|
|
|
|
pwm_config[j - 1].port = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 03:37:05 -07:00
|
|
|
void pwm_callback() {
|
2017-05-26 10:21:33 -07:00
|
|
|
if(__HAL_TIM_GET_FLAG(handle, TIM_FLAG_UPDATE) != RESET) {
|
|
|
|
if(__HAL_TIM_GET_IT_SOURCE(handle, TIM_IT_UPDATE) !=RESET) {
|
|
|
|
__HAL_TIM_CLEAR_IT(handle, TIM_IT_UPDATE);
|
|
|
|
|
2017-06-19 21:28:43 -07:00
|
|
|
counter += waitCycles;
|
2017-06-20 09:39:31 -07:00
|
|
|
uint32_t nextWaitCycles = TIMER_MAX_CYCLES;
|
2017-05-26 10:21:33 -07:00
|
|
|
|
|
|
|
for(size_t i=0; i<sizeof(pwm_config); i++) {
|
|
|
|
if (pwm_config[i].port != NULL) {
|
2017-06-20 09:39:31 -07:00
|
|
|
if (pwm_config[i].dutyCycle > counter % pwm_config[i].waveLengthCycles) {
|
2017-05-26 10:21:33 -07:00
|
|
|
pwm_config[i].port->BSRR = pwm_config[i].pin_mask;
|
2017-06-20 09:39:31 -07:00
|
|
|
nextWaitCycles = min(nextWaitCycles, pwm_config[i].dutyCycle - (counter % pwm_config[i].waveLengthCycles));
|
2017-05-26 10:21:33 -07:00
|
|
|
} else {
|
|
|
|
pwm_config[i].port->BSRR = pwm_config[i].pin_mask << 16;
|
2017-06-20 09:39:31 -07:00
|
|
|
nextWaitCycles = min(nextWaitCycles, pwm_config[i].waveLengthCycles - counter % pwm_config[i].waveLengthCycles);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwm_config[i].counterCycles > 0) {
|
|
|
|
if (pwm_config[i].counterCycles <= (int)waitCycles) {
|
|
|
|
stm32_pwm_disable(pwm_config[i].port, pwm_config[i].pin_mask);
|
|
|
|
} else {
|
|
|
|
pwm_config[i].counterCycles -= waitCycles;
|
|
|
|
}
|
2017-05-26 10:21:33 -07:00
|
|
|
}
|
2017-08-06 00:59:41 -07:00
|
|
|
} else if (pwm_config[i].callback != NULL) {
|
|
|
|
pwm_config[i].counterCycles -= waitCycles;
|
|
|
|
if (pwm_config[i].counterCycles < 0) {
|
|
|
|
pwm_config[i].callback();
|
|
|
|
pwm_config[i].counterCycles += pwm_config[i].waveLengthCycles;
|
|
|
|
}
|
2017-05-26 10:21:33 -07:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2017-04-16 06:40:46 -07:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:39:31 -07:00
|
|
|
if (!nextWaitCycles || nextWaitCycles > TIMER_MAX_CYCLES) {
|
|
|
|
nextWaitCycles = TIMER_MAX_CYCLES;
|
2017-05-26 10:21:33 -07:00
|
|
|
}
|
2017-06-20 09:39:31 -07:00
|
|
|
waitCycles = nextWaitCycles;
|
|
|
|
|
2017-06-19 21:28:43 -07:00
|
|
|
__HAL_TIM_SET_AUTORELOAD(handle, waitCycles);
|
2017-05-26 10:21:33 -07:00
|
|
|
}
|
2017-04-16 06:40:46 -07:00
|
|
|
}
|
2017-04-17 03:37:05 -07:00
|
|
|
}
|
|
|
|
|
2017-05-09 05:03:31 -07:00
|
|
|
#ifdef TIM2
|
2017-05-26 10:21:33 -07:00
|
|
|
extern void TIM2_IRQHandler(void) {
|
2017-05-09 05:03:31 -07:00
|
|
|
#else
|
2017-05-26 10:21:33 -07:00
|
|
|
extern void TIM3_IRQHandler(void) {
|
2017-05-09 05:03:31 -07:00
|
|
|
#endif
|
2017-04-17 03:37:05 -07:00
|
|
|
|
|
|
|
if (pwm_callback_func != NULL) {
|
|
|
|
(*pwm_callback_func)();
|
|
|
|
}
|
2017-04-16 06:40:46 -07:00
|
|
|
}
|