mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-07 00:04:36 +03:00
Initial Arduino IDE based on Processing.
This commit is contained in:
230
core/BConstants.h
Executable file
230
core/BConstants.h
Executable file
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
BConstats.h - main definitions file for Wiring
|
||||
Part of the Wiring project - http://wiring.processing.org
|
||||
|
||||
Copyright (c) 2003-04
|
||||
Hernando Barragan, Interaction Design Institute Ivrea
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef BConstants_h
|
||||
#define BConstants_h
|
||||
|
||||
#define PI (3.1415927)
|
||||
#define HALF_PI (1.57079)
|
||||
#define TWO_PI (6.2831854)
|
||||
#define DEG_TO_RAD (0.01745329)
|
||||
#define RAD_TO_DEG (57.2957786)
|
||||
#define EPSILON (0.0001)
|
||||
|
||||
#define WPIN0 (1<<0)
|
||||
#define WPIN1 (1<<1)
|
||||
#define WPIN2 (1<<2)
|
||||
#define WPIN3 (1<<3)
|
||||
#define WPIN4 (1<<4)
|
||||
#define WPIN5 (1<<5)
|
||||
#define WPIN6 (1<<6)
|
||||
#define WPIN7 (1<<7)
|
||||
|
||||
#define WPWMPIN5 (1<<5) // PINB5
|
||||
#define WPWMPIN4 (1<<6) // PINB6
|
||||
#define WPWMPIN3 (1<<7) // PINB7
|
||||
#define WPWMPIN2 (1<<3) // PINE3
|
||||
#define WPWMPIN1 (1<<4) // PINE4
|
||||
#define WPWMPIN0 (1<<5) // PINE5
|
||||
|
||||
#define WPORTA PORTA
|
||||
#define WPORTB PORTB
|
||||
#define WPORTC PORTC
|
||||
#define WPORTD PORTD
|
||||
#define WPORTE PORTE
|
||||
#define WPORTF PORTF
|
||||
#define WPORTG PORTG
|
||||
|
||||
#define WPINA PINA
|
||||
#define WPINB PINB
|
||||
#define WPINC PINC
|
||||
#define WPIND PIND
|
||||
#define WPINE PINE
|
||||
#define WPINF PINF
|
||||
#define WPING PING
|
||||
|
||||
#define WDDRA DDRA
|
||||
#define WDDRB DDRB
|
||||
#define WDDRC DDRC
|
||||
#define WDDRD DDRD
|
||||
#define WDDRE DDRE
|
||||
#define WDDRF DDRF
|
||||
#define WDDRG DDRG
|
||||
|
||||
|
||||
#define HIGH 0x1
|
||||
#define LOW 0x0
|
||||
|
||||
#define INPUT 0x0
|
||||
#define OUTPUT 0x1
|
||||
|
||||
#define true 0x1
|
||||
#define false 0x0
|
||||
|
||||
#define CPU_FREQ 16000000L
|
||||
|
||||
|
||||
|
||||
#define TIMER0OVERFLOW_INT 0
|
||||
#define TIMER0OUTCOMPARE_INT 1
|
||||
#define TIMER1OVERFLOW_INT 2
|
||||
#define TIMER1OUTCOMPAREA_INT 3
|
||||
#define TIMER1OUTCOMPAREB_INT 4
|
||||
#define TIMER1OUTCOMPAREC_INT 5
|
||||
#define TIMER1INPUTCAPTURE_INT 6
|
||||
#define TIMER2OVERFLOW_INT 7
|
||||
#define TIMER2OUTCOMPARE_INT 8
|
||||
#define TIMER3OVERFLOW_INT 9
|
||||
#define TIMER3OUTCOMPAREA_INT 10
|
||||
#define TIMER3OUTCOMPAREB_INT 11
|
||||
#define TIMER3OUTCOMPAREC_INT 12
|
||||
#define TIMER3INPUTCAPTURE_INT 13
|
||||
|
||||
#define TIMER_NUM_INTERRUPTS 14
|
||||
|
||||
#define TIMER_CLK_STOP 0x00
|
||||
#define TIMER_CLK_DIV1 0x01
|
||||
#define TIMER_CLK_DIV8 0x02
|
||||
#define TIMER_CLK_DIV64 0x03
|
||||
#define TIMER_CLK_DIV256 0x04
|
||||
#define TIMER_CLK_DIV1024 0x05
|
||||
#define TIMER_CLK_T_FALL 0x06
|
||||
#define TIMER_CLK_T_RISE 0x07
|
||||
#define TIMER_PRESCALE_MASK 0x07
|
||||
|
||||
#define TIMERRTC_CLK_STOP 0x00
|
||||
#define TIMERRTC_CLK_DIV1 0x01
|
||||
#define TIMERRTC_CLK_DIV8 0x02
|
||||
#define TIMERRTC_CLK_DIV32 0x03
|
||||
#define TIMERRTC_CLK_DIV64 0x04
|
||||
#define TIMERRTC_CLK_DIV128 0x05
|
||||
#define TIMERRTC_CLK_DIV256 0x06
|
||||
#define TIMERRTC_CLK_DIV1024 0x07
|
||||
#define TIMERRTC_PRESCALE_MASK 0x07
|
||||
|
||||
#define TIMER0PRESCALE TIMERRTC_CLK_DIV64
|
||||
#define TIMER1PRESCALE TIMER_CLK_DIV64
|
||||
#define TIMER2PRESCALE TIMER_CLK_DIV8
|
||||
#define TIMER3PRESCALE TIMER_CLK_DIV64
|
||||
|
||||
|
||||
volatile struct {
|
||||
uint8_t rx_int : 1;
|
||||
uint8_t uart0_tx_complete : 1;
|
||||
uint8_t uart1_tx_complete : 1;
|
||||
} event;
|
||||
|
||||
|
||||
typedef struct ringbuf_t {
|
||||
uint16_t buflen;
|
||||
uint16_t bufcnt;
|
||||
uint8_t * in;
|
||||
uint8_t * out;
|
||||
uint8_t * buf;
|
||||
} RINGBUF;
|
||||
|
||||
typedef uint8_t boolean;
|
||||
typedef uint8_t byte;
|
||||
|
||||
int serial;
|
||||
#define DEF_UART 0
|
||||
#define AUX_UART 1
|
||||
|
||||
#define UART_BUF_LEN 32
|
||||
|
||||
|
||||
|
||||
#define min(a,b) ((a<b)?(a):(b))
|
||||
#define max(a,b) ((a>b)?(a):(b))
|
||||
#define abs(x) ((x>0)?(x):(-x))
|
||||
#define constrain(amt,low,high) ((amt<low)?(low):((amt>high)?(high):(amt)))
|
||||
#define radians(deg) ((deg)*DEG_TO_RAD)
|
||||
#define degrees(rad) ((rad)*RAD_TO_DEG)
|
||||
#define sq(x) ((x)*(x))
|
||||
|
||||
#define SERVO_NUM_CHANNELS 8
|
||||
#define SERVO_MAX 142
|
||||
#define SERVO_MIN 34
|
||||
#define POSITION_MAX 255
|
||||
#define SERVO_DEFAULT_PORT PORTA
|
||||
|
||||
|
||||
static char uart_buffer[UART_BUF_LEN];
|
||||
RINGBUF uartbuf;
|
||||
typedef void (*voidFuncPtru08)(void);
|
||||
volatile static voidFuncPtru08 UartRxFunc;
|
||||
typedef void (*voidFuncPtr)(void);
|
||||
|
||||
|
||||
#define WD_CTRL_PORT PORTC
|
||||
#define WD_CTRL_DDR DDRC
|
||||
#define WD_CTRL_RS 0
|
||||
#define WD_CTRL_RW 1
|
||||
#define WD_CTRL_E 2
|
||||
#define WD_DATA_POUT PORTA
|
||||
#define WD_DATA_PIN PINA
|
||||
#define WD_DATA_DDR DDRA
|
||||
#define WD_LINE0_DDRAMADDR 0x00
|
||||
#define WD_LINE1_DDRAMADDR 0x40
|
||||
#define WD_LINE2_DDRAMADDR 0x14
|
||||
#define WD_LINE3_DDRAMADDR 0x54
|
||||
|
||||
#define WD_CLR 0
|
||||
#define WD_HOME 1
|
||||
#define WD_ENTRY_MODE 2
|
||||
#define WD_ENTRY_INC 1
|
||||
#define WD_ENTRY_SHIFT 0
|
||||
#define WD_ON_CTRL 3
|
||||
#define WD_ON_DISPLAY 2
|
||||
#define WD_ON_CURSOR 1
|
||||
#define WD_ON_BLINK 0
|
||||
#define WD_MOVE 4
|
||||
#define WD_MOVE_DISP 3
|
||||
#define WD_MOVE_RIGHT 2
|
||||
#define WD_FUNCTION 5
|
||||
#define WD_FUNCTION_8BIT 4
|
||||
#define WD_FUNCTION_2LINES 3
|
||||
#define WD_FUNCTION_10DOTS 2
|
||||
#define WD_CGRAM 6
|
||||
#define WD_DDRAM 7
|
||||
#define WD_BUSY 7
|
||||
|
||||
#define DATA_8_BITS 0x0
|
||||
#define DATA_4_BITS 0x1
|
||||
|
||||
#define SERIAL 0x0
|
||||
#define DISPLAY 0x1
|
||||
|
||||
#define EXTERNAL_NUM_INTERRUPTS 8
|
||||
#define NUM_ENCODERS 4
|
||||
#define EXTERNAL_INT_0 0
|
||||
#define EXTERNAL_INT_1 1
|
||||
#define EXTERNAL_INT_2 2
|
||||
#define EXTERNAL_INT_3 3
|
||||
#define EXTERNAL_INT_4 4
|
||||
#define EXTERNAL_INT_5 5
|
||||
#define EXTERNAL_INT_6 6
|
||||
#define EXTERNAL_INT_7 7
|
||||
|
||||
void print(const char *template, ...);
|
||||
|
||||
#endif
|
40
core/global.h
Executable file
40
core/global.h
Executable file
@@ -0,0 +1,40 @@
|
||||
/*! \file global.h \brief AVRlib project global include. */
|
||||
//*****************************************************************************
|
||||
//
|
||||
// File Name : 'global.h'
|
||||
// Title : AVRlib project global include
|
||||
// Author : Pascal Stang - Copyright (C) 2001-2002
|
||||
// Created : 7/12/2001
|
||||
// Revised : 9/30/2002
|
||||
// Version : 1.1
|
||||
// Target MCU : Atmel AVR series
|
||||
// Editor Tabs : 4
|
||||
//
|
||||
// Description : This include file is designed to contain items useful to all
|
||||
// code files and projects.
|
||||
//
|
||||
// This code is distributed under the GNU Public License
|
||||
// which can be found at http://www.gnu.org/licenses/gpl.txt
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#ifndef GLOBAL_H
|
||||
#define GLOBAL_H
|
||||
|
||||
// global AVRLIB defines
|
||||
#include "avrlibdefs.h"
|
||||
// global AVRLIB types definitions
|
||||
#include "avrlibtypes.h"
|
||||
|
||||
// project/system dependent defines
|
||||
|
||||
// CPU clock speed
|
||||
//#define F_CPU 16000000 // 16MHz processor
|
||||
//#define F_CPU 14745000 // 14.745MHz processor
|
||||
//#define F_CPU 8000000 // 8MHz processor
|
||||
//#define F_CPU 7372800 // 7.37MHz processor
|
||||
//#define F_CPU 4000000 // 4MHz processor
|
||||
//#define F_CPU 3686400 // 3.69MHz processor
|
||||
#define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per microsecond
|
||||
|
||||
#endif
|
138
core/pins_arduino.c
Executable file
138
core/pins_arduino.c
Executable file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
pins_arduino.c - pin definitions for the Arduino board
|
||||
Part of Arduino / Wiring Lite
|
||||
|
||||
Copyright (c) 2005 David A. Mellis
|
||||
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
|
||||
$Id: pins_arduino.c,v 1.4 2005/05/24 17:47:41 mellis Exp $
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include "BConstants.h"
|
||||
#include "wiring.h"
|
||||
|
||||
// On the Arduino board, digital pins are also used
|
||||
// for the analog output (software PWM). Analog input
|
||||
// pins are a separate set.
|
||||
|
||||
// ATMEL ATMEGA8 / ARDUINO
|
||||
//
|
||||
// +-\/-+
|
||||
// PC6 1| |28 PC5 (AI 0)
|
||||
// (D 0) PD0 2| |27 PC4 (AI 1)
|
||||
// (D 1) PD1 3| |26 PC3 (AI 2)
|
||||
// (D 2) PD2 4| |25 PC2 (AI 3)
|
||||
// (D 3) PD3 5| |24 PC1 (AI 4)
|
||||
// (D 4) PD4 6| |23 PC0 (AI 5)
|
||||
// VCC 7| |22 GND
|
||||
// GND 8| |21 AREF
|
||||
// PB6 9| |20 AVCC
|
||||
// PB7 10| |19 PB5 (D 13)
|
||||
// (D 5) PD5 11| |18 PB4 (D 12)
|
||||
// (D 6) PD6 12| |17 PB3 (D 11)
|
||||
// (D 7) PD7 13| |16 PB2 (D 10)
|
||||
// (D 8) PB0 14| |15 PB1 (D 9)
|
||||
// +----+
|
||||
|
||||
#define NUM_DIGITAL_PINS 14
|
||||
#define NUM_ANALOG_OUT_PINS 11
|
||||
#define NUM_ANALOG_IN_PINS 6
|
||||
#define NUM_PORTS 4
|
||||
|
||||
#define PB 2
|
||||
#define PC 3
|
||||
#define PD 4
|
||||
|
||||
// these arrays map port names (e.g. port B) to the
|
||||
// appropriate addresses for various functions (e.g. reading
|
||||
// and writing)
|
||||
int port_to_mode[NUM_PORTS + 1] = {
|
||||
NOT_A_PORT,
|
||||
NOT_A_PORT,
|
||||
_SFR_IO_ADDR(DDRB),
|
||||
_SFR_IO_ADDR(DDRC),
|
||||
_SFR_IO_ADDR(DDRD),
|
||||
};
|
||||
|
||||
int port_to_output[NUM_PORTS + 1] = {
|
||||
NOT_A_PORT,
|
||||
NOT_A_PORT,
|
||||
_SFR_IO_ADDR(PORTB),
|
||||
_SFR_IO_ADDR(PORTC),
|
||||
_SFR_IO_ADDR(PORTD),
|
||||
};
|
||||
|
||||
int port_to_input[NUM_PORTS + 1] = {
|
||||
NOT_A_PORT,
|
||||
NOT_A_PORT,
|
||||
_SFR_IO_ADDR(PINB),
|
||||
_SFR_IO_ADDR(PINC),
|
||||
_SFR_IO_ADDR(PIND),
|
||||
};
|
||||
|
||||
// these arrays map the pin numbers on the arduino
|
||||
// board to the atmega8 port and pin numbers
|
||||
pin_t digital_pin_to_port_array[NUM_DIGITAL_PINS] = {
|
||||
{ PD, 0 },
|
||||
{ PD, 1 },
|
||||
{ PD, 2 },
|
||||
{ PD, 3 },
|
||||
{ PD, 4 },
|
||||
{ PD, 5 },
|
||||
{ PD, 6 },
|
||||
{ PD, 7 },
|
||||
{ PB, 0 },
|
||||
{ PB, 1 },
|
||||
{ PB, 2 },
|
||||
{ PB, 3 },
|
||||
{ PB, 4 },
|
||||
{ PB, 5 },
|
||||
};
|
||||
|
||||
pin_t *digital_pin_to_port = digital_pin_to_port_array;
|
||||
|
||||
// Some of the digital pins also support hardware PWM (analog output).
|
||||
pin_t analog_out_pin_to_port_array[NUM_DIGITAL_PINS] = {
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ PB, 1 },
|
||||
{ PB, 2 },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
};
|
||||
|
||||
pin_t *analog_out_pin_to_port = analog_out_pin_to_port_array;
|
||||
|
||||
pin_t analog_in_pin_to_port_array[NUM_ANALOG_IN_PINS] = {
|
||||
{ PC, 5 },
|
||||
{ PC, 4 },
|
||||
{ PC, 3 },
|
||||
{ PC, 2 },
|
||||
{ PC, 1 },
|
||||
{ PC, 0 },
|
||||
};
|
||||
|
||||
pin_t *analog_in_pin_to_port = analog_in_pin_to_port_array;
|
120
core/pins_atmega8.c
Executable file
120
core/pins_atmega8.c
Executable file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
pin_atmega8.c - pin definitions for the atmega8
|
||||
Part of Arduino / Wiring Lite
|
||||
|
||||
Copyright (c) 2005 David A. Mellis
|
||||
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
|
||||
$Id: pins_atmega8.c,v 1.4 2005/05/24 17:47:41 mellis Exp $
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include "BConstants.h"
|
||||
#include "wiring.h"
|
||||
|
||||
// We map the pin numbers passed to digitalRead() or
|
||||
// analogRead() directly to the corresponding pin
|
||||
// numbers on the Atmega8. No distinction is made
|
||||
// between analog and digital pins.
|
||||
|
||||
// ATMEL ATMEGA8
|
||||
//
|
||||
// +-\/-+
|
||||
// PC6 1| |28 PC5
|
||||
// PD0 2| |27 PC4
|
||||
// PD1 3| |26 PC3
|
||||
// PD2 4| |25 PC2
|
||||
// PD3 5| |24 PC1
|
||||
// PD4 6| |23 PC0
|
||||
// VCC 7| |22 GND
|
||||
// GND 8| |21 AREF
|
||||
// PB6 9| |20 AVCC
|
||||
// PB7 10| |19 PB5
|
||||
// PD5 11| |18 PB4
|
||||
// PD6 12| |17 PB3
|
||||
// PD7 13| |16 PB2
|
||||
// PB0 14| |15 PB1
|
||||
// +----+
|
||||
|
||||
#define NUM_PINS 28
|
||||
#define NUM_PORTS 4
|
||||
|
||||
#define PB 2
|
||||
#define PC 3
|
||||
#define PD 4
|
||||
|
||||
int port_to_mode[NUM_PORTS + 1] = {
|
||||
NOT_A_PORT,
|
||||
NOT_A_PORT,
|
||||
_SFR_IO_ADDR(DDRB),
|
||||
_SFR_IO_ADDR(DDRC),
|
||||
_SFR_IO_ADDR(DDRD),
|
||||
};
|
||||
|
||||
int port_to_output[NUM_PORTS + 1] = {
|
||||
NOT_A_PORT,
|
||||
NOT_A_PORT,
|
||||
_SFR_IO_ADDR(PORTB),
|
||||
_SFR_IO_ADDR(PORTC),
|
||||
_SFR_IO_ADDR(PORTD),
|
||||
};
|
||||
|
||||
int port_to_input[NUM_PORTS + 1] = {
|
||||
NOT_A_PORT,
|
||||
NOT_A_PORT,
|
||||
_SFR_IO_ADDR(PINB),
|
||||
_SFR_IO_ADDR(PINC),
|
||||
_SFR_IO_ADDR(PIND),
|
||||
};
|
||||
|
||||
pin_t digital_pin_to_port_array[] = {
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
|
||||
{ PC, 6 },
|
||||
{ PD, 0 },
|
||||
{ PD, 1 },
|
||||
{ PD, 2 },
|
||||
{ PD, 3 },
|
||||
{ PD, 4 },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ PB, 6 },
|
||||
{ PB, 7 },
|
||||
{ PD, 5 },
|
||||
{ PD, 6 },
|
||||
{ PD, 7 },
|
||||
{ PB, 0 },
|
||||
|
||||
{ PB, 1 },
|
||||
{ PB, 2 },
|
||||
{ PB, 3 },
|
||||
{ PB, 4 },
|
||||
{ PB, 5 },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ NOT_A_PIN, NOT_A_PIN },
|
||||
{ PC, 0 },
|
||||
{ PC, 1 },
|
||||
{ PC, 2 },
|
||||
{ PC, 3 },
|
||||
{ PC, 4 },
|
||||
{ PC, 5 },
|
||||
};
|
||||
|
||||
pin_t *digital_pin_to_port = digital_pin_to_port_array;
|
||||
pin_t *analog_in_pin_to_port = digital_pin_to_port_array;
|
||||
pin_t *analog_out_pin_to_port = digital_pin_to_port_array;
|
297
core/wiring.c
Executable file
297
core/wiring.c
Executable file
@@ -0,0 +1,297 @@
|
||||
/*
|
||||
wiring.c - Wiring API Partial Implementation
|
||||
Part of Arduino / Wiring Lite
|
||||
|
||||
Copyright (c) 2005 David A. Mellis
|
||||
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
|
||||
$Id: wiring.c,v 1.7 2005/05/28 21:04:15 mellis Exp $
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/signal.h>
|
||||
#include <avr/delay.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifndef cbi
|
||||
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
||||
#endif
|
||||
#ifndef sbi
|
||||
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
||||
#endif
|
||||
|
||||
// from Pascal's avrlib
|
||||
#include "global.h"
|
||||
//#include "a2d.h"
|
||||
#include "timer.h"
|
||||
#include "uart.h"
|
||||
|
||||
// timer.h #defines delay to be delay_us, we need to undefine
|
||||
// it so our delay can be in milliseconds.
|
||||
#undef delay
|
||||
|
||||
#include "BConstants.h"
|
||||
#include "wiring.h"
|
||||
|
||||
// Get the hardware port of the given virtual pin number. This comes from
|
||||
// the pins_*.c file for the active board configuration.
|
||||
int digitalPinToPort(int pin)
|
||||
{
|
||||
return digital_pin_to_port[pin].port;
|
||||
}
|
||||
|
||||
// Get the bit location within the hardware port of the given virtual pin.
|
||||
// This comes from the pins_*.c file for the active board configuration.
|
||||
int digitalPinToBit(int pin)
|
||||
{
|
||||
return digital_pin_to_port[pin].bit;
|
||||
}
|
||||
|
||||
int analogOutPinToPort(int pin)
|
||||
{
|
||||
return analog_out_pin_to_port[pin].port;
|
||||
}
|
||||
|
||||
int analogOutPinToBit(int pin)
|
||||
{
|
||||
return analog_out_pin_to_port[pin].bit;
|
||||
}
|
||||
|
||||
int analogInPinToBit(int pin)
|
||||
{
|
||||
return analog_in_pin_to_port[pin].bit;
|
||||
}
|
||||
|
||||
void pinMode(int pin, int mode)
|
||||
{
|
||||
if (digitalPinToPort(pin) != NOT_A_PIN) {
|
||||
if (mode == INPUT)
|
||||
cbi(_SFR_IO8(port_to_mode[digitalPinToPort(pin)]), digitalPinToBit(pin));
|
||||
else
|
||||
sbi(_SFR_IO8(port_to_mode[digitalPinToPort(pin)]), digitalPinToBit(pin));
|
||||
}
|
||||
}
|
||||
|
||||
void digitalWrite(int pin, int val)
|
||||
{
|
||||
if (digitalPinToPort(pin) != NOT_A_PIN) {
|
||||
// If the pin that support PWM output, we need to turn it off
|
||||
// before doing a digital write.
|
||||
|
||||
if (analogOutPinToBit(pin) == 1)
|
||||
timer1PWMAOff();
|
||||
|
||||
if (analogOutPinToBit(pin) == 2)
|
||||
timer1PWMBOff();
|
||||
|
||||
if (val == LOW)
|
||||
cbi(_SFR_IO8(port_to_output[digitalPinToPort(pin)]), digitalPinToBit(pin));
|
||||
else
|
||||
sbi(_SFR_IO8(port_to_output[digitalPinToPort(pin)]), digitalPinToBit(pin));
|
||||
}
|
||||
}
|
||||
|
||||
int digitalRead(int pin)
|
||||
{
|
||||
if (digitalPinToPort(pin) != NOT_A_PIN) {
|
||||
// If the pin that support PWM output, we need to turn it off
|
||||
// before getting a digital reading.
|
||||
|
||||
if (analogOutPinToBit(pin) == 1)
|
||||
timer1PWMAOff();
|
||||
|
||||
if (analogOutPinToBit(pin) == 2)
|
||||
timer1PWMBOff();
|
||||
|
||||
return (_SFR_IO8(port_to_input[digitalPinToPort(pin)]) >> digitalPinToBit(pin)) & 0x01;
|
||||
}
|
||||
|
||||
return LOW;
|
||||
}
|
||||
|
||||
/*
|
||||
int analogRead(int pin)
|
||||
{
|
||||
unsigned long start_time = millis();
|
||||
int ch = analogInPinToBit(pin);
|
||||
volatile unsigned int low, high;
|
||||
|
||||
//return a2dConvert10bit(ch);
|
||||
|
||||
a2dSetChannel(ch);
|
||||
a2dStartConvert();
|
||||
|
||||
// wait until the conversion is complete or we
|
||||
// time out. without the timeout, this sometimes
|
||||
// becomes an infinite loop. page 245 of the atmega8
|
||||
// datasheet says the conversion should take at most
|
||||
// 260 microseconds, so if two milliseconds have ticked
|
||||
// by, something's wrong.
|
||||
//while (!a2dIsComplete() && millis() - start_time < 50);
|
||||
while (!a2dIsComplete());
|
||||
|
||||
// a2Convert10bit sometimes read ADCL and ADCH in the
|
||||
// wrong order (?) causing it to sometimes miss reading,
|
||||
// especially if called multiple times in rapid succession.
|
||||
//return a2dConvert10bit(ch);
|
||||
//return ADCW;
|
||||
low = ADCL;
|
||||
high = ADCH;
|
||||
|
||||
return (high << 8) | low;
|
||||
}
|
||||
*/
|
||||
|
||||
int analogRead(int pin)
|
||||
{
|
||||
unsigned int low, high, ch = analogInPinToBit(pin);
|
||||
|
||||
// the low 4 bits of ADMUX select the ADC channel
|
||||
ADMUX = (ADMUX & (unsigned int) 0xf0) | (ch & (unsigned int) 0x0f);
|
||||
|
||||
// without a delay, we seem to read from the wrong channel
|
||||
delay(1);
|
||||
|
||||
// start the conversion
|
||||
sbi(ADCSRA, ADSC);
|
||||
|
||||
// ADSC is cleared when the conversion finishes
|
||||
while (bit_is_set(ADCSRA, ADSC));
|
||||
|
||||
// we have to read ADCL first; doing so locks both ADCL
|
||||
// and ADCH until ADCH is read. reading ADCL second would
|
||||
// cause the results of each conversion to be discarded,
|
||||
// as ADCL and ADCH would be locked when it completed.
|
||||
low = ADCL;
|
||||
high = ADCH;
|
||||
|
||||
// combine the two bytes
|
||||
return (high << 8) | low;
|
||||
}
|
||||
|
||||
// Right now, PWM output only works on the pins with
|
||||
// hardware support. These are defined in the appropriate
|
||||
// pins_*.c file. For the rest of the pins, we default
|
||||
// to digital output.
|
||||
void analogWrite(int pin, int val)
|
||||
{
|
||||
// We need to make sure the PWM output is enabled for those pins
|
||||
// that support it, as we turn it off when digitally reading or
|
||||
// writing with them. Also, make sure the pin is in output mode
|
||||
// for consistenty with Wiring, which doesn't require a pinMode
|
||||
// call for the analog output pins.
|
||||
if (analogOutPinToBit(pin) == 1) {
|
||||
pinMode(pin, OUTPUT);
|
||||
timer1PWMAOn();
|
||||
timer1PWMASet(val);
|
||||
} else if (analogOutPinToBit(pin) == 2) {
|
||||
pinMode(pin, OUTPUT);
|
||||
timer1PWMBOn();
|
||||
timer1PWMBSet(val);
|
||||
} else if (val < 128)
|
||||
digitalWrite(pin, LOW);
|
||||
else
|
||||
digitalWrite(pin, HIGH);
|
||||
}
|
||||
|
||||
void beginSerial(int baud)
|
||||
{
|
||||
uartInit();
|
||||
uartSetBaudRate(baud);
|
||||
}
|
||||
|
||||
void serialWrite(unsigned char c)
|
||||
{
|
||||
uartSendByte(c);
|
||||
}
|
||||
|
||||
void printMode(int mode)
|
||||
{
|
||||
// do nothing, we only support serial printing, not lcd.
|
||||
}
|
||||
|
||||
void uartSendString(unsigned char *str)
|
||||
{
|
||||
while (*str)
|
||||
uartSendByte(*str++);
|
||||
}
|
||||
|
||||
void print(const char *format, ...)
|
||||
{
|
||||
char buf[256];
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
vsnprintf(buf, 256, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
uartSendString(buf);
|
||||
}
|
||||
|
||||
unsigned long millis()
|
||||
{
|
||||
// timer 0 increments every timer0GetPrescaler() cycles, and
|
||||
// overflows when it reaches 256. we calculate the total
|
||||
// number of clock cycles, then divide by the number of clock
|
||||
// cycles per millisecond.
|
||||
return timer0GetOverflowCount() * timer0GetPrescaler() * 256L / F_CPU * 1000L;
|
||||
}
|
||||
|
||||
void delay(unsigned long ms)
|
||||
{
|
||||
timerPause(ms);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
sei();
|
||||
|
||||
// timer 0 is used for millis() and delay()
|
||||
timer0Init();
|
||||
|
||||
// timer 1 is used for the hardware pwm
|
||||
timer1Init();
|
||||
timer1SetPrescaler(TIMER_CLK_DIV1);
|
||||
timer1PWMInit(8);
|
||||
|
||||
//a2dInit();
|
||||
//a2dSetPrescaler(ADC_PRESCALE_DIV128);
|
||||
|
||||
// set a2d reference to AVCC (5 volts)
|
||||
cbi(ADMUX, REFS1);
|
||||
sbi(ADMUX, REFS0);
|
||||
|
||||
// set a2d prescale factor to 128
|
||||
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
|
||||
// XXX: this will not work properly for other clock speeds, and
|
||||
// this code should use F_CPU to determine the prescale factor.
|
||||
sbi(ADCSRA, ADPS2);
|
||||
sbi(ADCSRA, ADPS1);
|
||||
sbi(ADCSRA, ADPS0);
|
||||
|
||||
// enable a2d conversions
|
||||
sbi(ADCSRA, ADEN);
|
||||
|
||||
setup();
|
||||
|
||||
for (;;)
|
||||
loop();
|
||||
|
||||
return 0;
|
||||
}
|
67
core/wiring.h
Executable file
67
core/wiring.h
Executable file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
wiring.h - Wiring API Partial Implementation
|
||||
Part of Arduino / Wiring Lite
|
||||
|
||||
Copyright (c) 2005 David A. Mellis
|
||||
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
|
||||
$Id: wiring.h,v 1.4 2005/05/24 17:47:41 mellis Exp $
|
||||
*/
|
||||
|
||||
#ifndef Wiring_h
|
||||
#define Wiring_h
|
||||
|
||||
|
||||
#define HIGH 0x1
|
||||
#define LOW 0x0
|
||||
|
||||
#define INPUT 0x0
|
||||
#define OUTPUT 0x1
|
||||
|
||||
#define true 0x1
|
||||
#define false 0x0
|
||||
|
||||
|
||||
#define NOT_A_PIN 0
|
||||
#define NOT_A_PORT -1
|
||||
|
||||
void delay(unsigned long);
|
||||
void delay_ms(unsigned short ms);
|
||||
void pinMode(int, int);
|
||||
void digitalWrite(int, int);
|
||||
int digitalRead(int);
|
||||
void analogWrite(int, int);
|
||||
int analogRead(int);
|
||||
unsigned long millis(void);
|
||||
void setup(void);
|
||||
void loop(void);
|
||||
void beginSerial(int);
|
||||
void serialWrite(unsigned char);
|
||||
|
||||
typedef struct {
|
||||
int port;
|
||||
int bit;
|
||||
} pin_t;
|
||||
|
||||
extern int port_to_mode[];
|
||||
extern int port_to_input[];
|
||||
extern int port_to_output[];
|
||||
extern pin_t *digital_pin_to_port;
|
||||
extern pin_t *analog_in_pin_to_port;
|
||||
extern pin_t *analog_out_pin_to_port;
|
||||
|
||||
#endif
|
15
core/wiringlite.inc
Executable file
15
core/wiringlite.inc
Executable file
@@ -0,0 +1,15 @@
|
||||
#define F_CPU 16000000UL
|
||||
#define CPU_FREQ 16000000L
|
||||
#define XTAL_CPU 16000000L
|
||||
#define UART_BAUD_RATE 9600
|
||||
|
||||
#ifndef __AVR_ATmega8__
|
||||
#define __AVR_ATmega8__
|
||||
#endif
|
||||
|
||||
#define __MCU_CLOCK_FREQUENCY__ _16.0000_MHz
|
||||
//#include "BConstants.h"
|
||||
#include "wiring.h"
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/signal.h>
|
Reference in New Issue
Block a user