pimoroni-pico/drivers/servo/multi_pwm.hpp

67 lines
2.0 KiB
C++
Raw Normal View History

2022-02-11 11:40:04 +00:00
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*
This code is significantly modified from the PIO apa102 example
found here: https://github.com/raspberrypi/pico-examples/tree/master/pio/ws2812
*/
#pragma once
#include <math.h>
#include <cstdint>
#include "multi_pwm.pio.h"
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/dma.h"
#include "hardware/irq.h"
#include "hardware/clocks.h"
#include "hardware/timer.h"
namespace servo {
2022-02-14 23:46:51 +00:00
struct TransitionData {
uint8_t servo;
2022-02-15 20:14:59 +00:00
uint32_t level;
2022-02-14 23:46:51 +00:00
bool state;
2022-02-15 20:14:59 +00:00
TransitionData() : servo(0), level(0), state(false) {};
TransitionData(uint8_t servo, uint32_t level, bool new_state) : servo(servo), level(level), state(new_state) {};
2022-02-14 23:46:51 +00:00
bool compare(const TransitionData& other) const {
2022-02-15 20:14:59 +00:00
return level <= other.level;
2022-02-14 23:46:51 +00:00
}
};
2022-02-11 11:40:04 +00:00
class MultiPWM {
public:
2022-02-15 20:14:59 +00:00
MultiPWM(PIO pio, uint sm, uint channel_mask);
2022-02-11 11:40:04 +00:00
~MultiPWM();
2022-02-15 20:14:59 +00:00
void set_wrap(uint32_t wrap, bool load = true);
void set_chan_level(uint8_t channel, uint32_t level, bool load = true);
void set_chan_offset(uint8_t channel, uint32_t offset, bool load = true);
void set_chan_polarity(uint8_t channel, bool polarity, bool load = true);
void set_clkdiv(float divider);
void set_clkdiv_int_frac(uint16_t integer, uint8_t fract);
//void set_phase_correct(bool phase_correct);
//void set_enabled(bool enabled);
void load_pwm();
2022-02-11 11:40:04 +00:00
private:
2022-02-15 20:14:59 +00:00
static bool bit_in_mask(uint bit, uint mask);
2022-02-14 23:46:51 +00:00
static void sorted_insert(TransitionData array[], uint &size, const TransitionData &data);
2022-02-15 20:14:59 +00:00
private:
2022-02-11 11:40:04 +00:00
PIO pio;
uint sm;
uint pio_program_offset;
2022-02-15 20:14:59 +00:00
uint channel_mask;
uint channel_levels[NUM_BANK0_GPIOS];
uint channel_offsets[NUM_BANK0_GPIOS];
uint channel_polarities;
uint wrap_level;
2022-02-11 11:40:04 +00:00
};
}