Ejemplo n.º 1
0
unsigned char CC2500xcvr::sendBurstCommand(unsigned char command, unsigned char* data, unsigned char length)
{
    spiTransactionBegin();	// enable device

    // send command byte
    spiTransfer(command);			    // this is a burst command
    unsigned char result = 0;

    // send/recv data bytes
    for (int i=0; i<length; ++i)
	{
        result = spiTransfer(data[i]);	// send
        data[i] = result;				// receive into the same buffer
    }

    spiTransactionEnd(); 	// disable device
    return result;	// return result
}
Ejemplo n.º 2
0
bool mpu6000ReadRegister(uint8_t reg, uint8_t length, uint8_t *data)
{
    ENABLE_MPU6000;
    spiTransferByte(MPU6000_SPI_INSTANCE, reg | 0x80); // read transaction
    spiTransfer(MPU6000_SPI_INSTANCE, data, NULL, length);
    DISABLE_MPU6000;

    return true;
}
Ejemplo n.º 3
0
void readEepromPage(uint16_t address, uint16_t length)
{
	// here again we have a word address
	for (uint16_t addr = address * 2, i = 0; i < length; i++)
	{
		uint8_t ee = spiTransfer(0xA0, addr++, 0xFF);
		Serial.write(ee);
	}
	Serial.write(STK_OK);
}
Ejemplo n.º 4
0
void LedControl::setRow(int addr, int row, byte value) {
    int offset;
    if(addr<0 || addr>=maxDevices)
	return;
    if(row<0 || row>7)
	return;
    offset=addr*8;
    status[offset+row]=value;
    spiTransfer(addr, row+1,status[offset+row]);
}
Ejemplo n.º 5
0
void writeEnable(void)
{
	ENABLE_EEPROM;

    spiTransfer(EEPROM_SPI, WRITE_ENABLE);

    DISABLE_EEPROM;

    delayMicroseconds(2);
}
Ejemplo n.º 6
0
void MAX7219::clearDisplay(int addr) {
	if (addr < 0 || addr >= maxDevices) return;

	int offset;
	offset = addr * 8;
	for (uint8_t i = 0; i < 8; i++) {
		status[offset + i] = 0;
		spiTransfer(addr, i + 1, status[offset + i]);
	}
}
Ejemplo n.º 7
0
bool mpu9250SlowReadRegister(uint8_t reg, uint8_t length, uint8_t *data)
{
	ENABLE_MPU9250;
    delayMicroseconds(1);
    spiTransferByte(MPU9250_SPI_INSTANCE, reg | 0x80); // read transaction
    spiTransfer(MPU9250_SPI_INSTANCE, data, NULL, length);
    DISABLE_MPU9250;
    delayMicroseconds(1);

    return true;
}
Ejemplo n.º 8
0
void LedControl::clearDisplay(int addr) {
    int offset;

    if(addr<0 || addr>=maxDevices)
	return;
    offset=addr*8;
    for(int i=0;i<8;i++) {
	status[offset+i]=0;
    }
    if (anodeMode) {
    	transposeData(addr);
    	for(int i=0;i<8;i++) {
	    spiTransfer(addr, i+1, statusTransposed[offset+i]);
    	}
    } else {
    	for(int i=0;i<8;i++) {
	    spiTransfer(addr, i+1, status[offset+i]);
    	}
    }
}
Ejemplo n.º 9
0
void LedControl::clearDisplay(int addr) {
    int offset;

    if(addr<0 || addr>=maxDevices)
	return;
    offset=addr*8;
    for(int i=0;i<8;i++) {
	status[offset+i]=0;
	spiTransfer(addr, i+1,status[offset+i]);
    }
}
Ejemplo n.º 10
0
void MAX7219::setDigit(int digit, uint8_t value, bool dp, int addr) {
	if (addr < 0 || addr >= maxDevices) return;
	if (digit < 0 || digit > 7 || value > 15) return;

	int offset = addr * 8;
	uint8_t v = charTable[value];
	if (dp) {
		v |= 0b10000000;
	}
	status[offset + digit] = v;
	spiTransfer(addr, digit + 1, v);
}
Ejemplo n.º 11
0
void m25p16_pageProgramBegin(uint32_t address)
{
    uint8_t command[] = { M25P16_INSTRUCTION_PAGE_PROGRAM, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF};

    m25p16_waitForReady(DEFAULT_TIMEOUT_MILLIS);

    m25p16_writeEnable();

    ENABLE_M25P16;

    spiTransfer(M25P16_SPI_INSTANCE, NULL, command, sizeof(command));
}
Ejemplo n.º 12
0
static bool spiTest(void)
{
    uint8_t out[] = { 0x9F, 0, 0, 0 };
    uint8_t in[4];

    spiSelect(true);
    spiTransfer(in, out, sizeof(out));
    spiSelect(false);
    if (in[1] == 0xEF)
        return true;

    return false;
}
Ejemplo n.º 13
0
MAX7219::MAX7219(SPI* spi, int numDevices) {
	assert(spi != nullptr);
	this->spi = spi;
	if (numDevices <= 0 || numDevices > 8) {
		numDevices = 8;
	}
	maxDevices = numDevices;

	for (uint8_t i = 0; i < 64; i++) {
		status[i] = 0x00;
	}
	for (int i = 0; i < maxDevices; i++) {
		spiTransfer(i, OP_DISPLAYTEST, 0);
		//scanlimit is set to max on startup
		setScanLimit(7, i);
		//decode is done in source
		spiTransfer(i, OP_DECODEMODE, 0);
		clearDisplay(i);
		//we go into shutdown-mode on startup
		shutdown(true, i);
	}
}
Ejemplo n.º 14
0
static uint8_t m25p16_readStatus()
{
    uint8_t command[2] = { M25P16_INSTRUCTION_READ_STATUS_REG, 0 };
    uint8_t in[2];

    ENABLE_M25P16;

    spiTransfer(M25P16_SPI_INSTANCE, in, command, sizeof(command));

    DISABLE_M25P16;

    return in[1];
}
Ejemplo n.º 15
0
void MAX7219::setChar(int digit, char value, bool dp, int addr) {
	if (addr < 0 || addr >= maxDevices) return;
	if (digit < 0 || digit > 7) return;

	int offset = addr * 8;
	uint8_t index = (uint8_t) value;
	if (index > 127) index = 32; // not defined beyond index 127, so we use the space char
	uint8_t v = charTable[index];
	if (dp) {
		v |= 0b10000000;
	}
	status[offset + digit] = v;
	spiTransfer(addr, digit + 1, v);
}
Ejemplo n.º 16
0
/**
 * Erase a sector full of bytes to all 1's at the given byte offset in the flash chip.
 */
