![](/media/cache/profiles/5f55d0baa59f4bc1dc393149183f1492.jpg.50x50_q85.jpg)
wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/atmel-rf-driver/source/at24mac.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /* |
JMF | 0:24d3eb812fd4 | 2 | * Copyright (c) 2016-2016 ARM Limited. All rights reserved. |
JMF | 0:24d3eb812fd4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
JMF | 0:24d3eb812fd4 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
JMF | 0:24d3eb812fd4 | 5 | * not use this file except in compliance with the License. |
JMF | 0:24d3eb812fd4 | 6 | * You may obtain a copy of the License at |
JMF | 0:24d3eb812fd4 | 7 | * |
JMF | 0:24d3eb812fd4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:24d3eb812fd4 | 9 | * |
JMF | 0:24d3eb812fd4 | 10 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:24d3eb812fd4 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
JMF | 0:24d3eb812fd4 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:24d3eb812fd4 | 13 | * See the License for the specific language governing permissions and |
JMF | 0:24d3eb812fd4 | 14 | * limitations under the License. |
JMF | 0:24d3eb812fd4 | 15 | */ |
JMF | 0:24d3eb812fd4 | 16 | #include "at24mac.h" |
JMF | 0:24d3eb812fd4 | 17 | |
JMF | 0:24d3eb812fd4 | 18 | /* Device addressing */ |
JMF | 0:24d3eb812fd4 | 19 | #define AT24MAC_EEPROM_ADDRESS (0x0A<<4) |
JMF | 0:24d3eb812fd4 | 20 | #define AT24MAC_RW_PROTECT_ADDRESS (0x06<<4) |
JMF | 0:24d3eb812fd4 | 21 | #define AT24MAC_SERIAL_ADDRESS (0x0B<<4) |
JMF | 0:24d3eb812fd4 | 22 | |
JMF | 0:24d3eb812fd4 | 23 | /* Known memory blocks */ |
JMF | 0:24d3eb812fd4 | 24 | #define AT24MAC_SERIAL_OFFSET (0x80) |
JMF | 0:24d3eb812fd4 | 25 | #define AT24MAC_EUI64_OFFSET (0x98) |
JMF | 0:24d3eb812fd4 | 26 | #define AT24MAC_EUI48_OFFSET (0x9A) |
JMF | 0:24d3eb812fd4 | 27 | |
JMF | 0:24d3eb812fd4 | 28 | #define SERIAL_LEN 16 |
JMF | 0:24d3eb812fd4 | 29 | #define EUI64_LEN 8 |
JMF | 0:24d3eb812fd4 | 30 | #define EUI48_LEN 6 |
JMF | 0:24d3eb812fd4 | 31 | |
JMF | 0:24d3eb812fd4 | 32 | AT24Mac::I2CReset::I2CReset(PinName sda, PinName scl) |
JMF | 0:24d3eb812fd4 | 33 | { |
JMF | 0:24d3eb812fd4 | 34 | mbed::DigitalInOut SDA(sda, PIN_OUTPUT, PullUp, 1); |
JMF | 0:24d3eb812fd4 | 35 | mbed::DigitalInOut SCL(scl, PIN_OUTPUT, PullUp, 0); |
JMF | 0:24d3eb812fd4 | 36 | //generate 9 clocks for worst-case scenario |
JMF | 0:24d3eb812fd4 | 37 | for (int i = 0; i < 10; ++i) { |
JMF | 0:24d3eb812fd4 | 38 | SCL = 1; |
JMF | 0:24d3eb812fd4 | 39 | wait_us(5); |
JMF | 0:24d3eb812fd4 | 40 | SCL = 0; |
JMF | 0:24d3eb812fd4 | 41 | wait_us(5); |
JMF | 0:24d3eb812fd4 | 42 | } |
JMF | 0:24d3eb812fd4 | 43 | //generate a STOP condition |
JMF | 0:24d3eb812fd4 | 44 | SDA = 0; |
JMF | 0:24d3eb812fd4 | 45 | wait_us(5); |
JMF | 0:24d3eb812fd4 | 46 | SCL = 1; |
JMF | 0:24d3eb812fd4 | 47 | wait_us(5); |
JMF | 0:24d3eb812fd4 | 48 | SDA = 1; |
JMF | 0:24d3eb812fd4 | 49 | wait_us(5); |
JMF | 0:24d3eb812fd4 | 50 | } |
JMF | 0:24d3eb812fd4 | 51 | |
JMF | 0:24d3eb812fd4 | 52 | /*I2C needs to be reset before constructing the I2C object (in case I2C is stuck) |
JMF | 0:24d3eb812fd4 | 53 | because they use the same pins, therefore i2c_reset has to be before _i2c |
JMF | 0:24d3eb812fd4 | 54 | in the initializer list*/ |
JMF | 0:24d3eb812fd4 | 55 | AT24Mac::AT24Mac(PinName sda, PinName scl) : i2c_reset(sda, scl), _i2c(sda, scl) |
JMF | 0:24d3eb812fd4 | 56 | { |
JMF | 0:24d3eb812fd4 | 57 | // Do nothing |
JMF | 0:24d3eb812fd4 | 58 | } |
JMF | 0:24d3eb812fd4 | 59 | |
JMF | 0:24d3eb812fd4 | 60 | int AT24Mac::read_serial(void *buf) |
JMF | 0:24d3eb812fd4 | 61 | { |
JMF | 0:24d3eb812fd4 | 62 | char offset = AT24MAC_SERIAL_OFFSET; |
JMF | 0:24d3eb812fd4 | 63 | if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true)) |
JMF | 0:24d3eb812fd4 | 64 | return -1; //No ACK |
JMF | 0:24d3eb812fd4 | 65 | return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, SERIAL_LEN); |
JMF | 0:24d3eb812fd4 | 66 | } |
JMF | 0:24d3eb812fd4 | 67 | |
JMF | 0:24d3eb812fd4 | 68 | int AT24Mac::read_eui64(void *buf) |
JMF | 0:24d3eb812fd4 | 69 | { |
JMF | 0:24d3eb812fd4 | 70 | char offset = AT24MAC_EUI64_OFFSET; |
JMF | 0:24d3eb812fd4 | 71 | if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true)) |
JMF | 0:24d3eb812fd4 | 72 | return -1; //No ACK |
JMF | 0:24d3eb812fd4 | 73 | return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, EUI64_LEN); |
JMF | 0:24d3eb812fd4 | 74 | } |
JMF | 0:24d3eb812fd4 | 75 | |
JMF | 0:24d3eb812fd4 | 76 | int AT24Mac::read_eui48(void *buf) |
JMF | 0:24d3eb812fd4 | 77 | { |
JMF | 0:24d3eb812fd4 | 78 | char offset = AT24MAC_EUI48_OFFSET; |
JMF | 0:24d3eb812fd4 | 79 | if (_i2c.write(AT24MAC_SERIAL_ADDRESS, &offset, 1, true)) |
JMF | 0:24d3eb812fd4 | 80 | return -1; //No ACK |
JMF | 0:24d3eb812fd4 | 81 | return _i2c.read(AT24MAC_SERIAL_ADDRESS, (char*)buf, EUI48_LEN); |
JMF | 0:24d3eb812fd4 | 82 | } |