spi.c 1.74 KB
Newer Older
Petter Breedveld's avatar
Petter Breedveld committed
1
2
3
//
// spi.c
//
4

Petter Breedveld's avatar
Petter Breedveld committed
5
6
7
8
9
10
11
12
13
14
15

// System headers
#include <avr/io.h>

// Project headers
#include "spi.h"

// In this file, you will need to write the contents of the SPI communication routines.
// You need to setup SPI communication in SPI_MasterInit() and
// transmit data in SPI_MasterTransmit(...).
//
Petter Breedveld's avatar
Petter Breedveld committed
16
// HINT: Check out the , and as always:
Petter Breedveld's avatar
Petter Breedveld committed
17
18
// RTFD

19
//Oled is connected to alternate SPI pins
Petter Breedveld's avatar
Petter Breedveld committed
20
21
22
#define MOSI_bm 1<<2 //on port C
#define SCK_bm 1<<0 //on port C
#define SS_bm 1<<3 //on port C
Petter Breedveld's avatar
Petter Breedveld committed
23
24
#define SPI_PORT PORTC //We can now use SPI_PORT.DIR, .OUT etc.

Petter Breedveld's avatar
Petter Breedveld committed
25
//We could also have defined bit positions for MOSI, SCL and SS.
Petter Breedveld's avatar
Petter Breedveld committed
26
27
28
29
30



void SPI_MasterInit()
{
31
32
33
	// Initialize the SPI port as master
	// You will need to set MOSI, SCK, SS (slave select) as outputs
	
Petter Breedveld's avatar
Petter Breedveld committed
34
	PORTMUX.CTRLB |= PORTMUX_SPI0_bm; //To use alternative SPI pins
35
	
Petter Breedveld's avatar
Petter Breedveld committed
36
	SPI_PORT.DIR |= MOSI_bm | SCK_bm | SS_bm; //Set pins as output
37

Petter Breedveld's avatar
Petter Breedveld committed
38
	SPI_PORT.OUTSET = SS_bm; //Set SS high -> OLED inactive
39
	
Petter Breedveld's avatar
Petter Breedveld committed
40
	
41
	// Now enable SPI, Master and set clock rate
Petter Breedveld's avatar
Petter Breedveld committed
42
	
Petter Breedveld's avatar
Petter Breedveld committed
43
	SPI0.CTRLA |= SPI_ENABLE_bm | SPI_MASTER_bm; //Default clock divisor of 4 is fine
Petter Breedveld's avatar
Petter Breedveld committed
44
45
	
	
46

Petter Breedveld's avatar
Petter Breedveld committed
47
48
	//Make sure SS does not disable master mode (Possibly not required)
	SPI0.CTRLB |= SPI_SSD_bm;
Petter Breedveld's avatar
Petter Breedveld committed
49
50
51
52
53
}


void SPI_MasterTransmit(char cData)
{
Petter Breedveld's avatar
Petter Breedveld committed
54
	// First select the correct slave by setting its slave select (SS) LOW
Petter Breedveld's avatar
Petter Breedveld committed
55
	SPI_PORT.OUTCLR = SS_bm;
Petter Breedveld's avatar
Petter Breedveld committed
56
57

	// Then start the transmission by assigning the data to the SPI data register
58
	SPI0.DATA = cData;
Petter Breedveld's avatar
Petter Breedveld committed
59

60
	// Now wait for the data transmission to complete by periodically checking the SPI status register
Petter Breedveld's avatar
Petter Breedveld committed
61
62
63
64
	//the SPI_IF and SPI_WRCOL is the only interrupt flag with a function in non-buffered mode.
	while(!(SPI0.INTFLAGS & SPI_IF_bm)){
		
	}
65
	SPI0.DATA; //Dummy read to clear flag
Petter Breedveld's avatar
Petter Breedveld committed
66
67

	// Finally set the slave select bit HIGH before leaving the function
Petter Breedveld's avatar
Petter Breedveld committed
68
	SPI_PORT.OUTSET = SS_bm;
Petter Breedveld's avatar
Petter Breedveld committed
69
}