mirror of
https://github.com/esp8266/Arduino.git
synced 2025-10-15 11:26:40 +03:00
Moved 'sam' platform inside arduino package.
This commit is contained in:
75
hardware/arduino/sam/libraries/SPI/SPI.cpp
Normal file
75
hardware/arduino/sam/libraries/SPI/SPI.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
* SPI Master library for arduino.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "SPI.h"
|
||||
|
||||
SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
|
||||
spi(_spi), id(_id), initCb(_initCb) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
void SPIClass::begin() {
|
||||
initCb();
|
||||
|
||||
// Set CS on NPCS3
|
||||
SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PCS(0x07));
|
||||
SPI_Enable( spi);
|
||||
setClockDivider(1);
|
||||
}
|
||||
|
||||
void SPIClass::end() {
|
||||
SPI_Disable( spi);
|
||||
}
|
||||
|
||||
void SPIClass::setBitOrder(uint8_t bitOrder) {
|
||||
// Not supported
|
||||
}
|
||||
|
||||
void SPIClass::setDataMode(uint8_t _mode) {
|
||||
mode = _mode;
|
||||
SPI_ConfigureNPCS(spi, 3, mode | SPI_CSR_SCBR(divider));
|
||||
}
|
||||
|
||||
void SPIClass::setClockDivider(uint8_t _divider) {
|
||||
divider = _divider;
|
||||
SPI_ConfigureNPCS(spi, 3, mode | SPI_CSR_SCBR(divider));
|
||||
}
|
||||
|
||||
byte SPIClass::transfer(byte _data) {
|
||||
SPI_Write(spi, 0, _data);
|
||||
return SPI_Read(spi);
|
||||
}
|
||||
|
||||
void SPIClass::attachInterrupt(void) {
|
||||
// Should be enableInterrupt()
|
||||
}
|
||||
|
||||
void SPIClass::detachInterrupt(void) {
|
||||
// Should be disableInterrupt()
|
||||
}
|
||||
|
||||
#if SPI_INTERFACES_COUNT > 0
|
||||
static void SPI0_Init(void) {
|
||||
PIO_Configure(g_APinDescription[PIN_SPI_MOSI].pPort,
|
||||
g_APinDescription[PIN_SPI_MOSI].ulPinType,
|
||||
g_APinDescription[PIN_SPI_MOSI].ulPin,
|
||||
g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
|
||||
PIO_Configure(g_APinDescription[PIN_SPI_MISO].pPort,
|
||||
g_APinDescription[PIN_SPI_MISO].ulPinType,
|
||||
g_APinDescription[PIN_SPI_MISO].ulPin,
|
||||
g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
|
||||
PIO_Configure(g_APinDescription[PIN_SPI_SCK].pPort,
|
||||
g_APinDescription[PIN_SPI_SCK].ulPinType,
|
||||
g_APinDescription[PIN_SPI_SCK].ulPin,
|
||||
g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
|
||||
}
|
||||
|
||||
SPIClass SPI0(SPI_INTERFACE, SPI_INTERFACE_ID, SPI0_Init);
|
||||
#endif
|
63
hardware/arduino/sam/libraries/SPI/SPI.h
Normal file
63
hardware/arduino/sam/libraries/SPI/SPI.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
* SPI Master library for arduino.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef _SPI_H_INCLUDED
|
||||
#define _SPI_H_INCLUDED
|
||||
|
||||
#include "variant.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define SPI_CLOCK_DIV4 0x00
|
||||
#define SPI_CLOCK_DIV16 0x01
|
||||
#define SPI_CLOCK_DIV64 0x02
|
||||
#define SPI_CLOCK_DIV128 0x03
|
||||
#define SPI_CLOCK_DIV2 0x04
|
||||
#define SPI_CLOCK_DIV8 0x05
|
||||
#define SPI_CLOCK_DIV32 0x06
|
||||
#define SPI_CLOCK_DIV64 0x07
|
||||
|
||||
#define SPI_MODE0 0x00
|
||||
#define SPI_MODE1 0x02
|
||||
#define SPI_MODE2 0x01
|
||||
#define SPI_MODE3 0x03
|
||||
|
||||
#define SPI_MODE_MASK 0x03 // CPOL = bit 3, CPHA = bit 2 on SPCR
|
||||
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
|
||||
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
|
||||
|
||||
class SPIClass {
|
||||
public:
|
||||
SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void));
|
||||
|
||||
byte transfer(byte _data);
|
||||
|
||||
// SPI Configuration methods
|
||||
|
||||
void attachInterrupt(void);
|
||||
void detachInterrupt(void); // Default
|
||||
|
||||
void begin(void); // Default
|
||||
void end(void);
|
||||
|
||||
void setBitOrder(uint8_t);
|
||||
void setDataMode(uint8_t);
|
||||
void setClockDivider(uint8_t);
|
||||
|
||||
private:
|
||||
Spi *spi;
|
||||
uint32_t id, divider, mode;
|
||||
void (*initCb)(void);
|
||||
};
|
||||
|
||||
#if SPI_INTERFACES_COUNT > 0
|
||||
extern SPIClass SPI0;
|
||||
#endif
|
||||
|
||||
#endif
|
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
SCP1000 Barometric Pressure Sensor Display
|
||||
|
||||
Shows the output of a Barometric Pressure Sensor on a
|
||||
Uses the SPI library. For details on the sensor, see:
|
||||
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
|
||||
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
|
||||
|
||||
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
|
||||
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
|
||||
|
||||
Circuit:
|
||||
SCP1000 sensor attached to pins 6, 7, 10 - 13:
|
||||
DRDY: pin 6
|
||||
CSB: pin 7
|
||||
MOSI: pin 11
|
||||
MISO: pin 12
|
||||
SCK: pin 13
|
||||
|
||||
created 31 July 2010
|
||||
modified 14 August 2010
|
||||
by Tom Igoe
|
||||
*/
|
||||
|
||||
// the sensor communicates using SPI, so include the library:
|
||||
#include <SPI.h>
|
||||
|
||||
//Sensor's memory register addresses:
|
||||
const int PRESSURE = 0x1F; //3 most significant bits of pressure
|
||||
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
|
||||
const int TEMPERATURE = 0x21; //16 bit temperature reading
|
||||
const byte READ = 0b11111100; // SCP1000's read command
|
||||
const byte WRITE = 0b00000010; // SCP1000's write command
|
||||
|
||||
// pins used for the connection with the sensor
|
||||
// the other you need are controlled by the SPI library):
|
||||
const int dataReadyPin = 6;
|
||||
const int chipSelectPin = 7;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
// start the SPI library:
|
||||
SPI.begin();
|
||||
|
||||
// initalize the data ready and chip select pins:
|
||||
pinMode(dataReadyPin, INPUT);
|
||||
pinMode(chipSelectPin, OUTPUT);
|
||||
|
||||
//Configure SCP1000 for low noise configuration:
|
||||
writeRegister(0x02, 0x2D);
|
||||
writeRegister(0x01, 0x03);
|
||||
writeRegister(0x03, 0x02);
|
||||
// give the sensor time to set up:
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//Select High Resolution Mode
|
||||
writeRegister(0x03, 0x0A);
|
||||
|
||||
// don't do anything until the data ready pin is high:
|
||||
if (digitalRead(dataReadyPin) == HIGH) {
|
||||
//Read the temperature data
|
||||
int tempData = readRegister(0x21, 2);
|
||||
|
||||
// convert the temperature to celsius and display it:
|
||||
float realTemp = (float)tempData / 20.0;
|
||||
Serial.print("Temp[C]=");
|
||||
Serial.print(realTemp);
|
||||
|
||||
|
||||
//Read the pressure data highest 3 bits:
|
||||
byte pressure_data_high = readRegister(0x1F, 1);
|
||||
pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
|
||||
|
||||
//Read the pressure data lower 16 bits:
|
||||
unsigned int pressure_data_low = readRegister(0x20, 2);
|
||||
//combine the two parts into one 19-bit number:
|
||||
long pressure = ((pressure_data_high << 16) | pressure_data_low)/4;
|
||||
|
||||
// display the temperature:
|
||||
Serial.println("\tPressure [Pa]=" + String(pressure));
|
||||
}
|
||||
}
|
||||
|
||||
//Read from or write to register from the SCP1000:
|
||||
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
|
||||
byte inByte = 0; // incoming byte from the SPI
|
||||
unsigned int result = 0; // result to return
|
||||
Serial.print(thisRegister, BIN);
|
||||
Serial.print("\t");
|
||||
// SCP1000 expects the register name in the upper 6 bits
|
||||
// of the byte. So shift the bits left by two bits:
|
||||
thisRegister = thisRegister << 2;
|
||||
// now combine the address and the command into one byte
|
||||
byte dataToSend = thisRegister & READ;
|
||||
Serial.println(thisRegister, BIN);
|
||||
// take the chip select low to select the device:
|
||||
digitalWrite(chipSelectPin, LOW);
|
||||
// send the device the register you want to read:
|
||||
SPI.transfer(dataToSend);
|
||||
// send a value of 0 to read the first byte returned:
|
||||
result = SPI.transfer(0x00);
|
||||
// decrement the number of bytes left to read:
|
||||
bytesToRead--;
|
||||
// if you still have another byte to read:
|
||||
if (bytesToRead > 0) {
|
||||
// shift the first byte left, then get the second byte:
|
||||
result = result << 8;
|
||||
inByte = SPI.transfer(0x00);
|
||||
// combine the byte you just got with the previous one:
|
||||
result = result | inByte;
|
||||
// decrement the number of bytes left to read:
|
||||
bytesToRead--;
|
||||
}
|
||||
// take the chip select high to de-select:
|
||||
digitalWrite(chipSelectPin, HIGH);
|
||||
// return the result:
|
||||
return(result);
|
||||
}
|
||||
|
||||
|
||||
//Sends a write command to SCP1000
|
||||
|
||||
void writeRegister(byte thisRegister, byte thisValue) {
|
||||
|
||||
// SCP1000 expects the register address in the upper 6 bits
|
||||
// of the byte. So shift the bits left by two bits:
|
||||
thisRegister = thisRegister << 2;
|
||||
// now combine the register address and the command into one byte:
|
||||
byte dataToSend = thisRegister | WRITE;
|
||||
|
||||
// take the chip select low to select the device:
|
||||
digitalWrite(chipSelectPin, LOW);
|
||||
|
||||
SPI.transfer(dataToSend); //Send register location
|
||||
SPI.transfer(thisValue); //Send value to record into register
|
||||
|
||||
// take the chip select high to de-select:
|
||||
digitalWrite(chipSelectPin, HIGH);
|
||||
}
|
||||
|
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Digital Pot Control
|
||||
|
||||
This example controls an Analog Devices AD5206 digital potentiometer.
|
||||
The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
|
||||
A - connect this to voltage
|
||||
W - this is the pot's wiper, which changes when you set it
|
||||
B - connect this to ground.
|
||||
|
||||
The AD5206 is SPI-compatible,and to command it, you send two bytes,
|
||||
one with the channel number (0 - 5) and one with the resistance value for the
|
||||
channel (0 - 255).
|
||||
|
||||
The circuit:
|
||||
* All A pins of AD5206 connected to +5V
|
||||
* All B pins of AD5206 connected to ground
|
||||
* An LED and a 220-ohm resisor in series connected from each W pin to ground
|
||||
* CS - to digital pin 10 (SS pin)
|
||||
* SDI - to digital pin 11 (MOSI pin)
|
||||
* CLK - to digital pin 13 (SCK pin)
|
||||
|
||||
created 10 Aug 2010
|
||||
by Tom Igoe
|
||||
|
||||
Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// inslude the SPI library:
|
||||
#include <SPI.h>
|
||||
|
||||
|
||||
// set pin 10 as the slave select for the digital pot:
|
||||
const int slaveSelectPin = 10;
|
||||
|
||||
void setup() {
|
||||
// set the slaveSelectPin as an output:
|
||||
pinMode (slaveSelectPin, OUTPUT);
|
||||
// initialize SPI:
|
||||
SPI.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// go through the six channels of the digital pot:
|
||||
for (int channel = 0; channel < 6; channel++) {
|
||||
// change the resistance on this channel from min to max:
|
||||
for (int level = 0; level < 255; level++) {
|
||||
digitalPotWrite(channel, level);
|
||||
delay(10);
|
||||
}
|
||||
// wait a second at the top:
|
||||
delay(100);
|
||||
// change the resistance on this channel from max to min:
|
||||
for (int level = 0; level < 255; level++) {
|
||||
digitalPotWrite(channel, 255 - level);
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int digitalPotWrite(int address, int value) {
|
||||
// take the SS pin low to select the chip:
|
||||
digitalWrite(slaveSelectPin,LOW);
|
||||
// send in the address and value via SPI:
|
||||
SPI.transfer(address);
|
||||
SPI.transfer(value);
|
||||
// take the SS pin high to de-select the chip:
|
||||
digitalWrite(slaveSelectPin,HIGH);
|
||||
}
|
36
hardware/arduino/sam/libraries/SPI/keywords.txt
Normal file
36
hardware/arduino/sam/libraries/SPI/keywords.txt
Normal file
@@ -0,0 +1,36 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map SPI
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
SPI KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
begin KEYWORD2
|
||||
end KEYWORD2
|
||||
transfer KEYWORD2
|
||||
setBitOrder KEYWORD2
|
||||
setDataMode KEYWORD2
|
||||
setClockDivider KEYWORD2
|
||||
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
SPI_CLOCK_DIV4 LITERAL1
|
||||
SPI_CLOCK_DIV16 LITERAL1
|
||||
SPI_CLOCK_DIV64 LITERAL1
|
||||
SPI_CLOCK_DIV128 LITERAL1
|
||||
SPI_CLOCK_DIV2 LITERAL1
|
||||
SPI_CLOCK_DIV8 LITERAL1
|
||||
SPI_CLOCK_DIV32 LITERAL1
|
||||
SPI_CLOCK_DIV64 LITERAL1
|
||||
SPI_MODE0 LITERAL1
|
||||
SPI_MODE1 LITERAL1
|
||||
SPI_MODE2 LITERAL1
|
||||
SPI_MODE3 LITERAL1
|
348
hardware/arduino/sam/libraries/Wire/Wire.cpp
Normal file
348
hardware/arduino/sam/libraries/Wire/Wire.cpp
Normal file
@@ -0,0 +1,348 @@
|
||||
/*
|
||||
* TwoWire.h - TWI/I2C library for Arduino Due
|
||||
* Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>.
|
||||
* 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
|
||||
*/
|
||||
|
||||
extern "C" {
|
||||
#include <string.h>
|
||||
#include "twi.h"
|
||||
}
|
||||
|
||||
#include "Wire.h"
|
||||
|
||||
static inline bool TWI_FailedAcknowledge(Twi *pTwi) {
|
||||
return pTwi->TWI_SR & TWI_SR_NACK;
|
||||
}
|
||||
|
||||
static inline bool TWI_WaitTransferComplete(Twi *_twi, uint32_t _timeout) {
|
||||
while (!TWI_TransferComplete(_twi)) {
|
||||
if (TWI_FailedAcknowledge(_twi))
|
||||
return false;
|
||||
if (--_timeout == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool TWI_WaitByteSent(Twi *_twi, uint32_t _timeout) {
|
||||
while (!TWI_ByteSent(_twi)) {
|
||||
if (TWI_FailedAcknowledge(_twi))
|
||||
return false;
|
||||
if (--_timeout == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool TWI_WaitByteReceived(Twi *_twi, uint32_t _timeout) {
|
||||
while (!TWI_ByteReceived(_twi)) {
|
||||
if (TWI_FailedAcknowledge(_twi))
|
||||
return false;
|
||||
if (--_timeout == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool TWI_STATUS_SVREAD(uint32_t status) {
|
||||
return (status & TWI_SR_SVREAD) == TWI_SR_SVREAD;
|
||||
}
|
||||
|
||||
static inline bool TWI_STATUS_SVACC(uint32_t status) {
|
||||
return (status & TWI_SR_SVACC) == TWI_SR_SVACC;
|
||||
}
|
||||
|
||||
static inline bool TWI_STATUS_GACC(uint32_t status) {
|
||||
return (status & TWI_SR_GACC) == TWI_SR_GACC;
|
||||
}
|
||||
|
||||
static inline bool TWI_STATUS_EOSACC(uint32_t status) {
|
||||
return (status & TWI_SR_EOSACC) == TWI_SR_EOSACC;
|
||||
}
|
||||
|
||||
static inline bool TWI_STATUS_NACK(uint32_t status) {
|
||||
return (status & TWI_SR_NACK) == TWI_SR_NACK;
|
||||
}
|
||||
|
||||
TwoWire::TwoWire(Twi *_twi, void(*_beginCb)(void)) :
|
||||
twi(_twi), rxBufferIndex(0), rxBufferLength(0), txAddress(0),
|
||||
txBufferLength(0), srvBufferIndex(0), srvBufferLength(0), status(
|
||||
UNINITIALIZED), onBeginCallback(_beginCb) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
void TwoWire::begin(void) {
|
||||
if (onBeginCallback)
|
||||
onBeginCallback();
|
||||
|
||||
// Disable PDC channel
|
||||
twi->TWI_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
|
||||
|
||||
TWI_ConfigureMaster(twi, TWI_CLOCK, VARIANT_MCK);
|
||||
status = MASTER_IDLE;
|
||||
}
|
||||
|
||||
void TwoWire::begin(uint8_t address) {
|
||||
if (onBeginCallback)
|
||||
onBeginCallback();
|
||||
|
||||
// Disable PDC channel
|
||||
twi->TWI_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
|
||||
|
||||
TWI_ConfigureSlave(twi, address);
|
||||
status = SLAVE_IDLE;
|
||||
TWI_EnableIt(twi, TWI_IER_SVACC);
|
||||
//| TWI_IER_RXRDY | TWI_IER_TXRDY | TWI_IER_TXCOMP);
|
||||
}
|
||||
|
||||
void TwoWire::begin(int address) {
|
||||
begin((uint8_t) address);
|
||||
}
|
||||
|
||||
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
|
||||
if (quantity > BUFFER_LENGTH)
|
||||
quantity = BUFFER_LENGTH;
|
||||
|
||||
// perform blocking read into buffer
|
||||
int readed = 0;
|
||||
TWI_StartRead(twi, address, 0, 0);
|
||||
do {
|
||||
// Stop condition must be set during the reception of last byte
|
||||
if (readed + 1 == quantity)
|
||||
TWI_SendSTOPCondition( twi);
|
||||
|
||||
TWI_WaitByteReceived(twi, RECV_TIMEOUT);
|
||||
rxBuffer[readed++] = TWI_ReadByte(twi);
|
||||
} while (readed < quantity);
|
||||
TWI_WaitTransferComplete(twi, RECV_TIMEOUT);
|
||||
|
||||
// set rx buffer iterator vars
|
||||
rxBufferIndex = 0;
|
||||
rxBufferLength = readed;
|
||||
|
||||
return readed;
|
||||
}
|
||||
|
||||
uint8_t TwoWire::requestFrom(int address, int quantity) {
|
||||
return requestFrom((uint8_t) address, (uint8_t) quantity);
|
||||
}
|
||||
|
||||
void TwoWire::beginTransmission(uint8_t address) {
|
||||
status = MASTER_SEND;
|
||||
|
||||
// save address of target and empty buffer
|
||||
txAddress = address;
|
||||
txBufferLength = 0;
|
||||
}
|
||||
|
||||
void TwoWire::beginTransmission(int address) {
|
||||
beginTransmission((uint8_t) address);
|
||||
}
|
||||
|
||||
uint8_t TwoWire::endTransmission(void) {
|
||||
// transmit buffer (blocking)
|
||||
TWI_StartWrite(twi, txAddress, 0, 0, txBuffer[0]);
|
||||
TWI_WaitByteSent(twi, XMIT_TIMEOUT);
|
||||
int sent = 1;
|
||||
while (sent < txBufferLength) {
|
||||
TWI_WriteByte(twi, txBuffer[sent++]);
|
||||
TWI_WaitByteSent(twi, XMIT_TIMEOUT);
|
||||
}
|
||||
TWI_Stop( twi);
|
||||
TWI_WaitTransferComplete(twi, XMIT_TIMEOUT);
|
||||
|
||||
// empty buffer
|
||||
txBufferLength = 0;
|
||||
|
||||
status = MASTER_IDLE;
|
||||
return sent;
|
||||
}
|
||||
|
||||
void TwoWire::write(uint8_t data) {
|
||||
if (status == MASTER_SEND) {
|
||||
if (txBufferLength >= BUFFER_LENGTH)
|
||||
return;
|
||||
txBuffer[txBufferLength++] = data;
|
||||
} else {
|
||||
if (srvBufferLength >= BUFFER_LENGTH)
|
||||
return;
|
||||
srvBuffer[srvBufferLength++] = data;
|
||||
}
|
||||
}
|
||||
|
||||
void TwoWire::write(const uint8_t *data, size_t quantity) {
|
||||
if (status == MASTER_SEND) {
|
||||
for (size_t i = 0; i < quantity; ++i) {
|
||||
if (txBufferLength >= BUFFER_LENGTH)
|
||||
return;
|
||||
txBuffer[txBufferLength++] = data[i];
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < quantity; ++i) {
|
||||
if (srvBufferLength >= BUFFER_LENGTH)
|
||||
return;
|
||||
srvBuffer[srvBufferLength++] = data[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TwoWire::write(const char *data) {
|
||||
write((uint8_t*) data, strlen(data));
|
||||
}
|
||||
|
||||
int TwoWire::available(void) {
|
||||
return rxBufferLength - rxBufferIndex;
|
||||
}
|
||||
|
||||
int TwoWire::read(void) {
|
||||
if (rxBufferIndex < rxBufferLength)
|
||||
return rxBuffer[rxBufferIndex++];
|
||||
return -1;
|
||||
}
|
||||
|
||||
int TwoWire::peek(void) {
|
||||
if (rxBufferIndex < rxBufferLength)
|
||||
return rxBuffer[rxBufferIndex];
|
||||
return -1;
|
||||
}
|
||||
|
||||
void TwoWire::flush(void) {
|
||||
// Do nothing, use endTransmission(..) to force
|
||||
// data transfer.
|
||||
}
|
||||
|
||||
void TwoWire::onReceive(void(*function)(int)) {
|
||||
onReceiveCallback = function;
|
||||
}
|
||||
|
||||
void TwoWire::onRequest(void(*function)(void)) {
|
||||
onRequestCallback = function;
|
||||
}
|
||||
|
||||
void TwoWire::onService(void) {
|
||||
// Retrieve interrupt status
|
||||
uint32_t sr = TWI_GetStatus(twi);
|
||||
|
||||
if (status == SLAVE_IDLE && TWI_STATUS_SVACC(sr)) {
|
||||
TWI_DisableIt(twi, TWI_IDR_SVACC);
|
||||
TWI_EnableIt(twi, TWI_IER_RXRDY | TWI_IER_GACC | TWI_IER_NACK
|
||||
| TWI_IER_EOSACC | TWI_IER_SCL_WS | TWI_IER_TXCOMP);
|
||||
|
||||
srvBufferLength = 0;
|
||||
srvBufferIndex = 0;
|
||||
|
||||
// Detect if we should go into RECV or SEND status
|
||||
// SVREAD==1 means *master* reading -> SLAVE_SEND
|
||||
if (!TWI_STATUS_SVREAD(sr)) {
|
||||
status = SLAVE_RECV;
|
||||
} else {
|
||||
status = SLAVE_SEND;
|
||||
|
||||
// Alert calling program to generate a response ASAP
|
||||
if (onRequestCallback)
|
||||
onRequestCallback();
|
||||
else
|
||||
// create a default 1-byte response
|
||||
write((uint8_t) 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (status != SLAVE_IDLE) {
|
||||
if (TWI_STATUS_TXCOMP(sr) && TWI_STATUS_EOSACC(sr)) {
|
||||
if (status == SLAVE_RECV && onReceiveCallback) {
|
||||
// Copy data into rxBuffer
|
||||
// (allows to receive another packet while the
|
||||
// user program reads actual data)
|
||||
for (uint8_t i = 0; i < srvBufferLength; ++i)
|
||||
rxBuffer[i] = srvBuffer[i];
|
||||
rxBufferIndex = 0;
|
||||
rxBufferLength = srvBufferLength;
|
||||
|
||||
// Alert calling program
|
||||
onReceiveCallback( rxBufferLength);
|
||||
}
|
||||
|
||||
// Transfer completed
|
||||
TWI_EnableIt(twi, TWI_SR_SVACC);
|
||||
TWI_DisableIt(twi, TWI_IDR_RXRDY | TWI_IDR_GACC | TWI_IDR_NACK
|
||||
| TWI_IDR_EOSACC | TWI_IDR_SCL_WS | TWI_IER_TXCOMP);
|
||||
status = SLAVE_IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
if (status == SLAVE_RECV) {
|
||||
if (TWI_STATUS_RXRDY(sr)) {
|
||||
if (srvBufferLength < BUFFER_LENGTH)
|
||||
srvBuffer[srvBufferLength++] = TWI_ReadByte(twi);
|
||||
}
|
||||
}
|
||||
|
||||
if (status == SLAVE_SEND) {
|
||||
if (TWI_STATUS_TXRDY(sr) && !TWI_STATUS_NACK(sr)) {
|
||||
uint8_t c = 'x';
|
||||
if (srvBufferIndex < srvBufferLength)
|
||||
c = srvBuffer[srvBufferIndex++];
|
||||
TWI_WriteByte(twi, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if WIRE_INTERFACES_COUNT > 0
|
||||
static void Wire_Init(void) {
|
||||
PMC_EnablePeripheral( WIRE_INTERFACE_ID);
|
||||
PIO_Configure(g_APinDescription[PIN_WIRE_SDA].pPort,
|
||||
g_APinDescription[PIN_WIRE_SDA].ulPinType,
|
||||
g_APinDescription[PIN_WIRE_SDA].ulPin,
|
||||
g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration);
|
||||
PIO_Configure(g_APinDescription[PIN_WIRE_SCL].pPort,
|
||||
g_APinDescription[PIN_WIRE_SCL].ulPinType,
|
||||
g_APinDescription[PIN_WIRE_SCL].ulPin,
|
||||
g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration);
|
||||
|
||||
NVIC_DisableIRQ(TWI1_IRQn);
|
||||
NVIC_ClearPendingIRQ(TWI1_IRQn);
|
||||
NVIC_SetPriority(TWI1_IRQn, 0);
|
||||
NVIC_EnableIRQ(TWI1_IRQn);
|
||||
}
|
||||
|
||||
TwoWire Wire = TwoWire(WIRE_INTERFACE, Wire_Init);
|
||||
|
||||
void WIRE_ISR_HANDLER(void) {
|
||||
Wire.onService();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WIRE_INTERFACES_COUNT > 1
|
||||
static void Wire1_Init(void) {
|
||||
PMC_EnablePeripheral( WIRE1_INTERFACE_ID);
|
||||
PIO_Configure(g_APinDescription[PIN_WIRE1_SDA].pPort,
|
||||
g_APinDescription[PIN_WIRE1_SDA].ulPinType,
|
||||
g_APinDescription[PIN_WIRE1_SDA].ulPin,
|
||||
g_APinDescription[PIN_WIRE1_SDA].ulPinConfiguration);
|
||||
PIO_Configure(g_APinDescription[PIN_WIRE1_SCL].pPort,
|
||||
g_APinDescription[PIN_WIRE1_SCL].ulPinType,
|
||||
g_APinDescription[PIN_WIRE1_SCL].ulPin,
|
||||
g_APinDescription[PIN_WIRE1_SCL].ulPinConfiguration);
|
||||
}
|
||||
|
||||
TwoWire Wire1 = TwoWire(WIRE1_INTERFACE, Wire1_Init);
|
||||
|
||||
void WIRE1_ISR_HANDLER(void) {
|
||||
Wire1.onService();
|
||||
}
|
||||
#endif
|
107
hardware/arduino/sam/libraries/Wire/Wire.h
Normal file
107
hardware/arduino/sam/libraries/Wire/Wire.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* TwoWire.h - TWI/I2C library for Arduino Due
|
||||
* Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>.
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef TwoWire_h
|
||||
#define TwoWire_h
|
||||
|
||||
#include "Stream.h"
|
||||
#include "twi.h"
|
||||
#include "variant.h"
|
||||
|
||||
#define BUFFER_LENGTH 32
|
||||
|
||||
class TwoWire: public Stream {
|
||||
public:
|
||||
TwoWire(Twi *twi, void(*begin_cb)(void));
|
||||
void begin();
|
||||
void begin(uint8_t);
|
||||
void begin(int);
|
||||
void beginTransmission(uint8_t);
|
||||
void beginTransmission(int);
|
||||
uint8_t endTransmission(void);
|
||||
uint8_t requestFrom(uint8_t, uint8_t);
|
||||
uint8_t requestFrom(int, int);
|
||||
virtual void write(uint8_t);
|
||||
virtual void write(const char *);
|
||||
virtual void write(const uint8_t *, size_t);
|
||||
virtual int available(void);
|
||||
virtual int read(void);
|
||||
virtual int peek(void);
|
||||
virtual void flush(void);
|
||||
void onReceive(void(*)(int));
|
||||
void onRequest(void(*)(void));
|
||||
|
||||
void onService(void);
|
||||
|
||||
private:
|
||||
// RX Buffer
|
||||
uint8_t rxBuffer[BUFFER_LENGTH];
|
||||
uint8_t rxBufferIndex;
|
||||
uint8_t rxBufferLength;
|
||||
|
||||
// TX Buffer
|
||||
uint8_t txAddress;
|
||||
uint8_t txBuffer[BUFFER_LENGTH];
|
||||
uint8_t txBufferLength;
|
||||
|
||||
// Service buffer
|
||||
uint8_t srvBuffer[BUFFER_LENGTH];
|
||||
uint8_t srvBufferIndex;
|
||||
uint8_t srvBufferLength;
|
||||
|
||||
// Callback user functions
|
||||
void (*onRequestCallback)(void);
|
||||
void (*onReceiveCallback)(int);
|
||||
|
||||
// Called before initialization
|
||||
void (*onBeginCallback)(void);
|
||||
|
||||
// TWI instance
|
||||
Twi *twi;
|
||||
|
||||
// TWI state
|
||||
enum TwoWireStatus {
|
||||
UNINITIALIZED,
|
||||
MASTER_IDLE,
|
||||
MASTER_SEND,
|
||||
MASTER_RECV,
|
||||
SLAVE_IDLE,
|
||||
SLAVE_RECV,
|
||||
SLAVE_SEND
|
||||
};
|
||||
TwoWireStatus status;
|
||||
|
||||
// TWI clock frequency
|
||||
static const uint32_t TWI_CLOCK = 100000;
|
||||
|
||||
// Timeouts (
|
||||
static const uint32_t RECV_TIMEOUT = 100000;
|
||||
static const uint32_t XMIT_TIMEOUT = 100000;
|
||||
};
|
||||
|
||||
#if WIRE_INTERFACES_COUNT > 0
|
||||
extern TwoWire Wire;
|
||||
#endif
|
||||
#if WIRE_INTERFACES_COUNT > 1
|
||||
extern TwoWire Wire1;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@@ -0,0 +1,87 @@
|
||||
// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
// and James Tichenor <http://www.jamestichenor.net>
|
||||
|
||||
// Demonstrates use of the Wire library reading data from the
|
||||
// Devantech Utrasonic Rangers SFR08 and SFR10
|
||||
|
||||
// Created 29 April 2006
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
Serial.begin(9600); // start serial communication at 9600bps
|
||||
}
|
||||
|
||||
int reading = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
// step 1: instruct sensor to read echoes
|
||||
Wire.beginTransmission(112); // transmit to device #112 (0x70)
|
||||
// the address specified in the datasheet is 224 (0xE0)
|
||||
// but i2c adressing uses the high 7 bits so it's 112
|
||||
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
|
||||
Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)
|
||||
// use 0x51 for centimeters
|
||||
// use 0x52 for ping microseconds
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
// step 2: wait for readings to happen
|
||||
delay(70); // datasheet suggests at least 65 milliseconds
|
||||
|
||||
// step 3: instruct sensor to return a particular echo reading
|
||||
Wire.beginTransmission(112); // transmit to device #112
|
||||
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
// step 4: request reading from sensor
|
||||
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
|
||||
|
||||
// step 5: receive reading from sensor
|
||||
if(2 <= Wire.available()) // if two bytes were received
|
||||
{
|
||||
reading = Wire.read(); // receive high byte (overwrites previous reading)
|
||||
reading = reading << 8; // shift high byte to be high 8 bits
|
||||
reading |= Wire.read(); // receive low byte as lower 8 bits
|
||||
Serial.println(reading); // print the reading
|
||||
}
|
||||
|
||||
delay(250); // wait a bit since people have to read the output :)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
|
||||
// usage: changeAddress(0x70, 0xE6);
|
||||
|
||||
void changeAddress(byte oldAddress, byte newAddress)
|
||||
{
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(byte(0xA0));
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(byte(0xAA));
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(byte(0xA5));
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(newAddress);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
*/
|
@@ -0,0 +1,39 @@
|
||||
// I2C Digital Potentiometer
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/>
|
||||
|
||||
// Demonstrates use of the Wire library
|
||||
// Controls AD5171 digital potentiometer via I2C/TWI
|
||||
|
||||
// Created 31 March 2006
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
}
|
||||
|
||||
byte val = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
|
||||
// device address is specified in datasheet
|
||||
Wire.write(byte(0x00)); // sends instruction byte
|
||||
Wire.write(val); // sends potentiometer value byte
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
val++; // increment value
|
||||
if(val == 64) // if reached 64th position (max)
|
||||
{
|
||||
val = 0; // start over from lowest value
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
|
@@ -0,0 +1,32 @@
|
||||
// Wire Master Reader
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
|
||||
// Demonstrates use of the Wire library
|
||||
// Reads data from an I2C/TWI slave device
|
||||
// Refer to the "Wire Slave Sender" example for use with this
|
||||
|
||||
// Created 29 March 2006
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
Serial.begin(9600); // start serial for output
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2
|
||||
|
||||
while(Wire.available()) // slave may send less than requested
|
||||
{
|
||||
char c = Wire.read(); // receive a byte as character
|
||||
Serial.print(c); // print the character
|
||||
}
|
||||
|
||||
delay(500);
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
// Wire Master Writer
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
|
||||
// Demonstrates use of the Wire library
|
||||
// Writes data to an I2C/TWI slave device
|
||||
// Refer to the "Wire Slave Receiver" example for use with this
|
||||
|
||||
// Created 29 March 2006
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
}
|
||||
|
||||
byte x = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
Wire.beginTransmission(4); // transmit to device #4
|
||||
Wire.write("x is "); // sends five bytes
|
||||
Wire.write(x); // sends one byte
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
x++;
|
||||
delay(500);
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
// Wire Slave Receiver
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
|
||||
// Demonstrates use of the Wire library
|
||||
// Receives data as an I2C/TWI slave device
|
||||
// Refer to the "Wire Master Writer" example for use with this
|
||||
|
||||
// Created 29 March 2006
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Wire.begin(4); // join i2c bus with address #4
|
||||
Wire.onReceive(receiveEvent); // register event
|
||||
Serial.begin(9600); // start serial for output
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// function that executes whenever data is received from master
|
||||
// this function is registered as an event, see setup()
|
||||
void receiveEvent(int howMany)
|
||||
{
|
||||
while(1 < Wire.available()) // loop through all but the last
|
||||
{
|
||||
char c = Wire.read(); // receive byte as a character
|
||||
Serial.print(c); // print the character
|
||||
}
|
||||
int x = Wire.read(); // receive byte as an integer
|
||||
Serial.println(x); // print the integer
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
// Wire Slave Sender
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
|
||||
// Demonstrates use of the Wire library
|
||||
// Sends data as an I2C/TWI slave device
|
||||
// Refer to the "Wire Master Reader" example for use with this
|
||||
|
||||
// Created 29 March 2006
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Wire.begin(2); // join i2c bus with address #2
|
||||
Wire.onRequest(requestEvent); // register event
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// function that executes whenever data is requested by master
|
||||
// this function is registered as an event, see setup()
|
||||
void requestEvent()
|
||||
{
|
||||
Wire.write("hello "); // respond with message of 6 bytes
|
||||
// as expected by master
|
||||
}
|
31
hardware/arduino/sam/libraries/Wire/keywords.txt
Normal file
31
hardware/arduino/sam/libraries/Wire/keywords.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For Wire
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
beginTransmission KEYWORD2
|
||||
endTransmission KEYWORD2
|
||||
requestFrom KEYWORD2
|
||||
send KEYWORD2
|
||||
receive KEYWORD2
|
||||
onReceive KEYWORD2
|
||||
onRequest KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
Wire KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
Reference in New Issue
Block a user