Il y avait des problèmes dans la libraire...
Fork of ST_FREQUENCY_DIVIDER by
targets/TARGET_STM/TARGET_STM32F4/stm_divider_api.c@3:ee5befdffaca, 2017-09-03 (annotated)
- Committer:
- qroche
- Date:
- Sun Sep 03 23:18:55 2017 +0000
- Revision:
- 3:ee5befdffaca
- Parent:
- 1:d75cd2582664
fin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davide.aliprandi@st.com | 0:f11903264b5e | 1 | /** |
davide.aliprandi@st.com | 0:f11903264b5e | 2 | ****************************************************************************** |
davide.aliprandi@st.com | 0:f11903264b5e | 3 | * @file stm_divider_api.c |
davide.aliprandi@st.com | 0:f11903264b5e | 4 | * @author AST / Software Platforms and Cloud |
davide.aliprandi@st.com | 0:f11903264b5e | 5 | * @version V1.0 |
davide.aliprandi@st.com | 0:f11903264b5e | 6 | * @date November 8th, 2016 |
davide.aliprandi@st.com | 0:f11903264b5e | 7 | * @brief Utilities header file for the X_NUCLEO_CCA02M1 expansion board. |
davide.aliprandi@st.com | 0:f11903264b5e | 8 | ****************************************************************************** |
davide.aliprandi@st.com | 0:f11903264b5e | 9 | * @attention |
davide.aliprandi@st.com | 0:f11903264b5e | 10 | * |
davide.aliprandi@st.com | 0:f11903264b5e | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
davide.aliprandi@st.com | 0:f11903264b5e | 12 | * |
davide.aliprandi@st.com | 0:f11903264b5e | 13 | * Redistribution and use in source and binary forms, with or without modification, |
davide.aliprandi@st.com | 0:f11903264b5e | 14 | * are permitted provided that the following conditions are met: |
davide.aliprandi@st.com | 0:f11903264b5e | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
davide.aliprandi@st.com | 0:f11903264b5e | 16 | * this list of conditions and the following disclaimer. |
davide.aliprandi@st.com | 0:f11903264b5e | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
davide.aliprandi@st.com | 0:f11903264b5e | 18 | * this list of conditions and the following disclaimer in the documentation |
davide.aliprandi@st.com | 0:f11903264b5e | 19 | * and/or other materials provided with the distribution. |
davide.aliprandi@st.com | 0:f11903264b5e | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
davide.aliprandi@st.com | 0:f11903264b5e | 21 | * may be used to endorse or promote products derived from this software |
davide.aliprandi@st.com | 0:f11903264b5e | 22 | * without specific prior written permission. |
davide.aliprandi@st.com | 0:f11903264b5e | 23 | * |
davide.aliprandi@st.com | 0:f11903264b5e | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
davide.aliprandi@st.com | 0:f11903264b5e | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
davide.aliprandi@st.com | 0:f11903264b5e | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
davide.aliprandi@st.com | 0:f11903264b5e | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
davide.aliprandi@st.com | 0:f11903264b5e | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
davide.aliprandi@st.com | 0:f11903264b5e | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
davide.aliprandi@st.com | 0:f11903264b5e | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
davide.aliprandi@st.com | 0:f11903264b5e | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
davide.aliprandi@st.com | 0:f11903264b5e | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
davide.aliprandi@st.com | 0:f11903264b5e | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
davide.aliprandi@st.com | 0:f11903264b5e | 34 | * |
davide.aliprandi@st.com | 0:f11903264b5e | 35 | ****************************************************************************** |
davide.aliprandi@st.com | 0:f11903264b5e | 36 | */ |
davide.aliprandi@st.com | 0:f11903264b5e | 37 | |
davide.aliprandi@st.com | 0:f11903264b5e | 38 | |
davide.aliprandi@st.com | 0:f11903264b5e | 39 | /* Includes ------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 0:f11903264b5e | 40 | |
davide.aliprandi@st.com | 0:f11903264b5e | 41 | #include "cmsis.h" |
davide.aliprandi@st.com | 0:f11903264b5e | 42 | #include "stm_divider_api.h" |
davide.aliprandi@st.com | 0:f11903264b5e | 43 | #include "component_def.h" |
davide.aliprandi@st.com | 0:f11903264b5e | 44 | |
davide.aliprandi@st.com | 0:f11903264b5e | 45 | |
davide.aliprandi@st.com | 0:f11903264b5e | 46 | /* Variables -----------------------------------------------------------------*/ |
davide.aliprandi@st.com | 0:f11903264b5e | 47 | |
davide.aliprandi@st.com | 0:f11903264b5e | 48 | static TIM_HandleTypeDef TimDividerHandle; |
davide.aliprandi@st.com | 0:f11903264b5e | 49 | static TIM_SlaveConfigTypeDef sSlaveConfig; |
davide.aliprandi@st.com | 0:f11903264b5e | 50 | static TIM_IC_InitTypeDef sICConfig; |
davide.aliprandi@st.com | 0:f11903264b5e | 51 | static TIM_OC_InitTypeDef sOCConfig; |
davide.aliprandi@st.com | 0:f11903264b5e | 52 | |
davide.aliprandi@st.com | 0:f11903264b5e | 53 | |
davide.aliprandi@st.com | 0:f11903264b5e | 54 | /* Functions -----------------------------------------------------------------*/ |
davide.aliprandi@st.com | 0:f11903264b5e | 55 | |
davide.aliprandi@st.com | 0:f11903264b5e | 56 | /** |
davide.aliprandi@st.com | 0:f11903264b5e | 57 | * @brief Initializing timer. |
davide.aliprandi@st.com | 0:f11903264b5e | 58 | * @param None. |
davide.aliprandi@st.com | 0:f11903264b5e | 59 | * @retval 0 in case of success, -1 otherwise. |
davide.aliprandi@st.com | 0:f11903264b5e | 60 | */ |
davide.aliprandi@st.com | 0:f11903264b5e | 61 | uint8_t ST_Timer_Init(void) |
davide.aliprandi@st.com | 0:f11903264b5e | 62 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 63 | GPIO_InitTypeDef GPIO_InitStruct; |
davide.aliprandi@st.com | 0:f11903264b5e | 64 | |
davide.aliprandi@st.com | 0:f11903264b5e | 65 | /* Enable AUDIO_TIMER clock*/ |
davide.aliprandi@st.com | 0:f11903264b5e | 66 | AUDIO_IN_TIMER_CLK_ENABLE(); |
davide.aliprandi@st.com | 0:f11903264b5e | 67 | AUDIO_IN_TIMER_CHOUT_GPIO_PORT_CLK_ENABLE(); |
davide.aliprandi@st.com | 0:f11903264b5e | 68 | AUDIO_IN_TIMER_CHIN_GPIO_PORT_CLK_ENABLE(); |
davide.aliprandi@st.com | 0:f11903264b5e | 69 | |
davide.aliprandi@st.com | 0:f11903264b5e | 70 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
davide.aliprandi@st.com | 0:f11903264b5e | 71 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
davide.aliprandi@st.com | 0:f11903264b5e | 72 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
davide.aliprandi@st.com | 0:f11903264b5e | 73 | |
davide.aliprandi@st.com | 0:f11903264b5e | 74 | GPIO_InitStruct.Alternate = AUDIO_IN_TIMER_CHIN_AF; |
davide.aliprandi@st.com | 0:f11903264b5e | 75 | GPIO_InitStruct.Pin = AUDIO_IN_TIMER_CHIN_PIN; |
davide.aliprandi@st.com | 0:f11903264b5e | 76 | HAL_GPIO_Init(AUDIO_IN_TIMER_CHIN_GPIO_PORT, &GPIO_InitStruct); |
davide.aliprandi@st.com | 0:f11903264b5e | 77 | |
davide.aliprandi@st.com | 0:f11903264b5e | 78 | GPIO_InitStruct.Alternate = AUDIO_IN_TIMER_CHOUT_AF; |
davide.aliprandi@st.com | 0:f11903264b5e | 79 | GPIO_InitStruct.Pin = AUDIO_IN_TIMER_CHOUT_PIN; |
davide.aliprandi@st.com | 0:f11903264b5e | 80 | HAL_GPIO_Init(AUDIO_IN_TIMER_CHOUT_GPIO_PORT, &GPIO_InitStruct); |
davide.aliprandi@st.com | 0:f11903264b5e | 81 | |
davide.aliprandi@st.com | 0:f11903264b5e | 82 | TimDividerHandle.Instance = AUDIO_IN_TIMER; |
davide.aliprandi@st.com | 0:f11903264b5e | 83 | |
davide.aliprandi@st.com | 0:f11903264b5e | 84 | /* Configure the Input: channel_1 */ |
davide.aliprandi@st.com | 0:f11903264b5e | 85 | sICConfig.ICPolarity = TIM_ICPOLARITY_RISING; |
davide.aliprandi@st.com | 0:f11903264b5e | 86 | sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; |
davide.aliprandi@st.com | 0:f11903264b5e | 87 | sICConfig.ICPrescaler = TIM_ICPSC_DIV1; |
davide.aliprandi@st.com | 0:f11903264b5e | 88 | sICConfig.ICFilter = 0; |
davide.aliprandi@st.com | 0:f11903264b5e | 89 | if (HAL_TIM_IC_ConfigChannel(&TimDividerHandle, &sICConfig, TIM_CHANNEL_1) != HAL_OK) |
davide.aliprandi@st.com | 0:f11903264b5e | 90 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 91 | return -1; |
davide.aliprandi@st.com | 0:f11903264b5e | 92 | } |
davide.aliprandi@st.com | 0:f11903264b5e | 93 | |
davide.aliprandi@st.com | 0:f11903264b5e | 94 | /* Configure TIM1 in Gated Slave mode for the external trigger (Filtered Timer |
davide.aliprandi@st.com | 0:f11903264b5e | 95 | Input 1) */ |
davide.aliprandi@st.com | 0:f11903264b5e | 96 | sSlaveConfig.InputTrigger = TIM_TS_TI1FP1; |
davide.aliprandi@st.com | 0:f11903264b5e | 97 | sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1; |
davide.aliprandi@st.com | 0:f11903264b5e | 98 | if (HAL_TIM_SlaveConfigSynchronization(&TimDividerHandle, &sSlaveConfig) != HAL_OK) |
davide.aliprandi@st.com | 0:f11903264b5e | 99 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 100 | return -1; |
davide.aliprandi@st.com | 0:f11903264b5e | 101 | } |
davide.aliprandi@st.com | 0:f11903264b5e | 102 | |
davide.aliprandi@st.com | 0:f11903264b5e | 103 | /* Initialize TIM3 peripheral in PWM mode*/ |
davide.aliprandi@st.com | 0:f11903264b5e | 104 | TimDividerHandle.Init.Period = 1; |
davide.aliprandi@st.com | 0:f11903264b5e | 105 | TimDividerHandle.Init.Prescaler = 0; |
davide.aliprandi@st.com | 0:f11903264b5e | 106 | TimDividerHandle.Init.ClockDivision = 0; |
davide.aliprandi@st.com | 0:f11903264b5e | 107 | TimDividerHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
davide.aliprandi@st.com | 0:f11903264b5e | 108 | TimDividerHandle.Init.RepetitionCounter = 0; |
davide.aliprandi@st.com | 0:f11903264b5e | 109 | if (HAL_TIM_PWM_Init(&TimDividerHandle) != HAL_OK) |
davide.aliprandi@st.com | 0:f11903264b5e | 110 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 111 | return -1; |
davide.aliprandi@st.com | 0:f11903264b5e | 112 | } |
davide.aliprandi@st.com | 0:f11903264b5e | 113 | |
davide.aliprandi@st.com | 0:f11903264b5e | 114 | /* Configure the PWM_channel_1 */ |
davide.aliprandi@st.com | 0:f11903264b5e | 115 | sOCConfig.OCMode = TIM_OCMODE_PWM1; |
davide.aliprandi@st.com | 0:f11903264b5e | 116 | sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
davide.aliprandi@st.com | 0:f11903264b5e | 117 | sOCConfig.Pulse = 1; |
davide.aliprandi@st.com | 0:f11903264b5e | 118 | if (HAL_TIM_PWM_ConfigChannel(&TimDividerHandle, &sOCConfig, TIM_CHANNEL_2) != HAL_OK) |
davide.aliprandi@st.com | 0:f11903264b5e | 119 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 120 | return -1; |
davide.aliprandi@st.com | 0:f11903264b5e | 121 | } |
davide.aliprandi@st.com | 0:f11903264b5e | 122 | |
davide.aliprandi@st.com | 0:f11903264b5e | 123 | return 0; |
davide.aliprandi@st.com | 0:f11903264b5e | 124 | } |
davide.aliprandi@st.com | 0:f11903264b5e | 125 | |
davide.aliprandi@st.com | 0:f11903264b5e | 126 | /** |
davide.aliprandi@st.com | 0:f11903264b5e | 127 | * @brief Starting timer. |
davide.aliprandi@st.com | 0:f11903264b5e | 128 | * @param None. |
davide.aliprandi@st.com | 0:f11903264b5e | 129 | * @retval 0 in case of success, -1 otherwise. |
davide.aliprandi@st.com | 0:f11903264b5e | 130 | */ |
davide.aliprandi@st.com | 0:f11903264b5e | 131 | uint8_t ST_Timer_Start(void) |
davide.aliprandi@st.com | 0:f11903264b5e | 132 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 133 | if (HAL_TIM_IC_Start(&TimDividerHandle, TIM_CHANNEL_1) != HAL_OK) |
davide.aliprandi@st.com | 0:f11903264b5e | 134 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 135 | return -1; |
davide.aliprandi@st.com | 0:f11903264b5e | 136 | } |
davide.aliprandi@st.com | 0:f11903264b5e | 137 | /* Start the Output Compare */ |
davide.aliprandi@st.com | 0:f11903264b5e | 138 | if (HAL_TIM_OC_Start(&TimDividerHandle, TIM_CHANNEL_2) != HAL_OK) |
davide.aliprandi@st.com | 0:f11903264b5e | 139 | { |
davide.aliprandi@st.com | 0:f11903264b5e | 140 | return -1; |
davide.aliprandi@st.com | 0:f11903264b5e | 141 | } |
davide.aliprandi@st.com | 0:f11903264b5e | 142 | |
davide.aliprandi@st.com | 0:f11903264b5e | 143 | return 0; |
davide.aliprandi@st.com | 0:f11903264b5e | 144 | } |