コード例 #1
0
int HAL_Feature_Set(HAL_Feature feature, bool enabled)
{
    switch (feature)
    {
        case FEATURE_RETAINED_MEMORY:
        {
            FunctionalState state = enabled ? ENABLE : DISABLE;
            // Switch on backup SRAM clock
            // Switch on backup power regulator, so that it survives the deep sleep mode,
            // software and hardware reset. Power must be supplied to VIN or VBAT to retain SRAM values.
            PWR_BackupRegulatorCmd(state);
            // Wait until backup power regulator is ready, should be fairly instantaneous... but timeout in 10ms.
            if (state == ENABLE) {
                system_tick_t start = HAL_Timer_Get_Milli_Seconds();
                while (PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET) {
                    if (HAL_Timer_Get_Milli_Seconds() - start > 10UL) {
                        return -2;
                    }
                };
            }
            return 0;
        }

    }
    return -1;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: RobertNewkirk/particle
// this is called on multiple threads - ideally need a mutex
void HAL_Notify_Button_State(uint8_t button, uint8_t pressed)
{
    if (button==0)
    {
        if (pressed)
        {
            wasListeningOnButtonPress = network.listening();
            buttonPushed = HAL_Timer_Get_Milli_Seconds();
            if (!wasListeningOnButtonPress)             // start of button press
            {
                system_notify_event(button_status, 0);
            }
        }
        else
        {
            int release_time = HAL_Timer_Get_Milli_Seconds();
            uint16_t duration = release_time-buttonPushed;

            if (!network.listening()) {
                system_notify_event(button_status, duration);
                handle_button_click(duration, release_time);
            }
            buttonPushed = 0;
            if (duration>3000 && duration<8000 && wasListeningOnButtonPress && network.listening())
                network.listen(true);
        }
    }
}
コード例 #3
0
ファイル: wlan_hal.c プロジェクト: spark/firmware
int wlan_connected_rssi()
{
    int _returnValue = 0;

    system_tick_t _functionStart = HAL_Timer_Get_Milli_Seconds();
    while ((HAL_Timer_Get_Milli_Seconds() - _functionStart) < 1000) {
        tNetappIpconfigRetArgs config;
        netapp_ipconfig((void*)&config);
        int l;
        for (l=0; l<16; l++)
        {
            char wlan_scan_results_table[50];
            if(wlan_ioctl_get_scan_results(0, (unsigned char*)wlan_scan_results_table) != 0) {
                _returnValue = 1;
                break;
            }
            if (wlan_scan_results_table[0] == 0)
                break;
            if (!strcmp(wlan_scan_results_table+12, config.uaSSID)) {
                _returnValue = ((wlan_scan_results_table[8] >> 1) - 127);
                break;
            }
        }
        if (_returnValue != 0) {
            break;
        }
    }
コード例 #4
0
ファイル: logging.cpp プロジェクト: gruvin/core-firmware
void log_message_v(int level, const char *category, LogAttributes *attr, void *reserved, const char *fmt, va_list args) {
    const log_message_callback_type msg_callback = log_msg_callback;
    if (!msg_callback && (!log_compat_callback || level < log_compat_level)) {
        return;
    }
    // Set default attributes
    if (!attr->has_time) {
        LOG_ATTR_SET(*attr, time, HAL_Timer_Get_Milli_Seconds());
    }
    char buf[LOG_MAX_STRING_LENGTH];
    if (msg_callback) {
        const int n = vsnprintf(buf, sizeof(buf), fmt, args);
        if (n > (int)sizeof(buf) - 1) {
            buf[sizeof(buf) - 2] = '~';
        }
        msg_callback(buf, level, category, attr, 0);
    } else {
        // Using compatibility callback
        const char* const levelName = log_level_name(level, 0);
        int n = 0;
        if (attr->has_file && attr->has_line && attr->has_function) {
            n = snprintf(buf, sizeof(buf), "%010u %s:%d, %s: %s", (unsigned)attr->time, attr->file, attr->line,
                    attr->function, levelName);
        } else {
            n = snprintf(buf, sizeof(buf), "%010u %s", (unsigned)attr->time, levelName);
        }
        if (n > (int)sizeof(buf) - 1) {
            buf[sizeof(buf) - 2] = '~';
        }
        log_compat_callback(buf);
        log_compat_callback(": ");
        n = vsnprintf(buf, sizeof(buf), fmt, args);
        if (n > (int)sizeof(buf) - 1) {
            buf[sizeof(buf) - 2] = '~';
        }
        log_compat_callback(buf);
        log_compat_callback("\r\n");
    }
}
コード例 #5
0
ファイル: active_object.cpp プロジェクト: spark/firmware
void ActiveObjectBase::run()
{
    /* XXX: We shouldn't constantly hold a mutex. This breaks priority inhertiance mechanisms in FreeRTOS. */
    /* It's not even used anywhere */
    // std::lock_guard<std::mutex> lck (_start);
    started = true;

    uint32_t last_background_run = 0;
    for (;;)
    {
    	uint32_t now;
        if (!process())
		{
        	configuration.background_task();
        }
        else if ((now=HAL_Timer_Get_Milli_Seconds())-last_background_run > configuration.take_wait)
        {
        	last_background_run = now;
        	configuration.background_task();
        }
    }
}
コード例 #6
0
ファイル: i2c_hal.c プロジェクト: RobertNewkirk/particle
uint8_t HAL_I2C_End_Transmission(HAL_I2C_Interface i2c, uint8_t stop, void* reserved)
{
    uint32_t _millis;

    _millis = HAL_Timer_Get_Milli_Seconds();
    /* While the I2C Bus is busy */
    while(I2C_GetFlagStatus(i2cMap[i2c]->I2C_Peripheral, I2C_FLAG_BUSY))
    {
        if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
        {
            /* SW Reset the I2C Peripheral */
            HAL_I2C_SoftwareReset(i2c);

            return 1;
        }
    }

    /* Send START condition */
    I2C_GenerateSTART(i2cMap[i2c]->I2C_Peripheral, ENABLE);

    _millis = HAL_Timer_Get_Milli_Seconds();
    while(!I2C_CheckEvent(i2cMap[i2c]->I2C_Peripheral, I2C_EVENT_MASTER_MODE_SELECT))
    {
        if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
        {
            /* SW Reset the I2C Peripheral */
            HAL_I2C_SoftwareReset(i2c);

            return 2;
        }
    }

    /* Ensure ackFailure flag is cleared */
    i2cMap[i2c]->ackFailure = false;

    /* Send Slave address for write */
    I2C_Send7bitAddress(i2cMap[i2c]->I2C_Peripheral, i2cMap[i2c]->txAddress, I2C_Direction_Transmitter);

    _millis = HAL_Timer_Get_Milli_Seconds();
    while(!I2C_CheckEvent(i2cMap[i2c]->I2C_Peripheral, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
    {
        /* STOP/RESET immediately if ACK failure detected */
        if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis) || i2cMap[i2c]->ackFailure)
        {
            /* Send STOP Condition */
            I2C_GenerateSTOP(i2cMap[i2c]->I2C_Peripheral, ENABLE);

            /* Wait to make sure that STOP control bit has been cleared */
            _millis = HAL_Timer_Get_Milli_Seconds();
            while(i2cMap[i2c]->I2C_Peripheral->CR1 & I2C_CR1_STOP)
            {
                if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
                {
                    break;
                }
            }

            /* SW Reset the I2C Peripheral */
            HAL_I2C_SoftwareReset(i2c);

            /* Ensure ackFailure flag is cleared */
            i2cMap[i2c]->ackFailure = false;

            return 3;
        }
    }

    uint8_t *pBuffer = i2cMap[i2c]->txBuffer;
    uint8_t NumByteToWrite = i2cMap[i2c]->txBufferLength;

    /* While there is data to be written */
    while(NumByteToWrite--)
    {
        /* Send the current byte to slave */
        I2C_SendData(i2cMap[i2c]->I2C_Peripheral, *pBuffer);

        /* Point to the next byte to be written */
        pBuffer++;

        _millis = HAL_Timer_Get_Milli_Seconds();
        while(!I2C_CheckEvent(i2cMap[i2c]->I2C_Peripheral, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
        {
            if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
            {
                /* SW Reset the I2C Peripheral */
                HAL_I2C_SoftwareReset(i2c);

                return 4;
            }
        }

        _millis = HAL_Timer_Get_Milli_Seconds();
        while(I2C_GetFlagStatus(i2cMap[i2c]->I2C_Peripheral, I2C_FLAG_BTF) == RESET)
        {
            if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
            {
                /* SW Reset the I2C Peripheral */
                HAL_I2C_SoftwareReset(i2c);

                return 5;
            }
        }
    }

    /* Send STOP Condition */
    if(stop == true)
    {
        /* Send STOP condition */
        I2C_GenerateSTOP(i2cMap[i2c]->I2C_Peripheral, ENABLE);
    }

    // reset tx buffer iterator vars
    i2cMap[i2c]->txBufferIndex = 0;
    i2cMap[i2c]->txBufferLength = 0;

    // indicate that we are done transmitting
    i2cMap[i2c]->transmitting = 0;

    return 0;
}
コード例 #7
0
ファイル: i2c_hal.c プロジェクト: RobertNewkirk/particle
uint32_t HAL_I2C_Request_Data(HAL_I2C_Interface i2c, uint8_t address, uint8_t quantity, uint8_t stop, void* reserved)
{
    uint32_t _millis;
    uint8_t bytesRead = 0;

    // clamp to buffer length
    if(quantity > BUFFER_LENGTH)
    {
        quantity = BUFFER_LENGTH;
    }

    /* Send START condition */
    I2C_GenerateSTART(i2cMap[i2c]->I2C_Peripheral, ENABLE);

    _millis = HAL_Timer_Get_Milli_Seconds();
    while(!I2C_CheckEvent(i2cMap[i2c]->I2C_Peripheral, I2C_EVENT_MASTER_MODE_SELECT))
    {
        if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
        {
            /* SW Reset the I2C Peripheral */
            HAL_I2C_SoftwareReset(i2c);

            return 0;
        }
    }

    /* Initialized variables here to minimize delays
     * between sending of slave addr and read loop
     */
    uint8_t *pBuffer = i2cMap[i2c]->rxBuffer;
    uint8_t numByteToRead = quantity;

    /* Ensure ackFailure flag is cleared */
    i2cMap[i2c]->ackFailure = false;

    /* Set ACK/NACK prior to I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED check
     * to minimize delays between sending of slave addr and read loop.
     * I2C_CheckEvent() will clear ADDR bit and allow SCL clocks to run, so we don't
     * have much time to check/set this correctly while I2C is in operation.
     */
    if (quantity == 1)
        /* Disable Acknowledgement */
        I2C_AcknowledgeConfig(i2cMap[i2c]->I2C_Peripheral, DISABLE);
    else
        I2C_AcknowledgeConfig(i2cMap[i2c]->I2C_Peripheral, ENABLE);

    /* Send Slave address for read */
    I2C_Send7bitAddress(i2cMap[i2c]->I2C_Peripheral, address << 1, I2C_Direction_Receiver);

    _millis = HAL_Timer_Get_Milli_Seconds();
    while(!I2C_CheckEvent(i2cMap[i2c]->I2C_Peripheral, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
    {
        /* STOP/RESET immediately if ACK failure detected */
        if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis) || i2cMap[i2c]->ackFailure)
        {
            /* Send STOP Condition */
            I2C_GenerateSTOP(i2cMap[i2c]->I2C_Peripheral, ENABLE);

            /* Wait to make sure that STOP control bit has been cleared */
            _millis = HAL_Timer_Get_Milli_Seconds();
            while(i2cMap[i2c]->I2C_Peripheral->CR1 & I2C_CR1_STOP)
            {
                if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
                {
                    break;
                }
            }

            /* SW Reset the I2C Peripheral */
            HAL_I2C_SoftwareReset(i2c);

            /* Ensure ackFailure flag is cleared */
            i2cMap[i2c]->ackFailure = false;

            return 0;
        }
    }

    /* DATA on SDA --> Shift Register (SR) [8 clocks by SCL] --> Data Register (DR) --> ACK/NACK (9th clock)
     *
     * SINGLE BYTE READ
     *  - SCL will run immediately after ADDR bit is cleared by reading SR2 (any I2C_CheckEvent)
     *  - DR will be empty
     *  - 1st byte from slave will be transferred to DR after 8th SCL clock
     *  - ACK/NACK will be sent immediately (SCL will run 9th clock)
     *
     * 2 BYTES LEFT TO READ
     *  - DR is not yet empty (2nd last byte is in DR; it was ACKed on its xfer to DR)
     *  - Last byte is being assembled in SR
     *  - SCL will be stretched after 8th clock (SR full) until DR is empty
     *  - Reading DR for 2nd last byte will then move last byte to DR and send ACK/NACK
     *
     * While there is data to be read, perform blocking read into buffer
     */
    while(numByteToRead)
    {
        /* Wait for DR to be full. As soon as DR is full, the byte that was xfer'd from SR
         * to DR will be ACK/NACK'd. So it is important to enable/disable ACK bit ahead of this event
         */
        _millis = HAL_Timer_Get_Milli_Seconds();
        while(I2C_GetFlagStatus(i2cMap[i2c]->I2C_Peripheral, I2C_FLAG_RXNE) == RESET)
        {
            if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
            {
                /* SW Reset the I2C Peripheral */
                HAL_I2C_SoftwareReset(i2c);

                return 0;
            }
        }

        switch  (numByteToRead) {
            case 2:
                /* Disable Acknowledgement on last byte which is being assembled in SR,
                 * 2nd last byte is in the DR and as soon as we read the 2nd last byte
                 * below, last byte will be moved into DR and ACK/NACK will be sent.
                 */
                I2C_AcknowledgeConfig(i2cMap[i2c]->I2C_Peripheral, DISABLE);
                break;

            case 1:
                /* Send STOP Condition */
                if (stop == true)
                        I2C_GenerateSTOP(i2cMap[i2c]->I2C_Peripheral, ENABLE);
                break;

            default:
                break;
        }

        /* Read the byte out of the Data Register that was received from the Slave.
         * This will enable the transfer of the next byte (if any) from the Shift Register to
         * the Data Register and also will send ACK/NACK for that byte
         */
        *pBuffer = I2C_ReceiveData(i2cMap[i2c]->I2C_Peripheral);

        bytesRead++;

        /* Point to the next location where the byte read will be saved */
        pBuffer++;

        /* Decrement the read bytes counter */
        numByteToRead--;

        /* Wait to make sure that STOP control bit has been cleared */
        _millis = HAL_Timer_Get_Milli_Seconds();
        while(i2cMap[i2c]->I2C_Peripheral->CR1 & I2C_CR1_STOP)
        {
            if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
            {
                /* SW Reset the I2C Peripheral */
                HAL_I2C_SoftwareReset(i2c);

                return 0;
            }
        }
    }

    // set rx buffer iterator vars
    i2cMap[i2c]->rxBufferIndex = 0;
    i2cMap[i2c]->rxBufferLength = bytesRead;

    return bytesRead;
}
コード例 #8
0
extern "C" uint32_t HAL_Timer_Get_Micro_Seconds()
{
	return HAL_Timer_Get_Milli_Seconds()*1000;
}
コード例 #9
0
ファイル: main.cpp プロジェクト: RobertNewkirk/particle
uint16_t system_button_pushed_duration(uint8_t button, void*)
{
    if (button || network.listening())
        return 0;
    return buttonPushed ? HAL_Timer_Get_Milli_Seconds()-buttonPushed : 0;
}