blob: dfe2ffd3e83541ddbac2e9198339dc39a39762d4 [file] [log] [blame]
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +00001/* Copyright 2022, Infineon Technologies AG. All rights reserved.
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 */
20
21#include "timebase_us.h"
22
23#include "cy_pdl.h"
24#include "bootutil/bootutil_log.h"
25
26static const cy_stc_tcpwm_counter_config_t tcpwm_config =
27{
28 .period = 0xFFFFFFFFU,
29 .clockPrescaler = CY_TCPWM_COUNTER_PRESCALER_DIVBY_8, /* Clk_counter = Clk_input / 4 */
30 .runMode = CY_TCPWM_COUNTER_CONTINUOUS, /* Wrap around at terminal count. */
31 .countDirection = CY_TCPWM_COUNTER_COUNT_UP, /* Up counter, counting from 0 to period value. */
32 .compareOrCapture = CY_TCPWM_COUNTER_MODE_COMPARE, /* Trigger interrupt/event signal when Counter value is equal to Compare0 */
33 .compare0 = 0U,
34 .compare1 = 0U,
35 .enableCompareSwap = false,
36 .interruptSources = CY_TCPWM_INT_NONE,
37 .captureInputMode = CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
38 .captureInput = CY_TCPWM_INPUT_0,
39 .reloadInputMode = CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
40 .reloadInput = CY_TCPWM_INPUT_0,
41 .startInputMode = CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
42 .startInput = CY_TCPWM_INPUT_0,
43 .stopInputMode = CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
44 .stopInput = CY_TCPWM_INPUT_0,
45 .countInputMode = CY_TCPWM_INPUT_LEVEL, /* Set this input to LEVEL and 1 (high logic level) */
46 .countInput = CY_TCPWM_INPUT_1 /* So the counter will count input clock periods (Clk_counter, taking into account the clock prescaler) */
47};
48
49/*******************************************************************************
50* Function Name: timebase_us_init
51****************************************************************************//**
52*
53* \brief Performs initialization of the TCPWM0 block as a microsecond time source.
54*
55*/
56void timebase_us_init(void)
57{
58#ifdef CYW20829
59 (void) Cy_SysClk_PeriphAssignDivider(PCLK_TCPWM0_CLOCK_COUNTER_EN0, CY_SYSCLK_DIV_8_BIT, 0UL);
60#else
61 (void) Cy_SysClk_PeriphAssignDivider(PCLK_TCPWM0_CLOCKS0, CY_SYSCLK_DIV_8_BIT, 0UL);
62#endif
63 (void) Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT, 0UL, 0UL);
64 (void) Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, 0UL);
65
66 (void) Cy_TCPWM_Counter_Init(TCPWM0, 0, &tcpwm_config);
67 Cy_TCPWM_Counter_Enable(TCPWM0, 0);
68 Cy_TCPWM_TriggerStart_Single(TCPWM0, 0);
69}
70
71/*******************************************************************************
72* Function Name: timebase_us_deinit
73****************************************************************************//**
74*
75* \brief Performs deinitialization of the TCPWM0.
76*
77*/
78void timebase_us_deinit(void)
79{
80 (void) Cy_SysClk_PeriphDisableDivider(CY_SYSCLK_DIV_8_BIT, 0UL);
81
82 Cy_TCPWM_Counter_DeInit(TCPWM0, 0, &tcpwm_config);
83 Cy_TCPWM_Counter_Disable(TCPWM0, 0);
84 Cy_TCPWM_TriggerStopOrKill_Single(TCPWM0, 0);
85}
86
87/*******************************************************************************
88* Function Name: timebase_us_get_tick
89****************************************************************************//**
90*
91* \brief Returns current timer counter value
92*
93* \return current timer counter value as uint32_t
94*
95*/
96uint32_t timebase_us_get_tick(void)
97{
98 return Cy_TCPWM_Counter_GetCounter(TCPWM0, 0);
99}