summaryrefslogtreecommitdiff
path: root/digital/avr/modules/twi/twi.h
blob: 4ce3e25e48635de06ca8c6d1fea43eaed480353c (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
#ifndef twi_h
#define twi_h
/* twi.h */
/* avr.twi - TWI AVR module. {{{
 *
 * Copyright (C) 2010 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

#if !AC_TWI_SLAVE_ENABLE && !AC_TWI_MASTER_ENABLE
# error "twi: no enabled mode"
#endif

/** Initialise module, configure slave address.  Slave address is ignored if
 * slave mode is disabled. */
void
twi_init (uint8_t addr);

/** Uninitialise module, will no longer respond to bus events. */
void
twi_uninit (void);

#if AC_TWI_SLAVE_ENABLE

# if AC_TWI_SLAVE_POLLED
#  ifdef AC_TWI_SLAVE_RECV
#   error "twi: AC_TWI_SLAVE_RECV should not be set in polled mode"
#  endif
#  define AC_TWI_SLAVE_RECV twi_slave_recv_polled
# endif

/** Called on data reception from master.
 *
 * May run under interrupt context.  Bus may be held until this function
 * returns, so it has to operate quickly. */
void
AC_TWI_SLAVE_RECV (const uint8_t *buffer, uint8_t size);

/** Update buffer to be sent to master if requested. */
void
twi_slave_update (const uint8_t *buffer, uint8_t size);

# if AC_TWI_SLAVE_POLLED

/** If new data has been received, copy received data to provided buffer and
 * return received data size.  Else, return 0. */
uint8_t
twi_slave_poll (uint8_t *buffer, uint8_t size);

# endif

#endif /* AC_TWI_SLAVE_ENABLE */

#if AC_TWI_MASTER_ENABLE

/** Send data to a slave.  This is non blocking, you have to poll master
 * status or use a callback to determine if transfer is terminated.
 *
 * Input buffer is not copied, it should be kept valid until transmission is
 * finished. */
void
twi_master_send (uint8_t addr, const uint8_t *buffer, uint8_t size);

/** Receive data from a slave.  This is non blocking, you have to poll master
 * status or use a callback to determine if transfer is terminated.
 *
 * Input buffer is directly written, and should be kept valid until reception
 * is finished. */
void
twi_master_recv (uint8_t addr, uint8_t *buffer, uint8_t size);

/** Master status, any other values are the number of bytes transferred. */
enum
{
    /** Busy transferring data. */
    TWI_MASTER_BUSY = 0xff,
    /** Last transfer finished badly, ready to process new transfer.
     * Among errors: no acknowledge, bad CRC (if activated), arbitration
     * lost... */
    TWI_MASTER_ERROR = 0,
};

/** Get current master status. */
uint8_t
twi_master_status (void);

/** Wait current master transfer completion and return new status. */
uint8_t
twi_master_wait (void);

# ifdef AC_TWI_MASTER_DONE

/** Called on master transfer completion.
 *
 * May run under interrupt context. */
void
AC_TWI_MASTER_DONE (void);

# endif

#endif /* AC_TWI_MASTER_ENABLE */

#if AC_TWI_NO_INTERRUPT

/** To be call on regular basis, poll events to simulate interrupts. */
void
twi_update (void);

#endif /* AC_TWI_NO_INTERRUPT */

/* Define selected driver. */
#define TWI_DRIVER_HARD 'h'
#define TWI_DRIVER_SOFT 's'
#define TWI_DRIVER_USI 'u'
#define TWI_DRIVER__(drv) TWI_DRIVER_ ## drv
#define TWI_DRIVER_(drv) TWI_DRIVER__ (drv)
#define TWI_DRIVER TWI_DRIVER_ (AC_TWI_DRIVER)

#endif /* twi_h */