china-io-test.c 5.99 KB
Newer Older
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
#include <avr/interrupt.h>
#include <avr/io.h>
#include <inttypes.h>
#include <stdio.h>
#include "i2c_master.h"

#define DDR_SPI DDRB
#define DD_MISO PB6
#define DD_MOSI PB5
#define DD_SCK PB7
#define DD_SS PB4
#define PORT_SPI PORTB

static void putch(unsigned char ch) 
{
  
  while ((UCSRA & 0x20) == 0) {};
  UDR =ch;
}

static void putonehex(char i) {
  if (i < 10) {
    putch(i + '0');
  } else if (i < 16) {
    putch(i - 10 + 'a');
  }
}

static void puthex(char value) {
  putonehex((value >> 4) & 0x0f);
  putonehex(value & 0x0f);
}

void putstr(char *message)
{
  for (int i = 0 ; message[i] ; i++) {
    putch(message[i]);
  }
}

void putint32(int32_t value)
{
  char buf[20];
  snprintf(buf, sizeof(buf), "%8ld", value);
  putstr(buf);
}

void putint16(int16_t value)
{
  char buf[20];
  snprintf(buf, sizeof(buf), "%8d", value);
  putstr(buf);
}


static uint8_t ADS8694_W(
  uint8_t length,
  const __flash uint8_t *data)
{
  DDR_SPI |= (1<<DD_MOSI) | (1<<DD_SCK) | (1<<DD_SS);
  SPCR &= (1<<SPIE);
  SPCR = (1<<SPE) | (1<<MSTR) | (1<<CPHA);
  SPSR = (1<<SPI2X);
  PORT_SPI |= (1<<DD_MISO); // Pull up
  
  PORT_SPI &= ~(1<<DD_SS);
  for (uint8_t i = 0 ; i < length ; i++) {
    SPDR = data[i];
    while ((SPSR & (1<<SPIF)) == 0) {}
  }
  uint8_t dummy = SPDR; // Clear SPIF flag
  PORT_SPI |= (1<<DD_SS);
  return dummy;
}

/*
 * DAC8574 4-channel D/A convert
 */

#define DAC8574ADDR 0x4c

static volatile struct dac8574 {
  union dac8574_channel {
    uint8_t bytes[3];
    struct {
      uint8_t control;
      uint8_t data[2];
    } reg;
  } a, b, c, d;
} dac8574 = {
  .a={ .reg={ .control=0x10 } },
  .b={ .reg={ .control=0x12 } },
  .c={ .reg={ .control=0x14 } },
  .d={ .reg={ .control=0x16 } },
};

static void dac8574_set_chan(
  volatile union dac8574_channel *channel,
  int value)
{
  channel->reg.data[0] = (value & 0xff00) >> 8;
  channel->reg.data[1] = (value & 0x00ff);
}

static const __flash struct i2c_transcation write_all[] = {
  I2C_WRITE_RAM(DAC8574ADDR, 3, dac8574.a.bytes),
  I2C_WRITE_RAM(DAC8574ADDR, 3, dac8574.b.bytes),
  I2C_WRITE_RAM(DAC8574ADDR, 3, dac8574.c.bytes),
  I2C_WRITE_RAM(DAC8574ADDR, 3, dac8574.d.bytes),
  I2C_END()
};

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

static volatile struct dac8574_check {
  struct dac8574_check_chan {
    uint8_t no_pdo[2];
    uint8_t pdo[3];
  } chan[4];
} dac8574_check;

static const __flash struct i2c_transcation read_all[] = {
  I2C_WRITE_1(DAC8574ADDR, 0x10),
  I2C_READ(DAC8574ADDR, 2, dac8574_check.chan[0].no_pdo),
  I2C_WRITE_1(DAC8574ADDR, 0x11),
  I2C_READ(DAC8574ADDR, 3, dac8574_check.chan[0].pdo),
  I2C_WRITE_1(DAC8574ADDR, 0x12),
  I2C_READ(DAC8574ADDR, 2, dac8574_check.chan[1].no_pdo),
  I2C_WRITE_1(DAC8574ADDR, 0x13),
  I2C_READ(DAC8574ADDR, 3, dac8574_check.chan[1].pdo),
  I2C_WRITE_1(DAC8574ADDR, 0x14),
  I2C_READ(DAC8574ADDR, 2, dac8574_check.chan[2].no_pdo),
  I2C_WRITE_1(DAC8574ADDR, 0x15),
  I2C_READ(DAC8574ADDR, 3, dac8574_check.chan[2].pdo),
  I2C_WRITE_1(DAC8574ADDR, 0x16),
  I2C_READ(DAC8574ADDR, 2, dac8574_check.chan[3].no_pdo),
  I2C_WRITE_1(DAC8574ADDR, 0x17),
  I2C_READ(DAC8574ADDR, 3, dac8574_check.chan[3].pdo),
  I2C_CHAIN(write_all),
  I2C_END()
};

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
static volatile int32_t ADS8694_value[4] = { 0, 0, 0, 0};

