How to handle interrupts on HTTP requests on the W6300-EVB-Pico
Implementing Interrupt-Driven Handling on HTTP Requests with W6300-EVB-Pico
This content was written by namo02268 on the Qiita website.
Original link: https://qiita.com/namo02268/items/c2fef489f4781b3f6fdf
Summary
This article describes how to enable and handle interrupt-driven events on the W6300-EVB-Pico board to respond to HTTP requests. Rather than polling sockets continuously, the author shows how to configure WIZnet chip and RP2040 GPIO interrupts so that specific socket events (connect, disconnect, receive, timeout) trigger a callback routine. This allows more efficient handling of network events, page updates, or waking from sleep states when HTTP requests arrive.
Project Introduction
This project sets up the W6300-EVB-Pico such that HTTP request events trigger hardware interrupts instead of relying on continuous polling. It combines WIZnet’s socket-level interrupt capabilities with RP2040 GPIO interrupts to enable more reactive and efficient network handling. Key aspects:
Interrupt Mask Configuration per Socket
For each socket, you can enable which events should produce interrupts:
SIK_CONNECTED (connection established)
SIK_DISCONNECTED (disconnection)
SIK_RECEIVED (data arrival)
SIK_TIMEOUT (timeout)
These are configured via ctlsocket(socket, CS_SET_INTMASK, &mask).
Note: The SIK_SENT flag is found to cause instability in some cases and is not recommended. Qiita
Chip-Level Interrupt Enable
After per-socket masks, you enable the WIZnet chip’s overall interrupt system via ctlwizchip(CW_SET_INTRMASK, &chip_intr), where chip_intr is a bitmask representing which socket interrupts are allowed. When any of the enabled socket events occur, the chip’s INT pin goes low. Qiita
RP2040 / GPIO Interrupt Handling
On the RP2040 side (i.e. Raspberry Pi Pico side), you set up a GPIO interrupt on the INT pin (for W6300, the INT is wired to GPIO 15 on the board) using gpio_set_irq_enabled_with_callback(...). When the INT pin transitions (falling edge), the callback interrupt_callback is invoked. Qiita
Interrupt Callback Logic
Inside the interrupt handler, for each socket you call ctlsocket(sock, CS_GET_INTERRUPT, &sock_intr) to retrieve which events happened. You can then branch logic according to whether SIK_CONNECTED, SIK_RECEIVED, etc. were flagged, perform updates (e.g. refresh page state, wake from sleep), and finally clear the interrupt flags with ctlsocket(sock, CS_CLR_INTERRUPT, &sock_intr). Qiita
Integrated Example Code
The article includes a full C code example combining network setup, HTTP server initialization, interrupt registration, and main loop. In main(), after initialization, the code repeatedly calls httpServer_run(i) in a loop, but actual event reactions occur via interrupts rather than heavy polling. Qiita
Behavior & Benefits
More efficient CPU usage, since the code doesn’t have to continuously poll sockets.
Timely reaction to incoming HTTP requests or disconnections.
Enables integration with sleep or low-power modes: the system can wake on relevant socket events.
Clear separation between network events and application logic in interrupt handler.
Korean Ver
요약
이 문서에서는 W6300-EVB-Pico 보드에서 HTTP 요청에 응답하기 위해 인터럽트 기반 이벤트를 활성화하고 처리하는 방법을 설명합니다 . 소켓을 지속적으로 폴링하는 대신, 저자는 WIZnet 칩과 RP2040 GPIO 인터럽트를 구성하여 특정 소켓 이벤트(연결, 연결 해제, 수신, 시간 초과)가 콜백 루틴을 트리거하도록 하는 방법을 보여줍니다. 이를 통해 네트워크 이벤트, 페이지 업데이트 또는 HTTP 요청 수신 시 절전 모드에서 해제하는 작업을 더욱 효율적으로 처리할 수 있습니다.
프로젝트 소개
이 프로젝트는 W6300-EVB-Pico를 설정하여 HTTP 요청 이벤트가 연속 폴링에 의존하는 대신 하드웨어 인터럽트를 트리거하도록 합니다. WIZnet의 소켓 수준 인터럽트 기능과 RP2040 GPIO 인터럽트를 결합하여 더욱 반응적이고 효율적인 네트워크 처리를 가능하게 합니다. 주요 특징은 다음과 같습니다.
소켓당 인터럽트 마스크 구성
각 소켓에 대해 어떤 이벤트가 인터럽트를 생성해야 하는지 활성화할 수 있습니다.
SIK_CONNECTED(연결이 설정됨)
SIK_DISCONNECTED(단절)
SIK_RECEIVED(데이터 도착)
SIK_TIMEOUT(시간 초과)
이는 다음을 통해 구성됩니다 ctlsocket(socket, CS_SET_INTMASK, &mask).
참고: 이 SIK_SENT플래그는 어떤 경우 불안정성을 유발하는 것으로 밝혀졌으며 권장되지 않습니다.
칩 레벨 인터럽트 활성화
소켓별 마스크 후, 를 통해 WIZnet 칩의 전체 인터럽트 시스템을 활성화합니다. ctlwizchip(CW_SET_INTRMASK, &chip_intr)여기서 는 chip_intr허용되는 소켓 인터럽트를 나타내는 비트마스크입니다. 활성화된 소켓 이벤트가 발생하면 칩의 INT 핀이 Low로 전환됩니다.
RP2040 / GPIO 인터럽트 처리
RP2040(예: Raspberry Pi Pico) 측에서는 INT 핀(W6300의 경우 INT는 보드의 GPIO 15에 연결됨)에 GPIO 인터럽트를 설정합니다 gpio_set_irq_enabled_with_callback(...). INT 핀이 하강 에지(하강 에지)로 전환되면 콜백이 interrupt_callback호출됩니다.
인터럽트 콜백 로직
인터럽트 핸들러 내부에서, 각 소켓에 대해 ctlsocket(sock, CS_GET_INTERRUPT, &sock_intr)발생한 이벤트를 검색하기 위해 호출합니다. 그런 다음 SIK_CONNECTED, SIK_RECEIVED, 등이 플래그 지정되었는지 여부에 따라 로직을 분기하고, 업데이트(예: 페이지 상태 새로 고침, 절전 모드 해제)를 수행하고, 마지막으로 를 사용하여 인터럽트 플래그를 지울 수 있습니다 ctlsocket(sock, CS_CLR_INTERRUPT, &sock_intr).
통합 예제 코드
문서에는 네트워크 설정, HTTP 서버 초기화, 인터럽트 등록 및 메인 루프를 결합한 전체 C 코드 예제가 포함되어 있습니다. 에서 main()초기화 후 코드는 httpServer_run(i)루프를 반복적으로 호출하지만, 실제 이벤트 반응은 무거운 폴링이 아닌 인터럽트를 통해 발생합니다.
행동 및 혜택
코드가 계속해서 소켓을 폴링할 필요가 없으므로 CPU 사용 효율성이 높아집니다.
들어오는 HTTP 요청이나 연결 끊김에 대한 적절한 대응.
절전 모드나 저전력 모드와의 통합을 가능하게 합니다. 시스템은 관련 소켓 이벤트에서 깨어날 수 있습니다.
인터럽트 핸들러에서 네트워크 이벤트와 애플리케이션 로직을 명확하게 분리합니다.
