@@ -43,28 +43,26 @@ BSD license, all text above must be included in any redistribution.
4343// be specified as any pin within a specific PORT register stated below.
4444
4545#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
46- // Arduino Mega hasn't actually been tested -- use at your own peril!
46+ // Arduino Mega is now tested and confirmed, with the following caveats:
4747 // Because digital pins 2-7 don't map to a contiguous port register,
48- // the Mega will require connecting the matrix data lines to different
49- // pins. Ports A, C, and L all offer the requisite contiguous 6 bits.
50- // I wanted to use PORTL in order to keep the external memory interface
51- // free, but accessing the upper PORT registers in assembly seems to
52- // require some additional flaming hoops and doesn't work with the
53- // inline code here. PORTA is used instead (Mega pins 22-29, though
54- // only 24-29 are actually connected to the LED matrix). Clock may be
55- // any pin on PORTB -- on the Mega, this CAN'T be pins 8 or 9 (these
56- // are on PORTH), thus the wiring will need to be slightly different
57- // than the tutorial's explanation on the Uno, etc. Pins 10-13 are all
58- // fair game for the clock, as are pins 50-53.
48+ // the Mega requires connecting the matrix data lines to different pins.
49+ // Digital pins 24-29 are used for the data interface, and 22 & 23 are
50+ // unavailable for other outputs because the software needs to write to
51+ // the full PORTA register for speed. Clock may be any pin on PORTB --
52+ // on the Mega, this CAN'T be pins 8 or 9 (these are on PORTH), thus the
53+ // wiring will need to be slightly different than the tutorial's
54+ // explanation on the Uno, etc. Pins 10-13 are all fair game for the
55+ // clock, as are pins 50-53.
5956 #define DATAPORT PORTA
6057 #define DATADIR DDRA
6158 #define SCLKPORT PORTB
6259#elif defined(__AVR_ATmega32U4__)
63- // Arduino Leonardo is still a work in progress -- DO NOT USE!!!
64- // Unlike the Uno, digital pins 2-7 do NOT map to a contiguous port
65- // register, dashing our hopes for compatible wiring. This is probably
66- // going to require changes both to bit-shifting code in the library,
67- // and how this board is wired to the LED matrix. Bummer.
60+ // Arduino Leonardo: this is vestigial code an unlikely to ever be
61+ // finished -- DO NOT USE!!! Unlike the Uno, digital pins 2-7 do NOT
62+ // map to a contiguous port register, dashing our hopes for compatible
63+ // wiring. Making this work would require significant changes both to
64+ // the bit-shifting code in the library, and how this board is wired to
65+ // the LED matrix. Bummer.
6866 #define DATAPORT PORTD
6967 #define DATADIR DDRD
7068 #define SCLKPORT PORTB
@@ -75,6 +73,8 @@ BSD license, all text above must be included in any redistribution.
7573 #define SCLKPORT PORTB
7674#endif
7775
76+ #define nPlanes 4
77+
7878// The fact that the display driver interrupt stuff is tied to the
7979// singular Timer1 doesn't really take well to object orientation with
8080// multiple RGBmatrixPanel instances. The solution at present is to
@@ -120,7 +120,6 @@ void RGBmatrixPanel::init(uint8_t rows, uint8_t a, uint8_t b, uint8_t c,
120120 addrbpin = digitalPinToBitMask (b);
121121 addrcport = portOutputRegister (digitalPinToPort (c));
122122 addrcpin = digitalPinToBitMask (c);
123- nPlanes = 4 ; // Other code is fixed at 4 planes; don't change this
124123 plane = nPlanes - 1 ;
125124 row = nRows - 1 ;
126125 swapflag = false ;
@@ -527,22 +526,24 @@ void RGBmatrixPanel::updateDisplay(void) {
527526 // A tiny bit of inline assembly is used; compiler doesn't pick
528527 // up on opportunity for post-increment addressing mode.
529528 // 5 instruction ticks per 'pew' = 160 ticks total
530- #define pew \
531- asm volatile (" ld __tmp_reg__,%a0+" :: " e" (ptr)); \
532- asm volatile (" out %0,__tmp_reg__" :: " I" (_SFR_IO_ADDR (DATAPORT))); \
533- asm volatile (" out %0,%1" :: " I" (_SFR_IO_ADDR (SCLKPORT))," r" (tick)); \
534- asm volatile (" out %0,%1" :: " I" (_SFR_IO_ADDR (SCLKPORT))," r" (tock));
529+ #define pew asm volatile ( \
530+ " ld __tmp_reg__, %a[ptr]+" " \n\t " \
531+ " out %[data] , __tmp_reg__" " \n\t " \
532+ " out %[clk] , %[tick]" " \n\t " \
533+ " out %[clk] , %[tock]" " \n " \
534+ :: [ptr] " e" (ptr), \
535+ [data] "I" (_SFR_IO_ADDR(DATAPORT)), \
536+ [clk] "I" (_SFR_IO_ADDR(SCLKPORT)), \
537+ [tick] "r" (tick), \
538+ [tock] "r" (tock));
535539
536540 // Loop is unrolled for speed:
537541 pew pew pew pew pew pew pew pew
538542 pew pew pew pew pew pew pew pew
539543 pew pew pew pew pew pew pew pew
540544 pew pew pew pew pew pew pew pew
541545
542- // From the "Unsolved Mysteries" department: "buffptr += 32" doesn't
543- // work here, scrambles the display. "buffptr = ptr" does, even though
544- // both should produce the same results. Couldn't tell you why.
545- buffptr = ptr;
546+ buffptr += 32 ;
546547
547548 } else { // 920 ticks from TCNT1=0 (above) to end of function
548549
0 commit comments