Exemplo n.º 1
0
void get_send_timeout_count(const ComType com, const GetSendTimeoutCount *data) {
#ifdef BRICK_CAN_BE_MASTER
	if(data->communication_method > COM_WIFI2) {
#else
	if(data->communication_method > COM_SPI_STACK) {
#endif
		com_return_error(data, sizeof(GetSendTimeoutCountReturn), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		logblete("Communication Method %d does not exist (get_send_timeout_count)\n\r", data->communication_method);
		return;
	}

	GetSendTimeoutCountReturn gtcr;

	gtcr.header        = data->header;
	gtcr.header.length = sizeof(GetSendTimeoutCountReturn);
	gtcr.timeout_count = com_timeout_count[data->communication_method];

	send_blocking_with_timeout(&gtcr, sizeof(GetSendTimeoutCountReturn), com);
}

#ifdef BRICK_HAS_CO_MCU_SUPPORT
void set_spitfp_baudrate(const ComType com, const SetSPITFPBaudrate *data) {
	uint8_t port = tolower((uint8_t)data->bricklet_port) - 'a';
	if(port >= BRICKLET_NUM) {
		com_return_error(data, sizeof(GetSPITFPErrorCountReturn), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		logblete("Bricklet Port %d does not exist (set_spitfp_baudrate)\n\r", port);
		return;
	}

	if(bricklet_attached[port] == BRICKLET_INIT_CO_MCU) {
		bricklet_spitfp_baudrate[port] = BETWEEN(CO_MCU_MINIMUM_BAUDRATE, data->baudrate, CO_MCU_MAXIMUM_BAUDRATE);
	}

	com_return_setter(com, data);
}
Exemplo n.º 2
0
bool com_route_message_brick(const char *data, const uint16_t length, const ComType com) {
	com_info.current = com;

	MessageHeader *header = (MessageHeader*)data;
	if(header->uid == 0) {
		const ComMessage *com_message = get_com_from_header(header);
		if(com_message != NULL && com_message->reply_func != NULL) {
			com_message->reply_func(com, (void*)data);
		}

#ifdef BRICK_HAS_CO_MCU_SUPPORT
		for(uint8_t i = 0; i < BRICKLET_NUM; i++) {
			if(bricklet_attached[i] == BRICKLET_INIT_CO_MCU) {
				bricklet_co_mcu_send(i, (void*)data, length);
			}
		}
#endif

		return false;
	} else if(header->uid == com_info.uid) {
		const ComMessage *com_message = get_com_from_header(header);
		if(com_message != NULL && com_message->reply_func != NULL) {
			com_message->reply_func(com, (void*)data);
			return true;
		} else {
			com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_NOT_SUPPORTED, com);
		}

		return false;
	}

	for(uint8_t i = 0; i < BRICKLET_NUM; i++) {
		if(bs[i].uid == header->uid) {
#ifdef BRICK_HAS_CO_MCU_SUPPORT
			if(bricklet_attached[i] == BRICKLET_INIT_CO_MCU) {
				bricklet_co_mcu_send(i, (void*)data, length);
			} else 
#endif
				
			if(header->fid == FID_GET_IDENTITY) {
				const ComMessage *com_message = get_com_from_header(header);
				if(com_message != NULL && com_message->reply_func != NULL) {
					com_message->reply_func(com, (void*)data);
					return true;
				} else {
					com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_NOT_SUPPORTED, com);
				}
			} else {
				baddr[i].entry(BRICKLET_TYPE_INVOCATION, com, (void*)data);
				return true;
			}
		}
	}

	return false;
}
Exemplo n.º 3
0
void get_spitfp_error_count(const ComType com, const GetSPITFPErrorCount *data) {
	uint8_t port = tolower((uint8_t)data->bricklet_port) - 'a';
	if(port >= BRICKLET_NUM) {
		com_return_error(data, sizeof(GetSPITFPErrorCountReturn), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		logblete("Bricklet Port %d does not exist (get_spitfp_error_count)\n\r", port);
		return;
	}

	GetSPITFPErrorCountReturn gsecr;

	gsecr.header                           = data->header;
	gsecr.header.length                    = sizeof(GetSPITFPErrorCountReturn);
	if(bricklet_attached[port] == BRICKLET_INIT_CO_MCU) {
		gsecr.error_count_ack_checksum     = CO_MCU_DATA(port)->error_count.error_count_ack_checksum;
		gsecr.error_count_message_checksum = CO_MCU_DATA(port)->error_count.error_count_message_checksum;
		gsecr.error_count_frame            = CO_MCU_DATA(port)->error_count.error_count_frame;

	} else {
		gsecr.error_count_ack_checksum     = 0;
		gsecr.error_count_message_checksum = 0;
		gsecr.error_count_frame            = 0;
	}

	// There is no overflow in SPITFP on master side possible
	// We keep it to here to be able to use the same API between Co-MCU Bricklets and Bricks
	gsecr.error_count_overflow             = 0;

	send_blocking_with_timeout(&gsecr, sizeof(GetSPITFPErrorCountReturn), com);
}
Exemplo n.º 4
0
void com_adc_calibrate(const ComType com, const ADCCalibrate *data) {
	uint8_t port = (uint8_t)(tolower((uint8_t)data->bricklet_port) - 'a');
	if(port >= BRICKLET_NUM) {
		com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		logblete("Bricklet Port %d does not exist (adc calibrate)\n\r", port);
		return;
	}

	adc_calibrate(bs[port].adc_channel);
	adc_write_calibration_to_flash();

	com_return_setter(com, data);
	logd("ADC Calibrate (port %c)\n\r", data->bricklet_port);
}
void set_step_configuration(const ComType com, const SetStepConfiguration *data) {
	if(data->step_resolution > 8) {
		com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		return;
	}

	tmc2130_reg_chopconf.bit.mres   = data->step_resolution;
	tmc2130_reg_chopconf.bit.intpol = data->interpolation;

	tcm2130_register_to_write_mask |= TMC2130_REG_CHOPCONF_BIT;
	tcm2130_handle_register_read_and_write();

	com_return_setter(com, data);
}
void set_spreadcycle_configuration(const ComType com, const SetSpreadcycleConfiguration *data) {
	if((data->slow_decay_duration > 15) ||
	   (data->fast_decay_duration > 15) ||
	   (data->hysteresis_start_value > 7) ||
	   (data->hysteresis_end_value < -3 || data->hysteresis_end_value > 12) ||
	   (data->sinewave_offset < -3 || data->sinewave_offset > 12) ||
	   (data->chopper_mode > 1) ||
	   (data->comperator_blank_time > 3)) {
		com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		return;
	}

	tmc2130_reg_chopconf.bit.toff      = data->slow_decay_duration;
	tmc2130_reg_chopconf.bit.rndtf     = data->enable_random_slow_decay;
	if(data->chopper_mode) {
		tmc2130_reg_chopconf.bit.hstrt = data->fast_decay_duration & 0b111;
		tmc2130_reg_chopconf.bit.fd3   = (data->fast_decay_duration >> 3) & 0b1;
		tmc2130_reg_chopconf.bit.hend  = data->sinewave_offset; // TODO: handle signednes correctly!
	} else {
Exemplo n.º 7
0
void get_spitfp_baudrate(const ComType com, const GetSPITFPBaudrate *data) {
	uint8_t port = tolower((uint8_t)data->bricklet_port) - 'a';
	if(port >= BRICKLET_NUM) {
		com_return_error(data, sizeof(GetSPITFPErrorCountReturn), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		logblete("Bricklet Port %d does not exist (get_spitfp_baudrate)\n\r", port);
		return;
	}

	GetSPITFPBaudrateReturn gsbr;

	gsbr.header        = data->header;
	gsbr.header.length = sizeof(GetSPITFPBaudrateReturn);
	if(bricklet_attached[port] == BRICKLET_INIT_CO_MCU) {
		gsbr.baudrate  = bricklet_spitfp_baudrate[port];
	} else {
		gsbr.baudrate  = 0;
	}

	send_blocking_with_timeout(&gsbr, sizeof(GetSPITFPBaudrateReturn), com);
}
Exemplo n.º 8
0
void get_protocol1_bricklet_name(const ComType com, const GetProtocol1BrickletName *data) {
	uint8_t port = tolower((uint8_t)data->port) - 'a';
	if(port >= BRICKLET_NUM) {
		com_return_error(data, sizeof(GetProtocol1BrickletName), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		logblete("Bricklet Port %d does not exist (get_protocol1_bricklet_name)\n\r", port);
		return;
	}

	GetProtocol1BrickletNameReturn gp1bnr = MESSAGE_EMPTY_INITIALIZER;
	gp1bnr.header           = data->header;
	gp1bnr.header.length    = sizeof(GetProtocol1BrickletNameReturn);
	gp1bnr.protocol_version = bricklet_attached[port];

	if(gp1bnr.protocol_version == BRICKLET_INIT_PROTOCOL_VERSION_1) {
		baddr[port].entry(BRICKLET_TYPE_INFO,
		                  0,
		                  (uint8_t*)&gp1bnr.firmware_version);
	}

	send_blocking_with_timeout(&gp1bnr, sizeof(GetProtocol1BrickletNameReturn), com);
	logbleti("get_protocol1_bricklet_name: %s (%d)\n\r", gp1bnr.name, gp1bnr.protocol_version);
}
void set_basic_configuration(const ComType com, const SetBasicConfiguration *data) {
	if((data->standstill_current > 31) ||
	   (data->motor_run_current > 31) ||
	   (data->standstill_delay_time > 15)) {
		com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
		return;
	}

	tmc2130_reg_ihold_run.bit.ihold       = data->standstill_current;
	tmc2130_reg_ihold_run.bit.irun        = data->motor_run_current;
	tmc2130_reg_ihold_run.bit.ihold_delay = data->standstill_delay_time;
	tmc2130_reg_tpowerdown.bit.delay      = data->power_down_time;
	tmc2130_reg_tpwmthrs.bit.velocity     = MIN(TCP2130_CLOCK_FREQUENCY/(data->stealth_threshold*256), 0xfffff);
	tmc2130_reg_tcoolthrs.bit.velocity    = MIN(TCP2130_CLOCK_FREQUENCY/(data->coolstep_threshold*256), 0xfffff);
	tmc2130_reg_thigh.bit.velocity        = MIN(TCP2130_CLOCK_FREQUENCY/(data->classic_threshold*256), 0xfffff);
	tmc2130_reg_chopconf.bit.vhighchm     = data->high_velocity_chopper_mode;

	tcm2130_register_to_write_mask |= (TMC2130_REG_IHOLD_IRUN_BIT | TMC2130_REG_TPOWERDOWN_BIT | TMC2130_REG_TPWMTHRS_BIT | TMC2130_REG_TCOOLTHRS_BIT | TMC2130_REG_THIGH_BIT | TMC2130_REG_CHOPCONF_BIT);
	tcm2130_handle_register_read_and_write();

	com_return_setter(com, data);
}