kx122_context kx122_init(int bus, int addr, int chip_select_pin, int spi_bus_frequency) { kx122_context dev = (kx122_context)malloc(sizeof(struct _kx122_context)); if(!dev){ return NULL; } dev->using_spi = false; dev->i2c = NULL; dev->spi = NULL; dev->chip_select = NULL; dev->gpio1 = NULL; dev->gpio2 = NULL; if(mraa_init() != MRAA_SUCCESS){ printf("%s: mraa_init() failed.\n", __FUNCTION__); kx122_close(dev); return NULL; } if(addr == -1){ dev->using_spi = true; } if(dev->using_spi){ if (spi_bus_frequency > 10000000){ // KX122 has a maximum SPI bus speed of 10MHz printf("%s: bus frequency too high - KX122 has a maximum SPI bus speed of 10MHz.\n", __FUNCTION__); kx122_close(dev); return NULL; } if (!(dev->spi = mraa_spi_init(bus))){ printf("%s: mraa_spi_init() failed.\n", __FUNCTION__); kx122_close(dev); return NULL; } if (!(dev->chip_select = mraa_gpio_init(chip_select_pin))){ printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__); kx122_close(dev); return NULL; } mraa_gpio_dir(dev->chip_select,MRAA_GPIO_OUT); mraa_spi_mode(dev->spi,MRAA_SPI_MODE0); if (mraa_spi_frequency(dev->spi, spi_bus_frequency)){ printf("%s: mraa_spi_frequency() failed.\n", __FUNCTION__); kx122_close(dev); return NULL; } } else{ //Using I2C if (!(dev->i2c = mraa_i2c_init(bus))){ printf("%s: mraa_i2c_init() failed, used bus: %d\n", __FUNCTION__,bus); kx122_close(dev); return NULL; } if (mraa_i2c_address(dev->i2c, addr)){ printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__); kx122_close(dev); return NULL; } } uint8_t who_am_i; kx122_get_who_am_i(dev,&who_am_i); if(who_am_i != KX122_WHO_AM_I_WIA_ID){ printf("%s: Wrong WHO AM I received, expected: 0x%x | got: 0x%x\n", __FUNCTION__,KX122_WHO_AM_I_WIA_ID,who_am_i); kx122_close(dev); return NULL; } kx122_set_default_values(dev); kx122_device_init(dev,KX122_ODR_50,HIGH_RES,KX122_RANGE_2G); return dev; }
uint8_t RF22::init() { // Wait for RF22 POR (up to 16msec) usleep (16); // Initialise the slave select pin _cs = mraa_gpio_init(_slaveSelectPin); mraa_gpio_dir(_cs, MRAA_GPIO_OUT); mraa_gpio_write(_cs, 0x1); // start the SPI library: // Note the RF22 wants mode 0, MSB first and default to 1 Mbps _spi = mraa_spi_init(0); mraa_spi_mode (_spi, MRAA_SPI_MODE0); mraa_spi_lsbmode(_spi, 0); mraa_spi_frequency(_spi, 1000000); // 1Mhz usleep (100); // Software reset the device reset(); // Get the device type and check it // This also tests whether we are really connected to a device _deviceType = spiRead(RF22_REG_00_DEVICE_TYPE); if ( _deviceType != RF22_DEVICE_TYPE_RX_TRX && _deviceType != RF22_DEVICE_TYPE_TX) return 0; _irq = mraa_gpio_init(_interrupt + 2); mraa_gpio_dir(_irq, MRAA_GPIO_IN); gpio_edge_t edge = MRAA_GPIO_EDGE_FALLING; // Set up interrupt handler if (_interrupt == 0) { _RF22ForInterrupt[0] = this; mraa_gpio_isr(_irq, edge, &RF22::isr0, NULL); } else if (_interrupt == 1) { _RF22ForInterrupt[1] = this; mraa_gpio_isr(_irq, edge, &RF22::isr1, NULL); } else return 0; clearTxBuf(); clearRxBuf(); // Most of these are the POR default spiWrite(RF22_REG_7D_TX_FIFO_CONTROL2, RF22_TXFFAEM_THRESHOLD); spiWrite(RF22_REG_7E_RX_FIFO_CONTROL, RF22_RXFFAFULL_THRESHOLD); spiWrite(RF22_REG_30_DATA_ACCESS_CONTROL, RF22_ENPACRX | RF22_ENPACTX | RF22_ENCRC | RF22_CRC_CRC_16_IBM); // Configure the message headers // Here we set up the standard packet format for use by the RF22 library // 8 nibbles preamble // 2 SYNC words 2d, d4 // Header length 4 (to, from, id, flags) // 1 octet of data length (0 to 255) // 0 to 255 octets data // 2 CRC octets as CRC16(IBM), computed on the header, length and data // On reception the to address is check for validity against RF22_REG_3F_CHECK_HEADER3 // or the broadcast address of 0xff // If no changes are made after this, the transmitted // to address will be 0xff, the from address will be 0xff // and all such messages will be accepted. This permits the out-of the box // RF22 config to act as an unaddresed, unreliable datagram service spiWrite(RF22_REG_32_HEADER_CONTROL1, RF22_BCEN_HEADER3 | RF22_HDCH_HEADER3); spiWrite(RF22_REG_33_HEADER_CONTROL2, RF22_HDLEN_4 | RF22_SYNCLEN_2); setPreambleLength(8); uint8_t syncwords[] = { 0x2d, 0xd4 }; setSyncWords(syncwords, sizeof(syncwords)); setPromiscuous(0); // Check the TO header against RF22_DEFAULT_NODE_ADDRESS spiWrite(RF22_REG_3F_CHECK_HEADER3, RF22_DEFAULT_NODE_ADDRESS); // Set the default transmit header values setHeaderTo(RF22_DEFAULT_NODE_ADDRESS); setHeaderFrom(RF22_DEFAULT_NODE_ADDRESS); setHeaderId(0); setHeaderFlags(0); // Ensure the antenna can be switched automatically according to transmit and receive // This assumes GPIO0(out) is connected to TX_ANT(in) to enable tx antenna during transmit // This assumes GPIO1(out) is connected to RX_ANT(in) to enable rx antenna during receive spiWrite (RF22_REG_0B_GPIO_CONFIGURATION0, 0x12) ; // TX state spiWrite (RF22_REG_0C_GPIO_CONFIGURATION1, 0x15) ; // RX state // Enable interrupts spiWrite(RF22_REG_05_INTERRUPT_ENABLE1, RF22_ENTXFFAEM | RF22_ENRXFFAFULL | RF22_ENPKSENT | RF22_ENPKVALID | RF22_ENCRCERROR | RF22_ENFFERR); spiWrite(RF22_REG_06_INTERRUPT_ENABLE2, RF22_ENPREAVAL); // Set some defaults. An innocuous ISM frequency, and reasonable pull-in setFrequency(434.0, 0.05); // setFrequency(900.0); // Some slow, reliable default speed and modulation setModemConfig(FSK_Rb2_4Fd36); // setModemConfig(FSK_Rb125Fd125); // Minimum power setTxPower(RF22_TXPOW_8DBM); // setTxPower(RF22_TXPOW_17DBM); return 1; }
int main(int argc, char **argv) { log_flag = 1; static struct option long_opts[] = { {"period", required_argument, NULL, 'p'}, {"scale", required_argument, NULL, 's'}, {"log", required_argument, NULL, 'l'}, {0, 0, 0, 0} }; int opt; while ((opt = getopt_long(argc, argv, "p:s:l:", long_opts, NULL)) >= 0) { if (opt == 'p') { duration = atoi(optarg); } else if (opt == 's') { switch (optarg[0]) { case 'C': scale = 'C'; break; case 'F': scale = 'F'; break; default: fprintf(stderr, "ERROR: incorrect scale ussage. Correct usage: --scale=(C/F)\n"); exit(2); } } else if (opt == 'l') { myFH = optarg; logOpt = 1; } else { fprintf(stderr, "ERROR: incorrect usage. Correct usage: ./lab4b --period=(duration) --scale=(C/F) --log\n"); exit(1); } } if (logOpt > 0) myFile = fopen(myFH, "a"); tempSensor = mraa_aio_init(1); if (tempSensor == NULL) { fprintf(stderr, "ERROR: failed to detect temperature sensor\n"); exit(1); } buttonSensor = mraa_gpio_init(73); if (buttonSensor == NULL) { fprintf(stderr, "ERROR: failed to detect button\n"); exit(1); } pthread_t *trd = malloc(sizeof(pthread_t)*3); pthread_create(trd, 0, logTemperature, 0); pthread_create(trd + 1, 0, checkButton, 0); pthread_create(trd + 2, 0, readInput, 0); pthread_join(*(trd), 0); pthread_join(*(trd + 1), 0); pthread_join(*(trd + 2), 0); free(trd); if (logOpt > 0) fclose(myFile); mraa_gpio_close(buttonSensor); mraa_aio_close(tempSensor); exit(0); }