📄 bsp.c
字号:
*********************************************************************************************************
*/
void Ser_Init (void)
{
CPU_INT32U pclk_freq;
pclk_freq = BSP_CPU_ClkFreq(); /* Get the UART input clock frequency */
AT91C_BASE_PIOA->PIO_PDR = GPIOA_DRXD | GPIOA_DTXD; /* Set GPIOA pins 9 and 10 as DBGU UART pins */
AT91C_BASE_PIOA->PIO_ASR = GPIOA_DRXD | GPIOA_DTXD; /* Select GPIOA attached peripheral (DBGU) */
AT91C_BASE_DBGU->DBGU_IDR = AT91C_US_RXRDY /* Disable Rx interrupts */
| AT91C_US_TXRDY; /* Disable Tx interrupt */
AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RXEN /* Enable the receiver */
| AT91C_US_TXEN; /* Enable the transmitter */
AT91C_BASE_DBGU->DBGU_MR = AT91C_US_USMODE_NORMAL /* Normal mode selected */
| AT91C_US_PAR_NONE; /* No parity bit selected */
/* Set the DBGU baud rate to 115,200 */
AT91C_BASE_DBGU->DBGU_BRGR = (CPU_INT16U)((pclk_freq) / 115200 / 16);
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_SYS); /* Enable the DBGU peripheral clock */
}
/*
*********************************************************************************************************
* Ser_WrByte()
*
* Description : Writes a single byte to a serial port.
*
* Argument(s) : tx_byte The character to output.
*
* Return(s) : none.
*
* Note(s) : (1) This functino blocks until room is available in the UART for the byte to be sent.
*********************************************************************************************************
*/
void Ser_WrByte (CPU_INT08U tx_byte)
{
while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY) == 0){ /* Wait for room in the transmit register. */
;
}
AT91C_BASE_DBGU->DBGU_THR = tx_byte;
}
/*
*********************************************************************************************************
* Ser_WrStr()
*
* Description : Write a character string to a serial port.
*
* Argument(s) : tx_str A character string.
*
* Return(s) : none.
*********************************************************************************************************
*/
void Ser_WrStr (CPU_CHAR *tx_str)
{
while ((*tx_str) != 0) {
Ser_WrByte(*tx_str++);
}
}
/*
*********************************************************************************************************
* Ser_RdByte()
*
* Description : Read a byte from a serial port and echo byte to port.
*
* Argument(s) : none.
*
* Return(s) : A byte containing the value of the received charcater.
*
* Note(s) : (1) This function blocks until a character appears at the port.
*********************************************************************************************************
*/
CPU_INT08U Ser_RdByte (void)
{
CPU_INT08U rx_byte;
while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY) == 0) { /* Wait for a byte to show up. */
OSTimeDly(2);
}
rx_byte = (CPU_INT08U)(AT91C_BASE_DBGU->DBGU_RHR & 0x00FF); /* Read the character. */
return (rx_byte);
}
/*
*********************************************************************************************************
* Ser_RdStr()
*
* Description : Read a string from a serial port.
*
* Argument(s) : rx_str A pointer to a buffer at which the string can be stored.
* len The size of the string that will be read.
*
* Return(s) : none.
*********************************************************************************************************
*/
void Ser_RdStr (CPU_CHAR *rx_str,
CPU_INT32U len)
{
CPU_CHAR input;
CPU_CHAR input_ix;
input_ix = 0;
rx_str[0] = 0;
while (1)
{
input = Ser_RdByte();
if ((input == '\r') ||
(input == '\n')) {
Ser_Printf("\n");
rx_str[input_ix] = 0;
break;
}
if (input == '\b') {
if (input_ix > 0) {
Ser_Printf("\b \b");
input_ix--;
rx_str[input_ix] = 0;
}
}
if (Str_IsPrint(input)) {
Ser_Printf("%c", input);
rx_str[input_ix] = input;
input_ix++;
if (input_ix >= len) {
input_ix = len;
}
}
}
}
/*
*********************************************************************************************************
* Ser_Printf()
*
* Description : Formatted outout to the serial port.
*
* Argument(s) : format Format string follwing the C format convention.
*
* Return(s) : none.
*********************************************************************************************************
*/
void Ser_Printf (CPU_CHAR *format, ...)
{
static CPU_CHAR buffer[80 + 1];
va_list vArgs;
va_start(vArgs, format);
vsprintf((char *)buffer, (char const *)format, vArgs);
va_end(vArgs);
Ser_WrStr((CPU_CHAR*) buffer);
}
/*
*********************************************************************************************************
*********************************************************************************************************
** LOCAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* BSP_DummyISR_Handler()
*
* Description : Handle an invalid IRQ.
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************
*/
static void BSP_DummyISR_Handler (void)
{
CPU_INT32U irq_id;
irq_id = AT91C_BASE_AIC->AIC_ISR & 0x1F; /* Retrieve the ID of the interrupting source */
AT91C_BASE_AIC->AIC_ICCR = (1 << irq_id); /* Clear the current interrupt */
AT91C_BASE_AIC->AIC_IVR = 0; /* Debug variant of vector read (protect mode is used) */
}
/*
*********************************************************************************************************
* PLL_Init()
*
* Description : Iinitializes the PLL & sets the proper number of flash wait states.
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************
*/
static void PLL_Init (void)
{
/* ----------------- SET FLASH WAIT STATES ---------------- */
AT91C_BASE_MC->MC0_FMR |= AT91C_MC_FWS_2FWS; /* Since we are running > 30mhz out of flash */
AT91C_BASE_MC->MC1_FMR |= AT91C_MC_FWS_2FWS; /* Since we are running > 30mhz out of flash */
/* -------------------- SETUP CPU CLOCK ------------------- */
/* Enable the Main Oscillator */
AT91C_BASE_PMC->PMC_MOR = AT91C_CKGR_OSCOUNT | AT91C_CKGR_MOSCEN;
/* Wait for the Main Oscillator to start up */
while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCS) == 0) {
;
}
AT91C_BASE_PMC->PMC_PLLR = 0x107C3F0C; /* Enable and set PLL */
/* [07:00] Divider = 12 */
/* [13:08] PLL Count = 63 slow clocks */
/* [26:16] Multiplier = (124 + 1) = 125 */
/* [29:28] USB Divider = 2 */
/* ===> MCLK = 18432000 * 125 / 12 = 192 MHz */
/* ===> USBCLK = MCLK / 4 = 48 MHz */
/* Wait for the PLL to start up */
while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_LOCK) == 0) {
;
}
while((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) == 0) {
;
}
/* Main Clock will be equal to 1/4 of the PLL Clock */
AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_PRES_CLK_4;
while((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) == 0) {
;
}
/* Specify the PLL Clock as the source of the Main Clock */
AT91C_BASE_PMC->PMC_MCKR |= AT91C_PMC_CSS_PLL_CLK;
while((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) == 0) {
;
}
}
/*
*********************************************************************************************************
* BSP_IntCtrlInit()
*
* Description : (1) Initialize interrupts:
* (a) Initialize exception vector table, performing a remap (if necessary).
* (b) Initialize vectors in Advance Interrupt Controller (AIC) to dummy ISR.
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************
*/
static void BSP_IntCtrlInit (void)
{
CPU_INT16U i;
BSP_RAM_REMAP_TEST_BYTE = 0xAA; /* Write a byte to RAM */
#ifndef RAM_REMAPPED
if (BSP_RAM_REMAP_TEST_BYTE == 0xAA) { /* Check if the write to RAM worked v */
AT91C_BASE_MC->MC_RCR = 1; /* If so, toggle REMAP register */
}
#else /*--------------BEGIN CODE RUNNING OUT OF RAM---------------*/
OS_CPU_InitExceptVect();
#endif /*---------------END CODE RUNNING OUT OF RAM----------------*/
AT91C_BASE_AIC->AIC_EOICR = 0x00000000; /* End-of-interrupt command */
for (i = 0; i < 32; i++) { /* Disable all ISRs */
AT91C_BASE_AIC->AIC_SVR[i] = (CPU_INT32U)BSP_DummyISR_Handler;
AT91C_BASE_AIC->AIC_SMR[i] = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -