GPIO via IPC
Lab 3: Hardware IPC GPIO (LED + Button Control)
Part 4 - IPC & Event Bus (Hardware Labs)
1. โครงสร้างภาพรวม (Overview)
Why? - ทำไมต้องเรียนรู้เรื่องนี้
ในระบบ Multi-Core จริง การควบคุม Hardware ไม่ได้ทำจาก core เดียว -- LED เป็น output (ส่งคำสั่งจาก UI ไปควบคุม GPIO) และ Button เป็น input (อ่าน GPIO แล้วส่ง event กลับมาแสดงบน UI) ทั้งสองเป็นคู่ธรรมชาติของ GPIO control ผ่าน IPC:
Output (LED):
Industrial Automation: หน้าจอ HMI (CM55) ส่งคำสั่งไปยัง PLC Controller (CM33) เพื่อเปิด/ปิดมอเตอร์ วาล์ว ไฟสัญญาณ
Smart Factory: Operator กด UI บน Dashboard --> คำสั่งเดินทางผ่าน IPC --> CM33 ควบคุม Actuator จริง
Safety Isolation: แยก UI core กับ Control core เพื่อความปลอดภัย ถ้า UI crash, control ยังทำงาน
Input (Button):
Industrial Safety: ปุ่ม E-STOP (Emergency Stop) อยู่ที่ PLC controller --> ส่ง event ไป HMI แสดงสถานะ
Remote Monitoring: ปุ่มกดที่ field device --> event ถูกส่งผ่าน bus --> แสดงที่ control room
Safety Interlock: ปุ่ม confirm ต้องถูกกดก่อน actuator จะทำงาน (Two-hand safety control)
ในบอร์ด PSoC Edge E84:
CM55 รัน LVGL display แต่ไม่มี GPIO access โดยตรง
CM33-NS ควบคุม GPIO, LED, PWM และอ่าน button ได้
การควบคุม LED จาก UI ต้องส่งคำสั่ง IPC จาก CM55 ไปยัง CM33-NS
การอ่าน Button ต้องให้ CM33-NS poll แล้วส่ง event กลับมา CM55
What? - จะได้เรียนรู้อะไร
Part A (Ex5 - IPC LED Control):
IPC LED Commands: ส่ง
IPC_CMD_LED_SETและIPC_CMD_LED_BRIGHTNESSจาก CM55Multi-LED Control: ควบคุม LED 3 ดวง (Red, Green, Blue) ผ่าน IPC
PWM Brightness: ควบคุมความสว่างผ่าน Slider --> IPC --> CM33 PWM
Status Feedback: CM33 ยืนยันสถานะ LED กลับมาแสดงบน UI
Flag-Based Pattern: อัพเดท LVGL อย่างปลอดภัยจาก IPC callback
IPC Timing: จัดการ 20ms delay ระหว่าง IPC sends
Part B (Ex6 - IPC Button Events):
Button Polling + Debounce: CM33-NS อ่าน button ด้วย debounce algorithm
IPC Button Events: ส่ง
IPC_CMD_BUTTON_EVENTจาก CM33 ไป CM55Long-Press Detection: ตรวจจับการกดค้าง (>1 วินาที) แยกจาก short press
Flag-Based UI Update: CM55 รับ event แล้ว update UI ผ่าน LVGL timer
Event Counter: นับจำนวนครั้งที่กด + แสดง timestamp
Event Logger: บันทึก button events พร้อม timestamp
How? - ทำอย่างไร
Part A: สร้าง LED Control Panel บน CM55 ที่มี Switch 3 ตัว + Brightness Slider --> เมื่อ user สั่งจาก UI --> CM55 ส่ง IPC command --> CM33-NS ควบคุม GPIO LED จริง --> CM33-NS ตอบกลับสถานะ --> CM55 อัพเดท UI
Part B: CM33-NS poll USER button ทุก 10ms พร้อม debounce --> เมื่อตรวจจับ press/release/long-press ส่ง IPC event ไป CM55 --> CM55 IPC callback ตั้ง flag --> LVGL timer อ่าน flag แล้ว update UI
2. หลักการทำงาน (Technical Principles)
2.1 Data Flow Direction Comparison
2.2 IPC LED Control Architecture (Part A)
2.3 IPC LED Command Protocol
2.4 Flag-Based Pattern (Thread Safety)
2.5 IPC Button Event Architecture (Part B)
2.6 Button Debounce Algorithm
2.7 IPC Button Data Structure
3. ฟังก์ชันสำคัญ (API Reference)
3.1 CM55 IPC Send Functions (LED Control)
cm55_ipc_set_led(led_id, state)
ส่งคำสั่ง LED ON/OFF ไป CM33
cy_en_ipc_pipe_status_t
cm55_ipc_set_led_brightness(led_id, brightness)
ส่งคำสั่ง brightness (0-100)
cy_en_ipc_pipe_status_t
cm55_ipc_send_retry(msg, retries)
ส่ง IPC message พร้อม retry
cy_en_ipc_pipe_status_t
cm55_ipc_register_callback(cb, data)
ลงทะเบียน IPC callback
void
3.2 CM33-NS GPIO Functions
aic_gpio_init()
Initialize GPIO subsystem
void
aic_gpio_led_set(led, state)
ควบคุม LED GPIO จริง
void
aic_gpio_button_read_raw(AIC_BTN_USER)
อ่าน button state (0=pressed)
uint8_t
Cy_SysLib_Delay(ms)
Delay (CM33, ไม่ใช่ FreeRTOS)
void
3.3 CM33-NS IPC Functions
cm33_ipc_send_button_event(id, pressed)
ส่ง button event ไป CM55
cy_en_ipc_pipe_status_t
cm33_ipc_send_retry(msg, retries)
ส่ง IPC message พร้อม retry
cy_en_ipc_pipe_status_t
cm33_ipc_send_led_state(led_id, state)
ส่งยืนยันสถานะ LED กลับ
cy_en_ipc_pipe_status_t
3.4 IPC Data Structures
ipc_led_data_t
led_id, state, brightness, reserved
LED control payload
ipc_button_data_t
button_id, pressed, long_press, timestamp
Button event payload
ipc_msg_t
client_id, intr_mask, cmd, value, data[]
IPC message wrapper
3.5 LVGL Timer
lv_timer_create(cb, period_ms, user_data)
สร้าง timer สำหรับ poll flags
lv_timer_t *
lv_led_set_brightness(led, bright)
ตั้ง LED widget brightness (0-255)
void
4. Part A: IPC LED Control (Ex5)
Part A สอน output direction -- CM55 ส่งคำสั่งไปควบคุม LED จริงบน CM33-NS ผ่าน IPC พร้อม ACK feedback
4.1 Program Flowchart
4.2 CM55 Code - LED Control UI + IPC Send
4.3 CM33-NS Code - LED IPC Handler
5. Part B: IPC Button Read (Ex6)
Part B สอน input direction -- CM33-NS อ่าน button ด้วย debounce state machine แล้วส่ง event ไป CM55 แสดงผล
5.1 CM33-NS Code - Button Polling with Debounce
5.2 CM55 Code - Button Event UI Display
6. องค์ความรู้และเทคนิค (Patterns & Tips)
6.1 IPC Message Packing Pattern
6.2 Convenience API vs Raw Message
6.3 Multiple LED Animation with Delay
6.4 Debounce Timing Selection
6.5 Long-Press vs Short-Press Pattern
6.6 Circular Event Log Buffer
7. แบบฝึกหัด (Exercises)
Exercise 1: LED Animation Controller (Part A - Intermediate)
สร้าง LED Sequence Pattern Controller ที่ส่ง pattern ผ่าน IPC:
Requirements:
4 pattern buttons: "Chase", "Blink All", "Alternate", "Fade"
Chase: LED แดง -> เขียว -> น้ำเงิน วนไป
Blink All: กะพริบพร้อมกัน (ON 500ms -> OFF 500ms)
Alternate: สลับ (Red+Blue ON, Green OFF -> Red+Blue OFF, Green ON)
Fade: ไล่ brightness จาก 0-100-0 แบบ smooth
ปุ่ม STOP หยุด animation ทันที
แสดง pattern ปัจจุบันบน UI
Hints:
สร้าง FreeRTOS task แยกสำหรับ animation
ใช้ global flag
animation_running+animation_typeSTOP button ตั้ง flag false --> task หยุดวน
ระหว่าง IPC sends ใน animation ต้อง delay 20ms
Fade: ใช้ loop step ละ 5% ส่ง brightness ทุก 50ms
Exercise 2: Long-Press Detection with Different Actions (Part B - Intermediate)
เพิ่ม long-press detection ที่มี action แตกต่างจาก short press:
Requirements:
Short press (<1s): Toggle LED widget (ON/OFF) + increment counter
Long press (>1s): Reset counter เป็น 0 + flash LED widget 3 ครั้ง
Very long press (>3s): แสดง "FACTORY RESET" warning label (แค่แสดง ไม่ reset จริง)
LED widget เปลี่ยนสีตาม action (Green/Orange/Red)
Progress bar แสดง hold duration (0-3s) ขณะกดค้าง
Exercise 3: LED Brightness Sync (Part A - Advanced)
สร้างระบบ Sync ที่ Slider บน CM55 ควบคุม PWM brightness บน CM33 พร้อม feedback:
Requirements:
3 Sliders แยกสำหรับ Red, Green, Blue (range 0-100)
แต่ละ slider ส่ง
IPC_CMD_LED_BRIGHTNESSไป CM33CM33 set PWM duty cycle --> ส่ง ACK + actual brightness กลับ
แสดง "Requested: 75%" vs "Actual: 73%"
Color Preview: สี่เหลี่ยมแสดง mixed color จาก R+G+B values
มีปุ่ม "All 100%", "All 0%", "Random" presets
IPC round-trip time display
Exercise 4: Button Event Logger with Statistics (Part B - Advanced)
สร้าง full event logger ที่บันทึกทุก button event พร้อม timestamp และ statistics:
Requirements:
Event Log แสดง 12 entries ล่าสุด (circular buffer)
แต่ละ entry แสดง: [timestamp] event_type (duration)
Statistics panel: Total events, Average press duration, Longest/shortest press, Events per minute rate
Export button: รวม log เป็น string ส่งไป CM33 console ผ่าน IPC LOG
Clear button: ล้าง log + reset statistics
8. สรุป (Summary)
สิ่งที่เรียนรู้ใน Lab นี้
Part A (LED):
IPC LED Control: ส่งคำสั่ง
IPC_CMD_LED_SETจาก CM55 ไปควบคุม GPIO บน CM33-NSFlag-Based Pattern: วิธีเดียวที่ปลอดภัยในการ update LVGL จาก IPC callback
Status Feedback: CM33 ส่ง ACK กลับเพื่อยืนยันว่า LED ทำงานจริง
20ms IPC Delay: จำเป็นเมื่อส่ง IPC หลายครั้งติดกัน ป้องกัน buffer overwrite
Multi-LED Management: จัดการ LED หลายดวงด้วย struct array + user_data pattern
Part B (Button):
Button Debounce: State machine pattern สำหรับกรอง contact bounce
Long-Press Detection: ใช้ timer ตรวจ hold duration ในขณะ pressed state
IPC Button Events: ส่ง
ipc_button_data_tจาก CM33 ไป CM55 พร้อม timestampEvent Logging: Circular buffer pattern สำหรับเก็บ event history
Flag-Based Update: Volatile flags + LVGL timer = thread-safe UI update
กฎสำคัญที่ต้องจำ
Application ในงานจริง
Industrial Automation
HMI panel ควบคุม motor, valve, indicator light ผ่าน fieldbus
Smart Building
Touch panel ควบคุมไฟ, HVAC, ม่าน ผ่าน KNX/BACnet
Medical Device
UI แสดงผล + ควบคุม actuator (pump, valve) แยก core เพื่อ safety
Automotive
Instrument cluster ส่งคำสั่งไป body control module
Safety Systems
E-STOP button detection, safety interlock monitoring
Access Control
Keypad/card reader events --> central monitoring
IoT Gateway
Physical reset button --> cloud notification
Last updated