Statistics
| Branch: | Revision:

root / toolbox / main.c @ 208a6fb2

History | View | Annotate | Download (2.67 KB)

1
#include <stdint.h>
2
#include <util/delay.h>
3
#include "mb.h"
4
#include "mbport.h"
5
#include "tooltron_mb.h"
6
#include "rfid.h"
7

    
8
static char coils;
9

    
10
static inline void set_coil(char coil, char bit) {
11
  coils |= (bit << coil);
12
}
13

    
14
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
15
    eMBRegisterMode mode) {
16

    
17
  if (addr >= N_COILS || n_coils >= N_COILS-addr) {
18
    return MB_ENOREG;
19
  }
20

    
21
  if (mode == MB_REG_WRITE) {
22

    
23
    switch (addr) {
24

    
25
      case MB_COIL_NEW:
26
        /* nop */
27
        reg_buf[0] >>= 1;
28
        n_coils--;
29
        if (n_coils == 0) {
30
          return MB_ENOERR;
31
        }
32

    
33
      case MB_COIL_EN:
34
        set_coil(MB_COIL_NEW, 0);
35
        set_coil(MB_COIL_EN, reg_buf[0] & 1);
36
        reg_buf[0] >>= 1;
37
        n_coils--;
38
        if (n_coils == 0) {
39
          return MB_ENOERR;
40
        }
41

    
42
      case MB_COIL_REQ_DIS:
43
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
44
        reg_buf[0] >>= 1;
45
        n_coils--;
46
        if (n_coils == 0) {
47
          return MB_ENOERR;
48
        }
49

    
50
      case MB_COIL_INIT:
51
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
52
        reg_buf[0] >>= 1;
53
        n_coils--;
54
        if (n_coils == 0) {
55
          return MB_ENOERR;
56
        }
57
    }
58

    
59
  } else if (mode == MB_REG_READ) {
60

    
61
    reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1);
62
    return MB_ENOERR;
63

    
64
  }
65

    
66
  return MB_EIO;
67
}
68

    
69
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) {
70
  return MB_ENOREG;
71
}
72

    
73
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
74
  char serno[RFID_SERNO_SIZE];
75

    
76
  rfid_get_serno(serno);
77

    
78
  switch (addr) {
79

    
80
    case MB_INP_SERNOL:
81
      // TODO check that these (and the ones in SERNOH) are in the right order
82
      *reg_buf++ = serno[0];
83
      *reg_buf++ = serno[1];
84
      n_regs--;
85
      if (n_regs == 0) {
86
        return MB_ENOERR;
87
      }
88

    
89
    case MB_INP_SERNOH:
90
      *reg_buf++ = serno[2];
91
      *reg_buf++ = serno[3];
92
      n_regs--;
93
      if (n_regs == 0) {
94
        return MB_ENOERR;
95
      }
96

    
97
    case MB_INP_CURRENT:
98
      *reg_buf++ = 0;
99
      *reg_buf++ = 0;
100
      n_regs--;
101
      if (n_regs == 0) {
102
        return MB_ENOERR;
103
      }
104

    
105
    default:
106
      return MB_ENOREG;
107
  }
108
}
109

    
110
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
111
    eMBRegisterMode mode) {
112
  if (mode == MB_REG_WRITE) {
113
    return MB_ENOREG;
114
  } else if (mode == MB_REG_READ) {
115
    return MB_ENOREG;
116
  } else {
117
    return MB_EIO;
118
  }
119
}
120

    
121
int main() {
122
  rfid_init();
123

    
124
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
125
  eMBEnable();
126

    
127
  /* Set pin controlling relay to output */
128
  DDRA |= _BV(DDA1);
129

    
130
  sei();
131

    
132
  while (1) {
133
    rfid_read();
134
    /* TODO act on rfid input and coil state */
135
    eMBPoll();
136
    _delay_ms(200);
137
  }
138

    
139
  return 0;
140
}