//u8 test[300]; //void spi_write_fifo(unsigned char * data, int len) { // int j; ///* //// test[0] = WRITE_TX_FIFO; //// memcpy(test+1, data, len); //// mutex_lock(&mutex_spi); //// printk(KERN_ALERT "spi_write_fifo: write %d\n", len); // cs_low(); // u8 cmd = WRITE_TX_FIFO; // spidev_global.buffer = &cmd; // spidev_sync_write(&spidev_global, 1); //// for (j = 0; j < len; j++) { //// cmd = data[j]; //// spidev_sync_write(&spidev_global, 1); //// } // spidev_global.buffer = data; // spidev_sync_write(&spidev_global, len); // cs_high(); // //// mutex_unlock(&mutex_spi); // */ // ssize_t ret; // u8 cmd = WRITE_TX_FIFO; // struct spi_transfer t_cmd = { // .tx_buf = &cmd, // .len = 1, // }; // struct spi_transfer t_data = { // .tx_buf = data, // .len = len, // }; // struct spi_message m; // // spi_message_init(&m); // spi_message_add_tail(&t_cmd, &m); // spi_message_add_tail(&t_data, &m); // ret = spidev_sync(spidev_global, &m); //} void spi_write_fifo(unsigned char * data, int len) { int j; ssize_t ret; u8 cmd = 0x66; struct spi_transfer t_cmd = { .tx_buf = &cmd, .len = 1, .cs_change = 0, }; struct spi_transfer t_data = { .tx_buf = data, .len = len, .cs_change = 0, }; struct spi_message m; spi_message_init(&m); spi_message_add_tail(&t_cmd, &m); spi_message_add_tail(&t_data, &m); cs_low(); ret = spidev_sync(&spidev_global, &m); cs_high(); } void spi_read_fifo(unsigned char * st, int len) { int j; // mutex_lock(&mutex_spi); cs_low(); u8 cmd = READ_RX_FIFO; // u8 ret; spidev_global.buffer = &cmd; spidev_sync_write(&spidev_global, 1); cmd = 0xff; // for (j = 0; j < len; j++) { // spidev_sync_transfer(&spidev_global, &cmd, &(st[j]), 1); // // } spidev_global.buffer = st; spidev_sync_read(&spidev_global,len); cs_high(); // mutex_unlock(&mutex_spi); //Serial.println(); // unsigned char p[] = {READ_RX_FIFO}; // SendCmdReceiveAnswer(1,payload_length,p); } void get_fifo_info(u8 *rx) // 复位发射和接收 FIFO { // u8 rx[10]; unsigned char p[2]; p[0] = FIFO_INFO; p[1] = 0x00; SendCmdReceiveAnswer(2, 3, p, rx); // spi_write(2,p); }
u8 * SendCmdReceiveAnswer(int byteCountTx, int byteCountRx, u8 * in_buff, u8 * out_buff) { /* TEST */ // if (byteCountTx == 1) // byteCountTx++; char answer, i, j, k; //发送命令 //printk(KERN_ALERT "Send CMD! \n"); mutex_lock(&mutex_spi); cs_low(); // for (i=0; i<byteCountTx; i++){ // spidev_global.buffer = &(in_buff[i]); // //printk(KERN_ALERT "%x ", *(spidev_global.buffer)); // spidev_sync_write(&spidev_global, 1); // } spidev_global.buffer = in_buff; spidev_sync_write(&spidev_global, byteCountTx); cs_high(); // ndelay(100); // ndelay(10); cs_low(); // if(!getCTS_gpio()) getCTS(); if(byteCountRx == 0) { cs_high(); mutex_unlock(&mutex_spi); return NULL; } // for (k=0; k<byteCountRx; k++){ // spidev_global.buffer = &(out_buff[k]); // spidev_sync_read(&spidev_global, 1); // //// printk(KERN_ALERT "%x ", *(spidev_global.buffer)); // } spidev_global.buffer = out_buff; spidev_sync_read(&spidev_global, byteCountRx); cs_high(); mutex_unlock(&mutex_spi); return out_buff; }
int sdmmc_cmd(uint8_t cmd_code, uint32_t arg) { int i; uint8_t result; cs_low(); while (spi(0xff) != 0xff); spi(0x40 | cmd_code); spi((arg >> 24) & 0xff); spi((arg >> 16) & 0xff); spi((arg >> 8) & 0xff); spi(arg & 0xff); spi(cmd_code == 8 ? 0x87 : 0x95); if (cmd_code == 12) { spi(0xff); // discard stuff byte } for (i = 0; i < 256; i++) { result = spi(0xff); if ((result & 0x80) == 0) { return result; } } return -1; }
/* * write a command, consisting of 8 cmd bits, and 24 address bits. */ static void flash_write_cmd( uint8_t cmd, uint32_t addr ) { cs_low( ); flash_write_byte( cmd ); flash_write_byte( addr >> 24 ); flash_write_byte( addr >> 16 ); flash_write_byte( addr >> 8 ); }
int16_t parse_cmd_ustream_test(char *cmd, char *output, uint16_t len) { cs_low(); sci_write(0x00, (1<<SM_TESTS)|(1<<SM_SDISHARE)|(1<<SM_STREAM)|(1<<SM_SDINEW)); cs_high(); vs1053_sinetest(120); return ECMD_FINAL_OK; }
/* * read the Flash status byte */ static uint8_t flash_read_status( void ) { uint8_t status; cs_low( ); flash_write_byte( AT_STATUS ); status = flash_read_byte( ); cs_high( ); return status; }
void sci_write(char addr, int data) { cs_low(); spi_send(0x02); // write command spi_send(addr); // address spi_send((data >> 8) & 0xFF); // send first 8 msb's of data spi_send(data & 0xFF); // send the lsb's cs_high(); }
void vs1053_sinetest(char pitch) { cs_high(); spi_send(0x53); spi_send(0xEF); spi_send(0x6E); spi_send(pitch); spi_send(0x00); spi_send(0x00); spi_send(0x00); spi_send(0x00); cs_low(); }
void MidiSynth::MidiWriteRegister(uint8_t addressbyte, uint8_t highbyte, uint8_t lowbyte) { // skip if the chip is in reset. if(!digitalRead(MIDI_RESET)) return; // Wait for DREQ to go high indicating IC is available while(!digitalRead(MIDI_DREQ)) ; // Select control cs_low(); // SCI consists of instruction byte, address byte, and 16-bit data word. SPI.transfer(0x02); // Write instruction SPI.transfer(addressbyte); SPI.transfer(highbyte); SPI.transfer(lowbyte); while(!digitalRead(MIDI_DREQ)) ; // Wait for DREQ to go high indicating command is complete cs_high(); // Deselect Control }
int sci_read(char addr) { unsigned int temp; cs_low(); spi_send(0x03); // read command spi_send(addr); // address temp = spi_send(0x00); // dummy to get out data the msb's temp <<= 8; // shift temp += spi_send(0x00); // get the lsb cs_high(); return temp; }
u8 read_frr_a(void) { u8 cmd[2]; u8 rx[3]; int j=50; cmd[0] = FRR_A_READ; cmd[1] = 0x00; // mutex_lock(&mutex_spi); cs_low(); spidev_global.buffer = &cmd; // spidev_sync_write(&spidev_global, 1); // cmd = 0x00; spidev_sync_transfer(&spidev_global, &cmd, rx, 2); cs_high(); // mutex_unlock(&mutex_spi); // printk(KERN_ALERT "FRR READ: %x, %x\n", rx[0], rx[1]); // for(j=50; j>=0 && rx[1] != 0x22 && rx[1] != 0x20 // && rx[1] != 0x10 && rx[1] != 0x11 // /*(*value == 0 || *value == 0xff)*/; j--) if(rx[1] != 0 && rx[1] != 0xff) { return rx[1]; } do { get_ph_status(rx); // printk(KERN_ALERT "read frra retry!\n"); // cs_low(); // spidev_sync_transfer(&spidev_global, &cmd, rx, 2); // cs_high(); j--; } while( j>=0 && rx[2] == 0 || rx[2] == 0xff); if (j<=5){ printk(KERN_ALERT "get_ph_status ERROR!!!\n"); ppp(rx, 3); } return rx[2]; }
static inline void getCTS(void) { u8 reply = 0x00; u8 request = 0x44; int j = 250; while (reply != 0xFF) { request = 0x44; spidev_sync_transfer(&spidev_global, &request, &reply, 1); if (reply != 0xFF){ // printk(KERN_ALERT "getCTS: %x\n", reply); cs_high(); // spidev_sync_transfer(&spidev_global, in_buff, out_buff, byteCountTx); ndelay(10); cs_low(); } if (j-- < 0) { printk(KERN_ALERT "**********************getCTS ERROR!***********************\n", reply); break; } } }
uint16_t MidiSynth::MidiReadRegister (uint8_t addressbyte){ union twobyte resultvalue; // skip if the chip is in reset. if(!digitalRead(MIDI_RESET)) return 0; while(!digitalRead(MIDI_DREQ)) ; // Wait for DREQ to go high indicating IC is available cs_low(); // Select control // SCI consists of instruction byte, address byte, and 16-bit data word. SPI.transfer(0x03); // Read instruction SPI.transfer(addressbyte); resultvalue.byte[1] = SPI.transfer(0xFF); // Read the first byte while(!digitalRead(MIDI_DREQ)) ; // Wait for DREQ to go high indicating command is complete resultvalue.byte[0] = SPI.transfer(0xFF); // Read the second byte while(!digitalRead(MIDI_DREQ)) ; // Wait for DREQ to go high indicating command is complete cs_high(); // Deselect Control return resultvalue.word; }
void read_frr_b(u8 *value) { u8 cmd; u8 rx[3]; int j; cmd = FRR_B_READ; // mutex_lock(&mutex_spi); cs_low(); spidev_global.buffer = &cmd; spidev_sync_write(&spidev_global, 1); cmd = 0x00; spidev_sync_transfer(&spidev_global, &cmd, value, 1); cs_high(); // mutex_unlock(&mutex_spi); // while(*value == 0 || *value == 0xff) // { // mutex_lock(&mutex_spi); // get_ph_status(rx); // mutex_unlock(&mutex_spi); // *value = rx[1]; // } }
int16_t magneto_hal_write( uint16_t address, int16_t value ) { uint16_t command; switch( current_mode ) { case MAGNETO_I2C: { uint8_t buffer[ AS5048A_MAX_READ_SIZE ]; buffer[1] = ( uint8_t )value & 0xff; switch( address ) { case AS5048A_CLEAR_ERROR_FLAG: break; case AS5048A_PROGRAMMING_CONTROL: buffer[0] = 0x03; break; case AS5048A_OTP_REGISTER_ZERO_POS_HIGH: buffer[0] = 0x16; break; case AS5048A_OTP_REGISTER_ZERO_POS_LOW: buffer[0] = 0x17; break; } #if defined( __MIKROC_PRO_FOR_ARM__ ) #if defined( STM32F107VC ) || defined( STM32F407VG ) || \ defined( STM32F030C6 ) || defined( STM32F746VG ) i2c_start_p(); i2c_write_p( i2c_address, buffer, 2, END_MODE_STOP ); #elif defined( LM3S1165 ) || defined( TM4C129ENCZAD ) i2c_set_slave_address_p( i2c_address, _I2C_DIR_MASTER_TRANSMIT ); i2c_write_p( buffer[0], _I2C_MASTER_MODE_BURST_SEND_START ); i2c_write_p( buffer[1], _I2C_MASTER_MODE_BURST_SEND_FINISH ); #endif #elif defined(__MIKROC_PRO_FOR_FT90x__) i2c_set_slave_address_p( i2c_address ); //TODO: Send bytes i2c_write_p( buffer[0] ); i2c_write_p( buffer[1] ); #elif defined(__MIKROC_PRO_FOR_AVR__) || \ defined(__MIKROC_PRO_FOR_8051__) || \ defined(__MIKROC_PRO_FOR_DSPIC__) || \ defined(__MIKROC_PRO_FOR_PIC32__) || \ defined(__MIKROC_PRO_FOR_PIC__) i2c_start_p(); i2c_write_p( i2c_address | WRITE ); i2c_write_p( buffer[0] ); i2c_write_p( buffer[1] ); i2c_stop_p(); #elif defined( __GNUC__) printf( "Start\n" ); printf( "Address: 0x%02x\n", address ); printf( "\tData: 0x%02x\n", value ); #endif } break; case MAGNETO_SPI: #if defined ( __GNUC__ ) cs_low(); printf( "%s\n", byte_to_binary( command ) ); // Write command cs_high(); cs_low(); printf( "%s\n", byte_to_binary( value ) ); cs_high(); #else command = 0x00 | ( address & 0x3FFF ); command |= ( ( int16_t )parity_check( command ) << AS5048A_PARITY_BIT ); if( device_count == 1 ) { cs_low(); spi_write_p( ( command >> 8 ) & 0xff ); spi_write_p( command & 0xff ); cs_high(); value = 0x00 | ( value & 0x3FFF ); value |= ( ( int16_t )parity_check( value ) << AS5048A_PARITY_BIT ); cs_low(); spi_write_p( ( value >> 8 ) & 0xff ); spi_write_p( value & 0xff ); cs_high(); cs_low(); command = ( spi_read_p( 0x00 ) << 8 ); command |= spi_read_p( 0x00 ); cs_high(); } else {