Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / apps / demo_msp / free_rtos / mcu /

main.c

   1  /*******************************************************************************
   2     main: main for FreeRTOS (see http://www.freertos.org/)
   3     Copyright (C) 2010 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Josef Strnadel <strnadel AT stud.fit.vutbr.cz>
   6                Zdenek Vasicek <vasicek AT fit.vutbr.cz>
   7  
   8     LICENSE TERMS
   9  
  10     Redistribution and use in source and binary forms, with or without
  11     modification, are permitted provided that the following conditions
  12     are met:
  13     1. Redistributions of source code must retain the above copyright
  14        notice, this list of conditions and the following disclaimer.
  15     2. Redistributions in binary form must reproduce the above copyright
  16        notice, this list of conditions and the following disclaimer in
  17        the documentation and/or other materials provided with the
  18        distribution.
  19     3. All advertising materials mentioning features or use of this software
  20        or firmware must display the following acknowledgement:
  21  
  22          This product includes software developed by the University of
  23          Technology, Faculty of Information Technology, Brno and its
  24          contributors.
  25  
  26     4. Neither the name of the Company nor the names of its contributors
  27        may be used to endorse or promote products derived from this
  28        software without specific prior written permission.
  29  
  30     This software or firmware is provided ``as is'', and any express or implied
  31     warranties, including, but not limited to, the implied warranties of
  32     merchantability and fitness for a particular purpose are disclaimed.
  33     In no event shall the company or contributors be liable for any
  34     direct, indirect, incidental, special, exemplary, or consequential
  35     damages (including, but not limited to, procurement of substitute
  36     goods or services; loss of use, data, or profits; or business
  37     interruption) however caused and on any theory of liability, whether
  38     in contract, strict liability, or tort (including negligence or
  39     otherwise) arising in any way out of the use of this software, even
  40     if advised of the possibility of such damage.
  41  
  42     $Id$
  43  
  44  
  45  *******************************************************************************/
  46  
  47  #include <fitkitlib.h>
  48  #include "FreeRTOS.h" /* see http://www.freertos.org/ */
  49  #include "task.h"
  50  
  51  #include <keyboard/keyboard.h>
  52  #include <lcd/display.h>
  53  
  54  volatile char last_ch;
  55  unsigned char pressed = 0;
  56  
  57  unsigned long cnt = 0;
  58  
  59  enum eLightIntensity {lightOFF=0, lightLOW=10, lightBRIGHT=100};
  60  enum eLightIntensity lightIntensity = lightOFF;
  61  
  62  unsigned char charmap1[8] = {0x07, 0x08, 0x1e, 0x08, 0x00, 0x0f, 0x0e, 0x09};
  63  /*  76543210
  64   *  .....XXX
  65   *  ....X...
  66   *  ...XXXX.
  67   *  ....X...
  68   *  ........
  69   *  ....XXXX
  70   *  ....XXX.
  71   *  ....X..X
  72   */
  73  
  74  unsigned char charmap2[8] = {0x06, 0x08, 0x08, 0x08, 0x00, 0x1f, 0x04, 0x04};
  75   /* 76543210
  76   *  .....XX.
  77   *  ....X...
  78   *  ....X...
  79   *  ....X...
  80   *  ........
  81   *  ...XXXXX
  82   *  .....X..
  83   *  .....X..
  84   */
  85  
  86  unsigned char charmap3[8] = {0x0c, 0x12, 0x1c, 0x1e, 0x00, 0x0c, 0x12, 0x0c};
  87   /* 76543210
  88   *  ....XX..
  89   *  ...X..X.
  90   *  ...XXX..
  91   *  ...XXXX.
  92   *  ........
  93   *  ....XX..
  94   *  ...X..X.
  95   *  ....XX..
  96   */
  97  
  98  unsigned char charmap4[8] = {0x0c, 0x12, 0x1c, 0x1e, 0x00, 0x0f, 0x04, 0x1e};
  99   /* 76543210
 100   *  ....XX..
 101   *  ...X..X.
 102   *  ...XXX..
 103   *  ...XXXX.
 104   *  ........
 105   *  ....XXXX
 106   *  .....X..
 107   *  ...XXXX.
 108   */
 109  
 110  
 111  /*******************************************************************************
 112   * Bodies of the RT tasks running over FreeRTOS
 113  *******************************************************************************/
 114  
 115  /* ---------------------
 116     keyboard service task
 117     --------------------- */
 118  static void keyboardTask(void *param)
 119  {
 120    char ch;
 121  
 122    last_ch = 0;
 123    keyboard_init();
 124  
 125    for (;;)
 126    {
 127      set_led_d6(0);
 128  
 129      ch = key_decode(read_word_keyboard_4x4());
 130      if (ch != last_ch) {
 131        last_ch = ch;
 132        if (ch != 0) {
 133           switch (ch) {
 134             case 'A':
 135               pressed = 1;
 136               set_led_d6(1);
 137               term_send_str_crlf("\t\tkeyboardTask: 'A' press detected");
 138  
 139               break;
 140             default:
 141               break;
 142           }
 143        }
 144     }
 145     vTaskDelay( 10 / portTICK_RATE_MS); /* delay for 10 ms (= btn-press sampling period) */
 146    }
 147  }
 148  
 149  /* ---------------------
 150     terminal service task
 151     --------------------- */
 152  static void terminalTask(void *param)
 153  {
 154    for (;;) {
 155      terminal_idle();
 156      vTaskDelay( 1000 / portTICK_RATE_MS); /* delay for 1000 ms */
 157    }
 158  }
 159  
 160  /* -----------------
 161     lamp control task
 162     ----------------- */
 163  static void lampTask(void *param)
 164  {
 165    pressed = 0;
 166  
 167    for(;;)
 168    {
 169      /*--- OFF ---*/
 170      lightIntensity = lightOFF;
 171      term_send_str_crlf("lampLight: OFF");
 172      term_send_str_crlf("\tlampTask: waiting for 'A' press (OFF->LOW)");
 173      while(!pressed); //term_send_str_crlf("waiting 4 press");
 174      pressed = 0;
 175  
 176      /*--- LOW ---*/
 177      lightIntensity = lightLOW;
 178      term_send_str_crlf("lampLight: LOW");
 179      term_send_str_crlf("\tlampTask: waiting 500 ms for 'A' press (LOW -> BRIGHT)");
 180      vTaskDelay( 500 / portTICK_RATE_MS); /* delay for 500 ms */
 181      term_send_str_crlf("\tlampTask: time over");
 182  
 183      if(pressed)
 184      {
 185        /*--- BRIGHT ---*/
 186        term_send_str_crlf("\tlampTask: press detected during the time");
 187        pressed = 0;
 188        lightIntensity = lightBRIGHT;
 189        term_send_str_crlf("lampLight: BRIGHT");
 190      }
 191      else     term_send_str_crlf("\tlampTask: no press detected during the time");
 192  
 193      term_send_str_crlf("\tlampTask: waiting for 'A' press (-> OFF)");
 194      while(!pressed);
 195      pressed = 0;
 196    }
 197  }
 198  
 199  /*******************************************************************************
 200   * misc functions
 201  *******************************************************************************/
 202  void print_user_help(void)
 203  {
 204  }
 205  
 206  unsigned char decode_user_cmd(char *cmd_ucase, char *cmd)
 207  {
 208  }
 209  
 210  
 211  void LCD_info(char str[LCD_CHAR_COUNT])
 212  {
 213     /* display usr characters (FreeRTOS) */
 214     LCD_append_char('\x1');
 215     LCD_append_char('\x2');
 216     LCD_append_char('\x3');
 217     LCD_append_char('\x4');
 218  
 219     /* send futher data to LCD */
 220     LCD_send_cmd(0x0c, 0);
 221     LCD_append_string(str);
 222  }
 223  
 224  /*******************************************************************************
 225   * inits preceding main() call
 226  *******************************************************************************/
 227  void fpga_initialized()
 228  {
 229     LCD_init();
 230  
 231     /* load usr characters (FreeRTOS) to LCD */
 232     LCD_load_char(1, charmap1);
 233     LCD_load_char(2, charmap2);
 234     LCD_load_char(3, charmap3);
 235     LCD_load_char(4, charmap4);
 236  
 237     /* welcome text */
 238     LCD_info(" Lamp ctrl");
 239  }
 240  
 241  /*******************************************************************************
 242   * top level code
 243  *******************************************************************************/
 244  int main( void )
 245  {
 246    /*--- HW init ---*/
 247    initialize_hardware();
 248    WDG_stop();
 249    P1DIR |= LED0;
 250    /* timer init */
 251    TBCCTL0 = CCIE;           /* TIMER B interrupts enable */
 252    TBCCR0 = 0x0100;          /* interrupt each x ticks of clk source */
 253    TBCTL = TBSSEL_2 + MC_1;  /* SMCLK, continuous mode */
 254  
 255    term_send_crlf();
 256  
 257    /*--- install FreeRTOS tasks ---*/
 258    term_send_str_crlf("init FreeRTOS tasks...");
 259    xTaskCreate(terminalTask /* code */, "TERM" /* name */, 500 /* stack size */, NULL /* params */, 1 /* prio */, NULL /* handle */);
 260    xTaskCreate(keyboardTask, "KBD", 32, NULL, 1, NULL);
 261    xTaskCreate(lampTask, "LAMP", 32, NULL, 1, NULL);
 262  
 263    /* ------------------------------------------------
 264       FreeRTOS details
 265       can be found at
 266       http://www.freertos.org/
 267       ------------------------------------------------ */
 268  
 269    /*--- start FreeRTOS kernel ---*/
 270    term_send_str_crlf("starting FreeRTOS scheduler...\n");
 271    vTaskStartScheduler();
 272  
 273    return 0;
 274  }
 275  
 276  /*******************************************************************************
 277   * interrupt service routines
 278  *******************************************************************************/
 279  interrupt (TIMERB0_VECTOR) Timer_B (void)
 280  {
 281  
 282    cnt++;
 283  
 284    if((cnt>=0) && (cnt<=lightIntensity)) { P1OUT=0; } /* D5 on-time */
 285    else { P1OUT=1; }  /* D5 off-time */
 286    if(cnt>=(100)) {cnt=0;}
 287  
 288    TBCCR0 = 0x0100;          /* interrupt each x ticks of clk source */
 289  }
 290  
Zobrazeno: 770823x Naposledy: 28.3.2023 01:43:26