Skip to content

Modification of ArduinoCore-avr to use API #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 6 additions & 111 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
@@ -20,47 +20,16 @@
#ifndef Arduino_h
#define Arduino_h

#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>

#include "api/ArduinoAPI.h"
#include "api/Common.h"
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#include "binary.h"

#ifdef __cplusplus
extern "C"{
#endif

void yield(void);

#define HIGH 0x1
#define LOW 0x0

#define INPUT 0x0
#define OUTPUT 0x1
#define INPUT_PULLUP 0x2

#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
#define TWO_PI 6.283185307179586476925286766559
#define DEG_TO_RAD 0.017453292519943295769236907684886
#define RAD_TO_DEG 57.295779513082320876798154814105
#define EULER 2.718281828459045235360287471352

#define SERIAL 0x0
#define DISPLAY 0x1

#define LSBFIRST 0
#define MSBFIRST 1

#define CHANGE 1
#define FALLING 2
#define RISING 3

#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
#define DEFAULT 0
#define EXTERNAL 1
@@ -84,75 +53,18 @@ void yield(void);
#define EXTERNAL 0
#endif

// undefine stdlib's abs if encountered
#ifdef abs
#undef abs
#endif

#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 round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
#define sq(x) ((x)*(x))

#define interrupts() sei()
#define noInterrupts() cli()

#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )

#define lowByte(w) ((uint8_t) ((w) & 0xff))
#define highByte(w) ((uint8_t) ((w) >> 8))

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitToggle(value, bit) ((value) ^= (1UL << (bit)))
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))

// avr-libc defines _NOP() since 1.6.2
#ifndef _NOP
#define _NOP() do { __asm__ volatile ("nop"); } while (0)
#endif

typedef unsigned int word;

#define bit(b) (1UL << (b))

typedef bool boolean;
typedef uint8_t byte;

void init(void);
void initVariant(void);

int atexit(void (*func)()) __attribute__((weak));

void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);
int analogRead(uint8_t pin);
void analogReference(uint8_t mode);
void analogWrite(uint8_t pin, int val);

unsigned long millis(void);
unsigned long micros(void);
void delay(unsigned long ms);
void delayMicroseconds(unsigned int us);
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode);
void detachInterrupt(uint8_t interruptNum);

void setup(void);
void loop(void);

// 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.
@@ -228,31 +140,14 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
#endif

#ifdef __cplusplus
#include "WCharacter.h"
#include "WString.h"
#include "HardwareSerial.h"
#include "USBAPI.h"
#include "UART.h"
#include "USBCore.h"
#include "CDC.h"
#include "MSC.h"
#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
#error "Targets with both UART0 and CDC serial not supported"
#endif

uint16_t makeWord(uint16_t w);
uint16_t makeWord(byte h, byte l);

#define word(...) makeWord(__VA_ARGS__)

unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
void noTone(uint8_t _pin);

// WMath prototypes
long random(long);
long random(long, long);
void randomSeed(unsigned long);
long map(long, long, long, long, long);

#endif

#include "pins_arduino.h"
28 changes: 16 additions & 12 deletions cores/arduino/CDC.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


/* Copyright (c) 2011, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
@@ -16,25 +14,31 @@
** SOFTWARE.
*/

#include "USBAPI.h"
#define RINGBUFFER_FORCE_SMALL_SIZE

#include <avr/wdt.h>
#include <util/atomic.h>
#include <avr/pgmspace.h>
#include "CDC.h"
#include "api/USBAPI.h"
#include "USBCore.h"
#include "api/Common.h"

#if defined(USBCON)

typedef struct
{
u32 dwDTERate;
u8 bCharFormat;
u8 bParityType;
u8 bDataBits;
u8 lineState;
uint32_t dwDTERate;
uint8_t bCharFormat;
uint8_t bParityType;
uint8_t bDataBits;
uint8_t lineState;
} LineInfo;

static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
static volatile int32_t breakValue = -1;

static u8 wdtcsr_save;
static uint8_t wdtcsr_save;

#define WEAK __attribute__ ((weak))

@@ -62,16 +66,16 @@ bool isLUFAbootloader()
return pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE;
}

int CDC_GetInterface(u8* interfaceNum)
int CDC_GetInterface(uint8_t* interfaceNum)
{
interfaceNum[0] += 2; // uses 2
return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
}

