打印

PT32L007F8P7K开发板

[复制链接]
1755|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
#申请开发板#
PT32L007x数据手册1.1.pdf (2.88 MB)






PT32L007系列产品,支持最高主频64MHz,1.8~5.5v,Flash 64Kbyte,RAM 16Kbyte,深度睡眠时候的待机功耗为0.3微安,支持TSSOP20/QFN20等封装,广泛使用于无线模块、无磁水表、无线烟感等领域。同期也推出了PT32F007系列产品,该系列产品支持2.2~5.5v,Flash 64Kbyte,RAM 16Kbyte等,广泛应用于小家电、仪表盘等市场。超值系列产品还包括PT32Y003,PT32F/L005等产品。


更详细的规格请参考数据手册!附件已经提供数据手册


使用特权

评论回复

相关帖子

沙发
rickluo|  楼主 | 2024-3-22 09:02 | 只看该作者
#技术资源#  

PT32L007 参考手册

PRM0008 PT32x007x参考手册1.1.zip (6.17 MB)

附件有点大,上传压缩包 ,需要了自行下载解压




使用特权

评论回复
板凳
rickluo|  楼主 | 2024-3-22 09:07 | 只看该作者
#技术资源# PAI-IC.PT32x007x_DFP.1.5.0.zip (4.21 MB)
最新版本库文件,持续更新



批量出货芯片表面无丝印,避免二次打磨损伤






使用特权

评论回复
地板
rickluo|  楼主 | 2024-3-25 14:31 | 只看该作者
PT32L007  ADC 例程分享:

/******************************************************************************
  * @file    PT32x007x_adc.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief   This file provides firmware functions to manage the following
  *          functionalities of the ADC peripheral:
  *           + Initialization and Configuration
  *           + Interrupts and flags management
  *         
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_adc.h"


/** @defgroup ADC
  * @brief ADC driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define Init_CR1_MASK        (u32)0xFFC0F401
#define Init_CR2_MASK        (u32)0xFF00FFFF
#define SetSampleTime_CR2_MASK   (u32)0xFFFF00FF

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initializes the ADCx peripheral according to the specified parameters
  *         in the ADC_InitStruct.
  * @NOTE   This function is used to configure the global features of the ADC (
  *         Resolution, Data Alignment, continuous mode activation, External
  *         trigger source and edge, Sequence Scan Direction).
  * @param  ADCx: where x can be 1 to select the ADC peripheral.
  * @param  ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains
  *         the configuration information for the specified ADC peripheral.
  * @retval None
  */
void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
{
        u32 tmpreg = 0;
        /* Check the parameters */
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_ADC_Prescaler(ADC_InitStruct->ADC_Prescaler));
        assert_param(IS_ADC_Mode(ADC_InitStruct->ADC_Mode));
  assert_param(IS_ADC_TriggerSource(ADC_InitStruct->ADC_TriggerSource));
        assert_param(IS_ADC_TimerTriggerSource(ADC_InitStruct->ADC_TimerTriggerSource));
        assert_param(IS_ADC_Align(ADC_InitStruct->ADC_Align));
        assert_param(IS_ADC_Channel(ADC_InitStruct->ADC_Channel));
        assert_param(IS_ADC_BGVoltage(ADC_InitStruct->ADC_BGVoltage));
        assert_param(IS_ADC_ReferencePositive(ADC_InitStruct->ADC_ReferencePositive));  

        tmpreg = ADCx->CR1;
        tmpreg &= Init_CR1_MASK;
        tmpreg  |= ((ADC_InitStruct->ADC_Mode) |\
                           (ADC_InitStruct->ADC_TriggerSource)|\
                (ADC_InitStruct->ADC_TimerTriggerSource)|\
                (ADC_InitStruct->ADC_Align)|\
                (ADC_InitStruct->ADC_Channel)|\
                (ADC_InitStruct->ADC_BGVoltage)|\
                (ADC_InitStruct->ADC_ReferencePositive));
        ADCx->CR1 = tmpreg;
   
    tmpreg = ADCx->CR2;
        tmpreg &= Init_CR2_MASK;
    tmpreg |= ((ADC_InitStruct->ADC_Prescaler)<<16);
    ADCx->CR2 = tmpreg;
}

/**
  * @brief  Fills each ADC_InitStruct member with its default value.
  * @note   This function is used to initialize the global features of the ADC (
  *         Resolution, Data Alignment, continuous mode activation, External
  *         trigger source and edge, Sequence Scan Direction).
  * @param  ADC_InitStruct: pointer to an ADC_InitTypeDef structure which will
  *         be initialized.
  * @retval None
  */
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
{
        ADC_InitStruct->ADC_Prescaler = 2;                                                         //ADC二分频,8/2=4 MHz
        ADC_InitStruct->ADC_Mode = ADC_Mode_Single;                                                //单次转换模式
  ADC_InitStruct->ADC_TriggerSource = ADC_TriggerSource_Software;
        ADC_InitStruct->ADC_TimerTriggerSource=ADC_TimerTriggerSource_TIM1ADC;                                                //定时源触发选择TIM0事件
        ADC_InitStruct->ADC_Align = ADC_Align_Right;                                        //右对齐
        ADC_InitStruct->ADC_Channel=ADC_Channel_0;                                                //通道0,PC13
        ADC_InitStruct->ADC_ReferencePositive= ADC_ReferencePositive_VDD;        //选择VDDA作为正端参考电平
        ADC_InitStruct->ADC_BGVoltage=ADC_BGVoltage_BG1v2;
}

/**
  * @brief  Enables or disables the specified ADC peripheral.
  * @param  ADCx: where x can be 1 to select the ADC1 peripheral.
  * @param  NewState: new state of the ADCx peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Set the ADEN bit to Enable the ADC peripheral */
                ADCx->CR1 |= ADC_CR1_EN;
        }
        else
        {
                /* Reset the ADEN to Disable the ADC peripheral */
                ADCx->CR1 &= ~ADC_CR1_EN;
        }
}



/**
  * @brief  Configures for the selected ADC and its sampling time.
  * @param  ADCx: where x can be 1 to select the ADC peripheral.
  * @param  ADC_Channel: the ADC channel to configure.
  *          This parameter can be any combination of the following values:
  *            @ARG ADC_Channel_0: ADC Channel0 selected
  *            @arg ADC_Channel_1: ADC Channel1 selected
  *            @arg ADC_Channel_2: ADC Channel2 selected
  *            @arg ADC_Channel_3: ADC Channel3 selected
  *            @arg ADC_Channel_4: ADC Channel4 selected
  *            @arg ADC_Channel_5: ADC Channel5 selected
  *            @arg ADC_Channel_6: ADC Channel6 selected
  *            @arg ADC_Channel_7: ADC Channel7 selected
  *            @arg ADC_Channel_8: ADC Channel8 selected
  *            @arg ADC_Channel_9:
  *            @arg ADC_Channel_10:
  *            @arg ADC_Channel_11:
  * @retval None
  */
void ADC_ChannelConfig(ADC_TypeDef* ADCx, u32 ADC_Channel)
{
        u32 tmpreg=0;
        /* Check the parameters */
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_ADC_Channel(ADC_Channel));       
        tmpreg = ADCx->CR1;
        tmpreg &= ~ADC_CR1_CHS;
        tmpreg |= ADC_Channel;
        ADCx->CR1 = tmpreg;
}

void ADC_StartOfConversion(ADC_TypeDef* ADCx)
{
    assert_param(IS_ADC_ALL_PERIPH(ADCx));
    ADCx->CR1 |= ADC_CR1_SOC;
}

/**
  * @brief  Returns the last ADCx conversion result data for ADC channel.
  * @param  ADCx: where x can be 1 to select the ADC1 peripheral.
  * @retval The Data conversion value.
  */
u16 ADC_GetConversionValue(ADC_TypeDef* ADCx)
{
        /* Check the parameters */
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        /* Return the selected ADC conversion value */
        return ADCx->DR;
}

void ADC_ITConfig(ADC_TypeDef* ADCx, u32 ADC_IT, FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_ADC_ALL_PERIPH(ADCx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    assert_param(IS_ADC_IT(ADC_IT));

    if (NewState != DISABLE)
    {
        /* Enable the selected ADC interrupts */
        ADCx->CR1 |= ADC_IT;
    }
    else
    {
        /* Disable the selected ADC interrupts */
        ADCx->CR1 &= (~ADC_IT);
    }
}

/**
  * @brief  Checks whether the specified ADC flag is set or not.
  * @param  ADCx: where x can be 1 to select the ADC1 peripheral.
  * @param  ADC_FLAG: specifies the flag to check.
  *          This parameter can be one of the following values:
  *            @arg ADC_FLAG_EOC: End of conversion flag
  *            @arg ADC_FLAG_RDY: ADC Ready flag
  * @retval The new state of ADC_FLAG (SET or RESET).
  */
FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, u32 ADC_FLAG)
{
        FlagStatus bitstatus = RESET;
        /* Check the parameters */
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_ADC_FLAG(ADC_FLAG));
        /* Check the status of the specified ADC flag */
        if ((ADCx->SR & ADC_FLAG) != (u32)RESET)
        {
                /* ADC_FLAG is set */
                bitstatus = SET;
        }
        else
        {
                /* ADC_FLAG is reset */
                bitstatus = RESET;
        }
        /* Return the ADC_FLAG status */
        return  bitstatus;
}

/**
  * @brief ADC Average Command
  * @param ADCx:ADC Peripheral
  * @param NewState:FunctionalState type variable
  * @retval None
  */
void ADC_AverageCmd(ADC_TypeDef* ADCx,FunctionalState NewState)
{
    assert_param(IS_ADC_ALL_PERIPH(ADCx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    if(NewState!=DISABLE)
    {
        ADCx->CR1 |= ADC_CR1_AVGE;
    }
    else
    {
        ADCx->CR1 &= (~ADC_CR1_AVGE);
    }               
}

/**
  * @brief ADC Average Times Config
  * @param ADCx:ADC Peripheral
  * @param Times: the time of average conversion
        * @arg ADC_AverageTimes_1: Average conversion 1 times                                         
        * @arg ADC_AverageTimes_2: Average conversion 2 times                                                                                 
        * @arg ADC_AverageTimes_4: Average conversion 4 times                                                                                 
        * @arg ADC_AverageTimes_8: Average conversion 8 times                                                                                 
        * @arg ADC_AverageTimes_16: Average conversion 16 times                                                                                 
        * @arg ADC_AverageTimes_32: Average conversion 32 times                                                                                 
        * @arg ADC_AverageTimes_64: Average conversion 64 times                                         
        * @arg ADC_AverageTimes_128: Average conversion 128 times                                          
  * @retval None
  */
void ADC_AverageTimesConfig(ADC_TypeDef* ADCx,u32 Times)
{
    u32 tmpreg=0;       
    assert_param(IS_ADC_ALL_PERIPH(ADCx));
    assert_param(IS_ADC_AverageTimes(Times));

    tmpreg = ADCx->CR2;
    tmpreg &= (~ADC_CR2_AVGT);
    tmpreg |= Times;
    ADCx->CR2 = tmpreg;       
}

/**
  * @brief ADC Ready Time Config
  * @param ADCx:ADC Peripheral
  * @param ReadyTime:0x0-0xFF
  * @retval None
  */
void ADC_ReadyTimeConfig(ADC_TypeDef* ADCx,u32 ReadyTime)
{
    u32 tmpreg=0;
    assert_param(IS_ADC_ALL_PERIPH(ADCx));

    tmpreg = ADCx->CR2;
    tmpreg &= (~ADC_CR2_RDTC);
    tmpreg |= ReadyTime;
    ADCx->CR2 = tmpreg;       
}

/**
  * @brief  Set ADC sample times the specified ADC peripheral.
  * @param  ADCx: where x can be 1 to select the ADC1 peripheral.
  * @param  sampletime: sample time value (3~255).
  * @retval None
  */
void ADC_SampleTimeConfig(ADC_TypeDef* ADCx, u8 SampleTime)
{
    u32 tmpreg = 0;
        /* Check the parameters */
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_ADC_SampleTime(SampleTime));   
    tmpreg = ADCx->CR2;
    tmpreg &= SetSampleTime_CR2_MASK;
        tmpreg |= (SampleTime << 8);
    ADCx->CR2 = tmpreg;
}

/**
  * @brief ADC Scan Command
  * @param ADCx:ADC Peripheral
  * @param NewState:FunctionalState type variable
  * @retval None
  */
void ADC_ScanCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_ADC_ALL_PERIPH(ADCx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));

    if (NewState != DISABLE)
    {
        /* Set the SCANE bit to Enable the ADC scan */
        ADCx->CR1 |= ADC_CR1_SCANE;
    }
    else
    {
        ADCx->CR1 &= (~ADC_CR1_SCANE);
    }
}

/**
  * @brief ADC Scan Channel Config
  * @param ADCx:ADC Peripheral
  * @param ADC_Channel:ADC Peripheral
  * @param ADC_Channel:the ADC channel to configure.
        *          This parameter can be one of the following values:
  *            @arg ADC_Channel_0: ADC Channel0 selected
  *            @arg ADC_Channel_1: ADC Channel1 selected
  *            @arg ADC_Channel_2: ADC Channel2 selected
  *            @arg ADC_Channel_3: ADC Channel3 selected
  *            @arg ADC_Channel_4: ADC Channel4 selected
  *            @arg ADC_Channel_5: ADC Channel5 selected
  *            @arg ADC_Channel_6: ADC Channel6 selected
  *            @arg ADC_Channel_7: ADC Channel7 selected
  *            @arg ADC_Channel_8: ADC Channel8 selected
  *            @arg ADC_Channel_9: ADC Channel9 selected
  *            @arg ADC_Channel_10: ADC Channel10 selected
  *            @arg ADC_Channel_11: ADC Channel11 selected
  *            @arg ADC_Channel_12: ADC Channel12 selected
  *            @arg ADC_Channel_13: ADC Channel13 selected
  *            @arg ADC_Channel_14: ADC Channel14 selected
  *            @arg ADC_Channel_15: ADC Channel15 selected
        *            @arg ADC_Channel_16: ADC Channel16 selected
  *            @arg ADC_Channel_17: ADC Channel17 selected
  *            @arg ADC_Channel_18: ADC Channel18 selected
  * @param ScanChannel: ADC scan channel (0~19)
  * @retval None
  */
