示例#1
0
static void spi_write_reg(uint8_t reg, uint16_t value)
{
    spi_begin();
    spi_write(DEV_ID);
    spi_write(0);
    spi_write(reg);
    spi_end();
    spi_begin();
    spi_write(DEV_ID | RS);
    spi_write(value >> 8);
    spi_write(value & 0xff);
    spi_end();
}
示例#2
0
void flash_erase_64kB(int addr)
{
	spi_begin();
	spi_xfer(0xd8, 8);
	spi_xfer(addr, 24);
	spi_end();
}
示例#3
0
文件: max3421e.c 项目: ehujair/vivus
/*
 * Initialises the max3421e host shield. Initialises the SPI bus and sets the required pin directions.
 * Must be called before powerOn.
 */
void max3421e_init()
{
	spi_begin();

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

	// Set MAX_INT and MAX_GPX pins to input mode.
	DDRH &= ~(0x40 | 0x20);

	// Set SPI !SS pint to output mode.
	DDRB |= 0x10;

	// Set RESET pin to output
	DDRH |= 0x10;

#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)

	// Set MAX_INT and MAX_GPX pins to input mode.
	DDRB &= ~0x3;

	// Set RESET pin to output
	DDRD |= 0x80;

	// Set SS pin to output
	DDRB |= 0x4;

#endif

	// Sparkfun botched their first attempt at cloning Oleg's max3421e shield and reversed the GPX and RESET pins.
	// This hack is in place to make MicroBridge work with those shields. (see http://www.sparkfun.com/products/9628)
#ifdef SFHACK

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

	// Set MAX_GPX pin to input mode.
	DDRH &= ~0x10;

	// Set RESET pin to output
	DDRH |= 0x20;

#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)

	// Set GPX pin to input
	DDRD &= ~0x80;

	// Set RESET pin to output
	DDRB |= 0x1;

#endif

#endif


	// Pull SPI !SS high
	MAX_SS(1);

	// Reset
	MAX_RESET(1);
}
void Adafruit_STMPE610::writeRegister8(uint8_t reg, uint8_t val) {
	spi_begin();
    CSLow();
    spiOut(reg); 
    spiOut(val);
    CSHigh();
    spi_end();
}
/*!	\fn void mpu60x0_get_reading(int device, struct mpu60x0_stateType mpu60x0_state, struct reading_memory_type *reading)
 *	\brief Obtain a set of readings from the device
 * 
 * 	A set of readings is a new set of data for each of accelerometers, gyrometers, temperature sensing and timestamps.
 *	@param[in] device The spi device to enable interrupts on
 *	@param[in] mpu60x0_state a pointer to a state of the sensor which is used to determine the conversion ratios
 * 	@param[out] reading a pointer to a sensor reading structure this function will update
 */
