Skip to content

Commit a18f9ce

Browse files
committed
Add reset vector check to determine if the app was downloaded via bootloader
1 parent a0606a6 commit a18f9ce

File tree

9 files changed

+1367
-23
lines changed

9 files changed

+1367
-23
lines changed

secure_boot_and_secure_firmware_upgrade_over_canfd/app.X/app.mc3

Lines changed: 44 additions & 2 deletions
Large diffs are not rendered by default.

secure_boot_and_secure_firmware_upgrade_over_canfd/app.X/main.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@
2020
*/
2121
#include "mcc_generated_files/system/system.h"
2222
#include "mcc_generated_files/system/pins.h"
23+
#include "mcc_generated_files/flash/flash.h"
24+
#include "mcc_generated_files/flash/flash_types.h"
25+
#include "mcc_generated_files/boot/boot_config.h"
2326

2427
/*
2528
Main application
2629
*/
2730

2831
int main(void) {
29-
unsigned int counter = 0;
3032

3133
SYSTEM_Initialize();
32-
33-
while (1) {
34-
if ((counter++ % 0x8000) == 0) {
35-
IO_RE9_Toggle();
34+
35+
flash_data_t flashData[2];
36+
FLASH_Read(0x000000, 2, flashData);
37+
uint16_t resetAddressLower = flashData[0];
38+
uint32_t combinedResetAddress = (flashData[1]<<16) | resetAddressLower;
39+
40+
while (1)
41+
{
42+
if (combinedResetAddress < BOOT_CONFIG_PROGRAMMABLE_ADDRESS_LOW)
43+
{
44+
// ICSP Feature
3645
}
3746
}
3847
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
/**
3+
* FLASH Generated Driver Header File
4+
*
5+
* @file flash.h
6+
*
7+
* @ingroup Flash Driver
8+
*
9+
* @brief FLASH driver using dsPIC MCUs
10+
*
11+
* @skipline @version Firmware Driver Version 1.1.0
12+
*
13+
* @skipline @version PLIB Version 3.2.1
14+
*
15+
* @skipline Device : dsPIC33CK1024MP710
16+
*/
17+
18+
/*
19+
© [2024] Microchip Technology Inc. and its subsidiaries.
20+
21+
Subject to your compliance with these terms, you may use Microchip
22+
software and any derivatives exclusively with Microchip products.
23+
You are responsible for complying with 3rd party license terms
24+
applicable to your use of 3rd party software (including open source
25+
software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
26+
NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
27+
SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
28+
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
29+
WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
30+
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
31+
KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
32+
MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
33+
FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
34+
TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
35+
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
36+
THIS SOFTWARE.
37+
*/
38+
39+
#ifndef FLASH_H
40+
#define FLASH_H
41+
42+
#include <stdint.h>
43+
#include <stddef.h>
44+
#include <stdbool.h>
45+
#include "flash_hardware.h"
46+
#include "flash_interface.h"
47+
48+
// Section: Data Type Definitions
49+
50+
/**
51+
@ingroup flashdriver
52+
@brief Structure object of type FLASH_INTERFACE assigned with name
53+
displayed in the Melody Driver User interface. A structure pointer can be used to achieve portability
54+
across the FLASH having same interface structure.
55+
*/
56+
57+
extern const struct FLASH_INTERFACE flash;
58+
59+
/**
60+
* @ingroup flashdriver
61+
* @brief This function erases a page.
62+
* @param[in] flashAddress : Flash address
63+
* @param[in] unlockKey : Pointer of the data to be written
64+
* @return FLASH_RETURN_STATUS: returns FLASH_NO_ERROR if operation is successful , else returns errors like FLASH_INVALID_ADDRESS, FLASH_WRITE_ERROR
65+
*/
66+
enum FLASH_RETURN_STATUS FLASH_PageErase(flash_adr_t flashAddress, flash_key_t unlockKey);
67+
68+
69+
/**
70+
* @ingroup flashdriver
71+
* @brief This function writes the specified data to the specified address.
72+
* @param[in] flashAddress : Flash address
73+
* @param[in] data : Pointer of the data to be written.
74+
* This will write the TWO WORDS pointed to by the pointer.
75+
* @return FLASH_RETURN_STATUS: returns FLASH_NO_ERROR if operation is successful , else returns errors like FLASH_INVALID_ADDRESS, FLASH_INVALID_DATA, FLASH_WRITE_ERROR
76+
*/
77+
enum FLASH_RETURN_STATUS FLASH_WordWrite(flash_adr_t flashAddress, flash_data_t *data, flash_key_t unlockKey);
78+
79+
/**
80+
* @ingroup flashdriver
81+
* @brief This function writes the specified data to the specified address.
82+
* @param[in] flashAddress : Flash address
83+
* @param[in] data : Pointer of the data to be written
84+
* @return FLASH_RETURN_STATUS: returns FLASH_NO_ERROR if operation is successful , else returns errors like FLASH_INVALID_ADDRESS, FLASH_INVALID_DATA, FLASH_WRITE_ERROR
85+
*/
86+
enum FLASH_RETURN_STATUS FLASH_RowWrite(flash_adr_t flashAddress, flash_data_t *data, flash_key_t unlockKey);
87+
88+
/**
89+
* @ingroup flashdriver
90+
* @brief This function reads the data from the specified address.
91+
* @param[in] flashAddress : Flash address of
92+
* @param[out] data : Pointer to read the data
93+
* @return FLASH_RETURN_STATUS: returns FLASH_NO_ERROR if operation is successful , else returns errors like FLASH_INVALID_ADDRESS, FLASH_INVALID_DATA
94+
*/
95+
enum FLASH_RETURN_STATUS FLASH_Read(flash_adr_t flashAddress, size_t count, flash_data_t *data);
96+
97+
98+
#endif /* FLASH_H */
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
2+
/**
3+
* FLASH Generated Driver Header File
4+
*
5+
* @file flash.h
6+
*
7+
* @ingroup Flash Driver
8+
*
9+
* @brief This is the generated driver header file for the FLASH driver
10+
*
11+
* @skipline @version Firmware Driver Version 1.1.0
12+
*
13+
* @skipline @version PLIB Version 3.2.1
14+
*
15+
* @skipline Device : dsPIC33CK1024MP710
16+
*/
17+
18+
/*
19+
© [2024] Microchip Technology Inc. and its subsidiaries.
20+
21+
Subject to your compliance with these terms, you may use Microchip
22+
software and any derivatives exclusively with Microchip products.
23+
You are responsible for complying with 3rd party license terms
24+
applicable to your use of 3rd party software (including open source
25+
software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
26+
NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
27+
SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
28+
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
29+
WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
30+
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
31+
KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
32+
MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
33+
FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
34+
TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
35+
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
36+
THIS SOFTWARE.
37+
*/
38+
39+
#ifndef FLASH_HARDWARE_H
40+
#define FLASH_HARDWARE_H
41+
42+
#include <xc.h>
43+
#include <stdint.h>
44+
#include <stdbool.h>
45+
#include "flash_types.h"
46+
47+
/**
48+
@ingroup flashdriver
49+
@brief Defines FLASH Flash PC unit size
50+
*/
51+
#define FLASH_PC_UNIT_SIZE 2U
52+
/**
53+
@ingroup flashdriver
54+
@brief Defines FLASH unlock key value
55+
*/
56+
#define FLASH_UNLOCK_KEY 0x00AA0055
57+
/**
58+
@ingroup flashdriver
59+
@brief Defines FLASH write row size in instruction counts
60+
*/
61+
#define FLASH_WRITE_ROW_SIZE_IN_INSTRUCTIONS 128U
62+
/**
63+
@ingroup flashdriver
64+
@brief Defines FLASH write row size in PC counts
65+
*/
66+
#define FLASH_WRITE_ROW_SIZE_IN_PC_UNITS (FLASH_WRITE_ROW_SIZE_IN_INSTRUCTIONS * FLASH_PC_UNIT_SIZE)
67+
/**
68+
@ingroup flashdriver
69+
@brief Defines FLASH erase page size in instruction counts
70+
*/
71+
#define FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS 1024U
72+
/**
73+
@ingroup flashdriver
74+
@brief Defines FLASH erase page size in PC counts
75+
*/
76+
#define FLASH_ERASE_PAGE_SIZE_IN_PC_UNITS (FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS * FLASH_PC_UNIT_SIZE)
77+
/**
78+
@ingroup flashdriver
79+
@brief Defines if FLASH has ECC
80+
*/
81+
#define FLASH_HAS_ECC 1
82+
/**
83+
@ingroup flashdriver
84+
@brief Defines FLASH erase page mask value
85+
*/
86+
#define FLASH_ERASE_PAGE_MASK (~(FLASH_ERASE_PAGE_SIZE_IN_PC_UNITS - 1U))
87+
/**
88+
@ingroup flashdriver
89+
@brief Defines FLASH odd address mask value
90+
*/
91+
#define FLASH_ODD_ADDRESS_MASK 1U
92+
/**
93+
@ingroup flashdriver
94+
@brief Defines FLASH error mask value
95+
*/
96+
#define FLASH_ERROR_MASK 0x2100
97+
98+
/**
99+
* @ingroup flashdriver
100+
* @brief This routine erases a page indicated by the page aligned address.
101+
* @param[in] address - Flash page aligned address.
102+
* @return bool - Returns True if operation successful else returns false.
103+
*/
104+
bool FLASH_ErasePage(flash_adr_t address, flash_key_t unlockKey);
105+
106+
/* The address in the Read and Write word functions below must be even */
107+
108+
/**
109+
* @ingroup flashdriver
110+
* @brief This routine reads a 24 bit instruction from the indicated address in flash.
111+
* @param[in] address - Flash address.
112+
* @return uint32_t - 24 bit data.
113+
*/
114+
uint32_t FLASH_ReadWord24(flash_adr_t address);
115+
116+
117+
/**
118+
* @ingroup flashdriver
119+
* @brief This routine reads a 16 bit instruction from the indicated address in flash.
120+
* @param[in] address - Flash address.
121+
* @return uint16_t - 16 bit data.
122+
*/
123+
uint16_t FLASH_ReadWord16(flash_adr_t address);
124+
125+
126+
/**
127+
* @ingroup flashdriver
128+
* @brief This routine writes two 24 bit instructions to the indicated address in flash.
129+
* @param[in] address - Flash address.
130+
* @param[in] data1 - First 24 bit data to be written(First word).
131+
* @param[in] data2 - Second 24 bit data to be written(Second word).
132+
* @return bool - Returns true if operation is successful else returns false.
133+
*/
134+
bool FLASH_WriteDoubleWord24(flash_adr_t address, flash_data_t Data0, flash_data_t Data1, flash_key_t unlockKey);
135+
136+
137+
/**
138+
* @ingroup flashdriver
139+
* @brief This routine writes two ,16 bit instructions to the indicated address in flash.
140+
* @param[in] address - Flash address.
141+
* @param[in] data1 - First 16 bit data to be written(First word).
142+
* @param[in] data2 - Second 16 bit data to be written(Second word).
143+
* @return bool - Returns true if operation is successful else returns false.
144+
*/
145+
bool FLASH_WriteDoubleWord16(flash_adr_t flashAddress, uint16_t Data0, uint16_t Data1, flash_key_t unlockKey);
146+
147+
148+
/* Program the flash one row at a time. */
149+
150+
151+
/**
152+
* @ingroup flashdriver
153+
* @brief
154+
* This routine writes a single row of data from the location given in *data to the flash location in address.
155+
* Since the flash is only 24 bits wide all data in the upper 8 bits of the source will be lost.The address in
156+
* *data must be row aligned.
157+
* @param[in] address - Flash address.
158+
* @param[in] *data - Address of the data to be written.
159+
* @return bool - Returns true if operation is successful else returns false.
160+
*/
161+
bool FLASH_WriteRow24(flash_adr_t flashAddress, flash_data_t *data, flash_key_t unlockKey);
162+
163+
164+
/**
165+
* @ingroup flashdriver
166+
* @brief
167+
* This routine writes a single row of data from the location given in *data to the flash location in address.
168+
* Each 16 bit source data word is stored in the lower 16 bits of each flash entry and the upper 8 bits of the
169+
* flash is not programmed.The address in *data must be row aligned.
170+
* @param[in] address - Flash address.
171+
* @param[in] *data - Address of the data to be written.
172+
* @return bool - Returns true if operation is successful else returns false.
173+
*/
174+
bool FLASH_WriteRow16(flash_adr_t address, uint16_t *data, flash_key_t unlockKey);
175+
176+
177+
/**
178+
* @ingroup flashdriver
179+
* @brief This routiner returns the page offset the given flash address.
180+
* @param[in] address - Flash address.
181+
* @return uint32_t - returns offset.
182+
*/
183+
uint16_t FLASH_ErasePageOffsetGet(flash_adr_t address);
184+
185+
186+
/**
187+
* @ingroup flashdriver
188+
* @brief This routine returns the page aligned address for a given flash address..
189+
* @param[in] address - Flash address.
190+
* @return uint32_t - Returns page aligned flash address.
191+
*/
192+
uint32_t FLASH_ErasePageAddressGet(flash_adr_t address);
193+
194+
#endif /* FLASH_HARDWARE_H */

0 commit comments

Comments
 (0)