forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psoc6.c
169 lines (142 loc) · 4.21 KB
/
psoc6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* psoc6.c
*
* Copyright (C) 2021 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <stdint.h>
#include <target.h>
#include <string.h>
#include "image.h"
#include "cy_device_headers.h"
#include "cy_flash.h"
#include "cy_syspm.h"
#include "cy_sysclk.h"
#include "cy_syslib.h"
#include "cy_ipc_drv.h"
#ifdef WOLFSSL_PSOC6_CRYPTO
#include "wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h"
#endif
#include "psoc6_02_config.h"
#define ROW_SIZE (WOLFBOOT_SECTOR_SIZE)
#define FLASH_BASE_ADDRESS (0x10000000)
#define CPU_FREQ (100000000)
uint8_t psoc6_write_buffer[ROW_SIZE];
#ifndef NVM_FLASH_WRITEONCE
# error "wolfBoot psoc6 HAL: no WRITEONCE support detected. Please define NVM_FLASH_WRITEONCE"
#endif
#ifdef __WOLFBOOT
static const cy_stc_pll_manual_config_t srss_0_clock_0_pll_0_pllConfig =
{
.feedbackDiv = 100,
.referenceDiv = 2,
.outputDiv = 4,
.lfMode = false,
.outputMode = CY_SYSCLK_FLLPLL_OUTPUT_AUTO,
};
static void hal_set_pll(void)
{
/*Set clock path 1 source to IMO, this feeds PLL1*/
Cy_SysClk_ClkPathSetSource(1U, CY_SYSCLK_CLKPATH_IN_IMO);
/*Set the input for CLK_HF0 to the output of the PLL, which is on clock path 1*/
Cy_SysClk_ClkHfSetSource(0U, CY_SYSCLK_CLKHF_IN_CLKPATH1);
Cy_SysClk_ClkHfSetDivider(0U, CY_SYSCLK_CLKHF_NO_DIVIDE);
/*Set divider for CM4 clock to 0, might be able to lower this to save power if needed*/
Cy_SysClk_ClkFastSetDivider(0U);
/*Set divider for peripheral and CM0 clock to 0 - This must be 0 to get fastest clock to CM0*/
Cy_SysClk_ClkPeriSetDivider(0U);
/*Set divider for CM0 clock to 0*/
Cy_SysClk_ClkSlowSetDivider(0U);
/*Set flash memory wait states */
Cy_SysLib_SetWaitStates(false, 100);
/*Configure PLL for 100 MHz*/
if (CY_SYSCLK_SUCCESS != Cy_SysClk_PllManualConfigure(1U, &srss_0_clock_0_pll_0_pllConfig))
{
while(1)
;
}
/*Enable PLL*/
if (CY_SYSCLK_SUCCESS != Cy_SysClk_PllEnable(1U, 10000u))
{
while(1)
;
}
}
void hal_init(void)
{
#define VTOR (*(volatile uint32_t *)(0xE000ED08))
VTOR = FLASH_BASE_ADDRESS;
#undef VTOR
Cy_PDL_Init(CY_DEVICE_CFG);
Cy_Flash_Init();
hal_set_pll();
#ifdef WOLFSSL_PSOC6_CRYPTO
psoc6_crypto_port_init();
#endif
}
void hal_prepare_boot(void)
{
}
#endif
/* Only Row-aligned writes allowed. This is guaranteed by wolfBoot if NVM_CACHE is
* in use (via NVM_FLASH_WRITEONCE=1), as unaligned writes become cached.
*/
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
{
const uint8_t *src = data;
int ret;
if (len < ROW_SIZE)
return -1;
if ((((uint32_t)data) & FLASH_BASE_ADDRESS) == FLASH_BASE_ADDRESS) {
if (len != ROW_SIZE) {
return -1;
}
memcpy(psoc6_write_buffer, data, len);
src = psoc6_write_buffer;
}
while (len) {
ret = Cy_Flash_ProgramRow(address, (const uint32_t *) src);
if (ret)
return ret;
len -= ROW_SIZE;
if ((len > 0) && (len < ROW_SIZE))
return -1;
}
return 0;
}
void RAMFUNCTION hal_flash_unlock(void)
{
}
void RAMFUNCTION hal_flash_lock(void)
{
}
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
{
uint32_t end_address;
int ret;
uint32_t p = (uint32_t)address;
if (len == 0)
return -1;
end_address = address + len;
while ((end_address - p) >= ROW_SIZE) {
ret = Cy_Flash_EraseRow(p);
if (ret)
return ret;
p += ROW_SIZE;
}
return 0;
}