add debugging messages

This commit is contained in:
King Kévin 2016-05-01 20:35:11 +02:00
parent d479695a69
commit fc9172a278
1 changed files with 36 additions and 34 deletions

70
main.c
View File

@ -56,8 +56,8 @@ volatile bool photoresistor_flag = false; /**< flag set when ambient luminosity
* @note I have to use type variables because defines would be stored in signed integers, leading to an overflow it later calculations
* @{
*/
/** the number of ticks in one second (greater than 256*LED_WS2812B_LEDS/60) */
#define TICKS_PER_SECOND SQUARE_WAVE_FREQUENCY/SQUARE_WAVE_TICKS
/** the number of ticks in one second (32768 divisor greater than 256*LED_WS2812B_LEDS/60) */
#define TICKS_PER_SECOND (RTC_DS1307_SQUARE_WAVE_FREQUENCY/RTC_DS1307_SQUARE_WAVE_TICKS)
/** number of ticks in one second */
const uint32_t ticks_second = TICKS_PER_SECOND;
/** number of ticks in one minute */
@ -349,15 +349,20 @@ error:
int main(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock
// setup LED
rcc_periph_clock_enable(LED_RCC); // enable clock for LED
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull'
led_off(); // switch off LED per default
// setup USART and USB for user communication
usart_setup(); // setup USART (for printing)
cdcacm_setup(); // setup USB CDC ACM (for printing)
setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print
setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print
// setup LED
rcc_periph_clock_enable(LED_RCC); // enable clock for LED
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull'
led_off(); // switch off LED to indicate setup started
// minimal setup ready
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
// setup button
#if defined(BUTTON_RCC) && defined(BUTTON_PORT) && defined(BUTTON_PIN) && defined(BUTTON_EXTI) && defined(BUTTON_IRQ)
@ -366,29 +371,30 @@ int main(void)
gpio_clear(BUTTON_PORT, BUTTON_PIN); // pull down to be able to detect button push (go high)
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
exti_select_source(BUTTON_EXTI, BUTTON_PORT); // mask external interrupt of this pin only for this port
exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_RISING); // trigger on both edge
exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_RISING); // trigger when button is pressed
exti_enable_request(BUTTON_EXTI); // enable external interrupt
nvic_enable_irq(BUTTON_IRQ); // enable interrupt
#endif
// setup internal RTC
// setup RTC
printf("setup RTC: ");
rtc_auto_awake(RCC_LSE, 32768/ticks_second-1); // ensure RTC is on, uses the 32.678 kHz LSE, and the prescale is set to our tick speed, else update backup registers accordingly (power off the micro-controller for the change to take effect)
// setup external RTC
rtc_setup(); // setup RTC module
// generate gamma correction table (with fixed gamma value)
for (uint16_t i=0; i<LENGTH(gamma_correction_lut); i++) {
gamma_correction_lut[i] = powf((float)i / (float)LENGTH(gamma_correction_lut), 2.2)*LENGTH(gamma_correction_lut);
}
rtc_ds1307_setup(); // setup external RTC module
printf("OK\n");
// setup WS2812b LEDs
printf("setup LEDs: ");
for (uint16_t i=0; i<LENGTH(gamma_correction_lut); i++) { // generate gamma correction table
gamma_correction_lut[i] = powf((float)i / (float)LENGTH(gamma_correction_lut), 2.2)*LENGTH(gamma_correction_lut); // calculate using fixed gamma value
}
led_ws2812b_setup(); // setup WS2812b LEDs
clock_clear(); // clear all LEDs
clock_leds_set(); // set the colors of all LEDs
led_ws2812b_transmit(); // transmit set color
printf("OK\n");
// setup ADC to photo-resistor voltage
printf("setup brightness sensor: ");
rcc_periph_clock_enable(PHOTORESISTOR_PORT_RCC); // enable clock for photo-resistor GPIO peripheral
gpio_set_mode(PHOTORESISTOR_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, PHOTORESISTOR_PIN); // set photo-resistor GPIO as analogue input for the ADC
rcc_periph_clock_enable(RCC_ADC1); // enable clock for ADC peripheral
@ -405,31 +411,27 @@ int main(void)
}
adc_reset_calibration(ADC1); // remove previous non-calibration
adc_calibration(ADC1); // calibrate ADC for less accuracy errors
// read internal reference 1.2V
uint8_t channels[] = {ADC_CHANNEL17}; // voltages to convert
adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channels to convert
adc_start_conversion_regular(ADC1); // start conversion to get first voltage of this group
while (!adc_eoc(ADC1)); // wait until conversion finished
uint16_t ref_value = adc_read_regular(ADC1); // read internal reference 1.2V voltage value
// now use interrupts to only measure ambient luminosity
channels[0] = PHOTORESISTOR_ADC_CHANNEL; // only measure ambient luminosity
adc_set_regular_sequence(ADC1, 1, channels); // set now group
adc_enable_eoc_interrupt(ADC1); // enable interrupt for end of conversion
nvic_enable_irq(NVIC_ADC1_2_IRQ); // enable ADC interrupts
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
led_on(); // switch on LED to indicate setup completed
printf("OK\n");
// verify is external RTC is running
if (rtc_oscillator_disabled()) {
if (rtc_ds1307_oscillator_disabled()) {
printf("/!\\ RTC oscillator is disabled: the battery may be empty\n");
}
// variables to measure the RTC drift
uint32_t rtc_internal_start = rtc_get_counter_val();
uint32_t rtc_external_start = rtc_ticks;
uint32_t rtc_external_start = rtc_ds1307_ticks;
// get date and time
printf("current time: %02lu:%02lu:%02lu\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // display time
@ -480,26 +482,26 @@ int main(void)
}
button_flag = false; // reset flag
}
while (rtc_tick_flag) { // the RTC tick four our counter passed
rtc_tick_flag = false; // reset flag
while (rtc_ds1307_tick_flag) { // the RTC tick four our counter passed
rtc_ds1307_tick_flag = false; // reset flag
action = true; // action has been performed
if ((rtc_ticks%(ticks_second/10))==0) { // one tenth of a second passed
if ((rtc_ds1307_ticks%(ticks_second/10))==0) { // one tenth of a second passed
adc_start_conversion_regular(ADC1); // start measuring ambient luminosity
}
if ((rtc_ticks%ticks_second)==0) { // one second passed
led_toggle(); // don't use the LED, this confuses the 32.768 kHz oscillator
if ((rtc_ds1307_ticks%ticks_second)==0) { // one second passed
//led_toggle(); // don't use the LED, this confuses the 32.768 kHz oscillator
}
if ((rtc_ticks%ticks_minute)==0) { // one minute passed
if ((rtc_ds1307_ticks%ticks_minute)==0) { // one minute passed
printf("%02lu:%02lu:%02lu\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // display time
printf("internal RTC drift: %f%%\n", 1-((double)(rtc_ticks-rtc_external_start))/(double)(rtc_get_counter_val()-rtc_internal_start));
printf("internal RTC drift: %f%%\n", 1-((double)(rtc_ds1307_ticks-rtc_external_start))/(double)(rtc_get_counter_val()-rtc_internal_start));
}
if ((rtc_ticks%ticks_hour)==0) { // one hours passed
if ((rtc_ds1307_ticks%ticks_hour)==0) { // one hours passed
clock_hours(); // show hour markers
}
if (rtc_ticks>=ticks_midday*2) { // one day passed
if (rtc_ds1307_ticks>=ticks_midday*2) { // one day passed
rtc_set_counter_val(rtc_get_counter_val()%ticks_midday); // reset time counter
}
clock_set_time(rtc_ticks); // set time
clock_set_time(rtc_ds1307_ticks); // set time
}
while (photoresistor_flag) { // new photo-resistor value has been measured
photoresistor_flag = false; // reset flag
@ -527,7 +529,7 @@ int main(void)
}
#if defined(BUTTON_ISR) && defined(BUTTON_EXTI)
/** @brief interrupt service routine called when button is pressed of released */
/** @brief interrupt service routine called when button is pressed */
void BUTTON_ISR(void)
{
exti_reset_request(BUTTON_EXTI); // reset interrupt