firmware  v0.1.2
Chromation Spectrometer Dev-Kit
Spi.h
Go to the documentation of this file.
1 
28 #ifndef _SPI_H
29 #define _SPI_H
30 
31 #include <stdint.h>
32 #include "ReadWriteBits.h"
33 
34 // ---Hardware i/o types---
35 typedef uint8_t volatile * const spi_reg; // i/o reg address
36 typedef uint8_t const spi_bit; // bit index into i/o reg
37 
38 // ---i/o Registers---
40 extern spi_reg Spi_PortDirection;
42 extern spi_reg Spi_PortInput;
44 extern spi_reg Spi_PortOutput;
46 extern spi_reg Spi_PortPullup;
48 extern spi_reg Spi_SPCR;
50 extern spi_reg Spi_SPSR;
52 extern spi_reg Spi_SPDR;
53 
54 // ---i/o Pins---
55 extern spi_bit Spi_DataReady;
56 extern spi_bit Spi_Ss;
57 extern spi_bit Spi_Mosi;
58 extern spi_bit Spi_Miso;
59 extern spi_bit Spi_Sck;
60 
61 // ---i/o Bits---
62 extern spi_bit Spi_MasterSlaveSelect; // bit MSTR in SPCR (0x2C)
63 extern spi_bit Spi_ClockBit0; // bit 0 in SPCR (0x2C)
64 extern spi_bit Spi_ClockBit1; // bit 1 in SPCR (0x2C)
65 extern spi_bit Spi_DoubleClock; // bit 0 in SPSR (0x2D)
66 extern spi_bit Spi_Enable; // bit 6 in SPCR (0x2C)
67 extern spi_bit Spi_InterruptEnable; // bit 7 in SPCR (0x2C)
68 extern spi_bit Spi_InterruptFlag; // bit 7 in SPSR (0x2D)
69 
70 #ifdef USE_FAKES
71 #include "Spi_faked.h" // declare fakes
72 #endif
73 
74 // ---Private---
75 inline void _EnableSpiModule(void)
76 {
80  SetBit(Spi_SPCR, Spi_Enable);
81  // ---Expected Assembly---
82  // in r24, 0x2c;
83  // ori r24, 0x40;
84  // out 0x2c, r24
85  // This is three instructions because SPCR is outside the
86  // address range for using `sbi`.
87 }
88 inline bool _SpiTransferIsDone(void)
89 {
94  return BitIsSet(Spi_SPSR, Spi_InterruptFlag);
95 }
96 
97 // ---API (Go to the Doxygen documentation of this file)---
104 inline uint8_t ReadSpiStatusRegister(void)
105 {
106  return *Spi_SPSR;
107  // ---Expected Assembly---
108  // in r24, 0x2d ; 45
109 }
110 inline uint8_t ReadSpiDataRegister(void)
111 {
112  return *Spi_SPDR;
113  // ---Expected Assembly---
114  // in r24, 0x2e ; 46
115 }
116 inline void ClearSpiInterruptFlag(void)
117 {
119 
124  ReadSpiStatusRegister(); // in r24, 0x2d
125  ReadSpiDataRegister(); // in r24, 0x2e
126 }
127 
128 // ---API functions that call fakes when testing---
129 //
130 
131 #endif // _SPI_H
void _EnableSpiModule(void)
Definition: Spi.h:75
spi_reg Spi_PortOutput
Atmel PORT.
spi_reg Spi_SPDR
SPI Data Register.
spi_reg Spi_SPCR
SPI Control Register.
spi_reg Spi_PortPullup
Atmel PORT alias.
Definition: Spi-Hardware.h:16
spi_reg Spi_PortDirection
Atmel DDR.
bool _SpiTransferIsDone(void)
Definition: Spi.h:88
spi_reg Spi_PortInput
Atmel PIN.
spi_reg Spi_SPSR
SPI Status Register.
void ClearSpiInterruptFlag(void)
Definition: Spi.h:116