Compare commits
No commits in common. "dd9bd918628b40b090eaa3396053e4726a6d3116" and "781928dbdd0ccfc10e90d76367727de01aa2a1b4" have entirely different histories.
dd9bd91862
...
781928dbdd
|
@ -1,6 +1,5 @@
|
|||
#include "usbd_core.h"
|
||||
#include "usbd_cdc.h"
|
||||
#include "bflb_mtimer.h"
|
||||
#include <stdarg.h>
|
||||
/*!< endpoint address */
|
||||
/* Transmissions Device->Host (otherwise known as "IN" in these constants */
|
||||
|
@ -180,8 +179,11 @@ USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t debug_buffer[BUFFER_SIZE];
|
|||
volatile bool ep_tx_busy_flag = false;
|
||||
volatile bool ep_dbg_tx_busy_flag = false;
|
||||
|
||||
void (*data_received_ptr)(uint32_t, uint8_t *) = NULL;
|
||||
void (*dtr_changed_ptr)(bool);
|
||||
// TODO: Remove these. Debugging only
|
||||
volatile uint8_t debug_val_1 = 0;
|
||||
volatile uint8_t debug_val_2 = 0;
|
||||
volatile uint32_t debug_val32_1 = 0;
|
||||
volatile uint32_t debug_val32_2 = 0;
|
||||
|
||||
#ifdef CONFIG_USB_HS
|
||||
#define CDC_MAX_MPS 512
|
||||
|
@ -189,8 +191,6 @@ void (*dtr_changed_ptr)(bool);
|
|||
#define CDC_MAX_MPS 64
|
||||
#endif
|
||||
|
||||
void debuglog(const char *, ...);
|
||||
|
||||
void usbd_configure_done_callback(void)
|
||||
{
|
||||
/* setup first out ep read transfer */
|
||||
|
@ -200,10 +200,8 @@ void usbd_configure_done_callback(void)
|
|||
|
||||
void usbd_cdc_acm_bulk_out(uint8_t ep, uint32_t nbytes)
|
||||
{
|
||||
debug_val_1 = ep; debug_val32_1 = nbytes;
|
||||
USB_LOG_RAW("actual out len:%d\r\n", nbytes);
|
||||
debuglog("Bytes received from host. actual out len:%d\r\n", nbytes);
|
||||
|
||||
if (data_received_ptr != NULL) (*data_received_ptr)(nbytes, read_buffer);
|
||||
|
||||
/* setup next out ep read transfer */
|
||||
usbd_ep_start_read(ep, read_buffer, BUFFER_SIZE);
|
||||
|
@ -211,6 +209,7 @@ void usbd_cdc_acm_bulk_out(uint8_t ep, uint32_t nbytes)
|
|||
|
||||
void usbd_cdc_acm_bulk_in(uint8_t ep, uint32_t nbytes)
|
||||
{
|
||||
debug_val_2 = ep; debug_val32_2 = nbytes;
|
||||
USB_LOG_RAW("actual in len:%d\r\n", nbytes);
|
||||
|
||||
if ((nbytes % CDC_MAX_MPS) == 0 && nbytes) {
|
||||
|
@ -284,30 +283,27 @@ void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr)
|
|||
{
|
||||
/* Based on above init, intf = 0 is normal, intf = 2 is debug */
|
||||
if (dtr) {
|
||||
debuglog("Data terminal ready (DTR enabled) on intf: %d\r\n", intf);
|
||||
if (intf == 0) {
|
||||
dtr_enable = 1;
|
||||
if (dtr_changed_ptr != NULL) { (*dtr_changed_ptr)(dtr); }
|
||||
} else {
|
||||
dtr_dbg_enable = 1;
|
||||
}
|
||||
} else {
|
||||
debuglog("DTR disabled on intf: %d\r\n", intf);
|
||||
if (intf == 0) {
|
||||
if (dtr_changed_ptr != NULL) { (*dtr_changed_ptr)(dtr); }
|
||||
dtr_enable = 0;
|
||||
} else {
|
||||
dtr_dbg_enable = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint32_t out_inx = 0;
|
||||
bool is_color = true;
|
||||
|
||||
int prefix(bool is_debug, uint8_t lvl, uint8_t *buffer) {
|
||||
if (!is_debug) return 0;
|
||||
int len = 0;
|
||||
if (is_color) {
|
||||
len = sprintf((char *)buffer, "\033[32m[%.3f]:\033[00m ", bflb_mtimer_get_time_ms() / 1000.00);
|
||||
len = sprintf((char *)buffer, "\033[32m%d:\033[00m ", out_inx++);
|
||||
memcpy(buffer + len, "\033[", 5);
|
||||
switch (lvl) {
|
||||
case LVL_NORMAL:
|
||||
|
@ -322,7 +318,7 @@ int prefix(bool is_debug, uint8_t lvl, uint8_t *buffer) {
|
|||
}
|
||||
len += 8;
|
||||
}else{
|
||||
len = sprintf((char *)buffer, "[%.3f]: ", bflb_mtimer_get_time_ms() / 1000.00);
|
||||
len = sprintf((char *)buffer, "%d: ", out_inx++);
|
||||
}
|
||||
|
||||
return len;
|
||||
|
@ -348,6 +344,7 @@ void nprintf(uint8_t lvl, const uint8_t ep, const char *fmt, va_list ap) {
|
|||
} else {
|
||||
buffer = &debug_buffer[0];
|
||||
ep_dbg_tx_busy_flag = true;
|
||||
// TODO: Add debug prefix to buffer here...
|
||||
}
|
||||
int len = prefix(ep == CDC_IN_DBG_EP, lvl, buffer);
|
||||
len += vsnprintf(
|
||||
|
@ -358,19 +355,6 @@ void nprintf(uint8_t lvl, const uint8_t ep, const char *fmt, va_list ap) {
|
|||
);
|
||||
len = suffix(ep == CDC_IN_DBG_EP, lvl, buffer, len);
|
||||
usbd_ep_start_write(ep, buffer, len);
|
||||
if (ep == CDC_IN_EP) {
|
||||
//while (ep_tx_busy_flag) {}
|
||||
}else {
|
||||
//while (ep_dbg_tx_busy_flag) {}
|
||||
}
|
||||
}
|
||||
|
||||
void raw_output(size_t len, uint8_t *data) {
|
||||
if (!dtr_enable) return;
|
||||
|
||||
ep_tx_busy_flag = true;
|
||||
usbd_ep_start_write(CDC_IN_EP, data, len);
|
||||
//while (ep_dbg_tx_busy_flag) {}
|
||||
}
|
||||
|
||||
void output(const char *fmt, ...) {
|
||||
|
|
80
main.c
80
main.c
|
@ -10,85 +10,27 @@ extern void output(const char *, ...);
|
|||
extern void debuglog(const char *, ...);
|
||||
extern void debugwarn(const char *, ...);
|
||||
extern void debugerror(const char *, ...);
|
||||
extern void (*dtr_changed_ptr)(bool);
|
||||
extern void (*data_received_ptr)(uint32_t, uint8_t *);
|
||||
extern void raw_output(size_t, uint8_t *);
|
||||
|
||||
uint32_t buffer_init(char *);
|
||||
|
||||
char *prompt = "C:\\> ";
|
||||
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer_main[2048];
|
||||
|
||||
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cmd_buffer[1024];
|
||||
volatile bool display_prompt = false;
|
||||
volatile uint32_t curr_char = 0;
|
||||
|
||||
void process_cmd(uint8_t *cmd, uint32_t cmd_len){
|
||||
int prefix_len = strlen("echo ");
|
||||
if (strncmp((char *)cmd, "echo ", prefix_len) == 0){
|
||||
raw_output(cmd_len - prefix_len + 1, cmd + prefix_len);
|
||||
bflb_mtimer_delay_ms(1); /* There is a microsecond delay as well */
|
||||
output("\r\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void data_received(uint32_t nbytes, uint8_t *bytes) {
|
||||
/* I think we're getting an SOH after our output, but not sure why exactly */
|
||||
/* This if statement is a bit fragile (e.g. it doesn't cover SOH + data) */
|
||||
/* so we may need some further processing */
|
||||
if (curr_char == 0 && nbytes == 1 && *bytes == 0x01) return;
|
||||
/* if (nbytes == 1) */
|
||||
/* debuglog("Received the letter '%c'. curr_char %d\r\n", *bytes, curr_char); */
|
||||
if (curr_char + nbytes >= 1024) {
|
||||
/* We will overflow - bail */
|
||||
debugerror("command too long");
|
||||
output("\r\nCOMMAND TOO LONG\r\n%s", prompt);
|
||||
curr_char = 0;
|
||||
return;
|
||||
}
|
||||
/* Process new data */
|
||||
memcpy(&cmd_buffer[curr_char], bytes, nbytes);
|
||||
raw_output(nbytes, &cmd_buffer[curr_char]); /* Echo data back to console */
|
||||
if (nbytes == 1 && cmd_buffer[curr_char] == '\r') {
|
||||
/* User hit enter, process command */
|
||||
output("\r\n");
|
||||
bflb_mtimer_delay_ms(1); /* There is a microsecond delay as well */
|
||||
cmd_buffer[curr_char] = '\0';
|
||||
debuglog("Processing command '%s'\r\n", &cmd_buffer[0]);
|
||||
process_cmd(&cmd_buffer[0], curr_char - 1);
|
||||
output("%s", prompt);
|
||||
curr_char = 0;
|
||||
return;
|
||||
}
|
||||
curr_char += nbytes;
|
||||
}
|
||||
|
||||
void dtr_changed(bool dtr) {
|
||||
if (dtr) {
|
||||
debuglog("DTR enabled: requesting prompt\r\n");
|
||||
display_prompt = true;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int main(void)
|
||||
{
|
||||
board_init();
|
||||
|
||||
uint32_t inx = 0;
|
||||
cdc_acm_init();
|
||||
debuglog("Initialized");
|
||||
dtr_changed_ptr = &dtr_changed;
|
||||
data_received_ptr = &data_received;
|
||||
while (1) {
|
||||
if (display_prompt) {
|
||||
/* We can't display directly on the dtr_enabled interrupt, must be on the
|
||||
* main loop. Without any delay, we will not see a prompt. But even 1ms
|
||||
* is enough
|
||||
*/
|
||||
display_prompt = false;
|
||||
bflb_mtimer_delay_ms(1);
|
||||
output(prompt);
|
||||
debuglog("displayed prompt\r\n");
|
||||
curr_char = 0;
|
||||
if (inx++ >= 2000){
|
||||
output("Hello world\r\n");
|
||||
debuglog("Hello to debuggers!\r\n");
|
||||
debugwarn("Warning to debuggers!\r\n");
|
||||
debugerror("Oh no - an error!\r\n");
|
||||
inx = 0;
|
||||
}
|
||||
bflb_mtimer_delay_ms(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user