In the tivaware Bootloader library there is a function named
int UARTAutoBaud(uint32_t *pui32Ratio)
where pui32Ratio is the ratio of the processor’s crystal frequency to the baud rate being used by the
UART port for communications.
Description:
This function attempts to synchronize to the updater program that is trying to communicate with
the boot loader. The UART port is monitored for edges using interrupts. Once enough edges
are detected, the boot loader determines the ratio of baud rate and crystal frequency needed
to program the UART.
Returns:
Returns a value of 0 to indicate that this call successfully synchronized with the other device
communicating over the UART, and a negative value to indicate that this function did not successfully
synchronize with the other UART device.
The Thing is I can't figure out how to use this function. the function definition is:
int
UARTAutoBaud(uint32_t *pui32Ratio)
{
int32_t i32Pulse, i32ValidPulses, i32Temp, i32Total;
volatile int32_t i32Delay;
//
// Configure and enable SysTick. Set the reload value to the maximum;
// there are only 24 bits in the register but loading 32 bits of ones is
// more efficient.
//
HWREG(NVIC_ST_RELOAD) = 0xffffffff;
HWREG(NVIC_ST_CTRL) = NVIC_ST_CTRL_CLK_SRC | NVIC_ST_CTRL_ENABLE;
//
// Reset the counters that control the pulse detection.
//
i32ValidPulses = 0;
i32Total = 0;
g_ui32TickIndex = 0;
//
// Set the pad(s) for standard push-pull operation.
//
HWREG(GPIO_PORTA_BASE + GPIO_O_PUR) |= UART_RX;
HWREG(GPIO_PORTA_BASE + GPIO_O_DEN) |= UART_RX;
//
// Interrupt on both edges.
//
HWREG(GPIO_PORTA_BASE + GPIO_O_IBE) = UART_RX;
//
// Clear out all of the gpio interrupts in this register.
//
HWREG(GPIO_PORTA_BASE + GPIO_O_ICR) = UART_RX;
//
// Enable the GPIO pin corresponding to the UART RX pin.
//
HWREG(GPIO_PORTA_BASE + GPIO_O_IM) = UART_RX;
//
// Enable GPIOA Interrupt.
//
HWREG(NVIC_EN0) = 1;
//
// Wait for MIN_EDGE_COUNT to pass to collect enough edges.
//
while(g_ui32TickIndex < MIN_EDGE_COUNT)
{
}
//
// Disable GPIOA Interrupt.
//
HWREG(NVIC_DIS0) = 1;
//
// Calculate the pulse widths from the array of tick times.
//
for(i32Pulse = 0; i32Pulse < (MIN_EDGE_COUNT - 1); i32Pulse++)
{
i32Temp = (((int32_t)g_pui32DataBuffer[i32Pulse] -
(int32_t)g_pui32DataBuffer[i32Pulse + 1]) & 0x00ffffff);
g_pui32DataBuffer[i32Pulse] = i32Temp;
}
//
// This loops handles checking for consecutive pulses that have pulse
// widths that are within an acceptable margin.
//
for(i32Pulse = 0; i32Pulse < (MIN_EDGE_COUNT - 1); i32Pulse++)
{
//
// Calculate the absolute difference between two consecutive pulses.
//
i32Temp = (int32_t)g_pui32DataBuffer[i32Pulse];
i32Temp -= (int32_t)g_pui32DataBuffer[i32Pulse + 1];
if(i32Temp < 0)
{
i32Temp *= -1;
}
//
// This pulse detection code uses the following algorithm:
// If the following is true then we have consecutive acceptable pulses
// abs(Pulse[n] - Pulse[n + 1]) < Pulse[n + 1] / PULSE_DETECTION_MULT
// or
// PULSE_DETECTION_MULT * abs(Pulse[n] - Pulse[n + 1]) < Pulse[n + 1]
//
if((i32Temp * PULSE_DETECTION_MULT) <
(int32_t)g_pui32DataBuffer[i32Pulse + 1])
{
i32Total += (int32_t)g_pui32DataBuffer[i32Pulse];
i32ValidPulses++;
}
else
{
i32ValidPulses = 0;
i32Total = 0;
}
//
// Once we have 7 pulses calculate the ratio needed to program the
// UART.
//
if(i32ValidPulses == 7)
{
//
// Add in the last pulse and calculate the ratio.
//
i32Total += (int32_t)g_pui32DataBuffer[i32Pulse];
*pui32Ratio = i32Total >> 1;
//
// Wait for at least 2 UART clocks since we only wait for 18 of 20
// that are coming from the host. If we don't wait, we can turn
// on the UART while the last two pulses come down.
//
for(i32Delay = i32Total; i32Delay; i32Delay--)
{
}
//
// Indicate a successful auto baud operation.
//
return(0);
}
}
//
// Automatic baud rate detection failed.
//
return(-1);
}
what value do I pass for pui32ratio?
please guide me through this.
thanks.