Compare commits

..

No commits in common. "1ba352af4ec68cb42230d91553a103b711b8dab4" and "77701636b53c4561a97779ca53492175a6876d26" have entirely different histories.

2 changed files with 15 additions and 19 deletions

View File

@ -6,7 +6,6 @@
#define FUNCONF_DEBUG_HARDFAULT 0 #define FUNCONF_DEBUG_HARDFAULT 0
#define CH32X035 1 #define CH32X035 1
#define I2C_TARGET I2C1 #define I2C_TARGET I2C1
#define VCC_MV 3480
#endif // _FUNCONFIG_H #endif // _FUNCONFIG_H

View File

@ -8,6 +8,7 @@
// Pin definitions // Pin definitions
#define PIN_LED PA4 // debug led
#define PIN_VBUS PA0 // vbus voltage feedback #define PIN_VBUS PA0 // vbus voltage feedback
#define PIN_CURRENT PA1 // current feedback #define PIN_CURRENT PA1 // current feedback
#define PIN_NTC PA2 // ntc temperature sensor #define PIN_NTC PA2 // ntc temperature sensor
@ -32,12 +33,12 @@ const int16_t ntc_lut[] = {
1316, 197, 155, 133, 119, 108, 100, 93, 87, 82, 77, 73, 69, 66, 63, 60, 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, 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, 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, -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;
uint8_t pin = 0;
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
#define ENCODER_DEBOUNCE 6000 #define ENCODER_DEBOUNCE 6000
@ -51,21 +52,11 @@ static inline int16_t get_temp_k(uint16_t adc_reading)
uint8_t index = adc_reading / ntc_step_size; uint8_t index = adc_reading / ntc_step_size;
uint8_t remainder = 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_base = index < 64 ? ntc_lut[index] : 0;
int16_t temp_next = ntc_lut[index + 1]; int16_t temp_next = index < 63 ? ntc_lut[index + 1] : temp_base;
return temp_base + ((temp_next - temp_base) * remainder)/ntc_step_size; return temp_base + ((temp_next - temp_base) * remainder)/ntc_step_size;
} }
// convert the raw TPA191 adc reading to a current in milliamps
static inline int16_t get_current_ma(uint16_t adc_reading)
{
// Rshunt = 4 milliOhm
// Gain = 100
u32 mv = ((u32)adc_reading * VCC_MV) / 4096;
return (mv * 10) / 4;
}
// convert the raw tip reading from the adc to a temperature in kelvin // convert the raw tip reading from the adc to a temperature in kelvin
static inline int16_t get_tip_temp_k(uint16_t adc_reading, int16_t cold_temp_k) static inline int16_t get_tip_temp_k(uint16_t adc_reading, int16_t cold_temp_k)
{ {
@ -115,7 +106,7 @@ void handle_usbfs_input(int numbytes, uint8_t *data)
break; break;
case 's': case 's':
printf("Scanning I2C bus...\n"); printf("Scanning I2C bus...\n");
i2c_scan(I2C_TARGET, print_i2c_device); i2c_scan(I2C1, print_i2c_device);
break; break;
default: default:
printf("Unknown command '%c'\n", data[0]); printf("Unknown command '%c'\n", data[0]);
@ -183,6 +174,7 @@ __attribute__((noreturn)) int main(void)
funPinMode(PIN_NTC, GPIO_CFGLR_IN_ANALOG); funPinMode(PIN_NTC, GPIO_CFGLR_IN_ANALOG);
funPinMode(PIN_TEMP, GPIO_CFGLR_IN_ANALOG); funPinMode(PIN_TEMP, GPIO_CFGLR_IN_ANALOG);
funPinMode(PIN_LED, GPIO_CFGLR_OUT_10Mhz_PP);
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_PP); funPinMode(PIN_HEATER, GPIO_CFGLR_OUT_10Mhz_PP);
@ -196,6 +188,8 @@ __attribute__((noreturn)) int main(void)
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);
Delay_Ms(5000);
setup_i2c(); setup_i2c();
// Configure the IO as an interrupt. // Configure the IO as an interrupt.
@ -213,12 +207,15 @@ __attribute__((noreturn)) int main(void)
for (uint32_t x = 0; true ; x++) { for (uint32_t x = 0; true ; x++) {
poll_input(); // usb poll_input(); // usb
if ((x % 100) == 0) { if ((x % 100) == 0) {
uint16_t vbus_mv = ((u32)funAnalogRead(VBUS_ADC_CHANNEL)*VCC_MV*11)/4096; uint16_t vbus_mv = (funAnalogRead(VBUS_ADC_CHANNEL)*3300*11)/4095;
uint16_t current_ma = get_current_ma(funAnalogRead(CURRENT_ADC_CHANNEL)); uint16_t current_ma = ((uint32_t)funAnalogRead(CURRENT_ADC_CHANNEL) * 4125 + 1024) / 2048;
int16_t temp_k = get_temp_k(funAnalogRead(NTC_ADC_CHANNEL)); int16_t temp_k = get_temp_k(funAnalogRead(NTC_ADC_CHANNEL));
uint16_t tip_mv = ((u32)funAnalogRead(TEMP_ADC_CHANNEL)*VCC_MV)/4096; uint16_t temp_tip_k = funAnalogRead(TEMP_ADC_CHANNEL);
printf("[%d]: VBUS=%d, CURRENT=%d, TEMP=%d, TIP=%d, COUNTER=%d\n", count++, vbus_mv, current_ma, temp_k, tip_mv, encoder); printf("[%d]: VBUS=%d, CURRENT=%d, TEMP=%d, TIP=%d, COUNTER=%d\n", count++, vbus_mv, current_ma, temp_k, temp_tip_k, encoder);
funDigitalWrite(PIN_LED, pin);
pin = !pin;
} }
Delay_Ms(1); Delay_Ms(1);
} }