1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - WXC - demos - simple-1
3 File: user.c
4
5 Copyright 2005-2009 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Date:: 2007-11-15#$
14 $Rev: 2414 $
15 $Author: hatamoto_minoru $
16 *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18
19 #include <nitro/wxc.h>
20 #include "font.h"
21 #include "user.h"
22 #include "print.h"
23
24 static int data_exchange_count = 0;
25
26 /**** Debug Data Buffer ****/
27 #define DATA_SIZE 1024*20
28 static u8 send_data[DATA_SIZE];
29 static u8 recv_data[DATA_SIZE];
30
31 /* GGID used for testing */
32 #define SDK_MAKEGGID_SYSTEM(num) (0x003FFF00 | (num))
33 #define GGID_ORG_1 SDK_MAKEGGID_SYSTEM(0x51)
34
35 extern u16 gScreen[32 * 32]; // Virtual screen
36 u16 temp_gScreen[32 * 32]; // Virtual screen
37
38
39 /*---------------------------------------------------------------------------*
40 Name: CalcHash
41
42 Description: Hash calculation for a simple check.
43
44 Arguments: src: Calculation target buffer
45
46 Returns: The calculated value.
47 *---------------------------------------------------------------------------*/
CalcHash(const u8 * src)48 static u8 CalcHash(const u8 *src)
49 {
50 int i;
51 u8 sum = 0;
52 for (i = 0; i < DATA_SIZE - 1; i++)
53 {
54 sum ^= src[i];
55 }
56 return sum;
57 }
58
59 /* Callback function used when data exchange is completed in the chance encounter communication */
user_callback(WXCStateCode stat,void * ptr)60 static void user_callback(WXCStateCode stat, void *ptr)
61 {
62 #pragma unused(stat)
63
64 const WXCBlockDataFormat * block = (const WXCBlockDataFormat *)ptr;
65 u8 sum = 0;
66 u8 *data = (u8 *)block->buffer;
67
68 ++data_exchange_count;
69
70 // Move gScreen buffer by one line
71 MI_CpuClearFast((void *)temp_gScreen, sizeof(temp_gScreen));
72 MI_CpuCopyFast((void *)(&gScreen[(5 * 32)]), (void *)(&temp_gScreen[(7 * 32)]), sizeof(u16)*32*14);
73 MI_CpuCopyFast((void *)temp_gScreen, (void *)gScreen, sizeof(gScreen));
74
75 /* Displays debug receive data */
76 sum = CalcHash(data);
77
78 if (sum == data[DATA_SIZE - 1])
79 {
80 PrintString(1, 5, 0x1, "sum OK 0x%02X %6d %6d sec", sum, data_exchange_count,
81 OS_TicksToSeconds(OS_GetTick()));
82 PrintString(1, 6, 0x1, "%s", data);
83 }
84 else
85 {
86 PrintString(1, 5, 0x1, "sum NG 0x%02X sum 0x0%02X %6d sec", sum, data[DATA_SIZE - 1],
87 OS_TicksToSeconds(OS_GetTick()));
88 }
89 MI_CpuClear32(data, DATA_SIZE);
90
91 // End when communication is performed once
92 (void)WXC_End();
93 }
94
95
96 /*---------------------------------------------------------------------------*
97 Name: system_callback
98
99 Description: WXC library system callback.
100
101 Arguments: stat: Notification status (normally WXC_STATE_EXCHANGE_DONE)
102 arg: Notification argument (receive data buffer)
103
104 Returns: The calculated value.
105 *---------------------------------------------------------------------------*/
system_callback(WXCStateCode state,void * arg)106 static void system_callback(WXCStateCode state, void *arg)
107 {
108 switch (state)
109 {
110 case WXC_STATE_READY:
111 /*
112 * Issued from a WXC_Init function call.
113 * arg is always NULL.
114 */
115 break;
116
117 case WXC_STATE_ACTIVE:
118 /*
119 * Issued from a WXC_Start function call.
120 * arg is always NULL.
121 */
122 break;
123
124 case WXC_STATE_ENDING:
125 /*
126 * Issued from a WXC_End function call.
127 * arg is always NULL.
128 */
129 break;
130
131 case WXC_STATE_END:
132 /*
133 * Issued upon completion of the shutdown processing run by the WXC_End function.
134 * arg is internal work memory allocated by the WXC_Init function.
135 * At this point, work memory is deallocated by the user.
136 */
137 {
138 void *system_work = arg;
139 OS_Free(system_work);
140 }
141 break;
142
143 case WXC_STATE_EXCHANGE_DONE:
144 /*
145 * Data exchange complete (not currently issued)
146 */
147 break;
148 }
149 }
150
User_Init(void)151 void User_Init(void)
152 {
153 u8 MacAddress[6];
154
155 /* Initializes the internal status of the library */
156 WXC_Init(OS_Alloc(WXC_WORK_SIZE), system_callback, 2);
157
158 /* Set current situation data */
159 OS_GetMacAddress(MacAddress);
160 (void)OS_SPrintf((char *)send_data, "data %02X:%02X:%02X:%02X:%02X:%02X",
161 MacAddress[0], MacAddress[1], MacAddress[2],
162 MacAddress[3], MacAddress[4], MacAddress[5]);
163
164 send_data[DATA_SIZE - 1] = CalcHash(send_data);
165
166 /* Register own station data */
167 /* Target station data receive buffer. Data size is same as own station data size */
168 WXC_RegisterCommonData(GGID_ORG_1, user_callback, send_data, DATA_SIZE, recv_data, DATA_SIZE);
169
170 /* Library wireless startup */
171 WXC_Start();
172 }
173