Compare commits

50 Commits

Author SHA1 Message Date
NIIBE Yutaka
5458b77d36 Version 0.11 2016-05-19 12:22:30 +09:00
NIIBE Yutaka
03bba13005 eventflag API addtiion 2016-05-19 11:48:01 +09:00
NIIBE Yutaka
1b0fe5a6e8 Add eventflag_set_poll_desc 2016-05-18 21:32:32 +09:00
NIIBE Yutaka
5e33e7f468 fix eventflag 2016-05-18 17:00:25 +09:00
NIIBE Yutaka
db413813b6 eventflag rewrite 2016-05-18 16:52:00 +09:00
NIIBE Yutaka
7f009dbb5d update ChangeLog 2016-05-18 14:43:30 +09:00
NIIBE Yutaka
cea3200e48 Cleanup chopstx.c 2016-05-18 13:27:00 +09:00
NIIBE Yutaka
d93206882d Fix mutex_lock and join 2016-05-18 11:40:15 +09:00
NIIBE Yutaka
a17d12192f Fix example-cdc more 2016-05-18 09:55:23 +09:00
NIIBE Yutaka
4c1aa50f13 Fix example-cdc 2016-05-18 08:51:50 +09:00
NIIBE Yutaka
cf76b0bf13 example-cdc cleanup more 2016-05-17 21:33:58 +09:00
NIIBE Yutaka
76cbff737b example-cdc cleanup 2016-05-17 20:49:17 +09:00
NIIBE Yutaka
dce6c70ffc more fix for chopstx_poll 2016-05-17 17:51:41 +09:00
NIIBE Yutaka
3651aa64b4 ll_dequeue for PX only when not ready 2016-05-16 20:37:33 +09:00
NIIBE Yutaka
98977937cb fix chx_wakeup 2016-05-16 18:00:41 +09:00
NIIBE Yutaka
06d046b963 Improve system routines API for STM32F103 2016-05-16 14:59:05 +09:00
NIIBE Yutaka
b7c6dadcfb Fix IRQ handling and improve cancellation implementation 2016-05-16 14:50:04 +09:00
NIIBE Yutaka
a82acac8df Bug fix for interrupt preemption 2016-05-13 22:27:56 +09:00
NIIBE Yutaka
206f2a5f07 Fix intr_wait 2016-05-13 17:51:12 +09:00
NIIBE Yutaka
5046dd45f2 IRQ handling is now merged into polling 2016-05-13 16:35:35 +09:00
NIIBE Yutaka
db6e668524 more fixes for chopstx_poll 2016-05-13 14:52:38 +09:00
NIIBE Yutaka
daa7aebd6f Add READY field for chx_poll_XXX so that we can check if it's ready 2016-05-13 14:22:12 +09:00
NIIBE Yutaka
5fc2617ae5 Fix changelog 2016-05-13 11:11:01 +09:00
NIIBE Yutaka
fabd271196 Update example-cdc 2016-05-12 18:14:47 +09:00
NIIBE Yutaka
5730641ffd Bug fixes for Cortex-M3 and chopstx_poll 2016-05-12 18:12:52 +09:00
NIIBE Yutaka
5c1638c023 Fix mutex init 2016-05-12 11:17:17 +09:00
NIIBE Yutaka
420bd135af Adding ADC example for FS-BB48 2016-05-11 12:34:17 +09:00
NIIBE Yutaka
a538113c07 change poll behavior 2016-04-24 12:24:36 +09:00
NIIBE Yutaka
8668c8a88a update sys.c in examples 2016-04-22 17:21:50 +09:00
NIIBE Yutaka
fba1dc05ea Clean up FS-BB48 example 2016-04-22 17:21:29 +09:00
NIIBE Yutaka
1b12a78054 Modify chopstx_poll API 2016-04-22 13:42:01 +09:00
NIIBE Yutaka
5d40ffbffa Implement chopstx_poll (2) 2016-04-21 16:10:06 +09:00
NIIBE Yutaka
06d28b62fb Implement chopstx_poll 2016-04-21 15:59:34 +09:00
NIIBE Yutaka
437b2dc43c Update for STM32 2016-04-20 17:30:41 +09:00
NIIBE Yutaka
3eac245981 fix 2016-04-19 16:40:25 +09:00
NIIBE Yutaka
7c2cdaa6e4 Example for FS-BB48 to compute CRC32. 2016-04-19 15:58:26 +09:00
NIIBE Yutaka
0bbb43dd3a Fix USB driver and example 2016-04-18 17:23:10 +09:00
NIIBE Yutaka
82749ab97a Add support for FS-BB48 2016-04-18 11:46:14 +09:00
NIIBE Yutaka
92e17d3bdf USB stack for STM32F103 improvement 2016-04-07 14:52:39 +09:00
NIIBE Yutaka
674c19c495 SVC is required for Cortex-M3 2016-04-07 14:36:41 +09:00
NIIBE Yutaka
35426d7715 Fix MSP initial value and improve chx_sched 2016-04-07 09:59:13 +09:00
NIIBE Yutaka
6db2dd96c5 New function: chopstx_poll 2016-04-06 21:04:24 +09:00
NIIBE Yutaka
5e6a433457 New struct: chx_qh 2016-04-06 20:34:28 +09:00
NIIBE Yutaka
fa8dd7afc8 Don't use SVC for context switch 2016-04-06 19:49:18 +09:00
NIIBE Yutaka
8e40065311 Initialization of chx_spinlock 2016-04-05 19:27:25 +09:00
NIIBE Yutaka
a99c5c6048 fix struct NVIC 2016-04-05 19:17:44 +09:00
NIIBE Yutaka
e092a56c17 Fix USB GET 2016-03-15 10:31:16 +09:00
NIIBE Yutaka
25e5f21847 AAPCS stack alignment 2016-03-08 20:27:24 +09:00
NIIBE Yutaka
90ac7f7c13 update usb stack from Gnuk development branch 2016-03-08 20:20:49 +09:00
NIIBE Yutaka
fd8aee3cb0 CDC to be echo service. 2015-11-05 13:54:49 +09:00
40 changed files with 4992 additions and 1008 deletions

View File

@@ -23,7 +23,8 @@ Mateusz Zalega:
NIIBE Yutaka:
Write the library:
chopstx.c, eventflag.c, entry.c, clk_gpio_init.c
chopstx.c, eventflag.c, entry.c,
clk_gpio_init-kl.c, clk_gpio_init-stm32.c
chopstx.h, eventflag.h
Draw the logo:
chopstx.svg, chopstx.png

269
ChangeLog
View File

@@ -1,3 +1,266 @@
2016-05-19 NIIBE Yutaka <gniibe@fsij.org>
* VERSION: 0.11.
* doc/chopstx.texi (VERSION): 0.11.
2016-05-18 NIIBE Yutaka <gniibe@fsij.org>
* eventflag.c: Update using chopstx_poll and offer API for poll.
* chopstx.c (requeue): New.
(chopstx_mutex_lock, chopstx_join): Fix by requeue.
(chopstx_main_init): Remove.
(chopstx_setpriority): New.
* example-cdc/usb-cdc.c: Prepare for multiple TTYs.
2016-05-17 NIIBE Yutaka <gniibe@fsij.org>
* example-cdc/usb-cdc.c: Update as TTY input/output with line
editing support.
* example-cdc/sample.c: Likewise.
* example-cdc/tty.h: Rename from stream.h.
* chopstx.c (chopstx_poll): Set ->ready = 0.
Add spinlock for ll_dequeue.
2016-05-16 NIIBE Yutaka <gniibe@fsij.org>
* example-cdc/sys.c (nvic_enable_vector): Remove.
(usb_lld_sys_init): Don't setup NVIC priority.
(sys_version): sys version 3.0.
2016-05-16 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.h (CHOPSTX_EXIT_SUCCESS, CHOPSTX_EXIT_CANCELED)
(CHOPSTX_EXIT_CANCELED_IN_SYNC): Remove.
(CHOPSTX_CANCELED): New.
* chopstx.c (chopstx_testcancel): Use CHOPSTX_CANCELED.
(chx_sched, chx_snooze): Re-define return values cleanly.
(chopstx_cancel): Use return value of chx_sched to cancel.
(chopstx_cond_wait, chopstx_join, chopstx_poll): Implement
cancellation rather easier way with return value of chx_sched.
2016-05-15 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.c (chopstx_claim_irq): Don't register clean up function.
(chx_release_irq): Remove.
(chopstx_cancel): Disable IRQ when canceled at POLL.
(chopstx_poll): Fix IRQ handling.
2016-05-13 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.c (chx_handle_intr): Call chx_request_preemption.
(chx_wakeup, chopstx_cond_signal, chopstx_cond_broadcast)
(chx_intr_hook, chopstx_poll): Cleanup.
2016-05-13 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.c (chopstx_exit): Don't call chx_release_irq_thread.
(chx_release_irq_thread): Remove.
(q_intr): New variable.
(intr_top): Remove.
(chx_handle_intr, chx_init, chopstx_claim_irq)
(chopstx_intr_wait): Use Q_INTR.
(chx_intr_hook): New.
(chopstx_poll): Support CHOPSTX_POLL_INTR.
(chopstx_release_irq): Remove.
(chx_release_irq): New internal function.
(THREAD_WAIT_INT): Remove.
(chopstx_intr_wait): Rewrite by chopstx_poll.
2016-05-13 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.c (chx_sched) [__ARM_ARCH_6M__]: Fix asm.
(chx_handle_intr, chx_wakeup, chopstx_cancel): Fix for polling
with no timeout.
* example-cdc/sample.c (main): Update chopstx_poll example.
* example-fs-bb48/sample.c (main): Ditto.
* chopstx.c (struct chx_px): Add READY_P and LOCK.
(chx_proxy_init): Add initialization of new members.
(chx_wakeup): Spinlock PX.
(chopstx_mutex_lock): Add protection by splinlock.
(chx_cond_hook, chx_join_hook): Change arguments.
(chx_cond_unhook, chx_join_unhook): Remove.
(chopstx_claim_irq): Compatible to chx_poll.
(chopstx_join): Fix spinlocking.
(chopstx_cancel): Fix spinlocking.
2016-05-12 NIIBE Yutaka <gniibe@fsij.org>
* example-cdc/sample.c: Update using chopstx_poll.
* example-cdc/sample.ld: Likewise.
* example-cdc/stream.h: Likewise.
* example-cdc/usb-cdc.c: Likewise.
* chopstx.c (chopstx_mutex_init): Initialize OWNER, too.
(preempt) [__ARM_ARCH_7M__]: Bug fix for register 0 to inhibit
scheduling.
(chx_sched) [__ARM_ARCH_6M__]: Return YIELD normally.
(MAX_USEC_FOR_TIMER): Guarantee 24-bit tick.
(chx_snooze): Revert the change of 2016-04-24 and modify
chopstx_poll instead.
(chopstx_poll): Support waiting forever.
2016-04-24 Niibe Yutaka <gniibe@fsij.org>
* chopstx.c (chx_snooze): Wait forever when it's POLL and usec==0.
2016-04-22 Niibe Yutaka <gniibe@fsij.org>
* example-fs-bb48/first-pages.c: Rename from reset.c and merge
crc32.c.
* example-fs-bb48/sample.ld: Define section for first two pages.
* example-fs-bb48/usb_kl27z.c (__usb_buf__): Remove.
* chopstx.c (chx_cond_hook): Rename from chopstx_cond_hook and make
it internal.
(chx_cond_unhook): Likewise.
(chx_join_hook, chx_join_unhook): New.
(chopstx_poll): Change API, not exposing internals of hook/unhook.
* example-fs-bb48/sample.c (main): Follow the API change of
chopstx_poll.
2016-04-21 Niibe Yutaka <gniibe@fsij.org>
* chopstx.c (chx_snooze, chx_wakeup): New.
(chopstx_cond_hook, chopstx_cond_unhook): New.
(chopstx_cond_signal, chopstx_cond_broadcast): Fix for poll.
(chx_proxy_init): Initialize with RUNNING.
(chopstx_poll): Fix with chx_snooze.
(chx_exit): Use chx_wakeup.
* example-fs-bb48/sample.c (main): Update with chopstx_poll.
* example-fs-bb48/usb-cdc.c (stream_recv): Fix the loop clear
FLAG_RECV_AVAIL only after we process it.
2016-04-20 Niibe Yutaka <gniibe@fsij.org>
* example-cdc/usb_stm32f103.c (usb_lld_reset): Supply FEATURE
argument. Call usb_lld_set_configuration internally and set
FEATURE.
(usb_lld_set_feature): Remove.
* example-cdc/usb-cdc.c (usb_cb_device_reset): Update with new
API.
* example-cdc/sys.c: Include clk_gpio_init-stm32.c.
* example-led/sys.c, example-primer2/sys.c: Ditto.
2016-04-19 Niibe Yutaka <gniibe@fsij.org>
* example-fs-bb48/sample.c (main): Change the example to display
CRC32 value from input line.
2016-04-18 Niibe Yutaka <gniibe@fsij.org>
* example-fs-bb48: New directory for FS-BB48.
* clk_gpio_init-kl.c: New.
* clk_gpio_init-stm32.c: Rename from clk_gpio_init.c.
2016-04-07 Niibe Yutaka <gniibe@fsij.org>
* example-fsm-55/sys.c: Update for non-SVC Chopstx.
2016-04-07 Niibe Yutaka <gniibe@fsij.org>
* example-cdc/usb-cdc.c: Update.
* example-cdc/usb_stm32f103.c (usb_handle_transfer): Don't use
weak symbols for callbacks, but use explicit callbacks of
usb_cb_tx_done and usb_cb_rx_ready.
* example-cdc/usb_lld.h (usb_cb_tx_done, usb_cb_rx_ready): New
callbacks.
2016-04-07 Niibe Yutaka <gniibe@fsij.org>
* chopstx.c (chx_cpu_sched_lock, chx_cpu_sched_unlock): Use SVC
for Cortex-M3, because of ICI/IT of ESPR.
(chx_sched): Invoke svc for Cortex-M3.
(preempt, svc): Change back for Cortex-M3.
2016-04-07 Niibe Yutaka <gniibe@fsij.org>
* entry.c (vector_table): Since IDLE thread runs with PSP now, use
different value for MSP.
* chopstx.c (chx_sched): Push LR value to PC slot on stack, so
that it returns directly to caller.
2016-04-06 Niibe Yutaka <gniibe@fsij.org>
* chopstx.c (struct chx_pq): New struct for priority queue.
(struct chx_px): New struct for proxy.
(struct chx_thread): New member FLAG_IS_PROXY.
(ll_dequeue, ll_insert, ll_pop, ll_prio_push, ll_prio_enqueue)
Change API.
(THREAD_WAIT_POLL): New thread status.
(chx_ready_pop, chx_ready_push, chx_ready_enqueue): Type coercion.
(chx_timer_insert, chx_timer_dequeue, chx_timer_expired): Use
chx_pq.
(chx_handle_intr, chopstx_cancel): Handle THREAD_WAIT_POLL.
(chx_init): Type coercion.
(chx_exit): Handle proxy for join.
(chopstx_create): Initialize the member FLAG_IS_PROXY.
(chopstx_cond_signal) Handle proxy.
(chx_proxy_init, chopstx_poll): New.
2016-04-06 Niibe Yutaka <gniibe@fsij.org>
* chopstx.h (struct chx_qh): New struct.
* chopstx.c (struct chx_queue): Use chx_qh.
(struct chx_thread): New member PARENT.
(ll_empty): Use chx_qh.
(ll_prio_push, ll_prio_enqueue): Set the member PARENT.
(chx_ready_pop, chx_ready_push, chx_ready_enqueue): Use the
member Q.
(chx_timer_insert): Return TP.
(chx_timer_dequeue, chx_timer_expired): Use the member Q.
(chx_init): Initialize change for Q_READY, Q_TIMER, Q_JOIN.
(chx_sched, preempt): Handle return value of chx_timer_insert.
(chx_exit, chopstx_mutex_init, chopstx_cond_init): Use the member Q.
(chx_mutex_unlock): Type coercion to CHX_THREAD.
(chopstx_create): Initialize PARENT field.
(chopstx_mutex_lock): Use the PARENT field.
(chopstx_join): Use ll_prio_enqueue, instead of ll_insert.
(chopstx_wakeup_usec_wait): Set REG_R0 on the stack.
2016-04-06 Niibe Yutaka <gniibe@fsij.org>
* chopstx.c (struct chx_thread): Move tcontext field.
(chx_cpu_sched_lock, chx_cpu_sched_unlock): Use CPSID/CPSIE.
(chx_sched): Don't use SVC. Return integer value.
(chopstx_usec_wait_var): Don't use R8.
(preempt): Modify so that we don't use SVC.
2016-04-05 Niibe Yutaka <gniibe@fsij.org>
* chopstx.c (struct NVIC): Add volatile qualifier to members.
(chx_spin_init): New.
(chopstx_mutex_init, chopstx_cond_init): Call chx_spin_init.
(chx_init): Initialize the spinlocks for INTR_LOCK, Q_READY,
Q_TIMER, and Q_JOIN.
2016-03-08 Niibe Yutaka <gniibe@fsij.org>
* chopstx.h (CHOPSTX_THREAD_SIZE): Align by 8.
* chopstx.c (struct chx_thread): Add W field to align. This is to
comply AAPCS (ARM Architecture Procedure Call Standard).
(chx_init): Initialize W.
* example-cdc/usb_stm32f103.c, usb_lld.h: Update from Gnuk.
* example-cdc/usb-cdc.c: Update.
2015-11-05 Niibe Yutaka <gniibe@fsij.org>
* example-cdc/sample.c: Enhanced to be echo service.
2015-09-15 Niibe Yutaka <gniibe@fsij.org>
* VERSION: 0.10.
@@ -9,7 +272,7 @@
* chopstx.c (q_exit): Remove.
(chx_init, chx_exit): Remove access to Q_EXIT.
(chx_release_irq_thread): Fix removing form the list.
(chx_release_irq_thread): Fix removing from the list.
2015-09-11 Niibe Yutaka <gniibe@fsij.org>
@@ -79,7 +342,7 @@
* example-fsm-55/hacker-emblem.ld: Put vectors on ROM.
* example-fsm-55/sys.c: No system services.
* entry.c (entry): Can be public.
* clk_gpio_init.c [MCU_STM32F0] (clock_init): Don't change CFGR1.
* clk_gpio_init.c (clock_init) [MCU_STM32F0]: Don't change CFGR1.
2015-07-30 Niibe Yutaka <gniibe@fsij.org>
@@ -109,7 +372,7 @@
* example-cdc/sys.c (sys_board_id): New.
* example-cdc/sample.ld (.sys.board_id): New.
(__flash_start__, __flash_end__): Remove.
* entry.c [HAVE_SYS_H] (vector_table): By undefining STM32F10X_MD,
* entry.c (vector_table) [HAVE_SYS_H]: By undefining STM32F10X_MD,
prepare for high density device even compiled for MD device.
2015-07-13 Kaz Kojima <kkojima@rr.iij4u.or.jp>

