FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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