void ADC_ScanChannelConfig(ADC_TypeDef* ADCx,u32 ADC_Channel,u32 ScanChannel)
{
    u32 tmpreg=0;          
    assert_param(IS_ADC_ALL_PERIPH(ADCx));
    assert_param(IS_ADC_Channel(ADC_Channel));
    assert_param(IS_ADC_ScanChannel(ScanChannel));       

    if(ScanChannel <6)
    {
        tmpreg = ADCx->SCHR1;          
        tmpreg &= (~(ADC_SCHR1_CH0<<(ScanChannel*5)));
        tmpreg |= ((ADC_Channel>>16)<<ScanChannel*5);
        ADCx->SCHR1 = tmpreg;
    }
   else
    {
        tmpreg = ADCx->SCHR2;          
        tmpreg &= (~(ADC_SCHR2_CH6<<((ScanChannel-6)*5)));
        tmpreg |= ((ADC_Channel>>16)<<(ScanChannel-6)*5);
        ADCx->SCHR2 = tmpreg;
    }
}

/**
  * @brief ADC Scan Channel Number Config
  * @param ADCx:ADC Peripheral
  * @param ScanNumber: the number of ADC scan channel (2~20)
  * @retval None
  */
void ADC_ScanChannelNumberConfig(ADC_TypeDef* ADCx,u32 ScanNumber)
{
    u32 tmpreg=0;          
    assert_param(IS_ADC_ALL_PERIPH(ADCx));
    assert_param(IS_ADC_ScanChannelNumber(ScanNumber));       

    tmpreg = ADCx->SCHR2;          
    tmpreg &= (~ADC_SCHR2_SCNT);
    tmpreg |= ((ScanNumber-1) << 16);
    ADCx->SCHR2 = tmpreg;
}

u16 ADC_GetScanData(ADC_TypeDef* ADCx,u32 ScanChannel)
{
        /* Check the parameters */
        u16 data;
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_ADC_ScanChannel(ScanChannel));
        /* Return the selected ADC conversion value */
        if (ScanChannel==0)
        {
                data = ADCx->SCHDR0;
        }
        else if(ScanChannel==1)
        {
          data = ADCx->SCHDR1;
        }
        else if(ScanChannel==2)
        {
                data = ADCx->SCHDR2;
        }
        else if(ScanChannel==3)
        {
                data = ADCx->SCHDR3;
        }
        else if(ScanChannel==4)
        {
                data = ADCx->SCHDR4;
        }
        else if(ScanChannel==5)
        {
                data = ADCx->SCHDR5;
        }
        else if(ScanChannel==6)
        {
                data = ADCx->SCHDR6;
        }
        else
        {
                data = ADCx->SCHDR7;
        }

        return data;
}

/**
  * @brief  Configure the input negative for the specified ADC.
  * @param         ADCx: ADC Peripheral.
  * @param  SEL: the Input Negative of the ADC.
  *          This parameter can be one of the following values:
        *                                         @arg ADC_InputNegative_VSS: select the VSS as Input Negative
        *                                         @arg ADC_InputNegative_INN: select the INN as Input Negative
  * @retval None
  */
void ADC_InputNegative(ADC_TypeDef *ADCx, u32 SEL)
{
        u32 tmpreg = 0;
        /* Check the parameters */
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_ADC_InputNegative(SEL));
        tmpreg = ADCx->CR1;
        tmpreg &= (~ADC_CR1_INNS);
        tmpreg |= SEL;
        ADCx->CR1 = tmpreg;
}


/**
  * @brief         ADC BG_2v0 config
  * @param         ADCx:ADC Peripheral
  * @param  NewState: new state of the ADC BG 2v0 mode.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ADC_BG2v0Config(ADC_TypeDef *ADCx, FunctionalState NewState)
{
        assert_param(IS_ADC_ALL_PERIPH(ADCx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));

        if(NewState != DISABLE)
        {
                ADCx->BGCR |= ADC_BGCR_BGE;
        }
        else
        {
                ADCx->BGCR &= (~ADC_BGCR_BGE);
        }
}
/**
  * @}
  */
  

使用特权

评论回复
5
rickluo|  楼主 | 2024-3-25 14:33 | 只看该作者
/******************************************************************************
  * @file    PT32x007x_crc.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the CRC peripheral:
  *           + Initialization and Configuration
  *           + Interrupts and flags management
  *         
  *         
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_crc.h"

#define Init_CR_MASK        (u32)0xFFFFFFC3

/** @defgroup CRC
  * @brief CRC driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Initializes the CRC peripheral according to the specified parameters
  *         in the CRC_InitStruct.
  * @param  CRC_InitStruct: pointer to an CRC_InitTypeDef structure that contains
  *         the configuration information for the specified CRC peripheral.
  * @retval None
  */
void CRC_Init(CRC_InitTypeDef* CRC_InitStruct)
{
        u32 tmpreg = 0;
        /* Check the parameters */
        assert_param(IS_CRC_Poly(CRC_InitStruct->CRC_Poly));
        assert_param(IS_CRC_Input(CRC_InitStruct->CRC_Input));
        assert_param(IS_CRC_InputBitSequenceReversal(CRC_InitStruct->CRC_InputBitSequenceReversal));
        assert_param(IS_CRC_InputByteSequenceReversal(CRC_InitStruct->CRC_InputByteSequenceReversal));
        assert_param(IS_CRC_OutputBitSequenceReversal(CRC_InitStruct->CRC_OutputBitSequenceReversal));
        tmpreg = CRC->CR;
        tmpreg &= Init_CR_MASK;
        tmpreg  |= ((u32)(CRC_InitStruct->CRC_Input));
        tmpreg  |= ((u32)(CRC_InitStruct->CRC_InputBitSequenceReversal));
        tmpreg  |= ((u32)(CRC_InitStruct->CRC_InputByteSequenceReversal));
        tmpreg  |= ((u32)(CRC_InitStruct->CRC_OutputBitSequenceReversal));
        /* Write to CRC CR */
        CRC->CR = tmpreg;
        CRC->SEED = CRC_InitStruct->CRC_Seed;
        CRC->POLY = CRC_InitStruct->CRC_Poly;
}

/**
  * @brief  Fills each CRC_InitStruct member with its default value.
  * @param  CRC_InitStruct: pointer to an CRC_InitTypeDef structure which will
  *         be initialized.
  * @retval None
  */
void CRC_StructInit(CRC_InitTypeDef* CRC_InitStruct)
{       
        /* Initialize the CRC data input width */
        CRC_InitStruct->CRC_Input = CRC_Input_16b;
        /* Initialize the CRC data input bit reverse function */
        CRC_InitStruct->CRC_InputBitSequenceReversal = CRC_InputBitSequenceReversal_Disable;
        /* Initialize the CRC data output bit reverse function */
        CRC_InitStruct->CRC_OutputBitSequenceReversal = CRC_OutputBitSequenceReversal_Disable;
        /* Initialize the CRC data input byte reverse function */
        CRC_InitStruct->CRC_InputByteSequenceReversal = CRC_InputByteSequenceReversal_Enable;
        /* Initialize the CRC POLY */
        CRC_InitStruct->CRC_Poly = CRC_CCITT;
        /* Initialize the CRC SEED */
        CRC_InitStruct->CRC_Seed = 0;
}


/**
  * @brief  Enables or disables the specified CRC peripheral.
  * @param  NewState: new state of the CRC peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CRC_Cmd(FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the selected CRC peripheral */
                CRC->CR |= CRC_CR_EN;
        }
        else
        {
                /* Disable the selected CRC peripheral */
                CRC->CR &= ~CRC_CR_EN;
        }
}

/**
  * @brief   Resets the Data Out registers.
  * @retval None
  */
void CRC_ResetDout(void)
{
        CRC->CR |= CRC_CR_CRS;
}


/**
  * @brief  Computes the 16-bit CRC of a given 16-bit data.
  * @param  CRC_Data: data half-word(16-bit) to compute its CRC
  * @retval 16-bit CRC
  */
u32 CRC_CalculateCRC(u16 CRC_Data)
{
        CRC->DIN = (u16) CRC_Data;
        return (CRC->DOUT);
}


/**
  * @brief  Computes the 16-bit CRC of a given buffer of data word(16-bit).
  * @param  pBuffer: pointer to the buffer containing the data to be computed
  * @param  BufferLength: length of the buffer to be computed
  * @retval 16-bit CRC
  */
u32 CRC_CalculateBlockCRC(u16 pBuffer[], u32 BufferLength)
{
        u32 index = 0;
        for(index = 0; index < BufferLength; index++)
        {
                CRC->DIN = pBuffer[index];
        }
        return (CRC->DOUT);
}



/**
  * @brief  Returns the current CRC value.
  * @param  None
  * @retval 16-bit CRC
  */
u32 CRC_GetCRC(void)
{
        return (CRC->DOUT);
}


/**
  * @}
  */

使用特权

评论回复
6
rickluo|  楼主 | 2024-3-25 14:34 | 只看该作者
  /******************************************************************************
  * @file    PT32x007x_exti.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the functionality of EXTI peripherals
  *         
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_exti.h"


/** @defgroup EXTI
  * @brief EXTI driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Enables or disables the specified exti interrupts.
  * @param  EXTIx: where x can be (A, B, C, D, E or F) to select the EXTI peripheral.
  * @param  NewState: new state of the specified EXTI interrupts.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void EXTI_ITConfig(EXTI_TypeDef* EXTIx, u32 EXTI_IT, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_EXTI_ALL_PERIPH(EXTIx));
        assert_param(IS_EXTI_IT(EXTI_IT));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                EXTIx->IES |= EXTI_IT;
        }
        else
        {
                EXTIx->IEC |= EXTI_IT;
        }
}

/**
  * @brief Exti interrupts trigger types configuration
  * @param  EXTIx: where x can be (A, B, C, D, E or F) to select the EXTI peripheral.
  * @param  EXTI_Pin: specifies the port bit to be written.
  * @param  TriggerType: the source of EXTI Trigger.
        *    @arg EXTI_Trigger_Rising: set rising as the EXTI Trigger source
        *    @arg EXTI_Trigger_Falling: set falling as the EXTI Trigger source
        *    @arg EXTI_Trigger_RisingFalling: set rising and falling as the EXTI Trigger source
        *    @arg EXTI_Trigger_HighLevel: set high level as the EXTI Trigger source
        *    @arg EXTI_Trigger_LowLevel: set low level as the EXTI Trigger source
  * @retval None
  */
void EXTI_TriggerTypeConfig(EXTI_TypeDef* EXTIx, u32 EXTI_Pin, EXTI_Trigger TriggerType)
{
        /* Check the parameters */
        assert_param(IS_EXTI_ALL_PERIPH(EXTIx));
        assert_param(IS_EXTI_Pin(EXTI_Pin));
        assert_param(IS_EXTI_Trigger(TriggerType));
        switch( TriggerType )
        {
                case EXTI_Trigger_Rising :
                        EXTIx->ITS = EXTI_Pin;
                        EXTIx->PTS =  EXTI_Pin;
                        EXTIx->ITDC = EXTI_Pin;
                        break;
                case EXTI_Trigger_Falling :
                        EXTIx->ITS = EXTI_Pin;
                        EXTIx->PTC =  EXTI_Pin;
                        EXTIx->ITDC = EXTI_Pin;
                        break;
                case EXTI_Trigger_RisingFalling :
                        EXTIx->ITDS = EXTI_Pin;
                        break;
                case EXTI_Trigger_HighLevel :
                        EXTIx->ITC = EXTI_Pin;
                        EXTIx->PTS =  EXTI_Pin;
                        EXTIx->ITDC = EXTI_Pin;
                        break;
                case EXTI_Trigger_LowLevel :
                        EXTIx->ITC = EXTI_Pin;
                        EXTIx->PTC =  EXTI_Pin;
                        EXTIx->ITDC = EXTI_Pin;
               
                        break;
        }
}

/**
  * @brief  Clears the EXTI's pending flags.
  * @param  EXTIx: where x can be (A, B, C or F) to select the EXTI peripheral.
  * @param  EXTI_IT_FLAG: specifies the flag to clear.
  *          This parameter can be any combination of the following values:
  * @retval None
  */
void EXTI_ClearFlag(EXTI_TypeDef* EXTIx, u32 EXTI_IT_FLAG)
{
        /* Check the parameters */
        assert_param(IS_EXTI_ALL_PERIPH(EXTIx));
        assert_param(IS_EXTI_FLAG(EXTI_IT_FLAG));
        EXTIx->IF |= EXTI_IT_FLAG;
}