48
NEWS
View File

@@ -1,6 +1,54 @@
NEWS - Noteworthy changes
* Major changes in Chopstx 0.11
Released 2016-05-19
** New feature: polling
New function chopstx_poll is added to watch multiple condition
variables, threads' exit, or IRQ, simultaneously with timeout.
** Change API of eventflag
The initialization function eventflag_init only has an argument of EV.
An eventflag can be waited with timeout or can be waited with no
timeout, as caller like. It is not determined at initialization time
now. Besides, the eventflag can be waited by any threads. Functions
to poll eventflag together with other events (cond, join, and IRQ) are
provided.
** Removal of the function chopstx_release_irq
IRQ is enabled only when a thread is blocked in polling. When it (the
thread in polling) is canceled, IRQ is disabled.
** Removal of the function chopstx_main_init
It is removed because it's too special. Please use
chopstx_setpriority instead.
** New function: chopstx_setpriority
This function is not recommended in general. It is only added to
support the usage when main thread wants to change the schedule
priority after creating other threads.
** Function chopstx_intr_wait is deprecated
Use of chopstx_poll is recommended.
** FS-BB48: Kinetis L MCU
Support for FS-BB48 board with Kinetis L MCU is added.
** No HardFault at context switch on Cortex-M0
By its design, Chopstx does context switch holding the scheduler lock.
This is implemented with the feature of BASEPRI on Cortex-M3. Because
Cortex-M0 doesn't have support of BASEPRI, the context switch (before
version 0.11) always caused HardFault exception. Since Cortex-M0
doesn't have complex exception mechism of ICI/IT (which is supported
on Cortex-M3), it is actually possible to implement the context switch
in user mode. This is done.
** New sys.c (3.0)
Don't touch NVIC in usb_lld_sys_init.
* Major changes in Chopstx 0.10
Released 2015-09-15

17
README
View File

@@ -1,14 +1,14 @@
Chopstx - Threads and only Threads
Version 0.10
2015-09-15
Version 0.11
2016-05-19
Niibe Yutaka
Flying Stone Technology
What's Chopstx?
===============
Chopstx is an RT thread library for STM32F103 (ARM Cortex-M3)
or STM32F0 (ARM Cortex-M0).
Chopstx is an RT thread library for STM32F103 (ARM Cortex-M3),
STM32F0 (ARM Cortex-M0), or KL27Z (ARM Cortex-M0plus).
While most RTOSes come with many features, drivers, and stacks,
Chopstx just offers a simple RT thread library.
@@ -45,7 +45,10 @@ For STM32 Primer2, see the directory: example-primer2.
Future Works
============
Convenience function to determine bottom of thread stack, thread local
storage and support of interface like poll/select would be next thing
to be done.
Convenience function to determine the bottom of thread stack, thread
local storage would be next thing to be done.
Experimental SMP port for Cortex-A7 is under development. For SMP,
more careful considerations for shared access to objects of struct
chx_pq is needed. So, modifications required will not be small.
--

View File

@@ -1 +1 @@
release/0.10
release/0.11

5
board/board-fs-bb48.h Normal file
View File

@@ -0,0 +1,5 @@
#define BOARD_NAME "FS-BB48"
#define BOARD_ID 0xd1f5119c
/* echo -n "FST-01" | sha256sum | sed -e 's/^.*\(........\) -$/\1/' */
#define MCU_KINETIS_L 1