bool CDC_Setup(USBSetup& setup)
{
u8 r = setup.bRequest;
u8 requestType = setup.bmRequestType;
uint8_t r = setup.bRequest;
uint8_t requestType = setup.bmRequestType;

if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
{
104 changes: 104 additions & 0 deletions cores/arduino/CDC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#ifndef __CDC_H__
#define __CDC_H__

#include "Arduino.h"
#include "api/Stream.h"
#include "api/USBAPI.h"

#if defined (USBCON)

//================================================================================
//================================================================================
// Serial over CDC (Serial1 is the physical port)

#define RINGBUFFER_FORCE_SMALL_SIZE
#include "api/RingBuffer.h"

#ifndef SERIAL_BUFFER_SIZE
#if ((RAMEND - RAMSTART) < 1023)
#define SERIAL_BUFFER_SIZE 16
#else
#define SERIAL_BUFFER_SIZE 64
#endif
#endif
#if (SERIAL_BUFFER_SIZE > 256)
#error Please lower the CDC Buffer size
#endif

class Serial_ : public Stream
{
private:
int peek_buffer;
public:
Serial_() { peek_buffer = -1; };
void begin(unsigned long);
void begin(unsigned long, uint8_t);
void end(void);

virtual int available(void);
virtual int peek(void);
virtual int read(void);
virtual int availableForWrite(void);
virtual void flush(void);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t*, size_t);
using Print::write; // pull in write(str) and write(buf, size) from Print
operator bool();

//RingBuffer _rx_buffer(SERIAL_BUFFER_SIZE);

// This method allows processing "SEND_BREAK" requests sent by
// the USB host. Those requests indicate that the host wants to
// send a BREAK signal and are accompanied by a single uint16_t
// value, specifying the duration of the break. The value 0
// means to end any current break, while the value 0xffff means
// to start an indefinite break.
// readBreak() will return the value of the most recent break
// request, but will return it at most once, returning -1 when
// readBreak() is called again (until another break request is
// received, which is again returned once).
// This also mean that if two break requests are received
// without readBreak() being called in between, the value of the
// first request is lost.
// Note that the value returned is a long, so it can return
// 0-0xffff as well as -1.
int32_t readBreak();

// These return the settings specified by the USB host for the
// serial port. These aren't really used, but are offered here
// in case a sketch wants to act on these settings.
uint32_t baud();
uint8_t stopbits();
uint8_t paritytype();
uint8_t numbits();
bool dtr();
bool rts();
enum {
ONE_STOP_BIT = 0,
ONE_AND_HALF_STOP_BIT = 1,
TWO_STOP_BITS = 2,
};
enum {
NO_PARITY = 0,
ODD_PARITY = 1,
EVEN_PARITY = 2,
MARK_PARITY = 3,
SPACE_PARITY = 4,
};

};
extern Serial_ Serial;

#define HAVE_CDCSERIAL

//================================================================================
//================================================================================
// CSC 'Driver'

int CDC_GetInterface(uint8_t* interfaceNum);
int CDC_GetDescriptor(int i);
bool CDC_Setup(USBSetup& setup);

#endif

#endif
45 changes: 0 additions & 45 deletions cores/arduino/Client.h

This file was deleted.

114 changes: 0 additions & 114 deletions cores/arduino/IPAddress.cpp

This file was deleted.

78 changes: 0 additions & 78 deletions cores/arduino/IPAddress.h

This file was deleted.

19 changes: 19 additions & 0 deletions cores/arduino/MSC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if defined (USBCON)

#ifndef __MSC_H__
#define __MSC_H__

#include "api/USBAPI.h"

//================================================================================
//================================================================================
// MSC 'Driver'

int MSC_GetInterface(uint8_t* interfaceNum);
int MSC_GetDescriptor(int i);
bool MSC_Setup(USBSetup& setup);
bool MSC_Data(uint8_t rx,uint8_t tx);

#endif

#endif
115 changes: 0 additions & 115 deletions cores/arduino/PluggableUSB.cpp

This file was deleted.

74 changes: 0 additions & 74 deletions cores/arduino/PluggableUSB.h

This file was deleted.

266 changes: 0 additions & 266 deletions cores/arduino/Print.cpp

This file was deleted.

93 changes: 0 additions & 93 deletions cores/arduino/Print.h

This file was deleted.

40 changes: 0 additions & 40 deletions cores/arduino/Printable.h

This file was deleted.

30 changes: 0 additions & 30 deletions cores/arduino/Server.h

This file was deleted.

318 changes: 0 additions & 318 deletions cores/arduino/Stream.cpp

This file was deleted.

129 changes: 0 additions & 129 deletions cores/arduino/Stream.h

This file was deleted.

2 changes: 1 addition & 1 deletion cores/arduino/Tone.cpp
Original file line number Diff line number Diff line change
@@ -491,7 +491,7 @@ void noTone(uint8_t _pin)

disableTimer(_timer);

digitalWrite(_pin, 0);
digitalWrite(_pin, LOW);
}

