Beispiel #1
0
/*---------------------------------------------------------------------------*/
uint8_t
i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
{
  uint8_t recv = 0;
  if((len == 0) || data == NULL) {
    return I2CM_STAT_INVALID;
  }
  if(len == 1) {
    return i2c_single_receive(slave_addr, &data[0]);
  }
  i2c_master_set_slave_address(slave_addr, I2C_RECEIVE);
  i2c_master_command(I2C_MASTER_CMD_BURST_RECEIVE_START);
  while(i2c_master_busy());
  if(i2c_master_error() == I2C_MASTER_ERR_NONE) {
    data[0] = i2c_master_data_get();
    /* If we got 2 or more bytes pending to be received, keep going*/
    for(recv = 1; recv <= (len - 2); recv++) {
      i2c_master_command(I2C_MASTER_CMD_BURST_RECEIVE_CONT);
      while(i2c_master_busy());
      data[recv] = i2c_master_data_get();
    }
    /* This should be the last byte, stop receiving */
    i2c_master_command(I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    while(i2c_master_busy());
    data[len - 1] = i2c_master_data_get();
  }
  return i2c_master_error();
}
Beispiel #2
0
/*---------------------------------------------------------------------------*/
uint8_t
i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
{
  uint8_t sent;
  if((len == 0) || (data == NULL)) {
    return I2CM_STAT_INVALID;
  }
  if(len == 1) {
    return i2c_single_send(slave_addr, data[0]);
  }
  i2c_master_set_slave_address(slave_addr, I2C_SEND);
  i2c_master_data_put(data[0]);
  i2c_master_command(I2C_MASTER_CMD_BURST_SEND_START);
  while(i2c_master_busy());
  if(i2c_master_error() == I2C_MASTER_ERR_NONE) {
    for(sent = 1; sent <= (len - 2); sent++) {
      i2c_master_data_put(data[sent]);
      i2c_master_command(I2C_MASTER_CMD_BURST_SEND_CONT);
      while(i2c_master_busy());
    }
    /* This should be the last byte, stop sending */
    i2c_master_data_put(data[len - 1]);
    i2c_master_command(I2C_MASTER_CMD_BURST_SEND_FINISH);
    while(i2c_master_busy());
  }

  /* Return the STAT register of I2C module if error occurred, I2C_MASTER_ERR_NONE otherwise */
  return i2c_master_error();
}
Beispiel #3
0
/*---------------------------------------------------------------------------*/
uint8_t sht21_read(uint16_t * data, uint8_t regist)
{
  uint16_t temp;
  uint8_t dataByte[2];
  if(regist != SHT21_TEMP_REGISTER && regist != SHT21_HUMI_REGISTER) {
    return -1;
  }

  i2c_master_set_slave_address(SHT21_SLAVE_ADDRESS, I2C_SEND);
  i2c_master_data_put(regist);
  i2c_master_command(I2C_MASTER_CMD_BURST_SEND_START);
  while (i2c_master_busy()) {
  }
  if(i2c_master_error() == I2C_MASTER_ERR_NONE) {
    if(regist == SHT21_TEMP_REGISTER) {
      for (temp = 0; temp < 10; temp++) {
        clock_delay_usec(8500); //85ms
      }
    } else if(regist == SHT21_HUMI_REGISTER) {
      for (temp = 0; temp < 10; temp++) {
        clock_delay_usec(2900); //29ms
      }
    }
    /* Get the 2 bytes of data*/
    /* Data MSB */
    i2c_master_set_slave_address(SHT21_SLAVE_ADDRESS, I2C_RECEIVE);
    i2c_master_command(I2C_MASTER_CMD_BURST_RECEIVE_START);
    while (i2c_master_busy()) {
    }
    if(i2c_master_error() == I2C_MASTER_ERR_NONE) {
      *data = i2c_master_data_get() << 8;

      /* Data LSB */
      i2c_master_command(I2C_MASTER_CMD_BURST_RECEIVE_CONT);
      while (i2c_master_busy()) {
      }
      if(i2c_master_error() == I2C_MASTER_ERR_NONE) {
        *data |= i2c_master_data_get();

        /* Checksum */
        i2c_master_command(I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
        while (i2c_master_busy()) {
        }
        if(i2c_master_error() == I2C_MASTER_ERR_NONE) {
          dataByte[0] = (*data) >> 8;
          dataByte[1] = (*data) & 0x00FF;
          if(sht21_check_crc(dataByte, 2,
              i2c_master_data_get()) == I2C_MASTER_ERR_NONE) {
            return I2C_MASTER_ERR_NONE;
          }
        }
      }
Beispiel #4
0
/*---------------------------------------------------------------------------*/
void
bme280_arch_i2c_read_mem(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t bytes)
{
  i2c_master_enable();
  if(i2c_single_send(addr, reg) == I2C_MASTER_ERR_NONE) {
    while(i2c_master_busy());
    i2c_burst_receive(addr, buf, bytes);
  }
}
Beispiel #5
0
/*---------------------------------------------------------------------------*/
uint8_t
i2c_single_send(uint8_t slave_addr, uint8_t data)
{
  i2c_master_set_slave_address(slave_addr, I2C_SEND);
  i2c_master_data_put(data);
  i2c_master_command(I2C_MASTER_CMD_SINGLE_SEND);

  while(i2c_master_busy());

  /* Return the STAT register of I2C module if error occured, I2C_MASTER_ERR_NONE otherwise */
  return i2c_master_error();
}
Beispiel #6
0
/*---------------------------------------------------------------------------*/
uint8_t
i2c_single_receive(uint8_t slave_addr, uint8_t *data)
{
  uint32_t temp;

  i2c_master_set_slave_address(slave_addr, I2C_RECEIVE);
  i2c_master_command(I2C_MASTER_CMD_SINGLE_RECEIVE);

  while(i2c_master_busy());
  temp = i2c_master_error();
  if(temp == I2C_MASTER_ERR_NONE) {
    *data = i2c_master_data_get();
  }
  return temp;
}
Beispiel #7
0
void uart_recv_state(unsigned char byte) {
    
    switch (uc_ptr->state) {

        case UART_STATE_HEADER1:
            if ( byte == UART_HEADER1) {
//                WriteUSART(0x01);
                uc_ptr->state = UART_STATE_HEADER2;
            }
            break;

        case UART_STATE_HEADER2:
            if ( byte == UART_HEADER2 ) {
                //WriteUSART(0x02);
#if defined (ARM_PIC)
                uc_ptr->state = UART_STATE_COUNT;
#elif defined (MAIN_PIC)
                uc_ptr->state = UART_STATE_MSGTYPE;
#endif
            }
            else {
                uc_ptr->state = UART_STATE_HEADER1;
            }
            break;

        case UART_STATE_MSGTYPE:
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
//            WriteUSART(0x03);
            uc_ptr->msgtype = byte;
            uc_ptr->buffer[0] = byte;
            uc_ptr->state = UART_STATE_COUNT;
            break;

        case UART_STATE_COUNT:
//            WriteUSART(0x04);
            uc_ptr->count = byte;
#if defined (ARM_PIC)
                uc_ptr->state = UART_STATE_LENGTH;
                uc_ptr->buffer[0] = byte;
#elif defined (MAIN_PIC)
                uc_ptr->buffer[1] = byte;
                uc_ptr->state = UART_STATE_FOOTER;
#endif    
             break;

        case UART_STATE_LENGTH:
//            WriteUSART(0x05);
            uc_ptr->buffer[1] = byte;
            uc_ptr->data_length = byte;
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
            uc_ptr->state = UART_STATE_DATA;
            break;
            
        case UART_STATE_DATA:
//            WriteUSART(0x06);
            
            // Store the byte into buffer
            if ( uc_ptr->data_read + 2 < MAXUARTBUF ) {             
                uc_ptr->buffer[uc_ptr->data_read+2] = byte;
            }
            
            // Increment because we read 1 more byte
            uc_ptr->data_read += 1;
            
            // Keep reading data until, 9 BYTES for now
            if(UARTDATALEN == uc_ptr->data_read) {
                uc_ptr->state = UART_STATE_FOOTER;
            }
            
            break;
        case UART_STATE_FOOTER:
            // WriteUSART(0x07);
            if ( byte == UART_FOOTER ) {
            // WriteUSART(0x08);
                uc_ptr->state = UART_STATE_HEADER1;
                //LATBbits.LATB7 = !LATBbits.LATB7;

#if defined (ARM_PIC)
                // Copy over into a buffer before passing
                unsigned char temp[I2CMSGLEN];
                int i;
                for (i = 0; i < I2CMSGLEN; i++) {
                    temp[i] = uc_ptr->buffer[i];
                }
                LATB = 7; // Sequence 7
                LATAbits.LA0 = 0;
                // Put it in the roverDataBuf
                ToMainLow_sendmsg(I2CMSGLEN, MSGT_BUF_PUT_DATA, (void *) temp);
#elif defined (MAIN_PIC)
                // Wait for first to finish
                
                LATBbits.LATB0 = 1;
                LATBbits.LATB1 = 0;
                LATBbits.LATB2 = 0; // Sequence 1
                LATAbits.LA0 = 0;

                i2c_master_cmd message;

                message.msgType = uc_ptr->buffer[0];
                message.msgCount = uc_ptr->buffer[1];


                //if ( i2c_master_busy() == 0 )
                    // Put the message in the queue
                while(i2c_master_busy());
                putQueue(&i2c_q,message);
                //ToMainLow_sendmsg(2, MSGT_BUF_PUT_DATA, (void *) uc_ptr->buffer);
#endif
            }
            else {
                uc_ptr->state = UART_STATE_HEADER1;
            }
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
            break;
        default:
            break;
    }
    
}
Beispiel #8
0
void timer1_int_handler() {
    unsigned int result;

    // read the timer and then send an empty message to main()
    //LATBbits.LATB7 = !LATBbits.LATB7;
    result = ReadTimer1();
    //ToMainLow_sendmsg(0, MSGT_TIMER1, (void *) 0);
    

#if defined (MOTOR_PIC)
    // reset the timer
    WriteTimer1(TIMER1_START);
    ticks_left++;
    
    if ( executingEncode && ticks_left_C > 0)
        ticks_left_C--;
    if ( executingEncodeLong && ticks_left_C_Long > 0)
        ticks_left_C_Long--;

    ticks_left_total++;

    if ( ticks_left_C  <= 1 && ticks_right_C <= 1 && executingEncode ) {
        if ( motor_state == RoverMsgMotorForward)
            RoverForward();
        
        else if ( motor_state == RoverMsgMotorLeft2 || motor_state == RoverMsgMotorRight2 ) {
            motor_encode_lthread(RoverMsgMotorForwardCMDelim+10);
        }
        else
            RoverStop();

         executingEncode = 0;
    }
    else if ( ticks_right_C_Long  <= 1 && ticks_left_C_Long <= 1 && executingEncodeLong ) {
        if ( motor_state == RoverMsgMotorLeft2 ) {
            motor_encode_lthread(RoverMsgMotorRight2);
        }
        else if ( motor_state == RoverMsgMotorRight2 )
        {
            motor_encode_lthread(RoverMsgMotorLeft2);
        }
        else {
         RoverStop();
        }

         motor_state = RoverMsgMotorStop;
         executingEncodeLong = 0;
    }
    
#elif defined(MAIN_PIC)
    if ( !i2c_master_busy() )
        ToMainHigh_sendmsg(10, MSGT_GET_SENSOR_DATA, (void *) 0);
    
    if ( timer1_extender > 100 && tempWallCorrection == 0 ) {
        tempWallCorrection = 1;
    }
    
    timer1_extender++;

    WriteTimer1(0);
#else
    WriteTimer1(0);
#endif

}