int sdmmc_write_multiple_blocks(uint32_t address, uint32_t n_blocks, uint8_t (*next_byte)()) { int result; int i; if ((result = sdmmc_cmd(25, block_addressing ? address : address << 9)) < 0) { cs_high(); return result; } spi(0xff); // initiate write while (n_blocks--) { spi(0xfc); // data token for (i = 0; i < 512; i++) { // sector data spi(next_byte()); } spi(0xff); // CRC spi(0xff); result = spi(0xff); // data response if ((result & 0x1f) != 0x05) { cs_high(); return result; } i = 0; do { // wait while card is busy if (++i == 131072) { cs_high(); return -10; } } while (spi(0xff) != 0xff); } return sdmmc_stop_transmission(); }
//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_read_block(uint32_t address, uint8_t *buffer) { int i; int result; // CMD17 - READ_SINGLE_BLOCK result = sdmmc_cmd(17, block_addressing ? address : address << 9); if (result < 0) { cs_high(); return -1; } // wait for data token for (i = 0; i < TOKEN_COUNT; i++) { if (spi(0xff) == 0xfe) { break; } } if (i == TOKEN_COUNT) { return -2; } // read data for (i = 0; i < 512; i++) { *buffer++ = spi(0xff); } // skip CRC spi(0xff); spi(0xff); sdmmc_cmd_term(); return 0; }
void MidiSynth::end() { cs_high(); // MIDI_XCS, Init Control Select to deselected dcs_high(); // MIDI_XDCS, Init Data Select to deselected // most importantly... digitalWrite(MIDI_RESET, LOW); // Put VS1053 into hardware reset }
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; }
int sdmmc_stop_transmission() { int result; if ((result = sdmmc_cmd(12, 0)) < 0) { while (spi(0xff) != 0xff); cs_high(); return result; } return 0; }
int sdmmc_read_multiple_blocks_start(uint32_t address) { int result; if ((result = sdmmc_cmd(18, block_addressing ? address : address << 9)) < 0) { cs_high(); return result; } return 0; }
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(); }
/* * simple test: dump all contents of built-in cartridge */ void flash_test( void ) { uint8_t i = 0; long size = 2048L * 264L; flash_write_cmd( AT_CAR, 0 ); flash_clocks( 32 ); while( --size >= 0 && usb_peek() != 3 ) { printf( "%02x%c", flash_read_byte(), ++i % 16 ? ' ' : '\n' ); } 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(); }
// Initialization and powerdown -- vs_init() does the heavy lifting for begin() uint8_t MidiSynth::begin() { pinMode(MIDI_DREQ, INPUT); pinMode(MIDI_XCS, OUTPUT); pinMode(MIDI_XDCS, OUTPUT); pinMode(MIDI_RESET, OUTPUT); cs_high(); // MIDI_XCS, Init Control Select to deselected dcs_high(); // MIDI_XDCS, Init Data Select to deselected digitalWrite(MIDI_RESET, LOW); // Put VS1053 into hardware reset uint8_t result = vs_init(); if(result) { return result; } return 0; }
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; }
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 }
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]; // } }
rd_t open_px4_mpu6000(void) { /* Set up CS pin and set high */ *RCC_AHB1ENR |= RCC_AHB1ENR_GPIOBEN; /* PB0 */ gpio_moder(GPIOB, 0, GPIO_MODER_OUT); gpio_otyper(GPIOB, 0, GPIO_OTYPER_PP); gpio_pupdr(GPIOB, 0, GPIO_PUPDR_NONE); gpio_ospeedr(GPIOB, 0, GPIO_OSPEEDR_50M); /* idle CS */ cs_high(); resource *new_r = create_new_resource(); if (!new_r) { printk("OOPS: Could not allocate space for mpu6000 resource.\r\n"); return -1; } struct mpu6000 *env = (struct mpu6000 *) kmalloc(sizeof(struct mpu6000)); if (!env) { printk("OOPS: Could not allocate space for mpu6000 resource.\r\n"); kfree(new_r); return -1; } env->spi_port = &spi1; if (!env->spi_port->ready) { env->spi_port->init(); } env->spi_dev.cs_high = &cs_high; env->spi_dev.cs_low = &cs_low; env->read_ctr = 0; acquire(&spi1_semaphore); /* Active mode, clock with gyro X reference */ uint8_t data = MPU6000_PWR_MGMT_1_CLK_PLLGYROX; if (spi_write(env->spi_port, &env->spi_dev, MPU6000_PWR_MGMT_1, &data, 1) != 1) { /* Unable to activate :( */ release(&spi1_semaphore); kfree(env); kfree(new_r); return -1; } release(&spi1_semaphore); /* Let clock settle */ usleep(1000); acquire(&spi1_semaphore); /* 100Hz LPF, Gyro range +- 500deg/s, Accel range +-4g */ uint8_t config[3] = {MPU6000_CONFIG_LPF_100HZ, MPU6000_GYRO_CONFIG_500DPS, MPU6000_ACCEL_CONFIG_4G}; if (spi_write(env->spi_port, &env->spi_dev, MPU6000_CONFIG, config, 3) != 3) { data = MPU6000_PWR_MGMT_1_SLEEP; /* Sleep mode */ spi_write(env->spi_port, &env->spi_dev, MPU6000_PWR_MGMT_1, &data, 1); release(&spi1_semaphore); kfree(env); kfree(new_r); return -1; } release(&spi1_semaphore); new_r->env = (void *) env; new_r->reader = &px4_mpu6000_read; new_r->writer = &px4_mpu6000_write; new_r->closer = &px4_mpu6000_close; new_r->sem = &spi1_semaphore; return add_resource(curr_task->task, new_r); }
void sdmmc_cmd_term() { cs_high(); spi(0xff); }
int sdmmc_init() { int i; int result; // send > 74 clock pulses with CS = DI = 1 cs_high(); for (i = 0; i < 10; i++) { spi(0xff); } // CMD0 - GO_IDLE_STATE result = sdmmc_cmd(0, 0); sdmmc_cmd_term(); if (result != 0x01) { return -1; } // CMD8 - SEND_IF_COND result = sdmmc_cmd(8, 0x1aa); if (result != 0x01) { // TODO: initialize SD v1 or MMC v3 sdmmc_cmd_term(); return -2; } if ((result = sdmmc_read_word()) != 0x1aa) { sdmmc_cmd_term(); return -3; } sdmmc_cmd_term(); for (i = 0; i < CMD41_COUNT; i++) { // CMD55 - APP_CMD result = sdmmc_cmd(55, 0); sdmmc_cmd_term(); if (result < 0) { return -5; } // CMD41 - APP_SEND_OP_COND result = sdmmc_cmd(41, 0x40000000); sdmmc_cmd_term(); if (result == 0x00) { break; } else if (result != 0x01) { return -6; } } if (i == CMD41_COUNT) { return -7; } // CMD58 - READ_OCR result = sdmmc_cmd(58, 0); if (result < 0) { sdmmc_cmd_term(); return -8; } if ((result = sdmmc_read_word()) & 0x40000000) { block_addressing = 1; } else { block_addressing = 0; sdmmc_cmd_term(); // CMD16 - SET_BLOCK_LEN (512 bytes) result = sdmmc_cmd(16, 512); if (result < 0) { sdmmc_cmd_term(); return -9; } } sdmmc_cmd_term(); return 0; }
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 {
int magneto_hal_init( magneto_mode_t mode, uint8_t address_id, uint8_t num_devices ) { if( mode > MAGNETO_PWM ) return -1; current_mode = mode; device_count = num_devices; switch( current_mode ) { case MAGNETO_I2C: #if defined( __MIKROC_PRO_FOR_ARM__ ) || defined(__MIKROC_PRO_FOR_FT90x__) i2c_address = address_id; #else i2c_address = ( address_id << 1 ); #endif #if defined( __MIKROC_PRO_FOR_ARM__ ) #if defined( STM32F107VC ) || defined( STM32F407VG ) || \ defined( STM32F030C6 ) || defined( STM32F746VG ) i2c_start_p = I2C_Start_Ptr; i2c_write_p = I2C_Write_Ptr; i2c_read_p = I2C_Read_Ptr; #elif defined( LM3S1165 ) || defined( TM4C129ENCZAD ) i2c_set_slave_address_p = I2C_Master_Slave_Addr_Set_Ptr; i2c_write_p = I2C_Write_Ptr; i2c_read_p = I2C_Read_Ptr; #endif #elif defined( __MIKROC_PRO_FOR_AVR__ ) #if defined( ATMEGA32 ) i2c_busy_p = TWI_Busy; i2c_status_p = TWI_Status; i2c_close_p = TWI_Close; i2c_start_p = TWI_Start; i2c_stop_p = TWI_Stop; i2c_write_p = TWI_Write; i2c_read_p = TWI_Read; #elif defined ( ATXMEGA128A1 ) i2c_busy_p = TWIC_Busy; i2c_status_p = TWIC_Status; i2c_close_p = TWIC_Close; i2c_start_p = TWIC_Start; i2c_stop_p = TWIC_Stop; i2c_write_p = TWIC_Write; i2c_read_p = TWIC_Read; #endif #elif defined( __MIKROC_PRO_FOR_PIC__ ) i2c_is_idle_p = I2C1_Is_Idle; i2c_start_p = I2C1_Start; i2c_stop_p = I2C1_Stop; i2c_restart_p = I2C1_Repeated_Start; i2c_write_p = I2C1_Wr; i2c_read_p = I2C1_Rd; #elif defined( __MIKROC_PRO_FOR_PIC32__ ) i2c_is_idle_p = I2C_Is_Idle_Ptr; i2c_start_p = I2C_Start_Ptr; i2c_stop_p = I2C_Stop_Ptr; i2c_restart_p = I2C_Restart_Ptr; i2c_write_p = I2C_Write_Ptr; i2c_read_p = I2C_Read_Ptr; #elif defined( __MIKROC_PRO_FOR_DSPIC__ ) i2c_is_idle_p = I2C1_Is_Idle; i2c_start_p = I2C1_Start; i2c_stop_p = I2C1_Stop; i2c_restart_p = I2C1_Restart; i2c_write_p = I2C1_Write; i2c_read_p = I2C1_Read; #elif defined( __MIKROC_PRO_FOR_8051__ ) i2c_busy_p = TWI_Busy; i2c_status_p = TWI_Status; i2c_close_p = TWI_Close; i2c_start_p = TWI_Start; i2c_stop_p = TWI_Stop; i2c_write_p = TWI_Write; i2c_read_p = TWI_Read; #elif defined( __MIKROC_PRO_FOR_FT90x__ ) i2c_soft_reset_p = I2CM_Soft_Reset_Ptr; i2c_set_slave_address_p = I2CM_Set_Slave_Address_Ptr; i2c_write_p = I2CM_Write_Ptr; i2c_read_p = I2CM_Read_Ptr; i2c_write_bytes_p = I2CM_Write_Bytes_Ptr; i2c_read_bytes_p = I2CM_Read_Bytes_Ptr; i2c_write_10bit_p = I2CM_Write_10Bit_Ptr; i2c_read_10bit_p = I2CM_Read_10Bit_Ptr; #endif break; case MAGNETO_SPI: #if defined( __GNUC__ ) #else #if defined( __MIKROC_PRO_FOR_ARM__ ) || \ defined( __MIKROC_PRO_FOR_AVR__ ) || \ defined( __MIKROC_PRO_FOR_PIC__ ) || \ defined( __MIKROC_PRO_FOR_PIC32__ ) || \ defined( __MIKROC_PRO_FOR_DSPIC__ ) || \ defined( __MIKROC_PRO_FOR_8051__ ) spi_read_p = SPI_Rd_Ptr; spi_write_p = SPI_Wr_Ptr; cs_high(); #elif defined( __MIKROC_PRO_FOR_FT90x__ ) spi_read_p = SPIM_Rd_Ptr; spi_write_p = SPIM_Wr_Ptr; cs_high(); #endif #endif break; case MAGNETO_PWM: break; }; #ifdef __GNUC__ #else #if defined( __MIKROC_PRO_FOR_ARM__ ) || \ defined( __MIKROC_PRO_FOR_AVR__ ) || \ defined( __MIKROC_PRO_FOR_PIC__ ) || \ defined( __MIKROC_PRO_FOR_PIC32__ ) || \ defined( __MIKROC_PRO_FOR_DSPIC__ ) || \ defined( __MIKROC_PRO_FOR_8051__ ) || \ defined( __MIKROC_PRO_FOR_FT90x__ ) Delay_10ms(); #endif #endif return 0; }