UI led
This commit is contained in:
@@ -9,56 +9,58 @@
|
||||
|
||||
/* SQUEOW
|
||||
|
||||
TIM3 eventi 98304000/(49152×200) 10hz
|
||||
TIM2 PWM 98304000/2048 48khz
|
||||
TIM2 PWM 168000000/4096 = 41015,625 Hz
|
||||
TIM3 sys_tick 168000000/(16800×100) 100hz
|
||||
|
||||
risoluzione PWM 4*2048 -> 8192 (13bit)
|
||||
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
|
||||
uint8_t sys_tick, sys_tick_prescale, pwm_tick;
|
||||
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;
|
||||
|
||||
// ADC1
|
||||
uint16_t adc1_valore;
|
||||
// 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;
|
||||
|
||||
// ADC2
|
||||
uint16_t adc2_valori[4];
|
||||
uint8_t adc2_done, adc_blocco, adc_allarmi[4];
|
||||
|
||||
// audio
|
||||
uint16_t sample_value;
|
||||
uint8_t stato_audio;
|
||||
double sine_increment;
|
||||
uint16_t samples_ringbuf[SAMPLES_BUFFER_SIZE]; ///< buffer ad anello dei dati RX
|
||||
uint32_t samples_ringbuf_input_index, samples_ringbuf_output_index;
|
||||
uint8_t usb_audio_tick;
|
||||
|
||||
// VU
|
||||
uint8_t analog_wd_status;
|
||||
|
||||
// MOD
|
||||
uint16_t pwm_value1, pwm_value2, pwm_value3, pwm_value4;
|
||||
uint8_t rails_number;
|
||||
|
||||
// ###################################
|
||||
|
||||
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) {
|
||||
seriow_log(2, "squeow init");
|
||||
sine_increment = 0.4;
|
||||
samples_ringbuf_input_index = 0;
|
||||
samples_ringbuf_output_index = 0;
|
||||
freq = DEFAULT_SYNTH_FREQUENCY;
|
||||
adc2_valori[0] = 10;
|
||||
adc2_valori[1] = 20;
|
||||
adc2_valori[2] = 30;
|
||||
adc2_valori[3] = 40;
|
||||
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) {
|
||||
@@ -67,121 +69,165 @@ uint32_t sat_sub(uint16_t x, uint16_t y) {
|
||||
return res;
|
||||
}
|
||||
|
||||
uint16_t u12_sine(void) {
|
||||
static double angle;
|
||||
angle += sine_increment;
|
||||
if (angle >= 6.28)
|
||||
angle = 0;
|
||||
return (uint16_t)((sin(angle) * 0x7ff) + 0x7ff);
|
||||
}
|
||||
|
||||
/*
|
||||
uint16_t get_adc_sample(void) {
|
||||
uint16_t adc_sample_value;
|
||||
// stato_audio == STATO_AUDIO_ADC;
|
||||
HAL_ADC_Start(&hadc1);
|
||||
if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
|
||||
// store_sample(HAL_ADC_GetValue(&hadc1) << 4);
|
||||
adc_sample_value = HAL_ADC_GetValue(&hadc1);
|
||||
}
|
||||
HAL_ADC_Stop(&hadc1);
|
||||
return adc_sample_value;
|
||||
}
|
||||
*/
|
||||
|
||||
void store_samples(uint16_t *data, uint32_t size) {
|
||||
for (uint32_t i = 0; i < size; ++i) {
|
||||
store_sample(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void store_sample(uint16_t sample) {
|
||||
samples_ringbuf[samples_ringbuf_input_index] = sample;
|
||||
ringbuf_increment(&samples_ringbuf_input_index, SAMPLES_BUFFER_SIZE_MASK);
|
||||
}
|
||||
|
||||
uint16_t get_sample(void) {
|
||||
ringbuf_increment(&samples_ringbuf_output_index, SAMPLES_BUFFER_SIZE_MASK);
|
||||
return samples_ringbuf[samples_ringbuf_output_index];
|
||||
}
|
||||
|
||||
uint32_t ringbuf_increment(uint32_t *index, uint32_t buff_size_mask) {
|
||||
(*index)++;
|
||||
*index &= buff_size_mask;
|
||||
return *index;
|
||||
}
|
||||
|
||||
void store_buffer(uint8_t *buf, uint32_t size) {
|
||||
for (uint32_t s = 0; s < size / 4; s++) {
|
||||
uint16_t LL = buf[s * 2];
|
||||
uint16_t RR = buf[(s * 2) + 1];
|
||||
store_sample(RR);
|
||||
}
|
||||
}
|
||||
|
||||
int vu_meter(uint16_t sample) {
|
||||
#define ZEROD 2047
|
||||
#define ZEROU 2048
|
||||
uint16_t abs_sample;
|
||||
uint8_t scaled_abs_sample;
|
||||
abs_sample = (sample > ZEROD) ? sample - ZEROU : ZEROD - sample;
|
||||
scaled_abs_sample = abs_sample >> 3;
|
||||
|
||||
if (scaled_abs_sample >= vu_tmp_value)
|
||||
return scaled_abs_sample;
|
||||
else {
|
||||
return vu_tmp_value;
|
||||
}
|
||||
}
|
||||
|
||||
// adc
|
||||
|
||||
void adc_rileva_soglie(uint16_t *adc_valori) {
|
||||
if (adc_valori[0] > SOGLIA_TEMPERATURA) {
|
||||
seriow_log(1, "ADC0 threshold detect");
|
||||
HAL_GPIO_WritePin(TEMP_OL_GPIO_Port, TEMP_OL_Pin, 1);
|
||||
adc_allarmi[0] = 1;
|
||||
adc_blocco = 1;
|
||||
}
|
||||
if (adc_valori[1] > SOGLIA_CORRENTE) {
|
||||
seriow_log(1, "ADC1 threshold detect");
|
||||
adc_allarmi[1] = 1;
|
||||
adc_blocco = 1;
|
||||
}
|
||||
if (adc_valori[3] > SOGLIA_RIFLESSA) {
|
||||
seriow_log(1, "ADC3 threshold detect");
|
||||
HAL_GPIO_WritePin(REFL_OL_GPIO_Port, REFL_OL_Pin, 1);
|
||||
adc_allarmi[3] = 1;
|
||||
adc_blocco = 1;
|
||||
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 array_values_mean(uint16_t *valori, uint16_t *medie, uint8_t dimensione){
|
||||
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!
|
||||
seriow_log(2, "synth init");
|
||||
// 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(freq) {
|
||||
seriow_log(2, "synth set freq");
|
||||
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) {
|
||||
seriow_log(2, "synth on");
|
||||
#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) {
|
||||
seriow_log(2, "synth off");
|
||||
#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); }
|
||||
|
||||
Reference in New Issue
Block a user