/**
  * @brief  Checks whether the specified EXTI IT is set or not.
  * @param  EXTIx: where x can be (A, B, C or F) to select the EXTI peripheral.
  * @param  EXTI_IT: specifies the flag to get.
  *          This parameter can be one of the following values:
  *    @arg EXTI_IT_0  specifies the interrupt source for EXTIx0 interrupt
  *    @arg EXTI_IT_1  specifies the interrupt source for EXTIx1 interrupt
  *    @arg EXTI_IT_2  specifies the interrupt source for EXTIx2 interrupt
  *    @arg EXTI_IT_3  specifies the interrupt source for EXTIx3 interrupt
  *    @arg EXTI_IT_4  specifies the interrupt source for EXTIx4 interrupt
  *    @arg EXTI_IT_5  specifies the interrupt source for EXTIx5 interrupt
  *    @arg EXTI_IT_6  specifies the interrupt source for EXTIx6.interrupt
  *    @arg EXTI_IT_7  specifies the interrupt source for EXTIx7 interrupt
  *    @arg EXTI_IT_8  specifies the interrupt source for EXTIx8 interrupt
  *    @arg EXTI_IT_9  specifies the interrupt source for EXTIx9 interrupt
  *    @arg EXTI_IT_10 specifies the interrupt source for EXTIx10 interrupt
  *    @arg EXTI_IT_11 specifies the interrupt source for EXTIx11 interrupt
  *    @arg EXTI_IT_12 specifies the interrupt source for EXTIx12 interrupt
  *    @arg EXTI_IT_13 specifies the interrupt source for EXTIx13 interrupt
  *    @arg EXTI_IT_14 specifies the interrupt source for EXTIx14 interrupt
  *    @arg EXTI_IT_15 specifies the interrupt source for EXTIx15 interrupt
  *    @arg EXTI_IT_All specifies the interrupt source for All interrupt
  * @retval ITStatus:interrupts status,the value can be 'SET' or 'RESET'.
  */
ITStatus EXTI_GetITStatus(EXTI_TypeDef* EXTIx, u32 EXTI_IT)
{
        ITStatus bitstatus = RESET;
        u32 enablestatus = 0;
        /* Check the parameters */
        assert_param(IS_EXTI_ALL_PERIPH(EXTIx));
        assert_param(IS_EXTI_IT(EXTI_IT));

        enablestatus = (u32)(EXTIx->IES & EXTI_IT);
        if (((u32)(EXTIx->IF & EXTI_IT) != (u32)RESET) && (enablestatus != (u32)RESET))
        {
                bitstatus = SET;
        }
        else
        {
                bitstatus = RESET;
        }
        return  bitstatus;
}


/**
  * @}
  */

使用特权

评论回复
7
rickluo|  楼主 | 2024-3-25 14:35 | 只看该作者
  /******************************************************************************
  * @file    PT32x007x_gpio.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the GPIO peripheral:
  *           + Initialization and Configuration functions
  *           + GPIO Read and Write functions
  *           + GPIO Alternate functions configuration functions
  *         
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_gpio.h"


/** @defgroup GPIO
  * @brief GPIO driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initializes the GPIOx peripheral according to the specified
  *         parameters in the GPIO_InitStruct.
  * @param  GPIOx: where x can be (A, B, C, D) to select the GPIO peripheral.
  * @param  GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
  *         the configuration information for the specified GPIO peripheral.
  * @retval None
  */
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
        u32 currentpin = 0x00;
        /* Check the parameters */
        assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        assert_param(IS_GPIO_Pin(GPIO_InitStruct->GPIO_Pin));
    assert_param(IS_GPIO_Mode(GPIO_InitStruct->GPIO_Mode));
        assert_param(IS_GPIO_Pull(GPIO_InitStruct->GPIO_Pull));

        currentpin = GPIO_InitStruct->GPIO_Pin;
        if (((GPIO_InitStruct->GPIO_Mode) == GPIO_Mode_OutPP) || ((GPIO_InitStruct->GPIO_Mode) == GPIO_Mode_OutOD))
        {
                /* Output mode configuration */
                GPIOx->OES = currentpin;
                if((GPIO_InitStruct->GPIO_Mode) == GPIO_Mode_OutOD)
                {
                        GPIOx->ODS = currentpin;
                }
                else
                {
                        GPIOx->ODC = currentpin;
                }
        }
        if((GPIO_InitStruct->GPIO_Mode) == GPIO_Mode_In)
        {
                /* input mode configuration */
                GPIOx->OEC = currentpin;
                switch(((u32)GPIO_InitStruct->GPIO_Pull))
                {
                        case GPIO_Pull_NoPull :
                                GPIOx->PDC = currentpin;
                                GPIOx->PUC = currentpin;
                                break;
                        case GPIO_Pull_Up :
                                GPIOx->PDC = currentpin;
                                GPIOx->PUS = currentpin;
                                break;
                        case GPIO_Pull_Down :
                                GPIOx->PDS = currentpin;
                                GPIOx->PUC = currentpin;
                                break;
                }
        }
}

/**
  * @brief  Fills each GPIO_InitStruct member with its default value.
  * @param  GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure which will
  *         be initialized.
  * @retval None
  */
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
{
        /* Reset GPIO init structure parameters values */
        GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_0;
        GPIO_InitStruct->GPIO_Mode = GPIO_Mode_In;
        GPIO_InitStruct->GPIO_Pull = GPIO_Pull_NoPull;
}

u16 GPIO_ReadData(GPIO_TypeDef* GPIOx)
{
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        return ((u16)GPIOx->DR);
}

u8 GPIO_ReadDataBit(GPIO_TypeDef* GPIOx, u32 GPIO_Pin)
{
    u8 bitstatus = 0x00;
        /* Check the parameters */
        assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        assert_param(IS_GPIO_Pin(GPIO_Pin));
        if ((GPIOx->DR & GPIO_Pin) != (u32)RESET)
        {
                bitstatus = (u8)SET;
        }
        else
        {
                bitstatus = (u8)RESET;
        }
        return bitstatus;   
}

/**
  * @brief  Sets the selected data port bits.
  * @param  GPIOx: where x can be (A, B, C, D) to select the GPIO peripheral.
  * @param  GPIO_Pin: specifies the port bits to be written.
  *               @arg   This parameter can be GPIO_Pin_x where x can be(0~15) or GPIO_Pin_All
  * @retval None
  */
void GPIO_SetBits(GPIO_TypeDef* GPIOx, u32 GPIO_Pin)
{
        /* Check the parameters */
        assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        assert_param(IS_GPIO_Pin(GPIO_Pin));
        GPIOx->BSR = GPIO_Pin;
}

/**
  * @brief  Reset the selected data port bits.
  * @param  GPIOx: where x can be (A, B, C, D) to select the GPIO peripheral.
  * @param  GPIO_Pin: specifies the port bits to be written.
  *               @arg   This parameter can be GPIO_Pin_x where x can be(0~15) or GPIO_Pin_All
  * @retval None
  */
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, u32 GPIO_Pin)
{
        /* Check the parameters */
        assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        assert_param(IS_GPIO_Pin(GPIO_Pin));
        GPIOx->BRR = GPIO_Pin;
}
/**
  * @brief  Reverse the selected data port bits.
  * @param  GPIOx: where x can be (A, B, C, D) to select the GPIO peripheral.
  * @param  GPIO_Pin: specifies the port bits to be written.
  *               @arg   This parameter can be GPIO_Pin_x where x can be(0~15) or GPIO_Pin_All
  * @retval None
  */
void GPIO_ReverseBits(GPIO_TypeDef* GPIOx, u32 GPIO_Pin)
{
        /* Check the parameters */
        assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        assert_param(IS_GPIO_Pin(GPIO_Pin));
        GPIOx->DR ^= GPIO_Pin;
}

void GPIO_WriteDataBit(GPIO_TypeDef* GPIOx, u32 GPIO_Pin, BitAction Action)
{
        /* Check the parameters */
        assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        assert_param(IS_GPIO_Pin(GPIO_Pin));
    if (Action != RESET)
        {
                GPIOx->DR |= GPIO_Pin;
        }
        else
        {
                GPIOx->DR &= ~GPIO_Pin;
        }
}

void GPIO_WriteData(GPIO_TypeDef* GPIOx, u16 PortVal)
{
        /* Check the parameters */
        assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
        GPIOx->DR = PortVal;
}

void GPIO_AnalogRemapConfig(AFIO_TypeDef* AFIOx, u32 GPIO_Pin, FunctionalState NewState)
{
    /* Check the parameters */
        assert_param(IS_AFIO_ALL_PERIPH(AFIOx));
        assert_param(IS_GPIO_Pin(GPIO_Pin));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                AFIOx->ANAS |= GPIO_Pin;
        }
        else
        {
                AFIOx->ANAC |= GPIO_Pin;
        }

}

void GPIO_DigitalRemapConfig(AFIO_TypeDef* AFIOx, u32 GPIO_Pin, u8 AFIO_AF, FunctionalState NewState)
{
    u8 temp = 0;
        /* Check the parameters */
        assert_param(IS_AFIO_ALL_PERIPH(AFIOx));
        assert_param(IS_GPIO_Pin(GPIO_Pin));
        assert_param(IS_AFIO_AF(AFIO_AF));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                AFIOx->AFC |= GPIO_Pin;
     while(GPIO_Pin)                       
                {
                        if((GPIO_Pin&0x01)==1)       
                        {
        AFIOx->AFS0|=AFIO_AF<<(temp*4);
                        }
                        GPIO_Pin=GPIO_Pin>>1;
                        temp++;                               
                }
        }
        else
        {
                AFIOx->AFC |= GPIO_Pin;
        }
}
/**
  * @brief  Enable or Disable GPIO_Pin Pull_Up function.
  * @param  GPIOx: where x can be (A, B, C, F) to select the AFIO peripheral.
  * @param  GPIO_Pin: specifies the port bit to be written.
  *                            @arg   This parameter can be GPIO_Pin_x where x can be(0..15) or GPIO_Pin_All
  * @param  NewState: new state of the port pin schmitt function.
  *                            @arg   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void GPIO_PullUpConfig(GPIO_TypeDef *GPIOx, u32 GPIO_Pin, FunctionalState NewState)
{
          /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
    assert_param(IS_GPIO_Pin(GPIO_Pin));
          assert_param(IS_FUNCTIONAL_STATE(NewState));
       
          GPIOx->OEC = GPIO_Pin;
          if(NewState != DISABLE)
    {
                          GPIOx->PDC = GPIO_Pin;
        GPIOx->PUS = GPIO_Pin;
    }
    else
    {
        GPIOx->PUC = GPIO_Pin;
    }
       
}

/**
  * @brief  Enable or Disable GPIO_Pin Pull_Down function.
  * @param  GPIOx: where x can be (A, B, C, F) to select the AFIO peripheral.
  * @param  GPIO_Pin: specifies the port bit to be written.
  *                            @arg   This parameter can be GPIO_Pin_x where x can be(0..15) or GPIO_Pin_All
  * @param  NewState: new state of the port pin schmitt function.
  *                            @arg   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void GPIO_PullDownConfig(GPIO_TypeDef *GPIOx, u32 GPIO_Pin, FunctionalState NewState)
{
          /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
    assert_param(IS_GPIO_Pin(GPIO_Pin));
          assert_param(IS_FUNCTIONAL_STATE(NewState));
       
          GPIOx->OEC = GPIO_Pin;
          if(NewState != DISABLE)
    {
                          GPIOx->PUC = GPIO_Pin;
        GPIOx->PDS = GPIO_Pin;
    }
    else
    {
        GPIOx->PDC = GPIO_Pin;
    }
}

使用特权

评论回复
8
rickluo|  楼主 | 2024-3-25 14:36 | 只看该作者
PT32L007  IIC 例程

  /******************************************************************************
  * @file    PT32x007x_i2c.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the Inter-Integrated circuit (I2C0):
  *           + Initialization and Configuration
  *           + Communications handling
  *           + I2C0 registers management
  *           + Data transfers management
  *           + Interrupts and flags management
  *         
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/
  
/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_i2c.h"

/** @defgroup I2C0
  * @brief I2C0 driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
#define Init_CR_MASK   (u32)0xFC00FFFB
#define Init_CCR_MASK   (u32)0x3FF0004
/**
  * @brief  Initializes the I2Cx peripheral according to the specified
  *            parameters in the I2C_InitStruct.
  * @param  I2Cx: select the I2C0 peripheral.
  * @param  I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
  *               contains the configuration information for the specified I2C0 peripheral.
  * @retval None
  */
void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
{
        u32 tmpreg =0;
        u32 addr_bc=0;
        /* Check the parameters */
        assert_param(IS_I2C_ALL_PERIPH(I2Cx));
        assert_param(IS_I2C_Address(I2C_InitStruct->I2C_OwnAddress));
        assert_param(IS_I2C_Acknowledge(I2C_InitStruct->I2C_Acknowledge));
        assert_param(IS_I2C_Prescaler(I2C_InitStruct->I2C_Prescaler));
        assert_param(IS_I2C_Broadcast(I2C_InitStruct->I2C_Broadcast));
        /* Disable I2Cx Peripheral */
        I2Cx->CCR=Init_CCR_MASK;
        tmpreg=I2Cx->CR;
        tmpreg&=Init_CR_MASK;
        /*---------------------------- I2Cx OAR Configuration ---------------------*/
        /* I2Cx BroadCast Configuration */
        /* I2Cx Address Configuration */
        addr_bc = (u32)(((I2C_InitStruct->I2C_OwnAddress) << 0x01) | (I2C_InitStruct->I2C_Broadcast));
        /* Write to I2Cx OAR */
        I2Cx->OAR = addr_bc;
       
        /*---------------------------- I2Cx ACK Configuration ----------------------*/
        /* I2Cx ACK Configuration */
        /* I2Cx PSC Configuration */
        tmpreg |=  (u32)((I2C_InitStruct->I2C_Acknowledge) | (((I2C_InitStruct->I2C_Prescaler/4)-1) << 16));
        I2Cx->CR = tmpreg;
        if(I2C_InitStruct->I2C_Acknowledge==I2C_Acknowledge_Disable)
        {
                I2Cx->CCR=I2C_CCR_ACK;
        }
}

