integrate ccid and usb threads

This commit is contained in:
NIIBE Yutaka
2016-05-18 21:34:14 +09:00
parent 48905155c5
commit 51d4b754e6
5 changed files with 124 additions and 156 deletions

Submodule chopstx updated: 5e33e7f468...1b0fe5a6e8

View File

@@ -4,9 +4,9 @@
__main_stack_size__ = 0x0080; /* Exception handlers */
__process0_stack_size__ = 0x0180; /* main */
__process1_stack_size__ = 0x0160; /* ccid */
__process2_stack_size__ = 0x0140; /* rng */
__process2_stack_size__ = 0x0160; /* rng */
__process3_stack_size__ = 0x1640; /* gpg */
__process4_stack_size__ = 0x0100; /* intr: usb */
__process4_stack_size__ = 0; /* --- */
__process5_stack_size__ = @MSC_SIZE@; /* msc */
__process6_stack_size__ = @TIM_SIZE@; /* intr: timer */
__process7_stack_size__ = @EXT_SIZE@; /* intr: ext */

View File

@@ -37,65 +37,6 @@
#include "random.h"
#include "stm32f103.h"
#ifdef DEBUG
#include "debug.h"
struct stdout stdout;
static void
stdout_init (void)
{
chopstx_mutex_init (&stdout.m);
chopstx_mutex_init (&stdout.m_dev);
chopstx_cond_init (&stdout.cond_dev);
stdout.connected = 0;
}
void
_write (const char *s, int len)
{
int packet_len;
if (len == 0)
return;
chopstx_mutex_lock (&stdout.m);
chopstx_mutex_lock (&stdout.m_dev);
if (!stdout.connected)
chopstx_cond_wait (&stdout.cond_dev, &stdout.m_dev);
chopstx_mutex_unlock (&stdout.m_dev);
do
{
packet_len =
(len < VIRTUAL_COM_PORT_DATA_SIZE) ? len : VIRTUAL_COM_PORT_DATA_SIZE;
chopstx_mutex_lock (&stdout.m_dev);
usb_lld_write (ENDP3, s, packet_len);
chopstx_cond_wait (&stdout.cond_dev, &stdout.m_dev);
chopstx_mutex_unlock (&stdout.m_dev);
s += packet_len;
len -= packet_len;
}
/* Send a Zero-Length-Packet if the last packet is full size. */
while (len != 0 || packet_len == VIRTUAL_COM_PORT_DATA_SIZE);
chopstx_mutex_unlock (&stdout.m);
}
#else
void
_write (const char *s, int size)
{
(void)s;
(void)size;
}
#endif
extern void *ccid_thread (void *arg);
/*
* main thread does 1-bit LED display output
@@ -261,19 +202,13 @@ calculate_regnual_entry_address (const uint8_t *addr)
}
extern uint8_t __process1_stack_base__, __process1_stack_size__;
extern uint8_t __process4_stack_base__, __process4_stack_size__;
const uint32_t __stackaddr_ccid = (uint32_t)&__process1_stack_base__;
const size_t __stacksize_ccid = (size_t)&__process1_stack_size__;
const uint32_t __stackaddr_usb = (uint32_t)&__process4_stack_base__;
const size_t __stacksize_usb = (size_t)&__process4_stack_size__;
#define PRIO_CCID 3
#define PRIO_USB 4
#define PRIO_MAIN 5
extern void *usb_intr (void *arg);
extern void *ccid_thread (void *arg);
static void gnuk_malloc_init (void);
@@ -282,16 +217,12 @@ extern uint32_t bDeviceState;
/*
* Entry point.
*
* NOTE: the main function is already a thread in the system on entry.
* See the hwinit1_common function.
*/
int
main (int argc, char *argv[])
{
unsigned int count = 0;
uint32_t entry;
chopstx_t usb_thd;
chopstx_t ccid_thd;
(void)argc;
@@ -322,9 +253,6 @@ main (int argc, char *argv[])
msc_init ();
#endif
usb_thd = chopstx_create (PRIO_USB, __stackaddr_usb, __stacksize_usb,
usb_intr, NULL);
chopstx_setpriority (PRIO_MAIN);
while (1)
@@ -400,9 +328,6 @@ main (int argc, char *argv[])
/* Finish application. */
chopstx_join (ccid_thd, NULL);
chopstx_cancel (usb_thd);
chopstx_join (usb_thd, NULL);
/* Set vector */
SCB->VTOR = (uint32_t)&_regnual_start;
entry = calculate_regnual_entry_address (&_regnual_start);
@@ -444,6 +369,8 @@ main (int argc, char *argv[])
void
fatal (uint8_t code)
{
extern void _write (const char *s, int len);
fatal_code = code;
eventflag_signal (&led_event, LED_FATAL);
_write ("fatal\r\n", 7);

View File

@@ -29,8 +29,14 @@
#include "config.h"
#ifdef DEBUG
#include "debug.h"
struct stdout stdout;
#endif
#include "gnuk.h"
#include "usb_lld.h"
#include "usb_conf.h"
/*
* USB buffer size of USB-CCID driver
@@ -353,7 +359,7 @@ static void get_sw1sw2 (struct ep_in *epi, size_t len)
/*
* Tx done callback
*/
void
static void
EP1_IN_Callback (void)
{
struct ep_in *epi = &endpoint_in;
@@ -630,7 +636,7 @@ icc_prepare_receive (struct ccid *c)
* Rx ready callback
*/
void
static void
EP1_OUT_Callback (void)
{
struct ep_out *epo = &endpoint_out;
@@ -671,6 +677,46 @@ EP1_OUT_Callback (void)
}
extern void EP6_IN_Callback (void);
void
usb_cb_rx_ready (uint8_t ep_num)
{
if (ep_num == ENDP1)
EP1_OUT_Callback ();
#ifdef DEBUG
else if (ep_num == ENDP5)
{
chopstx_mutex_lock (&stdout.m_dev);
usb_lld_rx_enable (ep_num);
chopstx_mutex_unlock (&stdout.m_dev);
}
#endif
}
void
usb_cb_tx_done (uint8_t ep_num)
{
if (ep_num == ENDP1)
EP1_IN_Callback ();
else if (ep_num == ENDP2)
{
/* INTERRUPT Transfer done */
}
#ifdef DEBUG
else if (ep_num == ENDP3)
{
chopstx_mutex_lock (&stdout.m_dev);
chopstx_cond_signal (&stdout.cond_dev);
chopstx_mutex_unlock (&stdout.m_dev);
}
#endif
#ifdef PINPAD_SUPPORT
else if (ep_num == ENDP6)
EP6_IN_Callback ();
#endif
}
/*
* ATR (Answer To Reset) string
*
@@ -1297,15 +1343,6 @@ icc_handle_timeout (struct ccid *c)
static struct ccid ccid;
enum icc_state *icc_state_p = &ccid.icc_state;
/*
* Another Tx done callback
*/
void
EP2_IN_Callback (void)
{
}
void
ccid_card_change_signal (int how)
{
@@ -1331,15 +1368,22 @@ ccid_usb_reset (void)
#define NOTIFY_SLOT_CHANGE 0x50
#define INTR_REQ_USB 20
void *
ccid_thread (void *arg)
{
chopstx_intr_t interrupt;
struct ep_in *epi = &endpoint_in;
struct ep_out *epo = &endpoint_out;
struct ccid *c = &ccid;
struct apdu *a = &apdu;
(void)arg;
usb_lld_init (USB_INITIAL_FEATURE);
chopstx_claim_irq (&interrupt, INTR_REQ_USB);
usb_interrupt_handler ();
reset:
epi_init (epi, ENDP1, notify_tx, c);
@@ -1351,8 +1395,16 @@ ccid_thread (void *arg)
while (1)
{
eventmask_t m;
uint32_t timeout = USB_ICC_TIMEOUT;
chopstx_poll_cond_t poll_desc;
m = eventflag_wait_timeout (&c->ccid_comm, USB_ICC_TIMEOUT);
eventflag_set_poll_desc (&c->ccid_comm, &poll_desc);
chopstx_poll (&timeout, 2, &interrupt, &poll_desc);
if (interrupt.ready)
usb_interrupt_handler ();
if (poll_desc.ready)
m = eventflag_wait (&c->ccid_comm);
if (m == EV_USB_RESET)
{
@@ -1457,3 +1509,57 @@ ccid_thread (void *arg)
return NULL;
}
#ifdef DEBUG
static void
stdout_init (void)
{
chopstx_mutex_init (&stdout.m);
chopstx_mutex_init (&stdout.m_dev);
chopstx_cond_init (&stdout.cond_dev);
stdout.connected = 0;
}
void
_write (const char *s, int len)
{
int packet_len;
if (len == 0)
return;
chopstx_mutex_lock (&stdout.m);
chopstx_mutex_lock (&stdout.m_dev);
if (!stdout.connected)
chopstx_cond_wait (&stdout.cond_dev, &stdout.m_dev);
chopstx_mutex_unlock (&stdout.m_dev);
do
{
packet_len =
(len < VIRTUAL_COM_PORT_DATA_SIZE) ? len : VIRTUAL_COM_PORT_DATA_SIZE;
chopstx_mutex_lock (&stdout.m_dev);
usb_lld_write (ENDP3, s, packet_len);
chopstx_cond_wait (&stdout.cond_dev, &stdout.m_dev);
chopstx_mutex_unlock (&stdout.m_dev);
s += packet_len;
len -= packet_len;
}
/* Send a Zero-Length-Packet if the last packet is full size. */
while (len != 0 || packet_len == VIRTUAL_COM_PORT_DATA_SIZE);
chopstx_mutex_unlock (&stdout.m);
}
#else
void
_write (const char *s, int size)
{
(void)s;
(void)size;
}
#endif

View File

@@ -488,68 +488,3 @@ int usb_cb_interface (uint8_t cmd, struct req_args *arg)
return USB_SUCCESS;
}
}
#define INTR_REQ_USB 20
void *
usb_intr (void *arg)
{
chopstx_intr_t interrupt;
(void)arg;
usb_lld_init (USB_INITIAL_FEATURE);
chopstx_claim_irq (&interrupt, INTR_REQ_USB);
usb_interrupt_handler ();
while (1)
{
chopstx_intr_wait (&interrupt);
/* Process interrupt. */
usb_interrupt_handler ();
}
return NULL;
}
extern void EP1_IN_Callback (void);
extern void EP2_IN_Callback (void);
extern void EP1_OUT_Callback (void);
extern void EP6_IN_Callback (void);
void
usb_cb_rx_ready (uint8_t ep_num)
{
if (ep_num == ENDP1)
EP1_OUT_Callback ();
#ifdef DEBUG
else if (ep_num == ENDP5)
{
chopstx_mutex_lock (&stdout.m_dev);
usb_lld_rx_enable (ep_num);
chopstx_mutex_unlock (&stdout.m_dev);
}
#endif
}
void
usb_cb_tx_done (uint8_t ep_num)
{
if (ep_num == ENDP1)
EP1_IN_Callback ();
else if (ep_num == ENDP2)
EP2_IN_Callback ();
#ifdef DEBUG
else if (ep_num == ENDP3)
{
chopstx_mutex_lock (&stdout.m_dev);
chopstx_cond_signal (&stdout.cond_dev);
chopstx_mutex_unlock (&stdout.m_dev);
}
#endif
#ifdef PINPAD_SUPPORT
else if (ep_num == ENDP6)
EP6_IN_Callback ();
#endif
}