Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Arduino interface library added!

  • Loading branch information...
commit 278203cb591272ee84fcc089bf44dc6f2e5a1956 1 parent 2e8731a
ihsan_kehribar authored
View
107 arduino/LittleWire/LittleWire.cpp
@@ -0,0 +1,107 @@
+/*
+ - Little Wire -
+ Printf style debug over AVR-ISP pins using Little Wire.
+ http://kehribar.me/projects/Little-Wire/
+
+ Copyright (C) <2012> ihsan Kehribar <ihsan@kehribar.me>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
+ this software and associated documentation files (the "Software"), to deal in
+ the Software without restriction, including without limitation the rights to
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ of the Software, and to permit persons to whom the Software is furnished to do
+ so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+#include "LittleWire.h"
+#include <avr/interrupt.h>
+
+/*volatile uint8_t* LittleWire::txBuffer;
+volatile uint8_t LittleWire::index;
+volatile uint8_t LittleWire::length;
+volatile uint8_t LittleWire::messageSent;*/
+
+/* SPI transfer complete interrupt */
+ISR(SPI_STC_vect)
+{
+ if(lw_messageSent==0)
+ {
+ SPDR = lw_txBuffer[lw_index++]; /* Send a char to the computer */
+ if(lw_index==lw_length)
+ {
+ lw_index=0;
+ lw_messageSent=1;
+ }
+ }
+}
+
+/* Empty constructor */
+LittleWire::LittleWire()
+{
+ // ...
+}
+
+/* Init pins and SPI interface */
+void LittleWire::begin(void)
+{
+ lw_index = 0;
+ lw_length = 0;
+ lw_messageSent = 1;
+ setup_spi(SPI_MODE_1, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
+}
+
+/* Send the new message buffer */
+void LittleWire::send(uint8_t* buffer,uint8_t new_length)
+{
+ lw_length=new_length;
+ lw_txBuffer=buffer;
+ lw_index=0;
+ lw_messageSent = 0;
+}
+
+/* Return the state */
+uint8_t LittleWire::isBusy(void)
+{
+ if(lw_messageSent)
+ return 0;
+ else
+ return 1;
+}
+
+/* Ref: http://www.rocketnumbernine.com/2009/07/03/using-spi-on-an-avr-3 */
+void LittleWire::setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock)
+{
+ // specify pin directions for SPI pins on port B
+ if (clock == SPI_SLAVE) { // if slave SS and SCK is input
+ DDRB &= ~(1<<SPI_MOSI_PIN); // input
+ DDRB |= (1<<SPI_MISO_PIN); // output
+ DDRB &= ~(1<<SPI_SS_PIN); // input
+ DDRB &= ~(1<<SPI_SCK_PIN);// input
+ }
+ else {
+ DDRB |= (1<<SPI_MOSI_PIN); // output
+ DDRB &= ~(1<<SPI_MISO_PIN); // input
+ DDRB |= (1<<SPI_SCK_PIN);// output
+ DDRB |= (1<<SPI_SS_PIN);// output
+ }
+ SPCR = ((interrupt ? 1 : 0)<<SPIE) // interrupt enabled
+ | (1<<SPE) // enable SPI
+ | (dord<<DORD) // LSB or MSB
+ | (((clock != SPI_SLAVE) ? 1 : 0) <<MSTR) // Slave or Master
+ | (((mode & 0x02) == 2) << CPOL) // clock timing mode CPOL
+ | (((mode & 0x01)) << CPHA) // clock timing mode CPHA
+ | (((clock & 0x02) == 2) << SPR1) // cpu clock divisor SPR1
+ | ((clock & 0x01) << SPR0); // cpu clock divisor SPR0
+ SPSR = (((clock & 0x04) == 4) << SPI2X); // clock divisor SPI2X
+}
View
95 arduino/LittleWire/LittleWire.h
@@ -0,0 +1,95 @@
+/*
+ - Little Wire -
+ Printf style debug over AVR-ISP pins using Little Wire.
+ http://kehribar.me/projects/Little-Wire/
+
+ Copyright (C) <2012> ihsan Kehribar <ihsan@kehribar.me>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
+ this software and associated documentation files (the "Software"), to deal in
+ the Software without restriction, including without limitation the rights to
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ of the Software, and to permit persons to whom the Software is furnished to do
+ so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+#ifndef LittleWire_h
+#define LittleWire_h
+
+extern "C" {
+ #include <stdlib.h>
+ #include <string.h>
+ #include <inttypes.h>
+}
+
+#if (defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__))
+ #define SPI_SS_PIN PORTB0
+ #define SPI_SCK_PIN PORTB1
+ #define SPI_MOSI_PIN PORTB2
+ #define SPI_MISO_PIN PORTB3
+#elif (defined(__AVR_ATmega48__) || defined(_AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__))
+ #define SPI_SS_PIN PORTB2
+ #define SPI_SCK_PIN PORTB5
+ #define SPI_MOSI_PIN PORTB3
+ #define SPI_MISO_PIN PORTB4
+#else
+ #error unknown processor - add to LittleWire.h
+#endif
+
+// SPI clock modes
+#define SPI_MODE_0 0x00 /* Sample (Rising) Setup (Falling) CPOL=0, CPHA=0 */
+#define SPI_MODE_1 0x01 /* Setup (Rising) Sample (Falling) CPOL=0, CPHA=1 */
+#define SPI_MODE_2 0x02 /* Sample (Falling) Setup (Rising) CPOL=1, CPHA=0 */
+#define SPI_MODE_3 0x03 /* Setup (Falling) Sample (Rising) CPOL=1, CPHA=1 */
+
+// data direction
+#define SPI_LSB 1 /* send least significant bit (bit 0) first */
+#define SPI_MSB 0 /* send most significant bit (bit 7) first */
+
+// whether to raise interrupt when data received (SPIF bit received)
+#define SPI_NO_INTERRUPT 0
+#define SPI_INTERRUPT 1
+
+// slave or master with clock diviser
+#define SPI_SLAVE 0xF0
+#define SPI_MSTR_CLK4 0x00 /* chip clock/4 */
+#define SPI_MSTR_CLK16 0x01 /* chip clock/16 */
+#define SPI_MSTR_CLK64 0x02 /* chip clock/64 */
+#define SPI_MSTR_CLK128 0x03 /* chip clock/128 */
+#define SPI_MSTR_CLK2 0x04 /* chip clock/2 */
+#define SPI_MSTR_CLK8 0x05 /* chip clock/8 */
+#define SPI_MSTR_CLK32 0x06 /* chip clock/32 */
+
+static volatile uint8_t* lw_txBuffer;
+static volatile uint8_t lw_index;
+static volatile uint8_t lw_length;
+static volatile uint8_t lw_messageSent;
+
+class LittleWire
+{
+ private:
+ void setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock);
+ /*static volatile uint8_t* txBuffer;
+ static volatile uint8_t index;
+ static volatile uint8_t length;
+ static volatile uint8_t messageSent;*/
+ public:
+ LittleWire();
+ void begin(void);
+ void send(uint8_t* buffer,uint8_t new_length);
+ uint8_t isBusy(void);
+};
+
+#endif
+
View
29 arduino/LittleWire/examples/LittleWire_helloWorld/LittleWire_helloWorld.ino
@@ -0,0 +1,29 @@
+/*
+ Printf style debug over AVR-ISP pins using Little Wire.
+ Use the same cable for debugging and ISP!
+
+ http://kehribar.me/projects/Little-Wire/
+ ihsan Kehribar <ihsan@kehribar.me>
+*/
+
+#include <LittleWire.h>
+LittleWire lw;
+
+uint8_t txBuffer[32];
+uint8_t i=0;
+uint8_t length=0;
+
+void setup()
+{
+ lw.begin();
+ length=sprintf((char*)txBuffer,"Hello World!\n");
+ lw.send(txBuffer,length);
+}
+
+void loop()
+{
+ while(lw.isBusy());
+ length=sprintf((char*)txBuffer,"Count:%d\n",i++);
+ lw.send(txBuffer,length);
+}
+
View
24 arduino/LittleWire/keywords.txt
@@ -0,0 +1,24 @@
+#######################################
+# Syntax Coloring Map For Little Wire
+#######################################
+
+#######################################
+# Datatypes (KEYWORD1)
+#######################################
+
+#######################################
+# Methods and Functions (KEYWORD2)
+#######################################
+begin KEYWORD2
+wait KEYWORD2
+send KEYWORD2
+
+#######################################
+# Instances (KEYWORD2)
+#######################################
+LittleWire KEYWORD2
+
+#######################################
+# Constants (LITERAL1)
+#######################################
+
View
1  arduino/Readme.txt
@@ -0,0 +1 @@
+Put the LittleWire folder under the ...\arduino-1.0\libraries\ path
Please sign in to comment.
Something went wrong with that request. Please try again.