Skip to content

Commit 4986a66

Browse files
committed
Adapt to SAMD core
1 parent 47e23cc commit 4986a66

File tree

7 files changed

+86
-53
lines changed

7 files changed

+86
-53
lines changed

Diff for: api/ArduinoAPI.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
/* Standard C library includes */
4545
#include <stdlib.h>
46+
#include <stdint.h>
4647
#include <stdbool.h>
4748
#include <string.h>
4849
#include <math.h>

Diff for: api/Common.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "Common.h"
2+
3+
/* C++ prototypes */
4+
long map(long x, long in_min, long in_max, long out_min, long out_max)
5+
{
6+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
7+
}
8+
9+
uint16_t makeWord(uint16_t w) { return w; }
10+
uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8) | l; }

Diff for: api/Common.h

+50-37
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
1+
#include <stdint.h>
2+
13
#ifdef __cplusplus
24
extern "C"{
35
#endif
46

57
void yield(void);
68

79
typedef enum {
8-
LOW = 0x0,
9-
HIGH = 0x1,
10-
} _pinStatus;
10+
LOW = 0x0,
11+
HIGH = 0x1,
12+
} PinStatus;
1113

1214
typedef enum {
13-
INPUT = 0x0,
14-
OUTPUT = 0x1,
15-
INPUT_PULLUP = 0x2,
16-
INPUT_PULLDOWN = 0x3,
17-
} _pinMode;
15+
IRQ_LOW = 0,
16+
IRQ_HIGH = 1,
17+
CHANGE = 2,
18+
FALLING = 3,
19+
RISING = 4,
20+
} InterruptMode;
1821

19-
#define PI 3.1415926535897932384626433832795
20-
#define HALF_PI 1.5707963267948966192313216916398
21-
#define TWO_PI 6.283185307179586476925286766559
22-
#define DEG_TO_RAD 0.017453292519943295769236907684886
23-
#define RAD_TO_DEG 57.295779513082320876798154814105
24-
#define EULER 2.718281828459045235360287471352
22+
typedef enum {
23+
INPUT = 0x0,
24+
OUTPUT = 0x1,
25+
INPUT_PULLUP = 0x2,
26+
INPUT_PULLDOWN = 0x3,
27+
} PinMode;
2528

26-
#define SERIAL 0x0
27-
#define DISPLAY 0x1
29+
#define PI 3.1415926535897932384626433832795
30+
#define HALF_PI 1.5707963267948966192313216916398
31+
#define TWO_PI 6.283185307179586476925286766559
32+
#define DEG_TO_RAD 0.017453292519943295769236907684886
33+
#define RAD_TO_DEG 57.295779513082320876798154814105
34+
#define EULER 2.718281828459045235360287471352
2835

29-
typedef enum {
30-
CHANGE = 1,
31-
FALLING = 2,
32-
RISING = 3,
33-
} _interruptMode;
36+
#define SERIAL 0x0
37+
#define DISPLAY 0x1
3438

3539
typedef enum {
3640
LSBFIRST = 0,
3741
MSBFIRST = 1,
38-
} _spi_bitFirst_mode;
42+
} BitMode;
3943

