summaryrefslogtreecommitdiffstats
path: root/humidity_sensors/app/i2c_bb.c
blob: 7d0d8077121a49ba41e31c3f715492a6113d9ca5 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "project.h"


#define SCL ((u8) 1 <<4)
#define SDA ((u8) 1 <<5)
#define BANK GPIOB


static u8
set (u8 sda, u8 scl)
{
  u8 ret;

  if (sda)
    GPIO_WriteHigh (BANK, SDA);
  else
    GPIO_WriteLow (BANK, SDA);

  if (scl)
    GPIO_WriteHigh (BANK, SCL);
  else
    GPIO_WriteLow (BANK, SCL);

  delay (4);
  //delay_us (10);
  ret = (GPIO_ReadInputData (BANK) & SDA) ? 1 : 0;

  return ret;
}




u8
i2cb_send (u8 wot)
{
  u8 i;

  for (i = 0; i < 8; ++i) {
    set (wot & 0x80, 0);
    set (wot & 0x80, 1);
    set (wot & 0x80, 0);
    wot <<= 1;
  }

  set (1, 0);
  i = set (1, 1);
  set (1, 0);

  return i;
}

u8
i2cb_send_addr (u8 addr, u8 rnw)
{
  return i2cb_send (addr << 1 | rnw);
}

int
i2cb_send_data (u8 d)
{
  return i2cb_send (d);
}

u8
i2cb_read (u8 ack)
{
  u8 i, wot = 0;

  for (i = 0; i < 8; ++i) {
    wot <<= 1;
    set (1, 0);
    wot |= set (1, 1);
    set (1, 0);
  }

  set (ack, 0);
  set (ack, 1);
  set (ack, 0);
  return wot;
}


void
i2cb_start (void)
{
  set (1, 1);
  set (0, 1);
  set (0, 0);
}

void
i2cb_stop (void)
{
  set (0, 0);
  set (0, 1);
  set (1, 1);
}


int
i2cb_start_transaction (u8 a, u8 rnw)
{
  i2cb_start();
  return i2cb_send_addr (a, rnw);

}


void
i2cb_reset (void)
{
  i2cb_start();
  i2cb_stop();
  i2cb_start();
  i2cb_stop();
}



#if 0
void
i2cb_scan (void)
{
  u8 r;
  u16 i;

  i2cb_reset();

  for (i = 0; i < 128; ++i) {
    i2cb_start();
    i2cb_stop();
    i2cb_start();
    r = i2cb_send ((i << 1) | I2C_WRITE);
    i2cb_stop();

    if (!r)
      printf ("target found at %02x\n", i);

  }

  i2cb_reset();

}
#endif

void
i2cb_init (void)
{
  GPIO_Init (BANK, SDA, GPIO_MODE_OUT_OD_HIZ_SLOW);
  GPIO_Init (BANK, SCL, GPIO_MODE_OUT_OD_HIZ_SLOW);

  set (1, 1);
  set (0, 1);
  set (1, 0);
  set (1, 1);
}