利用MCF52259片上i2c 控制器读写常用的at24c02 eeprom,用的开发板是我们做的NSCF52259-R1开发板
//filename: i2c_driver.h
//mcf52259 i2c驱动头文件
#ifndef _NICROSYSTEM_NSCF52259R1_I2C_DRIVER_H_
#define _NICROSYSTEM_NSCF52259R1_I2C_DRIVER_H_
void init_i2c();
void i2c_start();
unsigned char i2c_send_byte(unsigned char c);
unsigned char i2c_recv_byte(unsigned char ack);
void i2c_stop();
void i2c_restart();
#endif
//Filename: i2c_driver.c
//MCF52259 I2c驱动
#include "support_common.h" /* include peripheral declarations and more */
#include "i2c_driver.h"
void init_i2c()
{
unsigned char temp;
MCF_GPIO_PASPAR |=MCF_GPIO_PASPAR_SCL0_SCL0 |MCF_GPIO_PASPAR_SDA0_SDA0 ;
MCF_I2C_I2FDR(0)=MCF_I2C_I2FDR_IC(0x3A);//about 100khz
MCF_I2C_I2ADR(0)=0xf0;
MCF_I2C_I2CR(0)=0x80;//enable i2c
// MCF_I2C1_I2CR=0;
if(MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IBB)
{
MCF_I2C_I2CR(0)=0x0;
MCF_I2C_I2CR(0)=MCF_I2C_I2CR_IEN | /* enable module */
MCF_I2C_I2CR_MSTA;
temp=MCF_I2C_I2DR(0);
MCF_I2C_I2SR(0)=0;
MCF_I2C_I2CR(0)=0x0;
MCF_I2C_I2CR(0)=0x80;//enable i2c
}
}
void i2c_start()
{
while(MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IBB);
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MTX ;
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MSTA ;
}
unsigned char i2c_send_byte(unsigned char c)
{
// MCF_I2C0_I2CR|=MCF_I2C_I2CR_MTX ;
// if(MCF_I2C0_I2SR|MCF_I2C_I2SR_IAL)
// {
//arbitration lost
// MCF_I2C0_I2SR&=~MCF_I2C_I2SR_IAL ;
// }
// i2c_start();
// MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MTX ;
// MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MSTA ;
MCF_I2C_I2DR(0)=c;
while((MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IIF)==0)
{
}
MCF_I2C_I2SR(0)&=~MCF_I2C_I2SR_IIF;
if(MCF_I2C_I2SR(0)&MCF_I2C_I2SR_RXAK)
return 1;//failed
else
return 0;//success
}
unsigned char i2c_recv_byte(unsigned char ack)
{
unsigned char temp;
if(ack>0)
{
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_TXAK;
}
else
{
MCF_I2C_I2CR(0)&=~MCF_I2C_I2CR_TXAK;
}
MCF_I2C_I2CR(0)&=~MCF_I2C_I2CR_MTX ;
temp=MCF_I2C_I2DR(0);
while((MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IIF)==0)
{
}
MCF_I2C_I2SR(0)&=~MCF_I2C_I2SR_IIF;
temp= MCF_I2C_I2DR(0);
while((MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IIF)==0)
{
}
MCF_I2C_I2SR(0)&=~MCF_I2C_I2SR_IIF;
return temp;
}
void i2c_stop()
{
MCF_I2C_I2CR(0)&=~MCF_I2C_I2CR_MSTA ;
}
void i2c_restart()
{
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_RSTA;
}
//Filename: uart.h
//串口驱动头文件
#ifndef _NICROSYSTEM_NSMCF52259R1_UART_H_
#define _NICROSYSTEM_NSMCF52259R1_UART_H_
#include "support_common.h"
void uart_putstr(uint8 channel, char *str);
void uart_putchar (uint8 channel, char ch);
char uart_getchar (uint8 channel);
void set_gpio_uart(uint8 channel);
uint8 uart_init(uint8 channel,uint32 sysClockKhz,uint32 baudRate);
#endif
//filename:uart.c
//串口驱动
#include "uart.h"
uint8 uart_init(uint8 channel,uint32 sysClockKhz,uint32 baudRate)
{
uint16 ubgs;
if(channel>2)
{
return 1;//error
}
set_gpio_uart(channel);
/*
* Reset Transmitter
*/
MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_TX;
/*
* Reset Receiver
*/
MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_RX;
/*
* Reset Mode Register
*/
MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_MR;
/*
* No parity, 8-bits per character
*/
MCF_UART_UMR(channel) = (0
| MCF_UART_UMR_PM_NONE
| MCF_UART_UMR_BC_8 );
/*
* No echo or loopback, 1 stop bit
*/
MCF_UART_UMR(channel) = (0
| MCF_UART_UMR_CM_NORMAL
| MCF_UART_UMR_SB_STOP_BITS_1);
/*
* Set Rx and Tx baud by SYSTEM CLOCK
*/
MCF_UART_UCSR(channel) = (0
| MCF_UART_UCSR_RCS_SYS_CLK
| MCF_UART_UCSR_TCS_SYS_CLK);
/*
* Mask all UART interrupts
*/
MCF_UART_UIMR(channel) = 0;
/*
* Calculate baud settings
*/
ubgs = (uint16)((sysClockKhz * 1000)/(baudRate * 32));
MCF_UART_UBG1(channel) = (uint8)((ubgs & 0xFF00) >> 8);
MCF_UART_UBG2(channel) = (uint8)(ubgs & 0x00FF);
/*
* Enable receiver and transmitter
*/
MCF_UART_UCR(channel) = (0
| MCF_UART_UCR_TX_ENABLED
| MCF_UART_UCR_RX_ENABLED);
return 0;//success
}
void set_gpio_uart(uint8 channel)
{
switch(channel)
{
case 0:
MCF_GPIO_PUAPAR|=MCF_GPIO_PUAPAR_UTXD0_UTXD0 |
MCF_GPIO_PUAPAR_URXD0_URXD0 |MCF_GPIO_PUAPAR_URTS0_URTS0 |
MCF_GPIO_PUAPAR_UCTS0_UCTS0 ;
break;
case 1:
MCF_GPIO_PUBPAR|=MCF_GPIO_PUBPAR_UTXD1_UTXD1 |
MCF_GPIO_PUBPAR_URXD1_URXD1 ;
break;
case 2:
MCF_GPIO_PUCPAR|=MCF_GPIO_PUCPAR_UTXD2_UTXD2 |
MCF_GPIO_PUCPAR_URXD2_URXD2 ;
break;
default:
break;
}
}
/*
* Wait for a character to be received on the specified UART
*
* Return Values:
* the received character
*/
char uart_getchar (uint8 channel)
{
/* Wait until character has been received */
while (!(MCF_UART_USR(channel) & MCF_UART_USR_RXRDY))
{
};
return (char)MCF_UART_URB(channel);
}
/********************************************************************/
/*
* Wait for space in the UART Tx FIFO and then send a character
*/
void uart_putchar (uint8 channel, char ch)
{
/* Wait until space is available in the FIFO */
while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY))
{
};
/* Send the character */
MCF_UART_UTB(channel) = (uint8)ch;
}
/*********************
* send a string using poll mode
*******************/
void uart_putstr(uint8 channel, char *str)
{
while(*str!=0)
{
uart_putchar(channel,*str++);
}
}
//filename: at24c02.h
//at24c02驱动头文件
#ifndef _NICROSYSTEM_NSCF52259R1_AT24C02_H_
#define _NICROSYSTEM_NSCF52259R1_AT24C02_H_
#include "support_common.h"
#define EEPROM_MAX_ADDR 256
#define WR24C02 0xA0
#define RD24C02 0xA1
unsigned char at24c02_write(unsigned char *buf,unsigned char num,uint8 addr);
unsigned char at24c02_read(unsigned char *buf, unsigned char num, uint8 addr);
#endif
//filename:at24c02.c
//at24c02驱动
#include "i2c_driver.h"
#include "at24c02.h"
#include "delay.h"
/* write 24xxxx I2C EEPROM
* 参数:
* buf---包含待写入数据的缓冲区
* addr---eeprom起始地址
* num--数据长度
* 返回:
* 0---success
* 1---failed
* 2---invalid address
*/
unsigned char at24c02_write(unsigned char *buf,unsigned char num,uint8 addr)
{
unsigned char i,ret;
if(addr+num>EEPROM_MAX_ADDR)
return 2;
i2c_start();
ret=i2c_send_byte(WR24C02);
if(ret)
{
i2c_stop();
return 1;
}
ret=i2c_send_byte(addr);
if(ret)
{
i2c_stop();
return 1;
}
for(i=0;i
{
if(i2c_send_byte(buf))
{
i2c_stop();
return 1;
}
}
i2c_stop();
delay_ms(80000,0,5);
return 0;
}
/*read 24xxxx I2C EEPROM
* arguments:
* buf---return the data
* addr---address of cell you want to write
* num--the numner of data we want to read
* return:
* 0---success
* 1---failed
* 2---invalid address
*/
unsigned char at24c02_read(unsigned char *buf, unsigned char num, uint8 addr)
{
unsigned char i,ret;
if(addr+num>EEPROM_MAX_ADDR)
return 2;
i2c_start();
if(i2c_send_byte(WR24C02))
{
i2c_stop();
return 1;
}
ret=i2c_send_byte(addr);
if(ret)
{
i2c_stop();
return 1;
}
i2c_restart();
if(i2c_send_byte(RD24C02))
{
i2c_stop();
return 1;
}
for(i=0;i
{
buf=i2c_recv_byte(0);
}
buf[i++]=i2c_recv_byte(1);
i2c_stop();
return 0;
}
//filename:main.c
//demo主程序
/*
* main implementation: use this sample to create your own application
*
*/
#include "support_common.h" /* include peripheral declarations and more */
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
/* Standard IO is only possible if Console or UART support is enabled. */
#include
#endif
#include "uart.h"
#include "i2c_driver.h"
#include "at24c02.h"
#include "delay.h"
int main(void)
{
int counter = 0;
char ch="0";
uint8 ret="0";
char buf[50];
uint8 addr="0";
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
printf("Hello World in C from MCF52259 derivative on MCF52259 board\n\r");
fflush(stdout);
#endif
uart_init(0,80000,19200);
pit_init(0);
init_i2c();
uart_putstr(0,(char*)("Nicrosystem NSCF52259R1 i2cdemo start....\n"));
for(;;) {
counter++;
ch=uart_getchar(0);
sprintf(buf,"received:%d\n",ch);
uart_putstr(0,buf);
ret=at24c02_write((uint8*)(&ch),1,addr);
if(ret)
{
sprintf(buf,"Write ADDR#%d failed\n",addr);
uart_putstr(0,buf);
}
else
{
sprintf(buf,"Write ADDR#%d success\n",addr);
uart_putstr(0,buf);
}
ch=0;
ret=at24c02_read((uint8*)(&ch),1,addr);
if(ret)
{
sprintf(buf,"Read ADDR#%d failed\n",addr);
uart_putstr(0,buf);
}
else
{
sprintf(buf,"Read ADDR#%d success, get:%d\n",addr,ch);
uart_putstr(0,buf);
}
addr++;
}
}