Exemplo n.º 1
0
void I2C_OnRxChar(void) {
  uint8_t data;
  uint16_t nofBytesReceived;
  uint8_t res;

  dbgF(I2C_DEBUG_RxChar);
  res = I2C1_RecvBlock(&data, 1, &nofBytesReceived);
  if (res == ERR_OK) { /* ok, no error */
    if (memDevice.nextRXisMemAddress) { /* it is the memory address received from the master */
      memDevice.nextRXisMemAddress = FALSE;
      memDevice.addr = data;
    } else { /* data byte received from the master to write to the memory */
      if (memDevice.addr<sizeof(memDevice.u)) {
        memDevice.u.mem[memDevice.addr] = data;
        memDevice.addr++;
      }
    }
  }
}
Exemplo n.º 2
0
      }
      EnterCritical();
      memDevice.u.data.cmdLength = strSize;
      UTIL1_strcpy(memDevice.u.data.cmd, sizeof(memDevice.u.data.cmd), buf);
      ExitCritical();
    }
#endif /* PL_HAS_UI */
}


#if 0 &&  PL_APP_MODE_I2C_TWR
  byte res;
  byte buf[1];
  word nofBytesReceived;

  res = I2C1_RecvBlock(&buf, sizeof(buf), &nofBytesReceived);
  if (res == ERR_OK) {
    switch(buf[0]) {
      case EVNT1_BUTTON_UI_LEFT:
      case EVNT1_BUTTON_UI_RIGHT:
      case EVNT1_BUTTON_UI_UP:
      case EVNT1_BUTTON_UI_DOWN:
      case EVNT1_BUTTON_UI_MIDDLE:
        EVNT1_SetEvent(buf[0]);
        break;
    } /* switch */
  }
#endif

