bool I2C::Init() { I2C_InitTypeDef I2C_InitStructure; #ifdef I2C_USE_DMA DMA_InitTypeDef DMA_InitStructure; uint8_t dmaTxIrq,dmaRxIrq; #endif NVIC_InitTypeDef NVIC_InitStructure; uint8_t eventIrq,errorIrq; uint32_t i2cClk;//i2c clock uint16_t sclPin,sdaPin; if(this->GetI2CNumber()==1)//i2c1 { #ifdef USE_I2C1 i2cClk=RCC_APB1Periph_I2C1;//i2 clock setting eventIrq=I2C1_EV_IRQn; errorIrq=I2C1_ER_IRQn; #ifdef I2C_USE_DMA if(mUseDma) { dmaTxIrq=DMA1_Channel6_IRQn; dmaRxIrq=DMA1_Channel7_IRQn; } #endif if(mRemap)//remap gpio pin { sclPin=GPIO_Pin_8; sdaPin=GPIO_Pin_9; } else { sclPin=GPIO_Pin_6; sdaPin=GPIO_Pin_7; } #endif } else { #ifdef USE_I2C2 i2cClk=RCC_APB1Periph_I2C2; sclPin=GPIO_Pin_10; sdaPin=GPIO_Pin_11; eventIrq=I2C2_EV_IRQn; errorIrq=I2C2_ER_IRQn; #ifdef I2C_USE_DMA if(mUseDma) { dmaTxIrq=DMA1_Channel4_IRQn; dmaRxIrq=DMA1_Channel5_IRQn; } #endif pI2C2=this; #endif } Soft_Reset();//清空I2C相关寄存器 //重要,不能删除 ////设置成默认值 mI2C->CR1 &= ~I2C_CR1_PE;//I2C失能 I2C_Cmd(I2C,DISABLE); //失能 I2C // I2CGPIODeInit(sclPin,sdaPin);//IO设置成默认值 // // RCC_APB1PeriphResetCmd(i2cClk,ENABLE);//重置clk时钟 防止有错误标志 // I2C_DeInit(I2C); //将IIC端口初始化,否则GPIO不能被操作 // RCC_APB1PeriphResetCmd(i2cClk,DISABLE);//关闭clk重置 // RCC_APB1PeriphClockCmd(i2cClk,DISABLE);//关闭CLK时钟 #ifdef I2C_USE_DMA if(mUseDma) { //dma默认值 重要,不能去掉,不然在硬件连接有错时可能会导致DMA无法恢复,以致不能使用DMA发送数据 DMA_DeInit(mDmaTxChannel); DMA_DeInit(mDmaRxChannel); } #endif /////初始化 //I2C CLK初始化 RCC_APB1PeriphResetCmd(i2cClk,ENABLE);//重置clk时钟 防止有错误标志 RCC_APB1PeriphResetCmd(i2cClk,DISABLE);//关闭clk重置 RCC_APB1PeriphClockCmd(i2cClk,ENABLE);//开启I2C时钟 if(!I2C_CHACK_BUSY_FIX(i2cClk,sclPin,sdaPin))//检测总线是否被从机拉低(SDA为低)(一般是因为传送过程中出错导致无法一直处于busy状态) { mState=STATE_ERROR; return 0; } I2CGPIOInit(sclPin,sdaPin); //i2c使能 I2C_Cmd(I2C,ENABLE); //mI2C->CR1 |= I2C_CR1_PE;//由于在下面I2C_Init()函数中会开启,所以这里屏蔽了 #ifdef I2C_USE_DMA if(mUseDma) { //使能DMA mI2C->CR2 |= I2C_CR2_DMAEN; } #endif I2C_InitStructure.I2C_ClockSpeed = mSpeed; //Initialize the I2C_ClockSpeed member I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; // Initialize the I2C_Mode member I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; // Initialize the I2C_DutyCycle member I2C_InitStructure.I2C_OwnAddress1 = 0; // Initialize the I2C_OwnAddress1 member I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; // Initialize the I2C_Ack member I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // Initialize the I2C_AcknowledgedAddress member I2C_Init(mI2C,&I2C_InitStructure);//iic初始化 #ifdef I2C_USE_DMA if(mUseDma) { //DMA初始化 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); // I2Cx Common Channel Configuration DMA_InitStructure.DMA_BufferSize = 0; 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_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // Select I2Cx DR Address register as DMA PeripheralBaseAddress DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)&(mI2C->DR); // Select Memory to Peripheral transfer direction DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_Init(mDmaTxChannel,&DMA_InitStructure); // Select Peripheral to Memory transfer direction DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_Init(mDmaRxChannel,&DMA_InitStructure); } #endif // Enable the IRQ channel NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Configure NVIC for I2Cx EVT Interrupt NVIC_InitStructure.NVIC_IRQChannel = eventIrq; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = mPriority[0]; NVIC_InitStructure.NVIC_IRQChannelSubPriority = mPriority[1]; NVIC_Init(&NVIC_InitStructure); mI2C->CR2 |= I2C_CR2_ITEVTEN;// 开启I2C事件中断 // Configure NVIC for I2Cx ERR Interrupt NVIC_InitStructure.NVIC_IRQChannel = errorIrq; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = mPriority[2]; NVIC_InitStructure.NVIC_IRQChannelSubPriority = mPriority[3]; NVIC_Init(&NVIC_InitStructure); mI2C->CR2 |= I2C_CR2_ITERREN;// 开启I2C错误中断 #ifdef I2C_USE_DMA if(mUseDma) { mI2C->CR2 &= ~I2C_IT_BUF; //使用DMA模式时勿打开 BUF中断 } else//开启了dma开关,但是选择不使用dma功能 { mI2C->CR2 |= I2C_IT_BUF; //使用中断模式打开BUF中断 } #else mI2C->CR2 |= I2C_IT_BUF; //使用中断模式打开BUF中断 #endif #ifdef I2C_USE_DMA if(mUseDma) { // Configure NVIC for DMA TX channel interrupt NVIC_InitStructure.NVIC_IRQChannel = dmaTxIrq ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = mPriority[4]; NVIC_InitStructure.NVIC_IRQChannelSubPriority = mPriority[5]; NVIC_Init(&NVIC_InitStructure); // Enable DMA TX Channel TCIT mDmaTxChannel->CCR |= DMA_IT_TC; //打开发送完成中断 // Enable DMA TX Channel TEIT mDmaTxChannel->CCR |= DMA_IT_TE; //打开错误中断 // Enable DMA TX Channel HTIT //I2C_DMA_TX_Channel->CCR |= DMA_IT_HT; // Configure NVIC for DMA RX channel interrupt NVIC_InitStructure.NVIC_IRQChannel = dmaRxIrq ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = mPriority[4]; NVIC_InitStructure.NVIC_IRQChannelSubPriority = mPriority[5]; NVIC_Init(&NVIC_InitStructure); // Enable DMA TX Channel TCIT mDmaRxChannel->CCR |= DMA_IT_TC; //打开接收完成中断 // Enable DMA TX Channel TEIT mDmaRxChannel->CCR |= DMA_IT_TE; //打开接收错误中断 // Enable DMA TX Channel HTIT //I2C_DMA_RX_Channel->CCR |= DMA_IT_HT; } #endif mState=STATE_READY;//队列状态设置为可以开始下一个任务 return 1; }
/* * @brief Initialize the ADC peripheral. */ void ADC_DMA_Init() { //Using "Dual Slow Interleaved ADC Mode" to achieve higher input impedance ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; // ADCCLK = PCLK2/6 = 72/6 = 12MHz RCC_ADCCLKConfig(RCC_PCLK2_Div6); // Enable DMA1 clock RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // Enable ADC1 and ADC2 clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); // DMA1 channel1 configuration DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_DualConvertedValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = ADC_DMA_BUFFERSIZE; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); // ADC1 configuration ADC_InitStructure.ADC_Mode = ADC_Mode_SlowInterl; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); // ADC2 configuration ADC_InitStructure.ADC_Mode = ADC_Mode_SlowInterl; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC2, &ADC_InitStructure); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); // Enable ADC1 reset calibration register ADC_ResetCalibration(ADC1); // Check the end of ADC1 reset calibration register while(ADC_GetResetCalibrationStatus(ADC1)); // Start ADC1 calibration ADC_StartCalibration(ADC1); // Check the end of ADC1 calibration while(ADC_GetCalibrationStatus(ADC1)); // Enable ADC2 ADC_Cmd(ADC2, ENABLE); // Enable ADC2 reset calibration register ADC_ResetCalibration(ADC2); // Check the end of ADC2 reset calibration register while(ADC_GetResetCalibrationStatus(ADC2)); // Start ADC2 calibration ADC_StartCalibration(ADC2); // Check the end of ADC2 calibration while(ADC_GetCalibrationStatus(ADC2)); }
/** * @brief ADC3 channel06 with DMA configuration * @param None * @retval None */ void ADC3_CH6_DMA_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable ADC3, DMA2 and GPIO clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOF, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); /* DMA2 Stream0 channel2 configuration **************************************/ DMA_InitStructure.DMA_Channel = DMA_Channel_2; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); /* Configure ADC3 Channel6 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOF, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC3 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel6 configuration *************************************/ ADC_RegularChannelConfig(ADC3, ADC_Channel_6, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE); /* Enable ADC3 DMA */ ADC_DMACmd(ADC3, ENABLE); /* Enable ADC3 */ ADC_Cmd(ADC3, ENABLE); }
/****************************************************************************************** *函数名:ADC_Init() * 参数:void * 返回值:void * 功能:ADC初始化配置 *********************************************************************************************/ void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; // RCC_Configuration(); // GPIO_Configuration(); // DMA_Configuration(); // ADC_Configuration(); // NVIC_Configuration(); /*------------------------ DMA1 configuration ------------------------------*/ /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* DMA1 channel1 configuration */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&ADC1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&adc_value[0]; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 4; 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_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /*----------------- ADC1 configuration with DMA enabled --------------------*/ /* Enable the HSI oscillator */ RCC_HSICmd(ENABLE); /* Enable GPIOB clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* Configure PB.12 (ADC Channel18) in analog mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); BAT_MEASURE_RESET; /* Check that HSI oscillator is ready */ while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); /* Enable ADC1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* ADC1 configuration */ ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel18 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_18, 1, ADC_SampleTime_48Cycles); /* Enable the request after last transfer for DMA Circular mode */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait until the ADC1 is ready */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET) { } /* Start ADC1 Software Conversion */ // ADC_SoftwareStartConv(ADC1); }
void ADC_DMA_Init(void) { ADC_InitTypeDef ADC_InitStruct; DMA_InitTypeDef DMA_InitStruct; /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); /* ADC1 DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStruct); /* Configure the ADC1 in continous mode with a resolutuion equal to 12 bits */ ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStruct); /* ADC CLOCK IS 12MHz*/ //0.05MHz, the minimum sampling rate ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_2, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_8, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_9, ADC_SampleTime_239_5Cycles); // ADC_VrefintCmd(ENABLE); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)ADC_DR_Address; DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&ADC_Data; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStruct.DMA_BufferSize = 6; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High; DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStruct); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); }
static void HAL_SPI_DMA_Config(HAL_SPI_Interface spi, void* tx_buffer, void* rx_buffer, uint32_t length) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable DMA Clock */ *spiMap[spi].SPI_RCC_AHBRegister |= spiMap[spi].SPI_RCC_AHBClockEnable; /* Deinitialize DMA Streams */ DMA_DeInit(spiMap[spi].SPI_TX_DMA_Stream); DMA_DeInit(spiMap[spi].SPI_RX_DMA_Stream); DMA_InitStructure.DMA_BufferSize = length; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&spiMap[spi].SPI_Peripheral->DR; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_Channel = spiMap[spi].SPI_DMA_Channel; uint8_t tempMemory = 0xFF; /* Configure Tx DMA Stream */ DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; if (tx_buffer) { DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)tx_buffer; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; } else { DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(&tempMemory); DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; } DMA_Init(spiMap[spi].SPI_TX_DMA_Stream, &DMA_InitStructure); /* Configure Rx DMA Stream */ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; if (rx_buffer) { DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)rx_buffer; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; } else { DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&tempMemory; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; } DMA_Init(spiMap[spi].SPI_RX_DMA_Stream, &DMA_InitStructure); /* Enable SPI TX DMA Stream Interrupt */ DMA_ITConfig(spiMap[spi].SPI_TX_DMA_Stream, DMA_IT_TC, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = spiMap[spi].SPI_TX_DMA_Stream_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the DMA Tx/Rx Stream */ DMA_Cmd(spiMap[spi].SPI_TX_DMA_Stream, ENABLE); DMA_Cmd(spiMap[spi].SPI_RX_DMA_Stream, ENABLE); /* Enable the SPI Rx/Tx DMA request */ SPI_I2S_DMACmd(spiMap[spi].SPI_Peripheral, SPI_I2S_DMAReq_Rx, ENABLE); SPI_I2S_DMACmd(spiMap[spi].SPI_Peripheral, SPI_I2S_DMAReq_Tx, ENABLE); }
void adcInit(drv_adc_config_t *init) { #if defined(CJMCU) || defined(CC3D) UNUSED(init); #endif uint8_t i; uint8_t configuredAdcChannels = 0; memset(&adcConfig, 0, sizeof(adcConfig)); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; #ifdef ADC1_GPIO if (init->channelMask & ADC_CHANNEL1_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC1_GPIO_PIN; GPIO_Init(ADC1_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_1].adcChannel = ADC1_CHANNEL; adcConfig[ADC_CHANNEL_1].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_1].enabled = true; adcConfig[ADC_CHANNEL_1].sampleTime = ADC_SampleTime_239Cycles5; } #endif #ifdef ADC2_GPIO if (init->channelMask & ADC_CHANNEL2_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC2_GPIO_PIN; GPIO_Init(ADC2_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_2].adcChannel = ADC2_CHANNEL; adcConfig[ADC_CHANNEL_2].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_2].enabled = true; adcConfig[ADC_CHANNEL_2].sampleTime = ADC_SampleTime_239Cycles5; } #endif #ifdef ADC3_GPIO if (init->channelMask & ADC_CHANNEL3_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC3_GPIO_PIN; GPIO_Init(ADC3_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_3].adcChannel = ADC3_CHANNEL; adcConfig[ADC_CHANNEL_3].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_3].enabled = true; adcConfig[ADC_CHANNEL_3].sampleTime = ADC_SampleTime_239Cycles5; } #endif #ifdef ADC4_GPIO if (init->channelMask & ADC_CHANNEL4_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC4_GPIO_PIN; GPIO_Init(ADC4_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_4].adcChannel = ADC4_CHANNEL; adcConfig[ADC_CHANNEL_4].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_4].enabled = true; adcConfig[ADC_CHANNEL_4].sampleTime = ADC_SampleTime_239Cycles5; } #endif RCC_ADCCLKConfig(RCC_PCLK2_Div8); // 9MHz from 72MHz APB2 clock(HSE), 8MHz from 64MHz (HSI) RCC_AHBPeriphClockCmd(ADC_AHB_PERIPHERAL, ENABLE); RCC_APB2PeriphClockCmd(ADC_ABP2_PERIPHERAL, ENABLE); // FIXME ADC driver assumes all the GPIO was already placed in 'AIN' mode DMA_DeInit(ADC_DMA_CHANNEL); DMA_InitTypeDef DMA_InitStructure; DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC_INSTANCE->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = configuredAdcChannels; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : 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_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(ADC_DMA_CHANNEL, &DMA_InitStructure); DMA_Cmd(ADC_DMA_CHANNEL, ENABLE); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = configuredAdcChannels > 1 ? ENABLE : DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = configuredAdcChannels; ADC_Init(ADC_INSTANCE, &ADC_InitStructure); uint8_t rank = 1; for (i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcConfig[i].enabled) { continue; } ADC_RegularChannelConfig(ADC_INSTANCE, adcConfig[i].adcChannel, rank++, adcConfig[i].sampleTime); } ADC_DMACmd(ADC_INSTANCE, ENABLE); ADC_Cmd(ADC_INSTANCE, ENABLE); ADC_ResetCalibration(ADC_INSTANCE); while(ADC_GetResetCalibrationStatus(ADC_INSTANCE)); ADC_StartCalibration(ADC_INSTANCE); while(ADC_GetCalibrationStatus(ADC_INSTANCE)); ADC_SoftwareStartConvCmd(ADC_INSTANCE, ENABLE); }
/* ********************************************************************************************************* * 函 数 名: bsp_InitADC * 功能说明: ADC初始化 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ void bsp_InitADC(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* 使能外设时钟 */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB| RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* DMA2 Stream0 channel0 配置-------------------------------------------------- */ DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCConvertedValue;; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 4; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); /* DMA2_Stream0 enable */ DMA_Cmd(DMA2_Stream0, ENABLE); /**************************************************************************** PCLK2 = HCLK / 2 下面选择的是2分频 ADCCLK = PCLK2 /8 = HCLK / 8 = 168 / 8 = 21M ADC采样频率: Sampling Time + Conversion Time = 480 + 12 cycles = 492cyc Conversion Time = 21MHz / 492cyc = 42.6ksps. *****************************************************************************/ /* ADC Common 配置 ----------------------------------------------------------*/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 regular channel 12 configuration ************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 4; ADC_Init(ADC1, &ADC_InitStructure); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 5, ADC_SampleTime_15Cycles); /* ADC1 regular channel18 (VBAT) configuration ******************************/ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_15Cycles); /* ADC1 regular channel18 (VBAT) configuration *****************************/ ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_15Cycles); /* ADC1 regular channels 10, 11 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 3, ADC_SampleTime_15Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 4, ADC_SampleTime_15Cycles); // /* Enable VBAT channel */ // ADC_VBATCmd(ENABLE); // // ADC_TempSensorVrefintCmd(ENABLE); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Enable ADC1 **************************************************************/ ADC_Cmd(ADC1, ENABLE); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); }
/** * @brief Configure the DMA controller according to the Stream parameters * defined in main.h file * @param None * @retval None */ void DMA_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; DMA_InitTypeDef DMA_InitStructure; __IO uint32_t Timeout = TIMEOUT_MAX; /* Enable DMA clock */ RCC_AHB1PeriphClockCmd(DMA_STREAM_CLOCK, ENABLE); /* Reset DMA Stream registers (for debug purpose) */ DMA_DeInit(DMA_STREAM); /* Check if the DMA Stream is disabled before enabling it. Note that this step is useful when the same Stream is used multiple times: enabled, then disabled then re-enabled... In this case, the DMA Stream disable will be effective only at the end of the ongoing data transfer and it will not be possible to re-configure it before making sure that the Enable bit has been cleared by hardware. If the Stream is used only once, this step might be bypassed. */ while (DMA_GetCmdStatus(DMA_STREAM) != DISABLE) { } /* Configure DMA Stream */ DMA_InitStructure.DMA_Channel = DMA_CHANNEL; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SRC_Const_Buffer; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DST_Buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToMemory; DMA_InitStructure.DMA_BufferSize = (uint32_t)BUFFER_SIZE; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA_STREAM, &DMA_InitStructure); /* Enable DMA Stream Transfer Complete interrupt */ DMA_ITConfig(DMA_STREAM, DMA_IT_TC, ENABLE); /* DMA Stream enable */ DMA_Cmd(DMA_STREAM, ENABLE); /* Check if the DMA Stream has been effectively enabled. The DMA Stream Enable bit is cleared immediately by hardware if there is an error in the configuration parameters and the transfer is no started (ie. when wrong FIFO threshold is configured ...) */ Timeout = TIMEOUT_MAX; while ((DMA_GetCmdStatus(DMA_STREAM) != ENABLE) && (Timeout-- > 0)) { } /* Check if a timeout condition occurred */ if (Timeout == 0) { /* Manage the error: to simplify the code enter an infinite loop */ while (1) { } } /* Enable the DMA Stream IRQ Channel */ NVIC_InitStructure.NVIC_IRQChannel = DMA_STREAM_IRQ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void adcInit(const adcConfig_t *config) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; uint8_t adcChannelCount = 0; memset(&adcOperatingConfig, 0, sizeof(adcOperatingConfig)); if (config->vbat.enabled) { adcOperatingConfig[ADC_BATTERY].tag = config->vbat.ioTag; } if (config->rssi.enabled) { adcOperatingConfig[ADC_RSSI].tag = config->rssi.ioTag; //RSSI_ADC_CHANNEL; } if (config->external1.enabled) { adcOperatingConfig[ADC_EXTERNAL1].tag = config->external1.ioTag; //EXTERNAL1_ADC_CHANNEL; } if (config->current.enabled) { adcOperatingConfig[ADC_CURRENT].tag = config->current.ioTag; //CURRENT_METER_ADC_CHANNEL; } ADCDevice device = adcDeviceByInstance(ADC_INSTANCE); if (device == ADCINVALID) return; #ifdef ADC24_DMA_REMAP SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_ADC2ADC4, ENABLE); #endif adcDevice_t adc = adcHardware[device]; bool adcActive = false; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcVerifyPin(adcOperatingConfig[i].tag, device)) { continue; } adcActive = true; IOInit(IOGetByTag(adcOperatingConfig[i].tag), OWNER_ADC_BATT + i, 0); IOConfigGPIO(IOGetByTag(adcOperatingConfig[i].tag), IO_CONFIG(GPIO_Mode_AN, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)); adcOperatingConfig[i].adcChannel = adcChannelByTag(adcOperatingConfig[i].tag); adcOperatingConfig[i].dmaIndex = adcChannelCount++; adcOperatingConfig[i].sampleTime = ADC_SampleTime_601Cycles5; adcOperatingConfig[i].enabled = true; } if (!adcActive) { return; } if ((device == ADCDEV_1) || (device == ADCDEV_2)) { // enable clock for ADC1+2 RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz } else { // enable clock for ADC3+4 RCC_ADCCLKConfig(RCC_ADC34PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz } RCC_ClockCmd(adc.rccADC, ENABLE); dmaInit(dmaGetIdentifier(adc.DMAy_Channelx), OWNER_ADC, 0); DMA_DeInit(adc.DMAy_Channelx); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&adc.ADCx->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = adcChannelCount; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = adcChannelCount > 1 ? DMA_MemoryInc_Enable : 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_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(adc.DMAy_Channelx, &DMA_InitStructure); DMA_Cmd(adc.DMAy_Channelx, ENABLE); // calibrate ADC_VoltageRegulatorCmd(adc.ADCx, ENABLE); delay(10); ADC_SelectCalibrationMode(adc.ADCx, ADC_CalibrationMode_Single); ADC_StartCalibration(adc.ADCx); while (ADC_GetCalibrationStatus(adc.ADCx) != RESET); ADC_VoltageRegulatorCmd(adc.ADCx, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonStructInit(&ADC_CommonInitStructure); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(adc.ADCx, &ADC_CommonInitStructure); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = adcChannelCount; ADC_Init(adc.ADCx, &ADC_InitStructure); uint8_t rank = 1; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcOperatingConfig[i].enabled) { continue; } ADC_RegularChannelConfig(adc.ADCx, adcOperatingConfig[i].adcChannel, rank++, adcOperatingConfig[i].sampleTime); } ADC_Cmd(adc.ADCx, ENABLE); while (!ADC_GetFlagStatus(adc.ADCx, ADC_FLAG_RDY)); ADC_DMAConfig(adc.ADCx, ADC_DMAMode_Circular); ADC_DMACmd(adc.ADCx, ENABLE); ADC_StartConversion(adc.ADCx); }
void Peripherals_Init(void) { #ifdef NVIC_AUTOINIT NVIC_Init(); #endif /* NVIC_AUTOINIT */ #ifdef SIM_AUTOINIT SIM_Init(); #endif /* SIM_AUTOINIT */ #ifdef MCM_AUTOINIT MCM_Init(); #endif /* MCM_AUTOINIT */ #ifdef PMC_AUTOINIT PMC_Init(); #endif /* PMC_AUTOINIT */ #ifdef PORTA_AUTOINIT PORTA_Init(); #endif /* PORTA_AUTOINIT */ #ifdef PORTB_AUTOINIT PORTB_Init(); #endif /* PORTB_AUTOINIT */ #ifdef PORTC_AUTOINIT PORTC_Init(); #endif /* PORTC_AUTOINIT */ #ifdef PORTD_AUTOINIT PORTD_Init(); #endif /* PORTD_AUTOINIT */ #ifdef PORTE_AUTOINIT PORTE_Init(); #endif /* PORTE_AUTOINIT */ #ifdef ADC0_AUTOINIT ADC0_Init(); #endif /* ADC0_AUTOINIT */ #ifdef ADC1_AUTOINIT ADC1_Init(); #endif /* ADC1_AUTOINIT */ #ifdef AIPS0_AUTOINIT AIPS0_Init(); #endif /* AIPS0_AUTOINIT */ #ifdef CMP0_AUTOINIT CMP0_Init(); #endif /* CMP0_AUTOINIT */ #ifdef CMP1_AUTOINIT CMP1_Init(); #endif /* CMP1_AUTOINIT */ #ifdef CRC_AUTOINIT CRC_Init(); #endif /* CRC_AUTOINIT */ #ifdef DAC0_AUTOINIT DAC0_Init(); #endif /* DAC0_AUTOINIT */ #ifdef DAC1_AUTOINIT DAC1_Init(); #endif /* DAC1_AUTOINIT */ #ifdef DMAMUX_AUTOINIT DMAMUX_Init(); #endif /* DMAMUX_AUTOINIT */ #ifdef DMA_AUTOINIT DMA_Init(); #endif /* DMA_AUTOINIT */ #ifdef EWM_AUTOINIT EWM_Init(); #endif /* EWM_AUTOINIT */ #ifdef FB_AUTOINIT FB_Init(); #endif /* FB_AUTOINIT */ #ifdef FMC_AUTOINIT FMC_Init(); #endif /* FMC_AUTOINIT */ #ifdef FTFA_AUTOINIT FTFA_Init(); #endif /* FTFA_AUTOINIT */ #ifdef FTM0_AUTOINIT FTM0_Init(); #endif /* FTM0_AUTOINIT */ #ifdef FTM1_AUTOINIT FTM1_Init(); #endif /* FTM1_AUTOINIT */ #ifdef FTM2_AUTOINIT FTM2_Init(); #endif /* FTM2_AUTOINIT */ #ifdef FTM3_AUTOINIT FTM3_Init(); #endif /* FTM3_AUTOINIT */ #ifdef I2C0_AUTOINIT I2C0_Init(); #endif /* I2C0_AUTOINIT */ #ifdef I2C1_AUTOINIT I2C1_Init(); #endif /* I2C1_AUTOINIT */ #ifdef I2S0_AUTOINIT I2S0_Init(); #endif /* I2S0_AUTOINIT */ #ifdef LLWU_AUTOINIT LLWU_Init(); #endif /* LLWU_AUTOINIT */ #ifdef LPTMR0_AUTOINIT LPTMR0_Init(); #endif /* LPTMR0_AUTOINIT */ #ifdef LPUART0_AUTOINIT LPUART0_Init(); #endif /* LPUART0_AUTOINIT */ #ifdef PDB0_AUTOINIT PDB0_Init(); #endif /* PDB0_AUTOINIT */ #ifdef PIT_AUTOINIT PIT_Init(); #endif /* PIT_AUTOINIT */ #ifdef PTA_AUTOINIT PTA_Init(); #endif /* PTA_AUTOINIT */ #ifdef PTB_AUTOINIT PTB_Init(); #endif /* PTB_AUTOINIT */ #ifdef PTC_AUTOINIT PTC_Init(); #endif /* PTC_AUTOINIT */ #ifdef PTD_AUTOINIT PTD_Init(); #endif /* PTD_AUTOINIT */ #ifdef PTE_AUTOINIT PTE_Init(); #endif /* PTE_AUTOINIT */ #ifdef RCM_AUTOINIT RCM_Init(); #endif /* RCM_AUTOINIT */ #ifdef RNG_AUTOINIT RNG_Init(); #endif /* RNG_AUTOINIT */ #ifdef RTC_AUTOINIT RTC_Init(); #endif /* RTC_AUTOINIT */ #ifdef SMC_AUTOINIT SMC_Init(); #endif /* SMC_AUTOINIT */ #ifdef SPI0_AUTOINIT SPI0_Init(); #endif /* SPI0_AUTOINIT */ #ifdef SPI1_AUTOINIT SPI1_Init(); #endif /* SPI1_AUTOINIT */ #ifdef SystemControl_AUTOINIT SystemControl_Init(); #endif /* SystemControl_AUTOINIT */ #ifdef SysTick_AUTOINIT SysTick_Init(); #endif /* SysTick_AUTOINIT */ #ifdef UART0_AUTOINIT UART0_Init(); #endif /* UART0_AUTOINIT */ #ifdef UART1_AUTOINIT UART1_Init(); #endif /* UART1_AUTOINIT */ #ifdef UART2_AUTOINIT UART2_Init(); #endif /* UART2_AUTOINIT */ #ifdef USB0_AUTOINIT USB0_Init(); #endif /* USB0_AUTOINIT */ #ifdef VREF_AUTOINIT VREF_Init(); #endif /* VREF_AUTOINIT */ #ifdef WDOG_AUTOINIT WDOG_Init(); #endif /* WDOG_AUTOINIT */ }
int hal_adc_open(HAL_ADC_HANDLE *handle, void *params) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; UNUSED(params); /* DMA2_Stream0 channel0 configuration **************************************/ DMA_DeInit(DMA2_Stream0); DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)convertedValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); /* DMA2_Stream0 enable */ DMA_Cmd(DMA2_Stream0, ENABLE); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* ADC1 regular channel18 (VBAT) configuration ******************************/ ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_480Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Enable ADC1 **************************************************************/ ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); *handle = (void *)convertedValues; return HAL_ADC_E_SUCCESS; }
void adc_init(void) { ADC_InitTypeDef ADC_InitStructure; { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = BATTERYPIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(BATTERYPORT, &GPIO_InitStructure); } RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); { DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x40012440; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcarray; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 2; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); } ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); ADC_DMACmd(ADC1, ENABLE); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, BATTERY_ADC_CHANNEL , ADC_SampleTime_239_5Cycles); ADC_VrefintCmd(ENABLE); ADC_GetCalibrationFactor(ADC1); ADC_Cmd(ADC1, ENABLE); ADC_StartOfConversion(ADC1); DMA_Cmd(DMA1_Channel1, ENABLE); // reference is measured a 3.3v, we are powered by 2.8, so a 1.17 multiplier // different vccs will translate to a different adc scale factor, // so actual vcc is not important as long as the voltage is correct in the end vref_cal = 1.17857f * (float) ( adcref_read ((adcrefcal *) 0x1FFFF7BA) ); }
/** * @brief Configures DCMI/DMA to capture image from the OV2640 camera. * @param ImageFormat: Image format BMP or JPEG * @param BMPImageSize: BMP Image size * @retval None */ void OV2640_Init(ImageFormat_TypeDef ImageFormat) { DCMI_InitTypeDef DCMI_InitStructure; DMA_InitTypeDef DMA_InitStructure; /*** Configures the DCMI to interface with the OV2640 camera module ***/ /* Enable DCMI clock */ RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE); /* DCMI configuration */ DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_Continuous; DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware; DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Rising; DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_Low; DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_Low; DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame; DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b; /* Configures the DMA2 to transfer Data from DCMI */ /* Enable DMA2 clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); /* DMA2 Stream1 Configuration */ DMA_DeInit(DMA2_Stream1); DMA_InitStructure.DMA_Channel = DMA_Channel_1; DMA_InitStructure.DMA_PeripheralBaseAddr = DCMI_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = FSMC_LCD_ADDRESS; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 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_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; switch(ImageFormat) { case BMP_QQVGA: { /* DCMI configuration */ DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High; DCMI_Init(&DCMI_InitStructure); /* DMA2 IRQ channel Configuration */ DMA_Init(DMA2_Stream1, &DMA_InitStructure); break; } case BMP_QVGA: { /* DCMI configuration */ DCMI_Init(&DCMI_InitStructure); /* DMA2 IRQ channel Configuration */ DMA_Init(DMA2_Stream1, &DMA_InitStructure); break; } default: { /* DCMI configuration */ DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High; DCMI_Init(&DCMI_InitStructure); /* DMA2 IRQ channel Configuration */ DMA_Init(DMA2_Stream1, &DMA_InitStructure); break; } } }
/*************************************************************************************************** * @fn ana_inputs_regist * * @brief Ò£¸Ë¡¢²¦ÂÖ¼°µçѹ¼à²â×ÊÔ´×¢²á * @param NULL * @return null ***************************************************************************************************/ void ana_inputs_regist(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //GPIO init //STICK RV RCC_AHB1PeriphClockCmd(STICK_RV_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = STICK_RV_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(STICK_RV_PORT, &GPIO_InitStructure); //STICK RH RCC_AHB1PeriphClockCmd(STICK_RH_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = STICK_RH_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(STICK_RH_PORT, &GPIO_InitStructure); //STICK LH RCC_AHB1PeriphClockCmd(STICK_LH_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = STICK_LH_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(STICK_LH_PORT, &GPIO_InitStructure); //STICK LV RCC_AHB1PeriphClockCmd(STICK_LV_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = STICK_LV_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(STICK_LV_PORT, &GPIO_InitStructure); //ROTATE_LU RCC_AHB1PeriphClockCmd(ROTATE_LU_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = ROTATE_LU_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(ROTATE_LU_PORT, &GPIO_InitStructure); //ROTATE_LD RCC_AHB1PeriphClockCmd(ROTATE_LD_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = ROTATE_LD_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(ROTATE_LD_PORT, &GPIO_InitStructure); //ROTATE_RU RCC_AHB1PeriphClockCmd(ROTATE_RU_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = ROTATE_RU_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(ROTATE_RU_PORT, &GPIO_InitStructure); //ROTATE_RD RCC_AHB1PeriphClockCmd(ROTATE_RD_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = ROTATE_RD_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(ROTATE_RD_PORT, &GPIO_InitStructure); //MONITOR_VOL RCC_AHB1PeriphClockCmd(MONITOR_VOL_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = MONITOR_VOL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(MONITOR_VOL_PORT, &GPIO_InitStructure); //--------------ADC init--------------------------------- RCC_APB2PeriphClockCmd(SKYBORNE_ADC_CLK, ENABLE); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = ADC_MODULE_NUMBER; ADC_Init(SKYBORNE_ADC, &ADC_InitStructure); //²ÉÑùÅÅÐò ADC_RegularChannelConfig(SKYBORNE_ADC, STICK_RV_ADC_CHANNEL, STICK_RV+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, STICK_RH_ADC_CHANNEL, STICK_RH+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, STICK_LH_ADC_CHANNEL, STICK_LH+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, STICK_LV_ADC_CHANNEL, STICK_LV+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, ROTATE_LU_ADC_CHANNEL, ROTATE_LU+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, ROTATE_LD_ADC_CHANNEL, ROTATE_LD+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, ROTATE_RU_ADC_CHANNEL, ROTATE_RU+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, ROTATE_RD_ADC_CHANNEL, ROTATE_RD+1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(SKYBORNE_ADC, MONITOR_VOL_ADC_CHANNEL, MONITOR_VOL+1, ADC_SampleTime_28Cycles); //DMA init RCC_AHB1PeriphClockCmd(SKYBORNE_ADC_DMA_CLK, ENABLE); DMA_Cmd(SKYBORNE_ADC_DMA_STREAM,DISABLE); DMA_DeInit(SKYBORNE_ADC_DMA_STREAM); DMA_InitStructure.DMA_Channel = SKYBORNE_ADC_DMA_CHANNEL; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&SKYBORNE_ADC->DR); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)g_adcs_value; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = ADC_MODULE_NUMBER; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(SKYBORNE_ADC_DMA_STREAM, &DMA_InitStructure); //NVIC NVIC_InitStructure.NVIC_IRQChannel = SKYBORNE_ADC_DMA_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = SKYBORNE_ADC_NVIC_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); ADC_Cmd(SKYBORNE_ADC, ENABLE); }
void adcInit(drv_adc_config_t *init) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; bool multiChannel = init->powerAdcChannel > 0; /* Enable ADC1, DMA2 clocks *************************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA2 Stream0 channel0 configuration **************************************/ DMA_DeInit(DMA2_Stream0); DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = multiChannel ? 2 : 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); /* Configure ADC3 Channel12 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_DeInit(); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = multiChannel ? ENABLE : DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = 0; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = multiChannel ? 2 : 1; ADC_Init(ADC1, &ADC_InitStructure); /* Enable ADC1 DMA */ //ADC_DMACmd(ADC1, ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles); if (multiChannel) ADC_RegularChannelConfig(ADC1, init->powerAdcChannel, 2, ADC_SampleTime_28Cycles); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); // Calibrate ADC //ADC_ResetCalibration(ADC1); //while(ADC_GetResetCalibrationStatus(ADC1)); // ADC_StartCalibration(ADC1); // while(ADC_GetCalibrationStatus(ADC1)); // Fire off ADC ADC_SoftwareStartConv(ADC1); }
int main() { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; DMA_InitTypeDef DMA_InitStructure; uint16_t PrescalerValue; LED_Init(); LED_R_ON(); LLIO_Init(115200); printf("\r\nPWM dimming\r\n"); /* Enable ADC, GPIO, Timer, DMA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); /* DMA2 Channel0(ADC) configuration */ DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; // source DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)TIM3_CCR1_Address; // destination DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); /* TIM3_CH1(PB4-LED) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3); /* ADC Common Init */ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; // ADC clock = PCLK2/prescaler. ADC freq = typical 30MHz, Max 36MHz. PCLK2 = 168/2=84MHz. ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC channel 10 (PC0) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADC channel configuration */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; // TIM3의 트리거 발생시 ADC 수행 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel10 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_3Cycles); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Compute the prescaler value */ SystemCoreClockUpdate(); PrescalerValue = (uint16_t) (SystemCoreClock / 2 / TIMER_PRESCALER_FREQ) - 1; // timer base counter에 1MHz 입력 /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = PWM_WIDTH - 1; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // TIM3 update 이벤트에서 트리거 발생하도록 설정 -> ADC trigger로 사용 TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = DUTY_IDLE; // default 50% TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); while(1) { __WFI(); } }
void main(void) #endif { RST_CLK_DeInit(); RST_CLK_CPU_PLLconfig (RST_CLK_CPU_PLLsrcHSIdiv2,0); /* Enable peripheral clocks --------------------------------------------------*/ RST_CLK_PCLKcmd((RST_CLK_PCLK_RST_CLK | RST_CLK_PCLK_SSP1 | RST_CLK_PCLK_SSP2 | RST_CLK_PCLK_DMA),ENABLE); RST_CLK_PCLKcmd((RST_CLK_PCLK_PORTF | RST_CLK_PCLK_PORTD), ENABLE); /* Init NVIC */ SCB->AIRCR = 0x05FA0000 | ((uint32_t)0x500); SCB->VTOR = 0x08000000; /* Disable all interrupt */ NVIC->ICPR[0] = 0xFFFFFFFF; NVIC->ICER[0] = 0xFFFFFFFF; /* Disable all DMA request */ MDR_DMA->CHNL_REQ_MASK_CLR = 0xFFFFFFFF; MDR_DMA->CHNL_USEBURST_CLR = 0xFFFFFFFF; /* Reset PORTD settings */ PORT_DeInit(MDR_PORTD); /* Reset PORTF settings */ PORT_DeInit(MDR_PORTF); /* Configure SSP2 pins: FSS, CLK, RXD, TXD */ /* Configure PORTD pins 2, 3, 5, 6 */ PORT_InitStructure.PORT_Pin = (PORT_Pin_2 | PORT_Pin_3 | PORT_Pin_5); PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_InitStructure.PORT_FUNC = PORT_FUNC_ALTER; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST; PORT_Init(MDR_PORTD, &PORT_InitStructure); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_Pin = (PORT_Pin_6); PORT_Init(MDR_PORTD, &PORT_InitStructure); /* Configure SSP1 pins: FSS, CLK, RXD, TXD */ /* Configure PORTF pins 0, 1, 2, 3 */ PORT_InitStructure.PORT_Pin = (PORT_Pin_3); PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_Init(MDR_PORTF, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_0 | PORT_Pin_1 | PORT_Pin_2); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_Init(MDR_PORTF, &PORT_InitStructure); /* Init RAM */ Init_RAM (DstBuf1, BufferSize); Init_RAM (SrcBuf1, BufferSize); Init_RAM (DstBuf2, BufferSize); Init_RAM (SrcBuf2, BufferSize); /* Reset all SSP settings */ SSP_DeInit(MDR_SSP1); SSP_DeInit(MDR_SSP2); SSP_BRGInit(MDR_SSP1,SSP_HCLKdiv16); SSP_BRGInit(MDR_SSP2,SSP_HCLKdiv16); /* SSP1 MASTER configuration ------------------------------------------------*/ SSP_StructInit (&sSSP); sSSP.SSP_SCR = 0x10; sSSP.SSP_CPSDVSR = 2; sSSP.SSP_Mode = SSP_ModeMaster; sSSP.SSP_WordLength = SSP_WordLength16b; sSSP.SSP_SPH = SSP_SPH_1Edge; sSSP.SSP_SPO = SSP_SPO_Low; sSSP.SSP_FRF = SSP_FRF_SPI_Motorola; sSSP.SSP_HardwareFlowControl = SSP_HardwareFlowControl_SSE; SSP_Init (MDR_SSP1,&sSSP); /* SSP2 SLAVE configuration ------------------------------------------------*/ sSSP.SSP_SPH = SSP_SPH_1Edge; sSSP.SSP_SPO = SSP_SPO_Low; sSSP.SSP_CPSDVSR = 12; sSSP.SSP_Mode = SSP_ModeSlave; SSP_Init (MDR_SSP2,&sSSP); /* Enable SSP1 DMA Rx and Tx request */ SSP_DMACmd(MDR_SSP1,(SSP_DMA_RXE | SSP_DMA_TXE), ENABLE); /* Enable SSP2 DMA Rx and Tx request */ SSP_DMACmd(MDR_SSP2,(SSP_DMA_RXE | SSP_DMA_TXE), ENABLE); /* Reset all DMA settings */ DMA_DeInit(); DMA_StructInit(&DMA_InitStr); /* DMA_Channel_SSP1_RX configuration ---------------------------------*/ /* Set Primary Control Data */ DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_SSP1->DR)); DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)DstBuf1; DMA_PriCtrlStr.DMA_SourceIncSize = DMA_SourceIncNo; DMA_PriCtrlStr.DMA_DestIncSize = DMA_DestIncHalfword; DMA_PriCtrlStr.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_PriCtrlStr.DMA_Mode = DMA_Mode_Basic; DMA_PriCtrlStr.DMA_CycleSize = BufferSize; DMA_PriCtrlStr.DMA_NumContinuous = DMA_Transfers_4; DMA_PriCtrlStr.DMA_SourceProtCtrl = DMA_SourcePrivileged; DMA_PriCtrlStr.DMA_DestProtCtrl = DMA_DestPrivileged; /* Set Channel Structure */ DMA_InitStr.DMA_PriCtrlData = &DMA_PriCtrlStr; DMA_InitStr.DMA_Priority = DMA_Priority_High; DMA_InitStr.DMA_UseBurst = DMA_BurstClear; DMA_InitStr.DMA_SelectDataStructure = DMA_CTRL_DATA_PRIMARY; /* Init DMA channel */ DMA_Init(DMA_Channel_SSP1_RX, &DMA_InitStr); /* DMA_Channel_SSP2_RX configuration ---------------------------------*/ /* Set Primary Control Data */ DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_SSP2->DR)); DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)DstBuf2; /* Init DMA channel */ DMA_Init(DMA_Channel_SSP2_RX, &DMA_InitStr); /* DMA_Channel_SSP1_TX configuration ---------------------------------*/ /* Set Primary Control Data */ DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)SrcBuf1; DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)(&(MDR_SSP1->DR)); DMA_PriCtrlStr.DMA_SourceIncSize = DMA_SourceIncHalfword; DMA_PriCtrlStr.DMA_DestIncSize = DMA_DestIncNo; DMA_InitStr.DMA_Priority = DMA_Priority_Default; /* Init DMA channel */ DMA_Init(DMA_Channel_SSP1_TX, &DMA_InitStr); /* DMA_Channel_SSP2_TX configuration ---------------------------------*/ /* Set Primary Control Data */ DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)SrcBuf2; DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)(&(MDR_SSP2->DR)); /* Init DMA channel */ DMA_Init(DMA_Channel_SSP2_TX, &DMA_InitStr); /* Enable SSP1 */ SSP_Cmd(MDR_SSP1, ENABLE); /* Enable SSP2 */ SSP_Cmd(MDR_SSP2, ENABLE); /* Transfer complete */ while((SSP_GetFlagStatus(MDR_SSP1, SSP_FLAG_BSY))) { } while((SSP_GetFlagStatus(MDR_SSP2, SSP_FLAG_BSY))) { } /* Check the corectness of written dada */ TransferStatus1 = Verif_mem ((BufferSize), SrcBuf1, DstBuf2); TransferStatus2 = Verif_mem ((BufferSize), SrcBuf2, DstBuf1); /* TransferStatus1, TransferStatus2 = PASSED, if the data transmitted and received are correct */ /* TransferStatus1, TransferStatus2 = FAILED, if the data transmitted and received are different */ while(1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* TIM1 and GPIOA clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA, ENABLE); /* DMA clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* GPIOA Configuration: Channel 1 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* TIM1 DeInit */ TIM_DeInit(TIM1); /* DMA1 Channel5 Config */ DMA_DeInit(DMA1_Channel5); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_DMAR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SRC_Buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 3; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel5, &DMA_InitStructure); /* Time base configuration */ /* ----------------------------------------------------------------------- TIM1 Configuration: generate 1 PWM signal using the DMA burst mode: The TIM1CLK frequency is set to SystemCoreClock (Hz), to get TIM1 counter clock at 24 MHz the Prescaler is computed as following: - Prescaler = (TIM1CLK / TIM1 counter clock) - 1 SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density and Connectivity line devices and to 24 MHz for Low-Density Value line and Medium-Density Value line devices The TIM1 period is 5.8 KHz: TIM1 Frequency = TIM1 counter clock/(ARR + 1) = 24 MHz / 4096 = 5.8KHz KHz TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR)* 100 = 33.33% ----------------------------------------------------------------------- */ TIM_TimeBaseStructure.TIM_Period = 0xFFFF; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 24000000) - 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* TIM Configuration in PWM Mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0xFFF; TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* TIM1 DMAR Base register and DMA Burst Length Config */ TIM_DMAConfig(TIM1, TIM_DMABase_ARR, TIM_DMABurstLength_3Transfers); /* TIM1 DMA Update enable */ TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE); /* TIM1 enable */ TIM_Cmd(TIM1, ENABLE); /* TIM1 PWM Outputs Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); /* DMA1 Channel5 enable */ DMA_Cmd(DMA1_Channel5, ENABLE); /* Wait until DMA1 Channel5 end of Transfer */ while (!DMA_GetFlagStatus(DMA1_FLAG_TC5)) { } /* Infinite loop */ while(1) { } }
/************************************************************************* * Function Name: SPI2_DmaTransfer * Parameters: pInt8U pData,Int32U Size, SPI_TransferDir_t SPI_TransferDir * Return: none * * Description: DMA transfer * *************************************************************************/ void SPI2_DmaTransfer(pInt8U pData,Int32U Size, SPI_TransferDir_t SPI_TransferDir) { DMA_InitTypeDef DMA_InitStructure; Int32U Dummy = 0xFF; // Initialize DMA Rx channel DMA_DeInit(DMA_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI2->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (SPI_TransferDir == SPI_RECEIVE)?(Int32U)pData:(Int32U)&Dummy; DMA_InitStructure.DMA_BufferSize = Size; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = (SPI_TransferDir == SPI_RECEIVE)?DMA_MemoryInc_Enable:DMA_MemoryInc_Disable; 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; // Init channel DMA_Init(DMA_Channel4, &DMA_InitStructure); // Initialize DMA Tx channel DMA_DeInit(DMA_Channel5); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI2->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (SPI_TransferDir == SPI_TRANSMIT)?(Int32U)pData:(Int32U)&Dummy; DMA_InitStructure.DMA_BufferSize = Size; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = (SPI_TransferDir == SPI_TRANSMIT)?DMA_MemoryInc_Enable:DMA_MemoryInc_Disable; 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; // Init channel DMA_Init(DMA_Channel5, &DMA_InitStructure); // Enable SPI2 DMA transfer SPI_DMACmd(SPI2,SPI_DMAReq_Rx,ENABLE); SPI_DMACmd(SPI2,SPI_DMAReq_Tx,ENABLE); #ifdef DMA_ERRATA ENTR_CRT_SECTION(); // Enable channel DMA_Cmd(DMA_Channel4,ENABLE); DMA_Cmd(DMA_Channel5,ENABLE); while(1) { if ( (DMA_GetITStatus(DMA_IT_TE4) == SET) || (DMA_GetITStatus(DMA_IT_TE5) == SET)) { DMA_ClearITPendingBit(DMA_IT_GL4 | DMA_IT_GL5); DMA_Cmd(DMA_Channel5,DISABLE); DMA_Cmd(DMA_Channel4,DISABLE); break; } if ( (DMA_GetITStatus(DMA_IT_TC4) == SET) && (DMA_GetITStatus(DMA_IT_TC5) == SET)) { break; } }; EXT_CRT_SECTION(); #else // set the flag DMA Transfer in progress TransferStatus = TRUE; DMA_ITConfig(DMA_Channel4, DMA_IT_TC | DMA_IT_TE, ENABLE); DMA_ITConfig(DMA_Channel5, DMA_IT_TE, ENABLE); // Enable SPI2 DMA transfer SPI_DMACmd(SPI2,SPI_DMAReq_Rx,ENABLE); SPI_DMACmd(SPI2,SPI_DMAReq_Tx,ENABLE); while(TransferStatus); #endif // wait until SPI transmit FIFO isn't empty while(SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE)==RESET); // wait until SPI receive FIFO isn't empty while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE)==SET); SPI_DMACmd(SPI2,SPI_DMAReq_Tx,DISABLE); SPI_DMACmd(SPI2,SPI_DMAReq_Rx,DISABLE); }
/* 函数名:ADC1_Mode_Config * 描述 :配置ADC1的工作模式为DMA模式 * 输入 : 无 * 输出 :无 * 调用 :内部调用 */ static void ADC1_Mode_Config(void) { DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* DMA channel1 configuration */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //ADC地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//内存地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 96; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址可变 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_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* ADC1 configuration */ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立ADC模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE ; //扫描模式,扫描模式用于多通道采集 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //开启连续转换模式,即不停地进行ADC转换 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //不使用外部触发转换 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //采集数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = 3; //要转换的通道数目3 ADC_Init(ADC1, &ADC_InitStructure); /*配置ADC时钟,为PCLK2的8分频,即9MHz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div8); /*配置ADC1的通道2.1.0为239.5个采样周期,序列为3,2,1,转换两路需要53.2us */ ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /*复位校准寄存器 */ ADC_ResetCalibration(ADC1); /*等待校准寄存器复位完成 */ while(ADC_GetResetCalibrationStatus(ADC1)); /* ADC校准 */ ADC_StartCalibration(ADC1); /* 等待校准完成*/ while(ADC_GetCalibrationStatus(ADC1)); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /*Enable interruput of DMA1_Channel1 */ DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); /* 由于没有采用外部触发,所以使用软件触发ADC转换 */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // Enable the DMA Interrupt }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* NVIC configuration ------------------------------------------------------*/ NVIC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration(); /* TIM1 configuration ------------------------------------------------------*/ /* Time Base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x4; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* TIM1 channel1 configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0x7F; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* DMA1 Channel1 Configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_RegularConvertedValueTab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 32; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel14 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5); /* Set injected sequencer length */ ADC_InjectedSequencerLengthConfig(ADC1, 1); /* ADC1 injected channel Configuration */ ADC_InjectedChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_71Cycles5); /* ADC1 injected external trigger configuration */ ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None); /* Enable automatic injected conversion start after regular one */ ADC_AutoInjectedConvCmd(ADC1, ENABLE); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 external trigger */ ADC_ExternalTrigConvCmd(ADC1, ENABLE); /* Enable JEOC interrupt */ ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* TIM1 counter enable */ TIM_Cmd(TIM1, ENABLE); /* TIM1 main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); /* Test on channel1 transfer complete flag */ while(!DMA_GetFlagStatus(DMA1_FLAG_TC1)); /* Clear channel1 transfer complete flag */ DMA_ClearFlag(DMA1_FLAG_TC1); /* TIM1 counter disable */ TIM_Cmd(TIM1, DISABLE); while (1) { } }
void COMInit(COM_TypeDef COM) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM] | RCC_AHB1Periph_GPIOD, ENABLE); if (COM == USART_COM1) { /* Enable UART clock */ RCC_APB2PeriphClockCmd(COM_USART_CLK[COM], ENABLE); } /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(COM_TX_PORT[COM], COM_TX_PIN_SOURCE[COM], COM_TX_AF[COM]); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(COM_RX_PORT[COM], COM_RX_PIN_SOURCE[COM], COM_RX_AF[COM]); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM]; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM]; GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure); /*USART Init code*/ USART_OverSampling8Cmd(COM_USART[COM], ENABLE); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(COM_USART[COM], &USART_InitStructure); /*USART Interrupt code*/ /*USART TX interrupt*/ NVIC_InitStructure.NVIC_IRQChannel = COM_DMA_TX_IRQn[COM]; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = COM_TX_DMA_PREPRIO[COM]; NVIC_InitStructure.NVIC_IRQChannelSubPriority = COM_TX_DMA_SUBPRIO[COM]; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*USART RX interrupt*/ NVIC_InitStructure.NVIC_IRQChannel = COM_DMA_RX_IRQn[COM]; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = COM_RX_DMA_PREPRIO[COM]; NVIC_InitStructure.NVIC_IRQChannelSubPriority = COM_RX_DMA_SUBPRIO[COM]; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); RCC_AHB1PeriphClockCmd(COM_DMA_CLK[COM], ENABLE); /*USART TX clean up*/ DMA_ClearFlag(COM_TX_DMA_STREAM[COM], COM_TX_DMA_FLAG_FEIF[COM] | COM_TX_DMA_FLAG_DMEIF[COM] | COM_TX_DMA_FLAG_TEIF[COM] | COM_TX_DMA_FLAG_HTIF[COM] | COM_TX_DMA_FLAG_TCIF[COM]); DMA_Cmd(COM_TX_DMA_STREAM[COM], DISABLE); DMA_DeInit(COM_TX_DMA_STREAM[COM]); /*USART RX Clean up*/ DMA_ClearFlag(COM_RX_DMA_STREAM[COM], COM_RX_DMA_FLAG_FEIF[COM] | COM_RX_DMA_FLAG_DMEIF[COM] | COM_RX_DMA_FLAG_TEIF[COM] | COM_RX_DMA_FLAG_HTIF[COM] | COM_RX_DMA_FLAG_TCIF[COM]); DMA_Cmd(COM_RX_DMA_STREAM[COM], DISABLE); DMA_DeInit(COM_RX_DMA_STREAM[COM]); /*USART TX Config*/ DMA_InitStructure.DMA_Channel = COM_TX_DMA_CHANNEL[COM]; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) COM_DR_ADDRESS[COM]; DMA_InitStructure.DMA_Memory0BaseAddr = COM_TX_BUFFER[COM]; /* This parameter will be configured durig communication */ DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */ DMA_InitStructure.DMA_BufferSize = 0xFF; /* This parameter will be configured durig communication */ 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(COM_TX_DMA_STREAM[COM], &DMA_InitStructure); //DMA_ITConfig(COM_TX_DMA_STREAM[COM], DMA_IT_TC, ENABLE); /*USART RX Config*/ DMA_InitStructure.DMA_Channel = COM_RX_DMA_CHANNEL[COM]; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) COM_DR_ADDRESS[COM]; DMA_InitStructure.DMA_Memory0BaseAddr = COM_RX_BUFFER[COM]; /* This parameter will be configured durig communication */ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; /* This parameter will be configured durig communication */ DMA_Init(COM_RX_DMA_STREAM[COM], &DMA_InitStructure); //DMA_ITConfig(COM_RX_DMA_STREAM[COM], DMA_IT_TC, ENABLE); /* Enable USART */ USART_Cmd(COM_USART[COM], ENABLE); }
void Timer3_init(void) { uint16_t PrescalerValue; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* GPIOB Configuration: PWM_TIMER Channel 1 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Compute the prescaler value */ RCC_ClocksTypeDef clocks; RCC_GetClocksFreq(&clocks); PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 20000000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD; // 800kHz TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 1; TIM_TimeBaseInit(PWM_TIMER, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0;//TIM_COMPARE_LOGIC_1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(PWM_TIMER, &TIM_OCInitStructure); /*** * Must enable reload for PWM (STMicroelectronicd RM0090 section 18.3.9 * "PWM mode": * * You must enable the corresponding preload register by setting the * OCxPE bit in the TIMx_CCMRx register. * * This is part of the fix for the pulse corruption (the runt pulse at * the start and the extra pulse at the end). */ TIM_OC1PreloadConfig(PWM_TIMER, TIM_OCPreload_Enable); /* configure DMA */ /* DMA clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); /* DMA1 Channel6 Config */ DMA_DeInit(DMA_STREAM); DMA_InitStructure.DMA_BufferSize = 24*24;//42; DMA_InitStructure.DMA_Channel = DMA_CHANNEL; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // data shifted from memory to peripheral DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&LED_BYTE_Buffer; // this is the buffer memory DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // automatically increase buffer index DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // stop DMA feed after buffer size is reached DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&PWM_TIMER->CCR1; // physical address of Timer 3 CCR1 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(DMA_STREAM, &DMA_InitStructure); /* PWM_TIMER CC1 DMA Request enable */ TIM_DMACmd(PWM_TIMER, DMA_SOURCE, ENABLE); DMA_Cmd(DMA_STREAM, ENABLE); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* USART configuration -----------------------------------------------------*/ USART_Config(); /* SysTick configuration ---------------------------------------------------*/ SysTickConfig(); /* LEDs configuration ------------------------------------------------------*/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* IO Expanderconfiguration ------------------------------------------------*/ #ifdef USART_TRANSMITTER_MODE TimeOut = USER_TIMEOUT; while ((IOE_Config() != IOE_OK) && (TimeOut != 0)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } #endif /* USART_TRANSMITTER_MODE */ while (1) { /******************************************************************************/ /* USART in Transmitter Mode */ /******************************************************************************/ #ifdef USART_TRANSMITTER_MODE /* Clear Buffers */ Fill_Buffer(CmdBuffer, 2); DMA_DeInit(USARTx_TX_DMA_STREAM); DMA_InitStructure.DMA_Channel = USARTx_TX_DMA_CHANNEL; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /****************** USART will Transmit Specific Command ******************/ /* Prepare the DMA to transfer the transaction command (2bytes) from the memory to the USART */ DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)CmdBuffer; DMA_InitStructure.DMA_BufferSize = (uint16_t)2; DMA_Init(USARTx_TX_DMA_STREAM, &DMA_InitStructure); /* Prepare Command to be transmitted */ /* Waiting joystick pressed */ PressedButton = JOY_NONE; while (PressedButton == JOY_NONE) { PressedButton = IOE_JoyStickGetState(); } /* Waiting joystick released */ ReleasedButton = IOE_JoyStickGetState(); while ((PressedButton == ReleasedButton) && (ReleasedButton != JOY_NONE)) { ReleasedButton = IOE_JoyStickGetState(); } if(PressedButton != JOY_NONE) { /* For each joystick state correspond a command to be sent through USART */ switch (PressedButton) { /* JOY_RIGHT button pressed */ case JOY_RIGHT: CmdBuffer[0] = CMD_RIGHT; CmdBuffer[1] = CMD_RIGHT_SIZE; break; /* JOY_LEFT button pressed */ case JOY_LEFT: CmdBuffer[0] = CMD_LEFT; CmdBuffer[1] = CMD_LEFT_SIZE; break; /* JOY_UP button pressed */ case JOY_UP: CmdBuffer[0] = CMD_UP; CmdBuffer[1] = CMD_UP_SIZE; break; /* JOY_DOWN button pressed */ case JOY_DOWN: CmdBuffer[0] = CMD_DOWN; CmdBuffer[1] = CMD_DOWN_SIZE; break; /* JOY_SEL button pressed */ case JOY_SEL: CmdBuffer[0] = CMD_SEL; CmdBuffer[1] = CMD_SEL_SIZE; break; default: break; } /* Enable the USART DMA requests */ USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); /* Clear the TC bit in the SR register by writing 0 to it */ USART_ClearFlag(USARTx, USART_FLAG_TC); /* Enable the DMA TX Stream, USART will start sending the command code (2bytes) */ DMA_Cmd(USARTx_TX_DMA_STREAM, ENABLE); /* Wait the USART DMA Tx transfer complete or time out */ TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(USARTx_TX_DMA_STREAM, USARTx_TX_DMA_FLAG_TCIF) == RESET)&&(TimeOut != 0)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } /* The software must wait until TC=1. The TC flag remains cleared during all data transfers and it is set by hardware at the last frame’s end of transmission*/ TimeOut = USER_TIMEOUT; while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear DMA Streams flags */ DMA_ClearFlag(USARTx_TX_DMA_STREAM, USARTx_TX_DMA_FLAG_HTIF | USARTx_TX_DMA_FLAG_TCIF); /* Disable the DMA Streams */ DMA_Cmd(USARTx_TX_DMA_STREAM, DISABLE); /* Disable the USART Tx DMA request */ USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); /******************* USART will Transmit Data Buffer ********************/ /* Prepare the DMA to transfer the transaction data (length defined by CmdBuffer[1] variable) from the memory to the USART */ DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)TxBuffer; DMA_InitStructure.DMA_BufferSize = (uint16_t)CmdBuffer[1]; DMA_Init(USARTx_TX_DMA_STREAM, &DMA_InitStructure); /* Enable the USART Tx DMA request */ USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); /* Clear the TC bit in the SR register by writing 0 to it */ USART_ClearFlag(USARTx, USART_FLAG_TC); /* Enable DMA USART Tx Stream */ DMA_Cmd(USARTx_TX_DMA_STREAM, ENABLE); /* Wait the USART DMA Tx transfer complete or time out */ TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(USARTx_TX_DMA_STREAM, USARTx_TX_DMA_FLAG_TCIF) == RESET)&&(TimeOut != 0)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } /* The software must wait until TC=1, The TC flag remains cleared during all data transfers and it is set by hardware at the last frame’s end of transmission */ TimeOut = USER_TIMEOUT; while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear all DMA Streams flags */ DMA_ClearFlag(USARTx_TX_DMA_STREAM, USARTx_TX_DMA_FLAG_HTIF | USARTx_TX_DMA_FLAG_TCIF); /* Disable the DMA Stream */ DMA_Cmd(USARTx_TX_DMA_STREAM, DISABLE); /* Disable the USART Tx DMA request */ USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); } #endif /* USART_TRANSMITTER_MODE */ /******************************************************************************/ /* USART in Receiver Mode */ /******************************************************************************/ #ifdef USART_RECEIVER_MODE /* Clear Buffers */ Fill_Buffer(RxBuffer, TXBUFFERSIZE); Fill_Buffer(CmdBuffer, 2); DMA_DeInit(USARTx_RX_DMA_STREAM); DMA_InitStructure.DMA_Channel = USARTx_RX_DMA_CHANNEL; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; /****************** USART will Receive Specific Command *******************/ /* Configure the DMA to receive 2 bytes (transaction command), in case of USART receiver */ DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)CmdBuffer; DMA_InitStructure.DMA_BufferSize = (uint16_t)2; DMA_Init(USARTx_RX_DMA_STREAM, &DMA_InitStructure); /* Enable the USART Rx DMA request */ USART_DMACmd(USARTx, USART_DMAReq_Rx, ENABLE); /* Enable the DMA RX Stream */ DMA_Cmd(USARTx_RX_DMA_STREAM, ENABLE); /* Wait the USART DMA Rx transfer complete (to receive the transaction command) */ while (DMA_GetFlagStatus(USARTx_RX_DMA_STREAM, USARTx_RX_DMA_FLAG_TCIF) == RESET) { } /* Clear all DMA Streams flags */ DMA_ClearFlag(USARTx_RX_DMA_STREAM, USARTx_RX_DMA_FLAG_HTIF | USARTx_RX_DMA_FLAG_TCIF); /* Disable the DMA Rx Stream */ DMA_Cmd(USARTx_RX_DMA_STREAM, DISABLE); /* Disable the USART Rx DMA requests */ USART_DMACmd(USARTx, USART_DMAReq_Rx, DISABLE); /************* USART will receive the the transaction data ****************/ /* Transaction data (length defined by CmdBuffer[1] variable) */ DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)RxBuffer; DMA_InitStructure.DMA_BufferSize = (uint16_t)CmdBuffer[1]; DMA_Init(USARTx_RX_DMA_STREAM, &DMA_InitStructure); /* Enable the USART Rx DMA requests */ USART_DMACmd(USARTx, USART_DMAReq_Rx , ENABLE); /* Enable the DMA Stream */ DMA_Cmd(USARTx_RX_DMA_STREAM, ENABLE); /* Wait the USART DMA Rx transfer complete or time out */ TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(USARTx_RX_DMA_STREAM, USARTx_RX_DMA_FLAG_TCIF) == RESET)&&(TimeOut != 0)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear all DMA Streams flags */ DMA_ClearFlag(USARTx_RX_DMA_STREAM, USARTx_RX_DMA_FLAG_HTIF | USARTx_RX_DMA_FLAG_TCIF); /* Disable the DMA Stream */ DMA_Cmd(USARTx_RX_DMA_STREAM, DISABLE); /* Disable the USART Rx DMA requests */ USART_DMACmd(USARTx, USART_DMAReq_Rx, DISABLE); switch (CmdBuffer[1]) { /* CMD_RIGHT command received */ case CMD_RIGHT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) != FAILED) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received */ case CMD_LEFT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) != FAILED) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received */ case CMD_UP_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) != FAILED) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received */ case CMD_DOWN_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) != FAILED) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received */ case CMD_SEL_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) != FAILED) { /* Turn ON all LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } #endif /* USART_RECEIVER_MODE */ } }
void main(void) #endif { uint32_t i; RST_CLK_DeInit(); RST_CLK_CPU_PLLconfig (RST_CLK_CPU_PLLsrcHSIdiv2,0); /* Enable peripheral clocks --------------------------------------------------*/ RST_CLK_PCLKcmd((RST_CLK_PCLK_RST_CLK | RST_CLK_PCLK_TIMER1 | RST_CLK_PCLK_DMA),ENABLE); RST_CLK_PCLKcmd((RST_CLK_PCLK_PORTA), ENABLE); /* Init NVIC */ SCB->AIRCR = 0x05FA0000 | ((uint32_t)0x500); SCB->VTOR = 0x08000000; /* Disable all interrupt */ NVIC->ICPR[0] = 0xFFFFFFFF; NVIC->ICER[0] = 0xFFFFFFFF; /* Disable all DMA request */ MDR_DMA->CHNL_REQ_MASK_CLR = 0xFFFFFFFF; MDR_DMA->CHNL_USEBURST_CLR = 0xFFFFFFFF; /* Reset PORTB settings */ PORT_DeInit(MDR_PORTB); /* Reset PORTF settings */ PORT_DeInit(MDR_PORTF); /* Configure TIMER1 pins: CH1, CH2 */ /* Configure PORTA pins 1, 3 */ PORT_InitStructure.PORT_Pin = PORT_Pin_1; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_ALTER; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST; PORT_Init(MDR_PORTA, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = PORT_Pin_3; PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_Init(MDR_PORTA, &PORT_InitStructure); /* Init RAM */ Init_RAM (DstBuf, BufferSize); /* Reset all TIMER1 settings */ TIMER_DeInit(MDR_TIMER1); TIMER_BRGInit(MDR_TIMER1,TIMER_HCLKdiv1); /* TIM1 configuration ------------------------------------------------*/ /* Initializes the TIMERx Counter ------------------------------------*/ sTIM_CntInit.TIMER_Prescaler = 0x10; sTIM_CntInit.TIMER_Period = 0x200; sTIM_CntInit.TIMER_CounterMode = TIMER_CntMode_ClkFixedDir; sTIM_CntInit.TIMER_CounterDirection = TIMER_CntDir_Up; sTIM_CntInit.TIMER_EventSource = TIMER_EvSrc_None; sTIM_CntInit.TIMER_FilterSampling = TIMER_FDTS_TIMER_CLK_div_1; sTIM_CntInit.TIMER_ARR_UpdateMode = TIMER_ARR_Update_Immediately; sTIM_CntInit.TIMER_ETR_FilterConf = TIMER_Filter_1FF_at_TIMER_CLK; sTIM_CntInit.TIMER_ETR_Prescaler = TIMER_ETR_Prescaler_None; sTIM_CntInit.TIMER_ETR_Polarity = TIMER_ETRPolarity_NonInverted; sTIM_CntInit.TIMER_BRK_Polarity = TIMER_BRKPolarity_NonInverted; TIMER_CntInit (MDR_TIMER1,&sTIM_CntInit); /* Initializes the TIMER1 Channel1 -------------------------------------*/ TIMER_ChnStructInit(&sTIM_ChnInit); sTIM_ChnInit.TIMER_CH_Number = TIMER_CHANNEL1; sTIM_ChnInit.TIMER_CH_Mode = TIMER_CH_MODE_PWM; sTIM_ChnInit.TIMER_CH_REF_Format = TIMER_CH_REF_Format3; TIMER_ChnInit(MDR_TIMER1, &sTIM_ChnInit); TIMER_SetChnCompare(MDR_TIMER1, TIMER_CHANNEL1, 0x100); /* Initializes the TIMER1 Channel1 Output -------------------------------*/ TIMER_ChnOutStructInit(&sTIM_ChnOutInit); sTIM_ChnOutInit.TIMER_CH_Number = TIMER_CHANNEL1; sTIM_ChnOutInit.TIMER_CH_DirOut_Polarity = TIMER_CHOPolarity_NonInverted; sTIM_ChnOutInit.TIMER_CH_DirOut_Source = TIMER_CH_OutSrc_REF; sTIM_ChnOutInit.TIMER_CH_DirOut_Mode = TIMER_CH_OutMode_Output; TIMER_ChnOutInit(MDR_TIMER1, &sTIM_ChnOutInit); /* Initializes the TIMER1 Channel2 -------------------------------------*/ TIMER_ChnStructInit(&sTIM_ChnInit); sTIM_ChnInit.TIMER_CH_Number = TIMER_CHANNEL2; sTIM_ChnInit.TIMER_CH_Mode = TIMER_CH_MODE_CAPTURE; TIMER_ChnInit(MDR_TIMER1, &sTIM_ChnInit); /* Initializes the TIMER1 Channel2 Output -------------------------------*/ TIMER_ChnOutStructInit(&sTIM_ChnOutInit); sTIM_ChnOutInit.TIMER_CH_Number = TIMER_CHANNEL2; sTIM_ChnOutInit.TIMER_CH_DirOut_Polarity = TIMER_CHOPolarity_NonInverted; sTIM_ChnOutInit.TIMER_CH_DirOut_Source = TIMER_CH_OutSrc_Only_0; sTIM_ChnOutInit.TIMER_CH_DirOut_Mode = TIMER_CH_OutMode_Input; TIMER_ChnOutInit(MDR_TIMER1, &sTIM_ChnOutInit); /* Enable TIMER1 DMA request */ TIMER_DMACmd(MDR_TIMER1,(TIMER_STATUS_CCR_CAP_CH2), ENABLE); /* Reset all DMA settings */ DMA_DeInit(); DMA_StructInit(&DMA_InitStr); /* DMA_Channel_TIM1 configuration ---------------------------------*/ /* Set Primary Control Data */ DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_TIMER1->CCR2)); DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)DstBuf; DMA_PriCtrlStr.DMA_SourceIncSize = DMA_SourceIncNo; DMA_PriCtrlStr.DMA_DestIncSize = DMA_DestIncHalfword; DMA_PriCtrlStr.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_PriCtrlStr.DMA_Mode = DMA_Mode_Basic; DMA_PriCtrlStr.DMA_CycleSize = BufferSize; DMA_PriCtrlStr.DMA_NumContinuous = DMA_Transfers_1; DMA_PriCtrlStr.DMA_SourceProtCtrl = DMA_SourcePrivileged; DMA_PriCtrlStr.DMA_DestProtCtrl = DMA_DestPrivileged; /* Set Channel Structure */ DMA_InitStr.DMA_PriCtrlData = &DMA_PriCtrlStr; DMA_InitStr.DMA_Priority = DMA_Priority_High; DMA_InitStr.DMA_UseBurst = DMA_BurstClear; DMA_InitStr.DMA_SelectDataStructure = DMA_CTRL_DATA_PRIMARY; /* Init DMA channel */ DMA_Init(DMA_Channel_TIM1, &DMA_InitStr); /* Enable TIMER1 */ TIMER_Cmd(MDR_TIMER1,ENABLE); /* Transfer complete */ while((DMA_GetFlagStatus(DMA_Channel_TIM1, DMA_FLAG_CHNL_ENA))) { } /* Check the corectness of written dada */ for(i = 0; i < BufferSize; i++) { if (DstBuf[i] != MDR_TIMER1->CCR1) { TransferStatus &= FAILED; break; } else { TransferStatus = PASSED; } } /* TransferStatus = PASSED, if the data transmitted are correct */ /* TransferStatus = FAILED, if the data transmitted are not correct */ while(1) { } }
/** * @brief ADC1 channel with DMA configuration * @param None * @retval None */ void ADC1_DMA_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* ADC1 DeInit */ ADC_DeInit(ADC1); /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); /* Configure ADC Channel11 as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 4; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 1 with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_55_5Cycles); /* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles); ADC_TempSensorCmd(ENABLE); /* Convert the ADC1 Vref with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles); ADC_VrefintCmd(ENABLE); /* Convert the ADC1 Vbat with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_Vbat , ADC_SampleTime_55_5Cycles); ADC_VbatCmd(ENABLE); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
/** * @brief ADC1 Channel Vbat configuration * @note This function Configure the ADC peripheral 1) Enable peripheral clocks 2) DMA2_Stream0 channel 0 configuration 3) Configure ADC1 Channel18 (VBAT) * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; /* Enable peripheral clocks *************************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); RCC_APB2PeriphClockCmd(ADCx_CLK, ENABLE); /* DMA2_Stream0 channel0 configuration **************************************/ DMA_DeInit(DMA2_Stream0); DMA_InitStructure.DMA_Channel = DMA_CHANNELx; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA_STREAMx, &DMA_InitStructure); /* DMA2_Stream0 enable */ DMA_Cmd(DMA_STREAMx, ENABLE); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADCx, &ADC_InitStructure); /* Enable ADC1 DMA */ ADC_DMACmd(ADCx, ENABLE); /* ADC1 regular channel18 (VBAT) configuration ******************************/ ADC_RegularChannelConfig(ADCx, ADC_Channel_Vbat, 1, ADC_SampleTime_15Cycles); /* Enable VBAT channel */ ADC_VBATCmd(ENABLE); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE); /* Enable ADC1 **************************************************************/ ADC_Cmd(ADCx, ENABLE); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Preconfiguration before using DAC----------------------------------------*/ DAC_Config(); /* TIM2 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 TRGO selection */ TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); /* Configures Button GPIO and EXTI Line */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); /* Infinite loop */ while (1) { /* If the wave form is changed */ if (WaveChange == 1) { /* Switch the selected waves forms according the Button status */ if (SelectedWavesForm == 1) { /* The sine wave has been selected */ /* Sine Wave generator ---------------------------------------------*/ DAC_DeInit(); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; /* DMA channel3 Configuration */ DMA_DeInit(DMA1_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 32; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel3, &DMA_InitStructure); /* Enable DMA1 Channel3 */ DMA_Cmd(DMA1_Channel3, ENABLE); /* DAC Channel1 Init */ DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DMA for DAC Channel1 */ DAC_DMACmd(DAC_Channel_1, ENABLE); } /* The Escalator wave has been selected */ else { /* Escalator Wave generator -----------------------------------------*/ DAC_DeInit(); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; /* DMA1 channel2 configuration */ DMA_DeInit(DMA1_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit; DMA_InitStructure.DMA_BufferSize = 6; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_Init(DMA1_Channel3, &DMA_InitStructure); /* Enable DMA1 Channel2 */ DMA_Cmd(DMA1_Channel3, ENABLE); /* DAC channel1 Configuration */ DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DMA for DAC Channel1 */ DAC_DMACmd(DAC_Channel_1, ENABLE); } WaveChange = !WaveChange; } } }
void I2C::EventIRQ() { static uint32_t I2C_Status=0; static u8 timeOut=0; I2C_Status = I2C_GetLastEvent(mI2C); switch(I2C_Status)//查询中断事件类型 { case I2C_EVENT_MASTER_MODE_SELECT: //EV5 //SB、BUSY、MSL位置位 if(mState==STATE_SEND_ADW)//发送从机地址+写信号 { #ifdef I2C_USE_DMA if(mUseDma) { mI2C->CR2 &= ~I2C_IT_BUF; //使用DMA,关闭BUF中断 mI2C->CR2 |= I2C_CR2_DMAEN; //使能IIC DMA传输 } #endif I2C_Send7bitAddress(mI2C,mCurrentCmd.slaveAddr,I2C_Direction_Transmitter); mState = STATE_SEND_DATA;//状态设置为需要发送数据(发送寄存器地址) #ifdef I2C_USE_DMA if(mUseDma) { /* I2Cx Common Channel Configuration */ if(mCurrentCmd.cmdType>=I2C_WRITE_BYTE)//写模式 { mDMA_InitStructure.DMA_BufferSize = mCurrentCmd.outDataLen; } else //读模式 设置长度为1 因为发送一个字节后需要再次发送起始信号 { mDMA_InitStructure.DMA_BufferSize = 1; } mDMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&mCurrentCmd.DataOut[0]; mDMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST; DMA_Init(mDmaTxChannel,&mDMA_InitStructure); DMA_Cmd(mDmaTxChannel, ENABLE); } #endif } else if(mState==STATE_SEND_ADR)//发送从机地址+读信号 { I2C_Send7bitAddress(mI2C,mCurrentCmd.slaveAddr,I2C_Direction_Receiver); mState = STATE_REVEIVE_DATA;//状态设置为需要发送数据(发送寄存器地址) #ifdef I2C_USE_DMA if(mUseDma) { if( mCurrentCmd.cmdType==I2C_READ_BYTE)//接收一个字节,不能使用DMA,使用中断的方式 { mI2C->CR2 |= I2C_IT_BUF; //接收一个字节,打开 BUF中断,不使用DMA mI2C->CR2 &= ~I2C_CR2_DMAEN; //失能IIC DMA传输 } else //接收的字节数大于1,使用DMA { mI2C->CR2|=I2C_CR2_LAST;//使能接收最后一位时发送NACK mI2C->CR2 &= ~I2C_IT_BUF; //使用DMA,关闭BUF中断 mI2C->CR2 |= I2C_CR2_DMAEN; //使能IIC DMA传输 mDMA_InitStructure.DMA_BufferSize = mCurrentCmd.inDataLen; mDMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)mCurrentCmd.pDataIn; mDMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC; DMA_Init(mDmaRxChannel,&mDMA_InitStructure); DMA_Cmd(mDmaRxChannel, ENABLE); } } #endif } break; case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED://EV6 //已: I2C_Send7bitAddress(W) 应: I2C_SendData() #ifndef I2C_USE_DMA // I2C->CR1 |= I2C_CR1_PE; I2C_SendData(mI2C,mCurrentCmd.DataOut[0]);//发送寄存器地址或者命令 ++mIndex_Send;//发送计数下标后移 --mCurrentCmd.outDataLen;//需要发送的数据长度减1 #else if(mUseDma==false) { // I2C->CR1 |= I2C_CR1_PE; I2C_SendData(mI2C,mCurrentCmd.DataOut[0]);//发送寄存器地址或者命令 ++mIndex_Send;//发送计数下标后移 --mCurrentCmd.outDataLen;//需要发送的数据长度减1 } #endif break; case I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED://EV6 //已: I2C_Send7bitAddress(R) if(mCurrentCmd.cmdType==I2C_READ_BYTE)//只接收一个字节 { I2C_AcknowledgeConfig(mI2C,DISABLE);//关应答 I2C_GenerateSTOP(mI2C,ENABLE);//产生停止信号 } break; case I2C_EVENT_MASTER_BYTE_RECEIVED://EV7 //数据到达,接收数据 if(mState==STATE_REVEIVE_DATA)//防止标志没来得及清掉,重复进入 { #ifdef I2C_USE_DMA if(mUseDma) { if(mCurrentCmd.cmdType==I2C_READ_BYTE)//只接收一个字节 { (*mCurrentCmd.pDataIn) = I2C_ReceiveData(mI2C); //保存接收到的数据 mI2C->CR2 &= ~I2C_IT_BUF; //使用DMA,关闭BUF中断 if(mCurrentCmd.isRecordInterval)//需要记录两次执行相同命令的间隔 mCurrentCmd.pDevice->Updated(); //检查队列是否为空 if(mCmdQueue.Size()>0)//队列不为空 { mCmdQueue.Get(mCurrentCmd);//下一个命令出队 //都要先发送从机地址+写信号 mState = STATE_SEND_ADW;//标志设置为需要发送从机地址+写信号 mIndex_Send = 0;//发送、接收计数下标清零 I2C_AcknowledgeConfig(mI2C,ENABLE); //使能应答 I2C_GenerateSTART(mI2C,ENABLE); //产生启动信号 } else//队列为空, { mState=STATE_READY;//将状态设置为 准备好发送 模式 } } } else { (*mCurrentCmd.pDataIn) = I2C_ReceiveData(mI2C); ++mCurrentCmd.pDataIn;//下标后移 --mCurrentCmd.inDataLen;//需要接收数据计数减一 if(mCurrentCmd.inDataLen==1)//还剩最后一个字节需要接收 { I2C_AcknowledgeConfig(mI2C,DISABLE);//关应答 I2C_GenerateSTOP(mI2C,ENABLE); //产生停止信号 } else if(mCurrentCmd.inDataLen==0)//接收完毕 { if(mCurrentCmd.isRecordInterval)//需要记录两次执行相同命令的间隔 mCurrentCmd.pDevice->Updated(); //检查队列是否为空 if(mCmdQueue.Size()>0)//队列不为空 { mCmdQueue.Get(mCurrentCmd);//下一个命令出队 mState = STATE_SEND_ADW;//标志设置为需要发送从机地址+写信号 mIndex_Send = 0;//发送、接收计数下标清零 I2C_AcknowledgeConfig(mI2C,ENABLE); //使能应答 I2C_GenerateSTART(mI2C,ENABLE); //产生启动信号 } else//队列为空, { mState=STATE_READY;//将状态设置为 准备好发送 模式 } } } #else (*mCurrentCmd.pDataIn) = I2C_ReceiveData(mI2C); ++mCurrentCmd.pDataIn;//下标后移 --mCurrentCmd.inDataLen;//需要接收数据计数减一 if(mCurrentCmd.inDataLen==1)//还剩最后一个字节需要接收 { I2C_AcknowledgeConfig(mI2C,DISABLE);//关应答 I2C_GenerateSTOP(mI2C,ENABLE); //产生停止信号 } else if(mCurrentCmd.inDataLen==0)//接收完毕 { if(mCurrentCmd.recordIntervalNumber)//需要记录两次执行相同命令的间隔 mCurrentCmd.pDevice->Updated(); //检查队列是否为空 if(mCmdQueue.Size()>0)//队列不为空 { mCmdQueue.Get(mCurrentCmd);//下一个命令出队 mState = STATE_SEND_ADW;//标志设置为需要发送从机地址+写信号 mIndex_Send = 0;//发送、接收计数下标清零 I2C_AcknowledgeConfig(mI2C,ENABLE); //使能应答 I2C_GenerateSTART(mI2C,ENABLE); //产生启动信号 } else//队列为空, { mState=STATE_READY;//将状态设置为 准备好发送 模式 } } #endif } break; case I2C_EVENT_MASTER_BYTE_TRANSMITTED: //EV8_2 //已: I2C_SendData() if(STATE_SEND_DATA==mState)//防止标志没来得及清掉,重复进入 { #ifdef I2C_USE_DMA if(mUseDma) { if(0==DMA_GetCurrDataCounter(mDmaTxChannel) && mState == STATE_SEND_DATA)//检查是否发送完毕 { DMA_Cmd(mDmaTxChannel, DISABLE);//关DMA if(mCurrentCmd.cmdType >= I2C_WRITE_BYTE)//处于写模式 单字节或者多字节 { I2C_GenerateSTOP(mI2C,ENABLE);//产生停止信号 if(mCurrentCmd.isRecordInterval)//需要记录两次执行相同命令的间隔 mCurrentCmd.pDevice->Updated(); //判断队列是否为空 if(mCmdQueue.Size()>0)//队列不为空 { mCmdQueue.Get(mCurrentCmd);//下一个命令出队 //都要先发送从机地址+写信号 mState = STATE_SEND_ADW;//标志设置为需要发送从机地址+写信号 mIndex_Send = 0;//发送、接收计数下标清零 I2C_AcknowledgeConfig(mI2C,ENABLE); //使能应答 I2C_GetLastEvent(mI2C);//!!这里再次获取一下状态的原因其实是起延时的作用,等待busy位复位 //因为只有在busy位为0时才可以设置start位才有用,否则可能导致开始信号无法发送的情况 /* Delay::Us(1); */ /* usart1<<"ok\n"; */ I2C_GenerateSTART(mI2C,ENABLE); //产生启动信号 } else//队列为空, { mState=STATE_READY;//将状态设置为 准备好发送 模式 } } else//处于读模式 { mState = STATE_SEND_ADR;//状态设置为 发送从机地址+读 I2C_GenerateSTART(mI2C,ENABLE);//起始信号 } } } else { if(mCurrentCmd.outDataLen>0)//还需要发送数据 { I2C_SendData(mI2C,mCurrentCmd.DataOut[mIndex_Send++]);//发送寄存器地址或者命令 --mCurrentCmd.outDataLen;//需要发送的数据长度减1 } else//数据已经发送完成 { if(mCurrentCmd.cmdType >= I2C_WRITE_BYTE)//处于写模式 单字节或者多字节 { //已经写完,发送停止信号 I2C_GenerateSTOP(mI2C,ENABLE);//产生停止信号 if(mCurrentCmd.isRecordInterval)//需要记录两次执行相同命令的间隔 mCurrentCmd.pDevice->Updated(); //判断队列是否为空 if(mCmdQueue.Size()>0)//队列不为空 { mCmdQueue.Get(mCurrentCmd);//下一个命令出队 mState = STATE_SEND_ADW;//标志设置为需要发送从机地址+写信号 mIndex_Send = 0;//发送、接收计数下标清零 I2C_AcknowledgeConfig(mI2C,ENABLE); //使能应答 I2C_GetLastEvent(mI2C);//!!这里再次获取一下状态的原因其实是起延时的作用,等待busy位复位 //因为只有在busy位为0时才可以设置start位才有用,否则可能导致开始信号无法发送的情况 I2C_GenerateSTART(mI2C,ENABLE); //产生启动信号 } else//队列为空, { mState=STATE_READY;//将状态设置为 准备好发送 模式 } } else//处于读模式 { mState = STATE_SEND_ADR;//状态设置为 发送从机地址+读 I2C_GenerateSTART(mI2C,ENABLE);//起始信号 } } } #else if(mCurrentCmd.outDataLen>0)//还需要发送数据 { I2C_SendData(mI2C,mCurrentCmd.DataOut[mIndex_Send++]);//发送寄存器地址或者命令 --mCurrentCmd.outDataLen;//需要发送的数据长度减1 } else//数据已经发送完成 { if(mCurrentCmd.cmdType >= I2C_WRITE_BYTE)//处于写模式 单字节或者多字节 { //已经写完,发送停止信号 I2C_GenerateSTOP(mI2C,ENABLE);//产生停止信号 if(mCurrentCmd.isRecordInterval)//需要记录两次执行相同命令的间隔 mCurrentCmd.pDevice->Updated(); //判断队列是否为空 if(mCmdQueue.Size()>0)//队列不为空 { mCmdQueue.Get(mCurrentCmd);//下一个命令出队 mState = STATE_SEND_ADW;//标志设置为需要发送从机地址+写信号 mIndex_Send = 0;//发送、接收计数下标清零 I2C_AcknowledgeConfig(mI2C,ENABLE); //使能应答 I2C_GetLastEvent(mI2C);//!!这里再次获取一下状态的原因其实是起延时的作用,等待busy位复位 //因为只有在busy位为0时才可以设置start位才有用,否则可能导致开始信号无法发送的情况 I2C_GenerateSTART(mI2C,ENABLE); //产生启动信号 } else//队列为空, { mState=STATE_READY;//将状态设置为 准备好发送 模式 } } else//处于读模式 { mState = STATE_SEND_ADR;//状态设置为 发送从机地址+读 I2C_GenerateSTART(mI2C,ENABLE);//起始信号 } } #endif } else { ++timeOut; if(timeOut>3) { Soft_Reset(); mState = STATE_ERROR;//状态设置为错误状态 } } break; // case I2C_EVENT_MASTER_BYTE_TRANSMITTING://EV8 /* TRA, BUSY, MSL, TXE flags */ // break; case 0x00030044://接收数据时出现数据溢出错误,DR为读出, I2C_ReceiveData(mI2C); //读出数据,清除BTF标志 break; case 0x00000010: //STOPF :从机收到停止信号,但本程序中并没有考虑从机模式,在这里判定为出错 //(如果总线上没有另外的主机,可能是由于:当主机发送完数据后(主动产生停止信号后)自动进入从机模式 //而此时比如sda和scl线接触不良,将导致产生错误的停止信号,从而出现此中断,故判定为断线,将iic设备关闭) Soft_Reset(); mState = STATE_ERROR;//状态设置为错误状态 break; case 0x00000040: I2C_ReceiveData(mI2C); //读出数据,清除RxNE标志 break; } if(I2C_Status!=I2C_EVENT_MASTER_BYTE_TRANSMITTED)// timeOut=0; if((I2C_Status&0x00010000)==0)//从机模式,主机主动产生停止信号之后便会进入从机模式 { // Soft_Reset(); // mState = STATE_ERROR;//状态设置为错误状态 } }