void mpu60x0_get_reading(int device, struct mpu60x0_stateType mpu60x0_state, struct reading_memory_type *reading)
{
	uint8_t byte_H;
	uint8_t byte_L;
	int16_t conv;
	double conv_double;
			
	reading->timestamp = systimer_get_us_32bit();

	// We start a SPI multibyte read of sensors
	spi_begin(device);
	spi_transfer(INV_MPU60x0_REG_RAW_ACCEL|0x80);	
	
	// Read AccelX
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	conv_double = conv;
	reading->a_x = conv_double/inv_mpu60x0_accl_conv_ratio[mpu60x0_state.accel_rate]; //FIXME:
	// Read AccelY
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	conv_double = conv;
	reading->a_y = conv_double/inv_mpu60x0_accl_conv_ratio[mpu60x0_state.accel_rate];
	// Read AccelZ
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	conv_double = conv;
	reading->a_z = conv_double/inv_mpu60x0_accl_conv_ratio[mpu60x0_state.accel_rate];
	// Read Temp
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	conv_double = conv;
	reading->temp = conv_double/340.0 + 36.53;
	// Read GyroX
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	conv_double = conv;
	reading->w_x = conv_double/inv_mpu60x0_gyro_conv_ratio[mpu60x0_state.gyro_rate]; //FIXME:
	// Read GyroY
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	conv_double = conv;
	reading->w_y = conv_double/inv_mpu60x0_gyro_conv_ratio[mpu60x0_state.gyro_rate]; //fixme!!
	// Read GyroZ
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	conv_double = conv;
	reading->w_z = conv_double/inv_mpu60x0_gyro_conv_ratio[mpu60x0_state.gyro_rate]; //fixme!!
	
	spi_end();
}
示例#6
0
文件: drv8301.c 项目: yuqlid/bldc
void drv8301_write_reg(int reg, int data) {
	uint16_t out = 0;
	out |= (reg & 0x0F) << 11;
	out |= data & 0x7FF;

	spi_begin();
	spi_exchange(out);
	spi_end();
}
示例#7
0
文件: drv8301.c 项目: yuqlid/bldc
unsigned int drv8301_read_reg(int reg) {
	uint16_t out = 0;
	out |= (1 << 15);
	out |= (reg & 0x0F) << 11;
	out |= 0x807F;

	if (reg != 0) {
		spi_begin();
		spi_exchange(out);
		spi_end();
	}

	spi_begin();
	uint16_t res = spi_exchange(0xFFFF);
	spi_end();

	return res;
}
/*!	\fn int spi_read(int pin, uint8_t reg, uint8_t *data) 
 *	\brief Write 8 address bits to an spi device then read 8 bits from spi device
 *	@param[in] pin the chipselect pin to toggle
 *	@param[in] reg the address bits to send to the spi device
 * 	@param[out] the data the spi device sent back
 * 	\return returns ERR_NOERR unless an attempt has been made to perform two simultaneous spi transactions, then an error is returned
 */
int spi_read(int pin, uint8_t reg, uint8_t *data) 
{
	int ret = spi_begin(pin);
	if (ret < 0)
		return ret;
	spi_transfer(reg|0x80);
	*data = spi_transfer(0);
	return spi_end();
}
示例#9
0
文件: atben.c 项目: cfriedt/ben-wpan
static void atben_reg_write(void *handle, uint8_t reg, uint8_t v)
{
	struct atben_dsc *dsc = handle;

	spi_begin(dsc);
	spi_send(dsc, AT86RF230_REG_WRITE | reg);
	spi_send(dsc, v);
	spi_end(dsc);
}
示例#10
0
void flash_read(int addr, char *data, int n)
{
	spi_begin();
	spi_xfer(0x03, 8);
	spi_xfer(addr, 24);
	while (n--)
		*(data++) = spi_xfer(0, 8);
	spi_end();
}
示例#11
0
void flash_write(int addr, char *data, int n)
{
	spi_begin();
	spi_xfer(0x02, 8);
	spi_xfer(addr, 24);
	while (n--)
		spi_xfer(*(data++), 8);
	spi_end();
}
示例#12
0
/*!	\fn int spi_write(int pin, uint8_t reg, uint8_t data)
 *	\brief Write 8 address bits to an spi device then writes 8 data bits to the spi device
 *	@param[in] pin the chipselect pin to toggle
 *	@param[in] reg the address bits to send to the spi device
 * 	@param[in] the data to send to the spi device
 * 	\return returns ERR_NOERR unless an attempt has been made to perform two simultaneous spi transactions, then an error is returned
 */
int spi_write(int pin, uint8_t reg, uint8_t data)
{
	int ret = spi_begin(pin);
	if (ret < 0)
		return ret;
	spi_transfer(reg);
	spi_transfer(data);
	return spi_end();	
}
示例#13
0
文件: encoder.c 项目: JarryChou/bldc
/**
 * Timer interrupt
 */
