##// END OF EJS Templates
Added OpenOCD target for olimex-arm-usb-tiny....
Added OpenOCD target for olimex-arm-usb-tiny. Working D51E5TA7601 driver. Added Framebuffer interface. Added generic memory to memory DMA api, mmainly used by framebuffer API. ADS7843 work in progress. Added SOSmartPSU bsp.

File last commit:

r80:c0daab91f95b dev_alexis
r103:3311a844031e dev_alexis
Show More
i2c.c
388 lines | 10.3 KiB | text/x-c | CLexer
/*------------------------------------------------------------------------------
-- This file is a part of the libuc, microcontroler library
-- Copyright (C) 2012, Alexis Jeandet
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------
-- Author : Alexis Jeandet
-- Mail : alexis.jeandet@member.fsf.org
-------------------------------------------------------------------------------*/
#include <i2c.h>
#include <stm32f4xx_usart.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <gpio.h>
#include <core.h>
#include <stdio.h>
#define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
#define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
int i2ctimeout=1000*1000;
I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3};
i2c_t i2copen(int count)
{
#define _INIT_DEV(_RCC_) \
RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \
RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \
RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \
RCC_APB1PeriphClockCmd(_RCC_, ENABLE);
switch(count)
{
case i2c1:
_INIT_DEV(RCC_APB1Periph_I2C1);
return i2c1;
break;
case i2c2:
_INIT_DEV(RCC_APB1Periph_I2C2);
return i2c2;
break;
case i2c3:
_INIT_DEV(RCC_APB1Periph_I2C3);
return i2c3;
break;
default:
break;
}
return -1;
}
i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL)
{
i2c_t dev = i2copen(count);
if(dev!=-1)
{
i2cclose(dev);
i2csetpins(dev,SDA,SCL);
i2copen(count);
i2csetspeed(dev,speed);
i2cenable(count);
}
return dev;
}
int i2cclose(i2c_t dev)
{
switch(dev)
{
case i2c1:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
break;
case i2c2:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
break;
case i2c3:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
break;
default:
break;
}
return 1;
}
int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL)
{
if((dev<i2c4)&&(dev>=i2c1))
{
gpio_t SDApin,SCLpin;
SDApin = gpioopen(SDA);
SCLpin = gpioopen(SCL);
SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
SCLpin |= gpiolowspeed | gpiooutdir | gpioopendraintype | gpionopulltype;
gpiosetconfig(&SCLpin);
for(int i=0;i<32;i++)
{
gpioclr(SCLpin);
for(int l=0;l<200;l++)
{__asm__("nop");}
gpioset(SCLpin);
for(int l=0;l<200;l++)
{__asm__("nop");}
}
SCLpin = gpioopen(SCL);
SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
gpiosetconfig(&SDApin);
gpiosetconfig(&SCLpin);
uint8_t gpioAFi2cx = GPIO_AF_I2C1;
switch(dev)
{
case i2c1:
gpioAFi2cx = GPIO_AF_I2C1;
break;
case i2c2:
gpioAFi2cx = GPIO_AF_I2C2;
break;
case i2c3:
gpioAFi2cx = GPIO_AF_I2C3;
break;
default:
break;
}
GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx);
GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx);
return 0;
}
return -1;
}
int i2cenable(i2c_t dev)
{
if((dev<i2c4)&&(dev>=i2c1))
{
I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
_dev_->CR1 |=1 ;
return 0;
}
return -1;
}
int i2cdisable(i2c_t dev)
{
if((dev<i2c4)&&(dev>=i2c1))
{
I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
_dev_->CR1 &= ~1;
return 0;
}
return -1;
}
int i2csetspeed(i2c_t dev,uint32_t speed)
{
if((dev<i2c4)&&(dev>=i2c1))
{
I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
int32_t APB1Freq=getAPB1Freq()/1000000;
if((APB1Freq>1)&&(APB1Freq<43))
{
uint16_t tmpreg=_dev_->CR2;
tmpreg &= ~(0x1f);
tmpreg |= APB1Freq;
_dev_->CR2=tmpreg;
i2cdisable(dev);
tmpreg=_dev_->CCR;
APB1Freq=getAPB1Freq();
if(speed>100000) //100kHz= standard mode, 400kHz= fast mode
{
if(speed<=400000)
{
tmpreg |= 1<<15;
tmpreg &= ~(1<<14);
tmpreg &= ~(0xfff);
tmpreg |= 0xfff & (APB1Freq/(3*speed));
}
}
else
{
tmpreg &= ~(1<<15);
tmpreg &= ~(0xfff);
tmpreg |= 0xfff & (APB1Freq/(2*speed));
}
_dev_->CCR=tmpreg;
tmpreg=_dev_->TRISE;
tmpreg &= ~(0x3f);
tmpreg |= (APB1Freq/1000000)+1;
_dev_->TRISE = tmpreg;
i2cenable(dev);
return 0;
}
}
return -1;
}
int i2cbusy(i2c_t dev)
{
if((dev<i2c4)&&(dev>=i2c1))
{
I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */
return 0; /* Dev isn't busy */
}
return -1; /* Error, dev is out of range */
}
int i2cwrite(i2c_t dev,char address,char* data,int count)
{
if((dev<i2c4)&&(dev>=i2c1))
{
int timeout=i2ctimeout;
I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
while(i2cbusy(dev))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
_dev_->CR1 |= 1<<8;
timeout=i2ctimeout;
while(!i2cStatusCheck(dev,((uint32_t)0x00030001)))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
_dev_->DR= address<<1;
timeout=i2ctimeout;
while(!i2cStatusCheck(dev, ((uint32_t)0x00070082)))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
address=_dev_->SR2;
for(int i=0;i<count;i++)
{
timeout=i2ctimeout;
while(!i2cStatusCheck(dev,((uint32_t)0x00070080)))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
_dev_->DR= data[i];
}
timeout=i2ctimeout;
while(!i2cStatusCheck(dev,1<<7))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
timeout=i2ctimeout;
while(!i2cStatusCheck(dev,1<<2))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
_dev_->CR1 |= 1<<9;
return count;
}
return -1;
}
int i2cread(i2c_t dev,char address,char* data,int count)
{
if((dev<i2c4)&&(dev>=i2c1))
{
int i=0;
int timeout=i2ctimeout;
while(i2cbusy(dev))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
_dev_->CR1 |= (1<<8) + (1<<10);
timeout=i2ctimeout;
while(!i2cStatusCheck(dev,0x00030001))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
_dev_->DR= (address<<1) + 1;
while(!i2cStatusCheck(dev,0x000002))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
if(count==1)
{
_dev_->CR1 &= ~(1<<10);
}
address=_dev_->SR2;
for(i=0;i<(count-1);i++)
{
timeout=i2ctimeout;
while(!i2cStatusCheck(dev,0x0000040))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
data[i]=_dev_->DR;
}
_dev_->CR1 &= ~(1<<10);
_dev_->CR1 |= 1<<9;
timeout=i2ctimeout;
while(!i2cStatusCheck(dev,0x0000040))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
data[i]=_dev_->DR;
timeout=i2ctimeout;
while(_dev_->CR1 & ((uint16_t)0x0200))
{
if(0==(timeout--))
{
printf("Exited on timeout @ line %d\n\r",__LINE__);
return -1;
}
}
_dev_->CR1 |= 1<<10;
return count;
}
return -1;
}
int i2cStatusCheck(i2c_t dev,int32_t flagMask)
{
int32_t flag;
if((dev<i2c4)&&(dev>=i2c1))
{
I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
flag= _dev_->SR1 + (_dev_->SR2<<16);
if(flagMask==(flag & flagMask))
return 1;
return 0;
}
return -1;
}