硬件IIC用DMA传输

2019-07-20 13:44发布

我用的是探索者stm32里DMA例程,ST官方提供IIC读写24CXX程序包,想配置IIC1向24CXX写入数据,现在的现象是IIC写起始信号,地址都正确,开启DMA传数据不成功,下面贴上我的代码,大神能否帮忙指点下?
int main(void)
{
        u16 j=0;
    u8 i = 0;
        u8 readbuf[64];

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        uart_init(115200);
        delay_init();
       
        BSP_LED_Init();  //μ÷êÔμÆ
        usmart_dev.init(84);  //μ÷êÔ×é¼t
       
       
       
        MYDMA_Config(DMA1_Stream7,DMA_Channel_1,(u32)&I2C1->DR,(u32)SendBuff,SEND_BUF_SIZE);//初始化DMA
        Hard_IIC_Init(I2C1, 0xA0, 0);//初始化IIC
        printf ("Hard IIC Test. ");
       
        Hard_IIC_WriteNByte(I2C1, 0xA0, 0x00, 5, buf, &i);  //-->OK


}
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)
{

        DMA_InitTypeDef  DMA_InitStructure;
       
        if((u32)DMA_Streamx>(u32)DMA2)//μÃμ½μ±Ç°streamêÇêôóúDMA2»1êÇDMA1
        {
          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2ê±Öóê1Äü
               
        }else
        {
          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1ê±Öóê1Äü
        }
  DMA_DeInit(DMA_Streamx);
       
        while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//μè′yDMA¿éÅäÖÃ
       
  /* ÅäÖÃ DMA Stream */
  DMA_InitStructure.DMA_Channel = chx;  //í¨μàÑ¡Ôñ
  DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMAíaéèμØÖ·
  DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA ′æ′¢Æ÷0μØÖ·
  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//′æ′¢Æ÷μ½íaéèÄ£ê½
  DMA_InitStructure.DMA_BufferSize = ndtr;//êy¾Y′«êäá¿
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//íaéè·ÇÔöá¿Ä£ê½
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//′æ′¢Æ÷Ôöá¿Ä£ê½
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//íaéèêy¾Y3¤¶è:8λ
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//′æ′¢Æ÷êy¾Y3¤¶è:8λ
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// ê1óÃÆÕí¨Ä£ê½
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//ÖDμèóÅÏ輶
  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;//íaéèí»·¢μ¥′Î′«êä
  DMA_Init(DMA_Streamx, &DMA_InitStructure);//3õê¼»ˉDMA Stream
       

  /* Enable the DMA Channels Interrupts */
  DMA_ITConfig(DMA1_Stream6, DMA_IT_TC, ENABLE);
// DMA_ITConfig(sEE_I2C_DMA_STREAM_RX, DMA_IT_TC, ENABLE);

}

void Hard_IIC_Init(I2C_TypeDef* IICx, u8 SlaveAdd, u8 F103IIC1_Remap/* Õë¶ÔF103μÄIIC1ÖØó3éäμÄ */)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        I2C_InitTypeDef I2C_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       

       
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
       
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        //
        //1ü½Å¸′óÃ
        //
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
       
       
        //
        //ÅäÖÃIIC2Îêy
        //
        I2C_InitStructure.I2C_Mode                = I2C_Mode_I2C;
        I2C_InitStructure.I2C_DutyCycle           = I2C_DutyCycle_2;
        I2C_InitStructure.I2C_OwnAddress1         = SlaveAdd;  //′óé豸μØÖ·
        I2C_InitStructure.I2C_Ack                 = I2C_Ack_Enable;
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
        I2C_InitStructure.I2C_ClockSpeed          = (100 * 1000);  //SCL×î′ó100KHz
       
       
        I2C_Cmd(IICx, ENABLE);
        I2C_Init(IICx, &I2C_InitStructure);
       
       
        /* Configure and enable I2C DMA TX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream6_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);

  /* Configure and enable I2C DMA RX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = sEE_I2C_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = sEE_I2C_DMA_SUBPRIO;
  NVIC_Init(&NVIC_InitStructure);
       
       
        I2C_AcknowledgeConfig(IICx, ENABLE);
       
       
        /* Enable the sEE_I2C peripheral DMA requests */
  I2C_DMACmd(IICx, ENABLE);
        //
        //½ûÖ1ê±ÖóÑó3¤
        //
        IICx->CR1 |= 1<<7;
}

void Hard_IIC_WriteNByte(I2C_TypeDef * IICx, u8 SlaveAdd, u8 WriteAdd, u32 NumToWrite, u8 * pBuffer, u8 * err)
{
        u16 sta = 0;
        u16 temp = 0;
       
        while (I2C_GetFlagStatus(IICx, I2C_FLAG_BUSY))  //μè′yIIC
        {
                temp++;
                if (temp > 800)
                {
                        *err |= 1<<0;
                        I2C_GenerateSTOP(IICx, ENABLE);  //2úéúí£&#214;1D&#197;o&#197;
                        return;
                }
        }
        I2C_GenerateSTART(IICx, ENABLE);  //2úéú&#198;eê&#188;D&#197;o&#197;
        temp = 0;
        //
        //EV5
        //
        while (!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_MODE_SELECT))
        {
                temp++;
                if (temp > 800)
                {
                        *err |= 1<<1;
                        I2C_GenerateSTOP(IICx, ENABLE);  //2úéúí£&#214;1D&#197;o&#197;
                        return;
                }
        }
        I2C_Send7bitAddress(IICx, SlaveAdd, I2C_Direction_Transmitter);  //·¢&#203;íéè±&#184;μ&#216;&#214;·
        temp = 0;
        //
        //EV6
        //
        while (!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
        {
                temp++;
                if (temp > 1000)
                {
                        *err |= 1<<2;
                        I2C_GenerateSTOP(IICx, ENABLE);  //2úéúí£&#214;1D&#197;o&#197;
                        return;
                }
        }
        //
        //&#182;áè&#161;SR2×′ì&#172;&#188;&#196;′&#230;&#198;÷
        //
        sta = IICx->SR2;  //èí&#188;t&#182;áè&#161;SR1&#188;&#196;′&#230;&#198;÷oó,&#182;&#212;SR2&#188;&#196;′&#230;&#198;÷μ&#196;&#182;á2ù×÷&#189;&#171;&#199;&#229;3yADDR&#206;&#187;£&#172;2&#187;&#191;ééù£&#161;£&#161;£&#161;£&#161;£&#161;£&#161;£&#161;£&#161;£&#161;
        I2C_SendData(IICx, WriteAdd);  //·¢&#203;í′&#230;′¢μ&#216;&#214;·
        temp = 0;
        //
        //EV8
        //
        while (!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
        {
                temp++;
                if (temp > 800)
                {
                        *err |= 1<<3;
                        I2C_GenerateSTOP(IICx, ENABLE);  //2úéúí£&#214;1D&#197;o&#197;
                        return;
                }
        }

         /* Configure the DMA Tx Channel with the buffer address and the buffer size */
  sEE_LowLevel_DMAConfig((uint32_t)pBuffer, NumToWrite, sEE_DIRECTION_TX);

  /* Enable the DMA Tx Channel */
  DMA_Cmd(DMA1_Stream6, ENABLE);

       
}

0条回答

一周热门 更多>