mirror of
https://github.com/jankae/LibreVNA.git
synced 2026-04-04 14:07:30 +00:00
embedded code copied from jankae/VNA and adjusted for STM32G4
This commit is contained in:
parent
7af204b349
commit
30d4ebe37b
215 changed files with 186208 additions and 0 deletions
138
Software/VNA_embedded/Application/Drivers/Exti.cpp
Normal file
138
Software/VNA_embedded/Application/Drivers/Exti.cpp
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
#include "Exti.hpp"
|
||||
|
||||
using Entry = struct {
|
||||
GPIO_TypeDef *gpio;
|
||||
Exti::Callback cb;
|
||||
void *ptr;
|
||||
};
|
||||
|
||||
static constexpr uint8_t MaxEntries = 16;
|
||||
|
||||
static Entry entries[MaxEntries];
|
||||
|
||||
void Exti::Init() {
|
||||
HAL_NVIC_SetPriority(EXTI0_IRQn, 1, 0);
|
||||
HAL_NVIC_SetPriority(EXTI1_IRQn, 1, 0);
|
||||
HAL_NVIC_SetPriority(EXTI2_IRQn, 1, 0);
|
||||
HAL_NVIC_SetPriority(EXTI3_IRQn, 1, 0);
|
||||
HAL_NVIC_SetPriority(EXTI4_IRQn, 1, 0);
|
||||
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 1, 0);
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 1, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI2_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
for (uint8_t i = 0; i < MaxEntries; i++) {
|
||||
entries[i].gpio = NULL;
|
||||
entries[i].cb = NULL;
|
||||
entries[i].ptr = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool Exti::SetCallback(GPIO_TypeDef *gpio, uint16_t pin, EdgeType edge,
|
||||
Pull pull, Callback cb, void *ptr) {
|
||||
uint8_t index = 31 - __builtin_clz(pin);
|
||||
|
||||
if (entries[index].gpio && entries[index].gpio != gpio) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GPIO_InitTypeDef g;
|
||||
g.Pin = pin;
|
||||
switch (edge) {
|
||||
case EdgeType::Falling:
|
||||
g.Mode = GPIO_MODE_IT_FALLING;
|
||||
break;
|
||||
case EdgeType::Rising:
|
||||
g.Mode = GPIO_MODE_IT_RISING;
|
||||
break;
|
||||
case EdgeType::Both:
|
||||
g.Mode = GPIO_MODE_IT_RISING_FALLING;
|
||||
break;
|
||||
}
|
||||
switch (pull) {
|
||||
case Pull::None:
|
||||
g.Pull = GPIO_NOPULL;
|
||||
break;
|
||||
case Pull::Up:
|
||||
g.Pull = GPIO_PULLUP;
|
||||
break;
|
||||
case Pull::Down:
|
||||
g.Pull = GPIO_PULLDOWN;
|
||||
break;
|
||||
}
|
||||
HAL_GPIO_Init(gpio, &g);
|
||||
|
||||
entries[index].gpio = gpio;
|
||||
entries[index].cb = cb;
|
||||
entries[index].ptr = ptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Exti::ClearCallback(GPIO_TypeDef *gpio, uint16_t pin) {
|
||||
uint8_t index = 31 - __builtin_clz(pin);
|
||||
if (entries[index].gpio) {
|
||||
if (gpio != entries[index].gpio) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
entries[index].gpio = NULL;
|
||||
entries[index].cb = NULL;
|
||||
entries[index].ptr = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void ExtiHandler(uint16_t source) {
|
||||
const uint32_t mask = 1 << source;
|
||||
if (__HAL_GPIO_EXTI_GET_IT(mask) != RESET) {
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(mask);
|
||||
if (entries[source].cb) {
|
||||
entries[source].cb(entries[source].ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void EXTI0_IRQHandler(void) {
|
||||
ExtiHandler(0);
|
||||
}
|
||||
|
||||
void EXTI1_IRQHandler(void) {
|
||||
ExtiHandler(1);
|
||||
}
|
||||
|
||||
void EXTI2_IRQHandler(void) {
|
||||
ExtiHandler(2);
|
||||
}
|
||||
|
||||
void EXTI3_IRQHandler(void) {
|
||||
ExtiHandler(3);
|
||||
}
|
||||
|
||||
void EXTI4_IRQHandler(void) {
|
||||
ExtiHandler(4);
|
||||
}
|
||||
|
||||
void EXTI9_5_IRQHandler(void) {
|
||||
ExtiHandler(5);
|
||||
ExtiHandler(6);
|
||||
ExtiHandler(7);
|
||||
ExtiHandler(8);
|
||||
ExtiHandler(9);
|
||||
}
|
||||
|
||||
void EXTI15_10_IRQHandler(void) {
|
||||
ExtiHandler(10);
|
||||
ExtiHandler(11);
|
||||
ExtiHandler(12);
|
||||
ExtiHandler(13);
|
||||
ExtiHandler(14);
|
||||
ExtiHandler(15);
|
||||
}
|
||||
}
|
||||
|
||||
27
Software/VNA_embedded/Application/Drivers/Exti.hpp
Normal file
27
Software/VNA_embedded/Application/Drivers/Exti.hpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "stm.hpp"
|
||||
|
||||
namespace Exti {
|
||||
|
||||
enum class EdgeType : uint8_t {
|
||||
Falling,
|
||||
Rising,
|
||||
Both,
|
||||
};
|
||||
|
||||
enum class Pull : uint8_t {
|
||||
None,
|
||||
Up,
|
||||
Down,
|
||||
};
|
||||
|
||||
using Callback = void(*)(void*);
|
||||
|
||||
void Init();
|
||||
bool SetCallback(GPIO_TypeDef *gpio, uint16_t pin, EdgeType edge, Pull pull, Callback cb, void *ptr = nullptr);
|
||||
bool ClearCallback(GPIO_TypeDef *gpio, uint16_t pin);
|
||||
|
||||
}
|
||||
293
Software/VNA_embedded/Application/Drivers/FPGA.cpp
Normal file
293
Software/VNA_embedded/Application/Drivers/FPGA.cpp
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
#include "FPGA.hpp"
|
||||
#include "delay.hpp"
|
||||
#include "stm.hpp"
|
||||
#include "main.h"
|
||||
|
||||
#define LOG_LEVEL LOG_LEVEL_DEBUG
|
||||
#define LOG_MODULE "FPGA"
|
||||
#include "Log.h"
|
||||
|
||||
#define FPGA_SPI hspi1
|
||||
extern SPI_HandleTypeDef FPGA_SPI;
|
||||
|
||||
static inline void Low(GPIO_TypeDef *gpio, uint16_t pin) {
|
||||
gpio->BSRR = pin << 16;
|
||||
}
|
||||
static inline void High(GPIO_TypeDef *gpio, uint16_t pin) {
|
||||
gpio->BSRR = pin;
|
||||
}
|
||||
|
||||
static FPGA::HaltedCallback halted_cb;
|
||||
static uint16_t SysCtrlReg = 0x0000;
|
||||
static uint16_t ISRMaskReg = 0x0000;
|
||||
|
||||
void WriteRegister(FPGA::Reg reg, uint16_t value) {
|
||||
uint16_t cmd[2] = {(uint16_t) (0x8000 | (uint16_t) reg), value};
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_Transmit(&FPGA_SPI, (uint8_t*) cmd, 2, 100);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
}
|
||||
|
||||
bool FPGA::Init(HaltedCallback cb) {
|
||||
halted_cb = cb;
|
||||
// Reset FPGA
|
||||
High(FPGA_RESET_GPIO_Port, FPGA_RESET_Pin);
|
||||
SetMode(Mode::FPGA);
|
||||
Delay::us(1);
|
||||
Low(FPGA_RESET_GPIO_Port, FPGA_RESET_Pin);
|
||||
Delay::ms(10);
|
||||
|
||||
// Check if FPGA response is as expected
|
||||
uint16_t cmd[2] = {0x4000, 0x0000};
|
||||
uint16_t recv[2];
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_TransmitReceive(&FPGA_SPI, (uint8_t*) cmd, (uint8_t*) recv, 2, 100);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
|
||||
if(recv[1] != 0xF0A5) {
|
||||
LOG_ERR("Initialization failed, got 0x%04x instead of 0xF0A5", recv[1]);
|
||||
}
|
||||
|
||||
LOG_DEBUG("Initialized, status register: 0x%04x", recv[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
void FPGA::SetNumberOfPoints(uint16_t npoints) {
|
||||
// register has to be set to number of points - 1
|
||||
npoints--;
|
||||
WriteRegister(Reg::SweepPoints, npoints);
|
||||
}
|
||||
|
||||
void FPGA::SetSamplesPerPoint(uint32_t nsamples) {
|
||||
// register has to be set to number of nsamples - 1
|
||||
nsamples--;
|
||||
// constrain to maximum value
|
||||
nsamples &= 0x1FFFF;
|
||||
// highest bit is located at the system control register
|
||||
SysCtrlReg &= ~0x0001;
|
||||
SysCtrlReg |= nsamples >> 16;
|
||||
WriteRegister(Reg::SystemControl, SysCtrlReg);
|
||||
WriteRegister(Reg::SamplesPerPoint, nsamples & 0xFFFF);
|
||||
}
|
||||
|
||||
void FPGA::SetSettlingTime(uint16_t us) {
|
||||
if (us > 639) {
|
||||
us = 639;
|
||||
}
|
||||
uint16_t regval = (uint32_t) us * 1024 / 10;
|
||||
WriteRegister(Reg::SettlingTime, regval);
|
||||
}
|
||||
|
||||
void FPGA::Enable(Periphery p, bool enable) {
|
||||
if (enable) {
|
||||
SysCtrlReg |= (uint16_t) p;
|
||||
WriteRegister(Reg::SystemControl, SysCtrlReg);
|
||||
} else {
|
||||
Disable(p);
|
||||
}
|
||||
}
|
||||
|
||||
void FPGA::Disable(Periphery p) {
|
||||
SysCtrlReg &= ~(uint16_t) p;
|
||||
WriteRegister(Reg::SystemControl, SysCtrlReg);
|
||||
}
|
||||
|
||||
void FPGA::EnableInterrupt(Interrupt i) {
|
||||
ISRMaskReg |= (uint16_t) i;
|
||||
WriteRegister(Reg::InterruptMask, ISRMaskReg);
|
||||
}
|
||||
|
||||
void FPGA::DisableInterrupt(Interrupt i) {
|
||||
ISRMaskReg &= ~(uint16_t) i;
|
||||
WriteRegister(Reg::InterruptMask, ISRMaskReg);
|
||||
}
|
||||
|
||||
void FPGA::WriteMAX2871Default(uint32_t *DefaultRegs) {
|
||||
WriteRegister(Reg::MAX2871Def0LSB, DefaultRegs[0] & 0xFFFF);
|
||||
WriteRegister(Reg::MAX2871Def0MSB, DefaultRegs[0] >> 16);
|
||||
WriteRegister(Reg::MAX2871Def1LSB, DefaultRegs[1] & 0xFFFF);
|
||||
WriteRegister(Reg::MAX2871Def1MSB, DefaultRegs[1] >> 16);
|
||||
WriteRegister(Reg::MAX2871Def3LSB, DefaultRegs[3] & 0xFFFF);
|
||||
WriteRegister(Reg::MAX2871Def3MSB, DefaultRegs[3] >> 16);
|
||||
WriteRegister(Reg::MAX2871Def4LSB, DefaultRegs[4] & 0xFFFF);
|
||||
WriteRegister(Reg::MAX2871Def4MSB, DefaultRegs[4] >> 16);
|
||||
}
|
||||
|
||||
void FPGA::WriteSweepConfig(uint16_t pointnum, bool lowband, uint32_t *SourceRegs, uint32_t *LORegs,
|
||||
uint8_t attenuation, uint64_t frequency, bool halt, LowpassFilter filter) {
|
||||
uint16_t send[8];
|
||||
// select which point this sweep config is for
|
||||
send[0] = pointnum & 0x1FFF;
|
||||
// assemble sweep config from required fields of PLL registers
|
||||
send[1] = (LORegs[4] & 0x00700000) >> 14 | (LORegs[3] & 0xFC000000) >> 26;
|
||||
if (halt) {
|
||||
send[1] |= 0x8000;
|
||||
}
|
||||
if (lowband) {
|
||||
send[1] |= 0x4000;
|
||||
}
|
||||
switch(filter) {
|
||||
case LowpassFilter::Auto:
|
||||
// Select source LP filter
|
||||
if (frequency >= 3500000000) {
|
||||
send[1] |= 0x0600;
|
||||
} else if (frequency >= 1800000000) {
|
||||
send[1] |= 0x0400;
|
||||
} else if (frequency >= 900000000) {
|
||||
send[1] |= 0x0200;
|
||||
}
|
||||
break;
|
||||
case LowpassFilter::M947: break;
|
||||
case LowpassFilter::M1880: send[1] |= 0x0200; break;
|
||||
case LowpassFilter::M3500: send[1] |= 0x0400; break;
|
||||
case LowpassFilter::None: send[1] |= 0x0600; break;
|
||||
}
|
||||
send[2] = (LORegs[1] & 0x00007FF8) << 1 | (LORegs[0] & 0x00007800) >> 11;
|
||||
send[3] = (LORegs[0] & 0x000007F8) << 5 | (LORegs[0] & 0x7F800000) >> 23;
|
||||
send[4] = (LORegs[0] & 0x007F8000) >> 7 | (attenuation & 0x7F) << 1 | (SourceRegs[4] & 0x00400000) >> 22;
|
||||
send[5] = (SourceRegs[4] & 0x00300000) >> 6 | (SourceRegs[3] & 0xFC000000) >> 18 | (SourceRegs[1] & 0x00007F80) >> 7;
|
||||
send[6] = (SourceRegs[1] & 0x00000078) << 9 | (SourceRegs[0] & 0x00007FF8) >> 3;
|
||||
send[7] = (SourceRegs[0] & 0x7FFF8000) >> 15;
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_Transmit(&FPGA_SPI, (uint8_t*) send, 8, 100);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
}
|
||||
|
||||
static inline int64_t sign_extend_64(int64_t x, uint16_t bits) {
|
||||
int64_t m = 1ULL << (bits - 1);
|
||||
return (x ^ m) - m;
|
||||
}
|
||||
|
||||
static FPGA::ReadCallback callback;
|
||||
static uint16_t raw[18];
|
||||
static bool halted;
|
||||
|
||||
bool FPGA::InitiateSampleRead(ReadCallback cb) {
|
||||
callback = cb;
|
||||
uint16_t cmd = 0xC000;
|
||||
uint16_t status;
|
||||
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_TransmitReceive(&FPGA_SPI, (uint8_t*) &cmd, (uint8_t*) &status, 1,
|
||||
100);
|
||||
|
||||
if (status & 0x0010) {
|
||||
halted = true;
|
||||
} else {
|
||||
halted = false;
|
||||
}
|
||||
|
||||
if (!(status & 0x0004)) {
|
||||
// no new data available yet
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
|
||||
if (halted) {
|
||||
if (halted_cb) {
|
||||
halted_cb();
|
||||
}
|
||||
} else {
|
||||
LOG_WARN("ISR without new data, status: 0x%04x", status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Start data read
|
||||
HAL_SPI_Receive_DMA(&FPGA_SPI, (uint8_t*) raw, 18);
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
|
||||
FPGA::SamplingResult result;
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
// Assemble data from words
|
||||
result.P1I = sign_extend_64(
|
||||
(uint64_t) raw[17] << 32 | (uint32_t) raw[16] << 16 | raw[15], 48);
|
||||
result.P1Q = sign_extend_64(
|
||||
(uint64_t) raw[14] << 32 | (uint32_t) raw[13] << 16 | raw[12], 48);
|
||||
result.P2I = sign_extend_64(
|
||||
(uint64_t) raw[11] << 32 | (uint32_t) raw[10] << 16 | raw[9], 48);
|
||||
result.P2Q = sign_extend_64(
|
||||
(uint64_t) raw[8] << 32 | (uint32_t) raw[7] << 16 | raw[6], 48);
|
||||
result.RefI = sign_extend_64(
|
||||
(uint64_t) raw[5] << 32 | (uint32_t) raw[4] << 16 | raw[3], 48);
|
||||
result.RefQ = sign_extend_64(
|
||||
(uint64_t) raw[2] << 32 | (uint32_t) raw[1] << 16 | raw[0], 48);
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
if (halted && halted_cb) {
|
||||
halted_cb();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FPGA::StartSweep() {
|
||||
Low(FPGA_AUX3_GPIO_Port, FPGA_AUX3_Pin);
|
||||
Delay::us(1);
|
||||
High(FPGA_AUX3_GPIO_Port, FPGA_AUX3_Pin);
|
||||
}
|
||||
|
||||
void FPGA::AbortSweep() {
|
||||
Low(FPGA_AUX3_GPIO_Port, FPGA_AUX3_Pin);
|
||||
}
|
||||
|
||||
void FPGA::SetMode(Mode mode) {
|
||||
switch(mode) {
|
||||
case Mode::FPGA:
|
||||
// Both AUX1/2 low
|
||||
Low(FPGA_AUX1_GPIO_Port, FPGA_AUX1_Pin);
|
||||
Low(FPGA_AUX2_GPIO_Port, FPGA_AUX2_Pin);
|
||||
Delay::us(1);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
break;
|
||||
case Mode::SourcePLL:
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
Low(FPGA_AUX2_GPIO_Port, FPGA_AUX2_Pin);
|
||||
Delay::us(1);
|
||||
High(FPGA_AUX1_GPIO_Port, FPGA_AUX1_Pin);
|
||||
break;
|
||||
case Mode::LOPLL:
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
Low(FPGA_AUX1_GPIO_Port, FPGA_AUX1_Pin);
|
||||
Delay::us(1);
|
||||
High(FPGA_AUX2_GPIO_Port, FPGA_AUX2_Pin);
|
||||
break;
|
||||
}
|
||||
Delay::us(1);
|
||||
}
|
||||
|
||||
uint16_t FPGA::GetStatus() {
|
||||
uint16_t cmd = 0x4000;
|
||||
uint16_t status;
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_TransmitReceive(&FPGA_SPI, (uint8_t*) &cmd, (uint8_t*) &status, 1,
|
||||
100);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
return status;
|
||||
}
|
||||
|
||||
FPGA::ADCLimits FPGA::GetADCLimits() {
|
||||
uint16_t cmd = 0xE000;
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_Transmit(&FPGA_SPI, (uint8_t*) &cmd, 1, 100);
|
||||
ADCLimits limits;
|
||||
HAL_SPI_Receive(&FPGA_SPI, (uint8_t*) &limits, 6, 100);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
return limits;
|
||||
}
|
||||
|
||||
void FPGA::ResetADCLimits() {
|
||||
uint16_t cmd = 0x6000;
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_Transmit(&FPGA_SPI, (uint8_t*) &cmd, 1, 100);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
}
|
||||
|
||||
void FPGA::ResumeHaltedSweep() {
|
||||
uint16_t cmd = 0x2000;
|
||||
Low(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
HAL_SPI_Transmit(&FPGA_SPI, (uint8_t*) &cmd, 1, 100);
|
||||
High(FPGA_CS_GPIO_Port, FPGA_CS_Pin);
|
||||
}
|
||||
|
||||
99
Software/VNA_embedded/Application/Drivers/FPGA.hpp
Normal file
99
Software/VNA_embedded/Application/Drivers/FPGA.hpp
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace FPGA {
|
||||
|
||||
static constexpr uint16_t MaxPoints = 4501;
|
||||
|
||||
enum class Reg {
|
||||
InterruptMask = 0x00,
|
||||
SweepPoints = 0x01,
|
||||
SamplesPerPoint = 0x02,
|
||||
SystemControl = 0x03,
|
||||
SettlingTime = 0x04,
|
||||
MAX2871Def0LSB = 0x08,
|
||||
MAX2871Def0MSB = 0x09,
|
||||
MAX2871Def1LSB = 0x0A,
|
||||
MAX2871Def1MSB = 0x0B,
|
||||
MAX2871Def3LSB = 0x0C,
|
||||
MAX2871Def3MSB = 0x0D,
|
||||
MAX2871Def4LSB = 0x0E,
|
||||
MAX2871Def4MSB = 0x0F,
|
||||
};
|
||||
|
||||
using SamplingResult = struct _samplingresult {
|
||||
int64_t P1I, P1Q;
|
||||
int64_t P2I, P2Q;
|
||||
int64_t RefI, RefQ;
|
||||
};
|
||||
|
||||
using ADCLimits = struct _adclimits {
|
||||
int16_t P1min, P1max;
|
||||
int16_t P2min, P2max;
|
||||
int16_t Rmin, Rmax;
|
||||
};
|
||||
|
||||
enum class Periphery {
|
||||
Port1Mixer = 0x8000,
|
||||
Port2Mixer = 0x4000,
|
||||
RefMixer = 0x2000,
|
||||
Amplifier = 0x1000,
|
||||
SourceRF = 0x0800,
|
||||
LO1RF = 0x0400,
|
||||
ExtRefLED = 0x0200,
|
||||
ReadyLED = 0x0100,
|
||||
DebugLED = 0x0080,
|
||||
SourceChip = 0x0010,
|
||||
LO1Chip = 0x0008,
|
||||
ExcitePort2 = 0x0004,
|
||||
ExcitePort1 = 0x0002,
|
||||
};
|
||||
|
||||
enum class Interrupt {
|
||||
LO1Unlock = 0x0001,
|
||||
SourceUnlock = 0x0002,
|
||||
NewData = 0x0004,
|
||||
DataOverrun = 0x0008,
|
||||
SweepHalted = 0x0010,
|
||||
};
|
||||
|
||||
enum class LowpassFilter {
|
||||
M947 = 0x00,
|
||||
M1880 = 0x01,
|
||||
M3500 = 0x02,
|
||||
None = 0x03,
|
||||
Auto = 0xFF,
|
||||
};
|
||||
|
||||
using HaltedCallback = void(*)(void);
|
||||
bool Init(HaltedCallback cb = nullptr);
|
||||
void SetNumberOfPoints(uint16_t npoints);
|
||||
void SetSamplesPerPoint(uint32_t nsamples);
|
||||
void SetSettlingTime(uint16_t us);
|
||||
void Enable(Periphery p, bool enable = true);
|
||||
void Disable(Periphery p);
|
||||
void EnableInterrupt(Interrupt i);
|
||||
void DisableInterrupt(Interrupt i);
|
||||
void WriteMAX2871Default(uint32_t *DefaultRegs);
|
||||
void WriteSweepConfig(uint16_t pointnum, bool lowband, uint32_t *SourceRegs, uint32_t *LORegs,
|
||||
uint8_t attenuation, uint64_t frequency, bool halt = false, LowpassFilter filter = LowpassFilter::Auto);
|
||||
using ReadCallback = void(*)(SamplingResult result);
|
||||
bool InitiateSampleRead(ReadCallback cb);
|
||||
ADCLimits GetADCLimits();
|
||||
void ResetADCLimits();
|
||||
void ResumeHaltedSweep();
|
||||
uint16_t GetStatus();
|
||||
|
||||
void StartSweep();
|
||||
void AbortSweep();
|
||||
|
||||
enum class Mode {
|
||||
FPGA,
|
||||
SourcePLL,
|
||||
LOPLL,
|
||||
};
|
||||
void SetMode(Mode mode);
|
||||
|
||||
|
||||
}
|
||||
163
Software/VNA_embedded/Application/Drivers/Log.cpp
Normal file
163
Software/VNA_embedded/Application/Drivers/Log.cpp
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
#include "Log.h"
|
||||
|
||||
#include "stm.hpp"
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
extern "C" {
|
||||
|
||||
/* Automatically build register and function names based on USART selection */
|
||||
#define USART_M2(y) USART ## y
|
||||
#define USART_M1(y) USART_M2(y)
|
||||
#define USART_BASE USART_M1(LOG_USART)
|
||||
|
||||
#define HANDLER_M2(x) USART ## x ## _IRQHandler
|
||||
#define HANDLER_M1(x) HANDLER_M2(x)
|
||||
#define HANDLER HANDLER_M1(LOG_USART)
|
||||
|
||||
#define NVIC_ISR_M2(x) USART ## x ## _IRQn
|
||||
#define NVIC_ISR_M1(x) NVIC_ISR_M2(x)
|
||||
#define NVIC_ISR NVIC_ISR_M1(LOG_USART)
|
||||
|
||||
#define CLK_ENABLE_M2(x) __HAL_RCC_USART ## x ## _CLK_ENABLE()
|
||||
#define CLK_ENABLE_M1(x) CLK_ENABLE_M2(x)
|
||||
#define CLK_ENABLE() CLK_ENABLE_M1(LOG_USART)
|
||||
|
||||
#define CLK_DISABLE_M2(x) __HAL_RCC_USART ## x ## _CLK_DISABLE()
|
||||
#define CLK_DISABLE_M1(x) CLK_DISABLE_M2(x)
|
||||
#define CLK_DISABLE() CLK_DISABLE_M1(LOG_USART)
|
||||
|
||||
#define MAX_LINE_LENGTH 256
|
||||
|
||||
#ifdef USART_SR_TXE
|
||||
#define USART_ISR_REG SR
|
||||
#define USART_RXNE USART_SR_RXNE
|
||||
#define USART_TXE USART_SR_TXE
|
||||
#define USART_TC USART_SR_TC
|
||||
#define USART_READ DR
|
||||
#define USART_WRITE DR
|
||||
#else
|
||||
#define USART_ISR_REG ISR
|
||||
#define USART_RXNE USART_ISR_RXNE
|
||||
#define USART_TXE USART_ISR_TXE
|
||||
#define USART_TC USART_ISR_TC
|
||||
#define USART_READ RDR
|
||||
#define USART_WRITE TDR
|
||||
#endif
|
||||
|
||||
static char fifo[LOG_SENDBUF_LENGTH + MAX_LINE_LENGTH];
|
||||
static uint16_t fifo_write, fifo_read;
|
||||
|
||||
#ifdef LOG_USE_MUTEX
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
static StaticSemaphore_t xMutex;
|
||||
static SemaphoreHandle_t mutex;
|
||||
#endif
|
||||
|
||||
#define INC_FIFO_POS(pos, inc) do { pos = (pos + inc) % LOG_SENDBUF_LENGTH; } while(0)
|
||||
|
||||
static uint16_t fifo_space() {
|
||||
uint16_t used;
|
||||
if(fifo_write >= fifo_read) {
|
||||
used = fifo_write - fifo_read;
|
||||
} else {
|
||||
used = fifo_write - fifo_read + LOG_SENDBUF_LENGTH;
|
||||
}
|
||||
return LOG_SENDBUF_LENGTH - used - 1;
|
||||
}
|
||||
|
||||
static log_redirect_t redirect;
|
||||
|
||||
void Log_Init() {
|
||||
fifo_write = 0;
|
||||
fifo_read = 0;
|
||||
redirect = NULL;
|
||||
#ifdef LOG_USE_MUTEXES
|
||||
mutex = xSemaphoreCreateMutexStatic(&xMutex);
|
||||
#endif
|
||||
|
||||
/* USART interrupt Init */
|
||||
HAL_NVIC_SetPriority(NVIC_ISR, 5, 0);
|
||||
HAL_NVIC_EnableIRQ(NVIC_ISR);
|
||||
}
|
||||
|
||||
void Log_SetRedirect(log_redirect_t redirect_function) {
|
||||
redirect = redirect_function;
|
||||
}
|
||||
|
||||
void _log_write(const char *module, const char *level, const char *fmt, ...) {
|
||||
int written = 0;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
#ifdef LOG_USE_MUTEX
|
||||
if (!STM::InInterrupt()) {
|
||||
xSemaphoreTake(mutex, portMAX_DELAY);
|
||||
}
|
||||
#endif
|
||||
written = snprintf(&fifo[fifo_write], MAX_LINE_LENGTH, "%05lu [%6.6s,%s]: ",
|
||||
HAL_GetTick(), module, level);
|
||||
written += vsnprintf(&fifo[fifo_write + written], MAX_LINE_LENGTH - written,
|
||||
fmt, args);
|
||||
written += snprintf(&fifo[fifo_write + written], MAX_LINE_LENGTH - written,
|
||||
"\r\n");
|
||||
if(redirect) {
|
||||
redirect(&fifo[fifo_write], written);
|
||||
}
|
||||
// check if line still fits into ring buffer
|
||||
#ifdef LOG_BLOCKING
|
||||
while (written > fifo_space()) {
|
||||
HAL_Delay(1);
|
||||
}
|
||||
#else
|
||||
if (written > fifo_space()) {
|
||||
// unable to fit line, skip
|
||||
#ifdef LOG_USE_MUTEX
|
||||
if (!stm_in_interrupt()) {
|
||||
xSemaphoreGive(mutex);
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
int16_t overflow = (fifo_write + written) - LOG_SENDBUF_LENGTH;
|
||||
if (overflow > 0) {
|
||||
// printf wrote over the end of the ring buffer -> wrap around
|
||||
memmove(&fifo[0], &fifo[LOG_SENDBUF_LENGTH], overflow);
|
||||
}
|
||||
INC_FIFO_POS(fifo_write, written);
|
||||
// enable interrupt
|
||||
CLK_ENABLE();
|
||||
USART_BASE->CR1 |= USART_CR1_TXEIE | USART_CR1_TCIE;
|
||||
#ifdef LOG_USE_MUTEX
|
||||
if (!stm_in_interrupt()) {
|
||||
xSemaphoreGive(mutex);
|
||||
}
|
||||
#endif
|
||||
#ifdef LOG_BLOCKING
|
||||
while(USART_BASE->CR1 & USART_CR1_TCIE);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Implemented directly here for speed reasons. Disable interrupt in CubeMX! */
|
||||
void HANDLER(void) {
|
||||
if (USART_BASE->USART_ISR_REG & USART_TC) {
|
||||
// clear flag
|
||||
USART_BASE->USART_ISR_REG &= ~USART_TC;
|
||||
if (!(USART_BASE->CR1 & USART_CR1_TXEIE)) {
|
||||
USART_BASE->CR1 &= ~USART_CR1_TCIE;
|
||||
CLK_DISABLE();
|
||||
}
|
||||
}
|
||||
if ((USART_BASE->CR1 & USART_CR1_TXEIE)
|
||||
&& (USART_BASE->USART_ISR_REG & USART_TXE)) {
|
||||
USART_BASE->USART_WRITE = fifo[fifo_read];
|
||||
INC_FIFO_POS(fifo_read, 1);
|
||||
if (fifo_read == fifo_write) {
|
||||
// all done, disable interrupt
|
||||
USART_BASE->CR1 &= ~USART_CR1_TXEIE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
64
Software/VNA_embedded/Application/Drivers/Log.h
Normal file
64
Software/VNA_embedded/Application/Drivers/Log.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//#define LOG_BLOCKING
|
||||
|
||||
#define LOG_USART 2
|
||||
#define LOG_SENDBUF_LENGTH 1024
|
||||
//#define LOG_USE_MUTEX
|
||||
|
||||
#define LOG_LEVEL_DEBUG 4
|
||||
#define LOG_LEVEL_INFO 3
|
||||
#define LOG_LEVEL_WARN 2
|
||||
#define LOG_LEVEL_ERR 1
|
||||
#define LOG_LEVEL_CRIT 0
|
||||
|
||||
#define LOG_LEVEL_DEFAULT LOG_LEVEL_ERR
|
||||
|
||||
#ifndef LOG_LEVEL
|
||||
#define LOG_LEVEL LOG_LEVEL_DEFAULT
|
||||
#endif
|
||||
|
||||
#ifndef LOG_MODULE
|
||||
#define LOG_MODULE "Log"
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_CRIT
|
||||
#define LOG_CRIT(fmt, ...) _log_write(LOG_MODULE, "CRT", fmt, ## __VA_ARGS__)
|
||||
#else
|
||||
#define LOG_CRIT(fmt, ...)
|
||||
#endif
|
||||
#if LOG_LEVEL >= LOG_LEVEL_ERR
|
||||
#define LOG_ERR(fmt, ...) _log_write(LOG_MODULE, "ERR", fmt, ## __VA_ARGS__)
|
||||
#else
|
||||
#define LOG_ERR(fmt, ...)
|
||||
#endif
|
||||
#if LOG_LEVEL >= LOG_LEVEL_WARN
|
||||
#define LOG_WARN(fmt, ...) _log_write(LOG_MODULE, "WRN", fmt, ## __VA_ARGS__)
|
||||
#else
|
||||
#define LOG_WARN(fmt, ...)
|
||||
#endif
|
||||
#if LOG_LEVEL >= LOG_LEVEL_INFO
|
||||
#define LOG_INFO(fmt, ...) _log_write(LOG_MODULE, "INF", fmt, ## __VA_ARGS__)
|
||||
#else
|
||||
#define LOG_INFO(fmt, ...)
|
||||
#endif
|
||||
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
|
||||
#define LOG_DEBUG(fmt, ...) _log_write(LOG_MODULE, "DBG", fmt, ## __VA_ARGS__)
|
||||
#else
|
||||
#define LOG_DEBUG(fmt, ...)
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void Log_Init();
|
||||
typedef void (*log_redirect_t)(const char *line, uint16_t length);
|
||||
void Log_SetRedirect(log_redirect_t redirect_function);
|
||||
void _log_write(const char *module, const char *level, const char *fmt, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
340
Software/VNA_embedded/Application/Drivers/Si5351C.cpp
Normal file
340
Software/VNA_embedded/Application/Drivers/Si5351C.cpp
Normal file
|
|
@ -0,0 +1,340 @@
|
|||
#include "Si5351C.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#define LOG_LEVEL LOG_LEVEL_INFO
|
||||
#define LOG_MODULE "SI5351"
|
||||
#include "Log.h"
|
||||
|
||||
bool Si5351C::Init(uint32_t clkin_freq) {
|
||||
bool success = true;
|
||||
|
||||
uint8_t clkinDiv = 0;
|
||||
while (clkin_freq / (1 << clkinDiv) > 40000000) {
|
||||
if (clkinDiv < 3) {
|
||||
clkinDiv++;
|
||||
} else {
|
||||
LOG_ERR("CLK in too high");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
FreqCLKINDiv = clkin_freq / (1 << clkinDiv);
|
||||
// Write CLK in divider
|
||||
uint8_t value;
|
||||
success &= ReadRegister(Reg::PLLInputSource, &value);
|
||||
value &= ~0xC0;
|
||||
value |= clkinDiv << 6;
|
||||
success &= WriteRegister(Reg::PLLInputSource, value);
|
||||
|
||||
// Disable OEB pin functionality
|
||||
success &= WriteRegister(Reg::OEBPinMask, 0xFF);
|
||||
// Disable all outputs
|
||||
success &= WriteRegister(Reg::OutputEnableControl, 0xFF);
|
||||
|
||||
// Enable fanouts
|
||||
success &= WriteRegister(Reg::FanoutEnable, 0xD0);
|
||||
|
||||
if(success) {
|
||||
LOG_INFO("Initialized");
|
||||
} else {
|
||||
LOG_ERR("Initialization failed");
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool Si5351C::SetPLL(PLL pll, uint32_t frequency, PLLSource src) {
|
||||
if (frequency < 600000000 || frequency > 900000000) {
|
||||
LOG_ERR("Requested PLL frequency out of range (600-900MHz): %lu", frequency);
|
||||
return false;
|
||||
}
|
||||
PLLConfig c;
|
||||
c.IntegerMode = false;
|
||||
c.source = src;
|
||||
|
||||
uint32_t srcFreq = src == PLLSource::XTAL ? FreqXTAL : FreqCLKINDiv;
|
||||
// see https://www.silabs.com/documents/public/application-notes/AN619.pdf (page 3)
|
||||
uint64_t div27 = (uint64_t) frequency * (1UL << 27) / srcFreq;
|
||||
// Check for valid range
|
||||
if (div27 < 15 * (1ULL << 27) || div27 > 90 * (1ULL << 27)) {
|
||||
LOG_ERR("Calculated divider out of range (15-90)");
|
||||
return false;
|
||||
}
|
||||
FindOptimalDivider(frequency, srcFreq, c.P1, c.P2, c.P3);
|
||||
|
||||
FreqPLL[(int) pll] = frequency;
|
||||
LOG_INFO("Setting PLL %c to %luHz", pll==PLL::A ? 'A' : 'B', frequency);
|
||||
return WritePLLConfig(c, pll);
|
||||
}
|
||||
|
||||
bool Si5351C::SetCLK(uint8_t clknum, uint32_t frequency, PLL source, DriveStrength strength, uint32_t PLLFreqOverride) {
|
||||
ClkConfig c;
|
||||
c.DivideBy4 = false;
|
||||
c.IntegerMode = false;
|
||||
c.Inverted = false;
|
||||
c.PoweredDown = false;
|
||||
c.RDiv = 1;
|
||||
c.source = source;
|
||||
c.strength = strength;
|
||||
|
||||
uint32_t pllFreq = PLLFreqOverride > 0 ? PLLFreqOverride : FreqPLL[(int) source];
|
||||
if (clknum > 5) {
|
||||
// outputs 6 and 7 are integer dividers only
|
||||
uint32_t div = pllFreq / frequency;
|
||||
if (div > 254 || div < 6) {
|
||||
LOG_ERR("Divider on CLK6/7 out of range (6-254), would need %lu", div);
|
||||
return false;
|
||||
}
|
||||
if(div & 0x01) {
|
||||
LOG_ERR("Divider on CLK6/7 must be even, clock frequency will not match exactly");
|
||||
}
|
||||
div &= 0xFE;
|
||||
uint32_t actualFreq = pllFreq / div;
|
||||
uint32_t deviation = abs(frequency - actualFreq);
|
||||
if (deviation > 0) {
|
||||
LOG_WARN("Optimal divider for %luHz/%luHz is: %u (%luHz deviation)",
|
||||
pllFreq, frequency, div, abs(frequency - actualFreq));
|
||||
}
|
||||
c.P1 = div;
|
||||
} else {
|
||||
while (pllFreq / (frequency * c.RDiv) >= 2048
|
||||
|| (frequency * c.RDiv) < 500000) {
|
||||
if (c.RDiv < 128) {
|
||||
c.RDiv *= 2;
|
||||
} else {
|
||||
LOG_ERR("Unable to reach requested frequency");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
FindOptimalDivider(pllFreq, frequency * c.RDiv, c.P1, c.P2, c.P3);
|
||||
}
|
||||
LOG_DEBUG("Setting CLK%d to %luHz", clknum, frequency);
|
||||
return WriteClkConfig(c, clknum);
|
||||
}
|
||||
|
||||
bool Si5351C::SetCLKtoXTAL(uint8_t clknum) {
|
||||
Reg reg = (Reg) ((int) Reg::CLK0Control + clknum);
|
||||
LOG_INFO("Connecting CLK%d to XTAL", clknum);
|
||||
return ClearBits(reg, 0x0C);
|
||||
}
|
||||
bool Si5351C::SetCLKToCLKIN(uint8_t clknum) {
|
||||
Reg reg = (Reg) ((int) Reg::CLK0Control + clknum);
|
||||
LOG_INFO("Connecting CLK%d to CLK in", clknum);
|
||||
return ClearBits(reg, 0x08) && SetBits(reg, 0x04);
|
||||
}
|
||||
|
||||
bool Si5351C::Enable(uint8_t clknum) {
|
||||
LOG_INFO("Enabling CLK%d", clknum);
|
||||
return ClearBits(Reg::OutputEnableControl, 1 << clknum);
|
||||
}
|
||||
|
||||
bool Si5351C::Disable(uint8_t clknum) {
|
||||
LOG_INFO("Disabling CLK%d", clknum);
|
||||
return SetBits(Reg::OutputEnableControl, 1 << clknum);
|
||||
}
|
||||
|
||||
bool Si5351C::Locked(PLL pll) {
|
||||
uint8_t mask = pll == PLL::A ? 0x20 : 0x40;
|
||||
uint8_t value;
|
||||
ReadRegister(Reg::DeviceStatus, &value);
|
||||
LOG_DEBUG("Device status: 0x%02x", value);
|
||||
if (value & mask) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool Si5351C::WritePLLConfig(PLLConfig config, PLL pll) {
|
||||
uint8_t PllData[8];
|
||||
// See register map in https://www.silabs.com/documents/public/application-notes/AN619.pdf (page 11)
|
||||
PllData[0] = (config.P3 >> 8) & 0xFF;
|
||||
PllData[1] = config.P3 & 0xFF;
|
||||
PllData[2] = (config.P1 >> 16) & 0x03;
|
||||
PllData[3] = (config.P1 >> 8) & 0xFF;
|
||||
PllData[4] = config.P1 & 0xFF;
|
||||
PllData[5] = ((config.P3 >> 12) & 0xF0) | ((config.P2 >> 16) & 0x0F);
|
||||
PllData[6] = (config.P2 >> 8) & 0xFF;
|
||||
PllData[7] = config.P2 & 0xFF;
|
||||
bool success = true;
|
||||
Reg reg = pll == PLL::A ? Reg::MSNA_CONFIG : Reg::MSNB_CONFIG;
|
||||
success &= WriteRegisterRange(reg, PllData, sizeof(PllData));
|
||||
reg = pll == PLL::A ? Reg::CLK6Control : Reg::CLK7Control;
|
||||
if (config.IntegerMode) {
|
||||
success &=SetBits(reg, 0x40);
|
||||
} else {
|
||||
success &=ClearBits(reg, 0x40);
|
||||
}
|
||||
uint8_t mask = pll == PLL::A ? 0x04 : 0x08;
|
||||
if (config.source == PLLSource::XTAL) {
|
||||
success &=ClearBits(Reg::PLLInputSource, mask);
|
||||
} else {
|
||||
success &=SetBits(Reg::PLLInputSource, mask);
|
||||
}
|
||||
|
||||
// Reset the PLL
|
||||
mask = pll == PLL::A ? 0x20 : 0x80;
|
||||
//success &=SetBits(Reg::PLLReset, mask);
|
||||
reg = pll == PLL::A ? Reg::MSNA_CONFIG : Reg::MSNB_CONFIG;
|
||||
for(uint8_t i=0;i<8;i++) {
|
||||
uint8_t readback;
|
||||
ReadRegister((Reg)((int)reg + i), &readback);
|
||||
LOG_DEBUG("PLL readback %d: 0x%02x", i, readback);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool Si5351C::WriteClkConfig(ClkConfig config, uint8_t clknum) {
|
||||
bool success = true;
|
||||
// compile CLKControl register
|
||||
uint8_t clkcontrol = 0x0C;
|
||||
if (config.PoweredDown) {
|
||||
clkcontrol |= 0x80;
|
||||
}
|
||||
if (clknum <= 5) {
|
||||
if (config.IntegerMode) {
|
||||
clkcontrol |= 0x40;
|
||||
}
|
||||
} else {
|
||||
// preserve bit 6
|
||||
uint8_t value = 0x00;
|
||||
success &= ReadRegister((Reg) ((int) Reg::CLK0Control + clknum),
|
||||
&value);
|
||||
clkcontrol |= value & 0x40;
|
||||
}
|
||||
if (config.source == PLL::B) {
|
||||
clkcontrol |= 0x20;
|
||||
}
|
||||
if (config.Inverted) {
|
||||
clkcontrol |= 0x10;
|
||||
}
|
||||
switch (config.strength) {
|
||||
case DriveStrength::mA2:
|
||||
break;
|
||||
case DriveStrength::mA4:
|
||||
clkcontrol |= 0x01;
|
||||
break;
|
||||
case DriveStrength::mA6:
|
||||
clkcontrol |= 0x02;
|
||||
break;
|
||||
case DriveStrength::mA8:
|
||||
clkcontrol |= 0x03;
|
||||
break;
|
||||
}
|
||||
Reg reg = (Reg) ((int) Reg::CLK0Control + clknum);
|
||||
success &= WriteRegister(reg, clkcontrol);
|
||||
if (clknum <= 5) {
|
||||
uint8_t ClkData[8];
|
||||
ClkData[0] = (config.P3 >> 8) & 0xFF;
|
||||
ClkData[1] = config.P3 & 0xFF;
|
||||
ClkData[2] = (31 - __builtin_clz(config.RDiv)) << 4
|
||||
| (config.DivideBy4 ? 0xC0 : 0x00) | ((config.P1 >> 16) & 0x03);
|
||||
ClkData[3] = (config.P1 >> 8) & 0xFF;
|
||||
ClkData[4] = config.P1 & 0xFF;
|
||||
ClkData[5] = ((config.P3 >> 12) & 0xF0) | ((config.P2 >> 16) & 0x0F);
|
||||
ClkData[6] = (config.P2 >> 8) & 0xFF;
|
||||
ClkData[7] = config.P2 & 0xFF;
|
||||
// Calculate address of register control block
|
||||
reg = (Reg) ((int) Reg::MS0_CONFIG + 8 * clknum);
|
||||
success &= WriteRegisterRange(reg, ClkData, sizeof(ClkData));
|
||||
} else if (clknum == 6) {
|
||||
success &= WriteRegister(Reg::MS6_CONFIG, config.P1 & 0xFF);
|
||||
} else {
|
||||
success &= WriteRegister(Reg::MS7_CONFIG, config.P1 & 0xFF);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool Si5351C::WriteRegister(Reg reg, uint8_t data) {
|
||||
return WriteRegisterRange(reg, &data, 1);
|
||||
}
|
||||
|
||||
bool Si5351C::ReadRegister(Reg reg, uint8_t *data) {
|
||||
return HAL_I2C_Mem_Read(i2c, address, (int) reg,
|
||||
I2C_MEMADD_SIZE_8BIT, data, 1, 100) == HAL_OK;
|
||||
}
|
||||
|
||||
bool Si5351C::SetBits(Reg reg, uint8_t bits) {
|
||||
uint8_t value = 0;
|
||||
if (!ReadRegister(reg, &value)) {
|
||||
return false;
|
||||
}
|
||||
value |= bits;
|
||||
return WriteRegister(reg, value);
|
||||
}
|
||||
|
||||
bool Si5351C::ClearBits(Reg reg, uint8_t bits) {
|
||||
uint8_t value = 0;
|
||||
if (!ReadRegister(reg, &value)) {
|
||||
return false;
|
||||
}
|
||||
value &= ~bits;
|
||||
return WriteRegister(reg, value);
|
||||
}
|
||||
|
||||
bool Si5351C::WriteRegisterRange(Reg start, const uint8_t *data, uint8_t len) {
|
||||
return HAL_I2C_Mem_Write(i2c, address, (int) start,
|
||||
I2C_MEMADD_SIZE_8BIT, (uint8_t*) data, len, 100) == HAL_OK;
|
||||
}
|
||||
|
||||
bool Si5351C::ReadRegisterRange(Reg start, uint8_t *data, uint8_t len) {
|
||||
return HAL_I2C_Mem_Read(i2c, address, (int) start,
|
||||
I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK;
|
||||
}
|
||||
|
||||
bool Si5351C::ResetPLL(PLL pll) {
|
||||
if (pll == PLL::A) {
|
||||
return SetBits(Reg::PLLReset, 0x20);
|
||||
} else {
|
||||
return SetBits(Reg::PLLReset, 0x80);
|
||||
}
|
||||
}
|
||||
|
||||
void Si5351C::FindOptimalDivider(uint32_t f_pll, uint32_t f, uint32_t &P1,
|
||||
uint32_t &P2, uint32_t &P3) {
|
||||
// see https://www.silabs.com/documents/public/application-notes/AN619.pdf (page 3/6)
|
||||
uint32_t a = f_pll / f;
|
||||
int32_t f_rem = f_pll - f * a;
|
||||
uint32_t best_b, best_c;
|
||||
uint32_t best_deviation = UINT32_MAX;
|
||||
for (uint32_t c = (1UL << 20) - 1; c >= (1UL << 19); c--) {
|
||||
uint32_t guess_b = (uint64_t) f_rem * c / f;
|
||||
for (uint32_t b = guess_b; b <= guess_b + 1; b++) {
|
||||
int32_t f_div = (uint64_t) f * b / c;
|
||||
uint32_t deviation = abs(f_rem - f_div);
|
||||
if (deviation < best_deviation) {
|
||||
best_b = b;
|
||||
best_c = c;
|
||||
best_deviation = deviation;
|
||||
if (deviation == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (best_deviation == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
LOG_DEBUG(
|
||||
"Optimal divider for %luHz/%luHz is: a=%lu, b=%lu, c=%lu (%luHz deviation)",
|
||||
f_pll, f, a, best_b, best_c, best_deviation);
|
||||
// convert to Si5351C parameters
|
||||
uint32_t floor = 128 * best_b / best_c;
|
||||
P1 = 128 * a + floor - 512;
|
||||
P2 = 128 * best_b - best_c * floor;
|
||||
P3 = best_c;
|
||||
LOG_DEBUG("P1=%lu, P2=%lu, P3=%lu", P1, P2, P3);
|
||||
}
|
||||
|
||||
bool Si5351C::WriteRawCLKConfig(uint8_t clknum, const uint8_t *config) {
|
||||
// Calculate address of register control block
|
||||
auto reg = (Reg) ((int) Reg::MS0_CONFIG + 8 * clknum);
|
||||
return WriteRegisterRange(reg, config, 8);
|
||||
}
|
||||
|
||||
bool Si5351C::ReadRawCLKConfig(uint8_t clknum, uint8_t *config) {
|
||||
// Calculate address of register control block
|
||||
auto reg = (Reg) ((int) Reg::MS0_CONFIG + 8 * clknum);
|
||||
return ReadRegisterRange(reg, config, 8);
|
||||
}
|
||||
121
Software/VNA_embedded/Application/Drivers/Si5351C.hpp
Normal file
121
Software/VNA_embedded/Application/Drivers/Si5351C.hpp
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
#pragma once
|
||||
|
||||
#include "stm.hpp"
|
||||
|
||||
class Si5351C {
|
||||
public:
|
||||
enum class PLL : uint8_t {
|
||||
A = 0,
|
||||
B = 1,
|
||||
};
|
||||
enum class DriveStrength : uint8_t {
|
||||
mA2 = 0x00,
|
||||
mA4 = 0x01,
|
||||
mA6 = 0x02,
|
||||
mA8 = 0x03,
|
||||
};
|
||||
enum class PLLSource : uint8_t {
|
||||
XTAL,
|
||||
CLKIN,
|
||||
};
|
||||
constexpr Si5351C(I2C_HandleTypeDef *i2c, uint32_t XTAL_freq, GPIO_InitTypeDef *intr_gpio = nullptr,
|
||||
uint16_t intr_pin = 0, GPIO_InitTypeDef *oeb_gpio = nullptr, uint16_t oeb_pin = 0):
|
||||
i2c(i2c),
|
||||
intr_gpio(intr_gpio),
|
||||
intr_pin(intr_pin),
|
||||
oeb_gpio(oeb_gpio),
|
||||
oeb_pin(oeb_pin),
|
||||
FreqPLL{},
|
||||
FreqXTAL(XTAL_freq),
|
||||
FreqCLKINDiv(0) {
|
||||
};
|
||||
bool Init(uint32_t clkin_freq = 0);
|
||||
bool SetPLL(PLL pll, uint32_t frequency, PLLSource src);
|
||||
bool SetCLK(uint8_t clknum, uint32_t frequency, PLL source, DriveStrength strength = DriveStrength::mA2, uint32_t PLLFreqOverride = 0);
|
||||
bool SetCLKtoXTAL(uint8_t clknum);
|
||||
bool SetCLKToCLKIN(uint8_t clknum);
|
||||
bool Enable(uint8_t clknum);
|
||||
bool Disable(uint8_t clknum);
|
||||
bool Locked(PLL pll);
|
||||
bool ResetPLL(PLL pll);
|
||||
|
||||
// Direct register access of clk configuration registers
|
||||
// config has to point to a buffer containing at least 8 bytes
|
||||
bool WriteRawCLKConfig(uint8_t clknum, const uint8_t *config);
|
||||
bool ReadRawCLKConfig(uint8_t clknum, uint8_t *config);
|
||||
private:
|
||||
void FindOptimalDivider(uint32_t f_pll, uint32_t f, uint32_t &P1, uint32_t &P2, uint32_t &P3);
|
||||
enum class Reg : uint8_t {
|
||||
DeviceStatus = 0,
|
||||
InterruptStatusSticky = 1,
|
||||
InterruptStatusMask = 2,
|
||||
OutputEnableControl = 3,
|
||||
OEBPinMask = 9,
|
||||
PLLInputSource = 15,
|
||||
CLK0Control = 16,
|
||||
CLK1Control = 17,
|
||||
CLK2Control = 18,
|
||||
CLK3Control = 19,
|
||||
CLK4Control = 20,
|
||||
CLK5Control = 21,
|
||||
CLK6Control = 22,
|
||||
CLK7Control = 23,
|
||||
CLK3_0DisableState = 24,
|
||||
CLK7_4DisableState = 25,
|
||||
MSNA_CONFIG = 26,
|
||||
MSNB_CONFIG = 34,
|
||||
MS0_CONFIG = 42,
|
||||
MS1_CONFIG = 50,
|
||||
MS2_CONFIG = 58,
|
||||
MS3_CONFIG = 66,
|
||||
MS4_CONFIG = 74,
|
||||
MS5_CONFIG = 82,
|
||||
MS6_CONFIG = 90,
|
||||
MS7_CONFIG = 91,
|
||||
R6_7_Divider = 92,
|
||||
// Left out: Spread Spectrum and VCXO parameters
|
||||
CLK0_Offset = 165,
|
||||
CLK1_Offset = 166,
|
||||
CLK2_Offset = 167,
|
||||
CLK3_Offset = 168,
|
||||
CLK4_Offset = 169,
|
||||
CLK5_Offset = 170,
|
||||
PLLReset = 177,
|
||||
CrystalLoadCapacitance = 183,
|
||||
FanoutEnable = 187,
|
||||
};
|
||||
|
||||
using PLLConfig = struct {
|
||||
uint32_t P1, P2, P3;
|
||||
bool IntegerMode;
|
||||
PLLSource source;
|
||||
};
|
||||
bool WritePLLConfig(PLLConfig config, PLL pll);
|
||||
|
||||
using ClkConfig = struct {
|
||||
uint32_t P1, P2, P3;
|
||||
uint8_t RDiv; // 1 to 128, only 2^n
|
||||
bool DivideBy4;
|
||||
bool PoweredDown;
|
||||
bool IntegerMode;
|
||||
PLL source;
|
||||
bool Inverted;
|
||||
DriveStrength strength;
|
||||
};
|
||||
bool WriteClkConfig(ClkConfig config, uint8_t clknum);
|
||||
|
||||
static constexpr uint8_t address = 0xC0;
|
||||
bool WriteRegister(Reg reg, uint8_t data);
|
||||
bool ReadRegister(Reg reg, uint8_t *data);
|
||||
bool SetBits(Reg reg, uint8_t bits);
|
||||
bool ClearBits(Reg reg, uint8_t bits);
|
||||
bool WriteRegisterRange(Reg start, const uint8_t *data, uint8_t len);
|
||||
bool ReadRegisterRange(Reg start, uint8_t *data, uint8_t len);
|
||||
I2C_HandleTypeDef *i2c;
|
||||
GPIO_InitTypeDef *intr_gpio;
|
||||
uint16_t intr_pin;
|
||||
GPIO_InitTypeDef *oeb_gpio;
|
||||
uint16_t oeb_pin;
|
||||
uint32_t FreqPLL[2];
|
||||
uint32_t FreqXTAL, FreqCLKINDiv;
|
||||
};
|
||||
|
|
@ -0,0 +1,175 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usbd_conf.h
|
||||
* @version : v2.0_Cube
|
||||
* @brief : Header for usbd_conf.c file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_CONF__H__
|
||||
#define __USBD_CONF__H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "main.h"
|
||||
#include "stm32g4xx.h"
|
||||
#include "stm32g4xx_hal.h"
|
||||
|
||||
/* USER CODE BEGIN INCLUDE */
|
||||
|
||||
/* USER CODE END INCLUDE */
|
||||
|
||||
/** @addtogroup USBD_OTG_DRIVER
|
||||
* @brief Driver for Usb device.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF USBD_CONF
|
||||
* @brief Configuration file for Usb otg low level driver.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Variables USBD_CONF_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Defines USBD_CONF_Exported_Defines
|
||||
* @brief Defines for configuration of the Usb device.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_NUM_INTERFACES 1U
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_NUM_CONFIGURATION 1U
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_STR_DESC_SIZ 512U
|
||||
/*---------- -----------*/
|
||||
#define USBD_SUPPORT_USER_STRING 0U
|
||||
/*---------- -----------*/
|
||||
#define USBD_DEBUG_LEVEL 0U
|
||||
/*---------- -----------*/
|
||||
#define USBD_LPM_ENABLED 1U
|
||||
/*---------- -----------*/
|
||||
#define USBD_SELF_POWERED 1U
|
||||
|
||||
/****************************************/
|
||||
/* #define for FS and HS identification */
|
||||
#define DEVICE_FS 0
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Macros USBD_CONF_Exported_Macros
|
||||
* @brief Aliases.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Memory management macros */
|
||||
|
||||
/** Alias for memory allocation. */
|
||||
#define USBD_malloc (uint32_t *)USBD_static_malloc
|
||||
|
||||
/** Alias for memory release. */
|
||||
#define USBD_free USBD_static_free
|
||||
|
||||
/** Alias for memory set. */
|
||||
#define USBD_memset /* Not used */
|
||||
|
||||
/** Alias for memory copy. */
|
||||
#define USBD_memcpy /* Not used */
|
||||
|
||||
/** Alias for delay. */
|
||||
#define USBD_Delay HAL_Delay
|
||||
|
||||
/* DEBUG macros */
|
||||
|
||||
#if (USBD_DEBUG_LEVEL > 0)
|
||||
#define USBD_UsrLog(...) printf(__VA_ARGS__);\
|
||||
printf("\n");
|
||||
#else
|
||||
#define USBD_UsrLog(...)
|
||||
#endif
|
||||
|
||||
#if (USBD_DEBUG_LEVEL > 1)
|
||||
|
||||
#define USBD_ErrLog(...) printf("ERROR: ") ;\
|
||||
printf(__VA_ARGS__);\
|
||||
printf("\n");
|
||||
#else
|
||||
#define USBD_ErrLog(...)
|
||||
#endif
|
||||
|
||||
#if (USBD_DEBUG_LEVEL > 2)
|
||||
#define USBD_DbgLog(...) printf("DEBUG : ") ;\
|
||||
printf(__VA_ARGS__);\
|
||||
printf("\n");
|
||||
#else
|
||||
#define USBD_DbgLog(...)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Types USBD_CONF_Exported_Types
|
||||
* @brief Types.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_FunctionsPrototype USBD_CONF_Exported_FunctionsPrototype
|
||||
* @brief Declaration of public functions for Usb device.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Exported functions -------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_CONF__H__ */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,161 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file usbd_core.h
|
||||
* @author MCD Application Team
|
||||
* @brief Header file for usbd_core.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* http://www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_CORE_H
|
||||
#define __USBD_CORE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_conf.h"
|
||||
#include "usbd_def.h"
|
||||
#include "usbd_ioreq.h"
|
||||
#include "usbd_ctlreq.h"
|
||||
|
||||
/** @addtogroup STM32_USB_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CORE
|
||||
* @brief This file is the Header file for usbd_core.c file
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_CORE_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
#ifndef USBD_DEBUG_LEVEL
|
||||
#define USBD_DEBUG_LEVEL 0U
|
||||
#endif /* USBD_DEBUG_LEVEL */
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_CORE_Exported_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/** @defgroup USBD_CORE_Exported_Macros
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CORE_Exported_Variables
|
||||
* @{
|
||||
*/
|
||||
#define USBD_SOF USBD_LL_SOF
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CORE_Exported_FunctionsPrototype
|
||||
* @{
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id);
|
||||
USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_Start (USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_Stop (USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass);
|
||||
|
||||
USBD_StatusTypeDef USBD_RunTestMode (USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup);
|
||||
USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev , uint8_t epnum, uint8_t *pdata);
|
||||
USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev , uint8_t epnum, uint8_t *pdata);
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, USBD_SpeedTypeDef speed);
|
||||
USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev);
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev);
|
||||
|
||||
/* USBD Low Level Driver */
|
||||
USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev);
|
||||
USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev,
|
||||
uint8_t ep_addr,
|
||||
uint8_t ep_type,
|
||||
uint16_t ep_mps);
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr);
|
||||
USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr);
|
||||
USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr);
|
||||
USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr);
|
||||
uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr);
|
||||
USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr);
|
||||
USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev,
|
||||
uint8_t ep_addr,
|
||||
uint8_t *pbuf,
|
||||
uint16_t size);
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
|
||||
uint8_t ep_addr,
|
||||
uint8_t *pbuf,
|
||||
uint16_t size);
|
||||
|
||||
uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr);
|
||||
void USBD_LL_Delay (uint32_t Delay);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_CORE_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file usbd_req.h
|
||||
* @author MCD Application Team
|
||||
* @brief Header file for the usbd_req.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* http://www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_REQUEST_H
|
||||
#define __USB_REQUEST_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
|
||||
/** @addtogroup STM32_USB_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_REQ
|
||||
* @brief header file for the usbd_req.c file
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_REQ_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ_Exported_Types
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ_Exported_Macros
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_REQ_Exported_Variables
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_REQ_Exported_FunctionsPrototype
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_StatusTypeDef USBD_StdDevReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
||||
USBD_StatusTypeDef USBD_StdItfReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
||||
USBD_StatusTypeDef USBD_StdEPReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
||||
|
||||
|
||||
void USBD_CtlError (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
||||
|
||||
void USBD_ParseSetupRequest (USBD_SetupReqTypedef *req, uint8_t *pdata);
|
||||
|
||||
void USBD_GetString (uint8_t *desc, uint8_t *unicode, uint16_t *len);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_REQUEST_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,342 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file usbd_def.h
|
||||
* @author MCD Application Team
|
||||
* @brief General defines for the usb device library
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* http://www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_DEF_H
|
||||
#define __USBD_DEF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_conf.h"
|
||||
|
||||
/** @addtogroup STM32_USBD_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USB_DEF
|
||||
* @brief general defines for the usb device library file
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USB_DEF_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0U
|
||||
#endif /* NULL */
|
||||
|
||||
#ifndef USBD_MAX_NUM_INTERFACES
|
||||
#define USBD_MAX_NUM_INTERFACES 1U
|
||||
#endif /* USBD_MAX_NUM_CONFIGURATION */
|
||||
|
||||
#ifndef USBD_MAX_NUM_CONFIGURATION
|
||||
#define USBD_MAX_NUM_CONFIGURATION 1U
|
||||
#endif /* USBD_MAX_NUM_CONFIGURATION */
|
||||
|
||||
#ifndef USBD_LPM_ENABLED
|
||||
#define USBD_LPM_ENABLED 0U
|
||||
#endif /* USBD_LPM_ENABLED */
|
||||
|
||||
#ifndef USBD_SELF_POWERED
|
||||
#define USBD_SELF_POWERED 1U
|
||||
#endif /*USBD_SELF_POWERED */
|
||||
|
||||
#ifndef USBD_SUPPORT_USER_STRING
|
||||
#define USBD_SUPPORT_USER_STRING 0U
|
||||
#endif /* USBD_SUPPORT_USER_STRING */
|
||||
|
||||
#define USB_LEN_DEV_QUALIFIER_DESC 0x0AU
|
||||
#define USB_LEN_DEV_DESC 0x12U
|
||||
#define USB_LEN_CFG_DESC 0x09U
|
||||
#define USB_LEN_IF_DESC 0x09U
|
||||
#define USB_LEN_EP_DESC 0x07U
|
||||
#define USB_LEN_OTG_DESC 0x03U
|
||||
#define USB_LEN_LANGID_STR_DESC 0x04U
|
||||
#define USB_LEN_OTHER_SPEED_DESC_SIZ 0x09U
|
||||
|
||||
#define USBD_IDX_LANGID_STR 0x00U
|
||||
#define USBD_IDX_MFC_STR 0x01U
|
||||
#define USBD_IDX_PRODUCT_STR 0x02U
|
||||
#define USBD_IDX_SERIAL_STR 0x03U
|
||||
#define USBD_IDX_CONFIG_STR 0x04U
|
||||
#define USBD_IDX_INTERFACE_STR 0x05U
|
||||
|
||||
#define USB_REQ_TYPE_STANDARD 0x00U
|
||||
#define USB_REQ_TYPE_CLASS 0x20U
|
||||
#define USB_REQ_TYPE_VENDOR 0x40U
|
||||
#define USB_REQ_TYPE_MASK 0x60U
|
||||
|
||||
#define USB_REQ_RECIPIENT_DEVICE 0x00U
|
||||
#define USB_REQ_RECIPIENT_INTERFACE 0x01U
|
||||
#define USB_REQ_RECIPIENT_ENDPOINT 0x02U
|
||||
#define USB_REQ_RECIPIENT_MASK 0x03U
|
||||
|
||||
#define USB_REQ_GET_STATUS 0x00U
|
||||
#define USB_REQ_CLEAR_FEATURE 0x01U
|
||||
#define USB_REQ_SET_FEATURE 0x03U
|
||||
#define USB_REQ_SET_ADDRESS 0x05U
|
||||
#define USB_REQ_GET_DESCRIPTOR 0x06U
|
||||
#define USB_REQ_SET_DESCRIPTOR 0x07U
|
||||
#define USB_REQ_GET_CONFIGURATION 0x08U
|
||||
#define USB_REQ_SET_CONFIGURATION 0x09U
|
||||
#define USB_REQ_GET_INTERFACE 0x0AU
|
||||
#define USB_REQ_SET_INTERFACE 0x0BU
|
||||
#define USB_REQ_SYNCH_FRAME 0x0CU
|
||||
|
||||
#define USB_DESC_TYPE_DEVICE 0x01U
|
||||
#define USB_DESC_TYPE_CONFIGURATION 0x02U
|
||||
#define USB_DESC_TYPE_STRING 0x03U
|
||||
#define USB_DESC_TYPE_INTERFACE 0x04U
|
||||
#define USB_DESC_TYPE_ENDPOINT 0x05U
|
||||
#define USB_DESC_TYPE_DEVICE_QUALIFIER 0x06U
|
||||
#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07U
|
||||
#define USB_DESC_TYPE_BOS 0x0FU
|
||||
|
||||
#define USB_CONFIG_REMOTE_WAKEUP 0x02U
|
||||
#define USB_CONFIG_SELF_POWERED 0x01U
|
||||
|
||||
#define USB_FEATURE_EP_HALT 0x00U
|
||||
#define USB_FEATURE_REMOTE_WAKEUP 0x01U
|
||||
#define USB_FEATURE_TEST_MODE 0x02U
|
||||
|
||||
#define USB_DEVICE_CAPABITY_TYPE 0x10U
|
||||
|
||||
#define USB_HS_MAX_PACKET_SIZE 512U
|
||||
#define USB_FS_MAX_PACKET_SIZE 64U
|
||||
#define USB_MAX_EP0_SIZE 64U
|
||||
|
||||
/* Device Status */
|
||||
#define USBD_STATE_DEFAULT 0x01U
|
||||
#define USBD_STATE_ADDRESSED 0x02U
|
||||
#define USBD_STATE_CONFIGURED 0x03U
|
||||
#define USBD_STATE_SUSPENDED 0x04U
|
||||
|
||||
|
||||
/* EP0 State */
|
||||
#define USBD_EP0_IDLE 0x00U
|
||||
#define USBD_EP0_SETUP 0x01U
|
||||
#define USBD_EP0_DATA_IN 0x02U
|
||||
#define USBD_EP0_DATA_OUT 0x03U
|
||||
#define USBD_EP0_STATUS_IN 0x04U
|
||||
#define USBD_EP0_STATUS_OUT 0x05U
|
||||
#define USBD_EP0_STALL 0x06U
|
||||
|
||||
#define USBD_EP_TYPE_CTRL 0x00U
|
||||
#define USBD_EP_TYPE_ISOC 0x01U
|
||||
#define USBD_EP_TYPE_BULK 0x02U
|
||||
#define USBD_EP_TYPE_INTR 0x03U
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_DEF_Exported_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct usb_setup_req
|
||||
{
|
||||
|
||||
uint8_t bmRequest;
|
||||
uint8_t bRequest;
|
||||
uint16_t wValue;
|
||||
uint16_t wIndex;
|
||||
uint16_t wLength;
|
||||
}USBD_SetupReqTypedef;
|
||||
|
||||
struct _USBD_HandleTypeDef;
|
||||
|
||||
typedef struct _Device_cb
|
||||
{
|
||||
uint8_t (*Init) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx);
|
||||
uint8_t (*DeInit) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx);
|
||||
/* Control Endpoints*/
|
||||
uint8_t (*Setup) (struct _USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req);
|
||||
uint8_t (*EP0_TxSent) (struct _USBD_HandleTypeDef *pdev );
|
||||
uint8_t (*EP0_RxReady) (struct _USBD_HandleTypeDef *pdev );
|
||||
/* Class Specific Endpoints*/
|
||||
uint8_t (*DataIn) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
|
||||
uint8_t (*DataOut) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
|
||||
uint8_t (*SOF) (struct _USBD_HandleTypeDef *pdev);
|
||||
uint8_t (*IsoINIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
|
||||
uint8_t (*IsoOUTIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
|
||||
|
||||
uint8_t *(*GetHSConfigDescriptor)(uint16_t *length);
|
||||
uint8_t *(*GetFSConfigDescriptor)(uint16_t *length);
|
||||
uint8_t *(*GetOtherSpeedConfigDescriptor)(uint16_t *length);
|
||||
uint8_t *(*GetDeviceQualifierDescriptor)(uint16_t *length);
|
||||
#if (USBD_SUPPORT_USER_STRING == 1U)
|
||||
uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev ,uint8_t index, uint16_t *length);
|
||||
#endif
|
||||
|
||||
} USBD_ClassTypeDef;
|
||||
|
||||
/* Following USB Device Speed */
|
||||
typedef enum
|
||||
{
|
||||
USBD_SPEED_HIGH = 0U,
|
||||
USBD_SPEED_FULL = 1U,
|
||||
USBD_SPEED_LOW = 2U,
|
||||
}USBD_SpeedTypeDef;
|
||||
|
||||
/* Following USB Device status */
|
||||
typedef enum {
|
||||
USBD_OK = 0U,
|
||||
USBD_BUSY,
|
||||
USBD_FAIL,
|
||||
}USBD_StatusTypeDef;
|
||||
|
||||
/* USB Device descriptors structure */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t *(*GetDeviceDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
uint8_t *(*GetLangIDStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
uint8_t *(*GetManufacturerStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
uint8_t *(*GetProductStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
uint8_t *(*GetSerialStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
uint8_t *(*GetConfigurationStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
uint8_t *(*GetInterfaceStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
#if (USBD_LPM_ENABLED == 1U)
|
||||
uint8_t *(*GetBOSDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
|
||||
#endif
|
||||
} USBD_DescriptorsTypeDef;
|
||||
|
||||
/* USB Device handle structure */
|
||||
typedef struct
|
||||
{
|
||||
uint32_t status;
|
||||
uint32_t is_used;
|
||||
uint32_t total_length;
|
||||
uint32_t rem_length;
|
||||
uint32_t maxpacket;
|
||||
} USBD_EndpointTypeDef;
|
||||
|
||||
/* USB Device handle structure */
|
||||
typedef struct _USBD_HandleTypeDef
|
||||
{
|
||||
uint8_t id;
|
||||
uint32_t dev_config;
|
||||
uint32_t dev_default_config;
|
||||
uint32_t dev_config_status;
|
||||
USBD_SpeedTypeDef dev_speed;
|
||||
USBD_EndpointTypeDef ep_in[15];
|
||||
USBD_EndpointTypeDef ep_out[15];
|
||||
uint32_t ep0_state;
|
||||
uint32_t ep0_data_len;
|
||||
uint8_t dev_state;
|
||||
uint8_t dev_old_state;
|
||||
uint8_t dev_address;
|
||||
uint8_t dev_connection_status;
|
||||
uint8_t dev_test_mode;
|
||||
uint32_t dev_remote_wakeup;
|
||||
|
||||
USBD_SetupReqTypedef request;
|
||||
USBD_DescriptorsTypeDef *pDesc;
|
||||
USBD_ClassTypeDef *pClass;
|
||||
void *pClassData;
|
||||
void *pUserData;
|
||||
void *pData;
|
||||
} USBD_HandleTypeDef;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/** @defgroup USBD_DEF_Exported_Macros
|
||||
* @{
|
||||
*/
|
||||
#define SWAPBYTE(addr) (((uint16_t)(*((uint8_t *)(addr)))) + \
|
||||
(((uint16_t)(*(((uint8_t *)(addr)) + 1U))) << 8U))
|
||||
|
||||
#define LOBYTE(x) ((uint8_t)(x & 0x00FFU))
|
||||
#define HIBYTE(x) ((uint8_t)((x & 0xFF00U) >> 8U))
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
|
||||
|
||||
#if defined ( __GNUC__ )
|
||||
#ifndef __weak
|
||||
#define __weak __attribute__((weak))
|
||||
#endif /* __weak */
|
||||
#ifndef __packed
|
||||
#define __packed __attribute__((__packed__))
|
||||
#endif /* __packed */
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
|
||||
/* In HS mode and when the DMA is used, all variables and data structures dealing
|
||||
with the DMA during the transaction process should be 4-bytes aligned */
|
||||
|
||||
#if defined (__GNUC__) /* GNU Compiler */
|
||||
#define __ALIGN_END __attribute__ ((aligned (4)))
|
||||
#define __ALIGN_BEGIN
|
||||
#else
|
||||
#define __ALIGN_END
|
||||
#if defined (__CC_ARM) /* ARM Compiler */
|
||||
#define __ALIGN_BEGIN __align(4)
|
||||
#elif defined (__ICCARM__) /* IAR Compiler */
|
||||
#define __ALIGN_BEGIN
|
||||
#elif defined (__TASKING__) /* TASKING Compiler */
|
||||
#define __ALIGN_BEGIN __align(4)
|
||||
#endif /* __CC_ARM */
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DEF_Exported_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DEF_Exported_FunctionsPrototype
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_DEF_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usbd_desc.c
|
||||
* @version : v2.0_Cube
|
||||
* @brief : Header for usbd_conf.c file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_DESC__C__
|
||||
#define __USBD_DESC__C__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/* USER CODE BEGIN INCLUDE */
|
||||
|
||||
/* USER CODE END INCLUDE */
|
||||
|
||||
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC USBD_DESC
|
||||
* @brief Usb device descriptors module.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants
|
||||
* @brief Constants.
|
||||
* @{
|
||||
*/
|
||||
#define DEVICE_ID1 (UID_BASE)
|
||||
#define DEVICE_ID2 (UID_BASE + 0x4)
|
||||
#define DEVICE_ID3 (UID_BASE + 0x8)
|
||||
|
||||
#define USB_SIZ_STRING_SERIAL 0x1A
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_CONSTANTS */
|
||||
|
||||
/* USER CODE END EXPORTED_CONSTANTS */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Defines USBD_DESC_Exported_Defines
|
||||
* @brief Defines.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_DEFINES */
|
||||
|
||||
/* USER CODE END EXPORTED_DEFINES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_TypesDefinitions USBD_DESC_Exported_TypesDefinitions
|
||||
* @brief Types.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_TYPES */
|
||||
|
||||
/* USER CODE END EXPORTED_TYPES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Macros USBD_DESC_Exported_Macros
|
||||
* @brief Aliases.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_MACRO */
|
||||
|
||||
/* USER CODE END EXPORTED_MACRO */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Variables USBD_DESC_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_VARIABLES */
|
||||
|
||||
/* USER CODE END EXPORTED_VARIABLES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_FunctionsPrototype USBD_DESC_Exported_FunctionsPrototype
|
||||
* @brief Public functions declaration.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
|
||||
|
||||
/* USER CODE END EXPORTED_FUNCTIONS */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_DESC__C__ */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file usbd_ioreq.h
|
||||
* @author MCD Application Team
|
||||
* @brief Header file for the usbd_ioreq.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* http://www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_IOREQ_H
|
||||
#define __USBD_IOREQ_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
#include "usbd_core.h"
|
||||
|
||||
/** @addtogroup STM32_USB_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_IOREQ
|
||||
* @brief header file for the usbd_ioreq.c file
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_IOREQ_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ_Exported_Types
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ_Exported_Macros
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_IOREQ_Exported_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_IOREQ_Exported_FunctionsPrototype
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_StatusTypeDef USBD_CtlSendData (USBD_HandleTypeDef *pdev,
|
||||
uint8_t *pbuf,
|
||||
uint16_t len);
|
||||
|
||||
USBD_StatusTypeDef USBD_CtlContinueSendData (USBD_HandleTypeDef *pdev,
|
||||
uint8_t *pbuf,
|
||||
uint16_t len);
|
||||
|
||||
USBD_StatusTypeDef USBD_CtlPrepareRx (USBD_HandleTypeDef *pdev,
|
||||
uint8_t *pbuf,
|
||||
uint16_t len);
|
||||
|
||||
USBD_StatusTypeDef USBD_CtlContinueRx (USBD_HandleTypeDef *pdev,
|
||||
uint8_t *pbuf,
|
||||
uint16_t len);
|
||||
|
||||
USBD_StatusTypeDef USBD_CtlSendStatus (USBD_HandleTypeDef *pdev);
|
||||
|
||||
USBD_StatusTypeDef USBD_CtlReceiveStatus (USBD_HandleTypeDef *pdev);
|
||||
|
||||
uint32_t USBD_GetRxCount (USBD_HandleTypeDef *pdev, uint8_t ep_addr);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_IOREQ_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,558 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usbd_conf.c
|
||||
* @version : v2.0_Cube
|
||||
* @brief : This file implements the board support package for the USB device library
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32g4xx.h"
|
||||
#include "stm32g4xx_hal.h"
|
||||
#include "usbd_def.h"
|
||||
#include "usbd_core.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
#define USB_HANDLE hpcd_USB_FS
|
||||
extern PCD_HandleTypeDef USB_HANDLE;
|
||||
void Error_Handler(void);
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* Exported function prototypes ----------------------------------------------*/
|
||||
extern USBD_StatusTypeDef USBD_LL_BatteryCharging(USBD_HandleTypeDef *pdev);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
static void SystemClockConfig_Resume(void);
|
||||
|
||||
/* USER CODE END 1 */
|
||||
extern void SystemClock_Config(void);
|
||||
|
||||
/*******************************************************************************
|
||||
LL Driver Callbacks (PCD -> USB Device Library)
|
||||
//*******************************************************************************/
|
||||
///* MSP Init */
|
||||
//
|
||||
//void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
|
||||
//{
|
||||
// if(pcdHandle->Instance==USB)
|
||||
// {
|
||||
// /* USER CODE BEGIN USB_MspInit 0 */
|
||||
//
|
||||
// /* USER CODE END USB_MspInit 0 */
|
||||
// /* Peripheral clock enable */
|
||||
// __HAL_RCC_USB_CLK_ENABLE();
|
||||
//
|
||||
// /* Peripheral interrupt init */
|
||||
// HAL_NVIC_SetPriority(USB_IRQn, 2, 0);
|
||||
// HAL_NVIC_EnableIRQ(USB_IRQn);
|
||||
// /* USER CODE BEGIN USB_MspInit 1 */
|
||||
//
|
||||
// /* USER CODE END USB_MspInit 1 */
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
|
||||
//{
|
||||
// if(pcdHandle->Instance==USB)
|
||||
// {
|
||||
// /* USER CODE BEGIN USB_MspDeInit 0 */
|
||||
//
|
||||
// /* USER CODE END USB_MspDeInit 0 */
|
||||
// /* Peripheral clock disable */
|
||||
// __HAL_RCC_USB_CLK_DISABLE();
|
||||
//
|
||||
// /* Peripheral interrupt Deinit*/
|
||||
// HAL_NVIC_DisableIRQ(USB_IRQn);
|
||||
//
|
||||
// /* USER CODE BEGIN USB_MspDeInit 1 */
|
||||
//
|
||||
// /* USER CODE END USB_MspDeInit 1 */
|
||||
// }
|
||||
//}
|
||||
//
|
||||
/**
|
||||
* @brief Setup stage callback
|
||||
* @param hpcd: PCD handle
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
|
||||
#else
|
||||
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Data Out stage callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @param epnum: Endpoint number
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#else
|
||||
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Data In stage callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @param epnum: Endpoint number
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#else
|
||||
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SOF callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
|
||||
#else
|
||||
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reset callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
|
||||
#else
|
||||
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
|
||||
|
||||
if ( hpcd->Init.speed != PCD_SPEED_FULL)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* Set Speed. */
|
||||
USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed);
|
||||
|
||||
/* Reset Device. */
|
||||
USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Suspend callback.
|
||||
* When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
|
||||
* @param hpcd: PCD handle
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
|
||||
#else
|
||||
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
/* Inform USB library that core enters in suspend Mode. */
|
||||
USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
|
||||
/* Enter in STOP mode. */
|
||||
/* USER CODE BEGIN 2 */
|
||||
if (hpcd->Init.low_power_enable)
|
||||
{
|
||||
/* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
|
||||
SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
|
||||
}
|
||||
/* USER CODE END 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resume callback.
|
||||
* When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
|
||||
* @param hpcd: PCD handle
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
|
||||
#else
|
||||
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
if (hpcd->Init.low_power_enable)
|
||||
{
|
||||
/* Reset SLEEPDEEP bit of Cortex System Control Register. */
|
||||
SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
|
||||
SystemClockConfig_Resume();
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ISOOUTIncomplete callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @param epnum: Endpoint number
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#else
|
||||
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ISOINIncomplete callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @param epnum: Endpoint number
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#else
|
||||
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connect callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
|
||||
#else
|
||||
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnect callback.
|
||||
* @param hpcd: PCD handle
|
||||
* @retval None
|
||||
*/
|
||||
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
|
||||
static void PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
|
||||
#else
|
||||
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
|
||||
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
|
||||
{
|
||||
USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
|
||||
}
|
||||
|
||||
static USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status)
|
||||
{
|
||||
if (hal_status == HAL_OK)
|
||||
return USBD_OK;
|
||||
else if (hal_status == HAL_BUSY)
|
||||
return USBD_BUSY;
|
||||
else
|
||||
return USBD_FAIL;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
LL Driver Interface (USB Device Library --> PCD)
|
||||
*******************************************************************************/
|
||||
/**
|
||||
* @brief Initializes the Low Level portion of the Device driver.
|
||||
* @param pdev: Device handle
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
// HAL_PWREx_EnableVddUSB();
|
||||
USB_HANDLE.pData = pdev;
|
||||
pdev->pData = &USB_HANDLE;
|
||||
// HAL_PCDEx_SetRxFiFo(&USB_HANDLE, 1024 / 4);
|
||||
// HAL_PCDEx_SetTxFiFo(&USB_HANDLE, 0, 1024 / 4);
|
||||
// HAL_PCDEx_SetTxFiFo(&USB_HANDLE, 1, 1024 / 4);
|
||||
/* USER CODE BEGIN EndPoint_Configuration */
|
||||
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18);
|
||||
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58);
|
||||
/* USER CODE END EndPoint_Configuration */
|
||||
/* USER CODE BEGIN EndPoint_Configuration_CDC */
|
||||
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0);
|
||||
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x100);
|
||||
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x140);
|
||||
//HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100);
|
||||
/* USER CODE END EndPoint_Configuration_CDC */
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief De-Initializes the Low Level portion of the Device driver.
|
||||
* @param pdev: Device handle
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_DeInit(pdev->pData);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Starts the Low Level portion of the Device driver.
|
||||
* @param pdev: Device handle
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_Start(pdev->pData);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stops the Low Level portion of the Device driver.
|
||||
* @param pdev: Device handle
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_Stop(pdev->pData);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Opens an endpoint of the Low Level Driver.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @param ep_type: Endpoint Type
|
||||
* @param ep_mps: Endpoint Max Packet Size
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr,
|
||||
uint8_t ep_type, uint16_t ep_mps)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type);
|
||||
pdev->ep_in[ep_addr & 0x7F].is_used = 1;
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Closes an endpoint of the Low Level Driver.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_EP_Close(pdev->pData, ep_addr);
|
||||
pdev->ep_in[ep_addr & 0x7F].is_used = 0;
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Flushes an endpoint of the Low Level Driver.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_EP_Flush(pdev->pData, ep_addr);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets a Stall condition on an endpoint of the Low Level Driver.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clears a Stall condition on an endpoint of the Low Level Driver.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev,
|
||||
uint8_t ep_addr)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns Stall condition.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @retval Stall (1: Yes, 0: No)
|
||||
*/
|
||||
uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
|
||||
{
|
||||
PCD_HandleTypeDef const *hpcd = pdev->pData;
|
||||
return ep_addr & 0x80 ? hpcd->IN_ep[ep_addr & 0x7F].is_stall : hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a USB address to the device.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev,
|
||||
uint8_t dev_addr)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_SetAddress(pdev->pData, dev_addr);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Transmits data over an endpoint.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @param pbuf: Pointer to data to be sent
|
||||
* @param size: Data size
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr,
|
||||
uint8_t *pbuf, uint16_t size)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Prepares an endpoint for reception.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @param pbuf: Pointer to data to be received
|
||||
* @param size: Data size
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
|
||||
uint8_t ep_addr, uint8_t *pbuf,
|
||||
uint16_t size)
|
||||
{
|
||||
HAL_StatusTypeDef const hal_status = HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
|
||||
return USBD_Get_USB_Status(hal_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the last transferred packet size.
|
||||
* @param pdev: Device handle
|
||||
* @param ep_addr: Endpoint Number
|
||||
* @retval Recived Data Size
|
||||
*/
|
||||
uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
|
||||
{
|
||||
return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*)pdev->pData, ep_addr);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Send LPM message to user layer
|
||||
* @param hpcd: PCD handle
|
||||
* @param msg: LPM message
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case PCD_LPM_L0_ACTIVE:
|
||||
if (hpcd->Init.low_power_enable)
|
||||
{
|
||||
SystemClockConfig_Resume();
|
||||
|
||||
/* Reset SLEEPDEEP bit of Cortex System Control Register. */
|
||||
SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
|
||||
}
|
||||
USBD_LL_Resume(hpcd->pData);
|
||||
break;
|
||||
|
||||
case PCD_LPM_L1_ACTIVE:
|
||||
USBD_LL_Suspend(hpcd->pData);
|
||||
|
||||
/* Enter in STOP mode. */
|
||||
if (hpcd->Init.low_power_enable)
|
||||
{
|
||||
/* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
|
||||
SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Delays routine for the USB Device Library.
|
||||
* @param Delay: Delay in ms
|
||||
* @retval None
|
||||
*/
|
||||
void USBD_LL_Delay(uint32_t Delay)
|
||||
{
|
||||
HAL_Delay(Delay);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 5 */
|
||||
/**
|
||||
* @brief Configures system clock after wake-up from USB resume callBack:
|
||||
* enable HSI, PLL and select PLL as system clock source.
|
||||
* @retval None
|
||||
*/
|
||||
static void SystemClockConfig_Resume(void)
|
||||
{
|
||||
SystemClock_Config();
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,598 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file usbd_core.c
|
||||
* @author MCD Application Team
|
||||
* @brief This file provides all the USBD core functions.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* http://www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_core.h"
|
||||
|
||||
/** @addtogroup STM32_USBD_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_CORE
|
||||
* @brief usbd core module
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CORE_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_CORE_Private_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_CORE_Private_Macros
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
/** @defgroup USBD_CORE_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CORE_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CORE_Private_Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief USBD_Init
|
||||
* Initializes the device stack and load the class driver
|
||||
* @param pdev: device instance
|
||||
* @param pdesc: Descriptor structure address
|
||||
* @param id: Low level core index
|
||||
* @retval None
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id)
|
||||
{
|
||||
/* Check whether the USB Host handle is valid */
|
||||
if(pdev == NULL)
|
||||
{
|
||||
#if (USBD_DEBUG_LEVEL > 1U)
|
||||
USBD_ErrLog("Invalid Device handle");
|
||||
#endif
|
||||
return USBD_FAIL;
|
||||
}
|
||||
|
||||
/* Unlink previous class*/
|
||||
if(pdev->pClass != NULL)
|
||||
{
|
||||
pdev->pClass = NULL;
|
||||
}
|
||||
|
||||
/* Assign USBD Descriptors */
|
||||
if(pdesc != NULL)
|
||||
{
|
||||
pdev->pDesc = pdesc;
|
||||
}
|
||||
|
||||
/* Set Device initial State */
|
||||
pdev->dev_state = USBD_STATE_DEFAULT;
|
||||
pdev->id = id;
|
||||
/* Initialize low level driver */
|
||||
USBD_LL_Init(pdev);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_DeInit
|
||||
* Re-Initialize th device library
|
||||
* @param pdev: device instance
|
||||
* @retval status: status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Set Default State */
|
||||
pdev->dev_state = USBD_STATE_DEFAULT;
|
||||
|
||||
/* Free Class Resources */
|
||||
pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config);
|
||||
|
||||
/* Stop the low level driver */
|
||||
USBD_LL_Stop(pdev);
|
||||
|
||||
/* Initialize low level driver */
|
||||
USBD_LL_DeInit(pdev);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_RegisterClass
|
||||
* Link class driver to Device Core.
|
||||
* @param pDevice : Device Handle
|
||||
* @param pclass: Class handle
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass)
|
||||
{
|
||||
USBD_StatusTypeDef status = USBD_OK;
|
||||
if(pclass != 0)
|
||||
{
|
||||
/* link the class to the USB Device handle */
|
||||
pdev->pClass = pclass;
|
||||
status = USBD_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if (USBD_DEBUG_LEVEL > 1U)
|
||||
USBD_ErrLog("Invalid Class handle");
|
||||
#endif
|
||||
status = USBD_FAIL;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_Start
|
||||
* Start the USB Device Core.
|
||||
* @param pdev: Device Handle
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_Start (USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
|
||||
/* Start the low level driver */
|
||||
USBD_LL_Start(pdev);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_Stop
|
||||
* Stop the USB Device Core.
|
||||
* @param pdev: Device Handle
|
||||
* @retval USBD Status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_Stop (USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Free Class Resources */
|
||||
pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config);
|
||||
|
||||
/* Stop the low level driver */
|
||||
USBD_LL_Stop(pdev);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_RunTestMode
|
||||
* Launch test mode process
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_RunTestMode (USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Prevent unused argument compilation warning */
|
||||
UNUSED(pdev);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_SetClassConfig
|
||||
* Configure device and start the interface
|
||||
* @param pdev: device instance
|
||||
* @param cfgidx: configuration index
|
||||
* @retval status
|
||||
*/
|
||||
|
||||
USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
USBD_StatusTypeDef ret = USBD_FAIL;
|
||||
|
||||
if(pdev->pClass != NULL)
|
||||
{
|
||||
/* Set configuration and Start the Class*/
|
||||
if(pdev->pClass->Init(pdev, cfgidx) == 0U)
|
||||
{
|
||||
ret = USBD_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_ClrClassConfig
|
||||
* Clear current configuration
|
||||
* @param pdev: device instance
|
||||
* @param cfgidx: configuration index
|
||||
* @retval status: USBD_StatusTypeDef
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
/* Clear configuration and De-initialize the Class process*/
|
||||
pdev->pClass->DeInit(pdev, cfgidx);
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief USBD_SetupStage
|
||||
* Handle the setup stage
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup)
|
||||
{
|
||||
USBD_ParseSetupRequest(&pdev->request, psetup);
|
||||
|
||||
pdev->ep0_state = USBD_EP0_SETUP;
|
||||
|
||||
pdev->ep0_data_len = pdev->request.wLength;
|
||||
|
||||
switch (pdev->request.bmRequest & 0x1FU)
|
||||
{
|
||||
case USB_REQ_RECIPIENT_DEVICE:
|
||||
USBD_StdDevReq (pdev, &pdev->request);
|
||||
break;
|
||||
|
||||
case USB_REQ_RECIPIENT_INTERFACE:
|
||||
USBD_StdItfReq(pdev, &pdev->request);
|
||||
break;
|
||||
|
||||
case USB_REQ_RECIPIENT_ENDPOINT:
|
||||
USBD_StdEPReq(pdev, &pdev->request);
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_LL_StallEP(pdev, (pdev->request.bmRequest & 0x80U));
|
||||
break;
|
||||
}
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_DataOutStage
|
||||
* Handle data OUT stage
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint index
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev,
|
||||
uint8_t epnum, uint8_t *pdata)
|
||||
{
|
||||
USBD_EndpointTypeDef *pep;
|
||||
|
||||
if(epnum == 0U)
|
||||
{
|
||||
pep = &pdev->ep_out[0];
|
||||
|
||||
if ( pdev->ep0_state == USBD_EP0_DATA_OUT)
|
||||
{
|
||||
if(pep->rem_length > pep->maxpacket)
|
||||
{
|
||||
pep->rem_length -= pep->maxpacket;
|
||||
|
||||
USBD_CtlContinueRx (pdev,
|
||||
pdata,
|
||||
(uint16_t)MIN(pep->rem_length, pep->maxpacket));
|
||||
}
|
||||
else
|
||||
{
|
||||
if((pdev->pClass->EP0_RxReady != NULL)&&
|
||||
(pdev->dev_state == USBD_STATE_CONFIGURED))
|
||||
{
|
||||
pdev->pClass->EP0_RxReady(pdev);
|
||||
}
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pdev->ep0_state == USBD_EP0_STATUS_OUT)
|
||||
{
|
||||
/*
|
||||
* STATUS PHASE completed, update ep0_state to idle
|
||||
*/
|
||||
pdev->ep0_state = USBD_EP0_IDLE;
|
||||
USBD_LL_StallEP(pdev, 0U);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if((pdev->pClass->DataOut != NULL) &&
|
||||
(pdev->dev_state == USBD_STATE_CONFIGURED))
|
||||
{
|
||||
pdev->pClass->DataOut(pdev, epnum);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* should never be in this condition */
|
||||
return USBD_FAIL;
|
||||
}
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_DataInStage
|
||||
* Handle data in stage
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint index
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev, uint8_t epnum,
|
||||
uint8_t *pdata)
|
||||
{
|
||||
USBD_EndpointTypeDef *pep;
|
||||
|
||||
if(epnum == 0U)
|
||||
{
|
||||
pep = &pdev->ep_in[0];
|
||||
|
||||
if ( pdev->ep0_state == USBD_EP0_DATA_IN)
|
||||
{
|
||||
if(pep->rem_length > pep->maxpacket)
|
||||
{
|
||||
pep->rem_length -= pep->maxpacket;
|
||||
|
||||
USBD_CtlContinueSendData (pdev, pdata, (uint16_t)pep->rem_length);
|
||||
|
||||
/* Prepare endpoint for premature end of transfer */
|
||||
USBD_LL_PrepareReceive (pdev, 0U, NULL, 0U);
|
||||
}
|
||||
else
|
||||
{ /* last packet is MPS multiple, so send ZLP packet */
|
||||
if((pep->total_length % pep->maxpacket == 0U) &&
|
||||
(pep->total_length >= pep->maxpacket) &&
|
||||
(pep->total_length < pdev->ep0_data_len))
|
||||
{
|
||||
USBD_CtlContinueSendData(pdev, NULL, 0U);
|
||||
pdev->ep0_data_len = 0U;
|
||||
|
||||
/* Prepare endpoint for premature end of transfer */
|
||||
USBD_LL_PrepareReceive (pdev, 0U, NULL, 0U);
|
||||
}
|
||||
else
|
||||
{
|
||||
if((pdev->pClass->EP0_TxSent != NULL)&&
|
||||
(pdev->dev_state == USBD_STATE_CONFIGURED))
|
||||
{
|
||||
pdev->pClass->EP0_TxSent(pdev);
|
||||
}
|
||||
USBD_LL_StallEP(pdev, 0x80U);
|
||||
USBD_CtlReceiveStatus(pdev);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((pdev->ep0_state == USBD_EP0_STATUS_IN) ||
|
||||
(pdev->ep0_state == USBD_EP0_IDLE))
|
||||
{
|
||||
USBD_LL_StallEP(pdev, 0x80U);
|
||||
}
|
||||
}
|
||||
|
||||
if (pdev->dev_test_mode == 1U)
|
||||
{
|
||||
USBD_RunTestMode(pdev);
|
||||
pdev->dev_test_mode = 0U;
|
||||
}
|
||||
}
|
||||
else if((pdev->pClass->DataIn != NULL) &&
|
||||
(pdev->dev_state == USBD_STATE_CONFIGURED))
|
||||
{
|
||||
pdev->pClass->DataIn(pdev, epnum);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* should never be in this condition */
|
||||
return USBD_FAIL;
|
||||
}
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_LL_Reset
|
||||
* Handle Reset event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Open EP0 OUT */
|
||||
USBD_LL_OpenEP(pdev, 0x00U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE);
|
||||
pdev->ep_out[0x00U & 0xFU].is_used = 1U;
|
||||
|
||||
pdev->ep_out[0].maxpacket = USB_MAX_EP0_SIZE;
|
||||
|
||||
/* Open EP0 IN */
|
||||
USBD_LL_OpenEP(pdev, 0x80U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE);
|
||||
pdev->ep_in[0x80U & 0xFU].is_used = 1U;
|
||||
|
||||
pdev->ep_in[0].maxpacket = USB_MAX_EP0_SIZE;
|
||||
/* Upon Reset call user call back */
|
||||
pdev->dev_state = USBD_STATE_DEFAULT;
|
||||
pdev->ep0_state = USBD_EP0_IDLE;
|
||||
pdev->dev_config= 0U;
|
||||
pdev->dev_remote_wakeup = 0U;
|
||||
|
||||
if (pdev->pClassData)
|
||||
{
|
||||
pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config);
|
||||
}
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_LL_Reset
|
||||
* Handle Reset event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, USBD_SpeedTypeDef speed)
|
||||
{
|
||||
pdev->dev_speed = speed;
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_Suspend
|
||||
* Handle Suspend event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
pdev->dev_old_state = pdev->dev_state;
|
||||
pdev->dev_state = USBD_STATE_SUSPENDED;
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_Resume
|
||||
* Handle Resume event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
pdev->dev_state = pdev->dev_old_state;
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_SOF
|
||||
* Handle SOF event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
|
||||
USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
if(pdev->dev_state == USBD_STATE_CONFIGURED)
|
||||
{
|
||||
if(pdev->pClass->SOF != NULL)
|
||||
{
|
||||
pdev->pClass->SOF(pdev);
|
||||
}
|
||||
}
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_IsoINIncomplete
|
||||
* Handle iso in incomplete event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
/* Prevent unused arguments compilation warning */
|
||||
UNUSED(pdev);
|
||||
UNUSED(epnum);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_IsoOUTIncomplete
|
||||
* Handle iso out incomplete event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
/* Prevent unused arguments compilation warning */
|
||||
UNUSED(pdev);
|
||||
UNUSED(epnum);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_DevConnected
|
||||
* Handle device connection event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Prevent unused argument compilation warning */
|
||||
UNUSED(pdev);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_DevDisconnected
|
||||
* Handle device disconnection event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Free Class Resources */
|
||||
pdev->dev_state = USBD_STATE_DEFAULT;
|
||||
pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
||||
|
|
@ -0,0 +1,848 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file usbd_req.c
|
||||
* @author MCD Application Team
|
||||
* @brief This file provides the standard USB requests following chapter 9.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* http://www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_ctlreq.h"
|
||||
#include "usbd_ioreq.h"
|
||||
|
||||
|
||||
/** @addtogroup STM32_USBD_STATE_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ
|
||||
* @brief USB standard requests module
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_REQ_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ_Private_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ_Private_Macros
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req);
|
||||
|
||||
static void USBD_SetAddress(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req);
|
||||
|
||||
static void USBD_SetConfig(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req);
|
||||
|
||||
static void USBD_GetConfig(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req);
|
||||
|
||||
static void USBD_GetStatus(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req);
|
||||
|
||||
static void USBD_SetFeature(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req);
|
||||
|
||||
static void USBD_ClrFeature(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req);
|
||||
|
||||
static uint8_t USBD_GetLen(uint8_t *buf);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_REQ_Private_Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @brief USBD_StdDevReq
|
||||
* Handle standard usb device requests
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_StdDevReq (USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req)
|
||||
{
|
||||
USBD_StatusTypeDef ret = USBD_OK;
|
||||
|
||||
switch (req->bmRequest & USB_REQ_TYPE_MASK)
|
||||
{
|
||||
case USB_REQ_TYPE_CLASS:
|
||||
case USB_REQ_TYPE_VENDOR:
|
||||
pdev->pClass->Setup(pdev, req);
|
||||
break;
|
||||
|
||||
case USB_REQ_TYPE_STANDARD:
|
||||
|
||||
switch (req->bRequest)
|
||||
{
|
||||
case USB_REQ_GET_DESCRIPTOR:
|
||||
|
||||
USBD_GetDescriptor (pdev, req);
|
||||
break;
|
||||
|
||||
case USB_REQ_SET_ADDRESS:
|
||||
USBD_SetAddress (pdev, req);
|
||||
break;
|
||||
|
||||
case USB_REQ_SET_CONFIGURATION:
|
||||
USBD_SetConfig (pdev, req);
|
||||
break;
|
||||
|
||||
case USB_REQ_GET_CONFIGURATION:
|
||||
USBD_GetConfig (pdev, req);
|
||||
break;
|
||||
|
||||
case USB_REQ_GET_STATUS:
|
||||
USBD_GetStatus (pdev, req);
|
||||
break;
|
||||
|
||||
|
||||
case USB_REQ_SET_FEATURE:
|
||||
USBD_SetFeature (pdev, req);
|
||||
break;
|
||||
|
||||
case USB_REQ_CLEAR_FEATURE:
|
||||
USBD_ClrFeature (pdev, req);
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_StdItfReq
|
||||
* Handle standard usb interface requests
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_StdItfReq (USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req)
|
||||
{
|
||||
USBD_StatusTypeDef ret = USBD_OK;
|
||||
|
||||
switch (req->bmRequest & USB_REQ_TYPE_MASK)
|
||||
{
|
||||
case USB_REQ_TYPE_CLASS:
|
||||
case USB_REQ_TYPE_VENDOR:
|
||||
case USB_REQ_TYPE_STANDARD:
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_DEFAULT:
|
||||
case USBD_STATE_ADDRESSED:
|
||||
case USBD_STATE_CONFIGURED:
|
||||
|
||||
if (LOBYTE(req->wIndex) <= USBD_MAX_NUM_INTERFACES)
|
||||
{
|
||||
ret = (USBD_StatusTypeDef)pdev->pClass->Setup (pdev, req);
|
||||
|
||||
if ((req->wLength == 0U) && (ret == USBD_OK))
|
||||
{
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_StdEPReq
|
||||
* Handle standard usb endpoint requests
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_StdEPReq (USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req)
|
||||
{
|
||||
|
||||
uint8_t ep_addr;
|
||||
USBD_StatusTypeDef ret = USBD_OK;
|
||||
USBD_EndpointTypeDef *pep;
|
||||
ep_addr = LOBYTE(req->wIndex);
|
||||
|
||||
switch (req->bmRequest & USB_REQ_TYPE_MASK)
|
||||
{
|
||||
|
||||
case USB_REQ_TYPE_CLASS:
|
||||
case USB_REQ_TYPE_VENDOR:
|
||||
pdev->pClass->Setup (pdev, req);
|
||||
break;
|
||||
|
||||
case USB_REQ_TYPE_STANDARD:
|
||||
/* Check if it is a class request */
|
||||
if ((req->bmRequest & 0x60U) == 0x20U)
|
||||
{
|
||||
ret = (USBD_StatusTypeDef)pdev->pClass->Setup (pdev, req);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch (req->bRequest)
|
||||
{
|
||||
|
||||
case USB_REQ_SET_FEATURE :
|
||||
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_ADDRESSED:
|
||||
if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
|
||||
{
|
||||
USBD_LL_StallEP(pdev, ep_addr);
|
||||
USBD_LL_StallEP(pdev, 0x80U);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_STATE_CONFIGURED:
|
||||
if (req->wValue == USB_FEATURE_EP_HALT)
|
||||
{
|
||||
if ((ep_addr != 0x00U) && (ep_addr != 0x80U) && (req->wLength == 0x00U))
|
||||
{
|
||||
USBD_LL_StallEP(pdev, ep_addr);
|
||||
}
|
||||
}
|
||||
USBD_CtlSendStatus(pdev);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_CLEAR_FEATURE :
|
||||
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_ADDRESSED:
|
||||
if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
|
||||
{
|
||||
USBD_LL_StallEP(pdev, ep_addr);
|
||||
USBD_LL_StallEP(pdev, 0x80U);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_STATE_CONFIGURED:
|
||||
if (req->wValue == USB_FEATURE_EP_HALT)
|
||||
{
|
||||
if ((ep_addr & 0x7FU) != 0x00U)
|
||||
{
|
||||
USBD_LL_ClearStallEP(pdev, ep_addr);
|
||||
}
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_GET_STATUS:
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_ADDRESSED:
|
||||
if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU]:\
|
||||
&pdev->ep_out[ep_addr & 0x7FU];
|
||||
|
||||
pep->status = 0x0000U;
|
||||
|
||||
USBD_CtlSendData (pdev, (uint8_t *)(void *)&pep->status, 2U);
|
||||
break;
|
||||
|
||||
case USBD_STATE_CONFIGURED:
|
||||
if((ep_addr & 0x80U) == 0x80U)
|
||||
{
|
||||
if (pdev->ep_in[ep_addr & 0xFU].is_used == 0U)
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pdev->ep_out[ep_addr & 0xFU].is_used == 0U)
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU]:\
|
||||
&pdev->ep_out[ep_addr & 0x7FU];
|
||||
|
||||
if ((ep_addr == 0x00U) || (ep_addr == 0x80U))
|
||||
{
|
||||
pep->status = 0x0000U;
|
||||
}
|
||||
else if(USBD_LL_IsStallEP(pdev, ep_addr))
|
||||
{
|
||||
pep->status = 0x0001U;
|
||||
}
|
||||
else
|
||||
{
|
||||
pep->status = 0x0000U;
|
||||
}
|
||||
|
||||
USBD_CtlSendData (pdev, (uint8_t *)(void *)&pep->status, 2U);
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* @brief USBD_GetDescriptor
|
||||
* Handle Get Descriptor requests
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req)
|
||||
{
|
||||
uint16_t len;
|
||||
uint8_t *pbuf;
|
||||
|
||||
|
||||
switch (req->wValue >> 8)
|
||||
{
|
||||
#if (USBD_LPM_ENABLED == 1U)
|
||||
case USB_DESC_TYPE_BOS:
|
||||
pbuf = pdev->pDesc->GetBOSDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
#endif
|
||||
case USB_DESC_TYPE_DEVICE:
|
||||
pbuf = pdev->pDesc->GetDeviceDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
|
||||
case USB_DESC_TYPE_CONFIGURATION:
|
||||
if(pdev->dev_speed == USBD_SPEED_HIGH )
|
||||
{
|
||||
pbuf = (uint8_t *)pdev->pClass->GetHSConfigDescriptor(&len);
|
||||
pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
|
||||
}
|
||||
else
|
||||
{
|
||||
pbuf = (uint8_t *)pdev->pClass->GetFSConfigDescriptor(&len);
|
||||
pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_DESC_TYPE_STRING:
|
||||
switch ((uint8_t)(req->wValue))
|
||||
{
|
||||
case USBD_IDX_LANGID_STR:
|
||||
pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_MFC_STR:
|
||||
pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_PRODUCT_STR:
|
||||
pbuf = pdev->pDesc->GetProductStrDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_SERIAL_STR:
|
||||
pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_CONFIG_STR:
|
||||
pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_INTERFACE_STR:
|
||||
pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev->dev_speed, &len);
|
||||
break;
|
||||
|
||||
default:
|
||||
#if (USBD_SUPPORT_USER_STRING == 1U)
|
||||
pbuf = pdev->pClass->GetUsrStrDescriptor(pdev, (req->wValue) , &len);
|
||||
break;
|
||||
#else
|
||||
USBD_CtlError(pdev , req);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case USB_DESC_TYPE_DEVICE_QUALIFIER:
|
||||
|
||||
if(pdev->dev_speed == USBD_SPEED_HIGH)
|
||||
{
|
||||
pbuf = (uint8_t *)pdev->pClass->GetDeviceQualifierDescriptor(&len);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev , req);
|
||||
return;
|
||||
}
|
||||
|
||||
case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION:
|
||||
if(pdev->dev_speed == USBD_SPEED_HIGH )
|
||||
{
|
||||
pbuf = (uint8_t *)pdev->pClass->GetOtherSpeedConfigDescriptor(&len);
|
||||
pbuf[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev , req);
|
||||
return;
|
||||
}
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev , req);
|
||||
return;
|
||||
}
|
||||
|
||||
if((len != 0U) && (req->wLength != 0U))
|
||||
{
|
||||
|
||||
len = MIN(len, req->wLength);
|
||||
|
||||
USBD_CtlSendData (pdev, pbuf, len);
|
||||
}
|
||||
|
||||
if(req->wLength == 0U)
|
||||
{
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_SetAddress
|
||||
* Set device address
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
static void USBD_SetAddress(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req)
|
||||
{
|
||||
uint8_t dev_addr;
|
||||
|
||||
if ((req->wIndex == 0U) && (req->wLength == 0U) && (req->wValue < 128U))
|
||||
{
|
||||
dev_addr = (uint8_t)(req->wValue) & 0x7FU;
|
||||
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED)
|
||||
{
|
||||
USBD_CtlError(pdev , req);
|
||||
}
|
||||
else
|
||||
{
|
||||
pdev->dev_address = dev_addr;
|
||||
USBD_LL_SetUSBAddress(pdev, dev_addr);
|
||||
USBD_CtlSendStatus(pdev);
|
||||
|
||||
if (dev_addr != 0U)
|
||||
{
|
||||
pdev->dev_state = USBD_STATE_ADDRESSED;
|
||||
}
|
||||
else
|
||||
{
|
||||
pdev->dev_state = USBD_STATE_DEFAULT;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_SetConfig
|
||||
* Handle Set device configuration request
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
static void USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
|
||||
{
|
||||
static uint8_t cfgidx;
|
||||
|
||||
cfgidx = (uint8_t)(req->wValue);
|
||||
|
||||
if (cfgidx > USBD_MAX_NUM_CONFIGURATION)
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_ADDRESSED:
|
||||
if (cfgidx)
|
||||
{
|
||||
pdev->dev_config = cfgidx;
|
||||
pdev->dev_state = USBD_STATE_CONFIGURED;
|
||||
if(USBD_SetClassConfig(pdev, cfgidx) == USBD_FAIL)
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
return;
|
||||
}
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_STATE_CONFIGURED:
|
||||
if (cfgidx == 0U)
|
||||
{
|
||||
pdev->dev_state = USBD_STATE_ADDRESSED;
|
||||
pdev->dev_config = cfgidx;
|
||||
USBD_ClrClassConfig(pdev, cfgidx);
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
else if (cfgidx != pdev->dev_config)
|
||||
{
|
||||
/* Clear old configuration */
|
||||
USBD_ClrClassConfig(pdev, (uint8_t)pdev->dev_config);
|
||||
|
||||
/* set new configuration */
|
||||
pdev->dev_config = cfgidx;
|
||||
if(USBD_SetClassConfig(pdev, cfgidx) == USBD_FAIL)
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
return;
|
||||
}
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
USBD_ClrClassConfig(pdev, cfgidx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_GetConfig
|
||||
* Handle Get device configuration request
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
|
||||
{
|
||||
if (req->wLength != 1U)
|
||||
{
|
||||
USBD_CtlError(pdev , req);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_DEFAULT:
|
||||
case USBD_STATE_ADDRESSED:
|
||||
pdev->dev_default_config = 0U;
|
||||
USBD_CtlSendData (pdev, (uint8_t *)(void *)&pdev->dev_default_config, 1U);
|
||||
break;
|
||||
|
||||
case USBD_STATE_CONFIGURED:
|
||||
USBD_CtlSendData (pdev, (uint8_t *)(void *)&pdev->dev_config, 1U);
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev , req);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_GetStatus
|
||||
* Handle Get Status request
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
|
||||
{
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_DEFAULT:
|
||||
case USBD_STATE_ADDRESSED:
|
||||
case USBD_STATE_CONFIGURED:
|
||||
if(req->wLength != 0x2U)
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
break;
|
||||
}
|
||||
|
||||
#if ( USBD_SELF_POWERED == 1U)
|
||||
pdev->dev_config_status = USB_CONFIG_SELF_POWERED;
|
||||
#else
|
||||
pdev->dev_config_status = 0U;
|
||||
#endif
|
||||
|
||||
if (pdev->dev_remote_wakeup)
|
||||
{
|
||||
pdev->dev_config_status |= USB_CONFIG_REMOTE_WAKEUP;
|
||||
}
|
||||
|
||||
USBD_CtlSendData (pdev, (uint8_t *)(void *)&pdev->dev_config_status, 2U);
|
||||
break;
|
||||
|
||||
default :
|
||||
USBD_CtlError(pdev , req);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief USBD_SetFeature
|
||||
* Handle Set device feature request
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
static void USBD_SetFeature(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req)
|
||||
{
|
||||
|
||||
if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
|
||||
{
|
||||
pdev->dev_remote_wakeup = 1U;
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief USBD_ClrFeature
|
||||
* Handle clear device feature request
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval status
|
||||
*/
|
||||
static void USBD_ClrFeature(USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req)
|
||||
{
|
||||
switch (pdev->dev_state)
|
||||
{
|
||||
case USBD_STATE_DEFAULT:
|
||||
case USBD_STATE_ADDRESSED:
|
||||
case USBD_STATE_CONFIGURED:
|
||||
if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
|
||||
{
|
||||
pdev->dev_remote_wakeup = 0U;
|
||||
USBD_CtlSendStatus(pdev);
|
||||
}
|
||||
break;
|
||||
|
||||
default :
|
||||
USBD_CtlError(pdev , req);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_ParseSetupRequest
|
||||
* Copy buffer into setup structure
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata)
|
||||
{
|
||||
req->bmRequest = *(uint8_t *) (pdata);
|
||||
req->bRequest = *(uint8_t *) (pdata + 1);
|
||||
req->wValue = SWAPBYTE (pdata + 2);
|
||||
req->wIndex = SWAPBYTE (pdata + 4);
|
||||
req->wLength = SWAPBYTE (pdata + 6);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CtlError
|
||||
* Handle USB low level Error
|
||||
* @param pdev: device instance
|
||||
* @param req: usb request
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
void USBD_CtlError( USBD_HandleTypeDef *pdev ,
|
||||
USBD_SetupReqTypedef *req)
|
||||
{
|
||||
USBD_LL_StallEP(pdev , 0x80U);
|
||||
USBD_LL_StallEP(pdev , 0U);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief USBD_GetString
|
||||
* Convert Ascii string into unicode one
|
||||
* @param desc : descriptor buffer
|
||||
* @param unicode : Formatted string buffer (unicode)
|
||||
* @param len : descriptor length
|
||||
* @retval None
|
||||
*/
|
||||
void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len)
|
||||
{
|
||||
uint8_t idx = 0U;
|
||||
|
||||
if (desc != NULL)
|
||||
{
|
||||
*len = (uint16_t)USBD_GetLen(desc) * 2U + 2U;
|
||||
unicode[idx++] = *(uint8_t *)(void *)len;
|
||||
unicode[idx++] = USB_DESC_TYPE_STRING;
|
||||
|
||||
while (*desc != '\0')
|
||||
{
|
||||
unicode[idx++] = *desc++;
|
||||
unicode[idx++] = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_GetLen
|
||||
* return the string length
|
||||
* @param buf : pointer to the ascii string buffer
|
||||
* @retval string length
|
||||
*/
|
||||
static uint8_t USBD_GetLen(uint8_t *buf)
|
||||
{
|
||||
uint8_t len = 0U;
|
||||
|
||||
while (*buf != '\0')
|
||||
{
|
||||
len++;
|
||||
buf++;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,449 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usbd_desc.c
|
||||
* @version : v2.0_Cube
|
||||
* @brief : This file implements the USB device descriptors.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_conf.h"
|
||||
|
||||
/* USER CODE BEGIN INCLUDE */
|
||||
|
||||
/* USER CODE END INCLUDE */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup USBD_DESC
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Private_TypesDefinitions USBD_DESC_Private_TypesDefinitions
|
||||
* @brief Private types.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN PRIVATE_TYPES */
|
||||
|
||||
/* USER CODE END PRIVATE_TYPES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
|
||||
* @brief Private defines.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#define USBD_VID 0x0483
|
||||
#define USBD_PID_FS 0x564E
|
||||
#define USBD_LANGID_STRING 0x0409
|
||||
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
|
||||
#define USBD_PRODUCT_STRING_FS "VNA"
|
||||
#define USBD_CONFIGURATION_STRING_FS "CustomUSBDevice Config"
|
||||
#define USBD_INTERFACE_STRING_FS "CustomUSBDevice Interface"
|
||||
|
||||
#define USB_SIZ_BOS_DESC 0x0C
|
||||
|
||||
/* USER CODE BEGIN PRIVATE_DEFINES */
|
||||
|
||||
/* USER CODE END PRIVATE_DEFINES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/** @defgroup USBD_DESC_Private_Macros USBD_DESC_Private_Macros
|
||||
* @brief Private macros.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN PRIVATE_MACRO */
|
||||
|
||||
/* USER CODE END PRIVATE_MACRO */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
|
||||
* @brief Private functions declaration.
|
||||
* @{
|
||||
*/
|
||||
|
||||
static void Get_SerialNum(void);
|
||||
static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
|
||||
* @brief Private functions declaration for FS.
|
||||
* @{
|
||||
*/
|
||||
|
||||
uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
|
||||
#ifdef USBD_SUPPORT_USER_STRING_DESC
|
||||
uint8_t * USBD_FS_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
|
||||
#endif /* USBD_SUPPORT_USER_STRING_DESC */
|
||||
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
|
||||
* @brief Private variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_DescriptorsTypeDef FS_Desc =
|
||||
{
|
||||
USBD_FS_DeviceDescriptor
|
||||
, USBD_FS_LangIDStrDescriptor
|
||||
, USBD_FS_ManufacturerStrDescriptor
|
||||
, USBD_FS_ProductStrDescriptor
|
||||
, USBD_FS_SerialStrDescriptor
|
||||
, USBD_FS_ConfigStrDescriptor
|
||||
, USBD_FS_InterfaceStrDescriptor
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
, USBD_FS_USR_BOSDescriptor
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
};
|
||||
|
||||
#if defined ( __ICCARM__ ) /* IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
/** USB standard device descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
|
||||
{
|
||||
0x12, /*bLength */
|
||||
USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
0x01, /*bcdUSB */ /* changed to USB version 2.01
|
||||
in order to support LPM L1 suspend
|
||||
resume test of USBCV3.0*/
|
||||
#else
|
||||
0x00, /*bcdUSB */
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
0x02,
|
||||
0xFF, /*bDeviceClass*/
|
||||
0x00, /*bDeviceSubClass*/
|
||||
0x00, /*bDeviceProtocol*/
|
||||
USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
|
||||
LOBYTE(USBD_VID), /*idVendor*/
|
||||
HIBYTE(USBD_VID), /*idVendor*/
|
||||
LOBYTE(USBD_PID_FS), /*idProduct*/
|
||||
HIBYTE(USBD_PID_FS), /*idProduct*/
|
||||
0x00, /*bcdDevice rel. 2.00*/
|
||||
0x02,
|
||||
USBD_IDX_MFC_STR, /*Index of manufacturer string*/
|
||||
USBD_IDX_PRODUCT_STR, /*Index of product string*/
|
||||
USBD_IDX_SERIAL_STR, /*Index of serial number string*/
|
||||
USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
|
||||
};
|
||||
|
||||
/* USB_DeviceDescriptor */
|
||||
/** BOS descriptor. */
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
#if defined ( __ICCARM__ ) /* IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
|
||||
{
|
||||
0x5,
|
||||
USB_DESC_TYPE_BOS,
|
||||
0xC,
|
||||
0x0,
|
||||
0x1, /* 1 device capability*/
|
||||
/* device capability*/
|
||||
0x7,
|
||||
USB_DEVICE_CAPABITY_TYPE,
|
||||
0x2,
|
||||
0x2, /* LPM capability bit set*/
|
||||
0x0,
|
||||
0x0,
|
||||
0x0
|
||||
};
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
|
||||
* @brief Private variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#if defined ( __ICCARM__ ) /* IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
|
||||
/** USB lang indentifier descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
|
||||
{
|
||||
USB_LEN_LANGID_STR_DESC,
|
||||
USB_DESC_TYPE_STRING,
|
||||
LOBYTE(USBD_LANGID_STRING),
|
||||
HIBYTE(USBD_LANGID_STRING)
|
||||
};
|
||||
|
||||
#if defined ( __ICCARM__ ) /* IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
/* Internal string descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
|
||||
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif
|
||||
__ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
|
||||
USB_SIZ_STRING_SERIAL,
|
||||
USB_DESC_TYPE_STRING,
|
||||
};
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
|
||||
* @brief Private functions.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Return the device descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_DeviceDesc);
|
||||
return USBD_FS_DeviceDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the LangID string descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_LangIDDesc);
|
||||
return USBD_LangIDDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the product string descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if(speed == 0)
|
||||
{
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the manufacturer string descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the serial number string descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = USB_SIZ_STRING_SERIAL;
|
||||
|
||||
/* Update the serial number string descriptor with the data from the unique
|
||||
* ID */
|
||||
Get_SerialNum();
|
||||
|
||||
return (uint8_t *) USBD_StringSerial;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the configuration string descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if(speed == USBD_SPEED_HIGH)
|
||||
{
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the interface string descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if(speed == 0)
|
||||
{
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
/**
|
||||
* @brief Return the BOS descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_BOSDesc);
|
||||
return (uint8_t*)USBD_FS_BOSDesc;
|
||||
}
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/**
|
||||
* @brief Create the serial number string descriptor
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void Get_SerialNum(void)
|
||||
{
|
||||
uint32_t deviceserial0, deviceserial1, deviceserial2;
|
||||
|
||||
deviceserial0 = *(uint32_t *) DEVICE_ID1;
|
||||
deviceserial1 = *(uint32_t *) DEVICE_ID2;
|
||||
deviceserial2 = *(uint32_t *) DEVICE_ID3;
|
||||
|
||||
deviceserial0 += deviceserial2;
|
||||
|
||||
if (deviceserial0 != 0)
|
||||
{
|
||||
IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
|
||||
IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert Hex 32Bits value into char
|
||||
* @param value: value to convert
|
||||
* @param pbuf: pointer to the buffer
|
||||
* @param len: buffer length
|
||||
* @retval None
|
||||
*/
|
||||
static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
for (idx = 0; idx < len; idx++)
|
||||
{
|
||||
if (((value >> 28)) < 0xA)
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + 'A' - 10;
|
||||
}
|
||||
|
||||
value = value << 4;
|
||||
|
||||
pbuf[2 * idx + 1] = 0;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
|
@ -0,0 +1,216 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file usbd_ioreq.c
|
||||
* @author MCD Application Team
|
||||
* @brief This file provides the IO requests APIs for control endpoints.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* http://www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_ioreq.h"
|
||||
|
||||
/** @addtogroup STM32_USB_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ
|
||||
* @brief control I/O requests module
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_IOREQ_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ_Private_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ_Private_Macros
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/** @defgroup USBD_IOREQ_Private_Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief USBD_CtlSendData
|
||||
* send data on the ctl pipe
|
||||
* @param pdev: device instance
|
||||
* @param buff: pointer to data buffer
|
||||
* @param len: length of data to be sent
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_CtlSendData (USBD_HandleTypeDef *pdev, uint8_t *pbuf,
|
||||
uint16_t len)
|
||||
{
|
||||
/* Set EP0 State */
|
||||
pdev->ep0_state = USBD_EP0_DATA_IN;
|
||||
pdev->ep_in[0].total_length = len;
|
||||
pdev->ep_in[0].rem_length = len;
|
||||
|
||||
/* Start the transfer */
|
||||
USBD_LL_Transmit (pdev, 0x00U, pbuf, len);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CtlContinueSendData
|
||||
* continue sending data on the ctl pipe
|
||||
* @param pdev: device instance
|
||||
* @param buff: pointer to data buffer
|
||||
* @param len: length of data to be sent
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_CtlContinueSendData (USBD_HandleTypeDef *pdev,
|
||||
uint8_t *pbuf, uint16_t len)
|
||||
{
|
||||
/* Start the next transfer */
|
||||
USBD_LL_Transmit (pdev, 0x00U, pbuf, len);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CtlPrepareRx
|
||||
* receive data on the ctl pipe
|
||||
* @param pdev: device instance
|
||||
* @param buff: pointer to data buffer
|
||||
* @param len: length of data to be received
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_CtlPrepareRx (USBD_HandleTypeDef *pdev, uint8_t *pbuf,
|
||||
uint16_t len)
|
||||
{
|
||||
/* Set EP0 State */
|
||||
pdev->ep0_state = USBD_EP0_DATA_OUT;
|
||||
pdev->ep_out[0].total_length = len;
|
||||
pdev->ep_out[0].rem_length = len;
|
||||
|
||||
/* Start the transfer */
|
||||
USBD_LL_PrepareReceive (pdev, 0U, pbuf, len);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CtlContinueRx
|
||||
* continue receive data on the ctl pipe
|
||||
* @param pdev: device instance
|
||||
* @param buff: pointer to data buffer
|
||||
* @param len: length of data to be received
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_CtlContinueRx (USBD_HandleTypeDef *pdev, uint8_t *pbuf,
|
||||
uint16_t len)
|
||||
{
|
||||
USBD_LL_PrepareReceive(pdev, 0U, pbuf, len);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CtlSendStatus
|
||||
* send zero lzngth packet on the ctl pipe
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_CtlSendStatus (USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Set EP0 State */
|
||||
pdev->ep0_state = USBD_EP0_STATUS_IN;
|
||||
|
||||
/* Start the transfer */
|
||||
USBD_LL_Transmit(pdev, 0x00U, NULL, 0U);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CtlReceiveStatus
|
||||
* receive zero lzngth packet on the ctl pipe
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
USBD_StatusTypeDef USBD_CtlReceiveStatus (USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
/* Set EP0 State */
|
||||
pdev->ep0_state = USBD_EP0_STATUS_OUT;
|
||||
|
||||
/* Start the transfer */
|
||||
USBD_LL_PrepareReceive (pdev, 0U, NULL, 0U);
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_GetRxCount
|
||||
* returns the received data length
|
||||
* @param pdev: device instance
|
||||
* @param ep_addr: endpoint address
|
||||
* @retval Rx Data blength
|
||||
*/
|
||||
uint32_t USBD_GetRxCount (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
|
||||
{
|
||||
return USBD_LL_GetRxDataSize(pdev, ep_addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
195
Software/VNA_embedded/Application/Drivers/USB/usb.c
Normal file
195
Software/VNA_embedded/Application/Drivers/USB/usb.c
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
#include "usb.h"
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_core.h"
|
||||
|
||||
USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
#define EP_DATA_IN_ADDRESS 0x81
|
||||
#define EP_DATA_OUT_ADDRESS 0x01
|
||||
#define EP_LOG_IN_ADDRESS 0x82
|
||||
|
||||
static uint8_t USBD_Class_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
static uint8_t USBD_Class_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
static uint8_t USBD_Class_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
static uint8_t USBD_Class_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
static uint8_t *USBD_Class_GetFSCfgDesc (uint16_t *length);
|
||||
static uint8_t *USBD_Class_GetDeviceQualifierDescriptor (uint16_t *length);
|
||||
|
||||
static usbd_callback_t cb;
|
||||
static uint8_t usb_receive_buffer[1024];
|
||||
static bool data_transmission_active = false;
|
||||
static bool log_transmission_active = true;
|
||||
|
||||
USBD_ClassTypeDef USBD_ClassDriver =
|
||||
{
|
||||
USBD_Class_Init,
|
||||
USBD_Class_DeInit,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
USBD_Class_DataIn,
|
||||
USBD_Class_DataOut,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
USBD_Class_GetFSCfgDesc,
|
||||
NULL,
|
||||
USBD_Class_GetDeviceQualifierDescriptor,
|
||||
};
|
||||
|
||||
/* USB Standard Device Descriptor */
|
||||
__ALIGN_BEGIN static uint8_t USBD_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
|
||||
{
|
||||
USB_LEN_DEV_QUALIFIER_DESC,
|
||||
USB_DESC_TYPE_DEVICE_QUALIFIER,
|
||||
0x00,
|
||||
0x02,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x40,
|
||||
0x01,
|
||||
0x00,
|
||||
};
|
||||
|
||||
#define USB_CONFIG_DESC_SIZ 39
|
||||
|
||||
/* USB CDC device Configuration Descriptor */
|
||||
__ALIGN_BEGIN uint8_t USBD_CfgFSDesc[USB_CONFIG_DESC_SIZ] __ALIGN_END =
|
||||
{
|
||||
0x09, /* bLength: Configuation Descriptor size */
|
||||
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
|
||||
USB_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */
|
||||
0x00,
|
||||
0x01, /*bNumInterfaces: 1 interface*/
|
||||
0x01, /*bConfigurationValue: Configuration value*/
|
||||
0x00, /*iConfiguration */
|
||||
0xC0, /*bmAttributes: bus powered and Supports Remote Wakeup */
|
||||
0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
|
||||
/* Interface */
|
||||
0x09, /* bLength */
|
||||
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
|
||||
0x00, /* bInterfaceNumber */
|
||||
0x00, /* bAlternateSetting */
|
||||
0x03, /* bNumEndpoints */
|
||||
0xFF, /* bInterfaceClass */
|
||||
0x00, /* bInterfaceSubClass */
|
||||
0x00, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface */
|
||||
/* Endpoint Data OUT */
|
||||
0x07, /* bLength */
|
||||
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType */
|
||||
EP_DATA_OUT_ADDRESS, /* bEndpointAddress */
|
||||
0x02, /* bmAttributes */
|
||||
LOBYTE(USB_FS_MAX_PACKET_SIZE), /* wMaxPacketSize */
|
||||
HIBYTE(USB_FS_MAX_PACKET_SIZE),
|
||||
0x00, /* bInterval */
|
||||
/* Endpoint Data IN */
|
||||
0x07, /* bLength */
|
||||
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType */
|
||||
EP_DATA_IN_ADDRESS, /* bEndpointAddress */
|
||||
0x02, /* bmAttributes */
|
||||
LOBYTE(USB_FS_MAX_PACKET_SIZE), /* wMaxPacketSize */
|
||||
HIBYTE(USB_FS_MAX_PACKET_SIZE),
|
||||
0x00, /* bInterval */
|
||||
/* Endpoint logging IN */
|
||||
0x07, /* bLength */
|
||||
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType */
|
||||
EP_LOG_IN_ADDRESS, /* bEndpointAddress */
|
||||
0x02, /* bmAttributes */
|
||||
LOBYTE(USB_FS_MAX_PACKET_SIZE), /* wMaxPacketSize */
|
||||
HIBYTE(USB_FS_MAX_PACKET_SIZE),
|
||||
0x00 /* bInterval */
|
||||
};
|
||||
|
||||
static uint8_t USBD_Class_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
// Open endpoints and start reception
|
||||
USBD_LL_OpenEP(pdev, EP_DATA_IN_ADDRESS, USBD_EP_TYPE_BULK, USB_FS_MAX_PACKET_SIZE);
|
||||
USBD_LL_OpenEP(pdev, EP_DATA_OUT_ADDRESS, USBD_EP_TYPE_BULK, USB_FS_MAX_PACKET_SIZE);
|
||||
USBD_LL_OpenEP(pdev, EP_LOG_IN_ADDRESS, USBD_EP_TYPE_BULK, USB_FS_MAX_PACKET_SIZE);
|
||||
USBD_LL_PrepareReceive(pdev, EP_DATA_OUT_ADDRESS, usb_receive_buffer, USB_FS_MAX_PACKET_SIZE);
|
||||
return USBD_OK;
|
||||
}
|
||||
static uint8_t USBD_Class_DeInit(USBD_HandleTypeDef *pdev,
|
||||
uint8_t cfgidx)
|
||||
{
|
||||
USBD_LL_CloseEP(pdev, EP_DATA_IN_ADDRESS);
|
||||
USBD_LL_CloseEP(pdev, EP_DATA_OUT_ADDRESS);
|
||||
USBD_LL_CloseEP(pdev, EP_LOG_IN_ADDRESS);
|
||||
return USBD_OK;
|
||||
}
|
||||
static uint8_t USBD_Class_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) {
|
||||
// A bulk transfer is complete when the endpoint does on of the following:
|
||||
// - Has transferred exactly the amount of data expected
|
||||
// - Transfers a packet with a payload size less than wMaxPacketSize or transfers a zero-length packet
|
||||
if (pdev->ep_in[epnum].total_length
|
||||
&& !(pdev->ep_in[epnum].total_length % USB_FS_MAX_PACKET_SIZE)) {
|
||||
pdev->ep_in[epnum].total_length = 0;
|
||||
USBD_LL_Transmit(pdev, epnum, NULL, 0);
|
||||
} else {
|
||||
if(epnum == (EP_DATA_IN_ADDRESS & 0x7F)) {
|
||||
data_transmission_active = false;
|
||||
} else {
|
||||
log_transmission_active = false;
|
||||
}
|
||||
}
|
||||
return USBD_OK;
|
||||
}
|
||||
static uint8_t USBD_Class_DataOut(USBD_HandleTypeDef *pdev,
|
||||
uint8_t epnum)
|
||||
{
|
||||
if(cb) {
|
||||
cb(usb_receive_buffer, USBD_LL_GetRxDataSize (pdev, epnum));
|
||||
}
|
||||
USBD_LL_PrepareReceive(pdev, EP_DATA_OUT_ADDRESS, usb_receive_buffer, USB_FS_MAX_PACKET_SIZE);
|
||||
return USBD_OK;
|
||||
}
|
||||
static uint8_t *USBD_Class_GetFSCfgDesc(uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_CfgFSDesc);
|
||||
return USBD_CfgFSDesc;
|
||||
}
|
||||
static uint8_t *USBD_Class_GetDeviceQualifierDescriptor(uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_DeviceQualifierDesc);
|
||||
return USBD_DeviceQualifierDesc;
|
||||
}
|
||||
|
||||
void usb_init(usbd_callback_t callback) {
|
||||
cb = callback;
|
||||
USBD_Init(&hUsbDeviceFS, &FS_Desc, 0);
|
||||
USBD_RegisterClass(&hUsbDeviceFS, &USBD_ClassDriver);
|
||||
USBD_Start(&hUsbDeviceFS);
|
||||
HAL_NVIC_EnableIRQ(USB_HP_IRQn);
|
||||
HAL_NVIC_EnableIRQ(USB_LP_IRQn);
|
||||
}
|
||||
|
||||
bool usb_transmit(const uint8_t *data, uint16_t length) {
|
||||
static bool first = true;
|
||||
if(first) {
|
||||
log_transmission_active = false;
|
||||
first = false;
|
||||
}
|
||||
if(!data_transmission_active) {
|
||||
data_transmission_active = true;
|
||||
hUsbDeviceFS.ep_in[EP_DATA_IN_ADDRESS & 0x7F].total_length = length;
|
||||
return USBD_LL_Transmit(&hUsbDeviceFS, EP_DATA_IN_ADDRESS, (uint8_t*) data, length) == USBD_OK;
|
||||
} else {
|
||||
// already have an ongoing transmission
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void usb_log(const char *log, uint16_t length) {
|
||||
if(!log_transmission_active) {
|
||||
static uint8_t buffer[256];
|
||||
memcpy(buffer, log, length);
|
||||
log_transmission_active = true;
|
||||
hUsbDeviceFS.ep_in[EP_LOG_IN_ADDRESS & 0x7F].total_length = length;
|
||||
USBD_LL_Transmit(&hUsbDeviceFS, EP_LOG_IN_ADDRESS, buffer, length);
|
||||
} else {
|
||||
// still busy, unable to send log
|
||||
}
|
||||
}
|
||||
29
Software/VNA_embedded/Application/Drivers/USB/usb.h
Normal file
29
Software/VNA_embedded/Application/Drivers/USB/usb.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* usb.h
|
||||
*
|
||||
* Created on: Aug 12, 2020
|
||||
* Author: jan
|
||||
*/
|
||||
|
||||
#ifndef DRIVERS_USB_USB_H_
|
||||
#define DRIVERS_USB_USB_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef void(*usbd_callback_t)(const uint8_t *buf, uint16_t len);
|
||||
|
||||
void usb_init(usbd_callback_t callback);
|
||||
bool usb_transmit(const uint8_t *data, uint16_t length);
|
||||
void usb_log(const char *log, uint16_t length);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DRIVERS_USB_USB_H_ */
|
||||
44
Software/VNA_embedded/Application/Drivers/algorithm.cpp
Normal file
44
Software/VNA_embedded/Application/Drivers/algorithm.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include "algorithm.hpp"
|
||||
|
||||
#include "stm.hpp"
|
||||
#include <cmath>
|
||||
|
||||
Algorithm::RationalApproximation Algorithm::BestRationalApproximation(float ratio, uint32_t max_denom) {
|
||||
RationalApproximation result;
|
||||
uint32_t a = 0, b = 1, c = 1, d = 1;
|
||||
while (b + d <= max_denom) {
|
||||
auto mediant = (float) (a + c) / (b + d);
|
||||
if (ratio == mediant) {
|
||||
if (b + d <= max_denom) {
|
||||
result.num = a + c;
|
||||
result.denom = b + d;
|
||||
return result;
|
||||
} else if (d > b) {
|
||||
result.num = c;
|
||||
result.denom = d;
|
||||
return result;
|
||||
} else {
|
||||
result.num = a;
|
||||
result.denom = b;
|
||||
return result;
|
||||
}
|
||||
} else if (ratio > mediant) {
|
||||
a = a + c;
|
||||
b = b + d;
|
||||
} else {
|
||||
c = a + c;
|
||||
d = b + d;
|
||||
}
|
||||
}
|
||||
// check which of the two is the better solution
|
||||
float dev_ab = (float) a / b - ratio;
|
||||
float dev_cd = (float) c / d - ratio;
|
||||
if(fabs(dev_cd) < fabs(dev_ab)) {
|
||||
result.num = c;
|
||||
result.denom = d;
|
||||
} else {
|
||||
result.num = a;
|
||||
result.denom = b;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
14
Software/VNA_embedded/Application/Drivers/algorithm.hpp
Normal file
14
Software/VNA_embedded/Application/Drivers/algorithm.hpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Algorithm {
|
||||
|
||||
using RationalApproximation = struct _rationalapproximation {
|
||||
uint32_t num;
|
||||
uint32_t denom;
|
||||
};
|
||||
|
||||
RationalApproximation BestRationalApproximation(float ratio, uint32_t max_denom);
|
||||
|
||||
}
|
||||
16
Software/VNA_embedded/Application/Drivers/delay.cpp
Normal file
16
Software/VNA_embedded/Application/Drivers/delay.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include "delay.hpp"
|
||||
|
||||
#include "stm.hpp"
|
||||
|
||||
void Delay::ms(uint32_t t) {
|
||||
while(t--) {
|
||||
us(1000);
|
||||
}
|
||||
}
|
||||
void Delay::us(uint32_t t) {
|
||||
TIM1->CNT = 0;
|
||||
TIM1->CR1 |= TIM_CR1_CEN;
|
||||
while (TIM1->CNT < t)
|
||||
;
|
||||
TIM1->CR1 &= ~TIM_CR1_CEN;
|
||||
}
|
||||
10
Software/VNA_embedded/Application/Drivers/delay.hpp
Normal file
10
Software/VNA_embedded/Application/Drivers/delay.hpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Delay {
|
||||
|
||||
void ms(uint32_t t);
|
||||
void us(uint32_t t);
|
||||
|
||||
}
|
||||
408
Software/VNA_embedded/Application/Drivers/max2871.cpp
Normal file
408
Software/VNA_embedded/Application/Drivers/max2871.cpp
Normal file
|
|
@ -0,0 +1,408 @@
|
|||
#include "max2871.hpp"
|
||||
#include <string.h>
|
||||
#include <algorithm.hpp>
|
||||
|
||||
#include "delay.hpp"
|
||||
#include <cmath>
|
||||
#define LOG_LEVEL LOG_LEVEL_WARN
|
||||
#define LOG_MODULE "MAX2871"
|
||||
#include "Log.h"
|
||||
|
||||
bool MAX2871::Init() {
|
||||
return Init(10000000, true, 1, false);
|
||||
}
|
||||
|
||||
bool MAX2871::Init(uint32_t f_ref, bool doubler, uint16_t r, bool div2) {
|
||||
for (uint8_t i = 0; i < 6; i++) {
|
||||
regs[i] = 0;
|
||||
}
|
||||
|
||||
ChipEnable(false);
|
||||
RFEnable(false);
|
||||
|
||||
SetReference(f_ref, doubler, r, div2);
|
||||
|
||||
// non-inverting loop filter
|
||||
regs[2] |= (1UL << 6);
|
||||
// select digital lock detect
|
||||
regs[5] |= (0x1UL << 22);
|
||||
|
||||
// fundamental VCO feedback
|
||||
regs[4] |= (1UL << 23);
|
||||
|
||||
// reserved, set to 0x03
|
||||
regs[4] |= (0x3UL << 29);
|
||||
|
||||
// enable double buffering for register 4
|
||||
regs[2] |= (1UL << 13);
|
||||
|
||||
// automatically switch to integer mode if F = 0
|
||||
regs[5] |= (1UL << 24);
|
||||
|
||||
SetMode(Mode::LowSpur2);
|
||||
// for all other CP modes the PLL reports unlock condition (output signal appears to be locked)
|
||||
SetCPMode(CPMode::CP20);
|
||||
SetCPCurrent(15);
|
||||
SetFrequency(1000000000);
|
||||
|
||||
// initial register write according to datasheet timing
|
||||
ChipEnable(true);
|
||||
Write(5, regs[5]);
|
||||
Delay::ms(20);
|
||||
Write(4, regs[4]);
|
||||
Write(3, regs[3]);
|
||||
Write(2, regs[2]);
|
||||
Write(1, regs[1]);
|
||||
Write(0, regs[0]);
|
||||
Write(5, regs[5]);
|
||||
Delay::ms(20);
|
||||
Write(4, regs[4]);
|
||||
Write(3, regs[3]);
|
||||
Write(2, regs[2]);
|
||||
Write(1, regs[1]);
|
||||
Write(0, regs[0]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MAX2871::ChipEnable(bool on) {
|
||||
if(!CE) {
|
||||
return;
|
||||
}
|
||||
if (on) {
|
||||
CE->BSRR = CEpin;
|
||||
} else {
|
||||
CE->BSRR = CEpin << 16;
|
||||
}
|
||||
}
|
||||
|
||||
void MAX2871::RFEnable(bool on) {
|
||||
if(!RF_EN) {
|
||||
return;
|
||||
}
|
||||
if (on) {
|
||||
RF_EN->BSRR = RF_ENpin;
|
||||
Read();
|
||||
} else {
|
||||
RF_EN->BSRR = RF_ENpin << 16;
|
||||
}
|
||||
}
|
||||
|
||||
bool MAX2871::Locked() {
|
||||
return LD->IDR & LDpin;
|
||||
}
|
||||
|
||||
void MAX2871::SetPowerOutA(Power p, bool enabled) {
|
||||
// only set power of port A
|
||||
regs[4] &= ~0x38;
|
||||
regs[4] |= ((uint16_t) p << 3);
|
||||
if (enabled) {
|
||||
regs[4] |= 0x20;
|
||||
}
|
||||
}
|
||||
|
||||
void MAX2871::SetPowerOutB(Power p, bool enabled) {
|
||||
// only set power of port B
|
||||
regs[4] &= ~0x1C0;
|
||||
regs[4] |= ((uint16_t) p << 6);
|
||||
if (enabled) {
|
||||
regs[4] |= 0x100;
|
||||
}
|
||||
}
|
||||
|
||||
void MAX2871::SetMode(Mode m) {
|
||||
regs[2] &= ~0x60000000;
|
||||
regs[2] |= ((uint32_t) m << 29);
|
||||
}
|
||||
|
||||
void MAX2871::SetCPMode(CPMode m) {
|
||||
regs[1] &= ~0x60000000;
|
||||
regs[1] |= ((uint32_t) m << 29);
|
||||
}
|
||||
|
||||
void MAX2871::SetCPCurrent(uint8_t mA) {
|
||||
if(mA > 15) {
|
||||
LOG_WARN("Clipping charge pump current to 15mA");
|
||||
mA = 15;
|
||||
}
|
||||
regs[2] &= ~0x1E00;
|
||||
regs[2] |= ((uint16_t) mA << 9);
|
||||
}
|
||||
|
||||
bool MAX2871::SetFrequency(uint64_t f) {
|
||||
if (f < 23500000 || f > MaxFreq) {
|
||||
LOG_ERR("Frequency must be between 23.5MHz and 6GHz");
|
||||
return false;
|
||||
}
|
||||
LOG_DEBUG("Setting frequency to %lu%06luHz...", (uint32_t ) (f / 1000000),
|
||||
(uint32_t ) (f % 1000000));
|
||||
// select divider
|
||||
uint64_t f_vco = f;
|
||||
uint8_t div = 0;
|
||||
if (f < 46875000) {
|
||||
div = 0x07;
|
||||
f_vco *= 128;
|
||||
} else if (f < 93750000) {
|
||||
div = 0x06;
|
||||
f_vco *= 64;
|
||||
} else if (f < 187500000) {
|
||||
div = 0x05;
|
||||
f_vco *= 32;
|
||||
} else if (f < 375000000) {
|
||||
div = 0x04;
|
||||
f_vco *= 16;
|
||||
} else if (f < 750000000) {
|
||||
div = 0x03;
|
||||
f_vco *= 8;
|
||||
} else if (f < 1500000000) {
|
||||
div = 0x02;
|
||||
f_vco *= 4;
|
||||
} else if (f < 3000000000) {
|
||||
div = 0x01;
|
||||
f_vco *= 2;
|
||||
}
|
||||
LOG_DEBUG("F_VCO: %lu%06luHz",
|
||||
(uint32_t ) (f_vco / 1000000), (uint32_t ) (f_vco % 1000000));
|
||||
if (gotVCOMap) {
|
||||
// manual VCO selection for lock time improvement
|
||||
uint16_t compare = f_vco / 100000;
|
||||
uint8_t vco = 0;
|
||||
for (; vco < 64; vco++) {
|
||||
if (VCOmax[vco] >= compare) {
|
||||
break;
|
||||
}
|
||||
} LOG_DEBUG("Manually selected VCO %d", vco);
|
||||
regs[3] &= ~0xFC000000;
|
||||
regs[3] |= (uint32_t) vco << 26;
|
||||
}
|
||||
uint16_t N = f_vco / f_PFD;
|
||||
if (N < 19 || N > 4091) {
|
||||
LOG_ERR("Invalid N value, should be between 19 and 4091, got %lu", N);
|
||||
return false;
|
||||
}
|
||||
uint32_t rem_f = f_vco - N * f_PFD;
|
||||
LOG_DEBUG("Remaining fractional frequency: %lu", rem_f);
|
||||
LOG_DEBUG("Looking for best fractional match");
|
||||
float fraction = (float) rem_f / f_PFD;
|
||||
|
||||
auto approx = Algorithm::BestRationalApproximation(fraction, 4095);
|
||||
|
||||
int32_t rem_approx = ((uint64_t) f_PFD * approx.num) / approx.denom;
|
||||
if(rem_approx != rem_f) {
|
||||
LOG_WARN("Best match is F=%u/M=%u, deviation of %luHz",
|
||||
approx.num, approx.denom, abs(rem_f - rem_approx));
|
||||
}
|
||||
|
||||
uint64_t f_set = (uint64_t) N * f_PFD + (f_PFD * approx.num) / approx.denom;
|
||||
f_set /= (1UL << div);
|
||||
|
||||
// write values to registers
|
||||
regs[4] &= ~0x00700000;
|
||||
regs[4] |= ((uint32_t) div << 20);
|
||||
regs[0] &= ~0x7FFFFFF8;
|
||||
regs[0] |= ((uint32_t) N << 15) | ((uint32_t) approx.num << 3);
|
||||
regs[1] &= ~0x00007FF8;
|
||||
regs[1] |= ((uint32_t) approx.denom << 3);
|
||||
|
||||
LOG_DEBUG("Set frequency to %lu%06luHz...",
|
||||
(uint32_t ) (f_set / 1000000), (uint32_t ) (f_set % 1000000));
|
||||
outputFrequency = f_set;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MAX2871::SetReference(uint32_t f_ref, bool doubler, uint16_t r,
|
||||
bool div2) {
|
||||
if (f_ref < 10000000) {
|
||||
LOG_ERR("Reference frequency must be >=10MHz, is %lu", f_ref);
|
||||
return false;
|
||||
} else if (f_ref > 105000000 && doubler) {
|
||||
LOG_ERR(
|
||||
"Reference frequency must be <=105MHz when used with doubler, is %lu",
|
||||
f_ref);
|
||||
return false;
|
||||
} else if (f_ref > 210000000) {
|
||||
LOG_ERR("Reference frequency must be <=210MHz, is %lu", f_ref);
|
||||
return false;
|
||||
}
|
||||
if (r < 1 || r > 1023) {
|
||||
LOG_ERR("Reference divider must be between 1 and 1023, is %d", r);
|
||||
return false;
|
||||
}
|
||||
// calculate PFD frequency
|
||||
uint32_t pfd = f_ref;
|
||||
if (doubler) {
|
||||
pfd *= 2;
|
||||
}
|
||||
pfd /= r;
|
||||
if (div2) {
|
||||
pfd /= 2;
|
||||
}
|
||||
if (pfd > 125000000) {
|
||||
LOG_ERR("PFD frequency must be <=125MHz, is %d",
|
||||
pfd);
|
||||
return false;
|
||||
}
|
||||
if(pfd > 32000000) {
|
||||
regs[2] |= (1UL << 31);
|
||||
} else {
|
||||
regs[2] &= ~(1UL << 31);
|
||||
}
|
||||
// input values are valid, adjust registers
|
||||
regs[2] &= ~0x03FFC000;
|
||||
if (doubler) {
|
||||
regs[2] |= (1UL << 25);
|
||||
}
|
||||
if (div2) {
|
||||
regs[2] |= (1UL << 24);
|
||||
}
|
||||
regs[2] |= (r << 14);
|
||||
f_PFD = pfd;
|
||||
LOG_INFO("Set PFD frequency to %lu", f_PFD);
|
||||
|
||||
// updating VAS state machine clock
|
||||
uint16_t BS = f_PFD / 50000;
|
||||
if (BS > 1023) {
|
||||
BS = 1023;
|
||||
} else if (BS < 1) {
|
||||
BS = 1;
|
||||
}
|
||||
LOG_DEBUG("BS set to %lu", BS);
|
||||
regs[4] &= ~0x030FF000;
|
||||
regs[4] |= ((BS & 0xFF) << 12);
|
||||
regs[4] |= (((BS >> 8) & 0x03) << 24);
|
||||
|
||||
// update ADC clock
|
||||
uint16_t cdiv = f_PFD/100000;
|
||||
LOG_DEBUG("CDIV set to %u", cdiv);
|
||||
regs[3] &= ~0x00007FF8;
|
||||
regs[3] |= (cdiv & 0xFFF) << 3;
|
||||
return true;
|
||||
}
|
||||
|
||||
void MAX2871::Update() {
|
||||
Write(5, regs[5]);
|
||||
Write(4, regs[4]);
|
||||
Write(3, regs[3]);
|
||||
Write(2, regs[2]);
|
||||
Write(1, regs[1]);
|
||||
Write(0, regs[0]);
|
||||
}
|
||||
|
||||
void MAX2871::UpdateFrequency() {
|
||||
Write(4, regs[4]);
|
||||
Write(3, regs[3]);
|
||||
Write(1, regs[1]);
|
||||
Write(0, regs[0]);
|
||||
}
|
||||
|
||||
void MAX2871::Write(uint8_t reg, uint32_t val) {
|
||||
uint16_t data[2];
|
||||
// split value into two 16 bit words
|
||||
data[0] = val >> 16;
|
||||
data[1] = (val & 0xFFF8) | reg;
|
||||
Delay::us(1);
|
||||
HAL_SPI_Transmit(hspi, (uint8_t*) data, 2, 20);
|
||||
LE->BSRR = LEpin;
|
||||
Delay::us(1);
|
||||
LE->BSRR = LEpin << 16;
|
||||
}
|
||||
|
||||
// Assumes that the MUX pin is already configured as "Read register 6" and connected to MISO
|
||||
uint32_t MAX2871::Read() {
|
||||
uint16_t transmit[2] = {0x0000, 0x0006};
|
||||
HAL_SPI_Transmit(hspi, (uint8_t*) transmit, 2, 20);
|
||||
LE->BSRR = LEpin;
|
||||
memset(transmit, 0, sizeof(transmit));
|
||||
uint16_t recv[2];
|
||||
HAL_SPI_TransmitReceive(hspi, (uint8_t*) transmit, (uint8_t*) recv, 2, 20);
|
||||
LE->BSRR = LEpin << 16;
|
||||
// assemble readback result
|
||||
uint32_t result = ((uint32_t) recv[0] << 16) | (recv[1] & 0xFFFF);
|
||||
result <<= 2;
|
||||
LOG_DEBUG("Readback: 0x%08x", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool MAX2871::BuildVCOMap() {
|
||||
memset(VCOmax, 0, sizeof(VCOmax));
|
||||
// save output frequency
|
||||
uint64_t oldFreq = outputFrequency;
|
||||
constexpr uint32_t step = 10000000;
|
||||
for (uint64_t freq = 3000000000; freq <= MaxFreq; freq += step) {
|
||||
SetFrequency(freq);
|
||||
UpdateFrequency();
|
||||
uint32_t start = HAL_GetTick();
|
||||
// set MUX to LD
|
||||
regs[2] &= ~(7UL << 26);
|
||||
regs[5] &= ~(1UL << 18);
|
||||
regs[2] |= (6UL << 26);
|
||||
Write(5, regs[5]);
|
||||
Write(2, regs[2]);
|
||||
while (!(MUX->IDR & MUXpin)) {
|
||||
if (HAL_GetTick() - start > 100) {
|
||||
LOG_ERR(
|
||||
"Failed to lock during VCO map build process, aborting (f=%lu%06luHz)",
|
||||
(uint32_t )(freq / 1000000),
|
||||
(uint32_t ) (freq % 1000000));
|
||||
gotVCOMap = false;
|
||||
// revert back to previous frequency
|
||||
SetFrequency(oldFreq);
|
||||
LE->BSRR = LEpin << 16;
|
||||
// Mux pin back to high impedance
|
||||
regs[2] &= ~(7UL << 26);
|
||||
regs[5] &= ~(1UL << 18);
|
||||
Update();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// set MUX to SPI read
|
||||
regs[2] &= ~(7UL << 26);
|
||||
regs[5] &= ~(1UL << 18);
|
||||
regs[2] |= (4UL << 26);
|
||||
regs[5] |= (1UL << 18);
|
||||
Write(5, regs[5]);
|
||||
Write(2, regs[2]);
|
||||
auto readback = Read();
|
||||
uint8_t vco = (readback & 0x01F8) >> 3;
|
||||
VCOmax[vco] = freq / 100000;
|
||||
LOG_INFO("VCO map: %lu%06luHz uses VCO %d",
|
||||
(uint32_t ) (freq / 1000000), (uint32_t ) (freq % 1000000), vco);
|
||||
}
|
||||
gotVCOMap = true;
|
||||
// revert back to previous frequency
|
||||
SetFrequency(oldFreq);
|
||||
// Mux pin back to high impedance
|
||||
regs[2] &= ~(7UL << 26);
|
||||
regs[5] &= ~(1UL << 18);
|
||||
|
||||
// Turn off VAS, select VCO manually from now on
|
||||
regs[3] |= (1UL << 25);
|
||||
Update();
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t MAX2871::GetTemp() {
|
||||
// select temperature channel and start ADC
|
||||
regs[5] &= ~0x00000078;
|
||||
regs[5] |= 0x00000048;
|
||||
Write(5, regs[5]);
|
||||
Delay::us(100);
|
||||
// set MUX to SPI read
|
||||
regs[2] &= ~(7UL << 26);
|
||||
regs[5] &= ~(1UL << 18);
|
||||
regs[2] |= (4UL << 26);
|
||||
regs[5] |= (1UL << 18);
|
||||
Write(5, regs[5]);
|
||||
Write(2, regs[2]);
|
||||
uint8_t ADC_raw = (Read() >> 16) & 0x7F;
|
||||
LOG_DEBUG("Raw temp ADC: %d", ADC_raw);
|
||||
// Disable ADC
|
||||
regs[5] &= ~0x00000078;
|
||||
// Mux pin back to high impedance
|
||||
regs[2] &= ~(7UL << 26);
|
||||
regs[5] &= ~(1UL << 18);
|
||||
Write(5, regs[5]);
|
||||
Write(2, regs[2]);
|
||||
// convert to celsius and return
|
||||
return 95 - 1.14f * ADC_raw;
|
||||
}
|
||||
81
Software/VNA_embedded/Application/Drivers/max2871.hpp
Normal file
81
Software/VNA_embedded/Application/Drivers/max2871.hpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#pragma once
|
||||
|
||||
#include "stm.hpp"
|
||||
|
||||
class MAX2871 {
|
||||
public:
|
||||
constexpr MAX2871(SPI_HandleTypeDef *hspi, GPIO_TypeDef *LE = nullptr,
|
||||
uint16_t LEpin = 0, GPIO_TypeDef *RF_EN = nullptr,
|
||||
uint16_t RF_ENpin = 0, GPIO_TypeDef *LD = nullptr, uint16_t LDpin = 0,
|
||||
GPIO_TypeDef *CE = nullptr, uint16_t CEpin = 0,
|
||||
GPIO_TypeDef *MUX = nullptr, uint16_t MUXpin = 0) :
|
||||
regs(), f_PFD(0),
|
||||
hspi(hspi),
|
||||
CE(CE), CEpin(CEpin),
|
||||
LE(LE), LEpin(LEpin),
|
||||
MUX(MUX), MUXpin(MUXpin),
|
||||
RF_EN(RF_EN), RF_ENpin(RF_ENpin),
|
||||
LD(LD), LDpin(LDpin),
|
||||
outputFrequency(0),
|
||||
VCOmax(),
|
||||
gotVCOMap(false)
|
||||
{};
|
||||
|
||||
bool Init();
|
||||
bool Init(uint32_t f_ref, bool doubler, uint16_t r, bool div2);
|
||||
bool SetReference(uint32_t f_ref, bool doubler, uint16_t r, bool div2);
|
||||
void ChipEnable(bool on);
|
||||
void RFEnable(bool on);
|
||||
bool Locked();
|
||||
enum class Power : uint8_t {
|
||||
n4dbm = 0x00,
|
||||
n1dbm = 0x01,
|
||||
p2dbm = 0x02,
|
||||
p5dbm = 0x03,
|
||||
};
|
||||
void SetPowerOutA(Power p, bool enabled = true);
|
||||
void SetPowerOutB(Power p, bool enabled = true);
|
||||
enum class Mode : uint8_t {
|
||||
LowNoise = 0x00,
|
||||
LowSpur1 = 0x02,
|
||||
LowSpur2 = 0x03,
|
||||
};
|
||||
void SetMode(Mode m);
|
||||
enum class CPMode : uint8_t {
|
||||
Disabled = 0x00,
|
||||
CP10 = 0x01,
|
||||
CP20 = 0x02,
|
||||
CP30 = 0x03,
|
||||
};
|
||||
void SetCPMode(CPMode m);
|
||||
void SetCPCurrent(uint8_t mA);
|
||||
bool SetFrequency(uint64_t f);
|
||||
void Update();
|
||||
void UpdateFrequency();
|
||||
bool BuildVCOMap();
|
||||
uint8_t GetTemp();
|
||||
uint32_t* GetRegisters() {
|
||||
return regs;
|
||||
}
|
||||
private:
|
||||
static constexpr uint64_t MaxFreq = 6100000000; // 6GHz according to datasheet, but slight overclocking is possible
|
||||
|
||||
uint32_t Read();
|
||||
void Write(uint8_t reg, uint32_t val);
|
||||
uint32_t regs[6];
|
||||
uint32_t f_PFD;
|
||||
SPI_HandleTypeDef *hspi;
|
||||
GPIO_TypeDef *CE;
|
||||
uint16_t CEpin;
|
||||
GPIO_TypeDef *LE;
|
||||
uint16_t LEpin;
|
||||
GPIO_TypeDef *MUX;
|
||||
uint16_t MUXpin;
|
||||
GPIO_TypeDef *RF_EN;
|
||||
uint16_t RF_ENpin;
|
||||
GPIO_TypeDef *LD;
|
||||
uint16_t LDpin;
|
||||
uint64_t outputFrequency;
|
||||
uint16_t VCOmax[64];
|
||||
bool gotVCOMap;
|
||||
};
|
||||
12
Software/VNA_embedded/Application/Drivers/stm.hpp
Normal file
12
Software/VNA_embedded/Application/Drivers/stm.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include "stm32g431xx.h"
|
||||
#include "stm32g4xx_hal.h"
|
||||
|
||||
namespace STM {
|
||||
|
||||
static inline bool InInterrupt() {
|
||||
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue