Skip to content

Commit 54909e3

Browse files
committed
Enable digitalRead on output pins, only enable pullup or pulldown in digitalWrite if pin is not output. arduino#56 arduino#79 arduino#94 arduino#101
1 parent b984e0e commit 54909e3

File tree

6 files changed

+89
-44
lines changed

6 files changed

+89
-44
lines changed

README.md

+39-13
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,25 @@ everything else (ie: W) resides in flash (in most cases).
266266

267267
#### Windows
268268

269-
There are currently four USB composite device combinations that include CDC as well as a CDC only device.
270-
Drivers are required for each of these five devices. The CDC only driver is required by the bootloader.
271-
The drivers are signed and support both 32 and 64 bit versions of Windows XP (SP3), Vista, 7, 8, and 10.
269+
Prior to core version 1.6.6-mt1, sketches compiled with both CDC and HID USB code by default, thus requiring a CDC
270+
driver for the bootloader and a CDC-HID driver for sketches. Now that PluggableUSB is supported, sketches compile
271+
with only CDC code by default. Thus, only one driver is needed. Since HID and MIDI are currently supported (and
272+
MSD potentially in the future), driver installation will be required for each different combination of USB devices.
273+
There are currently four USB composite device combinations that include CDC as well as a CDC only device. Each
274+
supported combination has a unique USB VID:PID pair, and these are listed in the .inf file. Once the first device
275+
is installed (the CDC only device), future installations *might* be automatic, otherwise, you may direct the
276+
installer to the same .inf file. The drivers are signed and support both 32 and 64 bit versions of Windows XP(SP3),
277+
Vista, 7, 8, and 10.
278+
272279

273280
1. If you do not already have the SAM-BA bootloader installed, see below.
274281
2. Download https://www.mattairtech.com/software/MattairTech_CDC_Driver_Signed.zip and unzip into any folder.
275282
3. Plug in the board while holding down button A to enter the bootloader. The LED should light.
276283
4. Windows will detect the board. Point the installer to the folder from above to install the bootloader driver.
277284
5. If you don't intend on using Arduino, you can skip the rest of this list. See Using Bossac Standalone below.
278285
6. If you do not already have the test firmware installed (comes preinstalled), see Using Bossac Standalone below.
279-
7. Press the reset button to run the test firmware (blink sketch with CDC-HID).
280-
8. Windows will detect the board. Point the installer to the folder from above to install the sketch driver.
286+
7. Press the reset button to run the test firmware (blink sketch).
287+
8. Windows will detect the board. Point the installer to the above folder to install the sketch driver (if needed).
281288
9. Continue with SAMD Core Installation below.
282289

