Skip to content

Commit 1fb3dbf

Browse files
committed
add write functionality
1 parent ed23883 commit 1fb3dbf

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ This is an example STM32 Blue Pill (STM32F103C8T6) project for learning and trai
1717

1818
At the bottom of VS Code click "✓" to build, "➔" to upload.
1919

20-
2120
# Attributions
2221
* [PlatformIO](https://github.com/platformio)
2322
* [FreeRTOS](https://github.com/freertos)
2423
* [libopencm3/libopencm3-examples](https://github.com/libopencm3/libopencm3-examples)
2524
* [bjwschaap/platformio-libopencm3-freertos](https://github.com/bjwschaap/platformio-libopencm3-freertos)
2625
* [dhylands/libopencm3-usb-serial](https://github.com/dhylands/libopencm3-usb-serial)
26+
* [ve3wwg/stm32f103c8t6](https://github.com/ve3wwg/stm32f103c8t6)
2727

2828
# License
2929
The Unlicense. Feel free to use or change it how you need.

include/USBSerial.h

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#ifndef __USBSERIAL_H__
22

33
#include <stdlib.h>
4+
#include <memory.h>
45
#include <libopencm3/usb/usbd.h>
56
#include <libopencm3/usb/cdc.h>
67

78
usbd_device *usbd_dev;
89
static char usb_serial[13]; // 12 digits plus a null terminator
910
static char command = '\0';
1011

12+
static char tx_buffer[256];
13+
static int tx_remaining_bytes = 0;
14+
1115
static const struct usb_device_descriptor dev = {
1216
.bLength = USB_DT_DEVICE_SIZE,
1317
.bDescriptorType = USB_DT_DEVICE,
@@ -206,7 +210,9 @@ static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
206210
if (len)
207211
{
208212
command = buf[0];
209-
usbd_ep_write_packet(usbd_dev, 0x82, buf, len);
213+
buf[len] = '\r';
214+
buf[len + 1] = '\n';
215+
usbd_ep_write_packet(usbd_dev, 0x82, buf, len + 2);
210216
}
211217
}
212218

@@ -252,6 +258,14 @@ static void fill_usb_serial(void) {
252258

253259
static void usbserial_pool() {
254260
usbd_poll(usbd_dev);
261+
262+
if (tx_remaining_bytes > 0) {
263+
int sent_bytes = 0;
264+
while (tx_remaining_bytes > 0) {
265+
sent_bytes = usbd_ep_write_packet(usbd_dev, 0x82, tx_buffer, tx_remaining_bytes);
266+
tx_remaining_bytes -= sent_bytes;
267+
}
268+
}
255269
}
256270

257271
static void usbserial_init()
@@ -272,4 +286,23 @@ static void usbserial_init()
272286
usbd_register_set_config_callback(usbd_dev, cdcacm_set_config);
273287
}
274288

289+
static void usbserial_write(const char* str) {
290+
int len = strlen(str);
291+
if (len > 0) {
292+
memcpy(tx_buffer + tx_remaining_bytes, str, len);
293+
tx_remaining_bytes += len;
294+
}
295+
}
296+
297+
static void usbserial_writeline(const char* str) {
298+
usbserial_write(str);
299+
usbserial_write("\r\n");
300+
}
301+
302+
static void usbserial_write_int(int i, int base) {
303+
char buff[16];
304+
__itoa(i, buff, base);
305+
usbserial_write(buff);
306+
}
307+
275308
#endif // __USBSERIAL_H__

src/main.c

+31-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,44 @@ static void usbTask(void* args) {
1414
if (command != '\0') {
1515
switch (command) {
1616
case '0':
17-
gpio_set(GPIOC, GPIO13); // turn led off
17+
usbserial_writeline("turning led off");
18+
usbserial_writeline("write '1' to turn led on");
19+
gpio_set(GPIOC, GPIO13);
1820
break;
1921

2022
case '1':
21-
gpio_clear(GPIOC, GPIO13); // turn led on
23+
usbserial_writeline("turning led on");
24+
usbserial_writeline("write '0' to turn led off");
25+
gpio_clear(GPIOC, GPIO13);
2226
break;
2327

2428
case 't':
25-
gpio_toggle(GPIOC, GPIO13); // toggle led
29+
usbserial_writeline("toggling led state");
30+
gpio_toggle(GPIOC, GPIO13);
2631
break;
32+
33+
case 'g':
34+
if (1) {
35+
int port_a_status = gpio_get(GPIOA, 0xFF);
36+
int port_b_status = gpio_get(GPIOB, 0xFF);
37+
int port_c_status = gpio_get(GPIOC, GPIO13);
38+
usbserial_writeline("GPIO PORT STATUSES");
39+
usbserial_write("A: ");
40+
usbserial_write_int(port_a_status, 2);
41+
usbserial_writeline("");
42+
usbserial_write("B: ");
43+
usbserial_write_int(port_b_status, 2);
44+
usbserial_writeline("");
45+
usbserial_write("C: ");
46+
usbserial_write_int(port_c_status, 2);
47+
usbserial_writeline("");
48+
}
49+
break;
50+
51+
default:
52+
53+
usbserial_writeline("unknown command");
54+
break;
2755
}
2856

2957
command = '\0';

0 commit comments

Comments
 (0)