components/ds3234/DS3234.cpp
/**
 * Run task for this instance.
 */
Run task for this instance.
Run task for this instance .
void DS3234::run()
{
    ESP_LOGD(TAG, "run, this:%p", this);
    spi_device_interface_config_t devcfg;
    memset(&devcfg, 0, sizeof(spi_device_interface_config_t));
    devcfg.command_bits = 8;
    devcfg.mode = 3;
    devcfg.clock_speed_hz = 1000000;
    devcfg.spics_io_num = -1;
    devcfg.flags = SPI_DEVICE_HALFDUPLEX;
    devcfg.dummy_bits = 0;
    devcfg.queue_size = 1;
    esp_err_t ret = spi_bus_add_device(host_id, &devcfg, &device_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "run, spi_bus_add_device (READ) failed (FATAL)");
        return;
    }
    gpio_config_t io_conf;
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.pin_bit_mask = (1ULL << cs_pin);
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    ret = gpio_config(&io_conf);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "run, gpio_config failed:%d (FATAL)", ret);
        return;
    }
    gpio_set_drive_capability(cs_pin, GPIO_DRIVE_CAP_0);
    gpio_set_level(cs_pin, 1);
    if (self_test() == false) {
        ESP_LOGE(TAG, "run, self test failed (FATAL)");
        return;
    }
    uint8_t raw[] = { 0, 0, 0, 0, 0, 0, 0 };
    time_t time = -1;
    pubsub_message_t message;
    while (true) {
        if (xQueueReceive(time_queue, &message, DS3432_LOOK_INTERVAL_MS / portTICK_PERIOD_MS)) {
            if (message.int_val < TM_MINIMUM) {
                time = TM_MINIMUM;
            }
            if (time != message.int_val) {
                ESP_LOGD(TAG, "run, set time");
                encode_time(message.int_val, raw);
                write_data(TIME_REG, raw, 7);
            }
        } else {
            ESP_LOGD(TAG, "run, read time");
            read_data(TIME_REG, raw, 7);
            time = decode_time(raw);
            pubsub_publish_int(timestamp_topic, time);
        }
    };
}
void DS3234 :: run ( ) { ESP_LOGD ( TAG , " " , this ) ; spi_device_interface_config_t devcfg ; memset ( & devcfg , 0 , sizeof ( spi_device_interface_config_t ) ) ; devcfg . command_bits = 8 ; devcfg . mode = 3 ; devcfg . clock_speed_hz = 1000000 ; devcfg . spics_io_num = -1 ; devcfg . flags = SPI_DEVICE_HALFDUPLEX ; devcfg . dummy_bits = 0 ; devcfg . queue_size = 1 ; esp_err_t ret = spi_bus_add_device ( host_id , & devcfg , & device_handle ) ; if ( ret != ESP_OK ) { ESP_LOGE ( TAG , " " ) ; return ; } gpio_config_t io_conf ; io_conf . intr_type = GPIO_INTR_DISABLE ; io_conf . pin_bit_mask = ( 1ULL << cs_pin ) ; io_conf . mode = GPIO_MODE_OUTPUT ; io_conf . pull_down_en = GPIO_PULLDOWN_DISABLE ; io_conf . pull_up_en = GPIO_PULLUP_DISABLE ; ret = gpio_config ( & io_conf ) ; if ( ret != ESP_OK ) { ESP_LOGE ( TAG , " " , ret ) ; return ; } gpio_set_drive_capability ( cs_pin , GPIO_DRIVE_CAP_0 ) ; gpio_set_level ( cs_pin , 1 ) ; if ( self_test ( ) == false ) { ESP_LOGE ( TAG , " " ) ; return ; } uint8_t raw [ ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; time_t time = -1 ; pubsub_message_t message ; while ( true ) { if ( xQueueReceive ( time_queue , & message , DS3432_LOOK_INTERVAL_MS / portTICK_PERIOD_MS ) ) { if ( message . int_val < TM_MINIMUM ) { time = TM_MINIMUM ; } if ( time != message . int_val ) { ESP_LOGD ( TAG , " " ) ; encode_time ( message . int_val , raw ) ; write_data ( TIME_REG , raw , 7 ) ; } } else { ESP_LOGD ( TAG , " " ) ; read_data ( TIME_REG , raw , 7 ) ; time = decode_time ( raw ) ; pubsub_publish_int ( timestamp_topic , time ) ; } } ; }
Run task for this instance.
Run task for this instance .
// configure spi device (READ) // manually drive CS to create extra time before first CLK signal // DS3234 determines SPI mode depending on level at CS start. // periodic listen for set time changes and publish time updates // truncate incoming date // ignore own publish action