#ifdef USE_TIMER0
31 changes: 16 additions & 15 deletions cores/arduino/HardwareSerial.cpp → cores/arduino/UART.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HardwareSerial.cpp - Hardware serial library for Wiring
UART.cpp - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
@@ -27,20 +27,21 @@
#include <string.h>
#include <inttypes.h>
#include <util/atomic.h>
#include <avr/io.h>
#include "Arduino.h"

#include "HardwareSerial.h"
#include "HardwareSerial_private.h"
#include "UART.h"
#include "UART_private.h"

// this next line disables the entire HardwareSerial.cpp,
// this next line disables the entire UART.cpp,
// this is so I can support Attiny series and any other chip without a uart
#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)

// SerialEvent functions are weak, so when the user doesn't define them,
// the linker just sets their address to 0 (which is checked below).
// The Serialx_available is just a wrapper around Serialx.available(),
// but we can refer to it weakly so we don't pull in the entire
// HardwareSerial instance if the user doesn't also refer to it.
// UART instance if the user doesn't also refer to it.
#if defined(HAVE_HWSERIAL0)
void serialEvent() __attribute__((weak));
bool Serial0_available() __attribute__((weak));
@@ -78,15 +79,15 @@ void serialEventRun(void)
}

// macro to guard critical sections when needed for large TX buffer sizes
#if (SERIAL_TX_BUFFER_SIZE>256)
#if (SERIAL_TX_BUFFER_SIZE > 256)
#define TX_BUFFER_ATOMIC ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
#else
#define TX_BUFFER_ATOMIC
#endif

// Actual interrupt handlers //////////////////////////////////////////////////////////////

