├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── Utilities └── STM32F4-Discovery │ ├── MCD-ST Image SW License Agreement 19Jul2011 v0.1.pdf │ ├── Release_Notes.html │ ├── libPDMFilter_GCC.a │ ├── libPDMFilter_IAR.a │ ├── libPDMFilter_Keil.lib │ ├── pdm_filter.h │ ├── stm32f4_discovery.c │ ├── stm32f4_discovery.h │ ├── stm32f4_discovery_audio_codec.c │ ├── stm32f4_discovery_audio_codec.h │ ├── stm32f4_discovery_lis302dl.c │ └── stm32f4_discovery_lis302dl.h ├── gencodebooks.cmake ├── lora_codec2 ├── app_sx126x.c ├── main.c ├── main.h ├── microphone.c ├── radio.c └── speaker.c ├── passthru ├── microphone.c ├── passthru.c ├── passthru.h └── speaker.c ├── pdm_fir ├── LICENSE ├── README.md ├── pdm_fir.c ├── pdm_fir.h ├── pdm_fir.py └── pdm_fir_.h ├── readme.md ├── startup_stm32f4xx.s ├── stm32_flash.ld ├── stm32f4discovery.dia ├── stm32f4discovery.png ├── sx126x ├── sx126x.c └── sx12xx.h ├── sx12xx_hal ├── radio.h └── radio_sx126x.c ├── system_stm32f4xx.c ├── usb_conf ├── usb_bsp.c ├── usb_bsp.h ├── usb_conf.h ├── usbd_conf.h ├── usbd_desc.c ├── usbd_desc.h └── usbd_usr.c ├── usb_lib ├── cdc │ ├── usbd_cdc_core.c │ ├── usbd_cdc_core.h │ ├── usbd_cdc_vcp.c │ └── usbd_cdc_vcp.h ├── core │ ├── usbd_core.c │ ├── usbd_core.h │ ├── usbd_def.h │ ├── usbd_ioreq.c │ ├── usbd_ioreq.h │ ├── usbd_req.c │ ├── usbd_req.h │ └── usbd_usr.h └── otg │ ├── usb_core.c │ ├── usb_core.h │ ├── usb_dcd.c │ ├── usb_dcd.h │ ├── usb_dcd_int.c │ ├── usb_dcd_int.h │ ├── usb_defines.h │ └── usb_regs.h └── vocoder_passthru ├── main.c ├── main.h ├── microphone.c └── speaker.c /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "codec2"] 2 | path = codec2 3 | url = https://github.com/drowe67/codec2 4 | -------------------------------------------------------------------------------- /Utilities/STM32F4-Discovery/MCD-ST Image SW License Agreement 19Jul2011 v0.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dudmuck/lora_codec2/49f053f5af1716d11c90ab4cffc48112768b026a/Utilities/STM32F4-Discovery/MCD-ST Image SW License Agreement 19Jul2011 v0.1.pdf -------------------------------------------------------------------------------- /Utilities/STM32F4-Discovery/libPDMFilter_GCC.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dudmuck/lora_codec2/49f053f5af1716d11c90ab4cffc48112768b026a/Utilities/STM32F4-Discovery/libPDMFilter_GCC.a -------------------------------------------------------------------------------- /Utilities/STM32F4-Discovery/libPDMFilter_IAR.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dudmuck/lora_codec2/49f053f5af1716d11c90ab4cffc48112768b026a/Utilities/STM32F4-Discovery/libPDMFilter_IAR.a -------------------------------------------------------------------------------- /Utilities/STM32F4-Discovery/libPDMFilter_Keil.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dudmuck/lora_codec2/49f053f5af1716d11c90ab4cffc48112768b026a/Utilities/STM32F4-Discovery/libPDMFilter_Keil.lib -------------------------------------------------------------------------------- /Utilities/STM32F4-Discovery/pdm_filter.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file pdm_filter.h 4 | * @author MCD Application Team 5 | * @version V1.1.0 6 | * @date 28-October-2011 7 | * @brief Header file for PDM audio software decoding Library. 8 | * This Library is used to decode and reconstruct the audio signal 9 | * produced by MP45DT02 MEMS microphone from STMicroelectronics. 10 | * For more details about this Library, please refer to document 11 | * "PDM audio software decoding on STM32 microcontrollers (AN3998)". 12 | ****************************************************************************** 13 | * @attention 14 | * 15 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 16 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 17 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 18 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 19 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 20 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 21 | * 22 | *

© COPYRIGHT 2011 STMicroelectronics

