diff --git a/examples/ARC_I2C1/master_reader/master_reader.ino b/examples/ARC_I2C1/master_reader/master_reader.ino new file mode 100644 index 0000000..a1f5983 --- /dev/null +++ b/examples/ARC_I2C1/master_reader/master_reader.ino @@ -0,0 +1,66 @@ +// Copyright (c) 2016 Intel Corporation. All rights reserved. +// See the bottom of this file for the license terms. + +/* + * This sketch is to demonstrate I2C reading from a device, as a bus master. It first makes a request to + * read from the device. Then proceed with reading out one byte at a time and dump in onto the serial term + * for display, in hex. The sketch continue doing the above steps. If the read operation fails, please check + * the connection to the external device: verify the signals, make sure the board and the device share a + * common ground connection. + * + * Please note: + * 1. This sketch makes use of ARC_I2C1. One of the two I2C connections from the ARC core. For the QUARK core I2C + * connection, please refer to the quarkI2C library. + * 2. The ARC_I2C1 connections are not available in the Arduino 101 hardware platform. + * 3. The sketch is accessing an external I2C device with an device number (address) of 8. This address can be + * changed to fit your setup. + */ + +#include + +void setup() +{ + Serial.begin(9600); // start serial for output + while(!Serial); // Wait here till serial terminal is ready/opened + + ARC_I2C1.begin(); // join i2c bus +} + +void loop() +{ + + int ret = ARC_I2C1.requestFrom(8, 20); // request 20 bytes from slave device #8 + if (ret == 0) + { + Serial.println("read from slave device failed"); + } + + while (ARC_I2C1.available()) // slave may send less than requested + { + char c = ARC_I2C1.read(); // receive a byte as character + Serial.println(c, HEX); + } + delay(1000); +} + + +/* + Copyright (c) 2016 Intel Corporation. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + + diff --git a/examples/ARC_I2C1/master_writer/master_writer.ino b/examples/ARC_I2C1/master_writer/master_writer.ino new file mode 100644 index 0000000..ce33caf --- /dev/null +++ b/examples/ARC_I2C1/master_writer/master_writer.ino @@ -0,0 +1,70 @@ +// Copyright (c) 2016 Intel Corporation. All rights reserved. +// See the bottom of this file for the license terms. + +/* + * This sketch is to demonstrate I2C writing to a device, as a bus master. It first makes a request to + * write to the device. Then proceed with write out one byte annd completed the transmission. The sketch + * continues with these steps and increament the byte value each lap. If the write operation fails, please check + * the connection to the external device: verify the signals, make sure the board and the device share a + * common ground connection. + * + * Please note: + * 1. This sketch makes use of ARC_I2C1. One of the two I2C connections from the ARC core. For the QUARK core I2C + * connection, please refer to the quarkI2C library. + * 2. The ARC_I2C1 connections are not available in the Arduino 101 hardware platform. + * 3. The sketch is accessing an external I2C device with an device number (address) of 8. This address can be + * changed to fit your setup. + */ + + +#include + +void setup() +{ + Serial.begin(9600); // start serial for output + while(!Serial); // Wait here till serial terminal is ready/opened + + ARC_I2C1.begin(); // join i2c bus +} + +byte x = 1; +byte rdata; +void loop() +{ + ARC_I2C1.beginTransmission(8); // transmit to device #8 + ARC_I2C1.write(x); // sends one byte + int result = ARC_I2C1.endTransmission(); // stop transmitting + if (result == 0) + { + Serial.print("x = "); + Serial.println(x); + } + else + { + Serial.print("transmit failed with error code "); + Serial.println(result); + } + x++; + delay(500); +} + + +/* + Copyright (c) 2016 Intel Corporation. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + diff --git a/examples/ARC_SPI0/MB85RS64V/MB85RS64V.ino b/examples/ARC_SPI0/MB85RS64V/MB85RS64V.ino new file mode 100644 index 0000000..c61c0f5 --- /dev/null +++ b/examples/ARC_SPI0/MB85RS64V/MB85RS64V.ino @@ -0,0 +1,96 @@ +// Copyright (c) 2016 Intel Corporation. All rights reserved. +// See the bottom of this file for the license terms. + +/* + + Please note: + 1. This sketch makes use of ARC_SPI0. One of the two SPI connections from the ARC core. For the QUARK core SPI + connection, please refer to the SPI library. + 2. The ARC_SPI0 connections are not available in the Arduino 101 hardware platform. + 3. The expected Chip ID is 0x04 + 4. 0xFF denotes connection errors + 5. If you need to read the dummy reg value you can do an immediate memory read +*/ +// This example uses a FRAM SPI chip +// Dataset is here : https://cdn-shop.adafruit.com/datasheets/MB85RS64V-DS501-00015-4v0-E.pdf +// Op codes: +/* Name Description Op-code + WREN Set Write Enable Latch 0000 0110B + WRDI Reset Write Enable Latch 0000 0100B + RDSR Read Status Register 0000 0101B + WRSR Write Status Register 0000 0001B + READ Read Memory Code 0000 0011B + WRITE Write Memory Code 0000 0010B + RDID Read Device ID 1001 1111B +*/ +// The routing on the TT is as follows +/* TPB29 SPI0_SS_MOSI + TPB24 SPI0_SS_MISO + TPB23 SPI0_SS_SCK + TPB22 SPI0_SS_CS0 + TPB27 SPI0_SS_CS1 + TPB16 SPI0_SS_CS3 +*/ + +// include the SPI library: +#include +const uint8_t dummy_reg = 0x7F; +void setup() +{ + + + Serial.begin(9600); // start serial for output + //while (!Serial) { }; + + //Move pins to correct Test Point and call the correct macro + //SPI_SE_1 = CS0 -> TBP22 + //SPI_SE_2 = CS1 -> TPB27 + //SPI_SE_4 = CS3 -> TBP16 + // default now to CS 0 uncomment and move pins as needed + ARC_SPI0.begin(SPI_SE_1); // initialize SPI: + //ARC_SPI0.begin(SPI_SE_2); // initialize SPI: + //ARC_SPI0.begin(SPI_SE_4); // initialize SPI: + + ARC_SPI0.transfer(dummy_reg); +} + +void loop() +{ + Serial.print("Transfer OpCode RDID. Return Value = 0x"); // print the character + uint8_t chip_id = ARC_SPI0.transfer(0x9F); + Serial.println(chip_id, HEX); // print the character + delay(1000); + + //ARC_SPI0.transfer(dummy_reg); + Serial.print("Transfer OpCode READ. Return Value = 0x"); // print the character + uint8_t mem_Read = ARC_SPI0.transfer(0x3); + Serial.println(mem_Read, HEX); // print the character + delay(1000); + + Serial.print("Transfer OpCode RDSR. Return Value = 0x"); // print the character + uint8_t status_Register = ARC_SPI0.transfer(0x5); + Serial.println(status_Register, HEX); // print the character + delay(1000); + + + +} + +/* + Copyright (c) 2016 Intel Corporation. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ diff --git a/examples/master_check/master_check.ino b/examples/Quark_I2C/master_check/master_check.ino similarity index 100% rename from examples/master_check/master_check.ino rename to examples/Quark_I2C/master_check/master_check.ino diff --git a/examples/master_reader/master_reader.ino b/examples/Quark_I2C/master_reader/master_reader.ino similarity index 100% rename from examples/master_reader/master_reader.ino rename to examples/Quark_I2C/master_reader/master_reader.ino diff --git a/examples/master_writer/master_writer.ino b/examples/Quark_I2C/master_writer/master_writer.ino similarity index 100% rename from examples/master_writer/master_writer.ino rename to examples/Quark_I2C/master_writer/master_writer.ino diff --git a/examples/slave_check/slave_check.ino b/examples/Quark_I2C/slave_check/slave_check.ino similarity index 100% rename from examples/slave_check/slave_check.ino rename to examples/Quark_I2C/slave_check/slave_check.ino diff --git a/examples/slave_receiver/slave_receiver.ino b/examples/Quark_I2C/slave_receiver/slave_receiver.ino similarity index 100% rename from examples/slave_receiver/slave_receiver.ino rename to examples/Quark_I2C/slave_receiver/slave_receiver.ino diff --git a/examples/slave_sender/slave_sender.ino b/examples/Quark_I2C/slave_sender/slave_sender.ino similarity index 100% rename from examples/slave_sender/slave_sender.ino rename to examples/Quark_I2C/slave_sender/slave_sender.ino diff --git a/keywords.txt b/keywords.txt index 4d77137..2a7115e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -5,28 +5,35 @@ ####################################### # Datatypes (KEYWORD1) ####################################### -quarkI2C KEYWORD1 +quarkI2C KEYWORD1 +ARC_SPI KEYWORD1 +SPISettings KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) ####################################### -begin KEYWORD2 -end KEYWORD2 -setSpeed KEYWORD2 -setAddressMode KEYWORD2 +begin KEYWORD2 +end KEYWORD2 +setSpeed KEYWORD2 +setAddressMode KEYWORD2 beginTransmission KEYWORD2 -endTransmission KEYWORD2 -requestFrom KEYWORD2 -write KEYWORD2 -available KEYWORD2 -read KEYWORD2 -peek KEYWORD2 -flush KEYWORD2 -onReceive KEYWORD2 -onRequest KEYWORD2 +endTransmission KEYWORD2 +requestFrom KEYWORD2 +write KEYWORD2 +available KEYWORD2 +read KEYWORD2 +peek KEYWORD2 +flush KEYWORD2 +onReceive KEYWORD2 +onRequest KEYWORD2 +setDataMode KEYWORD2 +setClockDivider KEYWORD2 +transfer KEYWORD2 ####################################### # Instances (KEYWORD2) ####################################### -I2C0 KEYWORD2 -I2C1 KEYWORD2 +I2C0 KEYWORD2 +I2C1 KEYWORD2 +ARC_I2C1 KEYWORD2 +ARC_SPI0 KEYWORD2 diff --git a/src/ARC_I2C1.cpp b/src/ARC_I2C1.cpp new file mode 100644 index 0000000..1ab7790 --- /dev/null +++ b/src/ARC_I2C1.cpp @@ -0,0 +1,33 @@ +/* + * ARC_I2C1.cpp is developed based upon the Wire.cpp module. + * + * TwoWire.h - TWI/I2C library for Linux Userspace + * Copyright (c) 2013 Parav https://github.com/meanbot. + * All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Modifications to support Intel Arduino 101 + * Copyright (C) 2015 Intel Corporation + * + * Modifications made to support the two I2C connections of the ARC core + * Copyright (C) 2016 Intel Corporation + */ + + +#include "ARC_I2C1.h" + +TwoWire ARC_I2C1 = TwoWire(I2C_SENSING_1); + diff --git a/src/ARC_I2C1.h b/src/ARC_I2C1.h new file mode 100644 index 0000000..db60d3d --- /dev/null +++ b/src/ARC_I2C1.h @@ -0,0 +1,33 @@ +/* + * ARC_I2C1.h is developed based upon the TwoWire.h module. + * + * TwoWire.h - TWI/I2C library for Linux Userspace + * Copyright (c) 2013 Parav https://github.com/meanbot. + * All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Modifications made to support the two I2C connections of the ARC core + * Copyright (C) 2016 Intel Corporation + */ + +#ifndef _ARC_I2C1_H_ +#define _ARC_I2C1_H_ + +#include "Wire.h" + +extern TwoWire ARC_I2C1; + +#endif diff --git a/src/ARC_SPI0.cpp b/src/ARC_SPI0.cpp new file mode 100644 index 0000000..f09aa5a --- /dev/null +++ b/src/ARC_SPI0.cpp @@ -0,0 +1,64 @@ +/* + * ARC_SPI0.cpp is developed based upon the Wire.cpp module. + * + * TwoWire.h - TWI/I2C library for Linux Userspace + * Copyright (c) 2013 Parav https://github.com/meanbot. + * All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Modifications to support Intel Arduino 101 + * Copyright (C) 2015 Intel Corporation + * + * Modifications made to support the two SPI connections of the ARC core + * Copyright (C) 2016 Intel Corporation + */ + +#include "ARC_SPI0.h" + +void ARC_SPI::begin(SPI_SLAVE_ENABLE spi_cs) +{ + ss_spi_init(controller_id, 2000, SPI_BUSMODE_0, SPI_8_BIT, spi_cs); +} + +void ARC_SPI::end() +{ + ss_spi_disable(controller_id); +} + +void ARC_SPI::beginTransaction(SPISettings settings) +{ + ss_spi_init(controller_id, settings.speedMaximum / 1000, settings.dataMode, SPI_8_BIT, settings.spi_cs); +} + +uint8_t ARC_SPI::transfer(uint8_t data) +{ + uint8_t buffer[1]; + buffer[0] = data; + ss_spi_xfer(controller_id, buffer, 1, 1); + return buffer[0]; +} + +void ARC_SPI::setClockDivider(uint8_t clockDiv) +{ + ss_spi_set_clock_divider(controller_id, clockDiv); +} + +void ARC_SPI::setDataMode(uint8_t dataMode) +{ + ss_spi_set_data_mode(controller_id, dataMode); +} + +ARC_SPI ARC_SPI0 = ARC_SPI(SPI_SENSING_0); diff --git a/src/ARC_SPI0.h b/src/ARC_SPI0.h new file mode 100644 index 0000000..75f91af --- /dev/null +++ b/src/ARC_SPI0.h @@ -0,0 +1,82 @@ +/* + * ARC_SPI0.h is developed based upon the TwoWire.h module. + * + * TwoWire.h - TWI/I2C library for Linux Userspace + * Copyright (c) 2013 Parav https://github.com/meanbot. + * All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Modifications made to support the two SPI connections of the ARC core + * Copyright (C) 2016 Intel Corporation + */ + +#ifndef _ARC_SPI_H_ +#define _ARC_SPI_H_ + +#include "ss_spi.h" + +#define LSBFIRST 0 +#define MSBFIRST 1 +#define SPI_MODE0 0x00 +#define SPI_MODE1 0x01 +#define SPI_MODE2 0x02 +#define SPI_MODE3 0x03 + +class SPISettings +{ + public: + SPISettings(uint32_t _speedMaximum, uint8_t _dataOrder, uint8_t _dataMode, SPI_SLAVE_ENABLE _spi_cs=SPI_SE_1) + : speedMaximum(_speedMaximum), dataOrder(_dataOrder), + dataMode((SPI_BUS_MODE)_dataMode), spi_cs(_spi_cs) + { + } + + SPISettings() + { + SPISettings(2000000, MSBFIRST, SPI_MODE0); + } + + private: + uint32_t speedMaximum; + uint8_t dataOrder; + SPI_BUS_MODE dataMode; + SPI_SLAVE_ENABLE spi_cs; + friend class ARC_SPI; +}; + +class ARC_SPI +{ +public: + ARC_SPI(SPI_CONTROLLER _controller_id) : controller_id(_controller_id) + { + } + + void begin(SPI_SLAVE_ENABLE spi_cs); + void end(); + + void beginTransaction(SPISettings settings); + + void setDataMode(uint8_t dataMode); + void setClockDivider(uint8_t clockDiv); + + uint8_t transfer(uint8_t data); + + private: + SPI_CONTROLLER controller_id; +}; +extern ARC_SPI ARC_SPI0; + +#endif diff --git a/src/quarkI2C.cpp b/src/quarkI2C.cpp index 28ff422..db8be95 100644 --- a/src/quarkI2C.cpp +++ b/src/quarkI2C.cpp @@ -50,7 +50,7 @@ quarkI2C::quarkI2C(SOC_I2C_CONTROLLER controller_id) : controllerId(controller_i void quarkI2C::begin(void) { - int i2c_speed = I2C_SPEED_FAST; + int i2c_speed = QUARKI2C_I2C_SPEED_FAST; int i2c_addr_mode = I2C_ADDR_7Bit; init_status = soc_i2c_open_adapter(controllerId, 0, i2c_speed, i2c_addr_mode); } @@ -92,8 +92,8 @@ uint8_t quarkI2C::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { int ret; - if (quantity > BUFFER_LENGTH) - quantity = BUFFER_LENGTH; + if (quantity > QUARK_I2C_BUFFER_LENGTH) + quantity = QUARK_I2C_BUFFER_LENGTH; /* Set slave address via ioctl */ soc_i2c_master_set_slave_address(controllerId, address); @@ -116,8 +116,8 @@ uint8_t quarkI2C::requestFrom(uint8_t address, uint8_t quantity) uint8_t quarkI2C::requestFrom(int address, int quantity, int sendStop) { int ret; - if (quantity > BUFFER_LENGTH) - quantity = BUFFER_LENGTH; + if (quantity > QUARK_I2C_BUFFER_LENGTH) + quantity = QUARK_I2C_BUFFER_LENGTH; /* Set slave address via ioctl */ soc_i2c_master_set_slave_address(controllerId, address); @@ -201,7 +201,7 @@ uint8_t quarkI2C::endTransmission(void) size_t quarkI2C::write(uint8_t data) { - if (txBufferLength >= BUFFER_LENGTH) + if (txBufferLength >= QUARK_I2C_BUFFER_LENGTH) return 0; txBuffer[txBufferLength++] = data; return 1; @@ -210,7 +210,7 @@ size_t quarkI2C::write(uint8_t data) size_t quarkI2C::write(const uint8_t *data, size_t quantity) { for (size_t i = 0; i < quantity; ++i) { - if (txBufferLength >= BUFFER_LENGTH) + if (txBufferLength >= QUARK_I2C_BUFFER_LENGTH) return i; txBuffer[txBufferLength++] = data[i]; } diff --git a/src/quarkI2C.h b/src/quarkI2C.h index 9b06987..0a8001a 100644 --- a/src/quarkI2C.h +++ b/src/quarkI2C.h @@ -30,12 +30,12 @@ #include "variant.h" #include "intel_qrk_i2c.h" -const int BUFFER_LENGTH = 32; +const int QUARK_I2C_BUFFER_LENGTH = 32; -const int I2C_SPEED_SLOW = 1; -const int I2C_SPEED_FAST = 2; -const int I2C_SPEED_HS = 3; +const int QUARKI2C_I2C_SPEED_SLOW = 1; +const int QUARKI2C_I2C_SPEED_FAST = 2; +const int I2C_SPEED_HS = 3; const int I2C_ADDR_7Bit = 0; const int I2C_ADDR_10Bit = 1; @@ -44,9 +44,9 @@ class quarkI2C : public Stream public: quarkI2C(SOC_I2C_CONTROLLER controller_id); void begin(); - void begin(uint8_t, int i2c_speed = I2C_SPEED_FAST, + void begin(uint8_t, int i2c_speed = QUARKI2C_I2C_SPEED_FAST, int i2c_addr_mode = I2C_ADDR_7Bit); - void begin(int, int i2c_speed = I2C_SPEED_FAST, + void begin(int, int i2c_speed = QUARKI2C_I2C_SPEED_FAST, int i2c_addr_mode = I2C_ADDR_7Bit); void end(); void setSpeed(uint32_t); @@ -88,12 +88,12 @@ class quarkI2C : public Stream using Print::write; private: - uint8_t rxBuffer[BUFFER_LENGTH]; + uint8_t rxBuffer[QUARK_I2C_BUFFER_LENGTH]; uint8_t rxBufferIndex; uint8_t rxBufferLength; uint8_t txAddress; - uint8_t txBuffer[BUFFER_LENGTH]; + uint8_t txBuffer[QUARK_I2C_BUFFER_LENGTH]; uint8_t txBufferLength; void (*onRequestUserCallback)(void);