123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
-
-
- #include "u8g.h"
-
- static uint8_t u8g_i2c_err_code;
-
-
- static uint8_t u8g_i2c_err_pos;
-
-
- void u8g_i2c_clear_error(void)
- {
- u8g_i2c_err_code = U8G_I2C_ERR_NONE;
- u8g_i2c_err_pos = 0;
- }
-
- uint8_t u8g_i2c_get_error(void)
- {
- return u8g_i2c_err_code;
- }
-
- uint8_t u8g_i2c_get_err_pos(void)
- {
- return u8g_i2c_err_pos;
- }
-
- static void u8g_i2c_set_error(uint8_t code, uint8_t pos)
- {
- if ( u8g_i2c_err_code > 0 )
- return;
- u8g_i2c_err_code |= code;
- u8g_i2c_err_pos = pos;
- }
-
-
-
- #if defined(__AVR__)
- #define U8G_ATMEGA_HW_TWI
-
-
- #if __AVR_ARCH__ == 2
- #undef U8G_ATMEGA_HW_TWI
- #endif
- #if __AVR_ARCH__ == 25
- #undef U8G_ATMEGA_HW_TWI
- #endif
- #endif
-
- #if defined(U8G_ATMEGA_HW_TWI)
-
- #include <avr/io.h>
- #include <util/twi.h>
-
-
-
- void u8g_i2c_init(uint8_t options)
- {
-
-
- TWSR = 0;
- TWBR = F_CPU/(2*100000)-8;
- u8g_i2c_clear_error();
- }
-
- uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
- {
- volatile uint16_t cnt = 2000;
- while( !(TWCR & mask) )
- {
- if ( cnt == 0 )
- {
- u8g_i2c_set_error(U8G_I2C_ERR_TIMEOUT, pos);
- return 0;
- }
- cnt--;
- }
- return 1;
- }
-
-
- uint8_t u8g_i2c_start(uint8_t sla)
- {
- register uint8_t status;
-
-
- TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
-
-
- if ( u8g_i2c_wait(_BV(TWINT), 1) == 0 )
- return 0;
-
- status = TW_STATUS;
-
-
- if ( status != TW_START && status != TW_REP_START )
- {
- u8g_i2c_set_error(U8G_I2C_ERR_BUS, 1);
- return 0;
- }
-
-
- TWDR = sla;
-
-
- TWCR = _BV(TWINT) | _BV(TWEN);
-
-
- if ( u8g_i2c_wait(_BV(TWINT), 2) == 0 )
- return 0;
- status = TW_STATUS;
-
-
- if ( status != TW_MT_SLA_ACK )
- {
- u8g_i2c_set_error(U8G_I2C_ERR_BUS, 2);
- return 0;
- }
-
- return 1;
- }
-
- uint8_t u8g_i2c_send_byte(uint8_t data)
- {
- register uint8_t status;
- TWDR = data;
- TWCR = _BV(TWINT) | _BV(TWEN);
- if ( u8g_i2c_wait(_BV(TWINT), 3) == 0 )
- return 0;
- status = TW_STATUS;
-
- if ( status != TW_MT_DATA_ACK )
- {
- u8g_i2c_set_error(U8G_I2C_ERR_BUS, 3);
- return 0;
- }
-
- return 1;
- }
-
- void u8g_i2c_stop(void)
- {
-
- TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWSTO);
-
-
- u8g_i2c_wait(_BV(TWSTO), 4);
-
- }
-
-
-
- #else
-
-
-
- void u8g_i2c_init(uint8_t options)
- {
- u8g_i2c_clear_error();
- }
-
- uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
- {
- return 1;
- }
-
- uint8_t u8g_i2c_start(uint8_t sla)
- {
- return 1;
- }
- uint8_t u8g_i2c_send_byte(uint8_t data)
- {
- return 1;
- }
-
- void u8g_i2c_stop(void)
- {
- }
-
-
- #endif
|