diff options
author | NIIBE Yutaka <gniibe@fsij.org> | 2013-05-22 16:46:45 +0900 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2013-05-22 16:46:45 +0900 |
commit | 8880a8b1f9a738e9b1ab17712131279a3dfb6881 (patch) | |
tree | 1de56a2bae50bd755acc34aeac705c2b3c683345 /example-cdc/sample.c | |
parent | aa3fd9876abe2131d2ebda9a418ade6a4b89f72c (diff) |
add cdc example
Diffstat (limited to 'example-cdc/sample.c')
-rw-r--r-- | example-cdc/sample.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/example-cdc/sample.c b/example-cdc/sample.c new file mode 100644 index 0000000..6534a26 --- /dev/null +++ b/example-cdc/sample.c @@ -0,0 +1,173 @@ +#include <stdint.h> +#include <stdlib.h> +#include <chopstx.h> +#include "sys.h" /* for set_led */ +#include "usb_lld.h" /* for set_led */ + +static chopstx_mutex_t mtx; +static chopstx_cond_t cnd0; +static chopstx_cond_t cnd1; + +chopstx_mutex_t usb_mtx; +chopstx_cond_t cnd_usb_connection; +chopstx_cond_t cnd_usb_buffer_ready; + +static uint8_t u, v; +static uint8_t m; /* 0..100 */ + +static void * +pwm (void *arg) +{ + (void)arg; + + chopstx_mutex_lock (&mtx); + chopstx_cond_wait (&cnd0, &mtx); + chopstx_mutex_unlock (&mtx); + + while (1) + { + set_led (u&v); + chopstx_usleep (m); + set_led (0); + chopstx_usleep (100-m); + } + + return NULL; +} + +static void * +blk (void *arg) +{ + (void)arg; + + chopstx_mutex_lock (&mtx); + chopstx_cond_wait (&cnd1, &mtx); + chopstx_mutex_unlock (&mtx); + + while (1) + { + v = 0; + chopstx_usleep (200*1000); + v = 1; + chopstx_usleep (200*1000); + } + + return NULL; +} + +#define INTR_REQ_USB 20 + +static void * +usb_intr (void *arg) +{ + extern void usb_lld_init (uint8_t feature); + extern void usb_interrupt_handler (void); + + chopstix_intr_t interrupt; + + (void)arg; + asm volatile ("cpsid i" : : : "memory"); + /* Disable because of usb_lld_init assumes interrupt handler. */ + usb_lld_init (0x80); /* Bus powered. */ + chopstx_intr_register (&interrupt, INTR_REQ_USB); + /* Enable */ + asm volatile ("cpsie i" : : : "memory"); + + while (1) + { + chopstx_wait_intr (&interrupt); + + /* Process interrupt. */ + usb_interrupt_handler (); + } + + return NULL; +} + +#define PRIO_PWM 3 +#define PRIO_BLK 2 +#define PRIO_INTR 4 + +extern uint8_t __process1_stack_base__, __process1_stack_size__; +extern uint8_t __process2_stack_base__, __process2_stack_size__; +extern uint8_t __process3_stack_base__, __process3_stack_size__; + +const uint32_t __stackaddr_pwm = (uint32_t)&__process1_stack_base__; +const size_t __stacksize_pwm = (size_t)&__process1_stack_size__; + +const uint32_t __stackaddr_blk = (uint32_t)&__process2_stack_base__; +const size_t __stacksize_blk = (size_t)&__process2_stack_size__; + +const uint32_t __stackaddr_intr = (uint32_t)&__process3_stack_base__; +const size_t __stacksize_intr = (size_t)&__process3_stack_size__; + + +int +main (int argc, const char *argv[]) +{ + chopstx_t thd; + chopstx_attr_t attr; + + (void)argc; + (void)argv; + + chopstx_mutex_init (&mtx); + chopstx_cond_init (&cnd0); + chopstx_cond_init (&cnd1); + + chopstx_mutex_init (&usb_mtx); + chopstx_cond_init (&cnd_usb_connection); + chopstx_cond_init (&cnd_usb_buffer_ready); + + m = 10; + + chopstx_attr_init (&attr); + chopstx_attr_setschedparam (&attr, PRIO_PWM); + chopstx_attr_setstack (&attr, __stackaddr_pwm, __stacksize_pwm); + + chopstx_create (&thd, &attr, pwm, NULL); + + chopstx_attr_setschedparam (&attr, PRIO_BLK); + chopstx_attr_setstack (&attr, __stackaddr_blk, __stacksize_blk); + + chopstx_create (&thd, &attr, blk, NULL); + + chopstx_attr_setschedparam (&attr, PRIO_INTR); + chopstx_attr_setstack (&attr, __stackaddr_intr, __stacksize_intr); + + chopstx_create (&thd, &attr, usb_intr, NULL); + + chopstx_usleep (200*1000); + + chopstx_mutex_lock (&mtx); + chopstx_cond_signal (&cnd0); + chopstx_cond_signal (&cnd1); + chopstx_mutex_unlock (&mtx); + + while (1) + { + extern uint8_t connected; + + /* waiting USB connection */ + chopstx_mutex_lock (&usb_mtx); + if (!connected) + chopstx_cond_wait (&cnd_usb_connection, &usb_mtx); + chopstx_mutex_unlock (&usb_mtx); + + while (1) + { + u ^= 1; + chopstx_usleep (200*1000*6); + + usb_lld_write (ENDP1, "Hello, World with Chopstx!\r\n", 28); + chopstx_mutex_lock (&usb_mtx); + chopstx_cond_wait (&cnd_usb_buffer_ready, &usb_mtx); + if (!connected) + break; + chopstx_mutex_unlock (&usb_mtx); + } + chopstx_mutex_unlock (&usb_mtx); + } + + return 0; +} |