00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include "board.h"
00046 #include "gpio.h"
00047 #include "rtc.h"
00048 #include "spi.h"
00049 #include "intc.h"
00050 #include "qt60168.h"
00051 #include "conf_qt60168.h"
00052
00053
00054 static unsigned short New_status = 0;
00055 static unsigned short Old_status = 0;
00056 static int Read_data=0;
00057 static unsigned short Data;
00058 static int report_state=0;
00059
00060 Bool special_qt60168_report_all_key(unsigned short *data);
00061 static int special_qt60168_get_reply(unsigned short *data);
00062 static int special_qt60168_send_cmd(unsigned char cmd);
00063
00064 #if __GNUC__
00065 __attribute__((__interrupt__))
00066 #elif __ICCAVR32__
00067 __interrupt
00068 #endif
00069 void rtc_irq(void)
00070 {
00071 static volatile int delay_count=0;
00072 static unsigned short all_key= 0;
00073 static int update_delay = 0;
00074
00075 delay_count++;
00076 update_delay++;
00077
00078 if(update_delay>10) {
00079 if(special_qt60168_report_all_key(&all_key)==TRUE) {
00080 update_delay=0;
00081
00082 Old_status = New_status;
00083 New_status = all_key;
00084 if(Old_status != New_status) {
00085 update_joystick_status( New_status);
00086 }
00087 if(New_status!=0) {
00088 gpio_clr_gpio_pin(LED2_GPIO);
00089 }
00090 else {
00091 gpio_set_gpio_pin(LED2_GPIO);
00092 }
00093 }
00094 }
00095
00096 if(Read_data==1) {
00097 Read_data=2;
00098 delay_count=0;
00099 }
00100 if((delay_count>1)&&(Read_data==2)) {
00101
00102
00103 spi_selectChip(QT60168_SPI,QT60168_SPI_NCPS);
00104
00105 spi_read(QT60168_SPI, &Data);
00106
00107 spi_unselectChip(QT60168_SPI,QT60168_SPI_NCPS);
00108 Read_data=0;
00109 }
00110
00111
00112 rtc_clear_interrupt(&AVR32_RTC);
00113
00114 }
00115
00116 void rtc_init_qt( void ) {
00117
00118
00119 Disable_global_interrupt();
00120
00121
00122 INTC_register_interrupt(&rtc_irq, AVR32_RTC_IRQ, AVR32_INTC_INT0);
00123
00124
00125
00126 rtc_init(&AVR32_RTC, RTC_OSC_32KHZ, 4);
00127
00128
00129 rtc_set_top_value(&AVR32_RTC, 1);
00130
00131 rtc_enable_interrupt(&AVR32_RTC);
00132
00133
00134 rtc_enable(&AVR32_RTC);
00135
00136
00137 Enable_global_interrupt();
00138
00139 }
00140
00141 Bool special_qt60168_report_all_key(unsigned short *data)
00142 {
00143 static int AllKey=0;
00144 unsigned short dataread;
00145
00146 switch(report_state) {
00147 case 0:
00148 AllKey=0;
00149 if(special_qt60168_send_cmd(QT60168_CMD_REPORT_ALL_KEYS)==0 ) {
00150 report_state=1;
00151 }
00152 break;
00153 case 1:
00154 if(special_qt60168_get_reply(&dataread)==0 ) {
00155 AllKey=dataread;
00156 report_state=2;
00157 }
00158 break;
00159 case 2:
00160 if(special_qt60168_get_reply(&dataread)==0 ) {
00161 AllKey|=dataread<<8;
00162 report_state=3;
00163 }
00164 break;
00165 case 3:
00166 if(special_qt60168_get_reply(&dataread)==0 ) {
00167 AllKey|=dataread<<16;
00168 report_state=4;
00169 }
00170 break;
00171 case 4:
00172 if(special_qt60168_get_reply(&dataread)==0 ) {
00173
00174 *data = AllKey;
00175 report_state=0;
00176 }
00177 break;
00178 default:
00179 report_state=0;
00180 }
00181 if (report_state==0) {
00182 return TRUE;
00183 }
00184 else {
00185 return FALSE;
00186 }
00187 }
00188
00189 static int special_qt60168_send_cmd(unsigned char cmd)
00190 {
00191 static int state=0;
00192 switch(state) {
00193 case 0:
00194
00195 spi_selectChip(QT60168_SPI,QT60168_SPI_NCPS);
00196
00197 spi_write(QT60168_SPI, cmd);
00198
00199 spi_unselectChip(QT60168_SPI,QT60168_SPI_NCPS);
00200 state = 1;
00201 Read_data=1;
00202 break;
00203 case 1:
00204 if(Read_data==0) {
00205 state=0;
00206 }
00207 break;
00208 default:
00209 state=0;
00210 }
00211
00212 return state;
00213 }
00214
00215 static int special_qt60168_get_reply(unsigned short *data)
00216 {
00217
00218 if(special_qt60168_send_cmd(QT60168_CMD_NULL_COMMAND)==0) {
00219 *data = Data;
00220 return 0;
00221 }
00222 else {
00223 return 1;
00224 };
00225 }