stm32 i2c dma之欧阳历创编Word格式文档下载.docx
《stm32 i2c dma之欧阳历创编Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《stm32 i2c dma之欧阳历创编Word格式文档下载.docx(26页珍藏版)》请在冰豆网上搜索。
#definesEE_MAX_TRIALS_NUMBER150
/*DefintionsforthestateoftheDMAtransfer*/
#definesEE_STATE_READY0
#definesEE_STATE_BUSY1
#definesEE_STATE_ERROR2
#definesEE_OK0
#definesEE_FAIL1
#definesEE_FLAG_TIMEOUT((uint32_t)0x1000)
#definesEE_LONG_TIMEOUT((uint32_t)(10*sEE_FLAG_TIMEOUT))
#definesEE_I2CI2C1
#definesEE_I2C_CLKRCC_APB1Periph_I2C1
#definesEE_I2C_SCL_PINGPIO_Pin_6
#definesEE_I2C_SCL_GPIO_PORTGPIOB
#definesEE_I2C_SCL_GPIO_CLKRCC_APB2Periph_GPIOB
#definesEE_I2C_SDA_PINGPIO_Pin_7
#definesEE_I2C_SDA_GPIO_PORTGPIOB
#definesEE_I2C_SDA_GPIO_CLKRCC_APB2Periph_GPIOB
#definesEE_M24C64_32
#definesEE_I2C_DMADMA1
#definesEE_I2C_DMA_CHANNEL_TXDMA1_Channel6
#definesEE_I2C_DMA_CHANNEL_RXDMA1_Channel7
#definesEE_I2C_DMA_FLAG_TX_TCDMA1_IT_TC6
#definesEE_I2C_DMA_FLAG_TX_GLDMA1_IT_GL6
#definesEE_I2C_DMA_FLAG_RX_TCDMA1_IT_TC7
#definesEE_I2C_DMA_FLAG_RX_GLDMA1_IT_GL7
#definesEE_I2C_DMA_CLKRCC_AHBPeriph_DMA1
#definesEE_I2C_DR_Address((uint32_t)0x40005410)
#definesEE_USE_DMA
#definesEE_I2C_DMA_TX_IRQnDMA1_Channel6_IRQn
#definesEE_I2C_DMA_RX_IRQnDMA1_Channel7_IRQn
#definesEE_I2C_DMA_TX_IRQHandlerDMA1_Channel6_IRQHandler
#definesEE_I2C_DMA_RX_IRQHandlerDMA1_Channel7_IRQHandler
#definesEE_I2C_DMA_PREPRIO0
#definesEE_I2C_DMA_SUBPRIO0
#definesEE_DIRECTION_TX0
#definesEE_DIRECTION_RX1
/*Timeconstantforthedelaycaclulationallowingtohaveamillisecond
incrementingcounter.Thisvalueshouldbeequalto(SystemClock/1000).
ie.ifsystemclock=24MHzthensEE_TIME_CONSTshouldbe24.*/
#definesEE_TIME_CONST48
voidsEE_DeInit(void);
voidsEE_Init(void);
uint32_tsEE_ReadBuffer(uint8_t*pBuffer,uint16_tReadAddr,uint16_t*NumByteToRead);
uint32_tsEE_WritePage(uint8_t*pBuffer,uint16_tWriteAddr,uint8_t*NumByteToWrite);
voidsEE_WriteBuffer(uint8_t*pBuffer,uint16_tWriteAddr,uint16_tNumByteToWrite);
uint32_tsEE_WaitEepromStandbyState(void);
/*USERCallbacks:
Thesearefunctionsforwhichprototypesonlyaredeclaredin
EEPROMdriverandthatshouldbeimplementedintouserapplicaiton.*/
/*sEE_TIMEOUT_UserCallback()functioniscalledwheneveratimeoutcondition
occureduringcommunication(waitingonaneventthatdoesn'
toccur,bus
errors,busydevices...).
Youcanusethedefaulttimeoutcallbackimplementationbyuncommentingthe
defineUSE_DEFAULT_TIMEOUT_CALLBACKinstm32_evel_i2c_ee.hfile.
TypicallytheuserimplementationofthiscallbackshouldresetI2Cperipheral
andreinitializecommunicationorinworstcaseresetalltheapplication.*/
uint32_tsEE_TIMEOUT_UserCallback(void);
/*StartandEndofcriticalsection:
thesecallbacksshouldbetypicallyused
todisableinterruptswhenenteringacriticalsectionofI2Ccommunication
Youmayusedefaultcallbacksprovidedintothisdriverbyuncommentingthe
defineUSE_DEFAULT_CRITICAL_CALLBACKinstm32_evel_i2c_ee.hfile..
Oryoucancommentthatlineandimplementthesecallbacksintoyour
application*/
voidsEE_EnterCriticalSection_UserCallback(void);
voidsEE_ExitCriticalSection_UserCallback(void);
}
#endif
以下是C文件
at24c256.h"
__IOuint16_tsEEAddress=0xa0;
__IOuint32_tsEETimeout=sEE_LONG_TIMEOUT;
__IOuint16_t*sEEDataReadPointer;
__IOuint8_t*sEEDataWritePointer;
__IOuint8_tsEEDataNum;
DMA_InitTypeDefsEEDMA_InitStructure;
voidsEE_LowLevel_DeInit(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
NVIC_InitTypeDefNVIC_InitStructure;
/*sEE_I2CPeripheralDisable*/
I2C_Cmd(sEE_I2C,DISABLE);
/*sEE_I2CDeInit*/
I2C_DeInit(sEE_I2C);
/*!
<
sEE_I2CPeriphclockdisable*/
RCC_APB1PeriphClockCmd(sEE_I2C_CLK,DISABLE);
GPIOconfiguration*/
ConfiguresEE_I2Cpins:
SCL*/
GPIO_InitStructure.GPIO_Pin=sEE_I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(sEE_I2C_SCL_GPIO_PORT,&
GPIO_InitStructure);
SDA*/
GPIO_InitStructure.GPIO_Pin=sEE_I2C_SDA_PIN;
GPIO_Init(sEE_I2C_SDA_GPIO_PORT,&
/*ConfigureandenableI2CDMATXChannelinterrupt*/
NVIC_InitStructure.NVIC_IRQChannel=sEE_I2C_DMA_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=sEE_I2C_DMA_PREPRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=sEE_I2C_DMA_SUBPRIO;
NVIC_InitStructure.NVIC_IRQChannelCmd=DISABLE;
NVIC_Init(&
NVIC_InitStructure);
/*ConfigureandenableI2CDMARXChannelinterrupt*/
NVIC_InitStructure.NVIC_IRQChannel=sEE_I2C_DMA_RX_IRQn;
/*DisableandDeinitializetheDMAchannels*/
DMA_Cmd(sEE_I2C_DMA_CHANNEL_TX,DISABLE);
DMA_Cmd(sEE_I2C_DMA_CHANNEL_RX,DISABLE);
DMA_DeInit(sEE_I2C_DMA_CHANNEL_TX);
DMA_DeInit(sEE_I2C_DMA_CHANNEL_RX);
/**
*@briefInitializesperipheralsusedbytheI2CEEPROMdriver.
*@paramNone
*@retvalNone
*/
voidsEE_LowLevel_Init(void)
sEE_I2C_SCL_GPIO_CLKandsEE_I2C_SDA_GPIO_CLKPeriphclockenable*/
RCC_APB2PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK|sEE_I2C_SDA_GPIO_CLK,ENABLE);
sEE_I2CPeriphclockenable*/
RCC_APB1PeriphClockCmd(sEE_I2C_CLK,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_OD;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
I2CDMATXandRXchannelsconfiguration*/
/*EnabletheDMAclock*/
RCC_AHBPeriphClockCmd(sEE_I2C_DMA_CLK,ENABLE);
/*I2CTXDMAChannelconfiguration*/
sEEDMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)sEE_I2C_DR_Address;
sEEDMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)0;
/*Thisparameterwillbeconfigureddurigcommunication*/
sEEDMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
sEEDMA_InitStructure.DMA_BufferSize=0xFFFF;
sEEDMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
sEEDMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
sEEDMA_InitStructure.DMA_PeripheralDataSize=DMA_MemoryDataSize_Byte;
sEEDMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
sEEDMA_InitStructure.DMA_Mode=DMA_Mode_Normal;
sEEDMA_InitStructure.DMA_Priority=DMA_Priority_VeryHigh;
sEEDMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
DMA_Init(sEE_I2C_DMA_CHANNEL_TX,&
sEEDMA_InitStructure);
/*I2CRXDMAChannelconfiguration*/
DMA_Init(sEE_I2C_DMA_CHANNEL_RX,&
/*EnabletheDMAChannelsInterrupts*/
DMA_ITConfig(sEE_I2C_DMA_CHANNEL_TX,DMA_IT_TC,ENABLE);
DMA_ITConfig(sEE_I2C_DMA_CHANNEL_RX,DMA_IT_TC,ENABLE);
*@briefInitializesDMAchannelusedbytheI2CEEPROMdriver.
voidsEE_LowLevel_DMAConfig(uint32_tpBuffer,uint32_tBufferSize,uint32_tDirection)
{
/*InitializetheDMAwiththenewparameters*/
if(Direction==sEE_DIRECTION_TX)
/*ConfiguretheDMATxChannelwiththebufferaddressandthebuffersize*/
sEEDMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)pBuffer;
sEEDMA_InitStructure.DMA_BufferSize=(uint32_t)BufferSize;
}
else
{
/*ConfiguretheDMARxChannelwiththebufferaddressandthebuffersize*/
sEEDMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
voidsEE_DeInit(void)
sEE_LowLevel_DeInit();
voidsEE_Init(void)
I2C_InitTypeDefI2C_InitStructure;
sEE_LowLevel_Init();
I2Cconfiguration*/
/*sEE_I2Cconfiguration*/
I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle=I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1=I2C_SLAVE_ADDRESS7;
I2C_InitStructure.I2C_Ack=I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed=I2C_SPEED;
/*sEE_I2CPeripheralEnable*/
I2C_Cmd(sEE_I2C,ENABLE);
/*Ap