summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Knispel <gknispel@avencall.com>2012-09-11 16:57:11 +0200
committerGuillaume Knispel <gknispel@avencall.com>2012-09-11 16:57:11 +0200
commit8d721670c3b54c77646e548b31c3f54618908590 (patch)
treef2c1aabd897af495f4a261eef37c568d996e84d1
parentf7f5da8a1c0cf5d8ac16fd732f00d384c000fa7c (diff)
add state change debug traces
Allow state changes to be traced in a special build-time mode selected by a new define: TRACE_SERIAL. In this mode a trace buffer of 64 bytes is used to record all the affectations to 'state'. When the button 2 is pressed, this buffer is dumped to the MSP430 serial port and then emptied. The implementation uses of the full DLib (IAR specific). To allow that in release mode, this commit also adds (dummy) implementations of remove(), __close(), and __lseek(), which the full DLib requires.
-rw-r--r--PwrSeq.ewp22
-rw-r--r--main.c169
2 files changed, 154 insertions, 37 deletions
diff --git a/PwrSeq.ewp b/PwrSeq.ewp
index 44e9e6b..51ad6d0 100644
--- a/PwrSeq.ewp
+++ b/PwrSeq.ewp
@@ -46,19 +46,19 @@
<option>
<name>GRuntimeLibSelect</name>
<version>0</version>
- <state>1</state>
+ <state>2</state>
</option>
<option>
<name>RTDescription</name>
- <state>Use the normal configuration of the C/EC++ runtime library. No locale interface, C locale, no file descriptor support, no multibytes in printf and scanf, and no hex floats in strtod.</state>
+ <state>Use the full configuration of the C/EC++ runtime library. Full locale interface, C locale, file descriptor support, multibytes in printf and scanf, and hex floats in strtod.</state>
</option>
<option>
<name>RTConfigPath</name>
- <state>$TOOLKIT_DIR$\LIB\DLIB\dl430fn.h</state>
+ <state>$TOOLKIT_DIR$\LIB\DLIB\dl430ff.h</state>
</option>
<option>
<name>RTLibraryPath</name>
- <state>$TOOLKIT_DIR$\LIB\DLIB\dl430fn.r43</state>
+ <state>$TOOLKIT_DIR$\LIB\DLIB\dl430ff.r43</state>
</option>
<option>
<name>Input variant</name>
@@ -81,7 +81,7 @@
<option>
<name>GRuntimeLibSelectSlave</name>
<version>0</version>
- <state>1</state>
+ <state>2</state>
</option>
<option>
<name>OGCore</name>
@@ -1021,19 +1021,19 @@
<option>
<name>GRuntimeLibSelect</name>
<version>0</version>
- <state>1</state>
+ <state>2</state>
</option>
<option>
<name>RTDescription</name>
- <state>Use the normal configuration of the C/EC++ runtime library. No locale interface, C locale, no file descriptor support, no multibytes in printf and scanf, and no hex floats in strtod.</state>
+ <state>Use the full configuration of the C/EC++ runtime library. Full locale interface, C locale, file descriptor support, multibytes in printf and scanf, and hex floats in strtod.</state>
</option>
<option>
<name>RTConfigPath</name>
- <state>$TOOLKIT_DIR$\LIB\DLIB\dl430fn.h</state>
+ <state>$TOOLKIT_DIR$\LIB\DLIB\dl430ff.h</state>
</option>
<option>
<name>RTLibraryPath</name>
- <state>$TOOLKIT_DIR$\LIB\DLIB\dl430fn.r43</state>
+ <state>$TOOLKIT_DIR$\LIB\DLIB\dl430ff.r43</state>
</option>
<option>
<name>Input variant</name>
@@ -1056,7 +1056,7 @@
<option>
<name>GRuntimeLibSelectSlave</name>
<version>0</version>
- <state>1</state>
+ <state>2</state>
</option>
<option>
<name>OGCore</name>
@@ -1748,7 +1748,7 @@
<option>
<name>ModuleLocalSym</name>
<version>0</version>
- <state>2</state>
+ <state>1</state>
</option>
<option>
<name>CrcBitOrder</name>
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;
}