flash patching

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
I am trying to use the Flash patch Hardware of the Cortex-m to remap one Funktion to another.
This is done on a Relax light board.

Funktion 1 blinks with LED1.
After 5 seconds I want to reprogram the FPB to have Funktion 2 ( blinking LED 2 ) running instead of Funktion 1
The code Looks like this:

/*
* Main.c
*
* Created on: 20.08.2014
* Author: trapp
*
* Test funktionen für die Flash Patch Unit
*
* - Programm wird gestartet LED1 blinkt 5 Sekunden
* - Programm patched Funktion 2 auf Funktion 1
* - LED soll nun blinken
*
*/





#include //Declarations from DAVE3 Code Generation (includes SFR declaration)
#include "timer.h"
#include "gpio.h"


unsigned int fp_remap_tab[8] __attribute__((aligned(64))); // Remap Table 8 word alligned

struct fpb {
uint32_t control; // Control register
uint32_t *remap; // remap register
uint32_t comp[6]; // address comparators
uint32_t lit[2]; // literal comparators
};

void func1(void)

{
static struct tim_ t;

timer(&t, STOP);
if (t.tics > 500) {
TOGGLE_P1_1;
timer(&t, START);
}
}

void func2(void)

{
static struct tim_ t;

timer(&t, STOP);
if (t.tics > 500) {
TOGGLE_P1_0;
timer(&t, START);
}
}



int main(void)

{
struct tim_ t;
unsigned char switched = FALSE;
unsigned int func_addr;
struct fpb *fp; // Pointer auf die Flash Patch Unit

DAVE_Init(); // Initialization of DAVE Apps

timer_install();

Control_P1_0(OUTPUT_PP_GP, MEDIUM);
Control_P1_1(OUTPUT_PP_GP, MEDIUM);

fp = (struct fpb *)0xe0002000; // base addr FPB
timer(&t, START);
while(1)
{
func1();
timer(&t, STOP);
if (t.tics > 5000) {
if (!switched) {
switched = TRUE;
fp_remap_tab[5] = func2; // remap to this function
func_addr = func1;
func_addr |= 0x01;
fp->comp[5] = func_addr; // Adresse von Func1 soll gepatched werden
func_addr = &fp_remap_tab;
fp->remap = func_addr; // Adresse der Remap Table eintragen
fp->control |= 0x03; // Enable
}
}
}
return 0;
}

All Registers are set to the values expected, but there is simply no effect.

Any ideas ?
0 Likes
4 Replies
Not applicable
Hi VTrapp,

According to the reference manual, the code patch function is unavailable.
0 Likes
Not applicable
Thanks Jackson,

there are two different statments in the Manual. i only read the first in Table 27-1 that says:

The FPB implements hardware breakpoints and patches code and
data from code space to system space.


in Table 27.2.1

The original M4 code patch function is not available.

Thats bad for me...

Do you have any other idea how to get the functionallity described in my first post running ?
Maybe I can get an exception when there is a address compare hit ?
0 Likes
AndreasG
Employee
Employee
10 replies posted 5 replies posted Welcome!
I think you can achieve the same in with a function pointer.
Adapting your main function,

int main(void)
{
void (*func_ptr) (void);

struct tim_ t;
unsigned char switched = FALSE;

DAVE_Init(); // Initialization of DAVE Apps

timer_install();

Control_P1_0(OUTPUT_PP_GP, MEDIUM);
Control_P1_1(OUTPUT_PP_GP, MEDIUM);

func_ptr = func1;

timer(&t, START);
while(1)
{
func_ptr();
timer(&t, STOP);
if (t.tics > 5000) {
if (!switched) {
switched = TRUE;
func_ptr = func2;
}
}
}
return 0;
}

I hope it runs like that (couldn't test it myself...).
May have a marginal performance impact as the flash patch mode is handled via hardware, while with the function pointer the address is handled via a RAM address. But this code will run on any CPU.
0 Likes
Not applicable
Thanks Andreas,

the solution you provided will work for the example.

But my application has to deal with some Special Debuggings. What I wanted to do is jumping to a function when a address is hit.
Thats nearly what the Debugger does, but I must not halt the processor. I have to use a Serial communication to do my Debugging.
So I tried to use the Flash patch functionality to jump to the Debugging routines. Since Flash patch seams to be not implemented
I am now trying to generate an exception with the Hardware breakpoints.

I am just figuring out how this can be done...
0 Likes