Skip to content

ATtiny827 digitalRead()/digitalWrite() Issue on Specific Pins - Loop Hangs #1190

@bmjjr

Description

@bmjjr

Description of the Problem:

I am experiencing an issue with the digitalRead() and digitalWrite() functions on an ATtiny827 microcontroller using the megaTinyCore. When these functions are used in combination within the loop() function to control an LED (on PIN_PA7) based on the state of a digital input pin (PIN_PC5), the loop() function appears to hang or stop executing after the setup() phase. One point to note, I have a hardware reset button on PIN_PB4 and chose the selection for remapping the button to PIN_PB4, using the option provided by megatinycore.

The LED will reflect the initial state of the input pin during setup() (e.g., if the input is HIGH during reset, the LED turns on), but it does not update afterward in the loop() function, even though the input pin's state changes. A heartbeat LED, used to indicate whether loop() is running, stops toggling, confirming that loop() is not executing.

However, if I use direct register manipulation to read the input pin and control the LED, the code works perfectly, and the LED updates as expected in response to changes on the input pin.

Minimal Code that Demonstrates the Issue (using digitalRead()/digitalWrite()):

#include <avr/io.h>
#include <util/delay.h>
#include "megaTinyCore.h"
#include "Arduino.h"

const byte vccPin = PIN_PC5;      // Charger detect input
const byte battLed50 = PIN_PA7;   // LED

void setup() {
  pinConfigure(vccPin, PIN_DIR_INPUT, PIN_PULLUP_OFF, PIN_INVERT_OFF, PIN_ISC_DISABLE);
  pinConfigure(battLed50, PIN_DIR_OUTPUT, PIN_PULLUP_OFF, PIN_INVERT_OFF, PIN_ISC_DISABLE);
  digitalWrite(battLed50, LOW); // LED off initially
}

void loop() {
  digitalWrite(battLed50, digitalRead(vccPin));
  _delay_ms(50); 
}

Code that Works (using Direct Register Manipulation):

#include <avr/io.h>
#include <util/delay.h>
#include "megaTinyCore.h"
#include "Arduino.h"

const byte vccPin = PIN_PC5;      // Charger detect input
const byte battLed50 = PIN_PA7;   // LED

void setup() {
  // Configure vccPin as input (no pull-up)
  PORTC.DIRCLR = (1 << 5); // Set PC5 as input
  PORTC.PIN5CTRL &= ~PORT_PULLUPEN_bm; // Disable pull-up

  // Configure battLed50 as output
  PORTA.DIRSET = (1 << 7); // Set PA7 as output
  PORTA.OUTCLR = (1 << 7); // Initially turn LED off
}

void loop() {
  // Check the state of vccPin (PC5)
  if (PORTC.IN & (1 << 5)) { 
    // vccPin is HIGH 
    PORTA.OUTSET = (1 << 7); // Turn LED on
  } else {
    // vccPin is LOW 
    PORTA.OUTCLR = (1 << 7); // Turn LED off
  }
  _delay_ms(50); 
}

Hardware Setup Details:

  • Microcontroller: ATtiny827
  • Clock Speed: 5 MHz (internal oscillator)
  • Startup Time: 8 ms
  • BOD Voltage Level: 1.8V
  • BOD Mode: Disabled/Disabled
  • Save EEPROM: EEPROM Retained
  • UPDI with Reset on: PB4
  • WDT Timeout: Disabled
  • PWM Pins: Default
  • Attach Interrupt Mode: On all pins, new implementation
  • megaTinyCore Version: 2.6.10
  • IDE: Arduino IDE 1.8.13
  • Programmer: Atmel-ICE
  • Operating System: Windows 10

Circuit Description:

The input pin (PIN_PC5) is connected to a voltage divider consisting of two 10K resistors. The voltage divider is connected to the 5V output of a USB-C connector (used for charger detection). The output of the divider is approximately 2.53V when the charger is connected and 0V when disconnected. The LED (PIN_PA7) is connected to the ATtiny827 with an appropriate current-limiting resistor.

Steps Taken:

  • Verified that the loop() function is not running by using a heartbeat LED.
  • Confirmed that the issue is not related to the watchdog timer or interrupt conflicts.
  • Isolated the problem to the use of digitalRead() and digitalWrite() in combination.
  • Developed a workaround using direct register manipulation.

Request:

I would appreciate it if you could investigate this issue. It appears to be a potential bug in the megaTinyCore when using digitalRead() and digitalWrite() with these specific pins on the ATtiny827, or there might be a specific configuration or interaction that I'm not aware of.

Please let me know if you need any further information or if you have any suggestions for further testing.

Thank you for your time and effort in developing and maintaining the megaTinyCore.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions