/*------------------------------------------------------------------------------ -- 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@gmail.com -------------------------------------------------------------------------------*/ #include #include #include #include #include #include #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) 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 0: _INIT_DEV(RCC_APB1Periph_I2C1); return (i2c_t) 0; break; case 1: _INIT_DEV(RCC_APB1Periph_I2C2); return (i2c_t) 1; break; case 2: _INIT_DEV(RCC_APB1Periph_I2C3); return (i2c_t) 2; 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); printf("dev = %d\n\r",dev); if(dev!=-1) { i2cenable(count); I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; _dev_->CR1 = (1<<15); _dev_->CR1 &=~(1<<15); i2cdisable(count); i2csetpins(dev,SDA,SCL); i2csetspeed(dev,speed); i2cenable(count); } return dev; } int i2cclose(i2c_t dev) { switch((int)dev) { case (int)0: RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); break; case (int)1: RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); break; case (int)2: RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE); break; default: break; } return 1; } int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL) { if((dev<3)&&(dev>=0)) { gpio_t SDApin,SCLpin; SDApin = gpioopen(SDA); SCLpin = gpioopen(SCL); SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; gpiosetconfig(&SDApin); gpiosetconfig(&SCLpin); uint8_t gpioAFi2cx = GPIO_AF_I2C1; switch((int)dev) { case 0: gpioAFi2cx = GPIO_AF_I2C1; break; case 1: gpioAFi2cx = GPIO_AF_I2C2; break; case 2: 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<3)&&(dev>=0)) { I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; _dev_->CR1 |=1 ; return 0; } return -1; } int i2cdisable(i2c_t dev) { if((dev<3)&&(dev>=0)) { 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<3)&&(dev>=0)) { I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; int32_t APB1Freq=getAPB1Freq()/1000000; if((APB1Freq>1)&&(APB1Freq<43)) { int enabled=((_dev_->CR1&1)==1); i2cdisable(dev); _dev_->CR2 &= ~(0x1f); _dev_->CR2 |= APB1Freq; if(speed>100000) //100kHz= standard mode, 400kHz= fast mode { if(speed<=400000) { _dev_->CCR |= 1<<15; _dev_->CCR &= ~(1<<14); _dev_->CCR &= ~(0xfff); _dev_->CCR |= 0xfff & (APB1Freq/(3*speed)); } } else { _dev_->CCR &= ~(1<<15); _dev_->CCR &= ~(0xfff); _dev_->CCR |= 0xfff & (APB1Freq/(2*speed)); } if(enabled)i2cenable(dev); return 0; } } return -1; } int i2cwrite(i2c_t dev,char address,char* data,int count) { if((dev<3)&&(dev>=0)) { I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; _dev_->CR1 |= 1<<8; while((_dev_->SR1&1)==0); _dev_->DR= address<<1; while((_dev_->SR1 & (1<<3))==0); address=_dev_->SR2; for(int i=0;iSR1 & (1<<7))==0); _dev_->DR= data[i]; } while((_dev_->SR1 & (1<<7))==0); while((_dev_->SR1 & (1<<2))==0); _dev_->CR1 |= 1<<9; return count; } return -1; } int i2cread(i2c_t dev,char address,char* data,int count) { if((dev<3)&&(dev>=0)) { I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; _dev_->CR1 |= (1<<8) | (1<<10); if(count==1) { _dev_->CR1 &= ~(1<<10); } while((_dev_->SR1&1)==0); _dev_->DR= (address<<1) + 1; while((_dev_->SR1 & (1<<3))==0); address=_dev_->SR2; for(int i=0;iSR1 & (1<<6))==0); data[i]=_dev_->DR; } _dev_->CR1 &= ~(1<<10); _dev_->CR1 |= 1<<9; while((_dev_->SR1 & (1<<6))==0); data[count-1]=_dev_->DR; return count; } return -1; }