/**
  * @brief  Fills each I2C_InitStruct member with its default value.
  * @param  I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
  * @retval None
  */
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
{
        I2C_InitStruct->I2C_Prescaler = 192;       
        I2C_InitStruct->I2C_Broadcast=I2C_Broadcast_Disable;       
        I2C_InitStruct->I2C_OwnAddress = 0;
        I2C_InitStruct->I2C_Acknowledge = I2C_Acknowledge_Disable;       
}

/**
  * @brief  Enables or disables the specified I2C0 peripheral.
  * @param  I2Cx: select the I2C0 peripheral.
  * @param  NewState: new state of the I2Cx peripheral.
  *               @arg   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_I2C_ALL_PERIPH(I2Cx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the selected I2C0 peripheral */
                I2Cx->CR |= I2C_CR_EN;
        }
        else
        {
                /* Disable the selected I2C0 peripheral */
                I2Cx->CCR |= I2C_CCR_EN;
        }
}

/**
  * @brief  Generates I2Cx communication event.
  * @param  I2Cx: select the I2C0 peripheral.
  * @param  Event: the types of I2Cx's event.
        *          This parameter can be one of the following values:
        *          @arg I2C_Event_Stop: I2C generate stop
        *          @arg I2C_Event_Start: I2C generates start
  * @param  NewState: new state of the I2C Event generation.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void I2C_GenerateEvent(I2C_TypeDef* I2Cx, u8 Event, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_I2C_ALL_PERIPH(I2Cx));
        assert_param(IS_I2C_Event(Event));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        /* Generate a STOP event */
        if(Event==I2C_Event_Stop)
        {
                if (NewState != DISABLE)
                {
                        /* Generate a STOP condition */
                I2Cx->CR |= I2C_CR_STOP;
                I2Cx->CCR |= I2C_CCR_SI;
                }
                else
                {
                        /* Disable the STOP condition generation */
                        I2Cx->CCR |= I2C_CCR_SI | I2C_CCR_STOP;
                }
        }
        /* Generate a START event */
        else if(Event==I2C_Event_Start)
        {
                if (NewState != DISABLE)
                {
                        /* Generate a START condition */
                        I2Cx->CR |= I2C_CR_STAR;
                }
                else
                {
                        /* Disable the START condition generation */
                        I2Cx->CCR |= I2C_CCR_STAR;
                }
        }
        else
        {
                if (NewState != DISABLE)
                {
                        /* Generate a START condition */
                        I2Cx->CR |= I2C_CR_STAR;
                        I2Cx->CCR |= I2C_CCR_SI;
                }
                else
                {
                        /* Disable the START condition generation */
                        I2Cx->CCR |= I2C_CCR_STAR;
                }
        }
}

/**
  * @brief  Sends a data byte through the I2Cx peripheral.
  * @param  I2Cx: select the I2C0 peripheral.
  * @param  Data: Byte to be transmitted..
  * @retval None
  */
void I2C_SendData(I2C_TypeDef* I2Cx, u8 Data)
{
        /* Check the parameters */
        assert_param(IS_I2C_ALL_PERIPH(I2Cx));
        /* Write in the DR register the data to be sent */
        I2Cx->DR = (u8)Data;
        I2Cx->CCR |= I2C_CCR_SI|I2C_CCR_STAR;
}
/**
  * @brief  Sends a data/addr byte through the I2Cx peripheral, then clear STA bit
  * @param  I2Cx: select the I2C0 peripheral.
  * @param  Data: Byte to be transmitted..
  * @retval None
  */
void I2C_SendAddr(I2C_TypeDef* I2Cx, u8 Data)
{
        /* Check the parameters */
        assert_param(IS_I2C_ALL_PERIPH(I2Cx));
        /* Write in the DR register the data to be sent */
        I2Cx->DR = (u8)Data;
        I2Cx->CCR |= I2C_CCR_SI | I2C_CCR_STAR;
}


/**
  * @brief  Returns the most recent received data by the I2Cx peripheral.
  * @param  I2Cx: select the I2C0 peripheral.
  * @retval The value of the received data.
  */
u8 I2C_ReceiveData(I2C_TypeDef* I2Cx)
{
        /* Check the parameters */
        assert_param(IS_I2C_ALL_PERIPH(I2Cx));
        /* Return the data in the DR register */
        return (I2Cx->DR);
}

/**
  * @brief  Checks whether the specified I2C0 flag is set or not.
  * @param  I2Cx: select the I2C0 peripheral.
  * @param  I2C_FLAG: specifies the flag to check.
  *               This parameter can be one of the following values:
  *               @arg I2C_FLAG_Stop                         总线上出现一个停止信号
  *               @arg I2C_FLAG_StartOk                         起始信号发送完毕
  *               @arg I2C_ReStartOk                         重复起始信号发送完毕
  *               @arg I2C_FLAG_MASGetAckW         主机, 地址字发送完毕, 收到 ACK
  *               @arg I2C_FLAG_MASGetNackW         主机, 地址字发送完毕, 收到 NACK
  *               @arg I2C_FLAG_MDSGetAck                 主机, 数据字发送完毕, 收到 ACK
  *               @arg I2C_FLAG_MDSGetNack                 主机, 数据字发送完毕, 收到 NACK
  *               @arg I2C_FLAG_MArbitrationlost          主机, 总线仲裁失败
  *               @arg I2C_FLAG_MASGetAckR                 主机, 地址字发送完毕, 收到 ACK
  *               @arg I2C_FLAG_MASGetNackR                 主机, 地址字发送完毕, 收到 NACK
  *               @arg I2C_FLAG_MDGSendAck                 主机, 数据字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_MDGSendNack                 主机, 数据字接收完毕, 回送 NACK
   
  *               @arg I2C_FLAG_SAGSendAckW         从机, 地址字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SALAGSendAckW         从机,总线仲裁失败转化的从机, 地址字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SBCAGSendAck         从机,广播地址字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SALBCAGSendAck         从机,总线仲裁失败转化的从机,广播地址字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SDGSendAck                 从机, 数据字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SDGSendNack                 从机, 数据字接收完毕, 回送 NACK
  *               @arg I2C_FLAG_SBCDGSendAck         从机, 广播数据字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SBCDGSendNack         从机, 广播数据字接收完毕, 回送 NACK
  *               @arg I2C_FLAG_SDGGSRS                         从机, 数据字接收完毕, 接收到停止信号或重复起始信号
  *               @arg I2C_FLAG_SAGSendAckR         从机, 地址字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SALAGSendAckR         从机, 主机总线仲裁失败转化的从机,地址字接收完毕, 回送 ACK
  *               @arg I2C_FLAG_SDSReadAck                 从机, 数据字发送完毕, 收到 ACK
  *               @arg I2C_FLAG_SDSReadNack                 从机, 数据字发送完毕, 收到 NACK
  *               @arg I2C_FLAG_SDSSAGSRS                 从机, 数据字接收完毕, 回送 ACK 后、接收到停止信号或重复起始信号
  *               @arg I2C_FLAG_IDLE                                 总线空闲
  * @retval The new state of I2C_FLAG (SET or RESET).
  */
FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
{
        FlagStatus bitstatus = RESET;
        /* Check the parameters */
        assert_param(IS_I2C_ALL_PERIPH(I2Cx));
        assert_param(IS_I2C_FLAG(I2C_FLAG));
        if (I2Cx->SR  == I2C_FLAG)
        {
                bitstatus = SET;
        }
        else
        {
                bitstatus = RESET;
        }
        return bitstatus;
}


/**
  * @}
  */


使用特权

评论回复
9
rickluo|  楼主 | 2024-3-25 14:37 | 只看该作者
PT32L007 PWM例程分享

  /******************************************************************************
  * @file    PT32x007x_pwm.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the TIM peripheral:
  *            + TimeBase management
  *            + Output Compare management
  *            + Input Capture management
  *            + Interrupts, flags management
  *            + Clocks management
  *            + Synchronization management
  *            + Specific interface management
  *            + Specific remapping management
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_pwm.h"

/** @defgroup PWM
  * @brief PWM driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define TimeBaseInit_CR1_MASK   ((u32)0xFFFFF9FB)

#define Init_CAPRCH1_MASK   ((u32)0XFFF8FFF8)
#define Init_CAPRCH2_MASK   ((u32)0XFF8FFF8F)
#define Init_CAPRCH3_MASK   ((u32)0XF8FFF8FF)
#define Init_CAPRCH4_MASK   ((u32)0X8FFF8FFF)

#define Init_OCMRCH1_MASK   ((u32)0XEEEEFFF8)
#define Init_OCMRCH2_MASK   ((u32)0XDDDDFF8F)
#define Init_OCMRCH3_MASK   ((u32)0XBBBBF8FF)
#define Init_OCMRCH4_MASK   ((u32)0X77778FFF)

#define Init_CR3CH1_MASK    ((u32)0XFFFFFFFC)
#define Init_CR3CH2_MASK    ((u32)0XFFFFFFCF)
#define Init_CR3CH3_MASK    ((u32)0XFFFFFCFF)
#define Init_CR3CH4_MASK    ((u32)0XFFFFCFFF)

#define Init_BDTR_MASK  ((u32)0X01FF0FFF)
#define Init_BKICR_MASK ((u32)0XFFFFFFCE)

#define Init_OCMRCH_MASK(x)         ((u32)(0x11110000 << (x)) | (0x00000007 << ((x)*4)))
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initializes the PWMx Time Base Unit peripheral according to
  *         the specified parameters in the TimeBaseInit.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  TimeBaseInit: pointer to a PWM_TimeBaseInitTypeDef
  *         structure that contains the configuration information for
  *         the specified PWM peripheral.
  * @retval None
  */
void PWM_TimeBaseInit(PWM_TypeDef* PWMx, PWM_TimeBaseInitTypeDef* TimeBaseInit)
{
        u32  tmpreg = 0;
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_ClockSource(TimeBaseInit->PWM_InternalClockSource));
        assert_param(IS_PWM_Direction(TimeBaseInit->PWM_Direction));
        assert_param(IS_PWM_CenterAlignedMode(TimeBaseInit->PWM_CenterAlignedMode));
        /* Set the counter reload value */
        PWMx->ARR = TimeBaseInit->PWM_AutoReloadValue;
        /* Set the PWM clcok  */
        tmpreg = PWMx->CR1;
        tmpreg &= TimeBaseInit_CR1_MASK;
        tmpreg |=  (TimeBaseInit->PWM_InternalClockSource | TimeBaseInit->PWM_Direction | TimeBaseInit->PWM_CenterAlignedMode);
        PWMx->CR1 = tmpreg;
        /* Set the Prescaler value */
        PWMx->PSC = TimeBaseInit->PWM_Prescaler;
}

/**
  * @brief  Fills each TimeBaseInit member with its default value.
  * @param  TimeBaseInit: pointer to a PWM_TimeBaseInitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void PWM_TimeBaseStructInit(PWM_TimeBaseInitTypeDef* TimeBaseInit)
{
        /* Set the default configuration */
        TimeBaseInit->PWM_AutoReloadValue = 0xFFFF;
        TimeBaseInit->PWM_InternalClockSource = PWM_InternalClockSource_PCLK;
        TimeBaseInit->PWM_Prescaler = 0x0000;
        TimeBaseInit->PWM_Direction = PWM_Direction_Up;
        TimeBaseInit->PWM_CenterAlignedMode = PWM_CenterAlignedMode_Disable;
}

/**
  * @brief  Initializes the PWMx peripheral output compare function according to
  *         the specified parameters in the OutInit.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  OutInit: pointer to a PWM_OCInitTypeDef
  *         structure that contains the configuration information for
  *         the specified PWM peripheral.
  * @retval None
  */
void PWM_OCInit(PWM_TypeDef* PWMx, PWM_OCInitTypeDef* OutInit)
{
        u8 ch = 0;
        u32 tmpreg1 = 0;
        u32 tmpreg2 = 0;
        u32 tmpreg4 = 0;
       
        PWM_OCInitTypeDef tmpreg;
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_Channel(OutInit->PWM_Channel));
        assert_param(IS_PWM_OCMode(OutInit->PWM_OCMode));
        assert_param(IS_PWM_OCIdleState(OutInit->PWM_OCIdleState));
        assert_param(IS_PWM_OCNIdleState(OutInit->PWM_OCNIdleState));
        assert_param(IS_PWM_OCOutput(OutInit->PWM_OCOutput));
        assert_param(IS_PWM_OCNOutput(OutInit->PWM_OCNOutput));
        assert_param(IS_PWM_OCPolarity(OutInit->PWM_OCPolarity));
        assert_param(IS_PWM_OCNPolarity(OutInit->PWM_OCNPolarity));

        ch = OutInit->PWM_Channel;
        /* Set the CHx Compare value */   
        tmpreg = *OutInit;
        tmpreg2=PWMx->CR3;

        tmpreg2&=~((1<<(ch*4))|(2<<(ch*4)));
  tmpreg4=(tmpreg.PWM_OCPolarity |tmpreg.PWM_OCNPolarity);
  tmpreg4=(tmpreg4<<(ch*4));

        /* Set the CHx Compare value */
        tmpreg1 = PWMx->OCMR;
        tmpreg1 &= ~Init_OCMRCH_MASK(ch);
        tmpreg1 |=        ((OutInit->PWM_OCMode << (4 * ch))                         | \
                                 (OutInit->PWM_OCIdleState << (1 * ch))  | \
                                 (OutInit->PWM_OCNIdleState << (1 * ch)) | \
                                 (OutInit->PWM_OCOutput << (1 * ch))                  | \
                                 (OutInit->PWM_OCNOutput << (1 * ch)));

        if(ch == PWM_Channel_1)
        {
          PWMx->OCR1 = OutInit->PWM_OCValue;
        }
        else if(ch == PWM_Channel_2)
        {
                PWMx->OCR2 = OutInit->PWM_OCValue;
        }
        else if(ch == PWM_Channel_3)
        {
                PWMx->OCR3 = OutInit->PWM_OCValue;
        }
        else if(ch == PWM_Channel_4)
        {
                PWMx->OCR4 = OutInit->PWM_OCValue;
        }
        PWMx->OCMR = tmpreg1;
       
        tmpreg2|=tmpreg4;
        PWMx->CR3=tmpreg2;
}

