made classes for drivers
parent
14eb7970de
commit
7f0a54a8ee
@ -1,3 +1,4 @@
|
||||
build
|
||||
ChibiOS_*
|
||||
**/.*
|
||||
|
||||
|
@ -0,0 +1,42 @@
|
||||
#include "adc.hpp"
|
||||
|
||||
const GPTConfig ADCd::m_gpt_config = {
|
||||
.frequency = 1000000,
|
||||
.callback = NULL,
|
||||
.cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
|
||||
.dier = 0
|
||||
};
|
||||
|
||||
void ADCd::start()
|
||||
{
|
||||
initPins();
|
||||
gptStart(m_gptd, &m_gpt_config);
|
||||
|
||||
m_adc_config.difsel = 0;
|
||||
m_adc_config.adcdinst = this;
|
||||
|
||||
adcStart(m_adcd, &m_adc_config);
|
||||
adcSTM32EnableVREF(m_adcd);
|
||||
}
|
||||
|
||||
adcsample_t *ADCd::getSamples(adcsample_t *buffer, size_t count)
|
||||
{
|
||||
m_is_adc_finished = false;
|
||||
adcStartConversion(m_adcd, &m_adc_group_config, buffer, count);
|
||||
gptStartContinuous(m_gptd, 100); // 10kHz
|
||||
while (!m_is_adc_finished);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void ADCd::initPins()
|
||||
{
|
||||
palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG);
|
||||
}
|
||||
|
||||
void ADCd::adcEndCallback(ADCDriver *adcd)
|
||||
{
|
||||
auto *_this = reinterpret_cast<const ADCdConfig *>(adcd->config)->adcdinst;
|
||||
gptStopTimer(_this->m_gptd);
|
||||
_this->m_is_adc_finished = true;
|
||||
}
|
||||
|
@ -0,0 +1,57 @@
|
||||
#ifndef STMDSP_ADC_HPP_
|
||||
#define STMDSP_ADC_HPP_
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
class ADCd;
|
||||
|
||||
struct ADCdConfig : public ADCConfig
|
||||
{
|
||||
ADCd *adcdinst;
|
||||
};
|
||||
|
||||
class ADCd
|
||||
{
|
||||
public:
|
||||
constexpr explicit ADCd(ADCDriver& adcd, GPTDriver& gptd) :
|
||||
m_adcd(&adcd), m_gptd(&gptd), m_adc_config{},
|
||||
m_adc_group_config(ADC_GROUP_CONFIG),
|
||||
m_is_adc_finished(false) {}
|
||||
|
||||
void start();
|
||||
adcsample_t *getSamples(adcsample_t *buffer, size_t count);
|
||||
|
||||
private:
|
||||
static const GPTConfig m_gpt_config;
|
||||
|
||||
ADCDriver *m_adcd;
|
||||
GPTDriver *m_gptd;
|
||||
ADCdConfig m_adc_config;
|
||||
ADCConversionGroup m_adc_group_config;
|
||||
|
||||
bool m_is_adc_finished;
|
||||
|
||||
void initPins();
|
||||
static void adcEndCallback(ADCDriver *adcd);
|
||||
|
||||
constexpr static const ADCConversionGroup ADC_GROUP_CONFIG = {
|
||||
.circular = false,
|
||||
.num_channels = 1,
|
||||
.end_cb = ADCd::adcEndCallback,
|
||||
.error_cb = nullptr,
|
||||
.cfgr = ADC_CFGR_EXTEN_RISING |
|
||||
ADC_CFGR_EXTSEL_SRC(12), /* TIM4_TRGO */
|
||||
.cfgr2 = 0,
|
||||
.tr1 = ADC_TR(0, 4095),
|
||||
.smpr = {
|
||||
ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_247P5), 0
|
||||
},
|
||||
.sqr = {
|
||||
ADC_SQR1_SQ1_N(ADC_CHANNEL_IN5),
|
||||
0, 0, 0
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif // STMDSP_ADC_HPP_
|
||||
|
@ -0,0 +1,27 @@
|
||||
#include "dac.hpp"
|
||||
|
||||
//static const DACConversionGroup dacGroupConfig = {
|
||||
// .num_channels = 1,
|
||||
// .end_cb = NULL,
|
||||
// .error_cb = NULL,
|
||||
// .trigger = DAC_TRG(0)
|
||||
//};
|
||||
|
||||
void DACd::init()
|
||||
{
|
||||
initPins();
|
||||
dacStart(m_driver, &m_config);
|
||||
}
|
||||
|
||||
void DACd::write(unsigned int channel, uint16_t value)
|
||||
{
|
||||
if (channel < 2)
|
||||
dacPutChannelX(m_driver, channel, value);
|
||||
}
|
||||
|
||||
void DACd::initPins()
|
||||
{
|
||||
palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG); // DAC out1, out2
|
||||
palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
#ifndef STMDSP_DAC_HPP_
|
||||
#define STMDSP_DAC_HPP_
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
class DACd
|
||||
{
|
||||
public:
|
||||
constexpr explicit DACd(DACDriver& driver, const DACConfig& config) :
|
||||
m_driver(&driver), m_config(config) {}
|
||||
|
||||
void init();
|
||||
|
||||
void write(unsigned int channel, uint16_t value);
|
||||
|
||||
private:
|
||||
DACDriver *m_driver;
|
||||
DACConfig m_config;
|
||||
|
||||
void initPins();
|
||||
};
|
||||
|
||||
#endif // STMDSP_DAC_HPP_
|
||||
|
@ -0,0 +1,41 @@
|
||||
#include "usbserial.hpp"
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
void USBSeriald::start()
|
||||
{
|
||||
initPins();
|
||||
|
||||
sduObjectInit(m_driver);
|
||||
sduStart(m_driver, &serusbcfg);
|
||||
|
||||
// Reconnect bus so device can re-enumerate on reset
|
||||
usbDisconnectBus(serusbcfg.usbp);
|
||||
chThdSleepMilliseconds(1500);
|
||||
usbStart(serusbcfg.usbp, &usbcfg);
|
||||
usbConnectBus(serusbcfg.usbp);
|
||||
}
|
||||
|
||||
bool USBSeriald::active() const
|
||||
{
|
||||
return m_driver->config->usbp->state == USB_ACTIVE;
|
||||
}
|
||||
|
||||
std::size_t USBSeriald::read(void *buffer, std::size_t count)
|
||||
{
|
||||
auto *bss = reinterpret_cast<BaseSequentialStream *>(m_driver);
|
||||
return streamRead(bss, static_cast<uint8_t *>(buffer), count);
|
||||
}
|
||||
|
||||
std::size_t USBSeriald::write(const void *buffer, std::size_t count)
|
||||
{
|
||||
auto *bss = reinterpret_cast<BaseSequentialStream *>(m_driver);
|
||||
return streamWrite(bss, static_cast<const uint8_t *>(buffer), count);
|
||||
}
|
||||
|
||||
void USBSeriald::initPins()
|
||||
{
|
||||
palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10));
|
||||
palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10));
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
#ifndef STMDSP_USBSERIAL_HPP_
|
||||
#define STMDSP_USBSERIAL_HPP_
|
||||
|
||||
#include "usbcfg.h"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
class USBSeriald
|
||||
{
|
||||
public:
|
||||
constexpr explicit USBSeriald(SerialUSBDriver& driver) :
|
||||
m_driver(&driver) {}
|
||||
|
||||
void start();
|
||||
|
||||
bool active() const;
|
||||
|
||||
std::size_t read(void *buffer, std::size_t count = 1);
|
||||
std::size_t write(const void *buffer, std::size_t count = 1);
|
||||
|
||||
private:
|
||||
SerialUSBDriver *m_driver;
|
||||
|
||||
void initPins();
|
||||
};
|
||||
|
||||
#endif // STMDSP_USBSERIAL_HPP_
|
||||
|
Loading…
Reference in New Issue