Skip to content

Commit 848919d

Browse files
committed
Adding pico probe from official Raspberry pi
1 parent 7cc8889 commit 848919d

15 files changed

+1125
-0
lines changed

pico-probe/CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
include(pico_sdk_import.cmake)
4+
5+
project(picoprobe)
6+
7+
pico_sdk_init()
8+
9+
add_executable(picoprobe
10+
src/led.c
11+
src/main.c
12+
src/usb_descriptors.c
13+
src/probe.c
14+
src/cdc_uart.c
15+
src/get_serial.c
16+
)
17+
18+
if (DEFINED ENV{PICOPROBE_LED})
19+
message("PICOPROBE_LED is defined as " $ENV{PICOPROBE_LED})
20+
target_compile_definitions(picoprobe PRIVATE PICOPROBE_LED=$ENV{PICOPROBE_LED})
21+
endif()
22+
23+
set(DBG_PIN_COUNT=4)
24+
25+
pico_generate_pio_header(picoprobe ${CMAKE_CURRENT_LIST_DIR}/src/probe.pio)
26+
27+
target_include_directories(picoprobe PRIVATE src)
28+
29+
target_link_libraries(picoprobe PRIVATE pico_stdlib pico_unique_id tinyusb_device tinyusb_board hardware_pio)
30+
31+
pico_add_extra_outputs(picoprobe)

pico-probe/pico_sdk_import.cmake

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
2+
3+
# This can be dropped into an external project to help locate this SDK
4+
# It should be include()ed prior to project()
5+
6+
# todo document
7+
8+
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
9+
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
10+
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
11+
endif ()
12+
13+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
14+
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
15+
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
16+
endif ()
17+
18+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
19+
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
20+
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
21+
endif ()
22+
23+
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the PICO SDK")
24+
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO SDK from git if not otherwise locatable")
25+
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
26+
27+
if (NOT PICO_SDK_PATH)
28+
if (PICO_SDK_FETCH_FROM_GIT)
29+
include(FetchContent)
30+
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
31+
if (PICO_SDK_FETCH_FROM_GIT_PATH)
32+
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
33+
endif ()
34+
FetchContent_Declare(
35+
pico_sdk
36+
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
37+
GIT_TAG master
38+
)
39+
if (NOT pico_sdk)
40+
message("Downloading PICO SDK")
41+
FetchContent_Populate(pico_sdk)
42+
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
43+
endif ()
44+
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
45+
else ()
46+
message(FATAL_ERROR
47+
"PICO SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
48+
)
49+
endif ()
50+
endif ()
51+
52+
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
53+
if (NOT EXISTS ${PICO_SDK_PATH})
54+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
55+
endif ()
56+
57+
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
58+
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
59+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the PICO SDK")
60+
endif ()
61+
62+
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the PICO SDK" FORCE)
63+
64+
include(${PICO_SDK_INIT_CMAKE_FILE})

pico-probe/src/cdc_uart.c

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <pico/stdlib.h>
27+
28+
#include "tusb.h"
29+
30+
#include "picoprobe_config.h"
31+
32+
void cdc_uart_init(void) {
33+
gpio_set_function(PICOPROBE_UART_TX, GPIO_FUNC_UART);
34+
gpio_set_function(PICOPROBE_UART_RX, GPIO_FUNC_UART);
35+
uart_init(PICOPROBE_UART_INTERFACE, PICOPROBE_UART_BAUDRATE);
36+
}
37+
38+
#define MAX_UART_PKT 64
39+
void cdc_task(void) {
40+
uint8_t rx_buf[MAX_UART_PKT];
41+
uint8_t tx_buf[MAX_UART_PKT];
42+
43+
// Consume uart fifo regardless even if not connected
44+
uint rx_len = 0;
45+
while(uart_is_readable(PICOPROBE_UART_INTERFACE) && (rx_len < MAX_UART_PKT)) {
46+
rx_buf[rx_len++] = uart_getc(PICOPROBE_UART_INTERFACE);
47+
}
48+
49+
if (tud_cdc_connected()) {
50+
// Do we have anything to display on the host's terminal?
51+
if (rx_len) {
52+
for (uint i = 0; i < rx_len; i++) {
53+
tud_cdc_write_char(rx_buf[i]);
54+
}
55+
tud_cdc_write_flush();
56+
}
57+
58+
if (tud_cdc_available()) {
59+
// Is there any data from the host for us to tx
60+
uint tx_len = tud_cdc_read(tx_buf, sizeof(tx_buf));
61+
uart_write_blocking(PICOPROBE_UART_INTERFACE, tx_buf, tx_len);
62+
}
63+
}
64+
}
65+
66+
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* line_coding) {
67+
picoprobe_info("New baud rate %d\n", line_coding->bit_rate);
68+
uart_init(PICOPROBE_UART_INTERFACE, line_coding->bit_rate);
69+
}

