kosmos/source/test/pwm/main.c
2016-08-09 12:32:19 +02:00

98 lines
2.1 KiB
C

/*
* main.c
*
* Created on: Aug 2, 2016
* Author: tkl
*/
#include <stdbool.h>
#include "driver.h"
#include "board.h"
#include "stack.h"
#include "queue.h"
#include "kernel.h"
#include "driver.h"
#include "list.h"
#include "shell.h"
#include "stm32f4xx.h"
#include "pwm.h"
#include "stm32f4_pwm.h"
/* apb1 clock = 84MHz */
/* period_reg = src_clk / presc / cnt_clk */
/* 4199 = 84MHZ / (0 + 1) / 20kHz - 1 */
static const TIM_TimeBaseInitTypeDef timer_4_cfg = {
.TIM_RepetitionCounter = 0x0000,
.TIM_Prescaler = 0,
.TIM_ClockDivision = TIM_CKD_DIV1,
.TIM_CounterMode = TIM_CounterMode_Up,
.TIM_Period = 4199
};
static const TIM_OCInitTypeDef t4_output_compare_cfg = {
.TIM_OutputNState = TIM_OutputNState_Disable,
.TIM_OCNPolarity = TIM_OCPolarity_High,
.TIM_OCIdleState = TIM_OCIdleState_Reset,
.TIM_OCNIdleState = TIM_OCNIdleState_Set,
.TIM_OCMode = TIM_OCMode_PWM1,
.TIM_OCPolarity = TIM_OCPolarity_High,
.TIM_OutputState = TIM_OutputState_Enable,
.TIM_Pulse = 0 // Initiale Pulsweite in Millisekunden
};
static const GPIO_InitTypeDef port_cfg = {
.GPIO_Pin = GPIO_Pin_15,
.GPIO_Mode = GPIO_Mode_AF,
.GPIO_OType = GPIO_OType_PP,
.GPIO_PuPd = GPIO_PuPd_UP,
.GPIO_Speed = GPIO_Speed_100MHz,
};
static struct stm32f4_pwm str32f4_pwm = {
.timer = TIM4,
.timer_cfg = &timer_4_cfg,
.output_compare_cfg = &t4_output_compare_cfg,
.port = GPIOD,
.pin_src = GPIO_PinSource15,
.port_cfg = &port_cfg,
.channel = channel_4,
};
static const struct pwm pwm_ch4 = {
.arch_dep_device = &str32f4_pwm,
.fp = &stm32f4_pwm_fp,
};
#define TH_STACK_SIZE 256
stack_t th_stack[TH_STACK_SIZE];
struct thread_context th_ctx;
static void th_func(void *arg)
{
unsigned int duty = 0;
pwm_open(&pwm_ch4);
while(1) {
while(duty < 100) {
pwm_set_duty_cycle(&pwm_ch4, duty);
sleep_ms(10);
duty++;
}
while(duty > 0) {
pwm_set_duty_cycle(&pwm_ch4, duty);
sleep_ms(10);
duty--;
}
}
}
int main(void)
{
thread_create(&th_ctx, th_stack, TH_STACK_SIZE, th_func, NULL, THREAD_PRIO_LOW);
schedule_start();
return 0;
}