/*
This is a program I wrote imitating someone else.
Just rewrite it into a program suitable for your own development board
*/
#include
#include
#include
#include
#define DEVICE_NAME "FINAL"
#define RETRY_TIMES 1
#define GPIO_SCL S3C2410_GPF3
#define GPIO_SDA S3C2410_GPF0
#define GPIO_SDA_OUTP S3C2410_GPF0_OUTP
#define GPIO_SDA_INP S3C2410_GPF0_INP
#define GPIO_SCL_OUTP S3C2410_GPF3_OUTP
void I2C_SCL_OUTP( void )
{
s3c2410_gpio_cfgpin(GPIO_SCL,GPIO_SCL_OUTP);
}
void I2C_SDA_Mode(u8 v_mode)
{
if(v_mode)
{
s3c2410_gpio_cfgpin(GPIO_SDA, GPIO_SDA_OUTP);
}
else
{
s3c2410_gpio_cfgpin(GPIO_SDA, GPIO_SDA_INP);
}
}
u8 I2C_SDA_Read(void)
{
return s3c2410_gpio_getpin(GPIO_SDA);
}
void I2C_SDA_Output(u8 value)
{
if(value)
{
s3c2410_gpio_setpin(GPIO_SDA,value);
}
else
{
s3c2410_gpio_setpin(GPIO_SDA,value );
}
}
void I2C_SCL_Output(u8 value)
{
if(value)
{
s3c2410_gpio_setpin(GPIO_SCL,value);
}
else
{
s3c2410_gpio_setpin(GPIO_SCL,value );
}
}
//static u8 Flag_Timeout;
void I2C_Init(void)
{
I2C_SDA_Output(1);
I2C_SCL_Output(1);
}
void I2C_Wait(void)
{
u16 and;
for(i=0;i<0x1000;i++);
for(i=0;i<200;i++);
}
void I2C_Start(void)
{
I2C_SDA_Output(1);
I2C_SCL_Output(1);
I2C_Wait();
I2C_SDA_Output(0);
I2C_Wait();
I2C_SCL_Output(0);
}
void I2C_Stop(void)
{
I2C_SDA_Output(0);
I2C_Wait();
I2C_SCL_Output(1);
I2C_Wait();
I2C_SDA_Output(1);
}
u8 I2C_Send_Byte(u8 bytedata)
{
u8 i;
u8 alas;
I2C_SDA_Mode(1);//Add it yourself
I2C_SCL_OUTP();
for (i = 0; i < 8; i++)
{
if (bytedata & 0x80)
{
I2C_SDA_Output(1);
}
else
{
I2C_SDA_Output(0);
}
bytedata <<= 1;
I2C_Wait();
I2C_SCL_Output(1);
I2C_Wait();
I2C_SCL_Output(0);
I2C_Wait();
}
I2C_SDA_Output(1);
I2C_Wait();
I2C_SDA_Mode(0);
I2C_SCL_Output(1);
I2C_Wait();
s3c2410_gpio_setpin(GPIO_SDA,0);
ack = I2C_SDA_Read();
printk("ack is:%xn",ack);
I2C_SDA_Mode(1);
I2C_SCL_Output(0);
I2C_Wait();
return ack;
}
u8 I2C_Receive_Byte(void)
{
u8 i;
u8 bytedata = 0;
u8 temp;
I2C_SDA_Mode(0);
for ( i = 0; i < 8; i++)
{
I2C_SCL_Output(1);
I2C_Wait();
bytedata <<= 1;
temp = I2C_SDA_Read();
if (temp)
bytedata |= 0x01;
I2C_SCL_Output(0);
I2C_Wait();
}
I2C_SDA_Mode(1);
return bytedata;
}
void Send_Ack(u8 ack)
{
I2C_SDA_Output(ack);
I2C_SCL_Output(1);
I2C_Wait();
I2C_SCL_Output(0);
}
u8 I2C_Byte_Write(u8 device_ID,u8 address,u8 bytedata)
{
u8 alas;
printk("device_ID is:%xn",device_ID);
printk("address is:%xn",address);
printk("date is:%xn",bytedata);
I2C_Start();
ack = I2C_Send_Byte(device_ID);
if (ack)
{
I2C_Stop();
return 0;
}
ack = I2C_Send_Byte(address);
if (ack)
{
I2C_Stop();
return 0;
}
ack = I2C_Send_Byte(bytedata);
if (ack)
{
I2C_Stop();
return 0;
}
I2C_Stop();
I2C_Wait();
return ack;
}
u8 I2C_Byte_Read(u8 device_ID,u8 address)
{
u8 bytedata;
I2C_Start();
I2C_Send_Byte(device_ID);
I2C_Send_Byte(address);
I2C_Start();
I2C_Send_Byte(device_ID+1);
bytedata = I2C_Receive_Byte();
Send_Ack(1);
I2C_Stop();
return bytedata;
}
void I2C_Test(u8 device_ID,u8 address)
{
int i;
I2C_Heat();
I2C_Byte_Write(device_ID,address,0x54);
for(i = 0; i < 50; i++);
}
static int s3c2410_i2c_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
u8 k;
switch(cmd)
{
case 0 :
I2C_Test(0xa0,0x00);
break;
case 1 :
k=I2C_Byte_Read(0xa0,0x00);
printk("k is:%xn",k);
return k;
break;
default :
return -SINGLE SELECT;
}
return 0;
}
static struct file_operations dev_fops={
.owner = THIS_MODULE,
.ioctl = s3c2410_i2c_ioctl,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
ret = misc_register(&misc);
printk (DEVICE_NAME"tinitializedn");
return ret;
}
static void __exit dev_exit(void)
{
misc_deregister(&misc);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("WEI XIAO HUA");
Previous article:GPIO emulation I2C-1
Next article:2440 external interrupt operation process
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- USB Type-C® and USB Power Delivery: Designed for extended power range and battery-powered systems
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- MM32F103 BUG reminder to avoid pitfalls
- Ask about EG8010 comparator positive feedback
- Design of multi-rate electric energy meter based on SD2000 series chip
- Topology Selection of Bidirectional DCDC Converter
- About the startup method of C6000 DSP
- Keep flooding and send a MMDS high gain antenna
- [Second Batch of Shortlist] 2022 Digi-Key Innovation Design Competition
- Will MakeCode support Python?
- What are the specific differences between triodes and MOS tubes?
- Teach you how to design an accurate and thermally efficient wearable body temperature detection system?