Modify USB<->USART example.

This commit is contained in:
NIIBE Yutaka
2017-12-15 16:00:59 +09:00
parent cb628fe317
commit 22039ee717
7 changed files with 110 additions and 54 deletions

View File

@@ -498,7 +498,7 @@ usart_main (void *arg)
} }
int int
usart_read (uint8_t dev_no, uint8_t *buf, uint16_t buflen) usart_read (uint8_t dev_no, char *buf, uint16_t buflen)
{ {
struct rb *rb; struct rb *rb;
@@ -509,11 +509,11 @@ usart_read (uint8_t dev_no, uint8_t *buf, uint16_t buflen)
else else
return -1; return -1;
return rb_read (rb, buf, buflen); return rb_read (rb, (uint8_t *)buf, buflen);
} }
int int
usart_write (uint8_t dev_no, uint8_t *buf, uint16_t buflen) usart_write (uint8_t dev_no, char *buf, uint16_t buflen)
{ {
struct rb *rb; struct rb *rb;
@@ -524,7 +524,7 @@ usart_write (uint8_t dev_no, uint8_t *buf, uint16_t buflen)
else else
return -1; return -1;
rb_write (rb, buf, buflen); rb_write (rb, (uint8_t *)buf, buflen);
return 0; return 0;
} }

View File

