Changeset 1926
- Timestamp:
- 11/11/08 19:15:01 (2 months ago)
- Files:
-
- trunk/bertos/cfg/cfg_i2c.h (modified) (1 diff)
- trunk/bertos/cpu/avr/drv/i2c_avr.c (modified) (8 diffs)
- trunk/bertos/drv/i2c.h (modified) (1 diff)
- trunk/bertos/drv/i2c_bitbang.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/bertos/cfg/cfg_i2c.h
r1883 r1926 51 51 #define CONFIG_I2C_START_TIMEOUT 100 52 52 53 /** 54 * I2C driver can have 2 backends: 55 * I2C_BACKEND_BUILTIN: Use (if present) the builtin i2c hardware. 56 * I2C_BACKEND_BITBANG: Use the emulated bitbang driver. 57 */ 58 #define CONFIG_I2C_BACKEND I2C_BACKEND_BUILTIN 59 53 60 /// Module logging level definition. 54 61 #define I2C_LOG_LEVEL LOG_LVL_INFO trunk/bertos/cpu/avr/drv/i2c_avr.c
r1879 r1926 68 68 * \return true on success, false otherwise. 69 69 */ 70 static bool i2c_ start(void)70 static bool i2c_builtin_start(void) 71 71 { 72 72 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN); … … 88 88 * \return true on success, false otherwise. 89 89 */ 90 bool i2c_ start_w(uint8_t id)90 bool i2c_builtin_start_w(uint8_t id) 91 91 { 92 92 /* … … 97 97 */ 98 98 ticks_t start = timer_clock(); 99 while (i2c_ start())99 while (i2c_builtin_start()) 100 100 { 101 101 TWDR = id & ~I2C_READBIT; … … 128 128 * \return true on success, false otherwise. 129 129 */ 130 bool i2c_ start_r(uint8_t id)131 { 132 if (i2c_ start())130 bool i2c_builtin_start_r(uint8_t id) 131 { 132 if (i2c_builtin_start()) 133 133 { 134 134 TWDR = id | I2C_READBIT; … … 149 149 * Send STOP condition. 150 150 */ 151 void i2c_ stop(void)151 void i2c_builtin_stop(void) 152 152 { 153 153 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO); … … 161 161 * \return true on success, false on error. 162 162 */ 163 bool i2c_ put(const uint8_t data)163 bool i2c_builtin_put(const uint8_t data) 164 164 { 165 165 TWDR = data; … … 182 182 * \return the byte read if ok, EOF on errors. 183 183 */ 184 int i2c_ get(bool ack)184 int i2c_builtin_get(bool ack) 185 185 { 186 186 TWCR = BV(TWINT) | BV(TWEN) | (ack ? BV(TWEA) : 0); … … 213 213 * Initialize TWI module. 214 214 */ 215 void i2c_ init(void)215 void i2c_builtin_init(void) 216 216 { 217 217 ATOMIC( trunk/bertos/drv/i2c.h
r1875 r1926 39 39 #define DRV_I2C_H 40 40 41 #include "cfg/cfg_i2c.h" 41 42 #include <cfg/compiler.h> 42 43 43 44 #define I2C_READBIT BV(0) 44 45 45 void i2c_init(void); 46 bool i2c_start_w(uint8_t id); 47 bool i2c_start_r(uint8_t id); 48 void i2c_stop(void); 49 bool i2c_put(uint8_t _data); 50 int i2c_get(bool ack); 46 /** 47 * I2C Backends. 48 * \{ 49 */ 50 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver 51 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver 52 /*\}*/ 53 54 void i2c_builtin_init(void); 55 bool i2c_builtin_start_w(uint8_t id); 56 bool i2c_builtin_start_r(uint8_t id); 57 void i2c_builtin_stop(void); 58 bool i2c_builtin_put(uint8_t _data); 59 int i2c_builtin_get(bool ack); 60 61 void i2c_bitbang_init(void); 62 bool i2c_bitbang_start_w(uint8_t id); 63 bool i2c_bitbang_start_r(uint8_t id); 64 void i2c_bitbang_stop(void); 65 bool i2c_bitbang_put(uint8_t _data); 66 int i2c_bitbang_get(bool ack); 67 68 69 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN 70 #define i2c_init i2c_builtin_init 71 #define i2c_start_w i2c_builtin_start_w 72 #define i2c_start_r i2c_builtin_start_r 73 #define i2c_stop i2c_builtin_stop 74 #define i2c_put i2c_builtin_put 75 #define i2c_get i2c_builtin_get 76 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG 77 #define i2c_init i2c_bitbang_init 78 #define i2c_start_w i2c_bitbang_start_w 79 #define i2c_start_r i2c_bitbang_start_r 80 #define i2c_stop i2c_bitbang_stop 81 #define i2c_put i2c_bitbang_put 82 #define i2c_get i2c_bitbang_get 83 #else 84 #error Unsupported i2c backend. 85 #endif 86 51 87 bool i2c_send(const void *_buf, size_t count); 52 88 bool i2c_recv(void *_buf, size_t count); trunk/bertos/drv/i2c_bitbang.c
r1880 r1926 52 52 #include "hw/hw_i2c_bitbang.h" 53 53 54 INLINE bool i2c_ start(void)54 INLINE bool i2c_bitbang_start(void) 55 55 { 56 56 SDA_HI; … … 63 63 } 64 64 65 void i2c_ stop(void)65 void i2c_bitbang_stop(void) 66 66 { 67 67 SDA_LO; … … 71 71 } 72 72 73 bool i2c_ put(uint8_t _data)73 bool i2c_bitbang_put(uint8_t _data) 74 74 { 75 75 /* Add ACK bit */ … … 95 95 } 96 96 97 bool i2c_ start_w(uint8_t id)97 bool i2c_bitbang_start_w(uint8_t id) 98 98 { 99 99 id &= ~I2C_READBIT; … … 105 105 */ 106 106 ticks_t start = timer_clock(); 107 while (i2c_ start())107 while (i2c_bitbang_start()) 108 108 { 109 if (i2c_ put(id))109 if (i2c_bitbang_put(id)) 110 110 return true; 111 111 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT)) … … 120 120 } 121 121 122 bool i2c_ start_r(uint8_t id)122 bool i2c_bitbang_start_r(uint8_t id) 123 123 { 124 124 id |= I2C_READBIT; 125 if (i2c_ start())125 if (i2c_bitbang_start()) 126 126 { 127 if (i2c_ put(id))127 if (i2c_bitbang_put(id)) 128 128 return true; 129 129 … … 134 134 } 135 135 136 int i2c_ get(bool ack)136 int i2c_bitbang_get(bool ack) 137 137 { 138 138 uint8_t data = 0; … … 170 170 * Initialize i2c module. 171 171 */ 172 void i2c_ init(void)172 void i2c_bitbang_init(void) 173 173 { 174 174 MOD_CHECK(timer);
