Example #1
0
static PyObject *I2CDev_read(I2CDev *self, PyObject *args, PyObject *kwds) {
  uint32_t n_bytes, i, addr;
  PyObject *data, *byte_obj;
  uint8_t *rxbuf; 
  if(!PyArg_ParseTuple(args, "II", &addr, &n_bytes)) {
    return NULL;
  }

  if (self->slave_addr != addr) {
    if (I2C_setSlaveAddress(self->i2c_fd, addr) < 0) {
      PyErr_SetString(PyExc_IOError, "could not configure I2C interface");
      return NULL;
    }
    self->slave_addr = addr;
  }

  rxbuf = malloc(n_bytes);
  if (I2C_read(self->i2c_fd, (void *) rxbuf, n_bytes) < 0) {
    PyErr_SetString(PyExc_IOError, "could not read from I2C device");
    free(rxbuf);
    return NULL;
  }

  data = PyList_New(0);
  for (i=0; i<n_bytes; i++) {
    byte_obj = PyInt_FromLong((long) rxbuf[i]);
    PyList_Append(data, byte_obj);
    Py_DECREF(byte_obj);
  }
  free(rxbuf);
  return data;
}
Example #2
0
static PyObject *I2CDev_write(I2CDev *self, PyObject *args, PyObject *kwds) {
  uint32_t n_bytes, i, addr;
  long byte;
  PyObject *data, *byte_obj;
  uint8_t *txbuf; 
  if(!PyArg_ParseTuple(args, "IO!", &addr, &PyList_Type, &data)) {
    return NULL;
  }

  if (self->slave_addr != addr) {
    if (I2C_setSlaveAddress(self->i2c_fd, addr) < 0) {
      PyErr_SetString(PyExc_IOError, "could not configure I2C interface");
      return NULL;
    }
    self->slave_addr = addr;
  }

  n_bytes = PyList_Size(data);
  txbuf = malloc(n_bytes);

  for (i=0; i<n_bytes; i++) {
    byte_obj = PyList_GetItem(data, i);
    if (!PyInt_Check(byte_obj)) {
      PyErr_SetString(PyExc_ValueError, 
        "data list to transmit can only contain integers");
      free(txbuf);
      return NULL;
    }
    byte = PyInt_AsLong(byte_obj);
    if (byte < 0) {
      // Check for error from PyInt_AsLong:
      if (PyErr_Occurred() != NULL) return NULL;
      // Negative numbers are set to 0:
      byte = 0;
    }
    // Just send the LSB if value longer than 1 byte:
    byte &= 255;
    txbuf[i] = (uint8_t) byte;
  }

  if (I2C_write(self->i2c_fd, (void *) txbuf, n_bytes) < 0) {
    PyErr_SetString(PyExc_IOError, "could not write to I2C device");
    free(txbuf);
    return NULL;
  }
  free(txbuf);
  Py_INCREF(Py_None);
  return Py_None;
}
void main (void)
{
    //Stop WDT
    WDT_hold(__MSP430_BASEADDRESS_WDT_A__);

    //Assign I2C pins to USCI_B0
    GPIO_setAsPeripheralModuleFunctionInputPin(__MSP430_BASEADDRESS_PORT3_R__,
        GPIO_PORT_P3,
        GPIO_PIN1 + GPIO_PIN2
        );

    //Initialize Master
    I2C_masterInit(__MSP430_BASEADDRESS_USCI_B0__,
        I2C_CLOCKSOURCE_SMCLK,
        UCS_getSMCLK(__MSP430_BASEADDRESS_UCS__),
        I2C_SET_DATA_RATE_400KBPS
        );

    //Specify slave address
    I2C_setSlaveAddress(__MSP430_BASEADDRESS_USCI_B0__,
        SLAVE_ADDRESS
        );

    //Set Master in receive mode
    I2C_setMode(__MSP430_BASEADDRESS_USCI_B0__,
        I2C_RECEIVE_MODE
        );

    //Enable I2C Module to start operations
    I2C_enable(__MSP430_BASEADDRESS_USCI_B0__);

    //Enable master Receive interrupt
    I2C_enableInterrupt(__MSP430_BASEADDRESS_USCI_B0__,
        I2C_RECEIVE_INTERRUPT
        );

    while (1)
    {
        //Initiate command to receive a single character from Slave
        I2C_masterSingleReceiveStart(__MSP430_BASEADDRESS_USCI_B0__);

        //Enter low power mode 0 with interrupts enabled.
        //Wait until character is received.
        __bis_SR_register(LPM0_bits + GIE);
        __no_operation();
    }
}