my customized lib

Committer:
DuyLionTran
Date:
Sun Nov 26 15:08:14 2017 +0000
Revision:
0:8094b249013c
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 0:8094b249013c 1 /*
DuyLionTran 0:8094b249013c 2 * Copyright (c) 2016-2016 ARM Limited. All rights reserved.
DuyLionTran 0:8094b249013c 3 * SPDX-License-Identifier: Apache-2.0
DuyLionTran 0:8094b249013c 4 * Licensed under the Apache License, Version 2.0 (the License); you may
DuyLionTran 0:8094b249013c 5 * not use this file except in compliance with the License.
DuyLionTran 0:8094b249013c 6 * You may obtain a copy of the License at
DuyLionTran 0:8094b249013c 7 *
DuyLionTran 0:8094b249013c 8 * http://www.apache.org/licenses/LICENSE-2.0
DuyLionTran 0:8094b249013c 9 *
DuyLionTran 0:8094b249013c 10 * Unless required by applicable law or agreed to in writing, software
DuyLionTran 0:8094b249013c 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
DuyLionTran 0:8094b249013c 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
DuyLionTran 0:8094b249013c 13 * See the License for the specific language governing permissions and
DuyLionTran 0:8094b249013c 14 * limitations under the License.
DuyLionTran 0:8094b249013c 15 */
DuyLionTran 0:8094b249013c 16 #include "at24mac.h"
DuyLionTran 0:8094b249013c 17
DuyLionTran 0:8094b249013c 18 /* Device addressing */
DuyLionTran 0:8094b249013c 19 #define AT24MAC_EEPROM_ADDRESS (0x0A<<4)
DuyLionTran 0:8094b249013c 20 #define AT24MAC_RW_PROTECT_ADDRESS (0x06<<4)
DuyLionTran 0:8094b249013c 21 #define AT24MAC_SERIAL_ADDRESS (0x0B<<4)
DuyLionTran 0:8094b249013c 22
DuyLionTran 0:8094b249013c 23 /* Known memory blocks */
DuyLionTran 0:8094b249013c 24 #define AT24MAC_SERIAL_OFFSET (0x80)
DuyLionTran 0:8094b249013c 25 #define AT24MAC_EUI64_OFFSET (0x98)
DuyLionTran 0:8094b249013c 26 #define AT24MAC_EUI48_OFFSET (0x9A)
DuyLionTran 0:8094b249013c 27
DuyLionTran 0:8094b249013c 28 #define SERIAL_LEN 16
DuyLionTran 0:8094b249013c 29 #define EUI64_LEN 8
DuyLionTran 0:8094b249013c 30 #define EUI48_LEN 6
DuyLionTran 0:8094b249013c 31
DuyLionTran 0:8094b249013c 32 AT24Mac::AT24Mac(PinName sda, PinName scl) : _i2c(sda, scl)
DuyLionTran 0:8094b249013c 33 {
DuyLionTran 0:8094b249013c 34 // Do nothing
DuyLionTran 0:8094b249013c 35 }
DuyLionTran 0:8094b249013c 36
DuyLionTran 0:8094b249013c 37 int AT24Mac::read_serial(void *buf)
DuyLionTran 0:8094b249013c 38 {
DuyLionTran 0:8094b249013c 39 char offset = AT24MAC_SERIAL_OFFSET;
DuyLionTran 0:8094b249013c 40 if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true))
DuyLionTran 0:8094b249013c 41 return -1; //No ACK
DuyLionTran 0:8094b249013c 42 return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, SERIAL_LEN);
DuyLionTran 0:8094b249013c 43 }
DuyLionTran 0:8094b249013c 44
DuyLionTran 0:8094b249013c 45 int AT24Mac::read_eui64(void *buf)
DuyLionTran 0:8094b249013c 46 {
DuyLionTran 0:8094b249013c 47 char offset = AT24MAC_EUI64_OFFSET;
DuyLionTran 0:8094b249013c 48 if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true))
DuyLionTran 0:8094b249013c 49 return -1; //No ACK
DuyLionTran 0:8094b249013c 50 return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, EUI64_LEN);
DuyLionTran 0:8094b249013c 51 }
DuyLionTran 0:8094b249013c 52
DuyLionTran 0:8094b249013c 53 int AT24Mac::read_eui48(void *buf)
DuyLionTran 0:8094b249013c 54 {
DuyLionTran 0:8094b249013c 55 char offset = AT24MAC_EUI48_OFFSET;
DuyLionTran 0:8094b249013c 56 if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true))
DuyLionTran 0:8094b249013c 57 return -1; //No ACK
DuyLionTran 0:8094b249013c 58 return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, EUI48_LEN);
DuyLionTran 0:8094b249013c 59 }