mbed library sources with internal temperature sensor for nucleo f401

Committer:
elessair
Date:
Sat Jan 17 18:03:58 2015 +0000
Revision:
0:7e2bd16f80af
nucleo f401re internal temperature added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:7e2bd16f80af 1 /* mbed Microcontroller Library
elessair 0:7e2bd16f80af 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:7e2bd16f80af 3 *
elessair 0:7e2bd16f80af 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:7e2bd16f80af 5 * you may not use this file except in compliance with the License.
elessair 0:7e2bd16f80af 6 * You may obtain a copy of the License at
elessair 0:7e2bd16f80af 7 *
elessair 0:7e2bd16f80af 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:7e2bd16f80af 9 *
elessair 0:7e2bd16f80af 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:7e2bd16f80af 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:7e2bd16f80af 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:7e2bd16f80af 13 * See the License for the specific language governing permissions and
elessair 0:7e2bd16f80af 14 * limitations under the License.
elessair 0:7e2bd16f80af 15 */
elessair 0:7e2bd16f80af 16 #include "I2C.h"
elessair 0:7e2bd16f80af 17
elessair 0:7e2bd16f80af 18 #if DEVICE_I2C
elessair 0:7e2bd16f80af 19
elessair 0:7e2bd16f80af 20 namespace mbed {
elessair 0:7e2bd16f80af 21
elessair 0:7e2bd16f80af 22 I2C *I2C::_owner = NULL;
elessair 0:7e2bd16f80af 23
elessair 0:7e2bd16f80af 24 I2C::I2C(PinName sda, PinName scl) : _i2c(), _hz(100000) {
elessair 0:7e2bd16f80af 25 // The init function also set the frequency to 100000
elessair 0:7e2bd16f80af 26 i2c_init(&_i2c, sda, scl);
elessair 0:7e2bd16f80af 27
elessair 0:7e2bd16f80af 28 // Used to avoid unnecessary frequency updates
elessair 0:7e2bd16f80af 29 _owner = this;
elessair 0:7e2bd16f80af 30 }
elessair 0:7e2bd16f80af 31
elessair 0:7e2bd16f80af 32 void I2C::frequency(int hz) {
elessair 0:7e2bd16f80af 33 _hz = hz;
elessair 0:7e2bd16f80af 34
elessair 0:7e2bd16f80af 35 // We want to update the frequency even if we are already the bus owners
elessair 0:7e2bd16f80af 36 i2c_frequency(&_i2c, _hz);
elessair 0:7e2bd16f80af 37
elessair 0:7e2bd16f80af 38 // Updating the frequency of the bus we become the owners of it
elessair 0:7e2bd16f80af 39 _owner = this;
elessair 0:7e2bd16f80af 40 }
elessair 0:7e2bd16f80af 41
elessair 0:7e2bd16f80af 42 void I2C::aquire() {
elessair 0:7e2bd16f80af 43 if (_owner != this) {
elessair 0:7e2bd16f80af 44 i2c_frequency(&_i2c, _hz);
elessair 0:7e2bd16f80af 45 _owner = this;
elessair 0:7e2bd16f80af 46 }
elessair 0:7e2bd16f80af 47 }
elessair 0:7e2bd16f80af 48
elessair 0:7e2bd16f80af 49 // write - Master Transmitter Mode
elessair 0:7e2bd16f80af 50 int I2C::write(int address, const char* data, int length, bool repeated) {
elessair 0:7e2bd16f80af 51 aquire();
elessair 0:7e2bd16f80af 52
elessair 0:7e2bd16f80af 53 int stop = (repeated) ? 0 : 1;
elessair 0:7e2bd16f80af 54 int written = i2c_write(&_i2c, address, data, length, stop);
elessair 0:7e2bd16f80af 55
elessair 0:7e2bd16f80af 56 return length != written;
elessair 0:7e2bd16f80af 57 }
elessair 0:7e2bd16f80af 58
elessair 0:7e2bd16f80af 59 int I2C::write(int data) {
elessair 0:7e2bd16f80af 60 return i2c_byte_write(&_i2c, data);
elessair 0:7e2bd16f80af 61 }
elessair 0:7e2bd16f80af 62
elessair 0:7e2bd16f80af 63 // read - Master Reciever Mode
elessair 0:7e2bd16f80af 64 int I2C::read(int address, char* data, int length, bool repeated) {
elessair 0:7e2bd16f80af 65 aquire();
elessair 0:7e2bd16f80af 66
elessair 0:7e2bd16f80af 67 int stop = (repeated) ? 0 : 1;
elessair 0:7e2bd16f80af 68 int read = i2c_read(&_i2c, address, data, length, stop);
elessair 0:7e2bd16f80af 69
elessair 0:7e2bd16f80af 70 return length != read;
elessair 0:7e2bd16f80af 71 }
elessair 0:7e2bd16f80af 72
elessair 0:7e2bd16f80af 73 int I2C::read(int ack) {
elessair 0:7e2bd16f80af 74 if (ack) {
elessair 0:7e2bd16f80af 75 return i2c_byte_read(&_i2c, 0);
elessair 0:7e2bd16f80af 76 } else {
elessair 0:7e2bd16f80af 77 return i2c_byte_read(&_i2c, 1);
elessair 0:7e2bd16f80af 78 }
elessair 0:7e2bd16f80af 79 }
elessair 0:7e2bd16f80af 80
elessair 0:7e2bd16f80af 81 void I2C::start(void) {
elessair 0:7e2bd16f80af 82 i2c_start(&_i2c);
elessair 0:7e2bd16f80af 83 }
elessair 0:7e2bd16f80af 84
elessair 0:7e2bd16f80af 85 void I2C::stop(void) {
elessair 0:7e2bd16f80af 86 i2c_stop(&_i2c);
elessair 0:7e2bd16f80af 87 }
elessair 0:7e2bd16f80af 88
elessair 0:7e2bd16f80af 89 } // namespace mbed
elessair 0:7e2bd16f80af 90
elessair 0:7e2bd16f80af 91 #endif