USBDevice library with Blue Pill STM32F103C8T6 board support.

Dependents:   STM32F103C8T6_USBSerial_Demo lightweight-weather-station

Fork of USBDevice by mbed official

Committer:
Kojto
Date:
Thu Jul 20 10:14:36 2017 +0100
Revision:
70:2c525a50f1b6
Update libraries (ed9d1da)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 70:2c525a50f1b6 1 /* Copyright (c) 2016 mbed.org, MIT License
Kojto 70:2c525a50f1b6 2 *
Kojto 70:2c525a50f1b6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 70:2c525a50f1b6 4 * and associated documentation files (the "Software"), to deal in the Software without
Kojto 70:2c525a50f1b6 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Kojto 70:2c525a50f1b6 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Kojto 70:2c525a50f1b6 7 * Software is furnished to do so, subject to the following conditions:
Kojto 70:2c525a50f1b6 8 *
Kojto 70:2c525a50f1b6 9 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 70:2c525a50f1b6 10 * substantial portions of the Software.
Kojto 70:2c525a50f1b6 11 *
Kojto 70:2c525a50f1b6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 70:2c525a50f1b6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 70:2c525a50f1b6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 70:2c525a50f1b6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 70:2c525a50f1b6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 70:2c525a50f1b6 17 */
Kojto 70:2c525a50f1b6 18 #ifndef USBHAL_STM32F103RB
Kojto 70:2c525a50f1b6 19 #define USBHAL_STM32F103RB
Kojto 70:2c525a50f1b6 20
Kojto 70:2c525a50f1b6 21 #define USBHAL_IRQn USB_LP_CAN1_RX0_IRQn
Kojto 70:2c525a50f1b6 22
Kojto 70:2c525a50f1b6 23
Kojto 70:2c525a50f1b6 24 #define NB_ENDPOINT 8
Kojto 70:2c525a50f1b6 25 /* must be multiple of 4 bytes */
Kojto 70:2c525a50f1b6 26 #define MAXTRANSFER_SIZE 0x200
Kojto 70:2c525a50f1b6 27 #define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
Kojto 70:2c525a50f1b6 28 #if (FIFO_USB_RAM_SIZE > 0x500)
Kojto 70:2c525a50f1b6 29 #error "FIFO dimensioning incorrect"
Kojto 70:2c525a50f1b6 30 #endif
Kojto 70:2c525a50f1b6 31
Kojto 70:2c525a50f1b6 32 typedef struct
Kojto 70:2c525a50f1b6 33 {
Kojto 70:2c525a50f1b6 34 USBHAL *inst;
Kojto 70:2c525a50f1b6 35 void (USBHAL::*bus_reset)(void);
Kojto 70:2c525a50f1b6 36 void (USBHAL::*sof)(int frame);
Kojto 70:2c525a50f1b6 37 void (USBHAL::*connect_change)(unsigned int connected);
Kojto 70:2c525a50f1b6 38 void (USBHAL::*suspend_change)(unsigned int suspended);
Kojto 70:2c525a50f1b6 39 void (USBHAL::*ep0_setup)(void);
Kojto 70:2c525a50f1b6 40 void (USBHAL::*ep0_in)(void);
Kojto 70:2c525a50f1b6 41 void (USBHAL::*ep0_out)(void);
Kojto 70:2c525a50f1b6 42 void (USBHAL::*ep0_read)(void);
Kojto 70:2c525a50f1b6 43 bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
Kojto 70:2c525a50f1b6 44 bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
Kojto 70:2c525a50f1b6 45 uint8_t epComplete[8];
Kojto 70:2c525a50f1b6 46 /* memorize dummy buffer used for reception */
Kojto 70:2c525a50f1b6 47 uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
Kojto 70:2c525a50f1b6 48 uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
Kojto 70:2c525a50f1b6 49 gpio_t usb_switch;
Kojto 70:2c525a50f1b6 50 }USBHAL_Private_t;
Kojto 70:2c525a50f1b6 51
Kojto 70:2c525a50f1b6 52 void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
Kojto 70:2c525a50f1b6 53 {
Kojto 70:2c525a50f1b6 54 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 55 gpio_write(&(priv->usb_switch),!state);
Kojto 70:2c525a50f1b6 56 }
Kojto 70:2c525a50f1b6 57
Kojto 70:2c525a50f1b6 58 uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
Kojto 70:2c525a50f1b6 59 {
Kojto 70:2c525a50f1b6 60 return 1024;
Kojto 70:2c525a50f1b6 61 }
Kojto 70:2c525a50f1b6 62 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 63 {
Kojto 70:2c525a50f1b6 64 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 65 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 66 uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
Kojto 70:2c525a50f1b6 67 void (USBHAL::*func)(int frame) = priv->sof;
Kojto 70:2c525a50f1b6 68 (obj->*func)(sofnum);
Kojto 70:2c525a50f1b6 69 }
Kojto 70:2c525a50f1b6 70
Kojto 70:2c525a50f1b6 71 USBHAL * USBHAL::instance;
Kojto 70:2c525a50f1b6 72
Kojto 70:2c525a50f1b6 73 USBHAL::USBHAL(void)
Kojto 70:2c525a50f1b6 74 {
Kojto 70:2c525a50f1b6 75 /* init parameter */
Kojto 70:2c525a50f1b6 76 USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
Kojto 70:2c525a50f1b6 77 /* initialized all field of init including 0 field */
Kojto 70:2c525a50f1b6 78 /* constructor does not fill with zero */
Kojto 70:2c525a50f1b6 79 hpcd.Instance = USB;
Kojto 70:2c525a50f1b6 80 /* initialized all field of init including 0 field */
Kojto 70:2c525a50f1b6 81 /* constructor does not fill with zero */
Kojto 70:2c525a50f1b6 82 memset(&hpcd.Init, 0, sizeof(hpcd.Init));
Kojto 70:2c525a50f1b6 83 hpcd.Init.dev_endpoints = NB_ENDPOINT;
Kojto 70:2c525a50f1b6 84 hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
Kojto 70:2c525a50f1b6 85 hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
Kojto 70:2c525a50f1b6 86 hpcd.Init.Sof_enable = 1;
Kojto 70:2c525a50f1b6 87 hpcd.Init.speed = PCD_SPEED_FULL;
Kojto 70:2c525a50f1b6 88 /* pass instance for usage inside call back */
Kojto 70:2c525a50f1b6 89 HALPriv->inst = this;
Kojto 70:2c525a50f1b6 90 HALPriv->bus_reset = &USBHAL::busReset;
Kojto 70:2c525a50f1b6 91 HALPriv->suspend_change = &USBHAL::suspendStateChanged;
Kojto 70:2c525a50f1b6 92 HALPriv->connect_change = &USBHAL::connectStateChanged;
Kojto 70:2c525a50f1b6 93 HALPriv->sof = &USBHAL::SOF;
Kojto 70:2c525a50f1b6 94 HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
Kojto 70:2c525a50f1b6 95 HALPriv->ep_realise = &USBHAL::realiseEndpoint;
Kojto 70:2c525a50f1b6 96 HALPriv->ep0_in = &USBHAL::EP0in;
Kojto 70:2c525a50f1b6 97 HALPriv->ep0_out = &USBHAL::EP0out;
Kojto 70:2c525a50f1b6 98 HALPriv->ep0_read = &USBHAL::EP0read;
Kojto 70:2c525a50f1b6 99 hpcd.pData = (void*)HALPriv;
Kojto 70:2c525a50f1b6 100 HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
Kojto 70:2c525a50f1b6 101 HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
Kojto 70:2c525a50f1b6 102 HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
Kojto 70:2c525a50f1b6 103 HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
Kojto 70:2c525a50f1b6 104 HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
Kojto 70:2c525a50f1b6 105 HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
Kojto 70:2c525a50f1b6 106 instance = this;
Kojto 70:2c525a50f1b6 107
Kojto 70:2c525a50f1b6 108
Kojto 70:2c525a50f1b6 109 /* Configure USB VBUS GPIO */
Kojto 70:2c525a50f1b6 110 gpio_init_out(&HALPriv->usb_switch,PB_14);
Kojto 70:2c525a50f1b6 111 gpio_mode(&HALPriv->usb_switch,OpenDrain);
Kojto 70:2c525a50f1b6 112 /* Configure USB FS GPIOs */
Kojto 70:2c525a50f1b6 113
Kojto 70:2c525a50f1b6 114 /* Configure DM DP Pins
Kojto 70:2c525a50f1b6 115 * - USB-DP (D+ of the USB connector) <======> PA12 (Nucleo board)
Kojto 70:2c525a50f1b6 116 * Make sure to connect a 1.5KOhm pull up to USB-DP PA12 pin
Kojto 70:2c525a50f1b6 117 * (permanent pull-up)
Kojto 70:2c525a50f1b6 118 - USB-DM (D- of the USB connector) <======> PA11 (Nucleo board)
Kojto 70:2c525a50f1b6 119 */
Kojto 70:2c525a50f1b6 120
Kojto 70:2c525a50f1b6 121 pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT));
Kojto 70:2c525a50f1b6 122 pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT));
Kojto 70:2c525a50f1b6 123
Kojto 70:2c525a50f1b6 124 __HAL_RCC_USB_CLK_ENABLE();
Kojto 70:2c525a50f1b6 125
Kojto 70:2c525a50f1b6 126 hpcd.State = HAL_PCD_STATE_RESET;
Kojto 70:2c525a50f1b6 127
Kojto 70:2c525a50f1b6 128 HAL_PCD_Init(&hpcd);
Kojto 70:2c525a50f1b6 129 /* hardcoded size of FIFO according definition*/
Kojto 70:2c525a50f1b6 130 HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
Kojto 70:2c525a50f1b6 131 HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
Kojto 70:2c525a50f1b6 132 HAL_PCDEx_PMAConfig(&hpcd , 0x01 , PCD_SNG_BUF, 0x90);
Kojto 70:2c525a50f1b6 133 HAL_PCDEx_PMAConfig(&hpcd , 0x81 , PCD_SNG_BUF, 0xb0);
Kojto 70:2c525a50f1b6 134 #if 0
Kojto 70:2c525a50f1b6 135 HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_DBL_BUF, 0x018000b0);
Kojto 70:2c525a50f1b6 136 #else
Kojto 70:2c525a50f1b6 137 HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_SNG_BUF, 0x100);
Kojto 70:2c525a50f1b6 138 #endif
Kojto 70:2c525a50f1b6 139 HAL_PCDEx_PMAConfig(&hpcd , 0x82, PCD_SNG_BUF, 0x120);
Kojto 70:2c525a50f1b6 140
Kojto 70:2c525a50f1b6 141 NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
Kojto 70:2c525a50f1b6 142 NVIC_SetPriority( USBHAL_IRQn, 1);
Kojto 70:2c525a50f1b6 143
Kojto 70:2c525a50f1b6 144 HAL_PCD_Start(&hpcd);
Kojto 70:2c525a50f1b6 145 }
Kojto 70:2c525a50f1b6 146
Kojto 70:2c525a50f1b6 147 #endif