/******************************************定时器和DMA驱动的nfr**************************************************/
#include "stm32f10x_nvic.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_exti.h"
#include "DMASPI_nfr24l01.h"
#include "stm32f10x_tim.h"
#include <absacc.h>
#include "../SystemConfig/Board.h"
#include "nrf_dma_cmd.c"
#include "tim2_dma_irq.c"
/***********************************初始化nfr *************************************/
u8 DMASPI_nfr_Init(void) {
vu8 tmp=0;
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
//tim2初始化
/* Timer configuration in Encoder mode */
TIM_DeInit(TIM2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢先优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 65535; //重载定时器周期,ARPE=1时更新事件时//
TIM_TimeBaseStructure.TIM_Prescaler = 0; //定时器时钟分频数,下次更新事件时采用//
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割//
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//这个必须的,会触发更新
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update|TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC3);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
//spi DMA clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//spi gpio spi2主模式 //clk mosi out //miso in
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//CSN CE out
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = NFR_CSN_IOBIT | NFR_CE_IOBIT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(NFR_CTRL_IOPORT, &GPIO_InitStructure);
//IRQ
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = NFR_IRQ_IOBIT;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;//
GPIO_Init(NFR_CTRL_IOPORT, &GPIO_InitStructure);
/* 中断 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢先优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// EXTI_GenerateSWInterrupt(EXTI_Line1);
//TXDMA
/* SPI2 TX USED DMA */
DMA_DeInit(NFR_SPI_TX_DMACHANNEL);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(NFR_SPI->DR));
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)NFR24l01_TX_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 1; //第一次初始化发送
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(NFR_SPI_TX_DMACHANNEL, &DMA_InitStructure);
/* SPI2 RX USED DMA */
DMA_DeInit(NFR_SPI_RX_DMACHANNEL);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(NFR_SPI->DR));
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)NFR24l01_RX_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(NFR_SPI_RX_DMACHANNEL, &DMA_InitStructure);
/* dma接收中断*/
NVIC_StructInit(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢先优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//spi2:
SPI_I2S_DeInit(NFR_SPI);
SPI_Cmd(NFR_SPI, DISABLE); //必须先禁用,才能改变MODE
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式,
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位,
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //空闲时钟低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; ////第一个时钟的下降沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件nss,
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;//时钟
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; ////MSBfirst
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(NFR_SPI, &SPI_InitStructure);
NFRSPI_DATA=0xff;
/* Enable SPI_SLAVE Rx request */
SPI_I2S_DMACmd(NFR_SPI, SPI_I2S_DMAReq_Tx|SPI_I2S_DMAReq_Rx, ENABLE); //允许DMA请求
SPI_Cmd(NFR_SPI, ENABLE);
DMA_Cmd(NFR_SPI_RX_DMACHANNEL, ENABLE);
DMA_Cmd(NFR_SPI_TX_DMACHANNEL, ENABLE);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); //开启中断
NRF24L01_Status=0; //初始化全0
while ((DMA1->ISR & DMA1_FLAG_TC5)==0); //等待发送完成
Time2_Counter=0;while (Time2_Counter<4);
NFRSPI_CE=0;NFRSPI_CSN=1; //初始CNS电平
Time2_Counter=0;while (Time2_Counter<4);
tmp=NFRSPI_DATA; //清空接收
return NRF_BaseSetting();
}
|