Skip to content

Commit

Permalink
Rework + add Subaru SH7058 support.
Browse files Browse the repository at this point in the history
Split things into  mfg-specific files for
- wdt pin management
- "special needs" like subaru FWE pin
  • Loading branch information
fenugrec committed Jun 10, 2022
1 parent bdffe56 commit b75ace9
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 107 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ add_custom_target(version_h BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/version.h"
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gitversion.cmake
)

set (COMMON_SRCS cmd_parser.c eep_funcs.c main.c crc.c
set (COMMON_SRCS cmd_parser.c eep_funcs.c main.c crc.c wdt.c
${CMAKE_CURRENT_BINARY_DIR}/version.h
)

set (NISSAN_SRCS ${COMMON_SRCS} start_705x.s)
set (SUBARU_SRCS ${COMMON_SRCS} start_ssm.s)
set (NISSAN_SRCS ${COMMON_SRCS} mfg_nissan.c start_705x.s)
set (SUBARU_SRCS ${COMMON_SRCS} mfg_ssm.c start_ssm.s)

## objcopy to produce .bin file
function(make_bin_file target)
Expand Down Expand Up @@ -77,6 +77,7 @@ function(add_kernel brand TGTNAME)
message(STATUS ${brand}_${TGTNAME})
target_compile_definitions(${brand}_${TGTNAME} PRIVATE PLATF=\"${TGTNAME}\")
target_compile_definitions(${brand}_${TGTNAME} PRIVATE ${TGTNAME})
target_compile_definitions(${brand}_${TGTNAME} PRIVATE ${brand})
target_include_directories(${brand}_${TGTNAME} PUBLIC ${PROJECT_BINARY_DIR})
make_bin_file(${brand}_${TGTNAME})
show_object_size(${brand}_${TGTNAME})
Expand Down
55 changes: 2 additions & 53 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,67 +21,17 @@

#include "functions.h" //for set_imask etc

#include "mfg.h"
#include "platf.h"
#include "cmd_parser.h"


/**** "preload" info struct filled before calling RAMjump. This probably varies but seems typical ? *****/
struct rj_preload {
uint16_t flag0; // ?
uint16_t wdt_pin; // pin # mask for PxDR
uint16_t s36k2_H;
uint16_t s36k2_L;
uint16_t wdt_portH; //&PxDR>>16
uint16_t wdt_portL; //&PxDR & 0xFFFF
};

u16 *wdt_dr; //such as &PLDR
u16 wdt_pin; //mask for PxDR


/** toggle WDT pin */
void wdt_tog(void) {
#ifdef DIAG_TAINTWDT
/* for debugging purposes, generate a recognizable pattern :
* skip one toggle every 16 periods */
static int ic = 0;
ic += 1;
if (ic & 0x0f) {
*wdt_dr ^= wdt_pin;
}
#else
*wdt_dr ^= wdt_pin;
#endif
return;
}

#if 0
/** poll timer to generate WDT pulse */
void do_wdt(void) {
if (ATU1.TCNTB > (WDT_MAXCNT + 100)) {
wdt_tog();
ATU1.TCNTB = 0;
ATU1.TSRB.BIT.CMF = 0; //TCNT1B compare match
}
return;
}
#endif



void main(void) {
struct rj_preload *rjp = (struct rj_preload *)RAMJUMP_PRELOAD_META;

set_imask(0x0F); // disable interrupts (mask = b'1111)

init_mfg();
init_platf();

/* parse preload struct to get wdt info */
wdt_dr = (u16 *) ((rjp->wdt_portH << 16) | (rjp->wdt_portL));
wdt_pin = rjp->wdt_pin;

init_wdt();

/* and lower prio mask to let WDT run */
set_imask(0x07);

Expand All @@ -92,4 +42,3 @@ void main(void) {
die();

}

13 changes: 13 additions & 0 deletions mfg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef MFG_H
#define MFG_H

/** defs for manufacturer-specific stuff */


/** Called before init_platf.
* This is manufacturer-specific, and implemented in a separate .c file
*/
void init_mfg(void);


#endif
35 changes: 35 additions & 0 deletions mfg_nissan.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* external WDT functions specific to Nissan ECUs
*
* (c) fenugrec 2022
* GPLv3
*
*/

#include <stdint.h>

#include "mfg.h"
#include "platf.h"
#include "stypes.h"
#include "wdt.h"


/**** "preload" info struct filled before calling RAMjump. This probably varies but seems typical ? *****/
struct rj_preload {
uint16_t flag0; // ?
uint16_t wdt_pin; // pin # mask for PxDR
uint16_t s36k2_H;
uint16_t s36k2_L;
uint16_t wdt_portH; //&PxDR>>16
uint16_t wdt_portL; //&PxDR & 0xFFFF
};


/** parse "preload" info
*/
void init_mfg(void) {
struct rj_preload *rjp = (struct rj_preload *)RAMJUMP_PRELOAD_META;
/* get pin info */
wdt_dr = (u16 *) ((rjp->wdt_portH << 16) | (rjp->wdt_portL));
wdt_pin = rjp->wdt_pin;
}

23 changes: 23 additions & 0 deletions mfg_ssm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* functions specific to Subaru ECUs
*
* (c) fenugrec 2022
* (c) rimwall 2022
* GPLv3
*
*/

#include <stdint.h>

#include "mfg.h"
#include "platf.h"
#include "wdt.h"



void init_mfg(void) {
wdt_dr = &PB.DR.WORD; //manually assign these values, not elegant but will do for now
wdt_pin = 0x8000;

// set PD8 to bring FWE high to prepare for erasing or flashing
PD.DR.WORD |= 0x0100;
}
95 changes: 52 additions & 43 deletions platf.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,59 @@

#include <stdbool.h>

/****** mcu-specific defines ******/
/*
* RAMJUMP_PRELOAD_META : where the pre-ramjump metadata is stored (wdt pin, s36k2, etc)
* RAM_MIN, RAM_MAX : whole RAM area
* " #include "reg_defines/????" : i/o peripheral registers
*/


#if defined(SH7058)
#include "reg_defines/7055_7058_180nm.h"
#define RAM_MIN 0xFFFF0000
#define RAM_MAX 0xFFFFBFFF
#define RAMJUMP_PRELOAD_META 0xffff8000
#define NPK_SCI SCI1

#elif defined(SH7055_18)
#include "reg_defines/7055_7058_180nm.h"
#define RAM_MIN 0xFFFF6000
#define RAM_MAX 0xFFFFDFFF
#define RAMJUMP_PRELOAD_META 0xffff8000
#define NPK_SCI SCI1

#elif defined(SH7055_35)
#include "reg_defines/7055_350nm.h"
#define RAM_MIN 0xFFFF6000
#define RAM_MAX 0xFFFFDFFF
#define RAMJUMP_PRELOAD_META 0xffff8000
#define NPK_SCI SCI1

#elif defined(SH7051)
#include "reg_defines/7051.h"
#define RAM_MIN 0xFFFFD800
#define RAM_MAX 0xFFFFFFFF
#define RAMJUMP_PRELOAD_META 0xffffD800
#define NPK_SCI SCI2

#else
#error No target specified !
/****** mfg- and mcu-specific defines ******
*
* RAM_MIN, RAM_MAX : whole RAM area
* " #include "reg_defines/????" : i/o peripheral registers
*/

#if defined(npk)

/* Nissan only: RAMJUMP_PRELOAD_META : pre-ramjump metadata address */
#if defined(SH7058)
#include "reg_defines/7055_7058_180nm.h"
#define RAM_MIN 0xFFFF0000
#define RAM_MAX 0xFFFFBFFF
#define RAMJUMP_PRELOAD_META 0xffff8000
#define NPK_SCI SCI1

#elif defined(SH7055_18)
#include "reg_defines/7055_7058_180nm.h"
#define RAM_MIN 0xFFFF6000
#define RAM_MAX 0xFFFFDFFF
#define RAMJUMP_PRELOAD_META 0xffff8000
#define NPK_SCI SCI1

#elif defined(SH7055_35)
#include "reg_defines/7055_350nm.h"
#define RAM_MIN 0xFFFF6000
#define RAM_MAX 0xFFFFDFFF
#define RAMJUMP_PRELOAD_META 0xffff8000
#define NPK_SCI SCI1

#elif defined(SH7051)
#include "reg_defines/7051.h"
#define RAM_MIN 0xFFFFD800
#define RAM_MAX 0xFFFFFFFF
#define RAMJUMP_PRELOAD_META 0xffffD800
#define NPK_SCI SCI2

#else
#error No target specified !
#endif

#elif defined(ssmk)
#if defined(SH7058)
#include "reg_defines/7055_7058_180nm.h"
#define RAM_MIN 0xFFFF0000
#define RAM_MAX 0xFFFFBFFF
#define NPK_SCI SCI2
#else
#error invalid target for ssmk
#endif
#endif



/*** WDT and master clock stuff
* want to toggle the WDT every 2ms (or 2000us)
*/
Expand Down Expand Up @@ -117,12 +128,10 @@ uint32_t platf_flash_wb(uint32_t dest, uint32_t src, uint32_t len);
/***** Init funcs ****/


/** init platform-specific stuff : SCI, clocks, interrupts */
/** init platform-specific stuff : SCI, clocks, interrupts, WDT etc */
void init_platf(void);

/** init necessary timers & shit for WDT interrupt !
*/
void init_wdt(void);


/** force reset by external supervisor and/or internal WDT.
*/
Expand Down
5 changes: 3 additions & 2 deletions platf_7050.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "stypes.h"
#include "platf.h"
#include "wdt.h"

/* init SCI to continue comms on K line */
static void init_sci(void) {
Expand All @@ -39,7 +40,7 @@ static void init_sci(void) {


/******* Interrupt stuff */
void wdt_tog(void);
static void init_wdt(void);

/** WDT toggle interrupt
*
Expand Down Expand Up @@ -189,7 +190,7 @@ void init_platf(void) {

init_mclk();
init_sci();

init_wdt();
}


Expand Down
14 changes: 8 additions & 6 deletions platf_7055.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* - external WDT
*/

/* (c) copyright fenugrec 2016
/* (c) copyright fenugrec 2016-2022
* GPLv3
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -29,6 +29,7 @@

#include "stypes.h"
#include "platf.h"
#include "wdt.h"

/* init SCI to continue comms on K line */
static void init_sci(void) {
Expand All @@ -39,7 +40,7 @@ static void init_sci(void) {


/******* Interrupt stuff */
void wdt_tog(void);
static void init_wdt(void);

/** WDT toggle interrupt
*
Expand All @@ -49,9 +50,7 @@ void INT_ATU11_IMI1A(void) ISR_attrib;
void INT_ATU11_IMI1A(void) {

wdt_tog();
//ATU1.TCNTA = 0;
ATU1.TCNTB = 0;
//ATU1.TSRA.BIT.IMFA = 0 ;
ATU1.TSRB.BIT.CMF = 0; //TCNT1B compare match
return;
}
Expand Down Expand Up @@ -200,7 +199,7 @@ void init_platf(void) {

init_mclk();
init_sci();

init_wdt();
}


Expand All @@ -209,7 +208,7 @@ void init_platf(void) {
* since host was taking care of it just before.
* This uses Channel 1 TCNTB CMF interrupt
*/
void init_wdt(void) {
static void init_wdt(void) {
ATU.TSTR1.BIT.STR12B = 0; //stop while configging
ATU1.TIORA.BYTE = 0;
ATU1.TIORB.BYTE = 0;
Expand All @@ -230,8 +229,11 @@ void init_wdt(void) {
return;
}


// internal WDT: used for 350nm reflash, and for forcing a reset in die().
#define WDT_RSTCSR_SETTING 0x5A5F; //power-on reset if TCNT overflows


/*
* Some ECUs (VC264) don't seem to reset properly with just the external WDT.
* But, internal WDT-triggered POR doesn't reset certain periph regs
Expand Down
Loading

0 comments on commit b75ace9

Please sign in to comment.