void rda1846RX(unsigned char useSq) { SPI(0x1F, 0); if (useSq) SPI(0x30, 0x302E); //2E RX else SPI(0x30, 0x3026); //2E RX }
int wait_initV2() { int i=20000; int r; spi_spin(); while(--i) { if((r=cmd_CMD55())==1) { // printf("CMD55 %d\n",r); SPI(0xff); if((r=cmd_CMD41())==0) { // printf("CMD41 %d\n",r); SPI(0xff); return(1); } // else // printf("CMD41 %d\n",r); spi_spin(); } // else // printf("CMD55 %d\n",r); } return(0); }
// The init procedure is executed once when the REXLANG function block initializes. long init(void) { spi_bus_handle = OpenSPI(spi_dev); // open SPI device // SPI data modes: // SPI_MODE0 = 0, // CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge // SPI_MODE1 = 1, // CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge // SPI_MODE2 = 2, // CPOL = 1, CPHA = 0, Clock idle high, data is clocked in on falling edge, output data (change) on rising edge // SPI_MODE3 = 3, // CPOL = 1, CPHA = 1, Clock idle high, data is clocked in on rising, edge output data (change) on falling edge spi_bufTx[0] = 0; //SPI data mode (0-3 mode, | 4=CS_HIGH | 8=LSB_FIRST | 16=3WIRE | 32=LOOP | 64=NO_CS | 128=READY) spi_bufTx[1] = 8; //bits per word spi_bufTx[2] = 1000000; //clock frequency [Hz] SetOptions(spi_bus_handle, spi_bufTx); // Verification of SPI bus settings GetOptions(spi_bus_handle, spi_bufRx); Trace(0, spi_bufRx[0]); // SPI mode Trace(1, spi_bufRx[1]); // bits per word Trace(2, spi_bufRx[2]); // clock frequency // Addressing the device spi_opcode_write = 0x40; // ( 0 | 1 | 0 | 0 | A2 | A1 | A0 | R/W ), write=0 spi_opcode_read = 0x41; // ( 0 | 1 | 0 | 0 | A2 | A1 | A0 | R/W ), read=1 // !!!!!!!!!!!!! // By default, IOCON.BANK=0, therefore see Table 1-6 in the datasheet for register mapping // // Table 1-5, which confusingly appears sooner in the datasheet, displays register addresses // for IOCON.BANK=1, which are significantly different // !!!!!!!!!!!!! //Setting PORTA to output spi_bufTx[0] = spi_opcode_write; spi_bufTx[1] = 0x00; //register no. (IODIRA) spi_bufTx[2] = 0x00; //IODIRA data, bitmask, 0=output, 1=input spi_write_count = 3; spi_read_count = 0; spi_ret_fun = SPI(spi_bus_handle, 0, spi_bufTx, spi_write_count, spi_bufRx, spi_read_count); //Setting PORTB to input spi_bufTx[0] = spi_opcode_write; spi_bufTx[1] = 0x01; //register no. (IODIRB) spi_bufTx[2] = 0xFF; //IODIRB data, bitmask, 0=output, 1=input spi_write_count = 3; spi_read_count = 0; spi_ret_fun = SPI(spi_bus_handle, 0, spi_bufTx, spi_write_count, spi_bufRx, spi_read_count); //Enabling pull-up resistors on PORTB spi_bufTx[0] = spi_opcode_write; spi_bufTx[1] = 0x0D; //register no. (GPPUB) spi_bufTx[2] = 0xFF; //GPPUB data, bitmask, 0=no pull-up, 1=pull-up enabled spi_write_count = 3; spi_read_count = 0; spi_ret_fun = SPI(spi_bus_handle, 0, spi_bufTx, spi_write_count, spi_bufRx, spi_read_count); return 0; }
void tos_set_video_adjust(char axis, char value) { config.video_adjust[axis] += value; EnableFpga(); SPI(MIST_SET_VADJ); SPI(config.video_adjust[0]); SPI(config.video_adjust[1]); DisableFpga(); }
void rda1846TXDTMF(unsigned char* values, unsigned int len, unsigned short delay) { int i=0; //Set tx mode SPI(0x1F, 0xC000); SPI(0x63, 0x01F0 ); //00000001 00010001 SPI(0x30, 0x3046); //TX for(i=0; i<len; i++) { if (values[i] < 0x10) { //Get data from dtmf table SPI(0x35, dtmfTone[values[i]][0]); SPI(0x36, dtmfTone[values[i]][1]); msDelay(delay); SPI(0x35, 0); SPI(0x36, 0); } } //Switch back to RX mode SPI(0x30, 0x302E); //RX SPI(0x1F, 0x0000); }
static void mist_memory_read(char *data, unsigned long words) { EnableFpga(); SPI(MIST_READ_MEMORY); // transmitted bytes must be multiple of 2 (-> words) while(words--) { *data++ = SPI(0); *data++ = SPI(0); } DisableFpga(); }
int SPI_PUMP() { int r=0; SPI(0xFF); r=SPI_READ(); SPI(0xFF); r=(r<<8)|SPI_READ(); SPI(0xFF); r=(r<<8)|SPI_READ(); SPI(0xFF); r=(r<<8)|SPI_READ(); return(r); }
int sd_read_sector(unsigned long lba,unsigned char *buf) { int result=0; int i; int r; // printf("sd_read_sector %d, %d\n",lba,buf); SPI(0xff); SPI_CS(1|(1<<HW_SPI_FAST)); SPI(0xff); r=cmd_read(lba); if(r!=0) { puts("Read failed\n"); // printf("Read command failed at %d (%d)\n",lba,r); return(result); } i=1500000; while(--i) { int v; SPI(0xff); // SPI_WAIT(); v=SPI_READ(); if(v==0xfe) { // puts("Reading sector data\n"); // spi_readsector((long *)buf); int j; // SPI(0xff); for(j=0;j<128;++j) { int t,v; t=SPI_PUMP(); *(int *)buf=t; // printf("%d: %d\n",buf,t); buf+=4; } i=1; // break out of the loop result=1; } } SPI(0xff); SPI(0xff); // Discard Two CRC bytes SPI_CS(0); return(result); }
static void mist_memory_set_address(unsigned long a, unsigned char s, char rw) { // iprintf("set addr = %x, %d, %d\n", a, s, rw); a |= rw?0x1000000:0; a >>= 1; EnableFpga(); SPI(MIST_SET_ADDRESS); SPI(s); SPI((a >> 16) & 0xff); SPI((a >> 8) & 0xff); SPI((a >> 0) & 0xff); DisableFpga(); }
//Get the signal stength indications void rda1846GetStatus(short* rssi, short* vssi) //, short* dtmf, short* flags) { *rssi = SPI(0x5F | 0x80, 0x0000) & 0x03FF; *rssi >>= 3; //devide by 8 *rssi -= 135; //Per datasheet *rssi *= -1; *vssi = SPI(0x60 | 0x80, 0x0000) & 0x7FFF; //*dtmf = SPI(0x6C | 0x80, 0x0000); //*flags = SPI(0x5C | 0x80, 0x0000); }
int spi_init() { int j; int i; int r; SDHCtype=1; j=5; while(--j) { SPI_CS(0); // Disable CS spi_spin(); puts("SD init...\n"); // puts("SPI Init()\n"); DBG("Activating CS\n"); SPI_CS(1); i=50; while(--i) { if(cmd_reset()==1) // Enable SPI mode { i=1; j=1; } DBG("Sent reset command\n"); if(i==2) { puts("SD card reset failed!\n"); return(0); } } } DBG("Card responded to reset\n"); SDHCtype=is_sdhc(); if(SDHCtype) DBG("SDHC card detected\n"); else // Not SDHC? Set blocksize to 512. { DBG("Sending cmd16\n"); cmd_CMD16(1); } SPI(0xFF); SPI_CS(0); SPI(0xFF); DBG("Init done\n"); return(1); }
static void mist_memory_write_block(char *data) { EnableFpga(); SPI(MIST_WRITE_MEMORY); spi_block_write(data); DisableFpga(); }
static void mist_memory_read_block(char *data) { EnableFpga(); SPI(MIST_READ_MEMORY); spi_block_read(data); DisableFpga(); }
// clear buffer <c> void OsdClear(void) { unsigned short n; // select OSD SPI device EnableOsd(); // select buffer to write to SPI(OSDCMDWRITE|0x18); // clear buffer for(n=0; n < (OSD_LINE_BYTES * OSD_NO_LINES); n++) { SPI(0x00); } // deselect OSD SPI device DisableOsd(); }
void spi_spin() { // puts("SPIspin\n"); int i; for(i=0; i<200; ++i) SPI(0xff); // puts("Done\n"); }
int sd_write_sector(unsigned long lba,unsigned char *buf) // FIXME - Stub { int i,t,timeout; SPI(0xff); SPI_CS(1|(1<<HW_SPI_FAST)); SPI(0xff); t=cmd_writesector(lba); if(t!=0) { puts("Write failed\n"); // printf("Read command failed at %d (%d)\n",lba,r); return(1); } SPI(0xFF); // one byte gap SPI(0xFE); // send Data Token // send sector bytes for (i = 0; i < 128; i++) { int t=*(int *)buf; SPI((t>>24)&255); SPI((t>>16)&255); SPI((t>>8)&255); SPI(t&255); buf+=4; } SPI(0xFF); // send CRC lo byte SPI(0xFF); // send CRC hi byte SPI(0xFF); // Pump the response byte timeout = 100000; do { SPI(0xFF); i=SPI_READ(); } while((i==0) && --timeout); SPI(0xff); SPI_CS(0); return(0); }
int cmd_write(unsigned long cmd, unsigned long lba) { int ctr; int result=0xff; DBG("In cmd_write\n"); SPI(cmd & 255); DBG("Command sent\n"); if(!SDHCtype) // If normal SD then we have to use byte offset rather than LBA offset. lba<<=9; DBG("Sending LBA!\n"); SPI((lba>>24)&255); DBG("Sent 1st byte\n"); SPI((lba>>16)&255); DBG("Sent 2nd byte\n"); SPI((lba>>8)&255); DBG("Sent 3rd byte\n"); SPI(lba&255); DBG("Sent 4th byte\n"); DBG("Sending CRC - if any\n"); SPI((cmd>>16)&255); // CRC, if any ctr=40000; result=SPI_READ(); while(--ctr && (result==0xff)) { SPI(0xff); result=SPI_READ(); } #ifdef SPI_DEBUG putchar('0'+(result>>4)); putchar('0'+(result&15)); #endif // printf("Got result %d \n",result); return(result); }
void rda1846TXDigital(unsigned char data, unsigned short t, unsigned short mark, unsigned short space) { SPI(0x3C, 0x4958 ); //0100 0001 00010001 SPI(0x1F, 0xC000); SPI(0x30, 0x3046); //TX //Start with no sound and wait a second //TODO: This is just used for testing, but needs to be Rewritten SPI(0x36, 0); msDelay(1000); unsigned char b=0; for(b=0; b<7; b++) { if (data & 0x01) SPI(0x36, mark); //1.415Khz else SPI(0x36, space); //1.585 msDelay(t); data >>= 1; } SPI(0x36, 0); rda1846RX(1); }
static void mist_set_control(unsigned long ctrl) { EnableFpga(); SPI(MIST_SET_CONTROL); SPI((ctrl >> 24) & 0xff); SPI((ctrl >> 16) & 0xff); SPI((ctrl >> 8) & 0xff); SPI((ctrl >> 0) & 0xff); DisableFpga(); }
// The main procedure is executed once in each sampling period long main(void) { //Controlling outputs spi_bufTx[0] = spi_opcode_write; spi_bufTx[1] = 0x12; //register no. (GPIOA) spi_bufTx[2] = digital_out & 0xFF; //masking the data to control the outputs spi_write_count = 3; spi_read_count = 0; spi_ret_fun = SPI(spi_bus_handle, 0, spi_bufTx, spi_write_count, spi_bufRx, spi_read_count); //Reading inputs spi_bufTx[0] = spi_opcode_read; spi_bufTx[1] = 0x13; //register no. (GPIOB) spi_write_count = 2; spi_read_count = 3; // we need to read 1 byte of response after the 2-byte command is sent, therefore 3 bytes in total, the first 2 bytes will be zero spi_ret_fun = SPI(spi_bus_handle, 0, spi_bufTx, spi_write_count, spi_bufRx, spi_read_count); digital_in = spi_bufRx[2]; //publishing the received data return 0; }
void rda1846Init() { int i; RDA1846_SEN = 1; RDA1846_SCK = 1; for(i=0; i<30; i++) { SPI(RDAinit[i].address, RDAinit[i].data); } }
int wait_init() { int i=20; int r; SPI(0xff); // puts("Cmd_init\n"); while(--i) { if((r=cmd_init())==0) { // printf("init %d\n ",r); SPI(0xff); return(1); } // else // printf("init %d\n ",r); spi_spin(); } return(0); }
void mist_memory_set(char data, unsigned long words) { EnableFpga(); SPI(MIST_WRITE_MEMORY); while(words--) { SPI_WRITE(data); SPI_WRITE(data); } DisableFpga(); }
static void mist_memory_write(char *data, unsigned long words) { EnableFpga(); SPI(MIST_WRITE_MEMORY); while(words--) { SPI_WRITE(*data++); SPI_WRITE(*data++); } DisableFpga(); }
void rda1846SetGPIO(unsigned char gpio) { rda1846GPIO = 0; //Should we reset each time if (gpio & TX_LED) rda1846GPIO = 0xC0; // if (gpio & TX_ //{ 0x1F, 0x1EB9 }, //GPIO selection 0001 1110 1011 1001 SPI(0x1F, rda1846GPIO); }
void setReg(unsigned char addr, short data) { uartSendMsg("Set2: "); uartSendNum(addr, 16); uartSendMsg(" to "); uartSendNum(data, 16); uartSendMsg("\r\n"); //SPI(0x3C, 0x0958 ); //00000001 00010001 //SPI(addr, data); SPI(addr, data ); }
void user_io_file_tx(fileTYPE *file, unsigned char index) { unsigned long bytes2send = file->size; /* transmit the entire file using one transfer */ iprintf("Selected file %.11s with %lu bytes to send for index %d\n", file->name, bytes2send, index); // set index byte (0=bios rom, 1-n=OSD entry index) user_io_set_index(index); // send directory entry (for alpha amstrad core) EnableFpga(); SPI(UIO_FILE_INFO); spi_write((void*)(DirEntry+sort_table[iSelectedEntry]), sizeof(DIRENTRY)); DisableFpga(); // hexdump(DirEntry+sort_table[iSelectedEntry], sizeof(DIRENTRY), 0); // prepare transmission of new file EnableFpga(); SPI(UIO_FILE_TX); SPI(0xff); DisableFpga(); while(bytes2send) { iprintf("."); unsigned short c, chunk = (bytes2send>512)?512:bytes2send; char *p; FileRead(file, sector_buffer); EnableFpga(); SPI(UIO_FILE_TX_DAT); for(p = sector_buffer, c=0;c < chunk;c++) SPI(*p++); DisableFpga(); bytes2send -= chunk; // still bytes to send? read next sector if(bytes2send) FileNextSector(file); } // signal end of transmission EnableFpga(); SPI(UIO_FILE_TX); SPI(0x00); DisableFpga(); iprintf("\n"); }
int spi_init() { int i; int r; SDHCtype=1; SPI_CS(0); // Disable CS spi_spin(); puts("SPI Init()\n"); DBG("Activating CS\n"); SPI_CS(1); i=8; while(--i) { if(cmd_reset()==1) // Enable SPI mode i=1; DBG("Sent reset command\n"); if(i==2) { DBG("SD card initialization error!\n"); return(0); } } DBG("Card responded to reset\n"); SDHCtype=is_sdhc(); if(SDHCtype) DBG("SDHC card detected\n"); else // If not SDHC, Set blocksize to 512 bytes { DBG("Sending cmd16 (blocksize)\n"); cmd_CMD16(1); } SPI(0xFF); SPI_CS(0); SPI(0xFF); DBG("Init done\n"); return(1); }
// SendFileV2 (for minimig_v2) void SendFileV2(RAFile* file, unsigned char* key, int keysize, int address, int size) { int i,j; unsigned int keyidx=0; iprintf("File size: %dkB\r", size>>1); iprintf("["); if (keysize) { // read header RARead(file, sector_buffer, 0xb); } for (i=0; i<size; i++) { if (!(i&31)) iprintf("*"); RARead(file, sector_buffer, 512); if (keysize) { // decrypt ROM for (j=0; j<512; j++) { sector_buffer[j] ^= key[keyidx++]; if(keyidx >= keysize) keyidx -= keysize; } } EnableOsd(); unsigned int adr = address + i*512; SPI(OSD_CMD_WR); SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); for (j=0; j<512; j=j+4) { SPI(sector_buffer[j+0]); SPI(sector_buffer[j+1]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPI(sector_buffer[j+2]); SPI(sector_buffer[j+3]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); } DisableOsd(); } iprintf("]\r"); }
int cmd_write(unsigned long cmd, unsigned long lba) { int ctr; int result=0xff; // puts("In cmd_write\n"); SPI(cmd & 255); // puts("Command sent\n"); if(!SDHCtype) // If normal SD then we have to use byte offset rather than LBA offset. lba<<=9; // puts("Sending LBA\n"); SPI((lba>>24)&255); SPI((lba>>16)&255); SPI((lba>>8)&255); SPI(lba&255); // puts("Sending CRC - if any\n"); SPI((cmd>>16)&255); // CRC, if any ctr=40000; result=SPI_READ(); while(--ctr && (result==0xff)) { SPI(0xff); result=SPI_READ(); } // printf("Got result %d \n",result); return(result); }