mbed library sources

Dependents:   bare

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI.cpp Source File

SPI.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "SPI.h"
00017 
00018 #if DEVICE_SPI
00019 
00020 namespace mbed {
00021 
00022 SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused) {
00023     spi_init(&_spi, mosi, miso, sclk, NC);
00024     _bits = 8;
00025     _mode = 0;
00026     _hz = 1000000;
00027     spi_format(&_spi, _bits, _mode, 0);
00028     spi_frequency(&_spi, _hz);
00029 }
00030 
00031 void SPI::format(int bits, int mode) {
00032     _bits = bits;
00033     _mode = mode;
00034     SPI::_owner = NULL; // Not that elegant, but works. rmeyer
00035     aquire();
00036 }
00037 
00038 void SPI::frequency(int hz) {
00039     _hz = hz;
00040     SPI::_owner = NULL; // Not that elegant, but works. rmeyer
00041     aquire();
00042 }
00043 
00044 SPI* SPI::_owner = NULL;
00045 
00046 // ignore the fact there are multiple physical spis, and always update if it wasnt us last
00047 void SPI::aquire() {
00048      if (_owner != this) {
00049         spi_format(&_spi, _bits, _mode, 0);
00050         spi_frequency(&_spi, _hz);
00051         _owner = this;
00052     }
00053 }
00054 
00055 int SPI::write(int value) {
00056     aquire();
00057     return spi_master_write(&_spi, value);
00058 }
00059 
00060 } // namespace mbed
00061 
00062 #endif