SingingCat 0
application
fets.c
Go to the documentation of this file.
1#include "fets.h"
2#include "main-header.h"
3
4#define FET_TIMER 2
5#define TIMER_FREQUENCY 10000
6
7#define MAX_PIN_STATES 20
8typedef struct pin_state {
9 uint8_t pin;
10 uint8_t pwm; //0=on/off, 1=pwm
11 uint32_t target_pwm; //if ispwm and target_pwm != value, we dim until target_pwm == value
12 uint32_t speed;
13 uint32_t value; // either pwm value or 1==on,0==off
15static struct pin_state pinstates[MAX_PIN_STATES];
16static uint32_t volatile dim_ctr;
17// #include "bare-metal.h"
18
23static uint8_t timer_enabled = 0;
24static uint8_t timer_use = 0;
28void fets_init() {
29 int r;
30
31 dim_ctr = 0;
32 memset(&pinstates, 0, sizeof(pinstates));
33 timer_enabled = 0;
34 timer_use = 0;
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);
37 }
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);
40 }
41}
42
43/*
44 * static struct pin_state *get_pin(uint8_t pin) {
45 * int i;
46 * struct pin_state *lps = NULL;
47 * for(i=0;i<MAX_PIN_STATES;i++) {
48 * struct pin_state *ps = &pinstates[i];
49 * if ( (lps == NULL) && (ps->pin == 0) ) {
50 * lps = ps;
51 * }
52 * if (ps->pin == pin) {
53 * return ps;
54 * }
55 * }
56 * return NULL;
57 * }
58 *
59 */
60static uint32_t calc_speed(uint32_t value, uint32_t target) {
61 uint32_t v1 = value;
62 uint32_t v2 = target;
63
64 if (v1 > v2) {
65 v2 = value;
66 v1 = target;
67 }
68 // v2 is now bigger v1
69 // v2>v1 == true
70 uint32_t v = v2 - v1;
71
72 v = v / 100;
73 if (v == 0) {
74 v = 1;
75 }
76 return v;
77}
78static struct pin_state *store_pin(uint8_t pin, uint8_t ispwm, uint32_t value) {
79 int i;
80 struct pin_state *lps = NULL;
81
82 for (i = 0; i < MAX_PIN_STATES; i++) {
83 struct pin_state *ps = &pinstates[i];
84 if ((lps == NULL) && (ps->pin == 0)) {
85 lps = ps;
86 }
87 if (ps->pin == pin) {
88 ps->pwm = ispwm;
89 ps->target_pwm = value;
90 if (!ps->pwm) {
91 ps->value = value;
92 }
93 ps->speed = calc_speed(ps->value, ps->target_pwm);
94 return ps;
95 }
96 }
97 if (lps == NULL) {
98 printf("[fet] no more pin states available\r\n");
99 return NULL;
100 }
101 lps->pin = pin;
102 lps->pwm = ispwm;
103 lps->target_pwm = value;
104 if (!lps->pwm) {
105 lps->value = value;
106 }
107 lps->speed = calc_speed(lps->value, lps->target_pwm);
108 return lps;
109}
110
111static void do_dim() {
112 if (dim_ctr < (TIMER_FREQUENCY / 100)) {
113 return;
114 }
115 dim_ctr = 0;
116 int i;
117
118 for (i = 0; i < MAX_PIN_STATES; i++) {
119 struct pin_state *ps = &pinstates[i];
120 if ((ps->pin == 0) || (ps->pwm == 0) || (ps->speed == 0)) {
121 continue;
122 }
123 if (ps->value == ps->target_pwm) {
124 continue;
125 }
126 uint32_t s = ps->speed;
127 if (ps->value > ps->target_pwm) {
128 if (s > ps->value) {
129 s = ps->value;
130 }
131 if (ps->value - s < ps->target_pwm) {
132 s = ps->value - ps->target_pwm;
133 }
134 ps->value = ps->value - s;
135 } else {
136 if (ps->value + s > ps->target_pwm) {
137 s = ps->target_pwm - ps->value;
138 }
139 ps->value = ps->value + s;
140 }
141 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, FET_TIMER, ps->pin, ps->value);
142 // printf("Set pwm for pin %i to %i (stepped by %i to reach %i)\r\n",ps->pin,ps->value,s,ps->target_pwm);
143 }
144}
145
146void fets_loop() {
147 if ((timer_enabled != 0) && (timer_use == 0)) {
148 int r = mculib_timer_disable(MAIN_MCULIBHANDLE, FET_TIMER);
149 if (r != 0) {
150 printf("[fet] failed to init timer %i: %s\r\n,", FET_TIMER, r);
151 } else {
152 timer_enabled = 0;
153 printf("[fet] timer %i disabled \r\n", FET_TIMER);
154 }
155 }
156 do_dim();
157}
158static void set_pin(int pin, int state) {
159 timer_use &= ~(1 << (pin - 100));
160 int r;
161
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);
166 }
167 r = mculib_pin_set(MAIN_MCULIBHANDLE, pin, state);
168 if (r != 0) {
169 printf("[fet] failed to set pin %i: %i\r\n", pin, r);
170 }
171}
172
173static void timer_irq() {
174 dim_ctr++;
175}
176
177
178void fets_set_com(struct command *com) {
179 int pin = atoi(get_arg(com, 0));
180 uint32_t state = atoi(get_arg(com, 1));
181 int flags = 0;
182
183 if (com->argctr >= 3) {
184 flags = atoi(get_arg(com, 2));
185 }
186 pin_pwm(MAIN_MCULIBHANDLE, pin, state, flags);
187}
188void pin_pwm(MCULIB_HANDLE handle, int pin, uint32_t state, int flags) {
189 if ((flags == 0) || (state == 0)) {
190 set_pin(pin, state);
191 return;
192 }
193 struct pin_state *ps = store_pin(pin, 1, state);
194
195 // do a pwm
196 if (timer_enabled == 0) {
197 int r = mculib_timer_enable_simple(MAIN_MCULIBHANDLE, FET_TIMER, TIMER_FREQUENCY, &timer_irq);
198 if (r != 0) {
199 printf("[fet] failed to init timer %i: %s\r\n,", FET_TIMER, r);
200 return;
201 }
202 timer_enabled = 1;
203 printf("[fet] FET Timer (%i) enabled\r\n", FET_TIMER);
204 }
205
206 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, FET_TIMER, pin, ps->value);
207
208 timer_use |= (1 << (pin - 100));
209 int r = mculib_timer_attach_pin_pwm(MAIN_MCULIBHANDLE, FET_TIMER, pin);
210
211 if (r != 0) {
212 printf("[fet] Failed to attach pin %i to timer: %i\r\n", pin, r);
213 return;
214 }
215 /*
216 * r = mculib_timer_set_pwm(MAIN_MCULIBHANDLE, FET_TIMER, pin, state);
217 * if (r != 0) {
218 * printf("[fet] Failed to set pwm on pin %i to timer: %i\r\n", pin, r);
219 * return;
220 * }
221 */
222
223 printf("[fet] pin %i set to pwm %i\r\n", pin, state);
224}
225
226void print_pwm_state() {
227 printf("pin pwm target_pwm speed value\r\n");
228 int i;
229
230 for (i = 0; i < MAX_PIN_STATES; i++) {
231 struct pin_state *ps = &pinstates[i];
232 if (ps->pin == 0) {
233 continue;
234 }
235 printf("%i %i %i %i %i\r\n", ps->pin, ps->pwm, ps->target_pwm, ps->speed, ps->value);
236 }
237}
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...
uint8_t argctr
Definition: command.h:24
Definition: fets.c:8