void m25p16_eraseSector(uint32_t address)
{
    uint8_t out[] = { M25P16_INSTRUCTION_SECTOR_ERASE, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF};

    m25p16_waitForReady(SECTOR_ERASE_TIMEOUT_MILLIS);

    m25p16_writeEnable();

    ENABLE_M25P16;

    spiTransfer(M25P16_SPI_INSTANCE, NULL, out, sizeof(out));

    DISABLE_M25P16;
}
/** \brief SPI command.

    Setup DC and SS pins, then send command via SPI to SSD1306 controller.
*/
void MicroOLED::command(uint8_t c) {

	if (interface == MODE_SPI)
	{
		digitalWrite(dcPin, LOW);
		digitalWrite(csPin, LOW);
		spiTransfer(c);
		digitalWrite(csPin, HIGH);
	}
	else if (interface == MODE_I2C)
	{
		digitalWrite(dcPin, LOW);	// DC pin LOW
		i2cWrite(I2C_ADDRESS_SA0_1, I2C_COMMAND, c);
	}
}
Ejemplo n.º 18
0
void CC2500xcvr::reset()
// REFERENCES:	19.1.2 "Manual Reset" in [1]
{
    // enable device
    digitalWrite(m_pinCS_n, LOW);
    delayMicroseconds(1);

    // disable device and wait at least 40 microseconds
    digitalWrite(m_pinCS_n, HIGH);
    delayMicroseconds(41);
    
	spiTransactionBegin();	// enable device
    spiTransfer(0x30);	// send reset command (SRES)
    spiTransactionEnd(); 	// disable device
}
Ejemplo n.º 19
0
void writeFlash(uint16_t address, uint16_t length)
{
	if (length > _param.flash_pagesize || length > BUFF_LENGTH)
	{
		_error = true;
		Serial.write(STK_FAILED);
		return;
	}

	fill(length);

	if (!receiveEop())
		return;

	for (uint8_t *p = _buff, word_length = length >> 1, i = 0; i < word_length;
			i++)
	{
		spiTransfer(0x40, i, *p++);
		spiTransfer(0x48, i, *p++);
	}
	spiTransfer(0x4C, getPage(address), 0);

	Serial.write(STK_OK);
}
/*
 * Class:     io_silverspoon_bulldog_linux_jni_NativeSpi
 * Method:    spiTransfer
 * Signature: (IJJIIII)I
 */
JNIEXPORT jint JNICALL Java_io_silverspoon_bulldog_linux_jni_NativeSpi_spiTransfer
  (JNIEnv * env , jclass clazz, jint fileDescriptor, jobject txBuffer, jobject rxBuffer, jint transferLength, jint delay, jint speed, jint bitsPerWord) {
	unsigned int* pTx = NULL;
	unsigned int* pRx = NULL;

	if(txBuffer != NULL) {
		pTx = (unsigned int*) (*env)->GetDirectBufferAddress(env, txBuffer);
	}

	if(rxBuffer != NULL) {
		pRx = (unsigned int*) (*env)->GetDirectBufferAddress(env, rxBuffer);
	}

	return spiTransfer((int)fileDescriptor, (unsigned int*)pTx, (unsigned int*)pTx, (int)transferLength, (int)delay, (int)speed, (int)bitsPerWord);
}
/** \brief SPI data.

    Setup DC and SS pins, then send data via SPI to SSD1306 controller.
*/
void MicroOLED::data(uint8_t c) {

	if (interface == MODE_SPI)
	{
		digitalWrite(dcPin, HIGH);
		digitalWrite(csPin, LOW);
		spiTransfer(c);
		digitalWrite(csPin, HIGH);
	}
	else if (interface == MODE_I2C)
	{

		digitalWrite(dcPin, HIGH); // DC pin HIGH
		i2cWrite(I2C_ADDRESS_SA0_1, I2C_DATA, c);
	}
}
Ejemplo n.º 22
0
void LedControl::setDigit(int addr, int digit, byte value, boolean dp) {
    int offset;
    byte v;

    if(addr<0 || addr>=maxDevices)
	return;
    if(digit<0 || digit>7 || value>15)
	return;
    offset=addr*8;
    v=charTable[value];
    if(dp)
	v|=B10000000;
    status[offset+digit]=v;
    spiTransfer(addr, digit+1,v);
    
}
Ejemplo n.º 23
0
void beginProgramming()
{
	SPI.begin();

	digitalWrite(RESET, HIGH);
	pinMode(RESET, OUTPUT);

	digitalWrite(SCK, LOW);
	digitalWrite(RESET, LOW);
	digitalWrite(RESET, HIGH);
	digitalWrite(RESET, LOW);

	delay(20);

	spiTransfer(0xAC, 0x53, 0x00, 0x00);
	_programming = true;
}
Ejemplo n.º 24
0
int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams)
{
    char _data = 0;
    int i =0, ii = 0;

    char    *index[WL_SSID_MAX_LENGTH];

    for (i = 0 ; i < WL_NETWORKS_LIST_MAXNUM ; i++)
            index[i] = (char *)params + WL_SSID_MAX_LENGTH*i;

    IF_CHECK_START_CMD(_data)
    {
        CHECK_DATA(cmd | REPLY_FLAG, _data){};

        uint8_t numParam = readChar();

        if (numParam > maxNumParams)
        {
            numParam = maxNumParams;
        }
        *numParamRead = numParam;
        if (numParam != 0)
        {
            for (i=0; i<numParam; ++i)
            {
            	uint8_t paramLen = readParamLen8();
                for (ii=0; ii<paramLen; ++ii)
                {
                	//ssid[ii] = spiTransfer(DUMMY_DATA);
                    // Get Params data
                    index[i][ii] = (uint8_t)spiTransfer(DUMMY_DATA);

                }
                index[i][ii]=0;
            }
        } else
        {
            WARN("Error numParams == 0");
            readAndCheckChar(END_CMD, &_data);
            return 0;
        }
        readAndCheckChar(END_CMD, &_data);
    }
    return 1;
}
Ejemplo n.º 25
0
uint8_t detectSpiDevice(void)
{
    uint8_t out[] = { M25P16_INSTRUCTION_RDID, 0, 0, 0 };
    uint8_t in[4];
    uint32_t flash_id;

    // try autodetect flash chip
    delay(50); // short delay required after initialisation of SPI device instance.
    ENABLE_SPI_CS;
    spiTransfer(NAZE_SPI_INSTANCE, in, out, sizeof(out));
    DISABLE_SPI_CS;

    flash_id = in[1] << 16 | in[2] << 8 | in[3];
    if (flash_id == FLASH_M25P16_ID)
        return SPI_DEVICE_FLASH;

    return SPI_DEVICE_NONE;
}
Ejemplo n.º 26
0
void LedControl::setLed(int addr, int row, int column, boolean state) {
    int offset;
    byte val=0x00;

    if(addr<0 || addr>=maxDevices)
	return;
    if(row<0 || row>7 || column<0 || column>7)
	return;
    offset=addr*8;
    val=B10000000 >> column;
    if(state)
	status[offset+row]=status[offset+row]|val;
    else {
	val=~val;
	status[offset+row]=status[offset+row]&val;
    }
    spiTransfer(addr, row+1,status[offset+row]);
}
Ejemplo n.º 27
0
/**
 * \brief Transfer data full-duplex over SPI
 *
 * Transfer raw data full-duplex over a wire.
 * \param[in,out] buffer	Data to send. Received data is returned here.
 * \param 		size		Size of data to send.
 * \returns
 * Data is returned in the buffer. \n
 * return 0 always.
 */