void HardwareSerial::_tx_udr_empty_irq(void)
void UartClass::_tx_udr_empty_irq(void)
{
// If interrupts are enabled, there must be more data in the output
// buffer. Send the next byte
@@ -114,7 +115,7 @@ void HardwareSerial::_tx_udr_empty_irq(void)

// Public Methods //////////////////////////////////////////////////////////////

void HardwareSerial::begin(unsigned long baud, byte config)
void UartClass::begin(unsigned long baud, uint16_t config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config is later assigned to a *_ucsrc which is declared as a uint8_t in UART.h. Was this change intentional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to https://github.com/arduino/ArduinoCore-API/blob/master/api/HardwareSerial.h#L91 , so the signature must be uint16_t .
Said that, I'll push a commit to fix this by casting the assignment
*_ucsrc = (uint8_t)config; (since we are assured that config fits an uint8_t)

{
// Try u2x mode first
uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
@@ -149,7 +150,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
cbi(*_ucsrb, UDRIE0);
}

void HardwareSerial::end()
void UartClass::end()
{
// wait for transmission of outgoing data
flush();
@@ -163,12 +164,12 @@ void HardwareSerial::end()
_rx_buffer_head = _rx_buffer_tail;
}

int HardwareSerial::available(void)
int UartClass::available(void)
{
return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE;
}

int HardwareSerial::peek(void)
int UartClass::peek(void)
{
if (_rx_buffer_head == _rx_buffer_tail) {
return -1;
@@ -177,7 +178,7 @@ int HardwareSerial::peek(void)
}
}

int HardwareSerial::read(void)
int UartClass::read(void)
{
// if the head isn't ahead of the tail, we don't have any characters
if (_rx_buffer_head == _rx_buffer_tail) {
@@ -189,7 +190,7 @@ int HardwareSerial::read(void)
}
}

int HardwareSerial::availableForWrite(void)
int UartClass::availableForWrite(void)
{
tx_buffer_index_t head;
tx_buffer_index_t tail;
@@ -202,7 +203,7 @@ int HardwareSerial::availableForWrite(void)
return tail - head - 1;
}

void HardwareSerial::flush()
void UartClass::flush()
{
// If we have never written a byte, no need to flush. This special
// case is needed since there is no way to force the TXC (transmit
@@ -222,7 +223,7 @@ void HardwareSerial::flush()
// the hardware finished tranmission (TXC is set).
}

size_t HardwareSerial::write(uint8_t c)
size_t UartClass::write(uint8_t c)
{
_written = true;
// If the buffer and the data register is empty, just write the byte
53 changes: 40 additions & 13 deletions cores/arduino/HardwareSerial.h → cores/arduino/UART.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HardwareSerial.h - Hardware serial library for Wiring
UART.h - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
@@ -21,12 +21,16 @@
Modified 3 December 2013 by Matthijs Kooijman
*/

#ifndef HardwareSerial_h
#define HardwareSerial_h
#ifndef _UART_H_
#define _UART_H_

#include "Arduino.h"
#include <inttypes.h>
#include "api/HardwareSerial.h"

#include "Stream.h"
#if ARDUINO_API_VERSION > 10000
using namespace arduino;
Copy link
Contributor

@bxparks bxparks Apr 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment to explain why this using namespace statement is needed. It us contrary to the usual recommendation of avoiding using namespace in a C++ header file: https://stackoverflow.com/questions/5849457/using-namespace-in-c-headers

#endif

// Define constants and variables for buffering incoming serial data. We're
// using a ring buffer (I think), in which head is the index of the location
@@ -65,6 +69,31 @@ typedef uint8_t rx_buffer_index_t;
#endif

// Define config for Serial.begin(baud, config);
#undef SERIAL_5N1
#undef SERIAL_6N1
#undef SERIAL_7N1
#undef SERIAL_8N1
#undef SERIAL_5N2
#undef SERIAL_6N2
#undef SERIAL_7N2
#undef SERIAL_8N2
#undef SERIAL_5E1
#undef SERIAL_6E1
#undef SERIAL_7E1
#undef SERIAL_8E1
#undef SERIAL_5E2
#undef SERIAL_6E2
#undef SERIAL_7E2
#undef SERIAL_8E2
#undef SERIAL_5O1
#undef SERIAL_6O1
#undef SERIAL_7O1
#undef SERIAL_8O1
#undef SERIAL_5O2
#undef SERIAL_6O2
#undef SERIAL_7O2
#undef SERIAL_8O2

#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
@@ -90,7 +119,7 @@ typedef uint8_t rx_buffer_index_t;
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E

class HardwareSerial : public Stream
class UartClass : public HardwareSerial
{
protected:
volatile uint8_t * const _ubrrh;
@@ -114,12 +143,12 @@ class HardwareSerial : public Stream
unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];

public:
inline HardwareSerial(
inline UartClass(
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *ucsrc, volatile uint8_t *udr);
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
void begin(unsigned long, uint8_t);
void begin(unsigned long, uint16_t);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above, it's not clear why this was changed to a uint16_t, when the config is later assigned only to a uint8_t.

void end();
virtual int available(void);
virtual int peek(void);
@@ -140,22 +169,20 @@ class HardwareSerial : public Stream
};

#if defined(UBRRH) || defined(UBRR0H)
extern HardwareSerial Serial;
extern UartClass Serial;
#define HAVE_HWSERIAL0
#endif
#if defined(UBRR1H)
extern HardwareSerial Serial1;
extern UartClass Serial1;
#define HAVE_HWSERIAL1
#endif
#if defined(UBRR2H)
extern HardwareSerial Serial2;
extern UartClass Serial2;
#define HAVE_HWSERIAL2
#endif
#if defined(UBRR3H)
extern HardwareSerial Serial3;
extern UartClass Serial3;
#define HAVE_HWSERIAL3
#endif

extern void serialEventRun(void) __attribute__((weak));

#endif
14 changes: 7 additions & 7 deletions cores/arduino/HardwareSerial0.cpp → cores/arduino/UART0.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HardwareSerial0.cpp - Hardware serial library for Wiring
UART0.cpp - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
*/

#include "Arduino.h"
#include "HardwareSerial.h"
#include "HardwareSerial_private.h"
#include "UART.h"
#include "UART_private.h"

// Each HardwareSerial is defined in its own file, since the linker pulls
// Each UartClass is defined in its own file, since the linker pulls
// in the entire file when any element inside is used. --gc-sections can
// additionally cause unused symbols to be dropped, but ISRs have the
// "used" attribute so are never dropped and they keep the
// HardwareSerial instance in as well. Putting each instance in its own
// UartClass instance in as well. Putting each instance in its own
// file prevents the linker from pulling in any unused instances in the
// first place.

@@ -65,9 +65,9 @@ ISR(USART_UDRE_vect)
}

#if defined(UBRRH) && defined(UBRRL)
HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
UartClass Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
#else
HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
UartClass Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
#endif

// Function that can be weakly referenced by serialEventRun to prevent
12 changes: 6 additions & 6 deletions cores/arduino/HardwareSerial1.cpp → cores/arduino/UART1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HardwareSerial1.cpp - Hardware serial library for Wiring
UART1.cpp - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
*/

#include "Arduino.h"
#include "HardwareSerial.h"
#include "HardwareSerial_private.h"
#include "UART.h"
#include "UART_private.h"

// Each HardwareSerial is defined in its own file, since the linker pulls
// Each UartClass is defined in its own file, since the linker pulls
// in the entire file when any element inside is used. --gc-sections can
// additionally cause unused symbols to be dropped, but ISRs have the
// "used" attribute so are never dropped and they keep the
// HardwareSerial instance in as well. Putting each instance in its own
// UartClass instance in as well. Putting each instance in its own
// file prevents the linker from pulling in any unused instances in the
// first place.

@@ -58,7 +58,7 @@ ISR(USART1_UDRE_vect)
Serial1._tx_udr_empty_irq();
}

HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
UartClass Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);

