uint8 portAccess(uint8 portNumber, uint8 bitMask, uint8 drive, uint8 high) { uint8 tempByte = 0x00; #ifdef DEBUG usartSendByteHex(portNumber); usartSendByte(':'); usartSendByteHex(bitMask); usartSendByte(':'); usartSendByteHex(drive); usartSendByte(':'); usartSendByteHex(high); usartSendByte('\r'); #endif switch ( portNumber ) { case 0: updatePort(A); break; case 1: updatePort(B); break; case 2: updatePort(C); break; case 3: updatePort(D); break; case 4: updatePort(E); break; } return tempByte; }
// -------------------------------------------------------------------------- void rs4xxSendByte(usart_cfg_st *usart, const BYTE c_byte) { RS4XX_TX_ENA(); usartSendByte(usart, c_byte); WAIT_FOR(USART_GetFlagStatus((USART_TypeDef*)usart->usart_if, USART_FLAG_TC) == RESET); RS4XX_TX_DIS(); }
//Send a string of data using USART void usartSendDataString(const char *dataString) { while(*dataString) { usartSendByte(*dataString); dataString++; } }
//main function int main (void) { // dummy for testing USART char *dataString="\r......Booting up.\r"; //init. USART usartInitialize();//--------*****-////TO DO: implement light diode blink if(usartInitialize()); else ERROR BLINK CODE.. also write reference for blinking code... //Send a string of data usartSendDataString(dataString); if(USART_ENABLE_printf) printf("NOTEFICATION: printf() is LINKED to Tx on USART!\r\n"); //Make some conversation... usartSendDataString("Do you want to echo Rx UART data using interrupts?\r\n*Enter 'y' to accept"); if(1)//byteOfData=usartReceiveByte()=='y') { interruptOn=true; UCSR0B |= (1<<RXCIE0); // Enable the USART Receive Complete interrupt (USART_RXCIE0) sei(); //macro for enabling interrupts globally (<avr/interrupt.h> must be included) usartSendDataString("->interrupt on Rx is ENABLED\r\n"); } else usartSendDataString("->interrupt on Rx DISABLED\r\n"); usartSendDataString("@ECHO ON\r\n"); //Program loop while(true) { if(!interruptOn) { //echo received data byteOfData=usartReceiveByte(); usartSendByte(byteOfData); } } }
// Execute pending USART read/write operations void usartExecute(void) { usbSelectEndpoint(OUT_ENDPOINT_ADDR); if ( usbOutPacketReady() ) { uint8 byte, chan; uint16 count; do { // Read/write flag & channel chan = usbRecvByte(); usartSendByte(chan); // Count high byte byte = usbRecvByte(); usartSendByte(byte); count = byte; // Count low byte byte = usbRecvByte(); usartSendByte(byte); count <<= 8; count |= byte; // Check to see if it's a read or a write if ( chan & 0x80 ) { // The host is reading a channel usbAckPacket(); // acknowledge the OUT packet usbSelectEndpoint(IN_ENDPOINT_ADDR); // switch to the IN endpoint #if USART_DEBUG == 1 debugSendFlashString(PSTR("READ(")); debugSendByteHex(count); debugSendByte(')'); debugSendByte('\r'); #endif while ( !(UCSR1A & (1<<TXC1)) ); // wait for send complete __asm volatile( "nop\nnop\nnop\nnop\n" // give things a chance to settle "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" "nop\nnop\nnop\nnop\n" ::); UCSR1B = (1<<RXEN1); // TX disabled, RX enabled while ( !usbInPacketReady() ); USART_OUT &= ~bmTX; // TX low says "I'm ready" do { byte = usartRecvByte(); if ( !usbReadWriteAllowed() ) { USART_OUT |= bmTX; // TX high says "I'm not ready" usbFlushPacket(); while ( !usbInPacketReady() ); USART_OUT &= ~bmTX; // TX low says "OK I'm ready now" } usbPutByte(byte); count--; } while ( count ); UCSR1B = (1<<TXEN1); // TX enabled, RX disabled USART_OUT |= bmTX; // TX high says "I acknowledge receipt of your data" usbFlushPacket(); // flush final packet usbSelectEndpoint(OUT_ENDPOINT_ADDR); // ready for next command return; // there cannot be any more work to do } else { // The host is writing a channel #if USART_DEBUG == 1 debugSendFlashString(PSTR("WRITE(")); debugSendByteHex(count); debugSendByte(')'); debugSendByte('\r'); #endif do { byte = usbRecvByte(); while ( PIND & bmRX ); // ensure RX is still low usartSendByte(byte); count--; } while ( count ); } } while ( usbReadWriteAllowed() ); usbAckPacket(); }
// Called once at startup // void mainInit(void) { __xdata uint8 thisByte = 0xFF; __xdata uint16 blockSize; // This is only necessary for cases where you want to load firmware into the RAM of an FX2 that // has already loaded firmware from an EEPROM. It should definitely be removed for firmwares // which are themselves to be loaded from EEPROM. #ifndef EEPROM RENUMERATE_UNCOND(); #endif // Needs to be matched to stuff in HDMI2USB/cypress/hdmi2usb.c TD_Init // void TD_Init(void) // Called once at startup // Clear wakeup (see AN15813: http://www.cypress.com/?docID=4633) WAKEUPCS = bmWU | bmDPEN | bmWUEN; WAKEUPCS = bmWU | bmDPEN | bmWUEN; // Disable alternate functions for PORTA 0,1,3 & 7. PORTACFG = 0x00; /* // Return FIFO settings back to default just in case previous firmware messed with them. SYNCDELAY; PINFLAGSAB = 0x00; SYNCDELAY; PINFLAGSCD = 0x00; SYNCDELAY; FIFOPINPOLAR = 0x00; // Global settings SYNCDELAY; REVCTL = (bmDYN_OUT | bmENH_PKT); // 0x03 SYNCDELAY; CPUCS = bmCLKSPD1; // 48MHz 0x10 // Drive IFCLK at 48MHz, enable slave FIFOs //SYNCDELAY; IFCONFIG = (bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | bmFIFOS); SYNCDELAY; IFCONFIG = (bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | bmPORTS); // 0xe0 // EP1OUT & EP1IN SYNCDELAY; EP1OUTCFG = (bmVALID | bmBULK); // 0xa0 SYNCDELAY; EP1INCFG = (bmVALID | bmBULK); // 0xa0 // EP2OUT & EP6IN are quad-buffered bulk endpoints SYNCDELAY; EP2CFG = (bmVALID | bmBULK); // 0xa0 SYNCDELAY; EP4CFG = 0x00; SYNCDELAY; EP6CFG = (bmVALID | bmBULK | bmDIR); // 0xe0 SYNCDELAY; EP8CFG = 0x00; // EP2OUT & EP6IN automatically commit packets SYNCDELAY; EP2FIFOCFG = bmAUTOOUT; // 0x10 SYNCDELAY; EP4FIFOCFG = 0x00; SYNCDELAY; EP6FIFOCFG = bmAUTOIN; // 0x08 SYNCDELAY; EP8FIFOCFG = 0x00; // Reset FIFOs for EP2OUT & EP6IN SYNCDELAY; FIFORESET = bmNAKALL; // 0x80 SYNCDELAY; FIFORESET = 2; // reset EP2OUT SYNCDELAY; FIFORESET = 6; // reset EP6IN SYNCDELAY; FIFORESET = 0x00; // Arm EP1OUT EP1OUTBC = 0x00; // Arm the EP2OUT buffers. Done four times because it's quad-buffered SYNCDELAY; OUTPKTEND = bmSKIP | 2; // EP2OUT 0x82 SYNCDELAY; OUTPKTEND = bmSKIP | 2; SYNCDELAY; OUTPKTEND = bmSKIP | 2; SYNCDELAY; OUTPKTEND = bmSKIP | 2; // Auto-commit 512-byte packets from EP6IN (master may commit early by asserting PKTEND) SYNCDELAY; EP6AUTOINLENH = 0x02; SYNCDELAY; EP6AUTOINLENL = 0x00; // Turbo I2C I2CTL |= bm400KHZ; // Auto-pointers AUTOPTRSETUP = bmAPTREN | bmAPTR1INC | bmAPTR2INC; // 0x07 // Port lines all inputs... IOA = 0xFF; OEA = 0x00; IOB = 0xFF; OEB = 0x00; IOC = 0xFF; OEC = 0x00; IOD = 0xFF; OED = 0x00; IOE = 0xFF; OEE = 0x00; #ifdef EEPROM #ifdef BSP #include STR(boards/BSP.c) #endif #endif */ I2CTL |= bm400KHZ; TD_Init(); #ifdef DEBUG usartInit(); { const uint8 *s = dev_strings; uint8 len; s = s + *s; len = (*s)/2 - 1; s += 2; while ( len ) { usartSendByte(*s); s += 2; len--; } usartSendByte(' '); len = (*s)/2 - 1; s += 2; while ( len ) { usartSendByte(*s); s += 2; len--; } usartSendByte('\r'); } #endif }
// Read "length" bytes from RAM at "buf", and write them to the attached EEPROM at address "addr". // bool promWrite(bool bank, uint16 addr, uint8 length, const __xdata uint8 *buf) { __xdata uint8 i; const __xdata uint8 i2cAddr = bank ? BANK_1 : BANK_0; #ifdef DEBUG usartSendString("promWrite("); usartSendByte(bank?'1':'0'); usartSendByte(','); usartSendWordHex(addr); usartSendByte(','); usartSendWordHex(length); usartSendByte(')'); usartSendByte('\r'); #endif // Wait for I2C idle // while ( I2CS & bmSTOP ); // Send the WRITE command // I2CS = bmSTART; I2DAT = i2cAddr; // Write I2C address byte (WRITE) if ( promWaitForAck() ) { return true; } // Send the address, MSB first // I2DAT = MSB(addr); // Write MSB of address if ( promWaitForAck() ) { return true; } I2DAT = LSB(addr); // Write LSB of address if ( promWaitForAck() ) { return true; } // Write the data // for ( i = 0; i < length; i++ ) { I2DAT = *buf++; if ( promWaitForDone() ) { return true; } } I2CS |= bmSTOP; // Wait for I2C idle // while ( I2CS & bmSTOP ); do { I2CS = bmSTART; I2DAT = i2cAddr; // Write I2C address byte (WRITE) if ( promWaitForDone() ) { return true; } I2CS |= bmSTOP; while ( I2CS & bmSTOP ); } while ( !(I2CS & bmACK) ); return false; }
// Read "length" bytes from address "addr" in the attached EEPROM, and write them to RAM at "buf". // bool promRead(bool bank, uint16 addr, uint8 length, __xdata uint8 *buf) { __xdata uint8 i; const __xdata uint8 i2cAddr = bank ? BANK_1 : BANK_0; #ifdef DEBUG usartSendString("promRead("); usartSendByte(bank?'1':'0'); usartSendByte(','); usartSendWordHex(addr); usartSendByte(','); usartSendWordHex(length); usartSendByte(')'); usartSendByte('\r'); #endif // Wait for I2C idle // while ( I2CS & bmSTOP ); // Send the WRITE command // I2CS = bmSTART; I2DAT = i2cAddr; // Write I2C address byte (WRITE) if ( promWaitForAck() ) { return true; } // Send the address, MSB first // I2DAT = MSB(addr); // Write MSB of address if ( promWaitForAck() ) { return true; } I2DAT = LSB(addr); // Write LSB of address if ( promWaitForAck() ) { return true; } // Send the READ command // I2CS = bmSTART; I2DAT = (i2cAddr | READ); // Write I2C address byte (READ) if ( promWaitForDone() ) { return true; } // Read dummy byte // i = I2DAT; if ( promWaitForDone() ) { return true; } // Now get the actual data // for ( i = 0; i < (length-1); i++ ) { buf[i] = I2DAT; if ( promWaitForDone() ) { return true; } } // Terminate the read operation and get last byte // I2CS = bmLASTRD; if ( promWaitForDone() ) { return true; } buf[i] = I2DAT; if ( promWaitForDone() ) { return true; } I2CS = bmSTOP; i = I2DAT; return false; }