23 | ****************************************************************************** 24 | */ 25 | 26 | /* Define to prevent recursive inclusion -------------------------------------*/ 27 | #ifndef __PDM_FILTER_H 28 | #define __PDM_FILTER_H 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /* Includes ------------------------------------------------------------------*/ 35 | #include 36 | 37 | /* Exported types ------------------------------------------------------------*/ 38 | typedef struct { 39 | uint16_t Fs; 40 | float LP_HZ; 41 | float HP_HZ; 42 | uint16_t In_MicChannels; 43 | uint16_t Out_MicChannels; 44 | char InternalFilter[34]; 45 | } PDMFilter_InitStruct; 46 | 47 | /* Exported constants --------------------------------------------------------*/ 48 | /* Exported macros -----------------------------------------------------------*/ 49 | #define HTONS(A) ((((u16)(A) & 0xff00) >> 8) | \ 50 | (((u16)(A) & 0x00ff) << 8)) 51 | 52 | /* Exported functions ------------------------------------------------------- */ 53 | void PDM_Filter_Init(PDMFilter_InitStruct * Filter); 54 | 55 | int32_t PDM_Filter_64_MSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); 56 | int32_t PDM_Filter_80_MSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); 57 | int32_t PDM_Filter_64_LSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); 58 | int32_t PDM_Filter_80_LSB(uint8_t* data, uint16_t* dataOut, uint16_t MicGain, PDMFilter_InitStruct * Filter); 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif /* __PDM_FILTER_H */ 65 | 66 | /*******************(C)COPYRIGHT 2011 STMicroelectronics *****END OF FILE******/ 67 | -------------------------------------------------------------------------------- /Utilities/STM32F4-Discovery/stm32f4_discovery.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f4_discovery.c 4 | * @author MCD Application Team 5 | * @version V1.1.0 6 | * @date 28-October-2011 7 | * @brief This file provides set of firmware functions to manage Leds and 8 | * push-button available on STM32F4-Discovery Kit from STMicroelectronics. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Includes ------------------------------------------------------------------*/ 24 | #include "stm32f4_discovery.h" 25 | 26 | /** @addtogroup Utilities 27 | * @{ 28 | */ 29 | 30 | /** @addtogroup STM32F4_DISCOVERY 31 | * @{ 32 | */ 33 | 34 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL 35 | * @brief This file provides set of firmware functions to manage Leds and push-button 36 | * available on STM32F4-Discovery Kit from STMicroelectronics. 37 | * @{ 38 | */ 39 | 40 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions 41 | * @{ 42 | */ 43 | /** 44 | * @} 45 | */ 46 | 47 | 48 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Defines 49 | * @{ 50 | */ 51 | /** 52 | * @} 53 | */ 54 | 55 | 56 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Macros 57 | * @{ 58 | */ 59 | /** 60 | * @} 61 | */ 62 | 63 | 64 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Variables 65 | * @{ 66 | */ 67 | GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT, LED3_GPIO_PORT, LED5_GPIO_PORT, 68 | LED6_GPIO_PORT}; 69 | const uint16_t GPIO_PIN[LEDn] = {LED4_PIN, LED3_PIN, LED5_PIN, 70 | LED6_PIN}; 71 | const uint32_t GPIO_CLK[LEDn] = {LED4_GPIO_CLK, LED3_GPIO_CLK, LED5_GPIO_CLK, 72 | LED6_GPIO_CLK}; 73 | 74 | GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT }; 75 | 76 | const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN }; 77 | 78 | const uint32_t BUTTON_CLK[BUTTONn] = {USER_BUTTON_GPIO_CLK }; 79 | 80 | const uint16_t BUTTON_EXTI_LINE[BUTTONn] = {USER_BUTTON_EXTI_LINE }; 81 | 82 | const uint8_t BUTTON_PORT_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PORT_SOURCE}; 83 | 84 | const uint8_t BUTTON_PIN_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PIN_SOURCE }; 85 | const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn }; 86 | 87 | NVIC_InitTypeDef NVIC_InitStructure; 88 | 89 | /** 90 | * @} 91 | */ 92 | 93 | 94 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes 95 | * @{ 96 | */ 97 | 98 | /** 99 | * @} 100 | */ 101 | 102 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Functions 103 | * @{ 104 | */ 105 | 106 | /** 107 | * @brief Configures LED GPIO. 108 | * @param Led: Specifies the Led to be configured. 109 | * This parameter can be one of following parameters: 110 | * @arg LED4 111 | * @arg LED3 112 | * @arg LED5 113 | * @arg LED6 114 | * @retval None 115 | */ 116 | void STM_EVAL_LEDInit(Led_TypeDef Led) 117 | { 118 | GPIO_InitTypeDef GPIO_InitStructure; 119 | 120 | /* Enable the GPIO_LED Clock */ 121 | RCC_AHB1PeriphClockCmd(GPIO_CLK[Led], ENABLE); 122 | 123 | /* Configure the GPIO_LED pin */ 124 | GPIO_InitStructure.GPIO_Pin = GPIO_PIN[Led]; 125 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 126 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 127 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 128 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 129 | GPIO_Init(GPIO_PORT[Led], &GPIO_InitStructure); 130 | } 131 | 132 | /** 133 | * @brief Turns selected LED On. 134 | * @param Led: Specifies the Led to be set on. 135 | * This parameter can be one of following parameters: 136 | * @arg LED4 137 | * @arg LED3 138 | * @arg LED5 139 | * @arg LED6 140 | * @retval None 141 | */ 142 | void STM_EVAL_LEDOn(Led_TypeDef Led) 143 | { 144 | GPIO_PORT[Led]->BSRRL = GPIO_PIN[Led]; 145 | } 146 | 147 | /** 148 | * @brief Turns selected LED Off. 149 | * @param Led: Specifies the Led to be set off. 150 | * This parameter can be one of following parameters: 151 | * @arg LED4 152 | * @arg LED3 153 | * @arg LED5 154 | * @arg LED6 155 | * @retval None 156 | */ 157 | void STM_EVAL_LEDOff(Led_TypeDef Led) 158 | { 159 | GPIO_PORT[Led]->BSRRH = GPIO_PIN[Led]; 160 | } 161 | 162 | /** 163 | * @brief Toggles the selected LED. 164 | * @param Led: Specifies the Led to be toggled. 165 | * This parameter can be one of following parameters: 166 | * @arg LED4 167 | * @arg LED3 168 | * @arg LED5 169 | * @arg LED6 170 | * @retval None 171 | */ 172 | void STM_EVAL_LEDToggle(Led_TypeDef Led) 173 | { 174 | GPIO_PORT[Led]->ODR ^= GPIO_PIN[Led]; 175 | } 176 | 177 | /** 178 | * @brief Configures Button GPIO and EXTI Line. 179 | * @param Button: Specifies the Button to be configured. 180 | * This parameter should be: BUTTON_USER 181 | * @param Button_Mode: Specifies Button mode. 182 | * This parameter can be one of following parameters: 183 | * @arg BUTTON_MODE_GPIO: Button will be used as simple IO 184 | * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt 185 | * generation capability 186 | * @retval None 187 | */ 188 | void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode) 189 | { 190 | GPIO_InitTypeDef GPIO_InitStructure; 191 | EXTI_InitTypeDef EXTI_InitStructure; 192 | NVIC_InitTypeDef NVIC_InitStructure; 193 | 194 | /* Enable the BUTTON Clock */ 195 | RCC_AHB1PeriphClockCmd(BUTTON_CLK[Button], ENABLE); 196 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 197 | 198 | /* Configure Button pin as input */ 199 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 200 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 201 | GPIO_InitStructure.GPIO_Pin = BUTTON_PIN[Button]; 202 | GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStructure); 203 | 204 | if (Button_Mode == BUTTON_MODE_EXTI) 205 | { 206 | /* Connect Button EXTI Line to Button GPIO Pin */ 207 | SYSCFG_EXTILineConfig(BUTTON_PORT_SOURCE[Button], BUTTON_PIN_SOURCE[Button]); 208 | 209 | /* Configure Button EXTI line */ 210 | EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[Button]; 211 | EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 212 | EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 213 | EXTI_InitStructure.EXTI_LineCmd = ENABLE; 214 | EXTI_Init(&EXTI_InitStructure); 215 | 216 | /* Enable and set Button EXTI Interrupt to the lowest priority */ 217 | NVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn[Button]; 218 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; 219 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; 220 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 221 | 222 | NVIC_Init(&NVIC_InitStructure); 223 | } 224 | } 225 | 226 | /** 227 | * @brief Returns the selected Button state. 228 | * @param Button: Specifies the Button to be checked. 229 | * This parameter should be: BUTTON_USER 230 | * @retval The Button GPIO pin value. 231 | */ 232 | uint32_t STM_EVAL_PBGetState(Button_TypeDef Button) 233 | { 234 | return GPIO_ReadInputDataBit(BUTTON_PORT[Button], BUTTON_PIN[Button]); 235 | } 236 | 237 | /** 238 | * @} 239 | */ 240 | 241 | /** 242 | * @} 243 | */ 244 | 245 | /** 246 | * @} 247 | */ 248 | 249 | /** 250 | * @} 251 | */ 252 | 253 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 254 | -------------------------------------------------------------------------------- /Utilities/STM32F4-Discovery/stm32f4_discovery.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f4_discovery.h 4 | * @author MCD Application Team 5 | * @version V1.1.0 6 | * @date 28-October-2011 7 | * @brief This file contains definitions for STM32F4-Discovery Kit's Leds and 8 | * push-button hardware resources. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F4_DISCOVERY_H 25 | #define __STM32F4_DISCOVERY_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f4xx.h" 33 | 34 | /** @addtogroup Utilities 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup STM32F4_DISCOVERY 39 | * @{ 40 | */ 41 | 42 | /** @addtogroup STM32F4_DISCOVERY_LOW_LEVEL 43 | * @{ 44 | */ 45 | 46 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Types 47 | * @{ 48 | */ 49 | typedef enum 50 | { 51 | LED4 = 0, 52 | LED3 = 1, 53 | LED5 = 2, 54 | LED6 = 3 55 | } Led_TypeDef; 56 | 57 | typedef enum 58 | { 59 | BUTTON_USER = 0, 60 | } Button_TypeDef; 61 | 62 | typedef enum 63 | { 64 | BUTTON_MODE_GPIO = 0, 65 | BUTTON_MODE_EXTI = 1 66 | } ButtonMode_TypeDef; 67 | /** 68 | * @} 69 | */ 70 | 71 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Constants 72 | * @{ 73 | */ 74 | 75 | /** @addtogroup STM32F4_DISCOVERY_LOW_LEVEL_LED 76 | * @{ 77 | */ 78 | #define LEDn 4 79 | 80 | #define LED4_PIN GPIO_Pin_12 81 | #define LED4_GPIO_PORT GPIOD 82 | #define LED4_GPIO_CLK RCC_AHB1Periph_GPIOD 83 | 84 | #define LED3_PIN GPIO_Pin_13 85 | #define LED3_GPIO_PORT GPIOD 86 | #define LED3_GPIO_CLK RCC_AHB1Periph_GPIOD 87 | 88 | #define LED5_PIN GPIO_Pin_14 89 | #define LED5_GPIO_PORT GPIOD 90 | #define LED5_GPIO_CLK RCC_AHB1Periph_GPIOD 91 | 92 | #define LED6_PIN GPIO_Pin_15 93 | #define LED6_GPIO_PORT GPIOD 94 | #define LED6_GPIO_CLK RCC_AHB1Periph_GPIOD 95 | /** 96 | * @} 97 | */ 98 | 99 | /** @addtogroup STM32F4_DISCOVERY_LOW_LEVEL_BUTTON 100 | * @{ 101 | */ 102 | #define BUTTONn 1 103 | 104 | /** 105 | * @brief Wakeup push-button 106 | */ 107 | #define USER_BUTTON_PIN GPIO_Pin_0 108 | #define USER_BUTTON_GPIO_PORT GPIOA 109 | #define USER_BUTTON_GPIO_CLK RCC_AHB1Periph_GPIOA 110 | #define USER_BUTTON_EXTI_LINE EXTI_Line0 111 | #define USER_BUTTON_EXTI_PORT_SOURCE EXTI_PortSourceGPIOA 112 | #define USER_BUTTON_EXTI_PIN_SOURCE EXTI_PinSource0 113 | #define USER_BUTTON_EXTI_IRQn EXTI0_IRQn 114 | /** 115 | * @} 116 | */ 117 | 118 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Macros 119 | * @{ 120 | */ 121 | /** 122 | * @} 123 | */ 124 | 125 | 126 | /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Functions 127 | * @{ 128 | */ 129 | void STM_EVAL_LEDInit(Led_TypeDef Led); 130 | void STM_EVAL_LEDOn(Led_TypeDef Led); 131 | void STM_EVAL_LEDOff(Led_TypeDef Led); 132 | void STM_EVAL_LEDToggle(Led_TypeDef Led); 133 | void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode); 134 | uint32_t STM_EVAL_PBGetState(Button_TypeDef Button); 135 | /** 136 | * @} 137 | */ 138 | 139 | #ifdef __cplusplus 140 | } 141 | #endif 142 | 143 | #endif /* __STM32F4_DISCOVERY_H */ 144 | /** 145 | * @} 146 | */ 147 | 148 | /** 149 | * @} 150 | */ 151 | 152 | /** 153 | * @} 154 | */ 155 | 156 | 157 | 158 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 159 | -------------------------------------------------------------------------------- /gencodebooks.cmake: -------------------------------------------------------------------------------- 1 | # 2 | # Generated sources 3 | # 4 | 5 | set(D ${CMAKE_CURRENT_SOURCE_DIR}/codec2/src/codebook) 6 | 7 | # lsp quantisers 8 | 9 | set(CODEBOOKS 10 | ${D}/lsp1.txt 11 | ${D}/lsp2.txt 12 | ${D}/lsp3.txt 13 | ${D}/lsp4.txt 14 | ${D}/lsp5.txt 15 | ${D}/lsp6.txt 16 | ${D}/lsp7.txt 17 | ${D}/lsp8.txt 18 | ${D}/lsp9.txt 19 | ${D}/lsp10.txt 20 | ) 21 | 22 | # lspd quantisers 23 | 24 | set(CODEBOOKSD 25 | ${D}/dlsp1.txt 26 | ${D}/dlsp2.txt 27 | ${D}/dlsp3.txt 28 | ${D}/dlsp4.txt 29 | ${D}/dlsp5.txt 30 | ${D}/dlsp6.txt 31 | ${D}/dlsp7.txt 32 | ${D}/dlsp8.txt 33 | ${D}/dlsp9.txt 34 | ${D}/dlsp10.txt 35 | ) 36 | 37 | set(CODEBOOKSJVM 38 | ${D}/lspjvm1.txt 39 | ${D}/lspjvm2.txt 40 | ${D}/lspjvm3.txt 41 | ) 42 | 43 | set(CODEBOOKSMEL 44 | ${D}/mel1.txt 45 | ${D}/mel2.txt 46 | ${D}/mel3.txt 47 | ${D}/mel4.txt 48 | ${D}/mel5.txt 49 | ${D}/mel6.txt 50 | ) 51 | 52 | set(CODEBOOKSLSPMELVQ 53 | ${D}/lspmelvq1.txt 54 | ${D}/lspmelvq2.txt 55 | ${D}/lspmelvq3.txt 56 | ) 57 | 58 | set(CODEBOOKSGE ${D}/gecb.txt) 59 | 60 | set(CODEBOOKSNEWAMP1 61 | ${D}/train_120_1.txt 62 | ${D}/train_120_2.txt 63 | ) 64 | 65 | set(CODEBOOKSNEWAMP1_ENERGY 66 | ${D}/newamp1_energy_q.txt 67 | ) 68 | 69 | set(CODEBOOKSNEWAMP2 70 | ${D}/codes_450.txt 71 | ) 72 | 73 | set(CODEBOOKSNEWAMP2_ENERGY 74 | ${D}/newamp2_energy_q.txt 75 | ) 76 | 77 | # when crosscompiling we need a native executable 78 | if(CMAKE_CROSSCOMPILING) 79 | include(ExternalProject) 80 | set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/codec2) 81 | message ( "____________ ${SOURCE_DIR} ___________________ " ) 82 | ExternalProject_Add(codec2_native 83 | SOURCE_DIR ${SOURCE_DIR} 84 | BINARY_DIR ${CMAKE_BINARY_DIR}/src/codec2_native 85 | CONFIGURE_COMMAND ${CMAKE_COMMAND} ${SOURCE_DIR} 86 | BUILD_COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/src/codec2_native --target generate_codebook 87 | INSTALL_COMMAND ${CMAKE_COMMAND} -E copy src/generate_codebook ${CMAKE_CURRENT_BINARY_DIR} 88 | ) 89 | add_executable(generate_codebook IMPORTED) 90 | set_target_properties(generate_codebook 91 | PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/generate_codebook) 92 | add_dependencies(generate_codebook codec2_native) 93 | else(CMAKE_CROSSCOMPILING) 94 | # Build code generator binaries. These do not get installed. 95 | # generate_codebook 96 | add_executable(generate_codebook generate_codebook.c) 97 | target_link_libraries(generate_codebook ${CMAKE_REQUIRED_LIBRARIES}) 98 | # Make native builds available for cross-compiling. 99 | export(TARGETS generate_codebook 100 | FILE ${CMAKE_BINARY_DIR}/ImportExecutables.cmake) 101 | endif(CMAKE_CROSSCOMPILING) 102 | 103 | 104 | # codebook.c 105 | add_custom_command( 106 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebook.c 107 | COMMAND generate_codebook lsp_cb ${CODEBOOKS} > ${CMAKE_CURRENT_BINARY_DIR}/codebook.c 108 | DEPENDS generate_codebook ${CODEBOOKS} 109 | ) 110 | 111 | # codebookd.c 112 | add_custom_command( 113 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebookd.c 114 | COMMAND generate_codebook lsp_cbd ${CODEBOOKSD} > ${CMAKE_CURRENT_BINARY_DIR}/codebookd.c 115 | DEPENDS generate_codebook ${CODEBOOKSD} 116 | ) 117 | 118 | # codebookjvm.c 119 | add_custom_command( 120 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebookjvm.c 121 | COMMAND generate_codebook lsp_cbjvm ${CODEBOOKSJVM} > ${CMAKE_CURRENT_BINARY_DIR}/codebookjvm.c 122 | DEPENDS generate_codebook ${CODEBOOKSJVM} 123 | ) 124 | 125 | 126 | # codebookmel.c 127 | add_custom_command( 128 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebookmel.c 129 | COMMAND generate_codebook mel_cb ${CODEBOOKSMEL} > ${CMAKE_CURRENT_BINARY_DIR}/codebookmel.c 130 | DEPENDS generate_codebook ${CODEBOOKSMEL} 131 | ) 132 | 133 | # codebooklspmelvq.c 134 | add_custom_command( 135 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebooklspmelvq.c 136 | COMMAND generate_codebook lspmelvq_cb ${CODEBOOKSLSPMELVQ} > ${CMAKE_CURRENT_BINARY_DIR}/codebooklspmelvq.c 137 | DEPENDS generate_codebook ${CODEBOOKSLSPMELVQ} 138 | ) 139 | 140 | # codebookge.c 141 | add_custom_command( 142 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebookge.c 143 | COMMAND generate_codebook ge_cb ${CODEBOOKSGE} > ${CMAKE_CURRENT_BINARY_DIR}/codebookge.c 144 | DEPENDS generate_codebook ${CODEBOOKSGE} 145 | ) 146 | 147 | # codebooknewamp1.c 148 | add_custom_command( 149 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp1.c 150 | COMMAND generate_codebook newamp1vq_cb ${CODEBOOKSNEWAMP1} > ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp1.c 151 | DEPENDS generate_codebook ${CODEBOOKSNEWAMP1} 152 | ) 153 | 154 | # codebooknewamp1_energy.c 155 | add_custom_command( 156 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp1_energy.c 157 | COMMAND generate_codebook newamp1_energy_cb ${CODEBOOKSNEWAMP1_ENERGY} > ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp1_energy.c 158 | DEPENDS generate_codebook ${CODEBOOKSNEWAMP1_ENERGY} 159 | ) 160 | 161 | # codebooknewamp2.c 162 | add_custom_command( 163 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp2.c 164 | COMMAND generate_codebook newamp2vq_cb ${CODEBOOKSNEWAMP2} > ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp2.c 165 | DEPENDS generate_codebook ${CODEBOOKSNEWAMP2} 166 | ) 167 | 168 | # codebooknewamp2_energy.c 169 | add_custom_command( 170 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp2_energy.c 171 | COMMAND generate_codebook newamp2_energy_cb ${CODEBOOKSNEWAMP2_ENERGY} > ${CMAKE_CURRENT_BINARY_DIR}/codebooknewamp2_energy.c 172 | DEPENDS generate_codebook ${CODEBOOKSNEWAMP2_ENERGY} 173 | ) 174 | 175 | -------------------------------------------------------------------------------- /lora_codec2/app_sx126x.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "radio.h" 3 | 4 | void radio_print_status() 5 | { 6 | float MHz; 7 | 8 | Radio_printOpMode(); 9 | 10 | { 11 | loraConfig0_t conf0; 12 | conf0.octet = SX126x_readReg(REG_ADDR_LORA_CONFIG0, 1); 13 | vcp_printf("bw%u sf%u ", LORA_BW_KHZ, conf0.bits.modem_sf); 14 | } 15 | MHz = SX126x_getMHz(); 16 | // float printf crashing 17 | vcp_printf("%uKHz\r\n", (unsigned)(MHz*1000)); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /lora_codec2/main.h: -------------------------------------------------------------------------------- 1 | 2 | /* Includes ------------------------------------------------------------------*/ 3 | #include 4 | #include 5 | #include "stm32f4xx.h" 6 | #include "stm32f4_discovery.h" 7 | #include "stm32f4_discovery_audio_codec.h" 8 | #include "stm32f4_discovery_lis302dl.h" 9 | #include "stm32f4xx_it.h" 10 | #include "codec2.h" 11 | 12 | #if (CODEC2_MODE == CODEC2_MODE_3200) 13 | #define LORA_PAYLOAD_LENGTH 128 14 | #define SF_AT_500KHZ 10 15 | #define INTER_PKT_TIMEOUT 40 16 | #elif (CODEC2_MODE == CODEC2_MODE_2400) 17 | #define LORA_PAYLOAD_LENGTH 96 18 | #define SF_AT_500KHZ 10 19 | #define INTER_PKT_TIMEOUT 40 20 | #elif (CODEC2_MODE == CODEC2_MODE_1600) 21 | #define LORA_PAYLOAD_LENGTH 128 22 | #define SF_AT_500KHZ 11 23 | #define INTER_PKT_TIMEOUT 40 24 | #elif (CODEC2_MODE == CODEC2_MODE_1400) 25 | #define LORA_PAYLOAD_LENGTH 112 26 | #define SF_AT_500KHZ 11 27 | #define INTER_PKT_TIMEOUT 40 28 | #elif (CODEC2_MODE == CODEC2_MODE_1300) 29 | #define LORA_PAYLOAD_LENGTH 52 /* 8 c2 frames per packet */ 30 | #define SF_AT_500KHZ 11 31 | #define INTER_PKT_TIMEOUT 40 32 | #elif (CODEC2_MODE == CODEC2_MODE_1200) 33 | #define LORA_PAYLOAD_LENGTH 96 34 | //#define LORA_PAYLOAD_LENGTH 192 /* 1281ms per packet */ 35 | #define SF_AT_500KHZ 11 36 | #define INTER_PKT_TIMEOUT 40 37 | #elif (CODEC2_MODE == CODEC2_MODE_700C) 38 | #define LORA_PAYLOAD_LENGTH 56 /* 16 c2 frames per packet */ 39 | #define SF_AT_500KHZ 12 40 | #if (LORA_BW_KHZ == 500) 41 | #error unreliable_at_500KHz 42 | #endif 43 | #define INTER_PKT_TIMEOUT 40 44 | #elif (CODEC2_MODE == CODEC2_MODE_450) /* 360 samples per frame, 18 bits per frame */ 45 | #define LORA_PAYLOAD_LENGTH 36 /* 16 c2 frames per packet */ 46 | #define SF_AT_500KHZ 12 47 | #if (LORA_BW_KHZ == 500) 48 | #error unreliable_at_500KHz 49 | #endif 50 | #define INTER_PKT_TIMEOUT 70 51 | #endif 52 | 53 | #if (LORA_BW_KHZ == 500) 54 | #define SPREADING_FACTOR SF_AT_500KHZ 55 | #elif (LORA_BW_KHZ == 250) 56 | #define SPREADING_FACTOR (SF_AT_500KHZ-1) 57 | #elif (LORA_BW_KHZ == 125) 58 | #define SPREADING_FACTOR (SF_AT_500KHZ-2) 59 | #elif (LORA_BW_KHZ == 62) 60 | #define SPREADING_FACTOR (SF_AT_500KHZ-3) 61 | #elif (LORA_BW_KHZ == 31) 62 | #define SPREADING_FACTOR (SF_AT_500KHZ-4) 63 | #endif 64 | 65 | #define USER_BUTTON GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) 66 | 67 | /* Exported types ------------------------------------------------------------*/ 68 | typedef enum { 69 | SPKR_NONE = 0, 70 | SPKR_LOWER, 71 | SPKR_UPPER 72 | } spkr_e; 73 | extern spkr_e fill_spkr; 74 | 75 | typedef enum { 76 | MIC_RDY_NONE = 0, 77 | MIC_RDY_LOWER, 78 | MIC_RDY_UPPER 79 | } mic_e; 80 | extern mic_e mic_ready; 81 | /* Exported constants --------------------------------------------------------*/ 82 | 83 | /* Exported macro ------------------------------------------------------------*/ 84 | /* Exported functions ------------------------------------------------------- */ 85 | void ColorfulRingOfDeath(void); 86 | void rxDoneCB(uint8_t size, float rssi, float snr); 87 | void lora_rx_begin(void); 88 | 89 | extern volatile unsigned _micSampCnt; 90 | extern volatile unsigned micPutCnt; 91 | extern volatile uint8_t micFrameCnt; 92 | extern float micGain; 93 | extern volatile uint8_t micOverrun; 94 | extern volatile uint8_t _waveType; 95 | extern volatile uint8_t vol; 96 | extern volatile uint32_t _ticker; // one millisecond systick 97 | extern volatile uint32_t dbg_tick_at_radio_irq; 98 | extern volatile uint32_t dbg_tick_at_send; 99 | extern volatile uint32_t call_tx_encoded_at_tick; 100 | extern volatile uint8_t to_rx_at_txdone; // flag 101 | extern volatile uint8_t _sched_tx_encoded; // flag 102 | extern volatile uint8_t need_fhss_lfsr; // flag 103 | extern volatile uint8_t tx_buf_idx; 104 | #ifdef FHSS_BASE_FREQ 105 | void fhss_set_next_channel(const char*); 106 | #endif /* FHSS_BASE_FREQ */ 107 | 108 | 109 | extern unsigned nsamp; 110 | extern unsigned nsamp_x2; 111 | void vcp_printf( const char* format, ... ); 112 | 113 | void _speaker_init(unsigned); 114 | void _microphone_init(void); 115 | #define MAX_SPKR_BUFFER_SIZE 2560 // all codec2 modes except 450pwb 116 | extern int16_t spkr_buffer[]; 117 | 118 | #define SINE_TABLE_LENGTH 1024 119 | extern const uint16_t sine_table[]; 120 | 121 | extern short mic_buf[]; 122 | 123 | /* radio.c: */ 124 | void start_radio(void); 125 | extern volatile uint8_t txing; 126 | 127 | /* app_.c: */ 128 | void radio_print_status(void); 129 | -------------------------------------------------------------------------------- /lora_codec2/microphone.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "pdm_fir.h" 4 | 5 | 6 | #define MICROPHONE_USE_DMA 7 | 8 | /* SPI Configuration defines */ 9 | #define SPI_SCK_PIN GPIO_Pin_10 10 | #define SPI_SCK_GPIO_PORT GPIOB 11 | #define SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB 12 | #define SPI_SCK_SOURCE GPIO_PinSource10 13 | #define SPI_SCK_AF GPIO_AF_SPI2 14 | 15 | #define SPI_MOSI_PIN GPIO_Pin_3 16 | #define SPI_MOSI_GPIO_PORT GPIOC 17 | #define SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOC 18 | #define SPI_MOSI_SOURCE GPIO_PinSource3 19 | #define SPI_MOSI_AF GPIO_AF_SPI2 20 | 21 | #define MICROPHONE_I2S SPI2 22 | 23 | #ifdef MICROPHONE_USE_DMA 24 | /* I2S DMA Stream definitions */ 25 | #define MICROPHONE_I2S_DMA_CLOCK RCC_AHB1Periph_DMA1 26 | #define MICROPHONE_I2S_DMA_DREG (SPI2_BASE + 0x0c) /* SPI2_DR &SPI2->DR */ 27 | 28 | #define MICROPHONE_I2S_DMA_RX_CHANNEL DMA_Channel_0 29 | #define MICROPHONE_I2S_DMA_RX_REQ SPI_I2S_DMAReq_Rx 30 | #define MICROPHONE_I2S_DMA_RX_STREAM DMA1_Stream3 31 | #define MICROPHONE_I2S_DMA_RX_IRQ DMA1_Stream3_IRQn 32 | #define MICROPHONE_I2S_DMA_RX_FLAG_TC DMA_FLAG_TCIF3 33 | #define MICROPHONE_I2S_DMA_RX_FLAG_HT DMA_FLAG_HTIF3 34 | #define MICROPHONE_I2S_DMA_RX_FLAG_FE DMA_FLAG_FEIF3 35 | #define MICROPHONE_I2S_DMA_RX_FLAG_TE DMA_FLAG_TEIF3 36 | #define MICROPHONE_I2S_DMA_RX_FLAG_DME DMA_FLAG_DMEIF3 37 | #define Mic_DMA_I2S_RX_IRQHandler DMA1_Stream3_IRQHandler 38 | #else 39 | #define AUDIO_REC_SPI_IRQHANDLER SPI2_IRQHandler 40 | #endif 41 | 42 | 43 | static uint32_t AudioRecInited = 0; 44 | struct pdm_fir_filter filter; 45 | __IO uint32_t Data_Status =0; 46 | 47 | volatile unsigned mic_buf_idx; 48 | short mic_buf[1280]; 49 | mic_e mic_ready; 50 | volatile uint8_t micOverrun; 51 | 52 | /** 53 | * @brief Initialize GPIO for wave recorder. 54 | * @param None 55 | * @retval None 56 | */ 57 | static void WaveRecorder_GPIO_Init(void) 58 | { 59 | GPIO_InitTypeDef GPIO_InitStructure; 60 | 61 | /* Enable GPIO clocks */ 62 | RCC_AHB1PeriphClockCmd(SPI_SCK_GPIO_CLK | SPI_MOSI_GPIO_CLK, ENABLE); 63 | 64 | /* Enable GPIO clocks */ 65 | RCC_AHB1PeriphClockCmd(SPI_SCK_GPIO_CLK | SPI_MOSI_GPIO_CLK, ENABLE); 66 | 67 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 68 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 69 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 70 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 71 | 72 | /* SPI SCK pin configuration */ 73 | GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN; 74 | GPIO_Init(SPI_SCK_GPIO_PORT, &GPIO_InitStructure); 75 | 76 | /* Connect SPI pins to AF5 */ 77 | GPIO_PinAFConfig(SPI_SCK_GPIO_PORT, SPI_SCK_SOURCE, SPI_SCK_AF); 78 | 79 | /* SPI MOSI pin configuration */ 80 | GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN; 81 | GPIO_Init(SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); 82 | GPIO_PinAFConfig(SPI_MOSI_GPIO_PORT, SPI_MOSI_SOURCE, SPI_MOSI_AF); 83 | } 84 | 85 | #ifdef MICROPHONE_USE_DMA 86 | #define DMA_BUF_LEN 32 87 | uint16_t dma_buf[DMA_BUF_LEN]; 88 | #endif /* !MICROPHONE_USE_DMA */ 89 | 90 | /** 91 | * @brief Initialize SPI peripheral. 92 | * @param Freq :Audio frequency 93 | * @retval None 94 | */ 95 | static void WaveRecorder_SPI_Init(uint32_t Freq) 96 | { 97 | #ifdef MICROPHONE_USE_DMA 98 | DMA_InitTypeDef DMA_InitStructure; 99 | #endif /* MICROPHONE_USE_DMA */ 100 | I2S_InitTypeDef I2S_InitStructure; 101 | 102 | /* Enable the SPI clock */ 103 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); 104 | 105 | /* SPI configuration */ 106 | SPI_I2S_DeInit(MICROPHONE_I2S); 107 | I2S_InitStructure.I2S_AudioFreq = Freq; 108 | I2S_InitStructure.I2S_Standard = I2S_Standard_LSB; 109 | I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b; 110 | I2S_InitStructure.I2S_CPOL = I2S_CPOL_High; 111 | I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx; 112 | I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable; 113 | /* Initialize the I2S peripheral with the structure above */ 114 | I2S_Init(MICROPHONE_I2S, &I2S_InitStructure); 115 | 116 | #ifdef MICROPHONE_USE_DMA 117 | RCC_AHB1PeriphClockCmd(MICROPHONE_I2S_DMA_CLOCK, ENABLE); 118 | 119 | /* Configure the DMA Stream */ 120 | DMA_Cmd(AUDIO_I2S_DMA_STREAM, DISABLE); 121 | DMA_DeInit(AUDIO_I2S_DMA_STREAM); 122 | 123 | DMA_InitStructure.DMA_BufferSize = DMA_BUF_LEN; 124 | DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; 125 | DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull; 126 | DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; 127 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; 128 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 129 | DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 130 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) (&(MICROPHONE_I2S->DR)); 131 | DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 132 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 133 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; 134 | DMA_InitStructure.DMA_Priority = DMA_Priority_High; 135 | 136 | DMA_InitStructure.DMA_Channel = MICROPHONE_I2S_DMA_RX_CHANNEL; 137 | DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)dma_buf; 138 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 139 | DMA_Init(MICROPHONE_I2S_DMA_RX_STREAM, &DMA_InitStructure); 140 | 141 | DMA_ITConfig(MICROPHONE_I2S_DMA_RX_STREAM, DMA_IT_TC, ENABLE); 142 | DMA_ITConfig(MICROPHONE_I2S_DMA_RX_STREAM, DMA_IT_HT, ENABLE); 143 | 144 | SPI_I2S_DMACmd(MICROPHONE_I2S, MICROPHONE_I2S_DMA_RX_REQ, ENABLE); 145 | 146 | #else 147 | /* Enable the Rx buffer not empty interrupt */ 148 | SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE); 149 | #endif 150 | } 151 | 152 | /** 153 | * @brief Initialize the NVIC. 154 | * @param None 155 | * @retval None 156 | */ 157 | static void WaveRecorder_NVIC_Init(void) 158 | { 159 | NVIC_InitTypeDef NVIC_InitStructure; 160 | 161 | #ifdef MICROPHONE_USE_DMA 162 | NVIC_InitStructure.NVIC_IRQChannel = MICROPHONE_I2S_DMA_RX_IRQ; 163 | #else 164 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 165 | /* Configure the SPI interrupt priority */ 166 | NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn; 167 | #endif 168 | 169 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 170 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 171 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 172 | NVIC_Init(&NVIC_InitStructure); 173 | } 174 | 175 | /** 176 | * @brief Initialize wave recording 177 | * @param AudioFreq: Sampling frequency 178 | * BitRes: Audio recording Samples format (from 8 to 16 bits) 179 | * ChnlNbr: Number of input microphone channel 180 | * @retval None 181 | */ 182 | uint32_t WaveRecorderInit(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr) 183 | { 184 | /* Check if the interface is already initialized */ 185 | if (AudioRecInited) 186 | { 187 | /* No need for initialization */ 188 | return 0; 189 | } 190 | else 191 | { 192 | /* Enable CRC module */ 193 | RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN; 194 | 195 | pdm_fir_flt_init(&filter); 196 | 197 | /* Configure the GPIOs */ 198 | WaveRecorder_GPIO_Init(); 199 | 200 | /* Configure the interrupts (for timer) */ 201 | WaveRecorder_NVIC_Init(); 202 | 203 | /* Configure the SPI */ 204 | WaveRecorder_SPI_Init(AudioFreq); 205 | 206 | /* Set state of the audio recorder to initialized */ 207 | AudioRecInited = 1; 208 | 209 | /* Return 0 if all operations are OK */ 210 | return 0; 211 | } 212 | } 213 | 214 | 215 | 216 | /** 217 | * @brief Start audio recording 218 | * @param pbuf: pointer to a buffer 219 | * size: Buffer size 220 | * @retval None 221 | */ 222 | uint8_t WaveRecorderStart(/*uint16_t* pbuf*/) 223 | { 224 | /* Check if the interface has already been initialized */ 225 | if (AudioRecInited) 226 | { 227 | #ifndef MICROPHONE_USE_DMA 228 | /* Enable the Rx buffer not empty interrupt */ 229 | SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE); 230 | /* The Data transfer is performed in the SPI interrupt routine */ 231 | /* Enable the SPI peripheral */ 232 | #else 233 | DMA_Cmd(MICROPHONE_I2S_DMA_RX_STREAM, ENABLE); 234 | #endif 235 | I2S_Cmd(SPI2, ENABLE); 236 | 237 | /* Return 0 if all operations are OK */ 238 | return 0; 239 | } 240 | else 241 | { 242 | /* Cannot perform operation */ 243 | return 1; 244 | } 245 | } 246 | 247 | void _microphone_init() 248 | { 249 | mic_ready = MIC_RDY_NONE; 250 | WaveRecorderInit(64000, 16, 1); 251 | 252 | WaveRecorderStart(/*audio_buffer*/); 253 | } 254 | 255 | #ifdef MICROPHONE_USE_DMA 256 | void Mic_DMA_I2S_RX_IRQHandler() 257 | { 258 | unsigned n, put = 0; 259 | 260 | /* Transfer complete interrupt */ 261 | if (DMA_GetFlagStatus(MICROPHONE_I2S_DMA_RX_STREAM, MICROPHONE_I2S_DMA_RX_FLAG_TC) != RESET) 262 | { 263 | GPIO_ToggleBits(GPIOA, GPIO_Pin_2); 264 | for (n = 0; n < 16; n++) 265 | pdm_fir_flt_put(&filter, dma_buf[n]); 266 | 267 | put = 1; 268 | DMA_ClearFlag(MICROPHONE_I2S_DMA_RX_STREAM, MICROPHONE_I2S_DMA_RX_FLAG_TC); 269 | } 270 | 271 | /* Half Transfer complete interrupt */ 272 | if (DMA_GetFlagStatus(MICROPHONE_I2S_DMA_RX_STREAM, MICROPHONE_I2S_DMA_RX_FLAG_HT) != RESET) 273 | { 274 | GPIO_ToggleBits(GPIOA, GPIO_Pin_3); 275 | for (n = 16; n < 32; n++) 276 | pdm_fir_flt_put(&filter, dma_buf[n]); 277 | 278 | put = 1; 279 | DMA_ClearFlag(MICROPHONE_I2S_DMA_RX_STREAM, MICROPHONE_I2S_DMA_RX_FLAG_HT); 280 | } 281 | 282 | if (put) { 283 | short sample = pdm_fir_flt_get(&filter, 16); 284 | short out = sample * micGain; 285 | micPutCnt++; 286 | 287 | if (mic_ready != MIC_RDY_NONE) { 288 | micOverrun = 1; 289 | } 290 | 291 | mic_buf[mic_buf_idx++] = out; 292 | if (mic_buf_idx == nsamp) { 293 | mic_ready = MIC_RDY_LOWER; 294 | micFrameCnt++; 295 | } else if (mic_buf_idx == nsamp_x2) { 296 | mic_ready = MIC_RDY_UPPER; 297 | mic_buf_idx = 0; 298 | micFrameCnt++; 299 | } 300 | } 301 | } 302 | #endif /* MICROPHONE_USE_DMA */ 303 | 304 | volatile unsigned _micSampCnt; 305 | volatile unsigned micPutCnt; 306 | volatile uint8_t micFrameCnt; // number of codec2 unencoded samples 307 | float micGain; 308 | 309 | volatile unsigned mic_abi = 0; 310 | 311 | #ifdef MICROPHONE_USE_DMA 312 | #endif /* MICROPHONE_USE_DMA */ 313 | 314 | #ifndef MICROPHONE_USE_DMA 315 | /** 316 | * @brief This function handles AUDIO_REC_SPI global interrupt request. 317 | * @param None 318 | * @retval None 319 | */ 320 | void AUDIO_REC_SPI_IRQHANDLER(void) 321 | { 322 | u16 app; 323 | static uint8_t cnt = 0; 324 | 325 | //GPIO_SetBits(GPIOA, GPIO_Pin_3); 326 | /* Check if data are available in SPI Data register */ 327 | if (SPI_GetITStatus(SPI2, SPI_I2S_IT_RXNE) != RESET) { 328 | app = SPI_I2S_ReceiveData(SPI2); 329 | 330 | pdm_fir_flt_put(&filter, app); 331 | if (cnt++ == 15) { 332 | short sample = pdm_fir_flt_get(&filter, 16); 333 | cnt = 0; 334 | short out = sample * micGain; 335 | audio_buffer[mic_abi++] = out; // left 336 | audio_buffer[mic_abi++] = out; // right 337 | micPutCnt += 2; 338 | 339 | if (mic_abi >= AUDIO_BUFFER_SIZE) 340 | mic_abi = 0; 341 | 342 | _micSampCnt++; 343 | GPIO_ToggleBits(GPIOA, GPIO_Pin_2); 344 | } 345 | 346 | } 347 | GPIO_ToggleBits(GPIOA, GPIO_Pin_3); 348 | } 349 | #endif /* !MICROPHONE_USE_DMA */ 350 | 351 | -------------------------------------------------------------------------------- /lora_codec2/radio.c: -------------------------------------------------------------------------------- 1 | /* abstracted radio interface: works with any radio chip */ 2 | #include "main.h" 3 | #include "radio.h" 4 | #include "codec2.h" 5 | 6 | #define TX_DBM 20 7 | #define CF_HZ 917600000 8 | 9 | volatile uint8_t txing; 10 | 11 | void radio_irq_callback() 12 | { 13 | dbg_tick_at_radio_irq = _ticker; 14 | } 15 | 16 | void txDoneCB() 17 | { 18 | txing = 0; 19 | vcp_printf(" txDone_Dur:%u,%u,%u ", dbg_tick_at_radio_irq - dbg_tick_at_send, to_rx_at_txdone, _sched_tx_encoded); 20 | 21 | if (need_fhss_lfsr) { 22 | Radio_SetLoRaSymbolTimeout(24); 23 | Radio_Rx(999); 24 | } else if (to_rx_at_txdone) { 25 | vcp_printf("toRx "); 26 | lora_rx_begin(); 27 | to_rx_at_txdone = 0; 28 | } else if (_sched_tx_encoded) { 29 | call_tx_encoded_at_tick = dbg_tick_at_radio_irq + 10; 30 | vcp_printf("schedIn:%d_forTxLen:%u ", call_tx_encoded_at_tick - _ticker, tx_buf_idx); 31 | to_rx_at_txdone = 1; 32 | _sched_tx_encoded = 0; 33 | } 34 | #ifdef FHSS_BASE_FREQ 35 | else { 36 | fhss_set_next_channel("txing" ); // hopping on transmitter side during voice transmission 37 | } 38 | #endif /* FHSS_BASE_FREQ */ 39 | } 40 | 41 | void rxTimeoutCB() 42 | { 43 | vcp_printf("rxTimeout\r\n"); 44 | #ifdef FHSS_BASE_FREQ 45 | /* only occurrs upon unanswered fhss request */ 46 | if (USER_BUTTON) { 47 | fhss_set_next_channel("rxTimeout"); // LFSR request timeout 48 | Radio_Send(0); 49 | } 50 | #endif /* FHSS_BASE_FREQ */ 51 | } 52 | 53 | const RadioEvents_t rev = { 54 | /* DioPin_top_half */ radio_irq_callback, 55 | /* TxDone_topHalf */ NULL, 56 | /* TxDone_botHalf */ txDoneCB, 57 | /* TxTimeout */ NULL, 58 | /* RxDone */ rxDoneCB, 59 | /* RxTimeout */ rxTimeoutCB, 60 | /* RxError */ NULL, 61 | /* FhssChangeChannel */NULL, 62 | /* CadDone */ NULL 63 | }; 64 | 65 | void start_radio() 66 | { 67 | Radio_Init(&rev); 68 | 69 | Radio_Standby(); 70 | Radio_LoRaModemConfig(LORA_BW_KHZ, SPREADING_FACTOR, 1); 71 | Radio_SetChannel(CF_HZ); 72 | 73 | Radio_set_tx_dbm(TX_DBM); 74 | 75 | // preambleLen, fixLen, crcOn, invIQ 76 | Radio_LoRaPacketConfig(8, false, false, false); // crcOff 77 | } 78 | 79 | -------------------------------------------------------------------------------- /lora_codec2/speaker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | //#define SAMPLE_RATE_DEBUG 6 | 7 | #define SPEAKER_VOLUME 95 // headphones create noise on negative edge if > 75 8 | 9 | const uint16_t sine_table[SINE_TABLE_LENGTH] = 10 | { 11 | 0x8000,0x80c9,0x8192,0x825b,0x8324,0x83ee,0x84b7,0x8580, 12 | 0x8649,0x8712,0x87db,0x88a4,0x896c,0x8a35,0x8afe,0x8bc6, 13 | 0x8c8e,0x8d57,0x8e1f,0x8ee7,0x8fae,0x9076,0x913e,0x9205, 14 | 0x92cc,0x9393,0x945a,0x9521,0x95e7,0x96ad,0x9773,0x9839, 15 | 0x98fe,0x99c4,0x9a89,0x9b4d,0x9c12,0x9cd6,0x9d9a,0x9e5e, 16 | 0x9f21,0x9fe4,0xa0a7,0xa169,0xa22b,0xa2ed,0xa3af,0xa470, 17 | 0xa530,0xa5f1,0xa6b1,0xa770,0xa830,0xa8ef,0xa9ad,0xaa6b, 18 | 0xab29,0xabe6,0xaca3,0xad5f,0xae1b,0xaed7,0xaf92,0xb04d, 19 | 0xb107,0xb1c0,0xb27a,0xb332,0xb3ea,0xb4a2,0xb559,0xb610, 20 | 0xb6c6,0xb77c,0xb831,0xb8e5,0xb999,0xba4d,0xbb00,0xbbb2, 21 | 0xbc64,0xbd15,0xbdc6,0xbe76,0xbf25,0xbfd4,0xc082,0xc12f, 22 | 0xc1dc,0xc288,0xc334,0xc3df,0xc489,0xc533,0xc5dc,0xc684, 23 | 0xc72c,0xc7d3,0xc879,0xc91f,0xc9c3,0xca67,0xcb0b,0xcbae, 24 | 0xcc4f,0xccf1,0xcd91,0xce31,0xced0,0xcf6e,0xd00b,0xd0a8, 25 | 0xd144,0xd1df,0xd279,0xd313,0xd3ac,0xd443,0xd4db,0xd571, 26 | 0xd606,0xd69b,0xd72f,0xd7c2,0xd854,0xd8e5,0xd975,0xda05, 27 | 0xda93,0xdb21,0xdbae,0xdc3a,0xdcc5,0xdd4f,0xddd9,0xde61, 28 | 0xdee9,0xdf6f,0xdff5,0xe07a,0xe0fd,0xe180,0xe202,0xe283, 29 | 0xe303,0xe382,0xe400,0xe47d,0xe4fa,0xe575,0xe5ef,0xe668, 30 | 0xe6e0,0xe758,0xe7ce,0xe843,0xe8b7,0xe92b,0xe99d,0xea0e, 31 | 0xea7e,0xeaed,0xeb5b,0xebc8,0xec34,0xec9f,0xed09,0xed72, 32 | 0xedda,0xee41,0xeea7,0xef0b,0xef6f,0xefd1,0xf033,0xf093, 33 | 0xf0f2,0xf150,0xf1ad,0xf209,0xf264,0xf2be,0xf316,0xf36e, 34 | 0xf3c4,0xf41a,0xf46e,0xf4c1,0xf513,0xf564,0xf5b3,0xf602, 35 | 0xf64f,0xf69b,0xf6e6,0xf730,0xf779,0xf7c1,0xf807,0xf84d, 36 | 0xf891,0xf8d4,0xf916,0xf956,0xf996,0xf9d4,0xfa11,0xfa4d, 37 | 0xfa88,0xfac1,0xfafa,0xfb31,0xfb67,0xfb9c,0xfbd0,0xfc02, 38 | 0xfc33,0xfc63,0xfc92,0xfcc0,0xfcec,0xfd17,0xfd42,0xfd6a, 39 | 0xfd92,0xfdb8,0xfdde,0xfe01,0xfe24,0xfe46,0xfe66,0xfe85, 40 | 0xfea3,0xfec0,0xfedb,0xfef5,0xff0e,0xff26,0xff3c,0xff52, 41 | 0xff66,0xff79,0xff8a,0xff9b,0xffaa,0xffb8,0xffc4,0xffd0, 42 | 0xffda,0xffe3,0xffeb,0xfff1,0xfff6,0xfffa,0xfffd,0xffff, 43 | 0xffff,0xfffe,0xfffc,0xfff8,0xfff4,0xffee,0xffe7,0xffdf, 44 | 0xffd5,0xffca,0xffbe,0xffb1,0xffa2,0xff93,0xff82,0xff6f, 45 | 0xff5c,0xff47,0xff31,0xff1a,0xff02,0xfee8,0xfece,0xfeb1, 46 | 0xfe94,0xfe76,0xfe56,0xfe35,0xfe13,0xfdf0,0xfdcb,0xfda5, 47 | 0xfd7e,0xfd56,0xfd2d,0xfd02,0xfcd6,0xfca9,0xfc7b,0xfc4b, 48 | 0xfc1b,0xfbe9,0xfbb6,0xfb82,0xfb4c,0xfb16,0xfade,0xfaa5, 49 | 0xfa6b,0xfa2f,0xf9f3,0xf9b5,0xf976,0xf936,0xf8f5,0xf8b2, 50 | 0xf86f,0xf82a,0xf7e4,0xf79d,0xf755,0xf70c,0xf6c1,0xf675, 51 | 0xf629,0xf5db,0xf58c,0xf53b,0xf4ea,0xf498,0xf444,0xf3ef, 52 | 0xf399,0xf342,0xf2ea,0xf291,0xf237,0xf1db,0xf17f,0xf121, 53 | 0xf0c3,0xf063,0xf002,0xefa0,0xef3d,0xeed9,0xee74,0xee0e, 54 | 0xeda6,0xed3e,0xecd5,0xec6a,0xebff,0xeb92,0xeb24,0xeab6, 55 | 0xea46,0xe9d6,0xe964,0xe8f1,0xe87d,0xe809,0xe793,0xe71c, 56 | 0xe6a4,0xe62c,0xe5b2,0xe537,0xe4bc,0xe43f,0xe3c1,0xe343, 57 | 0xe2c3,0xe243,0xe1c1,0xe13f,0xe0bc,0xe037,0xdfb2,0xdf2c, 58 | 0xdea5,0xde1d,0xdd94,0xdd0a,0xdc80,0xdbf4,0xdb68,0xdada, 59 | 0xda4c,0xd9bd,0xd92d,0xd89c,0xd80b,0xd778,0xd6e5,0xd651, 60 | 0xd5bc,0xd526,0xd48f,0xd3f8,0xd35f,0xd2c6,0xd22c,0xd192, 61 | 0xd0f6,0xd05a,0xcfbd,0xcf1f,0xce80,0xcde1,0xcd41,0xcca0, 62 | 0xcbff,0xcb5c,0xcab9,0xca16,0xc971,0xc8cc,0xc826,0xc77f, 63 | 0xc6d8,0xc630,0xc588,0xc4de,0xc434,0xc38a,0xc2de,0xc232, 64 | 0xc186,0xc0d9,0xc02b,0xbf7c,0xbecd,0xbe1e,0xbd6d,0xbcbd, 65 | 0xbc0b,0xbb59,0xbaa6,0xb9f3,0xb940,0xb88b,0xb7d6,0xb721, 66 | 0xb66b,0xb5b5,0xb4fe,0xb446,0xb38e,0xb2d6,0xb21d,0xb164, 67 | 0xb0aa,0xafef,0xaf34,0xae79,0xadbd,0xad01,0xac45,0xab88, 68 | 0xaaca,0xaa0c,0xa94e,0xa88f,0xa7d0,0xa711,0xa651,0xa591, 69 | 0xa4d0,0xa40f,0xa34e,0xa28c,0xa1ca,0xa108,0xa045,0x9f83, 70 | 0x9ebf,0x9dfc,0x9d38,0x9c74,0x9bb0,0x9aeb,0x9a26,0x9961, 71 | 0x989c,0x97d6,0x9710,0x964a,0x9584,0x94bd,0x93f7,0x9330, 72 | 0x9269,0x91a1,0x90da,0x9012,0x8f4b,0x8e83,0x8dbb,0x8cf3, 73 | 0x8c2a,0x8b62,0x8a99,0x89d1,0x8908,0x883f,0x8776,0x86ad, 74 | 0x85e4,0x851b,0x8452,0x8389,0x82c0,0x81f7,0x812d,0x8064, 75 | 0x7f9b,0x7ed2,0x7e08,0x7d3f,0x7c76,0x7bad,0x7ae4,0x7a1b, 76 | 0x7952,0x7889,0x77c0,0x76f7,0x762e,0x7566,0x749d,0x73d5, 77 | 0x730c,0x7244,0x717c,0x70b4,0x6fed,0x6f25,0x6e5e,0x6d96, 78 | 0x6ccf,0x6c08,0x6b42,0x6a7b,0x69b5,0x68ef,0x6829,0x6763, 79 | 0x669e,0x65d9,0x6514,0x644f,0x638b,0x62c7,0x6203,0x6140, 80 | 0x607c,0x5fba,0x5ef7,0x5e35,0x5d73,0x5cb1,0x5bf0,0x5b2f, 81 | 0x5a6e,0x59ae,0x58ee,0x582f,0x5770,0x56b1,0x55f3,0x5535, 82 | 0x5477,0x53ba,0x52fe,0x5242,0x5186,0x50cb,0x5010,0x4f55, 83 | 0x4e9b,0x4de2,0x4d29,0x4c71,0x4bb9,0x4b01,0x4a4a,0x4994, 84 | 0x48de,0x4829,0x4774,0x46bf,0x460c,0x4559,0x44a6,0x43f4, 85 | 0x4342,0x4292,0x41e1,0x4132,0x4083,0x3fd4,0x3f26,0x3e79, 86 | 0x3dcd,0x3d21,0x3c75,0x3bcb,0x3b21,0x3a77,0x39cf,0x3927, 87 | 0x3880,0x37d9,0x3733,0x368e,0x35e9,0x3546,0x34a3,0x3400, 88 | 0x335f,0x32be,0x321e,0x317f,0x30e0,0x3042,0x2fa5,0x2f09, 89 | 0x2e6d,0x2dd3,0x2d39,0x2ca0,0x2c07,0x2b70,0x2ad9,0x2a43, 90 | 0x29ae,0x291a,0x2887,0x27f4,0x2763,0x26d2,0x2642,0x25b3, 91 | 0x2525,0x2497,0x240b,0x237f,0x22f5,0x226b,0x21e2,0x215a, 92 | 0x20d3,0x204d,0x1fc8,0x1f43,0x1ec0,0x1e3e,0x1dbc,0x1d3c, 93 | 0x1cbc,0x1c3e,0x1bc0,0x1b43,0x1ac8,0x1a4d,0x19d3,0x195b, 94 | 0x18e3,0x186c,0x17f6,0x1782,0x170e,0x169b,0x1629,0x15b9, 95 | 0x1549,0x14db,0x146d,0x1400,0x1395,0x132a,0x12c1,0x1259, 96 | 0x11f1,0x118b,0x1126,0x10c2,0x105f,0xffd,0xf9c,0xf3c, 97 | 0xede,0xe80,0xe24,0xdc8,0xd6e,0xd15,0xcbd,0xc66, 98 | 0xc10,0xbbb,0xb67,0xb15,0xac4,0xa73,0xa24,0x9d6, 99 | 0x98a,0x93e,0x8f3,0x8aa,0x862,0x81b,0x7d5,0x790, 100 | 0x74d,0x70a,0x6c9,0x689,0x64a,0x60c,0x5d0,0x594, 101 | 0x55a,0x521,0x4e9,0x4b3,0x47d,0x449,0x416,0x3e4, 102 | 0x3b4,0x384,0x356,0x329,0x2fd,0x2d2,0x2a9,0x281, 103 | 0x25a,0x234,0x20f,0x1ec,0x1ca,0x1a9,0x189,0x16b, 104 | 0x14e,0x131,0x117,0xfd,0xe5,0xce,0xb8,0xa3, 105 | 0x90,0x7d,0x6c,0x5d,0x4e,0x41,0x35,0x2a, 106 | 0x20,0x18,0x11,0xb,0x7,0x3,0x1,0x0, 107 | 0x0,0x2,0x5,0x9,0xe,0x14,0x1c,0x25, 108 | 0x2f,0x3b,0x47,0x55,0x64,0x75,0x86,0x99, 109 | 0xad,0xc3,0xd9,0xf1,0x10a,0x124,0x13f,0x15c, 110 | 0x17a,0x199,0x1b9,0x1db,0x1fe,0x221,0x247,0x26d, 111 | 0x295,0x2bd,0x2e8,0x313,0x33f,0x36d,0x39c,0x3cc, 112 | 0x3fd,0x42f,0x463,0x498,0x4ce,0x505,0x53e,0x577, 113 | 0x5b2,0x5ee,0x62b,0x669,0x6a9,0x6e9,0x72b,0x76e, 114 | 0x7b2,0x7f8,0x83e,0x886,0x8cf,0x919,0x964,0x9b0, 115 | 0x9fd,0xa4c,0xa9b,0xaec,0xb3e,0xb91,0xbe5,0xc3b, 116 | 0xc91,0xce9,0xd41,0xd9b,0xdf6,0xe52,0xeaf,0xf0d, 117 | 0xf6c,0xfcc,0x102e,0x1090,0x10f4,0x1158,0x11be,0x1225, 118 | 0x128d,0x12f6,0x1360,0x13cb,0x1437,0x14a4,0x1512,0x1581, 119 | 0x15f1,0x1662,0x16d4,0x1748,0x17bc,0x1831,0x18a7,0x191f, 120 | 0x1997,0x1a10,0x1a8a,0x1b05,0x1b82,0x1bff,0x1c7d,0x1cfc, 121 | 0x1d7c,0x1dfd,0x1e7f,0x1f02,0x1f85,0x200a,0x2090,0x2116, 122 | 0x219e,0x2226,0x22b0,0x233a,0x23c5,0x2451,0x24de,0x256c, 123 | 0x25fa,0x268a,0x271a,0x27ab,0x283d,0x28d0,0x2964,0x29f9, 124 | 0x2a8e,0x2b24,0x2bbc,0x2c53,0x2cec,0x2d86,0x2e20,0x2ebb, 125 | 0x2f57,0x2ff4,0x3091,0x312f,0x31ce,0x326e,0x330e,0x33b0, 126 | 0x3451,0x34f4,0x3598,0x363c,0x36e0,0x3786,0x382c,0x38d3, 127 | 0x397b,0x3a23,0x3acc,0x3b76,0x3c20,0x3ccb,0x3d77,0x3e23, 128 | 0x3ed0,0x3f7d,0x402b,0x40da,0x4189,0x4239,0x42ea,0x439b, 129 | 0x444d,0x44ff,0x45b2,0x4666,0x471a,0x47ce,0x4883,0x4939, 130 | 0x49ef,0x4aa6,0x4b5d,0x4c15,0x4ccd,0x4d85,0x4e3f,0x4ef8, 131 | 0x4fb2,0x506d,0x5128,0x51e4,0x52a0,0x535c,0x5419,0x54d6, 132 | 0x5594,0x5652,0x5710,0x57cf,0x588f,0x594e,0x5a0e,0x5acf, 133 | 0x5b8f,0x5c50,0x5d12,0x5dd4,0x5e96,0x5f58,0x601b,0x60de, 134 | 0x61a1,0x6265,0x6329,0x63ed,0x64b2,0x6576,0x663b,0x6701, 135 | 0x67c6,0x688c,0x6952,0x6a18,0x6ade,0x6ba5,0x6c6c,0x6d33, 136 | 0x6dfa,0x6ec1,0x6f89,0x7051,0x7118,0x71e0,0x72a8,0x7371, 137 | 0x7439,0x7501,0x75ca,0x7693,0x775b,0x7824,0x78ed,0x79b6, 138 | 0x7a7f,0x7b48,0x7c11,0x7cdb,0x7da4,0x7e6d,0x7f36,0x8000 139 | }; 140 | 141 | int16_t spkr_buffer[MAX_SPKR_BUFFER_SIZE]; 142 | spkr_e fill_spkr; 143 | 144 | void _speaker_init(unsigned nsamp_x4) 145 | { 146 | /* Initialize I2S interface */ 147 | EVAL_AUDIO_SetAudioInterface(AUDIO_INTERFACE_I2S); 148 | 149 | vol = SPEAKER_VOLUME; 150 | /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */ 151 | EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, vol, 8000); 152 | 153 | fill_spkr = SPKR_NONE; 154 | EVAL_AUDIO_Play((uint16_t *)spkr_buffer, nsamp_x4 * 8); // Only stereo is supported*/ 155 | 156 | vcp_printf("spkrInit\r\n"); 157 | } 158 | 159 | #define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) 160 | #define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) 161 | 162 | volatile uint8_t rshift; 163 | volatile unsigned skip; 164 | volatile uint8_t _waveType; 165 | 166 | void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size) 167 | { 168 | /* upper half of buffer was just completed */ 169 | GPIO_ToggleBits(GPIOC, GPIO_Pin_1); 170 | fill_spkr = SPKR_UPPER; // will be playing lower half: software to fill upper half 171 | } 172 | 173 | void EVAL_AUDIO_HalfTransfer_CallBack(uint32_t pBuffer, uint32_t Size) 174 | { 175 | /* lower half of buffer was just completed */ 176 | GPIO_ToggleBits(GPIOC, GPIO_Pin_2); 177 | fill_spkr = SPKR_LOWER; // will be playing upper half: software to fill lower half 178 | } 179 | 180 | void EVAL_AUDIO_Error_CallBack(void* pData) 181 | { 182 | ColorfulRingOfDeath(); 183 | } 184 | 185 | uint16_t EVAL_AUDIO_GetSampleCallBack(void) 186 | { 187 | return 0; 188 | } 189 | -------------------------------------------------------------------------------- /passthru/passthru.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "passthru.h" 4 | #include "usbd_cdc_vcp.h" 5 | #include "usbd_usr.h" 6 | #include "usbd_desc.h" 7 | #include "usb_dcd_int.h" 8 | 9 | unsigned audio_buffer_idx; 10 | uint16_t audio_buffer[AUDIO_BUFFER_SIZE]; 11 | 12 | volatile uint32_t ticker, downTicker; 13 | 14 | volatile uint8_t usb_connected; 15 | volatile uint8_t usb_suspended; 16 | 17 | volatile uint8_t left_enable; 18 | volatile uint8_t right_enable; 19 | 20 | /* 21 | * The USB data must be 4 byte aligned if DMA is enabled. This macro handles 22 | * the alignment, if necessary (it's actually magic, but don't tell anyone). 23 | */ 24 | __ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END; 25 | 26 | 27 | char str[512]; 28 | 29 | void vcp_printf( const char* format, ... ) 30 | { 31 | va_list arglist; 32 | int n; 33 | 34 | va_start( arglist, format ); 35 | n = vsnprintf(str, sizeof(str), format, arglist); 36 | va_end( arglist ); 37 | 38 | VCP_send_buffer((uint8_t*)str, n); 39 | } 40 | 41 | void SysTick_Handler(void) 42 | { 43 | ticker++; 44 | if (downTicker > 0) 45 | { 46 | downTicker--; 47 | } 48 | } 49 | 50 | /* 51 | * Call this to indicate a failure. Blinks the STM32F4 discovery LEDs 52 | * in sequence. At 168Mhz, the blinking will be very fast - about 5 Hz. 53 | * Keep that in mind when debugging, knowing the clock speed might help 54 | * with debugging. 55 | */ 56 | void ColorfulRingOfDeath(void) 57 | { 58 | uint16_t ring = 1; 59 | while (1) 60 | { 61 | uint32_t count = 0; 62 | while (count++ < 500000); 63 | 64 | GPIOD->BSRRH = (ring << 12); 65 | ring = ring << 1; 66 | if (ring >= 1<<4) 67 | { 68 | ring = 1; 69 | } 70 | GPIOD->BSRRL = (ring << 12); 71 | } 72 | } 73 | 74 | void init() 75 | { 76 | GPIO_InitTypeDef GPIO_InitStructure; 77 | /* STM32F4 discovery LEDs */ 78 | GPIO_InitTypeDef LED_Config; 79 | 80 | /* Always remember to turn on the peripheral clock... If not, you may be up till 3am debugging... */ 81 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 82 | LED_Config.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15; 83 | LED_Config.GPIO_Mode = GPIO_Mode_OUT; 84 | LED_Config.GPIO_OType = GPIO_OType_PP; 85 | LED_Config.GPIO_Speed = GPIO_Speed_25MHz; 86 | LED_Config.GPIO_PuPd = GPIO_PuPd_NOPULL; 87 | GPIO_Init(GPIOD, &LED_Config); 88 | 89 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 90 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 91 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 92 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 93 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; // audio out debug pins 94 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; 95 | GPIO_Init(GPIOC, &GPIO_InitStructure); 96 | 97 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 98 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2; // microphone debug pins 99 | GPIO_Init(GPIOA, &GPIO_InitStructure); 100 | 101 | 102 | /* Setup SysTick or CROD! */ 103 | if (SysTick_Config(SystemCoreClock / 1000)) 104 | { 105 | ColorfulRingOfDeath(); 106 | } 107 | 108 | /* Setup USB */ 109 | USBD_Init(&USB_OTG_dev, 110 | USB_OTG_FS_CORE_ID, 111 | &USR_desc, 112 | &USBD_CDC_cb, 113 | &USR_cb); 114 | 115 | left_enable = 1; 116 | right_enable = 1; 117 | 118 | return; 119 | } 120 | 121 | volatile uint8_t _waveType; 122 | volatile uint8_t vol; 123 | 124 | void set_test_pattern() 125 | { 126 | unsigned n, i; 127 | for (n = 0; n < AUDIO_BUFFER_SIZE; n++) { 128 | audio_buffer[n] = 0; 129 | } 130 | 131 | n = (AUDIO_BUFFER_SIZE / 2) - 1; 132 | for (i = 0; i < 32; i++) { 133 | audio_buffer[n--] = -8192; 134 | } 135 | 136 | n = AUDIO_BUFFER_SIZE - 1; 137 | for (i = 0; i < 32; i++) { 138 | audio_buffer[n--] = 8192; 139 | } 140 | } 141 | 142 | /** 143 | * @brief Main program. 144 | * @param None 145 | * @retval None 146 | */ 147 | int main(void) 148 | { 149 | /* Set up the system clocks */ 150 | SystemInit(); 151 | 152 | /* Initialize USB, IO, SysTick, and all those other things you do in the morning */ 153 | init(); 154 | 155 | _microphone_init(); 156 | _speaker_init(); 157 | 158 | _waveType = 3; // 3: microphone audio 159 | set_test_pattern(); 160 | 161 | micGain = 5.5; 162 | 163 | while (1) 164 | { 165 | /* Blink the orange LED at 1Hz */ 166 | if (500 == ticker) 167 | { 168 | GPIOD->BSRRH = GPIO_Pin_13; 169 | } 170 | else if (1000 == ticker) 171 | { 172 | ticker = 0; 173 | GPIOD->BSRRL = GPIO_Pin_13; 174 | } 175 | 176 | 177 | /* If there's data on the virtual serial port: 178 | * - Echo it back 179 | * - Turn the green LED on for 10ms 180 | */ 181 | uint8_t theByte; 182 | if (VCP_get_char(&theByte)) 183 | { 184 | VCP_put_char(theByte); 185 | 186 | 187 | GPIOD->BSRRL = GPIO_Pin_12; 188 | downTicker = 10; 189 | 190 | if (theByte == '.') { 191 | vcp_printf("micSampCnt:%u spkr en:%u,%u\r\n", micSampCnt, left_enable, right_enable); 192 | vcp_printf("_waveType:%u\r\n", _waveType); 193 | } 194 | else if (theByte == '?') { 195 | vcp_printf("'R': switch audio modes\r\n"); 196 | vcp_printf("'q' 'w': right-shift down, up\r\n"); 197 | vcp_printf("'a' 's': sine tone down, up\r\n"); 198 | vcp_printf("'o' 'p': mic gain down, up\r\n"); 199 | vcp_printf("'z' 'x': speaker volume down, up\r\n"); 200 | } else if (theByte == 'q' || theByte == 'w') { 201 | if (theByte == 'q' && rshift > 0) { 202 | rshift--; 203 | } 204 | if (theByte == 'w' && rshift < 15) { 205 | rshift++; 206 | } 207 | vcp_printf("rshift %u\r\n", rshift); 208 | } else if (theByte == 'a' || theByte == 's') { 209 | if (theByte == 'a' && skip > 0) { 210 | skip--; 211 | } 212 | if (theByte == 's' && skip < 1023) { 213 | skip++; 214 | } 215 | vcp_printf("skip %u\r\n", skip); 216 | } else if (theByte == 'o' || theByte == 'p') { 217 | if (theByte == 'o' && micGain > 0.1) { 218 | micGain -= 0.1; 219 | } else if (theByte == 'p' && micGain < 10.0) { 220 | micGain += 0.1; 221 | } 222 | vcp_printf("micGain %f\r\n", (double)micGain); 223 | } else if (theByte == 'z' || theByte == 'x') { 224 | if (theByte == 'z' && vol > 0) { 225 | vol--; 226 | } 227 | if (theByte == 'x' && vol < 255) { 228 | vol++; 229 | } 230 | vcp_printf("vol %u\r\n", vol); 231 | EVAL_AUDIO_VolumeCtl(vol); 232 | } else if (theByte == 'l') { 233 | left_enable ^= 1; 234 | vcp_printf("left_enable:%u\r\n", left_enable); 235 | } else if (theByte == 'r') { 236 | right_enable ^= 1; 237 | vcp_printf("right_enable:%u\r\n", right_enable); 238 | } else if (theByte == 'R') { 239 | if (_waveType == 0) { 240 | _waveType = 1; 241 | vcp_printf("sine\r\n"); 242 | } else if (_waveType == 1) { 243 | _waveType = 2; 244 | vcp_printf("ramp\r\n"); 245 | } else if (_waveType == 2) { 246 | _waveType = 3; 247 | vcp_printf("mic\r\n"); 248 | } else if (_waveType == 3) { 249 | _waveType = 0; 250 | vcp_printf("test\r\n"); 251 | set_test_pattern(); 252 | } 253 | } 254 | } 255 | if (0 == downTicker) 256 | { 257 | GPIOD->BSRRH = GPIO_Pin_12; 258 | } 259 | 260 | 261 | } // ..while (1) 262 | } 263 | 264 | uint32_t Codec_TIMEOUT_UserCallback(void) 265 | { 266 | return 0; 267 | } 268 | 269 | 270 | void OTG_FS_IRQHandler(void) 271 | { 272 | USBD_OTG_ISR_Handler (&USB_OTG_dev); 273 | } 274 | 275 | void OTG_FS_WKUP_IRQHandler(void) 276 | { 277 | if(USB_OTG_dev.cfg.low_power) 278 | { 279 | *(uint32_t *)(0xE000ED10) &= 0xFFFFFFF9 ; 280 | SystemInit(); 281 | USB_OTG_UngateClock(&USB_OTG_dev); 282 | } 283 | EXTI_ClearITPendingBit(EXTI_Line18); 284 | } 285 | 286 | struct CODEC2 *c2; 287 | void c2_check_mode(struct CODEC2 *c2, uint8_t x) { } 288 | volatile uint8_t ___; 289 | -------------------------------------------------------------------------------- /passthru/passthru.h: -------------------------------------------------------------------------------- 1 | 2 | /* Define to prevent recursive inclusion -------------------------------------*/ 3 | #ifndef __MAIN_H 4 | #define __MAIN_H 5 | 6 | /* Includes ------------------------------------------------------------------*/ 7 | #include 8 | #include 9 | #include "stm32f4xx.h" 10 | #include "stm32f4_discovery.h" 11 | #include "stm32f4_discovery_audio_codec.h" 12 | #include "stm32f4_discovery_lis302dl.h" 13 | #include "stm32f4xx_it.h" 14 | 15 | /* Exported types ------------------------------------------------------------*/ 16 | /* Exported constants --------------------------------------------------------*/ 17 | 18 | /* Exported macro ------------------------------------------------------------*/ 19 | /* Exported functions ------------------------------------------------------- */ 20 | void TimingDelay_Decrement(void); 21 | void Delay(__IO uint32_t nTime); 22 | 23 | #endif /* __MAIN_H */ 24 | 25 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 26 | 27 | void _microphone_init(void); 28 | void _speaker_init(void); 29 | void ColorfulRingOfDeath(void); 30 | void vcp_printf( const char* format, ... ); 31 | extern volatile unsigned micSampCnt; 32 | extern volatile unsigned micPutCnt; 33 | 34 | //#define AUDIO_BUFFER_SIZE 2048 35 | //#define AUDIO_BUFFER_SIZE 512 36 | #define AUDIO_BUFFER_SIZE 256 37 | //#define AUDIO_BUFFER_SIZE 64 38 | //#define AUDIO_BUFFER_SIZE 4 39 | extern uint16_t audio_buffer[]; 40 | extern unsigned audio_buffer_idx; 41 | 42 | extern volatile uint8_t rshift; 43 | extern volatile unsigned skip; 44 | extern volatile uint8_t _waveType; 45 | extern volatile uint8_t vol; 46 | extern volatile unsigned mic_abi; 47 | extern float micGain; 48 | 49 | extern volatile uint8_t left_enable; 50 | extern volatile uint8_t right_enable; 51 | 52 | extern float rc; 53 | -------------------------------------------------------------------------------- /passthru/speaker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "passthru.h" 3 | 4 | //#define SAMPLE_RATE_DEBUG 5 | 6 | #define SPEAKER_VOLUME 75 // headphones create noise on negative edge if > 75 7 | 8 | #define SINE_TABLE_LENGTH 1024 9 | 10 | const uint16_t sine_table[SINE_TABLE_LENGTH] = 11 | { 12 | 0x8000,0x80c9,0x8192,0x825b,0x8324,0x83ee,0x84b7,0x8580, 13 | 0x8649,0x8712,0x87db,0x88a4,0x896c,0x8a35,0x8afe,0x8bc6, 14 | 0x8c8e,0x8d57,0x8e1f,0x8ee7,0x8fae,0x9076,0x913e,0x9205, 15 | 0x92cc,0x9393,0x945a,0x9521,0x95e7,0x96ad,0x9773,0x9839, 16 | 0x98fe,0x99c4,0x9a89,0x9b4d,0x9c12,0x9cd6,0x9d9a,0x9e5e, 17 | 0x9f21,0x9fe4,0xa0a7,0xa169,0xa22b,0xa2ed,0xa3af,0xa470, 18 | 0xa530,0xa5f1,0xa6b1,0xa770,0xa830,0xa8ef,0xa9ad,0xaa6b, 19 | 0xab29,0xabe6,0xaca3,0xad5f,0xae1b,0xaed7,0xaf92,0xb04d, 20 | 0xb107,0xb1c0,0xb27a,0xb332,0xb3ea,0xb4a2,0xb559,0xb610, 21 | 0xb6c6,0xb77c,0xb831,0xb8e5,0xb999,0xba4d,0xbb00,0xbbb2, 22 | 0xbc64,0xbd15,0xbdc6,0xbe76,0xbf25,0xbfd4,0xc082,0xc12f, 23 | 0xc1dc,0xc288,0xc334,0xc3df,0xc489,0xc533,0xc5dc,0xc684, 24 | 0xc72c,0xc7d3,0xc879,0xc91f,0xc9c3,0xca67,0xcb0b,0xcbae, 25 | 0xcc4f,0xccf1,0xcd91,0xce31,0xced0,0xcf6e,0xd00b,0xd0a8, 26 | 0xd144,0xd1df,0xd279,0xd313,0xd3ac,0xd443,0xd4db,0xd571, 27 | 0xd606,0xd69b,0xd72f,0xd7c2,0xd854,0xd8e5,0xd975,0xda05, 28 | 0xda93,0xdb21,0xdbae,0xdc3a,0xdcc5,0xdd4f,0xddd9,0xde61, 29 | 0xdee9,0xdf6f,0xdff5,0xe07a,0xe0fd,0xe180,0xe202,0xe283, 30 | 0xe303,0xe382,0xe400,0xe47d,0xe4fa,0xe575,0xe5ef,0xe668, 31 | 0xe6e0,0xe758,0xe7ce,0xe843,0xe8b7,0xe92b,0xe99d,0xea0e, 32 | 0xea7e,0xeaed,0xeb5b,0xebc8,0xec34,0xec9f,0xed09,0xed72, 33 | 0xedda,0xee41,0xeea7,0xef0b,0xef6f,0xefd1,0xf033,0xf093, 34 | 0xf0f2,0xf150,0xf1ad,0xf209,0xf264,0xf2be,0xf316,0xf36e, 35 | 0xf3c4,0xf41a,0xf46e,0xf4c1,0xf513,0xf564,0xf5b3,0xf602, 36 | 0xf64f,0xf69b,0xf6e6,0xf730,0xf779,0xf7c1,0xf807,0xf84d, 37 | 0xf891,0xf8d4,0xf916,0xf956,0xf996,0xf9d4,0xfa11,0xfa4d, 38 | 0xfa88,0xfac1,0xfafa,0xfb31,0xfb67,0xfb9c,0xfbd0,0xfc02, 39 | 0xfc33,0xfc63,0xfc92,0xfcc0,0xfcec,0xfd17,0xfd42,0xfd6a, 40 | 0xfd92,0xfdb8,0xfdde,0xfe01,0xfe24,0xfe46,0xfe66,0xfe85, 41 | 0xfea3,0xfec0,0xfedb,0xfef5,0xff0e,0xff26,0xff3c,0xff52, 42 | 0xff66,0xff79,0xff8a,0xff9b,0xffaa,0xffb8,0xffc4,0xffd0, 43 | 0xffda,0xffe3,0xffeb,0xfff1,0xfff6,0xfffa,0xfffd,0xffff, 44 | 0xffff,0xfffe,0xfffc,0xfff8,0xfff4,0xffee,0xffe7,0xffdf, 45 | 0xffd5,0xffca,0xffbe,0xffb1,0xffa2,0xff93,0xff82,0xff6f, 46 | 0xff5c,0xff47,0xff31,0xff1a,0xff02,0xfee8,0xfece,0xfeb1, 47 | 0xfe94,0xfe76,0xfe56,0xfe35,0xfe13,0xfdf0,0xfdcb,0xfda5, 48 | 0xfd7e,0xfd56,0xfd2d,0xfd02,0xfcd6,0xfca9,0xfc7b,0xfc4b, 49 | 0xfc1b,0xfbe9,0xfbb6,0xfb82,0xfb4c,0xfb16,0xfade,0xfaa5, 50 | 0xfa6b,0xfa2f,0xf9f3,0xf9b5,0xf976,0xf936,0xf8f5,0xf8b2, 51 | 0xf86f,0xf82a,0xf7e4,0xf79d,0xf755,0xf70c,0xf6c1,0xf675, 52 | 0xf629,0xf5db,0xf58c,0xf53b,0xf4ea,0xf498,0xf444,0xf3ef, 53 | 0xf399,0xf342,0xf2ea,0xf291,0xf237,0xf1db,0xf17f,0xf121, 54 | 0xf0c3,0xf063,0xf002,0xefa0,0xef3d,0xeed9,0xee74,0xee0e, 55 | 0xeda6,0xed3e,0xecd5,0xec6a,0xebff,0xeb92,0xeb24,0xeab6, 56 | 0xea46,0xe9d6,0xe964,0xe8f1,0xe87d,0xe809,0xe793,0xe71c, 57 | 0xe6a4,0xe62c,0xe5b2,0xe537,0xe4bc,0xe43f,0xe3c1,0xe343, 58 | 0xe2c3,0xe243,0xe1c1,0xe13f,0xe0bc,0xe037,0xdfb2,0xdf2c, 59 | 0xdea5,0xde1d,0xdd94,0xdd0a,0xdc80,0xdbf4,0xdb68,0xdada, 60 | 0xda4c,0xd9bd,0xd92d,0xd89c,0xd80b,0xd778,0xd6e5,0xd651, 61 | 0xd5bc,0xd526,0xd48f,0xd3f8,0xd35f,0xd2c6,0xd22c,0xd192, 62 | 0xd0f6,0xd05a,0xcfbd,0xcf1f,0xce80,0xcde1,0xcd41,0xcca0, 63 | 0xcbff,0xcb5c,0xcab9,0xca16,0xc971,0xc8cc,0xc826,0xc77f, 64 | 0xc6d8,0xc630,0xc588,0xc4de,0xc434,0xc38a,0xc2de,0xc232, 65 | 0xc186,0xc0d9,0xc02b,0xbf7c,0xbecd,0xbe1e,0xbd6d,0xbcbd, 66 | 0xbc0b,0xbb59,0xbaa6,0xb9f3,0xb940,0xb88b,0xb7d6,0xb721, 67 | 0xb66b,0xb5b5,0xb4fe,0xb446,0xb38e,0xb2d6,0xb21d,0xb164, 68 | 0xb0aa,0xafef,0xaf34,0xae79,0xadbd,0xad01,0xac45,0xab88, 69 | 0xaaca,0xaa0c,0xa94e,0xa88f,0xa7d0,0xa711,0xa651,0xa591, 70 | 0xa4d0,0xa40f,0xa34e,0xa28c,0xa1ca,0xa108,0xa045,0x9f83, 71 | 0x9ebf,0x9dfc,0x9d38,0x9c74,0x9bb0,0x9aeb,0x9a26,0x9961, 72 | 0x989c,0x97d6,0x9710,0x964a,0x9584,0x94bd,0x93f7,0x9330, 73 | 0x9269,0x91a1,0x90da,0x9012,0x8f4b,0x8e83,0x8dbb,0x8cf3, 74 | 0x8c2a,0x8b62,0x8a99,0x89d1,0x8908,0x883f,0x8776,0x86ad, 75 | 0x85e4,0x851b,0x8452,0x8389,0x82c0,0x81f7,0x812d,0x8064, 76 | 0x7f9b,0x7ed2,0x7e08,0x7d3f,0x7c76,0x7bad,0x7ae4,0x7a1b, 77 | 0x7952,0x7889,0x77c0,0x76f7,0x762e,0x7566,0x749d,0x73d5, 78 | 0x730c,0x7244,0x717c,0x70b4,0x6fed,0x6f25,0x6e5e,0x6d96, 79 | 0x6ccf,0x6c08,0x6b42,0x6a7b,0x69b5,0x68ef,0x6829,0x6763, 80 | 0x669e,0x65d9,0x6514,0x644f,0x638b,0x62c7,0x6203,0x6140, 81 | 0x607c,0x5fba,0x5ef7,0x5e35,0x5d73,0x5cb1,0x5bf0,0x5b2f, 82 | 0x5a6e,0x59ae,0x58ee,0x582f,0x5770,0x56b1,0x55f3,0x5535, 83 | 0x5477,0x53ba,0x52fe,0x5242,0x5186,0x50cb,0x5010,0x4f55, 84 | 0x4e9b,0x4de2,0x4d29,0x4c71,0x4bb9,0x4b01,0x4a4a,0x4994, 85 | 0x48de,0x4829,0x4774,0x46bf,0x460c,0x4559,0x44a6,0x43f4, 86 | 0x4342,0x4292,0x41e1,0x4132,0x4083,0x3fd4,0x3f26,0x3e79, 87 | 0x3dcd,0x3d21,0x3c75,0x3bcb,0x3b21,0x3a77,0x39cf,0x3927, 88 | 0x3880,0x37d9,0x3733,0x368e,0x35e9,0x3546,0x34a3,0x3400, 89 | 0x335f,0x32be,0x321e,0x317f,0x30e0,0x3042,0x2fa5,0x2f09, 90 | 0x2e6d,0x2dd3,0x2d39,0x2ca0,0x2c07,0x2b70,0x2ad9,0x2a43, 91 | 0x29ae,0x291a,0x2887,0x27f4,0x2763,0x26d2,0x2642,0x25b3, 92 | 0x2525,0x2497,0x240b,0x237f,0x22f5,0x226b,0x21e2,0x215a, 93 | 0x20d3,0x204d,0x1fc8,0x1f43,0x1ec0,0x1e3e,0x1dbc,0x1d3c, 94 | 0x1cbc,0x1c3e,0x1bc0,0x1b43,0x1ac8,0x1a4d,0x19d3,0x195b, 95 | 0x18e3,0x186c,0x17f6,0x1782,0x170e,0x169b,0x1629,0x15b9, 96 | 0x1549,0x14db,0x146d,0x1400,0x1395,0x132a,0x12c1,0x1259, 97 | 0x11f1,0x118b,0x1126,0x10c2,0x105f,0xffd,0xf9c,0xf3c, 98 | 0xede,0xe80,0xe24,0xdc8,0xd6e,0xd15,0xcbd,0xc66, 99 | 0xc10,0xbbb,0xb67,0xb15,0xac4,0xa73,0xa24,0x9d6, 100 | 0x98a,0x93e,0x8f3,0x8aa,0x862,0x81b,0x7d5,0x790, 101 | 0x74d,0x70a,0x6c9,0x689,0x64a,0x60c,0x5d0,0x594, 102 | 0x55a,0x521,0x4e9,0x4b3,0x47d,0x449,0x416,0x3e4, 103 | 0x3b4,0x384,0x356,0x329,0x2fd,0x2d2,0x2a9,0x281, 104 | 0x25a,0x234,0x20f,0x1ec,0x1ca,0x1a9,0x189,0x16b, 105 | 0x14e,0x131,0x117,0xfd,0xe5,0xce,0xb8,0xa3, 106 | 0x90,0x7d,0x6c,0x5d,0x4e,0x41,0x35,0x2a, 107 | 0x20,0x18,0x11,0xb,0x7,0x3,0x1,0x0, 108 | 0x0,0x2,0x5,0x9,0xe,0x14,0x1c,0x25, 109 | 0x2f,0x3b,0x47,0x55,0x64,0x75,0x86,0x99, 110 | 0xad,0xc3,0xd9,0xf1,0x10a,0x124,0x13f,0x15c, 111 | 0x17a,0x199,0x1b9,0x1db,0x1fe,0x221,0x247,0x26d, 112 | 0x295,0x2bd,0x2e8,0x313,0x33f,0x36d,0x39c,0x3cc, 113 | 0x3fd,0x42f,0x463,0x498,0x4ce,0x505,0x53e,0x577, 114 | 0x5b2,0x5ee,0x62b,0x669,0x6a9,0x6e9,0x72b,0x76e, 115 | 0x7b2,0x7f8,0x83e,0x886,0x8cf,0x919,0x964,0x9b0, 116 | 0x9fd,0xa4c,0xa9b,0xaec,0xb3e,0xb91,0xbe5,0xc3b, 117 | 0xc91,0xce9,0xd41,0xd9b,0xdf6,0xe52,0xeaf,0xf0d, 118 | 0xf6c,0xfcc,0x102e,0x1090,0x10f4,0x1158,0x11be,0x1225, 119 | 0x128d,0x12f6,0x1360,0x13cb,0x1437,0x14a4,0x1512,0x1581, 120 | 0x15f1,0x1662,0x16d4,0x1748,0x17bc,0x1831,0x18a7,0x191f, 121 | 0x1997,0x1a10,0x1a8a,0x1b05,0x1b82,0x1bff,0x1c7d,0x1cfc, 122 | 0x1d7c,0x1dfd,0x1e7f,0x1f02,0x1f85,0x200a,0x2090,0x2116, 123 | 0x219e,0x2226,0x22b0,0x233a,0x23c5,0x2451,0x24de,0x256c, 124 | 0x25fa,0x268a,0x271a,0x27ab,0x283d,0x28d0,0x2964,0x29f9, 125 | 0x2a8e,0x2b24,0x2bbc,0x2c53,0x2cec,0x2d86,0x2e20,0x2ebb, 126 | 0x2f57,0x2ff4,0x3091,0x312f,0x31ce,0x326e,0x330e,0x33b0, 127 | 0x3451,0x34f4,0x3598,0x363c,0x36e0,0x3786,0x382c,0x38d3, 128 | 0x397b,0x3a23,0x3acc,0x3b76,0x3c20,0x3ccb,0x3d77,0x3e23, 129 | 0x3ed0,0x3f7d,0x402b,0x40da,0x4189,0x4239,0x42ea,0x439b, 130 | 0x444d,0x44ff,0x45b2,0x4666,0x471a,0x47ce,0x4883,0x4939, 131 | 0x49ef,0x4aa6,0x4b5d,0x4c15,0x4ccd,0x4d85,0x4e3f,0x4ef8, 132 | 0x4fb2,0x506d,0x5128,0x51e4,0x52a0,0x535c,0x5419,0x54d6, 133 | 0x5594,0x5652,0x5710,0x57cf,0x588f,0x594e,0x5a0e,0x5acf, 134 | 0x5b8f,0x5c50,0x5d12,0x5dd4,0x5e96,0x5f58,0x601b,0x60de, 135 | 0x61a1,0x6265,0x6329,0x63ed,0x64b2,0x6576,0x663b,0x6701, 136 | 0x67c6,0x688c,0x6952,0x6a18,0x6ade,0x6ba5,0x6c6c,0x6d33, 137 | 0x6dfa,0x6ec1,0x6f89,0x7051,0x7118,0x71e0,0x72a8,0x7371, 138 | 0x7439,0x7501,0x75ca,0x7693,0x775b,0x7824,0x78ed,0x79b6, 139 | 0x7a7f,0x7b48,0x7c11,0x7cdb,0x7da4,0x7e6d,0x7f36,0x8000 140 | }; 141 | 142 | 143 | void _speaker_init() 144 | { 145 | /* Initialize I2S interface */ 146 | EVAL_AUDIO_SetAudioInterface(AUDIO_INTERFACE_I2S); 147 | 148 | vol = SPEAKER_VOLUME; 149 | /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */ 150 | //EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, SPEAKER_VOLUME, 32000); 151 | //EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, vol, 4000); 152 | EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, vol, 8000); 153 | 154 | EVAL_AUDIO_Play((uint16_t *)audio_buffer, AUDIO_BUFFER_SIZE * 8); // Only stereo is supported*/ 155 | 156 | vcp_printf("spkrInit\r\n"); 157 | } 158 | 159 | #define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) 160 | #define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) 161 | 162 | volatile uint8_t rshift; 163 | volatile unsigned skip; 164 | volatile uint8_t _waveType; 165 | 166 | void ramp_gen(unsigned *np) 167 | { 168 | static unsigned short ramp = 0; 169 | short sample; 170 | unsigned n = *np; 171 | 172 | sample = (ramp >> rshift) - 0x8000; 173 | ramp += (skip+1); 174 | 175 | audio_buffer[n++] = sample; // left 176 | audio_buffer[n++] = sample; // right 177 | 178 | *np = n; 179 | } 180 | 181 | void sine_gen(unsigned *np) 182 | { 183 | short sample; 184 | static unsigned table_idx = 0; 185 | unsigned n = *np; 186 | 187 | sample = (sine_table[table_idx] >> rshift) - 0x8000; 188 | if (++table_idx == SINE_TABLE_LENGTH) 189 | table_idx = 0; 190 | 191 | if (skip > 0) { 192 | unsigned foo; 193 | for (foo = 0; foo < skip; foo++) { 194 | if (++table_idx == SINE_TABLE_LENGTH) 195 | table_idx = 0; 196 | } 197 | } 198 | 199 | audio_buffer[n++] = sample; // left 200 | audio_buffer[n++] = sample; // right 201 | 202 | *np = n; 203 | } 204 | 205 | void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size) 206 | { 207 | unsigned n; 208 | #ifdef SAMPLE_RATE_DEBUG 209 | static unsigned prev_micSampCnt = 0; 210 | static unsigned prev_micPutCnt = 0; 211 | #endif /* SAMPLE_RATE_DEBUG */ 212 | 213 | //spkrAt2ndHalf = 0; 214 | #ifdef AUDIO_MAL_MODE_CIRCULAR 215 | #endif /* AUDIO_MAL_MODE_CIRCULAR */ 216 | if (_waveType == 1 || _waveType == 2) { 217 | for (n = (AUDIO_BUFFER_SIZE/2); n < AUDIO_BUFFER_SIZE; ) { 218 | if (_waveType == 1) 219 | sine_gen(&n); 220 | else 221 | ramp_gen(&n); 222 | } 223 | } 224 | GPIO_ToggleBits(GPIOC, GPIO_Pin_1); 225 | #ifdef SAMPLE_RATE_DEBUG 226 | vcp_printf("spkrComplete %u", micSampCnt - prev_micSampCnt); 227 | vcp_printf(": %u\r\n", micPutCnt - prev_micPutCnt ); 228 | prev_micSampCnt = micSampCnt; 229 | prev_micPutCnt = micPutCnt; 230 | #endif /* SAMPLE_RATE_DEBUG */ 231 | } 232 | 233 | void EVAL_AUDIO_HalfTransfer_CallBack(uint32_t pBuffer, uint32_t Size) 234 | { 235 | unsigned n; 236 | //spkrAt2ndHalf = 1; 237 | #ifdef AUDIO_MAL_MODE_CIRCULAR 238 | 239 | #endif /* AUDIO_MAL_MODE_CIRCULAR */ 240 | if (_waveType == 1 || _waveType == 2) { 241 | for (n = 0; n < (AUDIO_BUFFER_SIZE/2); ) { 242 | if (_waveType == 1) 243 | sine_gen(&n); 244 | else 245 | ramp_gen(&n); 246 | } 247 | } 248 | GPIO_ToggleBits(GPIOC, GPIO_Pin_2); 249 | } 250 | 251 | void EVAL_AUDIO_Error_CallBack(void* pData) 252 | { 253 | ColorfulRingOfDeath(); 254 | } 255 | 256 | uint16_t EVAL_AUDIO_GetSampleCallBack(void) 257 | { 258 | return 0; 259 | } 260 | -------------------------------------------------------------------------------- /pdm_fir/LICENSE: -------------------------------------------------------------------------------- 1 | PDM bitstream FIR filter 2 | 3 | (C) 2013, Oleg Volkov 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 3. The name of the author may not be used to endorse or promote products 14 | derived from this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /pdm_fir/README.md: -------------------------------------------------------------------------------- 1 | # PDM bitstream FIR filter 2 | 3 | The code utilizes 2 simple approaches to minimize computational efforts: 4 | - circular buffer 5 | - work with words rather than individual bits of the PDM stream 6 | 7 | The latter requires using more memory for coefficients but flush is pretty 8 | cheep now. The provided python script is used to automate coefficients generation. 9 | -------------------------------------------------------------------------------- /pdm_fir/pdm_fir.c: -------------------------------------------------------------------------------- 1 | #include "pdm_fir.h" 2 | 3 | /* PDM FIR filter. 4 | * The source frequency is expected to be 1024kHz so we are receiving 16 bit words at 64kHz rate MSB first. 5 | * The filter cutoff frequency is 8kHz. 6 | */ 7 | 8 | /* The following file contains tables generated by pdm_fir.py script. You can easily customize filter 9 | * parameters by modifying the pdm_fir.py script and regenerating tables. 10 | */ 11 | #include "pdm_fir_.h" 12 | 13 | /* Initialize filter */ 14 | void pdm_fir_flt_init(struct pdm_fir_filter* f) 15 | { 16 | int t; 17 | f->next_tap = 0; 18 | for (t = 0; t < PDM_FTL_TAPS; ++t) 19 | f->buffer[t] = 0x5555; 20 | } 21 | 22 | /* Put 16 bits MSB first */ 23 | void pdm_fir_flt_put(struct pdm_fir_filter* f, uint16_t bits) 24 | { 25 | f->buffer[f->next_tap] = bits; 26 | if (++f->next_tap >= PDM_FTL_TAPS) 27 | f->next_tap = 0; 28 | } 29 | 30 | /* Retrieve output value. May be called at any rate since it does not change the filter state. 31 | * The output ranges from -(2**(out_bits-1)) to +(2**(out_bits-1)). Those values correspond to 32 | * all 0 or all 1 input signal. Note that the output value may still exceed this range so caller 33 | * should truncate return value on its own if necessary. 34 | */ 35 | int pdm_fir_flt_get(struct pdm_fir_filter const* f, int out_bits) 36 | { 37 | int t, i = 0, tot = 0; 38 | for (t = f->next_tap;;) { 39 | uint16_t v = f->buffer[t]; 40 | tot += byte_coeff[i++][(uint8_t)(v>>8)]; 41 | tot += byte_coeff[i++][(uint8_t)(v)]; 42 | if (++t >= PDM_FTL_TAPS) 43 | t = 0; 44 | if (t == f->next_tap) 45 | break; 46 | } 47 | /* Rescale to output range */ 48 | return tot >> (PDM_FTL_SCALE_BITS - out_bits + 1); 49 | } 50 | -------------------------------------------------------------------------------- /pdm_fir/pdm_fir.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /* PDM FIR low pass filter. 4 | * The source frequency is expected to be 1024kHz so we are receiving 16 bit words at 64kHz rate MSB first. 5 | * The filter cutoff frequency is 8kHz. Filter parameters may be easily customized by modifying 6 | * the pdm_fir.py script and regenerating tables in pdm_fir_.h header. 7 | */ 8 | 9 | #include 10 | 11 | #define PDM_FTL_TAPS 16 12 | 13 | struct pdm_fir_filter { 14 | uint16_t buffer[PDM_FTL_TAPS]; 15 | int next_tap; 16 | }; 17 | 18 | /* Initialize filter */ 19 | void pdm_fir_flt_init(struct pdm_fir_filter* f); 20 | 21 | /* Put 16 bits of input PDM signal (MSB first) */ 22 | void pdm_fir_flt_put(struct pdm_fir_filter* f, uint16_t bits); 23 | 24 | /* Retrieve output value. May be called at any rate since it does not change the filter state. 25 | * The output ranges from -(2**(out_bits-1)) to +(2**(out_bits-1)). Those values correspond to 26 | * all 0 or all 1 input signal. Note that the output value may still exceed this range so caller 27 | * should truncate return value on its own if necessary. 28 | */ 29 | int pdm_fir_flt_get(struct pdm_fir_filter const* f, int out_bits); 30 | -------------------------------------------------------------------------------- /pdm_fir/pdm_fir.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import scipy.signal as signal 3 | 4 | PDM_FTL_TAPS = 16 5 | PDM_FTL_SAMPLE_F = 1024 6 | PDM_FTL_CUT_OFF = 8 7 | PDM_FTL_SCALE_BITS = 30 8 | 9 | taps1 = signal.firwin(PDM_FTL_TAPS*16, PDM_FTL_CUT_OFF, nyq=PDM_FTL_SAMPLE_F/2) 10 | taps = (taps1 * (2**PDM_FTL_SCALE_BITS)).astype(int) 11 | 12 | def print_head(): 13 | print '/* Generated by pdm_fir.py */' 14 | print '#define PDM_FTL_SCALE_BITS %d' % PDM_FTL_SCALE_BITS 15 | 16 | def print_taps(): 17 | print 'static int const tap_coeff[PDM_FTL_TAPS*16] = {' 18 | print ','.join(['%i' % t for t in taps]) 19 | print '};' 20 | 21 | def byte_coef(i, b): 22 | bit, off, tot = 1<<7, i*8, 0 23 | while bit: 24 | if bit & b: 25 | tot += taps[off]; 26 | else: 27 | tot -= taps[off]; 28 | bit >>= 1 29 | off += 1 30 | return tot 31 | 32 | def print_byte_coefs(): 33 | print 'static int const byte_coeff[PDM_FTL_TAPS*2][256] = {' 34 | for i in range(PDM_FTL_TAPS*2): 35 | print ' { // [%i]' % i 36 | print ','.join(['%i' % byte_coef(i, b) for b in range(256)]) 37 | print ' },' 38 | print '};' 39 | 40 | if __name__ == '__main__': 41 | if 'plot' in sys.argv: 42 | import numpy as np 43 | from matplotlib.pylab import * 44 | title('Digital filter frequency response') 45 | w, h = signal.freqz(taps1) 46 | plot(w*PDM_FTL_SAMPLE_F/(2*np.pi), np.abs(h), 'b') 47 | ylabel('Amplitude') 48 | yscale('log') 49 | xlabel('Frequency (kHz)') 50 | xlim(0, PDM_FTL_SAMPLE_F/2) 51 | show() 52 | else: 53 | print_head() 54 | # print_taps() 55 | print_byte_coefs() 56 | 57 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## contains 3 projects 2 | 3 | * **passthru**: for checking [stm32f4-discovery](https://www.st.com/en/evaluation-tools/stm32f4discovery.html) hardware, microphone and speaker interface. Requires only one discovery board. Useful for anybody wishing to use this board for audio purpose. 4 | * **vocoder_passthru**: adds vocoder to passthru test using single discovery board: For checking codec2 from microphone encoding directly decoding out to speaker. This doubles CPU power required since both encoding and decoduing must be done for each frame. 5 | * **lora_codec2**: adds SX1262 radio, half duplex transceiver. Requires two discovery boards, each with its own sx1262. PTT is blue button (push to talk) 6 | 7 | ## QuickStart 8 | After cloning this repository: 9 | ``` 10 | $ cd codec2 11 | $ git submodule init 12 | $ git submodule update 13 | ``` 14 | Follow tool install instructions here: [steps 2 and 4 for toolchain and peripheral](https://github.com/drowe67/codec2/tree/master/stm32) library 15 | ``` 16 | (from root directory of this project) 17 | $ mkdir build 18 | $ cd build 19 | $ cmake .. -DCMAKE_TOOLCHAIN_FILE=../codec2/stm32/cmake/STM32_Toolchain.cmake -DPERIPHLIBDIR=/opt/STM32F4xx_DSP_StdPeriph_Lib_V1.8.0/ 20 | $ make 21 | ``` 22 | 23 | Select codec2 bit-rate: uncomment the line in ``CMakeLists.txt``: 24 | ``#target_compile_definitions(lora_codec2 PRIVATE -DCODEC2_MODE=CODEC2_MODE_)`` 25 | Select desired LoRa bandwidth on the line defining ``LORA_BW_KHZ`` in CMakelists.txt. Spreading factor will be selected appropriate for the vocoder bit-rate and LoRa bandwidth selected. Spreading factor functional range is 5 to 12. See file loRa_codec2/main.h where spreading factor is defined according to codec2 bitrate vs LoRa bandwidth. 26 | 27 | for execution logs, define ``ENABLE_VCP_PRINT`` in ``CMakeLists.txt``, however the micro-usb cable must be connected and a serial terminal program running on the PC to take the characters, or the program will halt waiting for characters to send on VCP. Disable this feature when you do not wish to connect the micro-usb cable. 28 | 29 | Flashing stm32f4-discovery 30 | https://github.com/texane/stlink 31 | providing address isnt needed when using st-flash with .hex files 32 | i.e: 33 | ``st-flash --format ihex write lora_codec2.hex`` 34 | Instead, you may also simply copy the ``.bin`` file from the build to the USB flash drive thru the mini-B USB on the stm32f4-discovery. 35 | 36 | 37 | 38 | codec2 mode | samples per frame | encoded bytes per frame | frames per lora packet | LoRa packet length bytes | LoRa BW KHz | LoRa SF | air-time used percent | packet duration (ms) 39 | ----------- | ----------------- | ------------------------ | ---------------------- | ------------------------ | ----------- | ------- | -------------------- | --------- 40 | 3200 | 160 | 8 | 16 | 128 | 500 | 10 | 96 | 41 | 2400 | 160 | 6 | 16 | 96 | 500 | 10 | 77 | 237 42 | 1600 | 320 | 8 | 8 | 64 | 500 | 10 | 56 | 43 | 1600 | 320 | 8 | 16 | 128 | 500 | 11 | 86 | 555 44 | 1400 | 320 | 7 | 16 | 112 | 500 | 11 | 77 | 493 45 | 1300 | 320 | 6.5 | 8 | 52 | 500 | 11 | 90 | 288 46 | 1200 | 320 | 6 | 16 | 96 | 500 | 11 | 71 | 452 47 | 1200 | 320 | 6 | 32 | 192 | 500 | 11 | 62 | 1281 48 | 700C | 320 | 3.5 | 16 | 56 | 250 | 11 | 96 | 576 49 | 450 | 320 | 2.25 | 16 | 36 | 250 | 11 | 71 | 453 50 | 51 | LoRa data rate selection is only possible in steps by a factor of two. Codec2 bit-rate change will affect LoRa packet duty cycle. When duty cycle is under 50%, the LoRa data-rate can be reduced (bandwidth reduced or SF increased). If packet duty cycle is over 100%, then LoRa data-rate must be increased to faster. Typical 2.5 to 2.7dB change in link budget for each step of LoRa data-rate. 52 | 53 | Latency across the radio link is due to LoRa packet duration. 54 | 55 | SX1272 or SX1276 shouldn't be used for this, because SX1261/SX1262 receives large LoRa packets with less errors. 56 | 57 | ## Wiring to LoRa transceiver 58 | pin function | port/pin | discovery connector | [sx1262 shield](https://os.mbed.com/components/SX126xMB2xAS/) pin 59 | ------------ | --------- | -------------------- | ----------- | 60 | Vdd | | P1-3 | J3-4 61 | Gnd | | P1-1 | J2-7 62 | NSS | PA10 | P2-41 | J1-8 63 | MOSI | PA7 | P1-17 | J2-4 64 | MISO | PA6 | P1-18 | J2-5 65 | SCLK | PA5 | P1-15 | J2-6 66 | DIO1 | PA2 | P1-14 | J1-6 67 | BUSY | PB1 | P1-21 | J1-4 68 | AntSwPwr | PB3 | P2-28 | J2-1 69 | Other modules could be also used, such as [dorji module](http://www.dorji.com/products-detail.php?ProId=63) 70 | 71 | ![wiring diagram](stm32f4discovery.png) 72 | 73 | ## implementation details 74 | DMA is used for microphone interface because PDM microphone operates at 16x the required sample rate. In the case of codec2, 8Ksps is required, with the PDM microphone operating at 64ksps. 16 samples are collected using DMA, needing an interrupt only when at least 16 samples are ready to send to PDM filter. 75 | 76 | 77 | ## credits 78 | * [PDM filter](https://github.com/olegv142/pdm_firx) for on-board microphone. 79 | * USB [CDC device for stm32f4-discovery](https://github.com/xenovacivus/STM32DiscoveryVCP). 80 | -------------------------------------------------------------------------------- /stm32_flash.ld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dudmuck/lora_codec2/49f053f5af1716d11c90ab4cffc48112768b026a/stm32_flash.ld -------------------------------------------------------------------------------- /stm32f4discovery.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dudmuck/lora_codec2/49f053f5af1716d11c90ab4cffc48112768b026a/stm32f4discovery.dia -------------------------------------------------------------------------------- /stm32f4discovery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dudmuck/lora_codec2/49f053f5af1716d11c90ab4cffc48112768b026a/stm32f4discovery.png -------------------------------------------------------------------------------- /sx12xx_hal/radio.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sx12xx.h" 4 | 5 | #define PA_OFF_DBM -127 6 | 7 | typedef struct { 8 | void (*DioPin_top_half)(void); 9 | /*! 10 | * \brief Tx Done callback prototype. 11 | */ 12 | void (*TxDone_topHalf)(void); // read irqAt for timestamp of interrupt 13 | void (*TxDone_botHalf)(void); // read irqAt for timestamp of interrupt 14 | /*! 15 | * \brief Tx Timeout callback prototype. 16 | */ 17 | void ( *TxTimeout )( void ); 18 | /*! 19 | * \brief Rx Done callback prototype. 20 | * 21 | * \param [IN] payload Received buffer pointer 22 | * \param [IN] size Received buffer size 23 | * \param [IN] rssi RSSI value computed while receiving the frame [dBm] 24 | * \param [IN] snr Raw SNR value given by the radio hardware 25 | * FSK : N/A ( set to 0 ) 26 | * LoRa: SNR value in dB 27 | * \param [IN] curTime captured time at RxDone event occurance 28 | */ 29 | //void ( *RxDone )(uint16_t size, int16_t rssi, int8_t snr); 30 | void ( *RxDone )(uint8_t size, float rssi, float snr); // read radio.rx_buf for payload, irqAt for timestamp of interrupt 31 | /*! 32 | * \brief Rx Timeout callback prototype. 33 | */ 34 | void ( *RxTimeout )( void ); 35 | /*! 36 | * \brief Rx Error callback prototype. 37 | */ 38 | void ( *RxError )( void ); 39 | /*! 40 | * \brief FHSS Change Channel callback prototype. 41 | * 42 | * \param [IN] currentChannel Index number of the current channel 43 | */ 44 | void ( *FhssChangeChannel )( uint8_t currentChannel ); 45 | 46 | /*! 47 | * \brief CAD Done callback prototype. 48 | * 49 | * \param [IN] channelDetected Channel Activity detected during the CAD 50 | */ 51 | void ( *CadDone ) ( bool channelActivityDetected ); 52 | } RadioEvents_t; 53 | 54 | void Radio_Init(const RadioEvents_t*); 55 | 56 | void Radio_Standby(void); 57 | void Radio_LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr); 58 | void Radio_SetChannel(unsigned hz); 59 | 60 | void Radio_set_tx_dbm(int8_t dbm); 61 | 62 | // preambleLen, fixLen, crcOn, invIQ 63 | void Radio_LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ); 64 | int Radio_Send(uint8_t size/*, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh*/); 65 | 66 | void Radio_printOpMode(void); 67 | void Radio_service(void); 68 | void Radio_Rx(unsigned timeout); 69 | void Radio_SetLoRaSymbolTimeout(uint16_t symbs); 70 | -------------------------------------------------------------------------------- /sx12xx_hal/radio_sx126x.c: -------------------------------------------------------------------------------- 1 | #include "radio.h" 2 | #include "stm32f4xx.h" 3 | 4 | #define CLEAR_ANTSWPWR GPIO_ResetBits(GPIOB, GPIO_Pin_3) 5 | #define SET_ANTSWPWR GPIO_SetBits(GPIOB, GPIO_Pin_3) 6 | 7 | static PacketParams_t pp; 8 | static bool paOff; 9 | static uint8_t loraTimeoutSymbols; 10 | 11 | const RadioEvents_t* RadioEvents; 12 | 13 | void vcp_printf( const char* format, ... ); // yyy remove me 14 | 15 | void Radio_printOpMode() 16 | { 17 | status_t status; 18 | SX126x_xfer(OPCODE_GET_STATUS, 0, 1, &status.octet); 19 | 20 | switch (status.bits.chipMode) { 21 | case 2: vcp_printf("STBY_RC "); break; // STBY_RC 22 | case 3: vcp_printf("STBY_XOSC "); break; // STBY_XOSC 23 | case 4: vcp_printf("FS "); break; // FS 24 | case 5: vcp_printf("RX "); break; // RX 25 | case 6: vcp_printf("TX "); break; // TX 26 | default: vcp_printf("?%d? ", status.bits.chipMode); break; 27 | } 28 | } 29 | 30 | int Radio_Send(uint8_t size/*, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh*/) 31 | { 32 | uint8_t buf[8]; 33 | uint8_t pktType = SX126x_getPacketType(); 34 | 35 | buf[0] = 0; // TX base address 36 | buf[1] = 0; // RX base address 37 | SX126x_xfer(OPCODE_SET_BUFFER_BASE_ADDR, 2, 0, buf); 38 | 39 | if (pktType == PACKET_TYPE_GFSK) { 40 | vcp_printf("Sendgfsklen%u ", size); 41 | pp.gfsk.PayloadLength = size; 42 | SX126x_xfer(OPCODE_SET_PACKET_PARAMS, 8, 0, pp.buf); 43 | } else if (pktType == PACKET_TYPE_LORA) { 44 | vcp_printf("SendLoRaLen%u ", size); 45 | pp.lora.PayloadLength = size; 46 | SX126x_xfer(OPCODE_SET_PACKET_PARAMS, 6, 0, pp.buf); 47 | } 48 | 49 | { 50 | IrqFlags_t irqEnable; 51 | irqEnable.word = 0; 52 | irqEnable.bits.TxDone = 1; 53 | irqEnable.bits.Timeout = 1; 54 | 55 | buf[0] = irqEnable.word >> 8; // enable bits 56 | buf[1] = irqEnable.word; // enable bits 57 | buf[2] = irqEnable.word >> 8; // dio1 58 | buf[3] = irqEnable.word; // dio1 59 | buf[4] = 0; // dio2 60 | buf[5] = 0; // dio2 61 | buf[6] = 0; // dio3 62 | buf[7] = 0; // dio3 63 | SX126x_xfer(OPCODE_SET_DIO_IRQ_PARAMS, 8, 0, buf); 64 | } 65 | 66 | SET_ANTSWPWR; // antswPower = 1; 67 | 68 | #if 0 69 | if (maxListenTime > 0) { 70 | int rssi; 71 | us_timestamp_t startAt, chFreeAt, now; 72 | uint8_t symbs = 0; 73 | 74 | SX126x_xfer(OPCODE_SET_LORA_SYMBOL_TIMEOUT, 1, 0, &symbs); 75 | 76 | radio.start_rx(RX_TIMEOUT_CONTINUOUS); 77 | startAt = lpt.read_us(); 78 | Lstart: 79 | do { 80 | now = lpt.read_us(); 81 | if ((now - startAt) > maxListenTime) { 82 | return -1; 83 | } 84 | SX126x_xfer(OPCODE_GET_RSSIINST, 0, 2, buf); 85 | rssi = buf[1] / -2; 86 | } while (rssi > rssiThresh); 87 | chFreeAt = lpt.read_us(); 88 | do { 89 | now = lpt.read_us(); 90 | SX126x_xfer(OPCODE_GET_RSSIINST, 0, 2, buf); 91 | rssi = buf[1] / -2; 92 | if (rssi > rssiThresh) { 93 | goto Lstart; 94 | } 95 | } while ((now - chFreeAt) < channelFreeTime); 96 | } 97 | #endif /* if 0 */ 98 | 99 | if (paOff) { 100 | unsigned v = SX126x_readReg(REG_ADDR_ANACTRL16, 1); 101 | if ((v & 0x10) == 0) { 102 | v |= 0x10; 103 | SX126x_writeReg(REG_ADDR_ANACTRL16, v, 1); 104 | } 105 | } 106 | SX126x_start_tx(size); 107 | 108 | return 0; 109 | } // ..Send() 110 | 111 | void Radio_LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ) 112 | { 113 | if (SX126x_getPacketType() != PACKET_TYPE_LORA) 114 | SX126x_setPacketType(PACKET_TYPE_LORA); 115 | 116 | pp.lora.PreambleLengthHi = preambleLen >> 8; 117 | pp.lora.PreambleLengthLo = preambleLen; 118 | pp.lora.HeaderType = fixLen; 119 | pp.lora.CRCType = crcOn; 120 | pp.lora.InvertIQ = invIQ; 121 | 122 | SX126x_xfer(OPCODE_SET_PACKET_PARAMS, 6, 0, pp.buf); 123 | } 124 | 125 | void Radio_set_tx_dbm(int8_t dbm) 126 | { 127 | unsigned v = SX126x_readReg(REG_ADDR_ANACTRL16, 1); 128 | 129 | if (dbm == PA_OFF_DBM) { 130 | /* bench test: prevent overloading receiving station (very low tx power) */ 131 | if ((v & 0x10) == 0) { 132 | v |= 0x10; 133 | SX126x_writeReg(REG_ADDR_ANACTRL16, v, 1); 134 | } 135 | paOff = true; 136 | } else { 137 | SX126x_set_tx_dbm(IS_SX1262, dbm); 138 | if (v & 0x10) { 139 | v &= ~0x10; 140 | SX126x_writeReg(REG_ADDR_ANACTRL16, v, 1); 141 | } 142 | paOff = false; 143 | } 144 | } 145 | 146 | void Radio_SetChannel(unsigned hz) 147 | { 148 | SX126x_setMHz(hz / 1000000.0); 149 | } 150 | 151 | void Radio_LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr) 152 | { 153 | ModulationParams_t mp; 154 | float khz, sp; 155 | 156 | if (SX126x_getPacketType() != PACKET_TYPE_LORA) 157 | SX126x_setPacketType(PACKET_TYPE_LORA); 158 | 159 | if (bwKHz > 250) { 160 | mp.lora.bandwidth = LORA_BW_500; 161 | khz = 500; 162 | } else if (bwKHz > 125) { 163 | mp.lora.bandwidth = LORA_BW_250; 164 | khz = 250; 165 | } else if (bwKHz > 63) { 166 | mp.lora.bandwidth = LORA_BW_125; 167 | khz = 125; 168 | } else if (bwKHz > 42) { 169 | mp.lora.bandwidth = LORA_BW_62; 170 | khz = 62.5; 171 | } else if (bwKHz > 32) { 172 | mp.lora.bandwidth = LORA_BW_41; 173 | khz = 41.67; 174 | } else if (bwKHz > 21) { 175 | mp.lora.bandwidth = LORA_BW_31; 176 | khz = 31.25; 177 | } else if (bwKHz > 16) { 178 | mp.lora.bandwidth = LORA_BW_20; 179 | khz = 20.83; 180 | } else if (bwKHz > 11) { 181 | mp.lora.bandwidth = LORA_BW_15; 182 | khz = 15.625; 183 | } else if (bwKHz > 11) { 184 | mp.lora.bandwidth = LORA_BW_10; 185 | khz = 10.42; 186 | } else { 187 | mp.lora.bandwidth = LORA_BW_7; 188 | khz = 7.81; 189 | } 190 | 191 | mp.lora.spreadingFactor = sf; 192 | mp.lora.codingRate = cr; 193 | 194 | sp = (1 << mp.lora.spreadingFactor) / khz; 195 | /* TCXO dependent */ 196 | if (sp > 16) 197 | mp.lora.LowDatarateOptimize = 1; // param4 198 | else 199 | mp.lora.LowDatarateOptimize = 0; // param4 200 | 201 | SX126x_xfer(OPCODE_SET_MODULATION_PARAMS, 4, 0, mp.buf); 202 | 203 | } 204 | 205 | void Radio_Standby() 206 | { 207 | SX126x_setStandby(STBY_RC); // STBY_XOSC 208 | 209 | CLEAR_ANTSWPWR; 210 | } 211 | 212 | 213 | void Radio_txDoneBottom() 214 | { 215 | if (RadioEvents->TxDone_botHalf) 216 | RadioEvents->TxDone_botHalf(); 217 | 218 | CLEAR_ANTSWPWR; 219 | } 220 | 221 | void Radio_rx_done(uint8_t size, float rssi, float snr) 222 | { 223 | RadioEvents->RxDone(size, rssi, snr); 224 | } 225 | 226 | void Radio_timeout_callback(bool tx) 227 | { 228 | if (!tx) { 229 | if (RadioEvents->RxTimeout) 230 | RadioEvents->RxTimeout(); 231 | #ifdef RX_INDICATION 232 | RX_INDICATION = 0; 233 | #endif 234 | } // else TODO tx timeout 235 | } 236 | 237 | void Radio_chipModeChange() 238 | { 239 | /* indicate radio mode with LEDs, if desired */ 240 | } 241 | 242 | void Radio_dio1_top_half() 243 | { 244 | if (RadioEvents->DioPin_top_half) 245 | RadioEvents->DioPin_top_half(); 246 | 247 | if (SX126x_chipMode == CHIPMODE_TX) { 248 | /* TxDone handling requires low latency */ 249 | if (RadioEvents->TxDone_topHalf) { 250 | RadioEvents->TxDone_topHalf(); 251 | } 252 | } else { 253 | #ifdef RX_INDICATION 254 | RX_INDICATION = 0; 255 | #endif 256 | } 257 | } 258 | 259 | void Radio_Init(const RadioEvents_t* e) 260 | { 261 | GPIO_InitTypeDef GPIO_InitStructure; 262 | 263 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 264 | 265 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 266 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 267 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 268 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // ant-sw-power 269 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; 270 | GPIO_Init(GPIOB, &GPIO_InitStructure); 271 | 272 | 273 | SX126x_txDone = Radio_txDoneBottom; 274 | SX126x_rxDone = Radio_rx_done; 275 | SX126x_timeout = Radio_timeout_callback; 276 | SX126x_chipModeChange = Radio_chipModeChange; 277 | SX126x_dio1_topHalf = Radio_dio1_top_half; 278 | 279 | RadioEvents = e; 280 | //lpt.start(); 281 | 282 | init_sx126x(); 283 | 284 | SX126x_SetDIO2AsRfSwitchCtrl(1); 285 | } 286 | 287 | void Radio_service() 288 | { 289 | SX126x_service(); 290 | } 291 | 292 | 293 | void Radio_Rx(unsigned timeout) 294 | { 295 | SET_ANTSWPWR; // antswPower = 1; 296 | 297 | { 298 | uint8_t buf[8]; 299 | IrqFlags_t irqEnable; 300 | irqEnable.word = 0; 301 | irqEnable.bits.RxDone = 1; 302 | irqEnable.bits.Timeout = 1; 303 | 304 | buf[0] = irqEnable.word >> 8; // enable bits 305 | buf[1] = irqEnable.word; // enable bits 306 | buf[2] = irqEnable.word >> 8; // dio1 307 | buf[3] = irqEnable.word; // dio1 308 | buf[4] = 0; // dio2 309 | buf[5] = 0; // dio2 310 | buf[6] = 0; // dio3 311 | buf[7] = 0; // dio3 312 | SX126x_xfer(OPCODE_SET_DIO_IRQ_PARAMS, 8, 0, buf); 313 | } 314 | 315 | #ifdef RX_INDICATION 316 | RX_INDICATION = 1; 317 | #endif 318 | if (timeout == 0) { 319 | uint8_t symbs = 0; 320 | if (SX126x_getPacketType() == PACKET_TYPE_LORA) // shut off timeout 321 | SX126x_xfer(OPCODE_SET_LORA_SYMBOL_TIMEOUT, 1, 0, &symbs); 322 | 323 | SX126x_start_rx(RX_TIMEOUT_CONTINUOUS); 324 | } else { 325 | if (SX126x_getPacketType() == PACKET_TYPE_LORA) 326 | SX126x_xfer(OPCODE_SET_LORA_SYMBOL_TIMEOUT, 1, 0, &loraTimeoutSymbols); 327 | 328 | SX126x_start_rx(timeout * RC_TICKS_PER_US); 329 | } 330 | } 331 | 332 | void Radio_SetLoRaSymbolTimeout(uint16_t symbs) 333 | { 334 | if (SX126x_getPacketType() != PACKET_TYPE_LORA) 335 | SX126x_setPacketType(PACKET_TYPE_LORA); 336 | 337 | loraTimeoutSymbols = symbs; 338 | SX126x_xfer(OPCODE_SET_LORA_SYMBOL_TIMEOUT, 1, 0, &loraTimeoutSymbols); 339 | } 340 | 341 | -------------------------------------------------------------------------------- /usb_conf/usb_bsp.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_bsp.c 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 19-September-2011 7 | * @brief This file is responsible to offer board support package and is 8 | * configurable by user. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Includes ------------------------------------------------------------------*/ 24 | #include "usb_bsp.h" 25 | #include "usbd_conf.h" 26 | #include "stm32f4xx_conf.h" 27 | #include "stm32f4xx_gpio.h" 28 | #include "stm32f4xx_exti.h" 29 | #include "stm32f4xx_rcc.h" 30 | #include "misc.h" 31 | 32 | 33 | void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev) { 34 | 35 | } 36 | 37 | void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev,uint8_t state) { 38 | 39 | } 40 | 41 | 42 | /** 43 | * @brief USB_OTG_BSP_Init 44 | * Initilizes BSP configurations 45 | * @param None 46 | * @retval None 47 | */ 48 | 49 | void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) 50 | { 51 | GPIO_InitTypeDef GPIO_InitStructure; 52 | 53 | #ifndef USE_ULPI_PHY 54 | #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT 55 | EXTI_InitTypeDef EXTI_InitStructure; 56 | NVIC_InitTypeDef NVIC_InitStructure; 57 | #endif 58 | #endif 59 | 60 | 61 | #ifdef USE_USB_OTG_FS 62 | 63 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); 64 | 65 | /* Configure SOF VBUS ID DM DP Pins */ 66 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | 67 | GPIO_Pin_9 | 68 | GPIO_Pin_11 | 69 | GPIO_Pin_12; 70 | 71 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 72 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 73 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 74 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 75 | GPIO_Init(GPIOA, &GPIO_InitStructure); 76 | 77 | GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ; 78 | GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; 79 | GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; 80 | GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; 81 | 82 | /* this for ID line debug */ 83 | 84 | 85 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 86 | GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; 87 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; 88 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 89 | GPIO_Init(GPIOA, &GPIO_InitStructure); 90 | GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ; 91 | 92 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 93 | RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; 94 | #else // USE_USB_OTG_HS 95 | 96 | #ifdef USE_ULPI_PHY // ULPI 97 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | 98 | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | 99 | RCC_AHB1Periph_GPIOI, ENABLE); 100 | 101 | 102 | GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0 103 | GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK 104 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1 105 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2 106 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7 107 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3 108 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4 109 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5 110 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6 111 | GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT 112 | GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR 113 | GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP 114 | 115 | // CLK 116 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; 117 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 118 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 119 | GPIO_Init(GPIOA, &GPIO_InitStructure); 120 | 121 | // D0 122 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; 123 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 124 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 125 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 126 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 127 | GPIO_Init(GPIOA, &GPIO_InitStructure); 128 | 129 | 130 | 131 | // D1 D2 D3 D4 D5 D6 D7 132 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | 133 | GPIO_Pin_5 | GPIO_Pin_10 | 134 | GPIO_Pin_11| GPIO_Pin_12 | 135 | GPIO_Pin_13 ; 136 | 137 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 138 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 139 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 140 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 141 | GPIO_Init(GPIOB, &GPIO_InitStructure); 142 | 143 | 144 | // STP 145 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; 146 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 147 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 148 | GPIO_Init(GPIOC, &GPIO_InitStructure); 149 | 150 | //NXT 151 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 152 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 153 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 154 | GPIO_Init(GPIOH, &GPIO_InitStructure); 155 | 156 | 157 | //DIR 158 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; 159 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 160 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 161 | GPIO_Init(GPIOI, &GPIO_InitStructure); 162 | 163 | 164 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | 165 | RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ; 166 | 167 | #else 168 | #ifdef USE_I2C_PHY 169 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE); 170 | /* Configure RESET INTN SCL SDA (Phy/I2C) Pins */ 171 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | 172 | GPIO_Pin_1 | 173 | GPIO_Pin_10 | 174 | GPIO_Pin_11; 175 | 176 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 177 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 178 | GPIO_Init(GPIOB, &GPIO_InitStructure); 179 | 180 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_OTG2_FS) ; 181 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_OTG2_FS) ; 182 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_FS) ; 183 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_FS); 184 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; 185 | 186 | #else 187 | 188 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE); 189 | 190 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | 191 | GPIO_Pin_13 | 192 | GPIO_Pin_14 | 193 | GPIO_Pin_15; 194 | 195 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 196 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 197 | GPIO_Init(GPIOB, &GPIO_InitStructure); 198 | 199 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; 200 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; 201 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; 202 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ; 203 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; 204 | #endif 205 | #endif // USE_ULPI_PHY 206 | 207 | #endif //USB_OTG_HS 208 | 209 | 210 | /* enable the PWR clock */ 211 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE); 212 | 213 | /* Configure the Key button in EXTI mode */ 214 | //STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); 215 | 216 | #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT 217 | EXTI_ClearITPendingBit(EXTI_Line18); 218 | 219 | EXTI_InitStructure.EXTI_Line = EXTI_Line18; 220 | EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 221 | EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 222 | EXTI_InitStructure.EXTI_LineCmd = ENABLE; 223 | EXTI_Init(&EXTI_InitStructure); 224 | 225 | EXTI_ClearITPendingBit(EXTI_Line18); 226 | 227 | NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_WKUP_IRQn; 228 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 229 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 230 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 231 | NVIC_Init(&NVIC_InitStructure); 232 | 233 | EXTI_ClearITPendingBit(EXTI_Line18); 234 | #endif 235 | 236 | #ifdef USB_OTG_HS_LOW_PWR_MGMT_SUPPORT 237 | EXTI_ClearITPendingBit(EXTI_Line20); 238 | 239 | EXTI_InitStructure.EXTI_Line = EXTI_Line20; 240 | EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 241 | EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 242 | EXTI_InitStructure.EXTI_LineCmd = ENABLE; 243 | EXTI_Init(&EXTI_InitStructure); 244 | 245 | EXTI_ClearITPendingBit(EXTI_Line20); 246 | 247 | NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_WKUP_IRQn; 248 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; 249 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; 250 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 251 | NVIC_Init(&NVIC_InitStructure); 252 | 253 | EXTI_ClearITPendingBit(EXTI_Line20); 254 | #endif 255 | 256 | EXTI_ClearITPendingBit(EXTI_Line0); 257 | } 258 | /** 259 | * @brief USB_OTG_BSP_EnableInterrupt 260 | * Enabele USB Global interrupt 261 | * @param None 262 | * @retval None 263 | */ 264 | void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev) 265 | { 266 | NVIC_InitTypeDef NVIC_InitStructure; 267 | 268 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 269 | #ifdef USE_USB_OTG_HS 270 | NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_IRQn; 271 | #else 272 | NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn; 273 | #endif 274 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 275 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; 276 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 277 | NVIC_Init(&NVIC_InitStructure); 278 | #ifdef USB_OTG_HS_DEDICATED_EP1_ENABLED 279 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 280 | NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_EP1_OUT_IRQn; 281 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 282 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; 283 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 284 | NVIC_Init(&NVIC_InitStructure); 285 | 286 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 287 | NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_EP1_IN_IRQn; 288 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 289 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 290 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 291 | NVIC_Init(&NVIC_InitStructure); 292 | #endif 293 | } 294 | /** 295 | * @brief USB_OTG_BSP_uDelay 296 | * This function provides delay time in micro sec 297 | * @param usec : Value of delay required in micro sec 298 | * @retval None 299 | */ 300 | void USB_OTG_BSP_uDelay (const uint32_t usec) 301 | { 302 | uint32_t count = 0; 303 | const uint32_t utime = (120 * usec / 7); 304 | do 305 | { 306 | if ( ++count > utime ) 307 | { 308 | return ; 309 | } 310 | } 311 | while (1); 312 | } 313 | 314 | 315 | /** 316 | * @brief USB_OTG_BSP_mDelay 317 | * This function provides delay time in milli sec 318 | * @param msec : Value of delay required in milli sec 319 | * @retval None 320 | */ 321 | void USB_OTG_BSP_mDelay (const uint32_t msec) 322 | { 323 | USB_OTG_BSP_uDelay(msec * 1000); 324 | } 325 | /** 326 | * @} 327 | */ 328 | 329 | /** 330 | * @} 331 | */ 332 | 333 | /** 334 | * @} 335 | */ 336 | 337 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 338 | -------------------------------------------------------------------------------- /usb_conf/usb_bsp.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_bsp.h 4 | * @author MCD Application Team 5 | * @version V2.0.0 6 | * @date 22-July-2011 7 | * @brief Specific api's relative to the used hardware platform 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __USB_BSP__H__ 24 | #define __USB_BSP__H__ 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usb_core.h" 28 | #include "stm32f4xx.h" 29 | 30 | /** @addtogroup USB_OTG_DRIVER 31 | * @{ 32 | */ 33 | 34 | /** @defgroup USB_BSP 35 | * @brief This file is the 36 | * @{ 37 | */ 38 | 39 | 40 | /** @defgroup USB_BSP_Exported_Defines 41 | * @{ 42 | */ 43 | /** 44 | * @} 45 | */ 46 | 47 | 48 | /** @defgroup USB_BSP_Exported_Types 49 | * @{ 50 | */ 51 | /** 52 | * @} 53 | */ 54 | 55 | 56 | /** @defgroup USB_BSP_Exported_Macros 57 | * @{ 58 | */ 59 | /** 60 | * @} 61 | */ 62 | 63 | /** @defgroup USB_BSP_Exported_Variables 64 | * @{ 65 | */ 66 | /** 67 | * @} 68 | */ 69 | 70 | /** @defgroup USB_BSP_Exported_FunctionsPrototype 71 | * @{ 72 | */ 73 | void BSP_Init(void); 74 | 75 | void USB_OTG_BSP_Init (USB_OTG_CORE_HANDLE *pdev); 76 | void USB_OTG_BSP_uDelay (const uint32_t usec); 77 | void USB_OTG_BSP_mDelay (const uint32_t msec); 78 | void USB_OTG_BSP_EnableInterrupt (USB_OTG_CORE_HANDLE *pdev); 79 | #ifdef USE_HOST_MODE 80 | void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev); 81 | void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev,uint8_t state); 82 | #endif 83 | /** 84 | * @} 85 | */ 86 | 87 | #endif //__USB_BSP__H__ 88 | 89 | /** 90 | * @} 91 | */ 92 | 93 | /** 94 | * @} 95 | */ 96 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 97 | 98 | -------------------------------------------------------------------------------- /usb_conf/usb_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_conf.h 4 | * @author MCD Application Team 5 | * @version V2.0.0 6 | * @date 22-July-2011 7 | * @brief general low level driver configuration 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __USB_CONF__H__ 24 | #define __USB_CONF__H__ 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "stm32f4xx.h" 28 | 29 | 30 | /** @addtogroup USB_OTG_DRIVER 31 | * @{ 32 | */ 33 | 34 | /** @defgroup USB_CONF 35 | * @brief USB low level driver configuration file 36 | * @{ 37 | */ 38 | 39 | /** @defgroup USB_CONF_Exported_Defines 40 | * @{ 41 | */ 42 | 43 | /* USB Core and PHY interface configuration. 44 | Tip: To avoid modifying these defines each time you need to change the USB 45 | configuration, you can declare the needed define in your toolchain 46 | compiler preprocessor. 47 | */ 48 | #ifndef USE_USB_OTG_FS 49 | #define USE_USB_OTG_FS 50 | #endif /* USE_USB_OTG_FS */ 51 | 52 | #ifndef USE_USB_OTG_HS 53 | //#define USE_USB_OTG_HS 54 | #endif /* USE_USB_OTG_HS */ 55 | 56 | #ifndef USE_ULPI_PHY 57 | #define USE_ULPI_PHY 58 | #endif /* USE_ULPI_PHY */ 59 | 60 | #ifndef USE_EMBEDDED_PHY 61 | //#define USE_EMBEDDED_PHY 62 | #endif /* USE_EMBEDDED_PHY */ 63 | 64 | #ifndef USE_I2C_PHY 65 | //#define USE_I2C_PHY 66 | #endif /* USE_I2C_PHY */ 67 | 68 | 69 | #ifdef USE_USB_OTG_FS 70 | #define USB_OTG_FS_CORE 71 | #endif 72 | 73 | #ifdef USE_USB_OTG_HS 74 | #define USB_OTG_HS_CORE 75 | #endif 76 | 77 | /******************************************************************************* 78 | * FIFO Size Configuration in Device mode 79 | * 80 | * (i) Receive data FIFO size = RAM for setup packets + 81 | * OUT endpoint control information + 82 | * data OUT packets + miscellaneous 83 | * Space = ONE 32-bits words 84 | * --> RAM for setup packets = 10 spaces 85 | * (n is the nbr of CTRL EPs the device core supports) 86 | * --> OUT EP CTRL info = 1 space 87 | * (one space for status information written to the FIFO along with each 88 | * received packet) 89 | * --> data OUT packets = (Largest Packet Size / 4) + 1 spaces 90 | * (MINIMUM to receive packets) 91 | * --> OR data OUT packets = at least 2*(Largest Packet Size / 4) + 1 spaces 92 | * (if high-bandwidth EP is enabled or multiple isochronous EPs) 93 | * --> miscellaneous = 1 space per OUT EP 94 | * (one space for transfer complete status information also pushed to the 95 | * FIFO with each endpoint's last packet) 96 | * 97 | * (ii)MINIMUM RAM space required for each IN EP Tx FIFO = MAX packet size for 98 | * that particular IN EP. More space allocated in the IN EP Tx FIFO results 99 | * in a better performance on the USB and can hide latencies on the AHB. 100 | * 101 | * (iii) TXn min size = 16 words. (n : Transmit FIFO index) 102 | * (iv) When a TxFIFO is not used, the Configuration should be as follows: 103 | * case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes) 104 | * --> Txm can use the space allocated for Txn. 105 | * case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes) 106 | * --> Txn should be configured with the minimum space of 16 words 107 | * (v) The FIFO is used optimally when used TxFIFOs are allocated in the top 108 | * of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones. 109 | *******************************************************************************/ 110 | 111 | /******************************************************************************* 112 | * FIFO Size Configuration in Host mode 113 | * 114 | * (i) Receive data FIFO size = (Largest Packet Size / 4) + 1 or 115 | * 2x (Largest Packet Size / 4) + 1, If a 116 | * high-bandwidth channel or multiple isochronous 117 | * channels are enabled 118 | * 119 | * (ii) For the host nonperiodic Transmit FIFO is the largest maximum packet size 120 | * for all supported nonperiodic OUT channels. Typically, a space 121 | * corresponding to two Largest Packet Size is recommended. 122 | * 123 | * (iii) The minimum amount of RAM required for Host periodic Transmit FIFO is 124 | * the largest maximum packet size for all supported periodic OUT channels. 125 | * If there is at least one High Bandwidth Isochronous OUT endpoint, 126 | * then the space must be at least two times the maximum packet size for 127 | * that channel. 128 | *******************************************************************************/ 129 | 130 | /****************** USB OTG HS CONFIGURATION **********************************/ 131 | #ifdef USB_OTG_HS_CORE 132 | #define RX_FIFO_HS_SIZE 512 133 | #define TX0_FIFO_HS_SIZE 512 134 | #define TX1_FIFO_HS_SIZE 512 135 | #define TX2_FIFO_HS_SIZE 0 136 | #define TX3_FIFO_HS_SIZE 0 137 | #define TX4_FIFO_HS_SIZE 0 138 | #define TX5_FIFO_HS_SIZE 0 139 | #define TXH_NP_HS_FIFOSIZ 96 140 | #define TXH_P_HS_FIFOSIZ 96 141 | 142 | //#define USB_OTG_HS_LOW_PWR_MGMT_SUPPORT 143 | //#define USB_OTG_HS_SOF_OUTPUT_ENABLED 144 | 145 | //#define USB_OTG_INTERNAL_VBUS_ENABLED 146 | #define USB_OTG_EXTERNAL_VBUS_ENABLED 147 | 148 | #ifdef USE_ULPI_PHY 149 | #define USB_OTG_ULPI_PHY_ENABLED 150 | #endif 151 | #ifdef USE_EMBEDDED_PHY 152 | #define USB_OTG_EMBEDDED_PHY_ENABLED 153 | #endif 154 | #ifdef USE_I2C_PHY 155 | #define USB_OTG_I2C_PHY_ENABLED 156 | #endif 157 | #define USB_OTG_HS_INTERNAL_DMA_ENABLED 158 | #define USB_OTG_HS_DEDICATED_EP1_ENABLED 159 | #endif 160 | 161 | /****************** USB OTG FS CONFIGURATION **********************************/ 162 | #ifdef USB_OTG_FS_CORE 163 | #define RX_FIFO_FS_SIZE 128 164 | #define TX0_FIFO_FS_SIZE 64 165 | #define TX1_FIFO_FS_SIZE 128 166 | #define TX2_FIFO_FS_SIZE 0 167 | #define TX3_FIFO_FS_SIZE 0 168 | #define TXH_NP_FS_FIFOSIZ 96 169 | #define TXH_P_FS_FIFOSIZ 96 170 | 171 | //#define USB_OTG_FS_LOW_PWR_MGMT_SUPPORT 172 | //#define USB_OTG_FS_SOF_OUTPUT_ENABLED 173 | #endif 174 | 175 | /****************** USB OTG MODE CONFIGURATION ********************************/ 176 | //#define USE_HOST_MODE 177 | #define USE_DEVICE_MODE 178 | //#define USE_OTG_MODE 179 | 180 | 181 | #ifndef USB_OTG_FS_CORE 182 | #ifndef USB_OTG_HS_CORE 183 | #error "USB_OTG_HS_CORE or USB_OTG_FS_CORE should be defined" 184 | #endif 185 | #endif 186 | 187 | 188 | #ifndef USE_DEVICE_MODE 189 | #ifndef USE_HOST_MODE 190 | #error "USE_DEVICE_MODE or USE_HOST_MODE should be defined" 191 | #endif 192 | #endif 193 | 194 | #ifndef USE_USB_OTG_HS 195 | #ifndef USE_USB_OTG_FS 196 | #error "USE_USB_OTG_HS or USE_USB_OTG_FS should be defined" 197 | #endif 198 | #else //USE_USB_OTG_HS 199 | #ifndef USE_ULPI_PHY 200 | #ifndef USE_EMBEDDED_PHY 201 | #ifndef USE_I2C_PHY 202 | #error "USE_ULPI_PHY or USE_EMBEDDED_PHY or USE_I2C_PHY should be defined" 203 | #endif 204 | #endif 205 | #endif 206 | #endif 207 | 208 | /****************** C Compilers dependant keywords ****************************/ 209 | /* In HS mode and when the DMA is used, all variables and data structures dealing 210 | with the DMA during the transaction process should be 4-bytes aligned */ 211 | #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED 212 | #if defined (__GNUC__) /* GNU Compiler */ 213 | #define __ALIGN_END __attribute__ ((aligned (4))) 214 | #define __ALIGN_BEGIN 215 | #else 216 | #define __ALIGN_END 217 | #if defined (__CC_ARM) /* ARM Compiler */ 218 | #define __ALIGN_BEGIN __align(4) 219 | #elif defined (__ICCARM__) /* IAR Compiler */ 220 | #define __ALIGN_BEGIN 221 | #elif defined (__TASKING__) /* TASKING Compiler */ 222 | #define __ALIGN_BEGIN __align(4) 223 | #endif /* __CC_ARM */ 224 | #endif /* __GNUC__ */ 225 | #else 226 | #define __ALIGN_BEGIN 227 | #define __ALIGN_END 228 | #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ 229 | 230 | /* __packed keyword used to decrease the data type alignment to 1-byte */ 231 | #if defined (__CC_ARM) /* ARM Compiler */ 232 | #define __packed __packed 233 | #elif defined (__ICCARM__) /* IAR Compiler */ 234 | #define __packed __packed 235 | #elif defined ( __GNUC__ ) /* GNU Compiler */ 236 | #define __packed __attribute__ ((__packed__)) 237 | #elif defined (__TASKING__) /* TASKING Compiler */ 238 | #define __packed __unaligned 239 | #endif /* __CC_ARM */ 240 | 241 | /** 242 | * @} 243 | */ 244 | 245 | 246 | /** @defgroup USB_CONF_Exported_Types 247 | * @{ 248 | */ 249 | /** 250 | * @} 251 | */ 252 | 253 | 254 | /** @defgroup USB_CONF_Exported_Macros 255 | * @{ 256 | */ 257 | /** 258 | * @} 259 | */ 260 | 261 | /** @defgroup USB_CONF_Exported_Variables 262 | * @{ 263 | */ 264 | /** 265 | * @} 266 | */ 267 | 268 | /** @defgroup USB_CONF_Exported_FunctionsPrototype 269 | * @{ 270 | */ 271 | /** 272 | * @} 273 | */ 274 | 275 | 276 | #endif //__USB_CONF__H__ 277 | 278 | 279 | /** 280 | * @} 281 | */ 282 | 283 | /** 284 | * @} 285 | */ 286 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 287 | 288 | -------------------------------------------------------------------------------- /usb_conf/usbd_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_conf.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief USB Device configuration file 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __USBD_CONF__H__ 24 | #define __USBD_CONF__H__ 25 | 26 | /** @defgroup USB_CONF_Exported_Defines 27 | * @{ 28 | */ 29 | #define USBD_CFG_MAX_NUM 1 30 | #define USBD_ITF_MAX_NUM 1 31 | #define USB_MAX_STR_DESC_SIZ 100 32 | 33 | /** @defgroup USB_VCP_Class_Layer_Parameter 34 | * @{ 35 | */ 36 | #define CDC_IN_EP 0x81 /* EP1 for data IN */ 37 | #define CDC_OUT_EP 0x01 /* EP1 for data OUT */ 38 | #define CDC_CMD_EP 0x82 /* EP2 for CDC commands */ 39 | 40 | /* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */ 41 | #ifdef USE_USB_OTG_HS 42 | #define CDC_DATA_MAX_PACKET_SIZE 512 /* Endpoint IN & OUT Packet size */ 43 | #define CDC_CMD_PACKET_SZE 8 /* Control Endpoint Packet size */ 44 | 45 | #define CDC_IN_FRAME_INTERVAL 40 /* Number of micro-frames between IN transfers */ 46 | #define APP_RX_DATA_SIZE 2048 /* Total size of IN buffer: 47 | APP_RX_DATA_SIZE*8/MAX_BAUDARATE*1000 should be > CDC_IN_FRAME_INTERVAL*8 */ 48 | #else 49 | #define CDC_DATA_MAX_PACKET_SIZE 64 /* Endpoint IN & OUT Packet size */ 50 | #define CDC_CMD_PACKET_SZE 8 /* Control Endpoint Packet size */ 51 | 52 | #define CDC_IN_FRAME_INTERVAL 5 /* Number of frames between IN transfers */ 53 | #define APP_RX_DATA_SIZE 2048 /* Total size of IN buffer: 54 | APP_RX_DATA_SIZE*8/MAX_BAUDARATE*1000 should be > CDC_IN_FRAME_INTERVAL */ 55 | #endif /* USE_USB_OTG_HS */ 56 | 57 | #define APP_FOPS VCP_fops 58 | /** 59 | * @} 60 | */ 61 | 62 | /** @defgroup USB_CONF_Exported_Types 63 | * @{ 64 | */ 65 | /** 66 | * @} 67 | */ 68 | 69 | 70 | /** @defgroup USB_CONF_Exported_Macros 71 | * @{ 72 | */ 73 | /** 74 | * @} 75 | */ 76 | 77 | /** @defgroup USB_CONF_Exported_Variables 78 | * @{ 79 | */ 80 | /** 81 | * @} 82 | */ 83 | 84 | /** @defgroup USB_CONF_Exported_FunctionsPrototype 85 | * @{ 86 | */ 87 | /** 88 | * @} 89 | */ 90 | 91 | 92 | #endif //__USBD_CONF__H__ 93 | 94 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 95 | 96 | -------------------------------------------------------------------------------- /usb_conf/usbd_desc.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_desc.c 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 19-September-2011 7 | * @brief This file provides the USBD descriptors and string formating method. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Includes ------------------------------------------------------------------*/ 23 | #include "usbd_core.h" 24 | #include "usbd_desc.h" 25 | #include "usbd_req.h" 26 | #include "usbd_conf.h" 27 | #include "usb_regs.h" 28 | 29 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 30 | * @{ 31 | */ 32 | 33 | 34 | /** @defgroup USBD_DESC 35 | * @brief USBD descriptors module 36 | * @{ 37 | */ 38 | 39 | /** @defgroup USBD_DESC_Private_TypesDefinitions 40 | * @{ 41 | */ 42 | /** 43 | * @} 44 | */ 45 | 46 | 47 | /** @defgroup USBD_DESC_Private_Defines 48 | * @{ 49 | */ 50 | 51 | #define USBD_VID 0x0483 52 | 53 | #define USBD_PID 0x5740 54 | 55 | /** @defgroup USB_String_Descriptors 56 | * @{ 57 | */ 58 | #define USBD_LANGID_STRING 0x409 59 | #define USBD_MANUFACTURER_STRING "STMicroelectronics" 60 | 61 | #define USBD_PRODUCT_HS_STRING "STM32 Virtual ComPort in HS mode" 62 | #define USBD_SERIALNUMBER_HS_STRING "00000000050B" 63 | 64 | #define USBD_PRODUCT_FS_STRING "STM32 Virtual ComPort in FS Mode" 65 | #define USBD_SERIALNUMBER_FS_STRING "00000000050C" 66 | 67 | #define USBD_CONFIGURATION_HS_STRING "VCP Config" 68 | #define USBD_INTERFACE_HS_STRING "VCP Interface" 69 | 70 | #define USBD_CONFIGURATION_FS_STRING "VCP Config" 71 | #define USBD_INTERFACE_FS_STRING "VCP Interface" 72 | /** 73 | * @} 74 | */ 75 | 76 | 77 | /** @defgroup USBD_DESC_Private_Macros 78 | * @{ 79 | */ 80 | /** 81 | * @} 82 | */ 83 | 84 | 85 | /** @defgroup USBD_DESC_Private_Variables 86 | * @{ 87 | */ 88 | 89 | USBD_DEVICE USR_desc = 90 | { 91 | USBD_USR_DeviceDescriptor, 92 | USBD_USR_LangIDStrDescriptor, 93 | USBD_USR_ManufacturerStrDescriptor, 94 | USBD_USR_ProductStrDescriptor, 95 | USBD_USR_SerialStrDescriptor, 96 | USBD_USR_ConfigStrDescriptor, 97 | USBD_USR_InterfaceStrDescriptor, 98 | 99 | }; 100 | 101 | #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED 102 | #if defined ( __ICCARM__ ) /*!< IAR Compiler */ 103 | #pragma data_alignment=4 104 | #endif 105 | #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ 106 | /* USB Standard Device Descriptor */ 107 | __ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_SIZ_DEVICE_DESC] __ALIGN_END = 108 | { 109 | 0x12, /*bLength */ 110 | USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/ 111 | 0x00, /*bcdUSB */ 112 | 0x02, 113 | 0x00, /*bDeviceClass*/ 114 | 0x00, /*bDeviceSubClass*/ 115 | 0x00, /*bDeviceProtocol*/ 116 | USB_OTG_MAX_EP0_SIZE, /*bMaxPacketSize*/ 117 | LOBYTE(USBD_VID), /*idVendor*/ 118 | HIBYTE(USBD_VID), /*idVendor*/ 119 | LOBYTE(USBD_PID), /*idVendor*/ 120 | HIBYTE(USBD_PID), /*idVendor*/ 121 | 0x00, /*bcdDevice rel. 2.00*/ 122 | 0x02, 123 | USBD_IDX_MFC_STR, /*Index of manufacturer string*/ 124 | USBD_IDX_PRODUCT_STR, /*Index of product string*/ 125 | USBD_IDX_SERIAL_STR, /*Index of serial number string*/ 126 | USBD_CFG_MAX_NUM /*bNumConfigurations*/ 127 | } ; /* USB_DeviceDescriptor */ 128 | 129 | #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED 130 | #if defined ( __ICCARM__ ) /*!< IAR Compiler */ 131 | #pragma data_alignment=4 132 | #endif 133 | #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ 134 | /* USB Standard Device Descriptor */ 135 | __ALIGN_BEGIN uint8_t USBD_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = 136 | { 137 | USB_LEN_DEV_QUALIFIER_DESC, 138 | USB_DESC_TYPE_DEVICE_QUALIFIER, 139 | 0x00, 140 | 0x02, 141 | 0x00, 142 | 0x00, 143 | 0x00, 144 | 0x40, 145 | 0x01, 146 | 0x00, 147 | }; 148 | 149 | #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED 150 | #if defined ( __ICCARM__ ) /*!< IAR Compiler */ 151 | #pragma data_alignment=4 152 | #endif 153 | #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ 154 | /* USB Standard Device Descriptor */ 155 | __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_SIZ_STRING_LANGID] __ALIGN_END = 156 | { 157 | USB_SIZ_STRING_LANGID, 158 | USB_DESC_TYPE_STRING, 159 | LOBYTE(USBD_LANGID_STRING), 160 | HIBYTE(USBD_LANGID_STRING), 161 | }; 162 | /** 163 | * @} 164 | */ 165 | 166 | 167 | /** @defgroup USBD_DESC_Private_FunctionPrototypes 168 | * @{ 169 | */ 170 | /** 171 | * @} 172 | */ 173 | 174 | 175 | /** @defgroup USBD_DESC_Private_Functions 176 | * @{ 177 | */ 178 | 179 | /** 180 | * @brief USBD_USR_DeviceDescriptor 181 | * return the device descriptor 182 | * @param speed : current device speed 183 | * @param length : pointer to data length variable 184 | * @retval pointer to descriptor buffer 185 | */ 186 | uint8_t * USBD_USR_DeviceDescriptor( uint8_t speed , uint16_t *length) 187 | { 188 | *length = sizeof(USBD_DeviceDesc); 189 | return USBD_DeviceDesc; 190 | } 191 | 192 | /** 193 | * @brief USBD_USR_LangIDStrDescriptor 194 | * return the LangID string descriptor 195 | * @param speed : current device speed 196 | * @param length : pointer to data length variable 197 | * @retval pointer to descriptor buffer 198 | */ 199 | uint8_t * USBD_USR_LangIDStrDescriptor( uint8_t speed , uint16_t *length) 200 | { 201 | *length = sizeof(USBD_LangIDDesc); 202 | return USBD_LangIDDesc; 203 | } 204 | 205 | 206 | /** 207 | * @brief USBD_USR_ProductStrDescriptor 208 | * return the product string descriptor 209 | * @param speed : current device speed 210 | * @param length : pointer to data length variable 211 | * @retval pointer to descriptor buffer 212 | */ 213 | uint8_t * USBD_USR_ProductStrDescriptor( uint8_t speed , uint16_t *length) 214 | { 215 | 216 | 217 | if(speed == 0) 218 | { 219 | USBD_GetString ((uint8_t*)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length); 220 | } 221 | else 222 | { 223 | USBD_GetString ((uint8_t*)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length); 224 | } 225 | return USBD_StrDesc; 226 | } 227 | 228 | /** 229 | * @brief USBD_USR_ManufacturerStrDescriptor 230 | * return the manufacturer string descriptor 231 | * @param speed : current device speed 232 | * @param length : pointer to data length variable 233 | * @retval pointer to descriptor buffer 234 | */ 235 | uint8_t * USBD_USR_ManufacturerStrDescriptor( uint8_t speed , uint16_t *length) 236 | { 237 | USBD_GetString ((uint8_t*)USBD_MANUFACTURER_STRING, USBD_StrDesc, length); 238 | return USBD_StrDesc; 239 | } 240 | 241 | /** 242 | * @brief USBD_USR_SerialStrDescriptor 243 | * return the serial number string descriptor 244 | * @param speed : current device speed 245 | * @param length : pointer to data length variable 246 | * @retval pointer to descriptor buffer 247 | */ 248 | uint8_t * USBD_USR_SerialStrDescriptor( uint8_t speed , uint16_t *length) 249 | { 250 | if(speed == USB_OTG_SPEED_HIGH) 251 | { 252 | USBD_GetString ((uint8_t*)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length); 253 | } 254 | else 255 | { 256 | USBD_GetString ((uint8_t*)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length); 257 | } 258 | return USBD_StrDesc; 259 | } 260 | 261 | /** 262 | * @brief USBD_USR_ConfigStrDescriptor 263 | * return the configuration string descriptor 264 | * @param speed : current device speed 265 | * @param length : pointer to data length variable 266 | * @retval pointer to descriptor buffer 267 | */ 268 | uint8_t * USBD_USR_ConfigStrDescriptor( uint8_t speed , uint16_t *length) 269 | { 270 | if(speed == USB_OTG_SPEED_HIGH) 271 | { 272 | USBD_GetString ((uint8_t*)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length); 273 | } 274 | else 275 | { 276 | USBD_GetString ((uint8_t*)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length); 277 | } 278 | return USBD_StrDesc; 279 | } 280 | 281 | 282 | /** 283 | * @brief USBD_USR_InterfaceStrDescriptor 284 | * return the interface string descriptor 285 | * @param speed : current device speed 286 | * @param length : pointer to data length variable 287 | * @retval pointer to descriptor buffer 288 | */ 289 | uint8_t * USBD_USR_InterfaceStrDescriptor( uint8_t speed , uint16_t *length) 290 | { 291 | if(speed == 0) 292 | { 293 | USBD_GetString ((uint8_t*)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length); 294 | } 295 | else 296 | { 297 | USBD_GetString ((uint8_t*)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length); 298 | } 299 | return USBD_StrDesc; 300 | } 301 | 302 | /** 303 | * @} 304 | */ 305 | 306 | 307 | /** 308 | * @} 309 | */ 310 | 311 | 312 | /** 313 | * @} 314 | */ 315 | 316 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 317 | 318 | -------------------------------------------------------------------------------- /usb_conf/usbd_desc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_desc.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 19-September-2011 7 | * @brief header file for the usbd_desc.c file 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | 24 | #ifndef __USB_DESC_H 25 | #define __USB_DESC_H 26 | 27 | /* Includes ------------------------------------------------------------------*/ 28 | #include "usbd_def.h" 29 | 30 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 31 | * @{ 32 | */ 33 | 34 | /** @defgroup USB_DESC 35 | * @brief general defines for the usb device library file 36 | * @{ 37 | */ 38 | 39 | /** @defgroup USB_DESC_Exported_Defines 40 | * @{ 41 | */ 42 | #define USB_DEVICE_DESCRIPTOR_TYPE 0x01 43 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02 44 | #define USB_STRING_DESCRIPTOR_TYPE 0x03 45 | #define USB_INTERFACE_DESCRIPTOR_TYPE 0x04 46 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05 47 | #define USB_SIZ_DEVICE_DESC 18 48 | #define USB_SIZ_STRING_LANGID 4 49 | 50 | /** 51 | * @} 52 | */ 53 | 54 | 55 | /** @defgroup USBD_DESC_Exported_TypesDefinitions 56 | * @{ 57 | */ 58 | /** 59 | * @} 60 | */ 61 | 62 | 63 | 64 | /** @defgroup USBD_DESC_Exported_Macros 65 | * @{ 66 | */ 67 | /** 68 | * @} 69 | */ 70 | 71 | /** @defgroup USBD_DESC_Exported_Variables 72 | * @{ 73 | */ 74 | extern uint8_t USBD_DeviceDesc [USB_SIZ_DEVICE_DESC]; 75 | extern uint8_t USBD_StrDesc[USB_MAX_STR_DESC_SIZ]; 76 | extern uint8_t USBD_OtherSpeedCfgDesc[USB_LEN_CFG_DESC]; 77 | extern uint8_t USBD_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC]; 78 | extern uint8_t USBD_LangIDDesc[USB_SIZ_STRING_LANGID]; 79 | extern USBD_DEVICE USR_desc; 80 | /** 81 | * @} 82 | */ 83 | 84 | /** @defgroup USBD_DESC_Exported_FunctionsPrototype 85 | * @{ 86 | */ 87 | 88 | 89 | uint8_t * USBD_USR_DeviceDescriptor( uint8_t speed , uint16_t *length); 90 | uint8_t * USBD_USR_LangIDStrDescriptor( uint8_t speed , uint16_t *length); 91 | uint8_t * USBD_USR_ManufacturerStrDescriptor ( uint8_t speed , uint16_t *length); 92 | uint8_t * USBD_USR_ProductStrDescriptor ( uint8_t speed , uint16_t *length); 93 | uint8_t * USBD_USR_SerialStrDescriptor( uint8_t speed , uint16_t *length); 94 | uint8_t * USBD_USR_ConfigStrDescriptor( uint8_t speed , uint16_t *length); 95 | uint8_t * USBD_USR_InterfaceStrDescriptor( uint8_t speed , uint16_t *length); 96 | 97 | #ifdef USB_SUPPORT_USER_STRING_DESC 98 | uint8_t * USBD_USR_USRStringDesc (uint8_t speed, uint8_t idx , uint16_t *length); 99 | #endif /* USB_SUPPORT_USER_STRING_DESC */ 100 | 101 | /** 102 | * @} 103 | */ 104 | 105 | #endif /* __USBD_DESC_H */ 106 | 107 | /** 108 | * @} 109 | */ 110 | 111 | /** 112 | * @} 113 | */ 114 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 115 | -------------------------------------------------------------------------------- /usb_conf/usbd_usr.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_usr.c 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 19-September-2011 7 | * @brief This file includes the user application layer 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | #include "usbd_usr.h" 23 | #include "usbd_ioreq.h" 24 | 25 | USBD_Usr_cb_TypeDef USR_cb = 26 | { 27 | USBD_USR_Init, 28 | USBD_USR_DeviceReset, 29 | USBD_USR_DeviceConfigured, 30 | USBD_USR_DeviceSuspended, 31 | USBD_USR_DeviceResumed, 32 | 33 | USBD_USR_DeviceConnected, 34 | USBD_USR_DeviceDisconnected, 35 | }; 36 | 37 | 38 | /** 39 | * @brief USBD_USR_Init 40 | * Displays the message on LCD for host lib initialization 41 | * @param None 42 | * @retval None 43 | */ 44 | void USBD_USR_Init(void) 45 | { 46 | 47 | } 48 | 49 | /** 50 | * @brief USBD_USR_DeviceReset 51 | * Displays the message on LCD on device Reset Event 52 | * @param speed : device speed 53 | * @retval None 54 | */ 55 | void USBD_USR_DeviceReset(uint8_t speed ) 56 | { 57 | switch (speed) 58 | { 59 | case USB_OTG_SPEED_HIGH: 60 | break; 61 | 62 | case USB_OTG_SPEED_FULL: 63 | break; 64 | default: 65 | break; 66 | 67 | } 68 | } 69 | 70 | 71 | /** 72 | * @brief USBD_USR_DeviceConfigured 73 | * Displays the message on LCD on device configuration Event 74 | * @param None 75 | * @retval Staus 76 | */ 77 | void USBD_USR_DeviceConfigured (void) 78 | { 79 | } 80 | 81 | extern volatile uint8_t usb_connected; 82 | extern volatile uint8_t usb_suspended; 83 | 84 | /** 85 | * @brief USBD_USR_DeviceConnected 86 | * Displays the message on LCD on device connection Event 87 | * @param None 88 | * @retval Staus 89 | */ 90 | void USBD_USR_DeviceConnected (void) 91 | { 92 | usb_connected = 1; 93 | } 94 | 95 | 96 | /** 97 | * @brief USBD_USR_DeviceDisonnected 98 | * Displays the message on LCD on device disconnection Event 99 | * @param None 100 | * @retval Staus 101 | */ 102 | void USBD_USR_DeviceDisconnected (void) 103 | { 104 | usb_connected = 0; 105 | } 106 | 107 | /** 108 | * @brief USBD_USR_DeviceSuspended 109 | * Displays the message on LCD on device suspend Event 110 | * @param None 111 | * @retval None 112 | */ 113 | void USBD_USR_DeviceSuspended(void) 114 | { 115 | /* Users can do their application actions here for the USB-Reset */ 116 | usb_suspended = 1; 117 | } 118 | 119 | 120 | /** 121 | * @brief USBD_USR_DeviceResumed 122 | * Displays the message on LCD on device resume Event 123 | * @param None 124 | * @retval None 125 | */ 126 | void USBD_USR_DeviceResumed(void) 127 | { 128 | /* Users can do their application actions here for the USB-Reset */ 129 | usb_suspended = 0; 130 | } 131 | 132 | 133 | -------------------------------------------------------------------------------- /usb_lib/cdc/usbd_cdc_core.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_cdc_core.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief header file for the usbd_cdc_core.c file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Includes ------------------------------------------------------------------*/ 23 | 24 | #ifndef __USB_CDC_CORE_H_ 25 | #define __USB_CDC_CORE_H_ 26 | 27 | #include "usbd_ioreq.h" 28 | 29 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 30 | * @{ 31 | */ 32 | 33 | /** @defgroup usbd_cdc 34 | * @brief This file is the Header file for USBD_cdc.c 35 | * @{ 36 | */ 37 | 38 | 39 | /** @defgroup usbd_cdc_Exported_Defines 40 | * @{ 41 | */ 42 | #define USB_CDC_CONFIG_DESC_SIZ (67) 43 | #define USB_CDC_DESC_SIZ (67-9) 44 | 45 | #define CDC_DESCRIPTOR_TYPE 0x21 46 | 47 | #define DEVICE_CLASS_CDC 0x02 48 | #define DEVICE_SUBCLASS_CDC 0x00 49 | 50 | 51 | #define USB_DEVICE_DESCRIPTOR_TYPE 0x01 52 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02 53 | #define USB_STRING_DESCRIPTOR_TYPE 0x03 54 | #define USB_INTERFACE_DESCRIPTOR_TYPE 0x04 55 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05 56 | 57 | #define STANDARD_ENDPOINT_DESC_SIZE 0x09 58 | 59 | #define CDC_DATA_IN_PACKET_SIZE *(uint16_t *)(((USB_OTG_CORE_HANDLE *)pdev)->dev.pConfig_descriptor + 57) 60 | 61 | #define CDC_DATA_OUT_PACKET_SIZE *(uint16_t *)(((USB_OTG_CORE_HANDLE *)pdev)->dev.pConfig_descriptor + 64) 62 | 63 | /*---------------------------------------------------------------------*/ 64 | /* CDC definitions */ 65 | /*---------------------------------------------------------------------*/ 66 | 67 | /**************************************************/ 68 | /* CDC Requests */ 69 | /**************************************************/ 70 | #define SEND_ENCAPSULATED_COMMAND 0x00 71 | #define GET_ENCAPSULATED_RESPONSE 0x01 72 | #define SET_COMM_FEATURE 0x02 73 | #define GET_COMM_FEATURE 0x03 74 | #define CLEAR_COMM_FEATURE 0x04 75 | #define SET_LINE_CODING 0x20 76 | #define GET_LINE_CODING 0x21 77 | #define SET_CONTROL_LINE_STATE 0x22 78 | #define SEND_BREAK 0x23 79 | #define NO_CMD 0xFF 80 | 81 | /** 82 | * @} 83 | */ 84 | 85 | 86 | /** @defgroup USBD_CORE_Exported_TypesDefinitions 87 | * @{ 88 | */ 89 | typedef struct _CDC_IF_PROP 90 | { 91 | uint16_t (*pIf_Init) (void); 92 | uint16_t (*pIf_DeInit) (void); 93 | uint16_t (*pIf_Ctrl) (uint32_t Cmd, uint8_t* Buf, uint32_t Len); 94 | uint16_t (*pIf_DataTx) (uint8_t* Buf, uint32_t Len); 95 | uint16_t (*pIf_DataRx) (uint8_t* Buf, uint32_t Len); 96 | } 97 | CDC_IF_Prop_TypeDef; 98 | /** 99 | * @} 100 | */ 101 | 102 | 103 | 104 | /** @defgroup USBD_CORE_Exported_Macros 105 | * @{ 106 | */ 107 | 108 | /** 109 | * @} 110 | */ 111 | 112 | /** @defgroup USBD_CORE_Exported_Variables 113 | * @{ 114 | */ 115 | 116 | extern USBD_Class_cb_TypeDef USBD_CDC_cb; 117 | /** 118 | * @} 119 | */ 120 | 121 | /** @defgroup USB_CORE_Exported_Functions 122 | * @{ 123 | */ 124 | /** 125 | * @} 126 | */ 127 | 128 | #endif // __USB_CDC_CORE_H_ 129 | /** 130 | * @} 131 | */ 132 | 133 | /** 134 | * @} 135 | */ 136 | 137 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 138 | -------------------------------------------------------------------------------- /usb_lib/cdc/usbd_cdc_vcp.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_cdc_vcp.c 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief Generic media access Layer. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED 23 | #pragma data_alignment = 4 24 | #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usbd_cdc_vcp.h" 28 | #include "stm32f4xx_conf.h" 29 | #include "stm32f4xx_usart.h" 30 | 31 | /* Private variables ---------------------------------------------------------*/ 32 | LINE_CODING linecoding = { 33 | 115200, /* baud rate*/ 34 | 0x00, /* stop bits-1*/ 35 | 0x00, /* parity - none*/ 36 | 0x08 /* nb. of bits 8*/ 37 | }; 38 | 39 | USART_InitTypeDef USART_InitStructure; 40 | 41 | /* These are external variables imported from CDC core to be used for IN 42 | transfer management. */ 43 | extern uint8_t APP_Rx_Buffer[]; /* Write CDC received data in this buffer. 44 | These data will be sent over USB IN endpoint 45 | in the CDC core functions. */ 46 | extern uint32_t APP_Rx_ptr_in; /* Increment this pointer or roll it back to 47 | start address when writing received data 48 | in the buffer APP_Rx_Buffer. */ 49 | 50 | /* Private function prototypes -----------------------------------------------*/ 51 | static uint16_t VCP_Init(void); 52 | static uint16_t VCP_DeInit(void); 53 | static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len); 54 | static uint16_t VCP_DataTx(uint8_t* Buf, uint32_t Len); 55 | static uint16_t VCP_DataRx(uint8_t* Buf, uint32_t Len); 56 | 57 | CDC_IF_Prop_TypeDef VCP_fops = { VCP_Init, VCP_DeInit, VCP_Ctrl, VCP_DataTx, 58 | VCP_DataRx }; 59 | 60 | /* Private functions ---------------------------------------------------------*/ 61 | /** 62 | * @brief VCP_Init 63 | * Initializes the Media on the STM32 64 | * @param None 65 | * @retval Result of the opeartion (USBD_OK in all cases) 66 | */ 67 | static uint16_t VCP_Init(void) { 68 | return USBD_OK; 69 | } 70 | 71 | /** 72 | * @brief VCP_DeInit 73 | * DeInitializes the Media on the STM32 74 | * @param None 75 | * @retval Result of the opeartion (USBD_OK in all cases) 76 | */ 77 | static uint16_t VCP_DeInit(void) { 78 | return USBD_OK; 79 | } 80 | 81 | /** 82 | * @brief VCP_Ctrl 83 | * Manage the CDC class requests 84 | * @param Cmd: Command code 85 | * @param Buf: Buffer containing command data (request parameters) 86 | * @param Len: Number of data to be sent (in bytes) 87 | * @retval Result of the opeartion (USBD_OK in all cases) 88 | */ 89 | static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len) { 90 | switch (Cmd) { 91 | case SEND_ENCAPSULATED_COMMAND: 92 | /* Not needed for this driver */ 93 | break; 94 | 95 | case GET_ENCAPSULATED_RESPONSE: 96 | /* Not needed for this driver */ 97 | break; 98 | 99 | case SET_COMM_FEATURE: 100 | /* Not needed for this driver */ 101 | break; 102 | 103 | case GET_COMM_FEATURE: 104 | /* Not needed for this driver */ 105 | break; 106 | 107 | case CLEAR_COMM_FEATURE: 108 | /* Not needed for this driver */ 109 | break; 110 | 111 | case SET_LINE_CODING: 112 | /* Not needed for this driver */ 113 | break; 114 | 115 | case GET_LINE_CODING: 116 | Buf[0] = (uint8_t) (linecoding.bitrate); 117 | Buf[1] = (uint8_t) (linecoding.bitrate >> 8); 118 | Buf[2] = (uint8_t) (linecoding.bitrate >> 16); 119 | Buf[3] = (uint8_t) (linecoding.bitrate >> 24); 120 | Buf[4] = linecoding.format; 121 | Buf[5] = linecoding.paritytype; 122 | Buf[6] = linecoding.datatype; 123 | break; 124 | 125 | case SET_CONTROL_LINE_STATE: 126 | /* Not needed for this driver */ 127 | break; 128 | 129 | case SEND_BREAK: 130 | /* Not needed for this driver */ 131 | break; 132 | 133 | default: 134 | break; 135 | } 136 | 137 | return USBD_OK; 138 | } 139 | 140 | /** 141 | * @brief putchar 142 | * Sends one char over the USB serial link. 143 | * @param buf: char to be sent 144 | * @retval none 145 | */ 146 | 147 | void VCP_put_char(uint8_t buf) { 148 | VCP_DataTx(&buf, 1); 149 | } 150 | 151 | void VCP_send_str(uint8_t* buf) { 152 | uint32_t i = 0; 153 | while (*(buf + i)) { 154 | i++; 155 | } 156 | VCP_DataTx(buf, i); 157 | } 158 | 159 | void VCP_send_buffer(uint8_t* buf, int len) { 160 | VCP_DataTx(buf, len); 161 | } 162 | 163 | /** 164 | * @brief VCP_DataTx 165 | * CDC received data to be send over USB IN endpoint are managed in 166 | * this function. 167 | * @param Buf: Buffer of data to be sent 168 | * @param Len: Number of data to be sent (in bytes) 169 | * @retval Result of the opeartion: USBD_OK if all operations are OK else VCP_FAIL 170 | */ 171 | static uint16_t VCP_DataTx(uint8_t* Buf, uint32_t Len) { 172 | uint32_t i = 0; 173 | while (i < Len) { 174 | APP_Rx_Buffer[APP_Rx_ptr_in] = *(Buf + i); 175 | APP_Rx_ptr_in++; 176 | i++; 177 | /* To avoid buffer overflow */ 178 | if (APP_Rx_ptr_in == APP_RX_DATA_SIZE) { 179 | APP_Rx_ptr_in = 0; 180 | } 181 | } 182 | 183 | return USBD_OK; 184 | } 185 | 186 | /** 187 | * @brief VCP_DataRx 188 | * Data received over USB OUT endpoint are sent over CDC interface 189 | * through this function. 190 | * 191 | * @note 192 | * This function will block any OUT packet reception on USB endpoint 193 | * until exiting this function. If you exit this function before transfer 194 | * is complete on CDC interface (ie. using DMA controller) it will result 195 | * in receiving more data while previous ones are still not sent. 196 | * 197 | * @param Buf: Buffer of data to be received 198 | * @param Len: Number of data received (in bytes) 199 | * @retval Result of the opeartion: USBD_OK if all operations are OK else VCP_FAIL 200 | */ 201 | 202 | #define APP_TX_BUF_SIZE 128 203 | uint8_t APP_Tx_Buffer[APP_TX_BUF_SIZE]; 204 | uint32_t APP_tx_ptr_head; 205 | uint32_t APP_tx_ptr_tail; 206 | 207 | static uint16_t VCP_DataRx(uint8_t* Buf, uint32_t Len) { 208 | uint32_t i; 209 | 210 | for (i = 0; i < Len; i++) { 211 | APP_Tx_Buffer[APP_tx_ptr_head] = *(Buf + i); 212 | APP_tx_ptr_head++; 213 | if (APP_tx_ptr_head == APP_TX_BUF_SIZE) 214 | APP_tx_ptr_head = 0; 215 | 216 | if (APP_tx_ptr_head == APP_tx_ptr_tail) 217 | return USBD_FAIL; 218 | } 219 | 220 | return USBD_OK; 221 | } 222 | 223 | int VCP_get_char(uint8_t *buf) { 224 | if (APP_tx_ptr_head == APP_tx_ptr_tail) 225 | return 0; 226 | 227 | *buf = APP_Tx_Buffer[APP_tx_ptr_tail]; 228 | APP_tx_ptr_tail++; 229 | if (APP_tx_ptr_tail == APP_TX_BUF_SIZE) 230 | APP_tx_ptr_tail = 0; 231 | 232 | return 1; 233 | } 234 | 235 | int VCP_get_string(uint8_t *buf) { 236 | if (APP_tx_ptr_head == APP_tx_ptr_tail) 237 | return 0; 238 | 239 | while (!APP_Tx_Buffer[APP_tx_ptr_tail] 240 | || APP_Tx_Buffer[APP_tx_ptr_tail] == '\n' 241 | || APP_Tx_Buffer[APP_tx_ptr_tail] == '\r') { 242 | APP_tx_ptr_tail++; 243 | if (APP_tx_ptr_tail == APP_TX_BUF_SIZE) 244 | APP_tx_ptr_tail = 0; 245 | if (APP_tx_ptr_head == APP_tx_ptr_tail) 246 | return 0; 247 | } 248 | 249 | int i = 0; 250 | do { 251 | *(buf + i) = APP_Tx_Buffer[i + APP_tx_ptr_tail]; 252 | i++; 253 | 254 | if ((APP_tx_ptr_tail + i) == APP_TX_BUF_SIZE) 255 | i = -APP_tx_ptr_tail; 256 | if (APP_tx_ptr_head == (APP_tx_ptr_tail + i)) 257 | return 0; 258 | 259 | } while (APP_Tx_Buffer[APP_tx_ptr_tail + i] 260 | && APP_Tx_Buffer[APP_tx_ptr_tail + i] != '\n' 261 | && APP_Tx_Buffer[APP_tx_ptr_tail + i] != '\r'); 262 | 263 | *(buf + i) = 0; 264 | APP_tx_ptr_tail += i; 265 | if (APP_tx_ptr_tail >= APP_TX_BUF_SIZE) 266 | APP_tx_ptr_tail -= APP_TX_BUF_SIZE; 267 | return i; 268 | } 269 | 270 | /** 271 | * @brief EVAL_COM_IRQHandler 272 | * 273 | * @param None. 274 | * @retval None. 275 | */ 276 | void EVAL_COM_IRQHandler(void) { 277 | 278 | } 279 | 280 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 281 | -------------------------------------------------------------------------------- /usb_lib/cdc/usbd_cdc_vcp.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_cdc_vcp.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief Header for usbd_cdc_vcp.c file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __USBD_CDC_VCP_H 24 | #define __USBD_CDC_VCP_H 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "stm32f4xx_conf.h" 28 | 29 | #include "usbd_cdc_core.h" 30 | #include "usbd_conf.h" 31 | #include 32 | 33 | /* Exported typef ------------------------------------------------------------*/ 34 | /* The following structures groups all needed parameters to be configured for the 35 | ComPort. These parameters can modified on the fly by the host through CDC class 36 | command class requests. */ 37 | typedef struct 38 | { 39 | uint32_t bitrate; 40 | uint8_t format; 41 | uint8_t paritytype; 42 | uint8_t datatype; 43 | }LINE_CODING; 44 | 45 | /* Exported constants --------------------------------------------------------*/ 46 | /* The following define is used to route the USART IRQ handler to be used. 47 | The IRQ handler function is implemented in the usbd_cdc_vcp.c file. */ 48 | #ifdef USE_STM322xG_EVAL 49 | #define EVAL_COM_IRQHandler USART3_IRQHandler 50 | #elif defined(USE_STM3210C_EVAL) 51 | #define EVAL_COM_IRQHandler USART2_IRQHandler 52 | #endif /* USE_STM322xG_EVAL */ 53 | 54 | void VCP_put_char(uint8_t buf); 55 | void VCP_send_str(uint8_t* buf); 56 | int VCP_get_char(uint8_t *buf); 57 | int VCP_get_string(uint8_t *buf); 58 | void VCP_send_buffer(uint8_t* buf, int len); 59 | 60 | #define DEFAULT_CONFIG 0 61 | #define OTHER_CONFIG 1 62 | 63 | /* Exported macro ------------------------------------------------------------*/ 64 | /* Exported functions ------------------------------------------------------- */ 65 | 66 | #endif /* __USBD_CDC_VCP_H */ 67 | 68 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 69 | -------------------------------------------------------------------------------- /usb_lib/core/usbd_core.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_core.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief Header file for usbd_core.c 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __USBD_CORE_H 24 | #define __USBD_CORE_H 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usb_dcd.h" 28 | #include "usbd_def.h" 29 | #include "usbd_conf.h" 30 | 31 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 32 | * @{ 33 | */ 34 | 35 | /** @defgroup USBD_CORE 36 | * @brief This file is the Header file for usbd_core.c file 37 | * @{ 38 | */ 39 | 40 | 41 | /** @defgroup USBD_CORE_Exported_Defines 42 | * @{ 43 | */ 44 | 45 | typedef enum { 46 | USBD_OK = 0, 47 | USBD_BUSY, 48 | USBD_FAIL, 49 | }USBD_Status; 50 | /** 51 | * @} 52 | */ 53 | 54 | 55 | /** @defgroup USBD_CORE_Exported_TypesDefinitions 56 | * @{ 57 | */ 58 | 59 | 60 | /** 61 | * @} 62 | */ 63 | 64 | 65 | 66 | /** @defgroup USBD_CORE_Exported_Macros 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup USBD_CORE_Exported_Variables 75 | * @{ 76 | */ 77 | 78 | /** 79 | * @} 80 | */ 81 | 82 | /** @defgroup USBD_CORE_Exported_FunctionsPrototype 83 | * @{ 84 | */ 85 | void USBD_Init(USB_OTG_CORE_HANDLE *pdev, 86 | USB_OTG_CORE_ID_TypeDef coreID, 87 | USBD_DEVICE *pDevice, 88 | USBD_Class_cb_TypeDef *class_cb, 89 | USBD_Usr_cb_TypeDef *usr_cb); 90 | 91 | USBD_Status USBD_DeInit(USB_OTG_CORE_HANDLE *pdev); 92 | 93 | USBD_Status USBD_ClrCfg(USB_OTG_CORE_HANDLE *pdev, uint8_t cfgidx); 94 | 95 | USBD_Status USBD_SetCfg(USB_OTG_CORE_HANDLE *pdev, uint8_t cfgidx); 96 | 97 | /** 98 | * @} 99 | */ 100 | 101 | #endif /* __USBD_CORE_H */ 102 | 103 | /** 104 | * @} 105 | */ 106 | 107 | /** 108 | * @} 109 | */ 110 | 111 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /usb_lib/core/usbd_def.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_def.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief general defines for the usb device library 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | 24 | #ifndef __USBD_DEF_H 25 | #define __USBD_DEF_H 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usbd_conf.h" 28 | 29 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 30 | * @{ 31 | */ 32 | 33 | /** @defgroup USB_DEF 34 | * @brief general defines for the usb device library file 35 | * @{ 36 | */ 37 | 38 | /** @defgroup USB_DEF_Exported_Defines 39 | * @{ 40 | */ 41 | 42 | #ifndef NULL 43 | #define NULL 0 44 | #endif 45 | 46 | #define USB_LEN_DEV_QUALIFIER_DESC 0x0A 47 | #define USB_LEN_DEV_DESC 0x12 48 | #define USB_LEN_CFG_DESC 0x09 49 | #define USB_LEN_IF_DESC 0x09 50 | #define USB_LEN_EP_DESC 0x07 51 | #define USB_LEN_OTG_DESC 0x03 52 | 53 | #define USBD_IDX_LANGID_STR 0x00 54 | #define USBD_IDX_MFC_STR 0x01 55 | #define USBD_IDX_PRODUCT_STR 0x02 56 | #define USBD_IDX_SERIAL_STR 0x03 57 | #define USBD_IDX_CONFIG_STR 0x04 58 | #define USBD_IDX_INTERFACE_STR 0x05 59 | 60 | #define USB_REQ_TYPE_STANDARD 0x00 61 | #define USB_REQ_TYPE_CLASS 0x20 62 | #define USB_REQ_TYPE_VENDOR 0x40 63 | #define USB_REQ_TYPE_MASK 0x60 64 | 65 | #define USB_REQ_RECIPIENT_DEVICE 0x00 66 | #define USB_REQ_RECIPIENT_INTERFACE 0x01 67 | #define USB_REQ_RECIPIENT_ENDPOINT 0x02 68 | #define USB_REQ_RECIPIENT_MASK 0x03 69 | 70 | #define USB_REQ_GET_STATUS 0x00 71 | #define USB_REQ_CLEAR_FEATURE 0x01 72 | #define USB_REQ_SET_FEATURE 0x03 73 | #define USB_REQ_SET_ADDRESS 0x05 74 | #define USB_REQ_GET_DESCRIPTOR 0x06 75 | #define USB_REQ_SET_DESCRIPTOR 0x07 76 | #define USB_REQ_GET_CONFIGURATION 0x08 77 | #define USB_REQ_SET_CONFIGURATION 0x09 78 | #define USB_REQ_GET_INTERFACE 0x0A 79 | #define USB_REQ_SET_INTERFACE 0x0B 80 | #define USB_REQ_SYNCH_FRAME 0x0C 81 | 82 | #define USB_DESC_TYPE_DEVICE 1 83 | #define USB_DESC_TYPE_CONFIGURATION 2 84 | #define USB_DESC_TYPE_STRING 3 85 | #define USB_DESC_TYPE_INTERFACE 4 86 | #define USB_DESC_TYPE_ENDPOINT 5 87 | #define USB_DESC_TYPE_DEVICE_QUALIFIER 6 88 | #define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 7 89 | 90 | 91 | #define USB_CONFIG_REMOTE_WAKEUP 2 92 | #define USB_CONFIG_SELF_POWERED 1 93 | 94 | #define USB_FEATURE_EP_HALT 0 95 | #define USB_FEATURE_REMOTE_WAKEUP 1 96 | #define USB_FEATURE_TEST_MODE 2 97 | 98 | /** 99 | * @} 100 | */ 101 | 102 | 103 | /** @defgroup USBD_DEF_Exported_TypesDefinitions 104 | * @{ 105 | */ 106 | /** 107 | * @} 108 | */ 109 | 110 | 111 | 112 | /** @defgroup USBD_DEF_Exported_Macros 113 | * @{ 114 | */ 115 | #define SWAPBYTE(addr) (((uint16_t)(*((uint8_t *)(addr)))) + \ 116 | (((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8)) 117 | 118 | #define LOBYTE(x) ((uint8_t)(x & 0x00FF)) 119 | #define HIBYTE(x) ((uint8_t)((x & 0xFF00) >>8)) 120 | /** 121 | * @} 122 | */ 123 | 124 | /** @defgroup USBD_DEF_Exported_Variables 125 | * @{ 126 | */ 127 | 128 | /** 129 | * @} 130 | */ 131 | 132 | /** @defgroup USBD_DEF_Exported_FunctionsPrototype 133 | * @{ 134 | */ 135 | 136 | /** 137 | * @} 138 | */ 139 | 140 | #endif /* __USBD_DEF_H */ 141 | 142 | /** 143 | * @} 144 | */ 145 | 146 | /** 147 | * @} 148 | */ 149 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 150 | -------------------------------------------------------------------------------- /usb_lib/core/usbd_ioreq.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_ioreq.c 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief This file provides the IO requests APIs for control endpoints. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Includes ------------------------------------------------------------------*/ 23 | #include "usbd_ioreq.h" 24 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 25 | * @{ 26 | */ 27 | 28 | 29 | /** @defgroup USBD_IOREQ 30 | * @brief control I/O requests module 31 | * @{ 32 | */ 33 | 34 | /** @defgroup USBD_IOREQ_Private_TypesDefinitions 35 | * @{ 36 | */ 37 | /** 38 | * @} 39 | */ 40 | 41 | 42 | /** @defgroup USBD_IOREQ_Private_Defines 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | 51 | /** @defgroup USBD_IOREQ_Private_Macros 52 | * @{ 53 | */ 54 | /** 55 | * @} 56 | */ 57 | 58 | 59 | /** @defgroup USBD_IOREQ_Private_Variables 60 | * @{ 61 | */ 62 | 63 | /** 64 | * @} 65 | */ 66 | 67 | 68 | /** @defgroup USBD_IOREQ_Private_FunctionPrototypes 69 | * @{ 70 | */ 71 | /** 72 | * @} 73 | */ 74 | 75 | 76 | /** @defgroup USBD_IOREQ_Private_Functions 77 | * @{ 78 | */ 79 | 80 | /** 81 | * @brief USBD_CtlSendData 82 | * send data on the ctl pipe 83 | * @param pdev: device instance 84 | * @param buff: pointer to data buffer 85 | * @param len: length of data to be sent 86 | * @retval status 87 | */ 88 | USBD_Status USBD_CtlSendData (USB_OTG_CORE_HANDLE *pdev, 89 | uint8_t *pbuf, 90 | uint16_t len) 91 | { 92 | USBD_Status ret = USBD_OK; 93 | 94 | pdev->dev.in_ep[0].total_data_len = len; 95 | pdev->dev.in_ep[0].rem_data_len = len; 96 | pdev->dev.device_state = USB_OTG_EP0_DATA_IN; 97 | 98 | DCD_EP_Tx (pdev, 0, pbuf, len); 99 | 100 | return ret; 101 | } 102 | 103 | /** 104 | * @brief USBD_CtlContinueSendData 105 | * continue sending data on the ctl pipe 106 | * @param pdev: device instance 107 | * @param buff: pointer to data buffer 108 | * @param len: length of data to be sent 109 | * @retval status 110 | */ 111 | USBD_Status USBD_CtlContinueSendData (USB_OTG_CORE_HANDLE *pdev, 112 | uint8_t *pbuf, 113 | uint16_t len) 114 | { 115 | USBD_Status ret = USBD_OK; 116 | 117 | DCD_EP_Tx (pdev, 0, pbuf, len); 118 | 119 | 120 | return ret; 121 | } 122 | 123 | /** 124 | * @brief USBD_CtlPrepareRx 125 | * receive data on the ctl pipe 126 | * @param pdev: USB OTG device instance 127 | * @param buff: pointer to data buffer 128 | * @param len: length of data to be received 129 | * @retval status 130 | */ 131 | USBD_Status USBD_CtlPrepareRx (USB_OTG_CORE_HANDLE *pdev, 132 | uint8_t *pbuf, 133 | uint16_t len) 134 | { 135 | USBD_Status ret = USBD_OK; 136 | 137 | pdev->dev.out_ep[0].total_data_len = len; 138 | pdev->dev.out_ep[0].rem_data_len = len; 139 | pdev->dev.device_state = USB_OTG_EP0_DATA_OUT; 140 | 141 | DCD_EP_PrepareRx (pdev, 142 | 0, 143 | pbuf, 144 | len); 145 | 146 | 147 | return ret; 148 | } 149 | 150 | /** 151 | * @brief USBD_CtlContinueRx 152 | * continue receive data on the ctl pipe 153 | * @param pdev: USB OTG device instance 154 | * @param buff: pointer to data buffer 155 | * @param len: length of data to be received 156 | * @retval status 157 | */ 158 | USBD_Status USBD_CtlContinueRx (USB_OTG_CORE_HANDLE *pdev, 159 | uint8_t *pbuf, 160 | uint16_t len) 161 | { 162 | USBD_Status ret = USBD_OK; 163 | 164 | DCD_EP_PrepareRx (pdev, 165 | 0, 166 | pbuf, 167 | len); 168 | return ret; 169 | } 170 | /** 171 | * @brief USBD_CtlSendStatus 172 | * send zero lzngth packet on the ctl pipe 173 | * @param pdev: USB OTG device instance 174 | * @retval status 175 | */ 176 | USBD_Status USBD_CtlSendStatus (USB_OTG_CORE_HANDLE *pdev) 177 | { 178 | USBD_Status ret = USBD_OK; 179 | pdev->dev.device_state = USB_OTG_EP0_STATUS_IN; 180 | DCD_EP_Tx (pdev, 181 | 0, 182 | NULL, 183 | 0); 184 | 185 | USB_OTG_EP0_OutStart(pdev); 186 | 187 | return ret; 188 | } 189 | 190 | /** 191 | * @brief USBD_CtlReceiveStatus 192 | * receive zero lzngth packet on the ctl pipe 193 | * @param pdev: USB OTG device instance 194 | * @retval status 195 | */ 196 | USBD_Status USBD_CtlReceiveStatus (USB_OTG_CORE_HANDLE *pdev) 197 | { 198 | USBD_Status ret = USBD_OK; 199 | pdev->dev.device_state = USB_OTG_EP0_STATUS_OUT; 200 | DCD_EP_PrepareRx ( pdev, 201 | 0, 202 | NULL, 203 | 0); 204 | 205 | USB_OTG_EP0_OutStart(pdev); 206 | 207 | return ret; 208 | } 209 | 210 | 211 | /** 212 | * @brief USBD_GetRxCount 213 | * returns the received data length 214 | * @param pdev: USB OTG device instance 215 | * epnum: endpoint index 216 | * @retval Rx Data blength 217 | */ 218 | uint16_t USBD_GetRxCount (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum) 219 | { 220 | return pdev->dev.out_ep[epnum].xfer_count; 221 | } 222 | 223 | /** 224 | * @} 225 | */ 226 | 227 | 228 | /** 229 | * @} 230 | */ 231 | 232 | 233 | /** 234 | * @} 235 | */ 236 | 237 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 238 | -------------------------------------------------------------------------------- /usb_lib/core/usbd_ioreq.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_ioreq.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief header file for the usbd_ioreq.c file 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | 24 | #ifndef __USBD_IOREQ_H_ 25 | #define __USBD_IOREQ_H_ 26 | 27 | /* Includes ------------------------------------------------------------------*/ 28 | #include "usbd_def.h" 29 | #include "usbd_core.h" 30 | 31 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 32 | * @{ 33 | */ 34 | 35 | /** @defgroup USBD_IOREQ 36 | * @brief header file for the usbd_ioreq.c file 37 | * @{ 38 | */ 39 | 40 | /** @defgroup USBD_IOREQ_Exported_Defines 41 | * @{ 42 | */ 43 | /** 44 | * @} 45 | */ 46 | 47 | 48 | /** @defgroup USBD_IOREQ_Exported_Types 49 | * @{ 50 | */ 51 | 52 | 53 | /** 54 | * @} 55 | */ 56 | 57 | 58 | 59 | /** @defgroup USBD_IOREQ_Exported_Macros 60 | * @{ 61 | */ 62 | 63 | /** 64 | * @} 65 | */ 66 | 67 | /** @defgroup USBD_IOREQ_Exported_Variables 68 | * @{ 69 | */ 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @defgroup USBD_IOREQ_Exported_FunctionsPrototype 76 | * @{ 77 | */ 78 | 79 | USBD_Status USBD_CtlSendData (USB_OTG_CORE_HANDLE *pdev, 80 | uint8_t *buf, 81 | uint16_t len); 82 | 83 | USBD_Status USBD_CtlContinueSendData (USB_OTG_CORE_HANDLE *pdev, 84 | uint8_t *pbuf, 85 | uint16_t len); 86 | 87 | USBD_Status USBD_CtlPrepareRx (USB_OTG_CORE_HANDLE *pdev, 88 | uint8_t *pbuf, 89 | uint16_t len); 90 | 91 | USBD_Status USBD_CtlContinueRx (USB_OTG_CORE_HANDLE *pdev, 92 | uint8_t *pbuf, 93 | uint16_t len); 94 | 95 | USBD_Status USBD_CtlSendStatus (USB_OTG_CORE_HANDLE *pdev); 96 | 97 | USBD_Status USBD_CtlReceiveStatus (USB_OTG_CORE_HANDLE *pdev); 98 | 99 | uint16_t USBD_GetRxCount (USB_OTG_CORE_HANDLE *pdev , 100 | uint8_t epnum); 101 | 102 | /** 103 | * @} 104 | */ 105 | 106 | #endif /* __USBD_IOREQ_H_ */ 107 | 108 | /** 109 | * @} 110 | */ 111 | 112 | /** 113 | * @} 114 | */ 115 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 116 | -------------------------------------------------------------------------------- /usb_lib/core/usbd_req.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_req.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief header file for the usbd_req.c file 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | 24 | #ifndef __USB_REQUEST_H_ 25 | #define __USB_REQUEST_H_ 26 | 27 | /* Includes ------------------------------------------------------------------*/ 28 | #include "usbd_def.h" 29 | #include "usbd_core.h" 30 | #include "usbd_conf.h" 31 | 32 | 33 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 34 | * @{ 35 | */ 36 | 37 | /** @defgroup USBD_REQ 38 | * @brief header file for the usbd_ioreq.c file 39 | * @{ 40 | */ 41 | 42 | /** @defgroup USBD_REQ_Exported_Defines 43 | * @{ 44 | */ 45 | /** 46 | * @} 47 | */ 48 | 49 | 50 | /** @defgroup USBD_REQ_Exported_Types 51 | * @{ 52 | */ 53 | /** 54 | * @} 55 | */ 56 | 57 | 58 | 59 | /** @defgroup USBD_REQ_Exported_Macros 60 | * @{ 61 | */ 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @defgroup USBD_REQ_Exported_Variables 67 | * @{ 68 | */ 69 | /** 70 | * @} 71 | */ 72 | 73 | /** @defgroup USBD_REQ_Exported_FunctionsPrototype 74 | * @{ 75 | */ 76 | 77 | USBD_Status USBD_StdDevReq (USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req); 78 | USBD_Status USBD_StdItfReq (USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req); 79 | USBD_Status USBD_StdEPReq (USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req); 80 | void USBD_ParseSetupRequest( USB_OTG_CORE_HANDLE *pdev, 81 | USB_SETUP_REQ *req); 82 | 83 | void USBD_CtlError( USB_OTG_CORE_HANDLE *pdev, 84 | USB_SETUP_REQ *req); 85 | 86 | void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len); 87 | /** 88 | * @} 89 | */ 90 | 91 | #endif /* __USB_REQUEST_H_ */ 92 | 93 | /** 94 | * @} 95 | */ 96 | 97 | /** 98 | * @} 99 | */ 100 | 101 | 102 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 103 | -------------------------------------------------------------------------------- /usb_lib/core/usbd_usr.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_usr.h 4 | * @author MCD Application Team 5 | * @version V1.0.0 6 | * @date 22-July-2011 7 | * @brief Header file for usbd_usr.c 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __USBD_USR_H__ 24 | #define __USBD_USR_H__ 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usbd_core.h" 28 | 29 | 30 | /** @addtogroup USBD_USER 31 | * @{ 32 | */ 33 | 34 | /** @addtogroup USBD_MSC_DEMO_USER_CALLBACKS 35 | * @{ 36 | */ 37 | 38 | /** @defgroup USBD_USR 39 | * @brief This file is the Header file for usbd_usr.c 40 | * @{ 41 | */ 42 | 43 | 44 | /** @defgroup USBD_USR_Exported_Types 45 | * @{ 46 | */ 47 | 48 | extern USBD_Usr_cb_TypeDef USR_cb; 49 | extern USBD_Usr_cb_TypeDef USR_FS_cb; 50 | extern USBD_Usr_cb_TypeDef USR_HS_cb; 51 | 52 | 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | 59 | 60 | /** @defgroup USBD_USR_Exported_Defines 61 | * @{ 62 | */ 63 | 64 | /** 65 | * @} 66 | */ 67 | 68 | /** @defgroup USBD_USR_Exported_Macros 69 | * @{ 70 | */ 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @defgroup USBD_USR_Exported_Variables 76 | * @{ 77 | */ 78 | 79 | void USBD_USR_Init(void); 80 | void USBD_USR_DeviceReset (uint8_t speed); 81 | void USBD_USR_DeviceConfigured (void); 82 | void USBD_USR_DeviceSuspended(void); 83 | void USBD_USR_DeviceResumed(void); 84 | 85 | void USBD_USR_DeviceConnected(void); 86 | void USBD_USR_DeviceDisconnected(void); 87 | 88 | void USBD_USR_FS_Init(void); 89 | void USBD_USR_FS_DeviceReset (uint8_t speed); 90 | void USBD_USR_FS_DeviceConfigured (void); 91 | void USBD_USR_FS_DeviceSuspended(void); 92 | void USBD_USR_FS_DeviceResumed(void); 93 | 94 | void USBD_USR_FS_DeviceConnected(void); 95 | void USBD_USR_FS_DeviceDisconnected(void); 96 | 97 | void USBD_USR_HS_Init(void); 98 | void USBD_USR_HS_DeviceReset (uint8_t speed); 99 | void USBD_USR_HS_DeviceConfigured (void); 100 | void USBD_USR_HS_DeviceSuspended(void); 101 | void USBD_USR_HS_DeviceResumed(void); 102 | 103 | void USBD_USR_HS_DeviceConnected(void); 104 | void USBD_USR_HS_DeviceDisconnected(void); 105 | 106 | /** 107 | * @} 108 | */ 109 | 110 | /** @defgroup USBD_USR_Exported_FunctionsPrototype 111 | * @{ 112 | */ 113 | /** 114 | * @} 115 | */ 116 | 117 | #endif /*__USBD_USR_H__*/ 118 | 119 | /** 120 | * @} 121 | */ 122 | 123 | /** 124 | * @} 125 | */ 126 | 127 | /** 128 | * @} 129 | */ 130 | 131 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /usb_lib/otg/usb_dcd.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_dcd.c 4 | * @author MCD Application Team 5 | * @version V2.0.0 6 | * @date 22-July-2011 7 | * @brief Peripheral Device Interface Layer 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | #include "usb_conf.h" 23 | #ifdef USE_DEVICE_MODE 24 | /* Includes ------------------------------------------------------------------*/ 25 | #include "usb_dcd.h" 26 | #include "usb_bsp.h" 27 | 28 | 29 | /** @addtogroup USB_OTG_DRIVER 30 | * @{ 31 | */ 32 | 33 | /** @defgroup USB_DCD 34 | * @brief This file is the interface between EFSL ans Host mass-storage class 35 | * @{ 36 | */ 37 | 38 | 39 | /** @defgroup USB_DCD_Private_Defines 40 | * @{ 41 | */ 42 | /** 43 | * @} 44 | */ 45 | 46 | 47 | /** @defgroup USB_DCD_Private_TypesDefinitions 48 | * @{ 49 | */ 50 | /** 51 | * @} 52 | */ 53 | 54 | 55 | 56 | /** @defgroup USB_DCD_Private_Macros 57 | * @{ 58 | */ 59 | /** 60 | * @} 61 | */ 62 | 63 | 64 | /** @defgroup USB_DCD_Private_Variables 65 | * @{ 66 | */ 67 | /** 68 | * @} 69 | */ 70 | 71 | 72 | /** @defgroup USB_DCD_Private_FunctionPrototypes 73 | * @{ 74 | */ 75 | 76 | /** 77 | * @} 78 | */ 79 | 80 | 81 | /** @defgroup USB_DCD_Private_Functions 82 | * @{ 83 | */ 84 | 85 | 86 | 87 | void DCD_Init(USB_OTG_CORE_HANDLE *pdev , 88 | USB_OTG_CORE_ID_TypeDef coreID) 89 | { 90 | uint32_t i; 91 | USB_OTG_EP *ep; 92 | 93 | USB_OTG_SelectCore (pdev , coreID); 94 | 95 | pdev->dev.device_status = USB_OTG_DEFAULT; 96 | pdev->dev.device_address = 0; 97 | 98 | /* Init ep structure */ 99 | for (i = 0; i < pdev->cfg.dev_endpoints ; i++) 100 | { 101 | ep = &pdev->dev.in_ep[i]; 102 | /* Init ep structure */ 103 | ep->is_in = 1; 104 | ep->num = i; 105 | ep->tx_fifo_num = i; 106 | /* Control until ep is actvated */ 107 | ep->type = EP_TYPE_CTRL; 108 | ep->maxpacket = USB_OTG_MAX_EP0_SIZE; 109 | ep->xfer_buff = 0; 110 | ep->xfer_len = 0; 111 | } 112 | 113 | for (i = 0; i < pdev->cfg.dev_endpoints; i++) 114 | { 115 | ep = &pdev->dev.out_ep[i]; 116 | /* Init ep structure */ 117 | ep->is_in = 0; 118 | ep->num = i; 119 | ep->tx_fifo_num = i; 120 | /* Control until ep is activated */ 121 | ep->type = EP_TYPE_CTRL; 122 | ep->maxpacket = USB_OTG_MAX_EP0_SIZE; 123 | ep->xfer_buff = 0; 124 | ep->xfer_len = 0; 125 | } 126 | 127 | USB_OTG_DisableGlobalInt(pdev); 128 | 129 | /*Init the Core (common init.) */ 130 | USB_OTG_CoreInit(pdev); 131 | 132 | 133 | /* Force Device Mode*/ 134 | USB_OTG_SetCurrentMode(pdev, DEVICE_MODE); 135 | 136 | /* Init Device */ 137 | USB_OTG_CoreInitDev(pdev); 138 | 139 | 140 | /* Enable USB Global interrupt */ 141 | USB_OTG_EnableGlobalInt(pdev); 142 | } 143 | 144 | 145 | /** 146 | * @brief Configure an EP 147 | * @param pdev : Device instance 148 | * @param epdesc : Endpoint Descriptor 149 | * @retval : status 150 | */ 151 | uint32_t DCD_EP_Open(USB_OTG_CORE_HANDLE *pdev , 152 | uint8_t ep_addr, 153 | uint16_t ep_mps, 154 | uint8_t ep_type) 155 | { 156 | USB_OTG_EP *ep; 157 | 158 | if ((ep_addr & 0x80) == 0x80) 159 | { 160 | ep = &pdev->dev.in_ep[ep_addr & 0x7F]; 161 | } 162 | else 163 | { 164 | ep = &pdev->dev.out_ep[ep_addr & 0x7F]; 165 | } 166 | ep->num = ep_addr & 0x7F; 167 | 168 | ep->is_in = (0x80 & ep_addr) != 0; 169 | ep->maxpacket = ep_mps; 170 | ep->type = ep_type; 171 | if (ep->is_in) 172 | { 173 | /* Assign a Tx FIFO */ 174 | ep->tx_fifo_num = ep->num; 175 | } 176 | /* Set initial data PID. */ 177 | if (ep_type == USB_OTG_EP_BULK ) 178 | { 179 | ep->data_pid_start = 0; 180 | } 181 | USB_OTG_EPActivate(pdev , ep ); 182 | return 0; 183 | } 184 | /** 185 | * @brief called when an EP is disabled 186 | * @param pdev: device instance 187 | * @param ep_addr: endpoint address 188 | * @retval : status 189 | */ 190 | uint32_t DCD_EP_Close(USB_OTG_CORE_HANDLE *pdev , uint8_t ep_addr) 191 | { 192 | USB_OTG_EP *ep; 193 | 194 | if ((ep_addr&0x80) == 0x80) 195 | { 196 | ep = &pdev->dev.in_ep[ep_addr & 0x7F]; 197 | } 198 | else 199 | { 200 | ep = &pdev->dev.out_ep[ep_addr & 0x7F]; 201 | } 202 | ep->num = ep_addr & 0x7F; 203 | ep->is_in = (0x80 & ep_addr) != 0; 204 | USB_OTG_EPDeactivate(pdev , ep ); 205 | return 0; 206 | } 207 | 208 | 209 | /** 210 | * @brief DCD_EP_PrepareRx 211 | * @param pdev: device instance 212 | * @param ep_addr: endpoint address 213 | * @param pbuf: pointer to Rx buffer 214 | * @param buf_len: data length 215 | * @retval : status 216 | */ 217 | uint32_t DCD_EP_PrepareRx( USB_OTG_CORE_HANDLE *pdev, 218 | uint8_t ep_addr, 219 | uint8_t *pbuf, 220 | uint16_t buf_len) 221 | { 222 | USB_OTG_EP *ep; 223 | 224 | ep = &pdev->dev.out_ep[ep_addr & 0x7F]; 225 | 226 | /*setup and start the Xfer */ 227 | ep->xfer_buff = pbuf; 228 | ep->xfer_len = buf_len; 229 | ep->xfer_count = 0; 230 | ep->is_in = 0; 231 | ep->num = ep_addr & 0x7F; 232 | 233 | if (pdev->cfg.dma_enable == 1) 234 | { 235 | ep->dma_addr = (uint32_t)pbuf; 236 | } 237 | 238 | if ( ep->num == 0 ) 239 | { 240 | USB_OTG_EP0StartXfer(pdev , ep); 241 | } 242 | else 243 | { 244 | USB_OTG_EPStartXfer(pdev, ep ); 245 | } 246 | return 0; 247 | } 248 | 249 | /** 250 | * @brief Transmit data over USB 251 | * @param pdev: device instance 252 | * @param ep_addr: endpoint address 253 | * @param pbuf: pointer to Tx buffer 254 | * @param buf_len: data length 255 | * @retval : status 256 | */ 257 | uint32_t DCD_EP_Tx ( USB_OTG_CORE_HANDLE *pdev, 258 | uint8_t ep_addr, 259 | uint8_t *pbuf, 260 | uint32_t buf_len) 261 | { 262 | USB_OTG_EP *ep; 263 | 264 | ep = &pdev->dev.in_ep[ep_addr & 0x7F]; 265 | 266 | /* Setup and start the Transfer */ 267 | ep->is_in = 1; 268 | ep->num = ep_addr & 0x7F; 269 | ep->xfer_buff = pbuf; 270 | ep->dma_addr = (uint32_t)pbuf; 271 | ep->xfer_count = 0; 272 | ep->xfer_len = buf_len; 273 | 274 | if ( ep->num == 0 ) 275 | { 276 | USB_OTG_EP0StartXfer(pdev , ep); 277 | } 278 | else 279 | { 280 | USB_OTG_EPStartXfer(pdev, ep ); 281 | } 282 | return 0; 283 | } 284 | 285 | 286 | /** 287 | * @brief Stall an endpoint. 288 | * @param pdev: device instance 289 | * @param epnum: endpoint address 290 | * @retval : status 291 | */ 292 | uint32_t DCD_EP_Stall (USB_OTG_CORE_HANDLE *pdev, uint8_t epnum) 293 | { 294 | USB_OTG_EP *ep; 295 | if ((0x80 & epnum) == 0x80) 296 | { 297 | ep = &pdev->dev.in_ep[epnum & 0x7F]; 298 | } 299 | else 300 | { 301 | ep = &pdev->dev.out_ep[epnum]; 302 | } 303 | 304 | ep->is_stall = 1; 305 | ep->num = epnum & 0x7F; 306 | ep->is_in = ((epnum & 0x80) == 0x80); 307 | 308 | USB_OTG_EPSetStall(pdev , ep); 309 | return (0); 310 | } 311 | 312 | 313 | /** 314 | * @brief Clear stall condition on endpoints. 315 | * @param pdev: device instance 316 | * @param epnum: endpoint address 317 | * @retval : status 318 | */ 319 | uint32_t DCD_EP_ClrStall (USB_OTG_CORE_HANDLE *pdev, uint8_t epnum) 320 | { 321 | USB_OTG_EP *ep; 322 | if ((0x80 & epnum) == 0x80) 323 | { 324 | ep = &pdev->dev.in_ep[epnum & 0x7F]; 325 | } 326 | else 327 | { 328 | ep = &pdev->dev.out_ep[epnum]; 329 | } 330 | 331 | ep->is_stall = 0; 332 | ep->num = epnum & 0x7F; 333 | ep->is_in = ((epnum & 0x80) == 0x80); 334 | 335 | USB_OTG_EPClearStall(pdev , ep); 336 | return (0); 337 | } 338 | 339 | 340 | /** 341 | * @brief This Function flushes the FIFOs. 342 | * @param pdev: device instance 343 | * @param epnum: endpoint address 344 | * @retval : status 345 | */ 346 | uint32_t DCD_EP_Flush (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum) 347 | { 348 | 349 | if ((epnum & 0x80) == 0x80) 350 | { 351 | USB_OTG_FlushTxFifo(pdev, epnum & 0x7F); 352 | } 353 | else 354 | { 355 | USB_OTG_FlushRxFifo(pdev); 356 | } 357 | 358 | return (0); 359 | } 360 | 361 | 362 | /** 363 | * @brief This Function set USB device address 364 | * @param pdev: device instance 365 | * @param address: new device address 366 | * @retval : status 367 | */ 368 | void DCD_EP_SetAddress (USB_OTG_CORE_HANDLE *pdev, uint8_t address) 369 | { 370 | USB_OTG_DCFG_TypeDef dcfg; 371 | dcfg.d32 = 0; 372 | dcfg.b.devaddr = address; 373 | USB_OTG_MODIFY_REG32( &pdev->regs.DREGS->DCFG, 0, dcfg.d32); 374 | } 375 | 376 | /** 377 | * @brief Connect device (enable internal pull-up) 378 | * @param pdev: device instance 379 | * @retval : None 380 | */ 381 | void DCD_DevConnect (USB_OTG_CORE_HANDLE *pdev) 382 | { 383 | #ifndef USE_OTG_MODE 384 | USB_OTG_DCTL_TypeDef dctl; 385 | dctl.d32 = USB_OTG_READ_REG32(&pdev->regs.DREGS->DCTL); 386 | /* Connect device */ 387 | dctl.b.sftdiscon = 0; 388 | USB_OTG_WRITE_REG32(&pdev->regs.DREGS->DCTL, dctl.d32); 389 | USB_OTG_BSP_mDelay(3); 390 | #endif 391 | } 392 | 393 | 394 | /** 395 | * @brief Disconnect device (disable internal pull-up) 396 | * @param pdev: device instance 397 | * @retval : None 398 | */ 399 | void DCD_DevDisconnect (USB_OTG_CORE_HANDLE *pdev) 400 | { 401 | #ifndef USE_OTG_MODE 402 | USB_OTG_DCTL_TypeDef dctl; 403 | dctl.d32 = USB_OTG_READ_REG32(&pdev->regs.DREGS->DCTL); 404 | /* Disconnect device for 3ms */ 405 | dctl.b.sftdiscon = 1; 406 | USB_OTG_WRITE_REG32(&pdev->regs.DREGS->DCTL, dctl.d32); 407 | USB_OTG_BSP_mDelay(3); 408 | #endif 409 | } 410 | 411 | 412 | /** 413 | * @brief returns the EP Status 414 | * @param pdev : Selected device 415 | * epnum : endpoint address 416 | * @retval : EP status 417 | */ 418 | 419 | uint32_t DCD_GetEPStatus(USB_OTG_CORE_HANDLE *pdev ,uint8_t epnum) 420 | { 421 | USB_OTG_EP *ep; 422 | uint32_t Status = 0; 423 | 424 | if ((0x80 & epnum) == 0x80) 425 | { 426 | ep = &pdev->dev.in_ep[epnum & 0x7F]; 427 | } 428 | else 429 | { 430 | ep = &pdev->dev.out_ep[epnum]; 431 | } 432 | 433 | Status = USB_OTG_GetEPStatus(pdev ,ep); 434 | 435 | /* Return the current status */ 436 | return Status; 437 | } 438 | 439 | /** 440 | * @brief Set the EP Status 441 | * @param pdev : Selected device 442 | * Status : new Status 443 | * epnum : EP address 444 | * @retval : None 445 | */ 446 | void DCD_SetEPStatus (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum , uint32_t Status) 447 | { 448 | USB_OTG_EP *ep; 449 | 450 | if ((0x80 & epnum) == 0x80) 451 | { 452 | ep = &pdev->dev.in_ep[epnum & 0x7F]; 453 | } 454 | else 455 | { 456 | ep = &pdev->dev.out_ep[epnum]; 457 | } 458 | 459 | USB_OTG_SetEPStatus(pdev ,ep , Status); 460 | } 461 | 462 | /** 463 | * @} 464 | */ 465 | 466 | /** 467 | * @} 468 | */ 469 | 470 | /** 471 | * @} 472 | */ 473 | #endif 474 | 475 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 476 | -------------------------------------------------------------------------------- /usb_lib/otg/usb_dcd.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_dcd.h 4 | * @author MCD Application Team 5 | * @version V2.0.0 6 | * @date 22-July-2011 7 | * @brief Peripheral Driver Header file 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __DCD_H__ 24 | #define __DCD_H__ 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usb_core.h" 28 | 29 | 30 | /** @addtogroup USB_OTG_DRIVER 31 | * @{ 32 | */ 33 | 34 | /** @defgroup USB_DCD 35 | * @brief This file is the 36 | * @{ 37 | */ 38 | 39 | 40 | /** @defgroup USB_DCD_Exported_Defines 41 | * @{ 42 | */ 43 | #define USB_OTG_EP_CONTROL 0 44 | #define USB_OTG_EP_ISOC 1 45 | #define USB_OTG_EP_BULK 2 46 | #define USB_OTG_EP_INT 3 47 | #define USB_OTG_EP_MASK 3 48 | 49 | /* Device Status */ 50 | #define USB_OTG_DEFAULT 1 51 | #define USB_OTG_ADDRESSED 2 52 | #define USB_OTG_CONFIGURED 3 53 | #define USB_OTG_SUSPENDED 4 54 | 55 | /** 56 | * @} 57 | */ 58 | 59 | 60 | /** @defgroup USB_DCD_Exported_Types 61 | * @{ 62 | */ 63 | /******************************************************************************** 64 | Data structure type 65 | ********************************************************************************/ 66 | typedef struct 67 | { 68 | uint8_t bLength; 69 | uint8_t bDescriptorType; 70 | uint8_t bEndpointAddress; 71 | uint8_t bmAttributes; 72 | uint16_t wMaxPacketSize; 73 | uint8_t bInterval; 74 | } 75 | EP_DESCRIPTOR , *PEP_DESCRIPTOR; 76 | 77 | /** 78 | * @} 79 | */ 80 | 81 | 82 | /** @defgroup USB_DCD_Exported_Macros 83 | * @{ 84 | */ 85 | /** 86 | * @} 87 | */ 88 | 89 | /** @defgroup USB_DCD_Exported_Variables 90 | * @{ 91 | */ 92 | /** 93 | * @} 94 | */ 95 | 96 | /** @defgroup USB_DCD_Exported_FunctionsPrototype 97 | * @{ 98 | */ 99 | /******************************************************************************** 100 | EXPORTED FUNCTION FROM THE USB-OTG LAYER 101 | ********************************************************************************/ 102 | void DCD_Init(USB_OTG_CORE_HANDLE *pdev , 103 | USB_OTG_CORE_ID_TypeDef coreID); 104 | 105 | void DCD_DevConnect (USB_OTG_CORE_HANDLE *pdev); 106 | void DCD_DevDisconnect (USB_OTG_CORE_HANDLE *pdev); 107 | void DCD_EP_SetAddress (USB_OTG_CORE_HANDLE *pdev, 108 | uint8_t address); 109 | uint32_t DCD_EP_Open(USB_OTG_CORE_HANDLE *pdev , 110 | uint8_t ep_addr, 111 | uint16_t ep_mps, 112 | uint8_t ep_type); 113 | 114 | uint32_t DCD_EP_Close (USB_OTG_CORE_HANDLE *pdev, 115 | uint8_t ep_addr); 116 | 117 | 118 | uint32_t DCD_EP_PrepareRx ( USB_OTG_CORE_HANDLE *pdev, 119 | uint8_t ep_addr, 120 | uint8_t *pbuf, 121 | uint16_t buf_len); 122 | 123 | uint32_t DCD_EP_Tx (USB_OTG_CORE_HANDLE *pdev, 124 | uint8_t ep_addr, 125 | uint8_t *pbuf, 126 | uint32_t buf_len); 127 | uint32_t DCD_EP_Stall (USB_OTG_CORE_HANDLE *pdev, 128 | uint8_t epnum); 129 | uint32_t DCD_EP_ClrStall (USB_OTG_CORE_HANDLE *pdev, 130 | uint8_t epnum); 131 | uint32_t DCD_EP_Flush (USB_OTG_CORE_HANDLE *pdev, 132 | uint8_t epnum); 133 | uint32_t DCD_Handle_ISR(USB_OTG_CORE_HANDLE *pdev); 134 | 135 | uint32_t DCD_GetEPStatus(USB_OTG_CORE_HANDLE *pdev , 136 | uint8_t epnum); 137 | 138 | void DCD_SetEPStatus (USB_OTG_CORE_HANDLE *pdev , 139 | uint8_t epnum , 140 | uint32_t Status); 141 | 142 | /** 143 | * @} 144 | */ 145 | 146 | 147 | #endif //__DCD_H__ 148 | 149 | 150 | /** 151 | * @} 152 | */ 153 | 154 | /** 155 | * @} 156 | */ 157 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 158 | 159 | -------------------------------------------------------------------------------- /usb_lib/otg/usb_dcd_int.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_dcd_int.h 4 | * @author MCD Application Team 5 | * @version V2.0.0 6 | * @date 22-July-2011 7 | * @brief Peripheral Device Interface Layer 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef USB_DCD_INT_H__ 24 | #define USB_DCD_INT_H__ 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usb_dcd.h" 28 | 29 | 30 | 31 | /** @addtogroup USB_OTG_DRIVER 32 | * @{ 33 | */ 34 | 35 | /** @defgroup USB_DCD_INT 36 | * @brief This file is the 37 | * @{ 38 | */ 39 | 40 | 41 | /** @defgroup USB_DCD_INT_Exported_Defines 42 | * @{ 43 | */ 44 | 45 | typedef struct _USBD_DCD_INT 46 | { 47 | uint8_t (* DataOutStage) (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum); 48 | uint8_t (* DataInStage) (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum); 49 | uint8_t (* SetupStage) (USB_OTG_CORE_HANDLE *pdev); 50 | uint8_t (* SOF) (USB_OTG_CORE_HANDLE *pdev); 51 | uint8_t (* Reset) (USB_OTG_CORE_HANDLE *pdev); 52 | uint8_t (* Suspend) (USB_OTG_CORE_HANDLE *pdev); 53 | uint8_t (* Resume) (USB_OTG_CORE_HANDLE *pdev); 54 | uint8_t (* IsoINIncomplete) (USB_OTG_CORE_HANDLE *pdev); 55 | uint8_t (* IsoOUTIncomplete) (USB_OTG_CORE_HANDLE *pdev); 56 | 57 | uint8_t (* DevConnected) (USB_OTG_CORE_HANDLE *pdev); 58 | uint8_t (* DevDisconnected) (USB_OTG_CORE_HANDLE *pdev); 59 | 60 | }USBD_DCD_INT_cb_TypeDef; 61 | 62 | extern USBD_DCD_INT_cb_TypeDef *USBD_DCD_INT_fops; 63 | /** 64 | * @} 65 | */ 66 | 67 | 68 | /** @defgroup USB_DCD_INT_Exported_Types 69 | * @{ 70 | */ 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @defgroup USB_DCD_INT_Exported_Macros 76 | * @{ 77 | */ 78 | 79 | #define CLEAR_IN_EP_INTR(epnum,intr) \ 80 | diepint.d32=0; \ 81 | diepint.b.intr = 1; \ 82 | USB_OTG_WRITE_REG32(&pdev->regs.INEP_REGS[epnum]->DIEPINT,diepint.d32); 83 | 84 | #define CLEAR_OUT_EP_INTR(epnum,intr) \ 85 | doepint.d32=0; \ 86 | doepint.b.intr = 1; \ 87 | USB_OTG_WRITE_REG32(&pdev->regs.OUTEP_REGS[epnum]->DOEPINT,doepint.d32); 88 | 89 | /** 90 | * @} 91 | */ 92 | 93 | /** @defgroup USB_DCD_INT_Exported_Variables 94 | * @{ 95 | */ 96 | /** 97 | * @} 98 | */ 99 | 100 | /** @defgroup USB_DCD_INT_Exported_FunctionsPrototype 101 | * @{ 102 | */ 103 | 104 | uint32_t USBD_OTG_ISR_Handler (USB_OTG_CORE_HANDLE *pdev); 105 | 106 | /** 107 | * @} 108 | */ 109 | 110 | 111 | #endif // USB_DCD_INT_H__ 112 | 113 | /** 114 | * @} 115 | */ 116 | 117 | /** 118 | * @} 119 | */ 120 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 121 | 122 | -------------------------------------------------------------------------------- /usb_lib/otg/usb_defines.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_defines.h 4 | * @author MCD Application Team 5 | * @version V2.0.0 6 | * @date 22-July-2011 7 | * @brief Header of the Core Layer 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __USB_DEF_H__ 24 | #define __USB_DEF_H__ 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "usb_conf.h" 28 | 29 | /** @addtogroup USB_OTG_DRIVER 30 | * @{ 31 | */ 32 | 33 | /** @defgroup USB_DEFINES 34 | * @brief This file is the 35 | * @{ 36 | */ 37 | 38 | 39 | /** @defgroup USB_DEFINES_Exported_Defines 40 | * @{ 41 | */ 42 | /** 43 | * @} 44 | */ 45 | 46 | 47 | /** @defgroup _CORE_DEFINES_ 48 | * @{ 49 | */ 50 | 51 | #define USB_OTG_SPEED_PARAM_HIGH 0 52 | #define USB_OTG_SPEED_PARAM_HIGH_IN_FULL 1 53 | #define USB_OTG_SPEED_PARAM_FULL 3 54 | 55 | #define USB_OTG_SPEED_HIGH 0 56 | #define USB_OTG_SPEED_FULL 1 57 | 58 | #define USB_OTG_ULPI_PHY 1 59 | #define USB_OTG_EMBEDDED_PHY 2 60 | #define USB_OTG_I2C_PHY 3 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | 67 | /** @defgroup _GLOBAL_DEFINES_ 68 | * @{ 69 | */ 70 | #define GAHBCFG_TXFEMPTYLVL_EMPTY 1 71 | #define GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0 72 | #define GAHBCFG_GLBINT_ENABLE 1 73 | #define GAHBCFG_INT_DMA_BURST_SINGLE 0 74 | #define GAHBCFG_INT_DMA_BURST_INCR 1 75 | #define GAHBCFG_INT_DMA_BURST_INCR4 3 76 | #define GAHBCFG_INT_DMA_BURST_INCR8 5 77 | #define GAHBCFG_INT_DMA_BURST_INCR16 7 78 | #define GAHBCFG_DMAENABLE 1 79 | #define GAHBCFG_TXFEMPTYLVL_EMPTY 1 80 | #define GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0 81 | #define GRXSTS_PKTSTS_IN 2 82 | #define GRXSTS_PKTSTS_IN_XFER_COMP 3 83 | #define GRXSTS_PKTSTS_DATA_TOGGLE_ERR 5 84 | #define GRXSTS_PKTSTS_CH_HALTED 7 85 | /** 86 | * @} 87 | */ 88 | 89 | 90 | /** @defgroup _OnTheGo_DEFINES_ 91 | * @{ 92 | */ 93 | #define MODE_HNP_SRP_CAPABLE 0 94 | #define MODE_SRP_ONLY_CAPABLE 1 95 | #define MODE_NO_HNP_SRP_CAPABLE 2 96 | #define MODE_SRP_CAPABLE_DEVICE 3 97 | #define MODE_NO_SRP_CAPABLE_DEVICE 4 98 | #define MODE_SRP_CAPABLE_HOST 5 99 | #define MODE_NO_SRP_CAPABLE_HOST 6 100 | #define A_HOST 1 101 | #define A_SUSPEND 2 102 | #define A_PERIPHERAL 3 103 | #define B_PERIPHERAL 4 104 | #define B_HOST 5 105 | #define DEVICE_MODE 0 106 | #define HOST_MODE 1 107 | #define OTG_MODE 2 108 | /** 109 | * @} 110 | */ 111 | 112 | 113 | /** @defgroup __DEVICE_DEFINES_ 114 | * @{ 115 | */ 116 | #define DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0 117 | #define DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1 118 | #define DSTS_ENUMSPD_LS_PHY_6MHZ 2 119 | #define DSTS_ENUMSPD_FS_PHY_48MHZ 3 120 | 121 | #define DCFG_FRAME_INTERVAL_80 0 122 | #define DCFG_FRAME_INTERVAL_85 1 123 | #define DCFG_FRAME_INTERVAL_90 2 124 | #define DCFG_FRAME_INTERVAL_95 3 125 | 126 | #define DEP0CTL_MPS_64 0 127 | #define DEP0CTL_MPS_32 1 128 | #define DEP0CTL_MPS_16 2 129 | #define DEP0CTL_MPS_8 3 130 | 131 | #define EP_SPEED_LOW 0 132 | #define EP_SPEED_FULL 1 133 | #define EP_SPEED_HIGH 2 134 | 135 | #define EP_TYPE_CTRL 0 136 | #define EP_TYPE_ISOC 1 137 | #define EP_TYPE_BULK 2 138 | #define EP_TYPE_INTR 3 139 | #define EP_TYPE_MSK 3 140 | 141 | #define STS_GOUT_NAK 1 142 | #define STS_DATA_UPDT 2 143 | #define STS_XFER_COMP 3 144 | #define STS_SETUP_COMP 4 145 | #define STS_SETUP_UPDT 6 146 | /** 147 | * @} 148 | */ 149 | 150 | 151 | /** @defgroup __HOST_DEFINES_ 152 | * @{ 153 | */ 154 | #define HC_PID_DATA0 0 155 | #define HC_PID_DATA2 1 156 | #define HC_PID_DATA1 2 157 | #define HC_PID_SETUP 3 158 | 159 | #define HPRT0_PRTSPD_HIGH_SPEED 0 160 | #define HPRT0_PRTSPD_FULL_SPEED 1 161 | #define HPRT0_PRTSPD_LOW_SPEED 2 162 | 163 | #define HCFG_30_60_MHZ 0 164 | #define HCFG_48_MHZ 1 165 | #define HCFG_6_MHZ 2 166 | 167 | #define HCCHAR_CTRL 0 168 | #define HCCHAR_ISOC 1 169 | #define HCCHAR_BULK 2 170 | #define HCCHAR_INTR 3 171 | 172 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 173 | 174 | /** 175 | * @} 176 | */ 177 | 178 | 179 | /** @defgroup USB_DEFINES_Exported_Types 180 | * @{ 181 | */ 182 | 183 | typedef enum 184 | { 185 | USB_OTG_HS_CORE_ID = 0, 186 | USB_OTG_FS_CORE_ID = 1 187 | }USB_OTG_CORE_ID_TypeDef; 188 | /** 189 | * @} 190 | */ 191 | 192 | 193 | /** @defgroup USB_DEFINES_Exported_Macros 194 | * @{ 195 | */ 196 | /** 197 | * @} 198 | */ 199 | 200 | /** @defgroup USB_DEFINES_Exported_Variables 201 | * @{ 202 | */ 203 | /** 204 | * @} 205 | */ 206 | 207 | /** @defgroup USB_DEFINES_Exported_FunctionsPrototype 208 | * @{ 209 | */ 210 | /** 211 | * @} 212 | */ 213 | 214 | 215 | /** @defgroup Internal_Macro's 216 | * @{ 217 | */ 218 | #define USB_OTG_READ_REG32(reg) (*(__IO uint32_t *)reg) 219 | #define USB_OTG_WRITE_REG32(reg,value) (*(__IO uint32_t *)reg = value) 220 | #define USB_OTG_MODIFY_REG32(reg,clear_mask,set_mask) \ 221 | USB_OTG_WRITE_REG32(reg, (((USB_OTG_READ_REG32(reg)) & ~clear_mask) | set_mask ) ) 222 | 223 | /******************************************************************************** 224 | ENUMERATION TYPE 225 | ********************************************************************************/ 226 | enum USB_OTG_SPEED { 227 | USB_SPEED_UNKNOWN = 0, 228 | USB_SPEED_LOW, 229 | USB_SPEED_FULL, 230 | USB_SPEED_HIGH 231 | }; 232 | 233 | #endif //__USB_DEFINES__H__ 234 | 235 | 236 | /** 237 | * @} 238 | */ 239 | 240 | /** 241 | * @} 242 | */ 243 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 244 | 245 | -------------------------------------------------------------------------------- /vocoder_passthru/main.h: -------------------------------------------------------------------------------- 1 | 2 | /* Includes ------------------------------------------------------------------*/ 3 | #include 4 | #include 5 | #include "stm32f4xx.h" 6 | #include "stm32f4_discovery.h" 7 | #include "stm32f4_discovery_audio_codec.h" 8 | #include "stm32f4_discovery_lis302dl.h" 9 | #include "stm32f4xx_it.h" 10 | 11 | /* Exported types ------------------------------------------------------------*/ 12 | typedef enum { 13 | SPKR_NONE = 0, 14 | SPKR_LOWER, 15 | SPKR_UPPER 16 | } spkr_e; 17 | extern spkr_e fill_spkr; 18 | 19 | typedef enum { 20 | MIC_RDY_NONE = 0, 21 | MIC_RDY_LOWER, 22 | MIC_RDY_UPPER 23 | } mic_e; 24 | extern mic_e mic_ready; 25 | /* Exported constants --------------------------------------------------------*/ 26 | 27 | /* Exported macro ------------------------------------------------------------*/ 28 | /* Exported functions ------------------------------------------------------- */ 29 | void ColorfulRingOfDeath(void); 30 | 31 | extern volatile unsigned micSampCnt; 32 | extern volatile unsigned micPutCnt; 33 | extern float micGain; 34 | extern volatile uint8_t micOverrun; 35 | extern volatile uint8_t _waveType; 36 | extern volatile uint8_t vol; 37 | extern unsigned nsamp; 38 | extern unsigned nsamp_x2; 39 | void vcp_printf( const char* format, ... ); 40 | 41 | void _speaker_init(unsigned); 42 | void _microphone_init(void); 43 | #define MAX_SPKR_BUFFER_SIZE 2560 44 | extern int16_t spkr_buffer[]; 45 | extern short mic_buf[]; 46 | 47 | -------------------------------------------------------------------------------- /vocoder_passthru/speaker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | //#define SAMPLE_RATE_DEBUG 6 | 7 | #define SPEAKER_VOLUME 95 // headphones create noise on negative edge if > 75 8 | 9 | #define SINE_TABLE_LENGTH 1024 10 | 11 | const uint16_t sine_table[SINE_TABLE_LENGTH] = 12 | { 13 | 0x8000,0x80c9,0x8192,0x825b,0x8324,0x83ee,0x84b7,0x8580, 14 | 0x8649,0x8712,0x87db,0x88a4,0x896c,0x8a35,0x8afe,0x8bc6, 15 | 0x8c8e,0x8d57,0x8e1f,0x8ee7,0x8fae,0x9076,0x913e,0x9205, 16 | 0x92cc,0x9393,0x945a,0x9521,0x95e7,0x96ad,0x9773,0x9839, 17 | 0x98fe,0x99c4,0x9a89,0x9b4d,0x9c12,0x9cd6,0x9d9a,0x9e5e, 18 | 0x9f21,0x9fe4,0xa0a7,0xa169,0xa22b,0xa2ed,0xa3af,0xa470, 19 | 0xa530,0xa5f1,0xa6b1,0xa770,0xa830,0xa8ef,0xa9ad,0xaa6b, 20 | 0xab29,0xabe6,0xaca3,0xad5f,0xae1b,0xaed7,0xaf92,0xb04d, 21 | 0xb107,0xb1c0,0xb27a,0xb332,0xb3ea,0xb4a2,0xb559,0xb610, 22 | 0xb6c6,0xb77c,0xb831,0xb8e5,0xb999,0xba4d,0xbb00,0xbbb2, 23 | 0xbc64,0xbd15,0xbdc6,0xbe76,0xbf25,0xbfd4,0xc082,0xc12f, 24 | 0xc1dc,0xc288,0xc334,0xc3df,0xc489,0xc533,0xc5dc,0xc684, 25 | 0xc72c,0xc7d3,0xc879,0xc91f,0xc9c3,0xca67,0xcb0b,0xcbae, 26 | 0xcc4f,0xccf1,0xcd91,0xce31,0xced0,0xcf6e,0xd00b,0xd0a8, 27 | 0xd144,0xd1df,0xd279,0xd313,0xd3ac,0xd443,0xd4db,0xd571, 28 | 0xd606,0xd69b,0xd72f,0xd7c2,0xd854,0xd8e5,0xd975,0xda05, 29 | 0xda93,0xdb21,0xdbae,0xdc3a,0xdcc5,0xdd4f,0xddd9,0xde61, 30 | 0xdee9,0xdf6f,0xdff5,0xe07a,0xe0fd,0xe180,0xe202,0xe283, 31 | 0xe303,0xe382,0xe400,0xe47d,0xe4fa,0xe575,0xe5ef,0xe668, 32 | 0xe6e0,0xe758,0xe7ce,0xe843,0xe8b7,0xe92b,0xe99d,0xea0e, 33 | 0xea7e,0xeaed,0xeb5b,0xebc8,0xec34,0xec9f,0xed09,0xed72, 34 | 0xedda,0xee41,0xeea7,0xef0b,0xef6f,0xefd1,0xf033,0xf093, 35 | 0xf0f2,0xf150,0xf1ad,0xf209,0xf264,0xf2be,0xf316,0xf36e, 36 | 0xf3c4,0xf41a,0xf46e,0xf4c1,0xf513,0xf564,0xf5b3,0xf602, 37 | 0xf64f,0xf69b,0xf6e6,0xf730,0xf779,0xf7c1,0xf807,0xf84d, 38 | 0xf891,0xf8d4,0xf916,0xf956,0xf996,0xf9d4,0xfa11,0xfa4d, 39 | 0xfa88,0xfac1,0xfafa,0xfb31,0xfb67,0xfb9c,0xfbd0,0xfc02, 40 | 0xfc33,0xfc63,0xfc92,0xfcc0,0xfcec,0xfd17,0xfd42,0xfd6a, 41 | 0xfd92,0xfdb8,0xfdde,0xfe01,0xfe24,0xfe46,0xfe66,0xfe85, 42 | 0xfea3,0xfec0,0xfedb,0xfef5,0xff0e,0xff26,0xff3c,0xff52, 43 | 0xff66,0xff79,0xff8a,0xff9b,0xffaa,0xffb8,0xffc4,0xffd0, 44 | 0xffda,0xffe3,0xffeb,0xfff1,0xfff6,0xfffa,0xfffd,0xffff, 45 | 0xffff,0xfffe,0xfffc,0xfff8,0xfff4,0xffee,0xffe7,0xffdf, 46 | 0xffd5,0xffca,0xffbe,0xffb1,0xffa2,0xff93,0xff82,0xff6f, 47 | 0xff5c,0xff47,0xff31,0xff1a,0xff02,0xfee8,0xfece,0xfeb1, 48 | 0xfe94,0xfe76,0xfe56,0xfe35,0xfe13,0xfdf0,0xfdcb,0xfda5, 49 | 0xfd7e,0xfd56,0xfd2d,0xfd02,0xfcd6,0xfca9,0xfc7b,0xfc4b, 50 | 0xfc1b,0xfbe9,0xfbb6,0xfb82,0xfb4c,0xfb16,0xfade,0xfaa5, 51 | 0xfa6b,0xfa2f,0xf9f3,0xf9b5,0xf976,0xf936,0xf8f5,0xf8b2, 52 | 0xf86f,0xf82a,0xf7e4,0xf79d,0xf755,0xf70c,0xf6c1,0xf675, 53 | 0xf629,0xf5db,0xf58c,0xf53b,0xf4ea,0xf498,0xf444,0xf3ef, 54 | 0xf399,0xf342,0xf2ea,0xf291,0xf237,0xf1db,0xf17f,0xf121, 55 | 0xf0c3,0xf063,0xf002,0xefa0,0xef3d,0xeed9,0xee74,0xee0e, 56 | 0xeda6,0xed3e,0xecd5,0xec6a,0xebff,0xeb92,0xeb24,0xeab6, 57 | 0xea46,0xe9d6,0xe964,0xe8f1,0xe87d,0xe809,0xe793,0xe71c, 58 | 0xe6a4,0xe62c,0xe5b2,0xe537,0xe4bc,0xe43f,0xe3c1,0xe343, 59 | 0xe2c3,0xe243,0xe1c1,0xe13f,0xe0bc,0xe037,0xdfb2,0xdf2c, 60 | 0xdea5,0xde1d,0xdd94,0xdd0a,0xdc80,0xdbf4,0xdb68,0xdada, 61 | 0xda4c,0xd9bd,0xd92d,0xd89c,0xd80b,0xd778,0xd6e5,0xd651, 62 | 0xd5bc,0xd526,0xd48f,0xd3f8,0xd35f,0xd2c6,0xd22c,0xd192, 63 | 0xd0f6,0xd05a,0xcfbd,0xcf1f,0xce80,0xcde1,0xcd41,0xcca0, 64 | 0xcbff,0xcb5c,0xcab9,0xca16,0xc971,0xc8cc,0xc826,0xc77f, 65 | 0xc6d8,0xc630,0xc588,0xc4de,0xc434,0xc38a,0xc2de,0xc232, 66 | 0xc186,0xc0d9,0xc02b,0xbf7c,0xbecd,0xbe1e,0xbd6d,0xbcbd, 67 | 0xbc0b,0xbb59,0xbaa6,0xb9f3,0xb940,0xb88b,0xb7d6,0xb721, 68 | 0xb66b,0xb5b5,0xb4fe,0xb446,0xb38e,0xb2d6,0xb21d,0xb164, 69 | 0xb0aa,0xafef,0xaf34,0xae79,0xadbd,0xad01,0xac45,0xab88, 70 | 0xaaca,0xaa0c,0xa94e,0xa88f,0xa7d0,0xa711,0xa651,0xa591, 71 | 0xa4d0,0xa40f,0xa34e,0xa28c,0xa1ca,0xa108,0xa045,0x9f83, 72 | 0x9ebf,0x9dfc,0x9d38,0x9c74,0x9bb0,0x9aeb,0x9a26,0x9961, 73 | 0x989c,0x97d6,0x9710,0x964a,0x9584,0x94bd,0x93f7,0x9330, 74 | 0x9269,0x91a1,0x90da,0x9012,0x8f4b,0x8e83,0x8dbb,0x8cf3, 75 | 0x8c2a,0x8b62,0x8a99,0x89d1,0x8908,0x883f,0x8776,0x86ad, 76 | 0x85e4,0x851b,0x8452,0x8389,0x82c0,0x81f7,0x812d,0x8064, 77 | 0x7f9b,0x7ed2,0x7e08,0x7d3f,0x7c76,0x7bad,0x7ae4,0x7a1b, 78 | 0x7952,0x7889,0x77c0,0x76f7,0x762e,0x7566,0x749d,0x73d5, 79 | 0x730c,0x7244,0x717c,0x70b4,0x6fed,0x6f25,0x6e5e,0x6d96, 80 | 0x6ccf,0x6c08,0x6b42,0x6a7b,0x69b5,0x68ef,0x6829,0x6763, 81 | 0x669e,0x65d9,0x6514,0x644f,0x638b,0x62c7,0x6203,0x6140, 82 | 0x607c,0x5fba,0x5ef7,0x5e35,0x5d73,0x5cb1,0x5bf0,0x5b2f, 83 | 0x5a6e,0x59ae,0x58ee,0x582f,0x5770,0x56b1,0x55f3,0x5535, 84 | 0x5477,0x53ba,0x52fe,0x5242,0x5186,0x50cb,0x5010,0x4f55, 85 | 0x4e9b,0x4de2,0x4d29,0x4c71,0x4bb9,0x4b01,0x4a4a,0x4994, 86 | 0x48de,0x4829,0x4774,0x46bf,0x460c,0x4559,0x44a6,0x43f4, 87 | 0x4342,0x4292,0x41e1,0x4132,0x4083,0x3fd4,0x3f26,0x3e79, 88 | 0x3dcd,0x3d21,0x3c75,0x3bcb,0x3b21,0x3a77,0x39cf,0x3927, 89 | 0x3880,0x37d9,0x3733,0x368e,0x35e9,0x3546,0x34a3,0x3400, 90 | 0x335f,0x32be,0x321e,0x317f,0x30e0,0x3042,0x2fa5,0x2f09, 91 | 0x2e6d,0x2dd3,0x2d39,0x2ca0,0x2c07,0x2b70,0x2ad9,0x2a43, 92 | 0x29ae,0x291a,0x2887,0x27f4,0x2763,0x26d2,0x2642,0x25b3, 93 | 0x2525,0x2497,0x240b,0x237f,0x22f5,0x226b,0x21e2,0x215a, 94 | 0x20d3,0x204d,0x1fc8,0x1f43,0x1ec0,0x1e3e,0x1dbc,0x1d3c, 95 | 0x1cbc,0x1c3e,0x1bc0,0x1b43,0x1ac8,0x1a4d,0x19d3,0x195b, 96 | 0x18e3,0x186c,0x17f6,0x1782,0x170e,0x169b,0x1629,0x15b9, 97 | 0x1549,0x14db,0x146d,0x1400,0x1395,0x132a,0x12c1,0x1259, 98 | 0x11f1,0x118b,0x1126,0x10c2,0x105f,0xffd,0xf9c,0xf3c, 99 | 0xede,0xe80,0xe24,0xdc8,0xd6e,0xd15,0xcbd,0xc66, 100 | 0xc10,0xbbb,0xb67,0xb15,0xac4,0xa73,0xa24,0x9d6, 101 | 0x98a,0x93e,0x8f3,0x8aa,0x862,0x81b,0x7d5,0x790, 102 | 0x74d,0x70a,0x6c9,0x689,0x64a,0x60c,0x5d0,0x594, 103 | 0x55a,0x521,0x4e9,0x4b3,0x47d,0x449,0x416,0x3e4, 104 | 0x3b4,0x384,0x356,0x329,0x2fd,0x2d2,0x2a9,0x281, 105 | 0x25a,0x234,0x20f,0x1ec,0x1ca,0x1a9,0x189,0x16b, 106 | 0x14e,0x131,0x117,0xfd,0xe5,0xce,0xb8,0xa3, 107 | 0x90,0x7d,0x6c,0x5d,0x4e,0x41,0x35,0x2a, 108 | 0x20,0x18,0x11,0xb,0x7,0x3,0x1,0x0, 109 | 0x0,0x2,0x5,0x9,0xe,0x14,0x1c,0x25, 110 | 0x2f,0x3b,0x47,0x55,0x64,0x75,0x86,0x99, 111 | 0xad,0xc3,0xd9,0xf1,0x10a,0x124,0x13f,0x15c, 112 | 0x17a,0x199,0x1b9,0x1db,0x1fe,0x221,0x247,0x26d, 113 | 0x295,0x2bd,0x2e8,0x313,0x33f,0x36d,0x39c,0x3cc, 114 | 0x3fd,0x42f,0x463,0x498,0x4ce,0x505,0x53e,0x577, 115 | 0x5b2,0x5ee,0x62b,0x669,0x6a9,0x6e9,0x72b,0x76e, 116 | 0x7b2,0x7f8,0x83e,0x886,0x8cf,0x919,0x964,0x9b0, 117 | 0x9fd,0xa4c,0xa9b,0xaec,0xb3e,0xb91,0xbe5,0xc3b, 118 | 0xc91,0xce9,0xd41,0xd9b,0xdf6,0xe52,0xeaf,0xf0d, 119 | 0xf6c,0xfcc,0x102e,0x1090,0x10f4,0x1158,0x11be,0x1225, 120 | 0x128d,0x12f6,0x1360,0x13cb,0x1437,0x14a4,0x1512,0x1581, 121 | 0x15f1,0x1662,0x16d4,0x1748,0x17bc,0x1831,0x18a7,0x191f, 122 | 0x1997,0x1a10,0x1a8a,0x1b05,0x1b82,0x1bff,0x1c7d,0x1cfc, 123 | 0x1d7c,0x1dfd,0x1e7f,0x1f02,0x1f85,0x200a,0x2090,0x2116, 124 | 0x219e,0x2226,0x22b0,0x233a,0x23c5,0x2451,0x24de,0x256c, 125 | 0x25fa,0x268a,0x271a,0x27ab,0x283d,0x28d0,0x2964,0x29f9, 126 | 0x2a8e,0x2b24,0x2bbc,0x2c53,0x2cec,0x2d86,0x2e20,0x2ebb, 127 | 0x2f57,0x2ff4,0x3091,0x312f,0x31ce,0x326e,0x330e,0x33b0, 128 | 0x3451,0x34f4,0x3598,0x363c,0x36e0,0x3786,0x382c,0x38d3, 129 | 0x397b,0x3a23,0x3acc,0x3b76,0x3c20,0x3ccb,0x3d77,0x3e23, 130 | 0x3ed0,0x3f7d,0x402b,0x40da,0x4189,0x4239,0x42ea,0x439b, 131 | 0x444d,0x44ff,0x45b2,0x4666,0x471a,0x47ce,0x4883,0x4939, 132 | 0x49ef,0x4aa6,0x4b5d,0x4c15,0x4ccd,0x4d85,0x4e3f,0x4ef8, 133 | 0x4fb2,0x506d,0x5128,0x51e4,0x52a0,0x535c,0x5419,0x54d6, 134 | 0x5594,0x5652,0x5710,0x57cf,0x588f,0x594e,0x5a0e,0x5acf, 135 | 0x5b8f,0x5c50,0x5d12,0x5dd4,0x5e96,0x5f58,0x601b,0x60de, 136 | 0x61a1,0x6265,0x6329,0x63ed,0x64b2,0x6576,0x663b,0x6701, 137 | 0x67c6,0x688c,0x6952,0x6a18,0x6ade,0x6ba5,0x6c6c,0x6d33, 138 | 0x6dfa,0x6ec1,0x6f89,0x7051,0x7118,0x71e0,0x72a8,0x7371, 139 | 0x7439,0x7501,0x75ca,0x7693,0x775b,0x7824,0x78ed,0x79b6, 140 | 0x7a7f,0x7b48,0x7c11,0x7cdb,0x7da4,0x7e6d,0x7f36,0x8000 141 | }; 142 | 143 | int16_t spkr_buffer[MAX_SPKR_BUFFER_SIZE]; 144 | spkr_e fill_spkr; 145 | 146 | void _speaker_init(unsigned nsamp_x4) 147 | { 148 | /* Initialize I2S interface */ 149 | EVAL_AUDIO_SetAudioInterface(AUDIO_INTERFACE_I2S); 150 | 151 | vol = SPEAKER_VOLUME; 152 | /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */ 153 | EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, vol, 8000); 154 | 155 | fill_spkr = SPKR_NONE; 156 | EVAL_AUDIO_Play((uint16_t *)spkr_buffer, nsamp_x4 * 8); // Only stereo is supported*/ 157 | 158 | vcp_printf("spkrInit\r\n"); 159 | } 160 | 161 | #define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) 162 | #define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) 163 | 164 | volatile uint8_t rshift; 165 | volatile unsigned skip; 166 | volatile uint8_t _waveType; 167 | 168 | void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size) 169 | { 170 | /* upper half of buffer was just completed */ 171 | GPIO_ToggleBits(GPIOC, GPIO_Pin_1); 172 | fill_spkr = SPKR_UPPER; // will be playing lower half: software to fill upper half 173 | } 174 | 175 | void EVAL_AUDIO_HalfTransfer_CallBack(uint32_t pBuffer, uint32_t Size) 176 | { 177 | /* lower half of buffer was just completed */ 178 | GPIO_ToggleBits(GPIOC, GPIO_Pin_2); 179 | fill_spkr = SPKR_LOWER; // will be playing upper half: software to fill lower half 180 | } 181 | 182 | void EVAL_AUDIO_Error_CallBack(void* pData) 183 | { 184 | ColorfulRingOfDeath(); 185 | } 186 | 187 | uint16_t EVAL_AUDIO_GetSampleCallBack(void) 188 | { 189 | return 0; 190 | } 191 | --------------------------------------------------------------------------------