1377
chopstx.c

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
/*
* chopstx.h - Threads and only threads.
*
* Copyright (C) 2013 Flying Stone Technology
* Copyright (C) 2013, 2016 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.
@@ -26,12 +26,15 @@
*
*/
struct chx_qh {
struct chx_pq *next, *prev;
};
typedef uint32_t chopstx_t;
typedef uint8_t chopstx_prio_t;
extern chopstx_t chopstx_main;
void chopstx_main_init (chopstx_prio_t);
/* NOTE: This signature is different to PTHREAD's one. */
chopstx_t
@@ -44,8 +47,6 @@ chopstx_create (uint32_t flags_and_prio,
#define CHOPSTX_PRIO_INHIBIT_PREEMPTION 248
void chopstx_usec_wait_var (uint32_t *arg);
void chopstx_usec_wait (uint32_t usec);
struct chx_spinlock {
@@ -53,9 +54,7 @@ struct chx_spinlock {
};
typedef struct chx_mtx {
struct {
struct chx_thread *next, *prev;
} q;
struct chx_qh q;
struct chx_spinlock lock;
struct chx_thread *owner;
struct chx_mtx *list;
@@ -69,9 +68,7 @@ void chopstx_mutex_lock (chopstx_mutex_t *mutex);
void chopstx_mutex_unlock (chopstx_mutex_t *mutex);
typedef struct chx_cond {
struct {
struct chx_thread *next, *prev;
} q;
struct chx_qh q;
struct chx_spinlock lock;
} chopstx_cond_t;
@@ -82,25 +79,13 @@ void chopstx_cond_wait (chopstx_cond_t *cond, chopstx_mutex_t *mutex);
void chopstx_cond_signal (chopstx_cond_t *cond);
void chopstx_cond_broadcast (chopstx_cond_t *cond);
typedef struct chx_intr {
struct chx_intr *next;
struct chx_thread *tp;
uint32_t ready;
uint8_t irq_num;
} chopstx_intr_t;
void chopstx_claim_irq (chopstx_intr_t *intr, uint8_t irq_num);
void chopstx_release_irq (chopstx_intr_t *intr);
void chopstx_intr_wait (chopstx_intr_t *intr);
/*
* Library provides default implementation as weak reference.
* User can replace it.
*/
void chx_fatal (uint32_t err_code) __attribute__((__weak__, __noreturn__));
void chopstx_join (chopstx_t, void **);
int chopstx_join (chopstx_t, void **);
void chopstx_exit (void *retval) __attribute__((__noreturn__));
@@ -110,11 +95,7 @@ enum {
CHOPSTX_ERR_JOIN,
};
enum {
CHOPSTX_EXIT_SUCCESS = 0,
CHOPSTX_EXIT_CANCELED = 256,
CHOPSTX_EXIT_CANCELED_IN_SYNC = 257,
};
#define CHOPSTX_CANCELED ((void *) -1)
void chopstx_cancel (chopstx_t thd);
void chopstx_testcancel (void);
@@ -122,17 +103,60 @@ void chopstx_testcancel (void);
/* NOTE: This signature is different to PTHREAD's one. */
int chopstx_setcancelstate (int);
struct chx_cleanup {
typedef struct chx_cleanup {
struct chx_cleanup *next;
void (*routine) (void *);
void *arg;
};
} chopstx_cleanup_t;
/* NOTE: This signature is different to PTHREAD's one. */
void chopstx_cleanup_push (struct chx_cleanup *clp);
void chopstx_cleanup_push (chopstx_cleanup_t *clp);
void chopstx_cleanup_pop (int execute);
void chopstx_wakeup_usec_wait (chopstx_t thd);
void chopstx_setpriority (chopstx_prio_t);
#define CHOPSTX_THREAD_SIZE 60
void chopstx_usec_wait_var (uint32_t *arg); /* DEPRECATED */
void chopstx_wakeup_usec_wait (chopstx_t thd); /* DEPRECATED */
enum {
CHOPSTX_POLL_COND = 0,
CHOPSTX_POLL_INTR,
CHOPSTX_POLL_JOIN,
};
struct chx_poll_cond {
uint16_t type;
uint16_t ready;
/**/
chopstx_cond_t *cond;
chopstx_mutex_t *mutex;
int (*check) (void *);
void *arg;
};
typedef struct chx_poll_cond chopstx_poll_cond_t;
struct chx_poll_join {
uint16_t type;
uint16_t ready;
/**/
chopstx_t thd;
};
typedef struct chx_poll_join chopstx_poll_join_t;
struct chx_intr {
uint16_t type;
uint16_t ready;
/**/
uint8_t irq_num;
};
typedef struct chx_intr chopstx_intr_t;
void chopstx_claim_irq (chopstx_intr_t *intr, uint8_t irq_num);
void chopstx_intr_wait (chopstx_intr_t *intr); /* DEPRECATED */
int chopstx_poll (uint32_t *usec_p, int n, ...);
#define CHOPSTX_THREAD_SIZE 64

140
clk_gpio_init-kl.c Normal file
View File

@@ -0,0 +1,140 @@
/*
* clk_gpio_init-kl.c - Clock and GPIO initialization for Kinetis L.
*
* Copyright (C) 2016 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.
*
* Chopstx is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chopstx is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As additional permission under GNU GPL version 3 section 7, you may
* distribute non-source form of the Program without the copy of the
* GNU GPL normally required by section 4, provided you inform the
* receipents of GNU GPL by a written offer.
*
*/
#include "kl_sim.h"
struct MCG {
volatile uint8_t C1; /* MCG Control Register 1 */
volatile uint8_t C2; /* MCG Control Register 2 */
uint8_t reserved0[4]; /* */
volatile uint8_t S; /* MCG Status Register */
uint8_t reserved1[1]; /* */
volatile uint8_t SC; /* MCG Status and Control Register */
uint8_t reserved2[15]; /* */
volatile uint8_t MC; /* MCG Miscellaneous Control Register */
};
static struct MCG *const MCG = (struct MCG *const)0x40064000;
struct USB_CLK_RECOVER {
volatile uint8_t CTRL; /* USB Clock */
uint8_t rsvd38[3]; /* recovery control */
volatile uint8_t IRC_EN; /* IRC48M oscillator */
uint8_t rsvd39[3]; /* enable register */
volatile uint8_t INT_EN; /* Clock recovery */
uint8_t rsvd40[3]; /* interrupt enable */
volatile uint8_t INT_STATUS; /* Clock recovery */
/* interrupt status */
};
static struct USB_CLK_RECOVER *const USB_CLK_RECOVER =
(struct USB_CLK_RECOVER *const)0x40072140;
static void __attribute__((used))
clock_init (void)
{
SIM->CLKDIV1 = (SIM->CLKDIV1 & 0xF0070000)
| (1 << 16) /* OUTDIV4 = 001: Divide-by-2 */
;
MCG->MC = 0x80; /* HIRC Enable, LIRC_DIV2=000: Division factor=1 */
MCG->C1 = 0x00; /* Select HIRC clock, LIRC disabled. */
/* Make sure HIRC clock is selected. */
while ((MCG->S & 0x0c) != 0)
;
SIM->SOPT2 = 0x00040060; /* USBSRC=IRC48, CLOKOUTSEL=LPO, RTC-clock */
SIM->SCGC4 = (1 << 18); /* Enable USB FS clock */
SIM->SCGC5 = (1 << 10); /* Enable Port B clock */
SIM->COPC = 0; /* COP disabled */
/* Crystal-less USB setup. */
USB_CLK_RECOVER->IRC_EN = 0x02;
USB_CLK_RECOVER->CTRL = 0x80;
}
struct PORT {
volatile uint32_t PCR0; volatile uint32_t PCR1;
volatile uint32_t PCR2; volatile uint32_t PCR3;
volatile uint32_t PCR4; volatile uint32_t PCR5;
volatile uint32_t PCR6; volatile uint32_t PCR7;
volatile uint32_t PCR8; volatile uint32_t PCR9;
volatile uint32_t PCR10; volatile uint32_t PCR11;
volatile uint32_t PCR12; volatile uint32_t PCR13;
volatile uint32_t PCR14; volatile uint32_t PCR15;
volatile uint32_t PCR16; volatile uint32_t PCR17;
volatile uint32_t PCR18; volatile uint32_t PCR19;
volatile uint32_t PCR20; volatile uint32_t PCR21;
volatile uint32_t PCR22; volatile uint32_t PCR23;
volatile uint32_t PCR24; volatile uint32_t PCR25;
volatile uint32_t PCR26; volatile uint32_t PCR27;
volatile uint32_t PCR28; volatile uint32_t PCR29;
volatile uint32_t PCR30; volatile uint32_t PCR31;
volatile uint32_t GPCLR; volatile uint32_t GPCHR;
uint32_t reserved[6];
volatile uint32_t ISFR;
};
static struct PORT *const PORTB = (struct PORT *const)0x4004A000;
static struct PORT *const PORTD = (struct PORT *const)0x4004C000;
static struct PORT *const PORTE = (struct PORT *const)0x4004D000;
struct GPIO {
volatile uint32_t PDOR; /* Port Data Output Register */
volatile uint32_t PSOR; /* Port Set Output Register */
volatile uint32_t PCOR; /* Port Clear Output Register */
volatile uint32_t PTOR; /* Port Toggle Output Register */
volatile uint32_t PDIR; /* Port Data Input Register */
volatile uint32_t PDDR; /* Port Data Direction Register */
};
static struct GPIO *const GPIOB = (struct GPIO *const)0x400FF040;
static struct GPIO *const GPIOD = (struct GPIO *const)0x400FF0C0;
static struct GPIO *const GPIOE = (struct GPIO *const)0x400FF100;
static void __attribute__((used))
gpio_init (void)
{
PORTB->PCR0 = (1<<8) /* GPIO */
| (0<<6) /* DriveStrengthEnable=0 */
| (0<<4) /* PassiveFilterEnable=0 */
| (1<<2) /* SlewRateEnable = slow */
| (0<<1) /* pull enable = 0 */
| (0<<0) /* puddselect= 0 */
;
PORTB->PCR1 = (1<<8) /* GPIO */
| (0<<6) /* DriveStrengthEnable=0 */
| (0<<4) /* PassiveFilterEnable=0 */
| (1<<2) /* SlewRateEnable = slow */
| (0<<1) /* pull enable = 0 */
| (0<<0) /* puddselect= 0 */
;
GPIOB->PDDR = (1 << 1) | (1 << 0); /* PTB0, PTB1 : Output */
GPIOB->PSOR = (1 << 0); /* PTB0: Set : Light off */
GPIOB->PCOR = (1 << 1); /* PTB1: Clear: Output 0 */
}

View File

@@ -1,5 +1,5 @@
/*
* clk_gpio_init.c - Clock and GPIO initialization.
* clk_gpio_init-stm32.c - Clock and GPIO initialization for STM32.
*
* Copyright (C) 2015 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>

View File

@@ -7,16 +7,6 @@ When it detects a coding error, this function will be called to
stop further execution of code. It never returns.
@end deftypefun
@subheading chopstx_main_init
@anchor{chopstx_main_init}
@deftypefun {void} {chopstx_main_init} (chopstx_prio_t @var{prio})
@var{prio}: priority
Initialize main thread with @var{prio}.
The thread main is created with priority CHX_PRIO_MAIN_INIT,
and it runs with that priority until this routine will is called.
@end deftypefun
@subheading chopstx_create
@anchor{chopstx_create}
@deftypefun {chopstx_t} {chopstx_create} (uint32_t @var{flags_and_prio}, uint32_t @var{stack_addr}, size_t @var{stack_size}, voidfunc @var{thread_entry}, void * @var{arg})
@@ -40,6 +30,8 @@ Create a thread. Returns thread ID.
Sleep for micro seconds, specified by @var{var}.
Another thread can clear @var{var} to stop the caller going into sleep.
This function is DEPRECATED. Please use chopstx_poll.
@end deftypefun
@subheading chopstx_usec_wait
@@ -118,20 +110,14 @@ Wake up all threads waiting on @var{cond}.
Claim interrupt @var{intr} with @var{irq_num} for this thread.
@end deftypefun
@subheading chopstx_release_irq
@anchor{chopstx_release_irq}
@deftypefun {void} {chopstx_release_irq} (chopstx_intr_t * @var{intr0})
@var{intr0}: Interrupt request to be unregistered
Release the interrupt request specified by @var{intr0}.
@end deftypefun
@subheading chopstx_intr_wait
@anchor{chopstx_intr_wait}
@deftypefun {void} {chopstx_intr_wait} (chopstx_intr_t * @var{intr})
@var{intr}: Pointer to INTR structure
Wait for the interrupt @var{intr} to be occured.
This function is DEPRECATED. Please use chopstx_poll.
@end deftypefun
@subheading chopstx_cleanup_push
@@ -156,20 +142,21 @@ clean-up will be executed.
@deftypefun {void} {chopstx_exit} (void * @var{retval})
@var{retval}: Return value (to be caught by a joining thread)
Calling this function terminates the execution of thread, after
calling clean up functions. If the calling thread still holds
mutexes, they will be released. If the calling thread claiming
IRQ, it will be released, too. This function never returns.
Calling this function terminates the execution of running thread,
after calling clean up functions. If the calling thread still
holds mutexes, they will be released. This function never
returns.
@end deftypefun
@subheading chopstx_join
@anchor{chopstx_join}
@deftypefun {void} {chopstx_join} (chopstx_t @var{thd}, void ** @var{ret})
@deftypefun {int} {chopstx_join} (chopstx_t @var{thd}, void ** @var{ret})
@var{thd}: Thread to wait
@var{ret}: Pointer to void * to store return value
Waits for the thread of @var{thd} to terminate.
Returns 0 on success, 1 when waiting is interrupted.
@end deftypefun
@subheading chopstx_wakeup_usec_wait
@@ -179,6 +166,9 @@ Waits for the thread of @var{thd} to terminate.
Canceling the timer, wake up the sleeping thread.
No return value.
This function is DEPRECATED. Please use chopstx_cond_signal,
where sleeping process calls chopstx_poll.
@end deftypefun
@subheading chopstx_cancel
@@ -209,3 +199,27 @@ Calling chopstx_setcancelstate sets cancelability state.
Returns old state which is 0 when it was enabled.
@end deftypefun
@subheading chopstx_poll
@anchor{chopstx_poll}
@deftypefun {int} {chopstx_poll} (uint32_t * @var{usec_p}, int @var{n}, @var{...})
@var{usec_p}: Pointer to usec for timeout. Forever if NULL.
@var{n}: Number of poll descriptors
Returns number of active descriptors.
@end deftypefun
@subheading chopstx_setpriority
@anchor{chopstx_setpriority}
@deftypefun {void} {chopstx_setpriority} (chopstx_prio_t @var{prio})
@var{prio}: priority
Change the schedule priority with @var{prio}.
In general, it is not recommended to use this function because
dynamically changing schedule priorities complicates the system.
Only a possible valid usage of this function is in the main thread
which starts its execution with priority of CHX_PRIO_MAIN_INIT, and
let it change its priority after initialization of other threads.
@end deftypefun

View File

@@ -1,7 +1,7 @@
\input texinfo @c -*-texinfo-*-
@c %**start of header
@setfilename chopstx.info
@set VERSION 0.10
@set VERSION 0.11
@settitle Chopstx Reference Manual
@c Unify some of the indices.
@syncodeindex tp fn
@@ -11,7 +11,7 @@
This manual is for Chopstx (version @value{VERSION}).
@noindent
Copyright @copyright{} 2013, 2015 Flying Stone Technology @*
Copyright @copyright{} 2013, 2015, 2016 Flying Stone Technology @*
@quotation
Permission is granted to copy, distribute and/or modify this document
@@ -84,7 +84,7 @@ Indexes
@chapter Introduction
Chopstx is an RT thread library for ARM Cortex-M0 and Cortex-M3.
Specifically, it is used for STM32F030 and STM32F103.
Specifically, it is used for STM32F030, STM32F103, and KL27Z.
While most RTOSes come with many features, drivers, and stacks,
Chopstx just offers a RT thread library.

31
entry.c
View File

@@ -1,7 +1,8 @@
/*
* entry.c - Entry routine when reset and interrupt vectors.
*
* Copyright (C) 2013, 2014, 2015 Flying Stone Technology
* Copyright (C) 2013, 2014, 2015, 2016
* Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.
@@ -37,7 +38,11 @@
#undef STM32F10X_MD /* Prepare for high density device, too. */
#else
#include "board.h"
#include "clk_gpio_init.c"
#if defined (MCU_KINETIS_L)
#include "clk_gpio_init-kl.c"
#else
#include "clk_gpio_init-stm32.c"
#endif
#endif
@@ -48,7 +53,9 @@
#endif
extern uint8_t __main_stack_end__;
#if defined(__ARM_ARCH_7M__)
extern void svc (void);
#endif
extern void preempt (void);
extern void chx_timer_expired (void);
extern void chx_handle_intr (void);
@@ -60,18 +67,7 @@ static void nmi (void)
static void hard_fault (void)
{
#if defined(__ARM_ARCH_6M__)
register uint32_t primask;
asm ("mrs %0, PRIMASK" : "=r" (primask));
if (primask)
asm volatile ("b svc");
else
for (;;);
#else
for (;;);
#endif
}
static void mem_manage (void)
@@ -105,7 +101,8 @@ uint32_t vectors_in_ram[48];
* This routine only changes PSP and not MSP.
*/
STATIC_ENTRY __attribute__ ((naked,section(".text.startup.0")))
void entry (void)
void
entry (void)
{
asm volatile ("bl clock_init\n\t"
/* Clear BSS section. */
@@ -169,7 +166,7 @@ void entry (void)
typedef void (*handler)(void);
handler vector_table[] __attribute__ ((section(".startup.vectors"))) = {
(handler)&__main_stack_end__,
(handler)(&__main_stack_end__ - 32),
entry,
nmi, /* nmi */
hard_fault, /* hard fault */
@@ -180,7 +177,11 @@ handler vector_table[] __attribute__ ((section(".startup.vectors"))) = {
none,
/* 0x20 */
none, none, none, /* reserved */
#if defined(__ARM_ARCH_6M__)
none, /* SVCall */
#elif defined(__ARM_ARCH_7M__)
svc, /* SVCall */
#endif
none, /* Debug */
none, /* reserved */
preempt, /* PendSV */

View File

@@ -1,7 +1,7 @@
/*
* eventflag.c - Eventflag with/without timeout
* eventflag.c - Eventflag
*
* Copyright (C) 2013 Flying Stone Technology
* Copyright (C) 2013, 2016 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.
@@ -31,24 +31,55 @@
#include <chopstx.h>
#include <eventflag.h>
enum {
EVENTFLAG_ERR_WAIT = CHOPSTX_ERR_JOIN + 1,
EVENTFLAG_ERR_TIMED_WAIT,
};
void
eventflag_init (struct eventflag *ev)
{
ev->flags = 0;
chopstx_cond_init (&ev->cond);
chopstx_mutex_init (&ev->mutex);
}
static int
eventflag_check (void *arg)
{
struct eventflag *ev = arg;
return ev->flags != 0;
}
void
eventflag_init (struct eventflag *ev, chopstx_t sleeper)
eventflag_prepare_poll (struct eventflag *ev, chopstx_poll_cond_t *poll_desc)
{
ev->sleeper = sleeper;
poll_desc->type = CHOPSTX_POLL_COND;
poll_desc->ready = 0;
poll_desc->cond = &ev->cond;
poll_desc->mutex = &ev->mutex;
poll_desc->check = eventflag_check;
poll_desc->arg = ev;
}
if (sleeper)
ev->u.wait_usec = 0;
eventmask_t
eventflag_get (struct eventflag *ev)
{
int n;
eventmask_t m;
chopstx_mutex_lock (&ev->mutex);
n = __builtin_ffs (ev->flags);
if (n)
{
m = (1 << (n - 1));
ev->flags &= ~m;
}
else
chopstx_cond_init (&ev->u.cond);
m = 0;
chopstx_mutex_unlock (&ev->mutex);
ev->flag = 0;
chopstx_mutex_init (&ev->mutex);
return m;
}
@@ -56,50 +87,34 @@ eventmask_t
eventflag_wait (struct eventflag *ev)
{
int n;
if (ev->sleeper)
chx_fatal (EVENTFLAG_ERR_WAIT);
eventmask_t m;
chopstx_mutex_lock (&ev->mutex);
if (!ev->flag)
chopstx_cond_wait (&ev->u.cond, &ev->mutex);
if (!ev->flags)
chopstx_cond_wait (&ev->cond, &ev->mutex);
n = __builtin_ffs (ev->flag);
ev->flag &= ~(1 << (n - 1));
n = __builtin_ffs (ev->flags);
if (n) /* Always n > 0 when waked up, but make sure no bad things. */
{
m = (1 << (n - 1));
ev->flags &= ~m;
}
else
m = 0;
chopstx_mutex_unlock (&ev->mutex);
return (1 << (n - 1));
return m;
}
eventmask_t
eventflag_wait_timeout (struct eventflag *ev, uint32_t usec)
{
eventmask_t em = 0;
int n;
chopstx_poll_cond_t poll_desc;
if (ev->sleeper == 0)
chx_fatal (EVENTFLAG_ERR_TIMED_WAIT);
chopstx_mutex_lock (&ev->mutex);
if (!ev->flag)
{
ev->u.wait_usec = usec;
chopstx_mutex_unlock (&ev->mutex);
chopstx_usec_wait_var (&ev->u.wait_usec);
chopstx_mutex_lock (&ev->mutex);
ev->u.wait_usec = 0;
}
n = __builtin_ffs (ev->flag);
if (n)
{
em = (1 << (n - 1));
ev->flag &= ~em;
}
chopstx_mutex_unlock (&ev->mutex);
return em;
eventflag_prepare_poll (ev, &poll_desc);
chopstx_poll (&usec, 1, &poll_desc);
return eventflag_get (ev);
}
@@ -107,16 +122,7 @@ void
eventflag_signal (struct eventflag *ev, eventmask_t m)
{
chopstx_mutex_lock (&ev->mutex);
ev->flag |= m;
if (ev->sleeper)
{
if (ev->u.wait_usec)
{
ev->u.wait_usec = 0;
chopstx_wakeup_usec_wait (ev->sleeper);
}
}
else
chopstx_cond_signal (&ev->u.cond);
ev->flags |= m;
chopstx_cond_signal (&ev->cond);
chopstx_mutex_unlock (&ev->mutex);
}

View File

@@ -1,16 +1,16 @@
typedef uint32_t eventmask_t;
struct eventflag {
chopstx_t sleeper;
eventmask_t flag;
eventmask_t flags;
chopstx_mutex_t mutex;
union {
uint32_t wait_usec;
chopstx_cond_t cond;
} u;
};
void eventflag_init (struct eventflag *ev, chopstx_t owner);
void eventflag_init (struct eventflag *ev);
eventmask_t eventflag_wait (struct eventflag *ev);
eventmask_t eventflag_wait_timeout (struct eventflag *ev, uint32_t usec);
void eventflag_signal (struct eventflag *ev, eventmask_t m);
/* For polling */
void eventflag_prepare_poll (struct eventflag *ev, chopstx_poll_cond_t *p);
eventmask_t eventflag_get (struct eventflag *ev);

View File

@@ -2,16 +2,15 @@
#include <stdlib.h>
#include <string.h>
#include <chopstx.h>
#include "sys.h" /* for set_led */
#include "usb_lld.h" /* for set_led */
#include "usb_lld.h"
#include "tty.h"
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;
static uint8_t u, v;
static uint8_t m; /* 0..100 */
@@ -55,55 +54,12 @@ blk (void *arg)
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);
chopstx_intr_t interrupt;
(void)arg;
usb_lld_init (0x80); /* Bus powered. */
chopstx_claim_irq (&interrupt, INTR_REQ_USB);
/*
* When USB interrupt occurs between usb_lld_init (which assumes
* ISR) and chopstx_claim_irq (which clears pending interrupt),
* invocation of usb_interrupt_handler won't occur.
*
* We can't call usb_lld_init after chopstx_claim_irq, as
* usb_lld_init does its own setting for NVIC. Calling
* chopstx_claim_irq after usb_lld_init overrides that.
*
* Calling usb_interrupt_handler is no harm even if there were no
* interrupts, thus, we call it unconditionally here, just in case
* if there is a request.
*
*/
usb_interrupt_handler ();
while (1)
{
chopstx_intr_wait (&interrupt);
/* Process interrupt. */
usb_interrupt_handler ();
}
chopstx_release_irq (&interrupt);
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__;
@@ -111,11 +67,10 @@ 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__;
static char hexchar (uint8_t x)
{
x &= 0x0f;
if (x <= 0x09)
return '0' + x;
else if (x <= 0x0f)
@@ -128,6 +83,7 @@ static char hexchar (uint8_t x)
int
main (int argc, const char *argv[])
{
struct tty *tty;
uint8_t count;
(void)argc;
@@ -137,15 +93,10 @@ main (int argc, const char *argv[])
chopstx_cond_init (&cnd0);
chopstx_cond_init (&cnd1);
chopstx_mutex_init (&usb_mtx);
chopstx_cond_init (&cnd_usb);
m = 10;
chopstx_create (PRIO_PWM, __stackaddr_pwm, __stacksize_pwm, pwm, NULL);
chopstx_create (PRIO_BLK, __stackaddr_blk, __stacksize_blk, blk, NULL);
chopstx_create (PRIO_INTR, __stackaddr_intr, __stacksize_intr,
usb_intr, NULL);
chopstx_usec_wait (200*1000);
@@ -154,41 +105,57 @@ main (int argc, const char *argv[])
chopstx_cond_signal (&cnd1);
chopstx_mutex_unlock (&mtx);
while (1)
{
extern uint8_t connected;
count= 0;
u = 1;
/* waiting USB connection */
chopstx_mutex_lock (&usb_mtx);
if (!connected)
chopstx_cond_wait (&cnd_usb, &usb_mtx);
chopstx_mutex_unlock (&usb_mtx);
tty = tty_open ();
tty_wait_configured (tty);
count = 0;
m = 50;
while (1)
{
char s[32];
uint8_t s[LINEBUFSIZE];
u ^= 1;
chopstx_usec_wait (200*1000*6);
u = 1;
tty_wait_connection (tty);
chopstx_usec_wait (500*1000);
/* Send ZLP at the beginning. */
tty_send (tty, s, 0);
memcpy (s, "xx: Hello, World with Chopstx!\r\n", 32);
s[0] = hexchar (count >> 4);
s[1] = hexchar (count & 0x0f);
count++;
chopstx_mutex_lock (&usb_mtx);
if (connected)
if (tty_send (tty, s, 32) < 0)
continue;
while (1)
{
usb_lld_write (ENDP1, s, 32);
chopstx_cond_wait (&cnd_usb, &usb_mtx);
}
else
int size;
uint32_t usec;
usec = 3000000; /* 3.0 seconds */
size = tty_recv (tty, s + 4, &usec);
if (size < 0)
break;
if (usec)
{
s[0] = hexchar (size >> 4);
s[1] = hexchar (size & 0x0f);
s[2] = ':';
s[3] = ' ';
s[size + 4] = '\r';
s[size + 5] = '\n';
if (tty_send (tty, s, size + 6) < 0)
break;
chopstx_mutex_unlock (&usb_mtx);
}
chopstx_mutex_unlock (&usb_mtx);
u ^= 1;
}
}
return 0;

View File

@@ -2,10 +2,10 @@
* ST32F103 memory setup.
*/
__main_stack_size__ = 0x0100; /* Exception handlers */
__process0_stack_size__ = 0x0100; /* Main program */
__process1_stack_size__ = 0x0100; /* first thread program */
__process2_stack_size__ = 0x0100; /* second thread program */
__process3_stack_size__ = 0x0100; /* third thread program */
__process0_stack_size__ = 0x0400; /* Main program */
__process1_stack_size__ = 0x0200; /* first thread program */
__process2_stack_size__ = 0x0200; /* second thread program */
__process3_stack_size__ = 0x0200; /* third thread program */
MEMORY
{

View File

@@ -1,7 +1,7 @@
/*
* sys.c - system routines for the initial page for STM32F103.
*
* Copyright (C) 2013, 2014, 2015 Flying Stone Technology
* Copyright (C) 2013, 2014, 2015, 2016 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* Copying and distribution of this file, with or without modification,
@@ -17,41 +17,8 @@
#include <stdlib.h>
#include "board.h"
#include "clk_gpio_init.c"
#include "clk_gpio_init-stm32.c"
#define CORTEX_PRIORITY_BITS 4
#define CORTEX_PRIORITY_MASK(n) ((n) << (8 - CORTEX_PRIORITY_BITS))
#define USB_LP_CAN1_RX0_IRQn 20
#define STM32_USB_IRQ_PRIORITY 11
struct NVIC {
uint32_t ISER[8];
uint32_t unused1[24];
uint32_t ICER[8];
uint32_t unused2[24];
uint32_t ISPR[8];
uint32_t unused3[24];
uint32_t ICPR[8];
uint32_t unused4[24];
uint32_t IABR[8];
uint32_t unused5[56];
uint32_t IPR[60];
};
static struct NVIC *const NVICBase = ((struct NVIC *const)0xE000E100);
#define NVIC_ISER(n) (NVICBase->ISER[n >> 5])
#define NVIC_ICPR(n) (NVICBase->ICPR[n >> 5])
#define NVIC_IPR(n) (NVICBase->IPR[n >> 2])
static void
nvic_enable_vector (uint32_t n, uint32_t prio)
{
unsigned int sh = (n & 3) << 3;
NVIC_IPR (n) = (NVIC_IPR(n) & ~(0xFF << sh)) | (prio << sh);
NVIC_ICPR (n) = 1 << (n & 0x1F);
NVIC_ISER (n) = 1 << (n & 0x1F);
}
static void
usb_cable_config (int enable)
@@ -118,13 +85,6 @@ usb_lld_sys_init (void)
usb_cable_config (1);
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
nvic_enable_vector (USB_LP_CAN1_RX0_IRQn,
CORTEX_PRIORITY_MASK (STM32_USB_IRQ_PRIORITY));
/*
* Note that we also have other IRQ(s):
* USB_HP_CAN1_TX_IRQn (for double-buffered or isochronous)
* USBWakeUp_IRQn (suspend/resume)
*/
RCC->APB1RSTR = RCC_APB1RSTR_USBRST;
RCC->APB1RSTR = 0;
}
@@ -423,8 +383,8 @@ handler vector[] __attribute__ ((section(".vectors"))) = {
const uint8_t sys_version[8] __attribute__((section(".sys.version"))) = {
3*2+2, /* bLength */
0x03, /* bDescriptorType = USB_STRING_DESCRIPTOR_TYPE */
/* sys version: "2.1" */
'2', 0, '.', 0, '1', 0,
/* sys version: "3.0" */
'3', 0, '.', 0, '0', 0,
};
const uint32_t __attribute__((section(".sys.board_id")))

9
example-cdc/tty.h Normal file
View File

@@ -0,0 +1,9 @@
#define LINEBUFSIZE 128
struct tty;
struct tty *tty_open (void);
void tty_wait_configured (struct tty *tty);
void tty_wait_connection (struct tty *tty);
int tty_send (struct tty *tty, uint8_t *buf, int count);
int tty_recv (struct tty *tty, uint8_t *buf, uint32_t *timeout);

View File

@@ -1,10 +1,84 @@
#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>
#include <string.h>
#include "usb_lld.h"
#include "tty.h"
static chopstx_intr_t usb_intr;
struct line_coding
{
uint32_t bitrate;
uint8_t format;
uint8_t paritytype;
uint8_t datatype;
} __attribute__((packed));
static const struct line_coding line_coding0 = {
115200, /* baud rate: 115200 */
0x00, /* stop bits: 1 */
0x00, /* parity: none */
0x08 /* bits: 8 */
};
/*
* Currently, we only support a single TTY.
*
* It is possible to extend to support multiple TTYs, for multiple
* interfaces.
*
* In that case, add argument to TTY_OPEN function and
* modify TTY_GET function to get the TTY structure. Functions which
* directy accesses TTY0 (usb_cb_device_reset and usb_cb_handle_event)
* should be modified, too.
*
* Modification of TTY_MAIN thread will be also needed to echo back
* input for each TTY, and the thread should run if one of TTY is
* opened.
*/
struct tty {
chopstx_mutex_t mtx;
chopstx_cond_t cnd;
uint8_t inputline[LINEBUFSIZE]; /* Line editing is supported */
uint8_t send_buf[LINEBUFSIZE]; /* Sending ring buffer for echo back */
uint32_t inputline_len : 8;
uint32_t send_head : 8;
uint32_t send_tail : 8;
uint32_t flag_connected : 1;
uint32_t flag_send_ready : 1;
uint32_t flag_input_avail : 1;
uint32_t : 2;
uint32_t device_state : 3; /* USB device status */
struct line_coding line_coding;
};
static struct tty tty0;
/*
* Locate TTY structure from interface number or endpoint number.
* Currently, it always returns tty0, because we only have the one.
*/
static struct tty *
tty_get (int interface, uint8_t ep_num)
{
struct tty *t = &tty0;
if (interface >= 0)
{
if (interface == 0)
t = &tty0;
}
else
{
if (ep_num == ENDP1 || ep_num == ENDP2 || ep_num == ENDP3)
t = &tty0;
}
return t;
}
extern chopstx_mutex_t usb_mtx;
extern chopstx_cond_t cnd_usb;
#define ENDP0_RXADDR (0x40)
#define ENDP0_TXADDR (0x80)
@@ -20,7 +94,7 @@ extern chopstx_cond_t cnd_usb;
/* USB Device Descriptor */
static const uint8_t vcom_device_desc[18] = {
18, /* bLength */
USB_DEVICE_DESCRIPTOR_TYPE, /* bDescriptorType */
DEVICE_DESCRIPTOR, /* bDescriptorType */
0x10, 0x01, /* bcdUSB = 1.1 */
0x02, /* bDeviceClass (CDC). */
0x00, /* bDeviceSubClass. */
@@ -35,20 +109,22 @@ static const uint8_t vcom_device_desc[18] = {
1 /* bNumConfigurations. */
};
#define VCOM_FEATURE_BUS_POWERED 0x80
/* Configuration Descriptor tree for a CDC.*/
static const uint8_t vcom_config_desc[67] = {
9,
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
CONFIG_DESCRIPTOR, /* bDescriptorType: Configuration */
/* Configuration Descriptor.*/
67, 0x00, /* wTotalLength. */
0x02, /* bNumInterfaces. */
0x01, /* bConfigurationValue. */
0, /* iConfiguration. */
0x80, /* bmAttributes (bus powered). */
VCOM_FEATURE_BUS_POWERED, /* bmAttributes. */
50, /* bMaxPower (100mA). */
/* Interface Descriptor.*/
9,
USB_INTERFACE_DESCRIPTOR_TYPE,
INTERFACE_DESCRIPTOR,
0x00, /* bInterfaceNumber. */
0x00, /* bAlternateSetting. */
0x01, /* bNumEndpoints. */
@@ -87,14 +163,14 @@ static const uint8_t vcom_config_desc[67] = {
0x01, /* bSlaveInterface0 (Data Class Interface). */
/* Endpoint 2 Descriptor.*/
7,
USB_ENDPOINT_DESCRIPTOR_TYPE,
ENDPOINT_DESCRIPTOR,
ENDP2|0x80, /* bEndpointAddress. */
0x03, /* bmAttributes (Interrupt). */
0x08, 0x00, /* wMaxPacketSize. */
0xFF, /* bInterval. */
/* Interface Descriptor.*/
9,
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: */
INTERFACE_DESCRIPTOR, /* bDescriptorType: */
0x01, /* bInterfaceNumber. */
0x00, /* bAlternateSetting. */
0x02, /* bNumEndpoints. */
@@ -104,14 +180,14 @@ static const uint8_t vcom_config_desc[67] = {
0x00, /* iInterface. */
/* Endpoint 3 Descriptor.*/
7,
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
ENDPOINT_DESCRIPTOR, /* bDescriptorType: Endpoint */
ENDP3, /* bEndpointAddress. */
0x02, /* bmAttributes (Bulk). */
0x40, 0x00, /* wMaxPacketSize. */
0x00, /* bInterval. */
/* Endpoint 1 Descriptor.*/
7,
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
ENDPOINT_DESCRIPTOR, /* bDescriptorType: Endpoint */
ENDP1|0x80, /* bEndpointAddress. */
0x02, /* bmAttributes (Bulk). */
0x40, 0x00, /* wMaxPacketSize. */
@@ -124,13 +200,13 @@ static const uint8_t vcom_config_desc[67] = {
*/
static const uint8_t vcom_string0[4] = {
4, /* bLength */
USB_STRING_DESCRIPTOR_TYPE,
STRING_DESCRIPTOR,
0x09, 0x04 /* LangID = 0x0409: US-English */
};
static const uint8_t vcom_string1[] = {
23*2+2, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
STRING_DESCRIPTOR, /* bDescriptorType */
/* Manufacturer: "Flying Stone Technology" */
'F', 0, 'l', 0, 'y', 0, 'i', 0, 'n', 0, 'g', 0, ' ', 0, 'S', 0,
't', 0, 'o', 0, 'n', 0, 'e', 0, ' ', 0, 'T', 0, 'e', 0, 'c', 0,
@@ -139,7 +215,7 @@ static const uint8_t vcom_string1[] = {
static const uint8_t vcom_string2[] = {
14*2+2, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
STRING_DESCRIPTOR, /* bDescriptorType */
/* Product name: "Chopstx Sample" */
'C', 0, 'h', 0, 'o', 0, 'p', 0, 's', 0, 't', 0, 'x', 0, ' ', 0,
'S', 0, 'a', 0, 'm', 0, 'p', 0, 'l', 0, 'e', 0,
@@ -150,86 +226,76 @@ static const uint8_t vcom_string2[] = {
*/
static const uint8_t vcom_string3[28] = {
28, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
STRING_DESCRIPTOR, /* bDescriptorType */
'0', 0, '.', 0, '0', 0, '0', 0, /* Version number */
};
#define NUM_INTERFACES 2
uint32_t bDeviceState = UNCONNECTED; /* USB device status */
uint8_t connected;
void
usb_cb_device_reset (void)
{
/* Set DEVICE as not configured */
usb_lld_set_configuration (0);
/* Current Feature initialization */
usb_lld_set_feature (vcom_config_desc[7]);
usb_lld_reset ();
usb_lld_reset (VCOM_FEATURE_BUS_POWERED);
/* Initialize Endpoint 0 */
usb_lld_setup_endpoint (ENDP0, EP_CONTROL, 0, ENDP0_RXADDR, ENDP0_TXADDR, 64);
chopstx_mutex_lock (&usb_mtx);
connected = 0;
bDeviceState = ATTACHED;
chopstx_cond_signal (&cnd_usb);
chopstx_mutex_unlock (&usb_mtx);
chopstx_mutex_lock (&tty0.mtx);
tty0.inputline_len = 0;
tty0.send_head = tty0.send_tail = 0;
tty0.flag_connected = 0;
tty0.flag_send_ready = 1;
tty0.flag_input_avail = 0;
tty0.device_state = ATTACHED;
memcpy (&tty0.line_coding, &line_coding0, sizeof (struct line_coding));
chopstx_mutex_unlock (&tty0.mtx);
}
#define CDC_CTRL_DTR 0x0001
void
usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, uint16_t value)
usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg)
{
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT)
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0
&& USB_SETUP_SET (req) && req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
{
struct tty *t = tty_get (arg->index, 0);
/* Open/close the connection. */
chopstx_mutex_lock (&usb_mtx);
connected = ((value & CDC_CTRL_DTR) != 0)? 1 : 0;
chopstx_cond_signal (&cnd_usb);
chopstx_mutex_unlock (&usb_mtx);
chopstx_mutex_lock (&t->mtx);
t->flag_connected = ((arg->value & CDC_CTRL_DTR) != 0);
chopstx_cond_signal (&t->cnd);
chopstx_mutex_unlock (&t->mtx);
}
}
struct line_coding
{
uint32_t bitrate;
uint8_t format;
uint8_t paritytype;
uint8_t datatype;
} __attribute__((packed));
static struct line_coding line_coding = {
115200, /* baud rate: 115200 */
0x00, /* stop bits: 1 */
0x00, /* parity: none */
0x08 /* bits: 8 */
};
static int
vcom_port_data_setup (uint8_t req, uint8_t req_no, struct control_info *detail)
vcom_port_data_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
{
if (USB_SETUP_GET (req))
{
struct tty *t = tty_get (arg->index, 0);
if (req_no == USB_CDC_REQ_GET_LINE_CODING)
return usb_lld_reply_request (&line_coding, sizeof(line_coding), detail);
return usb_lld_reply_request (&t->line_coding,
sizeof (struct line_coding), arg);
}
else /* USB_SETUP_SET (req) */
{
if (req_no == USB_CDC_REQ_SET_LINE_CODING
&& detail->len == sizeof (line_coding))
&& arg->len == sizeof (struct line_coding))
{
usb_lld_set_data_to_recv (&line_coding, sizeof (line_coding));
struct tty *t = tty_get (arg->index, 0);
usb_lld_set_data_to_recv (&t->line_coding,
sizeof (struct line_coding));
return USB_SUCCESS;
}
else if (req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
@@ -240,29 +306,29 @@ vcom_port_data_setup (uint8_t req, uint8_t req_no, struct control_info *detail)
}
int
usb_cb_setup (uint8_t req, uint8_t req_no, struct control_info *detail)
usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
{
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && detail->index == 0)
return vcom_port_data_setup (req, req_no, detail);
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0)
return vcom_port_data_setup (req, req_no, arg);
return USB_UNSUPPORT;
}
int
usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
struct control_info *detail)
struct req_args *arg)
{
if (rcp != DEVICE_RECIPIENT)
return USB_UNSUPPORT;
if (desc_type == DEVICE_DESCRIPTOR)
return usb_lld_reply_request (vcom_device_desc, sizeof (vcom_device_desc),
detail);
arg);
else if (desc_type == CONFIG_DESCRIPTOR)
return usb_lld_reply_request (vcom_config_desc, sizeof (vcom_config_desc),
detail);
arg);
else if (desc_type == STRING_DESCRIPTOR)
{
const uint8_t *str;
@@ -290,7 +356,7 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
return USB_UNSUPPORT;
}
return usb_lld_reply_request (str, size, detail);
return usb_lld_reply_request (str, size, arg);
}
return USB_UNSUPPORT;
@@ -312,6 +378,8 @@ vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
{
usb_lld_setup_endpoint (ENDP1, EP_BULK, 0, 0, ENDP1_TXADDR, 0);
usb_lld_setup_endpoint (ENDP3, EP_BULK, 0, ENDP3_RXADDR, 0, 64);
/* Start with no data receiving */
usb_lld_stall_rx (ENDP3);
}
else
{
@@ -321,7 +389,8 @@ vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
}
}
int usb_cb_handle_event (uint8_t event_type, uint16_t value)
int
usb_cb_handle_event (uint8_t event_type, uint16_t value)
{
int i;
uint8_t current_conf;
@@ -329,7 +398,9 @@ int usb_cb_handle_event (uint8_t event_type, uint16_t value)
switch (event_type)
{
case USB_EVENT_ADDRESS:
bDeviceState = ADDRESSED;
chopstx_mutex_lock (&tty0.mtx);
tty0.device_state = ADDRESSED;
chopstx_mutex_unlock (&tty0.mtx);
return USB_SUCCESS;
case USB_EVENT_CONFIG:
current_conf = usb_lld_current_configuration ();
@@ -341,7 +412,9 @@ int usb_cb_handle_event (uint8_t event_type, uint16_t value)
usb_lld_set_configuration (1);
for (i = 0; i < NUM_INTERFACES; i++)
vcom_setup_endpoints_for_interface (i, 0);
bDeviceState = CONFIGURED;
chopstx_mutex_lock (&tty0.mtx);
tty0.device_state = CONFIGURED;
chopstx_mutex_unlock (&tty0.mtx);
}
else if (current_conf != value)
{
@@ -351,12 +424,12 @@ int usb_cb_handle_event (uint8_t event_type, uint16_t value)
usb_lld_set_configuration (0);
for (i = 0; i < NUM_INTERFACES; i++)
vcom_setup_endpoints_for_interface (i, 1);
bDeviceState = ADDRESSED;
chopstx_mutex_lock (&tty0.mtx);
tty0.device_state = ADDRESSED;
chopstx_mutex_unlock (&tty0.mtx);
}
/* Do nothing when current_conf == value */
return USB_SUCCESS;
return USB_SUCCESS;
default:
break;
}
@@ -365,11 +438,12 @@ int usb_cb_handle_event (uint8_t event_type, uint16_t value)
}
int usb_cb_interface (uint8_t cmd, struct control_info *detail)
int
usb_cb_interface (uint8_t cmd, struct req_args *arg)
{
const uint8_t zero = 0;
uint16_t interface = detail->index;
uint16_t alt = detail->value;
uint16_t interface = arg->index;
uint16_t alt = arg->value;
if (interface >= NUM_INTERFACES)
return USB_UNSUPPORT;
@@ -386,7 +460,7 @@ int usb_cb_interface (uint8_t cmd, struct control_info *detail)
}
case USB_GET_INTERFACE:
return usb_lld_reply_request (&zero, 1, detail);
return usb_lld_reply_request (&zero, 1, arg);
default:
case USB_QUERY_INTERFACE:
@@ -394,21 +468,367 @@ int usb_cb_interface (uint8_t cmd, struct control_info *detail)
}
}
void
EP1_IN_Callback (void)
/*
* Put a character into the ring buffer to be send back.
*/
static void
put_char_to_ringbuffer (struct tty *t, int c)
{
chopstx_mutex_lock (&usb_mtx);
chopstx_cond_signal (&cnd_usb);
chopstx_mutex_unlock (&usb_mtx);
uint32_t next = (t->send_tail + 1) % LINEBUFSIZE;
if (t->send_head == next)
/* full */
/* All that we can do is ignore this char. */
return;
t->send_buf[t->send_tail] = c;
t->send_tail = next;
}
/*
* Get characters from ring buffer into S.
*/
static int
get_chars_from_ringbuffer (struct tty *t, uint8_t *s, int len)
{
int i = 0;
if (t->send_head == t->send_tail)
/* Empty */
return i;
do
{
s[i++] = t->send_buf[t->send_head];
t->send_head = (t->send_head + 1) % LINEBUFSIZE;
}
while (t->send_head != t->send_tail && i < len);
return i;
}
static void
tty_echo_char (struct tty *t, int c)
{
put_char_to_ringbuffer (t, c);
}
void
usb_cb_tx_done (uint8_t ep_num)
{
struct tty *t = tty_get (-1, ep_num);
if (ep_num == ENDP1)
{
chopstx_mutex_lock (&t->mtx);
if (t->flag_send_ready == 0)
{
t->flag_send_ready = 1;
chopstx_cond_signal (&t->cnd);
}
chopstx_mutex_unlock (&t->mtx);
}
else if (ep_num == ENDP2)
{
/* Nothing */
}
}
static int
tty_input_char (struct tty *t, int c)
{
unsigned int i;
int r = 0;
/* Process DEL, C-U, C-R, and RET as editing command. */
chopstx_mutex_lock (&t->mtx);
switch (c)
{
case 0x0d: /* Control-M */
tty_echo_char (t, 0x0d);
tty_echo_char (t, 0x0a);
t->flag_input_avail = 1;
r = 1;
chopstx_cond_signal (&t->cnd);
break;
case 0x12: /* Control-R */
tty_echo_char (t, '^');
tty_echo_char (t, 'R');
tty_echo_char (t, 0x0d);
tty_echo_char (t, 0x0a);
for (i = 0; i < t->inputline_len; i++)
tty_echo_char (t, t->inputline[i]);
break;
case 0x15: /* Control-U */
for (i = 0; i < t->inputline_len; i++)
{
tty_echo_char (t, 0x08);
tty_echo_char (t, 0x20);
tty_echo_char (t, 0x08);
}
t->inputline_len = 0;
break;
case 0x7f: /* DEL */
if (t->inputline_len > 0)
{
tty_echo_char (t, 0x08);
tty_echo_char (t, 0x20);
tty_echo_char (t, 0x08);
t->inputline_len--;
}
break;
default:
if (t->inputline_len < sizeof (t->inputline))
{
tty_echo_char (t, c);
t->inputline[t->inputline_len++] = c;
}
else
/* Beep */
tty_echo_char (t, 0x0a);
break;
}
chopstx_mutex_unlock (&t->mtx);
return r;
}
void
EP2_IN_Callback (void)
usb_cb_rx_ready (uint8_t ep_num)
{
}
uint8_t recv_buf[64];
struct tty *t = tty_get (-1, ep_num);
void
EP3_OUT_Callback (void)
{
if (ep_num == ENDP3)
{
int i, r;
r = usb_lld_rx_data_len (ENDP3);
usb_lld_rxcpy (recv_buf, ep_num, 0, r);
for (i = 0; i < r; i++)
if (tty_input_char (t, recv_buf[i]))
break;
chopstx_mutex_lock (&t->mtx);
if (t->flag_input_avail == 0)
usb_lld_rx_enable (ENDP3);
chopstx_mutex_unlock (&t->mtx);
}
}
static void *tty_main (void *arg);
#define INTR_REQ_USB 20
#define PRIO_TTY 4
extern uint8_t __process3_stack_base__, __process3_stack_size__;
const uint32_t __stackaddr_tty = (uint32_t)&__process3_stack_base__;
const size_t __stacksize_tty = (size_t)&__process3_stack_size__;
struct tty *
tty_open (void)
{
chopstx_mutex_init (&tty0.mtx);
chopstx_cond_init (&tty0.cnd);
tty0.inputline_len = 0;
tty0.send_head = tty0.send_tail = 0;
tty0.flag_connected = 0;
tty0.flag_send_ready = 1;
tty0.flag_input_avail = 0;
tty0.device_state = UNCONNECTED;
memcpy (&tty0.line_coding, &line_coding0, sizeof (struct line_coding));
chopstx_create (PRIO_TTY, __stackaddr_tty, __stacksize_tty, tty_main, &tty0);
return &tty0;
}
static void *
tty_main (void *arg)
{
struct tty *t = arg;
#if defined(OLDER_SYS_H)
/*
* Historically (before sys < 3.0), NVIC priority setting for USB
* interrupt was done in usb_lld_sys_init. Thus this code.
*
* When USB interrupt occurs between usb_lld_init (which assumes
* ISR) and chopstx_claim_irq (which clears pending interrupt),
* invocation of usb_interrupt_handler won't occur.
*
* Calling usb_interrupt_handler is no harm even if there were no
* interrupts, thus, we call it unconditionally here, just in case
* if there is a request.
*
* We can't call usb_lld_init after chopstx_claim_irq, as
* usb_lld_init does its own setting for NVIC. Calling
* chopstx_claim_irq after usb_lld_init overrides that.
*
*/
usb_lld_init (VCOM_FEATURE_BUS_POWERED);
chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
usb_interrupt_handler ();
#else
chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
usb_lld_init (VCOM_FEATURE_BUS_POWERED);
#endif
while (1)
{
chopstx_poll (NULL, 1, &usb_intr);
if (usb_intr.ready)
usb_interrupt_handler ();
chopstx_mutex_lock (&t->mtx);
if (t->device_state == CONFIGURED && t->flag_connected
&& t->flag_send_ready)
{
uint8_t line[32];
int len = get_chars_from_ringbuffer (t, line, sizeof (len));
if (len)
{
usb_lld_txcpy (line, ENDP1, 0, len);
usb_lld_tx_enable (ENDP1, len);
t->flag_send_ready = 0;
}
}
chopstx_mutex_unlock (&t->mtx);
}
return NULL;
}
void
tty_wait_configured (struct tty *t)
{
chopstx_mutex_lock (&t->mtx);
while (t->device_state != CONFIGURED)
chopstx_cond_wait (&t->cnd, &t->mtx);
chopstx_mutex_unlock (&t->mtx);
}
void
tty_wait_connection (struct tty *t)
{
chopstx_mutex_lock (&t->mtx);
while (t->flag_connected == 0)
chopstx_cond_wait (&t->cnd, &t->mtx);
t->flag_send_ready = 1;
t->flag_input_avail = 0;
t->send_head = t->send_tail = 0;
t->inputline_len = 0;
usb_lld_rx_enable (ENDP3); /* Accept input for line */
chopstx_mutex_unlock (&t->mtx);
}
static int
check_tx (struct tty *t)
{
if (t->flag_send_ready)
/* TX done */
return 1;
if (t->flag_connected == 0)
/* Disconnected */
return -1;
return 0;
}
int
tty_send (struct tty *t, uint8_t *buf, int len)
{
int r;
uint8_t *p;
int count;
p = buf;
count = len >= 64 ? 64 : len;
while (1)
{
chopstx_mutex_lock (&t->mtx);
while ((r = check_tx (t)) == 0)
chopstx_cond_wait (&t->cnd, &t->mtx);
if (r > 0)
{
usb_lld_txcpy (p, ENDP1, 0, count);
usb_lld_tx_enable (ENDP1, count);
t->flag_send_ready = 0;
}
chopstx_mutex_unlock (&t->mtx);
len -= count;
p += count;
if (len == 0 && count != 64)
/*
* The size of the last packet should be != 0
* If 64, send ZLP (zelo length packet)
*/
break;
count = len >= 64 ? 64 : len;
}
return r;
}
static int
check_rx (void *arg)
{
struct tty *t = arg;
if (t->flag_input_avail)
/* RX */
return 1;
if (t->flag_connected == 0)
/* Disconnected */
return 1;
return 0;
}
int
tty_recv (struct tty *t, uint8_t *buf, uint32_t *timeout)
{
int r;
chopstx_poll_cond_t poll_desc;
poll_desc.type = CHOPSTX_POLL_COND;
poll_desc.ready = 0;
poll_desc.cond = &t->cnd;
poll_desc.mutex = &t->mtx;
poll_desc.check = check_rx;
poll_desc.arg = t;
while (1)
{
chopstx_poll (timeout, 1, &poll_desc);
chopstx_mutex_lock (&t->mtx);
r = check_rx (t);
chopstx_mutex_unlock (&t->mtx);
if (r || (timeout != NULL && *timeout == 0))
break;
}
chopstx_mutex_lock (&t->mtx);
if (t->flag_connected == 0)
r = -1;
else if (t->flag_input_avail)
{
r = t->inputline_len;
memcpy (buf, t->inputline, r);
t->flag_input_avail = 0;
usb_lld_rx_enable (ENDP3);
t->inputline_len = 0;
}
else
r = 0;
chopstx_mutex_unlock (&t->mtx);
return r;
}

View File

@@ -1,9 +1,3 @@
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
#define USB_STRING_DESCRIPTOR_TYPE 0x03
#define USB_INTERFACE_DESCRIPTOR_TYPE 0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05
#define STANDARD_ENDPOINT_DESC_SIZE 0x09
/* endpoints enumeration */
@@ -24,7 +18,7 @@
enum RECIPIENT_TYPE
{
DEVICE_RECIPIENT, /* Recipient device */
DEVICE_RECIPIENT = 0, /* Recipient device */
INTERFACE_RECIPIENT, /* Recipient interface */
ENDPOINT_RECIPIENT, /* Recipient endpoint */
OTHER_RECIPIENT
@@ -55,19 +49,22 @@ enum
USB_SUCCESS = 1,
};
struct control_info {
struct req_args {
uint16_t value;
uint16_t index;
uint16_t len;
};
void usb_cb_device_reset (void);
int usb_cb_setup (uint8_t req, uint8_t req_no, struct control_info *detail);
int usb_cb_interface (uint8_t cmd, struct control_info *detail);
int usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg);
int usb_cb_interface (uint8_t cmd, struct req_args *arg);
int usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
struct control_info *detail);
struct req_args *arg);
int usb_cb_handle_event (uint8_t event_type, uint16_t value);
void usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, uint16_t value);
void usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no,
struct req_args *arg);
void usb_cb_tx_done (uint8_t ep_num);
void usb_cb_rx_ready (uint8_t ep_num);
enum {
USB_EVENT_ADDRESS,
@@ -103,18 +100,17 @@ void usb_lld_txcpy (const void *src, int ep_num, int offset, size_t len);
void usb_lld_tx_enable (int ep_num, size_t len);
void usb_lld_write (uint8_t ep_num, const void *buf, size_t len);
int usb_lld_reply_request (const void *buf, size_t buflen,
struct control_info *ctrl);
struct req_args *arg);
void usb_lld_rx_enable (int ep_num);
int usb_lld_rx_data_len (int ep_num);
void usb_lld_rxcpy (uint8_t *dst, int ep_num, int offset, size_t len);
void usb_lld_reset (void);
void usb_lld_reset (uint8_t feature);
void usb_lld_setup_endpoint (int ep_num, int ep_type, int ep_kind,
int ep_rx_addr, int ep_tx_addr,
int ep_rx_memory_size);
void usb_lld_set_configuration (uint8_t config);
uint8_t usb_lld_current_configuration (void);
void usb_lld_set_feature (uint8_t feature);
void usb_lld_set_data_to_recv (const void *p, size_t len);
void usb_lld_set_data_to_recv (void *p, size_t len);
void usb_lld_prepare_shutdown (void);
void usb_lld_shutdown (void);

View File

@@ -62,6 +62,8 @@ struct DEVICE_INFO
uint8_t bRequest;
/**/
uint16_t value;
uint16_t index;
uint16_t len;
};
static struct DEVICE_INFO device_info;
@@ -353,7 +355,7 @@ void usb_lld_init (uint8_t feature)
dev_p->state = IN_DATA;
usb_lld_set_configuration (0);
usb_lld_set_feature (feature);
dev_p->current_feature = feature;
/* Reset USB */
st103_set_cntr (CNTR_FRES);
@@ -462,26 +464,26 @@ static void handle_datastage_in (void)
st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
}
typedef int (*HANDLER) (uint8_t req, struct control_info *detail);
typedef int (*HANDLER) (uint8_t req, struct req_args *arg);
static int std_none (uint8_t req, struct control_info *detail)
static int std_none (uint8_t req, struct req_args *arg)
{
(void)req; (void)detail;
(void)req; (void)arg;
return USB_UNSUPPORT;
}
static int std_get_status (uint8_t req, struct control_info *detail)
static int std_get_status (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
uint16_t status_info = 0;
if (detail->value != 0 || detail->len != 2 || (detail->index >> 8) != 0
|| (req & REQUEST_DIR) == 0)
if (arg->value != 0 || arg->len != 2 || (arg->index >> 8) != 0
|| USB_SETUP_SET (req))
return USB_UNSUPPORT;
if (rcp == DEVICE_RECIPIENT)
{
if (detail->index == 0)
if (arg->index == 0)
{
/* Get Device Status */
uint8_t feature = dev_p->current_feature;
@@ -498,7 +500,7 @@ static int std_get_status (uint8_t req, struct control_info *detail)
else /* Self-powered */
status_info &= ~1;
return usb_lld_reply_request (&status_info, 2, detail);
return usb_lld_reply_request (&status_info, 2, arg);
}
}
else if (rcp == INTERFACE_RECIPIENT)
@@ -508,21 +510,21 @@ static int std_get_status (uint8_t req, struct control_info *detail)
if (dev_p->current_configuration == 0)
return USB_UNSUPPORT;
r = usb_cb_interface (USB_QUERY_INTERFACE, detail);
r = usb_cb_interface (USB_QUERY_INTERFACE, arg);
if (r != USB_SUCCESS)
return USB_UNSUPPORT;
return usb_lld_reply_request (&status_info, 2, detail);
return usb_lld_reply_request (&status_info, 2, arg);
}
else if (rcp == ENDPOINT_RECIPIENT)
{
uint8_t endpoint = (detail->index & 0x0f);
uint8_t endpoint = (arg->index & 0x0f);
uint16_t status;
if ((detail->index & 0x70) || endpoint == ENDP0)
if ((arg->index & 0x70) || endpoint == ENDP0)
return USB_UNSUPPORT;
if ((detail->index & 0x80))
if ((arg->index & 0x80))
{
status = st103_ep_get_tx_status (endpoint);
if (status == 0) /* Disabled */
@@ -539,25 +541,25 @@ static int std_get_status (uint8_t req, struct control_info *detail)
status_info |= 1; /* OUT Endpoint stalled */
}
return usb_lld_reply_request (&status_info, 2, detail);
return usb_lld_reply_request (&status_info, 2, arg);
}
return USB_UNSUPPORT;
}
static int std_clear_feature (uint8_t req, struct control_info *detail)
static int std_clear_feature (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
if ((req & REQUEST_DIR) == 1)
if (USB_SETUP_GET (req))
return USB_UNSUPPORT;
if (rcp == DEVICE_RECIPIENT)
{
if (detail->len != 0 || detail->index != 0)
if (arg->len != 0 || arg->index != 0)
return USB_UNSUPPORT;
if (detail->value == DEVICE_REMOTE_WAKEUP)
if (arg->value == DEVICE_REMOTE_WAKEUP)
{
dev_p->current_feature &= ~(1 << 5);
return USB_SUCCESS;
@@ -565,17 +567,17 @@ static int std_clear_feature (uint8_t req, struct control_info *detail)
}
else if (rcp == ENDPOINT_RECIPIENT)
{
uint8_t endpoint = (detail->index & 0x0f);
uint8_t endpoint = (arg->index & 0x0f);
uint16_t status;
if (dev_p->current_configuration == 0)
return USB_UNSUPPORT;
if (detail->len != 0 || (detail->index >> 8) != 0
|| detail->value != ENDPOINT_STALL || endpoint == ENDP0)
if (arg->len != 0 || (arg->index >> 8) != 0
|| arg->value != ENDPOINT_STALL || endpoint == ENDP0)
return USB_UNSUPPORT;
if ((detail->index & 0x80))
if ((arg->index & 0x80))
status = st103_ep_get_tx_status (endpoint);
else
status = st103_ep_get_rx_status (endpoint);
@@ -583,7 +585,7 @@ static int std_clear_feature (uint8_t req, struct control_info *detail)
if (status == 0) /* Disabled */
return USB_UNSUPPORT;
if (detail->index & 0x80) /* IN endpoint */
if (arg->index & 0x80) /* IN endpoint */
st103_ep_clear_dtog_tx (endpoint);
else /* OUT endpoint */
st103_ep_clear_dtog_rx (endpoint);
@@ -595,19 +597,19 @@ static int std_clear_feature (uint8_t req, struct control_info *detail)
return USB_UNSUPPORT;
}
static int std_set_feature (uint8_t req, struct control_info *detail)
static int std_set_feature (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
if ((req & REQUEST_DIR) == 1)
if (USB_SETUP_GET (req))
return USB_UNSUPPORT;
if (rcp == DEVICE_RECIPIENT)
{
if (detail->len != 0 || detail->index != 0)
if (arg->len != 0 || arg->index != 0)
return USB_UNSUPPORT;
if (detail->value == DEVICE_REMOTE_WAKEUP)
if (arg->value == DEVICE_REMOTE_WAKEUP)
{
dev_p->current_feature |= 1 << 5;
// event??
@@ -616,17 +618,17 @@ static int std_set_feature (uint8_t req, struct control_info *detail)
}
else if (rcp == ENDPOINT_RECIPIENT)
{
uint8_t endpoint = (detail->index & 0x0f);
uint8_t endpoint = (arg->index & 0x0f);
uint32_t status;
if (dev_p->current_configuration == 0)
return USB_UNSUPPORT;
if (detail->len != 0 || (detail->index >> 8) != 0
|| detail->value != 0 || endpoint == ENDP0)
if (arg->len != 0 || (arg->index >> 8) != 0
|| arg->value != 0 || endpoint == ENDP0)
return USB_UNSUPPORT;
if ((detail->index & 0x80))
if ((arg->index & 0x80))
status = st103_ep_get_tx_status (endpoint);
else
status = st103_ep_get_rx_status (endpoint);
@@ -634,7 +636,7 @@ static int std_set_feature (uint8_t req, struct control_info *detail)
if (status == 0) /* Disabled */
return USB_UNSUPPORT;
if (detail->index & 0x80)
if (arg->index & 0x80)
/* IN endpoint */
st103_ep_set_tx_status (endpoint, EP_TX_STALL);
else
@@ -648,96 +650,94 @@ static int std_set_feature (uint8_t req, struct control_info *detail)
return USB_UNSUPPORT;
}
static int std_set_address (uint8_t req, struct control_info *detail)
static int std_set_address (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
if ((req & REQUEST_DIR) == 1)
if (USB_SETUP_GET (req))
return USB_UNSUPPORT;
if (rcp == DEVICE_RECIPIENT && detail->len == 0 && detail->value <= 127
&& detail->index == 0 && dev_p->current_configuration == 0)
if (rcp == DEVICE_RECIPIENT && arg->len == 0 && arg->value <= 127
&& arg->index == 0 && dev_p->current_configuration == 0)
return USB_SUCCESS;
return USB_UNSUPPORT;
}
static int std_get_descriptor (uint8_t req, struct control_info *detail)
static int std_get_descriptor (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
if ((req & REQUEST_DIR) == 0)
if (USB_SETUP_SET (req))
return USB_UNSUPPORT;
return usb_cb_get_descriptor (rcp, (detail->value >> 8),
(detail->value & 0xff), detail);
return usb_cb_get_descriptor (rcp, (arg->value >> 8),
(arg->value & 0xff), arg);
}
static int std_get_configuration (uint8_t req, struct control_info *detail)
static int std_get_configuration (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
(void)detail;
if ((req & REQUEST_DIR) == 0)
if (USB_SETUP_SET (req))
return USB_UNSUPPORT;
if (rcp == DEVICE_RECIPIENT)
return usb_lld_reply_request (&dev_p->current_configuration, 1, detail);
return usb_lld_reply_request (&dev_p->current_configuration, 1, arg);
return USB_UNSUPPORT;
}
static int std_set_configuration (uint8_t req, struct control_info *detail)
static int std_set_configuration (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
if ((req & REQUEST_DIR) == 1)
if (USB_SETUP_GET (req))
return USB_UNSUPPORT;
if (rcp == DEVICE_RECIPIENT && detail->index == 0 && detail->len == 0)
return usb_cb_handle_event (USB_EVENT_CONFIG, detail->value);
if (rcp == DEVICE_RECIPIENT && arg->index == 0 && arg->len == 0)
return usb_cb_handle_event (USB_EVENT_CONFIG, arg->value);
return USB_UNSUPPORT;
}
static int std_get_interface (uint8_t req, struct control_info *detail)
static int std_get_interface (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
if ((req & REQUEST_DIR) == 0)
if (USB_SETUP_SET (req))
return USB_UNSUPPORT;
if (rcp == INTERFACE_RECIPIENT)
{
if (detail->value != 0 || (detail->index >> 8) != 0 || detail->len != 1)
if (arg->value != 0 || (arg->index >> 8) != 0 || arg->len != 1)
return USB_UNSUPPORT;
if (dev_p->current_configuration == 0)
return USB_UNSUPPORT;
return usb_cb_interface (USB_GET_INTERFACE, detail);
return usb_cb_interface (USB_GET_INTERFACE, arg);
}
return USB_UNSUPPORT;
}
static int std_set_interface (uint8_t req, struct control_info *detail)
static int std_set_interface (uint8_t req, struct req_args *arg)
{
uint8_t rcp = req & RECIPIENT;
if ((req & REQUEST_DIR) == 1 || rcp != INTERFACE_RECIPIENT
|| detail->len != 0 || (detail->index >> 8) != 0
|| (detail->value >> 8) != 0 || dev_p->current_configuration == 0)
if (USB_SETUP_GET (req) || rcp != INTERFACE_RECIPIENT
|| arg->len != 0 || (arg->index >> 8) != 0
|| (arg->value >> 8) != 0 || dev_p->current_configuration == 0)
return USB_UNSUPPORT;
return usb_cb_interface (USB_SET_INTERFACE, detail);
return usb_cb_interface (USB_SET_INTERFACE, arg);
}
static void handle_setup0 (void)
{
const uint16_t *pw;
struct control_info ctrl;
uint16_t w;
uint8_t req_no;
int r = USB_UNSUPPORT;
@@ -749,11 +749,11 @@ static void handle_setup0 (void)
dev_p->bmRequestType = w & 0xff;
dev_p->bRequest = req_no = w >> 8;
pw++;
ctrl.value = *pw++;
dev_p->value = *pw++;
pw++;
ctrl.index = *pw++;
dev_p->index = *pw++;
pw++;
ctrl.len = *pw;
dev_p->len = *pw;
data_p->addr = NULL;
data_p->len = 0;
@@ -777,11 +777,13 @@ static void handle_setup0 (void)
default: handler = std_none; break;
}
r = (*handler) (dev_p->bmRequestType, &ctrl);
r = (*handler) (dev_p->bmRequestType,
(struct req_args *)&dev_p->value);
}
}
else
r = usb_cb_setup (dev_p->bmRequestType, req_no, &ctrl);
r = usb_cb_setup (dev_p->bmRequestType, req_no,
(struct req_args *)&dev_p->value);
if (r != USB_SUCCESS)
dev_p->state = STALLED;
@@ -789,8 +791,7 @@ static void handle_setup0 (void)
{
if (USB_SETUP_SET (dev_p->bmRequestType))
{
dev_p->value = ctrl.value;
if (ctrl.len == 0)
if (dev_p->len == 0)
{
dev_p->state = WAIT_STATUS_IN;
st103_set_tx_count (ENDP0, 0);
@@ -820,7 +821,7 @@ static void handle_in0 (void)
}
else
usb_cb_ctrl_write_finish (dev_p->bmRequestType, dev_p->bRequest,
dev_p->value);
(struct req_args *)&dev_p->value);
dev_p->state = STALLED;
}
@@ -842,27 +843,6 @@ static void handle_out0 (void)
dev_p->state = STALLED;
}
static void nop_proc (void)
{
}
#define WEAK __attribute__ ((weak, alias ("nop_proc")))
void WEAK EP1_IN_Callback (void);
void WEAK EP2_IN_Callback (void);
void WEAK EP3_IN_Callback (void);
void WEAK EP4_IN_Callback (void);
void WEAK EP5_IN_Callback (void);
void WEAK EP6_IN_Callback (void);
void WEAK EP7_IN_Callback (void);
void WEAK EP1_OUT_Callback (void);
void WEAK EP2_OUT_Callback (void);
void WEAK EP3_OUT_Callback (void);
void WEAK EP4_OUT_Callback (void);
void WEAK EP5_OUT_Callback (void);
void WEAK EP6_OUT_Callback (void);
void WEAK EP7_OUT_Callback (void);
static void
usb_handle_transfer (uint16_t istr_value)
{
@@ -900,37 +880,21 @@ usb_handle_transfer (uint16_t istr_value)
if ((ep_value & EP_CTR_RX))
{
st103_ep_clear_ctr_rx (ep_index);
switch ((ep_index - 1))
{
case 0: EP1_OUT_Callback (); break;
case 1: EP2_OUT_Callback (); break;
case 2: EP3_OUT_Callback (); break;
case 3: EP4_OUT_Callback (); break;
case 4: EP5_OUT_Callback (); break;
case 5: EP6_OUT_Callback (); break;
case 6: EP7_OUT_Callback (); break;
}
usb_cb_rx_ready (ep_index);
}
if ((ep_value & EP_CTR_TX))
{
st103_ep_clear_ctr_tx (ep_index);
switch ((ep_index - 1))
{
case 0: EP1_IN_Callback (); break;
case 1: EP2_IN_Callback (); break;
case 2: EP3_IN_Callback (); break;
case 3: EP4_IN_Callback (); break;
case 4: EP5_IN_Callback (); break;
case 5: EP6_IN_Callback (); break;
case 6: EP7_IN_Callback (); break;
}
usb_cb_tx_done (ep_index);
}
}
}
void usb_lld_reset (void)
void usb_lld_reset (uint8_t feature)
{
usb_lld_set_configuration (0);
dev_p->current_feature = feature;
st103_set_btable ();
st103_set_daddr (0);
}
@@ -1037,14 +1001,9 @@ uint8_t usb_lld_current_configuration (void)
return dev_p->current_configuration;
}
void usb_lld_set_feature (uint8_t feature)
void usb_lld_set_data_to_recv (void *p, size_t len)
{
dev_p->current_feature = feature;
}
void usb_lld_set_data_to_recv (const void *p, size_t len)
{
data_p->addr = (uint8_t *)p;
data_p->addr = p;
data_p->len = len;
}
@@ -1129,7 +1088,7 @@ void usb_lld_from_pmabuf (void *dst, uint16_t addr, size_t n)
* BUFLEN: size of the data.
*/
int
usb_lld_reply_request (const void *buf, size_t buflen, struct control_info *ctl)
usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *ctl)
{
uint32_t len_asked = ctl->len;
uint32_t len;

31
example-fs-bb48/Makefile Normal file
View File

@@ -0,0 +1,31 @@
# Makefile for example application of Chopstx
PROJECT = sample
### Currently, it's for FS-BB48.
CHOPSTX = ..
LDSCRIPT= sample.ld
CSRC = sample.c first-pages.c usb_kl27z.c usb-cdc.c adc_kl27z.c
###################################
CROSS = arm-none-eabi-
CC = $(CROSS)gcc
LD = $(CROSS)gcc
OBJCOPY = $(CROSS)objcopy
MCU = cortex-m0plus
CWARN = -Wall -Wextra -Wstrict-prototypes
DEFS = -DFREE_STANDING -DMHZ=48
OPT = -O3 -Os -g
LIBS =
####################
include ../rules.mk
board.h:
@echo Please make a symbolic link \'board.h\' to a file in ../board;
@exit 1
distclean: clean
rm -f board.h

8
example-fs-bb48/adc.h Normal file
View File

@@ -0,0 +1,8 @@
void adc_init (void);
void adc_start (void);
void adc_stop (void);
extern uint32_t adc_buf[64];
void adc_start_conversion (int offset, int count);
int adc_wait_completion (chopstx_intr_t *intr);

223
example-fs-bb48/adc_kl27z.c Normal file
View File

@@ -0,0 +1,223 @@
/*
* adc_kl27z.c - ADC driver for KL27Z
* In this ADC driver, there are NeuG specific parts.
* You need to modify to use this as generic ADC driver.
*
* Copyright (C) 2016 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.
*
* Chopstx is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chopstx is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As additional permission under GNU GPL version 3 section 7, you may
* distribute non-source form of the Program without the copy of the
* GNU GPL normally required by section 4, provided you inform the
* receipents of GNU GPL by a written offer.
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>
#include "kl_sim.h"
#define INTR_REQ_ADC 15
struct ADC {
volatile uint32_t SC1[2];/* Status and Control Registers 1 */
volatile uint32_t CFG1; /* Configuration Register 1 */
volatile uint32_t CFG2; /* Configuration Register 2 */
volatile uint32_t R[2]; /* Data Result Register */
/* Compare Value Registers 1, 2 */
volatile uint32_t CV1;
volatile uint32_t CV2;
volatile uint32_t SC2; /* Status and Control Register 2 */
volatile uint32_t SC3; /* Status and Control Register 3 */
volatile uint32_t OFS; /* Offset Correction Register */
volatile uint32_t PG; /* Plus-Side Gain Register */
volatile uint32_t MG; /* Minus-Side Gain Register */
/* Plus-Side General Calibration Value Registers */
volatile uint32_t CLPD;
volatile uint32_t CLPS;
volatile uint32_t CLP4;
volatile uint32_t CLP3;
volatile uint32_t CLP2;
volatile uint32_t CLP1;
volatile uint32_t CLP0;
uint32_t rsvd0;
/* Minus-Side General Calibration Value Registers */
volatile uint32_t CLMD;
volatile uint32_t CLMS;
volatile uint32_t CLM4;
volatile uint32_t CLM3;
volatile uint32_t CLM2;
volatile uint32_t CLM1;
volatile uint32_t CLM0;
};
static struct ADC *const ADC = (struct ADC *const)0x4003B000;
/* SC1 */
#define ADC_SC1_DIFF (1 << 5)
#define ADC_SC1_AIEN (1 << 6)
#define ADC_SC1_COCO (1 << 7)
#define ADC_SC1_TEMPSENSOR 26
#define ADC_SC1_BANDGAP 27
#define ADC_SC1_ADCSTOP 31
/* CFG1 */
#define ADC_CLOCK_SOURCE_ASYNCH 3
#define ADC_RESOLUTION_16BIT 3
#define ADC_ADLSMP_SHORT 0
#define ADC_ADLSMP_LONG 1
#define ADC_ADIV_1 0
#define ADC_ADIV_8 3
#define ADC_ADLPC_NORMAL 1
/**/
#define ADC_CLOCK_SOURCE ADC_CLOCK_SOURCE_ASYNCH
#define ADC_MODE (ADC_RESOLUTION_16BIT << 2)
#define ADC_ADLSMP (ADC_ADLSMP_SHORT << 4)
#define ADC_ADIV (ADC_ADIV_8 << 5)
#define ADC_ADLPC (ADC_ADLPC_NORMAL << 7)
/* CFG2 */
#define ADC_ADLSTS_DEFAULT 0 /* 24 cycles if CFG1.ADLSMP=1, 4 if not. */
#define ADC_ADHSC_NORMAL 0
#define ADC_ADHSC_HIGHSPEED 1
#define ADC_ADACK_ENABLE 1
#define ADC_ADACK_DISABLE 0
#define ADC_MUXSEL_A 0
#define ADC_MUXSEL_B 1
/**/
#define ADC_ADLSTS ADC_ADLSTS_DEFAULT
#define ADC_ADHSC (ADC_ADHSC_NORMAL << 2)
#define ADC_ADACKEN (ADC_ADACK_ENABLE << 3)
#define ADC_MUXSEL (ADC_MUXSEL_A << 4)
/* SC2 */
#define ADC_SC2_ADTRG (1 << 6) /* For hardware trigger */
#define ADC_SC2_ACFE (1 << 5)
#define ADC_SC2_ACFGT (1 << 4)
#define ADC_SC2_ACREN (1 << 3)
#define ADC_SC2_DMAEN (1 << 2)
#define ADC_SC2_REFSEL_DEFAULT 0
/* SC3 */
#define ADC_SC3_CAL (1 << 7)
#define ADC_SC3_CALF (1 << 6)
#define ADC_SC3_ADCO (1 << 3)
#define ADC_SC3_AVGE (1 << 2)
#define ADC_SC3_AVGS11 0x03
/*
* Initialize ADC module, do calibration.
* This is called by MAIN, only once, before creating any other threads.
*/
int
adc_init (void)
{
uint32_t v;
/* Enable ADC0 clock. */
SIM->SCGC6 |= (1 << 27);
ADC->CFG1 = ADC_CLOCK_SOURCE | ADC_MODE | ADC_ADLSMP | ADC_ADIV | ADC_ADLPC;
ADC->CFG2 = ADC_ADLSTS | ADC_ADHSC | ADC_ADACKEN | ADC_MUXSEL;
ADC->SC2 = 0;
ADC->SC3 = ADC_SC3_CAL | ADC_SC3_CALF | ADC_SC3_AVGE | ADC_SC3_AVGS11;
ADC->SC1[0] = ADC_SC1_TEMPSENSOR;
/* Wait ADC completion */
while ((ADC->SC1[0] & ADC_SC1_COCO) == 0)
if ((ADC->SC3 & ADC_SC3_CALF) != 0)
/* Calibration failure */
return -1;
if ((ADC->SC3 & ADC_SC3_CALF) != 0)
/* Calibration failure */
return -1;
/* Configure PG by the calibration values. */
v = ADC->CLP0 + ADC->CLP1 + ADC->CLP2 + ADC->CLP3 + ADC->CLP4 + ADC->CLPS;
ADC->PG = 0x8000 | (v >> 1);
/* Configure MG by the calibration values. */
v = ADC->CLM0 + ADC->CLM1 + ADC->CLM2 + ADC->CLM3 + ADC->CLM4 + ADC->CLMS;
ADC->MG = 0x8000 | (v >> 1);
return 0;
}
/*
* Start using ADC.
*/
void
adc_start (void)
{
ADC->CFG1 = ADC_CLOCK_SOURCE | ADC_MODE | ADC_ADLSMP | ADC_ADIV | ADC_ADLPC;
ADC->CFG2 = ADC_ADLSTS | ADC_ADHSC | ADC_ADACKEN | ADC_MUXSEL;
ADC->SC2 = 0;
ADC->SC3 = 0;
}
/*
* Buffer to save ADC data.
*/
uint32_t adc_buf[64];
/*
* Kick getting data for COUNT times.
* Data will be saved in ADC_BUF starting at OFFSET.
*/
void
adc_start_conversion (int offset, int count)
{
ADC->SC1[0] = /*ADC_SC1_AIEN*/0 | ADC_SC1_TEMPSENSOR;
}
static void
adc_stop_conversion (void)
{
ADC->SC1[0] = ADC_SC1_ADCSTOP;
}
/*
* Stop using ADC.
*/
void
adc_stop (void)
{
SIM->SCGC6 &= ~(1 << 27);
}
/*
* Return 0 on success.
* Return 1 on error.
*/
int
adc_wait_completion (chopstx_intr_t *intr)
{
/* Wait ADC completion */
while ((ADC->SC1[0] & ADC_SC1_COCO) == 0)
;
adc_buf[0] = ADC->R[0];
adc_stop_conversion ();
return 0;
}

