kosmos/source/test/pwm/main.c

93 lines
2.0 KiB
C
Raw Normal View History

2016-08-08 09:19:08 +00:00
/*
* 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"
2016-08-09 08:43:47 +00:00
#include "stm32f4xx.h"
2016-08-09 10:28:01 +00:00
#include "pwm.h"
2016-08-09 09:57:54 +00:00
#include "stm32f4_pwm.h"
2016-08-09 08:43:47 +00:00
2016-08-09 09:20:33 +00:00
/* apb1 clock = 84MHz */
/* period_reg = src_clk / presc / cnt_clk */
/* 4199 = 84MHZ / (0 + 1) / 20kHz - 1 */
2016-08-09 08:43:47 +00:00
static const TIM_TimeBaseInitTypeDef timer_4_cfg = {
.TIM_RepetitionCounter = 0x0000,
2016-08-09 09:20:33 +00:00
.TIM_Prescaler = 0,
2016-08-09 08:43:47 +00:00
.TIM_ClockDivision = TIM_CKD_DIV1,
.TIM_CounterMode = TIM_CounterMode_Up,
2016-08-09 09:20:33 +00:00
.TIM_Period = 4199
2016-08-09 08:43:47 +00:00
};
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,
2016-08-09 09:20:33 +00:00
.TIM_Pulse = 0 // Initiale Pulsweite in Millisekunden
2016-08-09 08:43:47 +00:00
};
static const GPIO_InitTypeDef port_cfg = {
2016-08-09 09:20:33 +00:00
.GPIO_Pin = GPIO_Pin_15,
2016-08-09 08:43:47 +00:00
.GPIO_Mode = GPIO_Mode_AF,
.GPIO_OType = GPIO_OType_PP,
.GPIO_PuPd = GPIO_PuPd_UP,
2016-08-09 09:20:33 +00:00
.GPIO_Speed = GPIO_Speed_100MHz,
2016-08-09 08:43:47 +00:00
};
static struct stm32f4_pwm str32f4_pwm = {
.timer = TIM4,
.timer_cfg = &timer_4_cfg,
.output_compare_cfg = &t4_output_compare_cfg,
.port = GPIOD,
2016-08-09 09:20:33 +00:00
.pin_src = GPIO_PinSource15,
2016-08-09 08:43:47 +00:00
.port_cfg = &port_cfg,
2016-08-09 09:20:33 +00:00
.channel = channel_4,
2016-08-09 08:43:47 +00:00
};
2016-08-09 10:28:01 +00:00
static const struct pwm pwm_ch4 = {
.arch_dep_device = &str32f4_pwm,
.fp = &stm32f4_pwm_fp,
};
2016-08-09 08:43:47 +00:00
2016-08-08 09:19:08 +00:00
#define TH_STACK_SIZE 256
stack_t th_stack[TH_STACK_SIZE];
struct thread_context th_ctx;
static void th_func(void *arg)
{
2016-08-09 09:57:54 +00:00
unsigned int duty = 0;
2016-08-09 10:28:01 +00:00
pwm_open(&pwm_ch4);
2016-08-08 09:19:08 +00:00
while(1) {
2016-08-09 10:28:01 +00:00
pwm_set_duty_cycle(&pwm_ch4, duty);
2016-08-09 09:57:54 +00:00
sleep_ms(100);
duty++;
if(duty > 100)
duty = 0;
2016-08-08 09:19:08 +00:00
}
}
int main(void)
{
thread_create(&th_ctx, th_stack, TH_STACK_SIZE, th_func, NULL, THREAD_PRIO_LOW);
schedule_start();
return 0;
}