format utf-8

This commit is contained in:
yanmowudi 2021-05-06 10:42:41 +08:00
parent c00300d5f4
commit 2c05296948
46 changed files with 8127 additions and 8039 deletions

File diff suppressed because it is too large Load Diff

View File

@ -79,7 +79,7 @@ __Vectors DCD __initial_sp ; Top of Stack
DCD GPIOA5_Handler
DCD GPIOA6_Handler
DCD GPIOA7_Handler
DCD GPIOB0_Handler
DCD GPIOB0_Handler
DCD GPIOB1_Handler
DCD GPIOB2_Handler
DCD GPIOB3_Handler
@ -103,19 +103,19 @@ __Vectors DCD __initial_sp ; Top of Stack
DCD GPIOM5_Handler
DCD GPIOM6_Handler
DCD GPIOM7_Handler
DCD DMA_Handler
DCD DMA_Handler
DCD LCD_Handler
DCD NORFLC_Handler
DCD CAN_Handler
DCD CAN_Handler
DCD PULSE_Handler
DCD WDT_Handler
DCD PWM_Handler
DCD UART0_Handler
DCD UART1_Handler
DCD UART2_Handler
DCD UART3_Handler
DCD 0
DCD I2C0_Handler
DCD UART1_Handler
DCD UART2_Handler
DCD UART3_Handler
DCD 0
DCD I2C0_Handler
DCD I2C1_Handler
DCD SPI0_Handler
DCD ADC0_Handler
@ -130,13 +130,13 @@ __Vectors DCD __initial_sp ; Top of Stack
DCD GPIOP_Handler
DCD ADC1_Handler
DCD FPU_Handler
DCD SPI1_Handler
DCD TIMR0_Handler
DCD TIMR1_Handler
DCD TIMR2_Handler
DCD TIMR3_Handler
DCD TIMR4_Handler
DCD TIMR5_Handler
DCD SPI1_Handler
DCD TIMR0_Handler
DCD TIMR1_Handler
DCD TIMR2_Handler
DCD TIMR3_Handler
DCD TIMR4_Handler
DCD TIMR5_Handler
__Vectors_End

View File

@ -113,7 +113,7 @@ __isr_vector:
.long TIMR4_Handler
.long TIMR5_Handler
.section .text.Reset_Handler
.section .text.Reset_Handler
.align 2
.globl Reset_Handler
.type Reset_Handler, %function

View File

@ -84,7 +84,7 @@ __vector_table
DCD DMA_Handler
DCD LCD_Handler
DCD NORFLC_Handler
DCD CAN_Handler
DCD CAN_Handler
DCD PULSE_Handler
DCD WDT_Handler
DCD PWM_Handler

View File

@ -17,56 +17,48 @@
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
*******************************************************************************************************************************************/
#include <stdint.h>
#include "SWM320.h"
/******************************************************************************************************************************************
*
*****************************************************************************************************************************************/
#define SYS_CLK_20MHz 0 //0 内部高频20MHz RC振荡器
#define SYS_CLK_40MHz 1 //1 内部高频40MHz RC振荡器
#define SYS_CLK_32KHz 2 //2 内部低频32KHz RC振荡器
#define SYS_CLK_XTAL 3 //3 外部晶体振荡器2-30MHz
#define SYS_CLK_PLL 4 //4 片内锁相环输出
#define SYS_CLK_20MHz 0 //0 内部高频20MHz RC振荡器
#define SYS_CLK_40MHz 1 //1 内部高频40MHz RC振荡器
#define SYS_CLK_32KHz 2 //2 内部低频32KHz RC振荡器
#define SYS_CLK_XTAL 3 //3 外部晶体振荡器2-30MHz
#define SYS_CLK_PLL 4 //4 片内锁相环输出
#define SYS_CLK SYS_CLK_PLL
#define SYS_CLK SYS_CLK_PLL
#define SYS_CLK_DIV_1 0
#define SYS_CLK_DIV_2 1
#define SYS_CLK_DIV_1 0
#define SYS_CLK_DIV_2 1
#define SYS_CLK_DIV SYS_CLK_DIV_1
#define __HSI (20000000UL) //高速内部时钟
#define __LSI ( 32000UL) //低速内部时钟
#define __HSE (20000000UL) //高速外部时钟
#define SYS_CLK_DIV SYS_CLK_DIV_1
#define __HSI (20000000UL) //高速内部时钟
#define __LSI (32000UL) //低速内部时钟
#define __HSE (20000000UL) //高速外部时钟
/********************************** PLL 设定 **********************************************
* VCO输出频率 = PLL输入时钟 / INDIV * 4 * FBDIV
* PLL输出频率 = PLL输入时钟 / INDIV * 4 * FBDIV / OUTDIV = VCO输出频率 / OUTDIV
*****************************************************************************************/
#define SYS_PLL_SRC SYS_CLK_20MHz //可取值SYS_CLK_20MHz、SYS_CLK_XTAL
*****************************************************************************************/
#define SYS_PLL_SRC SYS_CLK_20MHz //可取值SYS_CLK_20MHz、SYS_CLK_XTAL
#define PLL_IN_DIV 5
#define PLL_IN_DIV 5
#define PLL_FB_DIV 60
#define PLL_FB_DIV 60
#define PLL_OUT_DIV8 0
#define PLL_OUT_DIV4 1
#define PLL_OUT_DIV2 2
#define PLL_OUT_DIV8 0
#define PLL_OUT_DIV4 1
#define PLL_OUT_DIV2 2
#define PLL_OUT_DIV PLL_OUT_DIV8
uint32_t SystemCoreClock = __HSI; //System Clock Frequency (Core Clock)
uint32_t CyclesPerUs = (__HSI / 1000000); //Cycles per micro second
#define PLL_OUT_DIV PLL_OUT_DIV8
uint32_t SystemCoreClock = __HSI; //System Clock Frequency (Core Clock)
uint32_t CyclesPerUs = (__HSI / 1000000); //Cycles per micro second
/******************************************************************************************************************************************
* :
@ -75,50 +67,51 @@ uint32_t CyclesPerUs = (__HSI / 1000000); //Cycles per micro second
* :
* :
******************************************************************************************************************************************/
void SystemCoreClockUpdate(void)
void SystemCoreClockUpdate(void)
{
if(SYS->CLKSEL & SYS_CLKSEL_SYS_Msk) //SYS_CLK <= HFCK
{
if(SYS->CLKSEL & SYS_CLKSEL_HFCK_Msk) //HFCK <= XTAL
{
SystemCoreClock = __HSE;
}
else //HFCK <= HRC
{
if(SYS->HRCCR & SYS_HRCCR_DBL_Msk) //HRC = 40MHz
{
SystemCoreClock = __HSI*2;
}
else //HRC = 20MHz
{
SystemCoreClock = __HSI;
}
}
}
else //SYS_CLK <= LFCK
{
if(SYS->CLKSEL & SYS_CLKSEL_LFCK_Msk) //LFCK <= PLL
{
if(SYS->PLLCR & SYS_PLLCR_INSEL_Msk) //PLL_SRC <= HRC
{
SystemCoreClock = __HSI;
}
else //PLL_SRC <= XTAL
{
SystemCoreClock = __HSE;
}
SystemCoreClock = SystemCoreClock / PLL_IN_DIV * PLL_FB_DIV * 4 / (2 << (2 - PLL_OUT_DIV));
}
else //LFCK <= LRC
{
SystemCoreClock = __LSI;
}
}
if(SYS->CLKDIV & SYS_CLKDIV_SYS_Msk) SystemCoreClock /= 2;
CyclesPerUs = SystemCoreClock / 1000000;
if (SYS->CLKSEL & SYS_CLKSEL_SYS_Msk) //SYS_CLK <= HFCK
{
if (SYS->CLKSEL & SYS_CLKSEL_HFCK_Msk) //HFCK <= XTAL
{
SystemCoreClock = __HSE;
}
else //HFCK <= HRC
{
if (SYS->HRCCR & SYS_HRCCR_DBL_Msk) //HRC = 40MHz
{
SystemCoreClock = __HSI * 2;
}
else //HRC = 20MHz
{
SystemCoreClock = __HSI;
}
}
}
else //SYS_CLK <= LFCK
{
if (SYS->CLKSEL & SYS_CLKSEL_LFCK_Msk) //LFCK <= PLL
{
if (SYS->PLLCR & SYS_PLLCR_INSEL_Msk) //PLL_SRC <= HRC
{
SystemCoreClock = __HSI;
}
else //PLL_SRC <= XTAL
{
SystemCoreClock = __HSE;
}
SystemCoreClock = SystemCoreClock / PLL_IN_DIV * PLL_FB_DIV * 4 / (2 << (2 - PLL_OUT_DIV));
}
else //LFCK <= LRC
{
SystemCoreClock = __LSI;
}
}
if (SYS->CLKDIV & SYS_CLKDIV_SYS_Msk)
SystemCoreClock /= 2;
CyclesPerUs = SystemCoreClock / 1000000;
}
/******************************************************************************************************************************************
@ -129,152 +122,160 @@ void SystemCoreClockUpdate(void)
* :
******************************************************************************************************************************************/
void SystemInit(void)
{
SYS->CLKEN |= (1 << SYS_CLKEN_ANAC_Pos);
Flash_Param_at_xMHz(120);
switch(SYS_CLK)
{
case SYS_CLK_20MHz: //0 内部高频20MHz RC振荡器
switchCLK_20MHz();
break;
case SYS_CLK_40MHz: //1 内部高频40MHz RC振荡器
switchCLK_40MHz();
break;
case SYS_CLK_32KHz: //2 内部低频32KHz RC振荡器
switchCLK_32KHz();
break;
case SYS_CLK_XTAL: //3 外部晶体振荡器2-30MHz
switchCLK_XTAL();
break;
case SYS_CLK_PLL: //4 片内锁相环输出
switchCLK_PLL();
break;
}
SYS->CLKDIV &= ~SYS_CLKDIV_SYS_Msk;
SYS->CLKDIV |= (SYS_CLK_DIV << SYS_CLKDIV_SYS_Pos);
SystemCoreClockUpdate();
if(SystemCoreClock > 80000000)
{
Flash_Param_at_xMHz(120);
}
else if(SystemCoreClock > 40000000)
{
Flash_Param_at_xMHz(80);
}
else if(SystemCoreClock > 30000000)
{
Flash_Param_at_xMHz(40);
}
else
{
Flash_Param_at_xMHz(30);
}
{
SYS->CLKEN |= (1 << SYS_CLKEN_ANAC_Pos);
Flash_Param_at_xMHz(120);
switch (SYS_CLK)
{
case SYS_CLK_20MHz: //0 内部高频20MHz RC振荡器
switchCLK_20MHz();
break;
case SYS_CLK_40MHz: //1 内部高频40MHz RC振荡器
switchCLK_40MHz();
break;
case SYS_CLK_32KHz: //2 内部低频32KHz RC振荡器
switchCLK_32KHz();
break;
case SYS_CLK_XTAL: //3 外部晶体振荡器2-30MHz
switchCLK_XTAL();
break;
case SYS_CLK_PLL: //4 片内锁相环输出
switchCLK_PLL();
break;
}
SYS->CLKDIV &= ~SYS_CLKDIV_SYS_Msk;
SYS->CLKDIV |= (SYS_CLK_DIV << SYS_CLKDIV_SYS_Pos);
SystemCoreClockUpdate();
if (SystemCoreClock > 80000000)
{
Flash_Param_at_xMHz(120);
}
else if (SystemCoreClock > 40000000)
{
Flash_Param_at_xMHz(80);
}
else if (SystemCoreClock > 30000000)
{
Flash_Param_at_xMHz(40);
}
else
{
Flash_Param_at_xMHz(30);
}
}
void switchCLK_20MHz(void)
{
uint32_t i;
SYS->HRCCR = (0 << SYS_HRCCR_OFF_Pos) |
(0 << SYS_HRCCR_DBL_Pos); //HRC = 20MHz
for(i = 0; i < 1000; i++) __NOP();
SYS->CLKSEL &= ~SYS_CLKSEL_HFCK_Msk; //HFCK <= HRC
SYS->CLKSEL |= (1 << SYS_CLKSEL_SYS_Pos); //SYS_CLK <= HFCK
uint32_t i;
SYS->HRCCR = (0 << SYS_HRCCR_OFF_Pos) |
(0 << SYS_HRCCR_DBL_Pos); //HRC = 20MHz
for (i = 0; i < 1000; i++)
__NOP();
SYS->CLKSEL &= ~SYS_CLKSEL_HFCK_Msk; //HFCK <= HRC
SYS->CLKSEL |= (1 << SYS_CLKSEL_SYS_Pos); //SYS_CLK <= HFCK
}
void switchCLK_40MHz(void)
{
uint32_t i;
SYS->HRCCR = (0 << SYS_HRCCR_OFF_Pos) |
(1 << SYS_HRCCR_DBL_Pos); //HRC = 40MHz
for(i = 0; i < 1000; i++) __NOP();
SYS->CLKSEL &= ~SYS_CLKSEL_HFCK_Msk; //HFCK <= HRC
SYS->CLKSEL |= (1 << SYS_CLKSEL_SYS_Pos); //SYS_CLK <= HFCK
uint32_t i;
SYS->HRCCR = (0 << SYS_HRCCR_OFF_Pos) |
(1 << SYS_HRCCR_DBL_Pos); //HRC = 40MHz
for (i = 0; i < 1000; i++)
__NOP();
SYS->CLKSEL &= ~SYS_CLKSEL_HFCK_Msk; //HFCK <= HRC
SYS->CLKSEL |= (1 << SYS_CLKSEL_SYS_Pos); //SYS_CLK <= HFCK
}
void switchCLK_32KHz(void)
{
uint32_t i;
SYS->CLKEN |= (1 << SYS_CLKEN_RTCBKP_Pos);
SYS->LRCCR &= ~(1 << SYS_LRCCR_OFF_Pos);
for(i = 0; i < 100; i++) __NOP();
SYS->CLKSEL &= ~SYS_CLKSEL_LFCK_Msk; //LFCK <= LRC
SYS->CLKSEL &= ~SYS_CLKSEL_SYS_Msk; //SYS_CLK <= LFCK
uint32_t i;
SYS->CLKEN |= (1 << SYS_CLKEN_RTCBKP_Pos);
SYS->LRCCR &= ~(1 << SYS_LRCCR_OFF_Pos);
for (i = 0; i < 100; i++)
__NOP();
SYS->CLKSEL &= ~SYS_CLKSEL_LFCK_Msk; //LFCK <= LRC
SYS->CLKSEL &= ~SYS_CLKSEL_SYS_Msk; //SYS_CLK <= LFCK
}
void switchCLK_XTAL(void)
{
uint32_t i;
SYS->XTALCR = (1 << SYS_XTALCR_EN_Pos);
for(i = 0; i < 1000; i++) __NOP();
SYS->CLKSEL |= (1 << SYS_CLKSEL_HFCK_Pos); //HFCK <= XTAL
SYS->CLKSEL |= (1 << SYS_CLKSEL_SYS_Pos); //SYS_CLK <= HFCK
uint32_t i;
SYS->XTALCR = (1 << SYS_XTALCR_EN_Pos);
for (i = 0; i < 1000; i++)
__NOP();
SYS->CLKSEL |= (1 << SYS_CLKSEL_HFCK_Pos); //HFCK <= XTAL
SYS->CLKSEL |= (1 << SYS_CLKSEL_SYS_Pos); //SYS_CLK <= HFCK
}
void switchCLK_PLL(void)
{
uint32_t i;
PLLInit();
SYS->PLLCR |= (1 << SYS_PLLCR_OUTEN_Pos);
for(i = 0; i < 10000; i++) __NOP();
SYS->CLKSEL |= (1 << SYS_CLKSEL_LFCK_Pos); //LFCK <= PLL
SYS->CLKSEL &= ~SYS_CLKSEL_SYS_Msk; //SYS_CLK <= LFCK
uint32_t i;
PLLInit();
SYS->PLLCR |= (1 << SYS_PLLCR_OUTEN_Pos);
for (i = 0; i < 10000; i++)
__NOP();
SYS->CLKSEL |= (1 << SYS_CLKSEL_LFCK_Pos); //LFCK <= PLL
SYS->CLKSEL &= ~SYS_CLKSEL_SYS_Msk; //SYS_CLK <= LFCK
}
void PLLInit(void)
{
uint32_t i;
if(SYS_PLL_SRC == SYS_CLK_20MHz)
{
SYS->HRCCR = (0 << SYS_HRCCR_OFF_Pos) |
(0 << SYS_HRCCR_DBL_Pos); //HRC = 20MHz
for(i = 0; i < 1000; i++) __NOP();
SYS->PLLCR |= (1 << SYS_PLLCR_INSEL_Pos); //PLL_SRC <= HRC
}
else if(SYS_PLL_SRC == SYS_CLK_XTAL)
{
SYS->XTALCR = (1 << SYS_XTALCR_EN_Pos);
for(i = 0; i < 20000; i++);
SYS->PLLCR &= ~(1 << SYS_PLLCR_INSEL_Pos); //PLL_SRC <= XTAL
}
SYS->PLLDIV &= ~(SYS_PLLDIV_INDIV_Msk |
SYS_PLLDIV_FBDIV_Msk |
SYS_PLLDIV_OUTDIV_Msk);
SYS->PLLDIV |= (PLL_IN_DIV << SYS_PLLDIV_INDIV_Pos) |
(PLL_FB_DIV << SYS_PLLDIV_FBDIV_Pos) |
(PLL_OUT_DIV<< SYS_PLLDIV_OUTDIV_Pos);
SYS->PLLCR &= ~(1 << SYS_PLLCR_OFF_Pos);
while(SYS->PLLLOCK == 0); //等待PLL锁定
uint32_t i;
if (SYS_PLL_SRC == SYS_CLK_20MHz)
{
SYS->HRCCR = (0 << SYS_HRCCR_OFF_Pos) |
(0 << SYS_HRCCR_DBL_Pos); //HRC = 20MHz
for (i = 0; i < 1000; i++)
__NOP();
SYS->PLLCR |= (1 << SYS_PLLCR_INSEL_Pos); //PLL_SRC <= HRC
}
else if (SYS_PLL_SRC == SYS_CLK_XTAL)
{
SYS->XTALCR = (1 << SYS_XTALCR_EN_Pos);
for (i = 0; i < 20000; i++)
;
SYS->PLLCR &= ~(1 << SYS_PLLCR_INSEL_Pos); //PLL_SRC <= XTAL
}
SYS->PLLDIV &= ~(SYS_PLLDIV_INDIV_Msk |
SYS_PLLDIV_FBDIV_Msk |
SYS_PLLDIV_OUTDIV_Msk);
SYS->PLLDIV |= (PLL_IN_DIV << SYS_PLLDIV_INDIV_Pos) |
(PLL_FB_DIV << SYS_PLLDIV_FBDIV_Pos) |
(PLL_OUT_DIV << SYS_PLLDIV_OUTDIV_Pos);
SYS->PLLCR &= ~(1 << SYS_PLLCR_OFF_Pos);
while (SYS->PLLLOCK == 0)
; //等待PLL锁定
}

View File

@ -2,28 +2,24 @@
#define __SYSTEM_SWM320_H__
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
extern uint32_t SystemCoreClock; // System Clock Frequency (Core Clock)
extern uint32_t CyclesPerUs; // Cycles per micro second
extern uint32_t SystemCoreClock; // System Clock Frequency (Core Clock)
extern uint32_t CyclesPerUs; // Cycles per micro second
extern void SystemInit(void);
extern void SystemCoreClockUpdate(void);
extern void SystemInit(void);
extern void SystemCoreClockUpdate (void);
extern void switchCLK_20MHz(void);
extern void switchCLK_40MHz(void);
extern void switchCLK_32KHz(void);
extern void switchCLK_XTAL(void);
extern void switchCLK_PLL(void);
extern void PLLInit(void);
extern void switchCLK_20MHz(void);
extern void switchCLK_40MHz(void);
extern void switchCLK_32KHz(void);
extern void switchCLK_XTAL(void);
extern void switchCLK_PLL(void);
extern void PLLInit(void);
#ifdef __cplusplus
}

View File

@ -19,7 +19,6 @@
#include "SWM320.h"
#include "SWM320_adc.h"
/******************************************************************************************************************************************
* : ADC_Init()
* : ADC模数转换器初始化
@ -28,128 +27,129 @@
* :
* :
******************************************************************************************************************************************/
void ADC_Init(ADC_TypeDef * ADCx, ADC_InitStructure * initStruct)
void ADC_Init(ADC_TypeDef *ADCx, ADC_InitStructure *initStruct)
{
switch((uint32_t)ADCx)
{
case ((uint32_t)ADC0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_ADC0_Pos);
break;
case ((uint32_t)ADC1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_ADC1_Pos);
break;
}
ADC_Close(ADCx); //一些关键寄存器只能在ADC关闭时设置
if(initStruct->clk_src == ADC_CLKSRC_HRC)
{
ADCx->CTRL |= (1 << ADC_CTRL_CLKSRC_Pos);
ADCx->CTRL2 &= ~ADC_CTRL2_CLKDIV_Msk;
ADCx->CTRL2 |= (initStruct->clk_div << ADC_CTRL2_CLKDIV_Pos);
}
else
{
if(SYS->PLLCR & SYS_PLLCR_OFF_Msk) PLLInit();
ADCx->CTRL &= ~(1 << ADC_CTRL_CLKSRC_Pos);
SYS->PLLDIV &= ~SYS_PLLDIV_ADVCO_Msk;
SYS->PLLDIV |= ((initStruct->clk_src - 2) << SYS_PLLDIV_ADVCO_Pos);
SYS->PLLDIV &= ~SYS_PLLDIV_ADDIV_Msk;
SYS->PLLDIV |= (initStruct->clk_div << SYS_PLLDIV_ADDIV_Pos);
}
ADCx->CALIBSET = (ADCx == ADC0) ? SYS->BKP[0] : SYS->BKP[1];
ADCx->CALIBEN = (1 << ADC_CALIBEN_OFFSET_Pos) | (1 << ADC_CALIBEN_K_Pos);
ADCx->CTRL2 &= ~(ADC_CTRL2_ADCEVCM_Msk | ADC_CTRL2_PGAIVCM_Msk | ADC_CTRL2_PGAGAIN_Msk | ADC_CTRL2_PGAVCM_Msk);
ADCx->CTRL2 |= (0 << ADC_CTRL2_ADCEVCM_Pos) |
(initStruct->pga_ref << ADC_CTRL2_PGAIVCM_Pos) |
(6 << ADC_CTRL2_PGAGAIN_Pos) |
((uint32_t)6 << ADC_CTRL2_PGAVCM_Pos);
ADCx->CTRL &= ~( 0xFF << ADC_CTRL_CH0_Pos);
ADCx->CTRL |= (initStruct->channels << ADC_CTRL_CH0_Pos);
ADCx->CTRL &= ~(ADC_CTRL_AVG_Msk | ADC_CTRL_TRIG_Msk | ADC_CTRL_CONT_Msk);
ADCx->CTRL |= (initStruct->samplAvg << ADC_CTRL_AVG_Pos) |
(initStruct->trig_src << ADC_CTRL_TRIG_Pos) |
(initStruct->Continue << ADC_CTRL_CONT_Pos);
ADCx->IF = 0xFFFFFFFF; //清除中断标志
ADCx->IE &= ~(ADC_IE_CH0EOC_Msk | ADC_IE_CH1EOC_Msk | ADC_IE_CH2EOC_Msk | ADC_IE_CH3EOC_Msk |
ADC_IE_CH4EOC_Msk | ADC_IE_CH5EOC_Msk | ADC_IE_CH6EOC_Msk | ADC_IE_CH7EOC_Msk);
ADCx->IE |= (((initStruct->EOC_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7EOC_Pos);
ADCx->IE &= ~(ADC_IE_CH0OVF_Msk | ADC_IE_CH1OVF_Msk | ADC_IE_CH2OVF_Msk | ADC_IE_CH3OVF_Msk |
ADC_IE_CH4OVF_Msk | ADC_IE_CH5OVF_Msk | ADC_IE_CH6OVF_Msk | ADC_IE_CH7OVF_Msk);
ADCx->IE |= (((initStruct->OVF_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7OVF_Pos);
ADCx->IE &= ~(ADC_IE_CH0HFULL_Msk | ADC_IE_CH1HFULL_Msk | ADC_IE_CH2HFULL_Msk | ADC_IE_CH3HFULL_Msk |
ADC_IE_CH4HFULL_Msk | ADC_IE_CH5HFULL_Msk | ADC_IE_CH6HFULL_Msk | ADC_IE_CH7HFULL_Msk);
ADCx->IE |= (((initStruct->HFULL_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7HFULL_Pos);
ADCx->IE &= ~(uint32_t)(ADC_IE_CH0FULL_Msk | ADC_IE_CH1FULL_Msk | ADC_IE_CH2FULL_Msk | ADC_IE_CH3FULL_Msk |
ADC_IE_CH4FULL_Msk | ADC_IE_CH5FULL_Msk | ADC_IE_CH6FULL_Msk | ADC_IE_CH7FULL_Msk);
ADCx->IE |= (((initStruct->FULL_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7FULL_Pos);
switch((uint32_t)ADCx)
{
case ((uint32_t)ADC0):
if(initStruct->EOC_IEn | initStruct->OVF_IEn | initStruct->HFULL_IEn | initStruct->FULL_IEn)
{
NVIC_EnableIRQ(ADC0_IRQn);
}
else
{
NVIC_DisableIRQ(ADC0_IRQn);
}
break;
case ((uint32_t)ADC1):
if(initStruct->EOC_IEn | initStruct->OVF_IEn | initStruct->HFULL_IEn | initStruct->FULL_IEn)
{
NVIC_EnableIRQ(ADC1_IRQn);
}
else
{
NVIC_DisableIRQ(ADC1_IRQn);
}
break;
}
switch ((uint32_t)ADCx)
{
case ((uint32_t)ADC0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_ADC0_Pos);
break;
case ((uint32_t)ADC1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_ADC1_Pos);
break;
}
ADC_Close(ADCx); //一些关键寄存器只能在ADC关闭时设置
if (initStruct->clk_src == ADC_CLKSRC_HRC)
{
ADCx->CTRL |= (1 << ADC_CTRL_CLKSRC_Pos);
ADCx->CTRL2 &= ~ADC_CTRL2_CLKDIV_Msk;
ADCx->CTRL2 |= (initStruct->clk_div << ADC_CTRL2_CLKDIV_Pos);
}
else
{
if (SYS->PLLCR & SYS_PLLCR_OFF_Msk)
PLLInit();
ADCx->CTRL &= ~(1 << ADC_CTRL_CLKSRC_Pos);
SYS->PLLDIV &= ~SYS_PLLDIV_ADVCO_Msk;
SYS->PLLDIV |= ((initStruct->clk_src - 2) << SYS_PLLDIV_ADVCO_Pos);
SYS->PLLDIV &= ~SYS_PLLDIV_ADDIV_Msk;
SYS->PLLDIV |= (initStruct->clk_div << SYS_PLLDIV_ADDIV_Pos);
}
ADCx->CALIBSET = (ADCx == ADC0) ? SYS->BKP[0] : SYS->BKP[1];
ADCx->CALIBEN = (1 << ADC_CALIBEN_OFFSET_Pos) | (1 << ADC_CALIBEN_K_Pos);
ADCx->CTRL2 &= ~(ADC_CTRL2_ADCEVCM_Msk | ADC_CTRL2_PGAIVCM_Msk | ADC_CTRL2_PGAGAIN_Msk | ADC_CTRL2_PGAVCM_Msk);
ADCx->CTRL2 |= (0 << ADC_CTRL2_ADCEVCM_Pos) |
(initStruct->pga_ref << ADC_CTRL2_PGAIVCM_Pos) |
(6 << ADC_CTRL2_PGAGAIN_Pos) |
((uint32_t)6 << ADC_CTRL2_PGAVCM_Pos);
ADCx->CTRL &= ~(0xFF << ADC_CTRL_CH0_Pos);
ADCx->CTRL |= (initStruct->channels << ADC_CTRL_CH0_Pos);
ADCx->CTRL &= ~(ADC_CTRL_AVG_Msk | ADC_CTRL_TRIG_Msk | ADC_CTRL_CONT_Msk);
ADCx->CTRL |= (initStruct->samplAvg << ADC_CTRL_AVG_Pos) |
(initStruct->trig_src << ADC_CTRL_TRIG_Pos) |
(initStruct->Continue << ADC_CTRL_CONT_Pos);
ADCx->IF = 0xFFFFFFFF; //清除中断标志
ADCx->IE &= ~(ADC_IE_CH0EOC_Msk | ADC_IE_CH1EOC_Msk | ADC_IE_CH2EOC_Msk | ADC_IE_CH3EOC_Msk |
ADC_IE_CH4EOC_Msk | ADC_IE_CH5EOC_Msk | ADC_IE_CH6EOC_Msk | ADC_IE_CH7EOC_Msk);
ADCx->IE |= (((initStruct->EOC_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6EOC_Pos) |
(((initStruct->EOC_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7EOC_Pos);
ADCx->IE &= ~(ADC_IE_CH0OVF_Msk | ADC_IE_CH1OVF_Msk | ADC_IE_CH2OVF_Msk | ADC_IE_CH3OVF_Msk |
ADC_IE_CH4OVF_Msk | ADC_IE_CH5OVF_Msk | ADC_IE_CH6OVF_Msk | ADC_IE_CH7OVF_Msk);
ADCx->IE |= (((initStruct->OVF_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6OVF_Pos) |
(((initStruct->OVF_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7OVF_Pos);
ADCx->IE &= ~(ADC_IE_CH0HFULL_Msk | ADC_IE_CH1HFULL_Msk | ADC_IE_CH2HFULL_Msk | ADC_IE_CH3HFULL_Msk |
ADC_IE_CH4HFULL_Msk | ADC_IE_CH5HFULL_Msk | ADC_IE_CH6HFULL_Msk | ADC_IE_CH7HFULL_Msk);
ADCx->IE |= (((initStruct->HFULL_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6HFULL_Pos) |
(((initStruct->HFULL_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7HFULL_Pos);
ADCx->IE &= ~(uint32_t)(ADC_IE_CH0FULL_Msk | ADC_IE_CH1FULL_Msk | ADC_IE_CH2FULL_Msk | ADC_IE_CH3FULL_Msk |
ADC_IE_CH4FULL_Msk | ADC_IE_CH5FULL_Msk | ADC_IE_CH6FULL_Msk | ADC_IE_CH7FULL_Msk);
ADCx->IE |= (((initStruct->FULL_IEn & ADC_CH0) ? 1 : 0) << ADC_IE_CH0FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH1) ? 1 : 0) << ADC_IE_CH1FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH2) ? 1 : 0) << ADC_IE_CH2FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH3) ? 1 : 0) << ADC_IE_CH3FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH4) ? 1 : 0) << ADC_IE_CH4FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH5) ? 1 : 0) << ADC_IE_CH5FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH6) ? 1 : 0) << ADC_IE_CH6FULL_Pos) |
(((initStruct->FULL_IEn & ADC_CH7) ? 1 : 0) << ADC_IE_CH7FULL_Pos);
switch ((uint32_t)ADCx)
{
case ((uint32_t)ADC0):
if (initStruct->EOC_IEn | initStruct->OVF_IEn | initStruct->HFULL_IEn | initStruct->FULL_IEn)
{
NVIC_EnableIRQ(ADC0_IRQn);
}
else
{
NVIC_DisableIRQ(ADC0_IRQn);
}
break;
case ((uint32_t)ADC1):
if (initStruct->EOC_IEn | initStruct->OVF_IEn | initStruct->HFULL_IEn | initStruct->FULL_IEn)
{
NVIC_EnableIRQ(ADC1_IRQn);
}
else
{
NVIC_DisableIRQ(ADC1_IRQn);
}
break;
}
}
/******************************************************************************************************************************************
@ -159,9 +159,9 @@ void ADC_Init(ADC_TypeDef * ADCx, ADC_InitStructure * initStruct)
* :
* :
******************************************************************************************************************************************/
void ADC_Open(ADC_TypeDef * ADCx)
void ADC_Open(ADC_TypeDef *ADCx)
{
ADCx->CTRL |= (0x01 << ADC_CTRL_EN_Pos);
ADCx->CTRL |= (0x01 << ADC_CTRL_EN_Pos);
}
/******************************************************************************************************************************************
@ -171,9 +171,9 @@ void ADC_Open(ADC_TypeDef * ADCx)
* :
* :
******************************************************************************************************************************************/
void ADC_Close(ADC_TypeDef * ADCx)
void ADC_Close(ADC_TypeDef *ADCx)
{
ADCx->CTRL &= ~(0x01 << ADC_CTRL_EN_Pos);
ADCx->CTRL &= ~(0x01 << ADC_CTRL_EN_Pos);
}
/******************************************************************************************************************************************
@ -183,9 +183,9 @@ void ADC_Close(ADC_TypeDef * ADCx)
* :
* :
******************************************************************************************************************************************/
void ADC_Start(ADC_TypeDef * ADCx)
void ADC_Start(ADC_TypeDef *ADCx)
{
ADCx->START |= (0x01 << ADC_START_GO_Pos);
ADCx->START |= (0x01 << ADC_START_GO_Pos);
}
/******************************************************************************************************************************************
@ -195,28 +195,44 @@ void ADC_Start(ADC_TypeDef * ADCx)
* :
* :
******************************************************************************************************************************************/
void ADC_Stop(ADC_TypeDef * ADCx)
{
ADCx->START &= ~(0x01 << ADC_START_GO_Pos);
void ADC_Stop(ADC_TypeDef *ADCx)
{
ADCx->START &= ~(0x01 << ADC_START_GO_Pos);
}
static uint32_t chn2idx(uint32_t chn)
{
uint32_t idx = 0;
switch(chn)
{
case 0x01: idx = 0; break;
case 0x02: idx = 1; break;
case 0x04: idx = 2; break;
case 0x08: idx = 3; break;
case 0x10: idx = 4; break;
case 0x20: idx = 5; break;
case 0x40: idx = 6; break;
case 0x80: idx = 7; break;
}
return idx;
uint32_t idx = 0;
switch (chn)
{
case 0x01:
idx = 0;
break;
case 0x02:
idx = 1;
break;
case 0x04:
idx = 2;
break;
case 0x08:
idx = 3;
break;
case 0x10:
idx = 4;
break;
case 0x20:
idx = 5;
break;
case 0x40:
idx = 6;
break;
case 0x80:
idx = 7;
break;
}
return idx;
}
/******************************************************************************************************************************************
@ -227,16 +243,16 @@ static uint32_t chn2idx(uint32_t chn)
* : uint32_t
* :
******************************************************************************************************************************************/
uint32_t ADC_Read(ADC_TypeDef * ADCx, uint32_t chn)
uint32_t ADC_Read(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t dat = 0;
uint32_t idx = chn2idx(chn);
dat = ADCx->CH[idx].DATA;
ADCx->CH[idx].STAT = 0x01; //清除EOC标志
return dat;
uint32_t dat = 0;
uint32_t idx = chn2idx(chn);
dat = ADCx->CH[idx].DATA;
ADCx->CH[idx].STAT = 0x01; //清除EOC标志
return dat;
}
/******************************************************************************************************************************************
@ -247,11 +263,11 @@ uint32_t ADC_Read(ADC_TypeDef * ADCx, uint32_t chn)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t ADC_IsEOC(ADC_TypeDef * ADCx, uint32_t chn)
uint32_t ADC_IsEOC(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
return (ADCx->CH[idx].STAT & ADC_STAT_EOC_Msk) ? 1 : 0;
uint32_t idx = chn2idx(chn);
return (ADCx->CH[idx].STAT & ADC_STAT_EOC_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -262,13 +278,12 @@ uint32_t ADC_IsEOC(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_ChnSelect(ADC_TypeDef * ADCx, uint32_t chns)
void ADC_ChnSelect(ADC_TypeDef *ADCx, uint32_t chns)
{
ADCx->CTRL &= ~(0xFF << ADC_CTRL_CH0_Pos);
ADCx->CTRL |= (chns << ADC_CTRL_CH0_Pos);
ADCx->CTRL &= ~(0xFF << ADC_CTRL_CH0_Pos);
ADCx->CTRL |= (chns << ADC_CTRL_CH0_Pos);
}
/******************************************************************************************************************************************
* : ADC_IntEOCEn()
* : 使
@ -277,11 +292,11 @@ void ADC_ChnSelect(ADC_TypeDef * ADCx, uint32_t chns)
* :
* :
******************************************************************************************************************************************/
void ADC_IntEOCEn(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntEOCEn(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx*4));
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx * 4));
}
/******************************************************************************************************************************************
@ -292,11 +307,11 @@ void ADC_IntEOCEn(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntEOCDis(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntEOCDis(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx*4));
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx * 4));
}
/******************************************************************************************************************************************
@ -307,11 +322,11 @@ void ADC_IntEOCDis(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntEOCClr(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntEOCClr(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx*4));
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx * 4));
}
/******************************************************************************************************************************************
@ -322,11 +337,11 @@ void ADC_IntEOCClr(ADC_TypeDef * ADCx, uint32_t chn)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t ADC_IntEOCStat(ADC_TypeDef * ADCx, uint32_t chn)
uint32_t ADC_IntEOCStat(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx*4))) ? 1 : 0;
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx * 4))) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -337,11 +352,11 @@ uint32_t ADC_IntEOCStat(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntOVFEn(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntOVFEn(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx*4+1));
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx * 4 + 1));
}
/******************************************************************************************************************************************
@ -352,11 +367,11 @@ void ADC_IntOVFEn(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntOVFDis(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntOVFDis(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx*4+1));
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx * 4 + 1));
}
/******************************************************************************************************************************************
@ -367,11 +382,11 @@ void ADC_IntOVFDis(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntOVFClr(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntOVFClr(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx*4+1));
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx * 4 + 1));
}
/******************************************************************************************************************************************
@ -382,11 +397,11 @@ void ADC_IntOVFClr(ADC_TypeDef * ADCx, uint32_t chn)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t ADC_IntOVFStat(ADC_TypeDef * ADCx, uint32_t chn)
uint32_t ADC_IntOVFStat(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx*4+1))) ? 1 : 0;
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx * 4 + 1))) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -397,11 +412,11 @@ uint32_t ADC_IntOVFStat(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntHFULLEn(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntHFULLEn(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx*4+2));
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx * 4 + 2));
}
/******************************************************************************************************************************************
@ -412,11 +427,11 @@ void ADC_IntHFULLEn(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntHFULLDis(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntHFULLDis(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx*4+2));
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx * 4 + 2));
}
/******************************************************************************************************************************************
@ -427,11 +442,11 @@ void ADC_IntHFULLDis(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntHFULLClr(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntHFULLClr(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx*4+2));
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx * 4 + 2));
}
/******************************************************************************************************************************************
@ -442,11 +457,11 @@ void ADC_IntHFULLClr(ADC_TypeDef * ADCx, uint32_t chn)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t ADC_IntHFULLStat(ADC_TypeDef * ADCx, uint32_t chn)
uint32_t ADC_IntHFULLStat(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx*4+2))) ? 1 : 0;
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx * 4 + 2))) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -457,11 +472,11 @@ uint32_t ADC_IntHFULLStat(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntFULLEn(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntFULLEn(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx*4+3));
uint32_t idx = chn2idx(chn);
ADCx->IE |= (0x01 << (idx * 4 + 3));
}
/******************************************************************************************************************************************
@ -472,11 +487,11 @@ void ADC_IntFULLEn(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntFULLDis(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntFULLDis(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx*4+3));
uint32_t idx = chn2idx(chn);
ADCx->IE &= ~(0x01 << (idx * 4 + 3));
}
/******************************************************************************************************************************************
@ -487,11 +502,11 @@ void ADC_IntFULLDis(ADC_TypeDef * ADCx, uint32_t chn)
* :
* :
******************************************************************************************************************************************/
void ADC_IntFULLClr(ADC_TypeDef * ADCx, uint32_t chn)
void ADC_IntFULLClr(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx*4+3));
uint32_t idx = chn2idx(chn);
ADCx->IF = (0x01 << (idx * 4 + 3));
}
/******************************************************************************************************************************************
@ -502,9 +517,9 @@ void ADC_IntFULLClr(ADC_TypeDef * ADCx, uint32_t chn)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t ADC_IntFULLStat(ADC_TypeDef * ADCx, uint32_t chn)
uint32_t ADC_IntFULLStat(ADC_TypeDef *ADCx, uint32_t chn)
{
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx*4+3))) ? 1 : 0;
uint32_t idx = chn2idx(chn);
return (ADCx->IF & (0x01 << (idx * 4 + 3))) ? 1 : 0;
}

View File

@ -1,79 +1,77 @@
#ifndef __SWM320_ADC_H__
#define __SWM320_ADC_H__
#define __SWM320_ADC_H__
typedef struct {
uint8_t clk_src; //ADC转换时钟源ADC_CLKSRC_HRC、ADC_CLKSRC_VCO_DIV16、ADC_CLKSRC_VCO_DIV32、ADC_CLKSRC_VCO_DIV32
uint8_t clk_div; //ADC转换时钟分频取值1--31
uint8_t pga_ref; //PGA基准PGA_REF_INTERNAL、PGA_REF_EXTERNAL
uint8_t channels; //ADC转换通道选中ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t samplAvg; //采样取平均触发启动ADC转换后ADC在一个通道上连续采样、转换多次并将它们的平均值作为该通道转换结果
uint8_t trig_src; //ADC触发方式ADC_TRIGSRC_SW、ADC_TRIGSRC_PWM、ADC_TRIGSRC_TIMR2、ADC_TRIGSRC_TIMR3
uint8_t Continue; //在软件触发模式下1 连续转换模式启动后一直采样、转换直到软件清除START位
// 0 单次转换模式转换完成后START位自动清除停止转换
uint8_t EOC_IEn; //EOC中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t OVF_IEn; //OVF中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t HFULL_IEn; //FIFO半满中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t FULL_IEn; //FIFO 满中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
typedef struct
{
uint8_t clk_src; //ADC转换时钟源ADC_CLKSRC_HRC、ADC_CLKSRC_VCO_DIV16、ADC_CLKSRC_VCO_DIV32、ADC_CLKSRC_VCO_DIV32
uint8_t clk_div; //ADC转换时钟分频取值1--31
uint8_t pga_ref; //PGA基准PGA_REF_INTERNAL、PGA_REF_EXTERNAL
uint8_t channels; //ADC转换通道选中ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t samplAvg; //采样取平均触发启动ADC转换后ADC在一个通道上连续采样、转换多次并将它们的平均值作为该通道转换结果
uint8_t trig_src; //ADC触发方式ADC_TRIGSRC_SW、ADC_TRIGSRC_PWM、ADC_TRIGSRC_TIMR2、ADC_TRIGSRC_TIMR3
uint8_t Continue; //在软件触发模式下1 连续转换模式启动后一直采样、转换直到软件清除START位
// 0 单次转换模式转换完成后START位自动清除停止转换
uint8_t EOC_IEn; //EOC中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t OVF_IEn; //OVF中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t HFULL_IEn; //FIFO半满中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
uint8_t FULL_IEn; //FIFO 满中断使能可针对每个通道设置其有效值为ADC_CH0、ADC_CH1、... ... 、ADC_CH7及其组合即“按位或”运算
} ADC_InitStructure;
#define ADC_CH0 0x01
#define ADC_CH1 0x02
#define ADC_CH2 0x04
#define ADC_CH3 0x08
#define ADC_CH4 0x10
#define ADC_CH5 0x20
#define ADC_CH6 0x40
#define ADC_CH7 0x80
#define ADC_CH0 0x01
#define ADC_CH1 0x02
#define ADC_CH2 0x04
#define ADC_CH3 0x08
#define ADC_CH4 0x10
#define ADC_CH5 0x20
#define ADC_CH6 0x40
#define ADC_CH7 0x80
#define ADC_CLKSRC_HRC 1
#define ADC_CLKSRC_VCO_DIV16 2
#define ADC_CLKSRC_VCO_DIV32 3
#define ADC_CLKSRC_VCO_DIV64 4
#define ADC_CLKSRC_HRC 1
#define ADC_CLKSRC_VCO_DIV16 2
#define ADC_CLKSRC_VCO_DIV32 3
#define ADC_CLKSRC_VCO_DIV64 4
#define ADC_AVG_SAMPLE1 0
#define ADC_AVG_SAMPLE2 1 //一次启动连续采样、转换2次并计算两次结果的平均值作为转换结果
#define ADC_AVG_SAMPLE4 3
#define ADC_AVG_SAMPLE8 7
#define ADC_AVG_SAMPLE16 15
#define ADC_AVG_SAMPLE1 0
#define ADC_AVG_SAMPLE2 1 //一次启动连续采样、转换2次并计算两次结果的平均值作为转换结果
#define ADC_AVG_SAMPLE4 3
#define ADC_AVG_SAMPLE8 7
#define ADC_AVG_SAMPLE16 15
#define ADC_TRIGSRC_SW 0 //软件触发即ADC->START.GO写1启动转换
#define ADC_TRIGSRC_PWM 1
#define ADC_TRIGSRC_SW 0 //软件触发即ADC->START.GO写1启动转换
#define ADC_TRIGSRC_PWM 1
#define PGA_REF_INTERNAL 1 //PGA输入共模电平由内部电路产生ADC_REFP和ADC_REFN可悬空
#define PGA_REF_EXTERNAL 0 //PGA输入共模电平由外部引脚提供(ADC_REFP + ADC_REFN) 电平值须与量程相同
#define PGA_REF_INTERNAL 1 //PGA输入共模电平由内部电路产生ADC_REFP和ADC_REFN可悬空
#define PGA_REF_EXTERNAL 0 //PGA输入共模电平由外部引脚提供(ADC_REFP + ADC_REFN) 电平值须与量程相同
void ADC_Init(ADC_TypeDef *ADCx, ADC_InitStructure *initStruct); //ADC模数转换器初始化
void ADC_Open(ADC_TypeDef *ADCx); //ADC开启可以软件启动、或硬件触发ADC转换
void ADC_Close(ADC_TypeDef *ADCx); //ADC关闭无法软件启动、或硬件触发ADC转换
void ADC_Start(ADC_TypeDef *ADCx); //启动指定ADC开始模数转换
void ADC_Stop(ADC_TypeDef *ADCx); //关闭指定ADC停止模数转换
void ADC_Init(ADC_TypeDef * ADCx, ADC_InitStructure * initStruct); //ADC模数转换器初始化
void ADC_Open(ADC_TypeDef * ADCx); //ADC开启可以软件启动、或硬件触发ADC转换
void ADC_Close(ADC_TypeDef * ADCx); //ADC关闭无法软件启动、或硬件触发ADC转换
void ADC_Start(ADC_TypeDef * ADCx); //启动指定ADC开始模数转换
void ADC_Stop(ADC_TypeDef * ADCx); //关闭指定ADC停止模数转换
uint32_t ADC_Read(ADC_TypeDef *ADCx, uint32_t chn); //从指定通道读取转换结果
uint32_t ADC_IsEOC(ADC_TypeDef *ADCx, uint32_t chn); //指定通道是否End Of Conversion
uint32_t ADC_Read(ADC_TypeDef * ADCx, uint32_t chn); //从指定通道读取转换结果
uint32_t ADC_IsEOC(ADC_TypeDef * ADCx, uint32_t chn); //指定通道是否End Of Conversion
void ADC_ChnSelect(ADC_TypeDef *ADCx, uint32_t chns);
void ADC_ChnSelect(ADC_TypeDef * ADCx, uint32_t chns);
void ADC_IntEOCEn(ADC_TypeDef *ADCx, uint32_t chn); //转换完成中断使能
void ADC_IntEOCDis(ADC_TypeDef *ADCx, uint32_t chn); //转换完成中断禁止
void ADC_IntEOCClr(ADC_TypeDef *ADCx, uint32_t chn); //转换完成中断标志清除
uint32_t ADC_IntEOCStat(ADC_TypeDef *ADCx, uint32_t chn); //转换完成中断状态
void ADC_IntOVFEn(ADC_TypeDef *ADCx, uint32_t chn); //数据溢出中断使能
void ADC_IntOVFDis(ADC_TypeDef *ADCx, uint32_t chn); //数据溢出中断禁止
void ADC_IntOVFClr(ADC_TypeDef *ADCx, uint32_t chn); //数据溢出中断标志清除
uint32_t ADC_IntOVFStat(ADC_TypeDef *ADCx, uint32_t chn); //数据溢出中断状态
void ADC_IntEOCEn(ADC_TypeDef * ADCx, uint32_t chn); //转换完成中断使能
void ADC_IntEOCDis(ADC_TypeDef * ADCx, uint32_t chn); //转换完成中断禁止
void ADC_IntEOCClr(ADC_TypeDef * ADCx, uint32_t chn); //转换完成中断标志清除
uint32_t ADC_IntEOCStat(ADC_TypeDef * ADCx, uint32_t chn); //转换完成中断状态
void ADC_IntOVFEn(ADC_TypeDef * ADCx, uint32_t chn); //数据溢出中断使能
void ADC_IntOVFDis(ADC_TypeDef * ADCx, uint32_t chn); //数据溢出中断禁止
void ADC_IntOVFClr(ADC_TypeDef * ADCx, uint32_t chn); //数据溢出中断标志清除
uint32_t ADC_IntOVFStat(ADC_TypeDef * ADCx, uint32_t chn); //数据溢出中断状态
void ADC_IntHFULLEn(ADC_TypeDef * ADCx, uint32_t chn); //FIFO半满中断使能
void ADC_IntHFULLDis(ADC_TypeDef * ADCx, uint32_t chn); //FIFO半满中断禁止
void ADC_IntHFULLClr(ADC_TypeDef * ADCx, uint32_t chn); //FIFO半满中断标志清除
uint32_t ADC_IntHFULLStat(ADC_TypeDef * ADCx, uint32_t chn);//FIFO半满中断状态
void ADC_IntFULLEn(ADC_TypeDef * ADCx, uint32_t chn); //FIFO满中断使能
void ADC_IntFULLDis(ADC_TypeDef * ADCx, uint32_t chn); //FIFO满中断禁止
void ADC_IntFULLClr(ADC_TypeDef * ADCx, uint32_t chn); //FIFO满中断标志清除
uint32_t ADC_IntFULLStat(ADC_TypeDef * ADCx, uint32_t chn); //FIFO满中断状态
void ADC_IntHFULLEn(ADC_TypeDef *ADCx, uint32_t chn); //FIFO半满中断使能
void ADC_IntHFULLDis(ADC_TypeDef *ADCx, uint32_t chn); //FIFO半满中断禁止
void ADC_IntHFULLClr(ADC_TypeDef *ADCx, uint32_t chn); //FIFO半满中断标志清除
uint32_t ADC_IntHFULLStat(ADC_TypeDef *ADCx, uint32_t chn); //FIFO半满中断状态
void ADC_IntFULLEn(ADC_TypeDef *ADCx, uint32_t chn); //FIFO满中断使能
void ADC_IntFULLDis(ADC_TypeDef *ADCx, uint32_t chn); //FIFO满中断禁止
void ADC_IntFULLClr(ADC_TypeDef *ADCx, uint32_t chn); //FIFO满中断标志清除
uint32_t ADC_IntFULLStat(ADC_TypeDef *ADCx, uint32_t chn); //FIFO满中断状态
#endif //__SWM320_ADC_H__

File diff suppressed because it is too large Load Diff

View File

@ -1,130 +1,134 @@
#ifndef __SWM320_CAN_H__
#define __SWM320_CAN_H__
#define CAN_FRAME_STD 0
#define CAN_FRAME_EXT 1
#define CAN_FRAME_STD 0
#define CAN_FRAME_EXT 1
typedef struct {
uint8_t Mode; //CAN_MODE_NORMAL、CAN_MODE_LISTEN、CAN_MODE_SELFTEST
uint8_t CAN_BS1; //CAN_BS1_1tq、CAN_BS1_2tq、... ... 、CAN_BS1_16tq
uint8_t CAN_BS2; //CAN_BS2_1tq、CAN_BS2_2tq、... ... 、CAN_BS2_8tq
uint8_t CAN_SJW; //CAN_SJW_1tq、CAN_SJW_2tq、CAN_SJW_3tq、CAN_SJW_4tq
uint32_t Baudrate; //波特率即位传输速率取值1--1000000
uint8_t FilterMode; //CAN_FILTER_16b、CAN_FILTER_32b
union {
uint32_t FilterMask32b; //FilterCheck & (~FilterMask) == ID & (~FilterMask)的Message通过过滤
struct { // 0 must match 1 don't care
uint16_t FilterMask16b1;
uint16_t FilterMask16b2;
};
};
union {
uint32_t FilterCheck32b;
struct {
uint16_t FilterCheck16b1;
uint16_t FilterCheck16b2;
};
};
uint8_t RXNotEmptyIEn; //接收FIFO非空有数据可读
uint8_t RXOverflowIEn; //接收FIFO溢出有数据丢失
uint8_t ArbitrLostIEn; //控制器丢失仲裁变成接收方
uint8_t ErrPassiveIEn; //接收/发送错误计数值达到127
typedef struct
{
uint8_t Mode; //CAN_MODE_NORMAL、CAN_MODE_LISTEN、CAN_MODE_SELFTEST
uint8_t CAN_BS1; //CAN_BS1_1tq、CAN_BS1_2tq、... ... 、CAN_BS1_16tq
uint8_t CAN_BS2; //CAN_BS2_1tq、CAN_BS2_2tq、... ... 、CAN_BS2_8tq
uint8_t CAN_SJW; //CAN_SJW_1tq、CAN_SJW_2tq、CAN_SJW_3tq、CAN_SJW_4tq
uint32_t Baudrate; //波特率即位传输速率取值1--1000000
uint8_t FilterMode; //CAN_FILTER_16b、CAN_FILTER_32b
union
{
uint32_t FilterMask32b; //FilterCheck & (~FilterMask) == ID & (~FilterMask)的Message通过过滤
struct
{ // 0 must match 1 don't care
uint16_t FilterMask16b1;
uint16_t FilterMask16b2;
};
};
union
{
uint32_t FilterCheck32b;
struct
{
uint16_t FilterCheck16b1;
uint16_t FilterCheck16b2;
};
};
uint8_t RXNotEmptyIEn; //接收FIFO非空有数据可读
uint8_t RXOverflowIEn; //接收FIFO溢出有数据丢失
uint8_t ArbitrLostIEn; //控制器丢失仲裁变成接收方
uint8_t ErrPassiveIEn; //接收/发送错误计数值达到127
} CAN_InitStructure;
#define CAN_MODE_NORMAL 0 //常规模式
#define CAN_MODE_LISTEN 1 //监听模式
#define CAN_MODE_SELFTEST 2 //自测模式
#define CAN_MODE_NORMAL 0 //常规模式
#define CAN_MODE_LISTEN 1 //监听模式
#define CAN_MODE_SELFTEST 2 //自测模式
#define CAN_BS1_1tq 0
#define CAN_BS1_2tq 1
#define CAN_BS1_3tq 2
#define CAN_BS1_4tq 3
#define CAN_BS1_5tq 4
#define CAN_BS1_6tq 5
#define CAN_BS1_7tq 6
#define CAN_BS1_8tq 7
#define CAN_BS1_9tq 8
#define CAN_BS1_10tq 9
#define CAN_BS1_11tq 10
#define CAN_BS1_12tq 11
#define CAN_BS1_13tq 12
#define CAN_BS1_14tq 13
#define CAN_BS1_15tq 14
#define CAN_BS1_16tq 15
#define CAN_BS1_1tq 0
#define CAN_BS1_2tq 1
#define CAN_BS1_3tq 2
#define CAN_BS1_4tq 3
#define CAN_BS1_5tq 4
#define CAN_BS1_6tq 5
#define CAN_BS1_7tq 6
#define CAN_BS1_8tq 7
#define CAN_BS1_9tq 8
#define CAN_BS1_10tq 9
#define CAN_BS1_11tq 10
#define CAN_BS1_12tq 11
#define CAN_BS1_13tq 12
#define CAN_BS1_14tq 13
#define CAN_BS1_15tq 14
#define CAN_BS1_16tq 15
#define CAN_BS2_1tq 0
#define CAN_BS2_2tq 1
#define CAN_BS2_3tq 2
#define CAN_BS2_4tq 3
#define CAN_BS2_5tq 4
#define CAN_BS2_6tq 5
#define CAN_BS2_7tq 6
#define CAN_BS2_8tq 7
#define CAN_BS2_1tq 0
#define CAN_BS2_2tq 1
#define CAN_BS2_3tq 2
#define CAN_BS2_4tq 3
#define CAN_BS2_5tq 4
#define CAN_BS2_6tq 5
#define CAN_BS2_7tq 6
#define CAN_BS2_8tq 7
#define CAN_SJW_1tq 0
#define CAN_SJW_2tq 1
#define CAN_SJW_3tq 2
#define CAN_SJW_4tq 3
#define CAN_SJW_1tq 0
#define CAN_SJW_2tq 1
#define CAN_SJW_3tq 2
#define CAN_SJW_4tq 3
#define CAN_FILTER_16b 0 //两个16位过滤器
#define CAN_FILTER_32b 1 //一个32位过滤器
#define CAN_FILTER_16b 0 //两个16位过滤器
#define CAN_FILTER_32b 1 //一个32位过滤器
typedef struct {
uint32_t id; //消息ID
uint8_t format; //帧格式CAN_FRAME_STD、CAN_FRAME_EXT
uint8_t remote; //消息是否为远程帧
uint8_t size; //接收到的数据个数
uint8_t data[8]; //接收到的数据
typedef struct
{
uint32_t id; //消息ID
uint8_t format; //帧格式CAN_FRAME_STD、CAN_FRAME_EXT
uint8_t remote; //消息是否为远程帧
uint8_t size; //接收到的数据个数
uint8_t data[8]; //接收到的数据
} CAN_RXMessage;
void CAN_Init(CAN_TypeDef *CANx, CAN_InitStructure *initStruct);
void CAN_Open(CAN_TypeDef *CANx);
void CAN_Close(CAN_TypeDef *CANx);
void CAN_Init(CAN_TypeDef * CANx, CAN_InitStructure * initStruct);
void CAN_Open(CAN_TypeDef * CANx);
void CAN_Close(CAN_TypeDef * CANx);
void CAN_Transmit(CAN_TypeDef *CANx, uint32_t format, uint32_t id, uint8_t data[], uint32_t size, uint32_t once);
void CAN_TransmitRequest(CAN_TypeDef *CANx, uint32_t format, uint32_t id, uint32_t once);
void CAN_Receive(CAN_TypeDef *CANx, CAN_RXMessage *msg);
void CAN_Transmit(CAN_TypeDef * CANx, uint32_t format, uint32_t id, uint8_t data[], uint32_t size, uint32_t once);
void CAN_TransmitRequest(CAN_TypeDef * CANx, uint32_t format, uint32_t id, uint32_t once);
void CAN_Receive(CAN_TypeDef * CANx, CAN_RXMessage *msg);
uint32_t CAN_TXComplete(CAN_TypeDef *CANx);
uint32_t CAN_TXSuccess(CAN_TypeDef *CANx);
uint32_t CAN_TXComplete(CAN_TypeDef * CANx);
uint32_t CAN_TXSuccess(CAN_TypeDef * CANx);
void CAN_AbortTransmit(CAN_TypeDef *CANx);
void CAN_AbortTransmit(CAN_TypeDef * CANx);
uint32_t CAN_TXBufferReady(CAN_TypeDef *CANx);
uint32_t CAN_RXDataAvailable(CAN_TypeDef *CANx);
uint32_t CAN_TXBufferReady(CAN_TypeDef * CANx);
uint32_t CAN_RXDataAvailable(CAN_TypeDef * CANx);
void CAN_SetBaudrate(CAN_TypeDef *CANx, uint32_t baudrate, uint32_t CAN_BS1, uint32_t CAN_BS2, uint32_t CAN_SJW);
void CAN_SetBaudrate(CAN_TypeDef * CANx, uint32_t baudrate, uint32_t CAN_BS1, uint32_t CAN_BS2, uint32_t CAN_SJW);
void CAN_SetFilter32b(CAN_TypeDef *CANx, uint32_t check, uint32_t mask);
void CAN_SetFilter16b(CAN_TypeDef *CANx, uint16_t check1, uint16_t mask1, uint16_t check2, uint16_t mask2);
void CAN_SetFilter32b(CAN_TypeDef * CANx, uint32_t check, uint32_t mask);
void CAN_SetFilter16b(CAN_TypeDef * CANx, uint16_t check1, uint16_t mask1, uint16_t check2, uint16_t mask2);
void CAN_INTRXNotEmptyEn(CAN_TypeDef *CANx);
void CAN_INTRXNotEmptyDis(CAN_TypeDef *CANx);
void CAN_INTTXBufEmptyEn(CAN_TypeDef *CANx);
void CAN_INTTXBufEmptyDis(CAN_TypeDef *CANx);
void CAN_INTRXNotEmptyEn(CAN_TypeDef * CANx);
void CAN_INTRXNotEmptyDis(CAN_TypeDef * CANx);
void CAN_INTErrWarningEn(CAN_TypeDef *CANx);
void CAN_INTErrWarningDis(CAN_TypeDef *CANx);
void CAN_INTTXBufEmptyEn(CAN_TypeDef * CANx);
void CAN_INTTXBufEmptyDis(CAN_TypeDef * CANx);
void CAN_INTRXOverflowEn(CAN_TypeDef *CANx);
void CAN_INTRXOverflowDis(CAN_TypeDef *CANx);
void CAN_INTRXOverflowClear(CAN_TypeDef *CANx);
void CAN_INTErrWarningEn(CAN_TypeDef * CANx);
void CAN_INTErrWarningDis(CAN_TypeDef * CANx);
void CAN_INTWakeupEn(CAN_TypeDef *CANx);
void CAN_INTWakeupDis(CAN_TypeDef *CANx);
void CAN_INTRXOverflowEn(CAN_TypeDef * CANx);
void CAN_INTRXOverflowDis(CAN_TypeDef * CANx);
void CAN_INTRXOverflowClear(CAN_TypeDef * CANx);
void CAN_INTErrPassiveEn(CAN_TypeDef *CANx);
void CAN_INTErrPassiveDis(CAN_TypeDef *CANx);
void CAN_INTWakeupEn(CAN_TypeDef * CANx);
void CAN_INTWakeupDis(CAN_TypeDef * CANx);
void CAN_INTArbitrLostEn(CAN_TypeDef *CANx);
void CAN_INTArbitrLostDis(CAN_TypeDef *CANx);
void CAN_INTErrPassiveEn(CAN_TypeDef * CANx);
void CAN_INTErrPassiveDis(CAN_TypeDef * CANx);
void CAN_INTBusErrorEn(CAN_TypeDef *CANx);
void CAN_INTBusErrorDis(CAN_TypeDef *CANx);
void CAN_INTArbitrLostEn(CAN_TypeDef * CANx);
void CAN_INTArbitrLostDis(CAN_TypeDef * CANx);
void CAN_INTBusErrorEn(CAN_TypeDef * CANx);
void CAN_INTBusErrorDis(CAN_TypeDef * CANx);
uint32_t CAN_INTStat(CAN_TypeDef * CANx);
uint32_t CAN_INTStat(CAN_TypeDef *CANx);
#endif //__SWM320_CAN_H__

View File

@ -1,10 +1,10 @@
/******************************************************************************************************************************************
* : SWM320_crc.c
* : SWM320单片机的CRC模块驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
* : SWM320_crc.c
* : SWM320单片机的CRC模块驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
*
*
*******************************************************************************************************************************************
@ -21,31 +21,30 @@
#include "SWM320.h"
#include "SWM320_crc.h"
/******************************************************************************************************************************************
* : CRC_Init()
* : CRC
* : CRC_TypeDef * CRCx CRC接口CRC
* uint32_t mode CRC32_IN32CRC32_IN16CRC32_IN8CRC16_IN16CRC16_IN8
* uint32_t out_not
* uint32_t out_rev
* uint32_t ini_val CRC初始值
* :
* :
* : CRC_Init()
* : CRC
* : CRC_TypeDef * CRCx CRC接口CRC
* uint32_t mode CRC32_IN32CRC32_IN16CRC32_IN8CRC16_IN16CRC16_IN8
* uint32_t out_not
* uint32_t out_rev
* uint32_t ini_val CRC初始值
* :
* :
******************************************************************************************************************************************/
void CRC_Init(CRC_TypeDef * CRCx, uint32_t mode, uint32_t out_not, uint32_t out_rev, uint32_t ini_val)
void CRC_Init(CRC_TypeDef *CRCx, uint32_t mode, uint32_t out_not, uint32_t out_rev, uint32_t ini_val)
{
switch((uint32_t)CRCx)
{
case ((uint32_t)CRC):
SYS->CLKEN |= (0x01 << SYS_CLKEN_CRC_Pos);
break;
}
CRCx->CR = (1 << CRC_CR_EN_Pos) |
(mode << CRC_CR_CRC16_Pos) |
(out_not << CRC_CR_ONOT_Pos) |
(out_rev << CRC_CR_OREV_Pos);
CRCx->INIVAL = ini_val;
switch ((uint32_t)CRCx)
{
case ((uint32_t)CRC):
SYS->CLKEN |= (0x01 << SYS_CLKEN_CRC_Pos);
break;
}
CRCx->CR = (1 << CRC_CR_EN_Pos) |
(mode << CRC_CR_CRC16_Pos) |
(out_not << CRC_CR_ONOT_Pos) |
(out_rev << CRC_CR_OREV_Pos);
CRCx->INIVAL = ini_val;
}

View File

@ -1,16 +1,13 @@
#ifndef __SWM320_CRC_H__
#define __SWM320_CRC_H__
#define CRC32_IN32 0 //CRC32算法输入数据32位
#define CRC32_IN16 2 //CRC32算法输入数据16位
#define CRC32_IN8 4 //CRC32算法输入数据 8位
#define CRC16_IN16 3 //CRC16算法输入数据16位
#define CRC16_IN8 5 //CRC16算法输入数据 8位
#define CRC32_IN32 0 //CRC32算法输入数据32位
#define CRC32_IN16 2 //CRC32算法输入数据16位
#define CRC32_IN8 4 //CRC32算法输入数据 8位
#define CRC16_IN16 3 //CRC16算法输入数据16位
#define CRC16_IN8 5 //CRC16算法输入数据 8位
void CRC_Init(CRC_TypeDef * CRCx, uint32_t mode, uint32_t out_not, uint32_t out_rev, uint32_t ini_val);
void CRC_Init(CRC_TypeDef *CRCx, uint32_t mode, uint32_t out_not, uint32_t out_rev, uint32_t ini_val);
/******************************************************************************************************************************************
* : CRC_Write()
@ -21,7 +18,7 @@ void CRC_Init(CRC_TypeDef * CRCx, uint32_t mode, uint32_t out_not, uint32_t out_
******************************************************************************************************************************************/
static __INLINE void CRC_Write(uint32_t data)
{
CRC->DATAIN = data;
CRC->DATAIN = data;
}
/******************************************************************************************************************************************
@ -33,7 +30,7 @@ static __INLINE void CRC_Write(uint32_t data)
******************************************************************************************************************************************/
static __INLINE uint32_t CRC_Result(void)
{
return CRC->RESULT;
return CRC->RESULT;
}
#endif //__SWM320_CRC_H__

View File

@ -1,10 +1,10 @@
/******************************************************************************************************************************************
* : SWM320_dma.c
* : SWM320单片机的DMA功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
* : SWM320_dma.c
* : SWM320单片机的DMA功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
*
*
*******************************************************************************************************************************************
@ -22,117 +22,119 @@
#include "SWM320_dma.h"
/******************************************************************************************************************************************
* : DMA_CHM_Config()
* : DMA通道配置Flash和RAM间
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* uint32_t src_addr 200
* uint32_t src_addr_incr 0 1
* uint32_t dst_addr 200
* uint32_t dst_addr_incr 0 1
* uint32_t num_word 1024
* uint32_t int_en 使1 0
* :
* :
* : DMA_CHM_Config()
* : DMA通道配置Flash和RAM间
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* uint32_t src_addr 200
* uint32_t src_addr_incr 0 1
* uint32_t dst_addr 200
* uint32_t dst_addr_incr 0 1
* uint32_t num_word 1024
* uint32_t int_en 使1 0
* :
* :
******************************************************************************************************************************************/
void DMA_CHM_Config(uint32_t chn, uint32_t src_addr, uint32_t src_addr_incr, uint32_t dst_addr, uint32_t dst_addr_incr, uint32_t num_word, uint32_t int_en)
{
DMA->EN = 1; //每个通道都有自己独立的开关控制,所以总开关可以是一直开启的
DMA_CH_Close(chn); //配置前先关闭该通道
DMA->CH[chn].SRC = src_addr;
DMA->CH[chn].DST = dst_addr;
DMA->CH[chn].CR = ((num_word*4-1) << DMA_CR_LEN_Pos) |
(0 << DMA_CR_AUTORE_Pos);
DMA->CH[chn].AM = (src_addr_incr << DMA_AM_SRCAM_Pos) |
(dst_addr_incr << DMA_AM_DSTAM_Pos) |
(0 << DMA_AM_BURST_Pos);
DMA->IF = (1 << chn); //清除中断标志
DMA->IE |= (1 << chn);
if(int_en) DMA->IM &= ~(1 << chn);
else DMA->IM |= (1 << chn);
if(int_en)
{
NVIC_EnableIRQ(DMA_IRQn);
}
else
{
//不能调用NVIC_DisalbeIRQ(DMA_IRQn)因为其他通道可能使用DMA中断
}
DMA->EN = 1; //每个通道都有自己独立的开关控制,所以总开关可以是一直开启的
DMA_CH_Close(chn); //配置前先关闭该通道
DMA->CH[chn].SRC = src_addr;
DMA->CH[chn].DST = dst_addr;
DMA->CH[chn].CR = ((num_word * 4 - 1) << DMA_CR_LEN_Pos) |
(0 << DMA_CR_AUTORE_Pos);
DMA->CH[chn].AM = (src_addr_incr << DMA_AM_SRCAM_Pos) |
(dst_addr_incr << DMA_AM_DSTAM_Pos) |
(0 << DMA_AM_BURST_Pos);
DMA->IF = (1 << chn); //清除中断标志
DMA->IE |= (1 << chn);
if (int_en)
DMA->IM &= ~(1 << chn);
else
DMA->IM |= (1 << chn);
if (int_en)
{
NVIC_EnableIRQ(DMA_IRQn);
}
else
{
//不能调用NVIC_DisalbeIRQ(DMA_IRQn)因为其他通道可能使用DMA中断
}
}
/******************************************************************************************************************************************
* : DMA_CH_Open()
* : DMA通道打开
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
* : DMA_CH_Open()
* : DMA通道打开
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
******************************************************************************************************************************************/
void DMA_CH_Open(uint32_t chn)
{
DMA->CH[chn].CR |= (1 << DMA_CR_TXEN_Pos);
DMA->CH[chn].CR |= (1 << DMA_CR_TXEN_Pos);
}
/******************************************************************************************************************************************
* : DMA_CH_Close()
* : DMA通道关闭
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
* : DMA_CH_Close()
* : DMA通道关闭
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
******************************************************************************************************************************************/
void DMA_CH_Close(uint32_t chn)
{
DMA->CH[chn].CR &= ~(1 << DMA_CR_TXEN_Pos);
DMA->CH[chn].CR &= ~(1 << DMA_CR_TXEN_Pos);
}
/******************************************************************************************************************************************
* : DMA_CH_INTEn()
* : DMA中断使能
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
* : DMA_CH_INTEn()
* : DMA中断使能
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
******************************************************************************************************************************************/
void DMA_CH_INTEn(uint32_t chn)
{
DMA->IM &= ~(1 << chn);
DMA->IM &= ~(1 << chn);
}
/******************************************************************************************************************************************
* : DMA_CH_INTDis()
* : DMA中断禁止
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
* : DMA_CH_INTDis()
* : DMA中断禁止
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
******************************************************************************************************************************************/
void DMA_CH_INTDis(uint32_t chn)
{
DMA->IM |= (1 << chn);
DMA->IM |= (1 << chn);
}
/******************************************************************************************************************************************
* : DMA_CH_INTClr()
* : DMA中断标志清除
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
* : DMA_CH_INTClr()
* : DMA中断标志清除
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* :
* :
******************************************************************************************************************************************/
void DMA_CH_INTClr(uint32_t chn)
{
DMA->IF = (1 << chn);
DMA->IF = (1 << chn);
}
/******************************************************************************************************************************************
* : DMA_CH_INTStat()
* : DMA中断状态查询
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* : uint32_t 1 0
* :
* : DMA_CH_INTStat()
* : DMA中断状态查询
* : uint32_t chn DMA_CH0DMA_CH1DMA_CH2
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t DMA_CH_INTStat(uint32_t chn)
{
return (DMA->IF & (1 << chn)) ? 1 : 0;
{
return (DMA->IF & (1 << chn)) ? 1 : 0;
}

View File

@ -1,20 +1,17 @@
#ifndef __SWM320_DMA_H__
#define __SWM320_DMA_H__
#define DMA_CH0 0
#define DMA_CH1 1
#define DMA_CH2 2
#define DMA_CH0 0
#define DMA_CH1 1
#define DMA_CH2 2
void DMA_CHM_Config(uint32_t chn, uint32_t src_addr, uint32_t src_addr_incr, uint32_t dst_addr, uint32_t dst_addr_incr, uint32_t num_word, uint32_t int_en); //DMA通道配置用于存储器间如Flash和RAM间搬运数据
void DMA_CH_Open(uint32_t chn); //DMA通道打开
void DMA_CH_Close(uint32_t chn); //DMA通道关闭
void DMA_CH_INTEn(uint32_t chn); //DMA中断使能数据搬运完成后触发中断
void DMA_CH_INTDis(uint32_t chn); //DMA中断禁止数据搬运完成后不触发中断
void DMA_CH_INTClr(uint32_t chn); //DMA中断标志清除
uint32_t DMA_CH_INTStat(uint32_t chn); //DMA中断状态查询1 数据搬运完成 0 数据搬运未完成
void DMA_CHM_Config(uint32_t chn, uint32_t src_addr, uint32_t src_addr_incr, uint32_t dst_addr, uint32_t dst_addr_incr, uint32_t num_word, uint32_t int_en); //DMA通道配置用于存储器间如Flash和RAM间搬运数据
void DMA_CH_Open(uint32_t chn); //DMA通道打开
void DMA_CH_Close(uint32_t chn); //DMA通道关闭
void DMA_CH_INTEn(uint32_t chn); //DMA中断使能数据搬运完成后触发中断
void DMA_CH_INTDis(uint32_t chn); //DMA中断禁止数据搬运完成后不触发中断
void DMA_CH_INTClr(uint32_t chn); //DMA中断标志清除
uint32_t DMA_CH_INTStat(uint32_t chn); //DMA中断状态查询1 数据搬运完成 0 数据搬运未完成
#endif //__SWM320_DMA_H__

View File

@ -1,10 +1,10 @@
/******************************************************************************************************************************************
* : SWM320_exti.c
* : SWM320单片机的外部中断功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
* : SWM320_exti.c
* : SWM320单片机的外部中断功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
*
*******************************************************************************************************************************************
* @attention
@ -21,111 +21,111 @@
#include "SWM320_exti.h"
/******************************************************************************************************************************************
* : EXTI_Init()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t mode EXTI_FALL_EDGEEXTI_RISE_EDGEEXTI_BOTH_EDGEEXTI_LOW_LEVELEXTI_HIGH_LEVEL
* :
* : GPIOAGPIOBGPIOCGPIOM的PIN0--7NVIC中的引脚中断GPIOA0_IRQnNVIC的组中断GPIOA_IRQn
* NVIC_EnableIRQ()使NVIC中断NVIC_EnableIRQ(GPIOA0_IRQn)NVIC_EnableIRQ(GPIOA_IRQn)
* : EXTI_Init()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t mode EXTI_FALL_EDGEEXTI_RISE_EDGEEXTI_BOTH_EDGEEXTI_LOW_LEVELEXTI_HIGH_LEVEL
* :
* : GPIOAGPIOBGPIOCGPIOM的PIN0--7NVIC中的引脚中断GPIOA0_IRQnNVIC的组中断GPIOA_IRQn
* NVIC_EnableIRQ()使NVIC中断NVIC_EnableIRQ(GPIOA0_IRQn)NVIC_EnableIRQ(GPIOA_IRQn)
******************************************************************************************************************************************/
void EXTI_Init(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t mode)
void EXTI_Init(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t mode)
{
EXTI_Close(GPIOx, n); //配置关键寄存器前先关闭
if(mode & 0x10)
{
GPIOx->INTLVLTRG |= (0x01 << n); //电平触发
if(mode & 0x01)
GPIOx->INTRISEEN |= (0x01 << n); //高电平触发
else
GPIOx->INTRISEEN &= ~(0x01 << n); //低电平触发
}
else
{
GPIOx->INTLVLTRG &= ~(0x01 << n); //边沿触发
if(mode & 0x02)
{
GPIOx->INTBE |= (0x01 << n); //双边沿触发
}
else
{
GPIOx->INTBE &= ~(0x01 << n); //单边沿触发
if(mode & 0x01)
GPIOx->INTRISEEN |= (0x01 << n); //上升沿触发
else
GPIOx->INTRISEEN &= ~(0x01 << n); //下降沿触发
}
}
GPIOx->INTCLR = (1 << n); //清除掉因为模式配置可能产生的中断
EXTI_Close(GPIOx, n); //配置关键寄存器前先关闭
if (mode & 0x10)
{
GPIOx->INTLVLTRG |= (0x01 << n); //电平触发
if (mode & 0x01)
GPIOx->INTRISEEN |= (0x01 << n); //高电平触发
else
GPIOx->INTRISEEN &= ~(0x01 << n); //低电平触发
}
else
{
GPIOx->INTLVLTRG &= ~(0x01 << n); //边沿触发
if (mode & 0x02)
{
GPIOx->INTBE |= (0x01 << n); //双边沿触发
}
else
{
GPIOx->INTBE &= ~(0x01 << n); //单边沿触发
if (mode & 0x01)
GPIOx->INTRISEEN |= (0x01 << n); //上升沿触发
else
GPIOx->INTRISEEN &= ~(0x01 << n); //下降沿触发
}
}
GPIOx->INTCLR = (1 << n); //清除掉因为模式配置可能产生的中断
}
/******************************************************************************************************************************************
* : EXTI_Open()
* : 使
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
* : EXTI_Open()
* : 使
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
******************************************************************************************************************************************/
void EXTI_Open(GPIO_TypeDef * GPIOx, uint32_t n)
void EXTI_Open(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->INTEN |= (0x01 << n);
GPIOx->INTEN |= (0x01 << n);
}
/******************************************************************************************************************************************
* : EXTI_Close()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
* : EXTI_Close()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
******************************************************************************************************************************************/
void EXTI_Close(GPIO_TypeDef * GPIOx, uint32_t n)
void EXTI_Close(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->INTEN &= ~(0x01 << n);
GPIOx->INTEN &= ~(0x01 << n);
}
/******************************************************************************************************************************************
* : EXTI_State()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 0
* :
* : EXTI_State()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t EXTI_State(GPIO_TypeDef * GPIOx, uint32_t n)
uint32_t EXTI_State(GPIO_TypeDef *GPIOx, uint32_t n)
{
return (GPIOx->INTSTAT >> n) & 0x01;
return (GPIOx->INTSTAT >> n) & 0x01;
}
/******************************************************************************************************************************************
* : EXTI_RawState()
* : //
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 / 0 /
* :
* : EXTI_RawState()
* : //
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 / 0 /
* :
******************************************************************************************************************************************/
uint32_t EXTI_RawState(GPIO_TypeDef * GPIOx, uint32_t n)
uint32_t EXTI_RawState(GPIO_TypeDef *GPIOx, uint32_t n)
{
return (GPIOx->INTRAWSTAT >> n) & 0x01;
return (GPIOx->INTRAWSTAT >> n) & 0x01;
}
/******************************************************************************************************************************************
* : EXTI_Clear()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* : 沿
* : EXTI_Clear()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* : 沿
******************************************************************************************************************************************/
void EXTI_Clear(GPIO_TypeDef * GPIOx, uint32_t n)
void EXTI_Clear(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->INTCLR = (0x01 << n);
GPIOx->INTCLR = (0x01 << n);
}

View File

@ -1,20 +1,18 @@
#ifndef __SWM320_EXTI_H__
#define __SWM320_EXTI_H__
#define __SWM320_EXTI_H__
void EXTI_Init(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t mode); //指定引脚外部中断初始化
void EXTI_Open(GPIO_TypeDef * GPIOx, uint32_t n); //指定引脚外部中断打开(即使能)
void EXTI_Close(GPIO_TypeDef * GPIOx, uint32_t n); //指定引脚外部中断关闭(即禁能)
void EXTI_Init(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t mode); //指定引脚外部中断初始化
void EXTI_Open(GPIO_TypeDef *GPIOx, uint32_t n); //指定引脚外部中断打开(即使能)
void EXTI_Close(GPIO_TypeDef *GPIOx, uint32_t n); //指定引脚外部中断关闭(即禁能)
uint32_t EXTI_State(GPIO_TypeDef * GPIOx, uint32_t n); //指定引脚是否触发了中断
uint32_t EXTI_RawState(GPIO_TypeDef * GPIOx, uint32_t n); //指定引脚是否满足过/了中断触发条件,当此中断关闭时可通过调用此函数以查询的方式检测引脚上是否满足过/了中断触发条件
void EXTI_Clear(GPIO_TypeDef * GPIOx, uint32_t n); //指定引脚外部中断清除(即清除中断标志,以免再次进入此中断)
#define EXTI_FALL_EDGE 0x00 //下降沿触发中断
#define EXTI_RISE_EDGE 0x01 //上升沿触发中断
#define EXTI_BOTH_EDGE 0x02 //双边沿触发中断
#define EXTI_LOW_LEVEL 0x10 //低电平触发中断
#define EXTI_HIGH_LEVEL 0x11 //高电平触发中断
uint32_t EXTI_State(GPIO_TypeDef *GPIOx, uint32_t n); //指定引脚是否触发了中断
uint32_t EXTI_RawState(GPIO_TypeDef *GPIOx, uint32_t n); //指定引脚是否满足过/了中断触发条件,当此中断关闭时可通过调用此函数以查询的方式检测引脚上是否满足过/了中断触发条件
void EXTI_Clear(GPIO_TypeDef *GPIOx, uint32_t n); //指定引脚外部中断清除(即清除中断标志,以免再次进入此中断)
#define EXTI_FALL_EDGE 0x00 //下降沿触发中断
#define EXTI_RISE_EDGE 0x01 //上升沿触发中断
#define EXTI_BOTH_EDGE 0x02 //双边沿触发中断
#define EXTI_LOW_LEVEL 0x10 //低电平触发中断
#define EXTI_HIGH_LEVEL 0x11 //高电平触发中断
#endif //__SWM320_EXTI_H__

View File

@ -19,13 +19,11 @@
#include "SWM320.h"
#include "SWM320_flash.h"
IAP_Cache_Reset_t IAP_Cache_Reset = (IAP_Cache_Reset_t)0x11000601;
IAP_Flash_Param_t IAP_Flash_Param = (IAP_Flash_Param_t)0x11000681;
IAP_Flash_Erase_t IAP_Flash_Erase = (IAP_Flash_Erase_t)0x11000781;
IAP_Flash_Write_t IAP_Flash_Write = (IAP_Flash_Write_t)0x11000801;
/******************************************************************************************************************************************
* : FLASH_Erase()
* : Flash擦除
@ -34,14 +32,14 @@ IAP_Flash_Write_t IAP_Flash_Write = (IAP_Flash_Write_t)0x11000801;
* :
******************************************************************************************************************************************/
void FLASH_Erase(uint32_t addr)
{
__disable_irq();
IAP_Flash_Erase(addr / 0x1000);
IAP_Cache_Reset();
__enable_irq();
{
__disable_irq();
IAP_Flash_Erase(addr / 0x1000);
IAP_Cache_Reset();
__enable_irq();
}
/******************************************************************************************************************************************
@ -55,13 +53,13 @@ void FLASH_Erase(uint32_t addr)
******************************************************************************************************************************************/
void FLASH_Write(uint32_t addr, uint32_t buff[], uint32_t count)
{
__disable_irq();
IAP_Flash_Write(addr, (uint32_t)buff, count/4);
IAP_Cache_Reset();
__enable_irq();
__disable_irq();
IAP_Flash_Write(addr, (uint32_t)buff, count / 4);
IAP_Cache_Reset();
__enable_irq();
}
/******************************************************************************************************************************************
@ -73,25 +71,25 @@ void FLASH_Write(uint32_t addr, uint32_t buff[], uint32_t count)
******************************************************************************************************************************************/
void Flash_Param_at_xMHz(uint32_t x)
{
__disable_irq();
switch(x)
{
case 30:
IAP_Flash_Param(0x489, 0xabf41f25);
break;
case 40:
IAP_Flash_Param(0x489, 0xabf42929);
break;
case 80:
IAP_Flash_Param(0x489, 0xabf8524d);
break;
case 120:
default:
IAP_Flash_Param(0x48a, 0xabfc7a6e);
break;
}
__enable_irq();
__disable_irq();
switch (x)
{
case 30:
IAP_Flash_Param(0x489, 0xabf41f25);
break;
case 40:
IAP_Flash_Param(0x489, 0xabf42929);
break;
case 80:
IAP_Flash_Param(0x489, 0xabf8524d);
break;
case 120:
default:
IAP_Flash_Param(0x48a, 0xabfc7a6e);
break;
}
__enable_irq();
}

View File

@ -1,14 +1,11 @@
#ifndef __SWM320_FLASH_H__
#define __SWM320_FLASH_H__
void FLASH_Erase(uint32_t addr);
void FLASH_Write(uint32_t addr, uint32_t buff[], uint32_t count);
void Flash_Param_at_xMHz(uint32_t x);
typedef void (*IAP_Cache_Reset_t)(void);
typedef void (*IAP_Flash_Param_t)(uint32_t cfg0, uint32_t cfg1);
typedef void (*IAP_Flash_Erase_t)(uint32_t sector);
@ -19,5 +16,4 @@ extern IAP_Flash_Param_t IAP_Flash_Param;
extern IAP_Flash_Erase_t IAP_Flash_Erase;
extern IAP_Flash_Write_t IAP_Flash_Write;
#endif //__SWM320_FLASH_H__

View File

@ -1,10 +1,10 @@
/******************************************************************************************************************************************
* : SWM320_gpio.c
* : SWM320单片机的通用输入输出功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
* : SWM320_gpio.c
* : SWM320单片机的通用输入输出功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
*
*
*******************************************************************************************************************************************
@ -21,358 +21,357 @@
#include "SWM320.h"
#include "SWM320_gpio.h"
/******************************************************************************************************************************************
* : GPIO_Init()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t dir 0 1
* uint32_t pull_up 0 1
* uint32_t pull_down 0 1
* :
* : GPIOAGPIOCGPIOMGPIOP只有上拉GPIOBGPION只有下拉PN0PN1PN2三个引脚有上拉没下拉
* : GPIO_Init()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t dir 0 1
* uint32_t pull_up 0 1
* uint32_t pull_down 0 1
* :
* : GPIOAGPIOCGPIOMGPIOP只有上拉GPIOBGPION只有下拉PN0PN1PN2三个引脚有上拉没下拉
******************************************************************************************************************************************/
void GPIO_Init(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t dir, uint32_t pull_up, uint32_t pull_down)
void GPIO_Init(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t dir, uint32_t pull_up, uint32_t pull_down)
{
switch((uint32_t)GPIOx)
{
case ((uint32_t)GPIOA):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOA_Pos);
PORT_Init(PORTA, n, 0, 1); //PORTA.PINn引脚配置为GPIO功能数字输入开启
if(dir == 1)
{
GPIOA->DIR |= (0x01 << n);
}
else
{
GPIOA->DIR &= ~(0x01 << n);
}
if(pull_up == 1)
PORT->PORTA_PULLU |= (0x01 << n);
else
PORT->PORTA_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPIOB):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOB_Pos);
PORT_Init(PORTB, n, 0, 1); //PORTB.PINn引脚配置为GPIO功能数字输入开启
if(dir == 1)
{
GPIOB->DIR |= (0x01 << n);
}
else
{
GPIOB->DIR &= ~(0x01 << n);
}
if(pull_down == 1)
PORT->PORTB_PULLD |= (0x01 << n);
else
PORT->PORTB_PULLD &= ~(0x01 << n);
break;
switch ((uint32_t)GPIOx)
{
case ((uint32_t)GPIOA):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOA_Pos);
case ((uint32_t)GPIOC):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOC_Pos);
PORT_Init(PORTC, n, 0, 1); //PORTC.PINn引脚配置为GPIO功能数字输入开启
if(dir == 1)
{
GPIOC->DIR |= (0x01 << n);
}
else
{
GPIOC->DIR &= ~(0x01 << n);
}
if(pull_up == 1)
PORT->PORTC_PULLU |= (0x01 << n);
else
PORT->PORTC_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPIOM):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOM_Pos);
PORT_Init(PORTM, n, 0, 1); //PORTM.PINn引脚配置为GPIO功能数字输入开启
if(dir == 1)
{
GPIOM->DIR |= (0x01 << n);
}
else
{
GPIOM->DIR &= ~(0x01 << n);
}
if(pull_up == 1)
PORT->PORTM_PULLU |= (0x01 << n);
else
PORT->PORTM_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPION):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPION_Pos);
PORT_Init(PORTN, n, 0, 1); //PORTN.PINn引脚配置为GPIO功能数字输入开启
if(dir == 1)
{
GPION->DIR |= (0x01 << n);
}
else
{
GPION->DIR &= ~(0x01 << n);
}
if(pull_down == 1)
PORT->PORTN_PULLD |= (0x01 << n);
else
PORT->PORTN_PULLD &= ~(0x01 << n);
break;
case ((uint32_t)GPIOP):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOP_Pos);
PORT_Init(PORTP, n, 0, 1); //PORTP.PINn引脚配置为GPIO功能数字输入开启
if(dir == 1)
{
GPIOP->DIR |= (0x01 << n);
}
else
{
GPIOP->DIR &= ~(0x01 << n);
}
if(pull_up == 1)
PORT->PORTP_PULLU |= (0x01 << n);
else
PORT->PORTP_PULLU &= ~(0x01 << n);
break;
}
PORT_Init(PORTA, n, 0, 1); //PORTA.PINn引脚配置为GPIO功能数字输入开启
if (dir == 1)
{
GPIOA->DIR |= (0x01 << n);
}
else
{
GPIOA->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTA_PULLU |= (0x01 << n);
else
PORT->PORTA_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPIOB):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOB_Pos);
PORT_Init(PORTB, n, 0, 1); //PORTB.PINn引脚配置为GPIO功能数字输入开启
if (dir == 1)
{
GPIOB->DIR |= (0x01 << n);
}
else
{
GPIOB->DIR &= ~(0x01 << n);
}
if (pull_down == 1)
PORT->PORTB_PULLD |= (0x01 << n);
else
PORT->PORTB_PULLD &= ~(0x01 << n);
break;
case ((uint32_t)GPIOC):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOC_Pos);
PORT_Init(PORTC, n, 0, 1); //PORTC.PINn引脚配置为GPIO功能数字输入开启
if (dir == 1)
{
GPIOC->DIR |= (0x01 << n);
}
else
{
GPIOC->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTC_PULLU |= (0x01 << n);
else
PORT->PORTC_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPIOM):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOM_Pos);
PORT_Init(PORTM, n, 0, 1); //PORTM.PINn引脚配置为GPIO功能数字输入开启
if (dir == 1)
{
GPIOM->DIR |= (0x01 << n);
}
else
{
GPIOM->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTM_PULLU |= (0x01 << n);
else
PORT->PORTM_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPION):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPION_Pos);
PORT_Init(PORTN, n, 0, 1); //PORTN.PINn引脚配置为GPIO功能数字输入开启
if (dir == 1)
{
GPION->DIR |= (0x01 << n);
}
else
{
GPION->DIR &= ~(0x01 << n);
}
if (pull_down == 1)
PORT->PORTN_PULLD |= (0x01 << n);
else
PORT->PORTN_PULLD &= ~(0x01 << n);
break;
case ((uint32_t)GPIOP):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOP_Pos);
PORT_Init(PORTP, n, 0, 1); //PORTP.PINn引脚配置为GPIO功能数字输入开启
if (dir == 1)
{
GPIOP->DIR |= (0x01 << n);
}
else
{
GPIOP->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTP_PULLU |= (0x01 << n);
else
PORT->PORTP_PULLU &= ~(0x01 << n);
break;
}
}
/******************************************************************************************************************************************
* : GPIO_SetBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
* : GPIO_SetBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
******************************************************************************************************************************************/
void GPIO_SetBit(GPIO_TypeDef * GPIOx, uint32_t n)
void GPIO_SetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->DATA |= (0x01 << n);
GPIOx->DATA |= (0x01 << n);
}
/******************************************************************************************************************************************
* : GPIO_ClrBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
* : GPIO_ClrBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
******************************************************************************************************************************************/
void GPIO_ClrBit(GPIO_TypeDef * GPIOx, uint32_t n)
void GPIO_ClrBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->DATA &= ~(0x01 << n);
GPIOx->DATA &= ~(0x01 << n);
}
/******************************************************************************************************************************************
* : GPIO_InvBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
* : GPIO_InvBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
******************************************************************************************************************************************/
void GPIO_InvBit(GPIO_TypeDef * GPIOx, uint32_t n)
void GPIO_InvBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->DATA ^= (0x01 << n);
GPIOx->DATA ^= (0x01 << n);
}
/******************************************************************************************************************************************
* : GPIO_GetBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : 0 1
* :
* : GPIO_GetBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : 0 1
* :
******************************************************************************************************************************************/
uint32_t GPIO_GetBit(GPIO_TypeDef * GPIOx, uint32_t n)
{
return ((GPIOx->DATA >> n) & 0x01);
}
/******************************************************************************************************************************************
* : GPIO_SetBits()
* : n开始的w位连续引脚的电平置高
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
******************************************************************************************************************************************/
void GPIO_SetBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w)
uint32_t GPIO_GetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA |= (bits << n);
return ((GPIOx->DATA >> n) & 0x01);
}
/******************************************************************************************************************************************
* : GPIO_ClrBits()
* : n开始的w位连续引脚的电平置
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
* : GPIO_SetBits()
* : n开始的w位连续引脚的电平置高
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
******************************************************************************************************************************************/
void GPIO_ClrBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w)
void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA &= ~(bits << n);
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA |= (bits << n);
}
/******************************************************************************************************************************************
* : GPIO_InvBits()
* : n开始的w位连续引脚的电平反转
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
* : GPIO_ClrBits()
* : n开始的w位连续引脚的电平置低
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
******************************************************************************************************************************************/
void GPIO_InvBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w)
void GPIO_ClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA ^= (bits << n);
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA &= ~(bits << n);
}
/******************************************************************************************************************************************
* : GPIO_GetBits()
* : n开始的w位连续引脚的电平状态
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* : n开始的w位连续引脚的电平状态 0 1
* 0n的电平状态1n+1... ...w位表示引脚n+w的电平状态
* :
* : GPIO_InvBits()
* : n开始的w位连续引脚的电平反转
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
******************************************************************************************************************************************/
uint32_t GPIO_GetBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w)
void GPIO_InvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
return ((GPIOx->DATA >> n) & bits);
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA ^= (bits << n);
}
/******************************************************************************************************************************************
* : GPIO_AtomicSetBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
* : GPIO_GetBits()
* : n开始的w位连续引脚的电平状态
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* : n开始的w位连续引脚的电平状态 0 1
* 0n的电平状态1n+1... ...w位表示引脚n+w的电平状态
* :
******************************************************************************************************************************************/
void GPIO_AtomicSetBit(GPIO_TypeDef * GPIOx, uint32_t n)
{
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000)*32 + n*4)) = 1;
}
/******************************************************************************************************************************************
* : GPIO_AtomicClrBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicClrBit(GPIO_TypeDef * GPIOx, uint32_t n)
uint32_t GPIO_GetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000)*32 + n*4)) = 0;
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
return ((GPIOx->DATA >> n) & bits);
}
/******************************************************************************************************************************************
* : GPIO_AtomicInvBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
* : GPIO_AtomicSetBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicInvBit(GPIO_TypeDef * GPIOx, uint32_t n)
void GPIO_AtomicSetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000)*32 + n*4)) = 1 - *((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000)*32 + n*4));
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 1;
}
/******************************************************************************************************************************************
* : GPIO_AtomicSetBits()
* : n开始的w位连续引脚的电平置高--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
* : GPIO_AtomicClrBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicSetBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w)
void GPIO_AtomicClrBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA |= (bits << n);
__enable_irq();
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 0;
}
/******************************************************************************************************************************************
* : GPIO_AtomicClrBits()
* : n开始的w位连续引脚的电平置低--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
* : GPIO_AtomicInvBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicClrBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w)
void GPIO_AtomicInvBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA &= ~(bits << n);
__enable_irq();
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 1 - *((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4));
}
/******************************************************************************************************************************************
* : GPIO_AtomicInvBits()
* : n开始的w位连续引脚的电平反转--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
* : GPIO_AtomicSetBits()
* : n开始的w位连续引脚的电平置高--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicInvBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w)
void GPIO_AtomicSetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA ^= (bits << n);
__enable_irq();
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA |= (bits << n);
__enable_irq();
}
/******************************************************************************************************************************************
* : GPIO_AtomicClrBits()
* : n开始的w位连续引脚的电平置低--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA &= ~(bits << n);
__enable_irq();
}
/******************************************************************************************************************************************
* : GPIO_AtomicInvBits()
* : n开始的w位连续引脚的电平反转--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicInvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA ^= (bits << n);
__enable_irq();
}

View File

@ -1,24 +1,22 @@
#ifndef __SWM320_GPIO_H__
#define __SWM320_GPIO_H__
#define __SWM320_GPIO_H__
void GPIO_Init(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t dir, uint32_t pull_up, uint32_t pull_down); //引脚初始化,包含引脚方向、上拉电阻、下拉电阻
void GPIO_Init(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t dir, uint32_t pull_up, uint32_t pull_down); //引脚初始化,包含引脚方向、上拉电阻、下拉电阻
void GPIO_SetBit(GPIO_TypeDef * GPIOx, uint32_t n); //将参数指定的引脚电平置高
void GPIO_ClrBit(GPIO_TypeDef * GPIOx, uint32_t n); //将参数指定的引脚电平置低
void GPIO_InvBit(GPIO_TypeDef * GPIOx, uint32_t n); //将参数指定的引脚电平反转
uint32_t GPIO_GetBit(GPIO_TypeDef * GPIOx, uint32_t n); //读取参数指定的引脚的电平状态
void GPIO_SetBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w); //将参数指定的从n开始的w位连续引脚的电平置高
void GPIO_ClrBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w); //将参数指定的从n开始的w位连续引脚的电平置低
void GPIO_InvBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w); //将参数指定的从n开始的w位连续引脚的电平反转
uint32_t GPIO_GetBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w); //读取参数指定的从n开始的w位连续引脚的电平状态
void GPIO_AtomicSetBit(GPIO_TypeDef * GPIOx, uint32_t n);
void GPIO_AtomicClrBit(GPIO_TypeDef * GPIOx, uint32_t n);
void GPIO_AtomicInvBit(GPIO_TypeDef * GPIOx, uint32_t n);
void GPIO_AtomicSetBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w);
void GPIO_AtomicClrBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w);
void GPIO_AtomicInvBits(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t w);
void GPIO_SetBit(GPIO_TypeDef *GPIOx, uint32_t n); //将参数指定的引脚电平置高
void GPIO_ClrBit(GPIO_TypeDef *GPIOx, uint32_t n); //将参数指定的引脚电平置低
void GPIO_InvBit(GPIO_TypeDef *GPIOx, uint32_t n); //将参数指定的引脚电平反转
uint32_t GPIO_GetBit(GPIO_TypeDef *GPIOx, uint32_t n); //读取参数指定的引脚的电平状态
void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w); //将参数指定的从n开始的w位连续引脚的电平置高
void GPIO_ClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w); //将参数指定的从n开始的w位连续引脚的电平置低
void GPIO_InvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w); //将参数指定的从n开始的w位连续引脚的电平反转
uint32_t GPIO_GetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w); //读取参数指定的从n开始的w位连续引脚的电平状态
void GPIO_AtomicSetBit(GPIO_TypeDef *GPIOx, uint32_t n);
void GPIO_AtomicClrBit(GPIO_TypeDef *GPIOx, uint32_t n);
void GPIO_AtomicInvBit(GPIO_TypeDef *GPIOx, uint32_t n);
void GPIO_AtomicSetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w);
void GPIO_AtomicClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w);
void GPIO_AtomicInvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w);
#endif //__SWM320_GPIO_H__

View File

@ -1,10 +1,10 @@
/******************************************************************************************************************************************
* : SWM320_i2c.c
* : SWM320单片机的I2C串行接口功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
* : SWM320_i2c.c
* : SWM320单片机的I2C串行接口功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
*
*
*******************************************************************************************************************************************
@ -22,194 +22,198 @@
#include "SWM320_i2c.h"
/******************************************************************************************************************************************
* : I2C_Init()
* : I2C初始化
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* I2C_InitStructure * initStruct I2C相关设定值的结构体
* :
* :
* : I2C_Init()
* : I2C初始化
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* I2C_InitStructure * initStruct I2C相关设定值的结构体
* :
* :
******************************************************************************************************************************************/
void I2C_Init(I2C_TypeDef * I2Cx, I2C_InitStructure * initStruct)
void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitStructure *initStruct)
{
switch((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_I2C0_Pos);
break;
case ((uint32_t)I2C1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_I2C1_Pos);
break;
}
I2C_Close(I2Cx); //一些关键寄存器只能在I2C关闭时设置
if(initStruct->Master == 1)
{
I2Cx->CLKDIV = SystemCoreClock/5/initStruct->MstClk;
I2Cx->MSTCMD = (I2Cx->MSTCMD & (~I2C_MSTCMD_IF_Msk)) | (1 << I2C_MSTCMD_IF_Pos); //使能中断之前先清除中断标志
I2Cx->CTRL &= ~I2C_CTRL_MSTIE_Msk;
I2Cx->CTRL |= (initStruct->MstIEn << I2C_CTRL_MSTIE_Pos);
switch((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
if(initStruct->MstIEn)
{
NVIC_EnableIRQ(I2C0_IRQn);
}
else
{
NVIC_DisableIRQ(I2C0_IRQn);
}
break;
case ((uint32_t)I2C1):
if(initStruct->MstIEn)
{
NVIC_EnableIRQ(I2C1_IRQn);
}
else
{
NVIC_DisableIRQ(I2C1_IRQn);
}
break;
}
}
else
{
I2Cx->SLVCR |= (1 << I2C_SLVCR_SLAVE_Pos);
I2Cx->SLVCR &= ~(I2C_SLVCR_ADDR7b_Msk | I2C_SLVCR_ADDR_Msk);
I2Cx->SLVCR |= (1 << I2C_SLVCR_ACK_Pos) |
(initStruct->Addr7b << I2C_SLVCR_ADDR7b_Pos) |
(initStruct->SlvAddr << I2C_SLVCR_ADDR_Pos);
I2Cx->SLVIF = I2C_SLVIF_RXEND_Msk | I2C_SLVIF_TXEND_Msk | I2C_SLVIF_STADET_Msk | I2C_SLVIF_STODET_Msk; //清中断标志
I2Cx->SLVCR &= ~(I2C_SLVCR_IM_RXEND_Msk | I2C_SLVCR_IM_TXEND_Msk | I2C_SLVCR_IM_STADET_Msk | I2C_SLVCR_IM_STODET_Msk |
I2C_SLVCR_IM_RDREQ_Msk | I2C_SLVCR_IM_WRREQ_Msk);
I2Cx->SLVCR |= ((initStruct->SlvRxEndIEn ? 0 : 1) << I2C_SLVCR_IM_RXEND_Pos) |
((initStruct->SlvTxEndIEn ? 0 : 1) << I2C_SLVCR_IM_TXEND_Pos) |
((initStruct->SlvSTADetIEn ? 0 : 1) << I2C_SLVCR_IM_STADET_Pos) |
((initStruct->SlvSTODetIEn ? 0 : 1) << I2C_SLVCR_IM_STODET_Pos) |
((initStruct->SlvRdReqIEn ? 0 : 1) << I2C_SLVCR_IM_RDREQ_Pos) |
((initStruct->SlvWrReqIEn ? 0 : 1) << I2C_SLVCR_IM_WRREQ_Pos);
switch((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
if(initStruct->SlvRxEndIEn | initStruct->SlvTxEndIEn | initStruct->SlvSTADetIEn |
initStruct->SlvSTODetIEn | initStruct->SlvRdReqIEn | initStruct->SlvWrReqIEn)
{
NVIC_EnableIRQ(I2C0_IRQn);
}
else
{
NVIC_DisableIRQ(I2C0_IRQn);
}
break;
case ((uint32_t)I2C1):
if(initStruct->SlvRxEndIEn | initStruct->SlvTxEndIEn | initStruct->SlvSTADetIEn |
initStruct->SlvSTODetIEn | initStruct->SlvRdReqIEn | initStruct->SlvWrReqIEn)
{
NVIC_EnableIRQ(I2C1_IRQn);
}
else
{
NVIC_DisableIRQ(I2C1_IRQn);
}
break;
}
}
switch ((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_I2C0_Pos);
break;
case ((uint32_t)I2C1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_I2C1_Pos);
break;
}
I2C_Close(I2Cx); //一些关键寄存器只能在I2C关闭时设置
if (initStruct->Master == 1)
{
I2Cx->CLKDIV = SystemCoreClock / 5 / initStruct->MstClk;
I2Cx->MSTCMD = (I2Cx->MSTCMD & (~I2C_MSTCMD_IF_Msk)) | (1 << I2C_MSTCMD_IF_Pos); //使能中断之前先清除中断标志
I2Cx->CTRL &= ~I2C_CTRL_MSTIE_Msk;
I2Cx->CTRL |= (initStruct->MstIEn << I2C_CTRL_MSTIE_Pos);
switch ((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
if (initStruct->MstIEn)
{
NVIC_EnableIRQ(I2C0_IRQn);
}
else
{
NVIC_DisableIRQ(I2C0_IRQn);
}
break;
case ((uint32_t)I2C1):
if (initStruct->MstIEn)
{
NVIC_EnableIRQ(I2C1_IRQn);
}
else
{
NVIC_DisableIRQ(I2C1_IRQn);
}
break;
}
}
else
{
I2Cx->SLVCR |= (1 << I2C_SLVCR_SLAVE_Pos);
I2Cx->SLVCR &= ~(I2C_SLVCR_ADDR7b_Msk | I2C_SLVCR_ADDR_Msk);
I2Cx->SLVCR |= (1 << I2C_SLVCR_ACK_Pos) |
(initStruct->Addr7b << I2C_SLVCR_ADDR7b_Pos) |
(initStruct->SlvAddr << I2C_SLVCR_ADDR_Pos);
I2Cx->SLVIF = I2C_SLVIF_RXEND_Msk | I2C_SLVIF_TXEND_Msk | I2C_SLVIF_STADET_Msk | I2C_SLVIF_STODET_Msk; //清中断标志
I2Cx->SLVCR &= ~(I2C_SLVCR_IM_RXEND_Msk | I2C_SLVCR_IM_TXEND_Msk | I2C_SLVCR_IM_STADET_Msk | I2C_SLVCR_IM_STODET_Msk |
I2C_SLVCR_IM_RDREQ_Msk | I2C_SLVCR_IM_WRREQ_Msk);
I2Cx->SLVCR |= ((initStruct->SlvRxEndIEn ? 0 : 1) << I2C_SLVCR_IM_RXEND_Pos) |
((initStruct->SlvTxEndIEn ? 0 : 1) << I2C_SLVCR_IM_TXEND_Pos) |
((initStruct->SlvSTADetIEn ? 0 : 1) << I2C_SLVCR_IM_STADET_Pos) |
((initStruct->SlvSTODetIEn ? 0 : 1) << I2C_SLVCR_IM_STODET_Pos) |
((initStruct->SlvRdReqIEn ? 0 : 1) << I2C_SLVCR_IM_RDREQ_Pos) |
((initStruct->SlvWrReqIEn ? 0 : 1) << I2C_SLVCR_IM_WRREQ_Pos);
switch ((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
if (initStruct->SlvRxEndIEn | initStruct->SlvTxEndIEn | initStruct->SlvSTADetIEn |
initStruct->SlvSTODetIEn | initStruct->SlvRdReqIEn | initStruct->SlvWrReqIEn)
{
NVIC_EnableIRQ(I2C0_IRQn);
}
else
{
NVIC_DisableIRQ(I2C0_IRQn);
}
break;
case ((uint32_t)I2C1):
if (initStruct->SlvRxEndIEn | initStruct->SlvTxEndIEn | initStruct->SlvSTADetIEn |
initStruct->SlvSTODetIEn | initStruct->SlvRdReqIEn | initStruct->SlvWrReqIEn)
{
NVIC_EnableIRQ(I2C1_IRQn);
}
else
{
NVIC_DisableIRQ(I2C1_IRQn);
}
break;
}
}
}
/******************************************************************************************************************************************
* : I2C_Open()
* : I2C打开
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* :
* :
* : I2C_Open()
* : I2C打开
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* :
* :
******************************************************************************************************************************************/
void I2C_Open(I2C_TypeDef * I2Cx)
void I2C_Open(I2C_TypeDef *I2Cx)
{
I2Cx->CTRL |= (0x01 << I2C_CTRL_EN_Pos);
I2Cx->CTRL |= (0x01 << I2C_CTRL_EN_Pos);
}
/******************************************************************************************************************************************
* : I2C_Close()
* : I2C关闭
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* :
* :
* : I2C_Close()
* : I2C关闭
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* :
* :
******************************************************************************************************************************************/
void I2C_Close(I2C_TypeDef * I2Cx)
void I2C_Close(I2C_TypeDef *I2Cx)
{
I2Cx->CTRL &= ~I2C_CTRL_EN_Msk;
I2Cx->CTRL &= ~I2C_CTRL_EN_Msk;
}
/******************************************************************************************************************************************
* : I2C_Start()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t addr
* : uint8_t 1 ACK 0 NACK
* :
* : I2C_Start()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t addr
* : uint8_t 1 ACK 0 NACK
* :
******************************************************************************************************************************************/
uint8_t I2C_Start(I2C_TypeDef * I2Cx, uint8_t addr)
uint8_t I2C_Start(I2C_TypeDef *I2Cx, uint8_t addr)
{
I2Cx->MSTDAT = addr;
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STA_Pos) |
(1 << I2C_MSTCMD_WR_Pos); //发送起始位和从机地址
while(I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk) __NOP(); //等待发送完成
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
I2Cx->MSTDAT = addr;
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STA_Pos) |
(1 << I2C_MSTCMD_WR_Pos); //发送起始位和从机地址
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待发送完成
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
* : I2C_Stop()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* :
* :
* : I2C_Stop()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* :
* :
******************************************************************************************************************************************/
void I2C_Stop(I2C_TypeDef * I2Cx)
void I2C_Stop(I2C_TypeDef *I2Cx)
{
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STO_Pos);
while(I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk) __NOP(); //等待发送完成
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STO_Pos);
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待发送完成
}
/******************************************************************************************************************************************
* : I2C_Write()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t data
* : uint8_t 1 ACK 0 NACK
* :
* : I2C_Write()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t data
* : uint8_t 1 ACK 0 NACK
* :
******************************************************************************************************************************************/
uint8_t I2C_Write(I2C_TypeDef * I2Cx, uint8_t data)
{
I2Cx->MSTDAT = data;
I2Cx->MSTCMD = (1 << I2C_MSTCMD_WR_Pos);
while(I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk) __NOP(); //等待发送完成
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
uint8_t I2C_Write(I2C_TypeDef *I2Cx, uint8_t data)
{
I2Cx->MSTDAT = data;
I2Cx->MSTCMD = (1 << I2C_MSTCMD_WR_Pos);
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待发送完成
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
* : I2C_Read()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t ack 1 ACK 0 NACK
* : uint8_t
* :
* : I2C_Read()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t ack 1 ACK 0 NACK
* : uint8_t
* :
******************************************************************************************************************************************/
uint8_t I2C_Read(I2C_TypeDef * I2Cx, uint8_t ack)
uint8_t I2C_Read(I2C_TypeDef *I2Cx, uint8_t ack)
{
I2Cx->MSTCMD = (1 << I2C_MSTCMD_RD_Pos) |
((ack ? 0 : 1) << I2C_MSTCMD_ACK_Pos);
while(I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk) __NOP(); //等待接收完成
I2Cx->MSTCMD = (1 << I2C_MSTCMD_RD_Pos) |
((ack ? 0 : 1) << I2C_MSTCMD_ACK_Pos);
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待接收完成
return I2Cx->MSTDAT;
}

View File

@ -1,31 +1,31 @@
#ifndef __SWM320_I2C_H__
#define __SWM320_I2C_H__
typedef struct {
uint8_t Master; //1 主机模式
uint8_t Addr7b; //1 7位地址 0 10位地址
uint32_t MstClk; //主机传输时钟频率
uint8_t MstIEn; //主机模式中断使能
uint16_t SlvAddr; //从机地址
uint8_t SlvRxEndIEn; //从机接收完成中断使能
uint8_t SlvTxEndIEn; //从机发送完成中断使能
uint8_t SlvSTADetIEn; //从机检测到起始中断使能
uint8_t SlvSTODetIEn; //从机检测到终止中断使能
uint8_t SlvRdReqIEn; //从机接收到读请求中断使能
uint8_t SlvWrReqIEn; //从机接收到写请求中断使能
typedef struct
{
uint8_t Master; //1 主机模式
uint8_t Addr7b; //1 7位地址 0 10位地址
uint32_t MstClk; //主机传输时钟频率
uint8_t MstIEn; //主机模式中断使能
uint16_t SlvAddr; //从机地址
uint8_t SlvRxEndIEn; //从机接收完成中断使能
uint8_t SlvTxEndIEn; //从机发送完成中断使能
uint8_t SlvSTADetIEn; //从机检测到起始中断使能
uint8_t SlvSTODetIEn; //从机检测到终止中断使能
uint8_t SlvRdReqIEn; //从机接收到读请求中断使能
uint8_t SlvWrReqIEn; //从机接收到写请求中断使能
} I2C_InitStructure;
void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitStructure *initStruct);
void I2C_Init(I2C_TypeDef * I2Cx, I2C_InitStructure * initStruct);
void I2C_Open(I2C_TypeDef *I2Cx);
void I2C_Close(I2C_TypeDef *I2Cx);
void I2C_Open(I2C_TypeDef * I2Cx);
void I2C_Close(I2C_TypeDef * I2Cx);
uint8_t I2C_Start(I2C_TypeDef * I2Cx, uint8_t addr);
void I2C_Stop(I2C_TypeDef * I2Cx);
uint8_t I2C_Write(I2C_TypeDef * I2Cx, uint8_t data);
uint8_t I2C_Read(I2C_TypeDef * I2Cx, uint8_t ack);
uint8_t I2C_Start(I2C_TypeDef *I2Cx, uint8_t addr);
void I2C_Stop(I2C_TypeDef *I2Cx);
uint8_t I2C_Write(I2C_TypeDef *I2Cx, uint8_t data);
uint8_t I2C_Read(I2C_TypeDef *I2Cx, uint8_t ack);
#endif //__SWM320_I2C_H__

View File

@ -31,45 +31,47 @@
* :
* :
******************************************************************************************************************************************/
void LCD_Init(LCD_TypeDef * LCDx, LCD_InitStructure * initStruct)
{
switch((uint32_t)LCDx)
{
case ((uint32_t)LCD):
SYS->CLKEN |= (0x01 << SYS_CLKEN_LCD_Pos);
break;
}
LCDx->CR0 = ((initStruct->HnPixel - 1) << LCD_CR0_HPIX_Pos) |
((initStruct->VnPixel - 1) << LCD_CR0_VPIX_Pos) |
(initStruct->ClkAlways << LCD_CR0_DCLK_Pos) |
(initStruct->HsyncWidth << LCD_CR0_HLOW_Pos);
LCDx->CR1 = ((initStruct->Hfp - 1) << LCD_CR1_HFP_Pos) |
((initStruct->Hbp - 1) << LCD_CR1_HBP_Pos) |
((initStruct->Vfp - 1) << LCD_CR1_VFP_Pos) |
((initStruct->Vbp - 1) << LCD_CR1_VBP_Pos) |
(initStruct->ClkDiv << LCD_CR1_DCLKDIV_Pos) |
(initStruct->SamplEdge << LCD_CR1_DCLKINV_Pos);
LCDx->IE = 1;
LCDx->IF = 1; //清除标志
if(initStruct->IntEOTEn) LCD_INTEn(LCDx);
else LCD_INTDis(LCDx);
switch((uint32_t)LCDx)
{
case ((uint32_t)LCD):
if(initStruct->IntEOTEn)
{
NVIC_EnableIRQ(LCD_IRQn);
}
else
{
NVIC_DisableIRQ(LCD_IRQn);
}
break;
}
void LCD_Init(LCD_TypeDef *LCDx, LCD_InitStructure *initStruct)
{
switch ((uint32_t)LCDx)
{
case ((uint32_t)LCD):
SYS->CLKEN |= (0x01 << SYS_CLKEN_LCD_Pos);
break;
}
LCDx->CR0 = ((initStruct->HnPixel - 1) << LCD_CR0_HPIX_Pos) |
((initStruct->VnPixel - 1) << LCD_CR0_VPIX_Pos) |
(initStruct->ClkAlways << LCD_CR0_DCLK_Pos) |
(initStruct->HsyncWidth << LCD_CR0_HLOW_Pos);
LCDx->CR1 = ((initStruct->Hfp - 1) << LCD_CR1_HFP_Pos) |
((initStruct->Hbp - 1) << LCD_CR1_HBP_Pos) |
((initStruct->Vfp - 1) << LCD_CR1_VFP_Pos) |
((initStruct->Vbp - 1) << LCD_CR1_VBP_Pos) |
(initStruct->ClkDiv << LCD_CR1_DCLKDIV_Pos) |
(initStruct->SamplEdge << LCD_CR1_DCLKINV_Pos);
LCDx->IE = 1;
LCDx->IF = 1; //清除标志
if (initStruct->IntEOTEn)
LCD_INTEn(LCDx);
else
LCD_INTDis(LCDx);
switch ((uint32_t)LCDx)
{
case ((uint32_t)LCD):
if (initStruct->IntEOTEn)
{
NVIC_EnableIRQ(LCD_IRQn);
}
else
{
NVIC_DisableIRQ(LCD_IRQn);
}
break;
}
}
/******************************************************************************************************************************************
@ -79,9 +81,9 @@ void LCD_Init(LCD_TypeDef * LCDx, LCD_InitStructure * initStruct)
* :
* :
******************************************************************************************************************************************/
void LCD_Start(LCD_TypeDef * LCDx)
void LCD_Start(LCD_TypeDef *LCDx)
{
LCDx->START |= (1 << LCD_START_GO_Pos) | (1 << LCD_START_BURST_Pos);
LCDx->START |= (1 << LCD_START_GO_Pos) | (1 << LCD_START_BURST_Pos);
}
/******************************************************************************************************************************************
@ -91,9 +93,9 @@ void LCD_Start(LCD_TypeDef * LCDx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t LCD_IsBusy(LCD_TypeDef * LCDx)
uint32_t LCD_IsBusy(LCD_TypeDef *LCDx)
{
return (LCDx->START & LCD_START_GO_Msk) ? 1 : 0;
return (LCDx->START & LCD_START_GO_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -103,9 +105,9 @@ uint32_t LCD_IsBusy(LCD_TypeDef * LCDx)
* :
* :
******************************************************************************************************************************************/
void LCD_INTEn(LCD_TypeDef * LCDx)
void LCD_INTEn(LCD_TypeDef *LCDx)
{
LCDx->IM = 0;
LCDx->IM = 0;
}
/******************************************************************************************************************************************
@ -115,9 +117,9 @@ void LCD_INTEn(LCD_TypeDef * LCDx)
* :
* :
******************************************************************************************************************************************/
void LCD_INTDis(LCD_TypeDef * LCDx)
void LCD_INTDis(LCD_TypeDef *LCDx)
{
LCDx->IM = 1;
LCDx->IM = 1;
}
/******************************************************************************************************************************************
@ -127,9 +129,9 @@ void LCD_INTDis(LCD_TypeDef * LCDx)
* :
* :
******************************************************************************************************************************************/
void LCD_INTClr(LCD_TypeDef * LCDx)
void LCD_INTClr(LCD_TypeDef *LCDx)
{
LCDx->IF = 1;
LCDx->IF = 1;
}
/******************************************************************************************************************************************
@ -139,7 +141,7 @@ void LCD_INTClr(LCD_TypeDef * LCDx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t LCD_INTStat(LCD_TypeDef * LCDx)
uint32_t LCD_INTStat(LCD_TypeDef *LCDx)
{
return (LCDx->IF & 0x01) ? 1 : 0;
return (LCDx->IF & 0x01) ? 1 : 0;
}

View File

@ -1,73 +1,70 @@
#ifndef __SWM320_LCD_H__
#define __SWM320_LCD_H__
typedef struct
{
uint16_t HnPixel; //水平方向像素个数最大取值1024
uint16_t VnPixel; //垂直方向像素个数,最大取值 768
uint8_t Hfp; //horizonal front porch最大取值32
uint8_t Hbp; //horizonal back porch 最大取值128
uint8_t Vfp; //vertical front porch 最大取值8
uint8_t Vbp; //vertical back porch 最大取值32
uint8_t ClkDiv; //系统时钟经ClkDiv分频后产生DOCCLK0 2分频 1 4分频 2 6分频 ... ... 31 64分频
uint8_t SamplEdge; //屏幕在DOTCLK的哪个边沿采样数据LCD_SAMPLEDGE_RISE、LCD_SAMPLEDGE_FALL
uint8_t ClkAlways; //1 一直输出DOTCLK 0 只在传输数据时输出DOTCLK
uint8_t HsyncWidth; //HSYNC低电平持续多少个DOTCLK取值LCD_HSYNC_1DOTCLK、LCD_HSYNC_2DOTCLK、LCD_HSYNC_3DOTCLK、LCD_HSYNC_4DOTCLK
typedef struct {
uint16_t HnPixel; //水平方向像素个数最大取值1024
uint16_t VnPixel; //垂直方向像素个数,最大取值 768
uint8_t Hfp; //horizonal front porch最大取值32
uint8_t Hbp; //horizonal back porch 最大取值128
uint8_t Vfp; //vertical front porch 最大取值8
uint8_t Vbp; //vertical back porch 最大取值32
uint8_t ClkDiv; //系统时钟经ClkDiv分频后产生DOCCLK0 2分频 1 4分频 2 6分频 ... ... 31 64分频
uint8_t SamplEdge; //屏幕在DOTCLK的哪个边沿采样数据LCD_SAMPLEDGE_RISE、LCD_SAMPLEDGE_FALL
uint8_t ClkAlways; //1 一直输出DOTCLK 0 只在传输数据时输出DOTCLK
uint8_t HsyncWidth; //HSYNC低电平持续多少个DOTCLK取值LCD_HSYNC_1DOTCLK、LCD_HSYNC_2DOTCLK、LCD_HSYNC_3DOTCLK、LCD_HSYNC_4DOTCLK
uint8_t IntEOTEn; //End of Transter传输完成中断使能
uint8_t IntEOTEn; //End of Transter传输完成中断使能
} LCD_InitStructure;
#define LCD_SAMPLEDGE_RISE 0 //屏幕在DOTCLK的上升沿采样数据
#define LCD_SAMPLEDGE_FALL 1 //屏幕在DOTCLK的下降沿采样数据
#define LCD_SAMPLEDGE_RISE 0 //屏幕在DOTCLK的上升沿采样数据
#define LCD_SAMPLEDGE_FALL 1 //屏幕在DOTCLK的下降沿采样数据
#define LCD_HSYNC_1DOTCLK 0 //1个DOTCLK
#define LCD_HSYNC_2DOTCLK 1
#define LCD_HSYNC_3DOTCLK 2
#define LCD_HSYNC_4DOTCLK 3
#define LCD_HSYNC_1DOTCLK 0 //1个DOTCLK
#define LCD_HSYNC_2DOTCLK 1
#define LCD_HSYNC_3DOTCLK 2
#define LCD_HSYNC_4DOTCLK 3
#define LCD_CLKDIV_2 0
#define LCD_CLKDIV_4 1
#define LCD_CLKDIV_6 2
#define LCD_CLKDIV_8 3
#define LCD_CLKDIV_10 4
#define LCD_CLKDIV_12 5
#define LCD_CLKDIV_14 6
#define LCD_CLKDIV_16 7
#define LCD_CLKDIV_18 8
#define LCD_CLKDIV_20 9
#define LCD_CLKDIV_22 10
#define LCD_CLKDIV_24 11
#define LCD_CLKDIV_26 12
#define LCD_CLKDIV_28 13
#define LCD_CLKDIV_30 14
#define LCD_CLKDIV_32 15
#define LCD_CLKDIV_34 16
#define LCD_CLKDIV_36 17
#define LCD_CLKDIV_38 18
#define LCD_CLKDIV_40 19
#define LCD_CLKDIV_42 20
#define LCD_CLKDIV_44 21
#define LCD_CLKDIV_46 22
#define LCD_CLKDIV_48 23
#define LCD_CLKDIV_50 24
#define LCD_CLKDIV_52 25
#define LCD_CLKDIV_54 26
#define LCD_CLKDIV_56 27
#define LCD_CLKDIV_58 28
#define LCD_CLKDIV_60 29
#define LCD_CLKDIV_62 30
#define LCD_CLKDIV_64 31
#define LCD_CLKDIV_2 0
#define LCD_CLKDIV_4 1
#define LCD_CLKDIV_6 2
#define LCD_CLKDIV_8 3
#define LCD_CLKDIV_10 4
#define LCD_CLKDIV_12 5
#define LCD_CLKDIV_14 6
#define LCD_CLKDIV_16 7
#define LCD_CLKDIV_18 8
#define LCD_CLKDIV_20 9
#define LCD_CLKDIV_22 10
#define LCD_CLKDIV_24 11
#define LCD_CLKDIV_26 12
#define LCD_CLKDIV_28 13
#define LCD_CLKDIV_30 14
#define LCD_CLKDIV_32 15
#define LCD_CLKDIV_34 16
#define LCD_CLKDIV_36 17
#define LCD_CLKDIV_38 18
#define LCD_CLKDIV_40 19
#define LCD_CLKDIV_42 20
#define LCD_CLKDIV_44 21
#define LCD_CLKDIV_46 22
#define LCD_CLKDIV_48 23
#define LCD_CLKDIV_50 24
#define LCD_CLKDIV_52 25
#define LCD_CLKDIV_54 26
#define LCD_CLKDIV_56 27
#define LCD_CLKDIV_58 28
#define LCD_CLKDIV_60 29
#define LCD_CLKDIV_62 30
#define LCD_CLKDIV_64 31
void LCD_Init(LCD_TypeDef * LCDx, LCD_InitStructure * initStruct);
void LCD_Start(LCD_TypeDef * LCDx);
uint32_t LCD_IsBusy(LCD_TypeDef * LCDx);
void LCD_INTEn(LCD_TypeDef * LCDx);
void LCD_INTDis(LCD_TypeDef * LCDx);
void LCD_INTClr(LCD_TypeDef * LCDx);
uint32_t LCD_INTStat(LCD_TypeDef * LCDx);
void LCD_Init(LCD_TypeDef *LCDx, LCD_InitStructure *initStruct);
void LCD_Start(LCD_TypeDef *LCDx);
uint32_t LCD_IsBusy(LCD_TypeDef *LCDx);
void LCD_INTEn(LCD_TypeDef *LCDx);
void LCD_INTDis(LCD_TypeDef *LCDx);
void LCD_INTClr(LCD_TypeDef *LCDx);
uint32_t LCD_INTStat(LCD_TypeDef *LCDx);
#endif //__SWM320_LCD_H__

View File

@ -1,10 +1,10 @@
/******************************************************************************************************************************************
* : SWM320_norflash.c
* : SWM320单片机的NOR Flash驱动程序
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
* : SWM320_norflash.c
* : SWM320单片机的NOR Flash驱动程序
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
*
*
*******************************************************************************************************************************************
@ -21,152 +21,167 @@
#include "SWM320.h"
#include "SWM320_norflash.h"
/******************************************************************************************************************************************
* : NORFL_Init()
* : NOR Flash控制器初始化
* : NORFL_InitStructure * initStruct NOR Flash控制器相关设定值的结构体
* :
* :
* : NORFL_Init()
* : NOR Flash控制器初始化
* : NORFL_InitStructure * initStruct NOR Flash控制器相关设定值的结构体
* :
* :
******************************************************************************************************************************************/
void NORFL_Init(NORFL_InitStructure * initStruct)
void NORFL_Init(NORFL_InitStructure *initStruct)
{
uint32_t i;
// 配置SRAM前需要刷新下SDRAM控制器
do {
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
while(SDRAMC->REFDONE == 0);
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
for(i = 0; i < 1000; i++) __NOP();
SYS->CLKEN &= ~(1 << SYS_CLKEN_SDRAM_Pos);
} while(0);
SYS->CLKEN |= (1 << SYS_CLKEN_NORFL_Pos);
NORFLC->CR = ((initStruct->DataWidth == 8 ? 1 : 0) << NORFLC_CR_BYTEIF_Pos) |
(initStruct->WELowPulseTime << NORFLC_CR_WRTIME_Pos) |
(initStruct->OEPreValidTime << NORFLC_CR_RDTIME_Pos);
NORFLC->IE = 3;
NORFLC->IF = 3; // 清除中断标志
if(initStruct->OperFinishIEn) NORFLC->IM &= ~(1 << NORFLC_IM_FINISH_Pos);
else NORFLC->IM |= (1 << NORFLC_IM_FINISH_Pos);
if(initStruct->OperTimeoutIEn) NORFLC->IM &= ~(1 << NORFLC_IM_TIMEOUT_Pos);
else NORFLC->IM |= (1 << NORFLC_IM_TIMEOUT_Pos);
uint32_t i;
// 配置SRAM前需要刷新下SDRAM控制器
do
{
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
while (SDRAMC->REFDONE == 0)
;
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
for (i = 0; i < 1000; i++)
__NOP();
SYS->CLKEN &= ~(1 << SYS_CLKEN_SDRAM_Pos);
} while (0);
SYS->CLKEN |= (1 << SYS_CLKEN_NORFL_Pos);
NORFLC->CR = ((initStruct->DataWidth == 8 ? 1 : 0) << NORFLC_CR_BYTEIF_Pos) |
(initStruct->WELowPulseTime << NORFLC_CR_WRTIME_Pos) |
(initStruct->OEPreValidTime << NORFLC_CR_RDTIME_Pos);
NORFLC->IE = 3;
NORFLC->IF = 3; // 清除中断标志
if (initStruct->OperFinishIEn)
NORFLC->IM &= ~(1 << NORFLC_IM_FINISH_Pos);
else
NORFLC->IM |= (1 << NORFLC_IM_FINISH_Pos);
if (initStruct->OperTimeoutIEn)
NORFLC->IM &= ~(1 << NORFLC_IM_TIMEOUT_Pos);
else
NORFLC->IM |= (1 << NORFLC_IM_TIMEOUT_Pos);
}
/******************************************************************************************************************************************
* : NORFL_ChipErase()
* : NOR Flash整片擦除
* :
* : uint32_t 0 1
* :
* : NORFL_ChipErase()
* : NOR Flash整片擦除
* :
* : uint32_t 0 1
* :
******************************************************************************************************************************************/
uint32_t NORFL_ChipErase(void)
{
uint32_t res;
NORFLC->CMD = (NORFL_CMD_CHIP_ERASE << NORFLC_CMD_CMD_Pos);
while(((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0)) __NOP();
if(NORFLC->IF & NORFLC_IF_FINISH_Msk) res = 0;
else res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
uint32_t res;
NORFLC->CMD = (NORFL_CMD_CHIP_ERASE << NORFLC_CMD_CMD_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
}
/******************************************************************************************************************************************
* : NORFL_SectorErase()
* : NOR Flash扇区擦除
* : uint32_t addr
* : uint32_t 0 1
* : MX29LV128DB 88K25564K MX29LV128DT 25564K88K
* : NORFL_SectorErase()
* : NOR Flash扇区擦除
* : uint32_t addr
* : uint32_t 0 1
* : MX29LV128DB 88K25564K MX29LV128DT 25564K88K
******************************************************************************************************************************************/
uint32_t NORFL_SectorErase(uint32_t addr)
{
uint32_t res;
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_SECTOR_ERASE << NORFLC_CMD_CMD_Pos);
while(((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0)) __NOP();
if(NORFLC->IF & NORFLC_IF_FINISH_Msk) res = 0;
else res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
uint32_t res;
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_SECTOR_ERASE << NORFLC_CMD_CMD_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
}
/******************************************************************************************************************************************
* : NORFL_Write()
* : NOR Flash写
* : uint32_t addr
* uint32_t data
* : uint32_t 0 1
* : 线16线8
* : NORFL_Write()
* : NOR Flash写
* : uint32_t addr
* uint32_t data
* : uint32_t 0 1
* : 线16线8
******************************************************************************************************************************************/
uint32_t NORFL_Write(uint32_t addr, uint32_t data)
{
uint32_t res;
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_PROGRAM << NORFLC_CMD_CMD_Pos) | (data << NORFLC_CMD_DATA_Pos);
while(((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0)) __NOP();
if(NORFLC->IF & NORFLC_IF_FINISH_Msk) res = 0;
else res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
uint32_t res;
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_PROGRAM << NORFLC_CMD_CMD_Pos) | (data << NORFLC_CMD_DATA_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
}
/******************************************************************************************************************************************
* : NORFL_Read()
* : NOR Flash读
* : uint32_t addr
* : uint32_t
* : 线16线8
* : NORFL_Read()
* : NOR Flash读
* : uint32_t addr
* : uint32_t
* : 线16线8
******************************************************************************************************************************************/
uint32_t NORFL_Read(uint32_t addr)
{
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_READ << NORFLC_CMD_CMD_Pos);
return (NORFLC->CMD & NORFLC_CMD_DATA_Msk);
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_READ << NORFLC_CMD_CMD_Pos);
return (NORFLC->CMD & NORFLC_CMD_DATA_Msk);
}
/******************************************************************************************************************************************
* : NORFL_ReadID()
* : NOR Flash读ID
* : uint32_t id_addr ID地址
* : uint16_t ID
* :
* : NORFL_ReadID()
* : NOR Flash读ID
* : uint32_t id_addr ID地址
* : uint16_t ID
* :
******************************************************************************************************************************************/
uint16_t NORFL_ReadID(uint32_t id_addr)
{
uint16_t id;
NORFLC->CMD = (NORFL_CMD_AUTO_SELECT << NORFLC_CMD_CMD_Pos);
NORFLC->ADDR = id_addr;
NORFLC->CMD = (NORFL_CMD_READ << NORFLC_CMD_CMD_Pos);
id = NORFLC->CMD & NORFLC_CMD_DATA_Msk;
NORFLC->CMD = (NORFL_CMD_RESET << NORFLC_CMD_CMD_Pos); // 退出ID读取模式
return id;
uint16_t id;
NORFLC->CMD = (NORFL_CMD_AUTO_SELECT << NORFLC_CMD_CMD_Pos);
NORFLC->ADDR = id_addr;
NORFLC->CMD = (NORFL_CMD_READ << NORFLC_CMD_CMD_Pos);
id = NORFLC->CMD & NORFLC_CMD_DATA_Msk;
NORFLC->CMD = (NORFL_CMD_RESET << NORFLC_CMD_CMD_Pos); // 退出ID读取模式
return id;
}

View File

@ -1,38 +1,34 @@
#ifndef __SWM320_NORFLASH_H__
#define __SWM320_NORFLASH_H__
typedef struct {
uint8_t DataWidth; // 8、16
uint8_t WELowPulseTime; // WE# pulse width单位为系统时钟周期最大值为7
uint8_t OEPreValidTime; // Valid data output after OE# low单位为系统时钟周期最大值为15
uint8_t OperFinishIEn; // 操作(写入、擦除)完成中断使能
uint8_t OperTimeoutIEn;
typedef struct
{
uint8_t DataWidth; // 8、16
uint8_t WELowPulseTime; // WE# pulse width单位为系统时钟周期最大值为7
uint8_t OEPreValidTime; // Valid data output after OE# low单位为系统时钟周期最大值为15
uint8_t OperFinishIEn; // 操作(写入、擦除)完成中断使能
uint8_t OperTimeoutIEn;
} NORFL_InitStructure;
void NORFL_Init(NORFL_InitStructure * initStruct);
void NORFL_Init(NORFL_InitStructure *initStruct);
uint32_t NORFL_ChipErase(void);
uint32_t NORFL_SectorErase(uint32_t addr);
uint32_t NORFL_Write(uint32_t addr, uint32_t data);
uint32_t NORFL_Read(uint32_t addr);
uint16_t NORFL_ReadID(uint32_t id_addr);
/* 当前版本总线读只支持字读
#define NORFL_Read8(addr) *((volatile uint8_t *)(NORFLM_BASE + addr))
#define NORFL_Read16(addr) *((volatile uint16_t *)(NORFLM_BASE + addr)) */
#define NORFL_Read32(addr) *((volatile uint32_t *)(NORFLM_BASE + addr))
#define NORFL_Read32(addr) *((volatile uint32_t *)(NORFLM_BASE + addr))
#define NORFL_CMD_READ 0
#define NORFL_CMD_RESET 1
#define NORFL_CMD_AUTO_SELECT 2
#define NORFL_CMD_PROGRAM 3
#define NORFL_CMD_CHIP_ERASE 4
#define NORFL_CMD_SECTOR_ERASE 5
#define NORFL_CMD_READ 0
#define NORFL_CMD_RESET 1
#define NORFL_CMD_AUTO_SELECT 2
#define NORFL_CMD_PROGRAM 3
#define NORFL_CMD_CHIP_ERASE 4
#define NORFL_CMD_SECTOR_ERASE 5
#endif // __SWM320_NORFLASH_H__

View File

@ -21,7 +21,6 @@
#include "SWM320.h"
#include "SWM320_port.h"
/******************************************************************************************************************************************
* : PORT_Init()
* : "SWM320_port.h"
@ -35,187 +34,187 @@
******************************************************************************************************************************************/
void PORT_Init(uint32_t PORTx, uint32_t n, uint32_t func, uint32_t digit_in_en)
{
switch((uint32_t)PORTx)
{
case ((uint32_t)PORTA):
if(func > 99)
{
if(n < PIN6)
{
PORT->PORTA_MUX0 &= ~(0x1F << (n*5));
PORT->PORTA_MUX0 |= (func-100) << (n*5);
}
else if(n < PIN12)
{
PORT->PORTA_MUX1 &= ~(0x1F << ((n-6)*5));
PORT->PORTA_MUX1 |= (func-100) << ((n-6)*5);
}
}
PORT->PORTA_SEL &= ~(0x03 << (n*2));
PORT->PORTA_SEL |= (func > 99 ? 1 : func) << (n*2);
PORT->PORTA_INEN &= ~(0x01 << n);
PORT->PORTA_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTB):
if(func > 99)
{
if(n < PIN6)
{
PORT->PORTB_MUX0 &= ~(0x1F << (n*5));
PORT->PORTB_MUX0 |= (func-100) << (n*5);
}
else if(n < PIN12)
{
PORT->PORTB_MUX1 &= ~(0x1F << ((n-6)*5));
PORT->PORTB_MUX1 |= (func-100) << ((n-6)*5);
}
}
PORT->PORTB_SEL &= ~(0x03 << (n*2));
PORT->PORTB_SEL |= (func > 99 ? 1 : func) << (n*2);
PORT->PORTB_INEN &= ~(0x01 << n);
PORT->PORTB_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTC):
if(func > 99)
{
if(n < PIN6)
{
PORT->PORTC_MUX0 &= ~(0x1F << (n*5));
PORT->PORTC_MUX0 |= (func-100) << (n*5);
}
else if(n < PIN12)
{
PORT->PORTC_MUX1 &= ~(0x1F << ((n-6)*5));
PORT->PORTC_MUX1 |= (func-100) << ((n-6)*5);
}
}
PORT->PORTC_SEL &= ~(0x03 << (n*2));
PORT->PORTC_SEL |= (func > 99 ? 1 : func) << (n*2);
PORT->PORTC_INEN &= ~(0x01 << n);
PORT->PORTC_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTM):
if(func > 99)
{
if(n < PIN6)
{
PORT->PORTM_MUX0 &= ~(0x1F << (n*5));
PORT->PORTM_MUX0 |= (func-100) << (n*5);
}
else if(n < PIN12)
{
PORT->PORTM_MUX1 &= ~(0x1F << ((n-6)*5));
PORT->PORTM_MUX1 |= (func-100) << ((n-6)*5);
}
else if(n < PIN18)
{
PORT->PORTM_MUX2 &= ~(0x1F << ((n-12)*5));
PORT->PORTM_MUX2 |= (func-100) << ((n-12)*5);
}
else if(n < PIN24)
{
PORT->PORTM_MUX3 &= ~(0x1F << ((n-18)*5));
PORT->PORTM_MUX3 |= (func-100) << ((n-18)*5);
}
}
if(n < 16)
{
PORT->PORTM_SEL0 &= ~(0x03 << (n*2));
PORT->PORTM_SEL0 |= (func > 99 ? 1 : func) << (n*2);
}
else
{
PORT->PORTM_SEL1 &= ~(0x03 << ((n-16)*2));
PORT->PORTM_SEL1 |= (func > 99 ? 1 : func) << ((n-16)*2);
}
PORT->PORTM_INEN &= ~(0x01 << n);
PORT->PORTM_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTN):
if(func > 99)
{
if(n < PIN6)
{
PORT->PORTN_MUX0 &= ~(0x1F << (n*5));
PORT->PORTN_MUX0 |= (func-100) << (n*5);
}
else if(n < PIN12)
{
PORT->PORTN_MUX1 &= ~(0x1F << ((n-6)*5));
PORT->PORTN_MUX1 |= (func-100) << ((n-6)*5);
}
else if(n < PIN18)
{
PORT->PORTN_MUX2 &= ~(0x1F << ((n-12)*5));
PORT->PORTN_MUX2 |= (func-100) << ((n-12)*5);
}
}
if(n < 16)
{
PORT->PORTN_SEL0 &= ~(0x03 << (n*2));
PORT->PORTN_SEL0 |= (func > 99 ? 1 : func) << (n*2);
}
else
{
PORT->PORTN_SEL1 &= ~(0x03 << ((n-16)*2));
PORT->PORTN_SEL1 |= (func > 99 ? 1 : func) << ((n-16)*2);
}
PORT->PORTN_INEN &= ~(0x01 << n);
PORT->PORTN_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTP):
if(func > 99)
{
if(n < PIN6)
{
PORT->PORTP_MUX0 &= ~(0x1F << (n*5));
PORT->PORTP_MUX0 |= (func-100) << (n*5);
}
else if(n < PIN12)
{
PORT->PORTP_MUX1 &= ~(0x1F << ((n-6)*5));
PORT->PORTP_MUX1 |= (func-100) << ((n-6)*5);
}
else if(n < PIN18)
{
PORT->PORTP_MUX2 &= ~(0x1F << ((n-12)*5));
PORT->PORTP_MUX2 |= (func-100) << ((n-12)*5);
}
else if(n < PIN24)
{
PORT->PORTP_MUX3 &= ~(0x1F << ((n-18)*5));
PORT->PORTP_MUX3 |= (func-100) << ((n-18)*5);
}
}
if(n < 16)
{
PORT->PORTP_SEL0 &= ~(0x03 << (n*2));
PORT->PORTP_SEL0 |= (func > 99 ? 1 : func) << (n*2);
}
else
{
PORT->PORTP_SEL1 &= ~(0x03 << ((n-16)*2));
PORT->PORTP_SEL1 |= (func > 99 ? 1 : func) << ((n-16)*2);
}
PORT->PORTP_INEN &= ~(0x01 << n);
PORT->PORTP_INEN |= (digit_in_en << n);
break;
}
switch ((uint32_t)PORTx)
{
case ((uint32_t)PORTA):
if (func > 99)
{
if (n < PIN6)
{
PORT->PORTA_MUX0 &= ~(0x1F << (n * 5));
PORT->PORTA_MUX0 |= (func - 100) << (n * 5);
}
else if (n < PIN12)
{
PORT->PORTA_MUX1 &= ~(0x1F << ((n - 6) * 5));
PORT->PORTA_MUX1 |= (func - 100) << ((n - 6) * 5);
}
}
PORT->PORTA_SEL &= ~(0x03 << (n * 2));
PORT->PORTA_SEL |= (func > 99 ? 1 : func) << (n * 2);
PORT->PORTA_INEN &= ~(0x01 << n);
PORT->PORTA_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTB):
if (func > 99)
{
if (n < PIN6)
{
PORT->PORTB_MUX0 &= ~(0x1F << (n * 5));
PORT->PORTB_MUX0 |= (func - 100) << (n * 5);
}
else if (n < PIN12)
{
PORT->PORTB_MUX1 &= ~(0x1F << ((n - 6) * 5));
PORT->PORTB_MUX1 |= (func - 100) << ((n - 6) * 5);
}
}
PORT->PORTB_SEL &= ~(0x03 << (n * 2));
PORT->PORTB_SEL |= (func > 99 ? 1 : func) << (n * 2);
PORT->PORTB_INEN &= ~(0x01 << n);
PORT->PORTB_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTC):
if (func > 99)
{
if (n < PIN6)
{
PORT->PORTC_MUX0 &= ~(0x1F << (n * 5));
PORT->PORTC_MUX0 |= (func - 100) << (n * 5);
}
else if (n < PIN12)
{
PORT->PORTC_MUX1 &= ~(0x1F << ((n - 6) * 5));
PORT->PORTC_MUX1 |= (func - 100) << ((n - 6) * 5);
}
}
PORT->PORTC_SEL &= ~(0x03 << (n * 2));
PORT->PORTC_SEL |= (func > 99 ? 1 : func) << (n * 2);
PORT->PORTC_INEN &= ~(0x01 << n);
PORT->PORTC_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTM):
if (func > 99)
{
if (n < PIN6)
{
PORT->PORTM_MUX0 &= ~(0x1F << (n * 5));
PORT->PORTM_MUX0 |= (func - 100) << (n * 5);
}
else if (n < PIN12)
{
PORT->PORTM_MUX1 &= ~(0x1F << ((n - 6) * 5));
PORT->PORTM_MUX1 |= (func - 100) << ((n - 6) * 5);
}
else if (n < PIN18)
{
PORT->PORTM_MUX2 &= ~(0x1F << ((n - 12) * 5));
PORT->PORTM_MUX2 |= (func - 100) << ((n - 12) * 5);
}
else if (n < PIN24)
{
PORT->PORTM_MUX3 &= ~(0x1F << ((n - 18) * 5));
PORT->PORTM_MUX3 |= (func - 100) << ((n - 18) * 5);
}
}
if (n < 16)
{
PORT->PORTM_SEL0 &= ~(0x03 << (n * 2));
PORT->PORTM_SEL0 |= (func > 99 ? 1 : func) << (n * 2);
}
else
{
PORT->PORTM_SEL1 &= ~(0x03 << ((n - 16) * 2));
PORT->PORTM_SEL1 |= (func > 99 ? 1 : func) << ((n - 16) * 2);
}
PORT->PORTM_INEN &= ~(0x01 << n);
PORT->PORTM_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTN):
if (func > 99)
{
if (n < PIN6)
{
PORT->PORTN_MUX0 &= ~(0x1F << (n * 5));
PORT->PORTN_MUX0 |= (func - 100) << (n * 5);
}
else if (n < PIN12)
{
PORT->PORTN_MUX1 &= ~(0x1F << ((n - 6) * 5));
PORT->PORTN_MUX1 |= (func - 100) << ((n - 6) * 5);
}
else if (n < PIN18)
{
PORT->PORTN_MUX2 &= ~(0x1F << ((n - 12) * 5));
PORT->PORTN_MUX2 |= (func - 100) << ((n - 12) * 5);
}
}
if (n < 16)
{
PORT->PORTN_SEL0 &= ~(0x03 << (n * 2));
PORT->PORTN_SEL0 |= (func > 99 ? 1 : func) << (n * 2);
}
else
{
PORT->PORTN_SEL1 &= ~(0x03 << ((n - 16) * 2));
PORT->PORTN_SEL1 |= (func > 99 ? 1 : func) << ((n - 16) * 2);
}
PORT->PORTN_INEN &= ~(0x01 << n);
PORT->PORTN_INEN |= (digit_in_en << n);
break;
case ((uint32_t)PORTP):
if (func > 99)
{
if (n < PIN6)
{
PORT->PORTP_MUX0 &= ~(0x1F << (n * 5));
PORT->PORTP_MUX0 |= (func - 100) << (n * 5);
}
else if (n < PIN12)
{
PORT->PORTP_MUX1 &= ~(0x1F << ((n - 6) * 5));
PORT->PORTP_MUX1 |= (func - 100) << ((n - 6) * 5);
}
else if (n < PIN18)
{
PORT->PORTP_MUX2 &= ~(0x1F << ((n - 12) * 5));
PORT->PORTP_MUX2 |= (func - 100) << ((n - 12) * 5);
}
else if (n < PIN24)
{
PORT->PORTP_MUX3 &= ~(0x1F << ((n - 18) * 5));
PORT->PORTP_MUX3 |= (func - 100) << ((n - 18) * 5);
}
}
if (n < 16)
{
PORT->PORTP_SEL0 &= ~(0x03 << (n * 2));
PORT->PORTP_SEL0 |= (func > 99 ? 1 : func) << (n * 2);
}
else
{
PORT->PORTP_SEL1 &= ~(0x03 << ((n - 16) * 2));
PORT->PORTP_SEL1 |= (func > 99 ? 1 : func) << ((n - 16) * 2);
}
PORT->PORTP_INEN &= ~(0x01 << n);
PORT->PORTP_INEN |= (digit_in_en << n);
break;
}
}

View File

@ -1,482 +1,474 @@
#ifndef __SWM320_PORT_H__
#define __SWM320_PORT_H__
void PORT_Init(uint32_t PORTx, uint32_t n, uint32_t func, uint32_t digit_in_en); //端口引脚功能选择,其可取值如下:
void PORT_Init(uint32_t PORTx, uint32_t n, uint32_t func, uint32_t digit_in_en); //端口引脚功能选择,其可取值如下:
#define PORTA 0
#define PORTB 1
#define PORTC 2
#define PORTM 3
#define PORTN 4
#define PORTP 5
#define PORTA 0
#define PORTB 1
#define PORTC 2
#define PORTM 3
#define PORTN 4
#define PORTP 5
#define PORTA_PIN0_GPIO 0
#define PORTA_PIN0_FUNMUX 1
#define PORTA_PIN0_SWCLK 2
#define PORTA_PIN0_GPIO 0
#define PORTA_PIN0_FUNMUX 1
#define PORTA_PIN0_SWCLK 2
#define PORTA_PIN1_GPIO 0
#define PORTA_PIN1_FUNMUX 1
#define PORTA_PIN1_SWDIO 2
#define PORTA_PIN1_GPIO 0
#define PORTA_PIN1_FUNMUX 1
#define PORTA_PIN1_SWDIO 2
#define PORTA_PIN2_GPIO 0
#define PORTA_PIN2_FUNMUX 1
#define PORTA_PIN2_GPIO 0
#define PORTA_PIN2_FUNMUX 1
#define PORTA_PIN3_GPIO 0
#define PORTA_PIN3_FUNMUX 1
#define PORTA_PIN3_GPIO 0
#define PORTA_PIN3_FUNMUX 1
#define PORTA_PIN4_GPIO 0
#define PORTA_PIN4_FUNMUX 1
#define PORTA_PIN4_GPIO 0
#define PORTA_PIN4_FUNMUX 1
#define PORTA_PIN5_GPIO 0
#define PORTA_PIN5_FUNMUX 1
#define PORTA_PIN5_GPIO 0
#define PORTA_PIN5_FUNMUX 1
#define PORTA_PIN6_GPIO 0
#define PORTA_PIN6_FUNMUX 1
#define PORTA_PIN6_GPIO 0
#define PORTA_PIN6_FUNMUX 1
#define PORTA_PIN7_GPIO 0
#define PORTA_PIN7_FUNMUX 1
#define PORTA_PIN7_GPIO 0
#define PORTA_PIN7_FUNMUX 1
#define PORTA_PIN8_GPIO 0
#define PORTA_PIN8_FUNMUX 1
#define PORTA_PIN8_GPIO 0
#define PORTA_PIN8_FUNMUX 1
#define PORTA_PIN9_GPIO 0
#define PORTA_PIN9_FUNMUX 1
#define PORTA_PIN9_ADC0_IN7 3
#define PORTA_PIN9_GPIO 0
#define PORTA_PIN9_FUNMUX 1
#define PORTA_PIN9_ADC0_IN7 3
#define PORTA_PIN10_GPIO 0
#define PORTA_PIN10_FUNMUX 1
#define PORTA_PIN10_ADC0_IN6 3
#define PORTA_PIN10_GPIO 0
#define PORTA_PIN10_FUNMUX 1
#define PORTA_PIN10_ADC0_IN6 3
#define PORTA_PIN11_GPIO 0
#define PORTA_PIN11_FUNMUX 1
#define PORTA_PIN11_ADC0_IN5 3
#define PORTA_PIN11_GPIO 0
#define PORTA_PIN11_FUNMUX 1
#define PORTA_PIN11_ADC0_IN5 3
#define PORTA_PIN12_GPIO 0
#define PORTA_PIN12_ADC0_IN4 3
#define PORTA_PIN12_GPIO 0
#define PORTA_PIN12_ADC0_IN4 3
#define PORTB_PIN0_GPIO 0
#define PORTB_PIN0_FUNMUX 1
#define PORTB_PIN0_SD_DETECT 2
#define PORTB_PIN0_GPIO 0
#define PORTB_PIN0_FUNMUX 1
#define PORTB_PIN0_SD_DETECT 2
#define PORTB_PIN1_GPIO 0
#define PORTB_PIN1_FUNMUX 1
#define PORTB_PIN1_SD_CLK 2
#define PORTB_PIN1_GPIO 0
#define PORTB_PIN1_FUNMUX 1
#define PORTB_PIN1_SD_CLK 2
#define PORTB_PIN2_GPIO 0
#define PORTB_PIN2_FUNMUX 1
#define PORTB_PIN2_SD_CMD 2
#define PORTB_PIN2_GPIO 0
#define PORTB_PIN2_FUNMUX 1
#define PORTB_PIN2_SD_CMD 2
#define PORTB_PIN3_GPIO 0
#define PORTB_PIN3_FUNMUX 1
#define PORTB_PIN3_SD_D0 2
#define PORTB_PIN3_GPIO 0
#define PORTB_PIN3_FUNMUX 1
#define PORTB_PIN3_SD_D0 2
#define PORTB_PIN4_GPIO 0
#define PORTB_PIN4_FUNMUX 1
#define PORTB_PIN4_SD_D1 2
#define PORTB_PIN4_GPIO 0
#define PORTB_PIN4_FUNMUX 1
#define PORTB_PIN4_SD_D1 2
#define PORTB_PIN5_GPIO 0
#define PORTB_PIN5_FUNMUX 1
#define PORTB_PIN5_SD_D2 2
#define PORTB_PIN5_GPIO 0
#define PORTB_PIN5_FUNMUX 1
#define PORTB_PIN5_SD_D2 2
#define PORTB_PIN6_GPIO 0
#define PORTB_PIN6_FUNMUX 1
#define PORTB_PIN6_SD_D3 2
#define PORTB_PIN6_GPIO 0
#define PORTB_PIN6_FUNMUX 1
#define PORTB_PIN6_SD_D3 2
#define PORTB_PIN7_GPIO 0
#define PORTB_PIN7_FUNMUX 1
#define PORTB_PIN7_SD_D4 2
#define PORTB_PIN7_GPIO 0
#define PORTB_PIN7_FUNMUX 1
#define PORTB_PIN7_SD_D4 2
#define PORTB_PIN8_GPIO 0
#define PORTB_PIN8_FUNMUX 1
#define PORTB_PIN8_SD_D5 2
#define PORTB_PIN8_GPIO 0
#define PORTB_PIN8_FUNMUX 1
#define PORTB_PIN8_SD_D5 2
#define PORTB_PIN9_GPIO 0
#define PORTB_PIN9_FUNMUX 1
#define PORTB_PIN9_SD_D6 2
#define PORTB_PIN9_GPIO 0
#define PORTB_PIN9_FUNMUX 1
#define PORTB_PIN9_SD_D6 2
#define PORTB_PIN10_GPIO 0
#define PORTB_PIN10_FUNMUX 1
#define PORTB_PIN10_SD_D7 2
#define PORTB_PIN10_GPIO 0
#define PORTB_PIN10_FUNMUX 1
#define PORTB_PIN10_SD_D7 2
#define PORTB_PIN11_GPIO 0
#define PORTB_PIN11_FUNMUX 1
#define PORTB_PIN11_GPIO 0
#define PORTB_PIN11_FUNMUX 1
#define PORTB_PIN12_GPIO 0
#define PORTB_PIN12_GPIO 0
#define PORTC_PIN0_GPIO 0
#define PORTC_PIN0_FUNMUX 1
#define PORTC_PIN1_GPIO 0
#define PORTC_PIN1_FUNMUX 1
#define PORTC_PIN0_GPIO 0
#define PORTC_PIN0_FUNMUX 1
#define PORTC_PIN2_GPIO 0
#define PORTC_PIN2_FUNMUX 1
#define PORTC_PIN1_GPIO 0
#define PORTC_PIN1_FUNMUX 1
#define PORTC_PIN3_GPIO 0
#define PORTC_PIN3_FUNMUX 1
#define PORTC_PIN2_GPIO 0
#define PORTC_PIN2_FUNMUX 1
#define PORTC_PIN4_GPIO 0
#define PORTC_PIN4_FUNMUX 1
#define PORTC_PIN4_ADC1_IN3 3
#define PORTC_PIN3_GPIO 0
#define PORTC_PIN3_FUNMUX 1
#define PORTC_PIN5_GPIO 0
#define PORTC_PIN5_FUNMUX 1
#define PORTC_PIN5_ADC1_IN2 3
#define PORTC_PIN4_GPIO 0
#define PORTC_PIN4_FUNMUX 1
#define PORTC_PIN4_ADC1_IN3 3
#define PORTC_PIN6_GPIO 0
#define PORTC_PIN6_FUNMUX 1
#define PORTC_PIN6_ADC1_IN1 3
#define PORTC_PIN5_GPIO 0
#define PORTC_PIN5_FUNMUX 1
#define PORTC_PIN5_ADC1_IN2 3
#define PORTC_PIN7_GPIO 0
#define PORTC_PIN7_FUNMUX 1
#define PORTC_PIN7_ADC1_IN0 3
#define PORTC_PIN6_GPIO 0
#define PORTC_PIN6_FUNMUX 1
#define PORTC_PIN6_ADC1_IN1 3
#define PORTM_PIN0_GPIO 0
#define PORTM_PIN0_FUNMUX 1
#define PORTM_PIN0_NORFL_D15 2
#define PORTC_PIN7_GPIO 0
#define PORTC_PIN7_FUNMUX 1
#define PORTC_PIN7_ADC1_IN0 3
#define PORTM_PIN1_GPIO 0
#define PORTM_PIN1_FUNMUX 1
#define PORTM_PIN1_NORFL_D14 2
#define PORTM_PIN2_GPIO 0
#define PORTM_PIN2_FUNMUX 1
#define PORTM_PIN2_NORFL_D13 2
#define PORTM_PIN0_GPIO 0
#define PORTM_PIN0_FUNMUX 1
#define PORTM_PIN0_NORFL_D15 2
#define PORTM_PIN3_GPIO 0
#define PORTM_PIN3_FUNMUX 1
#define PORTM_PIN3_NORFL_D12 2
#define PORTM_PIN1_GPIO 0
#define PORTM_PIN1_FUNMUX 1
#define PORTM_PIN1_NORFL_D14 2
#define PORTM_PIN4_GPIO 0
#define PORTM_PIN4_FUNMUX 1
#define PORTM_PIN4_NORFL_D11 2
#define PORTM_PIN2_GPIO 0
#define PORTM_PIN2_FUNMUX 1
#define PORTM_PIN2_NORFL_D13 2
#define PORTM_PIN5_GPIO 0
#define PORTM_PIN5_FUNMUX 1
#define PORTM_PIN5_NORFL_D10 2
#define PORTM_PIN3_GPIO 0
#define PORTM_PIN3_FUNMUX 1
#define PORTM_PIN3_NORFL_D12 2
#define PORTM_PIN6_GPIO 0
#define PORTM_PIN6_FUNMUX 1
#define PORTM_PIN6_NORFL_D9 2
#define PORTM_PIN4_GPIO 0
#define PORTM_PIN4_FUNMUX 1
#define PORTM_PIN4_NORFL_D11 2
#define PORTM_PIN7_GPIO 0
#define PORTM_PIN7_FUNMUX 1
#define PORTM_PIN7_NORFL_D8 2
#define PORTM_PIN5_GPIO 0
#define PORTM_PIN5_FUNMUX 1
#define PORTM_PIN5_NORFL_D10 2
#define PORTM_PIN8_GPIO 0
#define PORTM_PIN8_FUNMUX 1
#define PORTM_PIN8_NORFL_D7 2
#define PORTM_PIN6_GPIO 0
#define PORTM_PIN6_FUNMUX 1
#define PORTM_PIN6_NORFL_D9 2
#define PORTM_PIN9_GPIO 0
#define PORTM_PIN9_FUNMUX 1
#define PORTM_PIN9_NORFL_D6 2
#define PORTM_PIN7_GPIO 0
#define PORTM_PIN7_FUNMUX 1
#define PORTM_PIN7_NORFL_D8 2
#define PORTM_PIN10_GPIO 0
#define PORTM_PIN10_FUNMUX 1
#define PORTM_PIN10_NORFL_D5 2
#define PORTM_PIN8_GPIO 0
#define PORTM_PIN8_FUNMUX 1
#define PORTM_PIN8_NORFL_D7 2
#define PORTM_PIN11_GPIO 0
#define PORTM_PIN11_FUNMUX 1
#define PORTM_PIN11_NORFL_D4 2
#define PORTM_PIN9_GPIO 0
#define PORTM_PIN9_FUNMUX 1
#define PORTM_PIN9_NORFL_D6 2
#define PORTM_PIN12_GPIO 0
#define PORTM_PIN12_FUNMUX 1
#define PORTM_PIN12_NORFL_D3 2
#define PORTM_PIN10_GPIO 0
#define PORTM_PIN10_FUNMUX 1
#define PORTM_PIN10_NORFL_D5 2
#define PORTM_PIN13_GPIO 0
#define PORTM_PIN13_FUNMUX 1
#define PORTM_PIN13_NORFL_D2 2
#define PORTM_PIN11_GPIO 0
#define PORTM_PIN11_FUNMUX 1
#define PORTM_PIN11_NORFL_D4 2
#define PORTM_PIN14_GPIO 0
#define PORTM_PIN14_FUNMUX 1
#define PORTM_PIN14_NORFL_D1 2
#define PORTM_PIN12_GPIO 0
#define PORTM_PIN12_FUNMUX 1
#define PORTM_PIN12_NORFL_D3 2
#define PORTM_PIN15_GPIO 0
#define PORTM_PIN15_FUNMUX 1
#define PORTM_PIN15_NORFL_D0 2
#define PORTM_PIN13_GPIO 0
#define PORTM_PIN13_FUNMUX 1
#define PORTM_PIN13_NORFL_D2 2
#define PORTM_PIN14_GPIO 0
#define PORTM_PIN14_FUNMUX 1
#define PORTM_PIN14_NORFL_D1 2
#define PORTM_PIN15_GPIO 0
#define PORTM_PIN15_FUNMUX 1
#define PORTM_PIN15_NORFL_D0 2
#define PORTM_PIN16_GPIO 0
#define PORTM_PIN16_FUNMUX 1
#define PORTM_PIN16_NORFL_OEN 2
#define PORTM_PIN17_GPIO 0
#define PORTM_PIN17_FUNMUX 1
#define PORTM_PIN17_NORFL_WEN 2
#define PORTM_PIN18_GPIO 0
#define PORTM_PIN18_FUNMUX 1
#define PORTM_PIN18_NORFL_CSN 2
#define PORTM_PIN19_GPIO 0
#define PORTM_PIN19_FUNMUX 1
#define PORTM_PIN19_SDRAM_CSN 2
#define PORTM_PIN20_GPIO 0
#define PORTM_PIN20_FUNMUX 1
#define PORTM_PIN20_SRAM_CSN 2
#define PORTM_PIN21_GPIO 0
#define PORTM_PIN21_FUNMUX 1
#define PORTM_PIN21_SDRAM_CKE 2
#define PORTN_PIN0_GPIO 0
#define PORTN_PIN0_FUNMUX 1
#define PORTN_PIN0_LCD_D0 2
#define PORTN_PIN0_ADC1_IN4 3
#define PORTN_PIN1_GPIO 0
#define PORTN_PIN1_FUNMUX 1
#define PORTN_PIN1_LCD_D1 2
#define PORTN_PIN1_ADC1_IN5 3
#define PORTN_PIN2_GPIO 0
#define PORTN_PIN2_FUNMUX 1
#define PORTN_PIN2_LCD_D2 2
#define PORTN_PIN2_ADC1_IN6 3
#define PORTN_PIN3_GPIO 0
#define PORTN_PIN3_FUNMUX 1
#define PORTN_PIN3_LCD_D3 2
#define PORTN_PIN4_GPIO 0
#define PORTN_PIN4_FUNMUX 1
#define PORTN_PIN4_LCD_D4 2
#define PORTN_PIN5_GPIO 0
#define PORTN_PIN5_FUNMUX 1
#define PORTN_PIN5_LCD_D5 2
#define PORTN_PIN6_GPIO 0
#define PORTN_PIN6_FUNMUX 1
#define PORTN_PIN6_LCD_D6 2
#define PORTN_PIN7_GPIO 0
#define PORTN_PIN7_FUNMUX 1
#define PORTN_PIN7_LCD_D7 2
#define PORTN_PIN8_GPIO 0
#define PORTN_PIN8_FUNMUX 1
#define PORTN_PIN8_LCD_D8 2
#define PORTN_PIN9_GPIO 0
#define PORTN_PIN9_FUNMUX 1
#define PORTN_PIN9_LCD_D9 2
#define PORTN_PIN10_GPIO 0
#define PORTN_PIN10_FUNMUX 1
#define PORTN_PIN10_LCD_D10 2
#define PORTN_PIN11_GPIO 0
#define PORTN_PIN11_FUNMUX 1
#define PORTN_PIN11_LCD_D11 2
#define PORTN_PIN12_GPIO 0
#define PORTN_PIN12_FUNMUX 1
#define PORTN_PIN12_LCD_D12 2
#define PORTN_PIN13_GPIO 0
#define PORTN_PIN13_FUNMUX 1
#define PORTN_PIN13_LCD_D13 2
#define PORTN_PIN14_GPIO 0
#define PORTN_PIN14_FUNMUX 1
#define PORTN_PIN14_LCD_D14 2
#define PORTN_PIN15_GPIO 0
#define PORTN_PIN15_FUNMUX 1
#define PORTN_PIN15_LCD_D15 2
#define PORTN_PIN16_GPIO 0
#define PORTN_PIN16_FUNMUX 1
#define PORTN_PIN16_LCD_RD 2
#define PORTN_PIN16_LCD_DOTCK 2
#define PORTN_PIN17_GPIO 0
#define PORTN_PIN17_FUNMUX 1
#define PORTN_PIN17_LCD_CS 2
#define PORTN_PIN17_LCD_VSYNC 2
#define PORTN_PIN18_GPIO 0
#define PORTN_PIN18_LCD_RS 2
#define PORTN_PIN18_LCD_DATEN 2 //Data Enable
#define PORTN_PIN19_GPIO 0
#define PORTN_PIN19_LCD_WR 2
#define PORTN_PIN19_LCD_HSYNC 2
#define PORTP_PIN0_GPIO 0
#define PORTP_PIN0_FUNMUX 1
#define PORTP_PIN0_NORFL_A0 2
#define PORTP_PIN1_GPIO 0
#define PORTP_PIN1_FUNMUX 1
#define PORTP_PIN1_NORFL_A1 2
#define PORTP_PIN2_GPIO 0
#define PORTP_PIN2_FUNMUX 1
#define PORTP_PIN2_NORFL_A2 2
#define PORTP_PIN2_SD_D7 3
#define PORTP_PIN3_GPIO 0
#define PORTP_PIN3_FUNMUX 1
#define PORTP_PIN3_NORFL_A3 2
#define PORTP_PIN3_SD_D6 3
#define PORTP_PIN4_GPIO 0
#define PORTP_PIN4_FUNMUX 1
#define PORTP_PIN4_NORFL_A4 2
#define PORTP_PIN4_SD_D5 3
#define PORTP_PIN5_GPIO 0
#define PORTP_PIN5_FUNMUX 1
#define PORTP_PIN5_NORFL_A5 2
#define PORTP_PIN5_SD_D4 3
#define PORTP_PIN6_GPIO 0
#define PORTP_PIN6_FUNMUX 1
#define PORTP_PIN6_NORFL_A6 2
#define PORTP_PIN6_SD_D3 3
#define PORTP_PIN7_GPIO 0
#define PORTP_PIN7_FUNMUX 1
#define PORTP_PIN7_NORFL_A7 2
#define PORTP_PIN7_SD_D2 3
#define PORTP_PIN8_GPIO 0
#define PORTP_PIN8_FUNMUX 1
#define PORTP_PIN8_NORFL_A8 2
#define PORTP_PIN8_SD_D1 3
#define PORTP_PIN9_GPIO 0
#define PORTP_PIN9_FUNMUX 1
#define PORTP_PIN9_NORFL_A9 2
#define PORTP_PIN9_SD_D0 3
#define PORTP_PIN10_GPIO 0
#define PORTP_PIN10_FUNMUX 1
#define PORTP_PIN10_NORFL_A10 2
#define PORTP_PIN10_SD_CMD 3
#define PORTP_PIN11_GPIO 0
#define PORTP_PIN11_FUNMUX 1
#define PORTP_PIN11_NORFL_A11 2
#define PORTP_PIN11_SD_CLK 3
#define PORTP_PIN12_GPIO 0
#define PORTP_PIN12_FUNMUX 1
#define PORTP_PIN12_NORFL_A12 2
#define PORTP_PIN12_SD_DETECT 3
#define PORTP_PIN13_GPIO 0
#define PORTP_PIN13_FUNMUX 1
#define PORTP_PIN13_NORFL_A13 2
#define PORTP_PIN13_SDRAM_CLK 2
#define PORTP_PIN14_GPIO 0
#define PORTP_PIN14_FUNMUX 1
#define PORTP_PIN14_NORFL_A14 2
#define PORTP_PIN14_SDRAM_CAS 2
#define PORTP_PIN15_GPIO 0
#define PORTP_PIN15_FUNMUX 1
#define PORTP_PIN15_NORFL_A15 2
#define PORTP_PIN15_SDRAM_RAS 2
#define PORTP_PIN16_GPIO 0
#define PORTP_PIN16_FUNMUX 1
#define PORTP_PIN16_NORFL_A16 2
#define PORTP_PIN16_SDRAM_LDQ 2
#define PORTP_PIN17_GPIO 0
#define PORTP_PIN17_FUNMUX 1
#define PORTP_PIN17_NORFL_A17 2
#define PORTP_PIN17_SDRAM_UDQ 2
#define PORTP_PIN18_GPIO 0
#define PORTP_PIN18_FUNMUX 1
#define PORTP_PIN18_NORFL_A18 2
#define PORTP_PIN19_GPIO 0
#define PORTP_PIN19_FUNMUX 1
#define PORTP_PIN19_NORFL_A19 2
#define PORTP_PIN20_GPIO 0
#define PORTP_PIN20_FUNMUX 1
#define PORTP_PIN20_NORFL_A20 2
#define PORTP_PIN20_SDRAM_BA0 2
#define PORTP_PIN21_GPIO 0
#define PORTP_PIN21_FUNMUX 1
#define PORTP_PIN21_NORFL_A21 2
#define PORTP_PIN21_SDRAM_BA1 2
#define PORTP_PIN22_GPIO 0
#define PORTP_PIN22_FUNMUX 1
#define PORTP_PIN22_NORFL_A22 2
#define PORTP_PIN23_GPIO 0
#define PORTP_PIN23_FUNMUX 1
#define PORTP_PIN23_NORFL_A23 2
#define PORTM_PIN16_GPIO 0
#define PORTM_PIN16_FUNMUX 1
#define PORTM_PIN16_NORFL_OEN 2
#define PORTM_PIN17_GPIO 0
#define PORTM_PIN17_FUNMUX 1
#define PORTM_PIN17_NORFL_WEN 2
#define PORTM_PIN18_GPIO 0
#define PORTM_PIN18_FUNMUX 1
#define PORTM_PIN18_NORFL_CSN 2
#define PORTM_PIN19_GPIO 0
#define PORTM_PIN19_FUNMUX 1
#define PORTM_PIN19_SDRAM_CSN 2
#define PORTM_PIN20_GPIO 0
#define PORTM_PIN20_FUNMUX 1
#define PORTM_PIN20_SRAM_CSN 2
#define PORTM_PIN21_GPIO 0
#define PORTM_PIN21_FUNMUX 1
#define PORTM_PIN21_SDRAM_CKE 2
#define PORTN_PIN0_GPIO 0
#define PORTN_PIN0_FUNMUX 1
#define PORTN_PIN0_LCD_D0 2
#define PORTN_PIN0_ADC1_IN4 3
#define PORTN_PIN1_GPIO 0
#define PORTN_PIN1_FUNMUX 1
#define PORTN_PIN1_LCD_D1 2
#define PORTN_PIN1_ADC1_IN5 3
#define PORTN_PIN2_GPIO 0
#define PORTN_PIN2_FUNMUX 1
#define PORTN_PIN2_LCD_D2 2
#define PORTN_PIN2_ADC1_IN6 3
#define PORTN_PIN3_GPIO 0
#define PORTN_PIN3_FUNMUX 1
#define PORTN_PIN3_LCD_D3 2
#define PORTN_PIN4_GPIO 0
#define PORTN_PIN4_FUNMUX 1
#define PORTN_PIN4_LCD_D4 2
#define PORTN_PIN5_GPIO 0
#define PORTN_PIN5_FUNMUX 1
#define PORTN_PIN5_LCD_D5 2
#define PORTN_PIN6_GPIO 0
#define PORTN_PIN6_FUNMUX 1
#define PORTN_PIN6_LCD_D6 2
#define PORTN_PIN7_GPIO 0
#define PORTN_PIN7_FUNMUX 1
#define PORTN_PIN7_LCD_D7 2
#define PORTN_PIN8_GPIO 0
#define PORTN_PIN8_FUNMUX 1
#define PORTN_PIN8_LCD_D8 2
#define PORTN_PIN9_GPIO 0
#define PORTN_PIN9_FUNMUX 1
#define PORTN_PIN9_LCD_D9 2
#define PORTN_PIN10_GPIO 0
#define PORTN_PIN10_FUNMUX 1
#define PORTN_PIN10_LCD_D10 2
#define PORTN_PIN11_GPIO 0
#define PORTN_PIN11_FUNMUX 1
#define PORTN_PIN11_LCD_D11 2
#define PORTN_PIN12_GPIO 0
#define PORTN_PIN12_FUNMUX 1
#define PORTN_PIN12_LCD_D12 2
#define PORTN_PIN13_GPIO 0
#define PORTN_PIN13_FUNMUX 1
#define PORTN_PIN13_LCD_D13 2
#define PORTN_PIN14_GPIO 0
#define PORTN_PIN14_FUNMUX 1
#define PORTN_PIN14_LCD_D14 2
#define PORTN_PIN15_GPIO 0
#define PORTN_PIN15_FUNMUX 1
#define PORTN_PIN15_LCD_D15 2
#define PORTN_PIN16_GPIO 0
#define PORTN_PIN16_FUNMUX 1
#define PORTN_PIN16_LCD_RD 2
#define PORTN_PIN16_LCD_DOTCK 2
#define PORTN_PIN17_GPIO 0
#define PORTN_PIN17_FUNMUX 1
#define PORTN_PIN17_LCD_CS 2
#define PORTN_PIN17_LCD_VSYNC 2
#define PORTN_PIN18_GPIO 0
#define PORTN_PIN18_LCD_RS 2
#define PORTN_PIN18_LCD_DATEN 2 //Data Enable
#define PORTN_PIN19_GPIO 0
#define PORTN_PIN19_LCD_WR 2
#define PORTN_PIN19_LCD_HSYNC 2
#define PORTP_PIN0_GPIO 0
#define PORTP_PIN0_FUNMUX 1
#define PORTP_PIN0_NORFL_A0 2
#define PORTP_PIN1_GPIO 0
#define PORTP_PIN1_FUNMUX 1
#define PORTP_PIN1_NORFL_A1 2
#define PORTP_PIN2_GPIO 0
#define PORTP_PIN2_FUNMUX 1
#define PORTP_PIN2_NORFL_A2 2
#define PORTP_PIN2_SD_D7 3
#define PORTP_PIN3_GPIO 0
#define PORTP_PIN3_FUNMUX 1
#define PORTP_PIN3_NORFL_A3 2
#define PORTP_PIN3_SD_D6 3
#define PORTP_PIN4_GPIO 0
#define PORTP_PIN4_FUNMUX 1
#define PORTP_PIN4_NORFL_A4 2
#define PORTP_PIN4_SD_D5 3
#define PORTP_PIN5_GPIO 0
#define PORTP_PIN5_FUNMUX 1
#define PORTP_PIN5_NORFL_A5 2
#define PORTP_PIN5_SD_D4 3
#define PORTP_PIN6_GPIO 0
#define PORTP_PIN6_FUNMUX 1
#define PORTP_PIN6_NORFL_A6 2
#define PORTP_PIN6_SD_D3 3
#define PORTP_PIN7_GPIO 0
#define PORTP_PIN7_FUNMUX 1
#define PORTP_PIN7_NORFL_A7 2
#define PORTP_PIN7_SD_D2 3
#define PORTP_PIN8_GPIO 0
#define PORTP_PIN8_FUNMUX 1
#define PORTP_PIN8_NORFL_A8 2
#define PORTP_PIN8_SD_D1 3
#define PORTP_PIN9_GPIO 0
#define PORTP_PIN9_FUNMUX 1
#define PORTP_PIN9_NORFL_A9 2
#define PORTP_PIN9_SD_D0 3
#define PORTP_PIN10_GPIO 0
#define PORTP_PIN10_FUNMUX 1
#define PORTP_PIN10_NORFL_A10 2
#define PORTP_PIN10_SD_CMD 3
#define PORTP_PIN11_GPIO 0
#define PORTP_PIN11_FUNMUX 1
#define PORTP_PIN11_NORFL_A11 2
#define PORTP_PIN11_SD_CLK 3
#define PORTP_PIN12_GPIO 0
#define PORTP_PIN12_FUNMUX 1
#define PORTP_PIN12_NORFL_A12 2
#define PORTP_PIN12_SD_DETECT 3
#define PORTP_PIN13_GPIO 0
#define PORTP_PIN13_FUNMUX 1
#define PORTP_PIN13_NORFL_A13 2
#define PORTP_PIN13_SDRAM_CLK 2
#define PORTP_PIN14_GPIO 0
#define PORTP_PIN14_FUNMUX 1
#define PORTP_PIN14_NORFL_A14 2
#define PORTP_PIN14_SDRAM_CAS 2
#define PORTP_PIN15_GPIO 0
#define PORTP_PIN15_FUNMUX 1
#define PORTP_PIN15_NORFL_A15 2
#define PORTP_PIN15_SDRAM_RAS 2
#define PORTP_PIN16_GPIO 0
#define PORTP_PIN16_FUNMUX 1
#define PORTP_PIN16_NORFL_A16 2
#define PORTP_PIN16_SDRAM_LDQ 2
#define PORTP_PIN17_GPIO 0
#define PORTP_PIN17_FUNMUX 1
#define PORTP_PIN17_NORFL_A17 2
#define PORTP_PIN17_SDRAM_UDQ 2
#define PORTP_PIN18_GPIO 0
#define PORTP_PIN18_FUNMUX 1
#define PORTP_PIN18_NORFL_A18 2
#define PORTP_PIN19_GPIO 0
#define PORTP_PIN19_FUNMUX 1
#define PORTP_PIN19_NORFL_A19 2
#define PORTP_PIN20_GPIO 0
#define PORTP_PIN20_FUNMUX 1
#define PORTP_PIN20_NORFL_A20 2
#define PORTP_PIN20_SDRAM_BA0 2
#define PORTP_PIN21_GPIO 0
#define PORTP_PIN21_FUNMUX 1
#define PORTP_PIN21_NORFL_A21 2
#define PORTP_PIN21_SDRAM_BA1 2
#define PORTP_PIN22_GPIO 0
#define PORTP_PIN22_FUNMUX 1
#define PORTP_PIN22_NORFL_A22 2
#define PORTP_PIN23_GPIO 0
#define PORTP_PIN23_FUNMUX 1
#define PORTP_PIN23_NORFL_A23 2
/* 下面宏定义的取值全部在正确值的基础上“加100”以区分上面宏定义的值从而方便库函数的编写*/
/* 下面这些值是偶数编号引脚的功能取值如PIN0、PIN2、... */
#define FUNMUX0_UART0_RXD 100
#define FUNMUX0_UART1_RXD 101
#define FUNMUX0_UART2_RXD 102
#define FUNMUX0_UART3_RXD 103
#define FUNMUX0_I2C0_SCL 105
#define FUNMUX0_I2C1_SCL 106
#define FUNMUX0_PWM0A_OUT 107
#define FUNMUX0_PWM2A_OUT 108
#define FUNMUX0_PWM4A_OUT 109
#define FUNMUX0_PWM0B_OUT 110
#define FUNMUX0_PWM2B_OUT 111
#define FUNMUX0_PWM4B_OUT 112
#define FUNMUX0_PWM_BREAK 113
#define FUNMUX0_TIMR0_IN 114
#define FUNMUX0_TIMR2_IN 115
#define FUNMUX0_CAN_RX 116
#define FUNMUX0_SPI0_SSEL 117
#define FUNMUX0_SPI0_MOSI 118
#define FUNMUX0_SPI1_SSEL 119
#define FUNMUX0_SPI1_MOSI 120
#define FUNMUX0_UART0_CTS 121
#define FUNMUX0_UART1_CTS 122
#define FUNMUX0_UART2_CTS 123
#define FUNMUX0_UART3_CTS 124
#define FUNMUX0_UART0_RXD 100
#define FUNMUX0_UART1_RXD 101
#define FUNMUX0_UART2_RXD 102
#define FUNMUX0_UART3_RXD 103
#define FUNMUX0_I2C0_SCL 105
#define FUNMUX0_I2C1_SCL 106
#define FUNMUX0_PWM0A_OUT 107
#define FUNMUX0_PWM2A_OUT 108
#define FUNMUX0_PWM4A_OUT 109
#define FUNMUX0_PWM0B_OUT 110
#define FUNMUX0_PWM2B_OUT 111
#define FUNMUX0_PWM4B_OUT 112
#define FUNMUX0_PWM_BREAK 113
#define FUNMUX0_TIMR0_IN 114
#define FUNMUX0_TIMR2_IN 115
#define FUNMUX0_CAN_RX 116
#define FUNMUX0_SPI0_SSEL 117
#define FUNMUX0_SPI0_MOSI 118
#define FUNMUX0_SPI1_SSEL 119
#define FUNMUX0_SPI1_MOSI 120
#define FUNMUX0_UART0_CTS 121
#define FUNMUX0_UART1_CTS 122
#define FUNMUX0_UART2_CTS 123
#define FUNMUX0_UART3_CTS 124
/* 下面这些值是奇数编号引脚的功能取值如PIN1、PIN3、... */
#define FUNMUX1_UART0_TXD 100
#define FUNMUX1_UART1_TXD 101
#define FUNMUX1_UART2_TXD 102
#define FUNMUX1_UART3_TXD 103
#define FUNMUX1_I2C0_SDA 105
#define FUNMUX1_I2C1_SDA 106
#define FUNMUX1_PWM1A_OUT 107
#define FUNMUX1_PWM3A_OUT 108
#define FUNMUX1_PWM5A_OUT 109
#define FUNMUX1_PWM1B_OUT 110
#define FUNMUX1_PWM3B_OUT 111
#define FUNMUX1_PWM5B_OUT 112
#define FUNMUX1_PULSE_IN 113
#define FUNMUX1_TIMR1_IN 114
#define FUNMUX1_TIMR3_IN 115
#define FUNMUX1_CAN_TX 116
#define FUNMUX1_SPI0_SCLK 117
#define FUNMUX1_SPI0_MISO 118
#define FUNMUX1_SPI1_SCLK 119
#define FUNMUX1_SPI1_MISO 120
#define FUNMUX1_UART0_RTS 121
#define FUNMUX1_UART1_RTS 122
#define FUNMUX1_UART2_RTS 123
#define FUNMUX1_UART3_RTS 124
#define FUNMUX1_UART0_TXD 100
#define FUNMUX1_UART1_TXD 101
#define FUNMUX1_UART2_TXD 102
#define FUNMUX1_UART3_TXD 103
#define FUNMUX1_I2C0_SDA 105
#define FUNMUX1_I2C1_SDA 106
#define FUNMUX1_PWM1A_OUT 107
#define FUNMUX1_PWM3A_OUT 108
#define FUNMUX1_PWM5A_OUT 109
#define FUNMUX1_PWM1B_OUT 110
#define FUNMUX1_PWM3B_OUT 111
#define FUNMUX1_PWM5B_OUT 112
#define FUNMUX1_PULSE_IN 113
#define FUNMUX1_TIMR1_IN 114
#define FUNMUX1_TIMR3_IN 115
#define FUNMUX1_CAN_TX 116
#define FUNMUX1_SPI0_SCLK 117
#define FUNMUX1_SPI0_MISO 118
#define FUNMUX1_SPI1_SCLK 119
#define FUNMUX1_SPI1_MISO 120
#define FUNMUX1_UART0_RTS 121
#define FUNMUX1_UART1_RTS 122
#define FUNMUX1_UART2_RTS 123
#define FUNMUX1_UART3_RTS 124
#endif //__SWM320_PORT_H__

File diff suppressed because it is too large Load Diff

View File

@ -1,58 +1,57 @@
#ifndef __SWM320_PWM_H__
#define __SWM320_PWM_H__
#define __SWM320_PWM_H__
typedef struct {
uint8_t clk_div; //PWM_CLKDIV_1、PWM_CLKDIV_8
uint8_t mode; //PWM_MODE_INDEP、PWM_MODE_COMPL、PWM_MODE_INDEP_CALIGN、PWM_MODE_COMPL_CALIGN
uint16_t cycleA; //A路周期
uint16_t hdutyA; //A路占空比
uint16_t deadzoneA; //A路死区时长取值0--1023
uint8_t initLevelA; //A路初始输出电平0 低电平 1 高电平
uint16_t cycleB; //B路周期
uint16_t hdutyB; //B路占空比
uint16_t deadzoneB; //B路死区时长取值0--1023
uint8_t initLevelB; //B路初始输出电平0 低电平 1 高电平
uint8_t HEndAIEn; //A路高电平结束中断使能
uint8_t NCycleAIEn; //A路新周期开始中断使能
uint8_t HEndBIEn; //B路高电平结束中断使能
uint8_t NCycleBIEn; //B路新周期开始中断使能
typedef struct
{
uint8_t clk_div; //PWM_CLKDIV_1、PWM_CLKDIV_8
uint8_t mode; //PWM_MODE_INDEP、PWM_MODE_COMPL、PWM_MODE_INDEP_CALIGN、PWM_MODE_COMPL_CALIGN
uint16_t cycleA; //A路周期
uint16_t hdutyA; //A路占空比
uint16_t deadzoneA; //A路死区时长取值0--1023
uint8_t initLevelA; //A路初始输出电平0 低电平 1 高电平
uint16_t cycleB; //B路周期
uint16_t hdutyB; //B路占空比
uint16_t deadzoneB; //B路死区时长取值0--1023
uint8_t initLevelB; //B路初始输出电平0 低电平 1 高电平
uint8_t HEndAIEn; //A路高电平结束中断使能
uint8_t NCycleAIEn; //A路新周期开始中断使能
uint8_t HEndBIEn; //B路高电平结束中断使能
uint8_t NCycleBIEn; //B路新周期开始中断使能
} PWM_InitStructure;
#define PWM_CLKDIV_1 0
#define PWM_CLKDIV_8 1
#define PWM_CLKDIV_1 0
#define PWM_CLKDIV_8 1
#define PWM_MODE_INDEP 0 //A路和B路为两路独立输出
#define PWM_MODE_COMPL 1 //A路和B路为一路互补输出
#define PWM_MODE_INDEP_CALIGN 3 //A路和B路为两路独立输出中心对齐
#define PWM_MODE_COMPL_CALIGN 4 //A路和B路为一路互补输出中心对齐
#define PWM_MODE_INDEP 0 //A路和B路为两路独立输出
#define PWM_MODE_COMPL 1 //A路和B路为一路互补输出
#define PWM_MODE_INDEP_CALIGN 3 //A路和B路为两路独立输出中心对齐
#define PWM_MODE_COMPL_CALIGN 4 //A路和B路为一路互补输出中心对齐
#define PWM_CH_A 0
#define PWM_CH_B 1
#define PWM_CH_A 0
#define PWM_CH_B 1
void PWM_Init(PWM_TypeDef *PWMx, PWM_InitStructure *initStruct); //PWM初始化
void PWM_Start(PWM_TypeDef *PWMx, uint32_t chA, uint32_t chB); //启动PWM开始PWM输出
void PWM_Stop(PWM_TypeDef *PWMx, uint32_t chA, uint32_t chB); //关闭PWM停止PWM输出
void PWM_Init(PWM_TypeDef * PWMx, PWM_InitStructure * initStruct); //PWM初始化
void PWM_Start(PWM_TypeDef * PWMx, uint32_t chA, uint32_t chB); //启动PWM开始PWM输出
void PWM_Stop(PWM_TypeDef * PWMx, uint32_t chA, uint32_t chB); //关闭PWM停止PWM输出
void PWM_SetCycle(PWM_TypeDef * PWMx, uint32_t chn, uint16_t cycle); //设置周期
uint16_t PWM_GetCycle(PWM_TypeDef * PWMx, uint32_t chn); //获取周期
void PWM_SetHDuty(PWM_TypeDef * PWMx, uint32_t chn, uint16_t hduty); //设置高电平时长
uint16_t PWM_GetHDuty(PWM_TypeDef * PWMx, uint32_t chn); //获取高电平时长
void PWM_SetDeadzone(PWM_TypeDef * PWMx, uint32_t chn, uint8_t deadzone); //设置死区时长
uint8_t PWM_GetDeadzone(PWM_TypeDef * PWMx, uint32_t chn); //获取死区时长
void PWM_IntNCycleEn(PWM_TypeDef * PWMx, uint32_t chn); //新周期开始中断使能
void PWM_IntNCycleDis(PWM_TypeDef * PWMx, uint32_t chn); //新周期开始中断禁能
void PWM_IntNCycleClr(PWM_TypeDef * PWMx, uint32_t chn); //新周期开始中断标志清除
uint32_t PWM_IntNCycleStat(PWM_TypeDef * PWMx, uint32_t chn); //新周期开始中断是否发生
void PWM_IntHEndEn(PWM_TypeDef * PWMx, uint32_t chn); //高电平结束中断使能
void PWM_IntHEndDis(PWM_TypeDef * PWMx, uint32_t chn); //高电平结束中断禁能
void PWM_IntHEndClr(PWM_TypeDef * PWMx, uint32_t chn); //高电平结束中断标志清除
uint32_t PWM_IntHEndStat(PWM_TypeDef * PWMx, uint32_t chn); //高电平结束中断是否发生
void PWM_SetCycle(PWM_TypeDef *PWMx, uint32_t chn, uint16_t cycle); //设置周期
uint16_t PWM_GetCycle(PWM_TypeDef *PWMx, uint32_t chn); //获取周期
void PWM_SetHDuty(PWM_TypeDef *PWMx, uint32_t chn, uint16_t hduty); //设置高电平时长
uint16_t PWM_GetHDuty(PWM_TypeDef *PWMx, uint32_t chn); //获取高电平时长
void PWM_SetDeadzone(PWM_TypeDef *PWMx, uint32_t chn, uint8_t deadzone); //设置死区时长
uint8_t PWM_GetDeadzone(PWM_TypeDef *PWMx, uint32_t chn); //获取死区时长
void PWM_IntNCycleEn(PWM_TypeDef *PWMx, uint32_t chn); //新周期开始中断使能
void PWM_IntNCycleDis(PWM_TypeDef *PWMx, uint32_t chn); //新周期开始中断禁能
void PWM_IntNCycleClr(PWM_TypeDef *PWMx, uint32_t chn); //新周期开始中断标志清除
uint32_t PWM_IntNCycleStat(PWM_TypeDef *PWMx, uint32_t chn); //新周期开始中断是否发生
void PWM_IntHEndEn(PWM_TypeDef *PWMx, uint32_t chn); //高电平结束中断使能
void PWM_IntHEndDis(PWM_TypeDef *PWMx, uint32_t chn); //高电平结束中断禁能
void PWM_IntHEndClr(PWM_TypeDef *PWMx, uint32_t chn); //高电平结束中断标志清除
uint32_t PWM_IntHEndStat(PWM_TypeDef *PWMx, uint32_t chn); //高电平结束中断是否发生
#endif //__SWM320_PWM_H__

View File

@ -21,7 +21,6 @@
#include "SWM320.h"
#include "SWM320_rtc.h"
static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date);
/******************************************************************************************************************************************
* : RTC_Init()
@ -31,44 +30,45 @@ static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date);
* :
* :
******************************************************************************************************************************************/
void RTC_Init(RTC_TypeDef * RTCx, RTC_InitStructure * initStruct)
{
SYS->CLKEN |= (1 << SYS_CLKEN_RTCBKP_Pos);
SYS->LRCCR &= ~(1 << SYS_LRCCR_OFF_Pos); //RTC使用32KHz RC时钟
SYS->CLKEN |= (1 << SYS_CLKEN_RTC_Pos) |
((uint32_t)1 << SYS_CLKEN_ALIVE_Pos);
RTC_Stop(RTCx);
while(RTCx->CFGABLE == 0);
RTCx->MINSEC = (initStruct->Second << RTC_MINSEC_SEC_Pos) |
(initStruct->Minute << RTC_MINSEC_MIN_Pos);
RTCx->DATHUR = (initStruct->Hour << RTC_DATHUR_HOUR_Pos) |
(initStruct->Date << RTC_DATHUR_DATE_Pos);
RTCx->MONDAY = (calcWeekDay(initStruct->Year, initStruct->Month, initStruct->Date) << RTC_MONDAY_DAY_Pos) |
(initStruct->Month << RTC_MONDAY_MON_Pos);
RTCx->YEAR = initStruct->Year - 1901;
RTCx->LOAD = 1 << RTC_LOAD_TIME_Pos;
RTCx->IF = 0x1F;
RTCx->IE = (initStruct->SecondIEn << RTC_IE_SEC_Pos) |
(initStruct->MinuteIEn << RTC_IE_MIN_Pos);
if(initStruct->SecondIEn | initStruct->MinuteIEn)
{
NVIC_EnableIRQ(RTC_IRQn);
}
else
{
NVIC_DisableIRQ(RTC_IRQn);
}
void RTC_Init(RTC_TypeDef *RTCx, RTC_InitStructure *initStruct)
{
SYS->CLKEN |= (1 << SYS_CLKEN_RTCBKP_Pos);
SYS->LRCCR &= ~(1 << SYS_LRCCR_OFF_Pos); //RTC使用32KHz RC时钟
SYS->CLKEN |= (1 << SYS_CLKEN_RTC_Pos) |
((uint32_t)1 << SYS_CLKEN_ALIVE_Pos);
RTC_Stop(RTCx);
while (RTCx->CFGABLE == 0)
;
RTCx->MINSEC = (initStruct->Second << RTC_MINSEC_SEC_Pos) |
(initStruct->Minute << RTC_MINSEC_MIN_Pos);
RTCx->DATHUR = (initStruct->Hour << RTC_DATHUR_HOUR_Pos) |
(initStruct->Date << RTC_DATHUR_DATE_Pos);
RTCx->MONDAY = (calcWeekDay(initStruct->Year, initStruct->Month, initStruct->Date) << RTC_MONDAY_DAY_Pos) |
(initStruct->Month << RTC_MONDAY_MON_Pos);
RTCx->YEAR = initStruct->Year - 1901;
RTCx->LOAD = 1 << RTC_LOAD_TIME_Pos;
RTCx->IF = 0x1F;
RTCx->IE = (initStruct->SecondIEn << RTC_IE_SEC_Pos) |
(initStruct->MinuteIEn << RTC_IE_MIN_Pos);
if (initStruct->SecondIEn | initStruct->MinuteIEn)
{
NVIC_EnableIRQ(RTC_IRQn);
}
else
{
NVIC_DisableIRQ(RTC_IRQn);
}
}
/******************************************************************************************************************************************
@ -78,9 +78,9 @@ void RTC_Init(RTC_TypeDef * RTCx, RTC_InitStructure * initStruct)
* :
* :
******************************************************************************************************************************************/
void RTC_Start(RTC_TypeDef * RTCx)
void RTC_Start(RTC_TypeDef *RTCx)
{
RTCx->EN = 1;
RTCx->EN = 1;
}
/******************************************************************************************************************************************
@ -90,9 +90,9 @@ void RTC_Start(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_Stop(RTC_TypeDef * RTCx)
void RTC_Stop(RTC_TypeDef *RTCx)
{
RTCx->EN = 0;
RTCx->EN = 0;
}
/******************************************************************************************************************************************
@ -103,15 +103,15 @@ void RTC_Stop(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_GetDateTime(RTC_TypeDef * RTCx, RTC_DateTime * dateTime)
void RTC_GetDateTime(RTC_TypeDef *RTCx, RTC_DateTime *dateTime)
{
dateTime->Year = RTCx->YEAR + 1901;
dateTime->Month = (RTCx->MONDAY & RTC_MONDAY_MON_Msk) >> RTC_MONDAY_MON_Pos;
dateTime->Date = (RTCx->DATHUR & RTC_DATHUR_DATE_Msk) >> RTC_DATHUR_DATE_Pos;
dateTime->Day = 1 << ((RTCx->MONDAY & RTC_MONDAY_DAY_Msk) >> RTC_MONDAY_DAY_Pos);
dateTime->Hour = (RTCx->DATHUR & RTC_DATHUR_HOUR_Msk) >> RTC_DATHUR_HOUR_Pos;
dateTime->Minute = (RTCx->MINSEC & RTC_MINSEC_MIN_Msk) >> RTC_MINSEC_MIN_Pos;
dateTime->Second = (RTCx->MINSEC & RTC_MINSEC_SEC_Msk) >> RTC_MINSEC_SEC_Pos;
dateTime->Year = RTCx->YEAR + 1901;
dateTime->Month = (RTCx->MONDAY & RTC_MONDAY_MON_Msk) >> RTC_MONDAY_MON_Pos;
dateTime->Date = (RTCx->DATHUR & RTC_DATHUR_DATE_Msk) >> RTC_DATHUR_DATE_Pos;
dateTime->Day = 1 << ((RTCx->MONDAY & RTC_MONDAY_DAY_Msk) >> RTC_MONDAY_DAY_Pos);
dateTime->Hour = (RTCx->DATHUR & RTC_DATHUR_HOUR_Msk) >> RTC_DATHUR_HOUR_Pos;
dateTime->Minute = (RTCx->MINSEC & RTC_MINSEC_MIN_Msk) >> RTC_MINSEC_MIN_Pos;
dateTime->Second = (RTCx->MINSEC & RTC_MINSEC_SEC_Msk) >> RTC_MINSEC_SEC_Pos;
}
/******************************************************************************************************************************************
@ -122,24 +122,27 @@ void RTC_GetDateTime(RTC_TypeDef * RTCx, RTC_DateTime * dateTime)
* :
* :
******************************************************************************************************************************************/
void RTC_AlarmSetup(RTC_TypeDef * RTCx, RTC_AlarmStructure * alarmStruct)
void RTC_AlarmSetup(RTC_TypeDef *RTCx, RTC_AlarmStructure *alarmStruct)
{
while(RTCx->CFGABLE == 0);
RTCx->MINSECAL = (alarmStruct->Second << RTC_MINSECAL_SEC_Pos) |
(alarmStruct->Minute << RTC_MINSECAL_MIN_Pos);
RTCx->DAYHURAL = (alarmStruct->Hour << RTC_DAYHURAL_HOUR_Pos) |
(alarmStruct->Days << RTC_DAYHURAL_SUN_Pos);
RTCx->LOAD = 1 << RTC_LOAD_ALARM_Pos;
while(RTCx->LOAD & RTC_LOAD_ALARM_Msk);
RTCx->IF = (1 << RTC_IF_ALARM_Pos);
RTCx->IE &= ~RTC_IE_ALARM_Msk;
RTCx->IE |= (alarmStruct->AlarmIEn << RTC_IE_ALARM_Pos);
if(alarmStruct->AlarmIEn) NVIC_EnableIRQ(RTC_IRQn);
while (RTCx->CFGABLE == 0)
;
RTCx->MINSECAL = (alarmStruct->Second << RTC_MINSECAL_SEC_Pos) |
(alarmStruct->Minute << RTC_MINSECAL_MIN_Pos);
RTCx->DAYHURAL = (alarmStruct->Hour << RTC_DAYHURAL_HOUR_Pos) |
(alarmStruct->Days << RTC_DAYHURAL_SUN_Pos);
RTCx->LOAD = 1 << RTC_LOAD_ALARM_Pos;
while (RTCx->LOAD & RTC_LOAD_ALARM_Msk)
;
RTCx->IF = (1 << RTC_IF_ALARM_Pos);
RTCx->IE &= ~RTC_IE_ALARM_Msk;
RTCx->IE |= (alarmStruct->AlarmIEn << RTC_IE_ALARM_Pos);
if (alarmStruct->AlarmIEn)
NVIC_EnableIRQ(RTC_IRQn);
}
/******************************************************************************************************************************************
@ -155,21 +158,24 @@ static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date)
{
uint32_t i, cnt = 0;
const uint32_t daysOfMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(i = 1; i < month; i++) cnt += daysOfMonth[i];
for (i = 1; i < month; i++)
cnt += daysOfMonth[i];
cnt += date;
if((year%4 == 0) && ((year%100 != 0) || (year%400 == 0)) && (month >= 3)) cnt += 1;
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) && (month >= 3))
cnt += 1;
cnt += (year - 1901) * 365;
for(i = 1901; i < year; i++)
for (i = 1901; i < year; i++)
{
if((i%4 == 0) && ((i%100 != 0) || (i%400 == 0))) cnt += 1;
if ((i % 4 == 0) && ((i % 100 != 0) || (i % 400 == 0)))
cnt += 1;
}
return (cnt+1) % 7;
return (cnt + 1) % 7;
}
/******************************************************************************************************************************************
@ -179,9 +185,9 @@ static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date)
* :
* :
******************************************************************************************************************************************/
void RTC_IntSecondEn(RTC_TypeDef * RTCx)
{
RTCx->IE |= (1 << RTC_IE_SEC_Pos);
void RTC_IntSecondEn(RTC_TypeDef *RTCx)
{
RTCx->IE |= (1 << RTC_IE_SEC_Pos);
}
/******************************************************************************************************************************************
@ -191,9 +197,9 @@ void RTC_IntSecondEn(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntSecondDis(RTC_TypeDef * RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_SEC_Pos);
void RTC_IntSecondDis(RTC_TypeDef *RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_SEC_Pos);
}
/******************************************************************************************************************************************
@ -203,9 +209,9 @@ void RTC_IntSecondDis(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntSecondClr(RTC_TypeDef * RTCx)
{
RTCx->IF = (1 << RTC_IF_SEC_Pos);
void RTC_IntSecondClr(RTC_TypeDef *RTCx)
{
RTCx->IF = (1 << RTC_IF_SEC_Pos);
}
/******************************************************************************************************************************************
@ -215,9 +221,9 @@ void RTC_IntSecondClr(RTC_TypeDef * RTCx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t RTC_IntSecondStat(RTC_TypeDef * RTCx)
uint32_t RTC_IntSecondStat(RTC_TypeDef *RTCx)
{
return (RTCx->IF & RTC_IF_SEC_Msk) ? 1 : 0;
return (RTCx->IF & RTC_IF_SEC_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -227,9 +233,9 @@ uint32_t RTC_IntSecondStat(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntMinuteEn(RTC_TypeDef * RTCx)
{
RTCx->IE |= (1 << RTC_IE_MIN_Pos);
void RTC_IntMinuteEn(RTC_TypeDef *RTCx)
{
RTCx->IE |= (1 << RTC_IE_MIN_Pos);
}
/******************************************************************************************************************************************
@ -239,9 +245,9 @@ void RTC_IntMinuteEn(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntMinuteDis(RTC_TypeDef * RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_MIN_Pos);
void RTC_IntMinuteDis(RTC_TypeDef *RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_MIN_Pos);
}
/******************************************************************************************************************************************
@ -251,9 +257,9 @@ void RTC_IntMinuteDis(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntMinuteClr(RTC_TypeDef * RTCx)
{
RTCx->IF = (1 << RTC_IF_MIN_Pos);
void RTC_IntMinuteClr(RTC_TypeDef *RTCx)
{
RTCx->IF = (1 << RTC_IF_MIN_Pos);
}
/******************************************************************************************************************************************
@ -263,9 +269,9 @@ void RTC_IntMinuteClr(RTC_TypeDef * RTCx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t RTC_IntMinuteStat(RTC_TypeDef * RTCx)
uint32_t RTC_IntMinuteStat(RTC_TypeDef *RTCx)
{
return (RTCx->IF & RTC_IF_MIN_Msk) ? 1 : 0;
return (RTCx->IF & RTC_IF_MIN_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -275,9 +281,9 @@ uint32_t RTC_IntMinuteStat(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntHourEn(RTC_TypeDef * RTCx)
{
RTCx->IE |= (1 << RTC_IE_HOUR_Pos);
void RTC_IntHourEn(RTC_TypeDef *RTCx)
{
RTCx->IE |= (1 << RTC_IE_HOUR_Pos);
}
/******************************************************************************************************************************************
@ -287,9 +293,9 @@ void RTC_IntHourEn(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntHourDis(RTC_TypeDef * RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_HOUR_Pos);
void RTC_IntHourDis(RTC_TypeDef *RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_HOUR_Pos);
}
/******************************************************************************************************************************************
@ -299,9 +305,9 @@ void RTC_IntHourDis(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntHourClr(RTC_TypeDef * RTCx)
{
RTCx->IF = (1 << RTC_IF_HOUR_Pos);
void RTC_IntHourClr(RTC_TypeDef *RTCx)
{
RTCx->IF = (1 << RTC_IF_HOUR_Pos);
}
/******************************************************************************************************************************************
@ -311,9 +317,9 @@ void RTC_IntHourClr(RTC_TypeDef * RTCx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t RTC_IntHourStat(RTC_TypeDef * RTCx)
uint32_t RTC_IntHourStat(RTC_TypeDef *RTCx)
{
return (RTCx->IF & RTC_IF_HOUR_Msk) ? 1 : 0;
return (RTCx->IF & RTC_IF_HOUR_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -323,9 +329,9 @@ uint32_t RTC_IntHourStat(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntDateEn(RTC_TypeDef * RTCx)
{
RTCx->IE |= (1 << RTC_IE_DATE_Pos);
void RTC_IntDateEn(RTC_TypeDef *RTCx)
{
RTCx->IE |= (1 << RTC_IE_DATE_Pos);
}
/******************************************************************************************************************************************
@ -335,9 +341,9 @@ void RTC_IntDateEn(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntDateDis(RTC_TypeDef * RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_DATE_Pos);
void RTC_IntDateDis(RTC_TypeDef *RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_DATE_Pos);
}
/******************************************************************************************************************************************
@ -347,9 +353,9 @@ void RTC_IntDateDis(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntDateClr(RTC_TypeDef * RTCx)
{
RTCx->IF = (1 << RTC_IF_DATE_Pos);
void RTC_IntDateClr(RTC_TypeDef *RTCx)
{
RTCx->IF = (1 << RTC_IF_DATE_Pos);
}
/******************************************************************************************************************************************
@ -359,9 +365,9 @@ void RTC_IntDateClr(RTC_TypeDef * RTCx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t RTC_IntDateStat(RTC_TypeDef * RTCx)
uint32_t RTC_IntDateStat(RTC_TypeDef *RTCx)
{
return (RTCx->IF & RTC_IF_DATE_Msk) ? 1 : 0;
return (RTCx->IF & RTC_IF_DATE_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -371,9 +377,9 @@ uint32_t RTC_IntDateStat(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntAlarmEn(RTC_TypeDef * RTCx)
{
RTCx->IE |= (1 << RTC_IE_ALARM_Pos);
void RTC_IntAlarmEn(RTC_TypeDef *RTCx)
{
RTCx->IE |= (1 << RTC_IE_ALARM_Pos);
}
/******************************************************************************************************************************************
@ -383,9 +389,9 @@ void RTC_IntAlarmEn(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntAlarmDis(RTC_TypeDef * RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_ALARM_Pos);
void RTC_IntAlarmDis(RTC_TypeDef *RTCx)
{
RTCx->IE &= ~(1 << RTC_IE_ALARM_Pos);
}
/******************************************************************************************************************************************
@ -395,9 +401,9 @@ void RTC_IntAlarmDis(RTC_TypeDef * RTCx)
* :
* :
******************************************************************************************************************************************/
void RTC_IntAlarmClr(RTC_TypeDef * RTCx)
{
RTCx->IF = (1 << RTC_IF_ALARM_Pos);
void RTC_IntAlarmClr(RTC_TypeDef *RTCx)
{
RTCx->IF = (1 << RTC_IF_ALARM_Pos);
}
/******************************************************************************************************************************************
@ -407,7 +413,7 @@ void RTC_IntAlarmClr(RTC_TypeDef * RTCx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t RTC_IntAlarmStat(RTC_TypeDef * RTCx)
uint32_t RTC_IntAlarmStat(RTC_TypeDef *RTCx)
{
return (RTCx->IF & RTC_IF_ALARM_Msk) ? 1 : 0;
return (RTCx->IF & RTC_IF_ALARM_Msk) ? 1 : 0;
}

View File

@ -1,73 +1,73 @@
#ifndef __SWM320_RTC_H__
#define __SWM320_RTC_H__
#define RTC_SUN 0x01
#define RTC_MON 0x02
#define RTC_TUE 0x04
#define RTC_WED 0x08
#define RTC_THU 0x10
#define RTC_FRI 0x20
#define RTC_SAT 0x40
#define RTC_SUN 0x01
#define RTC_MON 0x02
#define RTC_TUE 0x04
#define RTC_WED 0x08
#define RTC_THU 0x10
#define RTC_FRI 0x20
#define RTC_SAT 0x40
typedef struct {
uint16_t Year;
uint8_t Month; //取值1--12
uint8_t Date; //取值1--31
uint8_t Hour; //取值0--23
uint8_t Minute; //取值0--59
uint8_t Second; //取值0--59
uint8_t SecondIEn;
uint8_t MinuteIEn;
typedef struct
{
uint16_t Year;
uint8_t Month; //取值1--12
uint8_t Date; //取值1--31
uint8_t Hour; //取值0--23
uint8_t Minute; //取值0--59
uint8_t Second; //取值0--59
uint8_t SecondIEn;
uint8_t MinuteIEn;
} RTC_InitStructure;
typedef struct {
uint8_t Days; //RTC_SUN、RTC_MON、RTC_TUE、RTC_WED、RTC_THU、RTC_FRI、RTC_SAT及其或运算组合
uint8_t Hour;
uint8_t Minute;
uint8_t Second;
uint8_t AlarmIEn;
typedef struct
{
uint8_t Days; //RTC_SUN、RTC_MON、RTC_TUE、RTC_WED、RTC_THU、RTC_FRI、RTC_SAT及其或运算组合
uint8_t Hour;
uint8_t Minute;
uint8_t Second;
uint8_t AlarmIEn;
} RTC_AlarmStructure;
typedef struct {
uint16_t Year;
uint8_t Month;
uint8_t Date;
uint8_t Day; //RTC_SUN、RTC_MON、RTC_TUE、RTC_WED、RTC_THU、RTC_FRI、RTC_SAT
uint8_t Hour;
uint8_t Minute;
uint8_t Second;
typedef struct
{
uint16_t Year;
uint8_t Month;
uint8_t Date;
uint8_t Day; //RTC_SUN、RTC_MON、RTC_TUE、RTC_WED、RTC_THU、RTC_FRI、RTC_SAT
uint8_t Hour;
uint8_t Minute;
uint8_t Second;
} RTC_DateTime;
void RTC_Init(RTC_TypeDef * RTCx, RTC_InitStructure * initStruct);
void RTC_Start(RTC_TypeDef * RTCx);
void RTC_Stop(RTC_TypeDef * RTCx);
void RTC_Init(RTC_TypeDef *RTCx, RTC_InitStructure *initStruct);
void RTC_Start(RTC_TypeDef *RTCx);
void RTC_Stop(RTC_TypeDef *RTCx);
void RTC_GetDateTime(RTC_TypeDef * RTCx, RTC_DateTime * dateTime);
void RTC_GetDateTime(RTC_TypeDef *RTCx, RTC_DateTime *dateTime);
void RTC_AlarmSetup(RTC_TypeDef * RTCx, RTC_AlarmStructure * alarmStruct);
void RTC_AlarmSetup(RTC_TypeDef *RTCx, RTC_AlarmStructure *alarmStruct);
void RTC_IntSecondEn(RTC_TypeDef * RTCx);
void RTC_IntSecondDis(RTC_TypeDef * RTCx);
void RTC_IntSecondClr(RTC_TypeDef * RTCx);
uint32_t RTC_IntSecondStat(RTC_TypeDef * RTCx);
void RTC_IntMinuteEn(RTC_TypeDef * RTCx);
void RTC_IntMinuteDis(RTC_TypeDef * RTCx);
void RTC_IntMinuteClr(RTC_TypeDef * RTCx);
uint32_t RTC_IntMinuteStat(RTC_TypeDef * RTCx);
void RTC_IntHourEn(RTC_TypeDef * RTCx);
void RTC_IntHourDis(RTC_TypeDef * RTCx);
void RTC_IntHourClr(RTC_TypeDef * RTCx);
uint32_t RTC_IntHourStat(RTC_TypeDef * RTCx);
void RTC_IntDateEn(RTC_TypeDef * RTCx);
void RTC_IntDateDis(RTC_TypeDef * RTCx);
void RTC_IntDateClr(RTC_TypeDef * RTCx);
uint32_t RTC_IntDateStat(RTC_TypeDef * RTCx);
void RTC_IntAlarmEn(RTC_TypeDef * RTCx);
void RTC_IntAlarmDis(RTC_TypeDef * RTCx);
void RTC_IntAlarmClr(RTC_TypeDef * RTCx);
uint32_t RTC_IntAlarmStat(RTC_TypeDef * RTCx);
void RTC_IntSecondEn(RTC_TypeDef *RTCx);
void RTC_IntSecondDis(RTC_TypeDef *RTCx);
void RTC_IntSecondClr(RTC_TypeDef *RTCx);
uint32_t RTC_IntSecondStat(RTC_TypeDef *RTCx);
void RTC_IntMinuteEn(RTC_TypeDef *RTCx);
void RTC_IntMinuteDis(RTC_TypeDef *RTCx);
void RTC_IntMinuteClr(RTC_TypeDef *RTCx);
uint32_t RTC_IntMinuteStat(RTC_TypeDef *RTCx);
void RTC_IntHourEn(RTC_TypeDef *RTCx);
void RTC_IntHourDis(RTC_TypeDef *RTCx);
void RTC_IntHourClr(RTC_TypeDef *RTCx);
uint32_t RTC_IntHourStat(RTC_TypeDef *RTCx);
void RTC_IntDateEn(RTC_TypeDef *RTCx);
void RTC_IntDateDis(RTC_TypeDef *RTCx);
void RTC_IntDateClr(RTC_TypeDef *RTCx);
uint32_t RTC_IntDateStat(RTC_TypeDef *RTCx);
void RTC_IntAlarmEn(RTC_TypeDef *RTCx);
void RTC_IntAlarmDis(RTC_TypeDef *RTCx);
void RTC_IntAlarmClr(RTC_TypeDef *RTCx);
uint32_t RTC_IntAlarmStat(RTC_TypeDef *RTCx);
#endif //__SWM320_RTC_H__

File diff suppressed because it is too large Load Diff

View File

@ -1,126 +1,120 @@
#ifndef __SWM320_SDIO_H__
#define __SWM320_SDIO_H__
#define SD_CMD_GO_IDLE_STATE ((uint8_t)0)
#define SD_CMD_SEND_OP_COND ((uint8_t)1)
#define SD_CMD_ALL_SEND_CID ((uint8_t)2)
#define SD_CMD_SET_REL_ADDR ((uint8_t)3)
#define SD_CMD_SET_DSR ((uint8_t)4)
#define SD_CMD_HS_SWITCH ((uint8_t)6)
#define SD_CMD_SEL_DESEL_CARD ((uint8_t)7)
#define SD_CMD_SEND_IF_COND ((uint8_t)8)
#define SD_CMD_SEND_CSD ((uint8_t)9)
#define SD_CMD_SEND_CID ((uint8_t)10)
#define SD_CMD_STOP_TRANSMISSION ((uint8_t)12)
#define SD_CMD_SEND_STATUS ((uint8_t)13)
#define SD_CMD_SET_BLOCKLEN ((uint8_t)16)
#define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17)
#define SD_CMD_READ_MULT_BLOCK ((uint8_t)18)
#define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24)
#define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25)
#define SD_CMD_PROG_CID ((uint8_t)26)
#define SD_CMD_PROG_CSD ((uint8_t)27)
#define SD_CMD_APP_CMD ((uint8_t)55)
#define SD_CMD_GO_IDLE_STATE ((uint8_t)0)
#define SD_CMD_SEND_OP_COND ((uint8_t)1)
#define SD_CMD_ALL_SEND_CID ((uint8_t)2)
#define SD_CMD_SET_REL_ADDR ((uint8_t)3)
#define SD_CMD_SET_DSR ((uint8_t)4)
#define SD_CMD_HS_SWITCH ((uint8_t)6)
#define SD_CMD_SEL_DESEL_CARD ((uint8_t)7)
#define SD_CMD_SEND_IF_COND ((uint8_t)8)
#define SD_CMD_SEND_CSD ((uint8_t)9)
#define SD_CMD_SEND_CID ((uint8_t)10)
#define SD_CMD_STOP_TRANSMISSION ((uint8_t)12)
#define SD_CMD_SEND_STATUS ((uint8_t)13)
#define SD_CMD_SET_BLOCKLEN ((uint8_t)16)
#define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17)
#define SD_CMD_READ_MULT_BLOCK ((uint8_t)18)
#define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24)
#define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25)
#define SD_CMD_PROG_CID ((uint8_t)26)
#define SD_CMD_PROG_CSD ((uint8_t)27)
#define SD_CMD_APP_CMD ((uint8_t)55)
/*Following commands are SD Card Specific commands.
SDIO_APP_CMD should be sent before sending these commands. */
#define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6)
#define SD_CMD_SD_APP_STAUS ((uint8_t)13)
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22)
#define SD_CMD_SD_APP_OP_COND ((uint8_t)41)
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42)
#define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51)
#define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52)
#define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53)
#define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6)
#define SD_CMD_SD_APP_STAUS ((uint8_t)13)
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22)
#define SD_CMD_SD_APP_OP_COND ((uint8_t)41)
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42)
#define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51)
#define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52)
#define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53)
#define SD_RESP_NO 0 //0 无响应
#define SD_RESP_32b 2 //2 32位响应
#define SD_RESP_128b 1 //1 128位响应
#define SD_RESP_32b_busy 3 //3 32位响应check Busy after response
#define SD_RESP_NO 0 //0 无响应
#define SD_RESP_32b 2 //2 32位响应
#define SD_RESP_128b 1 //1 128位响应
#define SD_RESP_32b_busy 3 //3 32位响应check Busy after response
#define SD_BUSWIDTH_1b 0
#define SD_BUSWIDTH_4b 2
#define SD_RES_OK 0
#define SD_RES_ERR 1
#define SD_RES_TIMEOUT 2
#define SD_BUSWIDTH_1b 0
#define SD_BUSWIDTH_4b 2
#define SD_RES_OK 0
#define SD_RES_ERR 1
#define SD_RES_TIMEOUT 2
typedef struct
{
__IO uint8_t CSDStruct; // CSD structure
__IO uint8_t SysSpecVersion; // System specification version
__IO uint8_t Reserved1; // Reserved
__IO uint8_t TAAC; // Data read access-time 1
__IO uint8_t NSAC; // Data read access-time 2 in CLK cycles
__IO uint8_t MaxBusClkFrec; // Max. bus clock frequency
__IO uint16_t CardComdClasses; //< Card command classes
__IO uint8_t RdBlockLen; // Max. read data block length
__IO uint8_t PartBlockRead; // Partial blocks for read allowed
__IO uint8_t WrBlockMisalign; // Write block misalignment
__IO uint8_t RdBlockMisalign; // Read block misalignment
__IO uint8_t DSRImpl; // DSR implemented
__IO uint8_t Reserved2; // Reserved
__IO uint32_t DeviceSize; // Device Size
__IO uint8_t MaxRdCurrentVDDMin; // Max. read current @ VDD min
__IO uint8_t MaxRdCurrentVDDMax; // Max. read current @ VDD max
__IO uint8_t MaxWrCurrentVDDMin; // Max. write current @ VDD min
__IO uint8_t MaxWrCurrentVDDMax; // Max. write current @ VDD max
__IO uint8_t DeviceSizeMul; // Device size multiplier
__IO uint8_t EraseGrSize; // Erase group size
__IO uint8_t EraseGrMul; // Erase group size multiplier
__IO uint8_t WrProtectGrSize; // Write protect group size
__IO uint8_t WrProtectGrEnable; // Write protect group enable
__IO uint8_t ManDeflECC; // Manufacturer default ECC
__IO uint8_t WrSpeedFact; // Write speed factor
__IO uint8_t MaxWrBlockLen; // Max. write data block length
__IO uint8_t WriteBlockPaPartial; // Partial blocks for write allowed
__IO uint8_t Reserved3; // Reserded
__IO uint8_t ContentProtectAppli; // Content protection application
__IO uint8_t FileFormatGrouop; // File format group
__IO uint8_t CopyFlag; // Copy flag (OTP)
__IO uint8_t PermWrProtect; // Permanent write protection
__IO uint8_t TempWrProtect; // Temporary write protection
__IO uint8_t FileFormat; // File Format
__IO uint8_t ECC; // ECC code
__IO uint8_t CSDStruct; // CSD structure
__IO uint8_t SysSpecVersion; // System specification version
__IO uint8_t Reserved1; // Reserved
__IO uint8_t TAAC; // Data read access-time 1
__IO uint8_t NSAC; // Data read access-time 2 in CLK cycles
__IO uint8_t MaxBusClkFrec; // Max. bus clock frequency
__IO uint16_t CardComdClasses; //< Card command classes
__IO uint8_t RdBlockLen; // Max. read data block length
__IO uint8_t PartBlockRead; // Partial blocks for read allowed
__IO uint8_t WrBlockMisalign; // Write block misalignment
__IO uint8_t RdBlockMisalign; // Read block misalignment
__IO uint8_t DSRImpl; // DSR implemented
__IO uint8_t Reserved2; // Reserved
__IO uint32_t DeviceSize; // Device Size
__IO uint8_t MaxRdCurrentVDDMin; // Max. read current @ VDD min
__IO uint8_t MaxRdCurrentVDDMax; // Max. read current @ VDD max
__IO uint8_t MaxWrCurrentVDDMin; // Max. write current @ VDD min
__IO uint8_t MaxWrCurrentVDDMax; // Max. write current @ VDD max
__IO uint8_t DeviceSizeMul; // Device size multiplier
__IO uint8_t EraseGrSize; // Erase group size
__IO uint8_t EraseGrMul; // Erase group size multiplier
__IO uint8_t WrProtectGrSize; // Write protect group size
__IO uint8_t WrProtectGrEnable; // Write protect group enable
__IO uint8_t ManDeflECC; // Manufacturer default ECC
__IO uint8_t WrSpeedFact; // Write speed factor
__IO uint8_t MaxWrBlockLen; // Max. write data block length
__IO uint8_t WriteBlockPaPartial; // Partial blocks for write allowed
__IO uint8_t Reserved3; // Reserded
__IO uint8_t ContentProtectAppli; // Content protection application
__IO uint8_t FileFormatGrouop; // File format group
__IO uint8_t CopyFlag; // Copy flag (OTP)
__IO uint8_t PermWrProtect; // Permanent write protection
__IO uint8_t TempWrProtect; // Temporary write protection
__IO uint8_t FileFormat; // File Format
__IO uint8_t ECC; // ECC code
} SD_CSD;
typedef struct
{
__IO uint8_t ManufacturerID; // ManufacturerID
__IO uint16_t OEM_AppliID; // OEM/Application ID
__IO uint32_t ProdName1; // Product Name part1
__IO uint8_t ProdName2; // Product Name part2
__IO uint8_t ProdRev; // Product Revision
__IO uint32_t ProdSN; // Product Serial Number
__IO uint8_t Reserved1; // Reserved1
__IO uint16_t ManufactDate; // Manufacturing Date
__IO uint8_t ManufacturerID; // ManufacturerID
__IO uint16_t OEM_AppliID; // OEM/Application ID
__IO uint32_t ProdName1; // Product Name part1
__IO uint8_t ProdName2; // Product Name part2
__IO uint8_t ProdRev; // Product Revision
__IO uint32_t ProdSN; // Product Serial Number
__IO uint8_t Reserved1; // Reserved1
__IO uint16_t ManufactDate; // Manufacturing Date
} SD_CID;
#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
#define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
#define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003)
#define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
#define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
#define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
#define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003)
#define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
#define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
typedef struct
{
SD_CSD SD_csd;
SD_CID SD_cid;
uint64_t CardCapacity; // Card Capacity
uint32_t CardBlockSize; // Card Block Size
uint16_t RCA;
uint8_t CardType;
SD_CSD SD_csd;
SD_CID SD_cid;
uint64_t CardCapacity; // Card Capacity
uint32_t CardBlockSize; // Card Block Size
uint16_t RCA;
uint8_t CardType;
} SD_CardInfo;
extern SD_CardInfo SD_cardInfo;
uint32_t SDIO_Init(uint32_t freq);
@ -135,9 +129,9 @@ uint32_t SDIO_DMABlockRead(uint32_t block_addr, uint16_t block_cnt, uint32_t buf
uint32_t _SDIO_SendCmd(uint32_t cmd, uint32_t arg, uint32_t resp_type, uint32_t *resp_data, uint32_t have_data, uint32_t data_read, uint16_t block_cnt, uint32_t use_dma);
#define SDIO_SendCmd(cmd, arg, resp_type, resp_data) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 0, 0, 0, 0)
#define SDIO_SendCmdWithData(cmd, arg, resp_type, resp_data, data_read, block_cnt) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 0)
#define SDIO_SendCmdWithDataByDMA(cmd, arg, resp_type, resp_data, data_read, block_cnt) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 1)
#define SDIO_SendCmd(cmd, arg, resp_type, resp_data) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 0, 0, 0, 0)
#define SDIO_SendCmdWithData(cmd, arg, resp_type, resp_data, data_read, block_cnt) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 0)
#define SDIO_SendCmdWithDataByDMA(cmd, arg, resp_type, resp_data, data_read, block_cnt) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 1)
void parseCID(uint32_t CID_Tab[4]);
void parseCSD(uint32_t CID_Tab[4]);

View File

@ -28,44 +28,55 @@
* :
* :
******************************************************************************************************************************************/
void SDRAM_Init(SDRAM_InitStructure * initStruct)
void SDRAM_Init(SDRAM_InitStructure *initStruct)
{
uint32_t row_n;
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
SYS->CLKDIV &= ~SYS_CLKDIV_SDRAM_Msk;
SYS->CLKDIV |= (1 << SYS_CLKDIV_SDRAM_Pos); //2分频
SDRAMC->CR0 = (2 << SDRAMC_CR0_BURSTLEN_Pos) | //2 Burst Length为4
(initStruct->CASLatency << SDRAMC_CR0_CASDELAY_Pos);
SDRAMC->CR1 = (initStruct->CellSize << SDRAMC_CR1_CELLSIZE_Pos) |
(initStruct->CellWidth << SDRAMC_CR1_CELL32BIT_Pos) |
(initStruct->CellBank << SDRAMC_CR1_BANK_Pos) |
(0 << SDRAMC_CR1_32BIT_Pos) |
(initStruct->TimeTMRD << SDRAMC_CR1_TMRD_Pos) |
(initStruct->TimeTRRD << SDRAMC_CR1_TRRD_Pos) |
(initStruct->TimeTRAS << SDRAMC_CR1_TRAS_Pos) |
(initStruct->TimeTRC << SDRAMC_CR1_TRC_Pos) |
(initStruct->TimeTRCD << SDRAMC_CR1_TRCD_Pos) |
(initStruct->TimeTRP << SDRAMC_CR1_TRP_Pos);
SDRAMC->LATCH = 0x02;
switch(initStruct->CellSize)
{
case SDRAM_CELLSIZE_16Mb: row_n = 11; break;
case SDRAM_CELLSIZE_64Mb: row_n = 12; break;
case SDRAM_CELLSIZE_128Mb: row_n = 12; break;
case SDRAM_CELLSIZE_256Mb: row_n = 13; break;
default: row_n = 13; break;
}
SDRAMC->REFRESH = (1 << SDRAMC_REFRESH_EN_Pos) |
(((SystemCoreClock/2)/1000*64 / (1 << row_n)) << SDRAMC_REFRESH_RATE_Pos);
while(SDRAMC->REFDONE == 0);
uint32_t row_n;
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
SYS->CLKDIV &= ~SYS_CLKDIV_SDRAM_Msk;
SYS->CLKDIV |= (1 << SYS_CLKDIV_SDRAM_Pos); //2分频
SDRAMC->CR0 = (2 << SDRAMC_CR0_BURSTLEN_Pos) | //2 Burst Length为4
(initStruct->CASLatency << SDRAMC_CR0_CASDELAY_Pos);
SDRAMC->CR1 = (initStruct->CellSize << SDRAMC_CR1_CELLSIZE_Pos) |
(initStruct->CellWidth << SDRAMC_CR1_CELL32BIT_Pos) |
(initStruct->CellBank << SDRAMC_CR1_BANK_Pos) |
(0 << SDRAMC_CR1_32BIT_Pos) |
(initStruct->TimeTMRD << SDRAMC_CR1_TMRD_Pos) |
(initStruct->TimeTRRD << SDRAMC_CR1_TRRD_Pos) |
(initStruct->TimeTRAS << SDRAMC_CR1_TRAS_Pos) |
(initStruct->TimeTRC << SDRAMC_CR1_TRC_Pos) |
(initStruct->TimeTRCD << SDRAMC_CR1_TRCD_Pos) |
(initStruct->TimeTRP << SDRAMC_CR1_TRP_Pos);
SDRAMC->LATCH = 0x02;
switch (initStruct->CellSize)
{
case SDRAM_CELLSIZE_16Mb:
row_n = 11;
break;
case SDRAM_CELLSIZE_64Mb:
row_n = 12;
break;
case SDRAM_CELLSIZE_128Mb:
row_n = 12;
break;
case SDRAM_CELLSIZE_256Mb:
row_n = 13;
break;
default:
row_n = 13;
break;
}
SDRAMC->REFRESH = (1 << SDRAMC_REFRESH_EN_Pos) |
(((SystemCoreClock / 2) / 1000 * 64 / (1 << row_n)) << SDRAMC_REFRESH_RATE_Pos);
while (SDRAMC->REFDONE == 0)
;
}
/******************************************************************************************************************************************
@ -77,12 +88,13 @@ void SDRAM_Init(SDRAM_InitStructure * initStruct)
******************************************************************************************************************************************/
void SDRAM_Enable(void)
{
uint32_t i;
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
SDRAMC->REFRESH |= (1 << SDRAMC_REFRESH_EN_Pos);
for(i = 0; i < 100; i++) __NOP();
uint32_t i;
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
SDRAMC->REFRESH |= (1 << SDRAMC_REFRESH_EN_Pos);
for (i = 0; i < 100; i++)
__NOP();
}
/******************************************************************************************************************************************
@ -94,10 +106,11 @@ void SDRAM_Enable(void)
******************************************************************************************************************************************/
void SDRAM_Disable(void)
{
uint32_t i;
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
for(i = 0; i < 100; i++) __NOP();
uint32_t i;
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
for (i = 0; i < 100; i++)
__NOP();
}

View File

@ -1,80 +1,79 @@
#ifndef __SWM320_SDRAM_H__
#define __SWM320_SDRAM_H__
typedef struct {
uint8_t CellSize; // SDRAM颗粒的容量SDRAM_CELLSIZE_16Mb、SDRAM_CELLSIZE_64Mb、SDRAM_CELLSIZE_128Mb、SDRAM_CELLSIZE_256Mb
uint8_t CellBank; // SDRAM颗粒有几个bankSDRAM_CELLBANK_2、SDRAM_CELLBANK_4
uint8_t CellWidth; // SDRAM颗粒的位宽SDRAM_CELLWIDTH_16、SDRAM_CELLWIDTH_32
uint8_t CASLatency; // 列地址到有效数据输出间隔SDRAM_CASLATENCY_2、SDRAM_CASLATENCY_3
uint8_t TimeTMRD; // MRS to New Command
uint8_t TimeTRRD; // Activate to activate on different banks
uint8_t TimeTRAS; // Self refresh time最小Self-refresh周期
uint8_t TimeTRC; // Row cycle delayRefresh命令到Activate命令间延时也是两个连续Refresh命令间延时
uint8_t TimeTRCD; // Row to column delay行地址到列地址间延时也即Activate命令到读写命令间延时
uint8_t TimeTRP; // Row precharge delayPrecharge命令到另一个命令间延时
typedef struct
{
uint8_t CellSize; // SDRAM颗粒的容量SDRAM_CELLSIZE_16Mb、SDRAM_CELLSIZE_64Mb、SDRAM_CELLSIZE_128Mb、SDRAM_CELLSIZE_256Mb
uint8_t CellBank; // SDRAM颗粒有几个bankSDRAM_CELLBANK_2、SDRAM_CELLBANK_4
uint8_t CellWidth; // SDRAM颗粒的位宽SDRAM_CELLWIDTH_16、SDRAM_CELLWIDTH_32
uint8_t CASLatency; // 列地址到有效数据输出间隔SDRAM_CASLATENCY_2、SDRAM_CASLATENCY_3
uint8_t TimeTMRD; // MRS to New Command
uint8_t TimeTRRD; // Activate to activate on different banks
uint8_t TimeTRAS; // Self refresh time最小Self-refresh周期
uint8_t TimeTRC; // Row cycle delayRefresh命令到Activate命令间延时也是两个连续Refresh命令间延时
uint8_t TimeTRCD; // Row to column delay行地址到列地址间延时也即Activate命令到读写命令间延时
uint8_t TimeTRP; // Row precharge delayPrecharge命令到另一个命令间延时
} SDRAM_InitStructure;
#define SDRAM_CELLSIZE_16Mb 3
#define SDRAM_CELLSIZE_64Mb 0
#define SDRAM_CELLSIZE_128Mb 1
#define SDRAM_CELLSIZE_256Mb 2
#define SDRAM_CELLSIZE_16Mb 3
#define SDRAM_CELLSIZE_64Mb 0
#define SDRAM_CELLSIZE_128Mb 1
#define SDRAM_CELLSIZE_256Mb 2
#define SDRAM_CELLBANK_2 0
#define SDRAM_CELLBANK_4 1
#define SDRAM_CELLBANK_2 0
#define SDRAM_CELLBANK_4 1
#define SDRAM_CELLWIDTH_16 0
#define SDRAM_CELLWIDTH_32 1
#define SDRAM_CELLWIDTH_16 0
#define SDRAM_CELLWIDTH_32 1
#define SDRAM_CASLATENCY_2 2
#define SDRAM_CASLATENCY_3 3
#define SDRAM_CASLATENCY_2 2
#define SDRAM_CASLATENCY_3 3
#define SDRAM_TMRD_3 3
#define SDRAM_TMRD_4 4
#define SDRAM_TMRD_5 5
#define SDRAM_TMRD_6 6
#define SDRAM_TMRD_7 7
#define SDRAM_TMRD_3 3
#define SDRAM_TMRD_4 4
#define SDRAM_TMRD_5 5
#define SDRAM_TMRD_6 6
#define SDRAM_TMRD_7 7
#define SDRAM_TRRD_2 2
#define SDRAM_TRRD_3 3
#define SDRAM_TRRD_2 2
#define SDRAM_TRRD_3 3
#define SDRAM_TRAS_2 2
#define SDRAM_TRAS_3 3
#define SDRAM_TRAS_4 4
#define SDRAM_TRAS_5 5
#define SDRAM_TRAS_6 6
#define SDRAM_TRAS_7 7
#define SDRAM_TRAS_2 2
#define SDRAM_TRAS_3 3
#define SDRAM_TRAS_4 4
#define SDRAM_TRAS_5 5
#define SDRAM_TRAS_6 6
#define SDRAM_TRAS_7 7
#define SDRAM_TRC_2 2
#define SDRAM_TRC_3 3
#define SDRAM_TRC_4 4
#define SDRAM_TRC_5 5
#define SDRAM_TRC_6 6
#define SDRAM_TRC_7 7
#define SDRAM_TRC_8 8
#define SDRAM_TRC_9 9
#define SDRAM_TRC_10 10
#define SDRAM_TRC_11 11
#define SDRAM_TRC_12 12
#define SDRAM_TRC_13 13
#define SDRAM_TRC_14 14
#define SDRAM_TRC_15 15
#define SDRAM_TRC_2 2
#define SDRAM_TRC_3 3
#define SDRAM_TRC_4 4
#define SDRAM_TRC_5 5
#define SDRAM_TRC_6 6
#define SDRAM_TRC_7 7
#define SDRAM_TRC_8 8
#define SDRAM_TRC_9 9
#define SDRAM_TRC_10 10
#define SDRAM_TRC_11 11
#define SDRAM_TRC_12 12
#define SDRAM_TRC_13 13
#define SDRAM_TRC_14 14
#define SDRAM_TRC_15 15
#define SDRAM_TRCD_3 3
#define SDRAM_TRCD_4 4
#define SDRAM_TRCD_5 5
#define SDRAM_TRCD_6 6
#define SDRAM_TRCD_7 7
#define SDRAM_TRCD_3 3
#define SDRAM_TRCD_4 4
#define SDRAM_TRCD_5 5
#define SDRAM_TRCD_6 6
#define SDRAM_TRCD_7 7
#define SDRAM_TRP_3 3
#define SDRAM_TRP_4 4
#define SDRAM_TRP_5 5
#define SDRAM_TRP_6 6
#define SDRAM_TRP_7 7
#define SDRAM_TRP_3 3
#define SDRAM_TRP_4 4
#define SDRAM_TRP_5 5
#define SDRAM_TRP_6 6
#define SDRAM_TRP_7 7
void SDRAM_Init(SDRAM_InitStructure * initStruct);
void SDRAM_Init(SDRAM_InitStructure *initStruct);
void SDRAM_Enable(void);
void SDRAM_Disable(void);

View File

@ -21,7 +21,6 @@
#include "SWM320.h"
#include "SWM320_spi.h"
/******************************************************************************************************************************************
* : SPI_Init()
* : SPI同步串行接口初始化FIFO触发设定
@ -30,61 +29,61 @@
* :
* :
******************************************************************************************************************************************/
void SPI_Init(SPI_TypeDef * SPIx, SPI_InitStructure * initStruct)
void SPI_Init(SPI_TypeDef *SPIx, SPI_InitStructure *initStruct)
{
switch((uint32_t)SPIx)
{
case ((uint32_t)SPI0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_SPI0_Pos);
break;
switch ((uint32_t)SPIx)
{
case ((uint32_t)SPI0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_SPI0_Pos);
break;
case ((uint32_t)SPI1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_SPI0_Pos); //与SPI0使用同一位时钟使能
break;
}
SPI_Close(SPIx); //一些关键寄存器只能在SPI关闭时设置
SPIx->CTRL &= ~(SPI_CTRL_FFS_Msk | SPI_CTRL_CPHA_Msk | SPI_CTRL_CPOL_Msk |
SPI_CTRL_SIZE_Msk | SPI_CTRL_MSTR_Msk | SPI_CTRL_CLKDIV_Msk | SPI_CTRL_SSN_H_Msk);
SPIx->CTRL |= (initStruct->FrameFormat << SPI_CTRL_FFS_Pos) |
(initStruct->SampleEdge << SPI_CTRL_CPHA_Pos) |
(initStruct->IdleLevel << SPI_CTRL_CPOL_Pos) |
((initStruct->WordSize-1) << SPI_CTRL_SIZE_Pos) |
(initStruct->Master << SPI_CTRL_MSTR_Pos) |
(initStruct->clkDiv << SPI_CTRL_CLKDIV_Pos) |
(0 << SPI_CTRL_SSN_H_Pos);
SPIx->IF = (0x01 << SPI_IF_RFOVF_Pos); //清除中断标志
SPIx->IE &= ~(SPI_IE_RFHF_Msk | SPI_IE_TFHF_Msk | SPI_IE_FTC_Msk);
SPIx->IE |= (initStruct->RXHFullIEn << SPI_IE_RFHF_Pos) |
(initStruct->TXEmptyIEn << SPI_IE_TFHF_Pos) |
(initStruct->TXCompleteIEn << SPI_IE_FTC_Pos);
switch((uint32_t)SPIx)
{
case ((uint32_t)SPI0):
if(initStruct->RXHFullIEn | initStruct->TXEmptyIEn | initStruct->TXCompleteIEn)
{
NVIC_EnableIRQ(SPI0_IRQn);
}
else
{
NVIC_DisableIRQ(SPI0_IRQn);
}
break;
case ((uint32_t)SPI1):
if(initStruct->RXHFullIEn | initStruct->TXEmptyIEn | initStruct->TXCompleteIEn)
{
NVIC_EnableIRQ(SPI1_IRQn);
}
else
{
NVIC_DisableIRQ(SPI1_IRQn);
}
break;
}
case ((uint32_t)SPI1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_SPI0_Pos); //与SPI0使用同一位时钟使能
break;
}
SPI_Close(SPIx); //一些关键寄存器只能在SPI关闭时设置
SPIx->CTRL &= ~(SPI_CTRL_FFS_Msk | SPI_CTRL_CPHA_Msk | SPI_CTRL_CPOL_Msk |
SPI_CTRL_SIZE_Msk | SPI_CTRL_MSTR_Msk | SPI_CTRL_CLKDIV_Msk | SPI_CTRL_SSN_H_Msk);
SPIx->CTRL |= (initStruct->FrameFormat << SPI_CTRL_FFS_Pos) |
(initStruct->SampleEdge << SPI_CTRL_CPHA_Pos) |
(initStruct->IdleLevel << SPI_CTRL_CPOL_Pos) |
((initStruct->WordSize - 1) << SPI_CTRL_SIZE_Pos) |
(initStruct->Master << SPI_CTRL_MSTR_Pos) |
(initStruct->clkDiv << SPI_CTRL_CLKDIV_Pos) |
(0 << SPI_CTRL_SSN_H_Pos);
SPIx->IF = (0x01 << SPI_IF_RFOVF_Pos); //清除中断标志
SPIx->IE &= ~(SPI_IE_RFHF_Msk | SPI_IE_TFHF_Msk | SPI_IE_FTC_Msk);
SPIx->IE |= (initStruct->RXHFullIEn << SPI_IE_RFHF_Pos) |
(initStruct->TXEmptyIEn << SPI_IE_TFHF_Pos) |
(initStruct->TXCompleteIEn << SPI_IE_FTC_Pos);
switch ((uint32_t)SPIx)
{
case ((uint32_t)SPI0):
if (initStruct->RXHFullIEn | initStruct->TXEmptyIEn | initStruct->TXCompleteIEn)
{
NVIC_EnableIRQ(SPI0_IRQn);
}
else
{
NVIC_DisableIRQ(SPI0_IRQn);
}
break;
case ((uint32_t)SPI1):
if (initStruct->RXHFullIEn | initStruct->TXEmptyIEn | initStruct->TXCompleteIEn)
{
NVIC_EnableIRQ(SPI1_IRQn);
}
else
{
NVIC_DisableIRQ(SPI1_IRQn);
}
break;
}
}
/******************************************************************************************************************************************
@ -94,9 +93,9 @@ void SPI_Init(SPI_TypeDef * SPIx, SPI_InitStructure * initStruct)
* :
* :
******************************************************************************************************************************************/
void SPI_Open(SPI_TypeDef * SPIx)
void SPI_Open(SPI_TypeDef *SPIx)
{
SPIx->CTRL |= (0x01 << SPI_CTRL_EN_Pos);
SPIx->CTRL |= (0x01 << SPI_CTRL_EN_Pos);
}
/******************************************************************************************************************************************
@ -106,9 +105,9 @@ void SPI_Open(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_Close(SPI_TypeDef * SPIx)
void SPI_Close(SPI_TypeDef *SPIx)
{
SPIx->CTRL &= ~SPI_CTRL_EN_Msk;
SPIx->CTRL &= ~SPI_CTRL_EN_Msk;
}
/******************************************************************************************************************************************
@ -118,9 +117,9 @@ void SPI_Close(SPI_TypeDef * SPIx)
* : uint32_t
* :
******************************************************************************************************************************************/
uint32_t SPI_Read(SPI_TypeDef * SPIx)
uint32_t SPI_Read(SPI_TypeDef *SPIx)
{
return SPIx->DATA;
return SPIx->DATA;
}
/******************************************************************************************************************************************
@ -131,9 +130,9 @@ uint32_t SPI_Read(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_Write(SPI_TypeDef * SPIx, uint32_t data)
void SPI_Write(SPI_TypeDef *SPIx, uint32_t data)
{
SPIx->DATA = data;
SPIx->DATA = data;
}
/******************************************************************************************************************************************
@ -144,13 +143,14 @@ void SPI_Write(SPI_TypeDef * SPIx, uint32_t data)
* :
* :
******************************************************************************************************************************************/
void SPI_WriteWithWait(SPI_TypeDef * SPIx, uint32_t data)
void SPI_WriteWithWait(SPI_TypeDef *SPIx, uint32_t data)
{
SPIx->STAT |= (1 << SPI_STAT_WTC_Pos);
SPIx->DATA = data;
while((SPIx->STAT & SPI_STAT_WTC_Msk) == 0);
SPIx->STAT |= (1 << SPI_STAT_WTC_Pos);
SPIx->DATA = data;
while ((SPIx->STAT & SPI_STAT_WTC_Msk) == 0)
;
}
/******************************************************************************************************************************************
@ -161,12 +161,13 @@ void SPI_WriteWithWait(SPI_TypeDef * SPIx, uint32_t data)
* : uint32_t
* : SPI模块SPI_Write()SPI_Write()SPI_STAT_RFNE状态
******************************************************************************************************************************************/
uint32_t SPI_ReadWrite(SPI_TypeDef * SPIx, uint32_t data)
uint32_t SPI_ReadWrite(SPI_TypeDef *SPIx, uint32_t data)
{
SPIx->DATA = data;
while(!(SPIx->STAT & SPI_STAT_RFNE_Msk));
return SPIx->DATA;
SPIx->DATA = data;
while (!(SPIx->STAT & SPI_STAT_RFNE_Msk))
;
return SPIx->DATA;
}
/******************************************************************************************************************************************
@ -176,9 +177,9 @@ uint32_t SPI_ReadWrite(SPI_TypeDef * SPIx, uint32_t data)
* : uint32_t 1 FIFO空 0 FIFO非空
* :
******************************************************************************************************************************************/
uint32_t SPI_IsRXEmpty(SPI_TypeDef * SPIx)
uint32_t SPI_IsRXEmpty(SPI_TypeDef *SPIx)
{
return (SPIx->STAT & SPI_STAT_RFNE_Msk) ? 0 : 1;
return (SPIx->STAT & SPI_STAT_RFNE_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
@ -188,9 +189,9 @@ uint32_t SPI_IsRXEmpty(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO满 0 FIFO不满
* :
******************************************************************************************************************************************/
uint32_t SPI_IsTXFull(SPI_TypeDef * SPIx)
uint32_t SPI_IsTXFull(SPI_TypeDef *SPIx)
{
return (SPIx->STAT & SPI_STAT_TFNF_Msk) ? 0 : 1;
return (SPIx->STAT & SPI_STAT_TFNF_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
@ -200,12 +201,11 @@ uint32_t SPI_IsTXFull(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO空 0 FIFO非空
* :
******************************************************************************************************************************************/
uint32_t SPI_IsTXEmpty(SPI_TypeDef * SPIx)
uint32_t SPI_IsTXEmpty(SPI_TypeDef *SPIx)
{
return (SPIx->STAT & SPI_STAT_TFE_Msk) ? 1 : 0;
return (SPIx->STAT & SPI_STAT_TFE_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
* : SPI_INTRXHalfFullEn()
* : FIFO半满中断使能
@ -213,9 +213,9 @@ uint32_t SPI_IsTXEmpty(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXHalfFullEn(SPI_TypeDef * SPIx)
void SPI_INTRXHalfFullEn(SPI_TypeDef *SPIx)
{
SPIx->IE |= (0x01 << SPI_IE_RFHF_Pos);
SPIx->IE |= (0x01 << SPI_IE_RFHF_Pos);
}
/******************************************************************************************************************************************
@ -225,9 +225,9 @@ void SPI_INTRXHalfFullEn(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXHalfFullDis(SPI_TypeDef * SPIx)
void SPI_INTRXHalfFullDis(SPI_TypeDef *SPIx)
{
SPIx->IE &= ~(0x01 << SPI_IE_RFHF_Pos);
SPIx->IE &= ~(0x01 << SPI_IE_RFHF_Pos);
}
/******************************************************************************************************************************************
@ -237,9 +237,9 @@ void SPI_INTRXHalfFullDis(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXHalfFullClr(SPI_TypeDef * SPIx)
void SPI_INTRXHalfFullClr(SPI_TypeDef *SPIx)
{
SPIx->IF = (1 << SPI_IF_RFHF_Pos);
SPIx->IF = (1 << SPI_IF_RFHF_Pos);
}
/******************************************************************************************************************************************
@ -249,9 +249,9 @@ void SPI_INTRXHalfFullClr(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO达到半满 0 FIFO未达到半满
* :
******************************************************************************************************************************************/
uint32_t SPI_INTRXHalfFullStat(SPI_TypeDef * SPIx)
uint32_t SPI_INTRXHalfFullStat(SPI_TypeDef *SPIx)
{
return (SPIx->IF & SPI_IF_RFHF_Msk) ? 1 : 0;
return (SPIx->IF & SPI_IF_RFHF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -261,9 +261,9 @@ uint32_t SPI_INTRXHalfFullStat(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXFullEn(SPI_TypeDef * SPIx)
void SPI_INTRXFullEn(SPI_TypeDef *SPIx)
{
SPIx->IE |= (0x01 << SPI_IE_RFF_Pos);
SPIx->IE |= (0x01 << SPI_IE_RFF_Pos);
}
/******************************************************************************************************************************************
@ -273,9 +273,9 @@ void SPI_INTRXFullEn(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXFullDis(SPI_TypeDef * SPIx)
void SPI_INTRXFullDis(SPI_TypeDef *SPIx)
{
SPIx->IE &= ~(0x01 << SPI_IE_RFF_Pos);
SPIx->IE &= ~(0x01 << SPI_IE_RFF_Pos);
}
/******************************************************************************************************************************************
@ -285,9 +285,9 @@ void SPI_INTRXFullDis(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXFullClr(SPI_TypeDef * SPIx)
void SPI_INTRXFullClr(SPI_TypeDef *SPIx)
{
SPIx->IF = (1 << SPI_IF_RFF_Pos);
SPIx->IF = (1 << SPI_IF_RFF_Pos);
}
/******************************************************************************************************************************************
@ -297,9 +297,9 @@ void SPI_INTRXFullClr(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO满 0 FIFO未满
* :
******************************************************************************************************************************************/
uint32_t SPI_INTRXFullStat(SPI_TypeDef * SPIx)
uint32_t SPI_INTRXFullStat(SPI_TypeDef *SPIx)
{
return (SPIx->IF & SPI_IF_RFF_Msk) ? 1 : 0;
return (SPIx->IF & SPI_IF_RFF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -309,9 +309,9 @@ uint32_t SPI_INTRXFullStat(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXOverflowEn(SPI_TypeDef * SPIx)
void SPI_INTRXOverflowEn(SPI_TypeDef *SPIx)
{
SPIx->IE |= (0x01 << SPI_IE_RFOVF_Pos);
SPIx->IE |= (0x01 << SPI_IE_RFOVF_Pos);
}
/******************************************************************************************************************************************
@ -321,9 +321,9 @@ void SPI_INTRXOverflowEn(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXOverflowDis(SPI_TypeDef * SPIx)
void SPI_INTRXOverflowDis(SPI_TypeDef *SPIx)
{
SPIx->IE &= ~(0x01 << SPI_IE_RFOVF_Pos);
SPIx->IE &= ~(0x01 << SPI_IE_RFOVF_Pos);
}
/******************************************************************************************************************************************
@ -333,9 +333,9 @@ void SPI_INTRXOverflowDis(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTRXOverflowClr(SPI_TypeDef * SPIx)
void SPI_INTRXOverflowClr(SPI_TypeDef *SPIx)
{
SPIx->IF = (0x01 << SPI_IF_RFOVF_Pos);
SPIx->IF = (0x01 << SPI_IF_RFOVF_Pos);
}
/******************************************************************************************************************************************
@ -345,9 +345,9 @@ void SPI_INTRXOverflowClr(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO溢出 0 FIFO未溢出
* :
******************************************************************************************************************************************/
uint32_t SPI_INTRXOverflowStat(SPI_TypeDef * SPIx)
uint32_t SPI_INTRXOverflowStat(SPI_TypeDef *SPIx)
{
return (SPIx->IF & SPI_IF_RFOVF_Msk) ? 1 : 0;
return (SPIx->IF & SPI_IF_RFOVF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -357,9 +357,9 @@ uint32_t SPI_INTRXOverflowStat(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXHalfFullEn(SPI_TypeDef * SPIx)
void SPI_INTTXHalfFullEn(SPI_TypeDef *SPIx)
{
SPIx->IE |= (0x01 << SPI_IE_TFHF_Pos);
SPIx->IE |= (0x01 << SPI_IE_TFHF_Pos);
}
/******************************************************************************************************************************************
@ -369,9 +369,9 @@ void SPI_INTTXHalfFullEn(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXHalfFullDis(SPI_TypeDef * SPIx)
void SPI_INTTXHalfFullDis(SPI_TypeDef *SPIx)
{
SPIx->IE &= ~(0x01 << SPI_IE_TFHF_Pos);
SPIx->IE &= ~(0x01 << SPI_IE_TFHF_Pos);
}
/******************************************************************************************************************************************
@ -381,9 +381,9 @@ void SPI_INTTXHalfFullDis(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXHalfFullClr(SPI_TypeDef * SPIx)
void SPI_INTTXHalfFullClr(SPI_TypeDef *SPIx)
{
SPIx->IF = (1 << SPI_IF_TFHF_Pos);
SPIx->IF = (1 << SPI_IF_TFHF_Pos);
}
/******************************************************************************************************************************************
@ -393,9 +393,9 @@ void SPI_INTTXHalfFullClr(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO达到半满 0 FIFO未达到半满
* :
******************************************************************************************************************************************/
uint32_t SPI_INTTXHalfFullStat(SPI_TypeDef * SPIx)
uint32_t SPI_INTTXHalfFullStat(SPI_TypeDef *SPIx)
{
return (SPIx->IF & SPI_IF_TFHF_Msk) ? 1 : 0;
return (SPIx->IF & SPI_IF_TFHF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -405,9 +405,9 @@ uint32_t SPI_INTTXHalfFullStat(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXEmptyEn(SPI_TypeDef * SPIx)
void SPI_INTTXEmptyEn(SPI_TypeDef *SPIx)
{
SPIx->IE |= (0x01 << SPI_IE_TFE_Pos);
SPIx->IE |= (0x01 << SPI_IE_TFE_Pos);
}
/******************************************************************************************************************************************
@ -417,9 +417,9 @@ void SPI_INTTXEmptyEn(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXEmptyDis(SPI_TypeDef * SPIx)
void SPI_INTTXEmptyDis(SPI_TypeDef *SPIx)
{
SPIx->IE &= ~(0x01 << SPI_IE_TFE_Pos);
SPIx->IE &= ~(0x01 << SPI_IE_TFE_Pos);
}
/******************************************************************************************************************************************
@ -429,9 +429,9 @@ void SPI_INTTXEmptyDis(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXEmptyClr(SPI_TypeDef * SPIx)
void SPI_INTTXEmptyClr(SPI_TypeDef *SPIx)
{
SPIx->IF = (1 << SPI_IF_TFE_Pos);
SPIx->IF = (1 << SPI_IF_TFE_Pos);
}
/******************************************************************************************************************************************
@ -441,9 +441,9 @@ void SPI_INTTXEmptyClr(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO空 0 FIFO非空
* :
******************************************************************************************************************************************/
uint32_t SPI_INTTXEmptyStat(SPI_TypeDef * SPIx)
uint32_t SPI_INTTXEmptyStat(SPI_TypeDef *SPIx)
{
return (SPIx->IF & SPI_IF_TFE_Msk) ? 1 : 0;
return (SPIx->IF & SPI_IF_TFE_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -453,9 +453,9 @@ uint32_t SPI_INTTXEmptyStat(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXCompleteEn(SPI_TypeDef * SPIx)
void SPI_INTTXCompleteEn(SPI_TypeDef *SPIx)
{
SPIx->IE |= (0x01 << SPI_IE_FTC_Pos);
SPIx->IE |= (0x01 << SPI_IE_FTC_Pos);
}
/******************************************************************************************************************************************
@ -465,9 +465,9 @@ void SPI_INTTXCompleteEn(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXCompleteDis(SPI_TypeDef * SPIx)
void SPI_INTTXCompleteDis(SPI_TypeDef *SPIx)
{
SPIx->IE &= ~(0x01 << SPI_IE_FTC_Pos);
SPIx->IE &= ~(0x01 << SPI_IE_FTC_Pos);
}
/******************************************************************************************************************************************
@ -477,9 +477,9 @@ void SPI_INTTXCompleteDis(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXCompleteClr(SPI_TypeDef * SPIx)
void SPI_INTTXCompleteClr(SPI_TypeDef *SPIx)
{
SPIx->IF = (1 << SPI_IF_FTC_Pos);
SPIx->IF = (1 << SPI_IF_FTC_Pos);
}
/******************************************************************************************************************************************
@ -489,9 +489,9 @@ void SPI_INTTXCompleteClr(SPI_TypeDef * SPIx)
* : uint32_t 1 FIFO空且发送移位寄存器空 0 FIFO或发送移位寄存器非空
* :
******************************************************************************************************************************************/
uint32_t SPI_INTTXCompleteStat(SPI_TypeDef * SPIx)
uint32_t SPI_INTTXCompleteStat(SPI_TypeDef *SPIx)
{
return (SPIx->IF & SPI_IF_FTC_Msk) ? 1 : 0;
return (SPIx->IF & SPI_IF_FTC_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -501,9 +501,9 @@ uint32_t SPI_INTTXCompleteStat(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXWordCompleteEn(SPI_TypeDef * SPIx)
void SPI_INTTXWordCompleteEn(SPI_TypeDef *SPIx)
{
SPIx->IE |= (0x01 << SPI_IE_WTC_Pos);
SPIx->IE |= (0x01 << SPI_IE_WTC_Pos);
}
/******************************************************************************************************************************************
@ -513,9 +513,9 @@ void SPI_INTTXWordCompleteEn(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXWordCompleteDis(SPI_TypeDef * SPIx)
void SPI_INTTXWordCompleteDis(SPI_TypeDef *SPIx)
{
SPIx->IE &= ~(0x01 << SPI_IE_WTC_Pos);
SPIx->IE &= ~(0x01 << SPI_IE_WTC_Pos);
}
/******************************************************************************************************************************************
@ -525,9 +525,9 @@ void SPI_INTTXWordCompleteDis(SPI_TypeDef * SPIx)
* :
* :
******************************************************************************************************************************************/
void SPI_INTTXWordCompleteClr(SPI_TypeDef * SPIx)
void SPI_INTTXWordCompleteClr(SPI_TypeDef *SPIx)
{
SPIx->IF = (1 << SPI_IF_WTC_Pos);
SPIx->IF = (1 << SPI_IF_WTC_Pos);
}
/******************************************************************************************************************************************
@ -537,7 +537,7 @@ void SPI_INTTXWordCompleteClr(SPI_TypeDef * SPIx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t SPI_INTTXWordCompleteStat(SPI_TypeDef * SPIx)
uint32_t SPI_INTTXWordCompleteStat(SPI_TypeDef *SPIx)
{
return (SPIx->IF & SPI_IF_WTC_Msk) ? 1 : 0;
return (SPIx->IF & SPI_IF_WTC_Msk) ? 1 : 0;
}

View File

@ -1,83 +1,80 @@
#ifndef __SWM320_SPI_H__
#define __SWM320_SPI_H__
typedef struct {
uint8_t FrameFormat; //帧格式SPI_FORMAT_SPI、SPI_FORMAT_TI_SSI
uint8_t SampleEdge; //在SPI帧格式下选择数据采样边沿SPI_FIRST_EDGE、SPI_SECOND_EDGE
uint8_t IdleLevel; //在SPI帧格式下选择空闲时无数据传输时时钟线的电平SPI_LOW_LEVEL、SPI_HIGH_LEVEL
uint8_t WordSize; //字长度, 有效值4-16
uint8_t Master; //1 主机模式 0 从机模式
uint8_t clkDiv; //SPI_CLK = SYS_CLK / clkDiv有效值SPI_CLKDIV_4、SPI_CLKDIV_8、... ... 、SPI_CLKDIV_512
uint8_t RXHFullIEn; //接收FIFO半满中断使能
uint8_t TXEmptyIEn; //发送FIFO 空中断使能
uint8_t TXCompleteIEn; //发送FIFO 空且发送移位寄存器空中断使能
typedef struct
{
uint8_t FrameFormat; //帧格式SPI_FORMAT_SPI、SPI_FORMAT_TI_SSI
uint8_t SampleEdge; //在SPI帧格式下选择数据采样边沿SPI_FIRST_EDGE、SPI_SECOND_EDGE
uint8_t IdleLevel; //在SPI帧格式下选择空闲时无数据传输时时钟线的电平SPI_LOW_LEVEL、SPI_HIGH_LEVEL
uint8_t WordSize; //字长度, 有效值4-16
uint8_t Master; //1 主机模式 0 从机模式
uint8_t clkDiv; //SPI_CLK = SYS_CLK / clkDiv有效值SPI_CLKDIV_4、SPI_CLKDIV_8、... ... 、SPI_CLKDIV_512
uint8_t RXHFullIEn; //接收FIFO半满中断使能
uint8_t TXEmptyIEn; //发送FIFO 空中断使能
uint8_t TXCompleteIEn; //发送FIFO 空且发送移位寄存器空中断使能
} SPI_InitStructure;
#define SPI_FORMAT_SPI 0 //Motorola SPI 格式
#define SPI_FORMAT_TI_SSI 1 //TI SSI 格式
#define SPI_FORMAT_SPI 0 //Motorola SPI 格式
#define SPI_FORMAT_TI_SSI 1 //TI SSI 格式
#define SPI_FIRST_EDGE 0 //第一个时钟沿开始采样
#define SPI_SECOND_EDGE 1 //第二个时钟沿开始采样
#define SPI_FIRST_EDGE 0 //第一个时钟沿开始采样
#define SPI_SECOND_EDGE 1 //第二个时钟沿开始采样
#define SPI_LOW_LEVEL 0 //空闲时时钟线保持低电平
#define SPI_HIGH_LEVEL 1 //空闲时时钟线保持高电平
#define SPI_LOW_LEVEL 0 //空闲时时钟线保持低电平
#define SPI_HIGH_LEVEL 1 //空闲时时钟线保持高电平
#define SPI_CLKDIV_4 0
#define SPI_CLKDIV_8 1
#define SPI_CLKDIV_16 2
#define SPI_CLKDIV_32 3
#define SPI_CLKDIV_64 4
#define SPI_CLKDIV_128 5
#define SPI_CLKDIV_256 6
#define SPI_CLKDIV_512 7
#define SPI_CLKDIV_4 0
#define SPI_CLKDIV_8 1
#define SPI_CLKDIV_16 2
#define SPI_CLKDIV_32 3
#define SPI_CLKDIV_64 4
#define SPI_CLKDIV_128 5
#define SPI_CLKDIV_256 6
#define SPI_CLKDIV_512 7
void SPI_Init(SPI_TypeDef *SPIx, SPI_InitStructure *initStruct); //SPI初始化
void SPI_Open(SPI_TypeDef *SPIx); //SPI打开允许收发
void SPI_Close(SPI_TypeDef *SPIx); //SPI关闭禁止收发
uint32_t SPI_Read(SPI_TypeDef *SPIx);
void SPI_Write(SPI_TypeDef *SPIx, uint32_t data);
void SPI_WriteWithWait(SPI_TypeDef *SPIx, uint32_t data);
uint32_t SPI_ReadWrite(SPI_TypeDef *SPIx, uint32_t data);
void SPI_Init(SPI_TypeDef * SPIx, SPI_InitStructure * initStruct); //SPI初始化
void SPI_Open(SPI_TypeDef * SPIx); //SPI打开允许收发
void SPI_Close(SPI_TypeDef * SPIx); //SPI关闭禁止收发
uint32_t SPI_IsRXEmpty(SPI_TypeDef *SPIx); //接收FIFO是否空如果不空则可以继续SPI_Read()
uint32_t SPI_IsTXFull(SPI_TypeDef *SPIx); //发送FIFO是否满如果不满则可以继续SPI_Write()
uint32_t SPI_IsTXEmpty(SPI_TypeDef *SPIx); //发送FIFO是否空
uint32_t SPI_Read(SPI_TypeDef * SPIx);
void SPI_Write(SPI_TypeDef * SPIx, uint32_t data);
void SPI_WriteWithWait(SPI_TypeDef * SPIx, uint32_t data);
uint32_t SPI_ReadWrite(SPI_TypeDef * SPIx, uint32_t data);
void SPI_INTRXHalfFullEn(SPI_TypeDef *SPIx);
void SPI_INTRXHalfFullDis(SPI_TypeDef *SPIx);
void SPI_INTRXHalfFullClr(SPI_TypeDef *SPIx);
uint32_t SPI_INTRXHalfFullStat(SPI_TypeDef *SPIx);
void SPI_INTRXFullEn(SPI_TypeDef *SPIx);
void SPI_INTRXFullDis(SPI_TypeDef *SPIx);
void SPI_INTRXFullClr(SPI_TypeDef *SPIx);
uint32_t SPI_INTRXFullStat(SPI_TypeDef *SPIx);
void SPI_INTRXOverflowEn(SPI_TypeDef *SPIx);
void SPI_INTRXOverflowDis(SPI_TypeDef *SPIx);
void SPI_INTRXOverflowClr(SPI_TypeDef *SPIx);
uint32_t SPI_INTRXOverflowStat(SPI_TypeDef *SPIx);
uint32_t SPI_IsRXEmpty(SPI_TypeDef * SPIx); //接收FIFO是否空如果不空则可以继续SPI_Read()
uint32_t SPI_IsTXFull(SPI_TypeDef * SPIx); //发送FIFO是否满如果不满则可以继续SPI_Write()
uint32_t SPI_IsTXEmpty(SPI_TypeDef * SPIx); //发送FIFO是否空
void SPI_INTRXHalfFullEn(SPI_TypeDef * SPIx);
void SPI_INTRXHalfFullDis(SPI_TypeDef * SPIx);
void SPI_INTRXHalfFullClr(SPI_TypeDef * SPIx);
uint32_t SPI_INTRXHalfFullStat(SPI_TypeDef * SPIx);
void SPI_INTRXFullEn(SPI_TypeDef * SPIx);
void SPI_INTRXFullDis(SPI_TypeDef * SPIx);
void SPI_INTRXFullClr(SPI_TypeDef * SPIx);
uint32_t SPI_INTRXFullStat(SPI_TypeDef * SPIx);
void SPI_INTRXOverflowEn(SPI_TypeDef * SPIx);
void SPI_INTRXOverflowDis(SPI_TypeDef * SPIx);
void SPI_INTRXOverflowClr(SPI_TypeDef * SPIx);
uint32_t SPI_INTRXOverflowStat(SPI_TypeDef * SPIx);
void SPI_INTTXHalfFullEn(SPI_TypeDef * SPIx);
void SPI_INTTXHalfFullDis(SPI_TypeDef * SPIx);
void SPI_INTTXHalfFullClr(SPI_TypeDef * SPIx);
uint32_t SPI_INTTXHalfFullStat(SPI_TypeDef * SPIx);
void SPI_INTTXEmptyEn(SPI_TypeDef * SPIx);
void SPI_INTTXEmptyDis(SPI_TypeDef * SPIx);
void SPI_INTTXEmptyClr(SPI_TypeDef * SPIx);
uint32_t SPI_INTTXEmptyStat(SPI_TypeDef * SPIx);
void SPI_INTTXCompleteEn(SPI_TypeDef * SPIx);
void SPI_INTTXCompleteDis(SPI_TypeDef * SPIx);
void SPI_INTTXCompleteClr(SPI_TypeDef * SPIx);
uint32_t SPI_INTTXCompleteStat(SPI_TypeDef * SPIx);
void SPI_INTTXWordCompleteEn(SPI_TypeDef * SPIx);
void SPI_INTTXWordCompleteDis(SPI_TypeDef * SPIx);
void SPI_INTTXWordCompleteClr(SPI_TypeDef * SPIx);
uint32_t SPI_INTTXWordCompleteStat(SPI_TypeDef * SPIx);
void SPI_INTTXHalfFullEn(SPI_TypeDef *SPIx);
void SPI_INTTXHalfFullDis(SPI_TypeDef *SPIx);
void SPI_INTTXHalfFullClr(SPI_TypeDef *SPIx);
uint32_t SPI_INTTXHalfFullStat(SPI_TypeDef *SPIx);
void SPI_INTTXEmptyEn(SPI_TypeDef *SPIx);
void SPI_INTTXEmptyDis(SPI_TypeDef *SPIx);
void SPI_INTTXEmptyClr(SPI_TypeDef *SPIx);
uint32_t SPI_INTTXEmptyStat(SPI_TypeDef *SPIx);
void SPI_INTTXCompleteEn(SPI_TypeDef *SPIx);
void SPI_INTTXCompleteDis(SPI_TypeDef *SPIx);
void SPI_INTTXCompleteClr(SPI_TypeDef *SPIx);
uint32_t SPI_INTTXCompleteStat(SPI_TypeDef *SPIx);
void SPI_INTTXWordCompleteEn(SPI_TypeDef *SPIx);
void SPI_INTTXWordCompleteDis(SPI_TypeDef *SPIx);
void SPI_INTTXWordCompleteClr(SPI_TypeDef *SPIx);
uint32_t SPI_INTTXWordCompleteStat(SPI_TypeDef *SPIx);
#endif //__SWM320_SPI_H__

View File

@ -21,7 +21,6 @@
#include "SWM320.h"
#include "SWM320_sram.h"
/******************************************************************************************************************************************
* : SRAM_Init()
* : SRAM控制器初始化
@ -29,25 +28,29 @@
* :
* :
******************************************************************************************************************************************/
void SRAM_Init(SRAM_InitStructure * initStruct)
void SRAM_Init(SRAM_InitStructure *initStruct)
{
uint32_t i;
// 配置SRAM前需要刷新下SDRAM控制器
do {
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
while(SDRAMC->REFDONE == 0);
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
for(i = 0; i < 1000; i++) __NOP();
SYS->CLKEN &= ~(1 << SYS_CLKEN_SDRAM_Pos);
} while(0);
SYS->CLKEN |= (1 << SYS_CLKEN_RAMC_Pos);
for(i = 0; i < 10; i++) __NOP();
SRAMC->CR = (initStruct->ClkDiv << SRAMC_CR_RWTIME_Pos) |
(initStruct->DataWidth << SRAMC_CR_BYTEIF_Pos) |
(0 << SRAMC_CR_HBLBDIS_Pos); // 使能字节、半字访问
uint32_t i;
// 配置SRAM前需要刷新下SDRAM控制器
do
{
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
while (SDRAMC->REFDONE == 0)
;
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
for (i = 0; i < 1000; i++)
__NOP();
SYS->CLKEN &= ~(1 << SYS_CLKEN_SDRAM_Pos);
} while (0);
SYS->CLKEN |= (1 << SYS_CLKEN_RAMC_Pos);
for (i = 0; i < 10; i++)
__NOP();
SRAMC->CR = (initStruct->ClkDiv << SRAMC_CR_RWTIME_Pos) |
(initStruct->DataWidth << SRAMC_CR_BYTEIF_Pos) |
(0 << SRAMC_CR_HBLBDIS_Pos); // 使能字节、半字访问
}

View File

@ -1,31 +1,29 @@
#ifndef __SWM320_SRAM_H__
#define __SWM320_SRAM_H__
typedef struct {
uint8_t ClkDiv; //SRAM_CLKDIV_5...SRAM_CLKDIV_16根据SRAM芯片所能跑的最高频率选择合适分频
uint8_t DataWidth; //SRAM_DATAWIDTH_8、SRAM_DATAWIDTH_16
typedef struct
{
uint8_t ClkDiv; //SRAM_CLKDIV_5...SRAM_CLKDIV_16根据SRAM芯片所能跑的最高频率选择合适分频
uint8_t DataWidth; //SRAM_DATAWIDTH_8、SRAM_DATAWIDTH_16
} SRAM_InitStructure;
#define SRAM_CLKDIV_4 3
#define SRAM_CLKDIV_5 4
#define SRAM_CLKDIV_6 5
#define SRAM_CLKDIV_7 6
#define SRAM_CLKDIV_8 7
#define SRAM_CLKDIV_9 8
#define SRAM_CLKDIV_10 9
#define SRAM_CLKDIV_11 10
#define SRAM_CLKDIV_12 11
#define SRAM_CLKDIV_13 12
#define SRAM_CLKDIV_14 13
#define SRAM_CLKDIV_15 14
#define SRAM_CLKDIV_16 15
#define SRAM_CLKDIV_4 3
#define SRAM_CLKDIV_5 4
#define SRAM_CLKDIV_6 5
#define SRAM_CLKDIV_7 6
#define SRAM_CLKDIV_8 7
#define SRAM_CLKDIV_9 8
#define SRAM_CLKDIV_10 9
#define SRAM_CLKDIV_11 10
#define SRAM_CLKDIV_12 11
#define SRAM_CLKDIV_13 12
#define SRAM_CLKDIV_14 13
#define SRAM_CLKDIV_15 14
#define SRAM_CLKDIV_16 15
#define SRAM_DATAWIDTH_8 1
#define SRAM_DATAWIDTH_16 0
void SRAM_Init(SRAM_InitStructure * initStruct);
#define SRAM_DATAWIDTH_8 1
#define SRAM_DATAWIDTH_16 0
void SRAM_Init(SRAM_InitStructure *initStruct);
#endif //__SWM320_SRAM_H__

View File

@ -21,7 +21,6 @@
#include "SWM320.h"
#include "SWM320_timr.h"
/******************************************************************************************************************************************
* : TIMR_Init()
* : TIMR定时器/
@ -32,67 +31,73 @@
* :
* :
******************************************************************************************************************************************/
void TIMR_Init(TIMR_TypeDef * TIMRx, uint32_t mode, uint32_t period, uint32_t int_en)
void TIMR_Init(TIMR_TypeDef *TIMRx, uint32_t mode, uint32_t period, uint32_t int_en)
{
SYS->CLKEN |= (0x01 << SYS_CLKEN_TIMR_Pos);
TIMR_Stop(TIMRx); //一些关键寄存器只能在定时器停止时设置
TIMRx->CTRL &= ~TIMR_CTRL_CLKSRC_Msk;
TIMRx->CTRL |= mode << TIMR_CTRL_CLKSRC_Pos;
TIMRx->LDVAL = period;
switch((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IF = (1 << TIMRG_IF_TIMR0_Pos); //使能中断前清除中断标志
TIMRG->IE &= ~TIMRG_IE_TIMR0_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR0_Pos);
if(int_en) NVIC_EnableIRQ(TIMR0_IRQn);
break;
case ((uint32_t)TIMR1):
TIMRG->IF = (1 << TIMRG_IF_TIMR1_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR1_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR1_Pos);
if(int_en) NVIC_EnableIRQ(TIMR1_IRQn);
break;
case ((uint32_t)TIMR2):
TIMRG->IF = (1 << TIMRG_IF_TIMR2_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR2_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR2_Pos);
if(int_en) NVIC_EnableIRQ(TIMR2_IRQn);
break;
case ((uint32_t)TIMR3):
TIMRG->IF = (1 << TIMRG_IF_TIMR3_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR3_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR3_Pos);
if(int_en) NVIC_EnableIRQ(TIMR3_IRQn);
break;
case ((uint32_t)TIMR4):
TIMRG->IF = (1 << TIMRG_IF_TIMR4_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR4_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR4_Pos);
if(int_en) NVIC_EnableIRQ(TIMR4_IRQn);
break;
case ((uint32_t)TIMR5):
TIMRG->IF = (1 << TIMRG_IF_TIMR5_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR5_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR5_Pos);
if(int_en) NVIC_EnableIRQ(TIMR5_IRQn);
break;
}
SYS->CLKEN |= (0x01 << SYS_CLKEN_TIMR_Pos);
TIMR_Stop(TIMRx); //一些关键寄存器只能在定时器停止时设置
TIMRx->CTRL &= ~TIMR_CTRL_CLKSRC_Msk;
TIMRx->CTRL |= mode << TIMR_CTRL_CLKSRC_Pos;
TIMRx->LDVAL = period;
switch ((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IF = (1 << TIMRG_IF_TIMR0_Pos); //使能中断前清除中断标志
TIMRG->IE &= ~TIMRG_IE_TIMR0_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR0_Pos);
if (int_en)
NVIC_EnableIRQ(TIMR0_IRQn);
break;
case ((uint32_t)TIMR1):
TIMRG->IF = (1 << TIMRG_IF_TIMR1_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR1_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR1_Pos);
if (int_en)
NVIC_EnableIRQ(TIMR1_IRQn);
break;
case ((uint32_t)TIMR2):
TIMRG->IF = (1 << TIMRG_IF_TIMR2_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR2_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR2_Pos);
if (int_en)
NVIC_EnableIRQ(TIMR2_IRQn);
break;
case ((uint32_t)TIMR3):
TIMRG->IF = (1 << TIMRG_IF_TIMR3_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR3_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR3_Pos);
if (int_en)
NVIC_EnableIRQ(TIMR3_IRQn);
break;
case ((uint32_t)TIMR4):
TIMRG->IF = (1 << TIMRG_IF_TIMR4_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR4_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR4_Pos);
if (int_en)
NVIC_EnableIRQ(TIMR4_IRQn);
break;
case ((uint32_t)TIMR5):
TIMRG->IF = (1 << TIMRG_IF_TIMR5_Pos);
TIMRG->IE &= ~TIMRG_IE_TIMR5_Msk;
TIMRG->IE |= (int_en << TIMRG_IE_TIMR5_Pos);
if (int_en)
NVIC_EnableIRQ(TIMR5_IRQn);
break;
}
}
/******************************************************************************************************************************************
@ -102,9 +107,9 @@ void TIMR_Init(TIMR_TypeDef * TIMRx, uint32_t mode, uint32_t period, uint32_t in
* :
* :
******************************************************************************************************************************************/
void TIMR_Start(TIMR_TypeDef * TIMRx)
void TIMR_Start(TIMR_TypeDef *TIMRx)
{
TIMRx->CTRL |= TIMR_CTRL_EN_Msk;
TIMRx->CTRL |= TIMR_CTRL_EN_Msk;
}
/******************************************************************************************************************************************
@ -114,9 +119,9 @@ void TIMR_Start(TIMR_TypeDef * TIMRx)
* :
* :
******************************************************************************************************************************************/
void TIMR_Stop(TIMR_TypeDef * TIMRx)
void TIMR_Stop(TIMR_TypeDef *TIMRx)
{
TIMRx->CTRL &= ~TIMR_CTRL_EN_Msk;
TIMRx->CTRL &= ~TIMR_CTRL_EN_Msk;
}
/******************************************************************************************************************************************
@ -126,34 +131,34 @@ void TIMR_Stop(TIMR_TypeDef * TIMRx)
* :
* :
******************************************************************************************************************************************/
void TIMR_Halt(TIMR_TypeDef * TIMRx)
void TIMR_Halt(TIMR_TypeDef *TIMRx)
{
switch((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR5_Pos);
break;
}
switch ((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR5_Pos);
break;
}
}
/******************************************************************************************************************************************
@ -163,34 +168,34 @@ void TIMR_Halt(TIMR_TypeDef * TIMRx)
* :
* :
******************************************************************************************************************************************/
void TIMR_Resume(TIMR_TypeDef * TIMRx)
void TIMR_Resume(TIMR_TypeDef *TIMRx)
{
switch((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR5_Pos);
break;
}
switch ((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR5_Pos);
break;
}
}
/******************************************************************************************************************************************
@ -201,9 +206,9 @@ void TIMR_Resume(TIMR_TypeDef * TIMRx)
* :
* :
******************************************************************************************************************************************/
void TIMR_SetPeriod(TIMR_TypeDef * TIMRx, uint32_t period)
void TIMR_SetPeriod(TIMR_TypeDef *TIMRx, uint32_t period)
{
TIMRx->LDVAL = period;
TIMRx->LDVAL = period;
}
/******************************************************************************************************************************************
@ -213,9 +218,9 @@ void TIMR_SetPeriod(TIMR_TypeDef * TIMRx, uint32_t period)
* : uint32_t /
* :
******************************************************************************************************************************************/
uint32_t TIMR_GetPeriod(TIMR_TypeDef * TIMRx)
uint32_t TIMR_GetPeriod(TIMR_TypeDef *TIMRx)
{
return TIMRx->LDVAL;
return TIMRx->LDVAL;
}
/******************************************************************************************************************************************
@ -225,9 +230,9 @@ uint32_t TIMR_GetPeriod(TIMR_TypeDef * TIMRx)
* : uint32_t
* :
******************************************************************************************************************************************/
uint32_t TIMR_GetCurValue(TIMR_TypeDef * TIMRx)
uint32_t TIMR_GetCurValue(TIMR_TypeDef *TIMRx)
{
return TIMRx->CVAL;
return TIMRx->CVAL;
}
/******************************************************************************************************************************************
@ -237,40 +242,40 @@ uint32_t TIMR_GetCurValue(TIMR_TypeDef * TIMRx)
* :
* :
******************************************************************************************************************************************/
void TIMR_INTEn(TIMR_TypeDef * TIMRx)
void TIMR_INTEn(TIMR_TypeDef *TIMRx)
{
switch((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR0_Pos);
NVIC_EnableIRQ(TIMR0_IRQn);
break;
case ((uint32_t)TIMR1):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR1_Pos);
NVIC_EnableIRQ(TIMR1_IRQn);
break;
case ((uint32_t)TIMR2):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR2_Pos);
NVIC_EnableIRQ(TIMR2_IRQn);
break;
case ((uint32_t)TIMR3):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR3_Pos);
NVIC_EnableIRQ(TIMR3_IRQn);
break;
case ((uint32_t)TIMR4):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR4_Pos);
NVIC_EnableIRQ(TIMR4_IRQn);
break;
case ((uint32_t)TIMR5):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR5_Pos);
NVIC_EnableIRQ(TIMR5_IRQn);
break;
}
switch ((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR0_Pos);
NVIC_EnableIRQ(TIMR0_IRQn);
break;
case ((uint32_t)TIMR1):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR1_Pos);
NVIC_EnableIRQ(TIMR1_IRQn);
break;
case ((uint32_t)TIMR2):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR2_Pos);
NVIC_EnableIRQ(TIMR2_IRQn);
break;
case ((uint32_t)TIMR3):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR3_Pos);
NVIC_EnableIRQ(TIMR3_IRQn);
break;
case ((uint32_t)TIMR4):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR4_Pos);
NVIC_EnableIRQ(TIMR4_IRQn);
break;
case ((uint32_t)TIMR5):
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR5_Pos);
NVIC_EnableIRQ(TIMR5_IRQn);
break;
}
}
/******************************************************************************************************************************************
@ -280,34 +285,34 @@ void TIMR_INTEn(TIMR_TypeDef * TIMRx)
* :
* :
******************************************************************************************************************************************/
void TIMR_INTDis(TIMR_TypeDef * TIMRx)
void TIMR_INTDis(TIMR_TypeDef *TIMRx)
{
switch((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR5_Pos);
break;
}
switch ((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR5_Pos);
break;
}
}
/******************************************************************************************************************************************
@ -317,34 +322,34 @@ void TIMR_INTDis(TIMR_TypeDef * TIMRx)
* :
* :
******************************************************************************************************************************************/
void TIMR_INTClr(TIMR_TypeDef * TIMRx)
void TIMR_INTClr(TIMR_TypeDef *TIMRx)
{
switch((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR5_Pos);
break;
}
switch ((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR0_Pos);
break;
case ((uint32_t)TIMR1):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR1_Pos);
break;
case ((uint32_t)TIMR2):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR2_Pos);
break;
case ((uint32_t)TIMR3):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR3_Pos);
break;
case ((uint32_t)TIMR4):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR4_Pos);
break;
case ((uint32_t)TIMR5):
TIMRG->IF = (0x01 << TIMRG_IF_TIMR5_Pos);
break;
}
}
/******************************************************************************************************************************************
@ -354,32 +359,31 @@ void TIMR_INTClr(TIMR_TypeDef * TIMRx)
* : uint32_t 0 TIMRx未产生中断 1 TIMRx产生了中断
* :
******************************************************************************************************************************************/
uint32_t TIMR_INTStat(TIMR_TypeDef * TIMRx)
uint32_t TIMR_INTStat(TIMR_TypeDef *TIMRx)
{
switch((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
return (TIMRG->IF & TIMRG_IF_TIMR0_Msk) ? 1 : 0;
case ((uint32_t)TIMR1):
return (TIMRG->IF & TIMRG_IF_TIMR1_Msk) ? 1 : 0;
case ((uint32_t)TIMR2):
return (TIMRG->IF & TIMRG_IF_TIMR2_Msk) ? 1 : 0;
case ((uint32_t)TIMR3):
return (TIMRG->IF & TIMRG_IF_TIMR3_Msk) ? 1 : 0;
case ((uint32_t)TIMR4):
return (TIMRG->IF & TIMRG_IF_TIMR4_Msk) ? 1 : 0;
case ((uint32_t)TIMR5):
return (TIMRG->IF & TIMRG_IF_TIMR5_Msk) ? 1 : 0;
}
return 0;
}
switch ((uint32_t)TIMRx)
{
case ((uint32_t)TIMR0):
return (TIMRG->IF & TIMRG_IF_TIMR0_Msk) ? 1 : 0;
case ((uint32_t)TIMR1):
return (TIMRG->IF & TIMRG_IF_TIMR1_Msk) ? 1 : 0;
case ((uint32_t)TIMR2):
return (TIMRG->IF & TIMRG_IF_TIMR2_Msk) ? 1 : 0;
case ((uint32_t)TIMR3):
return (TIMRG->IF & TIMRG_IF_TIMR3_Msk) ? 1 : 0;
case ((uint32_t)TIMR4):
return (TIMRG->IF & TIMRG_IF_TIMR4_Msk) ? 1 : 0;
case ((uint32_t)TIMR5):
return (TIMRG->IF & TIMRG_IF_TIMR5_Msk) ? 1 : 0;
}
return 0;
}
/******************************************************************************************************************************************
* : Pulse_Init()
@ -390,16 +394,17 @@ uint32_t TIMR_INTStat(TIMR_TypeDef * TIMRx)
* :
******************************************************************************************************************************************/
void Pulse_Init(uint32_t pulse, uint32_t int_en)
{
SYS->CLKEN |= (0x01 << SYS_CLKEN_TIMR_Pos);
TIMRG->PCTRL = (0 << TIMRG_PCTRL_CLKSRC_Pos) | // 系统时钟作为时钟源
(pulse << TIMRG_PCTRL_HIGH_Pos) |
(0 << TIMRG_PCTRL_EN_Pos);
TIMRG->IE |= (1 << TIMRG_IE_PULSE_Pos); //使能才能查询中断标志
if(int_en) NVIC_EnableIRQ(PULSE_IRQn);
{
SYS->CLKEN |= (0x01 << SYS_CLKEN_TIMR_Pos);
TIMRG->PCTRL = (0 << TIMRG_PCTRL_CLKSRC_Pos) | // 系统时钟作为时钟源
(pulse << TIMRG_PCTRL_HIGH_Pos) |
(0 << TIMRG_PCTRL_EN_Pos);
TIMRG->IE |= (1 << TIMRG_IE_PULSE_Pos); //使能才能查询中断标志
if (int_en)
NVIC_EnableIRQ(PULSE_IRQn);
}
/******************************************************************************************************************************************
@ -410,8 +415,8 @@ void Pulse_Init(uint32_t pulse, uint32_t int_en)
* :
******************************************************************************************************************************************/
void Pulse_Start(void)
{
TIMRG->PCTRL |= (1 << TIMRG_PCTRL_EN_Pos);
{
TIMRG->PCTRL |= (1 << TIMRG_PCTRL_EN_Pos);
}
/******************************************************************************************************************************************
@ -423,14 +428,14 @@ void Pulse_Start(void)
******************************************************************************************************************************************/
uint32_t Pulse_Done(void)
{
if(TIMRG->IF & TIMRG_IF_PULSE_Msk)
{
TIMRG->IF = TIMRG_IF_PULSE_Msk; // 清除中断标志
return 1;
}
else
{
return 0;
}
if (TIMRG->IF & TIMRG_IF_PULSE_Msk)
{
TIMRG->IF = TIMRG_IF_PULSE_Msk; // 清除中断标志
return 1;
}
else
{
return 0;
}
}

View File

@ -1,32 +1,29 @@
#ifndef __SWM320_TIMR_H__
#define __SWM320_TIMR_H__
#define TIMR_MODE_TIMER 0
#define TIMR_MODE_COUNTER 1
#define TIMR_MODE_TIMER 0
#define TIMR_MODE_COUNTER 1
void TIMR_Init(TIMR_TypeDef * TIMRx, uint32_t mode, uint32_t period, uint32_t int_en); //定时器/计数器初始化
void TIMR_Start(TIMR_TypeDef * TIMRx); //启动定时器,从初始值开始计时/计数
void TIMR_Stop(TIMR_TypeDef * TIMRx); //停止定时器
void TIMR_Halt(TIMR_TypeDef * TIMRx); //暂停定时器,计数值保持不变
void TIMR_Resume(TIMR_TypeDef * TIMRx); //恢复定时器,从暂停处继续计数
void TIMR_Init(TIMR_TypeDef *TIMRx, uint32_t mode, uint32_t period, uint32_t int_en); //定时器/计数器初始化
void TIMR_Start(TIMR_TypeDef *TIMRx); //启动定时器,从初始值开始计时/计数
void TIMR_Stop(TIMR_TypeDef *TIMRx); //停止定时器
void TIMR_Halt(TIMR_TypeDef *TIMRx); //暂停定时器,计数值保持不变
void TIMR_Resume(TIMR_TypeDef *TIMRx); //恢复定时器,从暂停处继续计数
void TIMR_SetPeriod(TIMR_TypeDef * TIMRx, uint32_t period); //设置定时/计数周期
uint32_t TIMR_GetPeriod(TIMR_TypeDef * TIMRx); //获取定时/计数周期
uint32_t TIMR_GetCurValue(TIMR_TypeDef * TIMRx); //获取当前计数值
void TIMR_SetPeriod(TIMR_TypeDef *TIMRx, uint32_t period); //设置定时/计数周期
uint32_t TIMR_GetPeriod(TIMR_TypeDef *TIMRx); //获取定时/计数周期
uint32_t TIMR_GetCurValue(TIMR_TypeDef *TIMRx); //获取当前计数值
void TIMR_INTEn(TIMR_TypeDef * TIMRx); //使能中断
void TIMR_INTDis(TIMR_TypeDef * TIMRx); //禁能中断
void TIMR_INTClr(TIMR_TypeDef * TIMRx); //清除中断标志
uint32_t TIMR_INTStat(TIMR_TypeDef * TIMRx); //获取中断状态
void TIMR_INTEn(TIMR_TypeDef *TIMRx); //使能中断
void TIMR_INTDis(TIMR_TypeDef *TIMRx); //禁能中断
void TIMR_INTClr(TIMR_TypeDef *TIMRx); //清除中断标志
uint32_t TIMR_INTStat(TIMR_TypeDef *TIMRx); //获取中断状态
#define PULSE_LOW 0
#define PULSE_HIGH 1
#define PULSE_LOW 0
#define PULSE_HIGH 1
void Pulse_Init(uint32_t pulse, uint32_t int_en);
void Pulse_Start(void);
uint32_t Pulse_Done(void);
#endif //__SWM320_TIMR_H__

View File

@ -21,7 +21,6 @@
#include "SWM320.h"
#include "SWM320_uart.h"
/******************************************************************************************************************************************
* : UART_Init()
* : UART串口初始化
@ -30,106 +29,114 @@
* :
* :
******************************************************************************************************************************************/
void UART_Init(UART_TypeDef * UARTx, UART_InitStructure * initStruct)
{
switch((uint32_t)UARTx)
{
case ((uint32_t)UART0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART0_Pos);
break;
case ((uint32_t)UART1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART1_Pos);
break;
case ((uint32_t)UART2):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART2_Pos);
break;
case ((uint32_t)UART3):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART3_Pos);
break;
}
UART_Close(UARTx); //一些关键寄存器只能在串口关闭时设置
UARTx->CTRL |= (0x01 << UART_CTRL_BAUDEN_Pos);
UARTx->BAUD &= ~UART_BAUD_BAUD_Msk;
UARTx->BAUD |= ((SystemCoreClock/16/initStruct->Baudrate - 1) << UART_BAUD_BAUD_Pos);
UARTx->CTRL &= ~(UART_CTRL_DATA9b_Msk | UART_CTRL_PARITY_Msk | UART_CTRL_STOP2b_Msk);
UARTx->CTRL |= (initStruct->DataBits << UART_CTRL_DATA9b_Pos) |
(initStruct->Parity << UART_CTRL_PARITY_Pos) |
(initStruct->StopBits << UART_CTRL_STOP2b_Pos);
/* 在SWM320中当 RXLVL >= RXTHR 时触发中断如果RXTHR设置为0的话在未接收到数据时就会一直触发中断
void UART_Init(UART_TypeDef *UARTx, UART_InitStructure *initStruct)
{
switch ((uint32_t)UARTx)
{
case ((uint32_t)UART0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART0_Pos);
break;
case ((uint32_t)UART1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART1_Pos);
break;
case ((uint32_t)UART2):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART2_Pos);
break;
case ((uint32_t)UART3):
SYS->CLKEN |= (0x01 << SYS_CLKEN_UART3_Pos);
break;
}
UART_Close(UARTx); //一些关键寄存器只能在串口关闭时设置
UARTx->CTRL |= (0x01 << UART_CTRL_BAUDEN_Pos);
UARTx->BAUD &= ~UART_BAUD_BAUD_Msk;
UARTx->BAUD |= ((SystemCoreClock / 16 / initStruct->Baudrate - 1) << UART_BAUD_BAUD_Pos);
UARTx->CTRL &= ~(UART_CTRL_DATA9b_Msk | UART_CTRL_PARITY_Msk | UART_CTRL_STOP2b_Msk);
UARTx->CTRL |= (initStruct->DataBits << UART_CTRL_DATA9b_Pos) |
(initStruct->Parity << UART_CTRL_PARITY_Pos) |
(initStruct->StopBits << UART_CTRL_STOP2b_Pos);
/* 在SWM320中当 RXLVL >= RXTHR 时触发中断如果RXTHR设置为0的话在未接收到数据时就会一直触发中断
RXLVL > RXTHR SWM320中RXTHR不能为0的问题APIRXTHR设置值加一
*/
switch((uint32_t)UARTx) // 软件复位不能清零 NVIC 寄存器,若不手动清除,下面的代码清零 RXTHR 时会导致一直进入 ISR
switch ((uint32_t)UARTx) // 软件复位不能清零 NVIC 寄存器,若不手动清除,下面的代码清零 RXTHR 时会导致一直进入 ISR
{
case ((uint32_t)UART0): NVIC_DisableIRQ(UART0_IRQn); break;
case ((uint32_t)UART1): NVIC_DisableIRQ(UART1_IRQn); break;
case ((uint32_t)UART2): NVIC_DisableIRQ(UART2_IRQn); break;
case ((uint32_t)UART3): NVIC_DisableIRQ(UART3_IRQn); break;
case ((uint32_t)UART0):
NVIC_DisableIRQ(UART0_IRQn);
break;
case ((uint32_t)UART1):
NVIC_DisableIRQ(UART1_IRQn);
break;
case ((uint32_t)UART2):
NVIC_DisableIRQ(UART2_IRQn);
break;
case ((uint32_t)UART3):
NVIC_DisableIRQ(UART3_IRQn);
break;
}
UARTx->FIFO &= ~(UART_FIFO_RXTHR_Msk | UART_FIFO_TXTHR_Msk);
UARTx->FIFO |= ((initStruct->RXThreshold + 1) << UART_FIFO_RXTHR_Pos) |
(initStruct->TXThreshold << UART_FIFO_TXTHR_Pos);
UARTx->CTRL &= ~UART_CTRL_TOTIME_Msk;
UARTx->CTRL |= (initStruct->TimeoutTime << UART_CTRL_TOTIME_Pos);
UARTx->CTRL &= ~(UART_CTRL_RXIE_Msk | UART_CTRL_TXIE_Msk | UART_CTRL_TOIE_Msk);
UARTx->CTRL |= (initStruct->RXThresholdIEn << UART_CTRL_RXIE_Pos) |
(initStruct->TXThresholdIEn << UART_CTRL_TXIE_Pos) |
(initStruct->TimeoutIEn << UART_CTRL_TOIE_Pos);
switch ((uint32_t)UARTx)
{
case ((uint32_t)UART0):
if (initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART0_IRQn);
}
else
{
NVIC_DisableIRQ(UART0_IRQn);
}
break;
case ((uint32_t)UART1):
if (initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART1_IRQn);
}
else
{
NVIC_DisableIRQ(UART1_IRQn);
}
break;
case ((uint32_t)UART2):
if (initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART2_IRQn);
}
else
{
NVIC_DisableIRQ(UART2_IRQn);
}
break;
case ((uint32_t)UART3):
if (initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART3_IRQn);
}
else
{
NVIC_DisableIRQ(UART3_IRQn);
}
break;
}
UARTx->FIFO &= ~(UART_FIFO_RXTHR_Msk | UART_FIFO_TXTHR_Msk);
UARTx->FIFO |= ((initStruct->RXThreshold + 1) << UART_FIFO_RXTHR_Pos) |
(initStruct->TXThreshold << UART_FIFO_TXTHR_Pos);
UARTx->CTRL &= ~UART_CTRL_TOTIME_Msk;
UARTx->CTRL |= (initStruct->TimeoutTime << UART_CTRL_TOTIME_Pos);
UARTx->CTRL &= ~(UART_CTRL_RXIE_Msk | UART_CTRL_TXIE_Msk | UART_CTRL_TOIE_Msk);
UARTx->CTRL |= (initStruct->RXThresholdIEn << UART_CTRL_RXIE_Pos) |
(initStruct->TXThresholdIEn << UART_CTRL_TXIE_Pos) |
(initStruct->TimeoutIEn << UART_CTRL_TOIE_Pos);
switch((uint32_t)UARTx)
{
case ((uint32_t)UART0):
if(initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART0_IRQn);
}
else
{
NVIC_DisableIRQ(UART0_IRQn);
}
break;
case ((uint32_t)UART1):
if(initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART1_IRQn);
}
else
{
NVIC_DisableIRQ(UART1_IRQn);
}
break;
case ((uint32_t)UART2):
if(initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART2_IRQn);
}
else
{
NVIC_DisableIRQ(UART2_IRQn);
}
break;
case ((uint32_t)UART3):
if(initStruct->RXThresholdIEn | initStruct->TXThresholdIEn | initStruct->TimeoutIEn)
{
NVIC_EnableIRQ(UART3_IRQn);
}
else
{
NVIC_DisableIRQ(UART3_IRQn);
}
break;
}
}
/******************************************************************************************************************************************
@ -139,9 +146,9 @@ void UART_Init(UART_TypeDef * UARTx, UART_InitStructure * initStruct)
* :
* :
******************************************************************************************************************************************/
void UART_Open(UART_TypeDef * UARTx)
void UART_Open(UART_TypeDef *UARTx)
{
UARTx->CTRL |= (0x01 << UART_CTRL_EN_Pos);
UARTx->CTRL |= (0x01 << UART_CTRL_EN_Pos);
}
/******************************************************************************************************************************************
@ -151,9 +158,9 @@ void UART_Open(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_Close(UART_TypeDef * UARTx)
void UART_Close(UART_TypeDef *UARTx)
{
UARTx->CTRL &= ~(0x01 << UART_CTRL_EN_Pos);
UARTx->CTRL &= ~(0x01 << UART_CTRL_EN_Pos);
}
/******************************************************************************************************************************************
@ -164,9 +171,9 @@ void UART_Close(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_WriteByte(UART_TypeDef * UARTx, uint32_t data)
void UART_WriteByte(UART_TypeDef *UARTx, uint32_t data)
{
UARTx->DATA = data;
UARTx->DATA = data;
}
/******************************************************************************************************************************************
@ -177,15 +184,16 @@ void UART_WriteByte(UART_TypeDef * UARTx, uint32_t data)
* : uint32_t 0 UART_ERR_PARITY
* :
******************************************************************************************************************************************/
uint32_t UART_ReadByte(UART_TypeDef * UARTx, uint32_t * data)
uint32_t UART_ReadByte(UART_TypeDef *UARTx, uint32_t *data)
{
uint32_t reg = UARTx->DATA;
*data = (reg & UART_DATA_DATA_Msk);
if(reg & UART_DATA_PAERR_Msk) return UART_ERR_PARITY;
return 0;
uint32_t reg = UARTx->DATA;
*data = (reg & UART_DATA_DATA_Msk);
if (reg & UART_DATA_PAERR_Msk)
return UART_ERR_PARITY;
return 0;
}
/******************************************************************************************************************************************
@ -195,9 +203,9 @@ uint32_t UART_ReadByte(UART_TypeDef * UARTx, uint32_t * data)
* : uint32_t 1 UART正在发送数据 0
* :
******************************************************************************************************************************************/
uint32_t UART_IsTXBusy(UART_TypeDef * UARTx)
uint32_t UART_IsTXBusy(UART_TypeDef *UARTx)
{
return (UARTx->CTRL & UART_CTRL_TXIDLE_Msk) ? 0 : 1;
return (UARTx->CTRL & UART_CTRL_TXIDLE_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
@ -207,9 +215,9 @@ uint32_t UART_IsTXBusy(UART_TypeDef * UARTx)
* : uint32_t 1 FIFO空 0 FIFO非空
* :
******************************************************************************************************************************************/
uint32_t UART_IsRXFIFOEmpty(UART_TypeDef * UARTx)
uint32_t UART_IsRXFIFOEmpty(UART_TypeDef *UARTx)
{
return (UARTx->CTRL & UART_CTRL_RXNE_Msk) ? 0 : 1;
return (UARTx->CTRL & UART_CTRL_RXNE_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
@ -219,9 +227,9 @@ uint32_t UART_IsRXFIFOEmpty(UART_TypeDef * UARTx)
* : uint32_t 1 FIFO满 0 FIFO不满
* :
******************************************************************************************************************************************/
uint32_t UART_IsTXFIFOFull(UART_TypeDef * UARTx)
uint32_t UART_IsTXFIFOFull(UART_TypeDef *UARTx)
{
return (UARTx->CTRL & UART_CTRL_TXFF_Msk) ? 1 : 0;
return (UARTx->CTRL & UART_CTRL_TXFF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -232,10 +240,10 @@ uint32_t UART_IsTXFIFOFull(UART_TypeDef * UARTx)
* :
* : 使UART_Close()
******************************************************************************************************************************************/
void UART_SetBaudrate(UART_TypeDef * UARTx, uint32_t baudrate)
void UART_SetBaudrate(UART_TypeDef *UARTx, uint32_t baudrate)
{
UARTx->BAUD &= ~UART_BAUD_BAUD_Msk;
UARTx->BAUD |= ((SystemCoreClock/16/baudrate - 1) << UART_BAUD_BAUD_Pos);
UARTx->BAUD &= ~UART_BAUD_BAUD_Msk;
UARTx->BAUD |= ((SystemCoreClock / 16 / baudrate - 1) << UART_BAUD_BAUD_Pos);
}
/******************************************************************************************************************************************
@ -245,9 +253,9 @@ void UART_SetBaudrate(UART_TypeDef * UARTx, uint32_t baudrate)
* : uint32_t
* :
******************************************************************************************************************************************/
uint32_t UART_GetBaudrate(UART_TypeDef * UARTx)
uint32_t UART_GetBaudrate(UART_TypeDef *UARTx)
{
return SystemCoreClock/16/(((UARTx->BAUD & UART_BAUD_BAUD_Msk) >> UART_BAUD_BAUD_Pos) + 1);
return SystemCoreClock / 16 / (((UARTx->BAUD & UART_BAUD_BAUD_Msk) >> UART_BAUD_BAUD_Pos) + 1);
}
/******************************************************************************************************************************************
@ -259,11 +267,11 @@ uint32_t UART_GetBaudrate(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_CTSConfig(UART_TypeDef * UARTx, uint32_t enable, uint32_t polarity)
void UART_CTSConfig(UART_TypeDef *UARTx, uint32_t enable, uint32_t polarity)
{
UARTx->CTSCR &= ~(UART_CTSCR_EN_Msk | UART_CTSCR_POL_Msk);
UARTx->CTSCR |= (enable << UART_CTSCR_EN_Pos) |
(polarity << UART_CTSCR_POL_Pos);
UARTx->CTSCR &= ~(UART_CTSCR_EN_Msk | UART_CTSCR_POL_Msk);
UARTx->CTSCR |= (enable << UART_CTSCR_EN_Pos) |
(polarity << UART_CTSCR_POL_Pos);
}
/******************************************************************************************************************************************
@ -273,9 +281,9 @@ void UART_CTSConfig(UART_TypeDef * UARTx, uint32_t enable, uint32_t polarity)
* : uint32_t 0 CTS线当前为低电平 1 CTS线当前为高电平
* :
******************************************************************************************************************************************/
uint32_t UART_CTSLineState(UART_TypeDef * UARTx)
uint32_t UART_CTSLineState(UART_TypeDef *UARTx)
{
return (UARTx->CTSCR & UART_CTSCR_STAT_Msk) ? 1 : 0;
return (UARTx->CTSCR & UART_CTSCR_STAT_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -288,12 +296,12 @@ uint32_t UART_CTSLineState(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_RTSConfig(UART_TypeDef * UARTx, uint32_t enable, uint32_t polarity, uint32_t threshold)
void UART_RTSConfig(UART_TypeDef *UARTx, uint32_t enable, uint32_t polarity, uint32_t threshold)
{
UARTx->RTSCR &= ~(UART_RTSCR_EN_Msk | UART_RTSCR_POL_Msk | UART_RTSCR_THR_Msk);
UARTx->RTSCR |= (enable << UART_RTSCR_EN_Pos) |
(polarity << UART_RTSCR_POL_Pos) |
(threshold << UART_RTSCR_THR_Pos);
UARTx->RTSCR &= ~(UART_RTSCR_EN_Msk | UART_RTSCR_POL_Msk | UART_RTSCR_THR_Msk);
UARTx->RTSCR |= (enable << UART_RTSCR_EN_Pos) |
(polarity << UART_RTSCR_POL_Pos) |
(threshold << UART_RTSCR_THR_Pos);
}
/******************************************************************************************************************************************
@ -303,9 +311,9 @@ void UART_RTSConfig(UART_TypeDef * UARTx, uint32_t enable, uint32_t polarity, ui
* : uint32_t 0 RTS线当前为低电平 1 RTS线当前为高电平
* :
******************************************************************************************************************************************/
uint32_t UART_RTSLineState(UART_TypeDef * UARTx)
uint32_t UART_RTSLineState(UART_TypeDef *UARTx)
{
return (UARTx->RTSCR & UART_RTSCR_STAT_Msk) ? 1 : 0;
return (UARTx->RTSCR & UART_RTSCR_STAT_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -317,11 +325,11 @@ uint32_t UART_RTSLineState(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_LINConfig(UART_TypeDef * UARTx, uint32_t detectedIEn, uint32_t generatedIEn)
void UART_LINConfig(UART_TypeDef *UARTx, uint32_t detectedIEn, uint32_t generatedIEn)
{
UARTx->LINCR &= ~(UART_LINCR_BRKDETIE_Msk | UART_LINCR_GENBRKIE_Msk);
UARTx->LINCR |= (detectedIEn << UART_LINCR_BRKDETIE_Pos) |
(generatedIEn << UART_LINCR_GENBRKIE_Pos);
UARTx->LINCR &= ~(UART_LINCR_BRKDETIE_Msk | UART_LINCR_GENBRKIE_Msk);
UARTx->LINCR |= (detectedIEn << UART_LINCR_BRKDETIE_Pos) |
(generatedIEn << UART_LINCR_GENBRKIE_Pos);
}
/******************************************************************************************************************************************
@ -331,9 +339,9 @@ void UART_LINConfig(UART_TypeDef * UARTx, uint32_t detectedIEn, uint32_t generat
* :
* :
******************************************************************************************************************************************/
void UART_LINGenerate(UART_TypeDef * UARTx)
void UART_LINGenerate(UART_TypeDef *UARTx)
{
UARTx->LINCR |= (1 << UART_LINCR_GENBRK_Pos);
UARTx->LINCR |= (1 << UART_LINCR_GENBRK_Pos);
}
/******************************************************************************************************************************************
@ -343,9 +351,9 @@ void UART_LINGenerate(UART_TypeDef * UARTx)
* : uint32_t 1 LIN Break 0 LIN Break
* :
******************************************************************************************************************************************/
uint32_t UART_LINIsDetected(UART_TypeDef * UARTx)
uint32_t UART_LINIsDetected(UART_TypeDef *UARTx)
{
return (UARTx->LINCR & UART_LINCR_BRKDETIF_Msk) ? 1 : 0;
return (UARTx->LINCR & UART_LINCR_BRKDETIF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -355,9 +363,9 @@ uint32_t UART_LINIsDetected(UART_TypeDef * UARTx)
* : uint32_t 1 LIN Break 0 LIN Break发送未完成
* :
******************************************************************************************************************************************/
uint32_t UART_LINIsGenerated(UART_TypeDef * UARTx)
uint32_t UART_LINIsGenerated(UART_TypeDef *UARTx)
{
return (UARTx->LINCR & UART_LINCR_GENBRKIF_Msk) ? 1 : 0;
return (UARTx->LINCR & UART_LINCR_GENBRKIF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -370,19 +378,25 @@ uint32_t UART_LINIsGenerated(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_ABRStart(UART_TypeDef * UARTx, uint32_t detectChar)
void UART_ABRStart(UART_TypeDef *UARTx, uint32_t detectChar)
{
uint32_t bits;
if((detectChar == 0xFF) || (detectChar == 0x1FF)) bits = 0;
else if((detectChar == 0xFE) || (detectChar == 0x1FE)) bits = 1;
else if((detectChar == 0xF8) || (detectChar == 0x1F8)) bits = 2;
else if((detectChar == 0x80) || (detectChar == 0x180)) bits = 3;
else while(1);
UARTx->BAUD &= ~(UART_BAUD_ABREN_Msk | UART_BAUD_ABRBIT_Msk);
UARTx->BAUD |= (1 << UART_BAUD_ABREN_Pos) |
(bits << UART_BAUD_ABRBIT_Pos);
uint32_t bits;
if ((detectChar == 0xFF) || (detectChar == 0x1FF))
bits = 0;
else if ((detectChar == 0xFE) || (detectChar == 0x1FE))
bits = 1;
else if ((detectChar == 0xF8) || (detectChar == 0x1F8))
bits = 2;
else if ((detectChar == 0x80) || (detectChar == 0x180))
bits = 3;
else
while (1)
;
UARTx->BAUD &= ~(UART_BAUD_ABREN_Msk | UART_BAUD_ABRBIT_Msk);
UARTx->BAUD |= (1 << UART_BAUD_ABREN_Pos) |
(bits << UART_BAUD_ABRBIT_Pos);
}
/******************************************************************************************************************************************
@ -392,20 +406,20 @@ void UART_ABRStart(UART_TypeDef * UARTx, uint32_t detectChar)
* : uint32_t 0 UART_ABR_RES_OK UART_ABR_RES_ERR
* :
******************************************************************************************************************************************/
uint32_t UART_ABRIsDone(UART_TypeDef * UARTx)
uint32_t UART_ABRIsDone(UART_TypeDef *UARTx)
{
if(UARTx->BAUD & UART_BAUD_ABREN_Msk)
{
return 0;
}
else if(UARTx->BAUD & UART_BAUD_ABRERR_Msk)
{
return UART_ABR_RES_ERR;
}
else
{
return UART_ABR_RES_OK;
}
if (UARTx->BAUD & UART_BAUD_ABREN_Msk)
{
return 0;
}
else if (UARTx->BAUD & UART_BAUD_ABRERR_Msk)
{
return UART_ABR_RES_ERR;
}
else
{
return UART_ABR_RES_OK;
}
}
/******************************************************************************************************************************************
@ -415,9 +429,9 @@ uint32_t UART_ABRIsDone(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTRXThresholdEn(UART_TypeDef * UARTx)
void UART_INTRXThresholdEn(UART_TypeDef *UARTx)
{
UARTx->CTRL |= (0x01 << UART_CTRL_RXIE_Pos);
UARTx->CTRL |= (0x01 << UART_CTRL_RXIE_Pos);
}
/******************************************************************************************************************************************
@ -427,9 +441,9 @@ void UART_INTRXThresholdEn(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTRXThresholdDis(UART_TypeDef * UARTx)
void UART_INTRXThresholdDis(UART_TypeDef *UARTx)
{
UARTx->CTRL &= ~(0x01 << UART_CTRL_RXIE_Pos);
UARTx->CTRL &= ~(0x01 << UART_CTRL_RXIE_Pos);
}
/******************************************************************************************************************************************
@ -439,9 +453,9 @@ void UART_INTRXThresholdDis(UART_TypeDef * UARTx)
* : uint32_t 1 RX FIFO中数据个数 >= RXThreshold 0 RX FIFO中数据个数 < RXThreshold
* : RXIF = RXTHRF & RXIE
******************************************************************************************************************************************/
uint32_t UART_INTRXThresholdStat(UART_TypeDef * UARTx)
uint32_t UART_INTRXThresholdStat(UART_TypeDef *UARTx)
{
return (UARTx->BAUD & UART_BAUD_RXIF_Msk) ? 1 : 0;
return (UARTx->BAUD & UART_BAUD_RXIF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -451,9 +465,9 @@ uint32_t UART_INTRXThresholdStat(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTTXThresholdEn(UART_TypeDef * UARTx)
void UART_INTTXThresholdEn(UART_TypeDef *UARTx)
{
UARTx->CTRL |= (0x01 << UART_CTRL_TXIE_Pos);
UARTx->CTRL |= (0x01 << UART_CTRL_TXIE_Pos);
}
/******************************************************************************************************************************************
@ -463,9 +477,9 @@ void UART_INTTXThresholdEn(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTTXThresholdDis(UART_TypeDef * UARTx)
void UART_INTTXThresholdDis(UART_TypeDef *UARTx)
{
UARTx->CTRL &= ~(0x01 << UART_CTRL_TXIE_Pos);
UARTx->CTRL &= ~(0x01 << UART_CTRL_TXIE_Pos);
}
/******************************************************************************************************************************************
@ -475,9 +489,9 @@ void UART_INTTXThresholdDis(UART_TypeDef * UARTx)
* : uint32_t 1 TX FIFO中数据个数 <= TXThreshold 0 TX FIFO中数据个数 > TXThreshold
* : TXIF = TXTHRF & TXIE
******************************************************************************************************************************************/
uint32_t UART_INTTXThresholdStat(UART_TypeDef * UARTx)
uint32_t UART_INTTXThresholdStat(UART_TypeDef *UARTx)
{
return (UARTx->BAUD & UART_BAUD_TXIF_Msk) ? 1 : 0;
return (UARTx->BAUD & UART_BAUD_TXIF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -487,9 +501,9 @@ uint32_t UART_INTTXThresholdStat(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTTimeoutEn(UART_TypeDef * UARTx)
void UART_INTTimeoutEn(UART_TypeDef *UARTx)
{
UARTx->CTRL |= (0x01 << UART_CTRL_TOIE_Pos);
UARTx->CTRL |= (0x01 << UART_CTRL_TOIE_Pos);
}
/******************************************************************************************************************************************
@ -499,9 +513,9 @@ void UART_INTTimeoutEn(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTTimeoutDis(UART_TypeDef * UARTx)
void UART_INTTimeoutDis(UART_TypeDef *UARTx)
{
UARTx->CTRL &= ~(0x01 << UART_CTRL_TOIE_Pos);
UARTx->CTRL &= ~(0x01 << UART_CTRL_TOIE_Pos);
}
/******************************************************************************************************************************************
@ -511,9 +525,9 @@ void UART_INTTimeoutDis(UART_TypeDef * UARTx)
* : uint32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t UART_INTTimeoutStat(UART_TypeDef * UARTx)
uint32_t UART_INTTimeoutStat(UART_TypeDef *UARTx)
{
return (UARTx->BAUD & UART_BAUD_TOIF_Msk) ? 1 : 0;
return (UARTx->BAUD & UART_BAUD_TOIF_Msk) ? 1 : 0;
}
/******************************************************************************************************************************************
@ -523,9 +537,9 @@ uint32_t UART_INTTimeoutStat(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTTXDoneEn(UART_TypeDef * UARTx)
void UART_INTTXDoneEn(UART_TypeDef *UARTx)
{
UARTx->CTRL |= (0x01 << UART_CTRL_TXDOIE_Pos);
UARTx->CTRL |= (0x01 << UART_CTRL_TXDOIE_Pos);
}
/******************************************************************************************************************************************
@ -535,9 +549,9 @@ void UART_INTTXDoneEn(UART_TypeDef * UARTx)
* :
* :
******************************************************************************************************************************************/
void UART_INTTXDoneDis(UART_TypeDef * UARTx)
void UART_INTTXDoneDis(UART_TypeDef *UARTx)
{
UARTx->CTRL &= ~(0x01 << UART_CTRL_TXDOIE_Pos);
UARTx->CTRL &= ~(0x01 << UART_CTRL_TXDOIE_Pos);
}
/******************************************************************************************************************************************
@ -547,7 +561,7 @@ void UART_INTTXDoneDis(UART_TypeDef * UARTx)
* : uint32_t 1 FIFO空且发送移位寄存器空 0 FIFO或发送移位寄存器未空
* :
******************************************************************************************************************************************/
uint32_t UART_INTTXDoneStat(UART_TypeDef * UARTx)
uint32_t UART_INTTXDoneStat(UART_TypeDef *UARTx)
{
return (UARTx->BAUD & UART_BAUD_TXDOIF_Msk) ? 1 : 0;
return (UARTx->BAUD & UART_BAUD_TXDOIF_Msk) ? 1 : 0;
}

View File

@ -1,94 +1,90 @@
#ifndef __SWM320_UART_H__
#define __SWM320_UART_H__
typedef struct {
uint32_t Baudrate;
uint8_t DataBits; //数据位位数可取值UART_DATA_8BIT、UART_DATA_9BIT
uint8_t Parity; //奇偶校验位可取值UART_PARITY_NONE、UART_PARITY_ODD、UART_PARITY_EVEN、UART_PARITY_ONE、UART_PARITY_ZERO
uint8_t StopBits; //停止位位数可取值UART_STOP_1BIT、UART_STOP_2BIT
uint8_t RXThreshold; //取值0--7
uint8_t RXThresholdIEn; //当RX FIFO中数据个数 > RXThreshold时触发中断
uint8_t TXThreshold; //取值0--7
uint8_t TXThresholdIEn; //当TX FIFO中数据个数 <= TXThreshold时触发中断
uint8_t TimeoutTime; //超时时长 = TimeoutTime/(Baudrate/10) 秒
uint8_t TimeoutIEn; //超时中断RX FIFO非空且超过 TimeoutTime/(Baudrate/10) 秒没有在RX线上接收到数据时触发中断
typedef struct
{
uint32_t Baudrate;
uint8_t DataBits; //数据位位数可取值UART_DATA_8BIT、UART_DATA_9BIT
uint8_t Parity; //奇偶校验位可取值UART_PARITY_NONE、UART_PARITY_ODD、UART_PARITY_EVEN、UART_PARITY_ONE、UART_PARITY_ZERO
uint8_t StopBits; //停止位位数可取值UART_STOP_1BIT、UART_STOP_2BIT
uint8_t RXThreshold; //取值0--7
uint8_t RXThresholdIEn; //当RX FIFO中数据个数 > RXThreshold时触发中断
uint8_t TXThreshold; //取值0--7
uint8_t TXThresholdIEn; //当TX FIFO中数据个数 <= TXThreshold时触发中断
uint8_t TimeoutTime; //超时时长 = TimeoutTime/(Baudrate/10) 秒
uint8_t TimeoutIEn; //超时中断RX FIFO非空且超过 TimeoutTime/(Baudrate/10) 秒没有在RX线上接收到数据时触发中断
} UART_InitStructure;
#define UART_DATA_8BIT 0
#define UART_DATA_9BIT 1
#define UART_DATA_8BIT 0
#define UART_DATA_9BIT 1
#define UART_PARITY_NONE 0
#define UART_PARITY_ODD 1
#define UART_PARITY_EVEN 3
#define UART_PARITY_ONE 5
#define UART_PARITY_ZERO 7
#define UART_PARITY_NONE 0
#define UART_PARITY_ODD 1
#define UART_PARITY_EVEN 3
#define UART_PARITY_ONE 5
#define UART_PARITY_ZERO 7
#define UART_STOP_1BIT 0
#define UART_STOP_2BIT 1
#define UART_STOP_1BIT 0
#define UART_STOP_2BIT 1
#define UART_RTS_1BYTE 0
#define UART_RTS_2BYTE 1
#define UART_RTS_4BYTE 2
#define UART_RTS_6BYTE 3
#define UART_RTS_1BYTE 0
#define UART_RTS_2BYTE 1
#define UART_RTS_4BYTE 2
#define UART_RTS_6BYTE 3
#define UART_ABR_RES_OK 1
#define UART_ABR_RES_ERR 2
#define UART_ABR_RES_OK 1
#define UART_ABR_RES_ERR 2
#define UART_ERR_FRAME 1
#define UART_ERR_PARITY 2
#define UART_ERR_NOISE 3
#define UART_ERR_FRAME 1
#define UART_ERR_PARITY 2
#define UART_ERR_NOISE 3
void UART_Init(UART_TypeDef *UARTx, UART_InitStructure *initStruct); //UART串口初始化
void UART_Open(UART_TypeDef *UARTx);
void UART_Close(UART_TypeDef *UARTx);
void UART_WriteByte(UART_TypeDef *UARTx, uint32_t data); //发送一个字节数据
uint32_t UART_ReadByte(UART_TypeDef *UARTx, uint32_t *data); //读取一个字节数据并指出数据是否Valid
void UART_Init(UART_TypeDef * UARTx, UART_InitStructure * initStruct); //UART串口初始化
void UART_Open(UART_TypeDef * UARTx);
void UART_Close(UART_TypeDef * UARTx);
uint32_t UART_IsTXBusy(UART_TypeDef *UARTx);
uint32_t UART_IsRXFIFOEmpty(UART_TypeDef *UARTx); //接收FIFO是否空如果不空则可以继续UART_ReadByte()
uint32_t UART_IsTXFIFOFull(UART_TypeDef *UARTx); //发送FIFO是否满如果不满则可以继续UART_WriteByte()
void UART_WriteByte(UART_TypeDef * UARTx, uint32_t data); //发送一个字节数据
uint32_t UART_ReadByte(UART_TypeDef * UARTx, uint32_t * data); //读取一个字节数据并指出数据是否Valid
void UART_SetBaudrate(UART_TypeDef *UARTx, uint32_t baudrate); //设置波特率
uint32_t UART_GetBaudrate(UART_TypeDef *UARTx); //获取当前使用的波特率
uint32_t UART_IsTXBusy(UART_TypeDef * UARTx);
uint32_t UART_IsRXFIFOEmpty(UART_TypeDef * UARTx); //接收FIFO是否空如果不空则可以继续UART_ReadByte()
uint32_t UART_IsTXFIFOFull(UART_TypeDef * UARTx); //发送FIFO是否满如果不满则可以继续UART_WriteByte()
void UART_CTSConfig(UART_TypeDef *UARTx, uint32_t enable, uint32_t polarity);
uint32_t UART_CTSLineState(UART_TypeDef *UARTx);
void UART_RTSConfig(UART_TypeDef *UARTx, uint32_t enable, uint32_t polarity, uint32_t threshold);
uint32_t UART_RTSLineState(UART_TypeDef *UARTx);
void UART_SetBaudrate(UART_TypeDef * UARTx, uint32_t baudrate); //设置波特率
uint32_t UART_GetBaudrate(UART_TypeDef * UARTx); //获取当前使用的波特率
void UART_LINConfig(UART_TypeDef *UARTx, uint32_t detectedIEn, uint32_t generatedIEn);
void UART_LINGenerate(UART_TypeDef *UARTx);
uint32_t UART_LINIsDetected(UART_TypeDef *UARTx);
uint32_t UART_LINIsGenerated(UART_TypeDef *UARTx);
void UART_CTSConfig(UART_TypeDef * UARTx, uint32_t enable, uint32_t polarity);
uint32_t UART_CTSLineState(UART_TypeDef * UARTx);
void UART_ABRStart(UART_TypeDef *UARTx, uint32_t detectChar);
uint32_t UART_ABRIsDone(UART_TypeDef *UARTx);
void UART_RTSConfig(UART_TypeDef * UARTx, uint32_t enable, uint32_t polarity, uint32_t threshold);
uint32_t UART_RTSLineState(UART_TypeDef * UARTx);
void UART_LINConfig(UART_TypeDef * UARTx, uint32_t detectedIEn, uint32_t generatedIEn);
void UART_LINGenerate(UART_TypeDef * UARTx);
uint32_t UART_LINIsDetected(UART_TypeDef * UARTx);
uint32_t UART_LINIsGenerated(UART_TypeDef * UARTx);
void UART_ABRStart(UART_TypeDef * UARTx, uint32_t detectChar);
uint32_t UART_ABRIsDone(UART_TypeDef * UARTx);
void UART_INTRXThresholdEn(UART_TypeDef * UARTx);
void UART_INTRXThresholdDis(UART_TypeDef * UARTx);
uint32_t UART_INTRXThresholdStat(UART_TypeDef * UARTx);
void UART_INTTXThresholdEn(UART_TypeDef * UARTx);
void UART_INTTXThresholdDis(UART_TypeDef * UARTx);
uint32_t UART_INTTXThresholdStat(UART_TypeDef * UARTx);
void UART_INTTimeoutEn(UART_TypeDef * UARTx);
void UART_INTTimeoutDis(UART_TypeDef * UARTx);
uint32_t UART_INTTimeoutStat(UART_TypeDef * UARTx);
void UART_INTTXDoneEn(UART_TypeDef * UARTx);
void UART_INTTXDoneDis(UART_TypeDef * UARTx);
uint32_t UART_INTTXDoneStat(UART_TypeDef * UARTx);
void UART_INTRXThresholdEn(UART_TypeDef *UARTx);
void UART_INTRXThresholdDis(UART_TypeDef *UARTx);
uint32_t UART_INTRXThresholdStat(UART_TypeDef *UARTx);
void UART_INTTXThresholdEn(UART_TypeDef *UARTx);
void UART_INTTXThresholdDis(UART_TypeDef *UARTx);
uint32_t UART_INTTXThresholdStat(UART_TypeDef *UARTx);
void UART_INTTimeoutEn(UART_TypeDef *UARTx);
void UART_INTTimeoutDis(UART_TypeDef *UARTx);
uint32_t UART_INTTimeoutStat(UART_TypeDef *UARTx);
void UART_INTTXDoneEn(UART_TypeDef *UARTx);
void UART_INTTXDoneDis(UART_TypeDef *UARTx);
uint32_t UART_INTTXDoneStat(UART_TypeDef *UARTx);
#endif //__SWM320_UART_H__

View File

@ -21,7 +21,6 @@
#include "SWM320.h"
#include "SWM320_wdt.h"
/******************************************************************************************************************************************
* : WDT_Init()
* : WDT看门狗初始化
@ -31,26 +30,26 @@
* :
* : 使
******************************************************************************************************************************************/
void WDT_Init(WDT_TypeDef * WDTx, uint32_t peroid, uint32_t mode)
void WDT_Init(WDT_TypeDef *WDTx, uint32_t peroid, uint32_t mode)
{
SYS->CLKEN |= (0x01 << SYS_CLKEN_WDT_Pos);
WDT_Stop(WDTx); //设置前先关闭
WDTx->LOAD = peroid;
if(mode == WDT_MODE_RESET)
{
NVIC_DisableIRQ(WDT_IRQn);
WDTx->CR |= (1 << WDT_CR_RSTEN_Pos);
}
else //mode == WDT_MODE_INTERRUPT
{
NVIC_EnableIRQ(WDT_IRQn);
WDTx->CR &= ~(1 << WDT_CR_RSTEN_Pos);
}
SYS->CLKEN |= (0x01 << SYS_CLKEN_WDT_Pos);
WDT_Stop(WDTx); //设置前先关闭
WDTx->LOAD = peroid;
if (mode == WDT_MODE_RESET)
{
NVIC_DisableIRQ(WDT_IRQn);
WDTx->CR |= (1 << WDT_CR_RSTEN_Pos);
}
else //mode == WDT_MODE_INTERRUPT
{
NVIC_EnableIRQ(WDT_IRQn);
WDTx->CR &= ~(1 << WDT_CR_RSTEN_Pos);
}
}
/******************************************************************************************************************************************
@ -60,9 +59,9 @@ void WDT_Init(WDT_TypeDef * WDTx, uint32_t peroid, uint32_t mode)
* :
* :
******************************************************************************************************************************************/
void WDT_Start(WDT_TypeDef * WDTx)
void WDT_Start(WDT_TypeDef *WDTx)
{
WDTx->CR |= (0x01 << WDT_CR_EN_Pos);
WDTx->CR |= (0x01 << WDT_CR_EN_Pos);
}
/******************************************************************************************************************************************
@ -72,9 +71,9 @@ void WDT_Start(WDT_TypeDef * WDTx)
* :
* :
******************************************************************************************************************************************/
void WDT_Stop(WDT_TypeDef * WDTx)
void WDT_Stop(WDT_TypeDef *WDTx)
{
WDTx->CR &= ~(0x01 << WDT_CR_EN_Pos);
WDTx->CR &= ~(0x01 << WDT_CR_EN_Pos);
}
/******************************************************************************************************************************************
@ -84,9 +83,9 @@ void WDT_Stop(WDT_TypeDef * WDTx)
* :
* :
******************************************************************************************************************************************/
void WDT_Feed(WDT_TypeDef * WDTx)
void WDT_Feed(WDT_TypeDef *WDTx)
{
WDTx->FEED = 0x55;
WDTx->FEED = 0x55;
}
/******************************************************************************************************************************************
@ -96,9 +95,9 @@ void WDT_Feed(WDT_TypeDef * WDTx)
* : int32_t
* :
******************************************************************************************************************************************/
int32_t WDT_GetValue(WDT_TypeDef * WDTx)
int32_t WDT_GetValue(WDT_TypeDef *WDTx)
{
return WDTx->VALUE;
return WDTx->VALUE;
}
/******************************************************************************************************************************************
@ -108,9 +107,9 @@ int32_t WDT_GetValue(WDT_TypeDef * WDTx)
* :
* :
******************************************************************************************************************************************/
void WDT_INTClr(WDT_TypeDef * WDTx)
void WDT_INTClr(WDT_TypeDef *WDTx)
{
WDTx->IF = 1;
WDTx->IF = 1;
}
/******************************************************************************************************************************************
@ -120,7 +119,7 @@ void WDT_INTClr(WDT_TypeDef * WDTx)
* : int32_t 1 0
* :
******************************************************************************************************************************************/
uint32_t WDT_INTStat(WDT_TypeDef * WDTx)
uint32_t WDT_INTStat(WDT_TypeDef *WDTx)
{
return WDTx->IF;
return WDTx->IF;
}

View File

@ -1,19 +1,18 @@
#ifndef __SWM320_WDT_H__
#define __SWM320_WDT_H__
#define __SWM320_WDT_H__
#define WDT_MODE_RESET 0
#define WDT_MODE_INTERRUPT 1
#define WDT_MODE_RESET 0
#define WDT_MODE_INTERRUPT 1
void WDT_Init(WDT_TypeDef * WDTx, uint32_t peroid, uint32_t mode); //WDT看门狗初始化
void WDT_Start(WDT_TypeDef * WDTx); //启动指定WDT开始倒计时
void WDT_Stop(WDT_TypeDef * WDTx); //关闭指定WDT停止倒计时
void WDT_Init(WDT_TypeDef *WDTx, uint32_t peroid, uint32_t mode); //WDT看门狗初始化
void WDT_Start(WDT_TypeDef *WDTx); //启动指定WDT开始倒计时
void WDT_Stop(WDT_TypeDef *WDTx); //关闭指定WDT停止倒计时
void WDT_Feed(WDT_TypeDef * WDTx); //喂狗,重新从装载值开始倒计时
void WDT_Feed(WDT_TypeDef *WDTx); //喂狗,重新从装载值开始倒计时
int32_t WDT_GetValue(WDT_TypeDef * WDTx); //获取指定看门狗定时器的当前倒计时值
int32_t WDT_GetValue(WDT_TypeDef *WDTx); //获取指定看门狗定时器的当前倒计时值
void WDT_INTClr(WDT_TypeDef *WDTx); //中断标志清除
uint32_t WDT_INTStat(WDT_TypeDef *WDTx); //中断状态查询
void WDT_INTClr(WDT_TypeDef * WDTx); //中断标志清除
uint32_t WDT_INTStat(WDT_TypeDef * WDTx); //中断状态查询
#endif //__SWM320_WDT_H__