283290
#### Linux
@@ -291,14 +298,17 @@ The drivers are signed and support both 32 and 64 bit versions of Windows XP (SP
291298

292299
#### OS X
293300

301+
UNTESTED
294302
1. As of this writing, only the 256 KB chip variants work with the OS X version of the upload tool, bossac.
295303
2. First, you will need to open boards.txt and change mattairtech_mt_d21e_bl8k.upload.tool to equal arduino:bossac.
296304
3. Open platform.txt and change tools.bossac.path to equal{runtime.tools.bossac-1.6.1-arduino.path}.
297-
4. No driver installation is needed. You may get a dialog box asking if you wish to open the “Network Preferences”:
305+
4. No driver installation is needed.
306+
5. Plug in the board. You may get a dialog box asking if you wish to open the “Network Preferences”:
298307
* Click the "Network Preferences..." button, then click "Apply".
299308
* The board will show up as “Not Configured”, but it will work fine.
300309
5. Continue with SAMD Core Installation below.
301310

311+
302312
### SAMD Core Installation
303313

304314
* To update from a previous version, click on MattairTech SAMD Boards in Boards Manager, then click Update.
@@ -319,6 +329,16 @@ The drivers are signed and support both 32 and 64 bit versions of Windows XP (SP
319329
13. You can now upload your own sketch.
320330

321331

332+
### Uploading the First Sketch
333+
334+
1. In the Arduino IDE 1.6.7 (or above), open File->Examples->01.Basics->Blink.
335+
2. Change the three instances of '13' to 'LED_BUILTIN'.
336+
3. Be sure the correct options are selected in the Tools menu (see AVR Core Installation above).
337+
4. With the board plugged in, select the correct port from Tools->Port.
338+
5. Click the Upload button. After compiling, the sketch should be transferred to the board.
339+
6. Once the bootloader exits, the blink sketch should be running.
340+
341+
322342
## SAM-BA USB CDC Bootloader (Arduino compatible)
323343

324344
The SAM-BA bootloader has both a CDC USB interface, and a UART interface (MT-D21E: TX: pin 10, RX: pin 11). It is
@@ -361,6 +381,7 @@ When the Arduino IDE initiates the bootloader, the following procedure is used:
361381
3. The board is reset. The bootloader (which always runs first) detects the blank flah row, so bootloader operation resumes.
362382
4. Opening and closing the port at a baud rate other than 1200bps will not erase or reset the SAMD.
363383

384+
364385
### Bootloader Firmware Installation
365386

366387
#### Bootloader Installation Using the Arduino IDE
@@ -383,6 +404,7 @@ When the Arduino IDE initiates the bootloader, the following procedure is used:
383404
* You can optionally set the EEPROM bits or anything else. The Arduino installation method uses factory defaults.
384405
4. Continue with driver installation above.
385406

407+
386408
### Using Bossac Standalone
387409

388410
When using Bossac standalone, you will need to ensure that your application starts at 0x00002000 for 8 KB bootloaders,
@@ -408,13 +430,13 @@ As an example, bossac will be used to upload the test firmware (blink sketch):
408430
1. Download firmware from https://www.mattairtech.com/software/SAM-BA-bootloader-test-firmware.zip and unzip.
409431
2. If you have not already installed the bootloader driver, see Driver Installation above.
410432
3. Be sure there is a binary that matches your chip. On the command line (change the binary to match yours):
411-
4. On Linux --port might be /dev/ttyACM0. If the device is not found, remove the --port argument for auto-detection.
412433

413434
```
414435
bossac.exe -d --port=COM5 -U true -i -e -w -v Blink_Demo_ATSAMD21E18A.bin -R
415436
```
437+
4. On Linux --port might be /dev/ttyACM0. If the device is not found, remove the --port argument for auto-detection.
416438
5. See http://manpages.ubuntu.com/manpages/vivid/man1/bossac.1.html for details.
417-
6. Continue with the CDC-HID driver installation above (optional).
439+
6. The board should reset automatically and the sketch should be running.
418440

419441

420442

@@ -504,25 +526,29 @@ bossac.exe -d --port=COM5 -U true -i -e -w -v Blink_Demo_ATSAMD21E18A.bin -R
504526

505527
## Possible Future Additions
506528

507-
* USB Host mode CDC ACM
529+
* SAML21 support in the works
530+
* Timer library in the works (like TimerOne, plus input capture and possibly waveform extensions)
531+
* USB Host mode CDC ACM (partially complete; BSD-like license?)
508532
* Features for lower power consumption (library?)
509533
* Enhanced SD card library
510534
* Optional use of single on-board LED as USB activity LED
511535
* Replace pulse with timer capture
512536
* MSC (Mass Storage) USB Device Class
513537
* Polyphonic tone
514538
* Better OS X support
539+
* Wired-AND, Wired-OR for port pins
540+
* High-speed port pin access (IOBUS)
515541
* Libraries for some hardware I plan on using:
516-
TFT LCD
517-
Motor controller
542+
TFT LCD (CFAF128128B-0145T)
543+
Motor controller (LV8711T)
518544
IR decoder
519545
I2S DAC/AMP and I2S MEMS microphone
520546
Battery management IC
521547
XBee/Xbee Pro devices
522548
RS485
523549
Several I2C (Wire) sensor devices:
524-
Accelerometer/gyro/magnetometer
525-
Barometer/altimeter
550+
Accelerometer/magnetometer (LSM303CTR)
551+
Barometer/altimeter (LPS22HBTR)
526552
Humidity/temperature
527553
Light/color sensor
528554

cores/arduino/Uart.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ void Uart::begin(unsigned long baudrate)
3636

3737
void Uart::begin(unsigned long baudrate, uint8_t config)
3838
{
39-
pinMode(uc_pinRX,INPUT_PULLUP);//Rx pin set INPUT_PULLUP mode
4039
pinPeripheral(uc_pinRX, PIO_SERCOM);
4140
pinPeripheral(uc_pinTX, PIO_SERCOM);
4241

cores/arduino/WVariant.h

+6
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ typedef enum _EPioType
161161
PIO_AC_GCLK=12, /* The pin is controlled by the AC_GCLK peripheral (output). */
162162

163163
PIO_MULTI, /* The pin can be configured to any type based on the attributes. */
164+
165+
PIO_STARTUP, /* Used as parameter to pinPeripheral() only to set startup state (enable INEN only) */
164166
} EPioType ;
165167

166168
/**
@@ -228,6 +230,10 @@ typedef enum _EPioPeripheral
228230
#define PER_ATTR_OUTPUT_TYPE_BUSKEEPER (3UL<<3)
229231
#define PER_ATTR_OUTPUT_TYPE_MASK (3UL<<3)
230232

233+
#define PER_ATTR_INPUT_SYNCHRONIZER_ON_DEMAND (0UL<<5)
234+
#define PER_ATTR_INPUT_SYNCHRONIZER_ALWAYS_ON (1UL<<5)
235+
#define PER_ATTR_INPUT_SYNCHRONIZER_MASK (1UL<<5)
236+
231237

232238
/* Types used for the table below
233239
* This struct MUST be 12 bytes long (elements are ordered to prevent unaligned access).

cores/arduino/wiring.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ void init( void )
9696
// Clock ADC/DAC for Analog
9797
PM->APBCMASK.reg |= PM_APBCMASK_ADC | PM_APBCMASK_DAC ;
9898

99-
// Setup all pins (digital and analog) in INPUT mode (default is nothing)
100-
// for ( ul = 0 ; ul < NUM_DIGITAL_PINS ; ul++ )
101-
// {
102-
// pinMode( ul, INPUT ) ;
103-
// }
99+
//Setup all pins (digital and analog) in STARTUP mode (enable INEN only)
100+
for ( ul = 0 ; ul < NUM_DIGITAL_PINS ; ul++ )
101+
{
102+
pinMode( ul, PIO_STARTUP ) ;
103+
}
104104

105105
// Initialize Analog Controller
106106
// Setting clock

cores/arduino/wiring_digital.c

+28-19
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,38 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal )
3939
uint32_t pinAttribute = g_APinDescription[ulPin].ulPinAttribute;
4040
uint8_t pinPort = g_APinDescription[ulPin].ulPort;
4141
uint8_t pinNum = g_APinDescription[ulPin].ulPin;
42-
uint8_t pullConfig = PORT->Group[pinPort].PINCFG[pinNum].reg;
43-
44-
// Enable pull resistor if pin attributes allow
45-
if ( (ulVal == HIGH && (pinAttribute & PIN_ATTR_INPUT_PULLUP)) || (ulVal == LOW && (pinAttribute & PIN_ATTR_INPUT_PULLDOWN)) )
46-
{
47-
pullConfig |= (uint8_t)(PORT_PINCFG_PULLEN) ;
42+
uint8_t pinConfig = PORT->Group[pinPort].PINCFG[pinNum].reg;
43+
uint8_t pinDir = PORT->Group[pinPort].DIR[pinNum].reg;
44+
uint8_t pinOut = PORT->Group[pinPort].OUT[pinNum].reg;
45+
46+
// Enable pull resistor if pin attributes allow and only if pin is not configured as output
47+
// Note that most pins should use PIN_ATTR_DIGITAL, which includes both PIN_ATTR_INPUT_PULLUP and PIN_ATTR_INPUT_PULLDOWN.
48+
if ( pinDir == 0 ) { // pin DIR is input
49+
if ( ulVal == HIGH )
50+
{
51+
if ( (pinOut == 1 && (pinAttribute & PIN_ATTR_INPUT_PULLUP)) || (pinOut == 0 && (pinAttribute & PIN_ATTR_INPUT_PULLDOWN)) )
52+
{
53+
pinConfig |= (uint8_t)(PORT_PINCFG_PULLEN) ;
54+
}
55+
}
56+
else
57+
{
58+
pinConfig &= ~(uint8_t)(PORT_PINCFG_PULLEN) ;
59+
}
60+
61+
PORT->Group[pinPort].PINCFG[pinNum].reg = pinConfig ;
4862
}
63+
// Set or clear OUT register only when pin DIR is set to output.
64+
// Pull direction (pullup or pulldown) is now set with pinMode only.
4965
else
5066
{
51-
pullConfig &= ~(uint8_t)(PORT_PINCFG_PULLEN) ;
52-
}
53-
54-
PORT->Group[pinPort].PINCFG[pinNum].reg = pullConfig ;
55-
56-
switch ( ulVal )
57-
{
58-
case LOW:
59-
PORT->Group[pinPort].OUTCLR.reg = (1ul << pinNum) ;
60-
break ;
61-
62-
default:
67+
if ( ulVal == HIGH ) {
6368
PORT->Group[pinPort].OUTSET.reg = (1ul << pinNum) ;
64-
break ;
69+
}
70+
else
71+
{
72+
PORT->Group[pinPort].OUTCLR.reg = (1ul << pinNum) ;
73+
}
6574
}
6675

6776
return ;

cores/arduino/wiring_private.c

+11-6
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ int pinPeripheral( uint32_t ulPin, uint32_t ulPeripheral )
3737
return -1 ;
3838
}
3939

40-
// If pinType is not PIO_MULTI in the pinDescription table, then it must match ulPeripheral
41-
if ( pinType != PIO_MULTI && pinType != ulPeripheral )
40+
// If pinType is not PIO_MULTI or PIO_STARTUP in the pinDescription table, then it must match ulPeripheral
41+
if ( pinType != PIO_MULTI && pinType != PIO_STARTUP && pinType != ulPeripheral )
4242
{
4343
return -1 ;
4444
}
4545

4646
// Make sure ulPeripheral is listed in the attributes
47-
if ( !(pinAttribute & (1UL << ulPeripheral)) )
47+
if ( !(pinAttribute & (1UL << ulPeripheral)) && pinType != PIO_STARTUP )
4848
{
4949
return -1 ;
5050
}
@@ -123,10 +123,14 @@ int pinPeripheral( uint32_t ulPin, uint32_t ulPeripheral )
123123

124124
uint8_t pinPort = g_APinDescription[ulPin].ulPort;
125125
uint8_t pinNum = g_APinDescription[ulPin].ulPin;
126-
uint8_t pinCfg = PORT_PINCFG_INEN;
126+
uint8_t pinCfg = PORT_PINCFG_INEN; // INEN should be enabled for both input and output (but not analog)
127127

128128
switch ( ulPeripheral )
129129
{
130+
case PIO_STARTUP:
131+
PORT->Group[pinPort].PINCFG[pinNum].reg=(uint8_t)pinCfg ; // Just enable INEN
132+
break;
133+
130134
// Set pin mode according to chapter '22.6.3 I/O Pin Configuration'
131135
case PIO_INPUT:
132136
case PIO_INPUT_PULLUP:
@@ -151,7 +155,6 @@ int pinPeripheral( uint32_t ulPin, uint32_t ulPeripheral )
151155
break ;
152156

153157
case PIO_OUTPUT:
154-
pinCfg = 0;
155158
if ( (peripheralAttribute & PER_ATTR_DRIVE_MASK) == PER_ATTR_DRIVE_STRONG )
156159
{
157160
pinCfg |= PORT_PINCFG_DRVSTR;
@@ -162,10 +165,12 @@ int pinPeripheral( uint32_t ulPin, uint32_t ulPeripheral )
162165
break ;
163166

164167

165-
case PIO_EXTINT:
166168
case PIO_ANALOG_ADC:
167169
case PIO_ANALOG_DAC:
168170
case PIO_ANALOG_REF:
171+
pinCfg = 0; // Disable INEN with analog
172+
173+
case PIO_EXTINT:
169174
case PIO_TIMER_PWM:
170175
case PIO_TIMER_CAPTURE:
171176
case PIO_SERCOM:

0 commit comments

Comments
 (0)