#include "stdbool.h"
#include "stm32f2xx_tim.h"
#include "CMPHY_Common.h"
#include "CMPHY_VPWM.h"
#include "CMPHY_PWM.h"
#define PWMn 1
const VPWM PWM[PWMn]={
{ { GPIOB,RCC_AHB1Periph_GPIOB,GPIO_Pin_4,GPIO_PinSource4,
TIM3,RCC_APB1Periph_TIM3,GPIO_AF_TIM3,
1,TIM_IT_CC1,TIM_FLAG_CC1},
{ GPIOB,RCC_AHB1Periph_GPIOB,GPIO_Pin_0,GPIO_PinSource0,
TIM3,RCC_APB1Periph_TIM3,GPIO_AF_TIM3,
3,TIM_IT_CC3,TIM_FLAG_CC3},
{0}
}
};
//const VPWM PWM[PWMn]={
// { { GPIOB,RCC_AHB1Periph_GPIOB,GPIO_Pin_4,GPIO_PinSource4,
// TIM3,RCC_APB1Periph_TIM3,GPIO_AF_TIM3,
// 1,TIM_IT_CC1,TIM_FLAG_CC1},
// { GPIOA,RCC_AHB1Periph_GPIOA,GPIO_Pin_9,GPIO_PinSource9,
// TIM1,RCC_APB2Periph_TIM1,GPIO_AF_TIM1,
// 2,TIM_IT_CC2,TIM_FLAG_CC2},
// { GPIOB,RCC_AHB1Periph_GPIOB,GPIO_Pin_0,GPIO_PinSource0,
// TIM1,RCC_APB2Periph_TIM1,GPIO_AF_TIM1,
// 2,TIM_IT_CC2,TIM_FLAG_CC2}
// }
//};
//const VPWM PWM[PWMn]={ GPIOB,GPIOB,
// RCC_AHB1Periph_GPIOB,RCC_AHB1Periph_GPIOB,
// GPIO_Pin_4,GPIO_Pin_0,
// GPIO_PinSource4,GPIO_PinSource0,
// TIM3,TIM3,
// RCC_APB1Periph_TIM3,RCC_APB1Periph_TIM3,
// GPIO_AF_TIM3,GPIO_AF_TIM3,
// 1,3,
// TIM_IT_CC1,TIM_IT_CC3,
// TIM_FLAG_CC1,TIM_FLAG_CC3
//};
VPWM_DATA PWM_Data[PWMn]={0};
__IO static PWM_PORT g_iPWM;
//------------------------------------------------------------------------------
// Funtion: 初始化PWM处理模块(进入系统时必须初始化)
// Input : none
// Output : none
// Return : none
// Info : none
//------------------------------------------------------------------------------
void PWM_Init()
{
uint8_t i;
for(i=0;i<PWMn;i++){
VPWM_Init((VPWM*)&PWM[i]);
}
}
void TIM3_IRQHandler(void)
{
VPWM_TxIsr((VPWM*)&PWM[PWMPORT1],&PWM_Data[PWMPORT1]);
}
//------------------------------------------------------------------------------
// 功能:J1850 - PWM 数据发送函数
// 输入:chbuf - 待发送命令
// chlen - 发送长度
// 输出:无
// 返回:true 表示发送成功 false 表示发送失败
// 附加:无
//------------------------------------------------------------------------------
char PWM_Send( unsigned char *chbuf, unsigned char chlen )
{
uint32_t num=(uint32_t)((chlen<<3)+2);
//get space
PWM_Data[g_iPWM].Period=(uint16_t *)malloc(num<<1);
if(PWM_Data[g_iPWM].Period==0) return false;
PWM_Data[g_iPWM].Duty =(uint16_t *)malloc(num<<1);
if(PWM_Data[g_iPWM].Duty == 0){
free(PWM_Data[g_iPWM].Period);
return false;
}
//data convert
if(!DatToPWM(&PWM_Data[g_iPWM],chbuf,chlen)) return false;
//Check BUS
// if(!VPWM_CheckBus((VPWM*)&PWM[g_iPWM])) return false;
// SetDelay(16);//SOF
// WaitDelay();
VPWM_Output((VPWM*)&PWM[g_iPWM],&PWM_Data[g_iPWM]);
// SetDelay(32);//EOF
// WaitDelay();
free(PWM_Data[g_iPWM].Period);
free(PWM_Data[g_iPWM].Duty);
return true;
}
//------------------------------------------------------------------------------
// Funtion: 接收PWM数据
// Input : chlen 接受长度
// ms 超时时间
// Output : chbuf 接受缓冲区
// Return : true/false
// Info : none
//------------------------------------------------------------------------------
uint32_t PWM_Receive( unsigned char *chbuf,unsigned char chlen,unsigned int *firstms,unsigned int ms)
{
uint32_t num=(uint32_t)((chlen<<3)+2);
//get space
PWM_Data[g_iPWM].HighTime=(uint16_t *)malloc(num<<1);
if(PWM_Data[g_iPWM].HighTime== 0)
return 0;
PWM_Data[g_iPWM].LowTime =(uint16_t *)malloc(num<<1);
if(PWM_Data[g_iPWM].LowTime == 0)
{
free(PWM_Data[g_iPWM].HighTime);
return 0;
}
PWM_Data[g_iPWM].Index=0;
PWM_Data[g_iPWM].Num=num;
*firstms=VPWM_Input((VPWM*)&PWM[g_iPWM],&PWM_Data[g_iPWM],TIM_ICPolarity_Falling/*TIM_ICPolarity_Rising*/,ms*1000)/1000;
num=PWMtoDat(&PWM_Data[g_iPWM],chbuf,chlen);
free(PWM_Data[g_iPWM].HighTime);
free(PWM_Data[g_iPWM].LowTime);
return num;
// return PWM_Input((VPWM*)&PWM[g_iPWM],chbuf,chlen,ms);
}
//------------------------------------------------------------------------------
// Funtion: 配置并打开PWM,所有收发操作前必须打开串口
// Input : port - 目标串口
// PortmodePortmode - 串行口参数
// Output : none
// Return : PWM已被打开时返回 false
// PWM串口成功返回 true
// Info : 请注意给出接收 FIFO 的长度,最好将 FIFO 设置为 1
//------------------------------------------------------------------------------
char PWM_Open( PWM_PORT port )
{
g_iPWM = port;
}
//------------------------------------------------------------------------------
// Funtion: 关闭当前PWM
// Input : port - 目标PWM
// Output : none
// Return : none
// Info : none
//------------------------------------------------------------------------------
void PWM_Close( PWM_PORT port )
{
} |