Compare commits
11 Commits
5ae8e41885
...
7c479aadb2
Author | SHA1 | Date |
---|---|---|
mkerna | 7c479aadb2 | |
supsm | 28f4b951a7 | |
Gabriel Marcano | 53556e5254 | |
Trevor | aef4834b49 | |
mkerna | 2f088935f2 | |
mkerna | 5a72b25472 | |
mkerna | 8e17b7baee | |
mkerna | ddb810edde | |
mkerna | e6daba9ea2 | |
mkerna | 0fbb30ea08 | |
mkerna | de4ef55738 |
|
@ -5,13 +5,14 @@
|
|||
See [Getting Started with the Raspberry Pi Pico](https://rptl.io/pico-get-started) and the README in the [pico-sdk](https://github.com/raspberrypi/pico-sdk) for information
|
||||
on getting up and running.
|
||||
|
||||
### First Examples
|
||||
### First Examples
|
||||
|
||||
App|Description | Link to prebuilt UF2
|
||||
---|---|---
|
||||
[hello_serial](hello_world/serial) | The obligatory Hello World program for Pico (Output over serial version) |
|
||||
[hello_usb](hello_world/usb) | The obligatory Hello World program for Pico (Output over USB version) | https://rptl.io/pico-hello-usb
|
||||
[blink](blink) | Blink an LED on and off. | https://rptl.io/pico-blink
|
||||
[blink](blink) | Blink an LED on and off. Pico W should use [picow_blink](pico_w/wifi/blink). | https://rptl.io/pico-blink
|
||||
[picow_blink](pico_w/wifi/blink) | Blinks the Pico W on-board LED (which is connected via the WiFi chip). | http://rptl.io/pico-w-blink
|
||||
|
||||
### ADC
|
||||
|
||||
|
@ -142,6 +143,7 @@ App|Description
|
|||
[picow_freertos_iperf_server_sys](pico_w/wifi/freertos/iperf) | Runs an "iperf" server for WiFi speed testing under FreeRTOS in NO_SYS=0 (i.e. full FreeRTOS integration) mode. The LED is blinked in another task
|
||||
[picow_freertos_ping_nosys](pico_w/wifi/freertos/ping) | Runs the lwip-contrib/apps/ping test app under FreeRTOS in NO_SYS=1 mode.
|
||||
[picow_freertos_ping_sys](pico_w/wifi/freertos/ping) | Runs the lwip-contrib/apps/ping test app under FreeRTOS in NO_SYS=0 (i.e. full FreeRTOS integration) mode. The test app uses the lwIP _socket_ API in this case.
|
||||
[picow_freertos_ntp_client_socket](pico_w/wifi/freertos/ntp_client_socket) | Connects to an NTP server using the LwIP Socket API with FreeRTOS in NO_SYS=0 (i.e. full FreeRTOS integration) mode.
|
||||
|
||||
### Pico W Bluetooth
|
||||
|
||||
|
|
|
@ -5,4 +5,5 @@ else()
|
|||
|
||||
add_subdirectory(iperf)
|
||||
add_subdirectory(ping)
|
||||
endif()
|
||||
add_subdirectory(ntp_client_socket)
|
||||
endif()
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
add_executable(picow_freertos_ntp_client_socket
|
||||
picow_freertos_ntp_client_socket.c
|
||||
)
|
||||
target_compile_definitions(picow_freertos_ntp_client_socket PRIVATE
|
||||
WIFI_SSID=\"${WIFI_SSID}\"
|
||||
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
|
||||
NO_SYS=0 # don't want NO_SYS (generally this would be in your lwipopts.h)
|
||||
LWIP_SOCKET=1 # we need the socket API (generally this would be in your lwipopts.h)
|
||||
)
|
||||
target_include_directories(picow_freertos_ntp_client_socket PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${CMAKE_CURRENT_LIST_DIR}/.. # for our common FreeRTOSConfig
|
||||
${CMAKE_CURRENT_LIST_DIR}/../.. # for our common lwipopts
|
||||
)
|
||||
target_link_libraries(picow_freertos_ntp_client_socket
|
||||
pico_cyw43_arch_lwip_sys_freertos
|
||||
pico_stdlib
|
||||
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
|
||||
)
|
||||
pico_add_extra_outputs(picow_freertos_ntp_client_socket)
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* FreeRTOS V202111.00
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://www.FreeRTOS.org
|
||||
* http://aws.amazon.com/freertos
|
||||
*
|
||||
* 1 tab == 4 spaces!
|
||||
*/
|
||||
|
||||
#ifndef FREERTOS_CONFIG_H
|
||||
#define FREERTOS_CONFIG_H
|
||||
|
||||
// This example uses a common include to avoid repetition
|
||||
#include "FreeRTOSConfig_examples_common.h"
|
||||
|
||||
#endif /* FREERTOS_CONFIG_H */
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef _LWIPOPTS_H
|
||||
#define _LWIPOPTS_H
|
||||
|
||||
// Generally you would define your own explicit list of lwIP options
|
||||
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html)
|
||||
//
|
||||
// This example uses a common include to avoid repetition
|
||||
#include "lwipopts_examples_common.h"
|
||||
|
||||
#if !NO_SYS
|
||||
#define TCPIP_THREAD_STACKSIZE 1024
|
||||
#define DEFAULT_THREAD_STACKSIZE 1024
|
||||
#define DEFAULT_RAW_RECVMBOX_SIZE 8
|
||||
#define DEFAULT_UDP_RECVMBOX_SIZE 8
|
||||
#define DEFAULT_TCP_RECVMBOX_SIZE 8
|
||||
#define TCPIP_MBOX_SIZE 8
|
||||
#define LWIP_TIMEVAL_PRIVATE 0
|
||||
|
||||
// not necessary, can be done either way
|
||||
#define LWIP_TCPIP_CORE_LOCKING_INPUT 1
|
||||
|
||||
#define LWIP_SO_RCVTIMEO 1
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,189 @@
|
|||
/**
|
||||
* Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "pico/cyw43_arch.h"
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
#include "lwip/ip4_addr.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/udp.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
#ifndef RUN_FREERTOS_ON_CORE
|
||||
#define RUN_FREERTOS_ON_CORE 0
|
||||
#endif
|
||||
|
||||
#define TEST_TASK_PRIORITY ( tskIDLE_PRIORITY + 1UL )
|
||||
|
||||
#define NTP_SERVER ("pool.ntp.org")
|
||||
#define NTP_MSG_LEN (48)
|
||||
#define NTP_PORT (123)
|
||||
// seconds between 1 Jan 1900 and 1 Jan 1970
|
||||
#define NTP_DELTA (2208988800)
|
||||
#define ntpEST_TIME (30 * 1000)
|
||||
#define NTP_FAIL_TIME (10)
|
||||
|
||||
void main_task(__unused void *params) {
|
||||
if (cyw43_arch_init()) {
|
||||
printf("failed to initialise\n");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
cyw43_arch_enable_sta_mode();
|
||||
printf("Connecting to Wi-Fi...\n");
|
||||
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
|
||||
printf("failed to connect.\n");
|
||||
vTaskDelete(NULL);
|
||||
} else {
|
||||
printf("Connected.\n");
|
||||
}
|
||||
|
||||
while(true) {
|
||||
const struct addrinfo ntp_info = {
|
||||
.ai_flags = 0,
|
||||
.ai_family = AF_UNSPEC,
|
||||
.ai_socktype = SOCK_DGRAM,
|
||||
.ai_protocol = 0,
|
||||
};
|
||||
|
||||
struct addrinfo *dns_result = NULL;
|
||||
printf("Trying DNS...\n");
|
||||
int err = getaddrinfo(NTP_SERVER, "123", &ntp_info, &dns_result);
|
||||
if (err != 0)
|
||||
{
|
||||
printf("DNS lookup failed with error: %d\n", err);
|
||||
continue;
|
||||
}
|
||||
#ifndef INET6_ADDRSTRLEN
|
||||
char address[INET_ADDRSTRLEN] = {0};
|
||||
#else
|
||||
char address[INET6_ADDRSTRLEN] = {0};
|
||||
#endif
|
||||
ip_addr_t ip_addr;
|
||||
#if LWIP_IPV4
|
||||
if (dns_result->ai_addr->sa_family == AF_INET) {
|
||||
const struct sockaddr_in *sock_addr = (struct sockaddr_in*)dns_result->ai_addr;
|
||||
inet_addr_to_ip4addr(ip_2_ip4(&ip_addr), &sock_addr->sin_addr);
|
||||
IP_SET_TYPE(&ip_addr, IPADDR_TYPE_V4);
|
||||
}
|
||||
#endif
|
||||
#if LWIP_IPV6
|
||||
if (dns_result->ai_addr->sa_family == AF_INET6) {
|
||||
const struct sockaddr_in6 *sock_addr = (struct sockaddr_in6*)dns_result->ai_addr;
|
||||
inet6_addr_to_ip6addr(ip_2_ip6(&ip_addr), &sock_addr->sin6_addr);
|
||||
IP_SET_TYPE(&ip_addr, IPADDR_TYPE_V6);
|
||||
}
|
||||
#endif
|
||||
inet_ntop(dns_result->ai_family, &ip_addr, address, sizeof(address));
|
||||
printf("Got DNS response: %s\n", address);
|
||||
|
||||
int sock = socket(dns_result->ai_family, dns_result->ai_socktype, dns_result->ai_protocol);
|
||||
if (sock == -1)
|
||||
{
|
||||
printf("Failed to allocate socket");
|
||||
freeaddrinfo(dns_result);
|
||||
continue;
|
||||
}
|
||||
|
||||
struct timeval timeout = {
|
||||
.tv_sec = NTP_FAIL_TIME,
|
||||
};
|
||||
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
err = connect(sock, dns_result->ai_addr, dns_result->ai_addrlen);
|
||||
freeaddrinfo(dns_result);
|
||||
if (err == -1)
|
||||
{
|
||||
printf("Failed to connect to NTP server");
|
||||
close(sock);
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned char request[NTP_MSG_LEN] = {0x1b, 0};
|
||||
int amount = send(sock, request, NTP_MSG_LEN, 0);
|
||||
if (amount != NTP_MSG_LEN)
|
||||
{
|
||||
printf("We were unable to send the complete NTP request");
|
||||
close(sock);
|
||||
continue;
|
||||
}
|
||||
|
||||
amount = recv(sock, request, NTP_MSG_LEN, 0);
|
||||
close(sock);
|
||||
if (amount != NTP_MSG_LEN)
|
||||
{
|
||||
printf("We did not receive a complete NTP response");
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t mode = request[0] & 0x7;
|
||||
uint8_t stratum = request[1];
|
||||
|
||||
// Check the result
|
||||
if (amount == NTP_MSG_LEN &&
|
||||
mode == 0x4 &&
|
||||
stratum != 0)
|
||||
{
|
||||
uint8_t seconds_buf[4] = {};
|
||||
memcpy(seconds_buf, request + 40, 4);
|
||||
uint32_t seconds_since_1900 = seconds_buf[0] << 24 | seconds_buf[1] << 16 | seconds_buf[2] << 8 | seconds_buf[3];
|
||||
uint32_t seconds_since_1970 = seconds_since_1900 - NTP_DELTA;
|
||||
time_t epoch = seconds_since_1970;
|
||||
struct tm *utc = gmtime(&epoch);
|
||||
printf("got ntp response: %02d/%02d/%04d %02d:%02d:%02d\n", utc->tm_mday, utc->tm_mon + 1, utc->tm_year + 1900,
|
||||
utc->tm_hour, utc->tm_min, utc->tm_sec);
|
||||
}
|
||||
|
||||
// This assumes one tick is one ms, which is true for the default configuration
|
||||
vTaskDelay(ntpEST_TIME);
|
||||
}
|
||||
|
||||
cyw43_arch_deinit();
|
||||
}
|
||||
|
||||
void vLaunch( void) {
|
||||
TaskHandle_t task;
|
||||
xTaskCreate(main_task, "TestMainThread", 4096, NULL, TEST_TASK_PRIORITY, &task);
|
||||
|
||||
#if NO_SYS && configUSE_CORE_AFFINITY && configNUM_CORES > 1
|
||||
// we must bind the main task to one core (well at least while the init is called)
|
||||
// (note we only do this in NO_SYS mode, because cyw43_arch_freertos
|
||||
// takes care of it otherwise)
|
||||
vTaskCoreAffinitySet(task, 1);
|
||||
#endif
|
||||
|
||||
/* Start the tasks and timer running. */
|
||||
vTaskStartScheduler();
|
||||
}
|
||||
|
||||
int main( void )
|
||||
{
|
||||
stdio_init_all();
|
||||
|
||||
/* Configure the hardware ready to run the demo. */
|
||||
const char *rtos_name;
|
||||
#if ( portSUPPORT_SMP == 1 )
|
||||
rtos_name = "FreeRTOS SMP";
|
||||
#else
|
||||
rtos_name = "FreeRTOS";
|
||||
#endif
|
||||
|
||||
#if ( portSUPPORT_SMP == 1 ) && ( configNUM_CORES == 2 )
|
||||
printf("Starting %s on both cores:\n", rtos_name);
|
||||
vLaunch();
|
||||
#elif ( RUN_FREERTOS_ON_CORE == 1 )
|
||||
printf("Starting %s on core 1:\n", rtos_name);
|
||||
multicore_launch_core1(vLaunch);
|
||||
while (true);
|
||||
#else
|
||||
printf("Starting %s on core 0:\n", rtos_name);
|
||||
vLaunch();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
|
@ -28,6 +28,8 @@
|
|||
#define PIN_RESET 4
|
||||
#define PIN_BL 5
|
||||
|
||||
#define SPI_CLOCK_POLARITY 0 // default to 0, some displays require polarity 1
|
||||
|
||||
#define SERIAL_CLK_DIV 1.f
|
||||
|
||||
#ifndef M_PI
|
||||
|
@ -90,7 +92,7 @@ int main() {
|
|||
PIO pio = pio0;
|
||||
uint sm = 0;
|
||||
uint offset = pio_add_program(pio, &st7789_lcd_program);
|
||||
st7789_lcd_program_init(pio, sm, offset, PIN_DIN, PIN_CLK, SERIAL_CLK_DIV);
|
||||
st7789_lcd_program_init(pio, sm, offset, SPI_CLOCK_POLARITY, PIN_DIN, PIN_CLK, SERIAL_CLK_DIV);
|
||||
|
||||
gpio_init(PIN_CS);
|
||||
gpio_init(PIN_DC);
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
// but we are using a threshold of 8 here (consume 1 byte from each FIFO entry
|
||||
// and discard the remainder) to make things easier for software on the other side
|
||||
|
||||
static inline void st7789_lcd_program_init(PIO pio, uint sm, uint offset, uint data_pin, uint clk_pin, float clk_div) {
|
||||
static inline void st7789_lcd_program_init(PIO pio, uint sm, uint offset, bool cpol, uint data_pin, uint clk_pin, float clk_div) {
|
||||
pio_gpio_init(pio, data_pin);
|
||||
pio_gpio_init(pio, clk_pin);
|
||||
pio_sm_set_consecutive_pindirs(pio, sm, data_pin, 1, true);
|
||||
|
@ -33,6 +33,11 @@ static inline void st7789_lcd_program_init(PIO pio, uint sm, uint offset, uint d
|
|||
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
|
||||
sm_config_set_clkdiv(&c, clk_div);
|
||||
sm_config_set_out_shift(&c, false, true, 8);
|
||||
|
||||
// The pin muxes can be configured to invert the output (among other things),
|
||||
// and this is a cheesy way to allow changing polarity
|
||||
gpio_set_outover(clk_pin, cpol ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL);
|
||||
|
||||
pio_sm_init(pio, sm, offset, &c);
|
||||
pio_sm_set_enabled(pio, sm, true);
|
||||
}
|
||||
|
|
|
@ -488,6 +488,9 @@ static void usb_handle_buff_status() {
|
|||
* @brief USB interrupt handler
|
||||
*
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/// \tag::isr_setup_packet[]
|
||||
void isr_usbctrl(void) {
|
||||
// USB interrupt handler
|
||||
|
@ -520,6 +523,9 @@ void isr_usbctrl(void) {
|
|||
panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled));
|
||||
}
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief EP0 in transfer complete. Either finish the SET_ADDRESS process, or receive a zero
|
||||
|
|
Loading…
Reference in New Issue