Skip to content

Commit 0d912c3

Browse files
committed
Composite test-device: better LED feedback
1 parent 2939dfd commit 0d912c3

File tree

13 files changed

+48
-46
lines changed

13 files changed

+48
-46
lines changed

test-devices/composite-stm32/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ To upload using the BlackPill's built-in bootloader:
7575
2. Press the *Boot* button while connecting the board via USB to your computer. By pressing the *Boot* button, the device enters bootloader mode.
7676
3. Verify with `dfu-util --list` that the bootloader is available via USB. If not unplug the device and repeat step 2.
7777
4. Run the below command from the project directory.
78-
5. Unplug and reconnect the board from your computer. The LED should now blink about twice a second.
78+
5. Unplug and reconnect the board from your computer. Both the power and user LED should be lit and the device should appear as a serial device (aka as COM port on Windows).
7979

8080
```
8181
dfu-util --device 0483:df11 --alt 0 --dfuse-address 0x08000000 --reset --download bin/blackpill-fxxx.bin
76 Bytes
Binary file not shown.
76 Bytes
Binary file not shown.
72 Bytes
Binary file not shown.

test-devices/composite-stm32/src/board_f1.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,16 @@ uint32_t board_millis(void) {
188188

189189
void board_led_write(bool on) {
190190
if (on)
191-
gpio_set(GPIOB, 12);
192-
else
193191
gpio_clear(GPIOB, 12);
192+
else
193+
gpio_set(GPIOB, 12);
194194
}
195195

196196

197197
// --- Interrupt handlers ---
198198

199199
void SysTick_Handler (void) {
200-
millis_count++;
201-
}
202-
203-
void USBWakeUp_IRQHandler(void) {
204-
tud_int_handler(0);
200+
millis_count++;
205201
}
206202

207203
void USB_HP_IRQHandler(void) {

test-devices/composite-stm32/src/board_f4.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,16 @@ uint32_t board_millis(void) {
262262

263263
void board_led_write(bool on) {
264264
if (on)
265-
gpio_set(GPIOC, 13);
266-
else
267265
gpio_clear(GPIOC, 13);
266+
else
267+
gpio_set(GPIOC, 13);
268268
}
269269

270270

271271
// --- Interrupt handlers ---
272272

273273
void SysTick_Handler (void) {
274-
millis_count++;
274+
millis_count++;
275275
}
276276

277277
void OTG_FS_IRQHandler(void) {

test-devices/composite-stm32/src/main.c

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ uint8_t loopback_buffer[512];
2525
// RX buffer for loopback
2626
uint8_t loopback_rx_buffer[64];
2727

28-
// Blink durations
29-
enum {
30-
BLINK_NOT_MOUNTED = 250,
31-
BLINK_MOUNTED = 1000,
32-
BLINK_SUSPENDED = 2500,
33-
};
28+
static bool is_blinking = true;
29+
static uint32_t led_on_until = 0;
30+
static uint32_t blink_toogle_at = 0;
31+
static bool is_blink_on = true;
3432

35-
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
33+
static inline bool has_expired(uint32_t deadline, uint32_t now) {
34+
return (int32_t)(now - deadline) >= 0;
35+
}
3636

37+
static void led_busy(void);
3738
static void led_blinking_task(void);
3839
static void cdc_task(void);
3940
static void loopback_init(void);
@@ -82,6 +83,7 @@ void loopback_check_tx(void) {
8283
n = 128;
8384

8485
cust_vendor_start_transmit(EP_LOOPBACK_TX, info.ptr_lin, n);
86+
led_busy();
8587
}
8688
}
8789

@@ -105,6 +107,7 @@ void cdc_task(void) {
105107

106108
tud_cdc_write(buf, n);
107109
tud_cdc_write_flush();
110+
led_busy();
108111
}
109112

110113

@@ -115,6 +118,7 @@ void cust_vendor_rx_cb(uint8_t ep_addr, uint32_t recv_bytes) {
115118
tu_fifo_write_n(&loopback_fifo, loopback_rx_buffer, recv_bytes);
116119
loopback_check_rx();
117120
loopback_check_tx();
121+
led_busy();
118122
}
119123

120124
// Invoked when last tx transfer finished
@@ -134,11 +138,14 @@ void cust_vendor_tx_cb(uint8_t ep_addr, uint32_t sent_bytes) {
134138
if ((sent_bytes & (BULK_MAX_PACKET_SIZE - 1)) == 0
135139
&& !cust_vendor_is_transmitting(ep_addr))
136140
cust_vendor_start_transmit(EP_LOOPBACK_TX, NULL, 0);
141+
142+
led_busy();
137143
}
138144

139145
// Invoked when interface has been opened
140146
void cust_vendor_intf_open_cb(uint8_t intf) {
141147
loopback_check_rx();
148+
led_busy();
142149
}
143150

144151
void cust_vendor_halt_cleared_cb(uint8_t ep_addr) {
@@ -152,6 +159,7 @@ void cust_vendor_halt_cleared_cb(uint8_t ep_addr) {
152159
default:
153160
break;
154161
}
162+
led_busy();
155163
}
156164

157165

@@ -176,6 +184,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
176184

177185
case REQUEST_SAVE_VALUE:
178186
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 0) {
187+
led_busy();
179188
// save value from wValue
180189
saved_value = request->wValue;
181190
return tud_control_status(rhport, request);
@@ -184,20 +193,23 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
184193

185194
case REQUEST_SAVE_DATA:
186195
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 4) {
196+
led_busy();
187197
// receive into `saved_value`
188198
return tud_control_xfer(rhport, request, &saved_value, 4);
189199
}
190200
break;
191201

192202
case REQUEST_SEND_DATA:
193203
if (request->bmRequestType_bit.direction == TUSB_DIR_IN && request->wLength == 4) {
204+
led_busy();
194205
// transmit from `saved_value`
195206
return tud_control_xfer(rhport, request, &saved_value, 4);
196207
}
197208
break;
198209

199210
case REQUEST_RESET_BUFFERS:
200211
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 0) {
212+
led_busy();
201213
reset_buffers();
202214
return tud_control_status(rhport, request);
203215
}
@@ -207,6 +219,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
207219
if (request->bmRequestType_bit.direction == TUSB_DIR_IN && request->wLength == 1) {
208220
uint8_t intf_num = request->wIndex & 0xff;
209221
if (intf_num < 4) {
222+
led_busy();
210223
// return inteface number
211224
return tud_control_xfer(rhport, request, &intf_num, 1);
212225
}
@@ -247,38 +260,27 @@ usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) {
247260

248261
// Invoked when device is mounted
249262
void tud_mount_cb(void) {
250-
blink_interval_ms = BLINK_MOUNTED;
263+
is_blinking = false;
251264
}
252265

253-
// Invoked when device is unmounted
254-
void tud_umount_cb(void) {
255-
blink_interval_ms = BLINK_NOT_MOUNTED;
256-
}
257266

258-
// Invoked when usb bus is suspended
259-
// remote_wakeup_en: if host allow us to perform remote wakeup
260-
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
261-
void tud_suspend_cb(bool remote_wakeup_en) {
262-
(void) remote_wakeup_en;
263-
blink_interval_ms = BLINK_SUSPENDED;
264-
}
267+
// --- LED blinking ---
265268

266-
// Invoked when usb bus is resumed
267-
void tud_resume_cb(void) {
268-
blink_interval_ms = BLINK_MOUNTED;
269+
void led_busy(void) {
270+
led_on_until = board_millis() + 100;
271+
board_led_write(true);
269272
}
270273

271-
// --- LED blinking ---
272-
273274
void led_blinking_task(void) {
274-
static uint32_t start_ms = 0;
275-
static bool led_state = false;
276-
277-
// Blink every interval ms
278-
if ( board_millis() - start_ms < blink_interval_ms)
279-
return; // not enough time
280-
start_ms += blink_interval_ms;
281-
282-
board_led_write(led_state);
283-
led_state = 1 - led_state; // toggle
275+
uint32_t now = board_millis();
276+
if (is_blinking) {
277+
if (has_expired(blink_toogle_at, now)) {
278+
is_blink_on = !is_blink_on;
279+
blink_toogle_at = now + 250;
280+
}
281+
board_led_write(is_blink_on && (now & 7) == 0);
282+
283+
} else if (has_expired(led_on_until, now)) {
284+
board_led_write((now & 3) == 0);
285+
}
284286
}

test-devices/loopback-stm32/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ The device can be put into suspend mode by the host. It will go into a low-power
5252

5353
To put the device into suspended mode, put the host computer to sleep or supended mode. To wake it up, wake up the host computer.
5454

55+
NOTE: *Due to a limitation of TinyUSB, the device will only go into suspended mode if the host has set a USB configuration. Usually it means that an application has communicated with the device after it was plugged in. The LED blinks as long as no USB configuration has been set.*
56+
57+
NOTE: *Suspend/resume has not been implemented for the STM32F723 Discovery board.*
58+
5559

5660

5761
## Building the firmware
100 Bytes
Binary file not shown.
100 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)