2021年11月28日日曜日

IRQ/MULTICORE (irq :: arduino/espr2/esp32, dualcore :: raspico,esp32)

Arduinoの割り込み機能を使ってみる | 物を作る者 (novicengineering.com) を

参考にしたが、タクトスイッチでは、一方に5Vをいれ、片方に10kプルダウン抵抗を入れて

その間からPin2への入力を取った 

attachInterruptの使い方 | 物を作る者 (novicengineering.com) によると

割り込みではdelayは利かんのも体験した1

-----------------------------------------------------------------------------

Esp32 GPIO 割り込みについて

https://blog.goo.ne.jp/jh7ubc/e/8c97dc4bfad4f93d44301f8341083a69

INPUT_PULLUPについては http://7ujm.net/micro/arduino_int.html および

https://e-words.jp/w/%E3%83%97%E3%83%AB%E3%82%A2%E3%83%83%E3%83%97%E6%8A%B5%E6%8A%97.html を参考に勉強した 

#define LED_Pin 27
#define SW_Pin 33
volatile int state = LOW;
 
void setup() {
  pinMode(SW_Pin, INPUT_PULLUP);
  pinMode(LED_Pin, OUTPUT);
  attachInterrupt(SW_Pin, LED_blink, FALLING);
}

void loop() {
  digitalWrite(LED_Pin, state);
}

void LED_blink(){
  delay(10);
  state = !state;
}

---------------------------------------------------------------------------------------------------

ここよりマルチコア

-------------------------------------------------------------------------------------------------

Multithreaded on Raspberry Pi Pico (MicroPython) - ElectroSoftCloud とても簡単

import machine
import utime
import _thread
# We configure the pin of the internal led as an output and
# we assign to internal_led
internal_led = machine.Pin(25, machine.Pin.OUT)
# Function that will block the thread with a while loop
# which will simply display a message every second
def second_thread():
while True:
print("Hello, I'm here in the second thread writting every second")
utime.sleep(1)
# Function that initializes execution in the second core
# The second argument is a list or dictionary with the arguments
# that will be passed to the function.
_thread.start_new_thread(second_thread, ())
# Second loop that will block the main thread, and what it will do
# that the internal led blinks every half second
while True:
internal_led.toggle()
utime.sleep(0.25) ---------------------------------------------------using semaphore--------------------
import machine
import utime
import _thread
# We configure the pin of the internal led as an output and
# we assign to internal_led
internal_led = machine.Pin(25, machine.Pin.OUT)
# We create a semaphore (A.K.A lock)
baton = _thread.allocate_lock()
# Function that will block the thread with a while loop
# which will simply display a message every second
def second_thread():
while True:
# We acquire the traffic light lock
baton.acquire()
print("Hello, I'm here in the second thread writting every second")
utime.sleep(1)
# We release the traffic light lock
baton.release()
# Function that initializes execution in the second core
# The second argument is a list or dictionary with the arguments
# that will be passed to the function.
_thread.start_new_thread(second_thread, ())
# Second loop that will block the main thread, and what it will do
# that the internal led blinks every half second
while True:
# We acquire the semaphore lock
baton.acquire()
internal_led.toggle()
utime.sleep(0.25)
# We release the semaphore lock
baton.release()

ーーーーーーーー-dual core usingーーーーーーーーーーーーーーーーーーーーーーーーー

https://programresource.net/2020/02/25/2994.html -> esp32 dual core program

https://robo.mydns.jp/Lecture/?%C5%C5%BB%D2%B9%A9%BA%EE/ESP32#va27dec2 

https://www.mgo-tec.com/blog-entry-arduino-esp32-multi-task-dual-core-01.html

いずれも粗い議論だが、入門には便利 以下のサイトはとても有益だった

 https://www.kerislab.jp/posts/2017-06-24-esp32-dual-core/ このままうごいた

#include "esp32-hal-log.h"
#include "freertos/task.h"

void task0 (void* arg) {
  while (1) {
    log_i("This is Core 0");
    delay(1000);
  }
}

void task1 (void* arg) {
  while (1) {
    log_i("This is Core 1");
    delay(1500);
  }
}

void setup () {
  log_i("Hello, this is ESP32:)");
  xTaskCreatePinnedToCore(task0, "Task0", 4096, NULL, 1, NULL, 0);
  xTaskCreatePinnedToCore(task1, "Task1", 4096, NULL, 1, NULL, 1);
}

void loop () {
  delay(1500);

0 件のコメント:

コメントを投稿