1
example-fs-bb48/board.h Symbolic link
View File

@@ -0,0 +1 @@
../board/board-fs-bb48.h

5
example-fs-bb48/crc32.h Normal file
View File

@@ -0,0 +1,5 @@
void crc32_init (void);
void crc32_u8 (unsigned char);
void crc32_u32 (unsigned int);
unsigned int crc32_value (void);

View File

@@ -0,0 +1,151 @@
/*
* first-pages.c - First pages for MKL27Z256.
*
* Copyright (C) 2016 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* Copying and distribution of this file, with or without modification,
* are permitted in any medium without royalty provided the copyright
* notice and this notice are preserved. This file is offered as-is,
* without any warranty.
*
*
* First two pages of Flash ROM is difficult to use because of
* predefined purposes. It's defined as a default vector page and
* flash configuration page.
*
* We put something useful to those two pages, together with the
* data for predefined purposes.
*/
#include <stdint.h>
extern uint8_t __main_stack_end__;
static void __attribute__ ((naked,section(".flash_config_page")))
reset (void)
{
uint32_t r3 = 0xe000ed08;
asm volatile ("str %2, [%0]\n\t" /* Set SCR->VTOR */
"ldr %0, [%2, #4]\n\t" /* Jump to the entry */
"bx %0\n\t"
".align 2\n"
: "=r" (r3)
: "0" (r3), "r" (0x00000800)
: "memory");
/* Never reach here. */
}
static uint32_t
stack_entry[] __attribute__ ((section(".first_page.first_words"),used)) = {
(uint32_t)(&__main_stack_end__ - 32),
(uint32_t)reset,
};
/*
* NOTE: We don't use backdoor comparison key. The area is used by
* CRC32 table.
*/
static uint32_t
flash_config[] __attribute__ ((section(".flash_config"),used)) = {
0xffffffff, /* Protection bytes */
0xffff3ffe, /* FSEC=0xfe, FOPT=0x3f */
/* FOPT=0x3f:
* BOOTSRC_SEL=00: Boot from flash
*/
/* FSEC=0xfe:
* unsecure
*/
};
/*
* CRC32 calculation routines.
*/
/*
* Table of CRC32, generated by gen_crc_table.py
*/
static unsigned int
crc32_table[256] __attribute__ ((section(".first_page"))) = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};
static unsigned int crc_reg;
__attribute__ ((section(".flash_config_page")))
void
crc32_init (void)
{
crc_reg = 0xffffffff;
}
__attribute__ ((section(".flash_config_page")))
unsigned int
crc32_value (void)
{
return crc_reg;
}
__attribute__ ((section(".flash_config_page")))
void
crc32_u8 (unsigned char bits_eight)
{
crc_reg = crc32_table[(crc_reg & 0xff) ^ bits_eight] ^ (crc_reg >> 8);
}
__attribute__ ((section(".flash_config_page")))
void
crc32_u32 (unsigned int u)
{
crc32_u8 (u & 0xff);
crc32_u8 ((u >> 8)& 0xff);
crc32_u8 ((u >> 16)& 0xff);
crc32_u8 ((u >> 24)& 0xff);
}

