Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "drivers/I2CSlave.h"
00018 
00019 #if DEVICE_I2CSLAVE
00020 
00021 namespace mbed {
00022 
00023 I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c()
00024 {
00025     i2c_init(&_i2c, sda, scl);
00026     i2c_frequency(&_i2c, 100000);
00027     i2c_slave_mode(&_i2c, 1);
00028 }
00029 
00030 I2CSlave::I2CSlave(const i2c_pinmap_t &static_pinmap) : _i2c()
00031 {
00032     i2c_init_direct(&_i2c, &static_pinmap);
00033     i2c_frequency(&_i2c, 100000);
00034     i2c_slave_mode(&_i2c, 1);
00035 }
00036 
00037 void I2CSlave::frequency(int hz)
00038 {
00039     i2c_frequency(&_i2c, hz);
00040 }
00041 
00042 void I2CSlave::address(int address)
00043 {
00044     int addr = (address & 0xFF) | 1;
00045     i2c_slave_address(&_i2c, 0, addr, 0);
00046 }
00047 
00048 int I2CSlave::receive(void)
00049 {
00050     return i2c_slave_receive(&_i2c);
00051 }
00052 
00053 int I2CSlave::read(char *data, int length)
00054 {
00055     return i2c_slave_read(&_i2c, data, length) != length;
00056 }
00057 
00058 int I2CSlave::read(void)
00059 {
00060     return i2c_byte_read(&_i2c, 0);
00061 }
00062 
00063 int I2CSlave::write(const char *data, int length)
00064 {
00065     return i2c_slave_write(&_i2c, data, length) != length;
00066 }
00067 
00068 int I2CSlave::write(int data)
00069 {
00070     return i2c_byte_write(&_i2c, data);
00071 }
00072 
00073 void I2CSlave::stop(void)
00074 {
00075     i2c_stop(&_i2c);
00076 }
00077 
00078 }
00079 
00080 #endif