stm32i2cdma讲解学习.docx
- 文档编号:25897475
- 上传时间:2023-06-16
- 格式:DOCX
- 页数:29
- 大小:22.29KB
stm32i2cdma讲解学习.docx
《stm32i2cdma讲解学习.docx》由会员分享,可在线阅读,更多相关《stm32i2cdma讲解学习.docx(29页珍藏版)》请在冰豆网上搜索。
stm32i2cdma讲解学习
stm32i2c
dma
#ifndef__AT24C256_H
#define__AT24C256_H
/*Includes*/
#include"stm32f10x.h"#ifdef__cplusplusextern"C"{#endif
#defineI2C_SPEED300000
#defineI2C_SLAVE_ADDRESS70xA0
#definesEE_PAGESIZE32
/*MaximumnumberoftrialsforsEE_WaitEepromStandbyState()function*/#definesEE_MAX_TRIALS_NUMBER150/*DefintionsforthestateoftheDMAtransfer*/
#definesEE_STATE_READY0
#definesEE_STATE_BUSY1
#definesEE_STATE_ERROR2
#definesEE_OK0
#definesEE_FAIL1
((uint32_t)0x1000)
((uint32_t)(10*sEE_FLAG_TIMEOUT))
#definesEE_FLAG_TIMEOUT#definesEE_LONG_TIMEOUT
#definesEE_I2CI2C1
#definesEE_I2C_CLK
RCC_APB1Periph_I2C1
#definesEE_I2C_SCL_PIN
#definesEE_I2C_SCL_GPIO_PORT
GPIO_Pin_6
GPIOB
#definesEE_I2C_SCL_GPIO_CLK
RCC_APB2Periph_GPIOB
#definesEE_I2C_SDA_PIN
#definesEE_I2C_SDA_GPIO_PORT
#definesEE_I2C_SDA_GPIO_CLK
#definesEE_M24C64_32
GPIO_Pin_7
GPIOB
RCC_APB2Periph_GPIOB
#definesEE_I2C_DMADMA1
#definesEE_I2C_DMA_CHANNEL_TX
#definesEE_I2C_DMA_CHANNEL_RX
#definesEE_I2C_DMA_FLAG_TX_TC
#definesEE_I2C_DMA_FLAG_TX_GL
#definesEE_I2C_DMA_FLAG_RX_TC
DMA1_Channel6
DMA1_Channel7
DMA1_IT_TC6
DMA1_IT_GL6
DMA1_IT_TC7
#definesEE_I2C_DMA_FLAG_RX_GL
#definesEE_I2C_DMA_CLK
#definesEE_I2C_DR_Address
#definesEE_USE_DMA
#definesEE_I2C_DMA_TX_IRQn
DMA1_IT_GL7
RCC_AHBPeriph_DMA1((uint32_t)0x40005410)
#definesEE_I2C_DMA_RX_IRQn
#definesEE_I2C_DMA_TX_IRQHandler
#definesEE_I2C_DMA_RX_IRQHandler
#definesEE_I2C_DMA_PREPRIO
#definesEE_I2C_DMA_SUBPRIO
#definesEE_DIRECTION_TX
#definesEE_DIRECTION_RX
DMA1_Channel6_IRQn
DMA1_Channel7_IRQn
DMA1_Channel6_IRQHandler
DMA1_Channel7_IRQHandler
0
0
0
/*Timeconstantforthedelaycaclulationallowingtohaveamillisecondincrementingcounter.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:
ThesearefunctionsforwhichprototypesonlyaredeclaredinEEPROMdriverandthatshouldbeimplementedintouserapplicaiton.*/
/*sEE_TIMEOUT_UserCallback()functioniscalledwheneveratimeoutconditionoccureduringcommunication(waitingonaneventthatdoesn'toccur,buserrors,busydevices...).
YoucanusethedefaulttimeoutcallbackimplementationbyuncommentingthedefineUSE_DEFAULT_TIMEOUT_CALLBACKinstm32_evel_i2c_ee.hfile.TypicallytheuserimplementationofthiscallbackshouldresetI2Cperipheralandre-initializecommunicationorinworstcaseresetalltheapplication.*/uint32_tsEE_TIMEOUT_UserCallback(void);
/*StartandEndofcriticalsection:
thesecallbacksshouldbetypicallyusedtodisableinterruptswhenenteringacriticalsectionofI2CcommunicationYoumayusedefaultcallbacksprovidedintothisdriverbyuncommentingthe
defineUSE_DEFAULT_CRITICAL_CALLBACKinstm32_evel_i2c_ee.hfile..
Oryoucancommentthatlineandimplementthesecallbacksintoyourapplication*/
voidsEE_EnterCriticalSection_UserCallback(void);voidsEE_ExitCriticalSection_UserCallback(void);
#ifdef__cplusplus
}
#endif
#endif
以下是C文件
#include"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);
/*!
/*! /*! 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,&GPIO_InitStructure); /*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;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=sEE_I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority=sEE_I2C_DMA_SUBPRIO;NVIC_Init(&NVIC_InitStructure); /*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) {GPIO_InitTypeDefGPIO_InitStructure;NVIC_InitTypeDefNVIC_InitStructure; /*! RCC_APB2PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK|sEE_I2C_SDA_GPIO_CLK,ENABLE); /*! RCC_APB1PeriphClockCmd(sEE_I2C_CLK,ENABLE); /*! /*! SCL*/GPIO_InitStructure.GPIO_Pin=sEE_I2C_SCL_PIN;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_OD;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,&GPIO_InitStructure); /*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=ENABLE; NVIC_Init(&NVIC_InitStructure); /*ConfigureandenableI2CDMARXChannelinterrupt*/NVIC_InitStructure.NVIC_IRQChannel=sEE_I2C_DMA_RX_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=sEE_I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority=sEE_I2C_DMA_SUBPRIO;NVIC_Init(&NVIC_InitStructure); /*! /*EnabletheDMAclock*/RCC_AHBPeriphClockCmd(sEE_I2C_DMA_CLK,ENABLE); /*I2CTXDMAChannelconfiguration*/DMA_DeInit(sEE_I2C_DMA_CHANNEL_TX);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;/*Thisparameterwillbeconfigureddurigcommunication*/ sEEDMA_InitStructure.DMA_BufferSize=0xFFFF;/*Thisparameterwill beconfigureddurigcommunication*/ 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_DeInit(sEE_I2C_DMA_CHANNEL_RX);DMA_Init(sEE_I2C_DMA_CHANNEL_RX,&sEEDMA_InitStructure); /*EnabletheDMAChannelsInterrupts*/ DMA_ITConfig(sEE_I2C_DMA_CHANNEL_TX,DMA_IT_TC,ENABLE);DMA_ITConfig(sEE_I2C_DMA_CHANNEL_RX,DMA_IT_TC,ENABLE); } /** *@briefInitializesDMAchannelusedbytheI2CEEPROMdriver. *@paramNone *@retvalNone */ 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_DIR=DMA_DIR_PeripheralDST;sEEDMA_InitStructure.DMA_BufferSize=(uint32_t)BufferSize; DMA_Init(sEE_I2C_DMA_CHANNEL_TX,&sEEDMA_InitStructure); } else { /*ConfiguretheDMARxChannelwiththebufferaddressandthebuffersize*/sEEDMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)pBuffer;sEEDMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;sEEDMA_InitStructure.DMA_BufferSize=(uint32_t)BufferSize; DMA_Init(sEE_I2C_DMA_CHANNEL_RX,&sEEDMA_InitStructure); } } voidsEE_DeInit(void) { sEE_LowLevel_DeInit(); } /** *@briefInitializesperipheralsusedbytheI2CEEPROMdriver. *@paramNone *@retvalNone */ voidsEE_Init(void) { I2C_InitTypeDefI2C_InitStructure; sEE_LowLevel_Init(); /*! /*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); /*ApplysEE_I2Cconfigurationafterenablingit*/ I2C_Init(sEE_I2C,&I2C_InitStructure); /*EnablethesEE_I2CperipheralDMArequests*/I2C_DMACmd(sEE_I2C,ENABLE); } /** *@briefReadsablockofdatafromtheEEPROM. *@param
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- stm32i2cdma 讲解 学习