summaryrefslogtreecommitdiff
path: root/src/arch/msp430fr5994lp/driver/spi.cc
blob: 2ef84bf44c455a23297bd6161880c2eb69cf0a96 (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
/*
 * Copyright 2020 Birte Kristina Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#include "driver/spi.h"
#include <msp430.h>

#ifndef F_I2C
#define F_I2C 1000000UL
#endif

void SPI::setup()
{
	UCB1CTLW0 |= UCSWRST;

	/* UCB1CLK Pin 5.2 */
	P5SEL1 &= ~BIT2;
	P5SEL0 |= BIT2;
	P5DIR |= BIT2;

	/* UCB1SIMO Pin 5.0 */
	P5SEL1 &= ~BIT0;
	P5SEL0 |= BIT0;
	P5DIR |= BIT0;

	/* UCB1SOMI Pin 5.1 */
	P5SEL1 &= ~BIT1;
	P5SEL0 |= BIT1;
	P5DIR &= ~BIT1;
	//P1REN |= BIT6;

	UCB1CTLW0 = UCCKPH | UCMSB | UCMST | UCSYNC | UCMODE_0 | UCSSEL__SMCLK | UCSWRST;
	UCB1BRW = (F_CPU/F_I2C)-1; // /16 -> 1MHz
	// UCB1BRW = (F_CPU / F_I2C) - 1
	UCB1CTLW0 &= ~UCSWRST;
}

static inline unsigned char clean_rxbuf()
{
	return UCB1RXBUF;
}

signed char SPI::xmit(unsigned int tx_len, unsigned char *tx_buf,
		unsigned int rx_len, unsigned char *rx_buf)
{
	if (tx_len < 1) {
		return -1;
	}

	while (UCB1STATW & UCBUSY) ;

	if (!(UCB1IFG & UCTXIFG)) {
		return -1;
	}

	UCB1IFG &= ~UCRXIFG;
	UCB1TXBUF = tx_buf[0];

	unsigned int tx_pos = 1;
	unsigned int rx_pos = 0;

	while (tx_pos < tx_len || rx_pos < rx_len) {
		if ((tx_pos < tx_len) && (UCB1IFG & UCTXIFG)) {
			UCB1TXBUF = tx_buf[tx_pos++];
		}
		if (UCB1IFG & UCRXIFG) {
			if (rx_pos < rx_len) {
				rx_buf[rx_pos] = UCB1RXBUF;
			} else {
				UCB1IFG &= ~UCRXIFG;
			}
			rx_pos++;
		}
	}
	while (UCB1STATW & UCBUSY) ;
	return 0;
}

SPI spi;