1 /*
2  * Copyright (c) 2020 Cypress Semiconductor Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6  /*
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *  http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  */
24  /*******************************************************************************/
25 
26 #include "system_psoc6.h"
27 #include "cy_pdl.h"
28 #include "cyhal.h"
29 #include "cy_retarget_io.h"
30 #include "watchdog.h"
31 
32 /* Define pins for UART debug output */
33 
34 #define CY_DEBUG_UART_TX (P5_1)
35 #define CY_DEBUG_UART_RX (P5_0)
36 
37 #if defined(PSOC_062_2M)
38 #warning "Check if User LED is correct for your target board."
39 #define LED_PORT GPIO_PRT13
40 #define LED_PIN 7U
41 #elif defined(PSOC_062_1M)
42 #define LED_PORT GPIO_PRT13
43 #define LED_PIN 7U
44 #elif defined(PSOC_062_512K)
45 #define LED_PORT GPIO_PRT11
46 #define LED_PIN 1U
47 #endif
48 
49 #define LED_NUM 5U
50 #define LED_DRIVEMODE CY_GPIO_DM_STRONG_IN_OFF
51 #define LED_INIT_DRIVESTATE 1
52 
53 const cy_stc_gpio_pin_config_t LED_config =
54 {
55     .outVal = 1,
56     .driveMode = CY_GPIO_DM_STRONG_IN_OFF,
57     .hsiom = HSIOM_SEL_GPIO,
58     .intEdge = CY_GPIO_INTR_DISABLE,
59     .intMask = 0UL,
60     .vtrip = CY_GPIO_VTRIP_CMOS,
61     .slewRate = CY_GPIO_SLEW_FAST,
62     .driveSel = CY_GPIO_DRIVE_FULL,
63     .vregEn = 0UL,
64     .ibufMode = 0UL,
65     .vtripSel = 0UL,
66     .vrefSel = 0UL,
67     .vohSel = 0UL,
68 };
69 
70 #define WATCHDOG_UPD_MESSAGE  "[BlinkyApp] Update watchdog timer started in MCUBootApp to mark successful start of user app\r\n"
71 #define WATCHDOG_FREE_MESSAGE "[BlinkyApp] Turn off watchdog timer\r\n"
72 
73 #ifdef BOOT_IMG
74     #define BLINK_PERIOD          (1000u)
75     #define GREETING_MESSAGE_VER  "[BlinkyApp] BlinkyApp v1.0 [CM4]\r\n"
76     #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 1 sec period\r\n"
77 #elif defined(UPGRADE_IMG)
78     #define BLINK_PERIOD          (250u)
79     #define GREETING_MESSAGE_VER  "[BlinkyApp] BlinkyApp v2.0 [+]\r\n"
80     #define GREETING_MESSAGE_INFO "[BlinkyApp] Red led blinks with 0.25 sec period\r\n"
81 #else
82     #error "[BlinkyApp] Please specify type of image: -DBOOT_IMG or -DUPGRADE_IMG\r\n"
83 #endif
84 
check_result(int res)85 void check_result(int res)
86 {
87     if (res != CY_RSLT_SUCCESS) {
88         CY_ASSERT(0);
89     }
90 }
91 
test_app_init_hardware(void)92 void test_app_init_hardware(void)
93 {
94     /* enable interrupts */
95     __enable_irq();
96 
97     /* Disabling watchdog so it will not interrupt normal flow later */
98     Cy_GPIO_Pin_Init(LED_PORT, LED_PIN, &LED_config);
99     /* Initialize retarget-io to use the debug UART port */
100     check_result(cy_retarget_io_init(CY_DEBUG_UART_TX, CY_DEBUG_UART_RX,
101                                      CY_RETARGET_IO_BAUDRATE));
102 
103     printf("\n===========================\r\n");
104     printf(GREETING_MESSAGE_VER);
105     printf("===========================\r\n");
106 
107     printf("[BlinkyApp] GPIO initialized \r\n");
108     printf("[BlinkyApp] UART initialized \r\n");
109     printf("[BlinkyApp] Retarget I/O set to 115200 baudrate \r\n");
110 
111 }
112 
main(void)113 int main(void)
114 {
115     uint32_t blinky_period = BLINK_PERIOD;
116 
117     test_app_init_hardware();
118 
119     printf(GREETING_MESSAGE_INFO);
120 
121     /* Update watchdog timer to mark successful start up of application */
122     printf(WATCHDOG_UPD_MESSAGE);
123     cy_wdg_kick();
124     printf(WATCHDOG_FREE_MESSAGE);
125     cy_wdg_free();
126 
127     for (;;)
128     {
129         /* Toggle the user LED periodically */
130         Cy_SysLib_Delay(blinky_period/2);
131 
132         /* Invert the USER LED state */
133         Cy_GPIO_Inv(LED_PORT, LED_PIN);
134     }
135     return 0;
136 }
137