Compare commits
No commits in common. "32cd24b5c8a28055d64fd6e9c7f25cbccb2f3749" and "35de89d95d2e4688bf93e9b19256247fd3774b8c" have entirely different histories.
32cd24b5c8
...
35de89d95d
10
fw/lib_i2c.h
10
fw/lib_i2c.h
@ -10,16 +10,6 @@
|
|||||||
|
|
||||||
#define I2C_DEFAULT_TIMEOUT 50000
|
#define I2C_DEFAULT_TIMEOUT 50000
|
||||||
|
|
||||||
|
|
||||||
#if defined(FUNCONF_USE_DEBUGPRINTF) || defined(FUNCONF_USE_USBPRINTF) || defined(FUNCONF_USE_UARTPRINTF)
|
|
||||||
#include <stdio.h>
|
|
||||||
static inline void print_i2c_device(uint8_t addr)
|
|
||||||
{
|
|
||||||
printf("Device found at 0x%02X\n", addr);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
//! ####################################
|
//! ####################################
|
||||||
//! I2C INIT FUNCTIONS
|
//! I2C INIT FUNCTIONS
|
||||||
//! ####################################
|
//! ####################################
|
||||||
|
|||||||
68
fw/main.c
68
fw/main.c
@ -9,18 +9,42 @@
|
|||||||
#include "filter.h"
|
#include "filter.h"
|
||||||
#include "sc7a20.h"
|
#include "sc7a20.h"
|
||||||
#include "pd.h"
|
#include "pd.h"
|
||||||
#include "ntc.h"
|
|
||||||
#include "fpmath.h"
|
#include "fpmath.h"
|
||||||
#include "coroutine.h"
|
#include "coroutine.h"
|
||||||
|
|
||||||
|
|
||||||
// constants
|
// constants
|
||||||
|
// LUT for converting NTC readings to degrees celsius
|
||||||
|
// Nominal: 10kOhm, Beta: 3380, Step: 64
|
||||||
|
// TODO: Add a fixed offset to account for toollerances
|
||||||
|
const uint8_t ntc_step_size = 64;
|
||||||
|
const int16_t ntc_lut[] = {
|
||||||
|
1316, 197, 155, 133, 119, 108, 100, 93, 87, 82, 77, 73, 69, 66, 63, 60,
|
||||||
|
57, 54, 52, 50, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 28, 27,
|
||||||
|
25, 23, 22, 20, 19, 17, 15, 14, 12, 11, 9, 7, 6, 4, 2, 0,
|
||||||
|
-1, -3, -5, -7, -9, -11, -14, -16, -19, -22, -25, -28, -32, -38, -44, -55,
|
||||||
|
-55 // extra value to not have an extra if statement
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
u8g2_t *u8g2;
|
u8g2_t *u8g2;
|
||||||
int16_t encoder = 0; // rotary encoder counter
|
int16_t encoder = 0; // rotary encoder counter
|
||||||
uint32_t last_interrupt = 0; // last time the encoder interrupt was triggered
|
uint32_t last_interrupt = 0; // last time the encoder interrupt was triggered
|
||||||
struct pd_profile_t pd_profile;
|
struct pd_profile_t pd_profile;
|
||||||
uint16_t vcc_mv = 3300;
|
uint16_t vcc_mv = 3300;
|
||||||
|
|
||||||
|
// Convert the raw adc reading to a temperature in celsius with the ntc lut,
|
||||||
|
// linearly interpolating between positions
|
||||||
|
static inline int16_t get_temp_c(uint16_t adc_reading)
|
||||||
|
{
|
||||||
|
if (adc_reading > 4095) return 0;
|
||||||
|
uint8_t index = adc_reading / ntc_step_size;
|
||||||
|
uint8_t remainder = adc_reading % ntc_step_size;
|
||||||
|
int16_t temp_base = index < 64 ? ntc_lut[index] : 0;
|
||||||
|
int16_t temp_next = ntc_lut[index + 1];
|
||||||
|
return temp_base + ((temp_next - temp_base) * remainder)/ntc_step_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// convert the raw TPA191 adc reading to a current in milliamps
|
// convert the raw TPA191 adc reading to a current in milliamps
|
||||||
static inline int16_t get_current_ma(uint16_t adc_reading)
|
static inline int16_t get_current_ma(uint16_t adc_reading)
|
||||||
@ -32,6 +56,12 @@ static inline int16_t get_current_ma(uint16_t adc_reading)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void print_i2c_device(uint8_t addr)
|
||||||
|
{
|
||||||
|
printf("Device found at 0x%02X\n", addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// this callback is mandatory when FUNCONF_USE_USBPRINTF is defined,
|
// this callback is mandatory when FUNCONF_USE_USBPRINTF is defined,
|
||||||
// can be empty though
|
// can be empty though
|
||||||
void handle_usbfs_input(int numbytes, uint8_t *data)
|
void handle_usbfs_input(int numbytes, uint8_t *data)
|
||||||
@ -138,6 +168,7 @@ static inline void setup_i2c(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// FIXME: this holds the max number of ADC channels, ideally this would be lower
|
||||||
volatile uint16_t adc_buffer[16];
|
volatile uint16_t adc_buffer[16];
|
||||||
// Set-up the adc to perform continous conversion of enabled channels, enable
|
// Set-up the adc to perform continous conversion of enabled channels, enable
|
||||||
// channel injection and setup DMA to automatically transfer conversion results
|
// channel injection and setup DMA to automatically transfer conversion results
|
||||||
@ -281,7 +312,7 @@ static inline void setup_pwm(void)
|
|||||||
// for channel 1 let CCxS stay 00 (output), set OC1M to 110 (PWM 1)
|
// for channel 1 let CCxS stay 00 (output), set OC1M to 110 (PWM 1)
|
||||||
// enabling preload (OC1PE) causes the new pulse width in compare capture
|
// enabling preload (OC1PE) causes the new pulse width in compare capture
|
||||||
// register only to come into effect when UG bit in SWEVGR is set
|
// register only to come into effect when UG bit in SWEVGR is set
|
||||||
// (= initiate update) (auto-clears)
|
// (= initiate update) (auto-clears)
|
||||||
TIM3->CHCTLR1 |= TIM_OC1M_2 | TIM_OC1M_1 | TIM_OC1PE;
|
TIM3->CHCTLR1 |= TIM_OC1M_2 | TIM_OC1M_1 | TIM_OC1PE;
|
||||||
|
|
||||||
// Enable channel 1 output (PA6)
|
// Enable channel 1 output (PA6)
|
||||||
@ -362,7 +393,7 @@ uint16_t pid(int16_t delta, int16_t max_duty)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ------------------------------ Board Setup ------------------------------- */
|
// Board Setup
|
||||||
static inline void setup(void)
|
static inline void setup(void)
|
||||||
{
|
{
|
||||||
// Generic system setup
|
// Generic system setup
|
||||||
@ -370,7 +401,7 @@ static inline void setup(void)
|
|||||||
funGpioInitAll();
|
funGpioInitAll();
|
||||||
USBFSSetup();
|
USBFSSetup();
|
||||||
|
|
||||||
// ====================== Analog pin configuration ====================== //
|
// Analog pin configuration
|
||||||
funPinMode(PIN_VBUS, GPIO_CFGLR_IN_ANALOG);
|
funPinMode(PIN_VBUS, GPIO_CFGLR_IN_ANALOG);
|
||||||
funPinMode(PIN_CURRENT, GPIO_CFGLR_IN_ANALOG);
|
funPinMode(PIN_CURRENT, GPIO_CFGLR_IN_ANALOG);
|
||||||
funPinMode(PIN_NTC, GPIO_CFGLR_IN_ANALOG);
|
funPinMode(PIN_NTC, GPIO_CFGLR_IN_ANALOG);
|
||||||
@ -388,45 +419,44 @@ static inline void setup(void)
|
|||||||
};
|
};
|
||||||
setup_adc_and_dma(adc_channels, sizeof(adc_channels), adc_injected, sizeof(adc_injected));
|
setup_adc_and_dma(adc_channels, sizeof(adc_channels), adc_injected, sizeof(adc_injected));
|
||||||
|
|
||||||
// ====================== Digital pin configuration ===================== //
|
// Digital pin configuration
|
||||||
funPinMode(PIN_12V, GPIO_CFGLR_OUT_10Mhz_PP);
|
funPinMode(PIN_12V, GPIO_CFGLR_OUT_10Mhz_PP);
|
||||||
funDigitalWrite(PIN_12V, 0);
|
funDigitalWrite(PIN_12V, 0);
|
||||||
funPinMode(PIN_HEATER, GPIO_CFGLR_OUT_10Mhz_AF_PP);
|
funPinMode(PIN_HEATER, GPIO_CFGLR_OUT_10Mhz_AF_PP);
|
||||||
funDigitalWrite(PIN_HEATER, 0);
|
funDigitalWrite(PIN_HEATER, 0);
|
||||||
funPinMode(PIN_DISP_RST, GPIO_CFGLR_OUT_10Mhz_PP);
|
funPinMode(PIN_DISP_RST, GPIO_CFGLR_OUT_10Mhz_PP);
|
||||||
funDigitalWrite(PIN_DISP_RST, 1);
|
funDigitalWrite(PIN_DISP_RST, 1); // start with display disabled
|
||||||
|
|
||||||
funPinMode(PIN_ENC_A, GPIO_CFGLR_IN_PUPD); // enable pull-up/down
|
funPinMode(PIN_ENC_A, GPIO_CFGLR_IN_PUPD); // enable pull-up/down
|
||||||
funDigitalWrite(PIN_ENC_A, 1); // specify pull-up
|
funDigitalWrite(PIN_ENC_A, 1); // specify pull-up
|
||||||
funPinMode(PIN_ENC_B, GPIO_CFGLR_IN_PUPD); // enable pull-up/down
|
funPinMode(PIN_ENC_B, GPIO_CFGLR_IN_PUPD); // enable pull-up/down
|
||||||
funDigitalWrite(PIN_ENC_B, 1); // specify pull-up
|
funDigitalWrite(PIN_ENC_B, 1); // specify pull-up
|
||||||
funPinMode(PIN_BTN, GPIO_CFGLR_IN_FLOAT);
|
funPinMode(PIN_BTN, GPIO_CFGLR_IN_FLOAT);
|
||||||
|
|
||||||
// Setup PWM timer, includes pin setup
|
// Setup PWM timer
|
||||||
setup_pwm();
|
setup_pwm();
|
||||||
pwm_off();
|
pwm_off();
|
||||||
|
|
||||||
// =========================== Encoder Setup ============================ //
|
// Setup i2c line, includes setting the pin alternate mode
|
||||||
|
setup_i2c();
|
||||||
|
|
||||||
// Configure the IO as an interrupt.
|
// Configure the IO as an interrupt.
|
||||||
// PIN_ENC_B is on port B, channel 11
|
// PIN_ENC_B is on port B, channel 11
|
||||||
AFIO->EXTICR1 |= AFIO_EXTICR1_EXTI11_PB; // Port B channel (pin) 11
|
AFIO->EXTICR1 |= AFIO_EXTICR1_EXTI11_PB; // Port B channel (pin) 11
|
||||||
EXTI->INTENR |= EXTI_INTENR_MR11; // Enable EXT11
|
EXTI->INTENR |= EXTI_INTENR_MR11; // Enable EXT11
|
||||||
EXTI->FTENR |= EXTI_FTENR_TR11; // Falling edge trigger
|
EXTI->FTENR |= EXTI_FTENR_TR11; // Falling edge trigger
|
||||||
EXTI->RTENR |= EXTI_RTENR_TR11; // Rising edge trigger
|
EXTI->RTENR |= EXTI_RTENR_TR11; // Rising edge trigger
|
||||||
// enable interrupt
|
// enable interrupt
|
||||||
NVIC_EnableIRQ(EXTI15_8_IRQn);
|
NVIC_EnableIRQ(EXTI15_8_IRQn);
|
||||||
|
|
||||||
// PIN_ENC_A is on port B, channel 3
|
// PIN_ENC_A is on port B, channel 3
|
||||||
AFIO->EXTICR1 |= AFIO_EXTICR1_EXTI3_PB; // Port B channel (pin) 3
|
AFIO->EXTICR1 |= AFIO_EXTICR1_EXTI3_PB; // Port B channel (pin) 3
|
||||||
EXTI->INTENR |= EXTI_INTENR_MR3; // Enable EXT3
|
EXTI->INTENR |= EXTI_INTENR_MR3; // Enable EXT3
|
||||||
EXTI->FTENR |= EXTI_FTENR_TR3; // Falling edge trigger
|
EXTI->FTENR |= EXTI_FTENR_TR3; // Falling edge trigger
|
||||||
EXTI->RTENR |= EXTI_RTENR_TR3; // Rising edge trigger
|
EXTI->RTENR |= EXTI_RTENR_TR3; // Rising edge trigger
|
||||||
// enable interrupt
|
// enable interrupt
|
||||||
NVIC_EnableIRQ(EXTI7_0_IRQn);
|
NVIC_EnableIRQ(EXTI7_0_IRQn);
|
||||||
|
|
||||||
// ============================= I2C Setup ============================== //
|
|
||||||
// Setup i2c line, includes setting the pin alternate mode
|
|
||||||
setup_i2c();
|
|
||||||
// Start i2c peripherals
|
// Start i2c peripherals
|
||||||
u8g2 = display_init();
|
u8g2 = display_init();
|
||||||
sc7a20_init();
|
sc7a20_init();
|
||||||
|
|||||||
31
fw/ntc.h
31
fw/ntc.h
@ -1,31 +0,0 @@
|
|||||||
#ifndef _NTC_H
|
|
||||||
#define _NTC_H
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
// LUT for converting NTC readings to degrees celsius
|
|
||||||
// Nominal: 10kOhm, Beta: 3380, Step: 64
|
|
||||||
// TODO: Add a fixed offset to account for toollerances
|
|
||||||
const uint8_t ntc_step_size = 64;
|
|
||||||
const int16_t ntc_lut[] = {
|
|
||||||
1316, 197, 155, 133, 119, 108, 100, 93, 87, 82, 77, 73, 69, 66, 63, 60,
|
|
||||||
57, 54, 52, 50, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 28, 27,
|
|
||||||
25, 23, 22, 20, 19, 17, 15, 14, 12, 11, 9, 7, 6, 4, 2, 0,
|
|
||||||
-1, -3, -5, -7, -9, -11, -14, -16, -19, -22, -25, -28, -32, -38, -44, -55,
|
|
||||||
-55 // extra value to not have an extra if statement
|
|
||||||
};
|
|
||||||
|
|
||||||
// Convert the raw adc reading to a temperature in celsius with the ntc lut,
|
|
||||||
// linearly interpolating between positions
|
|
||||||
static inline int16_t get_temp_c(uint16_t adc_reading)
|
|
||||||
{
|
|
||||||
if (adc_reading > 4095) return 0;
|
|
||||||
uint8_t index = adc_reading / ntc_step_size;
|
|
||||||
uint8_t remainder = adc_reading % ntc_step_size;
|
|
||||||
int16_t temp_base = index < ntc_step_size ? ntc_lut[index] : 0;
|
|
||||||
int16_t temp_next = ntc_lut[index + 1];
|
|
||||||
return temp_base + ((temp_next - temp_base) * remainder)/ntc_step_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Loading…
x
Reference in New Issue
Block a user