⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 partest.c

📁 430上实现简单的web服务器
💻 C
字号:
/*
	FreeRTOS V2.5.5 - Copyright (C) 2003 - 2005 Richard Barry.

	This file is part of the FreeRTOS distribution.

	FreeRTOS is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	FreeRTOS is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with FreeRTOS; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	A special exception to the GPL can be applied should you wish to distribute
	a combined work that includes FreeRTOS, without being obliged to provide
	the source code for any proprietary components.  See the licensing section 
	of http://www.FreeRTOS.org for full details of how and when the exception
	can be applied.

	***************************************************************************
	See http://www.FreeRTOS.org for documentation, latest information, license 
	and contact details.  Please ensure to read the configuration and relevant 
	port sections of the online documentation.
	***************************************************************************
*/

/*
 * This demo is configured to execute on the EasyWeb2 by Olimex.  That board
 * provides an only 1 LED but does have a 16x2 LCD.  For the demo the top line 
 * will be used to represent the state of the various LEDS, with one character
 * representing a LED.  Because some LED activity is MUCH faster than the LCD
 * refresh rate there are two types of `Toggle' behaviour supported:
 * - Normal: where a character alternates between '.' and '*'
 * - Incrementing: where the character displayed increments from ' ' to '~'
 *    at every toggle.
 * Just because we can, the bottom line displays either a scrolling string or
 * a scroll of the entire character set.
 */

/* Standard includes. */
#include <stdlib.h>
#include <signal.h>

/* Scheduler includes. */
#include "projdefs.h"
#include "task.h"
#include "portable.h"

/* Demo application includes. */
#include "partest.h"

// This code is based on code originaly from EasyWeb.c by Olimex.
// To be fair, have heavily modified it to suit our purposes of working
// with FreeRTOS.
// In this case we manage the LCD display by showing a string across
// the top line with a `.' representing an off LED and an `*' an on LED.
#define   LCD_Data           P2OUT          
#define   E                  3                 //P2.3
#define   RS                 2                 //P2.2
#define   CR                 0x0d
#define   LF                 0x0a

#define   STATUS_LED_ON      P2OUT &= ~BIT1    //STATUS_LED - P2.1
#define   STATUS_LED_OFF     P2OUT |= BIT1     //STATUS_LED - P2.1

#define   DISP_ON       0x0c          //LCD control constants
#define   DISP_OFF      0x08          //
#define   CLR_DISP      0x01          //
#define   CUR_HOME      0x02          //
#define   ENTRY_INC     0x06          //
#define   DD_RAM_ADDR   0x80          //
#define   DD_RAM_ADDR2  0xc0          //
#define   DD_RAM_ADDR3  0x28          //
#define   CG_RAM_ADDR   0x40          //

// vvv  Timing stuff
// For very short delays (< 1ms) we run in a tight loop.  
// *WARNING* Even though this is taken from the original Olimex code, timing
// routines like this are very unreliable (the compiler may choose to optimise
// away the loop).  This should be rewritten before being used in production 
// code.
// NOTE: For longer delays, make use of the FreeRTOS task control functions.
// (i.e. vTaskDelay() and vTaskDelayUntil() ).
// NOTE: The short timing routines are very dependant on cpu Hz, so if that 
// changes, then this will have to change as well.
// NOTE: If we need to do this sort of thing a lot, we might want to consider
// testing if a task switch has happened as well.
// NOTE: These timing routines are designed to return no sooner than the
// requested time (context switches taking several ms can occur).  If it is
// desired to return after an exact delay, don't use these.
#define   _100us             66               //66 cycles *12 + 9 = 801 / 801*125ns = 100us
#define   _10us              6                //6 cycles * 12 + 9 = 81 / 81*125ns=10us
void Delay (unsigned int a)
{
  unsigned portCHAR k;
  for (k=0; k != a; ++k);                     //9+a*12 cycles
}

// For longer delays we rely on the tick period.  Because we are still
// playing with that it _may_ be possible that some required periods are less
// than portTICK_RATE_MS, in which case the required delay is 1.  We define 
// the required delays in terms of ticks here.
#define   _05ms       1
#define   _10ms       ((portTickType) 10 / portTICK_RATE_MS)
#define   _100ms      ((portTickType) 100 / portTICK_RATE_MS)
// ^^^  Timing stuff

// LCD routines, modified to make use of FreeRTOS delay routines, for any 
// delay over 0.5ms.
void _E(void)
{
  bitset(P2OUT,E);    //toggle E for LCD
  Delay(_10us);
  bitclr(P2OUT,E);
}

void SEND_CHAR (unsigned char d)
{
  unsigned portCHAR temp;
  
  vTaskDelay(_05ms);              //.5ms  
  temp = d & 0xf0;                //get upper nibble  
  LCD_Data &= 0x0f;
  LCD_Data |= temp;               
  bitset(P2OUT,RS);               //set LCD to data mode
  _E();                           //toggle E for LCD
  temp = d & 0x0f;
  temp = temp << 4;               //get down nibble
  LCD_Data &= 0x0f;
  LCD_Data |= temp;
  bitset(P2OUT,RS);               //set LCD to data mode
  _E();                           //toggle E for LCD
}

void SEND_CMD (unsigned char e)
{
  unsigned portCHAR temp;
  
  vTaskDelay(_10ms);              //10ms
  temp = e & 0xf0;                //get upper nibble  
  LCD_Data &= 0x0f;
  LCD_Data |= temp;               //send CMD to LCD
  bitclr(P2OUT,RS);               //set LCD to CMD mode
  _E();                           //toggle E for LCD
  temp = e & 0x0f;
  temp = temp << 4;               //get down nibble
  LCD_Data &= 0x0f;
  LCD_Data |= temp;
  bitclr(P2OUT,RS);               //set LCD to CMD mode
  _E();                           //toggle E for LCD
}

void vLcdInit(void)
{
  bitclr(P2OUT,RS);
  vTaskDelay(_100ms);             //Delay 100ms
  LCD_Data |= BIT4 | BIT5;        //D7-D4 = 0011
  LCD_Data &= ~BIT6 & ~BIT7;
  _E();                           //toggle E for LCD
  vTaskDelay(_10ms);              //10ms
  _E();                           //toggle E for LCD
  vTaskDelay(_10ms);              //10ms
  _E();                           //toggle E for LCD
  vTaskDelay(_10ms);              //10ms
  LCD_Data &= ~BIT4;
  _E();                           //toggle E for LCD

  SEND_CMD(DISP_ON);
  SEND_CMD(CLR_DISP);   
}

#define LCD_NUM_COLS        ((unsigned portCHAR) 16)  // Num of LCD cols
#define LCD_NUM_ROWS        ((unsigned portCHAR) 2)   // Num of LCD rows
#define LCD_NUM_CHARS       (LCD_NUM_COLS * LCD_NUM_ROWS) // # of LCD chars 
#define LCD_IDX_ROW2        (LCD_NUM_COLS)                // Index to col 1 row 2
#define LCD_IDX_ROW3        (LCD_NUM_COLS + LCD_IDX_ROW2) // Index to col 1 row 3
                                                    // "                                "
                                                    // "1234567890abcdef1234567890abcdef"
static unsigned portCHAR lcdString[LCD_NUM_CHARS+1] = {"                1234567890abcdef"};
// this is the 2nd line to be displayed.  Can be anything but MUST be longer
// than LCD_NUM_COLS in length.
const unsigned portCHAR lcd2ndLine[] = {"EasyWeb2 port of FreeRTOS, brought to you by NOJA Power Switchgear Pty Ltd.  "};

// How often the LCD is redrawn.
#define   LCD_REFRESH_DELAY       ((portTickType) 50 / portTICK_RATE_MS)
#define   LCD_TASK_PRIORITY       (tskIDLE_PRIORITY)

// This is the LcdRefresh task.  This does all the hard work, simply 
// writes the string to the display.  
// NOTE that writing an individual char takes at least one tick (1 tick == 1ms) 
// --> time to write 32 chars == 32ms.  Add to that 2 x 130ms == 260ms to send 
// the 2 commands (to move to start of each line) and the total rewrite time
// is at least 292ms.  LCD_REFRESH_DELAY is the time that the process will
// wait before starting the whole thing over again
static void vLcdRefresh(void *pvParameters)
{
  unsigned portCHAR i;

  // Init the LCD
  vLcdInit();
    
  /* Cycle for ever, delaying then writing the string. */
  for( ;; )
  {
    /* Wait until the next time to refresh. */
    vTaskDelay(LCD_REFRESH_DELAY);

    SEND_CMD (DD_RAM_ADDR);                     // move to top line
    for (i=0; i < LCD_NUM_CHARS; i++)  
    {
      if (i==LCD_IDX_ROW2) 
        SEND_CMD (DD_RAM_ADDR2);                // move to 2nd line
      else if (i==LCD_IDX_ROW3) 
        SEND_CMD (DD_RAM_ADDR3);                // move to 3rd line
      SEND_CHAR(lcdString[i]);
    }  
  }
}

// Here be LCD code specific to the ParTest routines
// What we have are 3 types of LEDs emulations with different behaviours
// when specified in a call to vParTestToggleLED():
// -  TOGL LEDs will merely toggle between two states (LCD_LED_OFF and 
// LCD_LED_ON).  
// -  INCR LEDs will increment the value of the displayed ASCII char, 
// starting from LCD_LED_LO incrementing to LCD_LED_HI.  These are useful
// for capturing very fast events (e.g. ComTest).
// -  The REAL LED toggles the board's STATUS LED.  NOTE that this happens
// independently of the LCD refresh routine, so is useful as a low level
// diagnostic (the LED will blink even if the LCD routines are not running).
#define partstNUM_TOGL_LEDS ((unsigned portCHAR) 4) 
#define partstNUM_INCR_LEDS ((unsigned portCHAR) 3) 
#define partstREAL_LED      ((unsigned portCHAR) 10)
#define LCD_LED_OFF         ((unsigned portCHAR ) '.')
#define LCD_LED_ON          ((unsigned portCHAR ) '*')
#define LCD_LED_LO          ((unsigned portCHAR ) ' ')
#define LCD_LED_HI          ((unsigned portCHAR ) '~')


static void prvToggleOnBoardLED( void )
{
static unsigned portSHORT sState = pdFALSE;

  /* Toggle the state of the single genuine on board LED. */
  if( sState )  
  {
    STATUS_LED_OFF;
  }
  else
  {
    STATUS_LED_ON;
  }

  sState = !sState;
}

// Routine to put something interesting on the 2nd line.  
// Will be called periodically, probably by the ToggleLed routine, every
// `n' toggles.
static void prvSetSecondLine()
#if 1   // prvSetSecondLine()
// Something interesting is a scroll of the available characters (i.e. from
// 0 to 255).
{
  static unsigned portCHAR ucInitIdx = 0;
  unsigned portCHAR ucStrIdx = ucInitIdx++;  
  unsigned portCHAR i;
  
  for (i=LCD_IDX_ROW2; i < LCD_IDX_ROW3; i++)  
  {
    lcdString[i] = ucStrIdx++;
  }
}
#else   // #if 1   // prvSetSecondLine()
// Something interesting is merely the fixed text lcd2ndLine, starting from 
// the previous offset into that string + 1.
{
  static unsigned portCHAR ucInitIdx = 0;
  unsigned portCHAR ucStrIdx = ucInitIdx++;  
  unsigned portCHAR i;
  
  if (ucInitIdx == sizeof(lcd2ndLine)-1)
  {
    ucInitIdx = 0;
  }
  for (i=LCD_IDX_ROW2; i < LCD_IDX_ROW3; i++)  
  {
    lcdString[i] = lcd2ndLine[ucStrIdx++];
    if (ucStrIdx == sizeof(lcd2ndLine)-1)
    {
      ucStrIdx = 0;
    }
  }
}
#endif  // #if 1   // prvSetSecondLine()

// These are the ParTest routines
void vParTestInitialise( void )
{
  /* Start the 'LcdRefresh' routine.  */
  sTaskCreate( vLcdRefresh, "LcdRefresh", portMINIMAL_STACK_SIZE, NULL, LCD_TASK_PRIORITY, NULL );  
}


void vParTestSetLED( unsigned portCHAR ucLED, signed portCHAR cValue )
{
  if( ucLED < partstNUM_TOGL_LEDS )
  {
    if (cValue)
    {
      lcdString[ucLED] = LCD_LED_ON;
    }
    else
    {
      lcdString[ucLED] = LCD_LED_OFF;
    }
  }
  else if( ucLED < partstNUM_TOGL_LEDS + partstNUM_INCR_LEDS )
  {
    if (++lcdString[ucLED] > LCD_LED_HI)
    {
      lcdString[ucLED] = LCD_LED_LO;
    }
  }
}

// Just to make things interesting will periodically call prvSetSecondLine()
// every 2ND_LINE_MODIFY_POLL calls to a toggle LED.
#define LINE_2_MODIFY_POLL  5

void vParTestToggleLED( unsigned portCHAR ucLED )
{
  static unsigned portSHORT pollCnt = LINE_2_MODIFY_POLL;
  
  if( ucLED < partstNUM_TOGL_LEDS )
  {
    if (lcdString[ucLED] == LCD_LED_ON)
    {
      lcdString[ucLED] = LCD_LED_OFF;
    }
    else
    {
      lcdString[ucLED] = LCD_LED_ON;
    }
    if (--pollCnt == 0)
    {
      pollCnt = LINE_2_MODIFY_POLL;
      prvSetSecondLine();       // do something to the 2nd line
    }
  }
  else if( ucLED < partstNUM_TOGL_LEDS + partstNUM_INCR_LEDS )
  {
    if (++lcdString[ucLED] > LCD_LED_HI)
    {
      lcdString[ucLED] = LCD_LED_LO;
    }
  }
  else if( ucLED == partstREAL_LED )
  {
    prvToggleOnBoardLED();
  }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -