📄 main.c
字号:
/*-----------------------------------------------------------------------------
* Function Name : configure_leds
* Object : Configure pios to control led states
*-----------------------------------------------------------------------------*/
static void configure_leds(void)
{
/* Configure the pin in output */
AT91C_BASE_PIOA->PIO_OER = (LED_A | LED_B);
/* Set the PIO controller in PIO mode instead of peripheral mode */
AT91C_BASE_PIOA->PIO_PER = (LED_A | LED_B);
/* Disable pull-up */
AT91C_BASE_PIOA->PIO_PPUDR = (LED_A | LED_B);
/* Set the default state for the led */
if (led_a_active)
AT91C_BASE_PIOA->PIO_SODR = LED_A;
else
AT91C_BASE_PIOA->PIO_CODR = LED_A;
if (led_b_active)
AT91C_BASE_PIOA->PIO_SODR = LED_B;
else
AT91C_BASE_PIOA->PIO_CODR = LED_B;
}
/*-----------------------------------------------------------------------------
* Function Name : timer_handler
* Object : Handler for TC interrupt
*-----------------------------------------------------------------------------*/
void timer_handler(void)
{
volatile unsigned long dummy;
/* Clear status bit */
dummy = AT91C_BASE_TC0->TC_SR;
/* Toggle LED state */
if (AT91C_BASE_PIOA->PIO_ODSR & LED_B) {
AT91C_BASE_PIOA->PIO_CODR = LED_B;
} else {
AT91C_BASE_PIOA->PIO_SODR = LED_B;
}
}
/*-----------------------------------------------------------------------------
* Function Name : configure_tc
* Object : Configure TC
*-----------------------------------------------------------------------------*/
static void configure_tc(void)
{
volatile unsigned long dummy;
/* Enable periph clock for the PIO controller */
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC0);
/* Enable the periph */
/* Disable the clock and the interrupts */
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
AT91C_BASE_TC0->TC_IDR = 0xFFFFFFFF;
/* Clear status bit */
dummy = AT91C_BASE_TC0->TC_SR;
/* Set the Mode of the Timer Counter */
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV5_CLOCK | AT91C_TC_CPCTRG;
AT91C_BASE_TC0->TC_RC = AT91B_SLOW_CLOCK >> 2; /* SLOW_CLOCK divided by 4 */
/* Enable interrupts */
/* Disable the interrupt on the interrupt controller */
AT91C_BASE_AIC->AIC_IDCR = (1 << AT91C_ID_TC0);
/* Save the interrupt handler routine pointer and the interrupt priority */
AT91C_BASE_AIC->AIC_SVR[AT91C_ID_TC0] = (unsigned long) timer_handler;
/* Store the Source Mode Register */
AT91C_BASE_AIC->AIC_SMR[AT91C_ID_TC0] = 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_TC0);
AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS;
/* Enable the interrupt on the interrupt controller */
AT91C_BASE_AIC->AIC_IECR = (1 << AT91C_ID_TC0);
/* Enable the LED B timer */
if (led_b_active) {
/* 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;
}
}
/*----------------------------------------------------------------------------
* Function Name : dbgu_print_ascii
* Object : This function is used to send a string through the
* DBGU channel (Very low level debugging)
*----------------------------------------------------------------------------*/
void dbgu_print_ascii (const char *buffer)
{
while (*buffer != '\0') {
while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY));
AT91C_BASE_DBGU->DBGU_THR = (*buffer++ & 0x1FF);
}
}
/*----------------------------------------------------------------------------
* Function Name : dbgu_print_hex8
* Object : This function is used to print a 32-bit value in hexa
*----------------------------------------------------------------------------*/
void dbgu_print_hex8 (unsigned long value)
{
char c = 0;
char shift = sizeof(unsigned long) * 8;
dbgu_print_ascii("0x");
do {
shift -= 4;
while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY));
c = ((value >> shift) & 0xF);
if (c > 9)
AT91C_BASE_DBGU->DBGU_THR = (('A' + (c - 10)) & 0x1FF);
else
AT91C_BASE_DBGU->DBGU_THR = (('0' + c) & 0x1FF);
} while (shift != 0);
}
/*-----------------------------------------------------------------------------
* Function Name : configure_dbgu
* Object : Configure DBGU
*-----------------------------------------------------------------------------*/
static void configure_dbgu (void)
{
/* Reset and disable receiver */
AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RSTRX | AT91C_US_RSTTX;
/* Disable interrupts */
AT91C_BASE_DBGU->DBGU_IDR = 0xFFFFFFFF;
/* Configure PIOs for DBGU */
AT91C_BASE_PIOA->PIO_ASR = AT91C_PA9_DRXD | AT91C_PA10_DTXD;
AT91C_BASE_PIOA->PIO_BSR = 0;
AT91C_BASE_PIOA->PIO_PDR = AT91C_PA9_DRXD | AT91C_PA10_DTXD;
/* === Configure serial link === */
/* Define the baud rate divisor register [BRGR = MCK / (115200 * 16)] */
AT91C_BASE_DBGU->DBGU_BRGR = 54;
/* Define the USART mode */
AT91C_BASE_DBGU->DBGU_MR = AT91C_US_PAR_NONE | AT91C_US_CHMODE_NORMAL;
/* Disable the RX and TX PDC transfer requests */
AT91C_BASE_DBGU->DBGU_PTCR = AT91C_PDC_RXTDIS;
AT91C_BASE_DBGU->DBGU_PTCR = AT91C_PDC_TXTDIS;
/* Enable transmitter */
AT91C_BASE_DBGU->DBGU_CR = AT91C_US_TXEN;
}
/*-----------------------------------------------------------------------------
* Function Name : wait
* Object : Tempo using jiffies (updated by PITC)
*-----------------------------------------------------------------------------*/
void wait (unsigned long ms)
{
volatile unsigned long current_time = jiffies;
unsigned long prev_jiffies;
unsigned long target_time = current_time + ms;
// Handle the counter overflow
if (target_time < current_time) {
prev_jiffies = current_time;
while (prev_jiffies <= jiffies)
prev_jiffies = jiffies;
}
// Loop until the target time is reached
while (jiffies < target_time);
}
/*-----------------------------------------------------------------------------
* Function Name : Main
* Object : Software entry point
*-----------------------------------------------------------------------------*/
int main()
{
/* ==== PITC configuration ==== */
configure_pit();
/* ==== TIMER configuration ==== */
configure_tc();
/* === BUTTON configuration === */
configure_buttons();
/* === LED configuration === */
configure_leds();
/* ==== DBGU configuration ==== */
configure_dbgu();
dbgu_print_ascii("AT91SAM9261 Getting Started program launched ...\n\r");
dbgu_print_ascii("AT91SAM9261 chip ID : ");
dbgu_print_hex8(AT91C_BASE_DBGU->DBGU_CIDR);
dbgu_print_ascii("\n\r");
while (1) {
if (led_a_active) {
/* Switch on the led */
wait(500);
AT91C_BASE_PIOA->PIO_CODR = LED_A;
/* Switch off the led */
wait(500);
AT91C_BASE_PIOA->PIO_SODR = LED_A;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -