timer.c 8.5 KB

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