Skip to content

Commit

Permalink
Composite test-device: better LED feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Mar 17, 2024
1 parent 2939dfd commit 0d912c3
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion test-devices/composite-stm32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ To upload using the BlackPill's built-in bootloader:
2. Press the *Boot* button while connecting the board via USB to your computer. By pressing the *Boot* button, the device enters bootloader mode.
3. Verify with `dfu-util --list` that the bootloader is available via USB. If not unplug the device and repeat step 2.
4. Run the below command from the project directory.
5. Unplug and reconnect the board from your computer. The LED should now blink about twice a second.
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).

```
dfu-util --device 0483:df11 --alt 0 --dfuse-address 0x08000000 --reset --download bin/blackpill-fxxx.bin
Expand Down
Binary file modified test-devices/composite-stm32/bin/blackpill-f401cc.bin
Binary file not shown.
Binary file modified test-devices/composite-stm32/bin/blackpill-f411ce.bin
Binary file not shown.
Binary file modified test-devices/composite-stm32/bin/bluepill-f103c8.bin
Binary file not shown.
10 changes: 3 additions & 7 deletions test-devices/composite-stm32/src/board_f1.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,16 @@ uint32_t board_millis(void) {

void board_led_write(bool on) {
if (on)
gpio_set(GPIOB, 12);
else
gpio_clear(GPIOB, 12);
else
gpio_set(GPIOB, 12);
}


// --- Interrupt handlers ---

void SysTick_Handler (void) {
millis_count++;
}

void USBWakeUp_IRQHandler(void) {
tud_int_handler(0);
millis_count++;
}

void USB_HP_IRQHandler(void) {
Expand Down
6 changes: 3 additions & 3 deletions test-devices/composite-stm32/src/board_f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,16 @@ uint32_t board_millis(void) {

void board_led_write(bool on) {
if (on)
gpio_set(GPIOC, 13);
else
gpio_clear(GPIOC, 13);
else
gpio_set(GPIOC, 13);
}


// --- Interrupt handlers ---

void SysTick_Handler (void) {
millis_count++;
millis_count++;
}

void OTG_FS_IRQHandler(void) {
Expand Down
70 changes: 36 additions & 34 deletions test-devices/composite-stm32/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ uint8_t loopback_buffer[512];
// RX buffer for loopback
uint8_t loopback_rx_buffer[64];

// Blink durations
enum {
BLINK_NOT_MOUNTED = 250,
BLINK_MOUNTED = 1000,
BLINK_SUSPENDED = 2500,
};
static bool is_blinking = true;
static uint32_t led_on_until = 0;
static uint32_t blink_toogle_at = 0;
static bool is_blink_on = true;

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

static void led_busy(void);
static void led_blinking_task(void);
static void cdc_task(void);
static void loopback_init(void);
Expand Down Expand Up @@ -82,6 +83,7 @@ void loopback_check_tx(void) {
n = 128;

cust_vendor_start_transmit(EP_LOOPBACK_TX, info.ptr_lin, n);
led_busy();
}
}

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

tud_cdc_write(buf, n);
tud_cdc_write_flush();
led_busy();
}


Expand All @@ -115,6 +118,7 @@ void cust_vendor_rx_cb(uint8_t ep_addr, uint32_t recv_bytes) {
tu_fifo_write_n(&loopback_fifo, loopback_rx_buffer, recv_bytes);
loopback_check_rx();
loopback_check_tx();
led_busy();
}

// Invoked when last tx transfer finished
Expand All @@ -134,11 +138,14 @@ void cust_vendor_tx_cb(uint8_t ep_addr, uint32_t sent_bytes) {
if ((sent_bytes & (BULK_MAX_PACKET_SIZE - 1)) == 0
&& !cust_vendor_is_transmitting(ep_addr))
cust_vendor_start_transmit(EP_LOOPBACK_TX, NULL, 0);

led_busy();
}

// Invoked when interface has been opened
void cust_vendor_intf_open_cb(uint8_t intf) {
loopback_check_rx();
led_busy();
}

void cust_vendor_halt_cleared_cb(uint8_t ep_addr) {
Expand All @@ -152,6 +159,7 @@ void cust_vendor_halt_cleared_cb(uint8_t ep_addr) {
default:
break;
}
led_busy();
}


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

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

case REQUEST_SAVE_DATA:
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 4) {
led_busy();
// receive into `saved_value`
return tud_control_xfer(rhport, request, &saved_value, 4);
}
break;

case REQUEST_SEND_DATA:
if (request->bmRequestType_bit.direction == TUSB_DIR_IN && request->wLength == 4) {
led_busy();
// transmit from `saved_value`
return tud_control_xfer(rhport, request, &saved_value, 4);
}
break;

case REQUEST_RESET_BUFFERS:
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 0) {
led_busy();
reset_buffers();
return tud_control_status(rhport, request);
}
Expand All @@ -207,6 +219,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
if (request->bmRequestType_bit.direction == TUSB_DIR_IN && request->wLength == 1) {
uint8_t intf_num = request->wIndex & 0xff;
if (intf_num < 4) {
led_busy();
// return inteface number
return tud_control_xfer(rhport, request, &intf_num, 1);
}
Expand Down Expand Up @@ -247,38 +260,27 @@ usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) {

// Invoked when device is mounted
void tud_mount_cb(void) {
blink_interval_ms = BLINK_MOUNTED;
is_blinking = false;
}

// Invoked when device is unmounted
void tud_umount_cb(void) {
blink_interval_ms = BLINK_NOT_MOUNTED;
}

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

// Invoked when usb bus is resumed
void tud_resume_cb(void) {
blink_interval_ms = BLINK_MOUNTED;
void led_busy(void) {
led_on_until = board_millis() + 100;
board_led_write(true);
}

// --- LED blinking ---

void led_blinking_task(void) {
static uint32_t start_ms = 0;
static bool led_state = false;

// Blink every interval ms
if ( board_millis() - start_ms < blink_interval_ms)
return; // not enough time
start_ms += blink_interval_ms;

board_led_write(led_state);
led_state = 1 - led_state; // toggle
uint32_t now = board_millis();
if (is_blinking) {
if (has_expired(blink_toogle_at, now)) {
is_blink_on = !is_blink_on;
blink_toogle_at = now + 250;
}
board_led_write(is_blink_on && (now & 7) == 0);

} else if (has_expired(led_on_until, now)) {
board_led_write((now & 3) == 0);
}
}
4 changes: 4 additions & 0 deletions test-devices/loopback-stm32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ The device can be put into suspend mode by the host. It will go into a low-power

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.

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.*

NOTE: *Suspend/resume has not been implemented for the STM32F723 Discovery board.*



## Building the firmware
Expand Down
Binary file modified test-devices/loopback-stm32/bin/blackpill-f401cc.bin
Binary file not shown.
Binary file modified test-devices/loopback-stm32/bin/blackpill-f411ce.bin
Binary file not shown.
Binary file modified test-devices/loopback-stm32/bin/bluepill-f103c8.bin
Binary file not shown.
Binary file modified test-devices/loopback-stm32/bin/disco_f723ie.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion test-devices/loopback-stm32/src/board_f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const rcc_clock_setup_t clock_setup_hse_value_out_84mhz_3v3 = {
.hpre = RCC_CFGR_HPRE_DIV1,
.ppre1 = RCC_CFGR_PPRE1_DIV2,
.ppre2 = RCC_CFGR_PPRE2_DIV1,
.voltage_scale = PWR_CR_VOS_SCALE2,
.voltage_scale = PWR_CR_VOS_SCALE1,
.flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN | FLASH_ACR_LATENCY_2WS
};

Expand Down

0 comments on commit 0d912c3

Please sign in to comment.