View File

@@ -0,0 +1,22 @@
#
# Polynomial for CRC32:
# x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
#
# When it is represented in binary, it's:
# 0x04C11DB7
# =
# 0000 0100 1100 0001 0001 1101 1011 0111
#
# When we put in reverse bit-order, it's
# 0xedb88320
for i in range(0,256):
c = i
for j in range(0,8):
if (c&1):
c = 0xEDB88320 ^ (c >> 1)
else:
c = c >> 1
print("0x%08x," % c),
if (i % 6) == 5:
print("")

31
example-fs-bb48/kl_sim.h Normal file
View File

@@ -0,0 +1,31 @@
/* System Integration Module. */
struct SIM {
volatile uint32_t SOPT1; /* System Options Register 1 */
volatile uint32_t SOPT1CFG; /* SOPT1 Configuration Register */
uint32_t reserved0[1023]; /* */
volatile uint32_t SOPT2; /* System Options Register 2 */
uint32_t reserved1[1]; /* */
volatile uint32_t SOPT4; /* System Options Register 4 */
volatile uint32_t SOPT5; /* System Options Register 5 */
uint32_t reserved2[1]; /* */
volatile uint32_t SOPT7; /* System Options Register 7 */
uint32_t reserved3[2]; /* */
volatile uint32_t SDID; /* System Device Identification Register */
uint32_t reserved4[3]; /* */
volatile uint32_t SCGC4; /* System Clock Gating Control Register 4 */
volatile uint32_t SCGC5; /* System Clock Gating Control Register 5 */
volatile uint32_t SCGC6; /* System Clock Gating Control Register 6 */
volatile uint32_t SCGC7; /* System Clock Gating Control Register 7 */
volatile uint32_t CLKDIV1; /* System Clock Divider Register 1 */
uint32_t reserved5[1]; /* */
volatile uint32_t FCFG1; /* Flash Configuration Register 1 */
volatile uint32_t FCFG2; /* Flash Configuration Register 2 */
uint32_t reserved6[1]; /* */
volatile uint32_t UIDMH; /* Unique Identification Register Mid-High */
volatile uint32_t UIDML; /* Unique Identification Register Mid Low */
volatile uint32_t UIDL; /* Unique Identification Register Low */
uint32_t reserved7[39]; /* */
volatile uint32_t COPC; /* COP Control Register */
volatile uint32_t SRVCOP; /* Service COP */
};
static struct SIM *const SIM = (struct SIM *const)0x40047000;

298
example-fs-bb48/sample.c Normal file
View File

@@ -0,0 +1,298 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <chopstx.h>
#include "usb_lld.h"
#include "stream.h"
#include "board.h"
#include "crc32.h"
#include "adc.h"
struct GPIO {
volatile uint32_t PDOR; /* Port Data Output Register */
volatile uint32_t PSOR; /* Port Set Output Register */
volatile uint32_t PCOR; /* Port Clear Output Register */
volatile uint32_t PTOR; /* Port Toggle Output Register */
volatile uint32_t PDIR; /* Port Data Input Register */
volatile uint32_t PDDR; /* Port Data Direction Register */
};
static struct GPIO *const GPIOB = (struct GPIO *const)0x400FF040;
static struct GPIO *const GPIOD = (struct GPIO *const)0x400FF0C0;
static struct GPIO *const GPIOE = (struct GPIO *const)0x400FF100;
static void
set_led (int on)
{
if (on)
GPIOB->PCOR = (1 << 0); /* PTB0: Clear: Light on */
else
GPIOB->PSOR = (1 << 0); /* PTB0: Set : Light off */
}
static chopstx_mutex_t mtx;
static chopstx_cond_t cnd0;
static chopstx_cond_t cnd1;
uint8_t u;
static uint8_t v;
static uint8_t m; /* 0..100 */
static void
wait_for (uint32_t usec)
{
#if defined(BUSY_LOOP)
uint32_t count = usec * 6;
uint32_t i;
for (i = 0; i < count; i++)
asm volatile ("" : : "r" (i) : "memory");
#else
chopstx_usec_wait (usec);
#endif
}
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);
wait_for (m);
set_led (0);
wait_for (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;
wait_for (200*1000);
v = 1;
wait_for (200*1000);
}
return NULL;
}
#define INTR_REQ_USB 24
static void *
usb_intr (void *arg)
{
extern void usb_lld_init (uint8_t feature);
extern void usb_interrupt_handler (void);
chopstx_intr_t interrupt;
(void)arg;
chopstx_claim_irq (&interrupt, INTR_REQ_USB);
usb_lld_init (0x80); /* Bus powered. */
while (1)
{
chopstx_intr_wait (&interrupt);
/* Process interrupt. */
usb_interrupt_handler ();
}
return NULL;
}
#if defined(BUSY_LOOP)
#define PRIO_PWM (CHOPSTX_SCHED_RR|1)
#define PRIO_BLK (CHOPSTX_SCHED_RR|1)
#else
#define PRIO_PWM 3
#define PRIO_BLK 2
#endif
#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__;
static char hexchar (uint8_t x)
{
x &= 0x0f;
if (x <= 0x09)
return '0' + x;
else if (x <= 0x0f)
return 'a' + x - 10;
else
return '?';
}
static int
check_recv (void *arg)
{
struct stream *s = arg;
if ((s->flags & FLAG_CONNECTED) == 0)
return 1;
if ((s->flags & FLAG_RECV_AVAIL))
return 1;
return 0;
}
int
main (int argc, const char *argv[])
{
struct stream *st;
uint8_t count;
extern uint32_t bDeviceState;
(void)argc;
(void)argv;
adc_init ();
adc_start ();
chopstx_mutex_init (&mtx);
chopstx_cond_init (&cnd0);
chopstx_cond_init (&cnd1);
st = stream_open ();
m = 10;
chopstx_create (PRIO_PWM, __stackaddr_pwm, __stacksize_pwm, pwm, NULL);
chopstx_create (PRIO_BLK, __stackaddr_blk, __stacksize_blk, blk, NULL);
chopstx_create (PRIO_INTR, __stackaddr_intr, __stacksize_intr,
usb_intr, NULL);
chopstx_usec_wait (200*1000);
chopstx_mutex_lock (&mtx);
chopstx_cond_signal (&cnd0);
chopstx_cond_signal (&cnd1);
chopstx_mutex_unlock (&mtx);
u = 1;
while (bDeviceState != CONFIGURED)
chopstx_usec_wait (500*1000);
count = 0;
while (1)
{
uint8_t s[64];
u = 1;
if (stream_wait_connection (st) < 0)
{
chopstx_usec_wait (1000*1000);
continue;
}
chopstx_usec_wait (500*1000);
/* Send ZLP at the beginning. */
stream_send (st, s, 0);
memcpy (s, "xx: Hello, World with Chopstx!\r\n", 32);
s[0] = hexchar (count >> 4);
s[1] = hexchar (count & 0x0f);
count++;
if (stream_send (st, s, 32) < 0)
continue;
while (1)
{
int size;
uint32_t usec;
struct chx_poll_cond poll_desc;
poll_desc.type = CHOPSTX_POLL_COND;
poll_desc.ready = 0;
poll_desc.cond = &st->cnd;
poll_desc.mutex = &st->mtx;
poll_desc.check = check_recv;
poll_desc.arg = st;
/* With chopstx_poll, we can do timed cond_wait */
usec = 3000000;
if (chopstx_poll (&usec, 1, &poll_desc))
{
size = stream_recv (st, s + 4);
if (size < 0)
break;
if (size >= 0)
{
unsigned int value;
if (s[4] == 't')
{
s[0] = 'T';
s[1] = 'M';
adc_start_conversion (0, 1);
adc_wait_completion (NULL);
value = adc_buf[0];
}
else
{
int i;
crc32_init ();
s[0] = hexchar (size >> 4);
s[1] = hexchar (size & 0x0f);
for (i = 0; i < size; i++)
crc32_u8 (s[4 + i]);
value = crc32_value () ^ 0xffffffff;
}
s[4] = hexchar (value >> 28);
s[5] = hexchar (value >> 24);
s[6] = hexchar (value >> 20);
s[7] = hexchar (value >> 16);
s[8] = hexchar (value >> 12);
s[9] = hexchar (value >> 8);
s[10] = hexchar (value >> 4);
s[11] = hexchar (value);
s[12] = '\r';
s[13] = '\n';
if (stream_send (st, s, 14) < 0)
break;
}
}
u ^= 1;
}
}
return 0;
}

