From 00541d7627526e10e754909fe77c873e75c4b550 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Mon, 16 Jan 2012 12:17:45 +0900 Subject: [PATCH] clean up USB API --- ChangeLog | 21 +++++++++++++++++++++ src/stmusb.mk | 2 +- src/usb-icc.c | 31 +++++++++++++------------------ src/usb_lld.c | 1 + src/usb_lld.h | 35 +++++++++++++++++++++++++++++++++++ src/usb_prop.c | 11 +++++++---- 6 files changed, 78 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index c7f1f61..d62b31f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,24 @@ +2012-01-16 Niibe Yutaka + + Adopt new API. + * src/usb-icc.c (EP1_IN_Callback, icc_error, icc_power_on) + (icc_send_status, icc_send_data_block, icc_send_params): Use + usb_lld_write (was: USB_SIL_Write). + (EP2_OUT_Callback): Use usb_lld_get_data_len, usb_lld_rxcpy, + and usb_lld_rx_enable (was: USB_SIL_Read and SetEPRxValid). + (icc_prepare_receive): Use usb_lld_rx_enable. + + * src/stmusb.mk (STMUSBSRC): Dont' include usb_sil.c. + + * src/usb_lld.h (usb_lld_txcpy, usb_lld_tx_enable) + (usb_lld_write, usb_lld_rx_enable, usb_lld_get_data_len) + (usb_lld_rxcpy): New. + + * src/usb_prop.c (SetEPRxCount_allocated_size): Fix the + implementation. (ST's SetEPRxCount is actually meant to + setup allocated size, which is confusing). + (gnuk_device_init): Don't call USB_SIL_Init. + 2012-01-10 Niibe Yutaka * src/openpgp.c (GPGthread): Allow INS_RESET_RETRY_COUNTER and diff --git a/src/stmusb.mk b/src/stmusb.mk index 3d7c954..f0b1b5e 100644 --- a/src/stmusb.mk +++ b/src/stmusb.mk @@ -1,7 +1,7 @@ STMUSBDIR = ../STM32_USB-FS-Device_Driver STMUSBSRCDIR = $(STMUSBDIR)/src STMUSBINCDIR = $(STMUSBDIR)/inc -STMUSBSRC= $(STMUSBSRCDIR)/usb_sil.c \ +STMUSBSRC= \ $(STMUSBSRCDIR)/usb_init.c $(STMUSBSRCDIR)/usb_int.c \ $(STMUSBSRCDIR)/usb_mem.c $(STMUSBSRCDIR)/usb_core.c \ $(STMUSBSRCDIR)/usb_regs.c diff --git a/src/usb-icc.c b/src/usb-icc.c index a027bab..0f8db9c 100644 --- a/src/usb-icc.c +++ b/src/usb-icc.c @@ -29,6 +29,7 @@ #include "usb_mem.h" #include "hw_config.h" #include "usb_istr.h" +#include "usb_lld.h" #define ICC_SET_PARAMS 0x61 /* non-ICCD command */ #define ICC_POWER_ON 0x62 @@ -131,8 +132,7 @@ EP1_IN_Callback (void) { /* Send the last 0-DATA transcation of Bulk-IN in the transactions */ icc_next_p = NULL; - USB_SIL_Write (EP1_IN, icc_buffer, 0); - SetEPTxValid (ENDP1); + usb_lld_write (ENDP1, icc_buffer, 0); } else { @@ -146,8 +146,7 @@ EP1_IN_Callback (void) tx_size = &icc_buffer[icc_tx_size] - p; } - USB_SIL_Write (EP1_IN, p, tx_size); - SetEPTxValid (ENDP1); + usb_lld_write (ENDP1, p, tx_size); } } @@ -159,7 +158,7 @@ icc_prepare_receive (int chain) else icc_next_p = icc_buffer; - SetEPRxValid (ENDP2); + usb_lld_rx_enable (ENDP2); } /* @@ -173,10 +172,11 @@ EP2_OUT_Callback (void) int data_len_so_far; int data_len; - len = USB_SIL_Read (EP2_OUT, icc_next_p); + len = usb_lld_get_data_len (ENDP2); + usb_lld_rxcpy (icc_next_p, ENDP2, 0, len); if (len == 0) { /* Just ignore Zero Length Packet (ZLP), if any */ - SetEPRxValid (ENDP2); + usb_lld_rx_enable (ENDP2); return; } @@ -194,7 +194,7 @@ EP2_OUT_Callback (void) && data_len != data_len_so_far) /* The sequence of transactions continues */ { - SetEPRxValid (ENDP2); + usb_lld_rx_enable (ENDP2); if ((icc_next_p - icc_buffer) >= USB_BUF_SIZE) /* No room to receive any more */ { @@ -298,8 +298,7 @@ icc_error (int offset) icc_next_p = NULL; /* This is a single transaction Bulk-IN */ icc_tx_size = ICC_MSG_HEADER_SIZE; - USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size); - SetEPTxValid (ENDP1); + usb_lld_write (ENDP1, icc_reply, icc_tx_size); } static Thread *gpg_thread; @@ -332,8 +331,7 @@ icc_power_on (void) icc_next_p = NULL; /* This is a single transaction Bulk-IN */ icc_tx_size = ICC_MSG_HEADER_SIZE + size_atr; - USB_SIL_Write (EP1_IN, icc_buffer, icc_tx_size); - SetEPTxValid (ENDP1); + usb_lld_write (ENDP1, icc_buffer, icc_tx_size); DEBUG_INFO ("ON\r\n"); return ICC_STATE_WAIT; @@ -367,8 +365,7 @@ icc_send_status (void) icc_next_p = NULL; /* This is a single transaction Bulk-IN */ icc_tx_size = ICC_MSG_HEADER_SIZE; - USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size); - SetEPTxValid (ENDP1); + usb_lld_write (ENDP1, icc_reply, icc_tx_size); #ifdef DEBUG_MORE DEBUG_INFO ("St\r\n"); @@ -428,8 +425,7 @@ icc_send_data_block (int len, uint8_t status, uint8_t chain) else icc_next_p = p + USB_LL_BUF_SIZE; - USB_SIL_Write (EP1_IN, p, tx_size); - SetEPTxValid (ENDP1); + usb_lld_write (ENDP1, p, tx_size); #ifdef DEBUG_MORE DEBUG_INFO ("DATA\r\n"); #endif @@ -458,8 +454,7 @@ icc_send_params (void) icc_next_p = NULL; /* This is a single transaction Bulk-IN */ icc_tx_size = ICC_MSG_HEADER_SIZE + 7; - USB_SIL_Write (EP1_IN, icc_buffer, icc_tx_size); - SetEPTxValid (ENDP1); + usb_lld_write (ENDP1, icc_buffer, icc_tx_size); #ifdef DEBUG_MORE DEBUG_INFO ("DATA\r\n"); #endif diff --git a/src/usb_lld.c b/src/usb_lld.c index 9429712..97c9f8d 100644 --- a/src/usb_lld.c +++ b/src/usb_lld.c @@ -1,5 +1,6 @@ #include "ch.h" #include "hal.h" +#include "usb_lib.h" #include "usb_lld.h" extern void USB_Istr (void); diff --git a/src/usb_lld.h b/src/usb_lld.h index 9e0ecfd..58bcbda 100644 --- a/src/usb_lld.h +++ b/src/usb_lld.h @@ -1,2 +1,37 @@ #define STM32_USB_IRQ_PRIORITY 11 void usb_lld_init (void); + +extern inline void usb_lld_txcpy (const uint8_t *src, + int ep_num, int offset, size_t len) +{ + UserToPMABufferCopy ((uint8_t *)src, GetEPTxAddr (ep_num) + offset, len); +} + +extern inline void usb_lld_tx_enable (int ep_num, size_t len) +{ + SetEPTxCount (ep_num, len); + SetEPTxValid (ep_num); +} + +extern inline void usb_lld_write (uint8_t ep_num, void *buf, size_t len) +{ + UserToPMABufferCopy (buf, GetEPTxAddr (ep_num), len); + SetEPTxCount (ep_num, len); + SetEPTxValid (ep_num); +} + +extern inline void usb_lld_rx_enable (int ep_num) +{ + SetEPRxValid (ep_num); +} + +extern inline int usb_lld_get_data_len (int ep_num) +{ + return GetEPRxCount (ep_num); +} + +extern inline void usb_lld_rxcpy (uint8_t *dst, + int ep_num, int offset, size_t len) +{ + PMAToUserBufferCopy (dst, GetEPRxAddr (ep_num) + offset, len); +} diff --git a/src/usb_prop.c b/src/usb_prop.c index a0067bb..56c0cb3 100644 --- a/src/usb_prop.c +++ b/src/usb_prop.c @@ -41,17 +41,18 @@ #endif -void +static void SetEPRxCount_allocated_size (uint8_t bEpNum, uint16_t wCount) { /* Assume wCount is even */ + uint32_t *pdwReg = _pEPRxCount (bEpNum); uint16_t value; - if (wCount < 62) + if (wCount <= 62) value = (wCount & 0x3e) << 9; else value = 0x8000 | (((wCount >> 5) - 1) << 10); - SetEPRxCount (bEpNum, value); + *pdwReg = (uint32_t)value; } static void @@ -63,7 +64,9 @@ gnuk_device_init (void) PowerOn (); /* Perform basic device initialization operations */ - USB_SIL_Init (); + _SetISTR (0); + wInterrupt_Mask = IMR_MSK; + _SetCNTR (wInterrupt_Mask); bDeviceState = UNCONNECTED; }