static void clear_display(void) { matrix_clear(&display); // Clear all of the display bits (there can be random noise // in the RAM on startup) send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1); send_cmd3(ColumnAddr, 0, DisplayWidth - 1); if (i2c_start_write(SSD1306_ADDRESS)) { goto done; } if (i2c_master_write(0x40)) { // Data mode goto done; } for (uint8_t row = 0; row < MatrixRows; ++row) { for (uint8_t col = 0; col < DisplayWidth; ++col) { i2c_master_write(0); } } display.dirty = false; done: i2c_master_stop(); }
void i2c_master_setup(void) { ANSELBbits.ANSB2 = 0; // I2C2 analog off; ANSELBbits.ANSB3 = 0; // I2C2 analog off; I2C2BRG = 233; // for 100kHz; // I2CBRG = [1/(2*Fsck) - PGD]*Pblck - 2 I2C2CONbits.ON = 1; // turn on the I2C2 module i2c_master_write(IMU_ADDR,CTRL1_XL,CTRL1_VAL); // Setup the XL register i2c_master_write(IMU_ADDR,CTRL2_G ,CTRL2_VAL); // Setup the G register i2c_master_write(IMU_ADDR,CTRL3_C ,CTRL3_VAL); // Setup the C register }
// 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; }
void matrix_render(struct CharacterMatrix *matrix) { last_flush = timer_read(); iota_gfx_on(); #if DEBUG_TO_SCREEN ++displaying; #endif // Move to the home position send_cmd3(PageAddr, 0, MatrixRows - 1); send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1); if (i2c_start_write(SSD1306_ADDRESS)) { goto done; } if (i2c_master_write(0x40)) { // Data mode goto done; } for (uint8_t row = 0; row < MatrixRows; ++row) { for (uint8_t col = 0; col < MatrixCols; ++col) { const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth); for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) { uint8_t colBits = pgm_read_byte(glyph + glyphCol); i2c_master_write(colBits); } // 1 column of space between chars (it's not included in the glyph) //i2c_master_write(0); } } matrix->dirty = false; done: i2c_master_stop(); #if DEBUG_TO_SCREEN --displaying; #endif }
// Write command sequence. // Returns true on success. static inline bool _send_cmd1(uint8_t cmd) { bool res = false; if (i2c_start_write(SSD1306_ADDRESS)) { xprintf("failed to start write to %d\n", SSD1306_ADDRESS); goto done; } if (i2c_master_write(0x0 /* command byte follows */)) { print("failed to write control byte\n"); goto done; } if (i2c_master_write(cmd)) { xprintf("failed to write command %d\n", cmd); goto done; } res = true; done: i2c_master_stop(); return res; }
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; }
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; }
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); } }