]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/common/SPI.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / common / SPI.cpp
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "SPI.h"
17
18 #if DEVICE_SPI
19
20 namespace mbed {
21
22 SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused) :
23         _spi(),
24         _bits(8),
25         _mode(0),
26         _hz(1000000) {
27     spi_init(&_spi, mosi, miso, sclk, NC);
28     spi_format(&_spi, _bits, _mode, 0);
29     spi_frequency(&_spi, _hz);
30 }
31
32 void SPI::format(int bits, int mode) {
33     _bits = bits;
34     _mode = mode;
35     SPI::_owner = NULL; // Not that elegant, but works. rmeyer
36     aquire();
37 }
38
39 void SPI::frequency(int hz) {
40     _hz = hz;
41     SPI::_owner = NULL; // Not that elegant, but works. rmeyer
42     aquire();
43 }
44
45 SPI* SPI::_owner = NULL;
46
47 // ignore the fact there are multiple physical spis, and always update if it wasnt us last
48 void SPI::aquire() {
49      if (_owner != this) {
50         spi_format(&_spi, _bits, _mode, 0);
51         spi_frequency(&_spi, _hz);
52         _owner = this;
53     }
54 }
55
56 int SPI::write(int value) {
57     aquire();
58     return spi_master_write(&_spi, value);
59 }
60
61 } // namespace mbed
62
63 #endif