📄 main.c
字号:
"Demo",
"One",
"www.FreeRTOS.org",
""
};
/* Configure the LCD. */
uxIndex = 0;
while( uxIndex < sizeof( ucCFGData ) )
{
prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
uxIndex++;
vTaskDelay( mainCHAR_WRITE_DELAY );
}
/* Turn the LCD Backlight on. */
prvPDCWrite( PDC_CSR, 0x01 );
/* Clear display. */
vTaskDelay( mainCHAR_WRITE_DELAY );
prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR );
uxIndex = 0;
for( ;; )
{
/* Display the string on the LCD. */
prvWriteString( pcStringsToDisplay[ uxIndex ] );
/* Move on to the next string - wrapping if necessary. */
uxIndex++;
if( *( pcStringsToDisplay[ uxIndex ] ) == 0x00 )
{
uxIndex = 0;
/* Longer pause on the last string to be sent. */
vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
}
/* Wait until it is time to move onto the next string. */
vTaskDelay( mainSTRING_WRITE_DELAY );
}
}
/*-----------------------------------------------------------*/
static void vCommsRxTask( void * pvParameters )
{
static portCHAR cRxedChar, cExpectedChar;
/* Set the char we expect to receive to the start of the string. */
cExpectedChar = mainFIRST_TX_CHAR;
for( ;; )
{
/* Wait for a character to be received. */
xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, mainCOMMS_RX_DELAY );
/* Was the character recived (if any) the expected character. */
if( cRxedChar != cExpectedChar )
{
/* Got an unexpected character. This can sometimes occur when
reseting the system using the debugger leaving characters already
in the UART regsters. */
uxErrorStatus = pdFAIL;
/* Resync by waiting for the end of the current string. */
while( cRxedChar != mainLAST_TX_CHAR )
{
while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
}
/* The next expected character is the start of the string again. */
cExpectedChar = mainFIRST_TX_CHAR;
}
else
{
if( cExpectedChar == mainLAST_TX_CHAR )
{
/* We have reached the end of the string - we now expect to
receive the first character in the string again. The LED is
toggled to indicate that the entire string was received without
error. */
vParTestToggleLED( mainCOMMS_RX_LED );
cExpectedChar = mainFIRST_TX_CHAR;
}
else
{
/* We got the expected character, we now expect to receive the
next character in the string. */
cExpectedChar++;
}
}
}
}
/*-----------------------------------------------------------*/
static void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
{
portTickType xDelayPeriod;
static unsigned portLONG *pulRandomBytes = mainFIRST_PROGRAM_BYTES;
/* Co-routine MUST start with a call to crSTART. */
crSTART( xHandle );
for(;;)
{
/* Was the previously transmitted string received correctly? */
if( uxErrorStatus != pdPASS )
{
/* An error was encountered so set the error LED. */
vSetErrorLED();
}
/* The next character to Tx is the first in the string. */
cNextChar = mainFIRST_TX_CHAR;
UARTIntDisable( UART0_BASE, UART_INT_TX );
{
/* Send the first character. */
if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
{
HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
}
/* Move the variable to the char to Tx on so the ISR transmits
the next character in the string once this one has completed. */
cNextChar++;
}
UARTIntEnable(UART0_BASE, UART_INT_TX);
/* Toggle the LED to show a new string is being transmitted. */
vParTestToggleLED( mainCOMMS_TX_LED );
/* Delay before we start the string off again. A pseudo-random delay
is used as this will provide a better test. */
xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
pulRandomBytes++;
if( pulRandomBytes > mainTOTAL_PROGRAM_MEMORY )
{
pulRandomBytes = mainFIRST_PROGRAM_BYTES;
}
/* Make sure we don't wait too long... */
xDelayPeriod &= mainMAX_TX_DELAY;
/* ...but we do want to wait. */
if( xDelayPeriod < mainMIN_TX_DELAY )
{
xDelayPeriod = mainMIN_TX_DELAY;
}
/* Block for the random(ish) time. */
crDELAY( xHandle, xDelayPeriod );
}
/* Co-routine MUST end with a call to crEND. */
crEND();
}
/*-----------------------------------------------------------*/
static void vSerialInit( void )
{
/* Enable the UART. GPIOA has already been initialised. */
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
/* Set GPIO A0 and A1 as peripheral function. They are used to output the
UART signals. */
GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
/* Configure the UART for 8-N-1 operation. */
UARTConfigSet( UART0_BASE, mainBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
/* We dont want to use the fifo. This is for test purposes to generate
as many interrupts as possible. */
HWREG( UART0_BASE + UART_O_LCR_H ) &= ~mainFIFO_SET;
/* Enable both Rx and Tx interrupts. */
HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
IntEnable( INT_UART0 );
}
/*-----------------------------------------------------------*/
void vUART_ISR(void)
{
unsigned portLONG ulStatus;
portCHAR cRxedChar;
portBASE_TYPE xTaskWokenByPost = pdFALSE;
/* What caused the interrupt. */
ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
/* Clear the interrupt. */
UARTIntClear( UART0_BASE, ulStatus );
/* Was an Rx interrpt pending? */
if( ulStatus & UART_INT_RX )
{
if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
{
/* Get the char from the buffer and post it onto the queue of
Rxed chars. Posting the character should wake the task that is
blocked on the queue waiting for characters. */
cRxedChar = ( portCHAR ) HWREG( UART0_BASE + UART_O_DR );
xTaskWokenByPost = xQueueSendFromISR( xCommsQueue, &cRxedChar, xTaskWokenByPost );
}
}
/* Was a Tx interrupt pending? */
if( ulStatus & UART_INT_TX )
{
/* Send the next character in the string. We are not using the FIFO. */
if( cNextChar <= mainLAST_TX_CHAR )
{
if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
{
HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
}
cNextChar++;
}
}
if( xTaskWokenByPost )
{
/* If a task was woken by the character being received then we force
a context switch to occur in case the task is of higher priority than
the currently executing task (i.e. the task that this interrupt
interrupted.) */
portEND_SWITCHING_ISR( xTaskWokenByPost );
}
}
/*-----------------------------------------------------------*/
static void prvPDCWrite( portCHAR cAddress, portCHAR cData )
{
vTaskSuspendAll();
{
PDCWrite( cAddress, cData );
}
xTaskResumeAll();
}
/*-----------------------------------------------------------*/
void vSetErrorLED( void )
{
vParTestSetLED( mainCOMMS_FAIL_LED, pdTRUE );
}
/*-----------------------------------------------------------*/
void prvSetAndCheckRegisters( void )
{
/* Fill the general purpose registers with known values. */
__asm volatile( " mov r11, #10\n"
" add r0, r11, #1\n"
" add r1, r11, #2\n"
" add r2, r11, #3\n"
" add r3, r11, #4\n"
" add r4, r11, #5\n"
" add r5, r11, #6\n"
" add r6, r11, #7\n"
" add r7, r11, #8\n"
" add r8, r11, #9\n"
" add r9, r11, #10\n"
" add r10, r11, #11\n"
" add r12, r11, #12" );
/* Check the values are as expected. */
__asm volatile( " cmp r11, #10\n"
" bne set_error_led\n"
" cmp r0, #11\n"
" bne set_error_led\n"
" cmp r1, #12\n"
" bne set_error_led\n"
" cmp r2, #13\n"
" bne set_error_led\n"
" cmp r3, #14\n"
" bne set_error_led\n"
" cmp r4, #15\n"
" bne set_error_led\n"
" cmp r5, #16\n"
" bne set_error_led\n"
" cmp r6, #17\n"
" bne set_error_led\n"
" cmp r7, #18\n"
" bne set_error_led\n"
" cmp r8, #19\n"
" bne set_error_led\n"
" cmp r9, #20\n"
" bne set_error_led\n"
" cmp r10, #21\n"
" bne set_error_led\n"
" cmp r12, #22\n"
" bne set_error_led\n"
" bx lr" );
__asm volatile( "set_error_led:\n"
" push {r14}\n"
" ldr r1, =vSetErrorLED\n"
" blx r1\n"
" pop {r14}\n"
" bx lr" );
}
/*-----------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -