Пример #1
0
void spi_set_baud(int port, int baud, SPIOption13 option13, int option3, int option2, int option1) {
	if(port >= (NUM_SPIPORTS - 1)) {
		return;
	}

	SET_REG(SPIRegs[port].control, 0);

	switch(option13) {
		case SPIOption13Setting0:
			spi_info[port].option13 = 0;
			break;

		case SPIOption13Setting1:
			spi_info[port].option13 = 1;
			break;

		case SPIOption13Setting2:
			spi_info[port].option13 = 2;
			break;
	}

	spi_info[port].option2 = option2;
	spi_info[port].option1 = option1;

	uint32_t clockFrequency;

	if(spi_info[port].clockSource == PCLK) {
		clockFrequency = PeripheralFrequency;
	} else {
		clockFrequency = FixedFrequency;
	}

	uint32_t divider;

	if(chipid_spi_clocktype() != 0) {
		divider = clockFrequency / baud;
		if(divider < 2)
			divider = 2;
	} else {
		divider = clockFrequency / (baud * 2 - 1);
	}

	if(divider > MAX_DIVIDER) {
		return;
	}
	
	SET_REG(SPIRegs[port].clkDivider, divider);
	spi_info[port].baud = baud;
	spi_info[port].option3 = option3;

	uint32_t options = (option1 << 1)
			| (option2 << 2)
			| ((option3 ? 0x3 : 0) << 3)
			| ((spi_info[port].option5 ? 0x2 : 0x3D) << 5)
			| (spi_info[port].clockSource << CLOCK_SHIFT)
			| spi_info[port].option13 << 13;

	SET_REG(SPIRegs[port].setup, options);
	SET_REG(SPIRegs[port].unkReg1, 0);
	SET_REG(SPIRegs[port].control, 1);

}
Пример #2
0
void spi_set_baud(int port, int baud, SPIWordSize wordSize, int isMaster, int isActiveLow, int lastClockEdgeMissing) {
	if(port > (NUM_SPIPORTS - 1)) {
		return;
	}

	SET_REG(SPIRegs[port].control, 0);

	switch(wordSize) {
		case SPIWordSize8:
			spi_info[port].wordSize = 0;
			break;

		case SPIWordSize16:
			spi_info[port].wordSize = 1;
			break;

		case SPIWordSize32:
			spi_info[port].wordSize = 2;
			break;
	}

	spi_info[port].isActiveLow = isActiveLow;
	spi_info[port].lastClockEdgeMissing = lastClockEdgeMissing;

	uint32_t clockFrequency;

	if(spi_info[port].clockSource == PCLK) {
		clockFrequency = PeripheralFrequency;
	} else {
		clockFrequency = FixedFrequency;
	}

	uint32_t divider;

	if(chipid_spi_clocktype() != 0) {
		divider = clockFrequency / baud;
		if(divider < 2)
			divider = 2;
	} else {
		divider = clockFrequency / (baud * 2 - 1);
	}

	if(divider > MAX_DIVIDER) {
		return;
	}
	
	SET_REG(SPIRegs[port].clkDivider, divider);
	spi_info[port].baud = baud;
	spi_info[port].isMaster = isMaster;

	uint32_t options = (lastClockEdgeMissing << 1)
			| (isActiveLow << 2)
			| ((isMaster ? 0x3 : 0) << 3)
			| ((spi_info[port].option5 ? 0x2 : 0x3D) << 5)
			| (spi_info[port].clockSource << CLOCK_SHIFT)
			| spi_info[port].wordSize << 13;

	SET_REG(SPIRegs[port].setup, options);
	SET_REG(SPIRegs[port].pin, 0);
	SET_REG(SPIRegs[port].control, 1);

}