void I2C_HandleEvent(byte event) {
#if PL_APP_MODE_I2C_LCD
Exemplo n.º 3
0
void I2C_OnReceiveData(void) {
#if PL_IS_I2C_SLAVE
  uint8_t data, res;
  uint16_t nofBytesReceived;

  res = I2C1_RecvBlock(&data, 1, &nofBytesReceived);
  if (res == ERR_OK && nofBytesReceived==1) { /* ok, no error */
    if (bufIdx==0 && data==I2C_PRE_BYTE0) {
      buf[0] = data;
      bufIdx++;
      return;
    } else if (bufIdx==1 && data==I2C_PRE_BYTE1 && buf[0]==I2C_PRE_BYTE0) {
      buf[1] = data;
      bufIdx++;
      return;
    } else if (bufIdx==2) {
      buf[2] = data; /* size */
      bufIdx++;
      return;
    /*------------------------------------------------*/
    } else if (bufIdx==3 && data==I2C_MSG_MOTOR_SPEED && buf[2]==I2C_MOTOR_MSG_LEN) { /* start of motor speed message */
      buf[3] = data; /* I2C_MSG_MOTOR_SPEED */
      bufIdx++;
      return;
    } else if (bufIdx>=4 && buf[3]==I2C_MSG_MOTOR_SPEED && bufIdx <= I2C_MOTOR_MSG_MAX_IDX) { /* motor speed message */
      buf[bufIdx++] = data;
    /*------------------------------------------------*/
    } else if (bufIdx==3 && data==I2C_MSG_PID_SPEED && buf[2]==I2C_PID_MSG_LEN) { /* start of motor speed message */
      buf[3] = data; /* I2C_MSG_PID_SPEED */
      bufIdx++;
      return;
    } else if (bufIdx>=4 && buf[3]==I2C_MSG_PID_SPEED && bufIdx <= I2C_PID_MSG_MAX_IDX) { /* motor speed message */
      buf[bufIdx++] = data;
    /*------------------------------------------------*/
    } else if (bufIdx==3 && data==I2C_ACCEL_XYZ && buf[2]==I2C_ACCEL_MSG_LEN) { /* start of Accel message */
      buf[3] = data; /* I2C_ACCEL_XYZ */
      bufIdx++;
      return;
    } else if (bufIdx>=4 && buf[3]==I2C_ACCEL_XYZ && bufIdx <= I2C_ACCEL_MSG_MAX_IDX) { /* Accel message */
      buf[bufIdx++] = data;
    /*------------------------------------------------*/
    } else if (bufIdx==3 && data==I2C_MSG_GET_CMD && buf[2]==I2C_GET_CMD_MSG_LEN) { /* start of GetCMD message */
      buf[3] = data; /* I2C_GET_CMD */
      bufIdx++;
      return;
    } else if (bufIdx>=4 && buf[3]==I2C_MSG_GET_CMD && bufIdx <= I2C_GET_CMD_MSG_MAX_IDX) { /* GetCmd message */
      buf[bufIdx++] = data;
    /*------------------------------------------------*/
#if PL_HAS_RUNNER
    } else if (bufIdx==3 && data==I2C_MSG_RUNNER ) { /* start of runner message */
      buf[3] = data; /* I2C_MSG_RUNNER */
      bufIdx++;
      return;
    } else if (bufIdx>=4 && buf[3]==I2C_MSG_RUNNER && bufIdx < sizeof(buf)) { /* runner message */
      buf[bufIdx++] = data;
      if (data != '\0') {
        return; /* get next byte */
      }
#endif
    } else {
      bufIdx = 0;
      return;
    }
  } else { /* error */
    bufIdx = 0;
    return;
  }
  if (data==0) { /* message finished? */
    if (buf[3]==I2C_ACCEL_XYZ && buf[2]==I2C_ACCEL_MSG_LEN && bufIdx>I2C_ACCEL_MSG_MAX_IDX) { /* message finished */
      AccelX = (int16_t)((buf[4]<<8)|buf[5]);
      AccelY = (int16_t)((buf[6]<<8)|buf[7]);
      AccelZ = (int16_t)((buf[8]<<8)|buf[9]);
      bufIdx = 0; /* message completed */
    } else if (buf[3]==I2C_MSG_MOTOR_SPEED && buf[2]==I2C_MOTOR_MSG_LEN && bufIdx>I2C_MOTOR_MSG_MAX_IDX) { /* message finished */
      motorSpeed = (int32_t)((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
      bufIdx = 0; /* message completed */
    } else if (buf[3]==I2C_MSG_PID_SPEED && buf[2]==I2C_PID_MSG_LEN && bufIdx>I2C_PID_MSG_MAX_IDX) { /* message finished */
      PIDSpeed = (int32_t)((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
      bufIdx = 0; /* message completed */
    } else if (buf[3]==I2C_MSG_GET_CMD && buf[2]==I2C_GET_CMD_MSG_LEN && bufIdx>I2C_GET_CMD_MSG_MAX_IDX) { /* message finished */
      sendCmd = TRUE;
      bufIdx = 0; /* message completed */
#if PL_HAS_RUNNER
    } else if (buf[3]==I2C_MSG_RUNNER) { /* message finished */
      UTIL1_strcpy(RunnerBuf, sizeof(RunnerBuf), &buf[4]);
      bufIdx = 0; /* message completed */
      if (I2C_RunnerQueue!=NULL) {
        signed portBASE_TYPE higherTaskWoken;

        FRTOS1_xQueueSendToBackFromISR(I2C_RunnerQueue, RunnerBuf, &higherTaskWoken);
      }
#endif
    }
  }
  if (bufIdx>sizeof(buf)) {
    bufIdx = 0;
  }
#else
#if PL_APP_MODE_I2C_TWR
  byte res;
  byte buf[1];
  word nofBytesReceived;

  res = I2C1_RecvBlock(&buf, sizeof(buf), &nofBytesReceived);
  if (res == ERR_OK) {
    switch(buf[0]) {
      case EVNT1_BUTTON_UI_LEFT:
      case EVNT1_BUTTON_UI_RIGHT:
      case EVNT1_BUTTON_UI_UP:
      case EVNT1_BUTTON_UI_DOWN:
      case EVNT1_BUTTON_UI_MIDDLE:
        EVNT1_SetEvent(buf[0]);
        break;
    } /* switch */
  }
#endif
#endif
}