pico-probe/src/cdc_uart.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef CDC_UART_H
27+
#define CDC_UART_H
28+
29+
void cdc_uart_init(void);
30+
void cdc_task(void);
31+
32+
#endif

pico-probe/src/get_serial.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Federico Zuccardi Merli
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <stdint.h>
27+
#include "pico.h"
28+
#include "pico/unique_id.h"
29+
#include "get_serial.h"
30+
31+
/* C string for iSerialNumber in USB Device Descriptor, two chars per byte + terminating NUL */
32+
char usb_serial[PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2 + 1];
33+
34+
/* Why a uint8_t[8] array inside a struct instead of an uint64_t an inquiring mind might wonder */
35+
static pico_unique_board_id_t uID;
36+
37+
void usb_serial_init(void)
38+
{
39+
pico_get_unique_board_id(&uID);
40+
41+
for (int i = 0; i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2; i++)
42+
{
43+
/* Byte index inside the uid array */
44+
int bi = i / 2;
45+
/* Use high nibble first to keep memory order (just cosmetics) */
46+
uint8_t nibble = (uID.id[bi] >> 4) & 0x0F;
47+
uID.id[bi] <<= 4;
48+
/* Binary to hex digit */
49+
usb_serial[i] = nibble < 10 ? nibble + '0' : nibble + 'A' - 10;
50+
}
51+
}

pico-probe/src/get_serial.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Federico Zuccardi Merli
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef GET_SERIAL_H_
27+
#define GET_SERIAL_H_
28+
29+
/* Contains unique serial number string (NUL terminated) after call to init_usb_serial */
30+
extern char usb_serial[];
31+
32+
/* Fills unique_serial with the flash unique id */
33+
extern void usb_serial_init(void);
34+
35+
#endif

pico-probe/src/led.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 a-pushkin on GitHub
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <pico/stdlib.h>
27+
#include <stdint.h>
28+
29+
#include "picoprobe_config.h"
30+
31+
#define LED_COUNT_SHIFT 14
32+
#define LED_COUNT_MAX 5 * (1 << LED_COUNT_SHIFT)
33+
34+
static uint32_t led_count;
35+
36+
void led_init(void) {
37+
led_count = 0;
38+
39+
gpio_init(PICOPROBE_LED);
40+
gpio_set_dir(PICOPROBE_LED, GPIO_OUT);
41+
gpio_put(PICOPROBE_LED, 1);
42+
}
43+
44+
45+
46+
void led_task(void) {
47+
if (led_count != 0) {
48+
--led_count;
49+
gpio_put(PICOPROBE_LED, !((led_count >> LED_COUNT_SHIFT) & 1));
50+
}
51+
}
52+
53+
void led_signal_activity(uint total_bits) {
54+
if (led_count == 0) {
55+
gpio_put(PICOPROBE_LED, 0);
56+
}
57+
58+
if (led_count < LED_COUNT_MAX) {
59+
led_count += total_bits;
60+
}
61+
}

pico-probe/src/led.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 a-pushkin on GitHub
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef LED_H
27+
#define LED_H
28+
29+
void led_init(void);
30+
void led_task(void);
31+
void led_signal_activity(uint total_bits);
32+
33+
#endif

0 commit comments

Comments
 (0)