2#include "main-header.h"
5#define TIMER_FREQUENCY 10000
7#define MAX_PIN_STATES 20
15static struct pin_state pinstates[MAX_PIN_STATES];
16static uint32_t
volatile dim_ctr;
23static uint8_t timer_enabled = 0;
24static uint8_t timer_use = 0;
32 memset(&pinstates, 0,
sizeof(pinstates));
35 if ((r = mculib_pin_out(MAIN_MCULIBHANDLE, PIN_FET_A, HAL_PIN_FASTEST))) {
36 printf(
"[fet] Failed to set pin %i for outputmode: %i\r\n", PIN_FET_A, r);
38 if ((r = mculib_pin_out(MAIN_MCULIBHANDLE, PIN_FET_B, HAL_PIN_FASTEST))) {
39 printf(
"[fet] Failed to set pin %i for outputmode: %i\r\n", PIN_FET_A, r);
60static uint32_t calc_speed(uint32_t value, uint32_t target) {
78static struct pin_state *store_pin(uint8_t pin, uint8_t ispwm, uint32_t value) {
82 for (i = 0; i < MAX_PIN_STATES; i++) {
84 if ((lps == NULL) && (ps->pin == 0)) {
89 ps->target_pwm = value;
93 ps->speed = calc_speed(ps->value, ps->target_pwm);
98 printf(
"[fet] no more pin states available\r\n");
103 lps->target_pwm = value;
107 lps->speed = calc_speed(lps->value, lps->target_pwm);
111static void do_dim() {
112 if (dim_ctr < (TIMER_FREQUENCY / 100)) {
118 for (i = 0; i < MAX_PIN_STATES; i++) {
120 if ((ps->pin == 0) || (ps->pwm == 0) || (ps->speed == 0)) {
123 if (ps->value == ps->target_pwm) {
126 uint32_t s = ps->speed;
127 if (ps->value > ps->target_pwm) {
131 if (ps->value - s < ps->target_pwm) {
132 s = ps->value - ps->target_pwm;
134 ps->value = ps->value - s;
136 if (ps->value + s > ps->target_pwm) {
137 s = ps->target_pwm - ps->value;
139 ps->value = ps->value + s;
141 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, FET_TIMER, ps->pin, ps->value);
147 if ((timer_enabled != 0) && (timer_use == 0)) {
148 int r = mculib_timer_disable(MAIN_MCULIBHANDLE, FET_TIMER);
150 printf(
"[fet] failed to init timer %i: %s\r\n,", FET_TIMER, r);
153 printf(
"[fet] timer %i disabled \r\n", FET_TIMER);
158static void set_pin(
int pin,
int state) {
159 timer_use &= ~(1 << (pin - 100));
162 store_pin(pin, 0, state);
163 printf(
"Setting mosfet %i to %s\r\n", pin, (state ?
"ON":
"OFF"));
164 if ((r = mculib_pin_out(MAIN_MCULIBHANDLE, pin, HAL_PIN_FASTEST))) {
165 printf(
"[fet] Failed to set pin %i for outputmode: %i\r\n", pin, r);
167 r = mculib_pin_set(MAIN_MCULIBHANDLE, pin, state);
169 printf(
"[fet] failed to set pin %i: %i\r\n", pin, r);
173static void timer_irq() {
178void fets_set_com(
struct command *com) {
179 int pin = atoi(
get_arg(com, 0));
180 uint32_t state = atoi(
get_arg(com, 1));
186 pin_pwm(MAIN_MCULIBHANDLE, pin, state, flags);
188void pin_pwm(MCULIB_HANDLE handle,
int pin, uint32_t state,
int flags) {
189 if ((flags == 0) || (state == 0)) {
193 struct pin_state *ps = store_pin(pin, 1, state);
196 if (timer_enabled == 0) {
197 int r = mculib_timer_enable_simple(MAIN_MCULIBHANDLE, FET_TIMER, TIMER_FREQUENCY, &timer_irq);
199 printf(
"[fet] failed to init timer %i: %s\r\n,", FET_TIMER, r);
203 printf(
"[fet] FET Timer (%i) enabled\r\n", FET_TIMER);
206 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, FET_TIMER, pin, ps->value);
208 timer_use |= (1 << (pin - 100));
209 int r = mculib_timer_attach_pin_pwm(MAIN_MCULIBHANDLE, FET_TIMER, pin);
212 printf(
"[fet] Failed to attach pin %i to timer: %i\r\n", pin, r);
223 printf(
"[fet] pin %i set to pwm %i\r\n", pin, state);
226void print_pwm_state() {
227 printf(
"pin pwm target_pwm speed value\r\n");
230 for (i = 0; i < MAX_PIN_STATES; i++) {
235 printf(
"%i %i %i %i %i\r\n", ps->pin, ps->pwm, ps->target_pwm, ps->speed, ps->value);
const char * get_arg(const struct command *com, int index)
given an argument by index[0..n], will return a pointer to the bytearray (excluding the fieldtype) th...