int spiTransfer(char *buffer, int size) {
	// only a certain amount of byte can be written at a time. see below
	int bounded_size = (size > WRITE_MAX_BYTES ? WRITE_MAX_BYTES : size);
	struct timespec req = { 0, WRITE_CYCLE_U*1000L };
	struct timespec rem;

	wiringPiSPIDataRW(0, (unsigned char*)buffer, bounded_size);
	// Wait for the write to cycle
	//usleep(WRITE_CYCLE_U);
	nanosleep(&req, &rem);
	if (size > WRITE_MAX_BYTES) {
		// Only a certain amount of bytes can be writeen at a time
		// If we go over that limit, break the write up into multiple
		// chunks
		spiTransfer(&buffer[WRITE_MAX_BYTES], size-WRITE_MAX_BYTES);
	}

	return 0;
}
Ejemplo n.º 28
0
void MAX7219::setLed(int row, int column, bool state, int addr) {
	if (addr < 0 || addr >= maxDevices) return;

	int offset;
	uint8_t val = 0;

	if (row < 0 || row > 7 || column < 0 || column > 7) {
		return;
	}
	offset = addr * 8;
	val = 0b10000000 >> column;
	if (state) {
		status[offset + row] = status[offset + row] | val;
	} else {
		val = ~val;
		status[offset + row] = status[offset + row] & val;
	}
	spiTransfer(addr, row + 1, status[offset + row]);
}
/** \brief Send the display a command byte

    Send a command via SPI, I2C or parallel	to SSD1306 controller.
	For SPI we set the DC and CS pins here, and call spiTransfer(byte)
	to send the data. For I2C and Parallel we use the write functions
	defined in hardware.cpp to send the data.
*/
void MicroOLED::command(uint8_t c) {

	if (interface == MODE_SPI)
	{
		digitalWrite(dcPin, LOW);;	// DC pin LOW for a command
		spiTransfer(c);			// Transfer the command byte
	}
	else if (interface == MODE_I2C)
	{
		// Write to our address, make sure it knows we're sending a
		// command:
		i2cWrite(i2c_address, I2C_COMMAND, c);
	}
	else if (interface == MODE_PARALLEL)
	{
		// Write the byte to our parallel interface. Set DC LOW.
		parallelWrite(c, LOW);
	}
}
/** \brief Send the display a data byte

    Send a data byte via SPI, I2C or parallel to SSD1306 controller.
	For SPI we set the DC and CS pins here, and call spiTransfer(byte)
	to send the data. For I2C and Parallel we use the write functions
	defined in hardware.cpp to send the data.
*/
void MicroOLED::data(uint8_t c) {

	if (interface == MODE_SPI)
	{
		digitalWrite(dcPin, HIGH);	// DC HIGH for a data byte

		spiTransfer(c); 		// Transfer the data byte
	}
	else if (interface == MODE_I2C)
	{
		// Write to our address, make sure it knows we're sending a
		// data byte:
		i2cWrite(i2c_address, I2C_DATA, c);
	}
	else if (interface == MODE_PARALLEL)
	{
		// Write the byte to our parallel interface. Set DC HIGH.
		parallelWrite(c, HIGH);
	}
}