summaryrefslogtreecommitdiff
path: root/src/arch/msp430fr5994lp/driver/i2c.cc
blob: 57b4c30d2090ed4bd477ac15e68933f67595d1b8 (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
#include "driver/i2c.h"
#include "arch.h"
#include <msp430.h>

#ifndef F_I2C
#define F_I2C 100000
#endif

volatile unsigned short old_ifg = 0;

#if (F_CPU / F_I2C) < 45
inline void await_i2c_int(unsigned int ie_flags, unsigned int ifg_flags) {
	while (!(UCB1IFG & ifg_flags)) ;
	if (UCB1IFG & (UCNACKIFG | UCCLTOIFG)) {
		UCB1IFG &= ~(UCNACKIFG | UCCLTOIFG);
	}
}
#else
inline void await_i2c_int(unsigned int ie_flags, unsigned int ifg_flags)
{
	UCB1IFG = 0;
	old_ifg = 0;
	UCB1IE = ie_flags;
	do {
		arch.idle();
	} while (!(old_ifg & ifg_flags));
	UCB1IE = 0;
}
#endif

signed char I2C::setup()
{
#ifdef I2C_PULLUP_FIXED_GPIO
	P8DIR |= BIT2 | BIT3;
	P8OUT |= BIT2 | BIT3;
#endif
	UCB1CTL1 = UCSWRST;
	UCB1CTLW0 = UCMODE_3 | UCMST | UCSYNC | UCSSEL_2 | UCSWRST | UCCLTO_1;
	UCB1BRW = (F_CPU / F_I2C) - 1;
	P5DIR &= ~(BIT0 | BIT1);
	P5SEL0 |= (BIT0 | BIT1);
	P5SEL1 &= ~(BIT0 | BIT1);

	UCB1CTL1 &= ~UCSWRST;
	UCB1I2CSA = 0;

	arch.delay_us(100);

	if (UCB1STAT & UCBBUSY)
		return -1;

	return 0;
}

void I2C::scan(unsigned int *results)
{
	for (unsigned char address = 0; address < 128; address++) {
		UCB1I2CSA = address;
		UCB1CTL1 |= UCTR | UCTXSTT | UCTXSTP;

		while (UCB1CTL1 & UCTXSTP);

		if (UCB1IFG & UCNACKIFG) {
			UCB1IFG &= ~UCNACKIFG;
		} else {
			results[address / (8 * sizeof(unsigned int))] |= 1 << (address % (8 * sizeof(unsigned int)));
		}
	}
	UCB1IFG = 0;
}

signed char I2C::xmit(unsigned char address,
		unsigned char tx_len, unsigned char *tx_buf,
		unsigned char rx_len, unsigned char *rx_buf)
{
	unsigned char i;
	UCB1I2CSA = address;
	if (tx_len) {
		UCB1CTL1 |= UCTR | UCTXSTT;
		for (i = 0; i < tx_len; i++) {
			await_i2c_int(UCTXIE0 | UCNACKIE | UCCLTOIE, UCTXIFG0 | UCNACKIFG | UCCLTOIFG);
			if (old_ifg & (UCNACKIFG | UCCLTOIFG)) {
				UCB1CTL1 |= UCTXSTP;
				return -1;
			}
			old_ifg = 0;
			UCB1TXBUF = tx_buf[i];
		}
		await_i2c_int(UCTXIE0 | UCNACKIE | UCCLTOIE, UCTXIFG0 | UCNACKIFG | UCCLTOIFG);
		//if (UCB1IFG & (UCNACKIFG | UCCLTOIFG)) {
		//	UCB1IFG &= ~UCNACKIFG;
		//	UCB1IFG &= ~UCCLTOIFG;
		//	UCB1CTL1 |= UCTXSTP;
		//	return -1;
		//}
	}
	if (rx_len) {
		UCB1I2CSA = address;
		UCB1IFG = 0;
		UCB1CTL1 &= ~UCTR;
		UCB1CTL1 |= UCTXSTT;

		while (UCB1CTL1 & UCTXSTT);
		UCB1IFG &= ~UCTXIFG0;

		for (i = 0; i < rx_len; i++) {
			if (i == rx_len - 1)
				UCB1CTL1 |= UCTXSTP;
			await_i2c_int(UCRXIE | UCNACKIE | UCCLTOIE, UCRXIFG0 | UCNACKIFG | UCCLTOIFG);
			rx_buf[i] = UCB1RXBUF;
			UCB1IFG &= ~UCRXIFG0;
		}
		UCB1IFG &= ~UCRXIFG0;
	}

	UCB1CTL1 |= UCTXSTP;

	while (UCB1CTL1 & UCTXSTP);
	return 0;
}

__attribute__((interrupt(USCI_B1_VECTOR))) __attribute__((wakeup)) void handle_usci_b1()
{
	old_ifg = UCB1IFG;
	UCB1IFG = 0;
}

I2C i2c;