/**
  * @brief  Fills each OutInit member with its default value.
  * @param  OutInit: pointer to a PWM_OCInitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void PWM_OCStructInit(PWM_OCInitTypeDef* OutInit)
{
        /* Set the default configuration */
        OutInit->PWM_OCMode=TIM_OCMode_Timing;
        OutInit->PWM_OCIdleState=PWM_OCIdleState_High;
        OutInit->PWM_OCNIdleState=PWM_OCNIdleState_High;
        OutInit->PWM_OCOutput=PWM_OCOutput_Enable;
        OutInit->PWM_OCNOutput=PWM_OCNOutput_Enable;
        OutInit->PWM_OCPolarity=PWM_OCPolarity_High;
        OutInit->PWM_OCNPolarity=PWM_OCNPolarity_Low;
}

/**
  * @brief  Initializes the PWMx peripheral input capture function according to
  *         the specified parameters in the CapInit.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  CapInit: pointer to a PWM_ICInitTypeDef
  *         structure that contains the configuration information for
  *         the specified PWM peripheral.
  * @retval None
  */
void PWM_ICInit(PWM_TypeDef* PWMx, PWM_ICInitTypeDef* CapInit)
{
        u8 ch = 0;
        u32 tmpreg1 = 0;       
        u32 tmpreg2 = 0;       
        PWM_ICInitTypeDef tmpreg;
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_Channel(CapInit->PWM_Channel));
        assert_param(IS_PWM_ICRiseCapture(CapInit->PWM_ICRiseCapture));
        assert_param(IS_PWM_ICFallCapture(CapInit->PWM_ICFallCapture));
        assert_param(IS_PWM_ICResetCounter(CapInit->PWM_ICResetCounter));
        assert_param(IS_PWM_ICSource(CapInit->PWM_ICSource));

        ch=CapInit->PWM_Channel;
        tmpreg = *CapInit;   
        tmpreg1=PWMx->CAPR;
       
        tmpreg2=(tmpreg.PWM_ICRiseCapture|tmpreg.PWM_ICFallCapture |tmpreg.PWM_ICResetCounter|tmpreg.PWM_ICSource);
        tmpreg2=(tmpreg2<<(ch*4));
       
        if(ch == PWM_Channel_1)
        {
                PWMx->CAPR&=Init_CAPRCH1_MASK;
        }
        else if(ch == PWM_Channel_2)
        {
                PWMx->CAPR&=Init_CAPRCH2_MASK;
        }
        else if(ch == PWM_Channel_3)
        {
                PWMx->CAPR&=Init_CAPRCH3_MASK;
        }
        else if(ch == PWM_Channel_4)
        {
                PWMx->CAPR&=Init_CAPRCH4_MASK;
        }
       
        tmpreg1|=tmpreg2;
        PWMx->CAPR=tmpreg1;
}

/**
  * @brief  Fills each CapInit member with its default value.
  * @param  CapInit: pointer to a PWM_ICInitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void PWM_ICStructInit(PWM_ICInitTypeDef* CapInit)
{
        /* Set the default configuration */
        CapInit->PWM_Channel = PWM_Channel_1;
        CapInit->PWM_ICRiseCapture = PWM_ICRiseCapture_Enable;
        CapInit->PWM_ICFallCapture = PWM_ICFallCapture_Enable;
        CapInit->PWM_ICResetCounter = PWM_ICResetCounter_Enable;
        CapInit->PWM_ICSource = PWM_ICSource_ICS1;
}

/**
  * @brief  Initializes the PWMx peripheral brake and dead time function according to
  *         the specified parameters in the BDTRInit.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  BDTRInit: pointer to a PWM_BDTRInitTypeDef
  *         structure that contains the configuration information for
  *         the specified PWM peripheral.
  * @retval None
  */
void PWM_BDTRInit(PWM_TypeDef* PWMx, PWM_BDTRInitTypeDef* BDTRInit)
{
        u32 tmpreg = 0;
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_DeadTime(BDTRInit->PWM_DeadTime));
        assert_param(IS_PWM_Break(BDTRInit->PWM_Break));
        assert_param(IS_PWM_BreakPolarity(BDTRInit->PWM_BreakPolarity));
        assert_param(IS_PWM_BreakSoftwareControl(BDTRInit->PWM_BreakSoftwareControl));
        assert_param(IS_PWM_BreakInput(BDTRInit->PWM_BreakInput));
    assert_param(IS_PWM_BreakSource(BDTRInit->PWM_BreakSource));
   
   
        tmpreg = PWMx->BDTR;
        tmpreg &= Init_BDTR_MASK;
        tmpreg |= (BDTRInit->PWM_DeadTime |\
               BDTRInit->PWM_Break |\
                           BDTRInit->PWM_BreakPolarity |\
                           BDTRInit->PWM_BreakSoftwareControl |\
                           BDTRInit->PWM_BreakInput);
   
        PWMx->BDTR = tmpreg;
    tmpreg = PWMx->BKICR;
    tmpreg &=Init_BKICR_MASK;
    tmpreg |= (BDTRInit->PWM_BreakSource);
    PWMx->BKICR = tmpreg;
   
}

/**
  * @brief  Fills each BDTRInit member with its default value.
  * @param  BDTRInit: pointer to a PWM_BDTRInitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void PWM_BDTRStructInit(PWM_BDTRInitTypeDef* BDTRInit,u32 DeadTime)
{
        /* Set the default configuration */
        BDTRInit->PWM_DeadTime=DeadTime;
        BDTRInit->PWM_Break = PWM_Break_Enable;
        BDTRInit->PWM_BreakPolarity = PWM_BreakPolarity_Low;
        BDTRInit->PWM_BreakSoftwareControl = PWM_BreakSoftwareControl_Disable;
        BDTRInit->PWM_BreakInput = PWM_BreakInput_TIMIdle;
  BDTRInit->PWM_BreakSource = NULL;
}

/**
  * @brief  Set interrupt auto reload value for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  value: TIM interrupt auto reload value.
  * @retval None
  */
void PWM_SetInterruptAutoreload(PWM_TypeDef* PWMx, u8 value)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_RepeatTimes(value) );
        /* Set the PWM INT_RepeatTimes */
        PWMx->ITARR = value;
}

/**
  * @brief  Set auto reload value for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  value: Auto reload value.
  * @retval None
  */
void PWM_SetAutoreload(PWM_TypeDef* PWMx, u16 value)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        PWMx->ARR = value;
}

/**
  * @brief  Set value of Prescaler for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  value: Prescaler value.
  * @retval None
  */
void PWM_SetPrescaler(PWM_TypeDef* PWMx, u16 value)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        PWMx->PSC = value;
}

/**
  * @brief  Set value of output compare for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  value: output compare value.
  * @retval None
  */
void PWM_SetOCxValue(PWM_TypeDef* PWMx, u8 PWM_Channel, u16 value)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_Channel(PWM_Channel));
        if(PWM_Channel == PWM_Channel_1)        PWMx->OCR1 = value;
        else if(PWM_Channel == PWM_Channel_2)        PWMx->OCR2 = value;
        else if(PWM_Channel == PWM_Channel_3)        PWMx->OCR3 = value;
        else if(PWM_Channel == PWM_Channel_4)        PWMx->OCR4 = value;
}

/**
  * @brief  Set value of input capture for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  value: input capture value.
  * @retval None
  */
void PWM_SetICxValue(PWM_TypeDef* PWMx, u8 PWM_Channel, u16 value)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_Channel(PWM_Channel));
        if(PWM_Channel== PWM_Channel_1)        PWMx->ICR1 = value;
        else if(PWM_Channel == PWM_Channel_2)        PWMx->ICR2 = value;
        else if(PWM_Channel == PWM_Channel_3)        PWMx->ICR3 = value;
        else if(PWM_Channel == PWM_Channel_4)        PWMx->ICR4 = value;
}


/**
  * @brief  Get value of input capture channel for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  PWM_Channel: the input capture channel of PWM peripheral.
        *          This parameter can be one of the following values:
  *        @arg PWM_Channel_1: input capture channel 1 of PWM peripheral
  *        @arg PWM_Channel_2: input capture channel 2 of PWM peripheral
  *        @arg PWM_Channel_3: input capture channel 3 of PWM peripheral
  *        @arg PWM_Channel_4: input capture channel 4 of PWM peripheral
  * @retval the value of input capture channel
  */
u16 PWM_GetICxValue(PWM_TypeDef* PWMx, u8 PWM_Channel)
{
        u16 value=0;
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_Channel(PWM_Channel));
        if(PWM_Channel== PWM_Channel_1)        value=PWMx->ICR1;
        else if(PWM_Channel == PWM_Channel_2)        value=PWMx->ICR2;
        else if(PWM_Channel == PWM_Channel_3)        value=PWMx->ICR3;
        else if(PWM_Channel == PWM_Channel_4)        value=PWMx->ICR4;
        return value;
}

/**
  * @brief  Get counter value for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @retval counter value
  */
u16 PWM_GetCounter(PWM_TypeDef* PWMx)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        return PWMx->CNT;
}

/**
  * @brief  Get auto reload value for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @retval auto reload value
  */
u16 PWM_GetAutoreload(PWM_TypeDef* PWMx)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        return PWMx->ARR;
}

/**
  * @brief  Get prescaler value for PWM peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @retval prescaler value
  */
u16 PWM_GetPrescaler(PWM_TypeDef* PWMx)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        return PWMx->PSC;
}

/**
  * @brief  Configures the PWMx event to be generate by software.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  PWM_EventSource: specifies the event source.
  *   This parameter can be one or more of the following values:
  *     @arg PWM_EventSource_Update: Timer update Event source
  *     @arg PWM_EventSource_Break: Timer Break event source
  * @retval None
  */
void PWM_GenerateEvent(PWM_TypeDef* PWMx,u32 PWM_EventSource)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_EventSource(PWM_EventSource));
        if(PWM_EventSource==PWM_EventSource_Update)
        {
                PWMx->CR1 |= PWM_CR1_UG;
        }
        else if(PWM_EventSource==PWM_EventSource_Break)
        {
                PWMx->BDTR |= PWM_BDTR_BKSC;
        }
}

/**
  * @brief  Enable or Disable PWMx peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  NewState: new state of the PWMx peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void PWM_Cmd(PWM_TypeDef* PWMx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState == ENABLE)
        {
                /* Set the update bit */
                PWMx->CR1 |= PWM_CR1_EN;
        }
        else
        {
                /* Reset the update bit */
                PWMx->CR1 &= (~PWM_CR1_EN);
        }
}

/**
  * @brief  Enables or disables the specified PWM interrupts.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  PWM_IT: Specify the TIM interrupts sources to be enabled or disabled.
  *          This parameter can be one of the following values:
  *        @arg PWM_IT_ARI: Auto reload interrupt
  *        @arg PWM_IT_OC1I: output compare 1 interrupt
  *        @arg PWM_IT_OC2I: output compare 2 interrupt
  *        @arg PWM_IT_OC3I: output compare 3 interrupt
  *        @arg PWM_IT_OC4I: output compare 4 interrupt
  *        @arg PWM_IT_IC1I: input capture 1 interrupt
  *        @arg PWM_IT_IC2I: input capture 2 interrupt
  *        @arg PWM_IT_IC3I: input capture 3 interrupt
  *        @arg PWM_IT_IC4I: input capture 4 interrupt
  *        @arg PWM_IT_BKI: break interrupt
  *        @arg PWM_IT_UIE: update interrupt
  * @param  NewState: This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void PWM_ITConfig(PWM_TypeDef* PWMx, u32 PWM_IT, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_IT(PWM_IT));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if((PWM_IT == PWM_IT_ARI) | (PWM_IT == PWM_IT_OC1I) | (PWM_IT == PWM_IT_OC2I) | (PWM_IT == PWM_IT_OC3I) | (PWM_IT == PWM_IT_OC4I) | (PWM_IT == PWM_IT_UI))
        {
        if(PWM_IT == PWM_IT_IC1I)
        {
            if (NewState != DISABLE)
            {
                /* Enable the interrupt sources */
                PWMx->CR2 |= PWM_CAPR_IC1I;
            }
            else
            {
                /* Disable the interrupt sources */
                PWMx->CR2 &= ~PWM_CAPR_IC1I;
            }
        }
        else
        {
            if (NewState != DISABLE)
            {
                /* Enable the interrupt sources */
                PWMx->CR2 |= PWM_IT;
            }
            else
            {
                /* Disable the interrupt sources */
                PWMx->CR2 &= ~PWM_IT;
            }
        }               
        }
        if(PWM_IT == PWM_IT_BKI)
        {
                if (NewState != DISABLE)
                {
                        /* Enable the interrupt sources */
                        PWMx->BDTR |= PWM_IT;
                }
                else
                {
                        /* Disable the interrupt sources */
                        PWMx->BDTR &= ~PWM_IT;
                }
        }
        if((PWM_IT == PWM_IT_IC1I) | (PWM_IT == PWM_IT_IC2I) | (PWM_IT == PWM_IT_IC3I) | (PWM_IT == PWM_IT_IC4I))
        {
                if (NewState != DISABLE)
                {
                        /* Enable the interrupt sources */
                        PWMx->CAPR |= PWM_IT;
                }
                else
                {
                        /* Disable the interrupt sources */
                        PWMx->CAPR &= ~PWM_IT;
                }
        }
}