// Function that can be weakly referenced by serialEventRun to prevent
// pulling in this file if it's not otherwise used.
12 changes: 6 additions & 6 deletions cores/arduino/HardwareSerial2.cpp → cores/arduino/UART2.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HardwareSerial2.cpp - Hardware serial library for Wiring
UartClass2.cpp - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
*/

#include "Arduino.h"
#include "HardwareSerial.h"
#include "HardwareSerial_private.h"
#include "UART.h"
#include "UART_private.h"

// Each HardwareSerial is defined in its own file, since the linker pulls
// Each UartClass is defined in its own file, since the linker pulls
// in the entire file when any element inside is used. --gc-sections can
// additionally cause unused symbols to be dropped, but ISRs have the
// "used" attribute so are never dropped and they keep the
// HardwareSerial instance in as well. Putting each instance in its own
// UartClass instance in as well. Putting each instance in its own
// file prevents the linker from pulling in any unused instances in the
// first place.

@@ -46,7 +46,7 @@ ISR(USART2_UDRE_vect)
Serial2._tx_udr_empty_irq();
}

HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
UartClass Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);

// Function that can be weakly referenced by serialEventRun to prevent
// pulling in this file if it's not otherwise used.
12 changes: 6 additions & 6 deletions cores/arduino/HardwareSerial3.cpp → cores/arduino/UART3.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HardwareSerial3.cpp - Hardware serial library for Wiring
UartClass3.cpp - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
*/

#include "Arduino.h"
#include "HardwareSerial.h"
#include "HardwareSerial_private.h"
#include "UART.h"
#include "UART_private.h"

// Each HardwareSerial is defined in its own file, since the linker pulls
// Each UartClass is defined in its own file, since the linker pulls
// in the entire file when any element inside is used. --gc-sections can
// additionally cause unused symbols to be dropped, but ISRs have the
// "used" attribute so are never dropped and they keep the
// HardwareSerial instance in as well. Putting each instance in its own
// UartClass instance in as well. Putting each instance in its own
// file prevents the linker from pulling in any unused instances in the
// first place.

@@ -46,7 +46,7 @@ ISR(USART3_UDRE_vect)
Serial3._tx_udr_empty_irq();
}

HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
UartClass Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);

// Function that can be weakly referenced by serialEventRun to prevent
// pulling in this file if it's not otherwise used.
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@

// Constructors ////////////////////////////////////////////////////////////////

