234 lines
5.7 KiB
C
234 lines
5.7 KiB
C
#include <main.h>
|
||
#include <math.h>
|
||
#include <stdio.h>
|
||
#include <stm32g4xx_hal_conf.h>
|
||
|
||
#include "si5351.h"
|
||
#include "squeow.h"
|
||
#include "squeow_ui.h"
|
||
|
||
/* SQUEOW
|
||
|
||
TIM2 PWM 168000000/4096 = 41015,625 Hz
|
||
TIM3 sys_tick 168000000/(16800×100) 100hz
|
||
|
||
risoluzione PWM 4096 (12bit)
|
||
|
||
ADC1 agganciato a TIM2, valore passato attraverso TIM2_IRQHandler a TIM2->CCR1
|
||
ADC2 lanciato da software, quando completo adc2_done viene settato in HAL_ADC_ConvCpltCallback
|
||
|
||
*/
|
||
|
||
extern I2C_HandleTypeDef hi2c1;
|
||
|
||
// SYS
|
||
volatile uint8_t sys_tick, sys_tick_prescale;
|
||
|
||
// UART
|
||
uint8_t UART_RX_buf[UART_RX_BUF_SIZE];
|
||
|
||
uint8_t UART_TX_buf[UART_TX_BUF_SIZE];
|
||
uint8_t UART_TX_buf_lenght;
|
||
|
||
// SYNTH
|
||
uint32_t freq;
|
||
|
||
// ADC2 (16 o 32?) per usare buffer da 32 settare WORD nel canale DMA
|
||
uint32_t adc2_valori[4];
|
||
uint8_t adc2_done, blocco, blocco_fatto, codice_allarme;
|
||
|
||
|
||
// VU
|
||
uint8_t analog_wd_status;
|
||
|
||
// ###################################
|
||
|
||
int serial_write(char *ptr, size_t len) {
|
||
// todo sia dma che it corrompono (vedi sotto, static)
|
||
HAL_UART_Transmit_DMA(&huart1, ptr, len);
|
||
// HAL_UART_Transmit_IT(&huart1, ptr, len);
|
||
// HAL_UART_Transmit(&huart1, ptr, len, 1000);
|
||
// uart_sent = 0;
|
||
UART_TX_buf_lenght = 0;
|
||
return len;
|
||
}
|
||
|
||
void squeow_init(void) {
|
||
freq = DEFAULT_SYNTH_FREQUENCY;
|
||
blocco = 0;
|
||
adc2_valori[0] = 0;
|
||
adc2_valori[1] = 0;
|
||
adc2_valori[2] = 0;
|
||
adc2_valori[3] = 0;
|
||
codice_allarme = 0;
|
||
}
|
||
|
||
uint32_t sat_sub(uint16_t x, uint16_t y) {
|
||
uint16_t res = x - y;
|
||
res &= -(res <= x);
|
||
return res;
|
||
}
|
||
|
||
uint32_t ringbuf_increment(uint32_t *index, uint32_t buff_size_mask) {
|
||
(*index)++;
|
||
*index &= buff_size_mask;
|
||
return *index;
|
||
}
|
||
|
||
// adc
|
||
|
||
void adc_rileva_soglie(uint32_t *adc_valori) {
|
||
if (!blocco) {
|
||
#ifdef SQUEOW_SONDA_CORRENTE
|
||
if (adc_valori[SQUEOW_ADC_CORRENTE] > SOGLIA_CORRENTE) {
|
||
codice_allarme = SQUEOW_CODICE_CORRENTE;
|
||
blocco = 1;
|
||
}
|
||
#endif
|
||
#ifdef SQUEOW_SONDA_TEMPERATURA
|
||
if (adc_valori[SQUEOW_ADC_TEMPERATURA] > SOGLIA_TEMPERATURA) {
|
||
codice_allarme = SQUEOW_CODICE_TEMPERATURA;
|
||
blocco = 1;
|
||
}
|
||
#endif
|
||
#ifdef SQUEOW_SONDA_DIRETTA
|
||
if (adc_valori[SQUEOW_ADC_DIRETTA] > SOGLIA_DIRETTA) {
|
||
codice_allarme = SQUEOW_CODICE_DIRETTA;
|
||
blocco = 1;
|
||
}
|
||
#endif
|
||
#ifdef SQUEOW_SONDA_RIFLESSA
|
||
if (adc_valori[SQUEOW_ADC_RIFLESSA] > SOGLIA_RIFLESSA) {
|
||
codice_allarme = SQUEOW_CODICE_RIFLESSA;
|
||
blocco = 1;
|
||
}
|
||
#endif
|
||
}
|
||
}
|
||
|
||
void processa_blocco(void) {
|
||
static uint8_t vecchio_stato_blocco;
|
||
static uint16_t timer_blocco;
|
||
|
||
// è appena stato impostato blocco
|
||
if (blocco && !vecchio_stato_blocco) {
|
||
// è appena stato impostato blocco
|
||
#ifdef SQUEOW_UI_SERIOW
|
||
seriow_log('E', "LOCK!");
|
||
#endif
|
||
#ifdef SQUEOW_UI_TOSTA
|
||
tosta_log('E', "LOCK!");
|
||
#endif
|
||
#ifdef MODALITA_BLOCCO_PERMANENTE
|
||
|
||
#endif
|
||
#ifdef MODALITA_BLOCCO_BASSA_POTENZA
|
||
|
||
#endif
|
||
#ifdef MODALITA_BLOCCO_TEMPORANEO
|
||
timer_blocco = 0;
|
||
#endif
|
||
|
||
vecchio_stato_blocco = blocco;
|
||
} else if (!blocco && vecchio_stato_blocco) {
|
||
// è appena stato disinserito il blocco
|
||
#ifdef SQUEOW_UI_SERIOW
|
||
seriow_log('I', "UNLOCK");
|
||
#endif
|
||
#ifdef SQUEOW_UI_TOSTA
|
||
tosta_log('I', "UNLOCK");
|
||
#endif
|
||
codice_allarme = 0;
|
||
led_blocco(codice_allarme);
|
||
vecchio_stato_blocco = blocco;
|
||
}
|
||
|
||
#ifdef MODALITA_BLOCCO_TEMPORANEO
|
||
else {
|
||
// solo temporaneo
|
||
if (timer_blocco > TEMPO_BLOCCO_TEMPORANEO) {
|
||
timer_blocco = 0;
|
||
blocco = 0;
|
||
} else {
|
||
timer_blocco++;
|
||
}
|
||
}
|
||
#endif
|
||
}
|
||
|
||
// synth
|
||
|
||
void squeow_synth_init(void) {
|
||
// occhio che blocca!
|
||
#ifdef SQUEOW_UI_SERIOW
|
||
seriow_log('I', "synth init");
|
||
#endif
|
||
|
||
#ifdef SQUEOW_UI_TOSTA
|
||
tosta_log('I', "synth init");
|
||
#endif
|
||
si5351_initialize();
|
||
}
|
||
|
||
void squeow_synth_set(uint32_t freq) {
|
||
#ifdef SQUEOW_UI_SERIOW
|
||
// seriow_log('I', "synth set freq");
|
||
#endif
|
||
|
||
#ifdef SQUEOW_UI_TOSTA
|
||
// tosta_log('I', "synth set freq");
|
||
#endif
|
||
|
||
si5351_set_frequency(freq, 0);
|
||
si5351_set_frequency(freq, 1);
|
||
}
|
||
|
||
void squeow_synth_on(void) {
|
||
#ifdef SQUEOW_UI_SERIOW
|
||
seriow_log('I', "synth on");
|
||
#endif
|
||
|
||
#ifdef SQUEOW_UI_TOSTA
|
||
tosta_log('I', "synth on");
|
||
#endif
|
||
|
||
si5351_on_clk(0);
|
||
si5351_on_clk(1);
|
||
}
|
||
|
||
void squeow_synth_off(void) {
|
||
#ifdef SQUEOW_UI_SERIOW
|
||
#endif
|
||
|
||
#ifdef SQUEOW_UI_TOSTA
|
||
tosta_log('I', "synth off");
|
||
#endif
|
||
|
||
si5351_off_clk(0);
|
||
si5351_off_clk(1);
|
||
}
|
||
|
||
void i2c_write8(I2C_HandleTypeDef handler, uint8_t address, uint8_t reg, uint8_t value) {
|
||
while (HAL_I2C_IsDeviceReady(&handler, (uint16_t)(address << 1), 3, 100) != HAL_OK) {
|
||
}
|
||
HAL_I2C_Mem_Write(&handler, (uint8_t)(address << 1), (uint8_t)reg, I2C_MEMADD_SIZE_8BIT, (uint8_t *)(&value), 1, 100);
|
||
}
|
||
|
||
uint8_t i2c_read8(I2C_HandleTypeDef handler, uint8_t address, uint8_t reg, uint8_t *value) {
|
||
HAL_StatusTypeDef status = HAL_OK;
|
||
while (HAL_I2C_IsDeviceReady(&handler, (uint16_t)(address << 1), 3, 100) != HAL_OK) {
|
||
}
|
||
status = HAL_I2C_Mem_Read(&handler, // i2c handle
|
||
(uint8_t)(address << 1), // i2c address, left aligned
|
||
(uint8_t)reg, // register address
|
||
I2C_MEMADD_SIZE_8BIT, // 8bit register addresses
|
||
(uint8_t *)(&value), // write returned data to this variable
|
||
1, // how many bytes to expect returned
|
||
100); // timeout
|
||
return status;
|
||
}
|
||
|
||
void si5351_write8(uint8_t reg, uint8_t value) { i2c_write8(hi2c1, SI5351_ADDRESS, reg, value); }
|
||
|
||
uint8_t si5351_read8(uint8_t reg, uint8_t *value) { return i2c_read8(hi2c1, SI5351_ADDRESS, reg, value); }
|