summaryrefslogtreecommitdiffstats
path: root/source/AVR_Studio/Soccer/hal/i2c.h
blob: e983bf2150c354e7e8b7c07b4b92c8f81e1a2e78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef _IC2_H_
#define _I2C_H_

//------------------------------------------------------------------
// qfixI2C.h
//
// This class is used for low-level I2C communication.
//
// For TW_ constants see compat/twi.h
//
// Copyright 2005-2006 by KTB mechatronics GmbH
// Author: Stefan Enderle, Florian Schrapp
//------------------------------------------------------------------



#include "../global.h"
#include <compat/twi.h>

const int ACTION_SEND = 1;
const int ACTION_GET  = 2;
const int ACTION_UNKNOWN  = 3;


const uint8_t ERROR_NO_ACK   = 1;
const uint8_t ERROR_NO_START = 2;
const uint8_t ERROR_NOT_SENT = 3;     // send() could not send byte(s)



class I2C
{
private:
  uint8_t err;
  uint8_t adr;

  void sendStartSLA_W(uint8_t address);
  void sendStartSLA_R(uint8_t address);
  void sendByte(uint8_t data);
  void sendStop();
  void readByte(uint8_t& data);

public:
  I2C();
  uint8_t error();

// master side //

  void send(uint8_t address, uint8_t data);
  void send(uint8_t address, uint8_t* data, int length);
  void get(uint8_t address, uint8_t* data, int length);

// slave side //

  void setSlaveAdress(uint8_t address);
  bool isAction();
  bool isActionSend();  // true if master sent something
  bool isActionGet();   // true if master wants to get something
  int receive(uint8_t* data); // if action is send, receive the bytes
  void returnBytes(uint8_t* data, int len, bool lastOne);
};






#endif