car_fmw: straightforward refactoring of MCU GPIO example code

- Extract code to dedicated files, create more abstract interfaces
- Remove meaningless comments
This commit is contained in:
Eugene Batalov
2016-07-05 20:54:09 +03:00
parent 8779695a64
commit 46b994f65b
6 changed files with 125 additions and 140 deletions
+9 -6
View File
@@ -40,22 +40,25 @@ dircs=$(wildcard $(1)/*.c)
dircs_to_prefxd_objs=\
$(patsubst $(1)/%.c,$(BIN_DIR)/$(2)%.o,$(call dircs,$1))
$(CAR_FMW_BIN): $(CAR_FMW_ELF)
$(CAR_FMW_BIN): $(CAR_FMW_ELF) $(BIN_DIR)
$(OBJ_COPY) -O binary $< $@
$(CAR_FMW_ELF): $(CAR_FMW_OBJ) $(LIB_STM32F4D_OBJ) \
$(LIB_CMSIS_OBJ) $(LIB_STDPERIPH_OBJ)
$(LIB_CMSIS_OBJ) $(LIB_STDPERIPH_OBJ)
$(CC) $(CFLAGS) $^ -T $(SRC_DIR)/stm32_flash.ld -o $@
$(CAR_FMW_OBJ): $(BIN_DIR) $(call dircs_to_prefxd_objs,$(SRC_DIR))
CAR_OBJ_PREFIX=car_
$(CAR_FMW_OBJ): \
$(call dircs_to_prefxd_objs,$(SRC_DIR),$(CAR_OBJ_PREFIX))
$(LD_ALL_DEPS)
$(BIN_DIR)/$(CAR_OBJ_PREFIX)%.o: $(SRC_DIR)/%.c \
$(call dirhs,$(SRC_DIR))
$(CC_ALL_DEPS)
$(BIN_DIR):
mkdir -p $(BIN_DIR)
$(BIN_DIR)/%.o: $(SRC_DIR)/%.c
$(CC_ALL_DEPS)
flash: $(CAR_FMW_BIN)
$(ST_DIR)/st-flash write $(CAR_FMW_BIN) 0x8000000
+20
View File
@@ -0,0 +1,20 @@
#include "stm32f4_discovery.h"
#include "stm32f4xx_conf.h"
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
+58
View File
@@ -0,0 +1,58 @@
#pragma once
#include <stdbool.h>
#include "stm32f4_discovery.h"
#include "stm32f4xx_conf.h"
#define CAR_LED_GPIO_PORT_CLOCK RCC_AHB1Periph_GPIOD
#define CAR_LED_GPIO_PORT GPIOD
#define CAR_LED_PIN_GREEN GPIO_Pin_12
#define CAR_LED_PIN_ORANGE GPIO_Pin_13
#define CAR_LED_PIN_RED GPIO_Pin_14
#define CAR_LED_PIN_BLUE GPIO_Pin_15
inline void leds_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(CAR_LED_GPIO_PORT_CLOCK, ENABLE);
GPIO_InitStructure.GPIO_Pin = CAR_LED_PIN_GREEN
| CAR_LED_PIN_ORANGE
| CAR_LED_PIN_RED
| CAR_LED_PIN_BLUE;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(CAR_LED_GPIO_PORT, &GPIO_InitStructure);
}
typedef enum
{
LED_GREEN,
LED_ORANGE,
LED_RED,
LED_BLUE
} led_t;
inline void led_set(led_t led, bool on)
{
int led_pin = CAR_LED_PIN_GREEN;
switch(led) {
case LED_ORANGE:
led_pin = CAR_LED_PIN_ORANGE;
break;
case LED_RED:
led_pin = CAR_LED_PIN_RED;
break;
case LED_BLUE:
led_pin = CAR_LED_PIN_BLUE;
break;
}
if (on)
GPIO_SetBits(CAR_LED_GPIO_PORT, led_pin);
else
GPIO_ResetBits(CAR_LED_GPIO_PORT, led_pin);
}
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include "stm32f4_discovery.h"
#include "stm32f4xx_conf.h"
/**
* @brief Delay Function.
* @param nCount:specifies the Delay time length.
* @retval None
*/
inline void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
+1 -1
View File
@@ -5,5 +5,5 @@ $(LIB_STDPERIPH_OBJ): \
$(LD_ALL_DEPS)
$(BIN_DIR)/$(LIB_STDPRPH_OBJ_PREFIX)%.o: $(LIB_STDPERIPH_DIR)/%.c \
$(call dirhs, $(LIB_STDPERIPH_DIR))
$(call dirhs,$(LIB_STDPERIPH_DIR))
$(CC_ALL_DEPS)
+21 -133
View File
@@ -1,143 +1,31 @@
/**
******************************************************************************
* @file IO_Toggle/main.c
* @author MCD Application Team
* @version V1.0.0
* @date 19-September-2011
* @brief Main program body
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
#include "car_leds.h"
#include "delay.h"
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include "stm32f4xx_conf.h"
/** @addtogroup STM32F4_Discovery_Peripheral_Examples
* @{
*/
/** @addtogroup IO_Toggle
* @{
*/
/* Private typedef -----------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nCount);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f4xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f4xx.c file
system_stm32f4xx.c file
*/
/* GPIOD Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
leds_init();
/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
while (1)
{
/* PD12 to be toggled */
GPIO_SetBits(GPIOD, GPIO_Pin_12);
/* Insert delay */
Delay(0x3FFFFF);
/* PD13 to be toggled */
GPIO_SetBits(GPIOD, GPIO_Pin_13);
/* Insert delay */
Delay(0x3FFFFF);
/* PD14 to be toggled */
GPIO_SetBits(GPIOD, GPIO_Pin_14);
/* Insert delay */
Delay(0x3FFFFF);
/* PD15 to be toggled */
GPIO_SetBits(GPIOD, GPIO_Pin_15);
/* Insert delay */
Delay(0x7FFFFF);
GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
/* Insert delay */
Delay(0xFFFFFF);
}
while (1)
{
led_set(LED_GREEN, true);
Delay(0x3FFFFF);
led_set(LED_ORANGE, true);
Delay(0x3FFFFF);
led_set(LED_RED, true);
Delay(0x3FFFFF);
led_set(LED_BLUE, true);
Delay(0x7FFFFF);
led_set(LED_GREEN, false);
led_set(LED_ORANGE, false);
led_set(LED_RED, false);
led_set(LED_BLUE, false);
Delay(0xFFFFFF);
}
}
/**
* @brief Delay Function.
* @param nCount:specifies the Delay time length.
* @retval None
*/
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/