added stdout feature

This commit is contained in:
NIIBE Yutaka
2010-08-10 16:30:05 +09:00
parent 383424e1c3
commit 8d654d8b11

View File

@@ -51,17 +51,98 @@ static msg_t Thread1(void *arg) {
return 0; return 0;
} }
static
struct stdout {
Mutex m;
CondVar start_cnd;
CondVar finish_cnd;
const char *str;
int size;
} stdout;
static void
stdout_init (void)
{
chMtxInit (&stdout.m);
chCondInit (&stdout.start_cnd);
chCondInit (&stdout.finish_cnd);
stdout.size = 0;
stdout.str = NULL;
}
static int
_write (const char *s, int size)
{
chMtxLock (&stdout.m);
if (stdout.str)
chCondWait (&stdout.finish_cnd);
stdout.str = s;
stdout.size = size;
chCondSignal (&stdout.start_cnd);
chCondWait (&stdout.finish_cnd);
chMtxUnlock ();
return 0;
}
extern uint32_t count_in; extern uint32_t count_in;
extern __IO uint32_t count_out; extern __IO uint32_t count_out;
extern uint8_t buffer_in[VIRTUAL_COM_PORT_DATA_SIZE]; extern uint8_t buffer_in[VIRTUAL_COM_PORT_DATA_SIZE];
extern uint8_t buffer_out[VIRTUAL_COM_PORT_DATA_SIZE]; extern uint8_t buffer_out[VIRTUAL_COM_PORT_DATA_SIZE];
extern void USB_Init (void); extern void USB_Init (void);
static WORKING_AREA(waThread2, 128);
static msg_t Thread2 (void *arg)
{
(void)arg;
again:
while (1)
{
if (bDeviceState == CONFIGURED)
break;
chThdSleepMilliseconds (100);
}
while (1)
{
if (bDeviceState != CONFIGURED)
break;
chMtxLock (&stdout.m);
if (stdout.str == NULL)
chCondWait (&stdout.start_cnd);
{
/* assume DATA_SIZE is large enough */
int i;
for (i = 0; i < stdout.size; i++)
buffer_in[i] = stdout.str[i];
count_in = stdout.size;
USB_SIL_Write (EP1_IN, buffer_in, count_in);
SetEPTxValid (ENDP1);
}
stdout.str = NULL;
stdout.size = 0;
chCondSignal (&stdout.finish_cnd);
chMtxUnlock ();
}
goto again;
return 0;
}
/* /*
* Entry point, note, the main() function is already a thread in the system * Entry point, note, the main() function is already a thread in the system
* on entry. * on entry.
*/ */
int main(int argc, char **argv) { int main(int argc, char **argv)
{
int count = 0;
(void)argc; (void)argc;
(void)argv; (void)argv;
@@ -69,31 +150,29 @@ int main(int argc, char **argv) {
usb_lld_init (); usb_lld_init ();
USB_Init(); USB_Init();
stdout_init ();
/* /*
* Creates the blinker thread. * Creates the blinker thread.
*/ */
chThdCreateStatic (waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); chThdCreateStatic (waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
while (TRUE) { /*
* Creates 'stdout' thread.
*/
chThdCreateStatic (waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
while (1)
{
if (palReadPad(IOPORT1, GPIOA_BUTTON)) if (palReadPad(IOPORT1, GPIOA_BUTTON))
palSetPad (IOPORT3, GPIOC_LED); palSetPad (IOPORT3, GPIOC_LED);
if ((count_out != 0) && (bDeviceState == CONFIGURED)) { chThdSleepMilliseconds (100);
uint8_t i;
for (i = 0; i<count_out; i++) { if (bDeviceState == CONFIGURED && (count % 10) == 0)
buffer_in[(count_in+i)%64] = buffer_out[i]; _write ("Hello world\r\n", 13);
}
count_in += count_out; count++;
count_out = 0;
} }
if (count_in > 0) {
USB_SIL_Write (EP1_IN, buffer_in, count_in);
SetEPTxValid (ENDP1);
}
chThdSleepMilliseconds (50);
}
return 0; return 0;
} }