Chinaunix首页 | 论坛 | 博客
  • 博客访问: 801379
  • 博文数量: 124
  • 博客积分: 1927
  • 博客等级: 上尉
  • 技术积分: 932
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-31 14:06
文章分类

全部博文(124)

文章存档

2018年(5)

2017年(2)

2016年(6)

2015年(4)

2014年(24)

2013年(7)

2012年(11)

2011年(13)

2010年(52)

我的朋友

分类: LINUX

2010-09-02 10:07:27

  1. //*--------------------------------------------------------------------------------------    
  2. //*      ATMEL Microcontroller Software Support  -  ROUSSET  -    
  3. //*--------------------------------------------------------------------------------------    
  4. //* The software is delivered "AS IS" without warranty or condition of any    
  5. //* kind, either express, implied or statutory. This includes without    
  6. //* limitation any warranty or condition with respect to merchantability or    
  7. //* fitness for any particular purpose, or against the infringements of    
  8. //* intellectual property rights of others.    
  9. //*--------------------------------------------------------------------------------------    
  10. //* File Name           : twi.c    
  11. //* Object              : Basic TWI EEPROM driver    
  12. //* Translator          :    
  13. //* 1.0 25/11/02 NL     : Creation    
  14. //*--------------------------------------------------------------------------------------    
  15. #include                  // AT91RM9200 definitions    
  16. #include              // Library function definitions    
  17. #include "main.h"    
  18. #include "twi.h"    
  19.    
  20.    
  21. //*=========================================================    
  22. //*     WRITE    
  23. //*=========================================================    
  24. //*----------------------------------------------------------------------------    
  25. //* \fn    AT91F_TWI_Write    
  26. //* \brief Send n bytes to a slave device    
  27. //*----------------------------------------------------------------------------    
  28. int AT91F_TWI_Write(const AT91PS_TWI pTwi ,int address, char *data2send, int size)   
  29. {   
  30.    
  31.     // Set the TWI Master Mode Register    
  32.     pTwi->TWI_MMR = ( AT91C_EEPROM_I2C_ADDRESS | AT91C_TWI_IADRSZ_2_BYTE ) & ~AT91C_TWI_MREAD;      
  33.        
  34.     // Set TWI Internal Address Register    
  35.     pTwi->TWI_IADR = address;   
  36.            
  37.     pTwi->TWI_THR = *(data2send++);   
  38.        
  39.     pTwi->TWI_CR = AT91C_TWI_START;   
  40.            
  41.     while (size-- >1){   
  42.    
  43.         // Wait THR Holding register to be empty    
  44.         while (!(pTwi->TWI_SR & AT91C_TWI_TXRDY));   
  45.        
  46.         // Send first byte    
  47.         pTwi->TWI_THR = *(data2send++);   
  48.            
  49.     }   
  50.    
  51.     pTwi->TWI_CR = AT91C_TWI_STOP;          
  52.    
  53.     // Wait transfer is finished    
  54.     while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP));   
  55.            
  56.     return AT91C_EEPROM_WRITE_OK;   
  57. }   
  58.    
  59. //*=========================================================    
  60. //*     READ    
  61. //*=========================================================    
  62. //*----------------------------------------------------------------------------    
  63. //* \fn    AT91F_TWI_Read    
  64. //* \brief Read n bytes from a slave device    
  65. //*----------------------------------------------------------------------------    
  66. int AT91F_TWI_Read(const AT91PS_TWI pTwi , int address, char *data, int size)   
  67. {      
  68.     // Set the TWI Master Mode Register    
  69.     pTwi->TWI_MMR = AT91C_EEPROM_I2C_ADDRESS | AT91C_TWI_IADRSZ_2_BYTE | AT91C_TWI_MREAD;       
  70.        
  71.     // Set TWI Internal Address Register    
  72.     pTwi->TWI_IADR = address;   
  73.        
  74.     // Start transfer    
  75.     pTwi->TWI_CR = AT91C_TWI_START;   
  76.                
  77.     while (size-- >1){   
  78.            
  79.         // Wait RHR Holding register is full    
  80.         while (!(pTwi->TWI_SR & AT91C_TWI_RXRDY));   
  81.    
  82.         // Read byte    
  83.         *(data++) = pTwi->TWI_RHR;   
  84.        
  85.     }   
  86.        
  87.     pTwi->TWI_CR = AT91C_TWI_STOP;   
  88.    
  89.     // Wait transfer is finished    
  90.     while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP));   
  91.    
  92.     // Read last byte    
  93.     *data = pTwi->TWI_RHR;   
  94.            
  95.     return AT91C_EEPROM_READ_OK;   
 
 
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : twi.h
//* Object              :
//*----------------------------------------------------------------------------
#ifndef twi_h
#define twi_h

#define AT91C_EEPROM_I2C_ADDRESS  (0x32<<16)

#define AT91C_EEPROM_READ_OK 0
#define AT91C_EEPROM_WRITE_OK 0

extern int AT91F_TWI_Write (const AT91PS_TWI, int, char *, int );
extern int AT91F_TWI_Read  (const AT91PS_TWI, int, char *, int );

#endif
阅读(1374) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~