aboutsummaryrefslogtreecommitdiffstats
path: root/fw/spi.c
blob: 1793c4dae7456a2e71b4f3970b42f1a971e5a355 (plain) (blame)
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
#include <xc.h>

#include <stdint.h>

#include "sys.h"
#include "types.h"

#include "spi.h"

void
spiInit(void) {
	U8 junk;

	TRISBbits.TRISB4 = IN; // SDI
	TRISCbits.TRISC7 = OUT; // SDO
	TRISBbits.TRISB6 = OUT; // SCK

	SSPSTAT = 0x00;
	SSPCON1 = 0x01; // FOSC/16 => 3MHz SPI clock
	SSPCON1bits.SSPEN = 1; // enable
	junk = SSPBUF; // dummy read to clear BF
	(void)junk;
}

U8
spiTx(U8 c) {
	SSPBUF = c;
	while (!SSPSTATbits.BF) {}
	return SSPBUF;
}