@@ -55,6 +55,6 @@ struct usart_stat {
void usart_init (uint16_t prio, uintptr_t stack_addr, size_t stack_size); void usart_init (uint16_t prio, uintptr_t stack_addr, size_t stack_size);
int usart_config (uint8_t dev_no, uint32_t config_bits); int usart_config (uint8_t dev_no, uint32_t config_bits);
int usart_read (uint8_t dev_no, uint8_t *buf, uint16_t buflen); int usart_read (uint8_t dev_no, char *buf, uint16_t buflen);
int usart_write (uint8_t dev_no, uint8_t *buf, uint16_t buflen); int usart_write (uint8_t dev_no, char *buf, uint16_t buflen);
const struct usart_stat *usart_stat (uint8_t dev_no); const struct usart_stat *usart_stat (uint8_t dev_no);

View File

@@ -10,6 +10,7 @@ CHIP=stm32f103
USE_SYS = yes USE_SYS = yes
USE_USB = yes USE_USB = yes
USE_USART = yes
################################### ###################################
CROSS = arm-none-eabi- CROSS = arm-none-eabi-

View File

@@ -2,8 +2,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <chopstx.h> #include <chopstx.h>
#include <contrib/usart.h>
#include "usb_lld.h" #include <usb_lld.h>
#include "cdc.h" #include "cdc.h"
/* For set_led */ /* For set_led */
@@ -26,60 +27,58 @@ blk (void *arg)
return NULL; return NULL;
} }
#define PRIO_USART 4
#define PRIO_BLK 2 #define PRIO_CDC2USART 2
#define PRIO_CDC 3 #define PRIO_USART2CDC 2
#define STACK_MAIN #define STACK_MAIN
#define STACK_PROCESS_1
#define STACK_PROCESS_2 #define STACK_PROCESS_2
#define STACK_PROCESS_3
#define STACK_PROCESS_4
#define STACK_PROCESS_5
#define STACK_PROCESS_6
#include "stack-def.h" #include "stack-def.h"
#define STACK_ADDR_BLK ((uint32_t)process1_base) #define STACK_ADDR_USART ((uint32_t)process2_base)
#define STACK_SIZE_BLK (sizeof process1_base) #define STACK_SIZE_USART (sizeof process2_base)
#define STACK_ADDR_CDC ((uint32_t)process2_base) #define STACK_ADDR_CDC2USART0 ((uint32_t)process3_base)
#define STACK_SIZE_CDC (sizeof process2_base) #define STACK_SIZE_CDC2USART0 (sizeof process3_base)
#define STACK_ADDR_USART2CDC0 ((uint32_t)process4_base)
#define STACK_SIZE_USART2CDC0 (sizeof process4_base)
#define STACK_ADDR_CDC2USART1 ((uint32_t)process5_base)
#define STACK_SIZE_CDC2USART1 (sizeof process5_base)
#define STACK_ADDR_USART2CDC1 ((uint32_t)process6_base)
#define STACK_SIZE_USART2CDC1 (sizeof process6_base)
struct cdc_usart {
uint8_t dev_no;
struct cdc *cdc;
};
static void * static void *
cdc_loop (void *arg) usart_to_cdc_loop (void *arg)
{ {
struct cdc **cdc_array; struct cdc_usart *cdc_usart = arg;
struct cdc *cdc_another;
struct cdc *cdc;
cdc_array = arg;
cdc = cdc_array[0];
cdc_another = cdc_array[1];
while (1) while (1)
{ {
char s[BUFSIZE]; char s[BUFSIZE];
cdc_wait_connection (cdc); cdc_wait_connection (cdc_usart->cdc);
chopstx_usec_wait (50*1000); chopstx_usec_wait (100*1000);
/* Send ZLP at the beginning. */
cdc_send (cdc, s, 0);
while (1) while (1)
{ {
int size; int size = usart_read (cdc_usart->dev_no, s, BUFSIZE);
uint32_t usec = 3000000; /* 3.0 seconds */
size = cdc_recv (cdc_another, s, &usec); if (size > 0)
if (size < 0)
break;
if (size)
{ {
if (cdc_send (cdc, s, size) < 0) if (cdc_send (cdc_usart->cdc, s, size) < 0)
break;
}
else
{
if (cdc_send (cdc, "HELLO!\r\n", 8) < 0)
break; break;
} }
} }
@@ -88,28 +87,72 @@ cdc_loop (void *arg)
return NULL; return NULL;
} }
static void *
cdc_to_usart_loop (void *arg)
{
struct cdc_usart *cdc_usart = arg;
while (1)
{
char s[BUFSIZE];
cdc_wait_connection (cdc_usart->cdc);
chopstx_usec_wait (50*1000);
/* Send ZLP at the beginning. */
cdc_send (cdc_usart->cdc, s, 0);
while (1)
{
int size;
uint32_t usec = 3000000; /* 3.0 seconds */
size = cdc_recv (cdc_usart->cdc, s, &usec);
if (size < 0)
break;
if (size)
usart_write (cdc_usart->dev_no, s, size);
else
usart_write (cdc_usart->dev_no, "HELLO!\r\n", 8);
}
}
return NULL;
}
int int
main (int argc, const char *argv[]) main (int argc, const char *argv[])
{ {
struct cdc *cdc_array[2]; struct cdc_usart cdc_usart0;
struct cdc *cdc_array_dash[2]; struct cdc_usart cdc_usart1;
(void)argc; (void)argc;
(void)argv; (void)argv;
chopstx_create (PRIO_BLK, STACK_ADDR_BLK, STACK_SIZE_BLK, blk, NULL);
chopstx_usec_wait (200*1000); chopstx_usec_wait (200*1000);
cdc_init (); cdc_init ();
cdc_wait_configured (); cdc_wait_configured ();
cdc_array[0] = cdc_array_dash[1] = cdc_open (0); usart_init (PRIO_USART, STACK_ADDR_USART, STACK_SIZE_USART);
cdc_array[1] = cdc_array_dash[0] = cdc_open (1);
chopstx_create (PRIO_CDC, STACK_ADDR_CDC, STACK_SIZE_CDC, cdc_usart0.cdc = cdc_open (0);
cdc_loop, cdc_array); cdc_usart0.dev_no = 2;
cdc_loop (cdc_array_dash); cdc_usart1.cdc = cdc_open (1);
cdc_usart1.dev_no = 3;
chopstx_create (PRIO_USART2CDC, STACK_ADDR_USART2CDC0,
STACK_SIZE_USART2CDC0, usart_to_cdc_loop, &cdc_usart0);
chopstx_create (PRIO_USART2CDC, STACK_ADDR_USART2CDC1,
STACK_SIZE_USART2CDC1, usart_to_cdc_loop, &cdc_usart1);
chopstx_create (PRIO_CDC2USART, STACK_ADDR_CDC2USART0,
STACK_SIZE_CDC2USART0, cdc_to_usart_loop, &cdc_usart0);
chopstx_create (PRIO_CDC2USART, STACK_ADDR_CDC2USART1,
STACK_SIZE_CDC2USART1, cdc_to_usart_loop, &cdc_usart1);
blk (NULL);
return 0; return 0;
} }

View File

@@ -76,6 +76,8 @@ SECTIONS
.process_stack (NOLOAD) : .process_stack (NOLOAD) :
{ {
. = ALIGN(8); . = ALIGN(8);
*(.process_stack.6)
*(.process_stack.5)
*(.process_stack.4) *(.process_stack.4)
*(.process_stack.3) *(.process_stack.3)
*(.process_stack.2) *(.process_stack.2)

View File

@@ -27,3 +27,13 @@ char process3_base[0x0200] __attribute__ ((section(".process_stack.3")));
#if defined(STACK_PROCESS_4) #if defined(STACK_PROCESS_4)
char process4_base[0x0200] __attribute__ ((section(".process_stack.4"))); char process4_base[0x0200] __attribute__ ((section(".process_stack.4")));
#endif #endif
/* Fifth thread program */
#if defined(STACK_PROCESS_5)
char process5_base[0x0200] __attribute__ ((section(".process_stack.5")));
#endif
/* Sixth thread program */
#if defined(STACK_PROCESS_6)
char process6_base[0x0200] __attribute__ ((section(".process_stack.6")));
#endif

View File

@@ -43,10 +43,10 @@ struct cdc {
#define MAX_CDC 2 #define MAX_CDC 2
static struct cdc cdc_table[MAX_CDC]; static struct cdc cdc_table[MAX_CDC];
#define STACK_PROCESS_3 #define STACK_PROCESS_1
#include "stack-def.h" #include "stack-def.h"
#define STACK_ADDR_CDC ((uintptr_t)process3_base) #define STACK_ADDR_CDC ((uintptr_t)process1_base)
#define STACK_SIZE_CDC (sizeof process3_base) #define STACK_SIZE_CDC (sizeof process1_base)
/* /*
@@ -636,7 +636,7 @@ usb_rx_ready (uint8_t ep_num, uint16_t len)
static void *cdc_main (void *arg); static void *cdc_main (void *arg);
#define PRIO_CDC 4 #define PRIO_CDC 3
void void