HardwareSerial::HardwareSerial(
UartClass::UartClass(
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *ucsrc, volatile uint8_t *udr) :
@@ -98,7 +98,7 @@ HardwareSerial::HardwareSerial(

// Actual interrupt handlers //////////////////////////////////////////////////////////////

void HardwareSerial::_rx_complete_irq(void)
void UartClass::_rx_complete_irq(void)
{
if (bit_is_clear(*_ucsra, UPE0)) {
// No Parity error, read byte and store it in the buffer if there is
209 changes: 0 additions & 209 deletions cores/arduino/USBAPI.h

This file was deleted.

147 changes: 81 additions & 66 deletions cores/arduino/USBCore.cpp

Large diffs are not rendered by default.

196 changes: 131 additions & 65 deletions cores/arduino/USBCore.h
Original file line number Diff line number Diff line change
@@ -18,7 +18,25 @@
#ifndef __USBCORE_H__
#define __USBCORE_H__

#include "USBAPI.h"
#include <stdint.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

class USBDevice_
{
public:
USBDevice_();
bool configured();

void attach();
void detach(); // Serial port goes down too...
void poll();
bool wakeupHost(); // returns false, when wakeup cannot be processed

bool isSuspended();
};

extern USBDevice_ USBDevice;

// Standard requests
#define GET_STATUS 0
@@ -32,7 +50,6 @@
#define GET_INTERFACE 10
#define SET_INTERFACE 11


// bmRequestType
#define REQUEST_HOSTTODEVICE 0x00
#define REQUEST_DEVICETOHOST 0x80
@@ -133,110 +150,114 @@
#define USB_VERSION 0x200
#endif

#define TRANSFER_PGM 0x80
#define TRANSFER_RELEASE 0x40
#define TRANSFER_ZERO 0x20

// Device
typedef struct {
u8 len; // 18
u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
u16 usbVersion; // 0x200 or 0x210
u8 deviceClass;
u8 deviceSubClass;
u8 deviceProtocol;
u8 packetSize0; // Packet 0
u16 idVendor;
u16 idProduct;
u16 deviceVersion; // 0x100
u8 iManufacturer;
u8 iProduct;
u8 iSerialNumber;
u8 bNumConfigurations;
uint8_t len; // 18
uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
uint16_t usbVersion; // 0x200 or 0x210
uint8_t deviceClass;
uint8_t deviceSubClass;
uint8_t deviceProtocol;
uint8_t packetSize0; // Packet 0
uint16_t idVendor;
uint16_t idProduct;
uint16_t deviceVersion; // 0x100
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} DeviceDescriptor;

// Config
typedef struct {
u8 len; // 9
u8 dtype; // 2
u16 clen; // total length
u8 numInterfaces;
u8 config;
u8 iconfig;
u8 attributes;
u8 maxPower;
uint8_t len; // 9
uint8_t dtype; // 2
uint16_t clen; // total length
uint8_t numInterfaces;
uint8_t config;
uint8_t iconfig;
uint8_t attributes;
uint8_t maxPower;
} ConfigDescriptor;

// String

// Interface
typedef struct
{
u8 len; // 9
u8 dtype; // 4
u8 number;
u8 alternate;
u8 numEndpoints;
u8 interfaceClass;
u8 interfaceSubClass;
u8 protocol;
u8 iInterface;
uint8_t len; // 9
uint8_t dtype; // 4
uint8_t number;
uint8_t alternate;
uint8_t numEndpoints;
uint8_t interfaceClass;
uint8_t interfaceSubClass;
uint8_t protocol;
uint8_t iInterface;
} InterfaceDescriptor;

// Endpoint
typedef struct
{
u8 len; // 7
u8 dtype; // 5
u8 addr;
u8 attr;
u16 packetSize;
u8 interval;
uint8_t len; // 7
uint8_t dtype; // 5
uint8_t addr;
uint8_t attr;
uint16_t packetSize;
uint8_t interval;
} EndpointDescriptor;

// Interface Association Descriptor
// Used to bind 2 interfaces together in CDC compostite device
typedef struct
{
u8 len; // 8
u8 dtype; // 11
u8 firstInterface;
u8 interfaceCount;
u8 functionClass;
u8 funtionSubClass;
u8 functionProtocol;
u8 iInterface;
uint8_t len; // 8
uint8_t dtype; // 11
uint8_t firstInterface;
uint8_t interfaceCount;
uint8_t functionClass;
uint8_t funtionSubClass;
uint8_t functionProtocol;
uint8_t iInterface;
} IADDescriptor;

// CDC CS interface descriptor
typedef struct
{
u8 len; // 5
u8 dtype; // 0x24
u8 subtype;
u8 d0;
u8 d1;
uint8_t len; // 5
uint8_t dtype; // 0x24
uint8_t subtype;
uint8_t d0;
uint8_t d1;
} CDCCSInterfaceDescriptor;

typedef struct
{
u8 len; // 4
u8 dtype; // 0x24
u8 subtype;
u8 d0;
uint8_t len; // 4
uint8_t dtype; // 0x24
uint8_t subtype;
uint8_t d0;
} CDCCSInterfaceDescriptor4;

typedef struct
{
u8 len;
u8 dtype; // 0x24
u8 subtype; // 1
u8 bmCapabilities;
u8 bDataInterface;
uint8_t len;
uint8_t dtype; // 0x24
uint8_t subtype; // 1
uint8_t bmCapabilities;
uint8_t bDataInterface;
} CMFunctionalDescriptor;

typedef struct
{
u8 len;
u8 dtype; // 0x24
u8 subtype; // 1
u8 bmCapabilities;
uint8_t len;
uint8_t dtype; // 0x24
uint8_t subtype; // 1
uint8_t bmCapabilities;
} ACMFunctionalDescriptor;

typedef struct
@@ -301,4 +322,49 @@ typedef struct
#define NEW_LUFA_SIGNATURE 0xDCFB
#endif

/* This core is PluggableUSB capable */
#define PLUGGABLE_USB_ENABLED

/* Endpoints number */
#if defined(EPRST6)
#define USB_ENDPOINTS 7 // AtMegaxxU4
#else
#define USB_ENDPOINTS 5 // AtMegaxxU2
#endif

#define EP_TYPE_CONTROL (0x00)
#define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))
#define EP_TYPE_BULK_OUT (1<<EPTYPE1)
#define EP_TYPE_INTERRUPT_IN ((1<<EPTYPE1) | (1<<EPTYPE0) | (1<<EPDIR))
#define EP_TYPE_INTERRUPT_OUT ((1<<EPTYPE1) | (1<<EPTYPE0))
#define EP_TYPE_ISOCHRONOUS_IN ((1<<EPTYPE0) | (1<<EPDIR))
#define EP_TYPE_ISOCHRONOUS_OUT (1<<EPTYPE0)

// This definitions is usefull if you want to reduce the EP_SIZE to 16
// at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint
#ifndef USB_EP_SIZE
#define USB_EP_SIZE 64
#endif

#define ISERIAL_MAX_LEN 20

#define CDC_INTERFACE_COUNT 2
#define CDC_ENPOINT_COUNT 3

#define CDC_ACM_INTERFACE 0 // CDC ACM
#define CDC_DATA_INTERFACE 1 // CDC Data
#define CDC_FIRST_ENDPOINT 1
#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First
#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1)
#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2)

#define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT)

#define CDC_RX CDC_ENDPOINT_OUT
#define CDC_TX CDC_ENDPOINT_IN

#define IMANUFACTURER 1
#define IPRODUCT 2
#define ISERIAL 3

#endif
46 changes: 0 additions & 46 deletions cores/arduino/USBDesc.h

This file was deleted.

89 changes: 0 additions & 89 deletions cores/arduino/Udp.h

This file was deleted.

168 changes: 0 additions & 168 deletions cores/arduino/WCharacter.h

This file was deleted.

19 changes: 17 additions & 2 deletions cores/arduino/WInterrupts.c
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS] = {
#endif
};

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), PinStatus mode) {
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
intFunc[interruptNum] = userFunc;

@@ -76,7 +76,22 @@ void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
// the mode into place.

// Enable the interrupt.


switch (mode) {
case CHANGE:
mode = 1;
break;
case FALLING:
mode = 2;
break;
case RISING:
mode = 3;
break;
default:
// AVR doesn't support level triggered interrupts
return;
}

switch (interruptNum) {
#if defined(__AVR_ATmega32U4__)
// I hate doing this, but the register assignment differs between the 1280/2560
8 changes: 0 additions & 8 deletions cores/arduino/WMath.cpp
Original file line number Diff line number Diff line change
@@ -48,11 +48,3 @@ long random(long howsmall, long howbig)
long diff = howbig - howsmall;
return random(diff) + howsmall;
}

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

unsigned int makeWord(unsigned int w) { return w; }
unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }
750 changes: 0 additions & 750 deletions cores/arduino/WString.cpp

This file was deleted.

229 changes: 0 additions & 229 deletions cores/arduino/WString.h

This file was deleted.

Loading