[ESP-IDE] ESP32-PICO-D4 with VSCode
이렇게 하고 F1눌러서
ESP-IDF 터미널 열러준다.
idf.py build
idf.py -p COMx flash monitor
입력해주면 된다.
오늘은 Unexpected Maker에서 출시한 TinyPICO 보드를 가져와서, ESP-IDF 환경에서 개발을 진행해봤습니다.
이 포스팅의 목표는 다음과 같습니다:
ESP32-PICO-D4 칩을 사용하는 TinyPICO의 내장 DotStar RGB LED를 ESP-IDF로 제어해보자
1. 왜 ESP-IDF인가?
TinyPICO는 ESP32 기반의 초소형 보드입니다. 아두이노 IDE에서도 사용할 수 있지만, 아두이노 레이어는 ESP32의 모든 기능을 다루기엔 한계가 있습니다.
Espressif 공식 프레임워크인 **ESP-IDF(ESP IoT Development Framework)**를 사용하면 더 세밀한 제어가 가능하며, FreeRTOS 기반 비동기 처리가 가능합니다.
2. VSCode에서 ESP-IDF 프로젝트 생성
먼저 VSCode를 켜주자. -> vscode에서 esp-idf 설치는 다음 포스팅을 따르자
https://kksp12y.tistory.com/93
ESP-IDF를 사용하자
먼저 ESP32를 사용하는데 있어 아두이노 IDE를 사용하면 ESP32의 기능을 전부 사용하지 못한다. ESP-IDF는 ESP IoT Development Framework의 줄임말로 Espressif Systems에서 공식 지원하는 프레임워크이다.(아두이
kksp12y.tistory.com
프로젝트 생성
VSCode에서 F1 → ESP-IDF: New Project 실행
Toolchain 및 ESP-IDF 버전 선택
보드 선택 화면에서는 TinyPICO는 기본 제공 보드 목록에 없습니다. 따라서 Custom Board로 선택
다음과 같은 OpenOCD 설정을 반드시 입력
interface/ftdi/esp32_devkitj_v1.cfg,target/esp32.cfg
설정 파일 경로 | 역할 |
interface/ftdi/esp32_devkitj_v1.cfg | 디버깅용 인터페이스 하드웨어 (JTAG, SWD 등) 설정. 보통 USB 디버깅 어댑터 설정 포함. |
target/esp32.cfg | ESP32 칩 내부 구조 및 디버깅 프로토콜 설정. |
입력하지 않으면 프로젝트 생성이 실패할 수 있습니다. 이는 ESP-IDF에서 디버깅 환경 구성을 기본으로 시도하기 때문입니다.
3. 디렉토리 구조 및 핀맵
디렉토리 구성은 아래와 같습니다:
├── main/
│ ├── main.c // 메인 코드
│ ├── dotstar.c // DotStar 제어 소스
│ └── dotstar.h // DotStar 헤더
핀맵은 TinyPICO 공식 홈페이지를 참고했습니다.
https://www.tinypico.com
DotStar 관련 핀 배치는 다음과 같습니다:
- DOTSTAR_PWR → GPIO 13
- DOTSTAR_DATA (MOSI) → GPIO 2
- DOTSTAR_CLK → GPIO 12
4. 참고 라이브러리
TinyPICO용 아두이노 라이브러리를 변환하여 사용하였습니다.
- GitHub 원본: tinypico-arduino
GitHub - tinypico/tinypico-arduino: Arduino libraries and example code for TinyPICO
Arduino libraries and example code for TinyPICO. Contribute to tinypico/tinypico-arduino development by creating an account on GitHub.
github.com
5. 소스 코드
main.c
void app_main(void)
{
dotstar_init();
vTaskDelay(pdMS_TO_TICKS(10)); // 전원 안정화 시간
dotstar_set_rgb(0, 255, 0); // 첫 명령 보정 전송
vTaskDelay(pdMS_TO_TICKS(10)); // 보정 후 잠깐 딜레이
while (1) {
dotstar_set_rgb(0, 255, 0); // 초록색
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_set_rgb(255, 0, 0); // 빨간색
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_set_rgb(0, 0, 255); // 파란색
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_power(false); // 꺼짐
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_power(true); // 다시 켬
vTaskDelay(pdMS_TO_TICKS(10)); // 안정화 시간
dotstar_set_rgb(0, 255, 0); // 보정용 재전송
vTaskDelay(pdMS_TO_TICKS(10));
}
}
dotstar.h
#pragma once
#include <stdint.h>
#include <stdbool.h>
void dotstar_init(void);
void dotstar_set_rgb(uint8_t r, uint8_t g, uint8_t b);
void dotstar_power(bool on);
dotstar.c
#include "dotstar.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_log.h"
#define TAG "DOTSTAR"
#define DOTSTAR_PWR_PIN 13
#define DOTSTAR_MOSI_PIN 2
#define DOTSTAR_CLK_PIN 12
static spi_device_handle_t spi;
void dotstar_power(bool on)
{
gpio_set_level(DOTSTAR_PWR_PIN, on ? 0 : 1); // LOW = ON, HIGH = OFF
}
void dotstar_init(void)
{
gpio_reset_pin(DOTSTAR_PWR_PIN);
gpio_set_direction(DOTSTAR_PWR_PIN, GPIO_MODE_OUTPUT);
dotstar_power(true);
spi_bus_config_t buscfg = {
.mosi_io_num = DOTSTAR_MOSI_PIN,
.miso_io_num = -1,
.sclk_io_num = DOTSTAR_CLK_PIN,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 1000000,
.mode = 0,
.spics_io_num = -1,
.queue_size = 1,
};
spi_bus_initialize(HSPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
}
void dotstar_set_rgb(uint8_t r, uint8_t g, uint8_t b)
{
uint8_t data[12] = {
0x00, 0x00, 0x00, 0x00, // Start frame
0xFF, b, g, r, // Brightness + BGR
0xFF, 0xFF, 0xFF, 0xFF // End frame
};
spi_transaction_t t = {
.length = 8 * sizeof(data),
.tx_buffer = data,
};
esp_err_t ret = spi_device_transmit(spi, &t);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "SPI transmit failed");
}
}
6. 빌드 및 실행
F1 눌러서 터미널 열기
VSCode에서 터미널을 열고 아래 명령을 입력합니다:
idf.py build
idf.py -p COMx flash monitor
COMx는 자신의 보드가 연결된 포트 번호로 바꿔주세요.
구동 영상
https://youtube.com/shorts/kvVKQ68bw58?feature=share
* 실패한 코드
main.c
#include "dotstar.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void app_main(void)
{
dotstar_init();
while (1) {
dotstar_set_rgb(255, 0, 0); // 빨간색
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_set_rgb(0, 255, 0); // 초록색
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_set_rgb(0, 0, 255); // 파란색
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_power(false); // 꺼짐
vTaskDelay(pdMS_TO_TICKS(1000));
dotstar_power(true); // 다시 켬
}
}
첫 빨간색이 처음에만 나오고 뒤로는 안나오는 경우 발생.
- 예상 이유 : 키고나서 바로 빨간색으로 가서 안정화 시간이 필요하다고 생각. -> 해결완료