0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

初めてのRTOS (freeRTOS)

Posted at

VSCodeでWokwi,PlatformIOを使用し、シミュレータのみで実機も無しにESP32でのRT-OSを動作させた。

Arduino IDEと同じ。

動作させたマシンはManjaro Linux
関係ないけど、WMは i3

1765257964.png

_1strtos_9C25.cpp
#include <Arduino.h>

// vvv ArduinoIDEでは↓は記述する必要は無いが明示的に書いておく
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>

const int BUTTON_PIN = 4; // ボタン/入力に使用するピン (GPIO4)ニャ
const int LED_PIN = 2;    // LED/出力に使用するピン (GPIO2)ニャ

TaskHandle_t    sw_tskh ;
TaskHandle_t    led_tskh ;
QueueHandle_t sw_q ;

// ↓Arduino IDEだが、ESP32ではプロトタイプ宣言がないとcompile errorになった
void led_taskf(void *parameter);
void warikomi_go();

void setup() {

  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);

  xTaskCreate(
    led_taskf , //function
    "tact sw input task", 
    1024 , 
    NULL, // parameter
    12, 
    &led_tskh
  );
 
  sw_q = xQueueCreate(1, sizeof(byte));

  warikomi_go() ;
  Serial.begin(115200); // シリアル通信を開始し、ボーレートを設定
  Serial.println("Hello, World! from ESP32 WROVER"); // 文字列をシリアルポートに出力
}

const byte nandemo_ok = 0 ; //代入しないとCRASHする

//kes uint32_t lcome = 0 ;
void IRAM_ATTR sw_warikomif() {
//  if( millis() < lcome )return ;
//  lcome = millis() + 100 ;

  BaseType_t changeTask_to_sentQ_ifUcan = pdFALSE; // 初期値を代入しないとCRASHする
  detachInterrupt(digitalPinToInterrupt(BUTTON_PIN));//一旦 割り込み禁止
 //割り込みからこれは使用できない --> xQueueSend(led_taskh, nandemo_ok, 0) ;//queueがfullでも何もしない
 // ^^^ 0 queueがfullの時、1回だけその時間待機する。0ならすぐ帰る

  xQueueSendFromISR(sw_q,&nandemo_ok,&changeTask_to_sentQ_ifUcan);
}

void warikomi_go()
{ //  割り込みON
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), sw_warikomif, FALLING);
}

int couter ;

void led_taskf(void *parameter)
{
  byte leds = 1 ,ledb = 0;
  byte qrcv ;
  byte hc = 0 ;
  while( true )
  {
    //    vTaskDelay(pdMS_TO_TICKS(1000));
    if (xQueueReceive(sw_q, &qrcv, 222) == pdPASS) 
    {
      leds = 1-leds ;
      Serial.printf("Switch led mode %d %d\n",leds,couter++);
      ledb = 1 ;
      hc = 3 ; // 222msec x 3
    }else{
      ledb = 1-ledb ;
    }
      // 受信成功ニャ
    if( !leds )
    {
      ledb = 0 ; //消灯
    }
    
    while( hc && digitalRead(BUTTON_PIN) )
    {
      hc -- ;
      if( hc )break ;
      warikomi_go() ; //割り込み ON
      break ;
    }

    digitalWrite(LED_PIN , ledb );
  }//...while - true 
}

void loop() {
  delay(100);
  vTaskDelete(NULL); // 自身をKILL
}

/*
LED1赤GPIO2
LED2緑GPIO15
スイッチ1青GPIO4
スイッチ2赤GPIO5
*/

接続は↓

diagram.json
{
  "version": 1,
  "author": "Your Name",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-esp32-devkit-v1",
      "id": "esp",
      "top": 0,
      "left": 0,
      "attrs": {}
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 172.8,
      "left": 230.6,
      "rotate": 180,
      "attrs": {
        "color": "red"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 191.95,
      "left": 211.2,
      "rotate": 270,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 134.4,
      "left": 230.6,
      "rotate": 180,
      "attrs": {
        "color": "green"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": 153.55,
      "left": 211.2,
      "rotate": 270,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": 67.2,
      "left": 230.4,
      "rotate": 270,
      "attrs": {
        "color": "blue"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": 19.25,
      "left": 240,
      "rotate": 90,
      "attrs": {
        "value": "10000"
      }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": 28.8,
      "left": 230.4,
      "rotate": 270,
      "attrs": {
        "color": "red"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": -19.15,
      "left": 240,
      "rotate": 90,
      "attrs": {
        "value": "10000"
      }
    }
  ],
  "connections": [
    ["esp:TX0", "$serialMonitor:RX", "", []],
    ["esp:RX0", "$serialMonitor:TX", "", []],
    ["led1:A", "r1:1", "green", ["v0"]],
    ["r1:2", "esp:D2", "green", ["h0"]],
    ["led1:C", "esp:GND.1", "black", ["v0"]],
    ["led2:A", "r3:1", "green", ["v0"]],
    ["r3:2", "esp:D15", "green", ["h0"]],
    ["led2:C", "esp:GND.1", "black", ["v0"]],
    ["btn1:1.l", "esp:D4", "green", ["h0"]],
    ["btn1:2.l", "esp:GND.2", "black", ["h0"]],
    ["btn1:1.r", "r2:1", "red", ["h0"]],
    ["r2:2", "esp:3V3", "red", ["h0"]],
    ["btn2:1.l", "esp:D5", "green", ["h0"]],
    ["btn2:2.l", "esp:GND.2", "black", ["h0"]],
    ["btn2:1.r", "r4:1", "red", ["h0"]],
    ["r4:2", "esp:3V3", "red", ["h0"]]
  ]
}
platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
wokwi.toml
[wokwi]
version = 1
firmware = ".pio/build/esp32dev/firmware.bin"
elf = ".pio/build/esp32dev/firmware.elf"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?