📄 bsp.c
字号:
#include "..\App\includes.h"
#include "demo.h"
/*
*********************************************************************************************************
*/
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
#define TP_CS() GPIO_ResetBits(GPIOB,GPIO_Pin_7)
#define TP_DCS() GPIO_SetBits(GPIOB,GPIO_Pin_7)
u16 CCR1_Val=0xffff;
void tp_Config(void) ;
void SysTick_Config1(void);
void Light_PWM(void);
void RCC_Configuration(void)
{ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIO_LED clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
void RCC1_Configuration(void)
{ ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* TIM1, GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOD, ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
}
//关闭调试接口,作GPIO使用
void UnableJTAG(void)
{
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
AFIO->MAPR &= ~(7UL<<24); // clear used bit
AFIO->MAPR |= (4UL<<24); // set used bits
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED1
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_6|GPIO_Pin_3; //LED2, LED3
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; //LCD-RST
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
PE.14(D11), PE.15(D12) as alternate function push pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
GPIO_Pin_15;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* LCD Data(D0-D7) lines configuration */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15 |GPIO_Pin_0 | GPIO_Pin_1 ;
//GPIO_Init(GPIOD, &GPIO_InitStructure);
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ;
//GPIO_Init(GPIOE, &GPIO_InitStructure);
/* NOE and NWE configuration */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 |GPIO_Pin_4;
//GPIO_Init(GPIOD, &GPIO_InitStructure);
/* NE1 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* RS */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD, GPIO_Pin_7); //CS=1
GPIO_SetBits(GPIOD, GPIO_Pin_14| GPIO_Pin_15 |GPIO_Pin_0 | GPIO_Pin_1);
GPIO_SetBits(GPIOE, GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10);
GPIO_ResetBits(GPIOE, GPIO_Pin_0);
GPIO_ResetBits(GPIOE, GPIO_Pin_1); //RESET=0
GPIO_SetBits(GPIOD, GPIO_Pin_4); //RD=1
GPIO_SetBits(GPIOD, GPIO_Pin_5); //WR=1
//usart_init----------------------------------------------------
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOD, GPIO_Pin_13); //LIGHT
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
#if defined (VECT_TAB_RAM)
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#elif defined(VECT_TAB_FLASH_IAP)
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
///////////KEY INT|||||||||||||||||
/* Enable the EXTI2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
///////////NRF24L01 IRQ|||||||||||||||||
/* Enable the EXTI2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8); //NRF24L01 IRQ PB8
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line8; //NRF24L01 IRQ PB8
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void BSP_Init(void)
{
/* System Clocks Configuration */
RCC_Configuration();
GPIO_Configuration();
//Key_Config(); //键盘初始化
SPI2_NRF24L01_Init();
/* NVIC configuration */
NVIC_Configuration();
USART1_InitConfig(115200);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
FSMC_LCD_Init();
}
void Light_PWM(void){
TIM_TimeBaseInitTypeDef TIM4_TimeBaseStructure;
TIM_OCInitTypeDef TIM4_OCInitStructure;
TIM_BDTRInitTypeDef TIM4_BDTRInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//ErrorStatus HSEStartUpStatus;
//u16 CCR1_Val = 0xffff; //PWM 占空比调整参数, 0xffff 背光最亮 0x0 背光最暗
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_TIM4 , ENABLE);
TIM_DeInit(TIM4);
/* Time Base configuration */
TIM4_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM4_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM4_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM4_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM4_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM4,&TIM4_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM4_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM4_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//TIM4_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM4_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM4_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM4_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM4_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM4_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM4,&TIM4_OCInitStructure);
/* Automatic Output enable, Break, dead time and lock configuration*/
TIM4_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM4_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM4_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM4_BDTRInitStructure.TIM_DeadTime = 0x75;
TIM4_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM4_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM4_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM4,&TIM4_BDTRInitStructure);
/* TIM1 counter enable */
TIM_Cmd(TIM4,ENABLE);
/* Main Output Enable */
//TIM_CtrlPWMOutputs(TIM4,ENABLE);
//while(1);
}
//
void SysTick_Config1(void)
{
SysTick_Config(SystemFrequency / 1000);
}
//
CPU_INT32U BSP_CPU_ClkFreq (void)
{
RCC_ClocksTypeDef rcc_clocks;
RCC_GetClocksFreq(&rcc_clocks);
return ((CPU_INT32U)rcc_clocks.HCLK_Frequency);
}
INT32U OS_CPU_SysTickClkFreq (void)
{
INT32U freq;
freq = BSP_CPU_ClkFreq();
return (freq);
}
void OS_CPU_SysTickInit(void)
{
RCC_ClocksTypeDef rcc_clocks;
INT32U cnts;
RCC_GetClocksFreq(&rcc_clocks);
cnts = (INT32U)rcc_clocks.HCLK_Frequency/OS_TICKS_PER_SEC;
SysTick_Config(cnts);
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -