17 const PROGMEM uint8_t Arduboy2Core::lcdBootProgram[] = {
83 #ifdef ARDUBOY_SET_CPU_8MHZ
89 ADMUX = RAND_SEED_IN_ADMUX;
97 #ifdef ARDUBOY_SET_CPU_8MHZ
102 void Arduboy2Core::setCPUSpeed8MHz()
104 uint8_t oldSREG = SREG;
106 PLLCSR = _BV(PINDIV);
109 PLLCSR = _BV(PLLE) | _BV(PINDIV);
116 void Arduboy2Core::bootPins()
121 PORTB |= _BV(RED_LED_BIT) | _BV(GREEN_LED_BIT) | _BV(BLUE_LED_BIT) |
125 DDRB &= ~(_BV(B_BUTTON_BIT) | _BV(SPI_MISO_BIT));
127 DDRB |= _BV(RED_LED_BIT) | _BV(GREEN_LED_BIT) | _BV(BLUE_LED_BIT) |
128 _BV(SPI_MOSI_BIT) | _BV(SPI_SCK_BIT) | _BV(SPI_SS_BIT);
134 PORTD |= _BV(CS_BIT);
136 PORTD &= ~(_BV(RST_BIT));
139 DDRD |= _BV(RST_BIT) | _BV(CS_BIT) | _BV(DC_BIT);
142 PORTE |= _BV(A_BUTTON_BIT);
145 DDRE &= ~(_BV(A_BUTTON_BIT));
149 PORTF |= _BV(LEFT_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) |
150 _BV(UP_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT);
152 PORTF &= ~(_BV(RAND_SEED_IN_BIT));
154 DDRF &= ~(_BV(LEFT_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) |
155 _BV(UP_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT) |
156 _BV(RAND_SEED_IN_BIT));
159 #elif defined(AB_DEVKIT)
162 PORTB |= _BV(LEFT_BUTTON_BIT) | _BV(UP_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT) |
166 DDRB &= ~(_BV(LEFT_BUTTON_BIT) | _BV(UP_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT) |
169 DDRB |= _BV(SPI_MOSI_BIT) | _BV(SPI_SCK_BIT) | _BV(SPI_SS_BIT) |
173 PORTC |= _BV(RIGHT_BUTTON_BIT);
176 DDRC &= ~(_BV(RIGHT_BUTTON_BIT));
180 PORTD |= _BV(CS_BIT);
182 PORTD &= ~(_BV(RST_BIT));
185 DDRD |= _BV(RST_BIT) | _BV(CS_BIT) | _BV(DC_BIT);
190 PORTF |= _BV(A_BUTTON_BIT) | _BV(B_BUTTON_BIT);
192 PORTF &= ~(_BV(RAND_SEED_IN_BIT));
194 DDRF &= ~(_BV(A_BUTTON_BIT) | _BV(B_BUTTON_BIT) | _BV(RAND_SEED_IN_BIT));
201 void Arduboy2Core::bootOLED()
205 bitSet(RST_PORT, RST_BIT);
209 bitClear(CS_PORT, CS_BIT);
214 for (uint8_t i = 0; i <
sizeof(lcdBootProgram); i++) {
222 bitSet(DC_PORT, DC_BIT);
227 bitClear(DC_PORT, DC_BIT);
231 void Arduboy2Core::bootSPI()
234 SPCR = _BV(SPE) | _BV(MSTR);
249 while (!(SPSR & _BV(SPIF))) { }
265 #ifndef ARDUBOY_CORE // for Arduboy core timer 0 should remain enabled
268 power_timer0_disable();
285 void Arduboy2Core::bootPowerSaving()
289 PRR0 = _BV(PRTWI) | _BV(PRADC);
291 PRR1 |= _BV(PRUSART1);
302 bitClear(RST_PORT, RST_BIT);
338 " ldi %A[count], %[len_lsb] \n\t"
339 " ldi %B[count], %[len_msb] \n\t"
340 "1: ld __tmp_reg__, %a[ptr] ;2 \n\t"
341 " out %[spdr], __tmp_reg__ ;1 \n\t"
342 " cpse %[clear], __zero_reg__ ;1/2 \n\t"
343 " mov __tmp_reg__, __zero_reg__ ;1 \n\t"
344 "2: sbiw %A[count], 1 ;2 \n\t"
345 " sbrc %A[count], 0 ;1/2 \n\t"
347 " st %a[ptr]+, __tmp_reg__ ;2 \n\t"
348 " brne 1b ;1/2 :18 \n\t"
349 " in __tmp_reg__, %[spsr] \n\t"
350 : [ptr]
"+&e" (image),
351 [count]
"=&w" (count)
352 : [spdr]
"I" (_SFR_IO_ADDR(SPDR)),
353 [spsr]
"I" (_SFR_IO_ADDR(SPSR)),
390 while (!(SPSR & _BV(SPIF))) { }
396 while (!(SPSR & _BV(SPIF))) { }
417 sendLCDCommand(inverse ? OLED_PIXELS_INVERTED : OLED_PIXELS_NORMAL);
430 sendLCDCommand(flipped ? OLED_VERTICAL_FLIPPED : OLED_VERTICAL_NORMAL);
443 #ifdef ARDUBOY_10 // RGB, all the pretty colors
447 TCCR0A = _BV(COM0A1) | _BV(WGM01) | _BV(WGM00);
452 TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(COM1B1) | _BV(COM1B0) | _BV(WGM10);
455 #elif defined(AB_DEVKIT)
459 bitWrite(BLUE_LED_PORT, BLUE_LED_BIT, blue ?
RGB_ON :
RGB_OFF);
478 #elif defined(AB_DEVKIT)
482 bitWrite(BLUE_LED_PORT, BLUE_LED_BIT, val ?
RGB_ON :
RGB_OFF);
491 TCCR0A = _BV(WGM01) | _BV(WGM00);
499 bitWrite(RED_LED_PORT, RED_LED_BIT, red);
500 bitWrite(GREEN_LED_PORT, GREEN_LED_BIT, green);
501 bitWrite(BLUE_LED_PORT, BLUE_LED_BIT, blue);
502 #elif defined(AB_DEVKIT)
506 bitWrite(BLUE_LED_PORT, BLUE_LED_BIT, blue);
515 bitWrite(RED_LED_PORT, RED_LED_BIT, val);
519 bitWrite(GREEN_LED_PORT, GREEN_LED_BIT, val);
523 bitWrite(BLUE_LED_PORT, BLUE_LED_BIT, val);
525 #elif defined(AB_DEVKIT)
529 bitWrite(BLUE_LED_PORT, BLUE_LED_BIT, val);
543 (_BV(UP_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) |
544 _BV(LEFT_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT)));
546 if (bitRead(A_BUTTON_PORTIN, A_BUTTON_BIT) == 0) { buttons |=
A_BUTTON; }
548 if (bitRead(B_BUTTON_PORTIN, B_BUTTON_BIT) == 0) { buttons |=
B_BUTTON; }
549 #elif defined(AB_DEVKIT)
552 (_BV(DOWN_BUTTON_BIT) | _BV(LEFT_BUTTON_BIT) | _BV(UP_BUTTON_BIT)));
554 if (bitRead(RIGHT_BUTTON_PORTIN, RIGHT_BUTTON_BIT) == 0) { buttons |=
RIGHT_BUTTON; }
556 if (bitRead(A_BUTTON_PORTIN, A_BUTTON_BIT) == 0) { buttons |=
A_BUTTON; }
558 if (bitRead(B_BUTTON_PORTIN, B_BUTTON_BIT) == 0) { buttons |=
B_BUTTON; }
572 while (bit_is_set(ADCSRA, ADSC)) { }
574 seed = ((
unsigned long)ADC << 16) + micros();
584 delay((
unsigned long) ms);
593 *(uint8_t *)MAGIC_KEY_POS = lowByte(MAGIC_KEY);
594 *(uint8_t *)(MAGIC_KEY_POS + 1) = highByte(MAGIC_KEY);
597 WDTCSR = (_BV(WDCE) | _BV(WDE));
607 void Arduboy2NoUSB::mainNoUSB()
613 USBCON = _BV(FRZCLK);
625 bitSet(DOWN_BUTTON_PORT, DOWN_BUTTON_BIT);
626 bitClear(DOWN_BUTTON_DDR, DOWN_BUTTON_BIT);
632 if (bitRead(DOWN_BUTTON_PORTIN, DOWN_BUTTON_BIT) == 0) {