Exemple #1
0
// Get rows from other half over i2c
int i2c_transaction(void) {
    int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;

    int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
    if (err) goto i2c_error;

    // Matrix stored at 0x00-0x03
    err = i2c_master_write(0x00);
    if (err) goto i2c_error;

    // Start read
    err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
    if (err) goto i2c_error;

    if (!err) {
        int i;
        for (i = 0; i < ROWS_PER_HAND-1; ++i) {
            matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
        }
        matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
        i2c_master_stop();
    } else {
i2c_error: // the cable is disconnceted, or something else went wrong
        i2c_reset_state();
        return err;
    }

    return 0;
}
Exemple #2
0
/*
 * PCF8523 slightly changed its 7 bytes encoded in BCD:
 * 03h - Seconds 	- 00-59
 * 04h - Minutes 	- 00-59
 * 05h - Hours   	- 00-23
 * 06h - monthday	- 01-31
 * 07h - weekday	- 00-06
 * 08h - Month   	- 01-12
 * 09h - Year    	- 00-99
 *
 */
time_t rtc_readValue() {
	i2c_cmd_handle_t cmd = i2c_cmd_link_create();
	ESP_ERROR_CHECK(i2c_master_start(cmd));
	ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (RTC_ADDRESS << 1) | I2C_MASTER_WRITE, true /* expect ack */));
	ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 0x03, 1)); // start address
	ESP_ERROR_CHECK(i2c_master_start(cmd));
	ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (RTC_ADDRESS << 1) | I2C_MASTER_READ, true /* expect ack */));
	uint8_t data[7];
	ESP_ERROR_CHECK(i2c_master_read(cmd, data, 7, false));
	ESP_ERROR_CHECK(i2c_master_stop(cmd));
	COMMANDCHECKOKERR(i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS),"RTC COMMAND");
	i2c_cmd_link_delete(cmd);

	int i;
	for (i=0; i<7; i++) {
		ESP_LOGD(tag, "%d: 0x%.2x", i, data[i]);
	}

	struct tm tm;
	tm.tm_sec  = bcdToInt(data[0]);
	tm.tm_min  = bcdToInt(data[1]);
	tm.tm_hour = bcdToInt(data[2]);
	tm.tm_mday = bcdToInt(data[3]);
	tm.tm_mon  = bcdToInt(data[5]) - 1; // 0-11 - Note: The month on the PCF8523 is 1-12.
	tm.tm_year = bcdToInt(data[6]) + 100; // Years since 1900
	time_t readTime = mktime(&tm);
	return readTime;
}
Exemple #3
0
int i2c_write_read(uint8_t stop, uint8_t slave, uint8_t rsize, uint8_t *rbuf, uint8_t wsize, uint8_t *wbuf)
{
    if (rsize == 0 && wsize == 0) {
        return ESP_OK;
    }

    i2c_cmd_handle_t cmd;
    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    if (wsize) {
	i2c_master_write_byte(cmd, ( slave << 1 ) | I2C_MASTER_WRITE, ACK_CHECK);
	i2c_master_write(cmd, wbuf, wsize, ACK_CHECK);
	if (!rsize) {
    i2c_master_stop(cmd); \
    esp_err_t ret = i2c_master_cmd_begin(I2C_NUM, cmd, 1000 / portTICK_RATE_MS); \
    i2c_cmd_link_delete(cmd);
//	    I2C_FINISH;
	    return ret;
	}
	if (stop) { // rsize is nonzero
    i2c_master_stop(cmd); \
    esp_err_t ret = i2c_master_cmd_begin(I2C_NUM, cmd, 1000 / portTICK_RATE_MS); \
    i2c_cmd_link_delete(cmd);
//	    I2C_FINISH;
	    if (ret)
		return -1;
	    cmd = i2c_cmd_link_create();
	    i2c_master_start(cmd);
	} else {
	    i2c_master_start(cmd);
	}
	i2c_master_write_byte(cmd, ( slave << 1 ) | I2C_MASTER_READ, ACK_CHECK);
    } else {
	// rsize must be nonzero because of the initial check at the top
	i2c_master_write_byte(cmd, ( slave << 1 ) | I2C_MASTER_READ, ACK_CHECK);
    }

    if (rsize > 1) {
        i2c_master_read(cmd, rbuf, rsize - 1, ACK_VAL);
    }
    i2c_master_read_byte(cmd, rbuf + rsize - 1, NACK_VAL);

    I2C_FINISH;
    return ret;
}
Exemple #4
0
uint8_t matrix_scan(void)
{
    int ret = _matrix_scan();


    int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;

#ifdef USE_I2C
    // Get rows from other half over i2c
    // Matrix stored at 0x00-0x03
    int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
    if (err) goto i2c_error;
    err = i2c_master_write(0x00);
    if (err) goto i2c_error;
    err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
    if (err) goto i2c_error;

    if (!err) {
        // turn off the indicator led on no error
        //PORTD |= (1<<5);

        int i;
        for (i = 0; i < ROWS_PER_HAND-1; ++i) {
            matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
        }
        matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
        i2c_master_stop();
    } else {
i2c_error: // the cable is disconnceted, or something else went wrong

        // reset the i2c state
        TWCR = 0;

        // turn on the indicator led
        //PORTD &= ~(1<<5);

        // if we cannot communicate with the other half, then unset all of its keys
        for (int i = 0; i < ROWS_PER_HAND; ++i) {
            matrix[slaveOffset+i] = 0;
        }
    }
#else
    if( serial_transaction() ) {
        // turn on the indicator led
        //PORTD &= ~(1<<5);
        // if we cannot communicate with the other half, then unset all of its keys
        for (int i = 0; i < ROWS_PER_HAND; ++i) {
            matrix[slaveOffset+i] = 0;
        }
    } else {
        // turn off the indicator led on no error
        //PORTD |= (1<<5);
        // no error

        for (int i = 0; i < ROWS_PER_HAND; ++i) {
            matrix[slaveOffset+i] = serial_slave_buffer[i];
        }
    }
#endif

    return ret;
}
Exemple #5
0
void main() {
   
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISA = 0xFFCF; 
    TRISB = 0b0001111001110011;
   i2c_master_setup();
    ANSELBbits.ANSB2 = 0; //SDA2 set to digital
    ANSELBbits.ANSB3 = 0; //SCL2 set to digital
 
    __builtin_enable_interrupts();
   // SYSTEMConfigPerformance(48000000);
    
    SPI1_init(); 
    LCD_init();
    LCD_clearScreen(0);
//    RPB13Rbits.RPB13R = 0b0011; //SDO
//    SDI1Rbits.SDI1R = 0b0000; //A1
    RPB7Rbits.RPB7R = 0b0101; //OC1
    RPB8Rbits.RPB8R = 0b0101; //OC2
    PORTAbits.RA4 = 1; //led init
    T2CONbits.TCKPS = 2; //timer 2 prescale = 1:4
    PR2 = 1999; //period = (PR2+1) * N * 12.5 ns = 100 us, 10 kHz
    TMR2 = 0;
    OC1RS = 1000;
    OC1R = 1000;
    OC2RS = 1000;
    OC2R = 1000;
    OC1CONbits.OCTSEL = 0; //select timer2
    OC2CONbits.OCTSEL = 0;
    OC1CONbits.OCM = 0b110; //set pwm mode
    OC2CONbits.OCM = 0b110;
    T2CONbits.ON = 1;
    OC1CONbits.ON = 1;
    OC2CONbits.ON = 1;
    
    unsigned char x = 0; //sine counter
    unsigned char y = 0; //triangle counter
    char pressed = 0; //for tracking button logic
    char counter = 0;
    char m = 100; //(triangle wave frequency is 1000/2m)
    unsigned char voltage = 0;
    unsigned char channel = 0;
    int bytes = 14;
    unsigned char i2cdata[bytes];
    unsigned char i2cdatatest;
    unsigned char i2cwhoami;
    short temp = 0;
    short accel_x = 0;
    short accel_y = 0;
    short accel_z = 0;
    short gyro_x = 0;
    short gyro_y = 0;
    short gyro_z = 0;
    char i2cdatacount = 0;
    char textbuffer[20];
    char length = 0;
    
    i2cwhoami = i2c_master_read(GYRO,WHOAMI,0,0);
    i2c_master_write(GYRO,CTRL1_XL,0b10000000,0);
    i2c_master_write(GYRO,CTRL2_G,0b10000000,0);
    i2c_master_write(GYRO,CTRL3_C,0b00000100);
    //i2cdatatest = i2c_master_read(GYRO,CTRL1_XL,0,0);
//    i2c_master_write(GYRO,CTRL1_XL,0b10000000,1);
//    i2c_master_send(0b10000000);
//    i2c_master_send(0b00000100);
//    i2c_master_stop();
  
    
   
    CS = 1;
    int leet = 1337;
    sprintf(textbuffer,"Hello world %d!",leet);
    char text[2] = {'H','6'};
 
    //int text[5] = {40,30,50,30,20};
     LCD_clearScreen(0);
     
    while(1) {
        length = sizeof(textbuffer);//size must be taken here otherwise pointer size is taken instead of 
       LCD_type(28,32,textbuffer,length,0b1111100000000000);
       //LCD_char(28,32,30,0b1111100000000000);
        
    
       i2c_master_multiread(GYRO,OUT_TEMP_L,bytes,i2cdata);
//       //i2cdatatest = i2c_master_read(GYRO,0x28,0,0);
       temp = i2cdata[1];
       temp = (temp<<8)|i2cdata[0];
       temp = (unsigned short)temp;
        
       gyro_x = i2cdata[3];
       gyro_x = (gyro_x<<8)|i2cdata[2];
       gyro_x = (unsigned short)gyro_x;
       
       gyro_y = i2cdata[5];
       gyro_y = (gyro_y<<8)|i2cdata[4];
       gyro_y = (unsigned short)gyro_y;
       
       gyro_z = i2cdata[7];
       gyro_z = (gyro_z<<8)|i2cdata[6];
       gyro_z = (unsigned short)gyro_z;
       
       accel_x = i2cdata[9];
       accel_x = (i2cdata[9]<<8)|i2cdata[8];
       accel_x = (unsigned short)accel_x;
       
      accel_y = i2cdata[11];
       accel_y = (i2cdata[11]<<8)|i2cdata[10];
       accel_y = (unsigned short)accel_y;
       
       accel_z = i2cdata[13];
       accel_z = (accel_z<<8)|i2cdata[12];
       accel_z = (unsigned short)accel_z;
       
        
       OC1RS = floor((accel_x/16.768));
       OC2RS = floor((accel_y/16.768));
       if(OC1RS>2999){
           OC1RS = 2000;
            }
       else if(OC1RS<1000){
           OC1RS = 0;
       }
       else{
           OC1RS = OC1RS - 1000;
       }
       
       
       if(OC2RS>2999){
           OC2RS = 2000;
            }
       else if(OC2RS<1000){
           OC2RS = 0;
       }
       else{
           OC2RS = OC2RS - 1000;
       }
//       OC1R = floor((gyro_x/3.2768 + 10000));
//       OC2R = floor((gyro_y/3.2768 + 10000));
//-------------SPI debugging for IMU------------------
//       if (!PORTBbits.RB4){
//       CS = 0;
//            channel = counter;
//            //voltage = floor(100*sin((x*2*pi)/100)+100);
//            voltage = i2cwhoami;
//            //char voltage = 0b10101001;
//            spi1_set(channel,voltage);
//            //delay(6000);
//            CS = 1;
//       }
//       else {
//            CS = 0;
//            channel = counter;
//            //voltage = floor(100*sin((x*2*pi)/100)+100);
//            voltage = i2cdata[0];
//            //char voltage = 0b10101001;
//            spi1_set(channel,voltage);
//            //delay(6000);
//            CS = 1;
//            //++i2cdatacount %14;
//       }
//-------------------------------------------------------
       delay(960000);
                 }
            }