summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c169
1 files changed, 143 insertions, 26 deletions
diff --git a/main.c b/main.c
index a7179be..7d14ce9 100644
--- a/main.c
+++ b/main.c
@@ -20,13 +20,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+/* define TRACE_SERIAL to get debugging traces capabilities on the
+ * MSP430 serial port
+ * WARNING: This might disable some functions too, check the code
+ * to see exactly what that does in this particular version */
+#define TRACE_SERIAL
+
+#ifdef TRACE_SERIAL
+#include <stdio.h>
+#endif /* TRACE_SERIAL */
+
#include <stdbool.h>
#include <io430.h>
#include <intrinsics.h>
-#include <stdio.h>
-
#include "def.h"
#include "hardware.h"
@@ -75,6 +83,8 @@ volatile u8 bVCC3 = true;
#define RST_WAIT 30
+#ifdef TRACE_SERIAL
+
// For UCOS16 = 1
// BRCLK Baud Rate UCBRx UCBRSx UCBRFx Tx Error Rx Error
// 12,000,000 115200 6 0 8 -1.8 0 -2.2 0.4
@@ -104,6 +114,9 @@ static void ll_putchar(unsigned char c)
UCA0TXBUF = c;
}
+/* __write, remove, __close, and __lseek need to be implemented by the
+ * application for the full DLib to work.
+ */
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
@@ -120,6 +133,91 @@ size_t __write(int handle, const unsigned char * buffer, size_t size)
return nChars;
}
+int remove(const char *name)
+{
+ return -1;
+}
+
+int __close(int handle)
+{
+ return 0;
+}
+
+long __lseek(int handle, long offset, int whence)
+{
+ return -1;
+}
+
+// TB_SIZE must be a power of 2
+#define TB_SIZE 64
+static u8 trace_buffer[TB_SIZE];
+static u8 tb_beg;
+static u8 tb_fill;
+
+static inline u8 TB_I(u8 raw_idx)
+{
+ return raw_idx & (TB_SIZE - 1);
+}
+
+static void trace(u8 val)
+{
+ if (tb_fill == TB_SIZE) {
+ tb_beg = TB_I(tb_beg + 1);
+ tb_fill--;
+ }
+ trace_buffer[TB_I(tb_beg + (tb_fill++))];
+}
+
+static inline char hex_nibble(u8 n)
+{
+ return n + ((n > 9) ? 'a' - 10 : '0');
+}
+
+static inline void dump_byte(char b[3], u8 n)
+{
+ b[0] = hex_nibble(n >> 4);
+ b[1] = hex_nibble(n & 0xf);
+ b[2] = ' ';
+}
+
+static void dump_trace(void)
+{
+ static char buf[64];
+ int pos = 0;
+ #define flush() do { buf[pos] = 0; fputs(buf, stdout); pos = 0; } while (0)
+
+ int i;
+
+ fputs("TRACE:\r\n", stdout);
+ pos = 0;
+ for (i = 0; i < tb_fill; i++) {
+ dump_byte(buf + pos, trace_buffer[TB_I(tb_beg + i)]);
+ pos += 3;
+
+ if (i & 15 == 15) {
+ buf[pos++] = '\r';
+ buf[pos++] = '\n';
+ } else if (i & 7 == 7) {
+ buf[pos++] = ' ';
+ }
+
+ if (pos >= 56)
+ flush();
+ }
+ flush();
+ if (i & 15) puts("\r");
+}
+
+#define TRACE(v) trace(v)
+#define change_state(ns) do { trace(ns); state = (ns); } while (0)
+
+#else /* !TRACE_SERIAL */
+
+#define TRACE(v) /* noop */
+#define change_state(ns) do { state = (ns); } while (0)
+
+#endif /* TRACE_SERIAL */
+
int main(void)
{
@@ -131,7 +229,9 @@ int main(void)
InitPorts();
GlobalInit();
+#ifdef TRACE_SERIAL
SerialInit();
+#endif /* TRACE_SERIAL */
__enable_interrupt();
@@ -139,7 +239,21 @@ int main(void)
resetState = ON_STATE;
while (1) {
- switch (resetState) { //////////////////////////////////////////////
+ ////////////////////////////////////////////////////////////////////
+#ifdef TRACE_SERIAL // debug behavior
+ switch (resetState) {
+ case ON_STATE:
+ if (SW2State > 100)
+ dump_trace();
+ resetState = RST_WAIT;
+ break;
+ case RST_WAIT:
+ if (SW2State == 0)
+ resetState = ON_STATE;
+ break;
+ }
+#else // normal behavior
+ switch (resetState) {
case ON_STATE:
if (SW2State > 300) {
resetState = RST_STATE;
@@ -162,67 +276,70 @@ int main(void)
}
break;
}
+#endif /* TRACE_SERIAL */
- switch (state) { ///////////////////////////////////////////////////
+ ////////////////////////////////////////////////////////////////////
+ switch (state) {
case WAIT_START:
if (SW1State > 30)
- state = WAIT_START + 1;
+ change_state(WAIT_START + 1);
break;
case WAIT_START + 1:
if (SW1State == 0) {
SetBit(P4OUT, CMDPWR); // Start Atx Power Supply
Timer1 = 2000;
- state = WAIT_ATX_OK;
+ change_state(WAIT_ATX_OK);
}
break;
case WAIT_ATX_OK:
- if (SW1State || TENSION_EXPIRED)
- state = STOP;
+ if (SW1State || TENSION_EXPIRED) {
+ change_state(STOP);
+ }
if ((P4IN & ATX_PWROK) && TENSION_WAIT(bV2P5 && bVCC3)) {
ClrBit(P1OUT, V1P2_CORE_EN_N);
Timer1 = 30;
- state = WAIT_V1P2;
+ change_state(WAIT_V1P2);
}
break;
case WAIT_V1P2:
if (SW1State || TENSION_EXPIRED)
- state = STOP;
+ change_state(STOP);
if (TENSION_WAIT(bV1P2)) {
Timer1 = 30;
SetBit(P4OUT, V1P8_CMD);
- state = WAIT_RSMRST;
+ change_state(WAIT_RSMRST);
}
break;
case WAIT_RSMRST:
if (SW1State)
- state = STOP;
+ change_state(STOP);
if (Timer1 < 19) {
SetBit(P2OUT, IMCH_RSMRST_N);
- state = WAIT_V1P8;
+ change_state(WAIT_V1P8);
}
break;
case WAIT_V1P8:
if (SW1State || TENSION_EXPIRED)
- state = STOP;
+ change_state(STOP);
if (TENSION_WAIT(bV1P8)) {
ClrBit(P2OUT, CPU_VCCP_EN_N);
Timer1 = 30;
- state = WAIT_V1P0;
+ change_state(WAIT_V1P0);
}
break;
case WAIT_V1P0:
if (SW1State || TENSION_EXPIRED)
- state = STOP;
+ change_state(STOP);
if (TENSION_WAIT(bV1P0)) {
SetBit(P3OUT, VRMPWRGD);
ClrBit(P2OUT, GREEN_LED_N);
Timer1 = 3;
- state = CK410_VTT_GD;
+ change_state(CK410_VTT_GD);
}
break;
@@ -231,7 +348,7 @@ int main(void)
SetBit(P2DIR, CK410_PWR_GD_N);
ClrBit(P2OUT, CK410_PWR_GD_N);
Timer1 = 105;
- state = STATE_SYS_PWR_OK;
+ change_state(STATE_SYS_PWR_OK);
}
break;
@@ -239,46 +356,46 @@ int main(void)
if (Timer1 == 0) {
SetBit(P3OUT, SYS_PWR_OK);
Timer1 = 10;
- state = CPU_RUN;
+ change_state(CPU_RUN);
}
break;
case CPU_RUN:
if (SW1State)
- state = STOP;
+ change_state(STOP);
if (Timer1 == 0) {
// Start Tolapai: emulate the power button from its point of view
// we don't really know where is the best place to do that so
// we will try it here at first.
SetBit(P2DIR, IMCH_PWRBTN_N);
Timer1 = 200;
- state = CPU_RUN + 1;
+ change_state(CPU_RUN + 1);
}
break;
case CPU_RUN + 1:
if (SW1State)
- state = STOP;
+ change_state(STOP);
if (Timer1 == 0) {
ClrBit(P2DIR, IMCH_PWRBTN_N);
- state = WAIT_STOP;
+ change_state(WAIT_STOP);
}
break;
case WAIT_STOP:
if (SW1State >= 6000) // Sw1 button pressed for more than 6 seconds
- state = STOP;
+ change_state(STOP);
break;
case STOP:
InitPorts();
Timer1 = 500; // Disable any other Power up for 0.5 s.
ClrBit(P2OUT, RED_LED_N); // To show no restart is possible for now
- state = STOP + 1;
+ change_state(STOP + 1);
break;
case STOP + 1:
if (Timer1 == 0) {
SetBit(P2OUT, RED_LED_N); // To show restart is possible now
- state = WAIT_START;
+ change_state(WAIT_START);
}
break;
}