Skip to content

Commit

Permalink
digitalWrite and digitalRead cancel analogWrite enabled on a pin (esp…
Browse files Browse the repository at this point in the history
  • Loading branch information
me-no-dev authored Jul 26, 2016
1 parent 3fc3e9a commit b7c7bc0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cores/esp8266/core_esp8266_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void ICACHE_RAM_ATTR timer1_isr_handler(void *para){
}
}

void timer1_isr_init(){
void ICACHE_RAM_ATTR timer1_isr_init(){
ETS_FRC_TIMER1_INTR_ATTACH(timer1_isr_handler, NULL);
}

Expand All @@ -66,7 +66,7 @@ void ICACHE_RAM_ATTR timer1_write(uint32_t ticks){
if ((T1C & (1 << TCIT)) == 0) TEIE |= TEIE1;//edge int enable
}

void timer1_disable(){
void ICACHE_RAM_ATTR timer1_disable(){
T1C = 0;
T1I = 0;
}
Expand Down
18 changes: 11 additions & 7 deletions cores/esp8266/core_esp8266_wiring_digital.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
/*
digital.c - wiring digital implementation for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
Expand All @@ -25,9 +25,12 @@
#include "eagle_soc.h"
#include "ets_sys.h"

extern void pwm_stop_pin(uint8_t pin);

uint8_t esp8266_gpioToFn[16] = {0x34, 0x18, 0x38, 0x14, 0x3C, 0x40, 0x1C, 0x20, 0x24, 0x28, 0x2C, 0x30, 0x04, 0x08, 0x0C, 0x10};

extern void __pinMode(uint8_t pin, uint8_t mode) {
pwm_stop_pin(pin);
if(pin < 16){
if(mode == SPECIAL){
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
Expand Down Expand Up @@ -77,6 +80,7 @@ extern void __pinMode(uint8_t pin, uint8_t mode) {
}

extern void ICACHE_RAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
pwm_stop_pin(pin);
if(pin < 16){
if(val) GPOS = (1 << pin);
else GPOC = (1 << pin);
Expand All @@ -87,6 +91,7 @@ extern void ICACHE_RAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
}

extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin) {
pwm_stop_pin(pin);
if(pin < 16){
return GPIP(pin);
} else if(pin == 16){
Expand Down Expand Up @@ -121,12 +126,12 @@ void ICACHE_RAM_ATTR interrupt_handler(void *arg) {
while(!(changedbits & (1 << i))) i++;
changedbits &= ~(1 << i);
interrupt_handler_t *handler = &interrupt_handlers[i];
if (handler->fn &&
(handler->mode == CHANGE ||
if (handler->fn &&
(handler->mode == CHANGE ||
(handler->mode & 1) == !!(levels & (1 << i)))) {
// to make ISR compatible to Arduino AVR model where interrupts are disabled
// we disable them before we call the client ISR
uint32_t savedPS = xt_rsil(15); // stop other interrupts
uint32_t savedPS = xt_rsil(15); // stop other interrupts
handler->fn();
xt_wsr_ps(savedPS);
}
Expand Down Expand Up @@ -170,7 +175,7 @@ void initPins() {
for (int i = 12; i <= 16; ++i) {
pinMode(i, INPUT);
}

ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg);
ETS_GPIO_INTR_ENABLE();
}
Expand All @@ -180,4 +185,3 @@ extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("
extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead")));
extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__ ((weak, alias("__attachInterrupt")));
extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachInterrupt")));

25 changes: 16 additions & 9 deletions cores/esp8266/core_esp8266_wiring_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ void ICACHE_RAM_ATTR pwm_timer_isr() //103-138
TEIE &= ~TEIE1;//14
T1I = 0;//9
if(current_step < table->len) { //20/21
if(table->masks[current_step] & 0xFFFF) {
GPOC = table->masks[current_step] & 0xFFFF; //15/21
uint32_t mask = table->masks[current_step] & pwm_mask;
if(mask & 0xFFFF) {
GPOC = mask & 0xFFFF; //15/21
}
if(table->masks[current_step] & 0x10000) {
if(mask & 0x10000) {
GP16O = 0; //6/13
}
current_step++;//1
Expand Down Expand Up @@ -164,28 +165,34 @@ void pwm_start_timer()
timer1_write(1);
}

extern void __analogWrite(uint8_t pin, int value)
void ICACHE_RAM_ATTR pwm_stop_pin(uint8_t pin)
{
bool start_timer = false;
if(value == 0) {
if(pwm_mask){
pwm_mask &= ~(1 << pin);
prep_pwm_steps();
digitalWrite(pin, LOW);
if(pwm_mask == 0) {
ETS_FRC_TIMER1_NMI_INTR_ATTACH(NULL);
timer1_disable();
timer1_isr_init();
}
}
}

extern void __analogWrite(uint8_t pin, int value)
{
bool start_timer = false;
if(value == 0) {
digitalWrite(pin, LOW);
prep_pwm_steps();
return;
}
if((pwm_mask & (1 << pin)) == 0) {
if(pwm_mask == 0) {
memset(&_pwm_isr_data, 0, sizeof(struct pwm_isr_data*));
start_timer = true;
}
pwm_mask |= (1 << pin);
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
pwm_mask |= (1 << pin);
}
if((F_CPU / ESP8266_CLOCK) == 1) {
value = (value+1) / 2;
Expand Down

0 comments on commit b7c7bc0

Please sign in to comment.