Changeset - 49485c1fefae
[Not reviewed]
cortex-f0
2 7 2
Ethan Zonca - 9 years ago 2015-08-23 19:00:35
ez@ethanzonca.com
Migrate sysclk, cleanup
9 files changed with 49 insertions and 49 deletions:
0 comments (0 inline, 0 general)
Makefile
Show inline comments
 
# STM32F0xx Makefile
 
# #####################################
 
#
 
# Part of the uCtools project
 
# uctools.github.com
 
#
 
#######################################
 
# user configuration:
 
#######################################
 

	
 

	
 
# SOURCES: list of sources in the user application
 
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c bootlib.c storage.c flash.c max31855.c
 
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c syslib.c storage.c flash.c max31855.c max31865.c
 

	
 
# TARGET: name of the user application
 
TARGET = main
 

	
 
# BUILD_DIR: directory to place output files in
 
BUILD_DIR = build
 

	
 
# LD_SCRIPT: location of the linker script
 
LD_SCRIPT = stm32f042c6_flash.ld
 

	
 
# USER_DEFS user defined macros
 
USER_DEFS = -D HSI48_VALUE=48000000 -D HSE_VALUE=16000000
config.h
Show inline comments
 
@@ -20,15 +20,18 @@
 
// Pin settings
 
#define LED_POWER GPIOF,GPIO_PIN_0
 
#define MAX_CS GPIOA,GPIO_PIN_15
 

	
 
#define SW_BTN  GPIOB, GPIO_PIN_4
 
#define SW_UP   GPIOB, GPIO_PIN_7
 
#define SW_DOWN GPIOB, GPIO_PIN_3
 
#define SW_LEFT GPIOB, GPIO_PIN_5
 
#define SW_RIGHT GPIOB, GPIO_PIN_6
 

	
 
#define SSR_PIN GPIOA, GPIO_PIN_1
 

	
 
// Visual niceness
 
#define hal_init() HAL_Init()
 

	
 
#endif
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
display.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
#include "ssd1306.h"
 
#include "stringhelpers.h"
 
#include "display.h"
 
#include "config.h"
 
#include "states.h"
 
#include "bootlib.h"
 
#include "syslib.h"
 
#include "flash.h"
 
#include "gpio.h"
 

	
 
uint8_t goto_mode = 2;
 

	
 
// State machine
 
uint8_t sw_btn_last = 0;
 
uint8_t sw_up_last = 0;
 
uint8_t sw_down_last = 0;
 
uint8_t sw_left_last = 0;
 
uint8_t sw_right_last = 0;
 

	
main.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
 
#include "config.h"
 
#include "syslib.h"
 
#include "states.h"
 
#include "ssd1306.h"
 
#include "max31855.h"
 
#include "gpio.h"
 
#include "spi.h"
 
#include "flash.h"
 
#include "stringhelpers.h"
 
#include "display.h"
 
#include "storage.h"
 
 
#include "usb_device.h"
 
#include "usbd_cdc_if.h"
 
 
 
// Prototypes
 
// Move to header file
 
void process();
 
void SystemClock_Config(void);
 
 
therm_settings_t set;
 
therm_status_t status;
 
 
 
// Globalish setting vars
 
SPI_HandleTypeDef hspi1;
 
static __IO uint32_t TimingDelay;
 
 
void deinit(void)
 
{
 
    HAL_DeInit();
 
}
 
 
volatile int i=0;
 
int main(void)
 
{
 
 
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 
    HAL_Init();
 
    // Initialize HAL
 
    hal_init();
 
 
    /* Configure the system clock */
 
    SystemClock_Config();
 
    // Configure the system clock
 
    systemclock_config();
 
 
    /* Unset bootloader option bytes (if set) */
 
    // Unset bootloader option bytes (if set)
 
    void bootloader_unset(void);
 
 
    /* Initialize all configured peripherals */
 
    // Init GPIO
 
    init_gpio();
 
 
    // Init USB (TODO: Handle plugged/unplugged with external power)
 
    MX_USB_DEVICE_Init();
 
//    set.usb_plugged = 
 
 
    // USB startup delay
 
    HAL_Delay(1000);
 
    HAL_GPIO_WritePin(LED_POWER, 1);
 
 
    // Enter into bootloader if up button pressed on boot
 
    if(!HAL_GPIO_ReadPin(SW_UP))
 
        bootloader_enter(); // Resets into bootloader
 
 
    // TODO: Awesome pwm of power LED 
 
 
    // Configure 1ms SysTick (change if more temporal resolution needed) 
 
    //RCC_ClocksTypeDef RCC_Clocks;
 
    //RCC_GetClocksFreq(&RCC_Clocks);
 
    //SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
 
        bootloader_enter(); 
 
 
    // Init SPI busses
 
    init_spi();
 
 
    // Init OLED over SPI
 
    ssd1306_Init();
 
    ssd1306_clearscreen();
 
   
 
    // Default settings 
 
    set.boottobrew = 0;
 
    set.temp_units = TEMP_UNITS_CELSIUS;
 
    set.windup_guard = 1;
 
@@ -112,51 +106,24 @@ int main(void)
 
    // Main loop
 
    while(1)
 
    {
 
        // Process sensor inputs
 
        process();
 
 
        // Run state machine
 
        display_process(&set, &status); 
 
    }
 
 
}
 
 
// Clock configuration
 
void SystemClock_Config(void)
 
{
 
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
 
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
 
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
 
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
 
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
 
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
 
 
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
 
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
 
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
 
 
  __SYSCFG_CLK_ENABLE();
 
 
}
 
 
// PID implementation
 
// TODO: Make struct that has the last_temp and i_state in it, pass by ref. Make struct that has other input values maybe.
 
int16_t last_pid_temp = 0;
 
uint8_t last_pid_temp_frac = 0;
 
int32_t i_state = 0;
 
 
int16_t update_pid(uint16_t k_p, uint16_t k_i, uint16_t k_d, int16_t temp, uint8_t temp_frac, int16_t setpoint) 
 
{
 
  // Calculate instantaneous error
 
  int16_t error = setpoint - temp; // TODO: Use fixed point fraction
 
 
  // Proportional component
max31855.c
Show inline comments
 
@@ -82,13 +82,13 @@ void max31855_readtemp(SPI_HandleTypeDef
 
            status->temp += set->temp_offset;
 
        }
 

	
 
        // Use Celsius values
 
        else
 
        {
 
            status->temp = temp_pre * signint;
 
            status->temp += set->temp_offset;
 
        }
 
    }
 
}
 

	
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
max31855.h
Show inline comments
 
#ifndef MAX31855_H
 
#define MAX31855_H
 

	
 
void max31855_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status);
 

	
 
#endif
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
syslib.c
Show inline comments
 
file renamed from bootlib.c to syslib.c
 
@@ -52,12 +52,39 @@ void bootloader_enter(void) {
 
  
 
    HAL_FLASHEx_OBErase();
 
  
 
    HAL_FLASHEx_OBProgram(&OBParam);
 
  
 
    HAL_FLASH_OB_Lock();
 
    HAL_FLASH_Lock();
 
  
 
    HAL_FLASH_OB_Launch();
 
}
 

	
 

	
 
// Clock configuration
 
void systemclock_config(void)
 
{
 

	
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
 

	
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
 
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
 
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 

	
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
 
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
 
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
 

	
 
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
 
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
 
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
 

	
 
  __SYSCFG_CLK_ENABLE();
 

	
 
}
 

	
syslib.h
Show inline comments
 
file renamed from bootlib.h to syslib.h
 
#ifndef BOOTLIB_H
 
#define BOOTLIB_H
 

	
 
void bootloader_unset(void);
 
void bootloader_enter(void);
 
void systemclock_config(void);
 

	
 
#endif
usbd_conf.c
Show inline comments
 
@@ -44,25 +44,25 @@
 
PCD_HandleTypeDef hpcd_USB_FS;
 
 
/* USER CODE BEGIN 0 */
 
__IO uint32_t remotewakeupon=0;
 
/* USER CODE END 0 */
 
 
/* Private function prototypes -----------------------------------------------*/
 
/* Private functions ---------------------------------------------------------*/
 
/* USER CODE BEGIN 1 */
 
static void SystemClockConfig_Resume(void);
 
/* USER CODE END 1 */
 
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state);
 
extern void SystemClock_Config(void);
 
extern void systemclock_config(void);
 
 
/*******************************************************************************
 
		       LL Driver Callbacks (PCD -> USB Device Library)
 
*******************************************************************************/
 
/* MSP Init */
 
 
void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd)
 
{
 
  if(hpcd->Instance==USB)
 
  {
 
  /* USER CODE BEGIN USB_MspInit 0 */
 
 
@@ -493,25 +493,25 @@ void USBD_static_free(void *p)
 
 
}
 
 
/* USER CODE BEGIN 4 */
 
/**
 
  * @brief  Configures system clock after wake-up from USB Resume CallBack:
 
  *         enable HSI, PLL and select PLL as system clock source.
 
  * @param  None
 
  * @retval None
 
  */
 
static void SystemClockConfig_Resume(void)
 
{
 
	SystemClock_Config();
 
	systemclock_config();
 
}
 
/* USER CODE END 4 */
 
 
/**
 
* @brief Software Device Connection
 
* @param hpcd: PCD handle
 
* @param state: connection state (0 : disconnected / 1: connected)
 
* @retval None
 
*/
 
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
 
{
 
/* USER CODE BEGIN 5 */
0 comments (0 inline, 0 general)