CAPSENSE via IPC

Lab 5: CAPSENSE via IPC

Part 4 - IPC & Event Bus (Hardware Labs)


1. โครงสร้างภาพรวม (Overview)

Why? - ทำไมต้องเรียนรู้เรื่องนี้

ในระบบ Embedded จริง อุปกรณ์ Input (sensor, touch, keypad) มักแชร์ bus กับอุปกรณ์อื่น:

  • HMI Touch Panel + External Sensor: หน้าจอ touch กับ sensor ภายนอกใช้ I2C bus เดียวกัน ถ้าอ่านพร้อมกันจะ crash

  • Automotive Cluster: CAPSENSE steering wheel controls อ่านจาก MCU ตัวนึง ส่งข้อมูลผ่าน CAN bus ไปแสดงบน instrument cluster

  • Industrial Keypad: keypad module ต่อกับ controller ผ่าน I2C แล้วส่ง key event ผ่าน internal bus ไป HMI core

  • Medical Device: capacitive touch interface ต้องแยก core อ่าน touch กับ core แสดงผลเพื่อความปลอดภัย (safety isolation)

ในบอร์ด PSoC Edge E84:

  • PSoC 4000T CAPSENSE (2 ปุ่ม + 1 slider) ต่อผ่าน I2C SCB0 (address 0x08)

  • Display Touch ก็ใช้ I2C SCB0 เหมือนกัน

  • Part 1 Ex11: CM55 อ่าน CAPSENSE ตรง ต้อง disable touch screen --> ใช้ touch จากหน้าจอไม่ได้!

  • Part 4 Ex9: CM33-NS อ่าน CAPSENSE --> ส่ง IPC ไป CM55 --> touch screen ยังทำงานปกติ!

What? - จะได้เรียนรู้อะไร

  1. IPC-Based Peripheral Access: CM33 อ่าน CAPSENSE ผ่าน I2C แล้วส่งข้อมูลผ่าน IPC ไป CM55

  2. Thread-Safe Flag Pattern: IPC callback ตั้ง flag --> LVGL timer อ่าน flag --> update UI

  3. Edge Detection: ส่ง IPC เฉพาะเมื่อสถานะเปลี่ยน (ลด traffic)

  4. IPC Protocol Design: ออกแบบ command + payload สำหรับ CAPSENSE data

  5. Architecture Comparison: เปรียบเทียบ Direct I2C vs IPC approach

  6. Bus Conflict Resolution: แก้ปัญหา I2C bus sharing ด้วย multi-core architecture

How? - ทำอย่างไร

CM33-NS poll PSoC 4000T CAPSENSE ทุก 100ms ผ่าน I2C --> เปรียบเทียบกับ state ก่อนหน้า (edge detection) --> ถ้าเปลี่ยน ส่ง IPC_CMD_CAPSENSE_DATA พร้อม payload [BTN0, BTN1, Slider, SliderActive] ไป CM55 --> CM55 IPC callback เก็บข้อมูลใน volatile variables + set flag --> LVGL timer (50ms) ตรวจ flag --> update UI (slider, LED, panel backgrounds)


2. หลักการทำงาน (Technical Principles)

2.1 เปรียบเทียบ: Direct I2C vs IPC Approach

2.2 Comparison Table: Direct I2C vs IPC

หัวข้อ
Part 1 Ex11 (Direct I2C)
Part 4 Ex9 (IPC)

CAPSENSE อ่านจาก

CM55 (direct)

CM33-NS (via I2C)

Touch Screen

DISABLED (bus conflict)

WORKS normally

I2C Bus

CM55 ใช้ทั้ง touch + CAPSENSE

แยก: CM33=CAPSENSE, CM55=touch

Latency

~50ms (direct read)

~70ms (I2C + IPC + LVGL timer)

Code Complexity

ต่ำ (single core)

ปานกลาง (IPC callback + flags)

Thread Safety

ไม่มีปัญหา (single thread)

ต้องใช้ volatile + flag pattern

Architecture

ไม่ดี (bus conflict)

ดี (CM33=HW, CM55=UI)

Scalability

ยาก (bus เต็ม)

ดี (เพิ่ม sensor ได้)

Production Ready

ไม่ (touch ใช้ไม่ได้)

ใช่ (ทุกอย่างทำงาน)

I2C Contention

มี (must disable touch)

ไม่มี (แยก core)

2.3 CAPSENSE IPC Architecture

2.4 IPC CAPSENSE Data Format

2.5 Thread-Safe Flag Pattern (CRITICAL)

2.6 Program Flowchart


3. ฟังก์ชันสำคัญ (API Reference)

3.1 CM55 IPC Functions

Function
Description
Return

cm55_ipc_register_callback(cb, data)

ลงทะเบียน IPC callback

void

cm55_ipc_send_cmd(IPC_CMD_CAPSENSE_REQ, 0)

ขอ CAPSENSE state จาก CM33

cy_en_ipc_pipe_status_t

cm55_ipc_is_init()

ตรวจว่า IPC initialized หรือไม่

bool

cm55_ipc_get_stats(tx, rx, err)

ดึง IPC statistics

void

3.2 CM33-NS CAPSENSE Functions

Function
Description
Return

capsense_module_init(hw, ctx)

Initialize CAPSENSE module + I2C

void

capsense_module_poll()

อ่าน I2C + edge detect + ส่ง IPC

void

capsense_module_send_current()

ส่งสถานะปัจจุบัน (for REQ)

void

3.3 LVGL Widget Functions

Function
Description
Return

lv_slider_create(parent)

