예제 #1
0
boolean i2c_begin(I2CBus * wirex, uint32_t clkspeed) {
//	GPIO_InitTypeDef GPIO_InitStructure;
	I2C_InitTypeDef I2C_InitStructure;

	wirex->I2Cx = I2C1;
	wirex->sda = PB9;
	wirex->scl = PB8;

	/* I2C Periph clock enable */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); //  RCC_APB1PeriphClockCmd(I2C1_RCC, ENABLE);
	/* GPIO Periph clock enable */
	//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // PB5 (SMBA), PB6 (SCL), PB9 (SDA)  // RCC_APB2PeriphClockCmd(I2C1_GPIO_RCC, ENABLE);
	GPIOMode(PinPort(wirex->scl), PinBit(wirex->scl), GPIO_Mode_AF, GPIO_Speed_50MHz,
			GPIO_OType_OD, GPIO_PuPd_UP);
	GPIOMode(PinPort(wirex->sda), PinBit(wirex->sda), GPIO_Mode_AF, GPIO_Speed_50MHz,
			GPIO_OType_OD, GPIO_PuPd_UP);

	/* Configure I2C pins: SCL and SDA */
	GPIO_PinAFConfig(PinPort(wirex->scl), PinSource(wirex->scl), GPIO_AF_I2C1 );
	GPIO_PinAFConfig(PinPort(wirex->sda), PinSource(wirex->sda), GPIO_AF_I2C1 );

	//#if defined (REMAP_I2C1)