135
example-fs-bb48/sample.ld Normal file
View File

@@ -0,0 +1,135 @@
/*
* MK27Z memory setup.
*/
__main_stack_size__ = 0x0100; /* Exception handlers */
__process0_stack_size__ = 0x0300; /* Main program */
__process1_stack_size__ = 0x0200; /* first thread program */
__process2_stack_size__ = 0x0100; /* second thread program */
__process3_stack_size__ = 0x0200; /* third thread program */
MEMORY
{
flash : org = 0x00000000, len = 256k
ram : org = 0x1fffe000, len = 32k
}
__ram_start__ = ORIGIN(ram);
__ram_size__ = 32k;
__ram_end__ = __ram_start__ + __ram_size__;
SECTIONS
{
. = 0;
_text = .;
.f2 : ALIGN(16) SUBALIGN(8)
{
KEEP(*(.first_page.first_words))
KEEP(*(.first_page))
KEEP(*(.flash_config))
KEEP(*(.flash_config_page))
} > flash =0xffffffff
.text : ALIGN(16) SUBALIGN(16)
{
. = ALIGN(1024);
KEEP(*(.startup.vectors))
. = ALIGN(16);
*(.text.startup.*)
*(.text)
*(.text.*)
*(.rodata)
*(.rodata.*)
*(.glue_7t)
*(.glue_7)
*(.gcc*)
. = ALIGN(8);
} > flash =0xffffffff
/DISCARD/ :
{
*(.bss.startup.0)
}
.ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)} > flash
.ARM.exidx : {
PROVIDE(__exidx_start = .);
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
PROVIDE(__exidx_end = .);
} > flash =0xffffffff
.eh_frame_hdr : {*(.eh_frame_hdr)} > flash
.eh_frame : ONLY_IF_RO {*(.eh_frame)} > flash
.textalign : ONLY_IF_RO { . = ALIGN(8); } > flash
_etext = .;
_textdata = _etext;
.process_stack :
{
. = ALIGN(8);
__process3_stack_base__ = .;
. += __process3_stack_size__;
. = ALIGN(8);
__process3_stack_end__ = .;
__process2_stack_base__ = .;
. += __process2_stack_size__;
. = ALIGN(8);
__process2_stack_end__ = .;
__process1_stack_base__ = .;
. += __process1_stack_size__;
. = ALIGN(8);
__process1_stack_end__ = .;
__process0_stack_base__ = .;
. += __process0_stack_size__;
. = ALIGN(8);
__process0_stack_end__ = .;
} > ram
.main_stack :
{
. = ALIGN(8);
__main_stack_base__ = .;
. += __main_stack_size__;
. = ALIGN(8);
__main_stack_end__ = .;
} > ram
.data :
{
. = ALIGN(4);
PROVIDE(_data = .);
*(.data)
. = ALIGN(4);
*(.data.*)
. = ALIGN(4);
*(.ramtext)
. = ALIGN(4);
PROVIDE(_edata = .);
} > ram AT > flash =0xffffffff
.bss :
{
. = ALIGN(4);
PROVIDE(_bss_start = .);
*(.bss)
. = ALIGN(4);
*(.bss.*)
. = ALIGN(4);
*(COMMON)
. = ALIGN(512);
__usb_bdt__ = .;
. += 512;
PROVIDE(_bss_end = .);
} > ram
PROVIDE(end = .);
_end = .;
}
__heap_base__ = _end;
__heap_end__ = __ram_end__;

26
example-fs-bb48/stream.h Normal file
View File

@@ -0,0 +1,26 @@
#define BUFSIZE 128
#define FLAG_CONNECTED (1 << 0)
#define FLAG_SEND_AVAIL (1 << 1)
#define FLAG_RECV_AVAIL (1 << 2)
/*
* Current implementation is synchronous and buffers are not yet used.
*/
struct stream {
chopstx_mutex_t mtx;
chopstx_cond_t cnd;
int sending;
unsigned int recv_len;
uint8_t recv_buf[BUFSIZE];
uint8_t buf_send[BUFSIZE]; /* Not yet used. */
uint8_t cnt_send_head; /* Not yet used. */
uint8_t cnt_send_tail; /* Not yet used. */
uint8_t cnt_recv_head; /* Not yet used. */
uint8_t cnt_recv_tail; /* Not yet used. */
uint32_t flags;
};
struct stream *stream_open (void);
int stream_wait_connection (struct stream *st);
int stream_send (struct stream *st, uint8_t *buf, uint8_t count);
int stream_recv (struct stream *st, uint8_t *buf);

617
example-fs-bb48/usb-cdc.c Normal file
View File

