-volatile i2cm_state g_i2cm_NUM_state = NOT_INIT;
-volatile uint8_t g_i2cm_NUM_byte;
+volatile i2cm_state g_i2cm_state = NOT_INIT;
+volatile uint8_t g_i2cm_byte;
-void (*g_i2cm_NUM_event)(i2cm_state state);
+void (*g_i2cm_event)(i2cm_state state);
/*
Mini Arduino
A5 = PC5/SCL
*/
-#define I2CM_NUM_SCL_PORT PORTC
-#define I2CM_NUM_SCL_BIT 5
+#define I2CM_SCL_PORT PORTC
+#define I2CM_SCL_BIT 5
-#define I2CM_NUM_SDA_PORT PORTC
-#define I2CM_NUM_SDA_BIT 4
+#define I2CM_SDA_PORT PORTC
+#define I2CM_SDA_BIT 4
-#define I2CM_NUM_SCL I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT
-#define I2CM_NUM_SDA I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT
+#define I2CM_SCL I2CM_SCL_PORT, I2CM_SCL_BIT
+#define I2CM_SDA I2CM_SDA_PORT, I2CM_SDA_BIT
-void i2cm_NUM_init(void)
+void i2cm_init(void)
{
/*
- port SCL/SDA is set to 0
*/
// SCL high
- cbi(DDR(I2CM_NUM_SCL_PORT),I2CM_NUM_SCL_BIT);
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
+ cbi(DDR(I2CM_SCL_PORT),I2CM_SCL_BIT);
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
// SDA high
- cbi(DDR(I2CM_NUM_SDA_PORT),I2CM_NUM_SDA_BIT);
- I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_DELAY();
+ cbi(DDR(I2CM_SDA_PORT),I2CM_SDA_BIT);
+ I2C_HIGH(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_DELAY();
- g_i2cm_NUM_state = I2CM_READY;
+ g_i2cm_state = I2CM_READY;
}
-void i2cm_NUM_manage(void)
+void i2cm_manage(void)
{
- if ( (g_i2cm_NUM_event != NULL) && (g_i2cm_NUM_state != I2CM_READY) ) {
- g_i2cm_NUM_event( g_i2cm_NUM_state );
+ if ( (g_i2cm_event != NULL) && (g_i2cm_state != I2CM_READY) ) {
+ g_i2cm_event( g_i2cm_state );
// reset notification
- if (g_i2cm_NUM_state != I2CM_BUSY)
- g_i2cm_NUM_state = I2CM_READY;
+ if (g_i2cm_state != I2CM_BUSY)
+ g_i2cm_state = I2CM_READY;
}
}
-uint8_t i2cm_NUM_get_state(void)
+uint8_t i2cm_get_state(void)
{
- return g_i2cm_NUM_state;
+ return g_i2cm_state;
}
-uint8_t i2cm_NUM_get_received_byte(void)
+uint8_t i2cm_get_received_byte(void)
{
- g_i2cm_NUM_state = I2CM_READY;
- return g_i2cm_NUM_byte;
+ g_i2cm_state = I2CM_READY;
+ return g_i2cm_byte;
}
-void i2cm_NUM_register_event(void (*func)(i2cm_state state))
+void i2cm_register_event(void (*func)(i2cm_state state))
{
uint8_t flags;
IRQ_LOCK(flags);
- g_i2cm_NUM_event = func;
+ g_i2cm_event = func;
IRQ_UNLOCK(flags);
}
-uint8_t i2cm_NUM_send_byte(uint8_t byte)
+uint8_t i2cm_send_byte(uint8_t byte)
{
uint8_t mask;
uint8_t err = 0;
- g_i2cm_NUM_state = I2CM_BUSY;
+ g_i2cm_state = I2CM_BUSY;
// SCL should already be low
- // I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- // I2CM_NUM_DELAY();
+ // I2C_LOW(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ // I2CM_DELAY();
mask = 0x80;
while (mask) {
// data out
if (mask & byte)
- I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
+ I2C_HIGH(I2CM_SDA_PORT, I2CM_SDA_BIT);
else
- I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_DELAY();
+ I2C_LOW(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_DELAY();
mask >>=1;
// delay needed ?
- //I2CM_NUM_DELAY();
+ //I2CM_DELAY();
// clock High
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
- while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
+ while ( bit_is_clear(PIN(I2CM_SCL_PORT), I2CM_SCL_BIT) );// slave handshake
// clock low
- I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
+ I2C_LOW(I2CM_SCL_PORT, I2CM_SCL_BIT);
}
/* receive ack */
// release SDA
- I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_BIT_DELAY();
+ I2C_HIGH(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_BIT_DELAY();
// clock HIGH
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
- while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
+ while ( bit_is_clear(PIN(I2CM_SCL_PORT), I2CM_SCL_BIT) );// slave handshake
// receive ACK
- if (bit_is_set(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT))
+ if (bit_is_set(I2CM_SDA_PORT, I2CM_SDA_BIT))
err = I2CM_SENT_NO_ACK;
// clock low
- I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
+ I2C_LOW(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
return err;
}
-uint8_t i2cm_NUM_send_start(uint8_t sla_w)
+uint8_t i2cm_send_start(uint8_t sla_w)
{
uint8_t err;
uint16_t i;
- g_i2cm_NUM_state = I2CM_BUSY;
+ g_i2cm_state = I2CM_BUSY;
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2C_HIGH(I2CM_SDA_PORT, I2CM_SDA_BIT);
- I2CM_NUM_DELAY();
+ I2CM_DELAY();
/* wait for bus realese */
for (i=0;i<0x8000; i++){
- if( bit_is_set(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) && \
- bit_is_set(PIN(I2CM_NUM_SDA_PORT), I2CM_NUM_SDA_BIT) )
+ if( bit_is_set(PIN(I2CM_SCL_PORT), I2CM_SCL_BIT) && \
+ bit_is_set(PIN(I2CM_SDA_PORT), I2CM_SDA_BIT) )
break;
}
- // while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
+ // while ( bit_is_clear(PIN(I2CM_SCL_PORT), I2CM_SCL_BIT) );// slave handshake
// start condition
- I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_DELAY();
- I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
+ I2C_LOW(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_DELAY();
+ I2C_LOW(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
- err = i2cm_NUM_send_byte(sla_w);
+ err = i2cm_send_byte(sla_w);
return err;
}
-uint8_t i2cm_NUM_send_stop(void)
+uint8_t i2cm_send_stop(void)
{
- g_i2cm_NUM_state = I2CM_BUSY;
+ g_i2cm_state = I2CM_BUSY;
// data down
- I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_DELAY();
+ I2C_LOW(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2C_LOW(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_DELAY();
// stop condition
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
- I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_DELAY();
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
+ I2C_HIGH(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_DELAY();
- g_i2cm_NUM_state = I2CM_SENT_STOP;
+ g_i2cm_state = I2CM_SENT_STOP;
return 0;
}
-uint8_t i2cm_NUM_receive_byte(uint8_t last)
+uint8_t i2cm_receive_byte(uint8_t last)
{
uint8_t mask,data;
- g_i2cm_NUM_state = I2CM_BUSY;
+ g_i2cm_state = I2CM_BUSY;
- I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
+ I2C_HIGH(I2CM_SDA_PORT, I2CM_SDA_BIT);
data = 0;
while (mask) {
// clock High
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
- while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
+ while ( bit_is_clear(PIN(I2CM_SCL_PORT), I2CM_SCL_BIT) );// slave handshake
- if (bit_is_set(PIN(I2CM_NUM_SDA_PORT),I2CM_NUM_SDA_BIT))
+ if (bit_is_set(PIN(I2CM_SDA_PORT),I2CM_SDA_BIT))
data |= mask;
// clock low
- I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
+ I2C_LOW(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
mask >>=1;
}
if (!last){
/* send ack */
- I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_BIT_DELAY();
+ I2C_LOW(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_BIT_DELAY();
}
// clock HIGH
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
- while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
+ while ( bit_is_clear(PIN(I2CM_SCL_PORT), I2CM_SCL_BIT) );// slave handshake
// clock low
- I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
- I2CM_NUM_DELAY();
+ I2C_LOW(I2CM_SCL_PORT, I2CM_SCL_BIT);
+ I2CM_DELAY();
if (!last) {
// release DATA
- I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
- I2CM_NUM_BIT_DELAY();
+ I2C_HIGH(I2CM_SDA_PORT, I2CM_SDA_BIT);
+ I2CM_BIT_DELAY();
}
- g_i2cm_NUM_byte = data;
- g_i2cm_NUM_state = I2CM_RECEIVED_BYTE;
+ g_i2cm_byte = data;
+ g_i2cm_state = I2CM_RECEIVED_BYTE;
- I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
+ I2C_HIGH(I2CM_SCL_PORT, I2CM_SCL_BIT);
return 0;
}
uint8_t i;
uint8_t err = 0;
- err = i2cm_NUM_send_start((addr<<1) | 0);
+ err = i2cm_send_start((addr<<1) | 0);
if (err)
return err;
for (i=0; i<len; i++) {
- err = i2cm_NUM_send_byte(data[i]);
+ err = i2cm_send_byte(data[i]);
if (err)
break;
}
- i2cm_NUM_send_stop();
+ i2cm_send_stop();
return err;
}
uint8_t i;
uint8_t err = 0;
- err = i2cm_NUM_send_start((addr<<1) | 1);
+ err = i2cm_send_start((addr<<1) | 1);
if (err)
return err;
- I2CM_NUM_DELAY();
+ I2CM_DELAY();
for (i=0; i<len; i++){
- err =i2cm_NUM_receive_byte(i == len-1);
- i2c_buf[i] = g_i2cm_NUM_byte;
+ err =i2cm_receive_byte(i == len-1);
+ i2c_buf[i] = g_i2cm_byte;
if (err)
break;
}
- i2cm_NUM_send_stop();
+ i2cm_send_stop();
return err;
}