PSP Crashes in exit.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
angelo
Posts: 168
Joined: Wed Aug 29, 2007 9:34 pm

PSP Crashes in exit.

Post by angelo »

Hello everybody. I'm kinda new to PSP programming, and I'm stuck on this one realy anoying issue.

I've edited the piKey Sample so it works and runs in the 3.XX kernel. I've also added a callback.

The anoying thing, is that it freezes on the "Please Wait..." screen after home is pressed.

What could be wrong? I think maybe IRDA is still active and the PSP doesn't like it, but yet a callback has been added which should stop that.

Here's the code:

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <string.h>
#include <pikeylib.h>

#include "../sdk/pikeytypes.h"

/* Define the module info section */
PSP_MODULE_INFO&#40;"PIKEYTEST", 0x1000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;
PSP_MAIN_THREAD_STACK_SIZE_KB&#40;64&#41;;

/* Define printf, just to make typing easier */
#define printf	pspDebugScreenPrintf

	/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
		  sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCallback&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;

          return 0;
&#125;


/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

int done = 0;
int blink = 0;

int main&#40;void&#41;
&#123;
	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;
	int ii;

	printf&#40;"piKey Sample.\n"&#41;;

	initPikey&#40;&#41;;

	sceKernelDelayThread&#40;1000000&#41;;

	printf&#40;"piKey is detected as %s\n", isPikeyLoaded&#40;&#41; ? "running." &#58; "not running."&#41;;

	requestExclusive&#40;&#41;;

	setMode&#40;PIKEY_KEYMODE_KEYPRESS&#41;;

	sceKernelDelayThread&#40;1000000&#41;;

	pspDebugScreenClear&#40;&#41;;

	pspDebugScreenSetXY&#40;0, 5&#41;;
	printf&#40;"Waiting for keys, press ESC to exit...\n\n"&#41;;

	while&#40;!done&#41;
	&#123;
		sceKernelDelayThread&#40;1000&#41;;  // don't tight-loop
		sceDisplayWaitVblank&#40;&#41;;
blink += 1;
		// clear old display
if&#40;blink > 5&#41;&#123;
		pspDebugScreenClear&#40;&#41;;
		blink = 0;
&#125;

		pspDebugScreenSetXY&#40;0, 5&#41;;
		printf&#40;"Waiting for keys, press 'Home' to exit...\n\n"&#41;;

#if 0
		pspDebugScreenSetXY&#40;0,7&#41;;
		for &#40;ii = 7; ii < 20; ii++&#41;
		&#123;
			printf&#40;"                              \n"&#41;;
		&#125;
#endif

		// nothing to do if no keys pressed.
		if &#40;numKeysPressed&#40;&#41; == 0&#41;
		&#123;
			continue;
		&#125;

		pspDebugScreenSetXY&#40;0,7&#41;;
		for &#40;ii = 0; ii < KEY_MAX; ii++&#41;
		&#123;
			if &#40;isKeyPressed&#40;ii&#41;&#41;
			&#123;
				printf&#40;"Pressed scancode&#58; %d\n", ii&#41;;
			&#125;
		&#125;

		int ctrl, alt, shift;
		getMetaKeys&#40;&alt, &ctrl, &shift&#41;;

		pspDebugScreenSetXY&#40;0, 20&#41;;
		printf&#40;"  %s  %s  %s\n",
			     ctrl?  "CTRL"  &#58; "    ",
					 alt?   "ALT"   &#58; "   ",
					 shift? "SHIFT" &#58;	"     "&#41;;
	&#125;
	sceKernelSleepThread&#40;&#41;;
	return 0;
&#125;
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Well, for one you're creating your callback thread in kernel mode and you're not handling yielding the loop when the callback is called.

I would do something like this, not sure if it helps any or not :)

Code: Select all

#include <pspkernel.h> 
#include <pspdebug.h> 
#include <pspdisplay.h> 
#include <pspctrl.h> 
#include <stdlib.h> 
#include <string.h> 
#include <pikeylib.h> 

#include "../sdk/pikeytypes.h" 

/* Define the module info section */ 
PSP_MODULE_INFO&#40;"PIKEYTEST", 0x1000, 1, 1&#41;; 
PSP_MAIN_THREAD_ATTR&#40;0&#41;; 
PSP_MAIN_THREAD_STACK_SIZE_KB&#40;64&#41;;