/**
  * @brief  Checks whether the specified PWM flag is set or not.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  PWM_FLAG: Specify the flag to be checked.
  *         This parameter can be one of the following values:
  *         @arg PWM_FLAG_ARF: auto reload flag
  *         @arg PWM_FLAG_OC1F: output compare 1 flag
  *         @arg PWM_FLAG_OC2F: output compare 2 flag
  *         @arg PWM_FLAG_OC3F: output compare 3 flag
  *         @arg PWM_FLAG_OC4F: output compare 4 flag
  *         @arg PWM_FLAG_IC1R: input capture 1 rising edge flag
  *         @arg PWM_FLAG_IC1F: input capture 1 falling edge flag
  *         @arg PWM_FLAG_IC2R: input capture 2 rising edge flag
  *         @arg PWM_FLAG_IC3F:        input capture 2 falling edge flag
  *         @arg PWM_FLAG_IC3R: input capture 3 rising edge flag
  *         @arg PWM_FLAG_IC3F:        input capture 3 falling edge flag
  *         @arg PWM_FLAG_IC4R: input capture 4 rising edge flag
  *         @arg PWM_FLAG_IC4F:        input capture 4 falling edge flag
  *         @arg PWM_FLAG_BIF: break input flag
  *         @arg PWM_FLAG_TIF: trigger input flag
  *         @arg PWM_FLAG_UF: update flag
  * @return FlagStatus of PWM_FLAG (SET or RESET).
  */
FlagStatus PWM_GetFlagStatus(PWM_TypeDef* PWMx, u32 PWM_FLAG)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_FLAG(PWM_FLAG));
        if ((PWMx->SR & PWM_FLAG) != RESET)
        {
                return SET;
        }
        else
        {
                return RESET;
        }
}

/**
  * @brief  Clears the PWM's pending flags.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  PWM_FLAG: Specify the flag to be checked.
  *         This parameter can be one of the following values:
  *         @arg PWM_FLAG_ARF: auto reload flag
  *         @arg PWM_FLAG_OC1F: output compare 1 flag
  *         @arg PWM_FLAG_OC2F: output compare 2 flag
  *         @arg PWM_FLAG_OC3F: output compare 3 flag
  *         @arg PWM_FLAG_OC4F: output compare 4 flag
  *         @arg PWM_FLAG_IC1R: input capture 1 rising edge flag
  *         @arg PWM_FLAG_IC1F: input capture 1 falling edge flag
  *         @arg PWM_FLAG_IC2R: input capture 2 rising edge flag
  *         @arg PWM_FLAG_IC3F:        input capture 2 falling edge flag
  *         @arg PWM_FLAG_IC3R: input capture 3 rising edge flag
  *         @arg PWM_FLAG_IC3F:        input capture 3 falling edge flag
  *         @arg PWM_FLAG_IC4R: input capture 4 rising edge flag
  *         @arg PWM_FLAG_IC4F:        input capture 4 falling edge flag
  *         @arg PWM_FLAG_BIF: break input flag
  *         @arg PWM_FLAG_TIF: trigger input flag
  *         @arg PWM_FLAG_UF: update flag
  * @retval None
  */
void PWM_ClearFlag(PWM_TypeDef* PWMx, u32 PWM_FLAG)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_FLAG(PWM_FLAG));
        /* Clear the flags */
        PWMx->SR = PWM_FLAG;
}

/**
  * @brief  Enable the ADC trigger function for PWMx peripheral.
  * @param  PWMx: where x can be TIM1, TIM5 , TIM6 or TIM7 to select the PWM peripheral
  * @param  ADCTrigger: the mode of ADC trigger.
  *         This parameter can be one of the following values:
  *         @arg PWM_ADCTrigger_OC1UE: OC1U trigger ADC enable
  *         @arg PWM_ADCTrigger_OC1DE: OC1D trigger ADC enable
  *         @arg PWM_ADCTrigger_OC2UE: OC2U trigger ADC enable
  *         @arg PWM_ADCTrigger_OC2DE: OC2D trigger ADC enable
  *         @arg PWM_ADCTrigger_OC3UE: OC3U trigger ADC enable
  *         @arg PWM_ADCTrigger_OC3DE: OC3D trigger ADC enable
  *         @arg PWM_ADCTrigger_OC4UE: OC4U trigger ADC enable
  *         @arg PWM_ADCTrigger_OC4DE: OC4D trigger ADC enable
  *         @arg PWM_ADCTrigger_UOAE: UpOVF trigger ADC enable
  *         @arg PWM_ADCTrigger_DOAE: DownOVF trigger ADC enable
  *         @arg PWM_ADCTrigger_IC1RAE: IC1R trigger ADC enable
  *         @arg PWM_ADCTrigger_IC1FAE: IC1F trigger ADC enable
  *         @arg PWM_ADCTrigger_IC2RAE: IC2R trigger ADC enable
  *         @arg PWM_ADCTrigger_IC2FAE: IC2F trigger ADC enable
        *         @arg PWM_ADCTrigger_IC3RAE: IC3R trigger ADC enable
  *         @arg PWM_ADCTrigger_IC3FAE: IC3F trigger ADC enable
        *         @arg PWM_ADCTrigger_IC4RAE: IC4R trigger ADC enable
  *         @arg PWM_ADCTrigger_IC4FAE: IC4F trigger ADC enable
  * @retval None
  */
void PWM_ADCTrigger(PWM_TypeDef* PWMx, u32 ADCTrigger)
{
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_PWM_ADCTrigger(ADCTrigger));
        PWMx->TACR |= ADCTrigger;
}


/**
  * @brief  Enable or Disable Software Break.
  * @param  PWMx
  * @param  NewState: new state of the PWMx peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void PWM_SoftwareBreak_CMD(PWM_TypeDef *PWMx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_PWM_ALL_PERIPH(PWMx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));

        if(NewState!=DISABLE)
        {
          PWMx->BDTR|=PWM_BDTR_BKSC;
        }
        else
        {
          PWMx->BDTR&=~PWM_BDTR_BKSC;       
        }
}

使用特权

评论回复
10
rickluo|  楼主 | 2024-3-25 14:38 | 只看该作者
PT32L007  SPI

  /******************************************************************************
  * @file    PT32x007x_spi.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the Serial peripheral interface (SPI):
  *           + Initialization and Configuration
  *           + Data transfers functions
  *           + Interrupts and flags management
  *            
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/
  
/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_spi.h"


/** @defgroup SPI
  * @brief SPI driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define Init_CR1_MASK   (u32)0xFFFF0030
#define Init_CSS_MASK   (u32)0xFFFFFFF3
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t  SPITimeout = SPIT_LONG_TIMEOUT;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Initializes the SPIx  peripheral according to
  *            the specified parameters in the SPI_InitTypeDef.
  * @param  SPIx: select the PWM peripheral.
  * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef
  *         structure that contains the configuration information for
  *         the specified SPI peripheral.
  * @retval None
  */
void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
{
        u32 tmpreg = 0;
        assert_param(IS_SPI_ALL_PERIPH(SPIx));
        assert_param(IS_SPI_MasterSlaveMode(SPI_InitStruct->SPI_MasterSlaveMode));
        assert_param(IS_SPI_DataFrameFormat(SPI_InitStruct->SPI_DataFrameFormat));
        assert_param(IS_SPI_ClockPolarity(SPI_InitStruct->SPI_ClockPolarity));
        assert_param(IS_SPI_ClockPhase(SPI_InitStruct->SPI_ClockPhase));
        assert_param(IS_SPI_CSS(SPI_InitStruct->SPI_CSS));
  assert_param(IS_SPI_SoftwareControlCSS(SPI_InitStruct->SPI_SoftwareControlCSS));
        assert_param(IS_SPI_BaudRate(SPI_InitStruct->SPI_BaudRate));

        tmpreg = SPIx->CR1;
        tmpreg &= Init_CR1_MASK;
        tmpreg |= (SPI_InitStruct->SPI_DataFrameFormat | SPI_InitStruct->SPI_ClockPolarity |
                             SPI_InitStruct->SPI_ClockPhase | ((SPI_InitStruct->SPI_Prescaler)<<8));
        SPIx->CR1 = tmpreg;
   
        SPIx->CR2 &= ~SPI_CR2_MSM;
        SPIx->CR2 |= SPI_InitStruct->SPI_MasterSlaveMode;

        SPIx->BR = SPI_InitStruct->SPI_BaudRate;
       
        tmpreg = SPIx->CSS;
        tmpreg &= Init_CSS_MASK;
        tmpreg |= SPI_InitStruct->SPI_CSS | SPI_InitStruct->SPI_SoftwareControlCSS;
        SPIx->CSS = tmpreg;
}

/**
  * @brief  Fills each SPI_InitStruct member with its default value.
  * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure which will be initialized.
  * @retval None
  */
void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)
{
        /*--------------- Reset SPI init structure parameters values -----------------*/
        /* Initialize the SPI_Mode member */
        SPI_InitStruct->SPI_MasterSlaveMode = SPI_MasterSlaveMode_Slave;
        /* Initialize the SPI_DataSize member */
        SPI_InitStruct->SPI_DataFrameFormat = SPI_DataFrameFormat_8b;
        /* Initialize the SPI_CPOL member */
        SPI_InitStruct->SPI_ClockPolarity = SPI_ClockPolarity_Low;
        /* Initialize the SPI_CPHA member */
        SPI_InitStruct->SPI_ClockPhase = SPI_ClockPhase_1Edge;
        /* Initialize the SPI_CSS member */
        SPI_InitStruct->SPI_CSS = SPI_CSS_HardwareControl;
    /* Initialize the SPI_SWCS member */
        SPI_InitStruct->SPI_SoftwareControlCSS = SPI_SoftwareControlCSS_High;
        /* Initialize the SPI_Prescaler member */
        SPI_InitStruct->SPI_Prescaler = 2;
        /* Initialize the SPI_BaudRatePrescaler member */
        SPI_InitStruct->SPI_BaudRate = 16;
}

/**
  * @brief  Enables or disables the specified SPI peripheral.
  * @param  SPIx: select the PWM peripheral.
  * @param  NewState: new state of the SPIx peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_SPI_ALL_PERIPH(SPIx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the selected SPI peripheral */
                SPIx->CR2 |= SPI_CR2_EN;
        }
        else
        {
                /* Disable the selected SPI peripheral */
                SPIx->CR2 &= ~SPI_CR2_EN;
        }
}


/**
  * @brief  Configures internally by software the CS pin for the selected SPI.
  * @param  SPIx: select the PWM peripheral.
  * @param  SPI_SWCS_SEL: specifies the SPI CS internal state.
  *          This parameter can be one of the following values:
  *            @arg SPI_CSSInternalSoft_Set: Set CS pin internally
  *            @arg SPI_CSSInternalSoft_Reset: Reset CS pin internally
  * @retval None
  */
void SPI_SoftwareControlCSSConfig(SPI_TypeDef* SPIx, u32 SWCS_Signal)
{
        /* Check the parameters */
        assert_param(IS_SPI_ALL_PERIPH(SPIx));
        assert_param(IS_SPI_SoftwareControlCSS(SWCS_Signal));
        if (SWCS_Signal != SPI_SoftwareControlCSS_Low)
        {
                /* Set NSS pin internally by software */
                SPIx->CSS |= SPI_SoftwareControlCSS_High;
        }
        else
        {
                /* Reset NSS pin internally by software */
                SPIx->CSS &= ~SPI_SoftwareControlCSS_High;
        }
}


/**
  * @brief  Transmits a Data through the SPIx peripheral.
  * @param  SPIx: select the PWM peripheral.
  * @param  Data: Data to be transmitted.
  * @retval None
  */
void SPI_SendData(SPI_TypeDef* SPIx, u16 Data)
{
        /* Check the parameters */
        assert_param(IS_SPI_ALL_PERIPH(SPIx));

        /* Write in the DR register the data to be sent */
        SPIx->DR = Data;
}

/**
  * @brief  Returns the most recent received data by the SPIx peripheral.
  * @param  SPIx: select the PWM peripheral.
  * @retval The value of the received data.
  */
u16 SPI_ReceiveData(SPI_TypeDef* SPIx)
{
        /* Check the parameters */
        assert_param(IS_SPI_ALL_PERIPH(SPIx));

        /* Return the data in the DR register */
        return SPIx->DR;
}


/**
  * @brief  Enables or disables the specified SPI interrupts.
  * @param  SPIx: select the PWM peripheral.
  * @param  SPI_IT: specifies the SPI interrupt source to be enabled or disabled.
  *          This parameter can be one of the following values:
  *            @arg SPI_IE_OVRE
  *            @arg SPI_IE_OTE
  *            @arg SPI_IE_RXHE
  *            @arg SPI_IE_TXHE
  * @param  NewState: new state of the specified SPI interrupt.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void SPI_ITConfig(SPI_TypeDef* SPIx, u32 SPI_IT, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_SPI_ALL_PERIPH(SPIx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        assert_param(IS_SPI_IT(SPI_IT));
        if (NewState != DISABLE)
        {
                /* Enable the selected SPI interrupt */
                SPIx->IE |= SPI_IT;
        }
        else
        {
                /* Disable the selected SPI interrupt */
                SPIx->IE &= ~SPI_IT;
        }
}