สร้าง slider (read-only mode)

lv_obj_t *

lv_slider_set_value(slider, val, anim)

ตั้งค่า slider

void

lv_led_create(parent)

สร้าง LED widget

lv_obj_t *

lv_led_on(led) / lv_led_off(led)

เปิด/ปิด LED widget

void

lv_led_set_brightness(led, bright)

ตั้ง brightness (0-255)

void

lv_obj_set_style_bg_color(obj, color, sel)

เปลี่ยนสีพื้นหลัง panel

void

lv_obj_remove_flag(obj, LV_OBJ_FLAG_CLICKABLE)

ปิด click บน slider

void

3.4 IPC CAPSENSE Data

Field
Type
Byte
Description

btn0

uint8_t

data[0]

0=released, 1=pressed (CSB1)

btn1

uint8_t

data[1]

0=released, 1=pressed (CSB2)

slider_pos

uint8_t

data[2]

0-100 (0=no touch)

slider_active

uint8_t

data[3]

0=not touching, 1=touching


4. โค้ดตัวอย่าง (Code Examples)

4.1 CM55 Code - CAPSENSE IPC Display

4.2 CM33-NS Code - CAPSENSE I2C Reader + IPC Sender

4.3 Code Breakdown

Part A: Volatile Flag Pattern (CRITICAL)

Part B: Edge Detection (CM33 Side)

Part C: Edge Detection (CM55 Side)

Part D: Read-Only Slider Widget


5. องค์ความรู้และเทคนิค (Patterns & Tips)

5.1 I2C Bus Sharing Problem

5.2 IPC Message Protocol Design

5.3 PSoC 4000T I2C Protocol Details

5.4 CAPSENSE_REQ: Request-Response Pattern


6. แบบฝึกหัด (Exercises)

Exercise 1: CAPSENSE Event Counter + Statistics (Intermediate)

เพิ่มระบบนับ event และแสดง statistics ของ CAPSENSE:

Requirements:

  • นับจำนวนครั้งที่กด BTN0, BTN1, และ slider touch

  • แสดง total events, events per minute

  • Last event timestamp (tick count)

  • Longest slider touch duration (ms)

  • Average slider position (เฉพาะขณะ touch)

  • Statistics panel เพิ่มระหว่าง button panels

Expected Output:

Hints:

  • เก็บ touch_start_tick เมื่อ slider_active เปลี่ยนจาก 0 -> 1

  • คำนวณ duration เมื่อ slider_active เปลี่ยนจาก 1 -> 0

  • Average = total_position_sum / total_position_count

  • Rate = total_events * 60 / (uptime_seconds)

  • ใช้ panel ขนาด 300x80 อยู่กลางระหว่าง slider กับ buttons


Exercise 2: CAPSENSE LED Control via IPC (Advanced)

ใช้ CAPSENSE ควบคุม physical LED ผ่าน IPC chain:

Requirements:

  • BTN0 (CSB1): Toggle Red LED (กด = toggle ON/OFF)

  • BTN1 (CSB2): Toggle Green LED (กด = toggle ON/OFF)

  • Slider (CSS1): Blue LED brightness (0-100% via IPC_CMD_LED_BRIGHTNESS)

  • UI แสดง LED state + slider brightness

  • IPC flow: CAPSENSE data มาถึง CM55 -> CM55 ส่ง LED command กลับ CM33

  • 20ms delay ระหว่าง IPC sends (CRITICAL)

  • LED toggle ต้องมี edge detection (กดทีเดียว toggle ครั้งเดียว)

Expected Output:

Hints:

  • Toggle pattern: track prev_btn0 state, toggle only on 0->1 transition

  • Slider brightness: ส่ง cm55_ipc_set_led_brightness(2, slider_pos) เฉพาะเมื่อค่าเปลี่ยน

  • 20ms delay ระหว่าง LED commands:

  • Full IPC chain creates round-trip latency: ~100-150ms end-to-end

  • ใช้ cm55_ipc_get_stats() แสดง TX/RX statistics


7. สรุปและขั้นตอนถัดไป (Summary & Next Steps)

สิ่งที่เรียนรู้ใน Lab นี้

  1. IPC-Based Peripheral Access: ย้ายการอ่าน CAPSENSE จาก CM55 ไป CM33 ผ่าน IPC เพื่อแก้ปัญหา I2C bus conflict

  2. Thread-Safe Flag Pattern: ใช้ volatile flags + LVGL timer เพื่อ update UI อย่างปลอดภัยจาก IPC callback

  3. Edge Detection (Dual-Layer): ทั้ง CM33 (ลด IPC traffic) และ CM55 (ลด LVGL redraws)

  4. IPC Protocol Design: ออกแบบ command + payload สำหรับ CAPSENSE data (4 bytes)

  5. Architecture Decision: เลือก IPC approach แทน direct I2C เพื่อให้ touch screen ทำงานได้

Architecture Lesson

ขั้นตอนถัดไป

  • Previous: Lab 4: HW IPC Dashboard

  • Next: Mini Project Part 4 -- Multi-Core Sensor Fusion System

Application ในงานจริง

ด้าน
ตัวอย่าง

Automotive

Touch controls on steering wheel -> CAN -> instrument cluster display

Industrial HMI

Capacitive keypad on machine -> fieldbus -> SCADA display

Consumer Electronics

Touch slider (volume/brightness) -> MCU -> display controller

Medical Devices

Capacitive buttons (sealed enclosure) -> MCU -> UI processor

Smart Home

Touch panel -> Zigbee/BLE -> central hub display

Last updated

Was this helpful?