USBDevice library with Blue Pill STM32F103C8T6 board support.

Dependents:   STM32F103C8T6_USBSerial_Demo lightweight-weather-station

Fork of USBDevice by mbed official

Committer:
Konstantin Kochin
Date:
Fri Aug 04 11:17:37 2017 +0300
Revision:
73:da92595e61da
Added support of the BLUEPILL_F103C8 board

Added files of the BLUEPILL_F103C8 board, based on the
copy of the NUCLEO_F103RB

Who changed what in which revision?

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