/**
  * @brief  Checks whether the specified SPI flag is set or not.
  * @param  SPIx: select the PWM peripheral.
  * @param  SPI_FLAG: specifies the SPI flag to check.
  *          This parameter can be one of the following values:
  *            @arg SPI_FLAG_TXE
  *            @arg SPI_FLAG_TNF
  *            @arg SPI_FLAG_RXNE
  *            @arg SPI_FLAG_RXF
  *            @arg SPI_FLAG_BSY
  *            @arg SPI_FLAG_OVR
  *            @arg SPI_FLAG_OT
  *            @arg SPI_FLAG_RXH
  *            @arg SPI_FLAG_TXH
  * @retval The new state of SPI_FLAG (SET or RESET).
  */
FlagStatus SPI_GetFlagStatus(SPI_TypeDef* SPIx, u32 SPI_FLAG)
{
        FlagStatus bitstatus = RESET;
        /* Check the parameters */
        assert_param(IS_SPI_ALL_PERIPH(SPIx));
        assert_param(IS_SPI_FLAG(SPI_FLAG));
        if(SPI_FLAG <= SPI_FLAG_OVR)
        {
   if ((SPIx->SR1&SPI_FLAG) != RESET)
                        {
                                        /* */
                                        bitstatus = SET;
                        }
                        else
                        {
                                        /* */
                                        bitstatus = RESET;
                        }
        }               
        else
        {
                if ((SPIx->SR2&(SPI_FLAG&0xFFFEFFFF)) != RESET)
                {
                        /* */
                        bitstatus = SET;
                }
                else
                {
                        /* */
                        bitstatus = RESET;
                }
        }
        /*  */
        return  bitstatus;
}

/**
  * @brief  Clears the SPIx flag.
  * @param  SPIx: select the PWM peripheral.
  * @param  SPI_FLAG: Specify the flag to be cleared.
  *         This parameter can be one of the following values:
  *         @arg SPI_IFC_OVR
  *         @arg SPI_IFC_OT
  * @retval None
  */
void SPI_ClearFlag(SPI_TypeDef* SPIx, u32 SPI_FLAG)
{
        /* Check the parameters */
        assert_param(IS_SPI_ALL_PERIPH(SPIx));
        assert_param(IS_SPI_FLAG(SPI_FLAG));
    if(SPI_FLAG == SPI_FLAG_OVR)
    {
       SPIx->IC |= SPI_IC_OVR;
    }
    else if(SPI_FLAG == SPI_FLAG_OT)
    {
       SPIx->IC |= SPI_IC_OT;
    }
}


/**
  * @brief  This function reset the Rx and the Tx FIFOs
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @param  FIFO: Transmit FIFO or receive FIFO for the UART .
  *          This parameter can be:
  *            @arg Rx_FIFO: Receive FIFO .
  *            @arg Tx_FIFO: Transmit FIFO .
  * @retval None.
  */
void SPI_FifoReset(SPI_TypeDef* SPIx, u8 SPI_FIFO)
{
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
        assert_param(IS_SPI_FIFO(SPI_FIFO));
        switch (SPI_FIFO)
        {
                case FIFO_RXFR:
                        SPIx->RXFR = SPI_RXFR_RXFR;
                        break;
                case FIFO_TXFR :
                        SPIx->TXFR = SPI_TXFR_TXFR;
                        break;
        }
}

/**
  * @}
  */

使用特权

评论回复
11
rickluo|  楼主 | 2024-3-25 14:39 | 只看该作者
PT32L007 TIME

  /******************************************************************************
  * @file    PT32x007x_tim.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the TIM peripheral:
  *            + TimeBase management
  *            + Output Compare management
  *            + Input Capture management
  *            + Interrupts, flags management
  *            + Clocks management
  *            + Synchronization management
  *            + Specific interface management
  *            + Specific remapping management
  *            
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/
  

/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_tim.h"



/** @defgroup TIM
  * @brief TIM driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Initializes the TIMx Time Base Unit peripheral according to
  *         the specified parameters in the TIM_TimeBaseInitStruct.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @param  TimeBaseInit: pointer to a TIM_TimeBaseInitTypeDef
  *         structure that contains the configuration information for
  *         the specified TIM peripheral.
  * @retval None
  */
void TIM_TimeBaseInit(TIMx_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TimeBaseInit)
{
                u32  tmpreg = 0;
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
   
        if(TIMx == TIM2 || TIMx == TIM3)
        {
                assert_param(IS_TIM_Direction(TimeBaseInit->TIM_Direction));
                tmpreg=TIMx->CR2;
                tmpreg&=~TIM_CR2_DIR;
                tmpreg|=TimeBaseInit->TIM_Direction;
                TIMx->CR2=tmpreg;
        }
        /* Set the counter match value */
        TIMx->ARR = TimeBaseInit->TIM_AutoReload;
        /* Set the Prescaler value */
        TIMx->PSC = TimeBaseInit->TIM_Prescaler;   
}


/**
  * @brief  Fills each TIM_TimeBaseInitStruct member with its default value.
  * @param  TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TimeBaseInit)
{

        TimeBaseInit->TIM_AutoReload = 0xFFFF;
        TimeBaseInit->TIM_Prescaler = 0x0000;
        TimeBaseInit->TIM_Direction = TIM_Direction_Up;
}


/**
  * @brief  Configures the TIMx Prescaler.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @param  Prescaler: specifies the Prescaler Register value (0~65535)
  * @retval None
  */
void TIM_SetPrescaler(TIMx_TypeDef* TIMx, u16 Prescaler)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        /* Set the Prescaler value */
        TIMx->PSC = Prescaler;
}


/**
  * @brief  Configures the TIMx Reload value.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @param  Reload: specifies the Counter reload value (0~65535)
  * @retval None
  */
void TIM_SetAutoReload(TIMx_TypeDef* TIMx, u16 Reload)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        /* Set the counter reload value */
        TIMx->ARR = Reload;
}

/**
  * @brief  Configures the TIMx Current Value.
  * @param  TIMx: where x can be 2, 3 to select the TIM peripheral
  * @param  Counter: specifies the Counter register new value.
  * @retval None
  */
void TIM_SetCounter(TIMx_TypeDef* TIMx, u16 Counter)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        /* Set the Counter Register value */
        TIMx->CNT = Counter;
}

/**
  * @brief  Gets the TIMx Counter value.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @retval Counter Register value.
  */
u16 TIM_GetCounter(TIMx_TypeDef* TIMx)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        /* Get the Counter Register value */
        return  TIMx->CNT;
}

/**
  * @brief  Gets the TIMx Prescaler value.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @retval Prescaler Register value.
  */
u16 TIM_GetPrescaler(TIMx_TypeDef* TIMx)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        /* Get the Prescaler Register value */
        return  TIMx->PSC;
}

/**
  * @brief  Enable or Disable TIMx Counter
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @param  NewState: This parameter can be ENABLE or DISABLE.
  * @retval None
  */

void TIM_Cmd(TIMx_TypeDef* TIMx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState == ENABLE)
        {
                /* Set the update bit */
                TIMx->CR1 |= TIM_CR1_EN;
        }
        else
        {
                /* Reset the update bit */
                TIMx->CR1 &= ~TIM_CR1_EN;
        }
}

/**
  * @brief  Enables or disables the specified TIM interrupts.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @param  TIM_INT: Specify the TIM interrupts sources to be enabled or disabled.
  *          This parameter can be one of the following values:
  *        @arg TIM_CR2_ARI
  * @param  NewState: This parameter can be ENABLE or DISABLE.
  * @retval None
  */

void TIM_ITConfig(TIMx_TypeDef* TIMx, u32 TIM_IT, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        assert_param(IS_TIM_IT(TIM_IT));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the interrupt sources */
                TIMx->CR2 |= TIM_IT;
        }
        else
        {
                /* Disable the interrupt sources */
                TIMx->CR2 &= ~TIM_IT;
        }
}

/**
  * @brief  Checks whether the specified TIM flag is set or not.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @param  TIM_FLAG: Specify the flag to be checked.
  *         This parameter can be one of the following values:
  *         @arg TIMx_SR_ARF
  * @return FlagStatus of TIM_FLAG (SET or RESET).
  */
FlagStatus TIM_GetFlagStatus(TIMx_TypeDef* TIMx, u32 TIM_FLAG)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        assert_param(IS_TIM_FLAG(TIM_FLAG));
        if ((TIMx->SR & TIM_FLAG) != 0)
        {
                return SET;
        }
        else
        {
                return RESET;
        }
}

/**
  * @brief  Clears the TIM's pending flags.
  * @param  TIMx: where x can be 2, 3 and 4 to select the TIM peripheral
  * @param  TIM_FLAG: Specify the flag to be cleared.
  *         This parameter can be one of the following values:
  *         @arg TTIMx_SR_ARF
  * @retval None
  */
void TIM_ClearFlag(TIMx_TypeDef* TIMx, u32 TIM_FLAG)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        assert_param(IS_TIM_FLAG(TIM_FLAG));
        /* Clear the flags */
        TIMx->SR |= TIM_FLAG;
}

void TIM_GenerateEvent(TIMx_TypeDef* TIMx,u32 TIM_EventSource)
{
        /* Check the parameters */
        assert_param(IS_TIM_ALL_PERIPH(TIMx));
        assert_param(IS_TIM_EventSource(TIM_EventSource));
        if(TIM_EventSource==PWM_EventSource_Update)
        {
                TIMx->CR1 |= TIM_CR1_UG;
        }
}
/**
  * @}
  */
  

使用特权

评论回复
12
rickluo|  楼主 | 2024-3-25 14:39 | 只看该作者
PT32L007 串口

  /******************************************************************************
  * @file    PT32x007x_uart.c
  * @author  应用开发团队
  * @version V1.5.0
  * @date    2023/11/20
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the Universal synchronous asynchronous receiver
  *          transmitter (USART):
  *           + Initialization and Configuration
  *           + STOP Mode
  *           + BaudRate
  *           + Data transfers
  *           + Multi-Processor Communication
  *           + Half-duplex mode
  *           + Smartcard mode
  *           + Interrupts and flags management
  *            
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "PT32x007x_uart.h"


/** @defgroup UART
  * @brief UART driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/\
#define Init_CR_MASK        (u32)0xFFFFFE80
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Initializes the UARTx peripheral according to the specified
  *         parameters in the USART_InitStruct .
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @param  UART_InitStruct: pointer to a UART_InitTypeDef structure that contains
  *         the configuration information for the specified UART peripheral.
  * @retval None
  */
void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct)
{
        u32  tmpreg = 0, remainder = 0,quotient = 0, pclk = 0;
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_BaudRate(UART_InitStruct->UART_BaudRate));
        assert_param(IS_UART_WordLengthAndParity(UART_InitStruct->UART_WordLengthAndParity));
        assert_param(IS_UART_StopBitLength(UART_InitStruct->UART_StopBitLength));
        assert_param(IS_UART_ParityMode(UART_InitStruct->UART_ParityMode));
        assert_param(IS_UART_Receiver(UART_InitStruct->UART_Receiver));
   assert_param(IS_UART_LoopbackMode(UART_InitStruct->UART_LoopbackMode));
        UARTx->RXFR = UART_RXFR_RXFR;
        UARTx->TXFR = UART_TXFR_TXFR;
       
        tmpreg &= Init_CR_MASK;
        tmpreg |=  (UART_InitStruct ->UART_WordLengthAndParity  | \
                                                        UART_InitStruct ->UART_StopBitLength        | \
                                                        UART_InitStruct ->UART_ParityMode           | \
                                                        UART_InitStruct->UART_LoopbackMode          | \
                                                        UART_InitStruct->UART_Receiver);
        UARTx->CR = tmpreg;
       
        pclk = RCC_GetClockFreq(RCC_PCLK);
   
        quotient =  (pclk / (16 * UART_InitStruct->UART_BaudRate));
  remainder = (pclk % (16 * UART_InitStruct->UART_BaudRate));
  if(quotient==0)
        {
                UARTx->BRR=1;
        }
        else
        {
                if (remainder > (8 * UART_InitStruct->UART_BaudRate))
                {
                        UARTx->BRR = (u16) (quotient + 1);         
                }
                else
                {
                        UARTx->BRR = (u16) quotient;        
                }
        }
}

/**
  * @brief  Fills each UART_InitStruct member with its default value.
  * @param  UART_InitStruct: pointer to a UART_InitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void UART_StructInit(UART_InitTypeDef* UART_InitStruct)
{
        /* USART_InitStruct members default value */
        UART_InitStruct->UART_BaudRate = 19200;
        UART_InitStruct->UART_WordLengthAndParity = UART_WordLengthAndParity_8DP;
        UART_InitStruct->UART_StopBitLength = UART_StopBitLength_1;
        UART_InitStruct->UART_ParityMode = UART_ParityMode_Odd;
        UART_InitStruct->UART_Receiver = UART_Receiver_Enable;
        UART_InitStruct->UART_LoopbackMode = UART_LoopbackMode_Disable;
}

/**
  * @brief  Enables or disables the specified UART peripheral.
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @param  NewState: new state of the USARTx peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void UART_Cmd(UART_TypeDef* UARTx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the selected USART by setting the UE bit in the CR1 register */
                UARTx->CR |= UART_CR_EN;
        }
        else
        {
                /* Disable the selected USART by clearing the UE bit in the CR1 register */
                UARTx->CR &= (~UART_CR_EN);
        }
}