int done = 0; // moved so accessible by exit_callback

/* Define printf, just to make typing easier */ 
#define printf   pspDebugScreenPrintf 

   /* Exit callback */ 
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123; 
        done = 1; // set done to 1 on callback
          return 0; 
&#125; 

/* Callback thread */ 
int CallbackThread&#40;SceSize args, void *argp&#41; &#123; 
          int cbid; 

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;; 
          sceKernelRegisterExitCallback&#40;cbid&#41;; 

          sceKernelSleepThreadCB&#40;&#41;; 

          return 0; 
&#125; 


/* Sets up the callback thread and returns its thread id */ 
int SetupCallbacks&#40;void&#41; &#123; 
          int thid = 0; 

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0&#41;; // Changed to user mode 
          if&#40;thid >= 0&#41; &#123; 
                    sceKernelStartThread&#40;thid, 0, 0&#41;; 
          &#125; 

          return thid; 
&#125; 

int blink = 0; 

int main&#40;void&#41; 
&#123; 
   pspDebugScreenInit&#40;&#41;; 
   SetupCallbacks&#40;&#41;; 
   int ii; 

   printf&#40;"piKey Sample.\n"&#41;; 

   initPikey&#40;&#41;; 

   sceKernelDelayThread&#40;1000000&#41;; 

   printf&#40;"piKey is detected as %s\n", isPikeyLoaded&#40;&#41; ? "running." &#58; "not running."&#41;; 

   requestExclusive&#40;&#41;; 

   setMode&#40;PIKEY_KEYMODE_KEYPRESS&#41;; 

   sceKernelDelayThread&#40;1000000&#41;; 

   pspDebugScreenClear&#40;&#41;; 

   pspDebugScreenSetXY&#40;0, 5&#41;; 
   printf&#40;"Waiting for keys, press ESC to exit...\n\n"&#41;; 

   while&#40;!done&#41; 
   &#123; 
      sceKernelDelayThread&#40;1000&#41;;  // don't tight-loop 
      sceDisplayWaitVblank&#40;&#41;; 
blink += 1; 
      // clear old display 
if&#40;blink > 5&#41;&#123; 
      pspDebugScreenClear&#40;&#41;; 
      blink = 0; 
&#125; 

      pspDebugScreenSetXY&#40;0, 5&#41;; 
      printf&#40;"Waiting for keys, press 'Home' to exit...\n\n"&#41;; 

#if 0 
      pspDebugScreenSetXY&#40;0,7&#41;; 
      for &#40;ii = 7; ii < 20; ii++&#41; 
      &#123; 
         printf&#40;"                              \n"&#41;; 
      &#125; 
#endif 

      // nothing to do if no keys pressed. 
      if &#40;numKeysPressed&#40;&#41; == 0&#41; 
      &#123; 
         continue; 
      &#125; 

      pspDebugScreenSetXY&#40;0,7&#41;; 
      for &#40;ii = 0; ii < KEY_MAX; ii++&#41; 
      &#123; 
         if &#40;isKeyPressed&#40;ii&#41;&#41; 
         &#123; 
            printf&#40;"Pressed scancode&#58; %d\n", ii&#41;; 
         &#125; 
      &#125; 

      int ctrl, alt, shift; 
      getMetaKeys&#40;&alt, &ctrl, &shift&#41;; 

      pspDebugScreenSetXY&#40;0, 20&#41;; 
      printf&#40;"  %s  %s  %s\n", 
              ctrl?  "CTRL"  &#58; "    ", 
                alt?   "ALT"   &#58; "   ", 
                shift? "SHIFT" &#58;   "     "&#41;; 
   &#125; 
   sceKernelExitGame&#40;&#41;; // Exit game when loop is broken
   return 0; 
&#125;
One more thing, I'm unsure how pikey works as I've never used it, but stay out of kernel mode whenever possible. If the pikey init is all that requires kernel mode, then spawn a user mode thread after that point to handle everything else.
angelo
Posts: 168
Joined: Wed Aug 29, 2007 9:34 pm

Broke it...

Post by angelo »

O.K...

Now it just boots up and freezes with a black screen.

Very odd.
Post Reply