Skip to content

Commit 1d7b486

Browse files
committed
firmware v0.8.1
1 parent a4d3ba7 commit 1d7b486

13 files changed

+87
-90
lines changed

Makefile

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ all:
55
cd src/pico_build; make -j4
66
@echo "build pico_w"
77
mkdir -p src/pico_w_build
8-
cd src/pico_w_build; make -j4
8+
cd src/pico_w_build; make -
9+
#see fsfe reuse tool (https://git.fsfe.org/reuse/tool)
910
@echo "reuse (license) check"
10-
reuse lint
11+
pipx run reuse lint
1112

1213
# rebuild
1314
rebuild:
@@ -19,13 +20,3 @@ rebuild:
1920
rm -rf src/pico_w_build
2021
mkdir src/pico_w_build
2122
cd src/pico_w_build; cmake .. -DPICO_BOARD=pico_w; make -j4
22-
23-
#install fsfe reuse tool (https://git.fsfe.org/reuse/tool)
24-
# pre-conditions:
25-
# - Python 3.6+
26-
# - pip
27-
# install pre-conditions in Debian like linux distros:
28-
# - sudo apt install python3
29-
# - sudo apt install python3-pip
30-
reuse:
31-
pip3 install --user --upgrade reuse

README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ To mitigate some of the function setting issues the pico-cs command station is o
4949
- Connect the Raspberry Pi Pico to your PC via an USB cable
5050
- [Build](#build) the pico-cs firmware
5151
- Install firmware (Pico: cs.uf2, Pico W: cs_w.uf2) via BOOTSEL mode (see [Raspberry Pi Pico documentation](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html))
52-
- On macOS Ventura copying via drag&drop in Finder was broken but seems to work again with version 13.1 - anyway, copying the file via command line is always an option:
52+
- On macOS Ventura copying via drag&drop in Finder was broken but seems to work again with version 13.1 - anyway, copying the file via the command line is always an option:
5353
```
5454
cp -X cs.uf2 /Volumes/RPI-RP2/
5555
cp -X cs_w.uf2 /Volumes/RPI-RP2/
5656
```
57-
- Use a terminal emulation tool supporting serial over USB communication like the Serial Monotor of the [Arduino IDE](https://www.arduino.cc/en/software)
57+
- Use a terminal emulation tool supporting serial over USB communication like
58+
- the Serial Monotor of the [Arduino IDE](https://www.arduino.cc/en/software) or
59+
- the [Serial Monitor](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-serial-monitor) extension of [Visual Studio Code](https://code.visualstudio.com/)
5860
- Set the baud rate to 115200 and \<CR\> (Carriage Return) as command / message ending character
5961
- The firmware uses the following Raspberry Pi Pico GPIOs:
6062
- GP2: DCC signal output
@@ -113,15 +115,15 @@ File <path to firmware>/firmware/src/pico_build/cs.uf2:
113115
114116
Program Information
115117
name: cs
116-
version: v0.8.0
118+
version: <firmware version>
117119
web site: https://github.com/pico-cs
118120
description: pico-cs DCC command station
119121
features: Refresh buffer size 128
120122
double reset -> BOOTSEL
121123
UART stdin / stdout
122124
USB stdin / stdout
123-
binary start: 0x10000000
124-
binary end: 0x1000e534
125+
binary start: <start address>
126+
binary end: <end address>
125127
126128
Fixed Pin Information
127129
0: UART0 TX
@@ -133,21 +135,21 @@ Fixed Pin Information
133135
25: On-board LED
134136
135137
Build Information
136-
sdk version: 1.4.0
138+
sdk version: <sdk version>
137139
pico_board: pico
138140
boot2_name: boot2_w25q080
139-
build date: Feb 6 2023
141+
build date: <build date>
140142
build attributes: Release
141143
```
142144

143145
```
144146
./picotool info -a <path to firmware>/firmware/src/pico_w_build/cs_w.uf2
145147
146-
File ../../pico-cs/firmware/src/pico_w_build/cs_w.uf2:
148+
File <path to firmware>/firmware/src/pico_w_build/cs_w.uf2:
147149
148150
Program Information
149151
name: cs_w
150-
version: v0.8.0
152+
version: <firmware version>
151153
web site: https://github.com/pico-cs
152154
description: pico-cs DCC command station
153155
features: WiFi SSID MyWiFiSSID password MyWiFiPassword
@@ -156,8 +158,8 @@ Program Information
156158
double reset -> BOOTSEL
157159
UART stdin / stdout
158160
USB stdin / stdout
159-
binary start: 0x10000000
160-
binary end: 0x10055c2c
161+
binary start: <start address>
162+
binary end: <end address>
161163
162164
Fixed Pin Information
163165
0: CYW43 LED, UART0 TX
@@ -168,10 +170,10 @@ Fixed Pin Information
168170
5: Main track: DCC signal power (inverted)
169171
170172
Build Information
171-
sdk version: 1.4.0
173+
sdk version: <sdk version>
172174
pico_board: pico_w
173175
boot2_name: boot2_w25q080
174-
build date: Feb 6 2023
176+
build date: <build date>
175177
build attributes: Release
176178
```
177179

src/board.c

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ static board_type_t board_get_type() {
1616
}
1717

1818
void board_init_common(board_t *board, writer_t *logger) {
19-
mutex_init(&(board->mu));
20-
2119
board->logger = logger;
2220

2321
// unique board id

src/board.h

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _BOARD_H
22
#define _BOARD_H
33

4-
#include "pico/mutex.h"
54
#include "pico/unique_id.h"
65

76
#include "common.h"
@@ -15,7 +14,6 @@ typedef enum {
1514
#define MAC_SIZE_BYTES 6
1615

1716
typedef struct {
18-
mutex_t mu;
1917
writer_t *logger;
2018
board_type_t type;
2119
char id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES*3];

src/board_pico.c

+2-11
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ void board_deinit(board_t *board) {
2323
board_set_led(board, false);
2424
}
2525

26-
void board_set_led(board_t *board, bool v) {
27-
mutex_enter_blocking(&board->mu);
28-
gpio_put(PICO_DEFAULT_LED_PIN, v);
29-
mutex_exit(&board->mu);
30-
}
26+
void board_set_led(board_t *board, bool v) {gpio_put(PICO_DEFAULT_LED_PIN, v);}
3127

32-
bool board_get_led(board_t *board) {
33-
mutex_enter_blocking(&board->mu);
34-
bool rv = gpio_get(PICO_DEFAULT_LED_PIN);
35-
mutex_exit(&board->mu);
36-
return rv;
37-
}
28+
bool board_get_led(board_t *board) {return gpio_get(PICO_DEFAULT_LED_PIN);}

src/board_pico_w.c

+2-23
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,7 @@ bool board_init(board_t *board, writer_t *logger) {
3030
uint8_t mac[MAC_SIZE_BYTES];
3131
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, mac);
3232
uint8_hex_to_string(mac, MAC_SIZE_BYTES, board->mac, ':');
33-
34-
bool connected = false;
3533

36-
while (!connected) {
37-
38-
write_eventf(logger, "wifi: connecting MAC: %s ...", board->mac);
39-
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
40-
write_event(logger, "wifi: failed to connect");
41-
} else {
42-
write_event(logger, "wifi: connected");
43-
return true;
44-
}
45-
}
4634
return true;
4735
}
4836

@@ -51,15 +39,6 @@ void board_deinit(board_t *board) {
5139
cyw43_arch_deinit();
5240
}
5341

54-
void board_set_led(board_t *board, bool v) {
55-
mutex_enter_blocking(&board->mu);
56-
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, v);
57-
mutex_exit(&board->mu);
58-
}
42+
void board_set_led(board_t *board, bool v) {cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, v);}
5943

60-
bool board_get_led(board_t *board) {
61-
mutex_enter_blocking(&board->mu);
62-
bool rv = cyw43_arch_gpio_get(CYW43_WL_GPIO_LED_PIN);
63-
mutex_exit(&board->mu);
64-
return rv;
65-
}
44+
bool board_get_led(board_t *board) {return cyw43_arch_gpio_get(CYW43_WL_GPIO_LED_PIN);}

src/cmd.c

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ static cmd_rc_t cmd_mt_enabled(cmd_t *cmd, int num_prm, reader_t *reader, writer
100100
break;
101101
case 2:
102102
if (!parse_bool(reader_get_prm(reader, 1), &on)) return CMD_RC_INVPRM;
103+
104+
// switch led if CV flag is set
105+
if (cfg_get_cv(CFG_MT_CFG)&CFG_MT_CFG_LED) board_set_led(cmd->board, on);
106+
103107
cfg_set_mt_enabled(on);
104108
break;
105109
}

src/core.c

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
// command
44
static const int core_num_entry_queue = 1;
5-
static const bool core_dummy_data = true;
65

76
void core_init(core_t *core) {
87
queue_init(&core->q0, sizeof(bool), core_num_entry_queue);

src/cs.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010
#include "mt.h"
1111
#include "loop.h"
1212

13-
#define PROGRAM_VERSION "v0.8.0"
13+
#define PROGRAM_VERSION "v0.8.1"
1414
#define PROGRAM_DESCRIPTION "pico-cs DCC command station"
1515
#define PROGRAM_URL "https://github.com/pico-cs"
1616

1717
/*
1818
shared variables between cores
1919
*/
20-
core_t core; // core (inter core communication)
21-
board_t board; // board (shared for led)
22-
rbuf_t rbuf; // refresh buffer
23-
cmdq_t cmdq; // command queue (multicore)
24-
mt_t mt; // main track
20+
core_t core; // core (inter core communication)
21+
rbuf_t rbuf; // refresh buffer
22+
cmdq_t cmdq; // command queue (multicore)
2523

2624
void core1_main() {
2725
multicore_lockout_victim_init(); // prepare lockout (suspend) of core 1
2826

27+
mt_t mt; // main track
28+
2929
dcc_tx_pio_t tx_pio;
3030
dcc_tx_pio_init(&tx_pio);
3131

32-
mt_init(&mt, &tx_pio, &board, &rbuf, &cmdq);
32+
mt_init(&mt, &tx_pio, &rbuf, &cmdq);
3333

3434
core_signal_start1(&core); // signal core0 that core1 is started
3535

@@ -39,10 +39,10 @@ void core1_main() {
3939
}
4040
}
4141

42-
static const uint32_t wait_ms = 200;
43-
4442
int main() {
4543

44+
board_t board;
45+
4646
bi_decl(bi_program_version_string(PROGRAM_VERSION));
4747
bi_decl(bi_program_description(PROGRAM_DESCRIPTION));
4848
bi_decl(bi_program_url(PROGRAM_URL));
@@ -84,8 +84,8 @@ int main() {
8484
loop(&cmd, &usb_reader, &usb_writer); // finally loop until reboot
8585

8686
// reboot
87-
core_wait_stop1(&core); // wait for core1 to be stopped
87+
core_wait_stop1(&core); // wait for core1 to be stopped
8888
board_deinit(&board);
89-
sleep_ms(wait_ms); // wait for write output
90-
watchdog_enable(0, true); // reboot immediately
89+
uart_default_tx_wait_blocking(); // wait for write output
90+
watchdog_enable(0, true); // reboot immediately
9191
}

src/loop_pico_w.c

+45-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,40 @@
44
#include "loop.h"
55
#include "io.h"
66

7-
void loop(cmd_t *cmd, reader_t *usb_reader, writer_t *usb_writer) {
7+
static bool is_wifi_connected(cmd_t *cmd, writer_t *logger, bool log_success) {
8+
switch (cyw43_wifi_link_status(&cyw43_state,CYW43_ITF_STA)) {
9+
case CYW43_LINK_DOWN:
10+
write_event(logger, "wifi: down"); return false;
11+
case CYW43_LINK_JOIN:
12+
if (log_success) write_event(logger, "wifi: connected");
13+
return true;
14+
case CYW43_LINK_FAIL:
15+
write_event(logger, "wifi: connection failed"); return false;
16+
case CYW43_LINK_NONET:
17+
write_event(logger, "wifi: no matching SSID found"); return false;
18+
case CYW43_LINK_BADAUTH:
19+
write_event(logger, "wifi: authenticatation failure"); return false;
20+
default:
21+
write_event(logger, "wifi: unknown error"); return false;
22+
}
23+
}
24+
25+
static const int num_connect_retry = 5;
26+
static const int connect_timeout_ms = 30000; // 30 sec
27+
static const int check_wifi_int_us = 10000000; // 10 sec
828

29+
static bool wifi_connect(cmd_t *cmd, writer_t *logger) {
30+
write_eventf(logger, "wifi: connecting MAC: %s ...", cmd->board->mac);
31+
for (int i=0; i < num_connect_retry; i++) {
32+
cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, connect_timeout_ms);
33+
if (is_wifi_connected(cmd, logger, true)) return true;
34+
}
35+
return false;
36+
}
37+
38+
void loop(cmd_t *cmd, reader_t *usb_reader, writer_t *usb_writer) {
939
tcp_server_t server;
1040
tcp_server_init(&server, usb_writer);
11-
tcp_server_open(&server);
1241

1342
writer_t tcp_writer;
1443
writer_init(&tcp_writer, &server, &tcp_server_write);
@@ -17,11 +46,25 @@ void loop(cmd_t *cmd, reader_t *usb_reader, writer_t *usb_writer) {
1746

1847
byte usb_buf[PROT_BUFFER_SIZE];
1948

49+
if (wifi_connect(cmd, usb_writer)) tcp_server_open(&server);
50+
51+
absolute_time_t time_from = get_absolute_time();
52+
2053
while (true) {
2154

2255
server.recv_len = 0;
2356

2457
cyw43_arch_poll();
58+
59+
if (absolute_time_diff_us(time_from, get_absolute_time()) >= check_wifi_int_us) {
60+
if (!is_wifi_connected(cmd, usb_writer, false)) {
61+
if (wifi_connect(cmd, usb_writer)) {
62+
if (server.open) tcp_server_close(&server);
63+
tcp_server_open(&server);
64+
};
65+
};
66+
time_from = get_absolute_time();
67+
}
2568

2669
if (reader_read_frame(&tcp_reader, server.buffer_recv, server.recv_len)) {
2770
if (!cmd_dispatch(cmd, &tcp_reader, &tcp_writer)) break;

src/mt.c

+1-8
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,22 @@ static void mt_refresh_dcc(mt_t *mt, bool one_entry, cmdq_in_t *in) {
7171
static void mt_set_enabled(mt_t *mt, bool enabled) {
7272
mt->enabled = enabled;
7373
if (enabled) {
74-
// switch led on if CV flag is set
75-
if (cfg_get_cv(CFG_MT_CFG)&CFG_MT_CFG_LED) board_set_led(mt->board, true);
76-
7774
// when switching on set whether def or bidi depending on CV
7875
if (cfg_get_cv(CFG_MT_CFG)&CFG_MT_CFG_BIDI) {
7976
mt->tx_sm = &mt->tx_pio->tx_sm_bidi;
8077
} else {
8178
mt->tx_sm = &mt->tx_pio->tx_sm_def;
8279
}
83-
} else {
84-
// switch led off if CV flag is set
85-
if (cfg_get_cv(CFG_MT_CFG)&CFG_MT_CFG_LED) board_set_led(mt->board, false);
8680
}
8781
dcc_tx_sm_set_enabled(mt->tx_sm, enabled);
8882
}
8983

90-
void mt_init(mt_t *mt, dcc_tx_pio_t *tx_pio, board_t *board, rbuf_t *rbuf, cmdq_t *cmdq) {
84+
void mt_init(mt_t *mt, dcc_tx_pio_t *tx_pio, rbuf_t *rbuf, cmdq_t *cmdq) {
9185
mt->enabled = false; // start not enabled
9286

9387
mt->tx_pio = tx_pio;
9488
mt->tx_sm = &tx_pio->tx_sm_def;
9589

96-
mt->board = board;
9790
mt->rbuf = rbuf;
9891
mt->cmdq = cmdq;
9992

src/mt.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ typedef struct {
1010
bool enabled;
1111
dcc_tx_pio_t *tx_pio;
1212
dcc_tx_sm_t *tx_sm;
13-
board_t *board;
1413
rbuf_t *rbuf;
1514
cmdq_t *cmdq;
1615
} mt_t; // main track
1716

1817
// public interface
19-
void mt_init(mt_t *mt, dcc_tx_pio_t *tx_pio, board_t *board, rbuf_t *rbuf, cmdq_t *cmdq);
18+
void mt_init(mt_t *mt, dcc_tx_pio_t *tx_pio, rbuf_t *rbuf, cmdq_t *cmdq);
2019
void mt_dispatch(mt_t *mt);
2120

2221
#endif

0 commit comments

Comments
 (0)