|
- /*-
- * Copyright 2021 John-Mark Gurney.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
- #include <string.h>
-
- #include <misc.h>
-
- #include <sysinit.h>
-
- #include <main.h>
-
- static void
- c13led(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
-
- GPIO_InitStruct = (GPIO_InitTypeDef){
- .Pin = GPIO_PIN_3,
- .Mode = GPIO_MODE_OUTPUT_PP,
- .Pull = GPIO_NOPULL,
- .Speed = GPIO_SPEED_FREQ_LOW,
- };
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct = (GPIO_InitTypeDef){
- .Pin = GPIO_PIN_13,
- .Mode = GPIO_MODE_OUTPUT_PP,
- .Pull = GPIO_NOPULL,
- .Speed = GPIO_SPEED_FREQ_LOW,
- };
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
- }
- SYSINIT_VF(c13led, SI_SUB_HAL, SI_ORDER_SECOND, c13led);
-
- static TIM_HandleTypeDef htim2;
-
- /* 36.2.2 Step 1 */
- void
- HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
- {
- }
-
- #define PERIOD_VALUE (700 - 1)
-
- void
- tim2ch4(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- uint32_t uhPrescalerValue;
-
- /*
- * various parts from F1's
- * Projects/STM3210E_EVAL/Examples/TIM/TIM_PWMOutput/Src/main.c
- */
- uhPrescalerValue = (uint32_t)(SystemCoreClock / 2000000) - 1;
-
- /* 36.2.2 Step 2a */
- __HAL_RCC_TIM2_CLK_ENABLE();
-
- /* 36.2.2 Step 2b */
- __HAL_RCC_GPIOA_CLK_ENABLE();
-
- GPIO_InitStruct = (GPIO_InitTypeDef){
- .Pin = GPIO_PIN_3,
- .Mode = GPIO_MODE_AF_PP,
- .Pull = GPIO_NOPULL,
- .Speed = GPIO_SPEED_FREQ_LOW,
- };
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- /* 36.2.2 Step 4 */
- htim2 = (TIM_HandleTypeDef){
- .Instance = TIM2,
-
- /* TIM_Base_InitTypeDef */
- .Init.Prescaler = uhPrescalerValue,
- .Init.Period = PERIOD_VALUE,
- .Init.CounterMode = TIM_COUNTERMODE_UP,
- .Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE,
- };
- HAL_TIM_PWM_Init(&htim2);
-
- }
- SYSINIT_VF(tim2ch4, SI_SUB_HAL, SI_ORDER_THIRD, tim2ch4);
-
- int
- main(void)
- {
-
- debug_printf("starting...\n");
-
- #if 0
- /* toggle on LED */
- for (;;) {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
- HAL_Delay(250);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
- HAL_Delay(250);
- }
- #else
- /* PA3 TIM2_CH4 */
- TIM_OC_InitTypeDef oconfig;
- oconfig = (TIM_OC_InitTypeDef){
- .OCMode = TIM_OCMODE_PWM1,
- .OCPolarity = TIM_OCPOLARITY_HIGH,
- .OCFastMode = TIM_OCFAST_DISABLE,
- .OCNPolarity = TIM_OCNPOLARITY_HIGH,
- .OCNIdleState = TIM_OCNIDLESTATE_RESET,
-
- .OCIdleState = TIM_OCIDLESTATE_RESET,
-
- .Pulse = PERIOD_VALUE, // OFF
- };
- HAL_TIM_PWM_ConfigChannel(&htim2, &oconfig, TIM_CHANNEL_4);
- HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
- for (int i = 0;; i = (i + 1) % 3) {
- switch (i) {
- case 0:
- TIM2->CCR4 = PERIOD_VALUE;
- break;
- case 1:
- TIM2->CCR4 = PERIOD_VALUE / 2;
- break;
- case 2:
- TIM2->CCR4 = 0;
- break;
- }
- #if 0
- HAL_Delay(100);
- #else
- HAL_Delay(35);
- #endif
- }
- HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
- #endif
- }
|