/**
  * @brief  Transmits single data through the UARTx peripheral.
  * @param  UARTx: where x can be from 0 or 1 to select the UART peripheral.
  * @param  Data: the data to transmit.
  * @retval None
  */
void UART_SendData(UART_TypeDef* UARTx, u16 Data)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_DATA(Data));
        /* While the TxFIFO contain 8 characters. */
        while((UARTx->SR & UART_SR_TXF));
        /* Transmit Data */
        UARTx->DR = (Data & (u16)0x01FF);
}

/**
  * @brief  Returns the most recent received data by the UARTx peripheral.
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @retval The received data.
  */
u16 UART_ReceiveData(UART_TypeDef* UARTx)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        /* Receive Data */
        return (u16)(UARTx->DR & (u16)0x01FF);
}

/**
  * @brief  This function reset the Rx and the Tx FIFOs
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @param  FIFO: Transmit FIFO or receive FIFO for the UART .
  *          This parameter can be:
  *            @arg Rx_FIFO: Receive FIFO .
  *            @arg Tx_FIFO: Transmit FIFO .
  * @retval None.
  */
void UART_FifoReset(UART_TypeDef* UARTx, u8 UART_FIFO)
{
  assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_FIFO(UART_FIFO));
        switch (UART_FIFO)
        {
                case FIFO_RX :
                        UARTx->RXFR = UART_RXFR_RXFR;
                        break;
                case FIFO_TX :
                        UARTx->TXFR = UART_TXFR_TXFR;
                        break;
        }
}

/**
  * @brief  Enables or disables the UART's Half Duplex communication.
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @param  NewState: new state of the UART Communication.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void UART_HalfDuplexCmd(UART_TypeDef* UARTx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the Half-Duplex mode by setting the OWE bit in the CR register */
                UARTx->CR |= UART_CR_SLME;
        }
        else
        {
                /* Disable the Half-Duplex mode by clearing the OWE bit in the CR register */
                UARTx->CR  &= (u32)~((u32)UART_CR_SLME);
        }
}

/**
  * @brief  Enables or disables the UART's transmitter or receiver.
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @param  UART_Direction: specifies the UART direction.
  *          This parameter can be any combination of the following values:
  *            @arg UART_SingleLineDirection_Tx: UART Single Line Direction Transmitter
  *            @arg UART_SingleLineDirection_Rx: UART Single Line Direction Receiver
  * @param  NewState: new state of the USART transfer direction.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void UART_HalfDuplexDirectionConfig(UART_TypeDef* UARTx, u32 Direction)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_SingleLineDirection(Direction));
        if (Direction != UART_SingleLineDirection_Rx)
        {
                /* Set NSS pin internally by software */
                UARTx->CR |= UART_SingleLineDirection_Tx;
        }
        else
        {
                /* Reset NSS pin internally by software */
                UARTx->CR &= ~UART_SingleLineDirection_Tx;
        }
}

/**
  * @brief  Enables or disables the UART's IrDA interface.
  * @param  UARTx: where x can be 0 to select the UART peripheral.
  * @param  NewState: new state of the IrDA mode.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void UART_IrDACmd(UART_TypeDef* UARTx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the IrDA mode by setting the IRE bit in the IMCR register */
                UARTx->IMCR |= UART_IMCR_IRE;
        }
        else
        {
                /* Disable the IrDA mode by clearing the EN bit in the IMCR register */
                UARTx->IMCR &= ~UART_IMCR_IRE;
        }
}

/**
  * @brief  Configures the IrDA's Pin polarity
  * @param  UARTx: where x can be 0 to select the UART peripheral.
  * @param  NewState: new defined levels for the USART data.
  *          This parameter can be:
  *            @arg ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1).
  *            @arg pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0).
  * @note   This function has to be called before calling UART_Cmd() function.
  * @retval None
  */
void UART_IrDAPolarityConfig(UART_TypeDef* UARTx, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                /* Enable the binary data inversion feature by setting the DATAINV bit in
                   the CR2 register */
                UARTx->IMCR |= UART_IMCR_IRPN;
        }
        else
        {
                /* Disable the binary data inversion feature by clearing the DATAINV bit in
                   the CR2 register */
                UARTx->IMCR &= ~UART_IMCR_IRPN;
        }
}

/**
  * @brief  Sets the infrared duty cycle for UART peripheral.
  * @param  UARTx: where x can be 0 to select the UART peripheral.
  * @param  IrDADutyCycle: specifies the infrared duty cycle.
  * @note   This function has to be called before calling UART_Cmd() function.
  * @retval None
  */
void UART_IrDADutyCycleConfig(UART_TypeDef* UARTx, u16 IrDADutyCycle)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_InfraredDutyCycle(IrDADutyCycle));
        /* Clear the IrDA's modulation PWM duty cycle */
        UARTx->IMDCR &= ~UART_IMDCR_DUTY;
        /* Set the IrDA's modulation PWM duty cycle*/
        UARTx->IMDCR |= IrDADutyCycle;
}

/**
  * @brief  Enables or disables the specified USART interrupts.
  * @param  UARTx: where x can be from 1 to 8 to select the USART peripheral.
  * @param  UART_IT: specifies the UART interrupt sources to be enabled or disabled.
  *          This parameter can be one of the following values:
  *            @arg UART_IT_RXNEI: specifies the interrupt source for Receiver FIFO buffer non-empty interrupt.
  *            @arg UART_IT_RXFI: specifies the interrupt source for Receiver FIFO buffer full interrupt.
  *            @arg UART_IT_PEI: specifies the interrupt source for Parity error interrupt.
  *            @arg UART_IT_FEI: specifies the interrupt source for Frame error interrupt.
  *            @arg UART_IT_OVRI: specifies the interrupt source for Receiver FIFO buffer overflow interrupt.
  *            @arg UART_IT_TXEI: specifies the interrupt source for Transmitter FIFO buffer empty interrupt.
  *            @arg UART_IT_TXFI: specifies the interrupt source for Transmitter FIFO buffer full interrupt.
  *            @arg UART_IT_TXOI: specifies the interrupt source for Transfer end interrupt
  * @param  NewState: new state of the specified USARTx interrupts.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void UART_ITConfig(UART_TypeDef* UARTx, u32 UART_IT, FunctionalState NewState)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_IT(UART_IT));
        assert_param(IS_FUNCTIONAL_STATE(NewState));
        if (NewState != DISABLE)
        {
                UARTx->IE |= UART_IT;
        }
        else
        {
                UARTx->IE &= ~UART_IT;
        }
}

/**
  * @brief  Checks whether the specified UART flag is set or not.
  * @param  UARTx: where x can be from 0 to 1 to select the UART peripheral.
  * @param  UART_FLAG: specifies the flag to check.
  *          This parameter can be one of the following values:
  *            @arg UART_FLAG_RXNE: Receiver FIFO not empty flag.
  *            @arg UART_FLAG_RXF: Receiver FIFO full flag.
  *            @arg UART_FLAG_PE: Parity error flag.
  *            @arg UART_FLAG_FE: Frame error flag.
  *            @arg UART_FLAG_OVR: Receiver FIFO buffer overflow flag.
  *            @arg UART_FLAG_TXE: Transmitter FIFO empty flag.
  *            @arg UART_FLAG_TXF: Transmitter FIFO full flag.
  *            @arg UART_FLAG_TXO: Transfer end flag.
  * @retval The new state of UART_FLAG (SET or RESET).
  */
FlagStatus UART_GetFlagStatus(UART_TypeDef* UARTx, u32 UART_FLAG)
{
        FlagStatus bitstatus = RESET;
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_FLAG(UART_FLAG));
        if ((UARTx->SR & UART_FLAG) != (u16)RESET)
        {
                bitstatus = SET;
        }
        else
        {
                bitstatus = RESET;
        }
        return bitstatus;
}

/**
  * @brief  Clears the UARTx's pending flags.
  * @param  UARTx: where x can be from 0 to 1 to select the USART peripheral.
  * @param  USART_FLAG: specifies the flag to clear.
  *          This parameter can be any combination of the following values:
  *            @arg UART_FLAG_PE: Parity error flag.
  *            @arg UART_FLAG_FE: Frame error flag.
  *            @arg UART_FLAG_OVR: Receiver FIFO buffer overflow flag.
  * @retval None
  */
void UART_ClearFlag(UART_TypeDef* UARTx, u32 UART_FLAG)
{
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_FLAG(UART_FLAG));
        if(UART_FLAG==UART_FLAG_PE || UART_FLAG==UART_FLAG_FE || UART_FLAG==UART_FLAG_OVR)
        {
                UARTx->SR = UART_FLAG;
        }
}

/**
  * @brief  Checks whether the specified UART intterrupt status is set or not.
  * @param  UARTx: where x can be from 0 to 1 to select the USART peripheral.
  * @param  UART_IT: Specify the intterrupt status to be checked.
  *         This parameter can be one of the following values:
        *            @arg UART_IT_RXNEI: specifies the interrupt source for Receiver FIFO buffer non-empty interrupt.
  *            @arg UART_IT_RXFI: specifies the interrupt source for Receiver FIFO buffer full interrupt.
  *            @arg UART_IT_PEI: specifies the interrupt source for Parity error interrupt.
  *            @arg UART_IT_FEI: specifies the interrupt source for Frame error interrupt.
  *            @arg UART_IT_OVRI: specifies the interrupt source for Receiver FIFO buffer overflow interrupt.
  *            @arg UART_IT_TXEI: specifies the interrupt source for Transmitter FIFO buffer empty interrupt.
  *            @arg UART_IT_TXFI: specifies the interrupt source for Transmitter FIFO buffer full interrupt.
  *            @arg UART_IT_TXOI: specifies the interrupt source for Transfer completed interrupt
  * @return ITStatus of UART_IT (SET or RESET).
  */
ITStatus UART_GetITStatus(UART_TypeDef* UARTx, u32 UART_IT)
{
        ITStatus bitstatus = RESET;
        u32 enablestatus = 0;
        /* Check the parameters */
        assert_param(IS_UART_ALL_PERIPH(UARTx));
        assert_param(IS_UART_IT(UART_IT));
        /**/
        enablestatus = (u32)(UARTx->IE & UART_IT);
        /*  */
        if (((u32)(UARTx->SR & UART_IT) != (u32)RESET) && (enablestatus != (u32)RESET))
        {
                /* */
                bitstatus = SET;
        }
        else
        {
                /* */
                bitstatus = RESET;
        }
        /**/
        return  bitstatus;
}

/**
  * @}
  */


使用特权

评论回复
13
rickluo|  楼主 | 2024-5-18 12:34 | 只看该作者
/**********************************************************/
///< UART1 中断服务函数  波特率 9600
//***********************************************************
void UART0_Handler(void)
{
//        static boolean_t debug=FALSE ;
        //uint16_t ssss=(uint16_t )UART0 ->SR ;
       
        if(UART_GetITStatus(UART0,UART_IT_RXNEI)!=RESET)
        {
                        UART_ClearFlag(UART0,UART_IT_RXNEI);       
                       
                        uint8_t dat  =(uint8_t )(UART0 ->DR );                        //(uint8_t )UART_ReceiveData(UART0);
                        UART0 ->DR =dat ;
//                        u8RxData [u8RxCnt ++]=dat;        //               
//                        rxtimeout_cnt=0;                                                        /** 时间清0 **/
//                        if(u8RxCnt >=Num_UartBufMax)                        /*数据包出错*/
//                        {
//                                u8RxCnt =0;
//                                u8RxDatalen=0;
//                                return ;
//                        }                       
//                        /***********************************************/       
//                        /*****重新定位包头,****/
//                        if(u8RxCnt >3  && u8RxData[u8RxCnt-2 ]==0xAA && u8RxData[u8RxCnt -1]==0x55 )
//                        {
//                                if(u8RxData [0]!=0xAA || u8RxData [1]!=0x55)
//                                {
//                                        u8RxData [0]=0xAA;
//                                        u8RxData [1]=0x55;
//                                        u8RxCnt =2;
//                                        u8RxDatalen =0;
//                                }                       
//                        }
//                        /**************************************************/
//                        if(u8RxCnt ==5)                //接收完到包长度
//                        {
//                                if(u8RxData [0]==0xAA && u8RxData[1]==0x55)                        /* 包头正确 */
//                                {
//                                        u8RxDatalen=u8RxData[3]+3;                                /** H不要 **/
//                                        return ;
//                                }                                 
//                        }
//                        if(u8RxDatalen >5 && u8RxDatalen ==u8RxCnt && dat ==0xFB )                //接收完成
//                        {
////                                uint16_t crc=(uint16_t )u8RxData[u8RxCnt-2];
////                                crc<<=8;
////                                crc |=(uint16_t )u8RxData[u8RxCnt-3];
////                                uint16_t crc2 =CRC16 (&u8RxData [2],(u8RxCnt -2-3));
////                                if(crc2==crc)
////                                {
//                                        Uart1_RxEnd  =TRUE ;                       
//                                        RxCMD =u8RxData [5];
//                                        /**接收完成 数据转入buf2缓存 **/
//                                        for(uint8_t ii=0;ii<u8RxCnt ;ii++)
//                                        {
//                                                u8RxBuf2 [ii]=u8RxData[ii];               
//                                                //u8TxData[ii]=u8RxData[ii];
//                                        }
//                                        u8RxBuf2_Dlen =u8RxCnt ;
////                                }                               
//                                u8RxDatalen=0;
//                                u8RxCnt =0;
//                                //等待读取
//                                /****************************/                                 
//                        }                                               
        }
        if(UART0->SR & UART_SR_TXE )
        {
                UART0 ->SR |= (~(UART_SR_TXE));       

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

60

主题

431

帖子

4

粉丝