void encoder_tim_isr(void) {
	uint16_t pos;

	spi_begin();
	spi_transfer(&pos, 0, 1);
	spi_end();

	pos &= 0x3FFF;
	last_enc_angle = ((float)pos * 360.0) / 16384.0;
}
示例#14
0
文件: atben.c 项目: cfriedt/ben-wpan
static void atben_sram_write(void *handle, uint8_t addr, uint8_t v)
{
	struct atben_dsc *dsc = handle;

	spi_begin(dsc);
	spi_send(dsc, AT86RF230_SRAM_WRITE);
	spi_send(dsc, addr);
	spi_send(dsc, v);
	spi_end(dsc);
}
示例#15
0
void flash_read_id()
{
	int i;
	spi_begin();
	spi_xfer(0x9F, 8);
	printf("Flash ID:");
	for (i = 1; i < 21; i++)
		printf(" %02X", spi_xfer(0, 8));
	printf("\n");
	spi_end();
}
示例#16
0
文件: atben.c 项目: cfriedt/ben-wpan
static uint8_t atben_reg_read(void *handle, uint8_t reg)
{
	struct atben_dsc *dsc = handle;
	uint8_t res;

	spi_begin(dsc);
	spi_send(dsc, AT86RF230_REG_READ | reg);
	res = spi_recv(dsc);
	spi_end(dsc);
	return res;
}
uint8_t Adafruit_STMPE610::readRegister8(uint8_t reg) {
	uint8_t x ;
	spi_begin();
	CSLow();
	spiOut(0x80 | reg);
	spiOut(0x00);
	x = spiIn();
	CSHigh();
	spi_end();
	return x;
}
示例#18
0
文件: atben.c 项目: cfriedt/ben-wpan
static void atben_buf_write(void *handle, const void *buf, int size)
{
	struct atben_dsc *dsc = handle;

	spi_begin(dsc);
	spi_send(dsc, AT86RF230_BUF_WRITE);
	spi_send(dsc, size);
	while (size--)
		spi_send(dsc, *(uint8_t *) buf++);
	spi_end(dsc);
}
示例#19
0
/**
 * @ingroup SPI-DIO
 *
 * @param device_info
 * @return
 */
uint8_t bw_spi_dio_start(device_info_t *device_info) {
#if !defined(BARE_METAL) && !defined(__AVR_ARCH__)
	if (bcm2835_init() != 1)
		return 1;
#endif
	FUNC_PREFIX(spi_begin());

	if (device_info->slave_address <= 0)
		device_info->slave_address = BW_DIO_DEFAULT_SLAVE_ADDRESS;

	return 0;
}
示例#20
0
文件: atben.c 项目: cfriedt/ben-wpan
static uint8_t atben_sram_read(void *handle, uint8_t addr)
{
	struct atben_dsc *dsc = handle;
	uint8_t res;

	spi_begin(dsc);
	spi_send(dsc, AT86RF230_SRAM_READ);
	spi_send(dsc, addr);
	res = spi_recv(dsc);
	spi_end(dsc);
	return res;
}
示例#21
0
static void cmd_device(uint8_t bus, uint8_t port)
{
	if (spi_started)
		spi_end();

	if (spi_begin(bus, port) != 0) {
		fprintf(stderr, "problem accessing device\n");
		exit(EXIT_FAILURE);
	}

	spi_started = 1;
}
/*!	\fn void mpu60x0_get_reading_raw(int device, struct reading_memory_type *reading)
 *	\brief Obtain a set of "raw" readings from the device
 * 
 * 	The data returned from this function is actually the raw 16bit integer data converted to a double. This makes it easier when dealing with the serial protocol defined in the final report
 *	@param[in] device The spi device to enable interrupts on
 * 	@param[out] reading a pointer to a sensor reading structure this function will update
 */
