Browse Source

library - SPI

Richard Wackerbarth 9 years ago
parent
commit
efd5658e32

+ 66
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/libraries/SPI/SPI.cpp View File

@@ -0,0 +1,66 @@
1
+/*
2
+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
3
+ * SPI Master library for arduino.
4
+ *
5
+ * This file is free software; you can redistribute it and/or modify
6
+ * it under the terms of either the GNU General Public License version 2
7
+ * or the GNU Lesser General Public License version 2.1, both as
8
+ * published by the Free Software Foundation.
9
+ */
10
+
11
+#include "pins_arduino.h"
12
+#include "SPI.h"
13
+
14
+SPIClass SPI;
15
+
16
+void SPIClass::begin() {
17
+
18
+  // Set SS to high so a connected chip will be "deselected" by default
19
+  digitalWrite(SS, HIGH);
20
+
21
+  // When the SS pin is set as OUTPUT, it can be used as
22
+  // a general purpose output port (it doesn't influence
23
+  // SPI operations).
24
+  pinMode(SS, OUTPUT);
25
+
26
+  // Warning: if the SS pin ever becomes a LOW INPUT then SPI
27
+  // automatically switches to Slave, so the data direction of
28
+  // the SS pin MUST be kept as OUTPUT.
29
+  SPCR |= _BV(MSTR);
30
+  SPCR |= _BV(SPE);
31
+
32
+  // Set direction register for SCK and MOSI pin.
33
+  // MISO pin automatically overrides to INPUT.
34
+  // By doing this AFTER enabling SPI, we avoid accidentally
35
+  // clocking in a single bit since the lines go directly
36
+  // from "input" to SPI control.  
37
+  // http://code.google.com/p/arduino/issues/detail?id=888
38
+  pinMode(SCK, OUTPUT);
39
+  pinMode(MOSI, OUTPUT);
40
+}
41
+
42
+
43
+void SPIClass::end() {
44
+  SPCR &= ~_BV(SPE);
45
+}
46
+
47
+void SPIClass::setBitOrder(uint8_t bitOrder)
48
+{
49
+  if(bitOrder == LSBFIRST) {
50
+    SPCR |= _BV(DORD);
51
+  } else {
52
+    SPCR &= ~(_BV(DORD));
53
+  }
54
+}
55
+
56
+void SPIClass::setDataMode(uint8_t mode)
57
+{
58
+  SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
59
+}
60
+
61
+void SPIClass::setClockDivider(uint8_t rate)
62
+{
63
+  SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
64
+  SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
65
+}
66
+

+ 70
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/libraries/SPI/SPI.h View File

@@ -0,0 +1,70 @@
1
+/*
2
+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
3
+ * SPI Master library for arduino.
4
+ *
5
+ * This file is free software; you can redistribute it and/or modify
6
+ * it under the terms of either the GNU General Public License version 2
7
+ * or the GNU Lesser General Public License version 2.1, both as
8
+ * published by the Free Software Foundation.
9
+ */
10
+
11
+#ifndef _SPI_H_INCLUDED
12
+#define _SPI_H_INCLUDED
13
+
14
+#include <stdio.h>
15
+#include <Arduino.h>
16
+#include <avr/pgmspace.h>
17
+
18
+#define SPI_CLOCK_DIV4 0x00
19
+#define SPI_CLOCK_DIV16 0x01
20
+#define SPI_CLOCK_DIV64 0x02
21
+#define SPI_CLOCK_DIV128 0x03
22
+#define SPI_CLOCK_DIV2 0x04
23
+#define SPI_CLOCK_DIV8 0x05
24
+#define SPI_CLOCK_DIV32 0x06
25
+//#define SPI_CLOCK_DIV64 0x07
26
+
27
+#define SPI_MODE0 0x00
28
+#define SPI_MODE1 0x04
29
+#define SPI_MODE2 0x08
30
+#define SPI_MODE3 0x0C
31
+
32
+#define SPI_MODE_MASK 0x0C  // CPOL = bit 3, CPHA = bit 2 on SPCR
33
+#define SPI_CLOCK_MASK 0x03  // SPR1 = bit 1, SPR0 = bit 0 on SPCR
34
+#define SPI_2XCLOCK_MASK 0x01  // SPI2X = bit 0 on SPSR
35
+
36
+class SPIClass {
37
+public:
38
+  inline static byte transfer(byte _data);
39
+
40
+  // SPI Configuration methods
41
+
42
+  inline static void attachInterrupt();
43
+  inline static void detachInterrupt(); // Default
44
+
45
+  static void begin(); // Default
46
+  static void end();
47
+
48
+  static void setBitOrder(uint8_t);
49
+  static void setDataMode(uint8_t);
50
+  static void setClockDivider(uint8_t);
51
+};
52
+
53
+extern SPIClass SPI;
54
+
55
+byte SPIClass::transfer(byte _data) {
56
+  SPDR = _data;
57
+  while (!(SPSR & _BV(SPIF)))
58
+    ;
59
+  return SPDR;
60
+}
61
+
62
+void SPIClass::attachInterrupt() {
63
+  SPCR |= _BV(SPIE);
64
+}
65
+
66
+void SPIClass::detachInterrupt() {
67
+  SPCR &= ~_BV(SPIE);
68
+}
69
+
70
+#endif

+ 36
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/libraries/SPI/keywords.txt View File

@@ -0,0 +1,36 @@
1
+#######################################
2
+# Syntax Coloring Map SPI
3
+#######################################
4
+
5
+#######################################
6
+# Datatypes (KEYWORD1)
7
+#######################################
8
+
9
+SPI	KEYWORD1
10
+
11
+#######################################
12
+# Methods and Functions (KEYWORD2)
13
+#######################################
14
+begin	KEYWORD2
15
+end	KEYWORD2
16
+transfer	KEYWORD2
17
+setBitOrder	KEYWORD2
18
+setDataMode	KEYWORD2
19
+setClockDivider	KEYWORD2
20
+
21
+
22
+#######################################
23
+# Constants (LITERAL1)
24
+#######################################
25
+SPI_CLOCK_DIV4	LITERAL1
26
+SPI_CLOCK_DIV16	LITERAL1
27
+SPI_CLOCK_DIV64	LITERAL1
28
+SPI_CLOCK_DIV128	LITERAL1
29
+SPI_CLOCK_DIV2	LITERAL1
30
+SPI_CLOCK_DIV8	LITERAL1
31
+SPI_CLOCK_DIV32	LITERAL1
32
+SPI_CLOCK_DIV64	LITERAL1
33
+SPI_MODE0	LITERAL1
34
+SPI_MODE1	LITERAL1
35
+SPI_MODE2	LITERAL1
36
+SPI_MODE3	LITERAL1

Loading…
Cancel
Save