Skip to content

Commit

Permalink
Merge pull request #55 from jieter/main-cleanup
Browse files Browse the repository at this point in the history
Factor out part IO functions into io.(c|h)
  • Loading branch information
xnk committed Feb 14, 2015
2 parents 4306612 + 9891918 commit fee538e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 86 deletions.
95 changes: 93 additions & 2 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "LPC214x.h"
#include <stdint.h>
#include <stdio.h>
#include "t962.h"
#include "io.h"
#include "sched.h"
Expand Down Expand Up @@ -47,13 +48,13 @@ void Set_Fan(uint8_t enable) {
}

static int32_t Sleep_Work(void) {
//FIO0PIN ^= (1<<31); // Toggle debug LED
// FIO0PIN ^= (1<<31); // Toggle debug LED
// For some reason P0.31 status cannot be read out, so the following is used instead:
static uint8_t flip = 0;
if (flip) {
FIO0SET = (1<<31);
} else {
FIO0CLR = (1<<31);
FIO0CLR = (1<<31);
}
flip ^= 1;

Expand All @@ -65,6 +66,96 @@ static int32_t Sleep_Work(void) {
return TICKS_SECS(1);
}

void IO_InitWatchdog(void) {
// Setup watchdog
// Some margin (PCLKFREQ/4 would be exactly the period the WD is fed by sleep_work)
WDTC = PCLKFREQ / 3;
WDMOD = 0x03; // Enable
WDFEED = 0xaa;
WDFEED = 0x55;
}

void IO_PrintResetReason(void) {
uint8_t resetreason = RSIR;
RSIR = 0x0f; // Clear it out
printf(
"\nReset reason(s): %s%s%s%s",
(resetreason & (1 << 0)) ? "[POR]" : "",
(resetreason & (1 << 1)) ? "[EXTR]" : "",
(resetreason & (1 << 2)) ? "[WDTR]" : "",
(resetreason & (1 << 3)) ? "[BODR]" : "");
}


// Support for boot ROM functions (get part number etc)
typedef void (*IAP)(unsigned int [], unsigned int[]);
IAP iap_entry = (void*)0x7ffffff1;

partmapStruct partmap[] = {
{"LPC2131(/01)", 0x0002ff01}, // Probably pointless but present for completeness (32kB flash is too small for factory image)
{"LPC2132(/01)", 0x0002ff11},
{"LPC2134(/01)", 0x0002ff12},
{"LPC2136(/01)", 0x0002ff23},
{"LPC2138(/01)", 0x0002ff25},

{"LPC2141", 0x0402ff01}, // Probably pointless but present for completeness (32kB flash is too small for factory image)
{"LPC2142", 0x0402ff11},
{"LPC2144", 0x0402ff12},
{"LPC2146", 0x0402ff23},
{"LPC2148", 0x0402ff25},
};
#define NUM_PARTS (sizeof(partmap) / sizeof(partmap[0]))

uint32_t command[1];
uint32_t result[3];

int IO_Partinfo(char* buf, int n, char* format) {
uint32_t partrev;

// Request part number
command[0] = IAP_READ_PART;
iap_entry((void *)command, (void *)result);
const char* partstrptr = NULL;
for (int i = 0; i < NUM_PARTS; i++) {
if (result[1] == partmap[i].id) {
partstrptr = partmap[i].name;
break;
}
}

// Read part revision
partrev = *(uint8_t*)PART_REV_ADDR;
if (partrev == 0 || partrev > 0x1a) {
partrev = '-';
} else {
partrev += 'A' - 1;
}
return snprintf(buf, n, format, partstrptr, (int)partrev);
}

void IO_JumpBootloader(void) {
/* Hold F1-Key at boot to force ISP mode */
if ((IOPIN0 & (1 << 23)) == 0) {
// NB: If you want to call this later need to set a bunch of registers back
// to reset state. Haven't fully figured this out yet, might want to
// progmatically call bootloader, not sure. If calling later be sure
// to crank up watchdog time-out, as it's impossible to disable
//
// Bootloader must use legacy mode IO if you call this later too, so do:
// SCS = 0;

// Turn off FAN & Heater using legacy registers so they stay off during bootloader
// Fan = PIN0.8
// Heater = PIN0.9
IODIR0 = (1 << 8) | (1 << 9);
IOSET0 = (1 << 8) | (1 << 9);

//Re-enter ISP Mode, this function will never return
command[0] = IAP_REINVOKE_ISP;
iap_entry((void *)command, (void *)result);
}
}

void IO_Init(void) {
SCS = 0b11; // Enable fast GPIO on both port 0 and 1

Expand Down
14 changes: 13 additions & 1 deletion src/io.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#ifndef IO_H_
#define IO_H_

#define IAP_READ_PART (54)
#define IAP_REINVOKE_ISP (57)
#define PART_REV_ADDR (0x0007D070)

typedef struct {
const char* name;
const uint32_t id;
} partmapStruct;

void Set_Heater(uint8_t enable);
void Set_Fan(uint8_t enable);
void IO_InitWatchdog(void);
void IO_PrintResetReason(void);
int IO_Partinfo(char* buf, int n, char* format);
void IO_JumpBootloader(void);
void IO_Init(void);

#endif /* IO_H_ */
89 changes: 6 additions & 83 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,6 @@ __attribute__((weak)) const char* Version_GetGitVersion(void) {
return "no version info";
}

// Support for boot ROM functions (get part number etc)
typedef void (*IAP)(unsigned int [], unsigned int[]);
IAP iap_entry = (void*)0x7ffffff1;
#define IAP_READ_PART (54)
#define IAP_REINVOKE_ISP (57)
#define PART_REV_ADDR (0x0007D070)
typedef struct {
const char* name;
const uint32_t id;
} partmapStruct;

partmapStruct partmap[] = {
{"LPC2131(/01)", 0x0002ff01}, // Probably pointless but present for completeness (32kB flash is too small for factory image)
{"LPC2132(/01)", 0x0002ff11},
{"LPC2134(/01)", 0x0002ff12},
{"LPC2136(/01)", 0x0002ff23},
{"LPC2138(/01)", 0x0002ff25},

{"LPC2141", 0x0402ff01}, // Probably pointless but present for completeness (32kB flash is too small for factory image)
{"LPC2142", 0x0402ff11},
{"LPC2144", 0x0402ff12},
{"LPC2146", 0x0402ff23},
{"LPC2148", 0x0402ff25},
};
#define NUM_PARTS (sizeof(partmap) / sizeof(partmap[0]))

uint32_t partid, partrev;
uint32_t command[1];
uint32_t result[3];

char* format_about = \
"\nT-962-controller open source firmware (%s)" \
"\n" \
Expand All @@ -107,26 +77,7 @@ int main(void) {
char buf[22];
int len;

/* Hold F1-Key at boot to force ISP mode */
if ((IOPIN0 & (1 << 23)) == 0) {
// NB: If you want to call this later need to set a bunch of registers back
// to reset state. Haven't fully figured this out yet, might want to
// progmatically call bootloader, not sure. If calling later be sure
// to crank up watchdog time-out, as it's impossible to disable
//
// Bootloader must use legacy mode IO if you call this later too, so do:
// SCS = 0;

// Turn off FAN & Heater using legacy registers so they stay off during bootloader
// Fan = PIN0.8
// Heater = PIN0.9
IODIR0 = (1 << 8) | (1 << 9);
IOSET0 = (1 << 8) | (1 << 9);

//Re-enter ISP Mode, this function will never return
command[0] = IAP_REINVOKE_ISP;
iap_entry((void *)command, (void *)result);
}
IO_JumpBootloader();

PLLCFG = (1 << 5) | (4 << 0); //PLL MSEL=0x4 (+1), PSEL=0x1 (/2) so 11.0592*5 = 55.296MHz, Fcco = (2x55.296)*2 = 221MHz which is within 156 to 320MHz
PLLCON = 0x01;
Expand All @@ -147,46 +98,18 @@ int main(void) {
Set_Fan(0);
Serial_Init();
printf(format_about, Version_GetGitVersion());

I2C_Init();
EEPROM_Init();
NV_Init();

LCD_Init();
LCD_BMPDisplay(logobmp, 0, 0);

// Setup watchdog
WDTC = PCLKFREQ / 3; // Some margin (PCLKFREQ/4 would be exactly the period the WD is fed by sleep_work)
WDMOD = 0x03; // Enable
WDFEED = 0xaa;
WDFEED = 0x55;

uint8_t resetreason = RSIR;
RSIR = 0x0f; // Clear it out
printf(
"\nReset reason(s): %s%s%s%s",
(resetreason & (1 << 0)) ? "[POR]" : "",
(resetreason & (1 << 1)) ? "[EXTR]" : "",
(resetreason & (1 << 2)) ? "[WDTR]" : "",
(resetreason & (1 << 3)) ? "[BODR]" : "");

// Request part number
command[0] = IAP_READ_PART;
iap_entry((void *)command, (void *)result);
const char* partstrptr = NULL;
for (int i = 0; i < NUM_PARTS; i++) {
if (result[1] == partmap[i].id) {
partstrptr = partmap[i].name;
break;
}
}
// Read part revision
partrev = *(uint8_t*)PART_REV_ADDR;
if (partrev==0 || partrev > 0x1a) {
partrev = '-';
} else {
partrev += 'A' - 1;
}
len = snprintf(buf, sizeof(buf), "%s rev %c", partstrptr, (int)partrev);
IO_InitWatchdog();
IO_PrintResetReason();

len = IO_Partinfo(buf, sizeof(buf), "%s rev %c");
LCD_disp_str((uint8_t*)buf, len, 0, 64 - 6, FONT6X6);
printf("\nRunning on an %s", buf);

Expand Down

0 comments on commit fee538e

Please sign in to comment.