-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice_vn.c
104 lines (82 loc) · 2.65 KB
/
dice_vn.c
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
//
// Raspidapter library
//
// dice VN implementation
//
// Copyright (C) Dominik Wenger 2015
// No rights reserved
// You may treat this program as if it was in the public domain
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "raspidapter_common.h"
#include "dice_vn.h"
#define PTR_INPUT_REG 0
#define PTR_OUTPUT_REG 1
#define PTR_POL_REG 2
#define PTR_CONFIG_REG 3
#define DEV_BASE_ADDR 0x70
int dice_vn_setup(struct DICE* dice,int board, int slot,int number)
{
//error checking
if(dice == NULL)
return ERR_PARAM;
if(board == 0)
return ERR_PARAM;
if(slot <1 || slot > 4)
return ERR_PARAM;
if(number <1 && number > 4)
return ERR_PARAM;
//set type
dice->type = DICE_VN;
//calc bit numbers
dice->enable = (board-1)*32 + (slot-1)*8 + 0;
dice->ms1 = (board-1)*32 + (slot-1)*8 + 4;
dice->ms2 = (board-1)*32 + (slot-1)*8 + 5;
dice->ms3 = (board-1)*32 + (slot-1)*8 + 1;
dice->rs = (board-1)*32 + (slot-1)*8 + 2;
dice->dir = (board-1)*32 + (slot-1)*8 + 3;
dice->step = (board-1)*32 + (slot-1)*8 + 6;
dice->slp = (board-1)*32 + (slot-1)*8 + 7;
// set address bits
if( (number-1) & (1<<0))
{
iochain_setbit(dice->ms1);
}
else iochain_clearbit(dice->ms1);
if( (number-1) & (1<<1))
{
iochain_setbit(dice->ms2);
}
else iochain_clearbit(dice->ms2);
iochain_update();
//store address
dice->i2c_addr = DEV_BASE_ADDR | (number -1);
return 0;
}
int dice_vn_setoutput(struct DICE* dice,int pins)
{
return write_i2c(dice->i2c_addr,PTR_CONFIG_REG,1,(char*) &pins);
}
int dice_vn_setpolarity(struct DICE* dice,int pins)
{
return write_i2c(dice->i2c_addr,PTR_POL_REG,1,(char*) &pins);
}
int dice_vn_set(struct DICE* dice, int pins)
{
return write_i2c(dice->i2c_addr,PTR_OUTPUT_REG,1,(char*) &pins);
}
int dice_vn_read(struct DICE* dice, int pins)
{
return read_i2c(dice->i2c_addr,PTR_INPUT_REG,1,(char*) &pins);
}