📄 main.c
字号:
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support - ROUSSET -
* ----------------------------------------------------------------------------
* Copyright (c) 2006, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaiimer below.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the disclaimer below in the documentation and/or
* other materials provided with the distribution.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*-----------------------------------------------------------------------------
* File Name : main.c
* Object : main file
* Creation : FDy 10-Nov-2006
*-----------------------------------------------------------------------------
*/
/* Include Standard files */
#include "project.h"
/* Global variables */
#define LED_A AT91B_POWERLED
#define LED_B AT91B_LED1
#define BUTTON_A AT91B_BP3
#define BUTTON_B AT91B_BP4
#define MCK_FREQ 99328000
#define SLOW_CLOCK 32768
#define DEBOUNCE_TIME 500
/* The following global variables, control the LED blinking */
unsigned long led_a_active = 1;
unsigned long led_b_active = 1;
volatile unsigned long jiffies = 0;
static void configure_pit(void);
static void configure_buttons(void);
static void configure_leds(void);
static void configure_tc(void);
static void configure_dbgu(void);
/*-----------------------------------------------------------------------------
* Function Name : pabt_handler
* Object : Prefetch Abort Handler
*-----------------------------------------------------------------------------*/
void pabt_handler(void)
{
dbgu_print_ascii("-F- Prefetch abort\n\r");
}
/*-----------------------------------------------------------------------------
* Function Name : dabt_handler
* Object : Data Abort Handler
*-----------------------------------------------------------------------------*/
void dabt_handler(void)
{
dbgu_print_ascii("-F- Data abort\n\r");
}
/*-----------------------------------------------------------------------------
* Function Name : pitc_handler
* Object : Handler for PITC interrupt
*-----------------------------------------------------------------------------*/
void pitc_handler(void)
{
unsigned long pivr = 0;
unsigned long pisr = 0;
/* Read the PISR */
pisr = AT91C_BASE_PITC->PITC_PISR & AT91C_PITC_PITS;
if (pisr != 0) {
/* Read the PIVR. It acknowledges the IT */
pivr = AT91C_BASE_PITC->PITC_PIVR;
/* Add to jiffies PICNT: the number of occurrences of periodic intervals */
/* since the last read of PIT_PIVR */
jiffies += (pivr >> 20);
}
}
/*-----------------------------------------------------------------------------
* Function Name : configure_pit
* Object : Configure priodic interval timer
*-----------------------------------------------------------------------------*/
static void configure_pit(void)
{
volatile unsigned long pimr = 0;
/* Configure a resolution of 1 ms */
AT91C_BASE_PITC->PITC_PIMR = MCK_FREQ / (16 * 1000) - 1;
/* Enable interrupts */
/* Disable the interrupt on the interrupt controller */
AT91C_BASE_AIC->AIC_IDCR = (1 << AT91C_ID_SYS);
/* Save the interrupt handler routine pointer and the interrupt priority */
AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (unsigned long) pitc_handler;
/* Store the Source Mode Register */
AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | AT91C_AIC_PRIOR_LOWEST;
/* Clear the interrupt on the interrupt controller */
AT91C_BASE_AIC->AIC_ICCR = (1 << AT91C_ID_SYS);
/* Enable the interrupt on the interrupt controller */
AT91C_BASE_AIC->AIC_IECR = (1 << AT91C_ID_SYS);
/* Enable the interrupt on the pit */
pimr = AT91C_BASE_PITC->PITC_PIMR;
AT91C_BASE_PITC->PITC_PIMR = pimr | AT91C_PITC_PITIEN;
/* Enable the pit */
pimr = AT91C_BASE_PITC->PITC_PIMR;
AT91C_BASE_PITC->PITC_PIMR = pimr | AT91C_PITC_PITEN;
}
/*-----------------------------------------------------------------------------
* Function Name : button_handler
* Object : Button Interrupt Service Routine (PIO A)
*-----------------------------------------------------------------------------*/
void button_handler(void)
{
/* Read the Interrupt Status register (It acknowledge the IT) */
volatile unsigned long pio_isr = AT91C_BASE_PIOA->PIO_ISR;
volatile unsigned long pio_pdsr = AT91C_BASE_PIOA->PIO_PDSR;
static unsigned long button_a_jiffies = 0;
static unsigned long button_b_jiffies = 0;
/* If the BUTTON_A has been pushed or released */
if (pio_isr & BUTTON_A) {
/* Enable/disable the LED A blinking when BUTTON_A is released */
if (pio_pdsr & BUTTON_A) {
if (jiffies - button_a_jiffies > DEBOUNCE_TIME) {
button_a_jiffies = jiffies;
led_a_active = !led_a_active;
}
}
}
/* If the BUTTON_B has been pushed or released */
else if (pio_isr & BUTTON_B) {
/* Enable/disable the LED B blinking when BUTTON_B is pushed */
if (!(pio_pdsr & BUTTON_B)) {
if (jiffies - button_b_jiffies > DEBOUNCE_TIME) {
button_b_jiffies = jiffies;
if (led_b_active) {
/* Disable the clock */
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
} else {
/* Clock is started */
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN;
/* Counter is reset and the clock is started */
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
}
led_b_active = !led_b_active;
}
}
}
/* The else should not be executed since no other PIO are enabled */
else { }
}
/*-----------------------------------------------------------------------------
* Function Name : configure_buttons
* Object : Configure pios to capture buttons state
*-----------------------------------------------------------------------------*/
static void configure_buttons(void)
{
/* Enable the periph clock for the PIO controller */
/* This is mandatory when PIO are configured as input */
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_PIOA);
/* Set the PIO line in input */
AT91C_BASE_PIOA->PIO_ODR = (BUTTON_A | BUTTON_B);
/* Set the PIO controller in PIO mode instead of peripheral mode */
AT91C_BASE_PIOA->PIO_PER = (BUTTON_A | BUTTON_B);
/* Disable the interrupt on the interrupt controller */
AT91C_BASE_AIC->AIC_IDCR = (1 << AT91C_ID_PIOA);
/* Save the interrupt handler routine pointer and the interrupt priority */
AT91C_BASE_AIC->AIC_SVR[AT91C_ID_PIOA] = (unsigned long) button_handler;
/* Store the Source Mode Register */
AT91C_BASE_AIC->AIC_SMR[AT91C_ID_PIOA] = AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | AT91C_AIC_PRIOR_LOWEST;
/* Clear the interrupt on the interrupt controller */
AT91C_BASE_AIC->AIC_ICCR = (1 << AT91C_ID_PIOA);
/* Enable button interrupts generation through the PIO controller */
AT91C_BASE_PIOA->PIO_IER = (BUTTON_A | BUTTON_B);
/* Enable PIO interrupt in the interrupt controller */
AT91C_BASE_AIC->AIC_IECR = (1 << AT91C_ID_PIOA);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -