2016-05-25 17:37:40 +02:00
|
|
|
// Commands for controlling GPIO pins
|
|
|
|
//
|
|
|
|
// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2017-03-09 19:49:03 +01:00
|
|
|
#include "basecmd.h" // oid_alloc
|
2016-06-09 01:44:34 +02:00
|
|
|
#include "board/gpio.h" // struct gpio_out
|
2016-06-09 02:51:00 +02:00
|
|
|
#include "board/irq.h" // irq_disable
|
2017-03-25 04:01:08 +01:00
|
|
|
#include "board/misc.h" // timer_is_before
|
2016-05-25 17:37:40 +02:00
|
|
|
#include "command.h" // DECL_COMMAND
|
2017-03-11 04:12:05 +01:00
|
|
|
#include "sched.h" // sched_add_timer
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Digital out pins
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
struct digital_out_s {
|
|
|
|
struct timer timer;
|
|
|
|
struct gpio_out pin;
|
|
|
|
uint32_t max_duration;
|
|
|
|
uint8_t value, default_value;
|
|
|
|
};
|
|
|
|
|
2016-06-09 03:33:50 +02:00
|
|
|
static uint_fast8_t
|
2016-05-25 17:37:40 +02:00
|
|
|
digital_end_event(struct timer *timer)
|
|
|
|
{
|
|
|
|
shutdown("Missed scheduling of next pin event");
|
|
|
|
}
|
|
|
|
|
2016-06-09 03:33:50 +02:00
|
|
|
static uint_fast8_t
|
2016-05-25 17:37:40 +02:00
|
|
|
digital_out_event(struct timer *timer)
|
|
|
|
{
|
|
|
|
struct digital_out_s *d = container_of(timer, struct digital_out_s, timer);
|
|
|
|
gpio_out_write(d->pin, d->value);
|
|
|
|
if (d->value == d->default_value || !d->max_duration)
|
|
|
|
return SF_DONE;
|
|
|
|
d->timer.waketime += d->max_duration;
|
|
|
|
d->timer.func = digital_end_event;
|
|
|
|
return SF_RESCHEDULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
command_config_digital_out(uint32_t *args)
|
|
|
|
{
|
2017-10-12 04:50:29 +02:00
|
|
|
struct gpio_out pin = gpio_out_setup(args[1], args[2]);
|
2017-03-09 19:49:03 +01:00
|
|
|
struct digital_out_s *d = oid_alloc(args[0], command_config_digital_out
|
2016-05-25 17:37:40 +02:00
|
|
|
, sizeof(*d));
|
2017-10-12 04:50:29 +02:00
|
|
|
d->pin = pin;
|
|
|
|
d->default_value = args[3];
|
|
|
|
d->max_duration = args[4];
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
|
|
|
DECL_COMMAND(command_config_digital_out,
|
2017-10-12 04:50:29 +02:00
|
|
|
"config_digital_out oid=%c pin=%u value=%c default_value=%c"
|
2016-05-25 17:37:40 +02:00
|
|
|
" max_duration=%u");
|
|
|
|
|
|
|
|
void
|
|
|
|
command_schedule_digital_out(uint32_t *args)
|
|
|
|
{
|
2017-03-09 19:49:03 +01:00
|
|
|
struct digital_out_s *d = oid_lookup(args[0], command_config_digital_out);
|
2016-05-25 17:37:40 +02:00
|
|
|
sched_del_timer(&d->timer);
|
|
|
|
d->timer.func = digital_out_event;
|
|
|
|
d->timer.waketime = args[1];
|
|
|
|
d->value = args[2];
|
2017-03-11 04:12:05 +01:00
|
|
|
sched_add_timer(&d->timer);
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
|
|
|
DECL_COMMAND(command_schedule_digital_out,
|
|
|
|
"schedule_digital_out oid=%c clock=%u value=%c");
|
|
|
|
|
2018-06-27 17:54:45 +02:00
|
|
|
void
|
|
|
|
command_update_digital_out(uint32_t *args)
|
|
|
|
{
|
|
|
|
struct digital_out_s *d = oid_lookup(args[0], command_config_digital_out);
|
|
|
|
sched_del_timer(&d->timer);
|
|
|
|
uint8_t value = args[1];
|
|
|
|
gpio_out_write(d->pin, value);
|
|
|
|
if (value != d->default_value && d->max_duration) {
|
|
|
|
d->timer.waketime = timer_read_time() + d->max_duration;
|
|
|
|
d->timer.func = digital_end_event;
|
|
|
|
sched_add_timer(&d->timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DECL_COMMAND(command_update_digital_out, "update_digital_out oid=%c value=%c");
|
|
|
|
|
2017-05-26 15:14:26 +02:00
|
|
|
void
|
2016-05-25 17:37:40 +02:00
|
|
|
digital_out_shutdown(void)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
struct digital_out_s *d;
|
|
|
|
foreach_oid(i, d, command_config_digital_out) {
|
|
|
|
gpio_out_write(d->pin, d->default_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DECL_SHUTDOWN(digital_out_shutdown);
|
|
|
|
|
|
|
|
void
|
|
|
|
command_set_digital_out(uint32_t *args)
|
|
|
|
{
|
|
|
|
gpio_out_setup(args[0], args[1]);
|
|
|
|
}
|
|
|
|
DECL_COMMAND(command_set_digital_out, "set_digital_out pin=%u value=%c");
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Soft PWM output pins
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
struct soft_pwm_s {
|
|
|
|
struct timer timer;
|
|
|
|
uint32_t on_duration, off_duration, end_time;
|
|
|
|
uint32_t next_on_duration, next_off_duration;
|
2020-09-07 12:31:35 +02:00
|
|
|
uint32_t max_duration;
|
2016-05-25 17:37:40 +02:00
|
|
|
struct gpio_out pin;
|
|
|
|
uint8_t default_value, flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SPF_ON=1<<0, SPF_TOGGLING=1<<1, SPF_CHECK_END=1<<2, SPF_HAVE_NEXT=1<<3,
|
|
|
|
SPF_NEXT_ON=1<<4, SPF_NEXT_TOGGLING=1<<5, SPF_NEXT_CHECK_END=1<<6,
|
|
|
|
};
|
|
|
|
|
2016-06-09 03:33:50 +02:00
|
|
|
static uint_fast8_t soft_pwm_load_event(struct timer *timer);
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
// Normal pulse change event
|
2016-06-09 03:33:50 +02:00
|
|
|
static uint_fast8_t
|
2016-05-25 17:37:40 +02:00
|
|
|
soft_pwm_toggle_event(struct timer *timer)
|
|
|
|
{
|
|
|
|
struct soft_pwm_s *s = container_of(timer, struct soft_pwm_s, timer);
|
2018-05-15 23:04:30 +02:00
|
|
|
gpio_out_toggle_noirq(s->pin);
|
2016-05-25 17:37:40 +02:00
|
|
|
s->flags ^= SPF_ON;
|
|
|
|
uint32_t waketime = s->timer.waketime;
|
|
|
|
if (s->flags & SPF_ON)
|
|
|
|
waketime += s->on_duration;
|
|
|
|
else
|
|
|
|
waketime += s->off_duration;
|
2017-03-25 04:01:08 +01:00
|
|
|
if (s->flags & SPF_CHECK_END && !timer_is_before(waketime, s->end_time)) {
|
2016-05-25 17:37:40 +02:00
|
|
|
// End of normal pulsing - next event loads new pwm settings
|
|
|
|
s->timer.func = soft_pwm_load_event;
|
|
|
|
waketime = s->end_time;
|
|
|
|
}
|
|
|
|
s->timer.waketime = waketime;
|
|
|
|
return SF_RESCHEDULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load next pwm settings
|
2016-06-09 03:33:50 +02:00
|
|
|
static uint_fast8_t
|
2016-05-25 17:37:40 +02:00
|
|
|
soft_pwm_load_event(struct timer *timer)
|
|
|
|
{
|
|
|
|
struct soft_pwm_s *s = container_of(timer, struct soft_pwm_s, timer);
|
|
|
|
if (!(s->flags & SPF_HAVE_NEXT))
|
|
|
|
shutdown("Missed scheduling of next pwm event");
|
|
|
|
uint8_t flags = s->flags >> 4;
|
|
|
|
s->flags = flags;
|
|
|
|
gpio_out_write(s->pin, flags & SPF_ON);
|
|
|
|
if (!(flags & SPF_TOGGLING)) {
|
2017-12-16 05:43:24 +01:00
|
|
|
// Pin is in an always on (value=256) or always off (value=0) state
|
2016-05-25 17:37:40 +02:00
|
|
|
if (!(flags & SPF_CHECK_END))
|
|
|
|
return SF_DONE;
|
|
|
|
s->timer.waketime = s->end_time = s->end_time + s->max_duration;
|
|
|
|
return SF_RESCHEDULE;
|
|
|
|
}
|
|
|
|
// Schedule normal pin toggle timer events
|
|
|
|
s->timer.func = soft_pwm_toggle_event;
|
|
|
|
s->off_duration = s->next_off_duration;
|
|
|
|
s->on_duration = s->next_on_duration;
|
|
|
|
s->timer.waketime = s->end_time + s->on_duration;
|
|
|
|
s->end_time += s->max_duration;
|
|
|
|
return SF_RESCHEDULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
command_config_soft_pwm_out(uint32_t *args)
|
|
|
|
{
|
2020-09-07 12:31:35 +02:00
|
|
|
struct gpio_out pin = gpio_out_setup(args[1], !!args[2]);
|
2017-03-09 19:49:03 +01:00
|
|
|
struct soft_pwm_s *s = oid_alloc(args[0], command_config_soft_pwm_out
|
2016-05-25 17:37:40 +02:00
|
|
|
, sizeof(*s));
|
2017-10-12 04:50:29 +02:00
|
|
|
s->pin = pin;
|
2020-09-07 12:31:35 +02:00
|
|
|
s->default_value = !!args[3];
|
|
|
|
s->max_duration = args[4];
|
2016-05-25 17:37:40 +02:00
|
|
|
s->flags = s->default_value ? SPF_ON : 0;
|
|
|
|
}
|
|
|
|
DECL_COMMAND(command_config_soft_pwm_out,
|
2020-09-07 12:31:35 +02:00
|
|
|
"config_soft_pwm_out oid=%c pin=%u value=%c"
|
2017-10-12 04:50:29 +02:00
|
|
|
" default_value=%c max_duration=%u");
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
command_schedule_soft_pwm_out(uint32_t *args)
|
|
|
|
{
|
2017-03-09 19:49:03 +01:00
|
|
|
struct soft_pwm_s *s = oid_lookup(args[0], command_config_soft_pwm_out);
|
2020-09-07 12:31:35 +02:00
|
|
|
uint32_t time = args[1], next_on_duration = args[2],
|
|
|
|
next_off_duration = args[3];
|
2016-05-25 17:37:40 +02:00
|
|
|
uint8_t next_flags = SPF_CHECK_END | SPF_HAVE_NEXT;
|
2020-09-07 12:31:35 +02:00
|
|
|
if (next_on_duration == 0 || next_off_duration == 0) {
|
2019-05-20 23:41:42 +02:00
|
|
|
next_flags |= next_on_duration ? SPF_NEXT_ON : 0;
|
|
|
|
if (!!next_on_duration != s->default_value && s->max_duration)
|
2016-05-25 17:37:40 +02:00
|
|
|
next_flags |= SPF_NEXT_CHECK_END;
|
2019-05-20 23:41:42 +02:00
|
|
|
next_on_duration = next_off_duration = 0;
|
2016-05-25 17:37:40 +02:00
|
|
|
} else {
|
|
|
|
next_flags |= SPF_NEXT_ON | SPF_NEXT_TOGGLING;
|
|
|
|
if (s->max_duration)
|
|
|
|
next_flags |= SPF_NEXT_CHECK_END;
|
|
|
|
}
|
2016-06-09 02:51:00 +02:00
|
|
|
irq_disable();
|
2017-03-25 04:01:08 +01:00
|
|
|
if (s->flags & SPF_CHECK_END && timer_is_before(s->end_time, time))
|
2016-05-25 17:37:40 +02:00
|
|
|
shutdown("next soft pwm extends existing pwm");
|
|
|
|
s->end_time = time;
|
|
|
|
s->next_on_duration = next_on_duration;
|
|
|
|
s->next_off_duration = next_off_duration;
|
2017-12-16 05:43:24 +01:00
|
|
|
s->flags = (s->flags & 0xf) | next_flags;
|
2017-03-25 04:01:08 +01:00
|
|
|
if (s->flags & SPF_TOGGLING && timer_is_before(s->timer.waketime, time)) {
|
2016-05-25 17:37:40 +02:00
|
|
|
// soft_pwm_toggle_event() will schedule a load event when ready
|
|
|
|
} else {
|
|
|
|
// Schedule the loading of the pwm parameters at the requested time
|
|
|
|
sched_del_timer(&s->timer);
|
|
|
|
s->timer.waketime = time;
|
|
|
|
s->timer.func = soft_pwm_load_event;
|
2017-03-11 04:12:05 +01:00
|
|
|
sched_add_timer(&s->timer);
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
2016-06-09 02:51:00 +02:00
|
|
|
irq_enable();
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
|
|
|
DECL_COMMAND(command_schedule_soft_pwm_out,
|
2020-09-07 12:31:35 +02:00
|
|
|
"schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u off_ticks=%u");
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2017-05-26 15:14:26 +02:00
|
|
|
void
|
2016-05-25 17:37:40 +02:00
|
|
|
soft_pwm_shutdown(void)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
struct soft_pwm_s *s;
|
|
|
|
foreach_oid(i, s, command_config_soft_pwm_out) {
|
|
|
|
gpio_out_write(s->pin, s->default_value);
|
|
|
|
s->flags = s->default_value ? SPF_ON : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DECL_SHUTDOWN(soft_pwm_shutdown);
|