Skip to content
Permalink
Browse files
Move util timer from its own task into espruino task
The first idea was to move util timer task to 2nd cpu later, but it turned out taskhandling slows down a lot
Therefore it had to be moved
Move init of timer into main loop
Add a bitfield for usage of pins for soft pwm
Add handling of soft pwm pins to jshPinSetState
Add handling of soft PWM to jshPinAnalogOutput if soft:true is set in options
Set time for util timer to a min of 30 usecs
  • Loading branch information
jumjum123 committed Aug 9, 2017
1 parent 5efd80b commit 1b3f15a1951f5bc3fcb59cfa768abdb8840abb36
Showing with 33 additions and 17 deletions.
  1. +27 −1 targets/esp32/jshardware.c
  2. +2 −12 targets/esp32/main.c
  3. +4 −4 targets/esp32/rtosutil.c
@@ -73,6 +73,9 @@ static IOEventFlags pinToEV_EXTI(

static uint8_t g_pinState[JSH_PIN_COUNT];

// Whether a pin is being used for soft PWM or not
BITFIELD_DECL(jshPinSoftPWM, JSH_PIN_COUNT);

/**
* interrupt handler for gpio interrupts
*/
@@ -124,6 +127,7 @@ void jshPinDefaultPullup() {
void jshInit() {
esp32_wifi_init();
jshInitDevices();
BITFIELD_CLEAR(jshPinSoftPWM);
if (JSHPINSTATE_I2C != 13 || JSHPINSTATE_GPIO_IN_PULLDOWN != 6 || JSHPINSTATE_MASK != 15) {
jsError("JshPinState #defines have changed, please update pinStateToString()");
}
@@ -229,6 +233,12 @@ void jshPinSetState(
Pin pin, //!< The pin to have its state changed.
JshPinState state //!< The new desired state of the pin.
) {
/* Make sure we kill software PWM if we set the pin state
* after we've started it */
if (BITFIELD_GET(jshPinSoftPWM, pin)) {
BITFIELD_SET(jshPinSoftPWM, pin, 0);
jstPinPWM(0,0,pin);
}
gpio_mode_t mode;
gpio_pull_mode_t pull_mode=GPIO_FLOATING;
switch(state) {
@@ -321,11 +331,25 @@ JshPinFunction jshPinAnalogOutput(Pin pin,
JsVarFloat freq,
JshAnalogOutputFlags flags) { // if freq<=0, the default is used
UNUSED(flags);
if (value<0) value=0;
if (value>1) value=1;
if (!isfinite(freq)) freq=0;
if(pin == 25 || pin == 26){
if(flags & (JSAOF_ALLOW_SOFTWARE | JSAOF_FORCE_SOFTWARE)) jsError("pin does not support software PWM");
writeDAC(pin,(uint8_t)(value * 256));
}
else{
writePWM(pin,( uint16_t)(value * PWMTimerRange),(int) freq);
if(flags & JSAOF_ALLOW_SOFTWARE){
if (!jshGetPinStateIsManual(pin)){
BITFIELD_SET(jshPinSoftPWM, pin, 0);
jshPinSetState(pin, JSHPINSTATE_GPIO_OUT);
}
BITFIELD_SET(jshPinSoftPWM, pin, 1);
if ((freq<=0)) freq=50;
jstPinPWM(freq, value, pin);
return 0;
}
else writePWM(pin,( uint16_t)(value * PWMTimerRange),(int) freq);
}
return 0;
}
@@ -554,10 +578,12 @@ void jshUtilTimerDisable() {
}

void jshUtilTimerStart(JsSysTime period) {
if(period <= 30){period = 30;}
startTimer(0,(uint64_t) period);
}

void jshUtilTimerReschedule(JsSysTime period) {
if(period <= 30){period = 30;}
rescheduleTimer(0,(uint64_t) period);
}

@@ -32,16 +32,6 @@ static void uartTask(void *data) {
}
}

static void timerTask(void *data) {
timers_Init();
timer_Init("EspruinoTimer",0,0,0);
while(1) {
taskWaitNotify();
jstUtilTimerInterruptHandler();
}
}


static void espruinoTask(void *data) {
PWMInit();
RMTInit();
@@ -70,6 +60,8 @@ int app_main(void)
nvs_flash_init();
spi_flash_init();
tcpip_adapter_init();
timers_Init();
timer_Init("EspruinoTimer",0,0,0);

// Map the js_code partition into memory so can be accessed by E.setBootCode("")
const esp_partition_t* part;
@@ -90,11 +82,9 @@ int app_main(void)
tasks_init();
task_init(espruinoTask,"EspruinoTask",20000,5,0);
task_init(uartTask,"ConsoleTask",2000,20,0);
task_init(timerTask,"TimerTask",2048,19,0);
#else
xTaskCreatePinnedToCore(&espruinoTask, "espruinoTask", 20000, NULL, 5, NULL, 0);
xTaskCreatePinnedToCore(&uartTask,"uartTask",2000,NULL,20,NULL,0);
xTaskCreatePinnedToCore(&timerTask,"timerTask",2048,NULL,19,NULL,0);
#endif
return 0;
}
@@ -14,6 +14,9 @@
* Task, queue and timer specific exposed components.
* ----------------------------------------------------------------------------
*/

#include "jsinteractive.h"
#include "jstimer.h"

#include "rom/uart.h"
#include "rtosutil.h"
@@ -156,7 +159,6 @@ void taskWaitNotify(){
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
void IRAM_ATTR espruino_isr(void *para){
int idx = (int) para;
//printf("y%d",idx);
if(idx == 0){
TIMERG0.hw_timer[TIMER_0].update = 1;
TIMERG0.int_clr_timers.t0 = 1;
@@ -165,7 +167,7 @@ void IRAM_ATTR espruino_isr(void *para){
TIMERG0.hw_timer[TIMER_1].update = 1;
TIMERG0.int_clr_timers.t1 = 1;
}
vTaskNotifyGiveFromISR(RTOStasks[ESP32Timers[idx].taskToNotifyIdx].handle,&xHigherPriorityTaskWoken);
jstUtilTimerInterruptHandler();
}
void IRAM_ATTR test_isr(void *para){
int idx = (int) para;
@@ -228,14 +230,12 @@ int timer_Init(char *timerName,int group,int index,int isr_idx){
return -1;
}
void timer_Start(int idx,uint64_t duration){
//printf("StartTimer:%d = %d\n",idx,duration);
timer_enable_intr(ESP32Timers[idx].group, ESP32Timers[idx].index);
timer_set_alarm_value(ESP32Timers[idx].group, ESP32Timers[idx].index, duration - TIMER_FINE_ADJ);
TIMERG0.hw_timer[idx].config.alarm_en = 1;
timer_start(ESP32Timers[idx].group, ESP32Timers[idx].index);
}
void timer_Reschedule(int idx,uint64_t duration){
//printf("reschedule:%d\n",cnt++);
timer_set_alarm_value(ESP32Timers[idx].group, ESP32Timers[idx].index, duration - TIMER_FINE_ADJ);
TIMERG0.hw_timer[idx].config.alarm_en = 1;
}

2 comments on commit 1b3f15a

@wilberforce

This comment has been minimized.

Copy link
Member

@wilberforce wilberforce replied Sep 2, 2017

@jumjum123

line 221 of rtostil.c

       if(isr_idx == 0){
 	    ESP32Timers[i].taskToNotifyIdx = task_indexByName("TimerTask");		 	    ESP32Timers[i].taskToNotifyIdx = task_indexByName("TimerTask");
         timer_isr_register(group, index, espruino_isr, (void*) i, ESP_INTR_FLAG_IRAM, NULL);		         timer_isr_register(group, index, espruino_isr, (void*) i, ESP_INTR_FLAG_IRAM, NULL);
       }		       }

I don't think that there is a task with the name TimerTask any longer?

@jumjum123

This comment has been minimized.

Copy link
Contributor

@jumjum123 jumjum123 replied Sep 3, 2017

Please sign in to comment.