Skip to content

Commit

Permalink
Move util timer from its own task into espruino task
Browse files Browse the repository at this point in the history
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 1b3f15a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
28 changes: 27 additions & 1 deletion targets/esp32/jshardware.c
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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()");
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 2 additions & 12 deletions targets/esp32/main.c
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
8 changes: 4 additions & 4 deletions targets/esp32/rtosutil.c
Expand Up @@ -14,6 +14,9 @@
* Task, queue and timer specific exposed components.
* ----------------------------------------------------------------------------
*/

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

#include "rom/uart.h"
#include "rtosutil.h"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

2 comments on commit 1b3f15a

@wilberforce
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Contributor

@jumjum123 jumjum123 commented on 1b3f15a Sep 3, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.