From c545abd5ab46fce070b2ff6e7edc66fd88fad5e1 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Thu, 3 Jul 2025 18:28:12 +0200 Subject: [PATCH 1/4] switch pico-sdk branch to the functioning one --- pico-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-sdk b/pico-sdk index ee68c78..214c243 160000 --- a/pico-sdk +++ b/pico-sdk @@ -1 +1 @@ -Subproject commit ee68c78d0afae2b69c03ae1a72bf5cc267a2d94c +Subproject commit 214c24386ada42d9fff7b799739c6ea24ae179c0 From cdccb0843c4fbc81af83add3ba7ce625bd3efca4 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Thu, 3 Jul 2025 18:29:53 +0200 Subject: [PATCH 2/4] pico: init, read encoder and send data to server --- pico/.gitignore | 2 + pico/CMakeLists.txt | 15 ++++ pico/Makefile | 11 +++ pico/pico_sdk_import.cmake | 121 ++++++++++++++++++++++++++++ pico/src/lwipopts.h | 92 +++++++++++++++++++++ pico/src/main.c | 158 +++++++++++++++++++++++++++++++++++++ 6 files changed, 399 insertions(+) create mode 100644 pico/.gitignore create mode 100644 pico/CMakeLists.txt create mode 100755 pico/Makefile create mode 100644 pico/pico_sdk_import.cmake create mode 100644 pico/src/lwipopts.h create mode 100644 pico/src/main.c diff --git a/pico/.gitignore b/pico/.gitignore new file mode 100644 index 0000000..acf56c4 --- /dev/null +++ b/pico/.gitignore @@ -0,0 +1,2 @@ +bin +.vscode diff --git a/pico/CMakeLists.txt b/pico/CMakeLists.txt new file mode 100644 index 0000000..dddb817 --- /dev/null +++ b/pico/CMakeLists.txt @@ -0,0 +1,15 @@ +set(PROJECT wind_station) +cmake_minimum_required(VERSION 3.12) +set(PICO_BOARD pico_w) +include(pico_sdk_import.cmake) +project(${PROJECT} C CXX) +pico_sdk_init() +add_executable(${PROJECT} src/main.c) +target_include_directories(${PROJECT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src) +target_link_libraries(${PROJECT} + pico_stdlib + pico_cyw43_arch_none + pico_cyw43_arch_lwip_threadsafe_background + hardware_adc +) +pico_add_extra_outputs(${PROJECT}) diff --git a/pico/Makefile b/pico/Makefile new file mode 100755 index 0000000..4c14657 --- /dev/null +++ b/pico/Makefile @@ -0,0 +1,11 @@ + +build_thing: git_deps + cmake -B bin -DPICO_SDK_PATH=../pico-sdk -DCMAKE_BUILD_TYPE=Debug + make -j8 -C bin +# then manually copy bin/thing.uf2 to the pico + +git_deps: + cd ../pico-sdk && git submodule update --init lib/cyw43-driver lib/lwip + +clean: + rm -rf bin/* diff --git a/pico/pico_sdk_import.cmake b/pico/pico_sdk_import.cmake new file mode 100644 index 0000000..d493cc2 --- /dev/null +++ b/pico/pico_sdk_import.cmake @@ -0,0 +1,121 @@ +# This is a copy of /external/pico_sdk_import.cmake + +# This can be dropped into an external project to help locate this SDK +# It should be include()ed prior to project() + +# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the +# following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following +# disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) + set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) + message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) + set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) + message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) + set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) + message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG)) + set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG}) + message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')") +endif () + +if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG) + set(PICO_SDK_FETCH_FROM_GIT_TAG "master") + message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG") +endif() + +set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") +set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") +set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") +set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK") + +if (NOT PICO_SDK_PATH) + if (PICO_SDK_FETCH_FROM_GIT) + include(FetchContent) + set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) + if (PICO_SDK_FETCH_FROM_GIT_PATH) + get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") + endif () + FetchContent_Declare( + pico_sdk + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + ) + + if (NOT pico_sdk) + message("Downloading Raspberry Pi Pico SDK") + # GIT_SUBMODULES_RECURSE was added in 3.17 + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + GIT_SUBMODULES_RECURSE FALSE + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + else () + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + endif () + + set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) + endif () + set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) + else () + message(FATAL_ERROR + "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." + ) + endif () +endif () + +get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") +if (NOT EXISTS ${PICO_SDK_PATH}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") +endif () + +set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) +if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") +endif () + +set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) + +include(${PICO_SDK_INIT_CMAKE_FILE}) diff --git a/pico/src/lwipopts.h b/pico/src/lwipopts.h new file mode 100644 index 0000000..129d42b --- /dev/null +++ b/pico/src/lwipopts.h @@ -0,0 +1,92 @@ +#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H +#define _LWIPOPTS_EXAMPLE_COMMONH_H + + +// Common settings used in most of the pico_w examples +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) + +// allow override in some examples +#ifndef NO_SYS +#define NO_SYS 1 +#endif +// allow override in some examples +#ifndef LWIP_SOCKET +#define LWIP_SOCKET 0 +#endif +#if PICO_CYW43_ARCH_POLL +#define MEM_LIBC_MALLOC 1 +#else +// MEM_LIBC_MALLOC is incompatible with non polling versions +#define MEM_LIBC_MALLOC 0 +#endif +#define MEM_ALIGNMENT 4 +#ifndef MEM_SIZE +#define MEM_SIZE 4000 +#endif +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 +// #define ETH_PAD_SIZE 2 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 + +#ifndef NDEBUG +#define LWIP_DEBUG 1 +#define LWIP_STATS 1 +#define LWIP_STATS_DISPLAY 1 +#endif + +#define ETHARP_DEBUG LWIP_DBG_OFF +#define NETIF_DEBUG LWIP_DBG_OFF +#define PBUF_DEBUG LWIP_DBG_OFF +#define API_LIB_DEBUG LWIP_DBG_OFF +#define API_MSG_DEBUG LWIP_DBG_OFF +#define SOCKETS_DEBUG LWIP_DBG_OFF +#define ICMP_DEBUG LWIP_DBG_OFF +#define INET_DEBUG LWIP_DBG_OFF +#define IP_DEBUG LWIP_DBG_OFF +#define IP_REASS_DEBUG LWIP_DBG_OFF +#define RAW_DEBUG LWIP_DBG_OFF +#define MEM_DEBUG LWIP_DBG_OFF +#define MEMP_DEBUG LWIP_DBG_OFF +#define SYS_DEBUG LWIP_DBG_OFF +#define TCP_DEBUG LWIP_DBG_OFF +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#define TCP_WND_DEBUG LWIP_DBG_OFF +#define TCP_FR_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#define TCP_RST_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF +#define TCPIP_DEBUG LWIP_DBG_OFF +#define PPP_DEBUG LWIP_DBG_OFF +#define SLIP_DEBUG LWIP_DBG_OFF +#define DHCP_DEBUG LWIP_DBG_OFF + +#endif /* __LWIPOPTS_H__ */ \ No newline at end of file diff --git a/pico/src/main.c b/pico/src/main.c new file mode 100644 index 0000000..e40c4e9 --- /dev/null +++ b/pico/src/main.c @@ -0,0 +1,158 @@ +#include +#include "pico/stdlib.h" +#include "pico/cyw43_arch.h" +#include "pico/cyw43_driver.h" +#include "hardware/adc.h" + +#include "lwip/tcp.h" +#include "lwip/apps/http_client.h" + +#define u32 uint32_t +#define u16 uint16_t +#define u8 uint8_t + +char ssid[] = "TODO"; +char pass[] = "secret"; + +const ip_addr_t SERVER_IP = IPADDR4_INIT_BYTES(192, 168, 0, 108); +#define SERVER_PORT 25564 + + +#define SET_LED(state) cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, state) + +struct tcp_pcb *tcp_controller; +#define ENCODER_BITS 3 +u8 rotary_encoder_directon; +u8 rotary_encoder_bits; +u8 rotary_encoder_bit[ENCODER_BITS]; +u16 rotary_encoder_raw[ENCODER_BITS]; +u16 rotary_encoder_min[ENCODER_BITS]; +u16 rotary_encoder_max[ENCODER_BITS]; +u16 rotary_encoder_high[ENCODER_BITS]; +u16 rotary_encoder_low[ENCODER_BITS]; +// static const u32 encoder_adc_pins[ENCODER_BITS] = {26, 27, 28}; + + +void update_raw_values() { + for (int b = 0; b < ENCODER_BITS; b++) { + adc_select_input(b); + rotary_encoder_raw[b] = adc_read(); + } +} + +void calibrate_brightness() { + for (int b = 0; b < ENCODER_BITS; b++) { + rotary_encoder_min[b] = 0xffff; + rotary_encoder_max[b] = 0; + } + for (int i = 0; i < 300; i++) { + SET_LED(i & 2); + update_raw_values(); + for (int b = 0; b < ENCODER_BITS; b++) { + if (rotary_encoder_raw[b] > rotary_encoder_max[b]) + rotary_encoder_max[b] = rotary_encoder_raw[b]; + else if (rotary_encoder_raw[b] < rotary_encoder_min[b]) + rotary_encoder_min[b] = rotary_encoder_raw[b]; + } + sleep_ms(25); + } + for (int b = 0; b < ENCODER_BITS; b++) { + u16 diff = rotary_encoder_max[b] - rotary_encoder_min[b]; + rotary_encoder_low[b] = rotary_encoder_min[b] + diff / 3; + rotary_encoder_high[b] = rotary_encoder_max[b] - diff / 3; + } + SET_LED(1); + sleep_ms(500); + SET_LED(0); +} + +void update_encoder_value() { + update_raw_values(); + for (int b = 0; b < ENCODER_BITS; b++) { + if (rotary_encoder_raw[b] > rotary_encoder_high[b]) + rotary_encoder_bit[b] = 1; + else if (rotary_encoder_raw[b] < rotary_encoder_low[b]) + rotary_encoder_bit[b] = 0; + } + rotary_encoder_bits = 0; + for (int b = 0; b < ENCODER_BITS; b++) { + rotary_encoder_bits <<= 1; + rotary_encoder_bits |= !rotary_encoder_bit[b]; // inverse to make white = 0 + } + + // const u8 reverse_gray_code[1 << ENCODER_BITS] = {0, 1, 3, 2, 7, 6, 4, 5}; + // gray code: 0 1 5 7 3 2 6 4 + // index: 0 1 2 3 4 5 6 7 + // reverse : 0 1 5 4 7 2 6 3 + const u8 reverse_gray_code[1 << ENCODER_BITS] = {0, 1, 5, 4, 7, 2, 6, 3}; + rotary_encoder_directon = reverse_gray_code[rotary_encoder_bits]; +} + +static err_t connected_fn(void *arg, struct tcp_pcb *tpcb, err_t err) { + return ERR_OK; +} + +void ensure_server_connection() { + while (tcp_controller->state != ESTABLISHED) { + int err = tcp_connect(tcp_controller, &SERVER_IP, SERVER_PORT, connected_fn); + while (err++) { + SET_LED(1); + sleep_ms(150); + SET_LED(0); + sleep_ms(300); + } + } +} + +void send_data() { + ensure_server_connection(); + u8 buf[] = "direction: X=X\n"; + buf[11] = rotary_encoder_bits + '0'; + buf[13] = rotary_encoder_directon + '0'; + int err = tcp_write(tcp_controller, buf, 15, 0); +} + + +int main() { + adc_init(); + adc_gpio_init(26); + adc_gpio_init(27); + adc_gpio_init(28); + + if (cyw43_arch_init()) { + SET_LED(1); + sleep_ms(2000); + return -1; + } + cyw43_arch_enable_sta_mode(); + + int err = cyw43_arch_wifi_connect_timeout_ms(ssid, pass, CYW43_AUTH_WPA2_AES_PSK, 10000); + if (err) { + PICO_ERROR_BADAUTH; // jump to definition :) + while (err++) { + SET_LED(1); + sleep_ms(100); + SET_LED(0); + sleep_ms(100); + } + return -1; + } + + tcp_controller = tcp_new_ip_type(IPADDR_TYPE_V4); + ensure_server_connection(); + tcp_write(tcp_controller, "be gay do crime :3\n", 19, 0); + SET_LED(1); + sleep_ms(200); + update_raw_values(); + SET_LED(0); + + + // tcp_controller->state + calibrate_brightness(); + + while (true) { + update_encoder_value(); + send_data(); + sleep_ms(500); + } +} From 721a69e6cd43c91bbaf1a67833afb530bdec9a20 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Thu, 3 Jul 2025 18:35:21 +0200 Subject: [PATCH 3/4] put wifi and server config in a separate file --- pico/src/main.c | 14 ++++++-------- pico/src/wifi_cred.h | 8 ++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 pico/src/wifi_cred.h diff --git a/pico/src/main.c b/pico/src/main.c index e40c4e9..34ba1e5 100644 --- a/pico/src/main.c +++ b/pico/src/main.c @@ -3,20 +3,19 @@ #include "pico/cyw43_arch.h" #include "pico/cyw43_driver.h" #include "hardware/adc.h" - #include "lwip/tcp.h" -#include "lwip/apps/http_client.h" + +#include "wifi_cred.h" #define u32 uint32_t #define u16 uint16_t #define u8 uint8_t -char ssid[] = "TODO"; -char pass[] = "secret"; - -const ip_addr_t SERVER_IP = IPADDR4_INIT_BYTES(192, 168, 0, 108); -#define SERVER_PORT 25564 +char ssid[] = CONFIG_WIFI_SSID; +char pass[] = CONFIG_WIFI_PASSWORD; +const ip_addr_t SERVER_IP = CONFIG_SERVER_IP; +#define SERVER_PORT CONFIG_SERVER_PORT #define SET_LED(state) cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, state) @@ -80,7 +79,6 @@ void update_encoder_value() { rotary_encoder_bits |= !rotary_encoder_bit[b]; // inverse to make white = 0 } - // const u8 reverse_gray_code[1 << ENCODER_BITS] = {0, 1, 3, 2, 7, 6, 4, 5}; // gray code: 0 1 5 7 3 2 6 4 // index: 0 1 2 3 4 5 6 7 // reverse : 0 1 5 4 7 2 6 3 diff --git a/pico/src/wifi_cred.h b/pico/src/wifi_cred.h new file mode 100644 index 0000000..df80bbd --- /dev/null +++ b/pico/src/wifi_cred.h @@ -0,0 +1,8 @@ + +#define CONFIG_WIFI_SSID "Wifi network here" +#define CONFIG_WIFI_PASSWORD "password123" + +// local IP of the computer running the server software +// public IP would work too but requires port forwarding +#define CONFIG_SERVER_IP IPADDR4_INIT_BYTES(192, 168, 0, 108) +#define CONFIG_SERVER_PORT 13122 From 0e2400a47cb9944384a54a5f605fe77ec312e7d0 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Thu, 3 Jul 2025 18:36:06 +0200 Subject: [PATCH 4/4] add wifi note --- pico/src/wifi_cred.h | 1 + 1 file changed, 1 insertion(+) diff --git a/pico/src/wifi_cred.h b/pico/src/wifi_cred.h index df80bbd..dd1bf59 100644 --- a/pico/src/wifi_cred.h +++ b/pico/src/wifi_cred.h @@ -1,4 +1,5 @@ +// case sensitive, and must be 2.4GHz #define CONFIG_WIFI_SSID "Wifi network here" #define CONFIG_WIFI_PASSWORD "password123"