Skip to content

Commit e2c5e8b

Browse files
aiwyatt787davidflowers
authored andcommitted
Pull request #23: Feature/MCU16GITHUB-883 determine a way to verify if the application was programmed or loaded via bo
Merge in MCU16CE/dspic33-dsc-bootloader-code-examples from ~C74569/dspic33-dsc-bootloader-code-examples:feature/MCU16GITHUB-883-determine-a-way-to-verify-if-the-application-was-programmed-or-loaded-via-bo to feature/MCU16GITHUB-842-add-the-icsp-inhibit-feature * commit '9a4ce474499046b566a8650d6c8cf30f6f9e7bf1': Remove flash files from app Revert changes in app and add to icsp project Limit function scope Remove pin testing Fix masking and move to correct function Pull functionality out into functions, mask upper 16 bits of opcode, and add information about the GOTO instruction Retain post build workaround for Jenkin pipeline Add reset vector check to determine if the app was downloaded via bootloader
2 parents f2c7e7d + 9a4ce47 commit e2c5e8b

File tree

9 files changed

+1370
-2
lines changed

9 files changed

+1370
-2
lines changed

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/icsp_inhibit.mc3

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

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/main.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include <stdio.h>
2323
#include <string.h>
2424
#include <stdbool.h>
25+
#include "mcc_generated_files/flash/flash.h"
26+
#include "mcc_generated_files/flash/flash_types.h"
27+
#include "mcc_generated_files/boot/boot_config.h"
2528

2629
#define WINDOW_SIZE 10
2730
#define UNLOCK_COMMAND "LOCKDEVICE"
@@ -36,6 +39,8 @@ static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex
3639
static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex);
3740
static void ResetWindowOnMismatch(char *window, int *windowIndex);
3841
static void CheckForUnlockCommand(char *window, int *windowIndex);
42+
static uint32_t GetResetAddress();
43+
static bool WasLoadedByBootloader();
3944

4045
int main(void)
4146
{
@@ -129,4 +134,32 @@ static void CheckForUnlockCommand(char *window, int *windowIndex)
129134
*windowIndex = 0;
130135
memset(window, 0, WINDOW_SIZE + 1);
131136
}
137+
}
138+
139+
/* The following code finds the address used by GOTO instruction programmed
140+
* at the reset vector in order to determine whether or not the application
141+
* was downloaded directly or via the bootloader (i.e. if the address within the
142+
* GOTO instruction is within the bootloader or application), as the ICSP
143+
* inhibit functionality is only permitted if the application was
144+
* downloaded via the bootloader.
145+
*
146+
* The GOTO instruction contains two 24 bit words, the first of which contains
147+
* the lower 15 bits of the address and the second of which contains the upper
148+
* 7 bits. The following code reads the GOTO address at the reset vector and
149+
* parses the two words in order to determine the reset address. The opcode bits
150+
* in the GOTO instruction are masked.
151+
*
152+
* Refer to https://ww1.microchip.com/downloads/en/DeviceDoc/70000157g.pdf for
153+
* additional details on the GOTO instruction format.
154+
*/
155+
static uint32_t GetResetAddress()
156+
{
157+
flash_data_t flashData[2];
158+
FLASH_Read(0x000000, 2, flashData);
159+
return (((flashData[1] & 0x0000007F)<<16) | (flashData[0] & 0x0000FFFE));
160+
}
161+
162+
static bool WasLoadedByBootloader()
163+
{
164+
return (GetResetAddress() < BOOT_CONFIG_PROGRAMMABLE_ADDRESS_LOW);
132165
}
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)