Compare commits
12 Commits
release/0.
...
release/1.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40adf95c24 | ||
|
|
5a7381ece6 | ||
|
|
52626a3368 | ||
|
|
440188c373 | ||
|
|
a6541cbcc6 | ||
|
|
74c7b5bcb3 | ||
|
|
4d7e97028e | ||
|
|
d061e6f931 | ||
|
|
5f1c26ff17 | ||
|
|
78718e57df | ||
|
|
a756987d2a | ||
|
|
b6e3a1aba1 |
15
AUTHORS
15
AUTHORS
@@ -23,16 +23,21 @@ Mateusz Zalega:
|
||||
|
||||
NIIBE Yutaka:
|
||||
Write the library:
|
||||
chopstx.c, eventflag.c, entry.c,
|
||||
clk_gpio_init-kl.c, clk_gpio_init-stm32.c
|
||||
chopstx.c, entry.c, eventflag.c,
|
||||
chopstx.h, eventflag.h
|
||||
Write the drivers mcu/*:
|
||||
clk_gpio_init-mkl27z.c, clk_gpio_init-stm32.c,
|
||||
sys-stm32f103.c, sys-stm32f030.c, sys-mkl27z.c,
|
||||
adc-stm32f103.c, adc-mkl27z.c
|
||||
Draw the logo:
|
||||
chopstx.svg, chopstx.png
|
||||
Write examples:
|
||||
example-led, example-cdc, example-fsm-55
|
||||
Write board:
|
||||
example-led, example-cdc, example-fsm-55, example-fs-bb48
|
||||
Write board/*:
|
||||
board-fst-01.h, board-fst-01-00.h,
|
||||
board-olimex-stm32-h103.h, board-stm8s-discovery.h
|
||||
board-cq-starm.h, board-stbee-mini.h, board-stbee.h,
|
||||
|
||||
board-stm32f0-discovery.h, board-fsm-55.h
|
||||
board-stm32f0-discovery.h, board-fsm-55.h,
|
||||
|
||||
board-fs-bb48.h
|
||||
|
||||
67
ChangeLog
67
ChangeLog
@@ -1,3 +1,70 @@
|
||||
2016-06-16 Niibe Yutaka <gniibe@fsij.org>
|
||||
|
||||
* VERSION: 1.0.
|
||||
* doc/chopstx.texi (VERSION): 1.0.
|
||||
|
||||
* board/board-fst-01g.h: New.
|
||||
|
||||
2016-06-15 Niibe Yutaka <gniibe@fsij.org>
|
||||
|
||||
* chopstx.c (chopstx_exit): Add "naked" attribute.
|
||||
|
||||
2016-06-14 Niibe Yutaka <gniibe@fsij.org>
|
||||
|
||||
* mcu/usb-stm32f103.c (usb_lld_ctrl_recv): Fix the state and make
|
||||
the endpoint 0 RX_VALID.
|
||||
|
||||
2016-06-10 NIIBE Yutaka <gniibe@fsij.org>
|
||||
|
||||
* usb_lld.h (USB_EVENT_OK): Rename.
|
||||
* mcu/usb-stm32f103.c: Update.
|
||||
* mcu/usb-mkl27z.c: Likewise.
|
||||
|
||||
* example-cdc/usb-cdc.c: Follow the change of API.
|
||||
* example-fs-bb48/usb-cdc.c: Likewise.
|
||||
|
||||
2016-06-09 NIIBE Yutaka <gniibe@fsij.org>
|
||||
|
||||
* mcu/usb-stm32f103.c (usb_lld_ctrl_recv): Rename.
|
||||
(usb_lld_ctrl_send): Rename.
|
||||
(usb_lld_ctrl_ack): Rename and let have return value.
|
||||
* mcu/usb-mkl27z.c: Likewise.
|
||||
|
||||
* example-cdc/usb-cdc.c: Follow the change of API.
|
||||
* example-fs-bb48/usb-cdc.c: Likewise.
|
||||
|
||||
2016-06-08 NIIBE Yutaka <gniibe@fsij.org>
|
||||
|
||||
* mcu/usb-stm32f103.c: Rewrite to be event driven API.
|
||||
* mcu/usb-mkl27z.c: Likewise.
|
||||
|
||||
* example-cdc/usb-cdc.c: Update to new USB API.
|
||||
* example-fs-bb48/usb-cdc.c: Likewise.
|
||||
|
||||
* example-cdc/tty.h: Change tty API to be C string (char*)
|
||||
friendly.
|
||||
* example-fs-bb48/tty.h: Ditto.
|
||||
|
||||
2016-06-02 Niibe Yutaka <gniibe@fsij.org>
|
||||
|
||||
* contrib/adc-mkl27z.c: Move from mcu.
|
||||
* contrib/adc-stm32f103.c: Move from mcu.
|
||||
|
||||
* example-led/Makefile (CHIP): Define as stm32f0.
|
||||
|
||||
* mcu/sys-stm32f0.h: Rename from sys-stm32f030.h,
|
||||
as we share it among STM32F030 and STM32F050.
|
||||
* mcu/sys-stm32f0.c: Likewise.
|
||||
|
||||
* board/board-stm32f0-discovery.h (MCU_STM32F0): Define.
|
||||
|
||||
* sys.h: Support STM32F0.
|
||||
|
||||
2016-06-01 NIIBE Yutaka <gniibe@fsij.org>
|
||||
|
||||
* chopstx.h (chx_fatal): Remove weak attribute from declaration.
|
||||
* chopstx.c (chx_fatal): Add weak attribute to implementation.
|
||||
|
||||
2016-05-31 NIIBE Yutaka <gniibe@fsij.org>
|
||||
|
||||
* VERSION: 0.12.
|
||||
|
||||
15
NEWS
15
NEWS
@@ -1,6 +1,21 @@
|
||||
NEWS - Noteworthy changes
|
||||
|
||||
|
||||
* Major changes in Chopstx 1.0
|
||||
|
||||
Released 2016-06-16
|
||||
|
||||
** New USB API
|
||||
Now, USB driver is included in Chopstx. So, it should be good one.
|
||||
It used to be the code which was derived from interrupt driven API
|
||||
with callbacks. It's changed to event driven API, so that a user can
|
||||
do as wish, beyond the restriction of callbacks.
|
||||
|
||||
** New board support: FST-01G
|
||||
FST-01G is a new revision of original FST-01 with fixed pull-up of
|
||||
D+ line.
|
||||
|
||||
|
||||
* Major changes in Chopstx 0.12
|
||||
|
||||
Released 2016-05-31
|
||||
|
||||
4
README
4
README
@@ -1,6 +1,6 @@
|
||||
Chopstx - Threads and only Threads
|
||||
Version 0.12
|
||||
2016-05-31
|
||||
Version 1.0
|
||||
2016-06-16
|
||||
Niibe Yutaka
|
||||
Flying Stone Technology
|
||||
|
||||
|
||||
84
board/board-fst-01g.h
Normal file
84
board/board-fst-01g.h
Normal file
@@ -0,0 +1,84 @@
|
||||
#define BOARD_NAME "FST-01G"
|
||||
#define BOARD_ID 0x8801277f
|
||||
/* echo -n "FST-01G" | sha256sum | sed -e 's/^.*\(........\) -$/\1/' */
|
||||
|
||||
#define STM32F10X_MD /* Medium-density device */
|
||||
|
||||
#define STM32_PLLXTPRE STM32_PLLXTPRE_DIV1
|
||||
#define STM32_PLLMUL_VALUE 6
|
||||
#define STM32_HSECLK 12000000
|
||||
|
||||
#define GPIO_LED_BASE GPIOB_BASE
|
||||
#define GPIO_LED_SET_TO_EMIT 0
|
||||
#define GPIO_USB_BASE GPIOA_BASE
|
||||
#undef GPIO_OTHER_BASE
|
||||
|
||||
/*
|
||||
* Port A setup.
|
||||
* PA0 - input with pull-up (TIM2_CH1): AN0 for NeuG
|
||||
* PA1 - input with pull-down (TIM2_CH2)
|
||||
* PA2 - input with pull-up (TIM2_CH3) connected to CIR module
|
||||
* PA3 - input with pull-up: external pin available to user
|
||||
* PA4 - Push pull output (SPI1_NSS)
|
||||
* PA5 - Alternate Push pull output (SPI1_SCK)
|
||||
* PA6 - Alternate Push pull output (SPI1_MISO)
|
||||
* PA7 - Alternate Push pull output (SPI1_MOSI)
|
||||
* PA10 - Push pull output 1 default
|
||||
* (so that binary for FST-01G also works on FST-01)
|
||||
* PA11 - Push Pull output 10MHz 0 default (until USB enabled) (USBDM)
|
||||
* PA12 - Push Pull output 10MHz 0 default (until USB enabled) (USBDP)
|
||||
* ------------------------ Default
|
||||
* PA8 - input with pull-up.
|
||||
* PA9 - input with pull-up.
|
||||
* PA13 - input with pull-up.
|
||||
* PA14 - input with pull-up.
|
||||
* PA15 - input with pull-up.
|
||||
*/
|
||||
#define VAL_GPIO_USB_ODR 0xFFFFE7FD
|
||||
#define VAL_GPIO_USB_CRL 0xBBB38888 /* PA7...PA0 */
|
||||
#define VAL_GPIO_USB_CRH 0x88811388 /* PA15...PA8 */
|
||||
|
||||
/*
|
||||
* Port B setup.
|
||||
* PB0 - Push pull output (LED 1:ON 0:OFF)
|
||||
* PB1 - input with pull-up: AN9 for NeuG
|
||||
* ------------------------ Default
|
||||
* PBx - input with pull-up.
|
||||
*/
|
||||
#define VAL_GPIO_LED_ODR 0xFFFFFFFF
|
||||
#define VAL_GPIO_LED_CRL 0x88888883 /* PA7...PA0 */
|
||||
#define VAL_GPIO_LED_CRH 0x88888888 /* PA15...PA8 */
|
||||
|
||||
#define RCC_ENR_IOP_EN (RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN)
|
||||
#define RCC_RSTR_IOP_RST (RCC_APB2RSTR_IOPARST | RCC_APB2RSTR_IOPBRST)
|
||||
|
||||
/*
|
||||
* Board specific information other than clock and GPIO initial
|
||||
* setting should not be in board-*.h, but each driver should include
|
||||
* information by itself.
|
||||
*
|
||||
* Please see NeuG's ADC driver how board specific handling is done.
|
||||
*
|
||||
* Given the situation of Chopstx's boards support, which is not that
|
||||
* huge, this works well. If scalability and flexibility will matter,
|
||||
* we will need something like device tree in which boot process can
|
||||
* pass information to application program.
|
||||
*
|
||||
* Following constants are here, because experimental CIR driver is
|
||||
* written before this design decision of Chopstx.
|
||||
*
|
||||
* Those will be removed soon, once such an driver will be improved
|
||||
* in new style.
|
||||
*/
|
||||
/* For pin-cir settings of Gnuk */
|
||||
#define TIMx TIM2
|
||||
#define INTR_REQ_TIM TIM2_IRQ
|
||||
#define AFIO_EXTICR_INDEX 0
|
||||
#define AFIO_EXTICR1_EXTIx_Py AFIO_EXTICR1_EXTI2_PA
|
||||
#define EXTI_PR EXTI_PR_PR2
|
||||
#define EXTI_IMR EXTI_IMR_MR2
|
||||
#define EXTI_FTSR_TR EXTI_FTSR_TR2
|
||||
#define INTR_REQ_EXTI EXTI2_IRQ
|
||||
#define ENABLE_RCC_APB1
|
||||
#define RCC_APBnENR_TIMxEN RCC_APB1ENR_TIM2EN
|
||||
#define RCC_APBnRSTR_TIMxRST RCC_APB1RSTR_TIM2RST
|
||||
@@ -1,6 +1,8 @@
|
||||
#define BOARD_NAME "STM32F0 Discovery"
|
||||
#define BOARD_ID 0xde4b4bc1
|
||||
|
||||
#define MCU_STM32F0 1
|
||||
|
||||
/*
|
||||
* Running at 48MHz with HSI as clock source.
|
||||
*
|
||||
|
||||
14
chopstx.c
14
chopstx.c
@@ -208,6 +208,8 @@ chx_prio_init (void)
|
||||
| (CPU_EXCEPTION_PRIORITY_PENDSV << 16));
|
||||
}
|
||||
|
||||
void __attribute__((weak)) chx_fatal (uint32_t err_code);
|
||||
|
||||
/**
|
||||
* chx_fatal - Fatal error point.
|
||||
* @err_code: Error code
|
||||
@@ -444,8 +446,8 @@ enum {
|
||||
THREAD_WAIT_MTX,
|
||||
THREAD_WAIT_CND,
|
||||
THREAD_WAIT_TIME,
|
||||
THREAD_WAIT_EXIT,
|
||||
THREAD_WAIT_POLL,
|
||||
THREAD_JOIN,
|
||||
/**/
|
||||
THREAD_EXITED=0x0E,
|
||||
THREAD_FINISHED=0x0F
|
||||
@@ -1191,7 +1193,7 @@ requeue (struct chx_thread *tp)
|
||||
chx_spin_unlock (&cond->lock);
|
||||
/* We don't know who can wake up this thread. */
|
||||
}
|
||||
else if (tp->state == THREAD_JOIN)
|
||||
else if (tp->state == THREAD_WAIT_EXIT)
|
||||
/* Requeue is not needed as waiting for the thread is only by one. */
|
||||
return (struct chx_thread *)tp->v;
|
||||
|
||||
@@ -1500,6 +1502,12 @@ chopstx_cleanup_pop (int execute)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* We put "naked" attribute to chopstx_exit function. Since it never
|
||||
* returns, function prologue is not needed.
|
||||
*/
|
||||
void chopstx_exit (void *retval) __attribute__ ((naked));
|
||||
|
||||
/**
|
||||
* chopstx_exit - Terminate the execution of running thread
|
||||
* @retval: Return value (to be caught by a joining thread)
|
||||
@@ -1572,7 +1580,7 @@ chopstx_join (chopstx_t thd, void **ret)
|
||||
chx_spin_lock (&q_join.lock);
|
||||
ll_prio_enqueue ((struct chx_pq *)running, &q_join.q);
|
||||
running->v = (uint32_t)tp;
|
||||
running->state = THREAD_JOIN;
|
||||
running->state = THREAD_WAIT_EXIT;
|
||||
tp->flag_join_req = 1;
|
||||
|
||||
/* Priority inheritance. */
|
||||
|
||||
@@ -83,7 +83,7 @@ void chopstx_cond_broadcast (chopstx_cond_t *cond);
|
||||
* Library provides default implementation as weak reference.
|
||||
* User can replace it.
|
||||
*/
|
||||
void chx_fatal (uint32_t err_code) __attribute__((__weak__, __noreturn__));
|
||||
void chx_fatal (uint32_t err_code) __attribute__((__noreturn__));
|
||||
|
||||
int chopstx_join (chopstx_t, void **);
|
||||
void chopstx_exit (void *retval) __attribute__((__noreturn__));
|
||||
|
||||
@@ -7,22 +7,26 @@
|
||||
* Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of NeuG, a True Random Number Generator
|
||||
* implementation based on quantization error of ADC (for STM32F103).
|
||||
* This file is a part of Chopstx, a thread library for embedded.
|
||||
*
|
||||
* NeuG is free software: you can redistribute it and/or modify it
|
||||
* 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.
|
||||
*
|
||||
* NeuG 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.
|
||||
* 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>
|
||||
@@ -1,7 +1,7 @@
|
||||
\input texinfo @c -*-texinfo-*-
|
||||
@c %**start of header
|
||||
@setfilename chopstx.info
|
||||
@set VERSION 0.12
|
||||
@set VERSION 1.0
|
||||
@settitle Chopstx Reference Manual
|
||||
@c Unify some of the indices.
|
||||
@syncodeindex tp fn
|
||||
@@ -83,8 +83,9 @@ Indexes
|
||||
@node Introduction
|
||||
@chapter Introduction
|
||||
|
||||
Chopstx is an RT thread library for ARM Cortex-M0 and Cortex-M3.
|
||||
Specifically, it is used for STM32F030, STM32F103, and KL27Z.
|
||||
Chopstx is an RT thread library for ARM Cortex-M0, Cortex-M0plus and
|
||||
Cortex-M3. Specifically, it is used for STM32F030, MKL27Z and
|
||||
STM32F103.
|
||||
|
||||
While most RTOSes come with many features, drivers, and stacks,
|
||||
Chopstx just offers a RT thread library.
|
||||
|
||||
@@ -117,7 +117,7 @@ main (int argc, const char *argv[])
|
||||
m = 50;
|
||||
while (1)
|
||||
{
|
||||
uint8_t s[LINEBUFSIZE];
|
||||
char s[LINEBUFSIZE];
|
||||
|
||||
u = 1;
|
||||
tty_wait_connection (tty);
|
||||
|
||||
@@ -5,5 +5,5 @@ 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);
|
||||
int tty_send (struct tty *tty, const char *buf, int count);
|
||||
int tty_recv (struct tty *tty, char *buf, uint32_t *timeout);
|
||||
|
||||
@@ -30,7 +30,7 @@ static const struct line_coding line_coding0 = {
|
||||
*
|
||||
* 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)
|
||||
* directy accesses TTY0 (usb_device_reset and usb_set_configuration)
|
||||
* should be modified, too.
|
||||
*
|
||||
* Modification of TTY_MAIN thread will be also needed to echo back
|
||||
@@ -234,10 +234,10 @@ static const uint8_t vcom_string3[28] = {
|
||||
#define NUM_INTERFACES 2
|
||||
|
||||
|
||||
void
|
||||
usb_cb_device_reset (void)
|
||||
static void
|
||||
usb_device_reset (struct usb_dev *dev)
|
||||
{
|
||||
usb_lld_reset (VCOM_FEATURE_BUS_POWERED);
|
||||
usb_lld_reset (dev, VCOM_FEATURE_BUS_POWERED);
|
||||
|
||||
/* Initialize Endpoint 0 */
|
||||
usb_lld_setup_endpoint (ENDP0, EP_CONTROL, 0, ENDP0_RXADDR, ENDP0_TXADDR, 64);
|
||||
@@ -256,13 +256,15 @@ usb_cb_device_reset (void)
|
||||
|
||||
#define CDC_CTRL_DTR 0x0001
|
||||
|
||||
void
|
||||
usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
static void
|
||||
usb_ctrl_write_finish (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t type_rcp = arg->type & (REQUEST_TYPE|RECIPIENT);
|
||||
|
||||
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0
|
||||
&& USB_SETUP_SET (req) && req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
&& USB_SETUP_SET (arg->type)
|
||||
&& arg->request == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
{
|
||||
struct tty *t = tty_get (arg->index, 0);
|
||||
|
||||
@@ -272,63 +274,74 @@ usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
chopstx_cond_signal (&t->cnd);
|
||||
chopstx_mutex_unlock (&t->mtx);
|
||||
}
|
||||
|
||||
/*
|
||||
* The transaction was already finished. So, it is no use to call
|
||||
* usb_lld_ctrl_error when the condition does not match.
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
vcom_port_data_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
vcom_port_data_setup (struct usb_dev *dev)
|
||||
{
|
||||
if (USB_SETUP_GET (req))
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
{
|
||||
struct tty *t = tty_get (arg->index, 0);
|
||||
|
||||
if (req_no == USB_CDC_REQ_GET_LINE_CODING)
|
||||
return usb_lld_reply_request (&t->line_coding,
|
||||
sizeof (struct line_coding), arg);
|
||||
if (arg->request == USB_CDC_REQ_GET_LINE_CODING)
|
||||
return usb_lld_ctrl_send (dev, &t->line_coding,
|
||||
sizeof (struct line_coding));
|
||||
}
|
||||
else /* USB_SETUP_SET (req) */
|
||||
{
|
||||
if (req_no == USB_CDC_REQ_SET_LINE_CODING
|
||||
if (arg->request == USB_CDC_REQ_SET_LINE_CODING
|
||||
&& arg->len == sizeof (struct line_coding))
|
||||
{
|
||||
struct tty *t = tty_get (arg->index, 0);
|
||||
|
||||
usb_lld_set_data_to_recv (&t->line_coding,
|
||||
return usb_lld_ctrl_recv (dev, &t->line_coding,
|
||||
sizeof (struct line_coding));
|
||||
return USB_SUCCESS;
|
||||
}
|
||||
else if (req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
return USB_SUCCESS;
|
||||
else if (arg->request == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
return usb_lld_ctrl_ack (dev);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
static int
|
||||
usb_setup (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t type_rcp = arg->type & (REQUEST_TYPE|RECIPIENT);
|
||||
|
||||
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0)
|
||||
return vcom_port_data_setup (req, req_no, arg);
|
||||
return vcom_port_data_setup (dev);
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
|
||||
struct req_args *arg)
|
||||
static int
|
||||
usb_get_descriptor (struct usb_dev *dev)
|
||||
{
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
uint8_t desc_type = (arg->value >> 8);
|
||||
uint8_t desc_index = (arg->value & 0xff);
|
||||
|
||||
if (rcp != DEVICE_RECIPIENT)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if (desc_type == DEVICE_DESCRIPTOR)
|
||||
return usb_lld_reply_request (vcom_device_desc, sizeof (vcom_device_desc),
|
||||
arg);
|
||||
return usb_lld_ctrl_send (dev,
|
||||
vcom_device_desc, sizeof (vcom_device_desc));
|
||||
else if (desc_type == CONFIG_DESCRIPTOR)
|
||||
return usb_lld_reply_request (vcom_config_desc, sizeof (vcom_config_desc),
|
||||
arg);
|
||||
return usb_lld_ctrl_send (dev,
|
||||
vcom_config_desc, sizeof (vcom_config_desc));
|
||||
else if (desc_type == STRING_DESCRIPTOR)
|
||||
{
|
||||
const uint8_t *str;
|
||||
@@ -353,13 +366,13 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
|
||||
size = sizeof (vcom_string3);
|
||||
break;
|
||||
default:
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return usb_lld_reply_request (str, size, arg);
|
||||
return usb_lld_ctrl_send (dev, str, size);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -388,85 +401,89 @@ vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
usb_cb_handle_event (uint8_t event_type, uint16_t value)
|
||||
static int
|
||||
usb_set_configuration (struct usb_dev *dev)
|
||||
{
|
||||
int i;
|
||||
uint8_t current_conf;
|
||||
|
||||
switch (event_type)
|
||||
current_conf = usb_lld_current_configuration (dev);
|
||||
if (current_conf == 0)
|
||||
{
|
||||
case USB_EVENT_ADDRESS:
|
||||
if (dev->dev_req.value != 1)
|
||||
return -1;
|
||||
|
||||
usb_lld_set_configuration (dev, 1);
|
||||
for (i = 0; i < NUM_INTERFACES; i++)
|
||||
vcom_setup_endpoints_for_interface (i, 0);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = CONFIGURED;
|
||||
chopstx_cond_signal (&tty0.cnd);
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
}
|
||||
else if (current_conf != dev->dev_req.value)
|
||||
{
|
||||
if (dev->dev_req.value != 0)
|
||||
return -1;
|
||||
|
||||
usb_lld_set_configuration (dev, 0);
|
||||
for (i = 0; i < NUM_INTERFACES; i++)
|
||||
vcom_setup_endpoints_for_interface (i, 1);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = ADDRESSED;
|
||||
chopstx_cond_signal (&tty0.cnd);
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
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);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = CONFIGURED;
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
}
|
||||
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);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = ADDRESSED;
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
}
|
||||
/* Do nothing when current_conf == value */
|
||||
return USB_SUCCESS;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
usb_lld_ctrl_ack (dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
usb_cb_interface (uint8_t cmd, struct req_args *arg)
|
||||
static int
|
||||
usb_set_interface (struct usb_dev *dev)
|
||||
{
|
||||
const uint8_t zero = 0;
|
||||
uint16_t interface = arg->index;
|
||||
uint16_t alt = arg->value;
|
||||
uint16_t interface = dev->dev_req.index;
|
||||
uint16_t alt = dev->dev_req.value;
|
||||
|
||||
if (interface >= NUM_INTERFACES)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
switch (cmd)
|
||||
if (alt != 0)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
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;
|
||||
vcom_setup_endpoints_for_interface (interface, 0);
|
||||
usb_lld_ctrl_ack (dev);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
usb_get_interface (struct usb_dev *dev)
|
||||
{
|
||||
const uint8_t zero = 0;
|
||||
uint16_t interface = dev->dev_req.index;
|
||||
|
||||
if (interface >= NUM_INTERFACES)
|
||||
return -1;
|
||||
|
||||
/* We don't have alternate interface, so, always return 0. */
|
||||
return usb_lld_ctrl_send (dev, &zero, 1);
|
||||
}
|
||||
|
||||
static int
|
||||
usb_get_status_interface (struct usb_dev *dev)
|
||||
{
|
||||
const uint16_t status_info = 0;
|
||||
uint16_t interface = dev->dev_req.index;
|
||||
|
||||
if (interface >= NUM_INTERFACES)
|
||||
return -1;
|
||||
|
||||
return usb_lld_ctrl_send (dev, &status_info, 2);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Put a character into the ring buffer to be send back.
|
||||
@@ -515,8 +532,8 @@ tty_echo_char (struct tty *t, int c)
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
usb_cb_tx_done (uint8_t ep_num, uint32_t len)
|
||||
static void
|
||||
usb_tx_done (uint8_t ep_num, uint16_t len)
|
||||
{
|
||||
struct tty *t = tty_get (-1, ep_num);
|
||||
|
||||
@@ -598,19 +615,18 @@ tty_input_char (struct tty *t, int c)
|
||||
return r;
|
||||
}
|
||||
|
||||
void
|
||||
usb_cb_rx_ready (uint8_t ep_num)
|
||||
static void
|
||||
usb_rx_ready (uint8_t ep_num, uint16_t len)
|
||||
{
|
||||
uint8_t recv_buf[64];
|
||||
struct tty *t = tty_get (-1, ep_num);
|
||||
|
||||
if (ep_num == ENDP3)
|
||||
{
|
||||
int i, r;
|
||||
int i;
|
||||
|
||||
r = usb_lld_rx_data_len (ENDP3);
|
||||
usb_lld_rxcpy (recv_buf, ep_num, 0, r);
|
||||
for (i = 0; i < r; i++)
|
||||
usb_lld_rxcpy (recv_buf, ep_num, 0, len);
|
||||
for (i = 0; i < len; i++)
|
||||
if (tty_input_char (t, recv_buf[i]))
|
||||
break;
|
||||
|
||||
@@ -652,6 +668,8 @@ static void *
|
||||
tty_main (void *arg)
|
||||
{
|
||||
struct tty *t = arg;
|
||||
struct usb_dev dev;
|
||||
int e;
|
||||
|
||||
#if defined(OLDER_SYS_H)
|
||||
/*
|
||||
@@ -660,9 +678,9 @@ tty_main (void *arg)
|
||||
*
|
||||
* 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.
|
||||
* invocation of usb_lld_event_handler won't occur.
|
||||
*
|
||||
* Calling usb_interrupt_handler is no harm even if there were no
|
||||
* Calling usb_lld_event_handler is no harm even if there were no
|
||||
* interrupts, thus, we call it unconditionally here, just in case
|
||||
* if there is a request.
|
||||
*
|
||||
@@ -671,19 +689,114 @@ tty_main (void *arg)
|
||||
* chopstx_claim_irq after usb_lld_init overrides that.
|
||||
*
|
||||
*/
|
||||
usb_lld_init (VCOM_FEATURE_BUS_POWERED);
|
||||
usb_lld_init (&dev, VCOM_FEATURE_BUS_POWERED);
|
||||
chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
|
||||
usb_interrupt_handler ();
|
||||
goto event_handle;
|
||||
#else
|
||||
chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
|
||||
usb_lld_init (VCOM_FEATURE_BUS_POWERED);
|
||||
usb_lld_init (&dev, VCOM_FEATURE_BUS_POWERED);
|
||||
#endif
|
||||
|
||||
while (1)
|
||||
{
|
||||
chopstx_poll (NULL, 1, &usb_intr);
|
||||
if (usb_intr.ready)
|
||||
usb_interrupt_handler ();
|
||||
{
|
||||
uint8_t ep_num;
|
||||
#if defined(OLDER_SYS_H)
|
||||
event_handle:
|
||||
#endif
|
||||
/*
|
||||
* When interrupt is detected, call usb_lld_event_handler.
|
||||
* The event may be one of following:
|
||||
* (1) Transfer to endpoint (bulk or interrupt)
|
||||
* In this case EP_NUM is encoded in the variable E.
|
||||
* (2) "NONE" event: some trasfer was done, but all was
|
||||
* done by lower layer, no other work is needed in
|
||||
* upper layer.
|
||||
* (3) Device events: Reset or Suspend
|
||||
* (4) Device requests to the endpoint zero.
|
||||
*
|
||||
*/
|
||||
e = usb_lld_event_handler (&dev);
|
||||
ep_num = USB_EVENT_ENDP (e);
|
||||
|
||||
if (ep_num != 0)
|
||||
{
|
||||
if (USB_EVENT_TXRX (e))
|
||||
usb_tx_done (ep_num, USB_EVENT_LEN (e));
|
||||
else
|
||||
usb_rx_ready (ep_num, USB_EVENT_LEN (e));
|
||||
}
|
||||
else
|
||||
switch (USB_EVENT_ID (e))
|
||||
{
|
||||
case USB_EVENT_DEVICE_RESET:
|
||||
usb_device_reset (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_DEVICE_ADDRESSED:
|
||||
/* The addres is assigned to the device. We don't
|
||||
* need to do anything for this actually, but in this
|
||||
* application, we maintain the USB status of the
|
||||
* device. Usually, just "continue" as EVENT_OK is
|
||||
* OK.
|
||||
*/
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = ADDRESSED;
|
||||
chopstx_cond_signal (&tty0.cnd);
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_GET_DESCRIPTOR:
|
||||
if (usb_get_descriptor (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_SET_CONFIGURATION:
|
||||
if (usb_set_configuration (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_SET_INTERFACE:
|
||||
if (usb_set_interface (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_CTRL_REQUEST:
|
||||
/* Device specific device request. */
|
||||
if (usb_setup (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_GET_STATUS_INTERFACE:
|
||||
if (usb_get_status_interface (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_GET_INTERFACE:
|
||||
if (usb_get_interface (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_SET_FEATURE_DEVICE:
|
||||
case USB_EVENT_SET_FEATURE_ENDPOINT:
|
||||
case USB_EVENT_CLEAR_FEATURE_DEVICE:
|
||||
case USB_EVENT_CLEAR_FEATURE_ENDPOINT:
|
||||
usb_lld_ctrl_ack (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_CTRL_WRITE_FINISH:
|
||||
/* Control WRITE transfer finished. */
|
||||
usb_ctrl_write_finish (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_OK:
|
||||
case USB_EVENT_DEVICE_SUSPEND:
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
chopstx_mutex_lock (&t->mtx);
|
||||
if (t->device_state == CONFIGURED && t->flag_connected
|
||||
@@ -743,10 +856,10 @@ check_tx (struct tty *t)
|
||||
}
|
||||
|
||||
int
|
||||
tty_send (struct tty *t, uint8_t *buf, int len)
|
||||
tty_send (struct tty *t, const char *buf, int len)
|
||||
{
|
||||
int r;
|
||||
uint8_t *p;
|
||||
const char *p;
|
||||
int count;
|
||||
|
||||
p = buf;
|
||||
@@ -801,7 +914,7 @@ check_rx (void *arg)
|
||||
*
|
||||
*/
|
||||
int
|
||||
tty_recv (struct tty *t, uint8_t *buf, uint32_t *timeout)
|
||||
tty_recv (struct tty *t, char *buf, uint32_t *timeout)
|
||||
{
|
||||
int r;
|
||||
chopstx_poll_cond_t poll_desc;
|
||||
|
||||
@@ -23,7 +23,7 @@ struct command_table
|
||||
static void
|
||||
put_line (struct tty *tty, const char *line)
|
||||
{
|
||||
tty_send (tty, (uint8_t *)line, strlen (line));
|
||||
tty_send (tty, line, strlen (line));
|
||||
}
|
||||
|
||||
static const char *help_string =
|
||||
@@ -203,7 +203,7 @@ cmd_mdw (struct tty *tty, const char *line)
|
||||
|
||||
*s++ = '\r';
|
||||
*s++ = '\n';
|
||||
tty_send (tty, (uint8_t *)output, s - output);
|
||||
tty_send (tty, output, s - output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,7 +326,7 @@ cmd_crc32 (struct tty *tty, const char *line)
|
||||
s = compose_hex (string, v);
|
||||
*s++ = '\r';
|
||||
*s++ = '\n';
|
||||
tty_send (tty, (uint8_t *)string, sizeof (string));
|
||||
tty_send (tty, string, sizeof (string));
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -369,7 +369,7 @@ cmd_adc (struct tty *tty, const char *line)
|
||||
{
|
||||
*s++ = '\r';
|
||||
*s++ = '\n';
|
||||
tty_send (tty, (uint8_t *)output, s - output);
|
||||
tty_send (tty, output, s - output);
|
||||
s = output;
|
||||
if (i >= 64)
|
||||
break;
|
||||
@@ -393,14 +393,14 @@ cmd_sysinfo (struct tty *tty, const char *line)
|
||||
*s++ = sys_version[6];
|
||||
*s++ = '\r';
|
||||
*s++ = '\n';
|
||||
tty_send (tty, (uint8_t *)output, s - output);
|
||||
tty_send (tty, output, s - output);
|
||||
|
||||
memcpy (output, "Board ID: ", 10);
|
||||
s = output + 10;
|
||||
s = compose_hex (s, sys_board_id);
|
||||
*s++ = '\r';
|
||||
*s++ = '\n';
|
||||
tty_send (tty, (uint8_t *)output, s - output);
|
||||
tty_send (tty, output, s - output);
|
||||
|
||||
memcpy (output, "Board name: ", 12);
|
||||
s = output + 12;
|
||||
@@ -412,7 +412,7 @@ cmd_sysinfo (struct tty *tty, const char *line)
|
||||
|
||||
*s++ = '\r';
|
||||
*s++ = '\n';
|
||||
tty_send (tty, (uint8_t *)output, s - output);
|
||||
tty_send (tty, output, s - output);
|
||||
}
|
||||
|
||||
|
||||
@@ -470,10 +470,10 @@ cmd_dispatch (struct tty *tty, const char *line)
|
||||
(*command_table[i].handler) (tty, p);
|
||||
else
|
||||
{
|
||||
uint8_t crlf[] = { '\r', '\n' };
|
||||
char crlf[] = { '\r', '\n' };
|
||||
|
||||
put_line (tty, "No such command: ");
|
||||
tty_send (tty, (const uint8_t *)line, n);
|
||||
tty_send (tty, line, n);
|
||||
tty_send (tty, crlf, sizeof (crlf));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ main (int argc, const char *argv[])
|
||||
m = 50;
|
||||
while (1)
|
||||
{
|
||||
uint8_t s[LINEBUFSIZE];
|
||||
char s[LINEBUFSIZE];
|
||||
|
||||
connection_loop:
|
||||
u = 1;
|
||||
@@ -175,7 +175,7 @@ main (int argc, const char *argv[])
|
||||
uint32_t usec;
|
||||
|
||||
/* Prompt */
|
||||
if (tty_send (tty, (uint8_t *)"> ", 2) < 0)
|
||||
if (tty_send (tty, "> ", 2) < 0)
|
||||
break;
|
||||
|
||||
usec = 3000000; /* 3.0 seconds */
|
||||
|
||||
@@ -5,5 +5,5 @@ 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, const uint8_t *buf, int count);
|
||||
int tty_recv (struct tty *tty, uint8_t *buf, uint32_t *timeout);
|
||||
int tty_send (struct tty *tty, const char *buf, int count);
|
||||
int tty_recv (struct tty *tty, char *buf, uint32_t *timeout);
|
||||
|
||||
@@ -31,7 +31,7 @@ static const struct line_coding line_coding0 = {
|
||||
*
|
||||
* 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)
|
||||
* directy accesses TTY0 (usb_device_reset and usb_set_configuration)
|
||||
* should be modified, too.
|
||||
*
|
||||
* Modification of TTY_MAIN thread will be also needed to echo back
|
||||
@@ -231,13 +231,13 @@ static const uint8_t vcom_string3[28] = {
|
||||
#define NUM_INTERFACES 2
|
||||
|
||||
|
||||
void
|
||||
usb_cb_device_reset (void)
|
||||
static void
|
||||
usb_device_reset (struct usb_dev *dev)
|
||||
{
|
||||
usb_lld_reset (VCOM_FEATURE_BUS_POWERED);
|
||||
usb_lld_reset (dev, VCOM_FEATURE_BUS_POWERED);
|
||||
|
||||
/* Initialize Endpoint 0 */
|
||||
usb_lld_setup_endp (ENDP0, 1, 1);
|
||||
usb_lld_setup_endp (dev, ENDP0, 1, 1);
|
||||
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.inputline_len = 0;
|
||||
@@ -253,13 +253,15 @@ usb_cb_device_reset (void)
|
||||
|
||||
#define CDC_CTRL_DTR 0x0001
|
||||
|
||||
void
|
||||
usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
static void
|
||||
usb_ctrl_write_finish (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t type_rcp = arg->type & (REQUEST_TYPE|RECIPIENT);
|
||||
|
||||
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0
|
||||
&& USB_SETUP_SET (req) && req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
&& USB_SETUP_SET (arg->type)
|
||||
&& arg->request == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
{
|
||||
struct tty *t = tty_get (arg->index, 0);
|
||||
|
||||
@@ -272,60 +274,65 @@ usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
vcom_port_data_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
vcom_port_data_setup (struct usb_dev *dev)
|
||||
{
|
||||
if (USB_SETUP_GET (req))
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
{
|
||||
struct tty *t = tty_get (arg->index, 0);
|
||||
|
||||
if (req_no == USB_CDC_REQ_GET_LINE_CODING)
|
||||
return usb_lld_reply_request (&t->line_coding,
|
||||
sizeof (struct line_coding), arg);
|
||||
if (arg->request == USB_CDC_REQ_GET_LINE_CODING)
|
||||
return usb_lld_ctrl_send (dev, &t->line_coding,
|
||||
sizeof (struct line_coding));
|
||||
}
|
||||
else /* USB_SETUP_SET (req) */
|
||||
{
|
||||
if (req_no == USB_CDC_REQ_SET_LINE_CODING
|
||||
if (arg->request == USB_CDC_REQ_SET_LINE_CODING
|
||||
&& arg->len == sizeof (struct line_coding))
|
||||
{
|
||||
struct tty *t = tty_get (arg->index, 0);
|
||||
|
||||
usb_lld_set_data_to_recv (&t->line_coding,
|
||||
return usb_lld_ctrl_recv (dev, &t->line_coding,
|
||||
sizeof (struct line_coding));
|
||||
return USB_SUCCESS;
|
||||
}
|
||||
else if (req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
return USB_SUCCESS;
|
||||
else if (arg->request == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
|
||||
return usb_lld_ctrl_ack (dev);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg)
|
||||
static int
|
||||
usb_setup (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t type_rcp = arg->type & (REQUEST_TYPE|RECIPIENT);
|
||||
|
||||
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0)
|
||||
return vcom_port_data_setup (req, req_no, arg);
|
||||
return vcom_port_data_setup (dev);
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
|
||||
struct req_args *arg)
|
||||
static int
|
||||
usb_get_descriptor (struct usb_dev *dev)
|
||||
{
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
uint8_t desc_type = (arg->value >> 8);
|
||||
uint8_t desc_index = (arg->value & 0xff);
|
||||
|
||||
if (rcp != DEVICE_RECIPIENT)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if (desc_type == DEVICE_DESCRIPTOR)
|
||||
return usb_lld_reply_request (vcom_device_desc, sizeof (vcom_device_desc),
|
||||
arg);
|
||||
return usb_lld_ctrl_send (dev,
|
||||
vcom_device_desc, sizeof (vcom_device_desc));
|
||||
else if (desc_type == CONFIG_DESCRIPTOR)
|
||||
return usb_lld_reply_request (vcom_config_desc, sizeof (vcom_config_desc),
|
||||
arg);
|
||||
return usb_lld_ctrl_send (dev,
|
||||
vcom_config_desc, sizeof (vcom_config_desc));
|
||||
else if (desc_type == STRING_DESCRIPTOR)
|
||||
{
|
||||
const uint8_t *str;
|
||||
@@ -350,22 +357,23 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
|
||||
size = sizeof (vcom_string3);
|
||||
break;
|
||||
default:
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return usb_lld_reply_request (str, size, arg);
|
||||
return usb_lld_ctrl_send (dev, str, size);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
|
||||
vcom_setup_endpoints_for_interface (struct usb_dev *dev,
|
||||
uint16_t interface, int stop)
|
||||
{
|
||||
if (interface == 0)
|
||||
{
|
||||
if (!stop)
|
||||
usb_lld_setup_endp (ENDP2, 0, 1);
|
||||
usb_lld_setup_endp (dev, ENDP2, 0, 1);
|
||||
else
|
||||
usb_lld_stall (ENDP2);
|
||||
}
|
||||
@@ -373,8 +381,8 @@ vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
|
||||
{
|
||||
if (!stop)
|
||||
{
|
||||
usb_lld_setup_endp (ENDP1, 0, 1);
|
||||
usb_lld_setup_endp (ENDP3, 1, 0);
|
||||
usb_lld_setup_endp (dev, ENDP1, 0, 1);
|
||||
usb_lld_setup_endp (dev, ENDP3, 1, 0);
|
||||
/* Start with no data receiving (ENDP3 not enabled)*/
|
||||
}
|
||||
else
|
||||
@@ -385,85 +393,87 @@ vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
usb_cb_handle_event (uint8_t event_type, uint16_t value)
|
||||
static int
|
||||
usb_set_configuration (struct usb_dev *dev)
|
||||
{
|
||||
int i;
|
||||
uint8_t current_conf;
|
||||
|
||||
switch (event_type)
|
||||
current_conf = usb_lld_current_configuration (dev);
|
||||
if (current_conf == 0)
|
||||
{
|
||||
case USB_EVENT_ADDRESS:
|
||||
if (dev->dev_req.value != 1)
|
||||
return -1;
|
||||
|
||||
usb_lld_set_configuration (dev, 1);
|
||||
for (i = 0; i < NUM_INTERFACES; i++)
|
||||
vcom_setup_endpoints_for_interface (dev, i, 0);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = CONFIGURED;
|
||||
chopstx_cond_signal (&tty0.cnd);
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
}
|
||||
else if (current_conf != dev->dev_req.value)
|
||||
{
|
||||
if (dev->dev_req.value != 0)
|
||||
return -1;
|
||||
|
||||
usb_lld_set_configuration (dev, 0);
|
||||
for (i = 0; i < NUM_INTERFACES; i++)
|
||||
vcom_setup_endpoints_for_interface (dev, i, 1);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = ADDRESSED;
|
||||
chopstx_cond_signal (&tty0.cnd);
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
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);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = CONFIGURED;
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
}
|
||||
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);
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = ADDRESSED;
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
}
|
||||
/* Do nothing when current_conf == value */
|
||||
return USB_SUCCESS;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return usb_lld_ctrl_ack (dev);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
usb_cb_interface (uint8_t cmd, struct req_args *arg)
|
||||
static int
|
||||
usb_set_interface (struct usb_dev *dev)
|
||||
{
|
||||
const uint8_t zero = 0;
|
||||
uint16_t interface = arg->index;
|
||||
uint16_t alt = arg->value;
|
||||
uint16_t interface = dev->dev_req.index;
|
||||
uint16_t alt = dev->dev_req.value;
|
||||
|
||||
if (interface >= NUM_INTERFACES)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
switch (cmd)
|
||||
if (alt != 0)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
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;
|
||||
vcom_setup_endpoints_for_interface (dev, interface, 0);
|
||||
return usb_lld_ctrl_ack (dev);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
usb_get_interface (struct usb_dev *dev)
|
||||
{
|
||||
const uint8_t zero = 0;
|
||||
uint16_t interface = dev->dev_req.index;
|
||||
|
||||
if (interface >= NUM_INTERFACES)
|
||||
return -1;
|
||||
|
||||
/* We don't have alternate interface, so, always return 0. */
|
||||
return usb_lld_ctrl_send (dev, &zero, 1);
|
||||
}
|
||||
|
||||
static int
|
||||
usb_get_status_interface (struct usb_dev *dev)
|
||||
{
|
||||
const uint16_t status_info = 0;
|
||||
uint16_t interface = dev->dev_req.index;
|
||||
|
||||
if (interface >= NUM_INTERFACES)
|
||||
return -1;
|
||||
|
||||
return usb_lld_ctrl_send (dev, &status_info, 2);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Put a character into the ring buffer to be send back.
|
||||
@@ -511,8 +521,8 @@ tty_echo_char (struct tty *t, int c)
|
||||
put_char_to_ringbuffer (t, c);
|
||||
}
|
||||
|
||||
void
|
||||
usb_cb_tx_done (uint8_t ep_num, uint32_t len)
|
||||
static void
|
||||
usb_tx_done (uint8_t ep_num, uint16_t len)
|
||||
{
|
||||
struct tty *t = tty_get (-1, ep_num);
|
||||
|
||||
@@ -594,17 +604,16 @@ tty_input_char (struct tty *t, int c)
|
||||
return r;
|
||||
}
|
||||
|
||||
void
|
||||
usb_cb_rx_ready (uint8_t ep_num)
|
||||
static void
|
||||
usb_rx_ready (uint8_t ep_num, uint16_t len)
|
||||
{
|
||||
struct tty *t = tty_get (-1, ep_num);
|
||||
|
||||
if (ep_num == ENDP3)
|
||||
{
|
||||
int i, r;
|
||||
int i;
|
||||
|
||||
r = usb_lld_rx_data_len (ENDP3);
|
||||
for (i = 0; i < r; i++)
|
||||
for (i = 0; i < len; i++)
|
||||
if (tty_input_char (t, t->recv_buf0[i]))
|
||||
break;
|
||||
|
||||
@@ -646,6 +655,8 @@ static void *
|
||||
tty_main (void *arg)
|
||||
{
|
||||
struct tty *t = arg;
|
||||
struct usb_dev dev;
|
||||
int e;
|
||||
|
||||
#if defined(OLDER_SYS_H)
|
||||
/*
|
||||
@@ -654,9 +665,9 @@ tty_main (void *arg)
|
||||
*
|
||||
* 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.
|
||||
* invocation of usb_lld_event_handler won't occur.
|
||||
*
|
||||
* Calling usb_interrupt_handler is no harm even if there were no
|
||||
* Calling usb_lld_event_handler is no harm even if there were no
|
||||
* interrupts, thus, we call it unconditionally here, just in case
|
||||
* if there is a request.
|
||||
*
|
||||
@@ -665,19 +676,114 @@ tty_main (void *arg)
|
||||
* chopstx_claim_irq after usb_lld_init overrides that.
|
||||
*
|
||||
*/
|
||||
usb_lld_init (VCOM_FEATURE_BUS_POWERED);
|
||||
usb_lld_init (&dev, VCOM_FEATURE_BUS_POWERED);
|
||||
chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
|
||||
usb_interrupt_handler ();
|
||||
goto event_handle;
|
||||
#else
|
||||
chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
|
||||
usb_lld_init (VCOM_FEATURE_BUS_POWERED);
|
||||
usb_lld_init (&dev, VCOM_FEATURE_BUS_POWERED);
|
||||
#endif
|
||||
|
||||
while (1)
|
||||
{
|
||||
chopstx_poll (NULL, 1, &usb_intr);
|
||||
if (usb_intr.ready)
|
||||
usb_interrupt_handler ();
|
||||
{
|
||||
uint8_t ep_num;
|
||||
#if defined(OLDER_SYS_H)
|
||||
event_handle:
|
||||
#endif
|
||||
/*
|
||||
* When interrupt is detected, call usb_lld_event_handler.
|
||||
* The event may be one of following:
|
||||
* (1) Transfer to endpoint (bulk or interrupt)
|
||||
* In this case EP_NUM is encoded in the variable E.
|
||||
* (2) "NONE" event: some trasfer was done, but all was
|
||||
* done by lower layer, no other work is needed in
|
||||
* upper layer.
|
||||
* (3) Device events: Reset or Suspend
|
||||
* (4) Device requests to the endpoint zero.
|
||||
*
|
||||
*/
|
||||
e = usb_lld_event_handler (&dev);
|
||||
ep_num = USB_EVENT_ENDP (e);
|
||||
|
||||
if (ep_num != 0)
|
||||
{
|
||||
if (USB_EVENT_TXRX (e))
|
||||
usb_tx_done (ep_num, USB_EVENT_LEN (e));
|
||||
else
|
||||
usb_rx_ready (ep_num, USB_EVENT_LEN (e));
|
||||
}
|
||||
else
|
||||
switch (USB_EVENT_ID (e))
|
||||
{
|
||||
case USB_EVENT_DEVICE_RESET:
|
||||
usb_device_reset (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_DEVICE_ADDRESSED:
|
||||
/* The addres is assigned to the device. We don't
|
||||
* need to do anything for this actually, but in this
|
||||
* application, we maintain the USB status of the
|
||||
* device. Usually, just "continue" as EVENT_OK is
|
||||
* OK.
|
||||
*/
|
||||
chopstx_mutex_lock (&tty0.mtx);
|
||||
tty0.device_state = ADDRESSED;
|
||||
chopstx_cond_signal (&tty0.cnd);
|
||||
chopstx_mutex_unlock (&tty0.mtx);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_GET_DESCRIPTOR:
|
||||
if (usb_get_descriptor (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_SET_CONFIGURATION:
|
||||
if (usb_set_configuration (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_SET_INTERFACE:
|
||||
if (usb_set_interface (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_CTRL_REQUEST:
|
||||
/* Device specific device request. */
|
||||
if (usb_setup (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_GET_STATUS_INTERFACE:
|
||||
if (usb_get_status_interface (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_GET_INTERFACE:
|
||||
if (usb_get_interface (&dev) < 0)
|
||||
usb_lld_ctrl_error (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_SET_FEATURE_DEVICE:
|
||||
case USB_EVENT_SET_FEATURE_ENDPOINT:
|
||||
case USB_EVENT_CLEAR_FEATURE_DEVICE:
|
||||
case USB_EVENT_CLEAR_FEATURE_ENDPOINT:
|
||||
usb_lld_ctrl_ack (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_CTRL_WRITE_FINISH:
|
||||
/* Control WRITE transfer finished. */
|
||||
usb_ctrl_write_finish (&dev);
|
||||
continue;
|
||||
|
||||
case USB_EVENT_OK:
|
||||
case USB_EVENT_DEVICE_SUSPEND:
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
chopstx_mutex_lock (&t->mtx);
|
||||
if (t->device_state == CONFIGURED && t->flag_connected
|
||||
@@ -720,8 +826,7 @@ tty_wait_connection (struct tty *t)
|
||||
t->flag_input_avail = 0;
|
||||
t->send_head = t->send_tail = 0;
|
||||
t->inputline_len = 0;
|
||||
/* Accept input for line */
|
||||
usb_lld_rx_enable_buf (ENDP3, t->recv_buf0, 64);
|
||||
usb_lld_rx_enable_buf (ENDP3, t->recv_buf0, 64); /* Accept input for line */
|
||||
chopstx_mutex_unlock (&t->mtx);
|
||||
}
|
||||
|
||||
@@ -738,10 +843,10 @@ check_tx (struct tty *t)
|
||||
}
|
||||
|
||||
int
|
||||
tty_send (struct tty *t, const uint8_t *buf, int len)
|
||||
tty_send (struct tty *t, const char *buf, int len)
|
||||
{
|
||||
int r;
|
||||
const uint8_t *p;
|
||||
const char *p;
|
||||
int count;
|
||||
|
||||
p = buf;
|
||||
@@ -800,7 +905,7 @@ check_rx (void *arg)
|
||||
*
|
||||
*/
|
||||
int
|
||||
tty_recv (struct tty *t, uint8_t *buf, uint32_t *timeout)
|
||||
tty_recv (struct tty *t, char *buf, uint32_t *timeout)
|
||||
{
|
||||
int r;
|
||||
chopstx_poll_cond_t poll_desc;
|
||||
|
||||
@@ -10,7 +10,7 @@ LDSCRIPT= sample.ld
|
||||
### LDSCRIPT= sample.ld.m3
|
||||
CSRC = sample.c
|
||||
|
||||
CHIP=stm32f030
|
||||
CHIP=stm32f0
|
||||
USE_SYS = yes
|
||||
|
||||
###################################
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* clk_gpio_init-kl.c - Clock and GPIO initialization for Kinetis L.
|
||||
* clk_gpio_init-mkl27z.c - Clock and GPIO initialization for Kinetis L.
|
||||
*
|
||||
* Copyright (C) 2016 Flying Stone Technology
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
|
||||
@@ -16,8 +16,12 @@
|
||||
#endif
|
||||
|
||||
extern const uint8_t sys_version[8];
|
||||
#if defined(USE_SYS3) || defined(USE_SYS_BOARD_ID)
|
||||
extern const uint32_t sys_board_id;
|
||||
extern const uint8_t sys_board_name[];
|
||||
#else
|
||||
# define SYS_BOARD_ID BOARD_ID
|
||||
#endif
|
||||
|
||||
typedef void (*handler)(void);
|
||||
extern handler vector[16];
|
||||
@@ -113,22 +117,18 @@ nvic_system_reset (void)
|
||||
(*vector[12]) ();
|
||||
}
|
||||
|
||||
/*
|
||||
* Users can override INLINE by 'attribute((used))' to have an
|
||||
* implementation defined.
|
||||
*/
|
||||
#if !defined(INLINE)
|
||||
#define INLINE __inline__
|
||||
#endif
|
||||
#ifdef REQUIRE_CLOCK_GPIO_SETTING_IN_SYS
|
||||
/* Provide the function entries. */
|
||||
|
||||
static INLINE void
|
||||
static void __attribute__ ((used))
|
||||
clock_init (void)
|
||||
{
|
||||
(*vector[13]) ();
|
||||
}
|
||||
|
||||
static INLINE void
|
||||
static void __attribute__ ((used))
|
||||
gpio_init (void)
|
||||
{
|
||||
(*vector[14]) ();
|
||||
}
|
||||
#endif
|
||||
540
mcu/usb-mkl27z.c
540
mcu/usb-mkl27z.c
@@ -32,6 +32,9 @@
|
||||
|
||||
#include "usb_lld.h"
|
||||
|
||||
#define DATA0 0
|
||||
#define DATA1 1
|
||||
|
||||
struct endpoint_ctl {
|
||||
uint32_t rx_odd: 1;
|
||||
uint32_t tx_odd: 1;
|
||||
@@ -135,12 +138,6 @@ extern uint8_t __usb_bdt__;
|
||||
|
||||
static struct BD *const BD_table = (struct BD *const)&__usb_bdt__;
|
||||
|
||||
static uint8_t setup[8];
|
||||
/* bmRequestType, bRequest */
|
||||
/* Value: 2-byte */
|
||||
/* Index: 2-byte */
|
||||
/* Length: 2-byte */
|
||||
|
||||
static void
|
||||
kl27z_usb_init (void)
|
||||
{
|
||||
@@ -183,11 +180,11 @@ kl27z_set_daddr (uint8_t daddr)
|
||||
}
|
||||
|
||||
static void
|
||||
kl27z_prepare_ep0_setup (void)
|
||||
kl27z_prepare_ep0_setup (struct usb_dev *dev)
|
||||
{
|
||||
/* Endpoint 0, TX=0. */
|
||||
BD_table[ep[0].rx_odd].ctrl = 0x00080088; /* Len=8, OWN=1, DATA01=0, DTS=1 */
|
||||
BD_table[ep[0].rx_odd].buf = setup;
|
||||
BD_table[ep[0].rx_odd].buf = &dev->dev_req;
|
||||
|
||||
BD_table[!ep[0].rx_odd].ctrl = 0x0000; /* OWN=0 */
|
||||
BD_table[!ep[0].rx_odd].buf = NULL;
|
||||
@@ -296,17 +293,11 @@ enum STANDARD_REQUESTS {
|
||||
|
||||
|
||||
enum FEATURE_SELECTOR {
|
||||
ENDPOINT_STALL,
|
||||
DEVICE_REMOTE_WAKEUP
|
||||
FEATURE_ENDPOINT_HALT=0,
|
||||
FEATURE_DEVICE_REMOTE_WAKEUP=1
|
||||
};
|
||||
|
||||
|
||||
struct data_ctl {
|
||||
uint8_t *addr;
|
||||
uint16_t len;
|
||||
uint8_t require_zlp;
|
||||
};
|
||||
|
||||
/* The state machine states of a control pipe */
|
||||
enum {
|
||||
WAIT_SETUP,
|
||||
@@ -319,30 +310,7 @@ enum {
|
||||
PAUSE
|
||||
};
|
||||
|
||||
struct device_ctl {
|
||||
/* control pipe state */
|
||||
uint8_t state;
|
||||
|
||||
uint32_t tkdone;
|
||||
uint32_t reset;
|
||||
uint32_t error;
|
||||
uint32_t stall;
|
||||
|
||||
uint32_t send;
|
||||
uint32_t recv;
|
||||
|
||||
/* Device specific settings */
|
||||
uint8_t configuration;
|
||||
uint8_t feature;
|
||||
};
|
||||
|
||||
static struct device_ctl device_ctl;
|
||||
static struct data_ctl data_ctl;
|
||||
|
||||
static struct device_ctl *const dev_p = &device_ctl;
|
||||
static struct data_ctl *const data_p = &data_ctl;
|
||||
|
||||
static void handle_transaction (uint8_t stat);
|
||||
static int handle_transaction (struct usb_dev *dev, uint8_t stat);
|
||||
|
||||
void
|
||||
usb_lld_stall (int n)
|
||||
@@ -352,16 +320,28 @@ usb_lld_stall (int n)
|
||||
|
||||
|
||||
void
|
||||
usb_lld_init (uint8_t feature)
|
||||
usb_lld_ctrl_error (struct usb_dev *dev)
|
||||
{
|
||||
dev_p->state = WAIT_SETUP;
|
||||
dev_p->tkdone = 0;
|
||||
dev_p->reset = 0;
|
||||
dev_p->error = 0;
|
||||
dev_p->stall = 0;
|
||||
dev->state = STALLED;
|
||||
kl27z_ep_stall (ENDP0);
|
||||
}
|
||||
|
||||
usb_lld_set_configuration (0);
|
||||
dev_p->feature = feature;
|
||||
int
|
||||
usb_lld_ctrl_ack (struct usb_dev *dev)
|
||||
{
|
||||
/* Zero length packet for ACK. */
|
||||
dev->state = WAIT_STATUS_IN;
|
||||
kl27z_prepare_ep0_in (&dev->dev_req, 0, DATA1);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
usb_lld_init (struct usb_dev *dev, uint8_t feature)
|
||||
{
|
||||
usb_lld_set_configuration (dev, 0);
|
||||
dev->feature = feature;
|
||||
dev->state = WAIT_SETUP;
|
||||
|
||||
kl27z_set_daddr (0);
|
||||
kl27z_usb_init ();
|
||||
@@ -376,8 +356,12 @@ usb_lld_init (uint8_t feature)
|
||||
| USB_IS_ERROR | USB_IS_USBRST;
|
||||
}
|
||||
|
||||
void
|
||||
usb_interrupt_handler (void)
|
||||
|
||||
#define USB_MAKE_EV(event) (event<<24)
|
||||
#define USB_MAKE_TXRX(ep_num,txrx,len) ((txrx? (1<<23):0)|(ep_num<<16)|len)
|
||||
|
||||
int
|
||||
usb_lld_event_handler (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t istat_value = USB_CTRL1->ISTAT;
|
||||
uint8_t stat = USB_CTRL1->STAT;
|
||||
@@ -385,20 +369,14 @@ usb_interrupt_handler (void)
|
||||
if ((istat_value & USB_IS_USBRST))
|
||||
{
|
||||
USB_CTRL1->ISTAT = USB_IS_USBRST;
|
||||
usb_cb_device_reset ();
|
||||
dev_p->reset++;
|
||||
return USB_MAKE_EV (USB_EVENT_DEVICE_RESET);
|
||||
}
|
||||
else if ((istat_value & USB_IS_TOKDNE))
|
||||
{
|
||||
handle_transaction (stat);
|
||||
dev_p->tkdone++;
|
||||
}
|
||||
return handle_transaction (dev, stat);
|
||||
else if ((istat_value & USB_IS_ERROR))
|
||||
{ /* Clear Errors. */
|
||||
USB_CTRL1->ERRSTAT = USB_CTRL1->ERRSTAT;
|
||||
USB_CTRL1->ISTAT = USB_IS_ERROR;
|
||||
/*reset???*/
|
||||
dev_p->error++;
|
||||
}
|
||||
else if ((istat_value & USB_IS_STALL))
|
||||
{
|
||||
@@ -406,22 +384,22 @@ usb_interrupt_handler (void)
|
||||
|
||||
if (kl27z_ep_is_stall (0))
|
||||
{ /* It's endpoint 0, recover from erorr. */
|
||||
dev_p->state = WAIT_SETUP;
|
||||
dev->state = WAIT_SETUP;
|
||||
kl27z_ep_clear_stall (0);
|
||||
kl27z_prepare_ep0_setup ();
|
||||
kl27z_prepare_ep0_setup (dev);
|
||||
}
|
||||
|
||||
USB_CTRL1->ISTAT = USB_IS_STALL;
|
||||
dev_p->stall++;
|
||||
}
|
||||
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
#define DATA0 0
|
||||
#define DATA1 1
|
||||
|
||||
static void
|
||||
handle_datastage_out (uint8_t stat)
|
||||
handle_datastage_out (struct usb_dev *dev, uint8_t stat)
|
||||
{
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
int odd = (stat >> 2)&1;
|
||||
int data01 = !((BD_table[odd].ctrl >> 6)&1);
|
||||
uint32_t len = (BD_table[odd].ctrl >> 16)&0x3ff;
|
||||
@@ -436,43 +414,44 @@ handle_datastage_out (uint8_t stat)
|
||||
if (data_p->len == 0)
|
||||
{
|
||||
/* No more data to receive, proceed to send acknowledge for IN. */
|
||||
dev_p->state = WAIT_STATUS_IN;
|
||||
kl27z_prepare_ep0_in (setup, 0, DATA1);
|
||||
dev->state = WAIT_STATUS_IN;
|
||||
kl27z_prepare_ep0_in (&dev->dev_req, 0, DATA1);
|
||||
}
|
||||
else
|
||||
{
|
||||
dev_p->state = OUT_DATA;
|
||||
dev->state = OUT_DATA;
|
||||
kl27z_prepare_ep0_out (data_p->addr, len, data01);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
handle_datastage_in (uint8_t stat)
|
||||
handle_datastage_in (struct usb_dev *dev, uint8_t stat)
|
||||
{
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
int odd = (stat >> 2)&1;
|
||||
int data01 = !((BD_table[2+odd].ctrl >> 6)&1);
|
||||
uint32_t len = USB_MAX_PACKET_SIZE;
|
||||
|
||||
if ((data_p->len == 0) && (dev_p->state == LAST_IN_DATA))
|
||||
if ((data_p->len == 0) && (dev->state == LAST_IN_DATA))
|
||||
{
|
||||
if (data_p->require_zlp)
|
||||
{
|
||||
data_p->require_zlp = 0;
|
||||
|
||||
/* No more data to send. Send empty packet */
|
||||
kl27z_prepare_ep0_in (setup, 0, data01);
|
||||
kl27z_prepare_ep0_in (&dev->dev_req, 0, data01);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No more data to send, proceed to receive OUT acknowledge. */
|
||||
dev_p->state = WAIT_STATUS_OUT;
|
||||
kl27z_prepare_ep0_out (setup, 8, DATA1);
|
||||
dev->state = WAIT_STATUS_OUT;
|
||||
kl27z_prepare_ep0_out (&dev->dev_req, 8, DATA1);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dev_p->state = (data_p->len <= len) ? LAST_IN_DATA : IN_DATA;
|
||||
dev->state = (data_p->len <= len) ? LAST_IN_DATA : IN_DATA;
|
||||
|
||||
if (len > data_p->len)
|
||||
len = data_p->len;
|
||||
@@ -482,31 +461,32 @@ handle_datastage_in (uint8_t stat)
|
||||
data_p->addr += len;
|
||||
}
|
||||
|
||||
typedef int (*HANDLER) (uint8_t req, struct req_args *arg);
|
||||
typedef int (*HANDLER) (struct usb_dev *dev);
|
||||
|
||||
static int
|
||||
std_none (uint8_t req, struct req_args *arg)
|
||||
std_none (struct usb_dev *dev)
|
||||
{
|
||||
(void)req; (void)arg;
|
||||
return USB_UNSUPPORT;
|
||||
(void)dev;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_get_status (uint8_t req, struct req_args *arg)
|
||||
std_get_status (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
uint16_t status_info = 0;
|
||||
|
||||
if (arg->value != 0 || arg->len != 2 || (arg->index >> 8) != 0
|
||||
|| USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
|| USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
{
|
||||
if (arg->index == 0)
|
||||
{
|
||||
/* Get Device Status */
|
||||
uint8_t feature = dev_p->feature;
|
||||
uint8_t feature = dev->feature;
|
||||
|
||||
/* Remote Wakeup enabled */
|
||||
if ((feature & (1 << 5)))
|
||||
@@ -520,319 +500,318 @@ std_get_status (uint8_t req, struct req_args *arg)
|
||||
else /* Self-powered */
|
||||
status_info &= ~1;
|
||||
|
||||
return usb_lld_reply_request (&status_info, 2, arg);
|
||||
return usb_lld_ctrl_send (dev, &status_info, 2);
|
||||
}
|
||||
}
|
||||
else if (rcp == INTERFACE_RECIPIENT)
|
||||
{
|
||||
int r;
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (dev_p->configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
r = usb_cb_interface (USB_QUERY_INTERFACE, arg);
|
||||
if (r != USB_SUCCESS)
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
return usb_lld_reply_request (&status_info, 2, arg);
|
||||
return USB_EVENT_GET_STATUS_INTERFACE;
|
||||
}
|
||||
else if (rcp == ENDPOINT_RECIPIENT)
|
||||
{
|
||||
uint8_t n = (arg->index & 0x0f);
|
||||
|
||||
if ((arg->index & 0x70) || n == ENDP0)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if (kl27z_ep_is_disabled (n))
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
status_info = kl27z_ep_is_stall (n);
|
||||
return usb_lld_reply_request (&status_info, 2, arg);
|
||||
return usb_lld_ctrl_send (dev, &status_info, 2);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_clear_feature (uint8_t req, struct req_args *arg)
|
||||
std_clear_feature (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
{
|
||||
if (arg->len != 0 || arg->index != 0)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if (arg->value == DEVICE_REMOTE_WAKEUP)
|
||||
if (arg->value == FEATURE_DEVICE_REMOTE_WAKEUP)
|
||||
{
|
||||
dev_p->feature &= ~(1 << 5);
|
||||
return USB_SUCCESS;
|
||||
dev->feature &= ~(1 << 5);
|
||||
return USB_EVENT_CLEAR_FEATURE_DEVICE;
|
||||
}
|
||||
}
|
||||
else if (rcp == ENDPOINT_RECIPIENT)
|
||||
{
|
||||
uint8_t n = (arg->index & 0x0f);
|
||||
|
||||
if (dev_p->configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (arg->len != 0 || (arg->index >> 8) != 0
|
||||
|| arg->value != ENDPOINT_STALL || n == ENDP0)
|
||||
return USB_UNSUPPORT;
|
||||
|| arg->value != FEATURE_ENDPOINT_HALT || n == ENDP0)
|
||||
return -1;
|
||||
|
||||
if (kl27z_ep_is_disabled (n))
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
kl27z_ep_clear_dtog ((arg->index & 0x80) == 0, n);
|
||||
|
||||
// event??
|
||||
return USB_SUCCESS;
|
||||
return USB_EVENT_CLEAR_FEATURE_ENDPOINT;
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_set_feature (uint8_t req, struct req_args *arg)
|
||||
std_set_feature (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
{
|
||||
if (arg->len != 0 || arg->index != 0)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if (arg->value == DEVICE_REMOTE_WAKEUP)
|
||||
if (arg->value == FEATURE_DEVICE_REMOTE_WAKEUP)
|
||||
{
|
||||
dev_p->feature |= 1 << 5;
|
||||
// event??
|
||||
return USB_SUCCESS;
|
||||
dev->feature |= 1 << 5;
|
||||
return USB_EVENT_SET_FEATURE_DEVICE;
|
||||
}
|
||||
}
|
||||
else if (rcp == ENDPOINT_RECIPIENT)
|
||||
{
|
||||
uint8_t n = (arg->index & 0x0f);
|
||||
|
||||
if (dev_p->configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (arg->len != 0 || (arg->index >> 8) != 0
|
||||
|| arg->value != 0 || n == ENDP0)
|
||||
return USB_UNSUPPORT;
|
||||
|| arg->value != FEATURE_ENDPOINT_HALT || n == ENDP0)
|
||||
return -1;
|
||||
|
||||
if (kl27z_ep_is_disabled (n))
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
kl27z_ep_stall (n);
|
||||
|
||||
// event??
|
||||
return USB_SUCCESS;
|
||||
return USB_EVENT_SET_FEATURE_ENDPOINT;
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_set_address (uint8_t req, struct req_args *arg)
|
||||
std_set_address (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT && arg->len == 0 && arg->value <= 127
|
||||
&& arg->index == 0 && dev_p->configuration == 0)
|
||||
return USB_SUCCESS;
|
||||
&& arg->index == 0 && dev->configuration == 0)
|
||||
return usb_lld_ctrl_ack (dev);
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_get_descriptor (uint8_t req, struct req_args *arg)
|
||||
std_get_descriptor (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
if (USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
return usb_cb_get_descriptor (rcp, (arg->value >> 8),
|
||||
(arg->value & 0xff), arg);
|
||||
return USB_EVENT_GET_DESCRIPTOR;
|
||||
}
|
||||
|
||||
static int
|
||||
std_get_configuration (uint8_t req, struct req_args *arg)
|
||||
std_get_configuration (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
(void)arg;
|
||||
if (USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (arg->value != 0 || arg->index != 0 || arg->len != 1)
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
return usb_lld_reply_request (&dev_p->configuration, 1, arg);
|
||||
return usb_lld_ctrl_send (dev, &dev->configuration, 1);
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_set_configuration (uint8_t req, struct req_args *arg)
|
||||
std_set_configuration (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT && arg->index == 0 && arg->len == 0)
|
||||
return usb_cb_handle_event (USB_EVENT_CONFIG, arg->value);
|
||||
return USB_EVENT_SET_CONFIGURATION;
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_get_interface (uint8_t req, struct req_args *arg)
|
||||
std_get_interface (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (arg->value != 0 || (arg->index >> 8) != 0 || arg->len != 1)
|
||||
return -1;
|
||||
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (rcp == INTERFACE_RECIPIENT)
|
||||
{
|
||||
if (arg->value != 0 || (arg->index >> 8) != 0 || arg->len != 1)
|
||||
return USB_UNSUPPORT;
|
||||
return USB_EVENT_GET_INTERFACE;
|
||||
|
||||
if (dev_p->configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
return usb_cb_interface (USB_GET_INTERFACE, arg);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
std_set_interface (uint8_t req, struct req_args *arg)
|
||||
std_set_interface (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req) || rcp != INTERFACE_RECIPIENT
|
||||
if (USB_SETUP_GET (arg->type) || rcp != INTERFACE_RECIPIENT
|
||||
|| arg->len != 0 || (arg->index >> 8) != 0
|
||||
|| (arg->value >> 8) != 0 || dev_p->configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
|| (arg->value >> 8) != 0 || dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
return usb_cb_interface (USB_SET_INTERFACE, arg);
|
||||
return USB_EVENT_SET_INTERFACE;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
handle_setup0 (void)
|
||||
static int
|
||||
handle_setup0 (struct usb_dev *dev)
|
||||
{
|
||||
struct req_args *arg = (struct req_args *)&setup[2];
|
||||
int r = USB_UNSUPPORT;
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
int r = -1;
|
||||
HANDLER handler;
|
||||
|
||||
data_p->addr = NULL;
|
||||
data_p->len = 0;
|
||||
data_p->require_zlp = 0;
|
||||
|
||||
if ((setup[0] & REQUEST_TYPE) == STANDARD_REQUEST)
|
||||
if ((dev->dev_req.type & REQUEST_TYPE) == STANDARD_REQUEST)
|
||||
{
|
||||
if (setup[1] < TOTAL_REQUEST)
|
||||
switch (dev->dev_req.request)
|
||||
{
|
||||
switch (setup[1])
|
||||
{
|
||||
case 0: handler = std_get_status; break;
|
||||
case 1: handler = std_clear_feature; break;
|
||||
case 3: handler = std_set_feature; break;
|
||||
case 5: handler = std_set_address; break;
|
||||
case 6: handler = std_get_descriptor; break;
|
||||
case 8: handler = std_get_configuration; break;
|
||||
case 9: handler = std_set_configuration; break;
|
||||
case 10: handler = std_get_interface; break;
|
||||
case 11: handler = std_set_interface; break;
|
||||
default: handler = std_none; break;
|
||||
}
|
||||
|
||||
r = (*handler) (setup[0], arg);
|
||||
case 0: handler = std_get_status; break;
|
||||
case 1: handler = std_clear_feature; break;
|
||||
case 3: handler = std_set_feature; break;
|
||||
case 5: handler = std_set_address; break;
|
||||
case 6: handler = std_get_descriptor; break;
|
||||
case 8: handler = std_get_configuration; break;
|
||||
case 9: handler = std_set_configuration; break;
|
||||
case 10: handler = std_get_interface; break;
|
||||
case 11: handler = std_set_interface; break;
|
||||
default: handler = std_none; break;
|
||||
}
|
||||
|
||||
if ((r = (*handler) (dev)) < 0)
|
||||
{
|
||||
usb_lld_ctrl_error (dev);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
else
|
||||
return r;
|
||||
}
|
||||
else
|
||||
r = usb_cb_setup (setup[0], setup[1], arg);
|
||||
|
||||
if (r != USB_SUCCESS)
|
||||
dev_p->state = STALLED;
|
||||
else if (USB_SETUP_SET (setup[0]))
|
||||
{
|
||||
if (arg->len == 0)
|
||||
{
|
||||
/* Zero length packet for ACK. */
|
||||
kl27z_prepare_ep0_in (setup, 0, DATA1);
|
||||
dev_p->state = WAIT_STATUS_IN;
|
||||
}
|
||||
}
|
||||
return USB_EVENT_CTRL_REQUEST;
|
||||
}
|
||||
|
||||
static void
|
||||
handle_in0 (uint8_t stat)
|
||||
static int
|
||||
handle_in0 (struct usb_dev *dev, uint8_t stat)
|
||||
{
|
||||
if (dev_p->state == IN_DATA || dev_p->state == LAST_IN_DATA)
|
||||
handle_datastage_in (stat);
|
||||
else if (dev_p->state == WAIT_STATUS_IN)
|
||||
{ /* Control WRITE transfer done successfully. */
|
||||
uint16_t value = (setup[3]<<8) | setup[2];
|
||||
int r = 0;
|
||||
|
||||
if ((setup[1] == SET_ADDRESS) &&
|
||||
((setup[0] & (REQUEST_TYPE | RECIPIENT))
|
||||
if (dev->state == IN_DATA || dev->state == LAST_IN_DATA)
|
||||
handle_datastage_in (dev, stat);
|
||||
else if (dev->state == WAIT_STATUS_IN)
|
||||
{ /* Control WRITE transfer done successfully. */
|
||||
uint16_t value = dev->dev_req.value;
|
||||
|
||||
if ((dev->dev_req.request == SET_ADDRESS) &&
|
||||
((dev->dev_req.type & (REQUEST_TYPE | RECIPIENT))
|
||||
== (STANDARD_REQUEST | DEVICE_RECIPIENT)))
|
||||
{
|
||||
kl27z_set_daddr (value);
|
||||
usb_cb_handle_event (USB_EVENT_ADDRESS, value);
|
||||
ep[0].rx_odd = 0;
|
||||
r = USB_EVENT_DEVICE_ADDRESSED;
|
||||
}
|
||||
else
|
||||
usb_cb_ctrl_write_finish (setup[0], setup[1],
|
||||
(struct req_args *)&setup[2]);
|
||||
|
||||
dev_p->state = WAIT_SETUP;
|
||||
kl27z_prepare_ep0_setup ();
|
||||
r = USB_EVENT_CTRL_WRITE_FINISH;
|
||||
dev->state = WAIT_SETUP;
|
||||
kl27z_prepare_ep0_setup (dev);
|
||||
}
|
||||
else
|
||||
dev_p->state = STALLED;
|
||||
{
|
||||
dev->state = STALLED;
|
||||
kl27z_ep_stall (ENDP0);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static void
|
||||
handle_out0 (uint8_t stat)
|
||||
handle_out0 (struct usb_dev *dev, uint8_t stat)
|
||||
{
|
||||
if (dev_p->state == IN_DATA || dev_p->state == LAST_IN_DATA)
|
||||
/* Host aborts the control READ transfer before finish. */
|
||||
dev_p->state = STALLED;
|
||||
else if (dev_p->state == OUT_DATA)
|
||||
if (dev->state == OUT_DATA)
|
||||
/* It's normal control WRITE transfer. */
|
||||
handle_datastage_out (stat);
|
||||
else if (dev_p->state == WAIT_STATUS_OUT)
|
||||
handle_datastage_out (dev, stat);
|
||||
else if (dev->state == WAIT_STATUS_OUT)
|
||||
{ /* Control READ transfer done successfully. */
|
||||
dev_p->state = WAIT_SETUP;
|
||||
kl27z_prepare_ep0_setup ();
|
||||
dev->state = WAIT_SETUP;
|
||||
kl27z_prepare_ep0_setup (dev);
|
||||
}
|
||||
else
|
||||
dev_p->state = STALLED;
|
||||
{
|
||||
/*
|
||||
* dev->state == IN_DATA || dev->state == LAST_IN_DATA
|
||||
* (Host aborts the transfer before finish)
|
||||
* Or else, unexpected state.
|
||||
* STALL the endpoint, until we receive the next SETUP token.
|
||||
*/
|
||||
dev->state = STALLED;
|
||||
kl27z_ep_stall (ENDP0);
|
||||
}
|
||||
}
|
||||
|
||||
#define USB_TOKEN_ACK 0x02
|
||||
#define USB_TOKEN_IN 0x09
|
||||
#define USB_TOKEN_SETUP 0x0d
|
||||
|
||||
static void
|
||||
handle_transaction (uint8_t stat)
|
||||
static int
|
||||
handle_transaction (struct usb_dev *dev, uint8_t stat)
|
||||
{
|
||||
int odd = (stat >> 2)&1;
|
||||
uint8_t ep_num = (stat >> 4);
|
||||
int r;
|
||||
|
||||
if (ep_num == 0)
|
||||
{
|
||||
@@ -841,33 +820,36 @@ handle_transaction (uint8_t stat)
|
||||
ep[0].rx_odd ^= 1;
|
||||
if (TOK_PID (BD_table[odd].ctrl) == USB_TOKEN_SETUP)
|
||||
{
|
||||
handle_setup0 ();
|
||||
r = handle_setup0 (dev);
|
||||
USB_CTRL1->ISTAT = USB_IS_TOKDNE;
|
||||
USB_CTRL1->CTL = 0x01; /* Clear TXSUSPENDTOKENBUSY. */
|
||||
return USB_MAKE_EV (r);
|
||||
}
|
||||
else
|
||||
{
|
||||
handle_out0 (dev, stat);
|
||||
USB_CTRL1->ISTAT = USB_IS_TOKDNE;
|
||||
handle_out0 (stat);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ep[0].tx_odd ^= 1;
|
||||
r = handle_in0 (dev, stat);
|
||||
USB_CTRL1->ISTAT = USB_IS_TOKDNE;
|
||||
handle_in0 (stat);
|
||||
return USB_MAKE_EV (r);
|
||||
}
|
||||
|
||||
if (dev_p->state == STALLED)
|
||||
kl27z_ep_stall (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t len;
|
||||
|
||||
if ((stat & 0x08) == 0)
|
||||
{
|
||||
dev_p->recv++;
|
||||
len = (BD_table[4*ep_num+odd].ctrl >> 16)&0x3ff;
|
||||
ep[ep_num].rx_odd ^= 1;
|
||||
usb_cb_rx_ready (ep_num);
|
||||
USB_CTRL1->ISTAT = USB_IS_TOKDNE;
|
||||
return USB_MAKE_TXRX (ep_num, 0, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -878,8 +860,8 @@ handle_transaction (uint8_t stat)
|
||||
* host: IN ACK
|
||||
* device: DATA0/1
|
||||
*
|
||||
* It is not described in the specification (it's
|
||||
* ambiguous), but it is actually possible for some host
|
||||
* Although it is not described in the specification (it's
|
||||
* ambiguous), it is actually possible for some host
|
||||
* implementation to send back a NAK on erroneous case like
|
||||
* a device sent oversized data.
|
||||
*
|
||||
@@ -893,39 +875,30 @@ handle_transaction (uint8_t stat)
|
||||
*/
|
||||
uint32_t dmaerr = (USB_CTRL1->ERRSTAT & (1 << 5));
|
||||
int success = (dmaerr == 0);
|
||||
uint32_t len = (BD_table[4*ep_num+2+odd].ctrl >> 16)&0x3ff;
|
||||
|
||||
len = (BD_table[4*ep_num+2+odd].ctrl >> 16)&0x3ff;
|
||||
if (!success)
|
||||
{
|
||||
USB_CTRL1->ERRSTAT = dmaerr; /* Clear error. */
|
||||
dev_p->error++;
|
||||
}
|
||||
USB_CTRL1->ERRSTAT = dmaerr; /* Clear error. */
|
||||
|
||||
dev_p->send++;
|
||||
ep[ep_num].tx_odd ^= 1;
|
||||
usb_cb_tx_done (ep_num, len);
|
||||
USB_CTRL1->ISTAT = USB_IS_TOKDNE;
|
||||
return USB_MAKE_TXRX (ep_num, 1, len);
|
||||
}
|
||||
|
||||
USB_CTRL1->ISTAT = USB_IS_TOKDNE;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
usb_lld_reset (uint8_t feature)
|
||||
usb_lld_reset (struct usb_dev *dev, uint8_t feature)
|
||||
{
|
||||
dev_p->feature = feature;
|
||||
usb_lld_set_configuration (0);
|
||||
usb_lld_set_configuration (dev, 0);
|
||||
dev->feature = feature;
|
||||
dev->state = WAIT_SETUP;
|
||||
|
||||
/* Reset USB */
|
||||
USB_CTRL2->USBTRC0 = 0xc0;
|
||||
|
||||
USB_CTRL1->CTL = 0x00; /* Disable USB FS communication module */
|
||||
|
||||
dev_p->state = WAIT_SETUP;
|
||||
dev_p->tkdone = 0;
|
||||
dev_p->error = 0;
|
||||
dev_p->stall = 0;
|
||||
|
||||
kl27z_set_daddr (0);
|
||||
kl27z_usb_init ();
|
||||
|
||||
@@ -937,13 +910,13 @@ usb_lld_reset (uint8_t feature)
|
||||
}
|
||||
|
||||
void
|
||||
usb_lld_setup_endp (int n, int rx_en, int tx_en)
|
||||
usb_lld_setup_endp (struct usb_dev *dev, int n, int rx_en, int tx_en)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
/* Enable the endpoint 0. */
|
||||
USB_ENDPT[0].EP = 0x0d;
|
||||
kl27z_prepare_ep0_setup ();
|
||||
kl27z_prepare_ep0_setup (dev);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -968,27 +941,29 @@ usb_lld_setup_endp (int n, int rx_en, int tx_en)
|
||||
|
||||
|
||||
void
|
||||
usb_lld_set_configuration (uint8_t config)
|
||||
usb_lld_set_configuration (struct usb_dev *dev, uint8_t config)
|
||||
{
|
||||
dev_p->configuration = config;
|
||||
dev->configuration = config;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
usb_lld_current_configuration (void)
|
||||
usb_lld_current_configuration (struct usb_dev *dev)
|
||||
{
|
||||
return dev_p->configuration;
|
||||
return dev->configuration;
|
||||
}
|
||||
|
||||
void
|
||||
usb_lld_set_data_to_recv (void *p, size_t len)
|
||||
int
|
||||
usb_lld_ctrl_recv (struct usb_dev *dev, void *p, size_t len)
|
||||
{
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
data_p->addr = (uint8_t *)p;
|
||||
data_p->len = len;
|
||||
if (len > USB_MAX_PACKET_SIZE)
|
||||
len = USB_MAX_PACKET_SIZE;
|
||||
|
||||
kl27z_prepare_ep0_out (p, len, DATA1);
|
||||
dev_p->state = OUT_DATA;
|
||||
dev->state = OUT_DATA;
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -998,9 +973,10 @@ usb_lld_set_data_to_recv (void *p, size_t len)
|
||||
* BUFLEN: size of the data.
|
||||
*/
|
||||
int
|
||||
usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *a)
|
||||
usb_lld_ctrl_send (struct usb_dev *dev, const void *buf, size_t buflen)
|
||||
{
|
||||
uint32_t len_asked = a->len;
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
uint32_t len_asked = dev->dev_req.len;
|
||||
uint32_t len;
|
||||
|
||||
data_p->addr = (void *)buf;
|
||||
@@ -1016,12 +992,12 @@ usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *a)
|
||||
if (data_p->len < USB_MAX_PACKET_SIZE)
|
||||
{
|
||||
len = data_p->len;
|
||||
dev_p->state = LAST_IN_DATA;
|
||||
dev->state = LAST_IN_DATA;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = USB_MAX_PACKET_SIZE;
|
||||
dev_p->state = IN_DATA;
|
||||
dev->state = IN_DATA;
|
||||
}
|
||||
|
||||
if (len)
|
||||
@@ -1030,7 +1006,7 @@ usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *a)
|
||||
data_p->len -= len;
|
||||
data_p->addr += len;
|
||||
|
||||
return USB_SUCCESS;
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -1042,12 +1018,6 @@ usb_lld_rx_enable_buf (int n, void *buf, size_t len)
|
||||
BD_table[4*n+ep[n].rx_odd].buf = buf;
|
||||
}
|
||||
|
||||
int
|
||||
usb_lld_rx_data_len (int n)
|
||||
{
|
||||
return (BD_table[4*n+!ep[n].rx_odd].ctrl >> 16)&0x3ff;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
usb_lld_tx_enable_buf (int n, const void *buf, size_t len)
|
||||
|
||||
@@ -1,3 +1,31 @@
|
||||
/*
|
||||
* usb-stm32f103.c - USB driver for STM32F103
|
||||
*
|
||||
* 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>
|
||||
|
||||
@@ -28,50 +56,20 @@ enum STANDARD_REQUESTS
|
||||
enum CONTROL_STATE
|
||||
{
|
||||
WAIT_SETUP,
|
||||
SETTING_UP,
|
||||
IN_DATA,
|
||||
OUT_DATA,
|
||||
LAST_IN_DATA,
|
||||
WAIT_STATUS_IN,
|
||||
WAIT_STATUS_OUT,
|
||||
STALLED,
|
||||
PAUSE
|
||||
};
|
||||
|
||||
enum FEATURE_SELECTOR
|
||||
{
|
||||
ENDPOINT_STALL,
|
||||
DEVICE_REMOTE_WAKEUP
|
||||
FEATURE_ENDPOINT_HALT=0,
|
||||
FEATURE_DEVICE_REMOTE_WAKEUP=1
|
||||
};
|
||||
|
||||
struct DATA_INFO
|
||||
{
|
||||
uint8_t *addr;
|
||||
uint16_t len;
|
||||
uint8_t require_zlp;
|
||||
};
|
||||
|
||||
|
||||
struct DEVICE_INFO
|
||||
{
|
||||
uint8_t current_configuration;
|
||||
uint8_t current_feature;
|
||||
uint8_t state;
|
||||
/**/
|
||||
uint8_t bmRequestType;
|
||||
uint8_t bRequest;
|
||||
/**/
|
||||
uint16_t value;
|
||||
uint16_t index;
|
||||
uint16_t len;
|
||||
};
|
||||
|
||||
static struct DEVICE_INFO device_info;
|
||||
static struct DATA_INFO data_info;
|
||||
|
||||
static struct DEVICE_INFO *const dev_p = &device_info;
|
||||
static struct DATA_INFO *const data_p = &data_info;
|
||||
|
||||
#define REG_BASE (0x40005C00UL) /* USB_IP Peripheral Registers base address */
|
||||
#define PMA_ADDR (0x40006000UL) /* USB_IP Packet Memory Area base address */
|
||||
|
||||
@@ -153,7 +151,7 @@ static struct DATA_INFO *const data_p = &data_info;
|
||||
#define EPRX_DTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */
|
||||
#define EPRX_DTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */
|
||||
|
||||
static void usb_handle_transfer (uint16_t istr_value);
|
||||
static int usb_handle_transfer (struct usb_dev *dev, uint16_t istr_value);
|
||||
|
||||
static void st103_set_btable (void)
|
||||
{
|
||||
@@ -348,14 +346,29 @@ static void st103_ep_clear_dtog_tx (uint8_t ep_num)
|
||||
}
|
||||
}
|
||||
|
||||
void usb_lld_init (uint8_t feature)
|
||||
void
|
||||
usb_lld_ctrl_error (struct usb_dev *dev)
|
||||
{
|
||||
dev->state = STALLED;
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL);
|
||||
}
|
||||
|
||||
int
|
||||
usb_lld_ctrl_ack (struct usb_dev *dev)
|
||||
{
|
||||
dev->state = WAIT_STATUS_IN;
|
||||
st103_set_tx_count (ENDP0, 0);
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_NAK, EP_TX_VALID);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
void usb_lld_init (struct usb_dev *dev, uint8_t feature)
|
||||
{
|
||||
usb_lld_sys_init ();
|
||||
|
||||
dev_p->state = IN_DATA;
|
||||
|
||||
usb_lld_set_configuration (0);
|
||||
dev_p->current_feature = feature;
|
||||
dev->configuration = 0;
|
||||
dev->feature = feature;
|
||||
dev->state = WAIT_SETUP;
|
||||
|
||||
/* Reset USB */
|
||||
st103_set_cntr (CNTR_FRES);
|
||||
@@ -378,15 +391,18 @@ void usb_lld_shutdown (void)
|
||||
usb_lld_sys_shutdown ();
|
||||
}
|
||||
|
||||
void
|
||||
usb_interrupt_handler (void)
|
||||
#define USB_MAKE_EV(event) (event<<24)
|
||||
#define USB_MAKE_TXRX(ep_num,txrx,len) ((txrx? (1<<23):0)|(ep_num<<16)|len)
|
||||
|
||||
int
|
||||
usb_lld_event_handler (struct usb_dev *dev)
|
||||
{
|
||||
uint16_t istr_value = st103_get_istr ();
|
||||
|
||||
if ((istr_value & ISTR_RESET))
|
||||
{
|
||||
st103_set_istr (CLR_RESET);
|
||||
usb_cb_device_reset ();
|
||||
return USB_MAKE_EV (USB_EVENT_DEVICE_RESET);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -397,42 +413,45 @@ usb_interrupt_handler (void)
|
||||
st103_set_istr (CLR_ERR);
|
||||
|
||||
if ((istr_value & ISTR_CTR))
|
||||
usb_handle_transfer (istr_value);
|
||||
return usb_handle_transfer (dev, istr_value);
|
||||
}
|
||||
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
static void handle_datastage_out (void)
|
||||
static void handle_datastage_out (struct usb_dev *dev)
|
||||
{
|
||||
if (data_p->addr && data_p->len)
|
||||
if (dev->ctrl_data.addr && dev->ctrl_data.len)
|
||||
{
|
||||
uint32_t len = st103_get_rx_count (ENDP0);
|
||||
|
||||
if (len > data_p->len)
|
||||
len = data_p->len;
|
||||
if (len > dev->ctrl_data.len)
|
||||
len = dev->ctrl_data.len;
|
||||
|
||||
usb_lld_from_pmabuf (data_p->addr, st103_get_rx_addr (ENDP0), len);
|
||||
data_p->len -= len;
|
||||
data_p->addr += len;
|
||||
usb_lld_from_pmabuf (dev->ctrl_data.addr, st103_get_rx_addr (ENDP0), len);
|
||||
dev->ctrl_data.len -= len;
|
||||
dev->ctrl_data.addr += len;
|
||||
}
|
||||
|
||||
if (data_p->len == 0)
|
||||
if (dev->ctrl_data.len == 0)
|
||||
{
|
||||
dev_p->state = WAIT_STATUS_IN;
|
||||
dev->state = WAIT_STATUS_IN;
|
||||
st103_set_tx_count (ENDP0, 0);
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_VALID);
|
||||
st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
|
||||
}
|
||||
else
|
||||
{
|
||||
dev_p->state = OUT_DATA;
|
||||
dev->state = OUT_DATA;
|
||||
st103_ep_set_rx_status (ENDP0, EP_RX_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_datastage_in (void)
|
||||
static void handle_datastage_in (struct usb_dev *dev)
|
||||
{
|
||||
uint32_t len = USB_MAX_PACKET_SIZE;;
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
|
||||
if ((data_p->len == 0) && (dev_p->state == LAST_IN_DATA))
|
||||
if ((data_p->len == 0) && (dev->state == LAST_IN_DATA))
|
||||
{
|
||||
if (data_p->require_zlp)
|
||||
{
|
||||
@@ -440,19 +459,19 @@ static void handle_datastage_in (void)
|
||||
|
||||
/* No more data to send. Send empty packet */
|
||||
st103_set_tx_count (ENDP0, 0);
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_VALID, EP_TX_VALID);
|
||||
st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No more data to send, proceed to receive OUT acknowledge.*/
|
||||
dev_p->state = WAIT_STATUS_OUT;
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_VALID, EP_TX_STALL);
|
||||
/* No more data to send, proceed to receive OUT acknowledge. */
|
||||
dev->state = WAIT_STATUS_OUT;
|
||||
st103_ep_set_rx_status (ENDP0, EP_RX_VALID);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dev_p->state = (data_p->len <= len) ? LAST_IN_DATA : IN_DATA;
|
||||
dev->state = (data_p->len <= len) ? LAST_IN_DATA : IN_DATA;
|
||||
|
||||
if (len > data_p->len)
|
||||
len = data_p->len;
|
||||
@@ -464,29 +483,30 @@ static void handle_datastage_in (void)
|
||||
st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
|
||||
}
|
||||
|
||||
typedef int (*HANDLER) (uint8_t req, struct req_args *arg);
|
||||
typedef int (*HANDLER) (struct usb_dev *dev);
|
||||
|
||||
static int std_none (uint8_t req, struct req_args *arg)
|
||||
static int std_none (struct usb_dev *dev)
|
||||
{
|
||||
(void)req; (void)arg;
|
||||
return USB_UNSUPPORT;
|
||||
(void)dev;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_get_status (uint8_t req, struct req_args *arg)
|
||||
static int std_get_status (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = (arg->type & RECIPIENT);
|
||||
uint16_t status_info = 0;
|
||||
|
||||
if (arg->value != 0 || arg->len != 2 || (arg->index >> 8) != 0
|
||||
|| USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
|| USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
{
|
||||
if (arg->index == 0)
|
||||
{
|
||||
/* Get Device Status */
|
||||
uint8_t feature = dev_p->current_feature;
|
||||
uint8_t feature = dev->feature;
|
||||
|
||||
/* Remote Wakeup enabled */
|
||||
if ((feature & (1 << 5)))
|
||||
@@ -500,21 +520,15 @@ static int std_get_status (uint8_t req, struct req_args *arg)
|
||||
else /* Self-powered */
|
||||
status_info &= ~1;
|
||||
|
||||
return usb_lld_reply_request (&status_info, 2, arg);
|
||||
return usb_lld_ctrl_send (dev, &status_info, 2);
|
||||
}
|
||||
}
|
||||
else if (rcp == INTERFACE_RECIPIENT)
|
||||
{
|
||||
int r;
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (dev_p->current_configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
r = usb_cb_interface (USB_QUERY_INTERFACE, arg);
|
||||
if (r != USB_SUCCESS)
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
return usb_lld_reply_request (&status_info, 2, arg);
|
||||
return USB_EVENT_GET_STATUS_INTERFACE;
|
||||
}
|
||||
else if (rcp == ENDPOINT_RECIPIENT)
|
||||
{
|
||||
@@ -522,13 +536,13 @@ static int std_get_status (uint8_t req, struct req_args *arg)
|
||||
uint16_t status;
|
||||
|
||||
if ((arg->index & 0x70) || endpoint == ENDP0)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if ((arg->index & 0x80))
|
||||
{
|
||||
status = st103_ep_get_tx_status (endpoint);
|
||||
if (status == 0) /* Disabled */
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
else if (status == EP_TX_STALL)
|
||||
status_info |= 1; /* IN Endpoint stalled */
|
||||
}
|
||||
@@ -536,33 +550,34 @@ static int std_get_status (uint8_t req, struct req_args *arg)
|
||||
{
|
||||
status = st103_ep_get_rx_status (endpoint);
|
||||
if (status == 0) /* Disabled */
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
else if (status == EP_RX_STALL)
|
||||
status_info |= 1; /* OUT Endpoint stalled */
|
||||
}
|
||||
|
||||
return usb_lld_reply_request (&status_info, 2, arg);
|
||||
return usb_lld_ctrl_send (dev, &status_info, 2);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_clear_feature (uint8_t req, struct req_args *arg)
|
||||
static int std_clear_feature (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
{
|
||||
if (arg->len != 0 || arg->index != 0)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if (arg->value == DEVICE_REMOTE_WAKEUP)
|
||||
if (arg->value == FEATURE_DEVICE_REMOTE_WAKEUP)
|
||||
{
|
||||
dev_p->current_feature &= ~(1 << 5);
|
||||
return USB_SUCCESS;
|
||||
dev->feature &= ~(1 << 5);
|
||||
return USB_EVENT_CLEAR_FEATURE_DEVICE;
|
||||
}
|
||||
}
|
||||
else if (rcp == ENDPOINT_RECIPIENT)
|
||||
@@ -570,50 +585,49 @@ static int std_clear_feature (uint8_t req, struct req_args *arg)
|
||||
uint8_t endpoint = (arg->index & 0x0f);
|
||||
uint16_t status;
|
||||
|
||||
if (dev_p->current_configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (arg->len != 0 || (arg->index >> 8) != 0
|
||||
|| arg->value != ENDPOINT_STALL || endpoint == ENDP0)
|
||||
return USB_UNSUPPORT;
|
||||
|| arg->value != FEATURE_ENDPOINT_HALT || endpoint == ENDP0)
|
||||
return -1;
|
||||
|
||||
if ((arg->index & 0x80))
|
||||
status = st103_ep_get_tx_status (endpoint);
|
||||
else
|
||||
status = st103_ep_get_rx_status (endpoint);
|
||||
|
||||
if (status == 0) /* Disabled */
|
||||
return USB_UNSUPPORT;
|
||||
if (status == 0) /* It's disabled endpoint. */
|
||||
return -1;
|
||||
|
||||
if (arg->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);
|
||||
|
||||
// event??
|
||||
return USB_SUCCESS;
|
||||
return USB_EVENT_CLEAR_FEATURE_ENDPOINT;
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_set_feature (uint8_t req, struct req_args *arg)
|
||||
static int std_set_feature (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
{
|
||||
if (arg->len != 0 || arg->index != 0)
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
|
||||
if (arg->value == DEVICE_REMOTE_WAKEUP)
|
||||
if (arg->value == FEATURE_DEVICE_REMOTE_WAKEUP)
|
||||
{
|
||||
dev_p->current_feature |= 1 << 5;
|
||||
// event??
|
||||
return USB_SUCCESS;
|
||||
dev->feature |= 1 << 5;
|
||||
return USB_EVENT_SET_FEATURE_DEVICE;
|
||||
}
|
||||
}
|
||||
else if (rcp == ENDPOINT_RECIPIENT)
|
||||
@@ -621,282 +635,286 @@ static int std_set_feature (uint8_t req, struct req_args *arg)
|
||||
uint8_t endpoint = (arg->index & 0x0f);
|
||||
uint32_t status;
|
||||
|
||||
if (dev_p->current_configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (arg->len != 0 || (arg->index >> 8) != 0
|
||||
|| arg->value != 0 || endpoint == ENDP0)
|
||||
return USB_UNSUPPORT;
|
||||
|| arg->value != FEATURE_ENDPOINT_HALT || endpoint == ENDP0)
|
||||
return -1;
|
||||
|
||||
if ((arg->index & 0x80))
|
||||
status = st103_ep_get_tx_status (endpoint);
|
||||
else
|
||||
status = st103_ep_get_rx_status (endpoint);
|
||||
|
||||
if (status == 0) /* Disabled */
|
||||
return USB_UNSUPPORT;
|
||||
if (status == 0) /* It's disabled endpoint. */
|
||||
return -1;
|
||||
|
||||
if (arg->index & 0x80)
|
||||
/* IN endpoint */
|
||||
if (arg->index & 0x80) /* IN endpoint */
|
||||
st103_ep_set_tx_status (endpoint, EP_TX_STALL);
|
||||
else
|
||||
/* OUT endpoint */
|
||||
else /* OUT endpoint */
|
||||
st103_ep_set_rx_status (endpoint, EP_RX_STALL);
|
||||
|
||||
// event??
|
||||
return USB_SUCCESS;
|
||||
return USB_EVENT_SET_FEATURE_ENDPOINT;
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_set_address (uint8_t req, struct req_args *arg)
|
||||
static int std_set_address (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT && arg->len == 0 && arg->value <= 127
|
||||
&& arg->index == 0 && dev_p->current_configuration == 0)
|
||||
return USB_SUCCESS;
|
||||
&& arg->index == 0 && dev->configuration == 0)
|
||||
return usb_lld_ctrl_ack (dev);
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_get_descriptor (uint8_t req, struct req_args *arg)
|
||||
static int std_get_descriptor (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
if (USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
return usb_cb_get_descriptor (rcp, (arg->value >> 8),
|
||||
(arg->value & 0xff), arg);
|
||||
return USB_EVENT_GET_DESCRIPTOR;
|
||||
}
|
||||
|
||||
static int std_get_configuration (uint8_t req, struct req_args *arg)
|
||||
static int std_get_configuration (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (arg->value != 0 || arg->index != 0 || arg->len != 1)
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
return usb_lld_reply_request (&dev_p->current_configuration, 1, arg);
|
||||
return usb_lld_ctrl_send (dev, &dev->configuration, 1);
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_set_configuration (uint8_t req, struct req_args *arg)
|
||||
static int std_set_configuration (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_GET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (rcp == DEVICE_RECIPIENT && arg->index == 0 && arg->len == 0)
|
||||
return usb_cb_handle_event (USB_EVENT_CONFIG, arg->value);
|
||||
if (arg->index != 0 || arg->len != 0)
|
||||
return -1;
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
if (rcp == DEVICE_RECIPIENT)
|
||||
return USB_EVENT_SET_CONFIGURATION;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_get_interface (uint8_t req, struct req_args *arg)
|
||||
static int std_get_interface (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_SET (req))
|
||||
return USB_UNSUPPORT;
|
||||
if (USB_SETUP_SET (arg->type))
|
||||
return -1;
|
||||
|
||||
if (arg->value != 0 || (arg->index >> 8) != 0 || arg->len != 1)
|
||||
return -1;
|
||||
|
||||
if (dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
if (rcp == INTERFACE_RECIPIENT)
|
||||
{
|
||||
if (arg->value != 0 || (arg->index >> 8) != 0 || arg->len != 1)
|
||||
return USB_UNSUPPORT;
|
||||
return USB_EVENT_GET_INTERFACE;
|
||||
|
||||
if (dev_p->current_configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
|
||||
return usb_cb_interface (USB_GET_INTERFACE, arg);
|
||||
}
|
||||
|
||||
return USB_UNSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int std_set_interface (uint8_t req, struct req_args *arg)
|
||||
static int std_set_interface (struct usb_dev *dev)
|
||||
{
|
||||
uint8_t rcp = req & RECIPIENT;
|
||||
struct device_req *arg = &dev->dev_req;
|
||||
uint8_t rcp = arg->type & RECIPIENT;
|
||||
|
||||
if (USB_SETUP_GET (req) || rcp != INTERFACE_RECIPIENT
|
||||
if (USB_SETUP_GET (arg->type) || rcp != INTERFACE_RECIPIENT
|
||||
|| arg->len != 0 || (arg->index >> 8) != 0
|
||||
|| (arg->value >> 8) != 0 || dev_p->current_configuration == 0)
|
||||
return USB_UNSUPPORT;
|
||||
|| (arg->value >> 8) != 0 || dev->configuration == 0)
|
||||
return -1;
|
||||
|
||||
return usb_cb_interface (USB_SET_INTERFACE, arg);
|
||||
return USB_EVENT_SET_INTERFACE;
|
||||
}
|
||||
|
||||
|
||||
static void handle_setup0 (void)
|
||||
static int handle_setup0 (struct usb_dev *dev)
|
||||
{
|
||||
const uint16_t *pw;
|
||||
uint16_t w;
|
||||
uint8_t req_no;
|
||||
int r = USB_UNSUPPORT;
|
||||
HANDLER handler;
|
||||
|
||||
pw = (uint16_t *)(PMA_ADDR + (uint8_t *)(st103_get_rx_addr (ENDP0) * 2));
|
||||
w = *pw++;
|
||||
|
||||
dev_p->bmRequestType = w & 0xff;
|
||||
dev_p->bRequest = req_no = w >> 8;
|
||||
dev->dev_req.type = (w & 0xff);
|
||||
dev->dev_req.request = req_no = (w >> 8);
|
||||
pw++;
|
||||
dev_p->value = *pw++;
|
||||
dev->dev_req.value = *pw++;
|
||||
pw++;
|
||||
dev_p->index = *pw++;
|
||||
dev->dev_req.index = *pw++;
|
||||
pw++;
|
||||
dev_p->len = *pw;
|
||||
dev->dev_req.len = *pw;
|
||||
|
||||
data_p->addr = NULL;
|
||||
data_p->len = 0;
|
||||
data_p->require_zlp = 0;
|
||||
dev->ctrl_data.addr = NULL;
|
||||
dev->ctrl_data.len = 0;
|
||||
dev->ctrl_data.require_zlp = 0;
|
||||
|
||||
if ((dev_p->bmRequestType & REQUEST_TYPE) == STANDARD_REQUEST)
|
||||
if ((dev->dev_req.type & REQUEST_TYPE) == STANDARD_REQUEST)
|
||||
{
|
||||
if (req_no < TOTAL_REQUEST)
|
||||
{
|
||||
switch (req_no)
|
||||
{
|
||||
case 0: handler = std_get_status; break;
|
||||
case 1: handler = std_clear_feature; break;
|
||||
case 3: handler = std_set_feature; break;
|
||||
case 5: handler = std_set_address; break;
|
||||
case 6: handler = std_get_descriptor; break;
|
||||
case 8: handler = std_get_configuration; break;
|
||||
case 9: handler = std_set_configuration; break;
|
||||
case 10: handler = std_get_interface; break;
|
||||
case 11: handler = std_set_interface; break;
|
||||
default: handler = std_none; break;
|
||||
}
|
||||
int r;
|
||||
|
||||
r = (*handler) (dev_p->bmRequestType,
|
||||
(struct req_args *)&dev_p->value);
|
||||
switch (req_no)
|
||||
{
|
||||
case 0: handler = std_get_status; break;
|
||||
case 1: handler = std_clear_feature; break;
|
||||
case 3: handler = std_set_feature; break;
|
||||
case 5: handler = std_set_address; break;
|
||||
case 6: handler = std_get_descriptor; break;
|
||||
case 8: handler = std_get_configuration; break;
|
||||
case 9: handler = std_set_configuration; break;
|
||||
case 10: handler = std_get_interface; break;
|
||||
case 11: handler = std_set_interface; break;
|
||||
default: handler = std_none; break;
|
||||
}
|
||||
}
|
||||
else
|
||||
r = usb_cb_setup (dev_p->bmRequestType, req_no,
|
||||
(struct req_args *)&dev_p->value);
|
||||
|
||||
if (r != USB_SUCCESS)
|
||||
dev_p->state = STALLED;
|
||||
else
|
||||
{
|
||||
if (USB_SETUP_SET (dev_p->bmRequestType))
|
||||
if ((r = (*handler) (dev)) < 0)
|
||||
{
|
||||
if (dev_p->len == 0)
|
||||
{
|
||||
dev_p->state = WAIT_STATUS_IN;
|
||||
st103_set_tx_count (ENDP0, 0);
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_VALID);
|
||||
}
|
||||
else
|
||||
{
|
||||
dev_p->state = OUT_DATA;
|
||||
st103_ep_set_rx_status (ENDP0, EP_RX_VALID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_in0 (void)
|
||||
{
|
||||
if (dev_p->state == IN_DATA || dev_p->state == LAST_IN_DATA)
|
||||
handle_datastage_in ();
|
||||
else if (dev_p->state == WAIT_STATUS_IN)
|
||||
{
|
||||
if ((dev_p->bRequest == SET_ADDRESS) &&
|
||||
((dev_p->bmRequestType & (REQUEST_TYPE | RECIPIENT))
|
||||
== (STANDARD_REQUEST | DEVICE_RECIPIENT)))
|
||||
{
|
||||
st103_set_daddr (dev_p->value);
|
||||
usb_cb_handle_event (USB_EVENT_ADDRESS, dev_p->value);
|
||||
usb_lld_ctrl_error (dev);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
else
|
||||
usb_cb_ctrl_write_finish (dev_p->bmRequestType, dev_p->bRequest,
|
||||
(struct req_args *)&dev_p->value);
|
||||
|
||||
dev_p->state = STALLED;
|
||||
return r;
|
||||
}
|
||||
else
|
||||
dev_p->state = STALLED;
|
||||
return USB_EVENT_CTRL_REQUEST;
|
||||
}
|
||||
|
||||
static void handle_out0 (void)
|
||||
static int handle_in0 (struct usb_dev *dev)
|
||||
{
|
||||
if (dev_p->state == IN_DATA || dev_p->state == LAST_IN_DATA)
|
||||
/* host aborts the transfer before finish */
|
||||
dev_p->state = STALLED;
|
||||
else if (dev_p->state == OUT_DATA)
|
||||
handle_datastage_out ();
|
||||
else if (dev_p->state == WAIT_STATUS_OUT)
|
||||
dev_p->state = STALLED;
|
||||
/* Unexpect state, STALL the endpoint */
|
||||
int r = 0;
|
||||
|
||||
if (dev->state == IN_DATA || dev->state == LAST_IN_DATA)
|
||||
handle_datastage_in (dev);
|
||||
else if (dev->state == WAIT_STATUS_IN)
|
||||
{
|
||||
dev->state = WAIT_SETUP;
|
||||
|
||||
if ((dev->dev_req.request == SET_ADDRESS) &&
|
||||
((dev->dev_req.type & (REQUEST_TYPE | RECIPIENT))
|
||||
== (STANDARD_REQUEST | DEVICE_RECIPIENT)))
|
||||
{
|
||||
st103_set_daddr (dev->dev_req.value);
|
||||
r = USB_EVENT_DEVICE_ADDRESSED;
|
||||
}
|
||||
else
|
||||
r = USB_EVENT_CTRL_WRITE_FINISH;
|
||||
}
|
||||
else
|
||||
dev_p->state = STALLED;
|
||||
{
|
||||
dev->state = STALLED;
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static void
|
||||
usb_handle_transfer (uint16_t istr_value)
|
||||
|
||||
static void handle_out0 (struct usb_dev *dev)
|
||||
{
|
||||
if (dev->state == OUT_DATA)
|
||||
/* Usual case. */
|
||||
handle_datastage_out (dev);
|
||||
else if (dev->state == WAIT_STATUS_OUT)
|
||||
/*
|
||||
* Control READ transfer finished by ZLP.
|
||||
* Leave ENDP0 status RX_NAK, TX_NAK.
|
||||
*/
|
||||
dev->state = WAIT_SETUP;
|
||||
else
|
||||
{
|
||||
/*
|
||||
* dev->state == IN_DATA || dev->state == LAST_IN_DATA
|
||||
* (Host aborts the transfer before finish)
|
||||
* Or else, unexpected state.
|
||||
* STALL the endpoint, until we receive the next SETUP token.
|
||||
*/
|
||||
dev->state = STALLED;
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
usb_handle_transfer (struct usb_dev *dev, uint16_t istr_value)
|
||||
{
|
||||
uint16_t ep_value = 0;
|
||||
uint8_t ep_index;
|
||||
uint8_t ep_num = (istr_value & ISTR_EP_ID);
|
||||
|
||||
ep_index = (istr_value & ISTR_EP_ID);
|
||||
/* Decode and service non control endpoints interrupt */
|
||||
/* process related endpoint register */
|
||||
ep_value = st103_get_epreg (ep_index);
|
||||
ep_value = st103_get_epreg (ep_num);
|
||||
|
||||
if (ep_index == 0)
|
||||
if (ep_num == 0)
|
||||
{
|
||||
if ((ep_value & EP_CTR_TX))
|
||||
{
|
||||
st103_ep_clear_ctr_tx (ep_index);
|
||||
handle_in0 ();
|
||||
st103_ep_clear_ctr_tx (ep_num);
|
||||
return USB_MAKE_EV (handle_in0 (dev));
|
||||
}
|
||||
|
||||
if ((ep_value & EP_CTR_RX))
|
||||
{
|
||||
st103_ep_clear_ctr_rx (ep_index);
|
||||
st103_ep_clear_ctr_rx (ep_num);
|
||||
|
||||
if ((ep_value & EP_SETUP))
|
||||
handle_setup0 ();
|
||||
return USB_MAKE_EV (handle_setup0 (dev));
|
||||
else
|
||||
handle_out0 ();
|
||||
{
|
||||
handle_out0 (dev);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
}
|
||||
|
||||
if (dev_p->state == STALLED)
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t len;
|
||||
|
||||
if ((ep_value & EP_CTR_RX))
|
||||
{
|
||||
st103_ep_clear_ctr_rx (ep_index);
|
||||
usb_cb_rx_ready (ep_index);
|
||||
len = st103_get_rx_count (ep_num);
|
||||
st103_ep_clear_ctr_rx (ep_num);
|
||||
return USB_MAKE_TXRX (ep_num, 0, len);
|
||||
}
|
||||
|
||||
if ((ep_value & EP_CTR_TX))
|
||||
{
|
||||
uint32_t len = st103_get_tx_count (ep_index);
|
||||
|
||||
st103_ep_clear_ctr_tx (ep_index);
|
||||
usb_cb_tx_done (ep_index, len);
|
||||
len = st103_get_tx_count (ep_num);
|
||||
st103_ep_clear_ctr_tx (ep_num);
|
||||
return USB_MAKE_TXRX (ep_num, 1, len);
|
||||
}
|
||||
}
|
||||
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
void usb_lld_reset (uint8_t feature)
|
||||
void usb_lld_reset (struct usb_dev *dev, uint8_t feature)
|
||||
{
|
||||
usb_lld_set_configuration (0);
|
||||
dev_p->current_feature = feature;
|
||||
usb_lld_set_configuration (dev, 0);
|
||||
dev->feature = feature;
|
||||
st103_set_btable ();
|
||||
st103_set_daddr (0);
|
||||
}
|
||||
@@ -926,16 +944,6 @@ void usb_lld_tx_enable (int ep_num, size_t len)
|
||||
st103_ep_set_tx_status (ep_num, EP_TX_VALID);
|
||||
}
|
||||
|
||||
int usb_lld_tx_data_len (int ep_num)
|
||||
{
|
||||
return st103_get_tx_count (ep_num);
|
||||
}
|
||||
|
||||
int usb_lld_rx_data_len (int ep_num)
|
||||
{
|
||||
return st103_get_rx_count (ep_num);
|
||||
}
|
||||
|
||||
void usb_lld_stall_tx (int ep_num)
|
||||
{
|
||||
st103_ep_set_tx_status (ep_num, EP_TX_STALL);
|
||||
@@ -993,20 +1001,24 @@ void usb_lld_setup_endpoint (int ep_num, int ep_type, int ep_kind,
|
||||
st103_set_epreg (ep_num, epreg_value);
|
||||
}
|
||||
|
||||
void usb_lld_set_configuration (uint8_t config)
|
||||
void usb_lld_set_configuration (struct usb_dev *dev, uint8_t config)
|
||||
{
|
||||
dev_p->current_configuration = config;
|
||||
dev->configuration = config;
|
||||
}
|
||||
|
||||
uint8_t usb_lld_current_configuration (void)
|
||||
uint8_t usb_lld_current_configuration (struct usb_dev *dev)
|
||||
{
|
||||
return dev_p->current_configuration;
|
||||
return dev->configuration;
|
||||
}
|
||||
|
||||
void usb_lld_set_data_to_recv (void *p, size_t len)
|
||||
int usb_lld_ctrl_recv (struct usb_dev *dev, void *p, size_t len)
|
||||
{
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
data_p->addr = p;
|
||||
data_p->len = len;
|
||||
dev->state = OUT_DATA;
|
||||
st103_ep_set_rx_status (ENDP0, EP_RX_VALID);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
void usb_lld_to_pmabuf (const void *src, uint16_t addr, size_t n)
|
||||
@@ -1090,9 +1102,10 @@ 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 req_args *ctl)
|
||||
usb_lld_ctrl_send (struct usb_dev *dev, const void *buf, size_t buflen)
|
||||
{
|
||||
uint32_t len_asked = ctl->len;
|
||||
struct ctrl_data *data_p = &dev->ctrl_data;
|
||||
uint32_t len_asked = dev->dev_req.len;
|
||||
uint32_t len;
|
||||
|
||||
data_p->addr = (void *)buf;
|
||||
@@ -1108,12 +1121,12 @@ usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *ctl)
|
||||
if (data_p->len < USB_MAX_PACKET_SIZE)
|
||||
{
|
||||
len = data_p->len;
|
||||
dev_p->state = LAST_IN_DATA;
|
||||
dev->state = LAST_IN_DATA;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = USB_MAX_PACKET_SIZE;
|
||||
dev_p->state = IN_DATA;
|
||||
dev->state = IN_DATA;
|
||||
}
|
||||
|
||||
if (len)
|
||||
@@ -1124,6 +1137,6 @@ usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *ctl)
|
||||
}
|
||||
|
||||
st103_set_tx_count (ENDP0, len);
|
||||
st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
|
||||
return USB_SUCCESS;
|
||||
st103_ep_set_rxtx_status (ENDP0, EP_RX_NAK, EP_TX_VALID);
|
||||
return USB_EVENT_OK;
|
||||
}
|
||||
|
||||
2
rules.mk
2
rules.mk
@@ -13,7 +13,7 @@ ifneq ($(USE_USB),)
|
||||
CSRC += $(CHOPSTX)/mcu/usb-$(CHIP).c
|
||||
endif
|
||||
ifneq ($(USE_ADC),)
|
||||
CSRC += $(CHOPSTX)/mcu/adc-$(CHIP).c
|
||||
CSRC += $(CHOPSTX)/contrib/adc-$(CHIP).c
|
||||
endif
|
||||
|
||||
INCDIR += $(CHOPSTX)
|
||||
|
||||
4
sys.h
4
sys.h
@@ -1,5 +1,7 @@
|
||||
#ifdef MCU_KINETIS_L
|
||||
#if defined(MCU_KINETIS_L)
|
||||
#include "mcu/sys-mkl27z.h"
|
||||
#elif defined(MCU_STM32F0)
|
||||
#include "mcu/sys-stm32f0.h"
|
||||
#else
|
||||
#include "mcu/sys-stm32f103.h"
|
||||
#endif
|
||||
|
||||
117
usb_lld.h
117
usb_lld.h
@@ -37,45 +37,52 @@ enum DESCRIPTOR_TYPE
|
||||
#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 {
|
||||
struct device_req {
|
||||
uint8_t type;
|
||||
uint8_t request;
|
||||
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, uint32_t len);
|
||||
void usb_cb_rx_ready (uint8_t ep_num);
|
||||
struct ctrl_data {
|
||||
uint8_t *addr;
|
||||
uint16_t len;
|
||||
uint8_t require_zlp;
|
||||
};
|
||||
|
||||
enum {
|
||||
USB_EVENT_ADDRESS,
|
||||
USB_EVENT_CONFIG,
|
||||
USB_EVENT_SUSPEND,
|
||||
USB_EVENT_WAKEUP,
|
||||
USB_EVENT_STALL,
|
||||
struct usb_dev {
|
||||
uint8_t configuration;
|
||||
uint8_t feature;
|
||||
uint8_t state;
|
||||
struct device_req dev_req;
|
||||
struct ctrl_data ctrl_data;
|
||||
};
|
||||
|
||||
enum {
|
||||
USB_SET_INTERFACE,
|
||||
USB_GET_INTERFACE,
|
||||
USB_QUERY_INTERFACE,
|
||||
USB_EVENT_OK=0, /* Processed in lower layer. */
|
||||
/* Device reset and suspend. */
|
||||
USB_EVENT_DEVICE_RESET,
|
||||
USB_EVENT_DEVICE_SUSPEND,
|
||||
/* Device Requests (Control WRITE Transfer): Standard */
|
||||
USB_EVENT_SET_CONFIGURATION,
|
||||
USB_EVENT_SET_INTERFACE,
|
||||
USB_EVENT_SET_FEATURE_DEVICE,
|
||||
USB_EVENT_SET_FEATURE_ENDPOINT,
|
||||
USB_EVENT_CLEAR_FEATURE_DEVICE,
|
||||
USB_EVENT_CLEAR_FEATURE_ENDPOINT,
|
||||
/* Device Requests (Control READ Transfer): Standard */
|
||||
USB_EVENT_GET_STATUS_INTERFACE,
|
||||
USB_EVENT_GET_DESCRIPTOR,
|
||||
USB_EVENT_GET_INTERFACE,
|
||||
/* Device Requests (Control READ/WRITE Transfer): Non-Standard */
|
||||
USB_EVENT_CTRL_REQUEST,
|
||||
USB_EVENT_CTRL_WRITE_FINISH,
|
||||
/* Device addressed. */
|
||||
USB_EVENT_DEVICE_ADDRESSED,
|
||||
};
|
||||
|
||||
enum DEVICE_STATE
|
||||
{
|
||||
enum DEVICE_STATE {
|
||||
UNCONNECTED,
|
||||
ATTACHED,
|
||||
POWERED,
|
||||
@@ -84,23 +91,54 @@ enum DEVICE_STATE
|
||||
CONFIGURED
|
||||
};
|
||||
|
||||
void usb_lld_init (uint8_t feature);
|
||||
int usb_lld_reply_request (const void *buf, size_t buflen,
|
||||
struct req_args *arg);
|
||||
int usb_lld_rx_data_len (int ep_num);
|
||||
void usb_lld_reset (uint8_t feature);
|
||||
void usb_lld_set_configuration (uint8_t config);
|
||||
uint8_t usb_lld_current_configuration (void);
|
||||
void usb_lld_init (struct usb_dev *dev, uint8_t feature);
|
||||
|
||||
/*
|
||||
* Return value is encoded integer:
|
||||
* event-no: 8-bit, 0 if TX/RX
|
||||
* tx/rx-flag: 1-bit, 0 if rx, 1 if tx
|
||||
* endpoint no: 7-bit
|
||||
* length: 16-bit
|
||||
*/
|
||||
#define USB_EVENT_TXRX(e) ((e >> 23) & 1)
|
||||
#define USB_EVENT_LEN(e) (e & 0xffff)
|
||||
#define USB_EVENT_ENDP(e) ((e >> 16) & 0x7f)
|
||||
#define USB_EVENT_ID(e) ((e >> 24))
|
||||
int usb_lld_event_handler (struct usb_dev *dev);
|
||||
|
||||
/*
|
||||
* Control Endpoint ENDP0 does device requests handling.
|
||||
* In response to an event of
|
||||
* USB_EVENT_SET_CONFIGURATION
|
||||
* USB_EVENT_SET_INTERFACE
|
||||
* USB_EVENT_SET_FEATURE_DEVICE
|
||||
* USB_EVENT_SET_FEATURE_ENDPOINT
|
||||
* USB_EVENT_CLEAR_FEATURE_DEVICE
|
||||
* USB_EVENT_CLEAR_FEATURE_ENDPOINT
|
||||
* USB_EVENT_GET_STATUS_INTERFACE
|
||||
* USB_EVENT_GET_DESCRIPTOR
|
||||
* USB_EVENT_GET_INTERFACE
|
||||
* USB_EVENT_CTRL_REQUEST
|
||||
* a single action should be done, which is SEND, RECV, or,
|
||||
* ACKNOWLEDGE (no data to be sent, or to be received).
|
||||
* Otherwise, it's an error.
|
||||
*/
|
||||
int usb_lld_ctrl_send (struct usb_dev *dev, const void *buf, size_t buflen);
|
||||
int usb_lld_ctrl_recv (struct usb_dev *dev, void *p, size_t len);
|
||||
int usb_lld_ctrl_ack (struct usb_dev *dev);
|
||||
void usb_lld_ctrl_error (struct usb_dev *dev);
|
||||
|
||||
void usb_lld_reset (struct usb_dev *dev, uint8_t feature);
|
||||
void usb_lld_set_configuration (struct usb_dev *dev, uint8_t config);
|
||||
uint8_t usb_lld_current_configuration (struct usb_dev *dev);
|
||||
void usb_lld_prepare_shutdown (void);
|
||||
void usb_lld_shutdown (void);
|
||||
void usb_interrupt_handler (void);
|
||||
void usb_lld_set_data_to_recv (void *p, size_t len);
|
||||
|
||||
#ifdef MCU_KINETIS_L
|
||||
void usb_lld_tx_enable_buf (int ep_num, const void *buf, size_t len);
|
||||
void usb_lld_rx_enable_buf (int ep_num, void *buf, size_t len);
|
||||
|
||||
void usb_lld_setup_endp (int ep_num, int rx_en, int tx_en);
|
||||
void usb_lld_setup_endp (struct usb_dev *dev, int ep_num, int rx_en, int tx_en);
|
||||
void usb_lld_stall (int ep_num);
|
||||
#else
|
||||
/* EP_TYPE[1:0] EndPoint TYPE */
|
||||
@@ -118,10 +156,9 @@ void usb_lld_setup_endpoint (int ep_num, int ep_type, int ep_kind,
|
||||
void usb_lld_stall_tx (int ep_num);
|
||||
void usb_lld_stall_rx (int ep_num);
|
||||
|
||||
int usb_lld_tx_data_len (int ep_num);
|
||||
void usb_lld_txcpy (const void *src, int ep_num, int offset, size_t len);
|
||||
void usb_lld_write (uint8_t ep_num, const void *buf, size_t len);
|
||||
void usb_lld_rxcpy (uint8_t *dst, int ep_num, int offset, size_t len);
|
||||
void usb_lld_to_pmabuf (const void *src, uint16_t addr, size_t n);
|
||||
void usb_lld_from_pmabuf (void *dst, uint16_t addr, size_t n);
|
||||
void usb_lld_rxcpy (uint8_t *dst, int ep_num, int offset, size_t len);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user