OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [software/] [lib/] [mcp23s17.c] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
/* file:          mcp23s17.c
2
 * description:   Microchip mcp23s17 chip driver
3
 * date:          09/2015
4
 * author:        Sergio Johann Filho <sergio.filho@pucrs.br>
5
 */
6
 
7
#include <hf-risc.h>
8
#include <mcp23s17.h>
9
 
10
void mcp23s17_dir(uint8_t device, uint8_t bank, uint8_t reg){
11
        spi_start();
12
        spi_sendrecv(MCP23x17_ADDR | ((device & 0x07) << 1));
13
        if (bank)
14
                spi_sendrecv(MCP23x17_IODIRB);
15
        else
16
                spi_sendrecv(MCP23x17_IODIRA);
17
        spi_sendrecv(reg);
18
        spi_stop();
19
}
20
 
21
uint8_t mcp23s17_read(uint8_t device, uint8_t bank){
22
        uint8_t reg;
23
 
24
        spi_start();
25
        spi_sendrecv(MCP23x17_ADDR | ((device & 0x07) << 1) | 0x01);
26
        if (bank)
27
                spi_sendrecv(MCP23x17_GPIOB);
28
        else
29
                spi_sendrecv(MCP23x17_GPIOA);
30
        reg = spi_sendrecv(0);
31
        spi_stop();
32
 
33
        return reg;
34
}
35
 
36
void mcp23s17_write(uint8_t device, uint8_t bank, uint8_t reg){
37
        spi_start();
38
        spi_sendrecv(MCP23x17_ADDR | ((device & 0x07) << 1));
39
        if (bank)
40
                spi_sendrecv(MCP23x17_GPIOB);
41
        else
42
                spi_sendrecv(MCP23x17_GPIOA);
43
        spi_sendrecv(reg);
44
        spi_stop();
45
}
46
 
47
void mcp23s17_inten(uint8_t device, uint8_t bank, uint8_t reg){
48
        spi_start();
49
        spi_sendrecv(MCP23x17_ADDR | ((device & 0x07) << 1));
50
        if (bank)
51
                spi_sendrecv(MCP23x17_GPINTENB);
52
        else
53
                spi_sendrecv(MCP23x17_GPINTENA);
54
        spi_sendrecv(reg);
55
        spi_stop();
56
}
57
 
58
void mcp23s17_init(uint8_t device, uint8_t portdir_a, uint8_t portdir_b){
59
        uint8_t byte;
60
 
61
        spi_setup();
62
 
63
        // configure device, enable device addressing
64
        spi_start();
65
        spi_sendrecv(MCP23x17_ADDR | ((device & 0x07) << 1));
66
        spi_sendrecv(MCP23x17_IOCON);
67
        spi_sendrecv(0x00
68
                        | (0 << MCP23x17_IOCON_BANK)
69
                        | (0 << MCP23x17_IOCON_MIRROR)
70
                        | (0 << MCP23x17_IOCON_SEQOP)
71
                        | (0 << MCP23x17_IOCON_DISSLW)
72
                        | (1 << MCP23x17_IOCON_HAEN)
73
                        | (0 << MCP23x17_IOCON_ODR)
74
                        | (1 << MCP23x17_IOCON_INTPOL)
75
        );
76
        spi_stop();
77
 
78
        // set pin direction on both ports
79
        mcp23s17_dir(device, 0, portdir_a);
80
        mcp23s17_dir(device, 1, portdir_b);
81
        mcp23s17_write(device, 0, 0x00);
82
        mcp23s17_write(device, 1, 0x00);
83
 
84
        // enable weak internal pull-ups on both ports
85
        spi_start();
86
        spi_sendrecv(MCP23x17_ADDR | ((device & 0x07) << 1));
87
        spi_sendrecv(MCP23x17_GPPUA);
88
        spi_sendrecv(0xff);
89
        spi_sendrecv(0xff);
90
        spi_stop();
91
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.