PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
Pokitto
Date:
Wed Oct 11 20:35:27 2017 +0000
Revision:
5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src

Who changed what in which revision?

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