6363#include <WProgram.h>
6464#endif
6565
66- #include <inttypes.h>
66+ // #include <inttypes.h>
6767#include "I2C.h"
6868
6969
@@ -77,14 +77,20 @@ static uint8_t bufferIndex = 0;
7777static uint8_t totalBytes = 0 ;
7878static uint16_t timeOutDelay = 0 ;
7979
80+ static uint8_t start (void );
81+ static uint8_t sendAddress (uint8_t );
82+ static uint8_t sendByte (uint8_t );
83+ static uint8_t receiveByte (uint8_t );
84+ static uint8_t stop (void );
85+ static void lockUp (void );
8086
8187
8288////////////// Public Methods ////////////////////////////////////////
8389
8490
85-
8691void I2C_begin ()
8792{
93+ #if 0
8894 #if defined(__AVR_ATmega168__ ) || defined(__AVR_ATmega8__ ) || defined(__AVR_ATmega328P__ )
8995 // activate internal pull-ups for twi
9096 // as per note from atmega8 manual pg167
@@ -102,18 +108,34 @@ void I2C_begin()
102108 TWBR = ((F_CPU / 100000 ) - 16 ) / 2 ;
103109 // enable twi module and acks
104110 TWCR = _BV (TWEN ) | _BV (TWEA );
111+ #else
112+ /* I2C Initialize */
113+ I2C_Init (
114+ I2C_MAX_STANDARD_FREQ , // 100000// I2C_SPEED,
115+ 0xA0 , // OwnAddress, egal
116+ I2C_DUTYCYCLE_2 , // 0x00
117+ I2C_ACK_CURR , // 0x01
118+ I2C_ADDMODE_7BIT , // 0x00
119+ 16 // InputClockFreqencyMhz
120+ );
121+ #endif
105122}
106123
124+ #if 0
107125void I2C_end ()
108126{
109127 TWCR = 0 ;
110128}
129+ #endif
111130
131+ #if 0
112132void I2C_timeOut (uint16_t _timeOut )
113133{
114134 timeOutDelay = _timeOut ;
115135}
136+ #endif
116137
138+ #if 0
117139void I2C_setSpeed (uint8_t _fast )
118140{
119141 if (!_fast )
@@ -125,7 +147,9 @@ void I2C_setSpeed(uint8_t _fast)
125147 TWBR = ((F_CPU / 400000 ) - 16 ) / 2 ;
126148 }
127149}
150+ #endif
128151
152+ #if 0
129153void I2C_pullup (uint8_t activate )
130154{
131155 if (activate )
@@ -157,7 +181,9 @@ void I2C_pullup(uint8_t activate)
157181 #endif
158182 }
159183}
184+ #endif
160185
186+ #if 0
161187void I2C_scan ()
162188{
163189 uint16_t tempTime = timeOutDelay ;
@@ -194,13 +220,16 @@ void I2C_scan()
194220 if (!totalDevicesFound ){Serial .println ("No devices found" );}
195221 timeOutDelay = tempTime ;
196222}
223+ #endif
197224
198-
225+ #if 0
199226uint8_t I2C_available ()
200227{
201228 return (bytesAvailable );
202229}
230+ #endif
203231
232+ #if 0
204233uint8_t I2C_receive ()
205234{
206235 bufferIndex = totalBytes - bytesAvailable ;
@@ -212,7 +241,7 @@ uint8_t I2C_receive()
212241 bytesAvailable -- ;
213242 return (data [bufferIndex ]);
214243}
215-
244+ #endif
216245
217246/*return values for new functions that use the timeOut feature
218247 will now return at what point in the transmission the timeout
@@ -262,6 +291,7 @@ uint8_t I2C_write(uint8_t address, uint8_t registerAddress)
262291 return (returnStatus );
263292}
264293
294+ #if 1
265295uint8_t I2C_write_c (uint8_t address , uint8_t registerAddress , uint8_t data )
266296{
267297 returnStatus = 0 ;
@@ -293,14 +323,17 @@ uint8_t I2C_write_c(uint8_t address, uint8_t registerAddress, uint8_t data)
293323 }
294324 return (returnStatus );
295325}
326+ #endif
296327
328+ #if 0
297329uint8_t I2C_write_s (uint8_t address , uint8_t registerAddress , char * data )
298330{
299331 uint8_t bufferLength = strlen (data );
300332 returnStatus = 0 ;
301333 returnStatus = write (address , registerAddress , (uint8_t * )data , bufferLength );
302334 return (returnStatus );
303335}
336+ #endif
304337
305338uint8_t I2C_write_sn (uint8_t address , uint8_t registerAddress , uint8_t * data , uint8_t numberBytes )
306339{
@@ -337,6 +370,7 @@ uint8_t I2C_write_sn(uint8_t address, uint8_t registerAddress, uint8_t *data, ui
337370 return (returnStatus );
338371}
339372
373+ #if 0
340374uint8_t I2C_read (uint8_t address , uint8_t numberBytes )
341375{
342376 bytesAvailable = 0 ;
@@ -379,7 +413,9 @@ uint8_t I2C_read(uint8_t address, uint8_t numberBytes)
379413 }
380414 return (returnStatus );
381415}
416+ #endif
382417
418+ #if 0
383419uint8_t I2C_read (uint8_t address , uint8_t registerAddress , uint8_t numberBytes )
384420{
385421 bytesAvailable = 0 ;
@@ -439,7 +475,9 @@ uint8_t I2C_read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes)
439475 }
440476 return (returnStatus );
441477}
478+ #endif
442479
480+ #if 0
443481uint8_t I2C_read_sn (uint8_t address , uint8_t numberBytes , uint8_t * dataBuffer )
444482{
445483 bytesAvailable = 0 ;
@@ -481,7 +519,9 @@ uint8_t I2C_read_sn(uint8_t address, uint8_t numberBytes, uint8_t *dataBuffer)
481519 }
482520 return (returnStatus );
483521}
522+ #endif
484523
524+ #if 0
485525uint8_t I2C_read_sn (uint8_t address , uint8_t registerAddress , uint8_t numberBytes , uint8_t * dataBuffer )
486526{
487527 bytesAvailable = 0 ;
@@ -541,13 +581,13 @@ uint8_t I2C_read_sn(uint8_t address, uint8_t registerAddress, uint8_t numberByte
541581 }
542582 return (returnStatus );
543583}
544-
584+ #endif
545585
546586/////////////// Private Methods ////////////////////////////////////////
547587
548-
549- uint8_t I2C_start ()
588+ static uint8_t start (void )
550589{
590+ #if 0
551591 unsigned long startingTime = millis ();
552592 TWCR = (1 <<TWINT )|(1 <<TWSTA )|(1 <<TWEN );
553593 while (!(TWCR & (1 <<TWINT )))
@@ -571,10 +611,15 @@ uint8_t I2C_start()
571611 return (bufferedStatus );
572612 }
573613 return (TWI_STATUS );
614+ #else
615+ I2C -> CR2 |= I2C_CR2_START ; // I2C_GenerateSTART(enable);
616+ return 0 ;
617+ #endif
574618}
575619
576- uint8_t I2C_sendAddress (uint8_t i2cAddress )
620+ static uint8_t sendAddress (uint8_t i2cAddress )
577621{
622+ #if 0
578623 TWDR = i2cAddress ;
579624 unsigned long startingTime = millis ();
580625 TWCR = (1 <<TWINT ) | (1 <<TWEN );
@@ -603,10 +648,23 @@ uint8_t I2C_sendAddress(uint8_t i2cAddress)
603648 lockUp ();
604649 return (bufferedStatus );
605650 }
651+ #else
652+ /* Test on EV5 and clear it */
653+ while (!I2C_CheckEvent (I2C_EVENT_MASTER_MODE_SELECT ));
654+
655+ /* Send the Address + Direction */
656+ I2C -> DR = i2cAddress ; // I2C_Send7bitAddress()
657+
658+ /* Test on EV6 and clear it */
659+ while (!I2C_CheckEvent (I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ));
660+
661+ return 0 ;
662+ #endif
606663}
607664
608- uint8_t I2C_sendByte (uint8_t i2cData )
665+ uint8_t sendByte (uint8_t i2cData )
609666{
667+ #if 0
610668 TWDR = i2cData ;
611669 unsigned long startingTime = millis ();
612670 TWCR = (1 <<TWINT ) | (1 <<TWEN );
@@ -635,10 +693,18 @@ uint8_t I2C_sendByte(uint8_t i2cData)
635693 lockUp ();
636694 return (bufferedStatus );
637695 }
696+ #else
697+ /* Test on EV8 */
698+ while (!I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTING ));
699+
700+ I2C -> DR = i2cData ;
701+ return 1 ;
702+ #endif
638703}
639704
640- uint8_t I2C_receiveByte (uint8_t ack )
705+ static uint8_t receiveByte (uint8_t ack )
641706{
707+ #if 0
642708 unsigned long startingTime = millis ();
643709 if (ack )
644710 {
@@ -665,10 +731,15 @@ uint8_t I2C_receiveByte(uint8_t ack)
665731 return (bufferedStatus );
666732 }
667733 return (TWI_STATUS );
734+ #else
735+ (void ) ack ;
736+ return 1 ;
737+ #endif
668738}
669739
670- uint8_t I2C_stop ( )
740+ static uint8_t stop ( void )
671741{
742+ #if 0
672743 unsigned long startingTime = millis ();
673744 TWCR = (1 <<TWINT )|(1 <<TWEN )| (1 <<TWSTO );
674745 while ((TWCR & (1 <<TWSTO )))
@@ -681,11 +752,20 @@ uint8_t I2C_stop()
681752 }
682753
683754 }
755+ #else
756+ /* Test on EV8_2 */
757+ while (!I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED ));
758+
759+ /* Generate a STOP condition */
760+ I2C -> CR2 |= I2C_CR2_STOP ;
761+ #endif
684762 return (0 );
685763}
686764
687- void I2C_lockUp ( )
765+ static void lockUp ( void )
688766{
767+ #if 0
689768 TWCR = 0 ; //releases SDA and SCL lines to high impedance
690769 TWCR = _BV (TWEN ) | _BV (TWEA ); //reinitialize TWI
770+ #endif
691771}
0 commit comments