Version of easy-connect with the u-blox cellular platforms C027 and C030 added.

Dependents:   HelloMQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers at24mac.cpp Source File

at24mac.cpp

00001 /*
00002  * Copyright (c) 2016-2016 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * 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, WITHOUT
00012  * 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 "at24mac.h"
00017 
00018 /* Device addressing */
00019 #define AT24MAC_EEPROM_ADDRESS      (0x0A<<4)
00020 #define AT24MAC_RW_PROTECT_ADDRESS  (0x06<<4)
00021 #define AT24MAC_SERIAL_ADDRESS      (0x0B<<4)
00022 
00023 /* Known memory blocks */
00024 #define AT24MAC_SERIAL_OFFSET   (0x80)
00025 #define AT24MAC_EUI64_OFFSET    (0x98)
00026 #define AT24MAC_EUI48_OFFSET    (0x9A)
00027 
00028 #define SERIAL_LEN 16
00029 #define EUI64_LEN 8
00030 #define EUI48_LEN 6
00031 
00032 AT24Mac::I2CReset::I2CReset(PinName sda, PinName scl)
00033 {
00034     mbed::DigitalInOut SDA(sda, PIN_OUTPUT, PullUp, 1);
00035     mbed::DigitalInOut SCL(scl, PIN_OUTPUT, PullUp, 0);
00036     //generate 9 clocks for worst-case scenario
00037     for (int i = 0; i < 10; ++i) {
00038         SCL = 1;
00039         wait_us(5);
00040         SCL = 0;
00041         wait_us(5);
00042     }
00043     //generate a STOP condition
00044     SDA = 0;
00045     wait_us(5);
00046     SCL = 1;
00047     wait_us(5);
00048     SDA = 1;
00049     wait_us(5);
00050 }
00051 
00052 /*I2C needs to be reset before constructing the I2C object (in case I2C is stuck)
00053   because they use the same pins, therefore i2c_reset has to be before _i2c
00054   in the initializer list*/
00055 AT24Mac::AT24Mac(PinName sda, PinName scl) : i2c_reset(sda, scl), _i2c(sda, scl)
00056 {
00057     // Do nothing
00058 }
00059 
00060 int AT24Mac::read_serial(void *buf)
00061 {
00062     char offset = AT24MAC_SERIAL_OFFSET;
00063     if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true))
00064         return -1; //No ACK
00065     return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, SERIAL_LEN);
00066 }
00067 
00068 int AT24Mac::read_eui64(void *buf)
00069 {
00070     char offset = AT24MAC_EUI64_OFFSET;
00071     if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true))
00072         return -1; //No ACK
00073     return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, EUI64_LEN);
00074 }
00075 
00076 int AT24Mac::read_eui48(void *buf)
00077 {
00078     char offset = AT24MAC_EUI48_OFFSET;
00079     if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true))
00080         return -1; //No ACK
00081     return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, EUI48_LEN);
00082 }