MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.

Committer:
dgabino
Date:
Wed Apr 11 14:42:47 2018 +0000
Revision:
0:c76361bd82e8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgabino 0:c76361bd82e8 1 /* mbed Microcontroller Library
dgabino 0:c76361bd82e8 2 * Copyright (c) 2006-2013 ARM Limited
dgabino 0:c76361bd82e8 3 *
dgabino 0:c76361bd82e8 4 * Licensed under the Apache License, Version 2.0 (the "License");
dgabino 0:c76361bd82e8 5 * you may not use this file except in compliance with the License.
dgabino 0:c76361bd82e8 6 * You may obtain a copy of the License at
dgabino 0:c76361bd82e8 7 *
dgabino 0:c76361bd82e8 8 * http://www.apache.org/licenses/LICENSE-2.0
dgabino 0:c76361bd82e8 9 *
dgabino 0:c76361bd82e8 10 * Unless required by applicable law or agreed to in writing, software
dgabino 0:c76361bd82e8 11 * distributed under the License is distributed on an "AS IS" BASIS,
dgabino 0:c76361bd82e8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dgabino 0:c76361bd82e8 13 * See the License for the specific language governing permissions and
dgabino 0:c76361bd82e8 14 * limitations under the License.
dgabino 0:c76361bd82e8 15 */
dgabino 0:c76361bd82e8 16 #ifndef MBED_I2C_SLAVE_H
dgabino 0:c76361bd82e8 17 #define MBED_I2C_SLAVE_H
dgabino 0:c76361bd82e8 18
dgabino 0:c76361bd82e8 19 #include "platform.h"
dgabino 0:c76361bd82e8 20
dgabino 0:c76361bd82e8 21 #if DEVICE_I2CSLAVE
dgabino 0:c76361bd82e8 22
dgabino 0:c76361bd82e8 23 #include "i2c_api.h"
dgabino 0:c76361bd82e8 24
dgabino 0:c76361bd82e8 25 namespace mbed {
dgabino 0:c76361bd82e8 26
dgabino 0:c76361bd82e8 27 /** An I2C Slave, used for communicating with an I2C Master device
dgabino 0:c76361bd82e8 28 *
dgabino 0:c76361bd82e8 29 * @Note Synchronization level: Not protected
dgabino 0:c76361bd82e8 30 *
dgabino 0:c76361bd82e8 31 * Example:
dgabino 0:c76361bd82e8 32 * @code
dgabino 0:c76361bd82e8 33 * // Simple I2C responder
dgabino 0:c76361bd82e8 34 * #include <mbed.h>
dgabino 0:c76361bd82e8 35 *
dgabino 0:c76361bd82e8 36 * I2CSlave slave(p9, p10);
dgabino 0:c76361bd82e8 37 *
dgabino 0:c76361bd82e8 38 * int main() {
dgabino 0:c76361bd82e8 39 * char buf[10];
dgabino 0:c76361bd82e8 40 * char msg[] = "Slave!";
dgabino 0:c76361bd82e8 41 *
dgabino 0:c76361bd82e8 42 * slave.address(0xA0);
dgabino 0:c76361bd82e8 43 * while (1) {
dgabino 0:c76361bd82e8 44 * int i = slave.receive();
dgabino 0:c76361bd82e8 45 * switch (i) {
dgabino 0:c76361bd82e8 46 * case I2CSlave::ReadAddressed:
dgabino 0:c76361bd82e8 47 * slave.write(msg, strlen(msg) + 1); // Includes null char
dgabino 0:c76361bd82e8 48 * break;
dgabino 0:c76361bd82e8 49 * case I2CSlave::WriteGeneral:
dgabino 0:c76361bd82e8 50 * slave.read(buf, 10);
dgabino 0:c76361bd82e8 51 * printf("Read G: %s\n", buf);
dgabino 0:c76361bd82e8 52 * break;
dgabino 0:c76361bd82e8 53 * case I2CSlave::WriteAddressed:
dgabino 0:c76361bd82e8 54 * slave.read(buf, 10);
dgabino 0:c76361bd82e8 55 * printf("Read A: %s\n", buf);
dgabino 0:c76361bd82e8 56 * break;
dgabino 0:c76361bd82e8 57 * }
dgabino 0:c76361bd82e8 58 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
dgabino 0:c76361bd82e8 59 * }
dgabino 0:c76361bd82e8 60 * }
dgabino 0:c76361bd82e8 61 * @endcode
dgabino 0:c76361bd82e8 62 */
dgabino 0:c76361bd82e8 63 class I2CSlave {
dgabino 0:c76361bd82e8 64
dgabino 0:c76361bd82e8 65 public:
dgabino 0:c76361bd82e8 66 enum RxStatus {
dgabino 0:c76361bd82e8 67 NoData = 0,
dgabino 0:c76361bd82e8 68 ReadAddressed = 1,
dgabino 0:c76361bd82e8 69 WriteGeneral = 2,
dgabino 0:c76361bd82e8 70 WriteAddressed = 3
dgabino 0:c76361bd82e8 71 };
dgabino 0:c76361bd82e8 72
dgabino 0:c76361bd82e8 73 /** Create an I2C Slave interface, connected to the specified pins.
dgabino 0:c76361bd82e8 74 *
dgabino 0:c76361bd82e8 75 * @param sda I2C data line pin
dgabino 0:c76361bd82e8 76 * @param scl I2C clock line pin
dgabino 0:c76361bd82e8 77 */
dgabino 0:c76361bd82e8 78 I2CSlave(PinName sda, PinName scl);
dgabino 0:c76361bd82e8 79
dgabino 0:c76361bd82e8 80 /** Set the frequency of the I2C interface
dgabino 0:c76361bd82e8 81 *
dgabino 0:c76361bd82e8 82 * @param hz The bus frequency in hertz
dgabino 0:c76361bd82e8 83 */
dgabino 0:c76361bd82e8 84 void frequency(int hz);
dgabino 0:c76361bd82e8 85
dgabino 0:c76361bd82e8 86 /** Checks to see if this I2C Slave has been addressed.
dgabino 0:c76361bd82e8 87 *
dgabino 0:c76361bd82e8 88 * @returns
dgabino 0:c76361bd82e8 89 * A status indicating if the device has been addressed, and how
dgabino 0:c76361bd82e8 90 * - NoData - the slave has not been addressed
dgabino 0:c76361bd82e8 91 * - ReadAddressed - the master has requested a read from this slave
dgabino 0:c76361bd82e8 92 * - WriteAddressed - the master is writing to this slave
dgabino 0:c76361bd82e8 93 * - WriteGeneral - the master is writing to all slave
dgabino 0:c76361bd82e8 94 */
dgabino 0:c76361bd82e8 95 int receive(void);
dgabino 0:c76361bd82e8 96
dgabino 0:c76361bd82e8 97 /** Read from an I2C master.
dgabino 0:c76361bd82e8 98 *
dgabino 0:c76361bd82e8 99 * @param data pointer to the byte array to read data in to
dgabino 0:c76361bd82e8 100 * @param length maximum number of bytes to read
dgabino 0:c76361bd82e8 101 *
dgabino 0:c76361bd82e8 102 * @returns
dgabino 0:c76361bd82e8 103 * 0 on success,
dgabino 0:c76361bd82e8 104 * non-0 otherwise
dgabino 0:c76361bd82e8 105 */
dgabino 0:c76361bd82e8 106 int read(char *data, int length);
dgabino 0:c76361bd82e8 107
dgabino 0:c76361bd82e8 108 /** Read a single byte from an I2C master.
dgabino 0:c76361bd82e8 109 *
dgabino 0:c76361bd82e8 110 * @returns
dgabino 0:c76361bd82e8 111 * the byte read
dgabino 0:c76361bd82e8 112 */
dgabino 0:c76361bd82e8 113 int read(void);
dgabino 0:c76361bd82e8 114
dgabino 0:c76361bd82e8 115 /** Write to an I2C master.
dgabino 0:c76361bd82e8 116 *
dgabino 0:c76361bd82e8 117 * @param data pointer to the byte array to be transmitted
dgabino 0:c76361bd82e8 118 * @param length the number of bytes to transmite
dgabino 0:c76361bd82e8 119 *
dgabino 0:c76361bd82e8 120 * @returns
dgabino 0:c76361bd82e8 121 * 0 on success,
dgabino 0:c76361bd82e8 122 * non-0 otherwise
dgabino 0:c76361bd82e8 123 */
dgabino 0:c76361bd82e8 124 int write(const char *data, int length);
dgabino 0:c76361bd82e8 125
dgabino 0:c76361bd82e8 126 /** Write a single byte to an I2C master.
dgabino 0:c76361bd82e8 127 *
dgabino 0:c76361bd82e8 128 * @data the byte to write
dgabino 0:c76361bd82e8 129 *
dgabino 0:c76361bd82e8 130 * @returns
dgabino 0:c76361bd82e8 131 * '1' if an ACK was received,
dgabino 0:c76361bd82e8 132 * '0' otherwise
dgabino 0:c76361bd82e8 133 */
dgabino 0:c76361bd82e8 134 int write(int data);
dgabino 0:c76361bd82e8 135
dgabino 0:c76361bd82e8 136 /** Sets the I2C slave address.
dgabino 0:c76361bd82e8 137 *
dgabino 0:c76361bd82e8 138 * @param address The address to set for the slave (ignoring the least
dgabino 0:c76361bd82e8 139 * signifcant bit). If set to 0, the slave will only respond to the
dgabino 0:c76361bd82e8 140 * general call address.
dgabino 0:c76361bd82e8 141 */
dgabino 0:c76361bd82e8 142 void address(int address);
dgabino 0:c76361bd82e8 143
dgabino 0:c76361bd82e8 144 /** Reset the I2C slave back into the known ready receiving state.
dgabino 0:c76361bd82e8 145 */
dgabino 0:c76361bd82e8 146 void stop(void);
dgabino 0:c76361bd82e8 147
dgabino 0:c76361bd82e8 148 protected:
dgabino 0:c76361bd82e8 149 i2c_t _i2c;
dgabino 0:c76361bd82e8 150 };
dgabino 0:c76361bd82e8 151
dgabino 0:c76361bd82e8 152 } // namespace mbed
dgabino 0:c76361bd82e8 153
dgabino 0:c76361bd82e8 154 #endif
dgabino 0:c76361bd82e8 155
dgabino 0:c76361bd82e8 156 #endif