mbed library for NZ32-SC151

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CSlave.cpp Source File

I2CSlave.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 "I2CSlave.h"
00017 
00018 #if DEVICE_I2CSLAVE
00019 
00020 namespace mbed {
00021 
00022 I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c() {
00023     i2c_init(&_i2c, sda, scl);
00024     i2c_frequency(&_i2c, 100000);
00025     i2c_slave_mode(&_i2c, 1);
00026 }
00027 
00028 void I2CSlave::frequency(int hz) {
00029     i2c_frequency(&_i2c, hz);
00030 }
00031 
00032 void I2CSlave::address(int address) {
00033     int addr = (address & 0xFF) | 1;
00034     i2c_slave_address(&_i2c, 0, addr, 0);
00035 }
00036 
00037 int I2CSlave::receive(void) {
00038     return i2c_slave_receive(&_i2c);
00039 }
00040 
00041 int I2CSlave::read(char *data, int length) {
00042     return i2c_slave_read(&_i2c, data, length) != length;
00043 }
00044 
00045 int I2CSlave::read(void) {
00046     return i2c_byte_read(&_i2c, 0);
00047 }
00048 
00049 int I2CSlave::write(const char *data, int length) {
00050     return i2c_slave_write(&_i2c, data, length) != length;
00051 }
00052 
00053 int I2CSlave::write(int data) {
00054     return i2c_byte_write(&_i2c, data);
00055 }
00056 
00057 void I2CSlave::stop(void) {
00058     i2c_stop(&_i2c);
00059 }
00060 
00061 }
00062 
00063 #endif