void mpu60x0_get_reading_raw(int device, struct reading_memory_type *reading)
{
	uint8_t byte_H;
	uint8_t byte_L;
	int16_t conv;
	
	reading->timestamp = systimer_get_us_32bit();
			
	// We start a SPI multibyte read of sensors
	spi_begin(device);
	spi_transfer(INV_MPU60x0_REG_RAW_ACCEL|0x80);
	
	// Read AccelX
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	reading->a_x = (double) conv;
	
	// Read AccelY
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	reading->a_y = (double) conv;
	// Read AccelZ
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	reading->a_z = (double) conv;
	// Read Temp
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	reading->temp = (double) conv;
	// Read GyroX
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	reading->w_x = (double) conv;
	// Read GyroY
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	reading->w_y = (double) conv;
	// Read GyroZ
	byte_H = spi_transfer(0);
	byte_L = spi_transfer(0);
	conv = ((int16_t)byte_H<<8)| byte_L;
	reading->w_z = (double) conv;
	
	spi_end();
}
uint16_t Adafruit_STMPE610::readRegister16(uint8_t reg) {
  uint16_t x;
	spi_begin();
    CSLow();
    spiOut(0x80 | reg); 
    spiOut(0x00);
    x = spiIn(); 
    x<<=8;
    x |= spiIn(); 
    CSHigh();
    spi_end();
  //Serial.print("$"); Serial.print(reg, HEX); 
  //Serial.print(": 0x"); Serial.println(x, HEX);
  return x;
}
示例#24
0
文件: atben.c 项目: cfriedt/ben-wpan
static int atben_buf_read(void *handle, void *buf, int size)
{
	struct atben_dsc *dsc = handle;
	uint8_t len, i;

	spi_begin(dsc);
	spi_send(dsc, AT86RF230_BUF_READ);
	len = spi_recv(dsc);
	len++; /* LQI */
	if (len > size)
		len = size;
	for (i = 0; i != len; i++)
		*(uint8_t *) buf++ = spi_recv(dsc);
	spi_end(dsc);
	return len;
}
示例#25
0
void flash_wait()
{
	while (1)
	{
		spi_begin();
		spi_xfer(0x05, 8);
		int status = spi_xfer(0, 8);
		spi_end();

		if ((status & 0x01) == 0)
			break;

		// fprintf(stderr, "[wait]");
		usleep(1000);
	}

	// fprintf(stderr, "[ok]\n");
}
示例#26
0
/**
 * @ingroup SPI-LCD
 *
 * @param device_info
 * @return
 */
uint8_t bw_spi_lcd_start(device_info_t *device_info) {
#if !defined(BARE_METAL) && !defined(__AVR_ARCH__)
	if (bcm2835_init() != 1)
	return BW_LCD_ERROR;
#endif
	FUNC_PREFIX(spi_begin());

	if (device_info->slave_address == (uint8_t) 0) {
		device_info->slave_address = BW_LCD_DEFAULT_SLAVE_ADDRESS;
	}

	if (device_info->speed_hz == (uint32_t) 0) {
		device_info->speed_hz = (uint32_t) BW_LCD_SPI_SPEED_DEFAULT_HZ;
	} else if (device_info->speed_hz > (uint32_t) BW_LCD_SPI_SPEED_MAX_HZ) {
		device_info->speed_hz = (uint32_t) BW_LCD_SPI_SPEED_MAX_HZ;
	}
#ifdef __AVR_ARCH__
#else
	device_info->internal_clk_div = (uint16_t)((uint32_t) BCM2835_CORE_CLK_HZ / device_info->speed_hz);
#endif
	return BW_LCD_OK;
}
示例#27
0
void flash_power_up()
{
	spi_begin();
	spi_xfer(0xAB, 8);
	spi_end();
}
示例#28
0
void flash_write_enable()
{
	spi_begin();
	spi_xfer(0x06, 8);
	spi_end();
}
示例#29
0
/// @brief  Obtain SPI bus for TFT display, assert chip select
/// return: void
void tft_spi_begin()
{
	spi_begin(tft_clock, ILI9341_CS);
}
示例#30
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");
		}
		*/
	}
}