2727
2828#include "cy_flash.h"
2929#include "cy_syspm.h"
30+ #include "cy_sysclk.h"
31+ #include "cy_syslib.h"
3032#include "cy_ipc_drv.h"
3133
34+ #define ROW_SIZE (0x200)
35+ #define CPU_FREQ (100000000)
36+
3237#ifndef NVM_FLASH_WRITEONCE
3338# error "wolfBoot psoc6 HAL: no WRITEONCE support detected. Please define NVM_FLASH_WRITEONCE"
3439#endif
3540
41+ #if (NVM_CACHE_SIZE != ROW_SIZE )
42+ # error "Wrong NVM_CACHE_SIZE specified for this platform. Please set NVM_CACHE_SIZE to match ROW_SIZE"
43+ #endif
3644
3745#ifdef __WOLFBOOT
46+ /* Replace Cy_SysLib_DelayUs with a custom call that does not use SysTick
47+ * (required by Cy_SysClk_PllEnable)
48+ */
49+
50+ #if 0
51+ void Cy_SysLib_DelayUs (uint16_t delay_us )
52+ {
53+ volatile unsigned int i ;
54+ uint32_t cycles = ((CPU_FREQ / 1000000 )) * delay_us ;
55+ for (i = 0 ; i < cycles ; i ++ ) {
56+ asm volatile ("nop" );
57+ }
58+ }
59+ #endif
60+
61+ static const cy_stc_pll_manual_config_t srss_0_clock_0_pll_0_pllConfig =
62+ {
63+ .feedbackDiv = 100 ,
64+ .referenceDiv = 2 ,
65+ .outputDiv = 4 ,
66+ .lfMode = false,
67+ .outputMode = CY_SYSCLK_FLLPLL_OUTPUT_AUTO ,
68+ };
69+
3870void hal_init (void )
3971{
40- /* TODO: how to set clock full speed? */
72+ SystemInit ();
73+ #if 0
74+ /*Set clock path 1 source to IMO, this feeds PLL1*/
75+ Cy_SysClk_ClkPathSetSource (1U , CY_SYSCLK_CLKPATH_IN_IMO );
76+
77+ /*Set the input for CLK_HF0 to the output of the PLL, which is on clock path 1*/
78+ Cy_SysClk_ClkHfSetSource (0U , CY_SYSCLK_CLKHF_IN_CLKPATH1 );
79+ Cy_SysClk_ClkHfSetDivider (0U , CY_SYSCLK_CLKHF_NO_DIVIDE );
80+
81+ /*Set divider for CM4 clock to 0, might be able to lower this to save power if needed*/
82+ Cy_SysClk_ClkFastSetDivider (0U );
83+ /*Set divider for peripheral and CM0 clock to 0 - This must be 0 to get fastest clock to CM0*/
84+ Cy_SysClk_ClkPeriSetDivider (0U );
85+ /*Set divider for CM0 clock to 0*/
86+ Cy_SysClk_ClkSlowSetDivider (0U );
87+
88+ /*Configure PLL for 100 MHz*/
89+ if (CY_SYSCLK_SUCCESS != Cy_SysClk_PllManualConfigure (1U , & srss_0_clock_0_pll_0_pllConfig ))
90+ {
91+ while (1 )
92+ ;
93+ }
94+ /*Enable PLL*/
95+ if (CY_SYSCLK_SUCCESS != Cy_SysClk_PllEnable (1U , 10000u ))
96+ {
97+ while (1 )
98+ ;
99+ }
100+ #endif
101+ Cy_Flash_Init ();
41102}
42103
43104void hal_prepare_boot (void )
@@ -47,11 +108,20 @@ void hal_prepare_boot(void)
47108
48109#endif
49110
111+
112+ /* Only Row-aligned writes allowed. This is guaranteed by wolfBoot if NVM_CACHE is
113+ * in use (via NVM_FLASH_WRITEONCE=1), as unaligned writes become cached.
114+ */
50115int RAMFUNCTION hal_flash_write (uint32_t address , const uint8_t * data , int len )
51116{
52- if (len != WOLFBOOT_SECTOR_SIZE )
117+ if (len < NVM_CACHE_SIZE )
53118 return -1 ;
54- Cy_Flash_WriteRow (address ,(const uint32_t * ) data );
119+ while (len ) {
120+ Cy_Flash_WriteRow (address , (const uint32_t * ) data );
121+ len -= NVM_CACHE_SIZE ;
122+ if ((len > 0 ) && (len < NVM_CACHE_SIZE ))
123+ return -1 ;
124+ }
55125 return 0 ;
56126}
57127
@@ -67,12 +137,16 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
67137{
68138 int start = -1 , end = -1 ;
69139 uint32_t end_address ;
70- uint32_t p ;
140+ uint32_t p = ( uint32_t ) address ;
71141 if (len == 0 )
72142 return -1 ;
73- end_address = address + len - 1 ;
74- for (p = address ; p < end_address ; p += WOLFBOOT_SECTOR_SIZE ) {
143+ end_address = address + len ;
144+ /* Assume NVM_CACHE_SIZE is always defined for this platform
145+ * (see #error statements above)
146+ * */
147+ while ((end_address - p ) >= NVM_CACHE_SIZE ) {
75148 Cy_Flash_EraseRow (p );
149+ p += NVM_CACHE_SIZE ;
76150 }
77151 return 0 ;
78152}
0 commit comments