timer.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include "wonx/timer.h"
  8. #include "wonx_configure.h"
  9. #include "WonX.h"
  10. #include "etc.h"
  11. /* int tm_year; year - 1900 */
  12. static int get_year(struct tm * tblock) { return (tblock->tm_year - 100); }
  13. /* int tm_mon; month of year (0-11) */
  14. static int get_month(struct tm * tblock) { return (tblock->tm_mon + 1); }
  15. /* int tm_mday; day of month (1-31) */
  16. static int get_day(struct tm * tblock) { return (tblock->tm_mday); }
  17. /* int tm_wday; day of week (Sunday = 0) */
  18. static int get_week(struct tm * tblock) { return (tblock->tm_wday); }
  19. /* int tm_hour; hours (0 - 23) */
  20. static int get_hour(struct tm * tblock) { return (tblock->tm_hour); }
  21. /* int tm_min; minutes (0 - 59) */
  22. static int get_minute(struct tm * tblock) { return (tblock->tm_min); }
  23. /* int tm_sec; seconds (0 - 60) */
  24. static int get_second(struct tm * tblock) { return (tblock->tm_sec); }
  25. /*****************************************************************************/
  26. /* 互換関数の定義 */
  27. /*****************************************************************************/
  28. /*
  29. * Xサーバとの同期の整合性がとれなくなるなどの問題が考えられるので,
  30. * 互換関数の内部は UNIXTimer_Pause(), UNIXTimer_Unpause() でくくり,
  31. * タイマ割り込みを一時停止して処理を行う.また,unpause するまえに,
  32. * かならず sync するようにする.
  33. */
  34. /*
  35. * タイマの一時停止の2重解除の問題が出てくるので,
  36. * 互換関数から互換関数を呼んではいけない.
  37. * (一時停止はネストされるが,いちおう)
  38. * 似たような処理をする関数の場合は,必ず static な別関数に処理をまとめ,
  39. * そっちを呼び出すようにすること.
  40. * 引数の表示の問題もあるしね.
  41. */
  42. void rtc_set_datetime(int type, unsigned int value)
  43. {
  44. printf("call : rtc_set_datetime() : type = %d, value = %d\n",
  45. type, (int)value);
  46. fflush(stdout);
  47. /* 未サポート */
  48. printf("call : rtc_set_datetime() : not supported\n");
  49. printf("call : rtc_set_datetime() : return value = none\n");
  50. fflush(stdout);
  51. return;
  52. }
  53. unsigned int rtc_get_datetime(int type)
  54. {
  55. unsigned int ret = 0;
  56. time_t timer;
  57. struct tm * tblock;
  58. printf("call : rtc_get_datetime() : type = %d\n", type);
  59. fflush(stdout);
  60. time(&timer);
  61. tblock = localtime(&timer);
  62. switch (type) {
  63. case RTC_YEAR : ret = get_year( tblock); break;
  64. case RTC_MONTH : ret = get_month( tblock); break;
  65. case RTC_DATE : ret = get_day( tblock); break;
  66. case RTC_DAY_OF_WEEK : ret = get_week( tblock); break;
  67. case RTC_HOUR : ret = get_hour( tblock); break;
  68. case RTC_MIN : ret = get_minute(tblock); break;
  69. case RTC_SEC : ret = get_second(tblock); break;
  70. default : WonX_Error("rtc_get_datetime", "Unknown parameter.");
  71. }
  72. printf("call : rtc_get_datetime() : return value = %d\n", (int)ret);
  73. fflush(stdout);
  74. return (ret);
  75. }
  76. void rtc_set_datetime_struct(datetime_t * d)
  77. {
  78. printf("call : rtc_set_datetime_struct() : buffer = %p\n", (void *)d);
  79. fflush(stdout);
  80. /* 未サポート */
  81. printf("call : rtc_set_datetime_struct() : not supported\n");
  82. printf("call : rtc_set_datetime_struct() : return value = none\n");
  83. fflush(stdout);
  84. return;
  85. }
  86. void rtc_get_datetime_struct(datetime_t * d)
  87. {
  88. time_t timer;
  89. struct tm * tblock;
  90. printf("call : rtc_get_datetime_struct() : buffer = %p\n", (void *)d);
  91. fflush(stdout);
  92. time(&timer);
  93. tblock = localtime(&timer);
  94. d->year = get_year(tblock);
  95. d->month = get_month(tblock);
  96. d->date = get_day(tblock);
  97. d->day_of_week = get_week(tblock);
  98. d->hour = get_hour(tblock);
  99. d->minute = get_minute(tblock);
  100. d->second = get_second(tblock);
  101. printf("call : rtc_get_datetime_struct() : return value = none\n");
  102. fflush(stdout);
  103. return;
  104. }
  105. void rtc_enable_alarm(int hour, int minute)
  106. {
  107. printf("call : rtc_enable_alarm() : hour = %d, minute = %d\n", hour, minute);
  108. fflush(stdout);
  109. /* 未サポート */
  110. printf("call : rtc_enable_alarm() : not supported\n");
  111. printf("call : rtc_enable_alarm() : return value = none\n");
  112. fflush(stdout);
  113. return;
  114. }
  115. void rtc_disable_alarm(void)
  116. {
  117. printf("call : rtc_disable_alarm() : \n");
  118. fflush(stdout);
  119. /* 未サポート */
  120. printf("call : rtc_disable_alarm() : not supported\n");
  121. printf("call : rtc_disable_alarm() : return value = none\n");
  122. fflush(stdout);
  123. return;
  124. }
  125. void timer_enable(int type, unsigned int auto_preset, unsigned int count)
  126. {
  127. WWTimer ww_timer;
  128. if (!WonX_IsCreated()) WonX_Create();
  129. /* タイマを一時停止する */
  130. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  131. printf("call : timer_enable() : type = %d, auto_preset = %u, count = %u\n",
  132. type, (int)auto_preset, (int)count);
  133. fflush(stdout);
  134. /*
  135. * TIMER_HBLANK の場合は,1/(75*144) 秒?
  136. * TIMER_VBLANK の場合は,1/75 秒
  137. * だが,実際にそんな短い時間にしたら wonx の描画がついていけないので,
  138. * だいぶ長めにしてある.
  139. */
  140. switch (type) {
  141. case TIMER_VBLANK:
  142. ww_timer = WonXSystem_GetWWVBlankCountUpTimer(WonX_GetWonXSystem());
  143. WWTimer_SetPresetCounter(ww_timer, count * WONX_VBLANK_INTERVAL);
  144. break;
  145. case TIMER_HBLANK:
  146. ww_timer = WonXSystem_GetWWHBlankCountUpTimer(WonX_GetWonXSystem());
  147. WWTimer_SetPresetCounter(ww_timer, count * WONX_HBLANK_INTERVAL);
  148. break;
  149. default:
  150. /*
  151. * 無意味だが,gcc -Wall でコンパイルするとワーニングが出るので,
  152. * NULL に初期化する.
  153. */
  154. ww_timer = NULL;
  155. WonX_Error("timer_enable", "Invalid timer type.");
  156. }
  157. switch (auto_preset) {
  158. case TIMER_ONESHOT: WWTimer_SetAutoPresetOFF(ww_timer); break;
  159. case TIMER_AUTOPRESET: WWTimer_SetAutoPresetON( ww_timer); break;
  160. default: WonX_Error("timer_enable", "Invalid auto preset type.");
  161. }
  162. WWTimer_Reset(ww_timer);
  163. WWTimer_ON(ww_timer);
  164. printf("call : timer_enable() : return value = none\n");
  165. fflush(stdout);
  166. /* タイマをもとに戻す */
  167. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  168. return;
  169. }
  170. void timer_disable(int type)
  171. {
  172. WWTimer ww_timer;
  173. if (!WonX_IsCreated()) WonX_Create();
  174. /* タイマを一時停止する */
  175. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  176. printf("call : timer_disable() : type = %d\n", type);
  177. fflush(stdout);
  178. switch (type) {
  179. case TIMER_VBLANK:
  180. ww_timer = WonXSystem_GetWWVBlankCountUpTimer(WonX_GetWonXSystem());
  181. break;
  182. case TIMER_HBLANK:
  183. ww_timer = WonXSystem_GetWWHBlankCountUpTimer(WonX_GetWonXSystem());
  184. break;
  185. default:
  186. /*
  187. * 無意味だが,gcc -Wall でコンパイルするとワーニングが出るので,
  188. * NULL に初期化する.
  189. */
  190. ww_timer = NULL;
  191. WonX_Error("timer_disable", "Invalid timer type.");
  192. }
  193. WWTimer_OFF(ww_timer);
  194. printf("call : timer_disable() : return value = none\n");
  195. fflush(stdout);
  196. /* タイマをもとに戻す */
  197. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  198. return;
  199. }
  200. unsigned int timer_get_count(int type)
  201. {
  202. WWTimer ww_timer;
  203. unsigned int ret = 0;
  204. if (!WonX_IsCreated()) WonX_Create();
  205. /* タイマを一時停止する */
  206. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  207. printf("call : timer_get_count() : type = %d\n", type);
  208. fflush(stdout);
  209. switch (type) {
  210. case TIMER_VBLANK:
  211. ww_timer = WonXSystem_GetWWVBlankCountUpTimer(WonX_GetWonXSystem());
  212. break;
  213. case TIMER_HBLANK:
  214. ww_timer = WonXSystem_GetWWHBlankCountUpTimer(WonX_GetWonXSystem());
  215. break;
  216. default:
  217. /*
  218. * 無意味だが,gcc -Wall でコンパイルするとワーニングが出るので,
  219. * NULL に初期化する.
  220. */
  221. ww_timer = NULL;
  222. WonX_Error("timer_get_count", "Invalid timer type.");
  223. }
  224. ret = WWTimer_GetCounter(ww_timer);
  225. printf("call : timer_get_count() : return value = %u\n", ret);
  226. fflush(stdout);
  227. /* タイマをもとに戻す */
  228. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  229. return (ret);
  230. }
  231. /*****************************************************************************/
  232. /* ここまで */
  233. /*****************************************************************************/
  234. /*****************************************************************************/
  235. /* End of File. */
  236. /*****************************************************************************/