@@ -0,0 +1,617 @@
#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>
#include <string.h>
#include "usb_lld.h"
#include "stream.h"
static uint8_t send_buf[64];
static unsigned int send_len;
static uint8_t send_buf1[64];
static uint8_t recv_buf[64];
static unsigned int recv_len;
static uint8_t inputline[64];
static unsigned int inputline_len;
static struct stream stream;
#define USB_CDC_REQ_SET_LINE_CODING 0x20
#define USB_CDC_REQ_GET_LINE_CODING 0x21
#define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
#define USB_CDC_REQ_SEND_BREAK 0x23
/* USB Device Descriptor */
static const uint8_t vcom_device_desc[18] = {
18, /* bLength */
DEVICE_DESCRIPTOR, /* bDescriptorType */
0x10, 0x01, /* bcdUSB = 1.1 */
0x02, /* bDeviceClass (CDC). */
0x00, /* bDeviceSubClass. */
0x00, /* bDeviceProtocol. */
0x40, /* bMaxPacketSize. */
0xFF, 0xFF, /* idVendor */
0x01, 0x00, /* idProduct */
0x00, 0x01, /* bcdDevice */
1, /* iManufacturer. */
2, /* iProduct. */
3, /* iSerialNumber. */
1 /* bNumConfigurations. */
};
/* Configuration Descriptor tree for a CDC.*/
static const uint8_t vcom_config_desc[67] = {
9,
CONFIG_DESCRIPTOR, /* bDescriptorType: Configuration */
/* Configuration Descriptor.*/
67, 0x00, /* wTotalLength. */
0x02, /* bNumInterfaces. */
0x01, /* bConfigurationValue. */
0, /* iConfiguration. */
0x80, /* bmAttributes (bus powered). */
50, /* bMaxPower (100mA). */
/* Interface Descriptor.*/
9,
INTERFACE_DESCRIPTOR,
0x00, /* bInterfaceNumber. */
0x00, /* bAlternateSetting. */
0x01, /* bNumEndpoints. */
0x02, /* bInterfaceClass (Communications Interface Class,
CDC section 4.2). */
0x02, /* bInterfaceSubClass (Abstract Control Model, CDC
section 4.3). */
0x01, /* bInterfaceProtocol (AT commands, CDC section
4.4). */
0, /* iInterface. */
/* Header Functional Descriptor (CDC section 5.2.3).*/
5, /* bLength. */
0x24, /* bDescriptorType (CS_INTERFACE). */
0x00, /* bDescriptorSubtype (Header Functional Descriptor). */
0x10, 0x01, /* bcdCDC. */
/* Call Management Functional Descriptor. */
5, /* bFunctionLength. */
0x24, /* bDescriptorType (CS_INTERFACE). */
0x01, /* bDescriptorSubtype (Call Management Functional
Descriptor). */
0x03, /* bmCapabilities (D0+D1). */
0x01, /* bDataInterface. */
/* ACM Functional Descriptor.*/
4, /* bFunctionLength. */
0x24, /* bDescriptorType (CS_INTERFACE). */
0x02, /* bDescriptorSubtype (Abstract Control Management
Descriptor). */
0x02, /* bmCapabilities. */
/* Union Functional Descriptor.*/
5, /* bFunctionLength. */
0x24, /* bDescriptorType (CS_INTERFACE). */
0x06, /* bDescriptorSubtype (Union Functional
Descriptor). */
0x00, /* bMasterInterface (Communication Class
Interface). */
0x01, /* bSlaveInterface0 (Data Class Interface). */
/* Endpoint 2 Descriptor.*/
7,
ENDPOINT_DESCRIPTOR,
ENDP2|0x80, /* bEndpointAddress. */
0x03, /* bmAttributes (Interrupt). */
0x08, 0x00, /* wMaxPacketSize. */
0xFF, /* bInterval. */
/* Interface Descriptor.*/
9,
INTERFACE_DESCRIPTOR, /* bDescriptorType: */
0x01, /* bInterfaceNumber. */
0x00, /* bAlternateSetting. */
0x02, /* bNumEndpoints. */
0x0A, /* bInterfaceClass (Data Class Interface, CDC section 4.5). */
0x00, /* bInterfaceSubClass (CDC section 4.6). */
0x00, /* bInterfaceProtocol (CDC section 4.7). */
0x00, /* iInterface. */
/* Endpoint 3 Descriptor.*/
7,
ENDPOINT_DESCRIPTOR, /* bDescriptorType: Endpoint */
ENDP3, /* bEndpointAddress. */
0x02, /* bmAttributes (Bulk). */
0x40, 0x00, /* wMaxPacketSize. */
0x00, /* bInterval. */
/* Endpoint 1 Descriptor.*/
7,
ENDPOINT_DESCRIPTOR, /* bDescriptorType: Endpoint */
ENDP1|0x80, /* bEndpointAddress. */
0x02, /* bmAttributes (Bulk). */
0x40, 0x00, /* wMaxPacketSize. */
0x00 /* bInterval. */
};
/*
* U.S. English language identifier.
*/
static const uint8_t vcom_string0[4] = {
4, /* bLength */
STRING_DESCRIPTOR,
0x09, 0x04 /* LangID = 0x0409: US-English */
};
static const uint8_t vcom_string1[] = {
23*2+2, /* bLength */
STRING_DESCRIPTOR, /* bDescriptorType */
/* Manufacturer: "Flying Stone Technology" */
'F', 0, 'l', 0, 'y', 0, 'i', 0, 'n', 0, 'g', 0, ' ', 0, 'S', 0,
't', 0, 'o', 0, 'n', 0, 'e', 0, ' ', 0, 'T', 0, 'e', 0, 'c', 0,
'h', 0, 'n', 0, 'o', 0, 'l', 0, 'o', 0, 'g', 0, 'y', 0,
};
static const uint8_t vcom_string2[] = {
14*2+2, /* bLength */
STRING_DESCRIPTOR, /* bDescriptorType */
/* Product name: "Chopstx Sample" */
'C', 0, 'h', 0, 'o', 0, 'p', 0, 's', 0, 't', 0, 'x', 0, ' ', 0,
'S', 0, 'a', 0, 'm', 0, 'p', 0, 'l', 0, 'e', 0,
};
/*
* Serial Number string.
*/
static const uint8_t vcom_string3[28] = {
28, /* bLength */
STRING_DESCRIPTOR, /* bDescriptorType */
'0', 0, '.', 0, '0', 0, '0', 0, /* Version number */
};
#define NUM_INTERFACES 2
uint32_t bDeviceState = UNCONNECTED; /* USB device status */
void
usb_cb_device_reset (void)
{
usb_lld_reset (vcom_config_desc[7]);
/* Initialize Endpoint 0 */
usb_lld_setup_endpoint (ENDP0, 1, 1);
chopstx_mutex_lock (&stream.mtx);
stream.flags = 0;
bDeviceState = ATTACHED;
chopstx_mutex_unlock (&stream.mtx);
}
#define CDC_CTRL_DTR 0x0001
void
usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg)
{
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT)
&& USB_SETUP_SET (req) && req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
{
/* Open/close the connection. */
chopstx_mutex_lock (&stream.mtx);
stream.flags &= ~FLAG_CONNECTED;
stream.flags |= ((arg->value & CDC_CTRL_DTR) != 0)? FLAG_CONNECTED : 0;
chopstx_cond_signal (&stream.cnd);
recv_len = 0;
usb_lld_rx_enable (ENDP3, recv_buf, 64);
chopstx_mutex_unlock (&stream.mtx);
}
}
struct line_coding
{
uint32_t bitrate;
uint8_t format;
uint8_t paritytype;
uint8_t datatype;
} __attribute__((packed));
static struct line_coding line_coding = {
115200, /* baud rate: 115200 */
0x00, /* stop bits: 1 */
0x00, /* parity: none */
0x08 /* bits: 8 */
};
static int
vcom_port_data_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
{
if (USB_SETUP_GET (req))
{
if (req_no == USB_CDC_REQ_GET_LINE_CODING)
return usb_lld_reply_request (&line_coding, sizeof(line_coding), arg);
}
else /* USB_SETUP_SET (req) */
{
if (req_no == USB_CDC_REQ_SET_LINE_CODING
&& arg->len == sizeof (line_coding))
{
usb_lld_set_data_to_recv (&line_coding, sizeof (line_coding));
return USB_SUCCESS;
}
else if (req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
return USB_SUCCESS;
}
return USB_UNSUPPORT;
}
int
usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
{
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0)
return vcom_port_data_setup (req, req_no, arg);
return USB_UNSUPPORT;
}
int
usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
struct req_args *arg)
{
if (rcp != DEVICE_RECIPIENT)
return USB_UNSUPPORT;
if (desc_type == DEVICE_DESCRIPTOR)
return usb_lld_reply_request (vcom_device_desc, sizeof (vcom_device_desc),
arg);
else if (desc_type == CONFIG_DESCRIPTOR)
return usb_lld_reply_request (vcom_config_desc, sizeof (vcom_config_desc),
arg);
else if (desc_type == STRING_DESCRIPTOR)
{
const uint8_t *str;
int size;
switch (desc_index)
{
case 0:
str = vcom_string0;
size = sizeof (vcom_string0);
break;
case 1:
str = vcom_string1;
size = sizeof (vcom_string1);
break;
case 2:
str = vcom_string2;
size = sizeof (vcom_string2);
break;
case 3:
str = vcom_string3;
size = sizeof (vcom_string3);
break;
default:
return USB_UNSUPPORT;
}
return usb_lld_reply_request (str, size, arg);
}
return USB_UNSUPPORT;
}
static void
vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
{
if (interface == 0)
{
if (!stop)
usb_lld_setup_endpoint (ENDP2, 0, 1);
else
usb_lld_stall (ENDP2);
}
else if (interface == 1)
{
if (!stop)
{
usb_lld_setup_endpoint (ENDP1, 0, 1);
usb_lld_setup_endpoint (ENDP3, 1, 0);
#if 0
/* Start with no data receiving */
usb_lld_stall (ENDP3);
#endif
}
else
{
usb_lld_stall (ENDP1);
usb_lld_stall (ENDP3);
}
}
}
int
usb_cb_handle_event (uint8_t event_type, uint16_t value)
{
int i;
uint8_t current_conf;
switch (event_type)
{
case USB_EVENT_ADDRESS:
bDeviceState = ADDRESSED;
return USB_SUCCESS;
case USB_EVENT_CONFIG:
current_conf = usb_lld_current_configuration ();
if (current_conf == 0)
{
if (value != 1)
return USB_UNSUPPORT;
usb_lld_set_configuration (1);
for (i = 0; i < NUM_INTERFACES; i++)
vcom_setup_endpoints_for_interface (i, 0);
bDeviceState = CONFIGURED;
}
else if (current_conf != value)
{
if (value != 0)
return USB_UNSUPPORT;
usb_lld_set_configuration (0);
for (i = 0; i < NUM_INTERFACES; i++)
vcom_setup_endpoints_for_interface (i, 1);
bDeviceState = ADDRESSED;
}
/* Do nothing when current_conf == value */
return USB_SUCCESS;
return USB_SUCCESS;
default:
break;
}
return USB_UNSUPPORT;
}
int
usb_cb_interface (uint8_t cmd, struct req_args *arg)
{
const uint8_t zero = 0;
uint16_t interface = arg->index;
uint16_t alt = arg->value;
if (interface >= NUM_INTERFACES)
return USB_UNSUPPORT;
switch (cmd)
{
case USB_SET_INTERFACE:
if (alt != 0)
return USB_UNSUPPORT;
else
{
vcom_setup_endpoints_for_interface (interface, 0);
return USB_SUCCESS;
}
case USB_GET_INTERFACE:
return usb_lld_reply_request (&zero, 1, arg);
default:
case USB_QUERY_INTERFACE:
return USB_SUCCESS;
}
}
static void
stream_echo_char (int c)
{
chopstx_mutex_lock (&stream.mtx);
if (send_len < sizeof (send_buf))
send_buf[send_len++] = c;
else
{
/* All that we can is ignoring the output. */
;
}
if (stream.sending == 0)
{
memcpy (send_buf1, send_buf, send_len);
usb_lld_tx_enable (ENDP1, send_buf1, send_len);
send_len = 0;
stream.sending = 1;
}
chopstx_mutex_unlock (&stream.mtx);
}
void
usb_cb_tx_done (uint8_t ep_num)
{
if (ep_num == ENDP1)
{
chopstx_mutex_lock (&stream.mtx);
stream.sending = 0;
if (send_len)
{
stream.sending = 1;
memcpy (send_buf1, send_buf, send_len);
usb_lld_tx_enable (ENDP1, send_buf1, send_len);
send_len = 0;
}
else
{
if ((stream.flags & FLAG_SEND_AVAIL))
{
stream.flags &= ~FLAG_SEND_AVAIL;
chopstx_cond_signal (&stream.cnd);
}
}
chopstx_mutex_unlock (&stream.mtx);
}
else if (ep_num == ENDP2)
{
/* Nothing */
}
}
static void
stream_input_char (int c)
{
unsigned int i;
/* Process DEL, C-U, C-R, and RET as editing command. */
switch (c)
{
case 0x0d: /* Control-M */
stream_echo_char (0x0d);
stream_echo_char (0x0a);
chopstx_mutex_lock (&stream.mtx);
if ((stream.flags & FLAG_RECV_AVAIL) == 0)
{
memcpy (stream.recv_buf, inputline, inputline_len);
stream.recv_len = inputline_len;
stream.flags |= FLAG_RECV_AVAIL;
chopstx_cond_signal (&stream.cnd);
}
chopstx_mutex_unlock (&stream.mtx);
inputline_len = 0;
break;
case 0x12: /* Control-R */
stream_echo_char ('^');
stream_echo_char ('R');
stream_echo_char (0x0d);
stream_echo_char (0x0a);
for (i = 0; i < inputline_len; i++)
stream_echo_char (inputline[i]);
break;
case 0x15: /* Control-U */
for (i = 0; i < inputline_len; i++)
{
stream_echo_char (0x08);
stream_echo_char (0x20);
stream_echo_char (0x08);
}
inputline_len = 0;
break;
case 0x7f: /* DEL */
if (inputline_len > 0)
{
stream_echo_char (0x08);
stream_echo_char (0x20);
stream_echo_char (0x08);
inputline_len--;
}
break;
default:
if (inputline_len < sizeof (inputline))
{
stream_echo_char (c);
inputline[inputline_len++] = c;
}
else
/* Beep */
stream_echo_char (0x0a);
break;
}
}
void
usb_cb_rx_ready (uint8_t ep_num)
{
if (ep_num == ENDP3)
{
int i, r;
r = usb_lld_rx_data_len (ENDP3);
for (i = 0; i < r; i++)
stream_input_char (recv_buf[i]);
usb_lld_rx_enable (ENDP3, recv_buf, 64);
}
}
struct stream *
stream_open (void)
{
chopstx_mutex_init (&stream.mtx);
chopstx_cond_init (&stream.cnd);
return &stream;
}
int
stream_wait_connection (struct stream *st)
{
chopstx_mutex_lock (&st->mtx);
while ((stream.flags & FLAG_CONNECTED) == 0)
chopstx_cond_wait (&st->cnd, &st->mtx);
chopstx_mutex_unlock (&st->mtx);
stream.flags &= ~FLAG_SEND_AVAIL;
return 0;
}
int
stream_send (struct stream *st, uint8_t *buf, uint8_t count)
{
int r = 0;
chopstx_mutex_lock (&st->mtx);
if ((stream.flags & FLAG_CONNECTED) == 0)
r = -1;
else
{
stream.sending = 1;
usb_lld_tx_enable (ENDP1, buf, count);
stream.flags |= FLAG_SEND_AVAIL;
do
{
chopstx_cond_wait (&st->cnd, &st->mtx);
if ((stream.flags & FLAG_SEND_AVAIL) == 0)
break;
else if ((stream.flags & FLAG_CONNECTED) == 0)
{
r = -1;
break;
}
}
while (1);
}
stream.sending = 0;
chopstx_mutex_unlock (&st->mtx);
return r;
}
int
stream_recv (struct stream *st, uint8_t *buf)
{
int r;
chopstx_mutex_lock (&st->mtx);
if ((stream.flags & FLAG_CONNECTED) == 0)
r = -1;
else
{
while (1)
{
if ((stream.flags & FLAG_RECV_AVAIL))
{
r = stream.recv_len;
memcpy (buf, stream.recv_buf, r);
stream.flags &= ~FLAG_RECV_AVAIL;
break;
}
else if ((stream.flags & FLAG_CONNECTED) == 0)
{
r = -1;
break;
}
chopstx_cond_wait (&st->cnd, &st->mtx);
}
}
chopstx_mutex_unlock (&st->mtx);
return r;
}

1034
example-fs-bb48/usb_kl27z.c Normal file

File diff suppressed because it is too large Load Diff

115
example-fs-bb48/usb_lld.h Normal file
View File

@@ -0,0 +1,115 @@
#define STANDARD_ENDPOINT_DESC_SIZE 0x09
/* endpoints enumeration */
#define ENDP0 ((uint8_t)0)
#define ENDP1 ((uint8_t)1)
#define ENDP2 ((uint8_t)2)
#define ENDP3 ((uint8_t)3)
#define ENDP4 ((uint8_t)4)
#define ENDP5 ((uint8_t)5)
#define ENDP6 ((uint8_t)6)
#define ENDP7 ((uint8_t)7)
/* EP_TYPE[1:0] EndPoint TYPE */
#define EP_BULK (0x0000) /* EndPoint BULK */
#define EP_CONTROL (0x0200) /* EndPoint CONTROL */
#define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */
#define EP_INTERRUPT (0x0600) /* EndPoint INTERRUPT */
enum RECIPIENT_TYPE
{
DEVICE_RECIPIENT, /* Recipient device */
INTERFACE_RECIPIENT, /* Recipient interface */
ENDPOINT_RECIPIENT, /* Recipient endpoint */
OTHER_RECIPIENT
};
enum DESCRIPTOR_TYPE
{
DEVICE_DESCRIPTOR = 1,
CONFIG_DESCRIPTOR,
STRING_DESCRIPTOR,
INTERFACE_DESCRIPTOR,
ENDPOINT_DESCRIPTOR
};
#define REQUEST_DIR 0x80 /* Mask to get request dir */
#define REQUEST_TYPE 0x60 /* Mask to get request type */
#define STANDARD_REQUEST 0x00 /* Standard request */
#define CLASS_REQUEST 0x20 /* Class request */
#define VENDOR_REQUEST 0x40 /* Vendor request */
#define RECIPIENT 0x1F /* Mask to get recipient */
#define USB_SETUP_SET(req) ((req & REQUEST_DIR) == 0)
#define USB_SETUP_GET(req) ((req & REQUEST_DIR) != 0)
enum
{
USB_UNSUPPORT = 0,
USB_SUCCESS = 1,
};
struct req_args {
uint16_t value;
uint16_t index;
uint16_t len;
};
void usb_cb_device_reset (void);
int usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg);
int usb_cb_interface (uint8_t cmd, struct req_args *arg);
int usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
struct req_args *arg);
int usb_cb_handle_event (uint8_t event_type, uint16_t value);
void usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no,
struct req_args *arg);
void usb_cb_tx_done (uint8_t ep_num);
void usb_cb_rx_ready (uint8_t ep_num);
enum {
USB_EVENT_ADDRESS,
USB_EVENT_CONFIG,
USB_EVENT_SUSPEND,
USB_EVENT_WAKEUP,
USB_EVENT_STALL,
};
enum {
USB_SET_INTERFACE,
USB_GET_INTERFACE,
USB_QUERY_INTERFACE,
};
enum DEVICE_STATE
{
UNCONNECTED,
ATTACHED,
POWERED,
SUSPENDED,
ADDRESSED,
CONFIGURED
};
void usb_lld_init (uint8_t feature);
int usb_lld_reply_request (const void *buf, size_t buflen,
struct req_args *arg);
void usb_lld_set_data_to_recv (void *p, size_t len);
void usb_lld_tx_enable (uint8_t ep_num, const void *buf, size_t len);
int usb_lld_tx_result (int ep_num);
void usb_lld_rx_enable (int ep_num, void *buf, size_t len);
int usb_lld_rx_data_len (int ep_num);
void usb_lld_stall (int ep_num);
void usb_lld_reset (uint8_t feature);
void usb_lld_setup_endpoint (int n, int rx_en, int tx_en);
void usb_lld_set_configuration (uint8_t config);
uint8_t usb_lld_current_configuration (void);
void usb_lld_prepare_shutdown (void);
void usb_lld_shutdown (void);
void usb_interrupt_handler (void);

View File

@@ -31,7 +31,6 @@ reset (void)
}
extern uint8_t __main_stack_end__;
extern void svc (void);
extern void preempt (void);
extern void chx_timer_expired (void);
extern void chx_handle_intr (void);
@@ -44,13 +43,6 @@ static void nmi (void)
static void __attribute__ ((naked))
hard_fault (void)
{
register uint32_t primask;
asm ("mrs %0, PRIMASK" : "=r" (primask));
if (primask)
asm volatile ("b svc");
else
for (;;);
}
@@ -89,7 +81,7 @@ handler vector[] __attribute__ ((section(".vectors"))) = {
none,
/* 0x20 */
none, none, none, /* reserved */
svc, /* SVCall */
none, /* SVCall */
none, /* Debug */
none, /* reserved */
preempt, /* PendSV */

View File

@@ -17,7 +17,7 @@
#include <stdlib.h>
#include "board.h"
#include "clk_gpio_init.c"
#include "clk_gpio_init-stm32.c"
#define CORTEX_PRIORITY_BITS 4
#define CORTEX_PRIORITY_MASK(n) ((n) << (8 - CORTEX_PRIORITY_BITS))
@@ -25,17 +25,17 @@
#define STM32_USB_IRQ_PRIORITY 11
struct NVIC {
uint32_t ISER[8];
uint32_t unused1[24];
uint32_t ICER[8];
uint32_t unused2[24];
uint32_t ISPR[8];
uint32_t unused3[24];
uint32_t ICPR[8];
uint32_t unused4[24];
uint32_t IABR[8];
uint32_t unused5[56];
uint32_t IPR[60];
volatile uint32_t ISER[8];
volatile uint32_t unused1[24];
volatile uint32_t ICER[8];
volatile uint32_t unused2[24];
volatile uint32_t ISPR[8];
volatile uint32_t unused3[24];
volatile uint32_t ICPR[8];
volatile uint32_t unused4[24];
volatile uint32_t IABR[8];
volatile uint32_t unused5[56];
volatile uint32_t IPR[60];
};
static struct NVIC *const NVICBase = ((struct NVIC *const)0xE000E100);

View File

@@ -113,10 +113,6 @@ void adc3_conversion (uint32_t *result)
void adc3_stop (void)
{
#if USE_ADC3_INTR
chopstx_release_irq (&adc3_intr);
#endif
/* Power off. */
ADC3->CR1 = 0;
ADC3->CR2 = 0;

View File

@@ -27,7 +27,7 @@
(RCC_APB2RSTR_IOPARST | RCC_APB2RSTR_IOPDRST | RCC_APB2RSTR_IOPERST \
| RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_IOPBRST)
#include "clk_gpio_init.c"
#include "clk_gpio_init-stm32.c"
static struct GPIO *const GPIO_OTHER1 = ((struct GPIO *const) GPIOC_BASE);
static struct GPIO *const GPIO_OTHER2 = ((struct GPIO *const) GPIOB_BASE);
@@ -71,17 +71,17 @@ gpio_init_primer2 (void)
#define STM32_USB_IRQ_PRIORITY 11
struct NVIC {
uint32_t ISER[8];
uint32_t unused1[24];
uint32_t ICER[8];
uint32_t unused2[24];
uint32_t ISPR[8];
uint32_t unused3[24];
uint32_t ICPR[8];
uint32_t unused4[24];
uint32_t IABR[8];
uint32_t unused5[56];
uint32_t IPR[60];
volatile uint32_t ISER[8];
volatile uint32_t unused1[24];
volatile uint32_t ICER[8];
volatile uint32_t unused2[24];
volatile uint32_t ISPR[8];
volatile uint32_t unused3[24];
volatile uint32_t ICPR[8];
volatile uint32_t unused4[24];
volatile uint32_t IABR[8];
volatile uint32_t unused5[56];
volatile uint32_t IPR[60];
};
static struct NVIC *const NVICBase = ((struct NVIC *const)0xE000E100);