//Remap_I2C1_Configuration();
//#endif
	switch (clkspeed) {
	case 100000:
	case 400000:
		break;
	default:
		clkspeed = 100000;
		break;
	}
	/* I2C configuration */
	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed = clkspeed;

	/* Apply I2C configuration after enabling it */
	I2C_Init(wirex->I2Cx, &I2C_InitStructure);
	/* I2C Peripheral Enable */
	I2C_Cmd(wirex->I2Cx, ENABLE);

	wirex->status = NOT_READY;
	wirex->mode = I2C_MODE_NOTDEFINED;

	return true;
}
예제 #2
0
파일: main.cpp 프로젝트: ADTL/ARMWork
int main(void) {
	char tmp[256];

	const char message[] = 
			"This royal throne of kings, this scepter'd isle, \n"
			"This earth of majesty, this seat of Mars, \n" /*
			"This other Eden, demi-paradise, \n" 
			"This fortress built by Nature for herself\n"
			"Against infection and the hand of war, \n"
			"This happy breed of men, this little world,\n" 
			"This precious stone set in the silver sea, \n"
			"Which serves it in the office of a wall, \n"
			"Or as a moat defensive to a house, \n"
			"Against the envy of less happier lands, \n"
			"This blessed plot, this earth, this realm, this England,"*/
	;
	const uint16 messlen = strlen(message);
	
	cmcore_init();
	usart_init(&Serial6, USART6, PC7, PC6);
	usart_begin(&Serial6, 57600);

	spi_init(&spi1, SPI1, PA5, PA6, PA7, PA4);
	spi_begin(&spi1);

	usart_print(&Serial6, "Basic initialization has been finished.\n");
		
	RCC_ClocksTypeDef RCC_Clocks;
	RCC_GetClocksFreq(&RCC_Clocks);
	
	usart_print(&Serial6, message);
	usart_print(&Serial6, "\r\n\r\n");
	usart_flush(&Serial6);

	sprintf(tmp, "Clock frequencies: SYSCLK = %dl, HCLK = %dl, PCLK1 = %dl\r\n", 
		RCC_Clocks.SYSCLK_Frequency, RCC_Clocks.HCLK_Frequency, RCC_Clocks.PCLK1_Frequency);
	usart_print(&Serial6, tmp); 

	GPIOMode(PinPort(LED1), (PinBit(LED1) | PinBit(LED2) | PinBit(LED3) | PinBit(LED4)), 
					OUTPUT, FASTSPEED, PUSHPULL, NOPULL);
	
	nokiaLCD.init();
	nokiaLCD.clear();
	nokiaLCD.drawBitmap(PCD8544::SFEFlame);
		delay(1000);
		
	uint16 shift = 0;
	nokiaLCD.selectFont(PCD8544::CHICAGO10);
	
	while (1) {
		nokiaLCD.clear();
		nokiaLCD.cursor(shift);
		nokiaLCD.drawString("Nuke is absolutely safe!");
//		nokiaLCD.drawFont(Nokia5110::Chicago10x15, 'A');
//		nokiaLCD.drawFont(Nokia5110::Chicago10x15, 'W');
		shift++;
		shift %= 252;
		delay(250);

/*
		if ( millis() / 125 != shift ) {
			shift = millis()/ 125;
			nokiaLCD.clear();
			nokiaLCD.gotoXY(7- shift%7,0);
			strncpy(tmp, message+((shift/7) % messlen), 48);
			tmp[48] = 0;
			nokiaLCD.drawString(tmp);
//		usart_print(&Serial6, tmp);
//		usart_print(&Serial6, "\r\n");
		}
		*/
	}
}
예제 #3
0
파일: main.c 프로젝트: ADTL/ARMWork
int main(void) {
	uint16_t bits;
	uint32_t intval = 40;
	uint32_t tnow;
	char tmp[92];
	RCC_ClocksTypeDef RCC_Clocks;
	uint16_t i;
	
	TIM2_timer_start();

	usart_begin(&USerial3, USART3, PC11, PC10, 19200);

	usart_print(&USerial3, 
			"Happy are those who know they are spiritually poor; \n"
			"The kingdom of heaven belongs to them!\n");
	usart_flush(&USerial3);
	
	RCC_GetClocksFreq(&RCC_Clocks);

	sprintf(tmp, "SYSCLK = %ul\n", RCC_Clocks.SYSCLK_Frequency);
	usart_print(&USerial3, tmp);
	sprintf(tmp, "PCLK1 = %ul\n", RCC_Clocks.PCLK1_Frequency);
	usart_flush(&USerial3);

	GPIOMode(PinPort(PD12),
			(PinBit(PD12) | PinBit(PD13) | PinBit(PD14) | PinBit(PD15)), OUTPUT,
			FASTSPEED, PUSHPULL, NOPULL);
			/*
	spi_begin(SPI2, PB13, PB14, PB15, PB12);
	digitalWrite(PB12, HIGH);
*/
	I2C1_Init();
/*
	i2c_begin(&Wire1, PB9, PB8, 100000);
	lcd.init(&Wire1);
	lcd.begin();
	lcd.setContrast(46);
	lcd.print("Yappee!");       // Classic Hello World!
*/
	bits = GPIO_ReadOutputData(GPIOD );
	GPIOWrite(GPIOD, PinBit(PD13) | (bits & 0x0fff));
	delay_ms(intval);
	tnow = millis() / 1000;
	while (tnow == millis() / 1000)
		;
	tnow = millis() / 1000;

	while (1) {
		bits = GPIO_ReadOutputData(GPIOD );

		GPIOWrite(GPIOD, PinBit(PD13) | (bits & 0x0fff));
		delay_ms(intval);
		GPIOWrite(GPIOD, PinBit(PD14) | (bits & 0x0fff));
		delay_ms(intval);
		GPIOWrite(GPIOD, PinBit(PD15) | (bits & 0x0fff));
		delay_ms(intval);
		GPIOWrite(GPIOD, PinBit(PD12) | (bits & 0x0fff));
		delay_ms(intval);
		//
		bits &= 0x0fff;
		switch ((tnow % 60) / 15) {
		case 3:
			bits |= PinBit(PD12);
		case 2:
			bits |= PinBit(PD15);
		case 1:
			bits |= PinBit(PD14);
		case 0:
		default:
			bits |= PinBit(PD13);
			break;
		}
		GPIOWrite(GPIOD, bits);

		while (tnow == millis() / 1000);
		tnow = millis() / 1000;

		//Serial3.print(tmp);
		sprintf(tmp, "%04ld\n", millis());
		usart_print(&USerial3, tmp);

		/*
		digitalWrite(PB12, LOW);
		spi_transfer(SPI2, (uint8_t *) tmp, 8);
		digitalWrite(PB12, HIGH);
*/
		i = 0;
		if (usart_available(&USerial3) > 0) {
			while (usart_available(&USerial3) > 0 && i < 92) {
				tmp[i++] = (char) usart_read(&USerial3);
			}
			tmp[i] = 0;
			usart_print(&USerial3, "> ");
			usart_print(&USerial3, tmp);
			usart_print(&USerial3, "\n");
		}

	}
	return 0;
}
예제 #4
0
void usart_init(usart * usx, USART_TypeDef * USARTx, GPIOPin rx, GPIOPin tx) {
	//
	uint8_t af = GPIO_AF_USART1;
	usx->USARTx = USARTx;

	if ( usx->USARTx == USART1) {
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
		af = GPIO_AF_USART1;
		usx->irqn = USART1_IRQn;
//		usx->usid = USART1Serial;
		rxring[USART1Serial] = &usx->rxring;
		txring[USART1Serial] = &usx->txring;
	} else if (usx->USARTx == USART2) {
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
		af = GPIO_AF_USART2;
		usx->irqn = USART2_IRQn;
//		usx->usid = USART2Serial;
//		usx->USARTx = USART2;
		rxring[USART2Serial] = &usx->rxring;
		txring[USART2Serial] = &usx->txring;
	} else if (usx->USARTx == USART3) {
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
		af = GPIO_AF_USART3;
		usx->irqn = USART3_IRQn;
//		usx->usid = USART3Serial;
//		usx->USARTx = USART3;
		rxring[USART3Serial] = &usx->rxring;
		txring[USART3Serial] = &usx->txring;
	} else if (usx->USARTx == UART4) {
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
		af = GPIO_AF_UART4;
		usx->irqn = UART4_IRQn;
//		usx->usid = UART4Serial;
//		usx->USARTx = UART4;
		rxring[UART4Serial] = &usx->rxring;
		txring[UART4Serial] = &usx->txring;
	} else if (usx->USARTx == UART5) {
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
		af = GPIO_AF_UART5;
		usx->irqn = UART5_IRQn;
//		usx->usid = UART5Serial;
//		usx->USARTx = UART5;
		rxring[UART5Serial] = &usx->rxring;
		txring[UART5Serial] = &usx->txring;
	} else { // Serial6
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
		af = GPIO_AF_USART6;
		usx->irqn = USART6_IRQn;
//		usx->usid = USART6Serial;
//		usx->USARTx = USART6;
		rxring[USART6Serial] = &usx->rxring;
		txring[USART6Serial] = &usx->txring;
	}

	GPIOMode(PinPort(rx), PinBit(rx), GPIO_Mode_AF, GPIO_Speed_50MHz,
						GPIO_OType_PP, GPIO_PuPd_NOPULL);
	GPIOMode(PinPort(tx), PinBit(tx), GPIO_Mode_AF, GPIO_Speed_50MHz,
						GPIO_OType_PP, GPIO_PuPd_NOPULL);

	GPIO_PinAFConfig(PinPort(rx), PinSource(rx), af);
	GPIO_PinAFConfig(PinPort(tx), PinSource(tx), af);
}
예제 #5
0
void spi_init(SPIBus * spibx, SPI_TypeDef * SPIx, 
							GPIOPin sck, GPIOPin miso, GPIOPin mosi, GPIOPin nss) {
	uint8_t af; // = GPIO_AF_SPI1;

	/* PCLK2 = HCLK/2 */
	//RCC_PCLK2Config(RCC_HCLK_Div2);
	if ( SPIx == SPI2 ) {
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
		af = GPIO_AF_SPI2;
		spibx->SPIx = SPI2;
		// PB12, 13, 14, 15
	} else if ( SPIx == SPI3 ) {
		// SPI3
		RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
		af = GPIO_AF_SPI3;
		spibx->SPIx = SPI3;
	}	else { //if (SPIx == SPI1) {
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
		af = GPIO_AF_SPI1;
		spibx->SPIx = SPI1;
		// sck = PA5 / PB3, miso = PA6/ PB4, mosi = PA7 / PB5, nSS = PA4 / PA15
	} 

	GPIOMode(PinPort(sck), PinBit(sck), GPIO_Mode_AF, GPIO_Speed_25MHz,
			GPIO_OType_PP, GPIO_PuPd_UP);
	GPIOMode(PinPort(miso), PinBit(miso), GPIO_Mode_AF, GPIO_Speed_25MHz,
			GPIO_OType_PP, GPIO_PuPd_UP);
	GPIOMode(PinPort(mosi), PinBit(mosi), GPIO_Mode_AF, GPIO_Speed_25MHz,
			GPIO_OType_PP, GPIO_PuPd_UP);
	GPIO_PinAFConfig(PinPort(sck), PinSource(sck), af);
	GPIO_PinAFConfig(PinPort(miso), PinSource(miso), af);
	GPIO_PinAFConfig(PinPort(mosi), PinSource(mosi), af);
	// nSS by software
	GPIOMode(PinPort(nss), PinBit(nss), GPIO_Mode_OUT, GPIO_Speed_25MHz,
			GPIO_OType_PP, GPIO_PuPd_UP);
	digitalWrite(nss, HIGH);
	//GPIO_PinAFConfig(PinPort(nss), PinSource(nss), af);

	// set default parameters
	spibx->modeStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	spibx->modeStruct.SPI_Mode = SPI_Mode_Master;
	spibx->modeStruct.SPI_DataSize = SPI_DataSize_8b;
	spibx->modeStruct.SPI_CPOL = SPI_CPOL_Low;
	spibx->modeStruct.SPI_CPHA = SPI_CPHA_1Edge;
	spibx->modeStruct.SPI_NSS = SPI_NSS_Soft;
	spibx->modeStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
	spibx->modeStruct.SPI_FirstBit = SPI_FirstBit_MSB;
	spibx->modeStruct.SPI_CRCPolynomial = SPI_CRC_Rx;

	SPI_Init(spibx->SPIx, &spibx->modeStruct);

	SPI_Cmd(spibx->SPIx, ENABLE);
	/*          5. Enable the NVIC and the corresponding interrupt using the function
	 *             SPI_ITConfig() if you need to use interrupt mode.
	 *
	 *          6. When using the DMA mode
	 *                   - Configure the DMA using DMA_Init() function
	 *                   - Active the needed channel Request using SPI_I2S_DMACmd() function
	 *
	 *          7. Enable the SPI using the SPI_Cmd() function or enable the I2S using
	 *             I2S_Cmd().
	 *
	 *          8. Enable the DMA using the DMA_Cmd() function when using DMA mode.
	 *
	 *          9. Optionally, you can enable/configure the following parameters without
	 *             re-initialization (i.e there is no need to call again SPI_Init() function):
	 *              - When bidirectional mode (SPI_Direction_1Line_Rx or SPI_Direction_1Line_Tx)
	 *                is programmed as Data direction parameter using the SPI_Init() function
	 *                it can be possible to switch between SPI_Direction_Tx or SPI_Direction_Rx
	 *                using the SPI_BiDirectionalLineConfig() function.
	 *              - When SPI_NSS_Soft is selected as Slave Select Management parameter
	 *                using the SPI_Init() function it can be possible to manage the
	 *                NSS internal signal using the SPI_NSSInternalSoftwareConfig() function.
	 *              - Reconfigure the data size using the SPI_DataSizeConfig() function
	 *              - Enable or disable the SS output using the SPI_SSOutputCmd() function
	 *
	 *          10. To use the CRC Hardware calculation feature refer to the Peripheral
	 *              CRC hardware Calculation subsection.
	 */
}