📄 main.c
字号:
/****************************************************************************
* Appli.c
* Implementation of a simple led blink program with Ucos on a EB01
* Task1 : Red Led blink
* Task2 : Amber Led blink
* Task3 : Green Led blink
*
* - 1.0 21/08/00 EL : Creation
***************************************************************************/
extern void at91_irq_handler(void);
extern void Timer0INT(void);
void _Link(void);//memory usage
#ifndef AT91_DEBUG_NONE
#include <stdio.h>
#endif
#include <string.h>
#include "periph/stdc/std_c.h"
#include "drivers/terminal/terminal.h"
#include "periph/usart/usart.h"
#include "ucos-II200/includes.h"
/* allocate memory for tasks' stacks */
#define STACKSIZE 64
#ifdef EB40162
#define AT91R40008
#endif
//* Include files for EB40800
#ifdef AT91M40800
#include "EB40800.h"
#include "parts/m40800/reg_m40800.h"
#include "parts/m40800/lib_m40800.h"
#define BAUDS38400 (32768000 / (16 * 38400)) //* CD = 53
#endif
//* Include files for EB40807
#ifdef AT91R40807
#include "EB40807.h"
#include "parts/r40807/reg_r40807.h"
#include "parts/r40807/lib_r40807.h"
#define BAUDS38400 (32768000 / (16 * 38400)) //* CD = 53
#endif
//* Include files for EB40008 or EB40162
#ifdef AT91R40008
#include "EB40008.h"
#include "parts/r40008/reg_r40008.h"
#include "parts/r40008/lib_r40008.h"
//#define BAUDS38400 (66000000 / (16 * 38400)) //* CD = 107
#define BAUDS38400 (66000000 / (16 * 115200)) //* CD = 107
#endif
//* Include files for EB42
#ifdef AT91M42800
#include "eb42.h"
#include "parts/m42800/reg_m42800.h"
#include "parts/m42800/lib_m42800.h"
#define BAUDS38400 (32768000 / (16 * 38400)) //* CD = 53
#endif
//* Include files for EB55
#ifdef AT91M55800
#include "eb55.h"
//#include "parts/m55800/reg_m55800.h"
#include "parts/m55800/lib_m55800.h"
#define BAUDS38400 (32000000 / (16 * 38400)) //* CD = 52
#endif
//* Include files for EB63
#ifdef AT91M63200
#include "eb63.h"
#include "parts/m63200/reg_m63200.h"
#include "parts/m63200/lib_m63200.h"
#define BAUDS38400 (25000000 / (16 * 38400)) //* CD = 41
#endif
/* Global Variable */
unsigned int Stack1[STACKSIZE];
unsigned int Stack2[STACKSIZE];
unsigned int Stack3[STACKSIZE];
unsigned int Stack4[STACKSIZE];
unsigned int Stack5[STACKSIZE];
unsigned int MainStack[STACKSIZE];
//* Terminal declaration
TerminalDataDesc terminal_data_1;
TerminalDesc terminal_1;
char str[TERMINAL_SIZE_BUFFER];
char *pt_str = str;
char CR[1] = {0x0D};
char LF[1] = {0x0A};
//*----------------------------------------------------------------------------
//* Function Name : Task1
//* Object : Red Led blink (50ms)
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
void Task1(void *i)
{
while(1)
{
/* set LED */
at91_pio_write (&PIO_DESC, LED1|LED3, LED_ON ) ;
/* wait a short while */
OSTimeDly(100);
/* clear LED */
at91_pio_write (&PIO_DESC, LED1|LED3, LED_OFF ) ;
/* wait a short while */
OSTimeDly(120);
}
}
//*----------------------------------------------------------------------------
//* Function Name : Task2
//* Object : Amber Led blink (80ms)
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
void Task2(void *i)
{
while(1)
{
/* set LED */
at91_pio_write (&PIO_DESC, LED2|LED4, LED_ON ) ;
/* wait a short while */
OSTimeDly(80);
at91_pio_write (&PIO_DESC, LED2|LED4, LED_OFF ) ;
/* wait a short while */
OSTimeDly(80);
}
}
//*----------------------------------------------------------------------------
//* Function Name : Task3
//* Object : Green Led blink (200ms)
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
void Task3(void *i)
{
while(1)
{
/* set LED */
at91_pio_write (&PIO_DESC, LED5|LED6, LED_ON ) ;
/* wait a short while */
OSTimeDly(120);
/* clear LED */
at91_pio_write (&PIO_DESC, LED5|LED6, LED_OFF ) ;
/* wait a short while */
OSTimeDly(120);
}
}
//*----------------------------------------------------------------------------
//* Function Name : Task4
//* Object : communicate to PC using UART0
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
void Task4( void *i)
{
char m,max=0;
char str_send[] = "String Receive: ";
char str_send2[] = "String Input : ";
char str_error[] = "TERMINAL OVERFLOW : 256 character max !";
//* Terminal handler
while(1)
{
if (at91_terminal_read(&terminal_1,pt_str) != 0)
{
if (*pt_str == 0x0D)
{
at91_terminal_write(&terminal_1, &CR[0]);
at91_terminal_write(&terminal_1, &LF[0]);
//* Send str_send
for(m=0; m<strlen(str_send); m++)
at91_terminal_write(&terminal_1,&str_send[m]);
//* Send received string
pt_str = str;m=0;
while (*pt_str != 0x0D)
{
at91_terminal_write(&terminal_1, pt_str);str[m]=0;
pt_str++;m++;
}
}
if((max>0xFE)||terminal_data_1.error)
{
//* Send error string
for(m=0; m<40; m++)
at91_terminal_write(&terminal_1,&str_error[m]);
//* reset error flag
terminal_data_1.error = 0;
}
if((max>0xFE)||(*pt_str == 0x0D))
{
at91_terminal_write(&terminal_1,&CR[0]);
at91_terminal_write(&terminal_1,&LF[0]);
for(m=0; m<strlen(str_send2); m++)
at91_terminal_write(&terminal_1,&str_send2[m]);
pt_str=str;max=0;
//* enable TXRDY interrupt
terminal_1.usart_desc->usart_base->US_IER = terminal_1.usart_desc->usart_base->US_IMR | 0x02;
}
else
{
pt_str++;
max++;
}
}
/* wait a short while */
OSTimeDly(2);
}
}
//*----------------------------------------------------------------------------
//* Function Name : Task5
//* Object : a blank task for user
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
void Task5(void *i)
{
int j,k,m;
while(1)
{
for(j = 1000; j!=0; j--);
OSTimeDly(280);
}
}
//*----------------------------------------------------------------------------
//* Function Name : TaskStart
//* Object : First created task with lower priority
//* : Enable timer interrupt
//* : Creation of the other tasks
//* Input Parameters : Task creation arguments
//* Output Parameters : none
//*----------------------------------------------------------------------------
void TaskStart (void *i)
{
char Id1 = '1';
char Id2 = '2';
char Id3 = '3';
char Id4 = '4';
char Id5 = '5';
TC0_IER = TC_CPCS; // enable timer counter interrupt
/*
* create the tasks in uC/OS and assign decreasing
* priority to them
*/
OSTaskCreate(Task1, &Id1, &Stack1[STACKSIZE - 1], 2);
OSTaskCreate(Task2, &Id2, &Stack2[STACKSIZE - 1], 3);
OSTaskCreate(Task3, &Id3, &Stack3[STACKSIZE - 1], 4);
OSTaskCreate(Task4, &Id4, &Stack4[STACKSIZE - 1], 5);
OSTaskCreate(Task5, &Id5, &Stack5[STACKSIZE - 1], 6);
// Delete current task
OSTaskDel(OS_PRIO_SELF);
}
//*----------------------------------------------------------------------------
//* Function Name : SystemInitialization
//* Object : Setup timer interrupt every 10ms
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
void SystemInitialization (void)
{
u_int timer_value[3] = {0,0,40000}; // 10ms
//* define led at PIO output
at91_pio_open ( &PIO_DESC, LED_MASK, PIO_OUTPUT );
//* define switch at PIO input
at91_pio_open ( &PIO_DESC, SW_MASK, PIO_INPUT );
//* Timer initialization
at91_tc_open(&TC0_DESC, TC_WAVE|TC_CPCTRG|TC_CLKS_MCK8,0,0);
at91_tc_write(&TC0_DESC, timer_value);
at91_tc_trig_cmd(&TC0_DESC, TC_TRIG_CHANNEL);
at91_irq_open(TC0_DESC.periph_id, 7, AIC_SRCTYPE_INT_EDGE_TRIGGERED, OSTickISR); //Timer0INT
}
//*----------------------------------------------------------------------------
//* Function Name : main
//* Object : Main function
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
int main( void )
{
char Id0 = '0';
///////////////////////////////////////////////////////////
//*----------------------------------------------------------------------------
//*
//* Object : AT91 - USART Controller - transfert by polling
//* Input Parameters : None
//* Output Parameters : None
//* Functions called : None
//*----------------------------------------------------------------------------
char m;int i;
#ifdef AT91_DEBUG_NONE
char str_test[] = "AT91 TERMINAL TEST(Flash) : ";
#endif
#ifndef AT91_DEBUG_NONE
char str_test[] = "AT91 TERMINAL TEST(RAM) : ";
#endif
at91_pio_write (&PIO_DESC, LED1, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED2, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED3, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED4, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED5, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED6, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED7, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED8, LED_OFF ) ;
//* Terminal initialisation
terminal_1.usart_desc = &USART0_DESC;
terminal_1.term_data = &terminal_data_1;
terminal_1.baud_rate = (u_int) BAUDS38400;
terminal_1.format = (u_int) US_ASYNC_MODE;
terminal_1.terminal_asm_handler = at91_irq_handler;
//* Open terminal
at91_terminal_open(&terminal_1);
at91_terminal_write(&terminal_1,&CR[0]);
at91_terminal_write(&terminal_1,&LF[0]);
//* Transmit str_test
for(m=0; m<strlen(str_test); m++)
at91_terminal_write(&terminal_1,&str_test[m]);
//* USART0 IRQ initialisation
at91_irq_open(USART0_DESC.periph_id,7,0x40,at91_irq_handler);
//* Enable RXRDY et TXRDY interrupt
terminal_1.usart_desc->usart_base->US_IER = 0x03;
///////////////////////////////////////////////////////////
/* Hardware and software module related initializations */
SystemInitialization();
/* needed by uC/OS */
OSInit();
OSTimeSet(0);
/* only for Timer using test.../* /
__asm("swi 0\n\r");
TC0_IER = TC_CPCS; // enable timer counter interrupt
while(1)
{
for(i=80000;i>0;i--)
at91_pio_write (&PIO_DESC, LED1, LED_OFF ) ;
for(i=80000;i>0;i--)
at91_pio_write (&PIO_DESC, LED1, LED_ON ) ;
}
/* only for Timer using test...*/
/* create the start task */
OSTaskCreate(TaskStart, &Id0, &MainStack[STACKSIZE - 1], 6);
at91_pio_write (&PIO_DESC, LED1, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED2, LED_OFF ) ;
at91_pio_write (&PIO_DESC, LED3, LED_OFF ) ;
//_Link();//memory usage
/* start the operating system */
OSStart();
return(0);
//* End
}
#if 0
/////////////////////////////////////////////////////////////////////////
extern char Image_RO_Limit[];
extern char Image_RO_Base[];
extern char Image_RW_Limit[];
extern char Image_RW_Base[];
extern char Image_ZI_Limit[];
extern char Image_ZI_Base[];
void * MSG[][2]=
{
"Image_RO_Base = ",&Image_RO_Base,
&CR,&CR,
"Image_RO_Limit = ",&Image_RO_Limit,
&CR,&CR,
"Image_RW_Base = ",&Image_RW_Base,
&CR,&CR,
"Image_ZI_Base = ",&Image_ZI_Base,
&CR,&CR,
"Image_ZI_Limit = ",&Image_ZI_Limit,
0,0
};
void _Link(void)
{
char CR[1] = {0x0D};
char LF[1] = {0x0A};
int i=0;
while(1)
{
at91_terminal_write(&terminal_1,&MSG[i][0]);
at91_terminal_write(&terminal_1,&MSG[i][1]);
if((int)(MSG[i][0])==0)
break;
i++;
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -