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 "SPI.h"
|
---|
12 | #include "wiring_digital.h"
|
---|
13 | #include "assert.h"
|
---|
14 | #include "variant.h"
|
---|
15 |
|
---|
16 | SPIClass::SPIClass(SERCOM *p_sercom, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI)
|
---|
17 | {
|
---|
18 | assert(p_sercom != NULL );
|
---|
19 | _p_sercom = p_sercom;
|
---|
20 |
|
---|
21 | _uc_pinMiso = uc_pinMISO;
|
---|
22 | _uc_pinSCK = uc_pinSCK;
|
---|
23 | _uc_pinMosi = uc_pinMOSI;
|
---|
24 | }
|
---|
25 |
|
---|
26 | void SPIClass::begin()
|
---|
27 | {
|
---|
28 | // PIO init
|
---|
29 | pinPeripheral(_uc_pinMiso, g_APinDescription[_uc_pinMiso].ulPinType);
|
---|
30 | pinPeripheral(_uc_pinSCK, g_APinDescription[_uc_pinSCK].ulPinType);
|
---|
31 | pinPeripheral(_uc_pinMosi, g_APinDescription[_uc_pinMosi].ulPinType);
|
---|
32 |
|
---|
33 | // Default speed set to 4Mhz, SPI mode set to MODE 0 and Bit order set to MSB first.
|
---|
34 | _p_sercom->initSPI(SPI_PAD_2_SCK_3, SERCOM_RX_PAD_0, SPI_CHAR_SIZE_8_BITS, MSB_FIRST);
|
---|
35 | _p_sercom->initSPIClock(SERCOM_SPI_MODE_0, 4000000);
|
---|
36 |
|
---|
37 | _p_sercom->enableSPI();
|
---|
38 | }
|
---|
39 |
|
---|
40 | void SPIClass::end()
|
---|
41 | {
|
---|
42 | _p_sercom->resetSPI();
|
---|
43 | }
|
---|
44 |
|
---|
45 | void SPIClass::setBitOrder(BitOrder order)
|
---|
46 | {
|
---|
47 | if(order == LSBFIRST)
|
---|
48 | _p_sercom->setDataOrderSPI(LSB_FIRST);
|
---|
49 | else
|
---|
50 | _p_sercom->setDataOrderSPI(MSB_FIRST);
|
---|
51 | }
|
---|
52 |
|
---|
53 | void SPIClass::setDataMode(uint8_t mode)
|
---|
54 | {
|
---|
55 | switch(mode)
|
---|
56 | {
|
---|
57 | case SPI_MODE0:
|
---|
58 | _p_sercom->setClockModeSPI(SERCOM_SPI_MODE_0);
|
---|
59 | break;
|
---|
60 |
|
---|
61 | case SPI_MODE1:
|
---|
62 | _p_sercom->setClockModeSPI(SERCOM_SPI_MODE_1);
|
---|
63 | break;
|
---|
64 |
|
---|
65 | case SPI_MODE2:
|
---|
66 | _p_sercom->setClockModeSPI(SERCOM_SPI_MODE_2);
|
---|
67 | break;
|
---|
68 |
|
---|
69 | case SPI_MODE3:
|
---|
70 | _p_sercom->setClockModeSPI(SERCOM_SPI_MODE_3);
|
---|
71 | break;
|
---|
72 |
|
---|
73 | default:
|
---|
74 | break;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void SPIClass::setClockDivider(uint8_t div)
|
---|
79 | {
|
---|
80 | _p_sercom->setBaudrateSPI(div);
|
---|
81 | }
|
---|
82 |
|
---|
83 | byte SPIClass::transfer(uint8_t data)
|
---|
84 | {
|
---|
85 | //Writing the data
|
---|
86 | _p_sercom->writeDataSPI(data);
|
---|
87 |
|
---|
88 | //Read data
|
---|
89 | return _p_sercom->readDataSPI();
|
---|
90 | }
|
---|
91 |
|
---|
92 | void SPIClass::attachInterrupt() {
|
---|
93 | // Should be enableInterrupt()
|
---|
94 | }
|
---|
95 |
|
---|
96 | void SPIClass::detachInterrupt() {
|
---|
97 | // Should be disableInterrupt()
|
---|
98 | }
|
---|
99 |
|
---|
100 | SPIClass SPI(&sercom4, 18, 20, 21);
|
---|