SIGNAL(TIMER1_COMPA_vect)
{
  unsigned char data[5];

  SPCR = (1<<SPE) | (1<<MSTR) | (1<< CPHA);
  PORT_SPI &= ~(1<<DD_SS); // Chip select
  SPDR = 0x00; // NOP
  for (int i = 0 ; i < 5 ; i++) {
    while ((SPSR & 0x80) == 0) { }
    data[i] = SPDR;
    if (i < 4) {
      // More to read
      SPDR = 0;
    }
  }
  PORT_SPI |= (1<<DD_SS); // Chip deselect

  union {
    int32_t i;
    unsigned char b[4];
  } v;
  uint8_t chan;
  v.i = 0;
  v.b[2] = data[2];
  v.b[1] = data[3];
  v.b[0] = data[4];
  chan = (v.i>>2) & 0xf;
  ADS8694_value[chan] = (v.i>>6) - 0x20000;
}

static int16_t v = 0;
SIGNAL(USART_RXC_vect)
{
  char ch = UDR;
  
  puthex(ch);
  putstr("RX Status:");
  puthex(TWSR);
  puthex(TWCR);
  if (ch == '+') {
    v += 1024;
  } else if (ch == '-') {
    v -= 1024;
  }
  putstr("\r\n");
}

int main()
{
  /* Setup serial port */
  UCSRA = 0x00;     // USART: 
  UCSRB = 0x98;     // USART: RxIntEnable|RxEnable|TxEnable 
  UCSRC = 0x86;     // USART: 8bit, no parity 
  UBRRH = 0;        // USART: 115200 @ 14.7456MHz
  UBRRL = 7;        // USART: 115200 @ 14.7456MHz 

  /* Setup timer1 */
  TCCR1A = 0x00;    // Normal port mode (no output)
  TCCR1B = 0x09;    // Clock / 1
  OCR1A = 0x3fff;
  TIMSK =  0x10;    // Enable Timer1 compare A interrupts 

  /* Setup I2C (TWI), used by DAC8574 */
  TWBR = 0x10;
  TWSR = 0x00;

  /* Setup DAC8574 (SPI setup is done in ADS8694_W) */
  static const __flash uint8_t RST[] = { 0x85, 0x00, 0x00 };
  static const __flash uint8_t SEQUENCE[] = { (0x01<<1)|0x01, 0x0f, 0x00 };
  static const __flash uint8_t FEATURES[] = { (0x03<<1)|0x01, 0x03, 0x00 };
  static const __flash uint8_t AUTO_RST[] = { 0xa0, 0x00, 0x00, 0x00, 0x00 };
  ADS8694_W(3, RST);
  ADS8694_W(3, SEQUENCE);
  ADS8694_W(3, FEATURES);
  ADS8694_W(5, AUTO_RST);

  sei();      // Global interrupt enable
  
  putstr("\r\n\r\n");
  i2c_start(write_all);

  while (1) {
    struct dac8574 tmp;
    dac8574_set_chan(&tmp.a, 0x8000);
    dac8574_set_chan(&tmp.b, 0x8000);
    dac8574_set_chan(&tmp.c, v+0x0000);
    dac8574_set_chan(&tmp.d, v+0x8000);
//    v = (v + 1024) & 0xffff;
    putstr("i2c status=");
    putint16(i2c_ctx.status);
    putstr(" v=");
    putint16(v);
    putint16(v+0x8000);
    for (int i = 0 ; i < 4 ; i++) {
      putint32(ADS8694_value[i]);
    }
    putstr("\r");
    if (i2c_idle()) {
242
243
244
245
246
247
248
249
250
251
252
      putstr("\r\n");
      for (uint8_t i = 0 ; i < 4 ; i++) {
        for (int j = 0 ; j < 2 ; j++) {
          puthex(dac8574_check.chan[i].no_pdo[j]);
        }
        putch(' ');
        for (int j = 0 ; j < 3 ; j++) {
          puthex(dac8574_check.chan[i].pdo[j]);
        }
        putch(',');
      }
253
254
255
256
257
      cli();
      *dac8574.a.reg.data = *tmp.a.reg.data;
      *dac8574.b.reg.data = *tmp.b.reg.data;
      *dac8574.c.reg.data = *tmp.c.reg.data;
      *dac8574.d.reg.data = *tmp.d.reg.data;
258
      i2c_start(read_all);
259
260
261
262
263
      sei();
      putstr("\r\n");
    }
  }
}