Continuing the discussion from What is the inverse of analogWrite(pin,val)?:
Do you need to protect reads and writes to 16bit timer registers, such as OCR1A, TCNT1, ICR1 ) from interrupts?
The Arduino analog core code at ArduinoCore-avr/wiring_analog.c at master · arduino/ArduinoCore-avr · GitHub writes to 16 bit timer registers like:
...
case TIMER1A:
// connect pwm to pin on timer 1, channel A
sbi(TCCR1A, COM1A1);
OCR1A = val; // set pwm duty
break;
#endif
...
Why do they not protect the 16 bit writes as in http://ww1.microchip.com/downloads/en/Appnotes/doc1493.pdf or ATmega48/V / 88/V / 168/V or this section 15.3 from the Uno's datasheeet at https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf
This is a portion of the avr-objdump -Sj disassembly for a "void setup(){analogWrite(9,127);} void loop(){;}
" sketch:
#if defined(TCCR1A) && defined(COM1A1)
case TIMER1A:
// connect pwm to pin on timer 1, channel A
sbi(TCCR1A, COM1A1);
2de: 80 91 80 00 lds r24, 0x0080 ; 0x800080 <__DATA_REGION_ORIGIN__+0x20>
2e2: 80 68 ori r24, 0x80 ; 128
2e4: 80 93 80 00 sts 0x0080, r24 ; 0x800080 <__DATA_REGION_ORIGIN__+0x20>
/Users/xxx/Library/Arduino15/packages/arduino/hardware/avr/1.8.4/cores/arduino/wiring_analog.c:145
OCR1A = val; // set pwm duty
2e8: 8f e7 ldi r24, 0x7F ; 127
2ea: 90 e0 ldi r25, 0x00 ; 0
2ec: 90 93 89 00 sts 0x0089, r25 ; 0x800089 <__DATA_REGION_ORIGIN__+0x29>
2f0: 80 93 88 00 sts 0x0088, r24 ; 0x800088 <__DATA_REGION_ORIGIN__+0x28>
2f4: e7 cf rjmp .-50 ; 0x2c4 <main+0x150>
/Users/xxx/Library/Arduino15/packages/arduino/hardware/avr/1.8.4/cores/arduino/wiring_analog.c:176
#endif