📄 term.c
字号:
//*----------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name : term.c
//* Object : AT91 - USART Controller - Transfert by interrupt
//*
//* 1.0 10/04/00 EL : Terminal test
//* 1.1 10/01/02 PFi : Conditionnal compilations flags added for all
//* : Evaluation Boards.
//*----------------------------------------------------------------------------
/*
Configure the Terminal to send and receive under interrupt on the USART 0
The byte format is : start + 8 data (without parity) + 1 stop
The baud rate is counter is 53 : 38400 bauds with MCKI = 32.768 MHz
When a character is received, it is echoed in the interrupt handler and stored
in a buffer.
When the CR character is received, it is echoed and all buffer is transmitted.
The size of the buffer is limited at TERMINAL_SIZE_BUFFER = 256.
When more than 256 characters are received, an error message is displayed when
the character CR is received.
*/
#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 files for EB40
#ifdef AT91R40807
#include "targets/eb40/eb40.h"
#include "parts/r40807/reg_r40807.h"
#include "parts/r40807/lib_r40807.h"
#define BAUDS38400 (32768000 / (16 * 38400)) //* CD = 53
#endif
//* Include files for EB40A
#ifdef AT91R40008
#include "targets/eb40a/eb40a.h"
#include "parts/r40008/reg_r40008.h"
#include "parts/r40008/lib_r40008.h"
#define BAUDS38400 (66000000 / (16 * 38400)) //* CD = 107
#endif
//* Include files for EB42
#ifdef AT91M42800
#include "targets/eb42/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 "targets/eb55/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 "targets/eb63/eb63.h"
#include "parts/m63200/reg_m63200.h"
#include "parts/m63200/lib_m63200.h"
#define BAUDS38400 (25000000 / (16 * 38400)) //* CD = 41
#endif
extern void at91_irq_handler(void);
//* Terminal declaration
TerminalDataDesc terminal_data_1;
TerminalDesc terminal_1;
char str_test[25] = "AT91 TERMINAL TEST : ";
char str_send[10] = "AT91 : ";
char str_error[50] = "TERMINAL OVERFLOW : 256 character max !";
char str[TERMINAL_SIZE_BUFFER];
char CR[1] = {0x0D};
//*----------------------------------------------------------------------------
//* Function Name : main
//* Object : AT91 - USART Controller - transfert by polling
//* Input Parameters : None
//* Output Parameters : None
//* Functions called : None
//*----------------------------------------------------------------------------
int main ( void )
//* Begin
{
int i;
char *pt_str = str;
//* 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);
//* Transmit str_test
for(i=0; i<strlen(str_test); i++)
at91_terminal_write(&terminal_1,&str_test[i]);
//* 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;
//* Terminal handler
while(1)
{
if (at91_terminal_read(&terminal_1,pt_str) != 0)
{
if (*pt_str == 0x0D)
{
//* Send str_send
for(i=0; i<strlen(str_send); i++)
at91_terminal_write(&terminal_1,&str_send[i]);
//* Send received string
pt_str = str;
while ((*pt_str != 0x0D) && (terminal_data_1.error ==0))
{
at91_terminal_write(&terminal_1, pt_str);
pt_str++;
}
// if overflow error
if (terminal_data_1.error != 0)
{
//* Send error string
for(i=0; i<40; i++)
at91_terminal_write(&terminal_1,&str_error[i]);
//* reset error flag
terminal_data_1.error = 0;
}
at91_terminal_write(&terminal_1, &CR[0]);
pt_str = str;
//* enable TXRDY interrupt
terminal_1.usart_desc->usart_base->US_IER = terminal_1.usart_desc->usart_base->US_IMR | 0x02;
}
else
pt_str++;
}
}
//* Close terminal
at91_terminal_close(&terminal_1);
//* End
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -