예제 #1
0
void data_received(unsigned char src_address, unsigned char dst_address, unsigned char command, char* data, unsigned char size)
{
	if (command == CLUNET_COMMAND_TIME && size >= 6) // Синхронизация времени
	{
		set_time(data[3]+1900, data[4], data[5], data[0],data[1],data[2]);
	}
	else if (command == CLUNET_COMMAND_INTERCOM_MODE_REQUEST) // У нас запрашивают режим
	{
		send_current_mode(src_address);
	}
	else if (command == CLUNET_COMMAND_INTERCOM_MODE_SET) // Установка режима
	{
		if (size >= 1 && (unsigned char)data[0] != 0xFF) mode_current = data[0];
		if (size >= 2 && (unsigned char)data[1] != 0xFF) mode_temp = data[1];
		save_mode();
	}	
	else if (command == CLUNET_COMMAND_INTERCOM_RECORD_REQUEST) // Передача записей
	{
		if (size == 4) transfer_start(*((unsigned long int*)data), src_address); // Начинаем передачу
		else if (size == 1 && data[0] == 1) transfer_ack(); // Подтвержают приём, переходим к следующему куску
		else if (size == 1 && data[0] == 0) transfer_stop(); // Прекращение передачи данных
	}
}
int gestic_message_write(gestic_t *gestic, void *msg, int size)
{
    I2C_MODULE port = gestic->io.I2cPort;
    const unsigned char * const buffer = (unsigned char*)msg;
    int result = 0;
    int i;

    GESTIC_ASSERT(buffer[0] == size);

    if(transfer_start(port, gestic->io.I2cSlaveAddr, 0))
        result = -1;

    for(i = 0; !result && i < size; ++i) {
        if(transmit_byte(port, buffer[i]) < 0)
            result = -1;
        
        if(!I2CByteWasAcknowledged(port))
            result = -1;
    }

    transfer_stop(port);

    return result;
}
void* message_read(gestic_t *gestic, int *size)
{
    UINT8 RcvCount = 0;
    UINT8 writeIndex = 0;
    I2C_MODULE port = gestic->io.I2cPort;
    unsigned char *result = gestic->io.read_buffer;

    // Get TSLine
    if(gestic->io.device(GestICDev_TsLine_Get) == 1)
        result = NULL;

    if(result)
        gestic->io.device(GestICDev_TsLine_Assert);

    if(result && transfer_start(port, gestic->io.I2cSlaveAddr, 1))
        result = NULL;

    // Read the data from the desired address
    while(result) {
        UINT8 i2cbyte;

        /* Check for buffer overflow */
        if(I2CReceiverEnable(port, TRUE) == I2C_RECEIVE_OVERFLOW) {
            result = NULL;
            break;
        }

        while(!I2CReceivedDataIsAvailable(port));

        i2cbyte = I2CGetByte(gestic->io.I2cPort);
        result[writeIndex] = i2cbyte;
        writeIndex++;

        if(RcvCount == 0) {
            /* Set size if message fits into buffer */
            if(i2cbyte < sizeof(gestic->io.read_buffer))
                RcvCount = i2cbyte;
            else
                result = NULL;
        }

        /* In case of length == 0 or last byte (length == 1) send a NACK */
        if(RcvCount == 0 || RcvCount == 1)
            I2CAcknowledgeByte(port, FALSE);
        else
            I2CAcknowledgeByte(port, TRUE);

        while(!I2CAcknowledgeHasCompleted( gestic->io.I2cPort ));

        RcvCount--;
        if(RcvCount <= 0)
            break;
    }

    gestic->io.device(GestICDev_TsLine_Release);

    transfer_stop(gestic->io.I2cPort);
    
    if(result && size)
        *size = result[0];

    return result;
}