4044
#ifndef min
4145
#define min(a,b) \
@@ -67,6 +71,8 @@ typedef enum {
6771
#define sq(x) ((x)*(x))
6872
#endif
6973

74+
typedef void (*voidFuncPtr)(void);
75+
7076
// interrupts() / noInterrupts() must be defined by the core
7177

7278
#define lowByte(w) ((uint8_t) ((w) & 0xff))
@@ -77,40 +83,47 @@ typedef enum {
7783
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
7884
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
7985

80-
typedef unsigned int word;
81-
8286
#ifndef bit
8387
#define bit(b) (1UL << (b))
8488
#endif
8589

86-
typedef bool boolean;
87-
typedef uint8_t byte;
90+
/* TODO: request for removal */
91+
typedef bool boolean;
92+
typedef uint8_t byte;
93+
typedef uint16_t word;
8894

8995
void init(void);
9096
void initVariant(void);
9197

9298
int atexit(void (*func)()) __attribute__((weak));
9399
int main() __attribute__((weak));
94100

95-
void pinMode(uint8_t pinNumber, _pinMode pinMode);
96-
void digitalWrite(uint8_t pinNumber, _pinStatus status);
97-
_pinStatus digitalRead(uint8_t pinNumber);
98-
int analogRead(uint8_t pinNumber);
101+
#ifdef EXTENDED_PIN_MODE
102+
// Platforms who wnat to declare more than 256 pins need to define EXTENDED_PIN_MODE globally
103+
typedef uint32_t pin_size_t;
104+
#else
105+
typedef uint8_t pin_size_t;
106+
#endif
107+
108+
void pinMode(pin_size_t pinNumber, PinMode pinMode);
109+
void digitalWrite(pin_size_t pinNumber, PinStatus status);
110+
PinStatus digitalRead(pin_size_t pinNumber);
111+
int analogRead(pin_size_t pinNumber);
99112
void analogReference(uint8_t mode);
100-
void analogWrite(uint8_t pinNumber, int value);
113+
void analogWrite(pin_size_t pinNumber, int value);
101114

102115
unsigned long millis(void);
103116
unsigned long micros(void);
104117
void delay(unsigned long);
105118
void delayMicroseconds(unsigned int us);
106-
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
107-
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
119+
unsigned long pulseIn(pin_size_t pin, uint8_t state, unsigned long timeout);
120+
unsigned long pulseInLong(pin_size_t pin, uint8_t state, unsigned long timeout);
108121

109-
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
110-
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
122+
void shiftOut(pin_size_t dataPin, pin_size_t clockPin, uint8_t bitOrder, uint8_t val);
123+
pin_size_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, uint8_t bitOrder);
111124

112-
void attachInterrupt(uint8_t interruptNumber, void (*callback)(void), _interruptMode mode);
113-
void detachInterrupt(uint8_t interruptNumber);
125+
void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, InterruptMode mode);
126+
void detachInterrupt(pin_size_t interruptNumber);
114127

115128
void setup(void);
116129
void loop(void);

Diff for: api/RingBuffer.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <stdio.h>
2222
#include <stdlib.h>
2323

24-
RingBuffer::RingBuffer(rb_index_type size = 64) : size(size)
24+
RingBuffer::RingBuffer(rb_index_type size) : size(size)
2525
{
2626
_aucBuffer = (uint8_t*)malloc(size);
2727
memset( _aucBuffer, 0, size ) ;
@@ -79,6 +79,16 @@ int RingBuffer::available()
7979
return delta;
8080
}
8181

82+
int RingBuffer::availableForStore()
83+
{
84+
int delta = _iHead - _iTail;
85+
if (delta >= 0)
86+
return size + additionalSize - 1 - delta;
87+
else
88+
return -delta - 1;
89+
}
90+
91+
8292
int RingBuffer::peek()
8393
{
8494
if(_iTail == _iHead)

Diff for: api/RingBuffer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ typedef unsigned int rb_index_type;
3737
class RingBuffer
3838
{
3939
public:
40-
RingBuffer( rb_index_type size ) ;
40+
RingBuffer( rb_index_type size = 64 ) ;
4141
void store_char( uint8_t c ) ;
4242
void clear();
4343
int read_char();
4444
int available();
45+
int availableForStore();
4546
int peek();
4647
bool isFull();
4748
void addStorage(uint8_t* _buffer, rb_index_type _size) {

Diff for: api/String.h

+4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
#include <stdlib.h>
2727
#include <string.h>
2828
#include <ctype.h>
29+
#if defined(__AVR__)
2930
#include "avr/pgmspace.h"
31+
#else
32+
#include "deprecated-avr-comp/avr/pgmspace.h"
33+
#endif
3034

3135
// When compiling programs with this class, the following gcc parameters
3236
// dramatically increase performance and memory (RAM) efficiency, typically

Diff for: api/USBAPI.h

+8-14
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,20 @@
2222

2323
#include <stdint.h>
2424

25-
class USBDevice_
26-
{
27-
public:
28-
USBDevice_();
29-
bool configured();
30-
31-
void attach();
32-
void detach(); // Serial port goes down too...
33-
void poll();
34-
bool wakeupHost(); // returns false, when wakeup cannot be processed
35-
};
36-
extern USBDevice_ USBDevice;
37-
3825
//================================================================================
3926
//================================================================================
4027
// Low level API
4128

4229
typedef struct __attribute__((packed))
4330
{
44-
uint8_t bmRequestType;
31+
union {
32+
uint8_t bmRequestType;
33+
struct {
34+
uint8_t direction : 5;
35+
uint8_t type : 2;
36+
uint8_t transferDirection : 1;
37+
};
38+
};
4539
uint8_t bRequest;
4640
uint8_t wValueL;
4741
uint8_t wValueH;

0 commit comments

Comments
 (0)