Advertisement
Guest User

Untitled

a guest
Nov 28th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.09 KB | None | 0 0
  1. /*******************************************************************************
  2. *   $FILE:  main.c
  3. *   Atmel Corporation:  http://www.atmel.com \n
  4. *   Support email:  [email protected]
  5. ******************************************************************************/
  6.  
  7. /*  License
  8. *   Copyright (c) 2010, Atmel Corporation All rights reserved.
  9. *
  10. *   Redistribution and use in source and binary forms, with or without
  11. *   modification, are permitted provided that the following conditions are met:
  12. *
  13. *   1. Redistributions of source code must retain the above copyright notice,
  14. *   this list of conditions and the following disclaimer.
  15. *
  16. *   2. Redistributions in binary form must reproduce the above copyright notice,
  17. *   this list of conditions and the following disclaimer in the documentation
  18. *   and/or other materials provided with the distribution.
  19. *
  20. *   3. The name of ATMEL may not be used to endorse or promote products derived
  21. *   from this software without specific prior written permission.
  22. *
  23. *   THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  24. *   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
  26. *   SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
  27. *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34.  
  35. /*----------------------------------------------------------------------------*/
  36.  
  37. /*----------------------------------------------------------------------------
  38. Device Fuse Settings
  39. ----------------------------------------------------------------------------*/
  40. /*  The default fuse settings for the device will be correct for this project. However it is adviceable to verify
  41. the fuse settings before trying to run the application.
  42.  
  43. WDWP:       8 cycles (8ms @ 3.3V)
  44. WDP:        8 cycles (8ms @ 3.3V)
  45. BOOTRST:    Application Reset
  46. BODACT:     BOD Disabled
  47. BODPD:      BOD Disabled
  48. JTAGEN:     Programmed
  49.  
  50. Fusebyte0: 0xFF
  51. Fusebyte1: 0x00
  52. Fusebyte2: 0xFF
  53. Fusebyte4: 0xFE
  54. Fusebyte5: 0xFF
  55. */
  56.  
  57. /*----------------------------------------------------------------------------
  58. compiler information
  59. ----------------------------------------------------------------------------*/
  60.  
  61. /*----------------------------------------------------------------------------
  62. include files
  63. ----------------------------------------------------------------------------*/
  64.  
  65. #include <avr/io.h>
  66.  
  67. #include <util/delay_basic.h>
  68. #include <avr/interrupt.h>
  69.  
  70. #define __delay_cycles(n)     __builtin_avr_delay_cycles(n)
  71. #define __enable_interrupt()  sei()
  72.  
  73. /*  now include touch api.h with the localization defined above */
  74. #include "touch_api.h"
  75.  
  76. /* now include the debug files----------------------------------------------*/
  77. #include "QDebug.h"
  78. #include "QDebugTransport.h"
  79.  
  80. /*----------------------------------------------------------------------------
  81. manifest constants
  82. ----------------------------------------------------------------------------*/
  83.  
  84. /*----------------------------------------------------------------------------
  85. type definitions
  86. ----------------------------------------------------------------------------*/
  87.  
  88. /*----------------------------------------------------------------------------
  89. prototypes
  90. ----------------------------------------------------------------------------*/
  91. /*  initialise host app, pins, watchdog, etc    */
  92. static void init_system( void );
  93. /*  configure timer ISR to fire regularly   */
  94. static void init_timer_isr( void );
  95. /*  Assign the parameters values to global configuration parameter structure*/
  96. static void qt_set_parameters( void );
  97.  
  98. /* ON/OFF */
  99. #define ON 0xFF
  100. #define OFF 0x00
  101.  
  102. #define PD2 0x672
  103. /*----------------------------------------------------------------------------
  104. Structure Declarations
  105. ----------------------------------------------------------------------------*/
  106.  
  107. /*----------------------------------------------------------------------------
  108. macros
  109. ----------------------------------------------------------------------------*/
  110. /*  The timing information for timer to fire periodically to measure touch  */
  111. #ifdef TICKS_PER_MS
  112. #undef TICKS_PER_MS
  113. #define TICKS_PER_MS                1000u //500u. 1.1 change as clock is running at 8MHz
  114. #endif
  115.  
  116. #define GET_SENSOR_STATE(SENSOR_NUMBER) qt_measure_data.qt_touch_status.sensor_states[(SENSOR_NUMBER/8)] & (1 << (SENSOR_NUMBER % 8))
  117. #define GET_ROTOR_SLIDER_POSITION(ROTOR_SLIDER_NUMBER) qt_measure_data.qt_touch_status.rotor_slider_values[ROTOR_SLIDER_NUMBER]
  118.  
  119. #define TIMER_COUNTER_L TCC0.CNTL
  120. #define TIMER_COUNTER_H TCC0.CNTH
  121.  
  122. #ifndef PD2
  123. #define PD2 PORTD_PIN2CTRL
  124. #endif
  125.  
  126. /*----------------------------------------------------------------------------
  127. global variables
  128. ----------------------------------------------------------------------------*/
  129. uint8_t sensor_config[QT_NUM_CHANNELS];
  130. //board_info_t board_info;
  131.  
  132. /* Timer period in msec. */
  133. uint16_t qt_measurement_period_msec = 25u;
  134.  
  135. /* Declare the SNS_array[][] and the SNSK_array[][] here -------------------*/
  136. #ifdef QTOUCH_STUDIO_MASKS
  137. extern TOUCH_DATA_T SNS_array[2][2];
  138. extern TOUCH_DATA_T SNSK_array[2][2];
  139. #endif
  140.  
  141. /*------------------For Debug Data------------------------------------------*/
  142. #ifdef _DEBUG_INTERFACE_
  143.     extern uint16_t timestamp1_hword;
  144.     extern uint16_t timestamp1_lword;    
  145.     extern uint16_t timestamp2_hword;
  146.     extern uint16_t timestamp2_lword;
  147.     extern uint16_t timestamp3_hword;
  148.     extern uint16_t timestamp3_lword;
  149. #endif
  150.  
  151. /*---------------------------------------------------------------------------*/
  152.  
  153. /*----------------------------------------------------------------------------
  154. extern variables
  155. ----------------------------------------------------------------------------*/
  156. /* This configuration data structure parameters if needs to be changed will be
  157. changed in the qt_set_parameters function */
  158. extern qt_touch_lib_config_data_t qt_config_data;
  159. /* touch output - measurement data */
  160. extern qt_touch_lib_measure_data_t qt_measure_data;
  161. /* Get sensor delta values */
  162. extern int16_t qt_get_sensor_delta( uint8_t sensor);
  163. /* Output can be observed in the watch window using this pointer */
  164. qt_touch_lib_measure_data_t *pqt_measure_data = &qt_measure_data;
  165.  
  166. /*----------------------------------------------------------------------------
  167. static variables
  168. ----------------------------------------------------------------------------*/
  169. /* flag set by timer ISR when it's time to measure touch */
  170. static volatile uint8_t time_to_measure_touch = 0u;
  171. /* current time, set by timer ISR */
  172. volatile uint16_t current_time_ms_touch = 0u;
  173.  
  174. /*============================================================================
  175. Name    :   main
  176. ------------------------------------------------------------------------------
  177. Purpose :   main code entry point
  178. Input   :   n/a
  179. Output  :   n/a
  180. Notes   :
  181. ============================================================================*/
  182. int main( void )
  183. {
  184.     /*status flags to indicate the re-burst for library*/
  185.     uint16_t status_flag = 0u;
  186.     uint16_t burst_flag = 0u;
  187.  
  188.     init_timer_isr();
  189.     /*Pin configuration code as generated by Pin Configuration Wizard */
  190.     #ifdef QTOUCH_STUDIO_MASKS
  191.     SNS_array[0][0]= 0x55;
  192.     SNS_array[0][1]= 0xaa;
  193.     SNS_array[1][0]= 0x1d;
  194.     SNS_array[1][1]= 0xe2;
  195.     SNSK_array[0][0]= 0x55;
  196.     SNSK_array[0][1]= 0xaa;
  197.     SNSK_array[1][0]= 0x1d;
  198.     SNSK_array[1][1]= 0xe2;
  199.     #endif
  200.    
  201.     /*Add code for debug data */
  202.     #ifdef _DEBUG_INTERFACE_
  203.         timestamp1_hword = current_time_ms_touch;
  204.         timestamp1_lword = (uint16_t)TIMER_COUNTER_L;
  205.         timestamp1_lword |= (uint16_t)(TIMER_COUNTER_H << 8);
  206.     #endif
  207.    
  208.     /* initialise host app, pins, watchdog, etc */
  209.     init_system();
  210.    
  211.     /* Config Sensors */
  212.     qt_enable_key( CHANNEL_0, NO_AKS_GROUP, 10u, HYST_6_25 );
  213.     qt_enable_key( CHANNEL_1, NO_AKS_GROUP, 10u, HYST_6_25 );
  214.     qt_enable_key( CHANNEL_2, NO_AKS_GROUP, 10u, HYST_6_25 );
  215.     qt_enable_key( CHANNEL_3, NO_AKS_GROUP, 10u, HYST_6_25 );
  216.     qt_enable_key( CHANNEL_4, NO_AKS_GROUP, 10u, HYST_6_25 );
  217.     qt_enable_key( CHANNEL_5, NO_AKS_GROUP, 10u, HYST_6_25 );
  218.     qt_enable_key( CHANNEL_6, NO_AKS_GROUP, 10u, HYST_6_25 );
  219.     qt_enable_key( CHANNEL_7, NO_AKS_GROUP, 10u, HYST_6_25 );
  220.     qt_enable_key( CHANNEL_8, NO_AKS_GROUP, 10u, HYST_6_25 );
  221.     qt_enable_key( CHANNEL_9, NO_AKS_GROUP, 10u, HYST_6_25 );
  222.     qt_enable_slider( CHANNEL_10, CHANNEL_12, NO_AKS_GROUP, 10u, HYST_6_25, RES_8_BIT, 0u );
  223.     qt_enable_rotor(CHANNEL_13, CHANNEL_15, NO_AKS_GROUP, 10u, HYST_50, RES_8_BIT, 0u);
  224.    
  225.     qt_set_parameters();
  226.    
  227.     /* Config sensors  */
  228.     sensor_config[0] = SENSOR_CONFIG( CHANNEL_0, CHANNEL_0, SENSOR_TYPE_KEY );
  229.     /*sensor_config[1] = SENSOR_CONFIG( CHANNEL_1, CHANNEL_1, SENSOR_TYPE_KEY );
  230.     sensor_config[2] = SENSOR_CONFIG( CHANNEL_2, CHANNEL_2, SENSOR_TYPE_KEY );
  231.     sensor_config[3] = SENSOR_CONFIG( CHANNEL_3, CHANNEL_3, SENSOR_TYPE_KEY );
  232.     sensor_config[4] = SENSOR_CONFIG( CHANNEL_4, CHANNEL_4, SENSOR_TYPE_KEY );
  233.     sensor_config[5] = SENSOR_CONFIG( CHANNEL_5, CHANNEL_5, SENSOR_TYPE_KEY );
  234.     sensor_config[6] = SENSOR_CONFIG( CHANNEL_6, CHANNEL_6, SENSOR_TYPE_KEY );
  235.     sensor_config[7] = SENSOR_CONFIG( CHANNEL_7, CHANNEL_7, SENSOR_TYPE_KEY );
  236.     sensor_config[8] = SENSOR_CONFIG( CHANNEL_8, CHANNEL_8, SENSOR_TYPE_KEY );
  237.     sensor_config[9] = SENSOR_CONFIG( CHANNEL_9, CHANNEL_9, SENSOR_TYPE_KEY );
  238.     sensor_config[10] = SENSOR_CONFIG( CHANNEL_10, CHANNEL_12, SENSOR_TYPE_KEY );
  239.     sensor_config[11] = SENSOR_CONFIG( CHANNEL_13, CHANNEL_15, SENSOR_TYPE_KEY );*/
  240.            
  241.     /* Initialise and set touch parameters */
  242.     qt_init_sensing();
  243.        
  244.     /* Filter callback function */
  245.     qt_filter_callback = 0;
  246.    
  247.     /* Initialize debug protocol */
  248.     #ifdef _DEBUG_INTERFACE_
  249.     QDebug_Init();
  250.     #endif
  251.    
  252.     /* Process commands from PC */
  253.     __enable_interrupt();
  254.  
  255.     PORTD_DIR = (1<<2);
  256.      
  257.     /* loop forever */
  258.     for( ; ; )
  259.     {
  260.        
  261.         /* key_states will get populated with the binary states of the eight first sensors */
  262.         char key_states = qt_measure_data.qt_touch_status.sensor_states[1];
  263.      
  264.         /* pridobi slider pozicijo 255-0 z leve*/
  265.         uint16_t sliderval = qt_measure_data.qt_touch_status.rotor_slider_values[0];
  266.        
  267.         if ( key_states & 1) {
  268.             PORTD_OUT = (OFF<<2);
  269.         }
  270.         if ( GET_SENSOR_STATE(0) == 1) {
  271.             PORTD_OUT = (OFF<<2);
  272.         }
  273.         if ( GET_SENSOR_STATE(8) == 1) {
  274.             PORTD_OUT = (ON<<2);
  275.         }
  276.                    
  277.         if( time_to_measure_touch )
  278.         {
  279.             time_to_measure_touch = 0u;
  280.             do
  281.             {
  282.                 /*Add code to initialize timestamp2 */
  283.                 #ifdef _DEBUG_INTERFACE_
  284.                     timestamp2_hword = current_time_ms_touch;
  285.                     timestamp2_lword = (uint16_t)TIMER_COUNTER_L;
  286.                     timestamp2_lword |= (uint16_t)(TIMER_COUNTER_H << 8);
  287.                 #endif
  288.                 /* Measure touch once */
  289.                 status_flag = qt_measure_sensors(current_time_ms_touch);
  290.                 burst_flag = status_flag & QTLIB_BURST_AGAIN;
  291.                
  292.                
  293.                 /*Add code to initialize timestamp3 */
  294.                 #ifdef _DEBUG_INTERFACE_
  295.                     timestamp3_hword = current_time_ms_touch;
  296.                     timestamp3_lword = (uint16_t)TIMER_COUNTER_L;
  297.                     timestamp3_lword |= (uint16_t)(TIMER_COUNTER_H << 8);
  298.                 #endif
  299.                 /* send debug data */
  300.                 #ifdef _DEBUG_INTERFACE_
  301.                 QDebug_SendData(status_flag);
  302.                 #endif
  303.             }while (burst_flag) ;
  304.             /* Process commands from PC */
  305.             #ifdef _DEBUG_INTERFACE_
  306.             QDebug_ProcessCommands();
  307.             #endif
  308.         }    
  309.        
  310.     }
  311. }
  312.  
  313. /*============================================================================
  314. Name    :   qt_set_parameters
  315. ------------------------------------------------------------------------------
  316. Purpose :   This will fill the default threshold values in the configuration
  317. data structure.But User can change the values of these parameters .
  318. Input   :   n/a
  319. Output  :   n/a
  320. Notes   :   initialize configuration data for processing
  321. ============================================================================*/
  322. static void qt_set_parameters( void )
  323. {
  324.     qt_config_data.qt_di = DEF_QT_DI;
  325.     qt_config_data.qt_neg_drift_rate = DEF_QT_NEG_DRIFT_RATE;
  326.     qt_config_data.qt_pos_drift_rate = DEF_QT_POS_DRIFT_RATE;
  327.     qt_config_data.qt_max_on_duration = DEF_QT_MAX_ON_DURATION;
  328.     qt_config_data.qt_drift_hold_time = DEF_QT_DRIFT_HOLD_TIME;
  329.     qt_config_data.qt_recal_threshold = DEF_QT_RECAL_THRESHOLD;
  330.     qt_config_data.qt_pos_recal_delay = DEF_QT_POS_RECAL_DELAY;
  331. }
  332.  
  333. /*============================================================================
  334. Name    :   init_timer_isr
  335. ------------------------------------------------------------------------------
  336. Purpose :   configure timer ISR to fire regularly
  337. Input   :   n/a
  338. Output  :   n/a
  339. Notes   :
  340. ============================================================================*/
  341. static void init_timer_isr( void )
  342. {
  343.     /* Set timer period */
  344.     TCC0.PER = TICKS_PER_MS * qt_measurement_period_msec;
  345.     /* select clock source */
  346.     TCC0.CTRLA = (TOUCH_DATA_T)4;
  347.     /* Set Compare A interrupt to low level */
  348.     TCC0.INTCTRLB = 1u;
  349.     /* enable low lever interrupts in power manager interrupt control */
  350.     PMIC.CTRL |= 1u;
  351. }
  352. /*============================================================================
  353. Name    :   CCP write helper function written in assembly.
  354. ------------------------------------------------------------------------------
  355. Purpose :   This function is written in assembly because of the timecritial
  356. operation of writing to the registers for xmega.
  357. Input   :   address - A pointer to the address to write to.
  358. value   - The value to put in to the register.
  359. Notes   :
  360. ============================================================================*/
  361. void CCPWrite( volatile uint8_t * address, uint8_t value )
  362. {
  363.    volatile uint8_t * tmpAddr = address;
  364. #ifdef RAMPZ
  365.    RAMPZ = 0;
  366. #endif
  367.    asm volatile(
  368.                 "movw r30,  %0" "\n\t"
  369.                 "ldi  r16,  %2" "\n\t"
  370.                 "out   %3, r16" "\n\t"
  371.                 "st     Z,  %1"
  372.                 :
  373.                 : "r" (tmpAddr), "r" (value), "M" (CCP_IOREG_gc), "m" (CCP)
  374.                 : "r16", "r30", "r31"
  375.                );
  376. }
  377.  
  378. /*============================================================================
  379. Name    :   init_system
  380. ------------------------------------------------------------------------------
  381. Purpose :   initialise host app, pins, watchdog, etc
  382. Input   :   n/a
  383. Output  :   n/a
  384. Notes   :
  385. ============================================================================*/
  386. static void init_system( void )
  387. {
  388.    uint8_t PSconfig;
  389.    uint8_t clkCtrl;
  390.  
  391.    /*  Configure Oscillator and Clock source   */
  392.  
  393.    /*  Select Prescaler A divider as 4 and Prescaler B & C divider as (1,1) respectively.  */
  394.    /*  Overall divide by 4 i.e. A*B*C  */
  395.    PSconfig = (uint8_t) CLK_PSADIV_4_gc | CLK_PSBCDIV_1_1_gc;
  396.    /*  Enable internal 32 MHz ring oscillator. */
  397.    OSC.CTRL |= OSC_RC32MEN_bm;
  398.    CCPWrite( &CLK.PSCTRL, PSconfig );
  399.    /*  Wait until oscillator is ready. */
  400.    while ( ( OSC.STATUS & OSC_RC32MRDY_bm ) == 0 );
  401.    /*  Set the 32 MHz ring oscillator as the main clock source */
  402.    clkCtrl = ( CLK.CTRL & ~CLK_SCLKSEL_gm ) | CLK_SCLKSEL_RC32M_gc;
  403.    CCPWrite( &CLK.CTRL, clkCtrl );
  404.  
  405.    /*  Route clk signal to port pin    */
  406.    /*  PORTCFG_CLKEVOUT = 0x03;    */
  407.    /*  PORTE_DIRSET = 0x80;    */
  408. }
  409.  
  410. /*============================================================================
  411. Name    :   timer_isr
  412. ------------------------------------------------------------------------------
  413. Purpose :   timer 1 compare ISR
  414. Input   :   n/a
  415. Output  :   n/a
  416. Notes   :
  417. ============================================================================*/
  418. ISR(TCC0_CCA_vect)
  419. {
  420.    /* set flag: it's time to measure touch */
  421.     time_to_measure_touch = 1u;
  422.     /* update the current time */
  423.     current_time_ms_touch += qt_measurement_period_msec;
  424. }
  425.  
  426.  
  427.  
  428.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement