firmware  v0.1.2
Chromation Spectrometer Dev-Kit
UartSpi.h
Go to the documentation of this file.
1 #ifndef _UARTSPI_H
2 #define _UARTSPI_H
3 #include <stdint.h>
4 #include "ReadWriteBits.h"
5 //---Hardware types: register addresses, pin numbers, bit numbers---
6 typedef uint8_t volatile * const uspi_ptr; // i/o reg address
7 typedef uint16_t volatile * const uspi_ptr16; // i/o reg address
8 typedef uint8_t const uspi_pin; // bit index into i/o reg for an i/o pin
9 typedef uint8_t const uspi_bit; // bit index into i/o reg
10 // Register address, pin number, and bit definitions depend on compiler:
11  // "gcc" uses test/HardwareFake.h
12  // "avr-gcc" uses src/UartSpi-Hardware.h
13 // ---Registers---
14 extern uspi_ptr UartSpi_ddr;
15 extern uspi_ptr UartSpi_port;
16 extern uspi_ptr UartSpi_UCSR0A;
17 extern uspi_ptr UartSpi_UCSR0B;
18 extern uspi_ptr UartSpi_UCSR0C;
19 extern uspi_ptr UartSpi_UDR0;
20 extern uspi_ptr16 UartSpi_UBRR0;
21 // ---Pins---
22 extern uspi_pin UartSpi_Miso; // UART RX
23 extern uspi_pin UartSpi_AdcConv; // high: convert, low: readout
24 extern uspi_pin UartSpi_Sck; // UART XCK
25 // ---Bits---
26 extern uspi_bit UartSpi_UMSEL00; // set: mode select 0: SPI Master
27 extern uspi_bit UartSpi_UMSEL01; // set: mode select 1: SPI Master
28 extern uspi_bit UartSpi_RXEN0; // set: enable Rx
29 extern uspi_bit UartSpi_TXEN0; // set: enable Tx
30 extern uspi_bit UartSpi_UCPOL0; // set: CPOL=1 (Clock Polarity)
31 extern uspi_bit UartSpi_UCPHA0; // set: CPHA=1 (Clock Phase)
32 extern uspi_bit UartSpi_UDORD0; // clear: Data Order is MSB first
33 extern uspi_bit UartSpi_UDRE0; // (DataRegEmtpy) is set on Tx done
34 extern uspi_bit UartSpi_RXC0; // (RxComplete) is set on Rx done
35 
36 // ---Private---
37 inline void RunSpiAt5Mhz(void)
38 {
44  *UartSpi_UBRR0 = 0;
45  // iom328p.h: #define UBRR0 _SFR_MEM16(0xC4)
46  // ---Expected Assembly---
47  // sts 0x00C5, r1 ; 0x8000c5 <__TEXT_REGION_LENGTH__+0x7e00c5>
48  // sts 0x00C4, r1 ; 0x8000c4 <__TEXT_REGION_LENGTH__+0x7e00c4>
49 }
50 inline void SetSckAsOutput(void)
51 {
53  SetBit(UartSpi_ddr, UartSpi_Sck);
54  // ---Expected Assembly---
55  // sbi 0x0a, 4 ; 10
56 }
57 inline void AdcConvIdleLow(void)
58 {
59  ClearBit(UartSpi_port, UartSpi_AdcConv);
60  // ---Expected Assembly---
61  // cbi 0x0b, 2 ; 11
62 }
63 inline void SetAdcConvAsOutput(void)
64 {
70  SetBit(UartSpi_ddr, UartSpi_AdcConv);
71  // ---Expected Assembly---
72  // sbi 0x0a, 2 ; 10
73 }
74 inline void EnableAtmega328UsartInSpiMasterMode(void)
75 {
76  SetBit(UartSpi_UCSR0C, UartSpi_UMSEL00);
77  SetBit(UartSpi_UCSR0C, UartSpi_UMSEL01);
78  // ---Expected Assembly---
79  // &UCSR0C (reg 0xC2), UMSEL00 (bit 6) and UMSEL01 (bit 7)
80  // Set bits 6 and 7 in register 16-bit register 0xC2.
81  //
82  // Register Z is the indirect address 16-bit register:
83  // - r31 is the msb
84  // - r30 is the lsb
85  // ldi r30, 0xC2 ; 194
86  // ldi r31, 0x00 ; 0
87  // ld r24, Z
88  // - Set bit 6
89  // ori r24, 0x40 ; 64
90  // st Z, r24
91  // ld r24, Z
92  // - Set bit 7
93  // ori r24, 0x80 ; 128
94  // st Z, r24
95 }
96 inline void UseSpiDataModeCpol1CPha1(void)
97 {
103  SetBit(UartSpi_UCSR0C, UartSpi_UCPOL0); // clock idles high
104  SetBit(UartSpi_UCSR0C, UartSpi_UCPHA0); // load data then sample data
105  // ---Expected Assembly---
106  // reg: UCSR0C (reg 0xC2)
107  // set bit 0: UCPOL0
108  // set bit 1: UCPHA0
109  // - set bit 1
110  // ori r24, 0x01 ; 1
111  // st Z, r24
112  // ld r24, Z
113  // - set bit 2
114  // ori r24, 0x02 ; 2
115  // st Z, r24
116 }
117 inline void CfgSpiToTransferMsbFirst(void)
118 {
120  ClearBit(UartSpi_UCSR0C, UartSpi_UDORD0);
121  // ---Expected Assembly---
122  // reg UCSR0C (reg 0xC2), UDORD0 (bit 2)
123  // - clear bit 2
124  // andi r24, 0xFB ; 251
125  // st Z, r24
126 }
128 {
135  SetBit(UartSpi_UCSR0B, UartSpi_RXEN0);
136  SetBit(UartSpi_UCSR0B, UartSpi_TXEN0);
137  // ---Expected Assembly---
138  // reg UCSR0B (0xC1), RXEN0 (bit 4), TXEN0 (bit 3)
139  //
140  // ldi r30, 0xC1 ; 193
141  // ldi r31, 0x00 ; 0
142  // - set bit 4
143  // ld r24, Z
144  // ori r24, 0x10 ; 16
145  // st Z, r24
146  // - set bit 3
147  // ld r24, Z
148  // ori r24, 0x08 ; 8
149  // st Z, r24
150 }
151 inline void SpiMasterCfg(void)
152 {
153  EnableAtmega328UsartInSpiMasterMode();
157 }
158 // ---API---
164 inline void UartSpiInit(void)
165 {
176  // ---Expected Assembly---
177  RunSpiAt5Mhz();
178  SetSckAsOutput();
179  AdcConvIdleLow();
181  SpiMasterCfg();
182  RunSpiAt5Mhz();
183 }
184 inline void StartAdcConversion(void)
185 {
186  SetBit(UartSpi_port, UartSpi_AdcConv);
187  // ---Expected Assembly---
188  // sbi 0x0b, 2 ; 11
189 }
190 inline void StartAdcReadout(void)
191 {
192  ClearBit(UartSpi_port, UartSpi_AdcConv);
193  // write two dummy bytes to transfer 16 bits
194  *UartSpi_UDR0 = 0x00; *UartSpi_UDR0 = 0x00;
195  // ---Expected Assembly---
196  // cbi 0x0b, 2 ; 11
197 }
198 
199 #endif // _UARTSPI_H
void CfgSpiToTransferMsbFirst(void)
Definition: UartSpi.h:117
void UartSpiInit(void)
Definition: UartSpi.h:164
void SetAdcConvAsOutput(void)
Definition: UartSpi.h:63
void UseSpiDataModeCpol1CPha1(void)
Definition: UartSpi.h:96
void SetSckAsOutput(void)
Definition: UartSpi.h:50
void RunSpiAt5Mhz(void)
Definition: UartSpi.h:37
void GiveSpiControlOverMisoAndMosiPins(void)
Definition: UartSpi.h:127