@@ -0,0 +1,725 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* STM32 双向 DShot 电调驱动,BF 风格的遥测捕获(binding "misaki,dshot-bb")。
*
* TX:与 misaki,dshot 相同——定时器 PWM + DMA burst( TIMx->DMAR/DCR)。
*
* RX(仿 BF dshot_bitbang 的“收”):另起一个 pacer 定时器,频率 = 3 ×
* 符号率;帧后释放 4 条信号线(清 CCER.CCxE),启动 pacer,其 UPDATE 请求
* 触发 DMA 把 GPIOx->IDR 采样进缓冲(每端口一条 DMA),再用 BF 的游程 + GCR
* 解码得到 eRPM。TX 定时器的时基(PSC/ARR/CCMR)全程不动,只切 CCxE。
*
* 本板引脚:CH1=PE9, CH2=PE11, CH3=PA10, CH4=PE14(跨 GPIOE/GPIOA 两个端口)。
*/
# define DT_DRV_COMPAT misaki_dshot_bb
# include <zephyr/device.h>
# include <zephyr/kernel.h>
# include <zephyr/drivers/clock_control.h>
# include <zephyr/drivers/clock_control/stm32_clock_control.h>
# include <zephyr/drivers/dma.h>
# include <zephyr/drivers/pinctrl.h>
# include <zephyr/drivers/dshot.h>
# include <zephyr/cache.h>
# include <zephyr/logging/log.h>
# include <stm32_ll_tim.h>
# include <stm32_ll_rcc.h>
LOG_MODULE_REGISTER ( dshot_bb , CONFIG_DSHOT_BB_LOG_LEVEL ) ;
# define DSHOT_FRAME_BITS 16
# define DSHOT_GAP_ROWS 3 /* 末尾保持低电平,给电调同步 */
# define DSHOT_BUF_ROWS (DSHOT_FRAME_BITS + DSHOT_GAP_ROWS)
# define BB_RX_PORTS 2 /* GPIOE, GPIOA */
# define BB_RX_SAMPLES 150 /* ~3× 16 + 30us 窗口的采样数 */
# define BB_OVERSAMPLE 3 /* 3 样本 / 符号(BF 同款) */
/* 本板 DShot 通道 -> (采样缓冲下标, GPIO 位)。下标对应 rx-dmas 顺序:
* 0 = GPIOE, 1 = GPIOA。 */
struct bb_ch_map {
uint8_t buf ;
uint8_t bit ;
} ;
static const struct bb_ch_map bb_map [ DSHOT_MAX_CHANNELS ] = {
{ 0 , 9 } , /* CH1 = PE9 */
{ 0 , 11 } , /* CH2 = PE11 */
{ 1 , 10 } , /* CH3 = PA10 */
{ 0 , 14 } , /* CH4 = PE14 */
} ;
struct dshot_bb_config {
TIM_TypeDef * timer ; /* TX timer(父节点) */
uint32_t prescaler ;
struct stm32_pclken pclken ;
const struct pinctrl_dev_config * pcfg ;
const struct device * tx_dma_dev ;
uint32_t tx_dma_channel ;
uint32_t tx_dma_slot ;
uint32_t tx_dma_flags ;
TIM_TypeDef * pacer ; /* pacer timer */
struct stm32_pclken pacer_pclken ;
const struct device * rx_dma_dev [ BB_RX_PORTS ] ;
uint32_t rx_dma_channel [ BB_RX_PORTS ] ;
uint32_t rx_dma_slot [ BB_RX_PORTS ] ;
uint32_t rx_dma_flags [ BB_RX_PORTS ] ;
const uint32_t rx_gpio_base [ BB_RX_PORTS ] ; /* GPIOx->IDR 地址 */
uint32_t dshot_speed ; /* kbit/s */
uint32_t frame_interval_us ;
} ;
struct dshot_bb_data {
const struct device * dev ;
uint32_t dma_buf [ DSHOT_BUF_ROWS ] [ DSHOT_MAX_CHANNELS ] __aligned ( 32 ) ;
uint16_t frame_value [ DSHOT_MAX_CHANNELS ] ;
bool tlm_request [ DSHOT_MAX_CHANNELS ] ;
uint16_t cmd_repeats [ DSHOT_MAX_CHANNELS ] ;
uint16_t saved_throttle [ DSHOT_MAX_CHANNELS ] ;
struct dshot_output_config out_cfg ;
/* RX 采样缓冲(uint16_t = GPIO IDR) */
uint16_t rx_buf [ BB_RX_PORTS ] [ BB_RX_SAMPLES ] __aligned ( 32 ) ;
int32_t erpm [ DSHOT_MAX_CHANNELS ] ;
bool erpm_valid [ DSHOT_MAX_CHANNELS ] ;
uint8_t cap_ch ;
struct k_timer frame_timer ;
struct k_work frame_work ;
uint32_t pulse_0 ;
uint32_t pulse_1 ;
uint32_t period ;
uint32_t frame_us ;
bool running ;
} ;
/* DShot 帧组装 */
static uint16_t bb_compose_frame ( uint16_t value , bool tlm )
{
uint16_t frame = ( value < < 1 ) | ( tlm ? 1 : 0 ) ;
uint16_t csum = ( frame ^ ( frame > > 4 ) ^ ( frame > > 8 ) ) & 0x0F ;
return ( frame < < 4 ) | csum ;
}
/* BF 风格遥测解码(游程 + GCR),移植自 BF dshot_bitbang_decode.c */
# define BB_TLM_NOEDGE 0xffffU
static uint32_t bb_decode_value ( uint32_t value )
{
# define IV 0xffffffffU
static const uint32_t decode [ 32 ] = {
IV , IV , IV , IV , IV , IV , IV , IV , IV , 9 , 10 , 11 , IV , 13 , 14 , 15 ,
IV , IV , 2 , 3 , IV , 5 , 6 , 7 , IV , 0 , 8 , 1 , IV , 4 , 12 , IV ,
} ;
uint32_t decoded , csum ;
value & = 0xfffff ; /* 丢掉起始位 */
decoded = decode [ value & 0x1f ] ;
decoded | = decode [ ( value > > 5 ) & 0x1f ] < < 4 ;
decoded | = decode [ ( value > > 10 ) & 0x1f ] < < 8 ;
decoded | = decode [ ( value > > 15 ) & 0x1f ] < < 12 ;
csum = decoded ;
csum ^ = ( csum > > 8 ) ;
csum ^ = ( csum > > 4 ) ;
if ( ( csum & 0xf ) ! = 0xf | | decoded > 0xffff ) {
return BB_TLM_NOEDGE ;
}
return decoded > > 4 ;
# undef IV
}
/* 从采样缓冲(uint16 的 GPIO IDR)里取某一位,按 BF 方式解码成 12bit 值 */
static uint32_t bb_decode_samples ( const uint16_t * buf , unsigned count , uint8_t bit )
{
const uint16_t mask = BIT ( bit ) ;
uint32_t value = 0 ;
unsigned run = 0 ;
uint16_t last = 0 ;
unsigned i = 0 ;
/* 跳过前导高电平,找第一个 0 */
while ( i < count ) {
if ( ! ( buf [ i ] & mask ) ) {
break ;
}
i + + ;
}
if ( i > = count ) {
return BB_TLM_NOEDGE ;
}
last = buf [ i ] & mask ;
run = 0 ;
for ( ; i < count ; i + + ) {
uint16_t cur = buf [ i ] & mask ;
if ( cur = = last ) {
run + + ;
continue ;
}
/* 一段电平结束:长度 n(样本)-> (n+1)/3 个比特(3× 过采样) */
unsigned len = ( run + 1 ) / BB_OVERSAMPLE ;
if ( len < 1 ) {
len = 1 ;
}
value < < = len ;
value | = 1U < < ( len - 1 ) ;
last = cur ;
run = 1 ;
}
return bb_decode_value ( value ) ;
}
/* BF 的 eRPM 换算 */
static int32_t bb_value_to_erpm ( uint32_t v )
{
if ( v = = 0x0fff ) {
return 0 ;
}
v = ( v & 0x01ff ) < < ( ( v & 0xfe00 ) > > 9 ) ;
if ( ! v ) {
return - 1 ;
}
return ( int32_t ) ( ( 1000000 * 60 + v / 2 ) / v ) ;
}
/* TX 帧组装 */
static void bb_rebuild_buffer ( const struct device * dev )
{
struct dshot_bb_data * data = dev - > data ;
for ( int bit = 0 ; bit < DSHOT_FRAME_BITS ; bit + + ) {
for ( int ch = 0 ; ch < DSHOT_MAX_CHANNELS ; ch + + ) {
uint16_t frame = bb_compose_frame ( data - > frame_value [ ch ] ,
data - > tlm_request [ ch ] ) ;
bool one = frame & ( 0x8000 > > bit ) ;
data - > dma_buf [ bit ] [ ch ] = one ? data - > pulse_1 : data - > pulse_0 ;
}
}
for ( int row = DSHOT_FRAME_BITS ; row < DSHOT_BUF_ROWS ; row + + ) {
for ( int ch = 0 ; ch < DSHOT_MAX_CHANNELS ; ch + + ) {
data - > dma_buf [ row ] [ ch ] = 0 ;
}
}
sys_cache_data_flush_range ( data - > dma_buf , sizeof ( data - > dma_buf ) ) ;
}
/* RX: pacer 采样 + 解码 */
static void bb_capture ( struct dshot_bb_data * data )
{
const struct dshot_bb_config * cfg = data - > dev - > config ;
TIM_TypeDef * pacer = cfg - > pacer ;
uint8_t ch = data - > cap_ch ;
uint32_t window_us ;
/* 释放 4 条线:清 CCxE(不动时基/模式) */
cfg - > timer - > CCER & = ~ ( TIM_CCER_CC1E | TIM_CCER_CC2E |
TIM_CCER_CC3E | TIM_CCER_CC4E ) ;
/* 清采样缓冲 */
for ( int p = 0 ; p < BB_RX_PORTS ; p + + ) {
for ( int i = 0 ; i < BB_RX_SAMPLES ; i + + ) {
data - > rx_buf [ p ] [ i ] = 0 ;
}
( void ) dma_reload ( cfg - > rx_dma_dev [ p ] , cfg - > rx_dma_channel [ p ] ,
cfg - > rx_gpio_base [ p ] ,
( uintptr_t ) data - > rx_buf [ p ] ,
BB_RX_SAMPLES * sizeof ( uint16_t ) ) ;
( void ) dma_start ( cfg - > rx_dma_dev [ p ] , cfg - > rx_dma_channel [ p ] ) ;
}
/* 启动 pacer:计数器重启,UPDATE 请求触发采样 DMA */
LL_TIM_SetCounter ( pacer , 0 ) ;
LL_TIM_EnableCounter ( pacer ) ;
window_us = ( BB_RX_SAMPLES * 1000000U ) / ( cfg - > dshot_speed * 1000U * BB_OVERSAMPLE ) ;
k_busy_wait ( window_us + 5 ) ;
LL_TIM_DisableCounter ( pacer ) ;
for ( int p = 0 ; p < BB_RX_PORTS ; p + + ) {
( void ) dma_stop ( cfg - > rx_dma_dev [ p ] , cfg - > rx_dma_channel [ p ] ) ;
}
/* 恢复输出 */
cfg - > timer - > CCER | = ( TIM_CCER_CC1E | TIM_CCER_CC2E |
TIM_CCER_CC3E | TIM_CCER_CC4E ) ;
/* 解码本通道 */
uint32_t v = bb_decode_samples ( data - > rx_buf [ bb_map [ ch ] . buf ] ,
BB_RX_SAMPLES , bb_map [ ch ] . bit ) ;
if ( v ! = BB_TLM_NOEDGE ) {
int32_t erpm = bb_value_to_erpm ( v ) ;
if ( erpm > = 0 ) {
data - > erpm [ ch ] = erpm ;
data - > erpm_valid [ ch ] = true ;
}
LOG_DBG ( " ch%u raw=%u erpm=%d " , ch , v , erpm ) ;
} else {
LOG_DBG ( " ch%u noedge " , ch ) ;
}
data - > cap_ch = ( ch + 1 ) % DSHOT_MAX_CHANNELS ;
}
/* 周期发帧 */
static void bb_frame_work_handler ( struct k_work * work )
{
struct dshot_bb_data * data =
CONTAINER_OF ( work , struct dshot_bb_data , frame_work ) ;
const struct device * dev = data - > dev ;
const struct dshot_bb_config * cfg = dev - > config ;
for ( int ch = 0 ; ch < DSHOT_MAX_CHANNELS ; ch + + ) {
if ( data - > cmd_repeats [ ch ] > 0 ) {
data - > cmd_repeats [ ch ] - - ;
if ( data - > cmd_repeats [ ch ] = = 0 ) {
data - > frame_value [ ch ] = data - > saved_throttle [ ch ] ;
data - > tlm_request [ ch ] = false ;
}
}
}
bb_rebuild_buffer ( dev ) ;
LL_TIM_DisableDMAReq_UPDATE ( cfg - > timer ) ;
int ret = dma_reload ( cfg - > tx_dma_dev , cfg - > tx_dma_channel ,
( uintptr_t ) data - > dma_buf ,
( uintptr_t ) & cfg - > timer - > DMAR ,
sizeof ( data - > dma_buf ) ) ;
if ( ret < 0 ) {
LOG_ERR ( " tx dma_reload: %d " , ret ) ;
LL_TIM_EnableDMAReq_UPDATE ( cfg - > timer ) ;
return ;
}
( void ) dma_start ( cfg - > tx_dma_dev , cfg - > tx_dma_channel ) ;
LL_TIM_EnableDMAReq_UPDATE ( cfg - > timer ) ;
/* 等一帧发完再收 */
k_busy_wait ( data - > frame_us ) ;
bb_capture ( data ) ;
}
static void bb_frame_timer_handler ( struct k_timer * timer )
{
struct dshot_bb_data * data =
CONTAINER_OF ( timer , struct dshot_bb_data , frame_timer ) ;
k_work_submit ( & data - > frame_work ) ;
}
/* 公共 API(与 dshot.h 一致) */
static uint16_t bb_map_output ( const struct dshot_output_config * cfg , float value )
{
float idle = cfg - > idle ;
float mag ;
if ( idle < 0.0f ) {
idle = 0.0f ;
} else if ( idle > 1.0f ) {
idle = 1.0f ;
}
if ( value = = 0.0f ) {
return DSHOT_VALUE_STOP ;
}
if ( cfg - > output_3d ) {
mag = ( value < 0.0f ) ? - value : value ;
if ( mag < = cfg - > deadband ) {
return DSHOT_VALUE_STOP ;
}
if ( mag > 1.0f ) {
mag = 1.0f ;
}
mag = idle + mag * ( 1.0f - idle ) ;
if ( value > 0.0f ) {
return ( uint16_t ) ( DSHOT_3D_FORWARD_MIN +
mag * ( DSHOT_THROTTLE_MAX - DSHOT_3D_FORWARD_MIN ) + 0.5f ) ;
}
return ( uint16_t ) ( DSHOT_THROTTLE_MIN +
mag * ( DSHOT_3D_SPLIT - DSHOT_THROTTLE_MIN ) + 0.5f ) ;
}
if ( value < 0.0f ) {
return DSHOT_VALUE_STOP ;
}
if ( value > 1.0f ) {
value = 1.0f ;
}
value = idle + value * ( 1.0f - idle ) ;
return ( uint16_t ) ( DSHOT_THROTTLE_MIN +
value * ( DSHOT_THROTTLE_MAX - DSHOT_THROTTLE_MIN ) + 0.5f ) ;
}
int dshot_set_output_config ( const struct device * dev ,
const struct dshot_output_config * cfg )
{
struct dshot_bb_data * data = dev - > data ;
if ( cfg = = NULL ) {
data - > out_cfg . output_3d = false ;
data - > out_cfg . idle = 0.0f ;
data - > out_cfg . deadband = 0.0f ;
} else {
data - > out_cfg = * cfg ;
}
return 0 ;
}
int dshot_set_output ( const struct device * dev , uint8_t channel , float value )
{
struct dshot_bb_data * data = dev - > data ;
uint16_t raw ;
if ( channel > = DSHOT_MAX_CHANNELS ) {
return - EINVAL ;
}
raw = bb_map_output ( & data - > out_cfg , value ) ;
data - > saved_throttle [ channel ] = raw ;
if ( data - > cmd_repeats [ channel ] = = 0 ) {
data - > frame_value [ channel ] = raw ;
}
data - > tlm_request [ channel ] = false ;
return 0 ;
}
int dshot_set_throttle ( const struct device * dev , uint8_t channel ,
uint16_t throttle , bool telemetry_request )
{
struct dshot_bb_data * data = dev - > data ;
if ( channel > = DSHOT_MAX_CHANNELS | | throttle > DSHOT_THROTTLE_MAX ) {
return - EINVAL ;
}
if ( throttle > 0 & & throttle < DSHOT_THROTTLE_MIN ) {
return - EINVAL ;
}
data - > saved_throttle [ channel ] = throttle ;
if ( data - > cmd_repeats [ channel ] = = 0 ) {
data - > frame_value [ channel ] = throttle ;
}
data - > tlm_request [ channel ] = telemetry_request ;
return 0 ;
}
int dshot_send_command ( const struct device * dev , uint8_t channel ,
enum dshot_command cmd , uint16_t repeats )
{
struct dshot_bb_data * data = dev - > data ;
if ( channel > = DSHOT_MAX_CHANNELS | | cmd > DSHOT_CMD_MAX ) {
return - EINVAL ;
}
if ( repeats = = 0 ) {
repeats = 10 ;
}
if ( data - > cmd_repeats [ channel ] = = 0 ) {
data - > saved_throttle [ channel ] = data - > frame_value [ channel ] ;
}
data - > frame_value [ channel ] = ( uint16_t ) cmd ;
data - > tlm_request [ channel ] = ( cmd ! = DSHOT_CMD_MOTOR_STOP ) ;
data - > cmd_repeats [ channel ] = repeats ;
return 0 ;
}
int dshot_start ( const struct device * dev )
{
struct dshot_bb_data * data = dev - > data ;
const struct dshot_bb_config * cfg = dev - > config ;
if ( data - > running ) {
return 0 ;
}
LL_TIM_EnableCounter ( cfg - > timer ) ;
k_timer_start ( & data - > frame_timer , K_NO_WAIT , K_USEC ( cfg - > frame_interval_us ) ) ;
data - > running = true ;
return 0 ;
}
int dshot_stop ( const struct device * dev )
{
struct dshot_bb_data * data = dev - > data ;
const struct dshot_bb_config * cfg = dev - > config ;
k_timer_stop ( & data - > frame_timer ) ;
dma_stop ( cfg - > tx_dma_dev , cfg - > tx_dma_channel ) ;
LL_TIM_DisableCounter ( cfg - > timer ) ;
data - > running = false ;
return 0 ;
}
int dshot_get_erpm ( const struct device * dev , uint8_t channel , int32_t * erpm )
{
struct dshot_bb_data * data = dev - > data ;
if ( channel > = DSHOT_MAX_CHANNELS | | erpm = = NULL ) {
return - EINVAL ;
}
if ( ! data - > erpm_valid [ channel ] ) {
return - EAGAIN ;
}
* erpm = data - > erpm [ channel ] ;
data - > erpm_valid [ channel ] = false ;
return 0 ;
}
int dshot_set_duty_permille ( const struct device * dev , uint32_t t0h_permille )
{
struct dshot_bb_data * data = dev - > data ;
uint32_t p0 , p1 ;
if ( t0h_permille < 100U | | t0h_permille > 450U ) {
return - EINVAL ;
}
p0 = ( data - > period * t0h_permille ) / 1000U ;
p1 = p0 * 2U ;
if ( p0 = = 0U | | p1 > = data - > period ) {
return - EINVAL ;
}
data - > pulse_0 = p0 ;
data - > pulse_1 = p1 ;
return 0 ;
}
/* 初始化 */
static uint32_t bb_timer_clock ( const struct stm32_pclken * pclken ,
const struct device * clk )
{
uint32_t rate = 0 ;
( void ) clock_control_get_rate ( clk , ( clock_control_subsys_t ) pclken , & rate ) ;
switch ( pclken - > bus ) {
case STM32_CLOCK_BUS_APB1 :
if ( LL_RCC_GetAPB1Prescaler ( ) ! = LL_RCC_APB1_DIV_1 ) {
rate * = 2 ;
}
break ;
case STM32_CLOCK_BUS_APB2 :
if ( LL_RCC_GetAPB2Prescaler ( ) ! = LL_RCC_APB2_DIV_1 ) {
rate * = 2 ;
}
break ;
default :
break ;
}
return rate ;
}
static int dshot_bb_init ( const struct device * dev )
{
const struct dshot_bb_config * cfg = dev - > config ;
struct dshot_bb_data * data = dev - > data ;
const struct device * clk = DEVICE_DT_GET ( STM32_CLOCK_CONTROL_NODE ) ;
uint32_t tim_clk , pacer_clk , bit_freq ;
int ret ;
data - > dev = dev ;
if ( ! device_is_ready ( clk ) ) {
return - ENODEV ;
}
if ( clock_control_on ( clk , ( clock_control_subsys_t ) & cfg - > pclken ) < 0 ) {
return - EIO ;
}
if ( clock_control_on ( clk , ( clock_control_subsys_t ) & cfg - > pacer_pclken ) < 0 ) {
return - EIO ;
}
tim_clk = bb_timer_clock ( & cfg - > pclken , clk ) ;
pacer_clk = bb_timer_clock ( & cfg - > pacer_pclken , clk ) ;
LOG_INF ( " tx clk %u Hz, pacer clk %u Hz " , tim_clk , pacer_clk ) ;
if ( pinctrl_apply_state ( cfg - > pcfg , PINCTRL_STATE_DEFAULT ) < 0 ) {
return - EIO ;
}
/* TX 定时器 */
bit_freq = cfg - > dshot_speed * 1000U ;
data - > period = tim_clk / ( cfg - > prescaler + 1U ) / bit_freq ;
if ( data - > period < 10 | | data - > period > 65536 ) {
LOG_ERR ( " period out of range %u " , data - > period ) ;
return - EINVAL ;
}
data - > pulse_0 = data - > period / 3U ;
data - > pulse_1 = ( data - > period * 2U ) / 3U ;
data - > frame_us = ( DSHOT_BUF_ROWS * 1000000U + bit_freq / 2U ) / bit_freq + 5U ;
LL_TIM_SetPrescaler ( cfg - > timer , cfg - > prescaler ) ;
LL_TIM_SetAutoReload ( cfg - > timer , data - > period - 1U ) ;
LL_TIM_SetCounterMode ( cfg - > timer , LL_TIM_COUNTERMODE_UP ) ;
LL_TIM_SetRepetitionCounter ( cfg - > timer , 0 ) ;
LL_TIM_OC_InitTypeDef oc = { 0 } ;
oc . OCMode = LL_TIM_OCMODE_PWM1 ;
oc . OCState = LL_TIM_OCSTATE_ENABLE ;
oc . OCNState = LL_TIM_OCSTATE_DISABLE ;
oc . CompareValue = 0 ;
oc . OCPolarity = LL_TIM_OCPOLARITY_HIGH ;
oc . OCNPolarity = LL_TIM_OCPOLARITY_HIGH ;
oc . OCIdleState = LL_TIM_OCIDLESTATE_LOW ;
oc . OCNIdleState = LL_TIM_OCIDLESTATE_LOW ;
LL_TIM_OC_Init ( cfg - > timer , LL_TIM_CHANNEL_CH1 , & oc ) ;
LL_TIM_OC_Init ( cfg - > timer , LL_TIM_CHANNEL_CH2 , & oc ) ;
LL_TIM_OC_Init ( cfg - > timer , LL_TIM_CHANNEL_CH3 , & oc ) ;
LL_TIM_OC_Init ( cfg - > timer , LL_TIM_CHANNEL_CH4 , & oc ) ;
LL_TIM_OC_EnablePreload ( cfg - > timer , LL_TIM_CHANNEL_CH1 ) ;
LL_TIM_OC_EnablePreload ( cfg - > timer , LL_TIM_CHANNEL_CH2 ) ;
LL_TIM_OC_EnablePreload ( cfg - > timer , LL_TIM_CHANNEL_CH3 ) ;
LL_TIM_OC_EnablePreload ( cfg - > timer , LL_TIM_CHANNEL_CH4 ) ;
LL_TIM_EnableAllOutputs ( cfg - > timer ) ;
LL_TIM_ConfigDMABurst ( cfg - > timer , LL_TIM_DMABURST_BASEADDR_CCR1 ,
LL_TIM_DMABURST_LENGTH_4TRANSFERS ) ;
LL_TIM_EnableDMAReq_UPDATE ( cfg - > timer ) ;
if ( ! device_is_ready ( cfg - > tx_dma_dev ) ) {
return - ENODEV ;
}
struct dma_block_config blk = {
. source_address = ( uintptr_t ) data - > dma_buf ,
. dest_address = ( uintptr_t ) & cfg - > timer - > DMAR ,
. dest_addr_adj = DMA_ADDR_ADJ_NO_CHANGE ,
. block_size = sizeof ( data - > dma_buf ) ,
} ;
struct dma_config dma_cfg = {
. dma_slot = cfg - > tx_dma_slot ,
. channel_direction = MEMORY_TO_PERIPHERAL ,
. source_data_size = 4 ,
. dest_data_size = 4 ,
. block_count = 1 ,
. head_block = & blk ,
} ;
ret = dma_config ( cfg - > tx_dma_dev , cfg - > tx_dma_channel , & dma_cfg ) ;
if ( ret < 0 ) {
LOG_ERR ( " tx dma_config: %d " , ret ) ;
return ret ;
}
/* pacer 定时器:3× 符号率,UDE 打开但先不跑 */
uint32_t pacer_freq = bit_freq * BB_OVERSAMPLE ;
uint32_t pacer_div = pacer_clk / pacer_freq ;
if ( pacer_div = = 0 ) {
return - EINVAL ;
}
LL_TIM_SetPrescaler ( cfg - > pacer , 0 ) ;
LL_TIM_SetAutoReload ( cfg - > pacer , pacer_div - 1U ) ;
LL_TIM_SetCounterMode ( cfg - > pacer , LL_TIM_COUNTERMODE_UP ) ;
LL_TIM_SetRepetitionCounter ( cfg - > pacer , 0 ) ;
LL_TIM_EnableDMAReq_UPDATE ( cfg - > pacer ) ;
LL_TIM_DisableCounter ( cfg - > pacer ) ;
/* RX 采样 DMA: pacer UPDATE 触发,GPIOx->IDR -> 缓冲 */
for ( int p = 0 ; p < BB_RX_PORTS ; p + + ) {
if ( ! device_is_ready ( cfg - > rx_dma_dev [ p ] ) ) {
return - ENODEV ;
}
struct dma_block_config rblk = {
. source_address = cfg - > rx_gpio_base [ p ] ,
. dest_address = ( uintptr_t ) data - > rx_buf [ p ] ,
. source_addr_adj = DMA_ADDR_ADJ_NO_CHANGE ,
. dest_addr_adj = DMA_ADDR_ADJ_INCREMENT ,
. block_size = BB_RX_SAMPLES * sizeof ( uint16_t ) ,
} ;
struct dma_config rcfg = {
. dma_slot = cfg - > rx_dma_slot [ p ] ,
. channel_direction = PERIPHERAL_TO_MEMORY ,
. source_data_size = 2 ,
. dest_data_size = 2 ,
. block_count = 1 ,
. head_block = & rblk ,
} ;
ret = dma_config ( cfg - > rx_dma_dev [ p ] , cfg - > rx_dma_channel [ p ] , & rcfg ) ;
if ( ret < 0 ) {
LOG_ERR ( " rx dma_config[%d]: %d " , p , ret ) ;
return ret ;
}
}
data - > out_cfg . output_3d = false ;
data - > out_cfg . idle = 0.0f ;
data - > out_cfg . deadband = 0.0f ;
for ( int ch = 0 ; ch < DSHOT_MAX_CHANNELS ; ch + + ) {
data - > erpm [ ch ] = 0 ;
data - > erpm_valid [ ch ] = false ;
}
k_work_init ( & data - > frame_work , bb_frame_work_handler ) ;
k_timer_init ( & data - > frame_timer , bb_frame_timer_handler , NULL ) ;
LOG_INF ( " DShot%u BB ready: period=%u ticks, pacer ARR=%u " ,
cfg - > dshot_speed , data - > period , pacer_div - 1U ) ;
return 0 ;
}
/* 实例化 */
# define BB_PARENT(inst) DT_PARENT(DT_DRV_INST(inst))
# define BB_PACER(inst) DT_INST_PROP(inst, pacer)
# define DSHOT_BB_INIT(inst) \
PINCTRL_DT_INST_DEFINE(inst); \
\
static struct dshot_bb_data dshot_bb_data_##inst; \
\
static const struct dshot_bb_config dshot_bb_cfg_##inst = { \
.timer = (TIM_TypeDef *)DT_REG_ADDR(BB_PARENT(inst)), \
.prescaler = DT_PROP(BB_PARENT(inst), st_prescaler), \
.pclken = STM32_CLOCK_INFO(0, BB_PARENT(inst)), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
.tx_dma_dev = DEVICE_DT_GET(DT_INST_DMAS_CTLR_BY_NAME(inst, burst)), \
.tx_dma_channel = DT_INST_DMAS_CELL_BY_NAME(inst, burst, channel), \
.tx_dma_slot = DT_INST_DMAS_CELL_BY_NAME(inst, burst, slot), \
.tx_dma_flags = DT_INST_DMAS_CELL_BY_NAME(inst, burst, channel_config), \
.pacer = (TIM_TypeDef *)DT_REG_ADDR(BB_PACER(inst)), \
.pacer_pclken = STM32_CLOCK_INFO(0, BB_PACER(inst)), \
.rx_dma_dev = { \
DEVICE_DT_GET(DT_INST_DMAS_CTLR_BY_NAME(inst, rxe)), \
DEVICE_DT_GET(DT_INST_DMAS_CTLR_BY_NAME(inst, rxa)) }, \
.rx_dma_channel = { \
DT_INST_DMAS_CELL_BY_NAME(inst, rxe, channel), \
DT_INST_DMAS_CELL_BY_NAME(inst, rxa, channel) }, \
.rx_dma_slot = { \
DT_INST_DMAS_CELL_BY_NAME(inst, rxe, slot), \
DT_INST_DMAS_CELL_BY_NAME(inst, rxa, slot) }, \
.rx_dma_flags = { \
DT_INST_DMAS_CELL_BY_NAME(inst, rxe, channel_config), \
DT_INST_DMAS_CELL_BY_NAME(inst, rxa, channel_config) }, \
.rx_gpio_base = { \
DT_REG_ADDR(DT_NODELABEL(gpioe)) + 0x10, \
DT_REG_ADDR(DT_NODELABEL(gpioa)) + 0x10 }, \
.dshot_speed = DT_INST_PROP_OR(inst, dshot_speed, 300), \
.frame_interval_us = DT_INST_PROP_OR(inst, frame_interval_us, 1000), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, dshot_bb_init, NULL, \
&dshot_bb_data_##inst, &dshot_bb_cfg_##inst, \
POST_KERNEL, CONFIG_DSHOT_BB_INIT_PRIORITY, NULL);
DT_INST_FOREACH_STATUS_OKAY ( DSHOT_BB_INIT )