Assignment #5: USART Library Part 2

📚 Assignment 5 CMPE1250: USART Library Part 2

📋 Overview

In this ICA you ‘complete’ your library for USART. In the process of completing this assignment, you will:

  • Learn about escape sequences and implement terminal control functions.
  • Create string display and string positioning functions.

A discussion about basic escape sequences, string literals, sprintf, and terminal operation will occur during lectures and/or live demos. You should participate in these activities to gather the necessary examples you will need to work from.

1️⃣ Additional Library Functionality:

In case you don’t yet have these functions, you may use the following updated header content as a guide to what you need to include in your library. The implementation details are up to you, but some suggestions for each function are included below:

    // added prototypes / types for advanced UART library

    // transmit the NUL terminated string one character at a time with UART_TxStr
    void UART_TxStr (USART_TypeDef * pUSART, char * pStr);

    // Transmits an array of "size" bytes starting from *pData
    void UART_TxBuffer (USART_TypeDef* pUart, uint8_t* pData, uint16_t size);



2️⃣ Escape sequences to control terminal

The following functionality will only work on a terminal that does support escape sequences:

Here’s a quick summary of the link above:

Basic escape sequences for Tera Term

Escape Sequence Description
\x1b[0J Erase from cursor through the end of the display.
\x1b[1J Erase from the beginning of the display through the cursor.
\x1b[2J Erase the complete of display.
\x1b[0K Erase from the cursor through the end of the line.
\x1b[1K Erase from the beginning of the line through the cursor.
\x1b[2K Erase the complete of line.
\x1b[0;0H Go to position (0,0)
\x1b[1A Moves cursor up by 1 line in the same column.
\x1b[1B Moves cursor down by 1 line in the same column.
\x1b[1C Moves cursor to the right by 1 column.
\x1b[1D Moves cursor to the left by 1 column.
\x1b[44m Set attribute: background color to BLUE
\x1b[31m Set attribute: foreground color to RED

Other Colour Codes

Code Description
30 Set foreground color to Black. (Color No. 0)
31 Set foreground color to Red. (Color No. 1)
32 Set foreground color to Green. (Color No. 2)
33 Set foreground color to Yellow. (Color No. 3)
34 Set foreground color to Blue. (Color No. 4)
35 Set foreground color to Magenta. (Color No. 5)
36 Set foreground color to Cyan. (Color No. 6)
37 Set foreground color to White. (Color No. 7)
38 ; 2 ; r ; g ; b Set foreground color in RGB value, matching closest entry in 256 colors palette.
38 ; 5 ; Ps Set foreground color to color number Ps.
39 Set foreground color to default.
40 Set background color to Black. (Color No. 0)
41 Set background color to Red. (Color No. 1)
42 Set background color to Green. (Color No. 2)
43 Set background color to Yellow. (Color No. 3)
44 Set background color to Blue. (Color No. 4)
45 Set background color to Magenta. (Color No. 5)
46 Set background color to Cyan. (Color No. 6)
47 Set background color to White. (Color No. 7)
48 ; 2 ; r ; g ; b Set background color in RGB value, matching closest entry in 256 colors palette.

3️⃣ Function Implementations

void UART_TxStr (USART_TypeDef * pUSART, char * pStr)
{
  Iterate over non-NUL characters at pStr, using TxByte
  When a NUL character is found (could be first) return
}

void UART_TxBuffer (USART_TypeDef* pUart, uint8_t* pData, uint16_t size)
{
  Iterate over the buffer of size "size" at pData, using TxByte
}

void TERM_GotoXY (USART_TypeDef * pUSART, int iCol, int iRow)
{
  You may do limited error checking on iCol/iRow
  Format an escape sequence based on \x1b[R;CH using sprintf
  Send the formatted string to the USART with Tx_String
}

void TERM_TxStringXY (USART_TypeDef * pUSART, int iCol, int iRow, char * pStr)
{
  Use the GotoXY and TxString functions to build this function.
}

void TERM_ClearScreen (USART_TypeDef * pUSART)
{
  Use the \x1b[2J and \x1b[H escape sequences to clear the screen and go home.
}

4️⃣ Testing

  • Save the code from each part so that you may recall any part of this assignment, as needed.

Part A

  • In the one-time initializations section of your code, bring up USART2 at 38400 BAUD.
  • Clear the terminal screen.
  • Print your full name in the terminal aligned to the upper-left corner of a default 80x24 terminal.
  • Print CMPE1250 aligned to the bottom-right corner of a default 80x24 terminal.

Part B

In the one-time initializations section of your code, bring up USART2 at 9600 BAUD.

  • Enable GPIOA pin 4 as an output

  • In the infinite loop, insert a blocking delay of 100[ms]

  • Right after the blovking delay, using TX_Byte, transmit the uppercase letters of the alphabet in a loop (A to Z)

  • Just prior to the loop, set PA4 high

  • Just after the loop, set PA4 low

  • Use your scope to measure how long it takes to transmit all 26 bytes at 9600 BAUD.

  • Calculate the time that takes to transmit 1 character at 9600 BAUD dividing the total time by 26. Is this consistent with the expected time? (Hint: 9600 BAUD means 9600 bits per second, but how many bits are in a character?)

  • Record the time captured

Part C

In the one-time initializations section of your code, bring up USART2 at 115200 BAUD.

  • Clear the terminal screen.

  • Request two 4-digit decimal numbers from the user, with suitable prompts. The user has the option to enter shorter numbers. An invalid entry should be handled with an error message and a new prompt. You may assume that the user will not enter more than 4 digits, but you should handle the case where they enter nothing at all (i.e. just hit enter).

  • Internally convert the entered string to an actual number.

  • Display the number on the UART 7-segment display. The diplay should be connected to a different USART than the one you are using for the terminal, so you will need to initialize a second USART at 9600 BAUD for this purpose.