-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_spi.h
80 lines (62 loc) · 1.54 KB
/
my_spi.h
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 (c) 2010-2011 by Kevin Smith <[email protected]>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 3
* as published by the Free Software Foundation.
*/
#ifndef __SPI_H__
#define __SPI_H__
/* Set to '1' if building CAN library for Arduino
* '0' for a standalone C/C++ program */
#define ARDUINO 1
#include <stdint.h>
#if ARDUINO
#include <SPI.h>
#endif
#if ARDUINO
const int slaveSelectPin = 10;
/** Assert the slave select signal */
static inline void assert_ss (void)
{
digitalWrite (slaveSelectPin, LOW);
}
/** Deassert the slave select signal */
static inline void deassert_ss (void)
{
digitalWrite (slaveSelectPin, HIGH);
}
/**
* Initiate an SPI transfer.
* @param byte - byte to send
* @return - the byte received
*/
static uint8_t spi_transfer (uint8_t byte)
{
return SPI.transfer (byte);
}
#else
/** Initialize the SPI */
void init_spi (void);
/** Assert the slave select signal */
void assert_ss (void);
/** Deassert the slave select signal */
void deassert_ss (void);
/**
* Initiate an SPI transfer.
* @param byte - byte to send
* @return - the byte received
*/
uint8_t spi_transfer (uint8_t byte);
#endif
/** Convenience function for sending a byte and ignoring the receive value */
static inline void spi_send (uint8_t byte)
{
spi_transfer (byte);
}
/** Convenience function for receiving a byte and ignoring the send value */
static inline uint8_t spi_receive (void)
{
return spi_transfer (0);
}
#endif