maggu2810

stm32f4-discovery

First steps on my first STM32 project

Author: Markus Rathgeb


Create Project - STM32CubeMX

Update Heap / Stack Size

UART printf

Code

/* USER CODE BEGIN 0 */

int __io_putchar(int ch)
{
  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
  return ch;
}

/* USER CODE END 0 */

Wiring

Connect FTDI / Prolific / …

Cable Type RXD TXD GND
black, yellow, orange orange yellow black
black, white, green green white black

Host Side

It seems screen and minicom do not adding a carriage return on a line feed default. This result into a new line without beginning at the beginning of the line but continue new data on the next position.

For minicom we could edit ~/.minirc.dfl and add the following line

pu addcarreturn Yes

minicom could be started using (change the device):

minicom -b 115200 -D /dev/ttyUSB2 -o

Lecture

See also:

LED Test

   <3>
<4>   <5>
   <6>

main.c main loop (heartbeet)

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  uint32_t tickLast = HAL_GetTick();
  int next_action = GPIO_PIN_SET;
  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();

    /* USER CODE BEGIN 3 */
    const uint32_t tickCur = HAL_GetTick();
    if (tickCur - tickLast >= 1000) {
      tickLast = tickCur;
      HAL_GPIO_WritePin(GPIOD, LD4_Pin, next_action);
      next_action = next_action == GPIO_PIN_SET ? GPIO_PIN_RESET : GPIO_PIN_SET;
    }
  }
  /* USER CODE END 3 */

main.c error handler

void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  uint32_t tickLast = HAL_GetTick();
  int next_action = GPIO_PIN_SET;
  while (1)
  {
    const uint32_t tickCur = HAL_GetTick();
    if (tickCur - tickLast >= 500)
    {
      tickLast = tickCur;
      HAL_GPIO_WritePin(GPIOD, LD6_Pin, next_action);
      next_action = next_action == GPIO_PIN_SET ? GPIO_PIN_RESET : GPIO_PIN_SET;
    }
  }
  /* USER CODE END Error_Handler_Debug */
}

USB Host

Very import VBUS fix

USB_HOST/Target/usbh_platform.c

Fixed code looks like:

void MX_DriverVbusFS(uint8_t state)
{
  uint8_t data = state;
  /* USER CODE BEGIN PREPARE_GPIO_DATA_VBUS_FS */
  if(state == 0)
  {
    /* Drive high Charge pump */
    data = GPIO_PIN_SET;
  }
  else
  {
    /* Drive low Charge pump */
    data = GPIO_PIN_RESET;
  }
  /* USER CODE END PREPARE_GPIO_DATA_VBUS_FS */
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,(GPIO_PinState)data);
}

For further information see the end of: http://evenlund.blogspot.com/2016/10/usb-storage-with-stm32f4-discovery-and_58.html

CAN Bus

Configuration

Parameter Settings

For the “CAN bit-timing” values’ calculation you can use CAN Bit Time Calculation

STM32 CubeMX Configuration

GPIO Settings

PIN Name Signal on PIN GPIO output level GPIO mode GPIO Pull-up/Pull-down Maximum output speed
PD0 CAN1_RX n/a Alternate Function Push Pull Pull-up Very High
PD1 CAN1_TX n/a Alternate Function Push Pull Pull-up Very High