谁能 帮忙 看下 SPI DMA发送 我勒个去,是设备时序操作错误

[复制链接]
949|6
手机看帖
扫描二维码
随时随地手机跟帖
shen520|  楼主 | 2015-9-27 13:10 | 显示全部楼层 |阅读模式
本帖最后由 shen520 于 2015-9-27 14:28 编辑


void SPI_1_DMATx(u8 *Buf , u16 length)
{
         DMA_InitTypeDef DMA_InitStructure;
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);        //使能DMA传输
         
         DMA_DeInit(DMA1_Channel3);  
   DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI1->DR;              //设置  接收外设(0x4001300C) 地址(源地址)
   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Buf;                   //设置 SRAM 存储地址(源地址)
   DMA_InitStructure.DMA_DIR            = DMA_DIR_PeripheralDST;           //传输方向 内存-外设
   DMA_InitStructure.DMA_BufferSize     = length;                          //设置 SPI1 接收长度
   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_VeryHigh;       //中断方式-高(三级)
   DMA_InitStructure.DMA_M2M                = DMA_M2M_Disable;             //内存到内存方式禁止
   DMA_Init(DMA1_Channel3, &DMA_InitStructure);
    DMA_ClearFlag(DMA1_FLAG_TC3);                  //清除通道4传输完成标志

         SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Tx , ENABLE);
   DMA_Cmd(DMA1_Channel3, ENABLE);                   //开启 DMA 通道 DMA1_Channel3

         while(DMA_GetFlagStatus(DMA1_FLAG_TC3)==RESET);//判断通道4传输完成        

   DMA_ClearFlag(DMA1_FLAG_TC3);                  //清除通道4传输完成标志  
         
         SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Tx , DISABLE);
   DMA_Cmd(DMA1_Channel3, DISABLE);                   //开启 DMA 通道 DMA1_Channel3
         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, DISABLE);        //使能DMA传输
}
风吹小浪| | 2015-9-28 08:00 | 显示全部楼层
看了看,楼主好像用的是ST的库吧,出现问题很正常,建议用GD的库。

使用特权

评论回复
风吹小浪| | 2015-9-28 08:00 | 显示全部楼层
给你个AD DMA的例程看看
/**
  ******************************************************************************
  * [url=home.php?mod=space&uid=288409]@file[/url]    DMA/ADC1_DMA/main.c
  * [url=home.php?mod=space&uid=187600]@author[/url]  MCU SD
  * [url=home.php?mod=space&uid=895143]@version[/url] V1.0
  * [url=home.php?mod=space&uid=212281]@date[/url]    26-Dec-2014
  * [url=home.php?mod=space&uid=247401]@brief[/url]   Main program body.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "gd32f10x.h"
#include "main.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define RCC_GPIO_LED1               RCC_APB2PERIPH_GPIOC
#define GPIO_LED1_PORT              GPIOC
#define RCC_GPIO_LED2               RCC_APB2PERIPH_GPIOC
#define GPIO_LED2_PORT              GPIOC   
#define GPIO_LED1                   GPIO_PIN_0   
#define GPIO_LED2                   GPIO_PIN_2

#define RCC_GPIO_LED3               RCC_APB2PERIPH_GPIOE
#define GPIO_LED3_PORT              GPIOE
#define RCC_GPIO_LED4               RCC_APB2PERIPH_GPIOE
#define GPIO_LED4_PORT              GPIOE
#define GPIO_LED3                   GPIO_PIN_0   
#define GPIO_LED4                   GPIO_PIN_1

/* Private variables ---------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define ADC_DR_Address    ((u32)0x4001244C)

float AD_value;
__IO uint16_t ADC_ConvertedValue;

DMA_InitPara  DMA_InitStructure;

__IO uint32_t TimingDelay = 0;
void RCC_Configuration(void);
static void SysTickConfig(void);
void USART_Configuration(void);
void ADC_Configuration(void);

/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
      
    /* System Clocks Configuration */
    RCC_Configuration();
                           
    /* SysTick configuration */
    SysTickConfig();
                        
    LED_config() ;
    GPIO_SetBits(GPIO_LED1_PORT, GPIO_LED1);
    GPIO_ResetBits(GPIO_LED3_PORT, GPIO_LED3);
    GPIO_ResetBits(GPIO_LED2_PORT, GPIO_LED2);
    GPIO_ResetBits(GPIO_LED4_PORT, GPIO_LED4);
   
    USART_Configuration();

    /* DMA1 channel1 configuration */
    DMA_DeInit(DMA1_CHANNEL1);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_DR_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValue;
    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_DISABLE;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD;
    DMA_InitStructure.DMA_Mode = DMA_MODE_CIRCULAR;
    DMA_InitStructure.DMA_Priority = DMA_PRIORITY_VERYHIGH;
    DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
    DMA_Init(DMA1_CHANNEL1, &DMA_InitStructure);
            
    /* Enable DMA transfer */
    DMA_Enable(DMA1_CHANNEL1, ENABLE);
    ADC_Configuration();
    Delay(300);     
    while (1)
    {
        /***********************************************ADC采集转换********************************************************/
         
        /* Printf message with AD value to serial port every 1 second */
        AD_value = ADC_ConvertedValue;
        AD_value = (AD_value/4096)*3.3;
        printf("The current AD value = %4.2fV \r\n", AD_value);
        Delay(1000);   /* delay 1000ms */      
    }
}

/**
  * @brief  Led configure
  * @param  None
  * @retval None
  */
void LED_config(void)
{
    GPIO_InitPara GPIO_InitStructure;
   
    /* Enable the GPIO_LED Clock */
    RCC_APB2PeriphClock_Enable(RCC_GPIO_LED1 | RCC_GPIO_LED2 | RCC_GPIO_LED3 | RCC_GPIO_LED4, ENABLE);
   
    /* Configure the GPIO_LED pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_LED1 | GPIO_LED2;   
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_Init(GPIO_LED1_PORT, &GPIO_InitStructure);
    GPIO_Init(GPIO_LED2_PORT, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_LED3 | GPIO_LED4;   
    GPIO_Init(GPIO_LED3_PORT, &GPIO_InitStructure);
    GPIO_Init(GPIO_LED4_PORT, &GPIO_InitStructure);

}
/**
  * @brief  Configure the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
    /* Enable peripheral clocks */
    /* Enable DMA1 clock */
    RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_DMA1, ENABLE);

}

void USART_Configuration(void)
{
    GPIO_InitPara     GPIO_InitStructure;
    USART_InitPara    USART_InitStructure;

    /* Enable GPIO clock */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_AF, ENABLE);

    USART_InitStructure.USART_BRR = 115200;
    USART_InitStructure.USART_WL = USART_WL_8B;
    USART_InitStructure.USART_STBits = USART_STBITS_1;
    USART_InitStructure.USART_Parity = USART_PARITY_RESET;
    USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
    USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;

    /* Enable USART clock */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE);
   
  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
  GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART Rx as input floating */
  GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
  GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
     /* USART configuration */
    USART_Init(USART1, &USART_InitStructure);
   
    /* Enable USART */
    USART_Enable(USART1, ENABLE);
    USART_ClearBitState(USART1,USART_FLAG_TC);
}



/*******************************************************************************
* Function Name  : ADC_Configuration
* Description    : Configure the ADC.
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void ADC_Configuration(void)
{
    ADC_InitPara ADC_InitStructure;
    GPIO_InitPara GPIO_InitStructure;
   
    RCC_ADCCLKConfig(RCC_ADCCLK_APB2_DIV6);
   
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_ADC1 | RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_AF, ENABLE);
   
    /* Configure PC.03 (ADC12 Channel3)as analog input -------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_3;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AIN;
    GPIO_Init(GPIOC, &GPIO_InitStructure);   
   
    /* ADC1 configuration ------------------------------------------------------*/
    ADC_InitStructure.ADC_Mode = ADC_MODE_INDEPENDENT;
    ADC_InitStructure.ADC_Mode_Scan = ENABLE;
    ADC_InitStructure.ADC_Mode_Continuous = ENABLE;
    ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE;
    ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT;
    ADC_InitStructure.ADC_Channel_Number = 1;
    ADC_Init(ADC1, &ADC_InitStructure);
   
    /* ADC1 regular channel 0 configuration */
    ADC_RegularChannel_Config(ADC1, ADC_CHANNEL_13, 1, ADC_SAMPLETIME_239POINT5);
   
    /* Enable ADC1 DMA */
    ADC_DMA_Enable(ADC1, ENABLE);
   
    /* Enable ADC1 */
    ADC_Enable(ADC1, ENABLE);
   
    /* Enable ADC1 calibaration register */   
    ADC_Calibration(ADC1);
     
    /* Start ADC1 Software Conversion */
    ADC_SoftwareStartConv_Enable(ADC1, ENABLE);
}

/**
* @brief  Configure a SysTick Base time to 1 ms.
* @param  None
* @retval None
*/
static void SysTickConfig(void)
{
    /* Setup SysTick Timer for 1ms interrupts */
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        /* Capture error */
        while (1);
    }
   
    /* Configure the SysTick handler priority */
    NVIC_SetPriority(SysTick_IRQn, 0x0);
}

/**
* @brief  Insert a delay time.
* @param  nTime: specify the delay time length.
* @retval None
*/
void Delay(__IO uint32_t nTime)
{
    TimingDelay = nTime;

    while(TimingDelay != 0);
}

/**
* @brief  Decrement the TimingDelay variable.
* @param  None
* @retval None
*/
void TimingDelay_Decrement(void)
{
    if(TimingDelay != 0x00)
    {
        TimingDelay --;
    }
}

/**
  * @brief  Retarget the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART */
    USART_DataSend(USART1, (uint8_t) ch);
   
    /* Loop until the end of transmission */
    while (USART_GetBitState(USART1, USART_FLAG_TC) == RESET)
    {}
   
    return ch;
}

/******************* (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/

使用特权

评论回复
我的期待| | 2015-9-28 16:56 | 显示全部楼层
用ST库的话,要注意ST和GD SPI的异同

使用特权

评论回复
塞北旅途| | 2015-9-28 23:12 | 显示全部楼层
标志是不是清除的有点早了

使用特权

评论回复
elecintop| | 2015-9-29 20:50 | 显示全部楼层
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, DISABLE);        //使能DMA传输
这句是不是有问题了?DISABLE不是禁止的意思么

使用特权

评论回复
稳稳の幸福| | 2015-9-30 19:49 | 显示全部楼层
用GD的芯片最好用GD自己的头文件和库函数。

使用特权

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

本版积分规则

14

主题

24

帖子

1

粉丝