` 之前呢,有给大家提到过一片拆机的板子,
没错,就是这个东西,由于没有下载器呢,当时就选择了飞线的方式解决,然而,很完美的解决了问题。飞线用的是杜邦线,另一头当然是接我们的主板了。
由于这块板子上面已经有一片驱动芯片,所以是可以直接驱动的。
- /*Vref 设置驱动电流, PWM输出0-5V,感应器误差0.5V,响应时间500ns PB7
- Diag 过流保护报警 0-报警,1-正常 ==
- En 使能 1-使能工作,0-停止工作 PB5
- Fnd/Rev配置正反转 1-正转,0-反转 PB6
- Brake 制动 0-制动,1-不制动 PB3
- Tacho 转速 计数器,1-表示转了一圈,计数 ==
- H1,H2,H3霍尔 读取高低电平,确定转子角度
- APB2负责AD,I/O,串口1,高级定时器tiM
- APB1负责DA,串口2,3,4,5,普通定时器TIM, USB , IIC , CAN*/
- // ANSIC C C89 C语言的标准
- // C99 支持任意地方定义这些变量
- // C11 最新C语言标准
- //BLDC-TIM-PWM配置----static只允许该文件调取函数
- void BLDC_TIM_Config(u16 arr_b7,u16 psc_b7,u16 pulse_b7)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* GPIOB clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //APB2外设时钟为高速
- /*GPIOB Configuration: TIM4 channel 1-4 as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //设置TIM4CLK PWM
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//APB1外设时钟为低速
- TIM_DeInit(TIM4);
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器基本初始化数据
- TIM_OCInitTypeDef TIM_OCInitStructure;//定时输出比较控制器结构体
- // 自动重装载寄存器周期的值(计数值)
- TIM_TimeBaseStructure.TIM_Period=arr_b7;//1000kHz/20000=50Hz
- TIM_TimeBaseStructure.TIM_Prescaler=psc_b7;//时钟预分频数72Mhz/72=1MHz
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//对APB1外设时钟72Mhz的分频,调试发现没起作用
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_Pulse = pulse_b7;
- TIM_OC1Init(TIM4, &TIM_OCInitStructure);
- //TIM_OCInitStructure.TIM_Pulse = 2340;//A2212<2.34ms起转
- TIM_OC2Init(TIM4, &TIM_OCInitStructure);
- //TIM_OCInitStructure.TIM_Pulse = 10000;//10ms
-
-
- TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
-
- TIM_ARRPreloadConfig(TIM4, ENABLE);
- TIM_Cmd(TIM4, ENABLE);
-
- }
- void BLDC_Fnd_Rev_TIM_Config(u16 arr_b6,u16 psc_b6,u16 pulse_b6)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* GPIOB clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //APB2外设时钟为高速
- /*GPIOB Configuration: TIM4 channel 1-4 as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //设置TIM4CLK PWM
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//APB1外设时钟为低速
- TIM_DeInit(TIM4);
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器基本初始化数据
- TIM_OCInitTypeDef TIM_OCInitStructure;//定时输出比较控制器结构体
- // 自动重装载寄存器周期的值(计数值)
- TIM_TimeBaseStructure.TIM_Period=arr_b6;//1000kHz/20000=50Hz
- TIM_TimeBaseStructure.TIM_Prescaler=psc_b6;//时钟预分频数72Mhz/72=1MHz
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//对APB1外设时钟72Mhz的分频,调试发现没起作用
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_Pulse = pulse_b6;
- TIM_OC1Init(TIM4, &TIM_OCInitStructure);
- //TIM_OCInitStructure.TIM_Pulse = 2340;//A2212<2.34ms起转
- TIM_OC2Init(TIM4, &TIM_OCInitStructure);
- //TIM_OCInitStructure.TIM_Pulse = 10000;//10ms
-
-
- TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
-
- TIM_ARRPreloadConfig(TIM4, ENABLE);
- TIM_Cmd(TIM4, ENABLE);
- //TIM_Cmd(TIM3, DISABLE);//关闭
-
- }
- //L6235PD-EN使能-PB5
- void L6235PD_EN_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;//定义一个结构体
- RCC_APB2PeriphClockCmd(L6235PD_EN_GPIO_CLK, ENABLE);//调用APB2时钟控制指令(可定义一组外设/一组外设某一个接口/外设名字,使能)使能APB2总线时钟
-
- GPIO_InitStruct.GPIO_Pin = L6235PD_EN_GPIO_PIN;//配置GPIO端口,可具体到某一个端口或者自定义端口名称
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//输出频率50MHz
-
- GPIO_Init(L6235PD_EN_GPIO_PORT, &GPIO_InitStruct); //GPIO初始化函数,入口参数1为GPIOB,入口参数2为GPIO结构体首地址,因此&取地址
-
- }
- ////L6235PD-FND/REV 正反转-PB4
- //void L6235PD_FNDREV_GPIO_Config(void)
- //{
- // GPIO_InitTypeDef GPIO_InitStruct;//定义一个结构体
- // RCC_APB2PeriphClockCmd(L6235PD_FNDREV_GPIO_CLK, ENABLE);//调用APB2时钟控制指令(可定义一组外设/一组外设某一个接口/外设名字,使能)使能APB2总线时钟
- //
- // GPIO_InitStruct.GPIO_Pin = L6235PD_FNDREV_GPIO_PIN;//配置GPIO端口,可具体到某一个端口或者自定义端口名称
- // GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
- // GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//输出频率50MHz
- //
- // GPIO_Init(L6235PD_FNDREV_GPIO_PORT, &GPIO_InitStruct); //GPIO初始化函数,入口参数1为GPIOB,入口参数2为GPIO结构体首地址,因此&取地址
- //
- //}
- //L6235PD-Brake 制动PB3
- void L6235PD_BRAKE_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;//定义一个结构体
- RCC_APB2PeriphClockCmd(L6235PD_BRAKE_GPIO_CLK, ENABLE);//调用APB2时钟控制指令(可定义一组外设/一组外设某一个接口/外设名字,使能)使能APB2总线时钟
-
- GPIO_InitStruct.GPIO_Pin = L6235PD_BRAKE_GPIO_PIN;//配置GPIO端口,可具体到某一个端口或者自定义端口名称
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//输出频率50MHz
-
- GPIO_Init(L6235PD_BRAKE_GPIO_PORT, &GPIO_InitStruct); //GPIO初始化函数,入口参数1为GPIOB,入口参数2为GPIO结构体首地址,因此&取地址
-
- }
- //L6235PD 转子角度 霍尔H1/H2/H3
- void L6235PD_ANGLE_Config(void)
- {
-
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == 1)
- {
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == 0)
- {
- if( GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0)
- {
- printf("
- L6235PD_ANGLE_90
- ");
- }
- }
- }
-
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == 1)
- {
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == 1)
- {
- if( GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0)
- {
- printf("
- L6235PD_ANGLE_150
- ");
- }
- }
- }
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == 0)
- {
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == 1)
- {
- if( GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0)
- {
- printf("
- L6235PD_ANGLE_210
- ");
- }
- }
- }
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == 0)
- {
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == 0)
- {
- if( GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0)
- {
- printf("
- L6235PD_ANGLE_270
- ");
- }
- }
- }
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == 0)
- {
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == 1)
- {
- if( GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 1)
- {
- printf("
- L6235PD_ANGLE_330
- ");
- }
- }
- }
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == 1)
- {
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == 1)
- {
- if( GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 1)
- {
- printf("
- L6235PD_ANGLE_390
- ");
- }
- }
- }
-
- }
- //BLDC初始化
- void BLDC_VREF_TIM_Init(void)
- {
- //BLDC_TIM_Config(719,11,1);
- L6235PD_EN_GPIO_Config();
- //L6235PD_FNDREV_GPIO_Config();
- L6235PD_BRAKE_GPIO_Config();
- L6235PD_ANGLE_Config();
- }
复制代码
很简单的配置一下,然后修改我们的Demo,烧录。完美运行。
- else if(num == 16) //下发relay:16 REVERSE
- {
- TIM_SetCompare2(TIM4,100);//PB7-CH2
- printf("
- L6235PD_REVERSE
- ");
- }
- else if(num == 17) //下发relay:17 REVERSE
- {
- TIM_SetCompare2(TIM4,200);//PB7-CH2
- printf("
- L6235PD_REVERSE
- ");
- }
- else if(num == 18) //下发relay:18 REVERSE
- {
- TIM_SetCompare2(TIM4,300);//PB7-CH2
- printf("
- L6235PD_REVERSE
- ");
- }
- else if(num == 19) //下发relay:19 REVERSE
- {
- TIM_SetCompare2(TIM4,400);//PB7-CH2
- printf("
- L6235PD_REVERSE
- ");
- }
- else if(num == 20) //下发relay:20 REVERSE
- {
- TIM_SetCompare2(TIM4,500);//PB7-CH2
- printf("
- L6235PD_REVERSE
- ");
- }
复制代码
还算是比较简单的,
- /*
- * Copyright (c) 2016, Freescale Semiconductor, Inc.
- * Copyright 2016-2018 NXP
- * All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #include "mcdrv_adc_adc16.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- static bool_t s_statusPass;
- /*******************************************************************************
- * Code
- ******************************************************************************/
- /*!
- * [url=home.php?mod=space&uid=2666770]@Brief[/url] Initializes ADC driver to measure DC-bus current, DC-bus voltage
- * and BEMF voltage for BLDC sensorless algorithm
- *
- * [url=home.php?mod=space&uid=3142012]@param[/url] this Pointer to the current object
- * @param init Pointer to initialization structure
- *
- * [url=home.php?mod=space&uid=1141835]@Return[/url] boot_t true on success
- */
- bool_t MCDRV_Adc16Init(mcdrv_adc16_t *this, mcdrv_adc16_init_t *init)
- {
- s_statusPass = TRUE;
- /* offset filter window */
- this->ui16OffsetFiltWindow = 3;
- /* pointer to array with the channel numbers */
- this->pui16AdcArray = init->ui16AdcArray;
- /* default DC-bus current offset */
- this->ui16OffsetDcCurr = 0x3fff;
- /* check if there are one pair of assigned channels */
- if ((init->ui16AdcArray[MCDRV_ADC0_BEMFA] == MCDRV_CHAN_OFF) &&
- (init->ui16AdcArray[MCDRV_ADC1_BEMFA] == MCDRV_CHAN_OFF))
- {
- /* check if channel for phase A BEMF is assigned */
- s_statusPass = FALSE;
- }
- else if ((init->ui16AdcArray[MCDRV_ADC0_BEMFB] == MCDRV_CHAN_OFF) &&
- (init->ui16AdcArray[MCDRV_ADC1_BEMFB] == MCDRV_CHAN_OFF))
- {
- /* check if channel for phase B BEMF is assigned */
- s_statusPass = FALSE;
- }
- else if ((init->ui16AdcArray[MCDRV_ADC0_BEMFC] == MCDRV_CHAN_OFF) &&
- (init->ui16AdcArray[MCDRV_ADC1_BEMFC] == MCDRV_CHAN_OFF))
- {
- /* check if channel for phase C BEMF is assigned */
- s_statusPass = FALSE;
- }
- else if ((init->ui16AdcArray[MCDRV_ADC0_UDCB] == MCDRV_CHAN_OFF) &&
- (init->ui16AdcArray[MCDRV_ADC1_UDCB] == MCDRV_CHAN_OFF))
- {
- /* check if channel for DC-bus voltage is assigned */
- s_statusPass = FALSE;
- }
- else if ((init->ui16AdcArray[MCDRV_ADC0_IDCB] == MCDRV_CHAN_OFF) &&
- (init->ui16AdcArray[MCDRV_ADC1_IDCB] == MCDRV_CHAN_OFF))
- {
- /* check if channel for DC-bus current is assigned */
- s_statusPass = FALSE;
- }
- else
- {
- /* ADC module result register assignment */
- /* 0 -> result register 0 for ADC0 or ADC1 */
- /* 1 -> result register 1 for ADC0 or ADC1 */
- this->ui16IndexBemf = 0;
- this->ui16IndexAux = 0;
- this->ui16IndexUdcb = 1;
- this->ui16IndexIdcb = 1;
- /* BEMF phase A measurement */
- if (init->ui16AdcArray[MCDRV_ADC0_BEMFA] == MCDRV_CHAN_OFF)
- {
- this->pui32BemfAAdcBase = init->pui32Adc1Base;
- this->bldcAdc1SectorCfg[2] = init->ui16AdcArray[MCDRV_ADC1_BEMFA];
- this->bldcAdcSelCfg[2] = init->pui32Adc1Base;
- this->bldcAdc1SectorCfg[5] = init->ui16AdcArray[MCDRV_ADC1_BEMFA];
- this->bldcAdcSelCfg[5] = init->pui32Adc1Base;
- }
- else
- {
- this->pui32BemfAAdcBase = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[2] = init->ui16AdcArray[MCDRV_ADC0_BEMFA];
- this->bldcAdcSelCfg[2] = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[5] = init->ui16AdcArray[MCDRV_ADC0_BEMFA];
- this->bldcAdcSelCfg[5] = init->pui32Adc0Base;
- }
- /* BEMF phase B measurement */
- if (init->ui16AdcArray[MCDRV_ADC0_BEMFB] == MCDRV_CHAN_OFF)
- {
- this->pui32BemfBAdcBase = init->pui32Adc1Base;
- this->bldcAdc1SectorCfg[1] = init->ui16AdcArray[MCDRV_ADC1_BEMFB];
- this->bldcAdcSelCfg[1] = init->pui32Adc1Base;
- this->bldcAdc1SectorCfg[4] = init->ui16AdcArray[MCDRV_ADC1_BEMFB];
- this->bldcAdcSelCfg[4] = init->pui32Adc1Base;
- }
- else
- {
- this->pui32BemfBAdcBase = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[1] = init->ui16AdcArray[MCDRV_ADC0_BEMFB];
- this->bldcAdcSelCfg[1] = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[4] = init->ui16AdcArray[MCDRV_ADC0_BEMFB];
- this->bldcAdcSelCfg[4] = init->pui32Adc0Base;
- }
- /* BEMF phase C measurement */
- if (init->ui16AdcArray[MCDRV_ADC0_BEMFC] == MCDRV_CHAN_OFF)
- {
- this->pui32BemfCAdcBase = init->pui32Adc1Base;
- this->bldcAdc1SectorCfg[0] = init->ui16AdcArray[MCDRV_ADC1_BEMFC];
- this->bldcAdcSelCfg[0] = init->pui32Adc1Base;
- this->bldcAdc1SectorCfg[3] = init->ui16AdcArray[MCDRV_ADC1_BEMFC];
- this->bldcAdcSelCfg[3] = init->pui32Adc1Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32BemfCAdcBase->SC1[this->ui16IndexBemf] =
- (this->pui32BemfCAdcBase->SC1[this->ui16IndexBemf] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC1_BEMFC]));
- }
- else
- {
- this->pui32BemfCAdcBase = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[0] = init->ui16AdcArray[MCDRV_ADC0_BEMFC];
- this->bldcAdcSelCfg[0] = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[3] = init->ui16AdcArray[MCDRV_ADC0_BEMFC];
- this->bldcAdcSelCfg[3] = init->pui32Adc0Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32BemfCAdcBase->SC1[this->ui16IndexBemf] =
- (this->pui32BemfCAdcBase->SC1[this->ui16IndexBemf] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC0_BEMFC]));
- }
- /* DC-bus current measurement */
- if (init->ui16AdcArray[MCDRV_ADC0_IDCB] == MCDRV_CHAN_OFF)
- {
- this->pui32IdcbAdcBase = init->pui32Adc1Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] =
- (this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC1_IDCB]));
- }
- else
- {
- this->pui32IdcbAdcBase = init->pui32Adc0Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] =
- (this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC0_IDCB]));
- }
- /* auxiliary measurement */
- if (init->ui16AdcArray[MCDRV_ADC0_AUX] == MCDRV_CHAN_OFF)
- {
- this->pui32AuxAdcBase = init->pui32Adc1Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32AuxAdcBase->SC1[this->ui16IndexAux] =
- (this->pui32AuxAdcBase->SC1[this->ui16IndexAux] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC1_AUX]));
- }
- else
- {
- this->pui32AuxAdcBase = init->pui32Adc0Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32AuxAdcBase->SC1[this->ui16IndexAux] =
- (this->pui32AuxAdcBase->SC1[this->ui16IndexAux] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC0_AUX]));
- }
- /* DC-bus voltage measurement */
- if (init->ui16AdcArray[MCDRV_ADC0_UDCB] == MCDRV_CHAN_OFF)
- {
- this->pui32UdcbAdcBase = init->pui32Adc1Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32UdcbAdcBase->SC1[this->ui16IndexUdcb] =
- (this->pui32UdcbAdcBase->SC1[this->ui16IndexUdcb] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC1_UDCB]));
- }
- else
- {
- this->pui32UdcbAdcBase = init->pui32Adc0Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32UdcbAdcBase->SC1[this->ui16IndexUdcb] =
- (this->pui32UdcbAdcBase->SC1[this->ui16IndexUdcb] & ~(uint16_t)(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC0_UDCB]));
- }
- }
- return (s_statusPass);
- }
- /*!
- * @brief Function set new channel assignment for next BEMF voltage sensing
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_AssignBemfChannel(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- switch (this->ui16Sector)
- {
- /* BEMF phase C sensing */
- case 0:
- case 3:
- if ((this->bldcAdcSelCfg[this->ui16Sector]) == ADC0)
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc0SectorCfg[this->ui16Sector]));
- else
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc1SectorCfg[this->ui16Sector]));
- break;
- /* BEMF phase B sensing */
- case 1:
- case 4:
- if ((this->bldcAdcSelCfg[this->ui16Sector]) == ADC0)
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc0SectorCfg[this->ui16Sector]));
- else
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc1SectorCfg[this->ui16Sector]));
- break;
- /* BEMF phase A sensing */
- case 2:
- case 5:
- if ((this->bldcAdcSelCfg[this->ui16Sector]) == ADC0)
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc0SectorCfg[this->ui16Sector]));
- else
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc1SectorCfg[this->ui16Sector]));
- break;
- default:
- break;
- }
- return (s_statusPass);
- }
- /*!
- * @brief Function reads ADC result register containing actual BEMF voltage
- *
- * Result register value is shifted three times to the right and stored
- * to BEMF voltage pointer
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_BemfVoltageGet(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- switch (this->ui16Sector)
- {
- /* BEMF phase C sensing */
- case 0:
- case 3:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- /* BEMF phase B sensing */
- case 1:
- case 4:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- /* BEMF phase A sensing */
- case 2:
- case 5:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- /* default */
- default:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- }
- return (s_statusPass);
- }
- /*!
- * @brief Function reads ADC result register containing actual DC-bus voltage sample
- *
- * Result register value is shifted three times to the right and stored
- * to DC-bus voltage pointer
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_VoltDcBusGet(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* read DC-bus voltage sample from defined ADCx result register */
- *this->pf16UDcBus = (frac16_t)(MLIB_ShLSat_F16((this->pui32UdcbAdcBase->R[this->ui16IndexUdcb]), 3));
- return (s_statusPass);
- }
- /*!
- * @brief Function reads ADC result register containing actual DC-bus current sample
- *
- * Result register value is shifted three times to the right and stored
- * to DC-bus current pointer
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrDcBusGet(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* read DC-bus current channel sample from defined ADCx result register */
- *this->pf16IDcBus =
- (frac16_t)(MLIB_ShLSat_F16((this->pui32IdcbAdcBase->R[this->ui16IndexIdcb] - this->ui16OffsetDcCurr), 3));
- return (s_statusPass);
- }
- /*!
- * @brief Function reads ADC result register containing actual auxiliary sample
- *
- * Result register value is shifted 3 times right and stored to
- * auxiliary pointer
- *
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_AuxValGet(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* read auxiliary channel sample from defined ADCx result register */
- *this->pui16AuxChan = (frac16_t)(MLIB_ShLSat_F16((this->pui32AuxAdcBase->R[this->ui16IndexAux]), 3));
- return (s_statusPass);
- }
- /*!
- * @brief Function initializes phase current channel offset measurement
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrOffsetCalibInit(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* clear offset values */
- this->ui16OffsetDcCurr = 0x3fff;
- this->ui16CalibDcCurr = 0;
- /* initialize offset filters */
- this->ui16FiltDcCurr.u16Sh = this->ui16OffsetFiltWindow;
- GDFLIB_FilterMAInit_F16((frac16_t)0, &this->ui16FiltDcCurr);
- return (s_statusPass);
- }
- /*!
- * @brief Function reads current samples and filter them
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrOffsetCalib(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* sensing of DC Bus Current offset */
- this->ui16CalibDcCurr =
- GDFLIB_FilterMA_F16((frac16_t) this->pui32IdcbAdcBase->R[this->ui16IndexIdcb], &this->ui16FiltDcCurr);
- return (s_statusPass);
- }
- /*!
- * @brief Function passes measured offset values to main structure
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrOffsetCalibSet(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* pass Calib data for DC Bus current offset */
- this->ui16OffsetDcCurr = (this->ui16CalibDcCurr);
- return (s_statusPass);
- }
复制代码
- /*
- * Copyright (c) 2016, Freescale Semiconductor, Inc.
- * Copyright 2016-2018 NXP
- * All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #include "mcdrv_adc_adc16.h"
- #include "mcdrv_adc_adc16_frdm_ke16.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- static bool_t s_statusPass;
- static volatile bool_t s_bkey = TRUE;
- /*******************************************************************************
- * Code
- ******************************************************************************/
- /*!
- * @brief Initializes ADC driver to measure DC-bus current, DC-bus voltage
- * and BEMF voltage for BLDC sensorless algorithm
- *
- * Board specific file for the tower kv11
- *
- * @param this Pointer to the current object
- * @param init Pointer to initialization structure
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_Adc16Init_frdm_ke16(mcdrv_adc16_t *this, mcdrv_adc16_init_t *init)
- {
- s_statusPass = TRUE;
- /* offset filter window */
- this->ui16OffsetFiltWindow = 3;
- /* pointer to array with the channel numbers */
- this->pui16AdcArray = init->ui16AdcArray;
- /* default DC-bus current offset */
- this->ui16OffsetDcCurr = 0x3fff;
- /* ADC module result register assignment */
- /* 0 -> result register 0 for ADC0 */
- /* 1 -> result register 1 for ADC0 */
- /* 2 -> result register 2 for ADC0 */
- this->ui16IndexBemf = 0;
- this->ui16IndexUdcb = 1;
- this->ui16IndexIdcb = 2;
- /* BEMF phase A measurement */
- this->pui32BemfAAdcBase = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[2] = init->ui16AdcArray[MCDRV_ADC0_BEMFA];
- this->bldcAdcSelCfg[2] = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[5] = init->ui16AdcArray[MCDRV_ADC0_BEMFA];
- this->bldcAdcSelCfg[5] = init->pui32Adc0Base;
- /* BEMF phase B measurement */
- this->pui32BemfBAdcBase = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[1] = init->ui16AdcArray[MCDRV_ADC0_BEMFB];
- this->bldcAdcSelCfg[1] = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[4] = init->ui16AdcArray[MCDRV_ADC0_BEMFB];
- this->bldcAdcSelCfg[4] = init->pui32Adc0Base;
- /* BEMF phase C measurement */
- this->pui32BemfCAdcBase = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[0] = init->ui16AdcArray[MCDRV_ADC0_BEMFC];
- this->bldcAdcSelCfg[0] = init->pui32Adc0Base;
- this->bldcAdc0SectorCfg[3] = init->ui16AdcArray[MCDRV_ADC0_BEMFC];
- this->bldcAdcSelCfg[3] = init->pui32Adc0Base;
-
- /* Set ADC_SC1_ADCH bitfield for BEMFC */
- this->pui32BemfCAdcBase->SC1[this->ui16IndexBemf] =
- (this->pui32BemfCAdcBase->SC1[this->ui16IndexBemf] & ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC0_BEMFC]));
- /* DC-bus current measurement */
- this->pui32IdcbAdcBase = init->pui32Adc0Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] =
- (this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] & ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC0_IDCB]));
- /* DC-bus voltage measurement */
- this->pui32UdcbAdcBase = init->pui32Adc0Base;
- /* Set ADC_SC1_ADCH bitfield */
- this->pui32UdcbAdcBase->SC1[this->ui16IndexUdcb] =
- (this->pui32UdcbAdcBase->SC1[this->ui16IndexUdcb] & ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(init->ui16AdcArray[MCDRV_ADC0_UDCB]));
- return (s_statusPass);
- }
- /*!
- * @brief Function set new channel assignment for next BEMF voltage sensing
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_AssignBemfChannel_frdm_ke16(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- switch (this->ui16Sector)
- {
- /* BEMF phase C sensing */
- case 0:
- case 3:
- if ((this->bldcAdcSelCfg[this->ui16Sector]) == ADC0)
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc0SectorCfg[this->ui16Sector]));
- else
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc1SectorCfg[this->ui16Sector]));
- break;
- /* BEMF phase B sensing */
- case 1:
- case 4:
- if ((this->bldcAdcSelCfg[this->ui16Sector]) == ADC0)
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc0SectorCfg[this->ui16Sector]));
- else
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc1SectorCfg[this->ui16Sector]));
- break;
- /* BEMF phase A sensing */
- case 2:
- case 5:
- if ((this->bldcAdcSelCfg[this->ui16Sector]) == ADC0)
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc0SectorCfg[this->ui16Sector]));
- else
- /* Set ADC_SC1_ADCH bitfield */
- this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] =
- (this->bldcAdcSelCfg[this->ui16Sector]->SC1[this->ui16IndexBemf] &
- ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->bldcAdc1SectorCfg[this->ui16Sector]));
- break;
- default:
- break;
- }
- return (s_statusPass);
- }
- /*!
- * @brief Function reads ADC result register containing actual BEMF voltage
- *
- * Result register value is shifted three times to the right and stored
- * to BEMF voltage pointer
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_BemfVoltageGet_frdm_ke16(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- switch (this->ui16Sector)
- {
- /* BEMF phase C sensing */
- case 0:
- case 3:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- /* BEMF phase B sensing */
- case 1:
- case 4:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- /* BEMF phase A sensing */
- case 2:
- case 5:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- /* default */
- default:
- *this->pf16BemfVoltage =
- (frac16_t)(MLIB_ShLSat_F16((this->bldcAdcSelCfg[this->ui16Sector]->R[this->ui16IndexBemf]), 3));
- break;
- }
- return (s_statusPass);
- }
- /*!
- * @brief Function reads ADC result register containing actual DC-bus voltage sample
- *
- * Result register value is shifted three times to the right and stored
- * to DC-bus voltage pointer
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_VoltDcBusGet_frdm_ke16(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* read DC-bus voltage sample from defined ADCx result register */
- *this->pf16UDcBus = (frac16_t)(MLIB_ShLSat_F16((this->pui32UdcbAdcBase->R[this->ui16IndexUdcb]), 3));
-
- return (s_statusPass);
- }
- /*!
- * @brief Function reads ADC result register containing actual DC-bus current sample
- *
- * Result register value is shifted three times to the right and stored
- * to DC-bus current pointer
- *0
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrDcBusGet_frdm_ke16(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- *this->pf16IDcBus =
- (frac16_t)(MLIB_ShLSat_F16((this->pui32IdcbAdcBase->R[this->ui16IndexIdcb] - this->ui16OffsetDcCurr), 3));
- return (s_statusPass);
- }
- /*!
- * @brief Function reads current samples and filter them
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrOffsetCalib_frdm_ke16(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* sensing of DC Bus Current offset */
- this->ui16CalibDcCurr =
- GDFLIB_FilterMA_F16((frac16_t) this->pui32IdcbAdcBase->R[this->ui16IndexIdcb], &this->ui16FiltDcCurr);
- return (s_statusPass);
- }
- /*!
- * @brief Function initializes phase current channel offset measurement
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrOffsetCalibInit_frdm_ke16(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* clear offset values */
- this->ui16OffsetDcCurr = 0x3fff;
- this->ui16CalibDcCurr = 0;
- /* initialize offset filters */
- this->ui16FiltDcCurr.u16Sh = this->ui16OffsetFiltWindow;
- GDFLIB_FilterMAInit_F16((frac16_t)0, &this->ui16FiltDcCurr);
- return (s_statusPass);
- }
- /*!
- * @brief Function passes measured offset values to main structure
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_CurrOffsetCalibSet_frdm_ke16(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- /* pass Calib data for DC Bus current offset */
- this->ui16OffsetDcCurr = (this->ui16CalibDcCurr);
- return (s_statusPass);
- }
- /*!
- * @brief Function set new channel assignment for next BEMF voltage sensing
- *
- * @param this Pointer to the current object
- *
- * @return boot_t true on success
- */
- bool_t MCDRV_AssignDCBusChannel(mcdrv_adc16_t *this)
- {
- s_statusPass = TRUE;
- this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] =
- (this->pui32IdcbAdcBase->SC1[this->ui16IndexIdcb] & ~(ADC_SC1_ADCH(ADC_SC1_ADCH_MASK))) |
- (ADC_SC1_ADCH(this->pui16AdcArray[MCDRV_ADC0_IDCB]));
- return (s_statusPass);
- }
复制代码
`
|