STM32 - How To: Difference between revisions

From Embedded Workshop
Jump to navigation Jump to search
No edit summary
Line 64: Line 64:
When only a single LED is available, a trained Boy Scout can read the short and long pulses associated with Morse Code.
When only a single LED is available, a trained Boy Scout can read the short and long pulses associated with Morse Code.
  [http://merkles.com/example_source_code/morse_code/morse_code.c morse_code.c]
  [http://merkles.com/example_source_code/morse_code/morse_code.c morse_code.c]
  [[File:Download.jpg|60px|link=http://merkles.com/example_source_code/morse_code/morse_code.c]]
  [[File:Download.jpg|80px|link=http://merkles.com/example_source_code/morse_code/morse_code.c]]
[[Image:Download.jpg|80px|link={{http://merkles.com/example_source_code/morse_code/morse_code.c}}]]

Revision as of 13:30, 11 April 2022

Fix Eclipse's Small Icon issue

1 Find the installation folder for your STM32CubeIDE_1.9.0.
    For me, this is C:\ST\STM32CubeIDE_1.9.0\STM32CubeIDE
2 Open stm32cubeide.ini in your favorite text editor
3 Add the following bolded line of text in the -vmargs section:
 -vmargs
 -Dswt.autoScale=quarter
 -Dosgi.requiredJavaVersion=1.8
 -Dosgi.instance.area.default=

printf()

The printf() function is commonly used for "C" programming to output formatted text to a terminal.
This allows easy debugging in that variables, intermediate values, entering/leaving functions can
be monitored and status displayed with the printf() stdio library function.
The STM32 development environment provides an stdio printf() library function, but some
"connecting glue" is required to provide the desired output.
The printf() function calls _write() to perform an output function.
See the _write() function in syscalls.c.  This function was given the (weak) attribute, allowing
the function to be over-written (without causing a warning or error message.)
This default _write() function calls __io_putchar() to output one character at a time.
Redefining either function to write characters to a serial port will produce a functioning
printf().
Here's some sample code to get your printf() functional:

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define HAL_SMALL_DELAY  40
// Define serial input and output functions using UART2
int __io_putchar(int ch)
{
    HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_SMALL_DELAY); // infinite delay value
    return 1;
}

// Read a character from the UART with small timeout
// If HAL_UART_Receive() returns error, return EOF (-1), else return the character read
int __io_getchar(void)
{
    uint8_t byte;
    HAL_StatusTypeDef hal_status = HAL_UART_Receive(&huart2, &byte, sizeof(byte), HAL_SMALL_DELAY);
    if(HAL_OK == hal_status)
    {
        return byte;
    }
    else
        return EOF;
}
/* USER CODE END 0 */

I2C

We will use the upper right two pins on the Arduino header.
These are mapped as follows: D15: P8_8 (SCL) and D14: P8_9 (SDA)
By default, STM's pin selector will map I2C1 to different pins.
To change this, open the project's .ioc file, select "Pinout & Configuration" tab
Select Connectivity category, and then I2C1
If not yet enabled, enable it in the "Mode" section as "I2C"
In the "GPIO Settings" section, we want it to display PB8 for I2C_SCL and PB9 for I2C_SDA.
If it doesn't, go to the Pinout view on the right, select the PB8 pin, and then select the "I2C1_SCL" function.
  Doing this will usually define both pins.  If not, repeat the process for PB9, with function I2C_SDA.

Example Source Code

Morse Code

When only a single LED is available, a trained Boy Scout can read the short and long pulses associated with Morse Code.

morse_code.c