// Write a character to the VFD at the current position uint8_t VFDWriteChar( char newChar ) { if( VFDStatus != INITIALIZED ) return 1; // VFD is not initialized if( currentPositionX >= VFD_BUFFER_LENGTH ) return 1; // Out of bounds VFD_STROBE_START; sspSend(VFD_START_BYTE); sspSend(newChar); VFD_STROBE_STOP; currentPositionX += 1; return 0; }
void avrspi_tx(uint8_t byteToSend) { uint8_t spiBuf = byteToSend; gpioSetValue(RB_LED0, 1); sspSend(0, (uint8_t *)&spiBuf, 1); gpioSetValue(RB_LED0, 0); }
void nrf_write_long(const uint8_t cmd, int len, const uint8_t* data) { CS_LOW(); sspSendByte(cmd); sspSend(data,len); CS_HIGH(); };
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){ if(size > MAX_PKT) size=MAX_PKT; nrf_write_reg(R_CONFIG, R_CONFIG_PWR_UP| // Power on R_CONFIG_EN_CRC // CRC on, single byte ); // nrf_write_long(C_W_TX_PAYLOAD,size,pkt); uint16_t crc=crc16(pkt,size-2); pkt[size-2]=(crc >>8) & 0xff; pkt[size-1]=crc & 0xff; if(key !=NULL) xxtea_encode_words((uint32_t*)pkt,size/4,key); CS_LOW(); xmit_spi(C_W_TX_PAYLOAD); sspSend(0,pkt,size); CS_HIGH(); CE_HIGH(); delayms(1); // Send it. (only needs >10ys, i think) CE_LOW(); return nrf_cmd_status(C_NOP); };
// Set the VFD brightness uint8_t VFDSetBrightness( uint8_t newBright ) { if (VFDStatus != INITIALIZED) return 1; // VFD is not initialized currentBrightness = newBright; newBright = ~newBright; newBright >>= 4; newBright &= 0b00001111; newBright |= 0b00110000; VFD_STROBE_START; sspSend(VFD_COMMAND_BYTE); sspSend(newBright); VFD_STROBE_STOP; return 0; }
uint8_t at25GetRSR() { AT25_SELECT(); src_addr[0] = AT25_RDSR; sspSend(0, (uint8_t *)src_addr, 1); sspReceive(0, (uint8_t *)dest_addr, 1); AT25_DESELECT(); return dest_addr[0] & (AT25_RDSR_WEN | AT25_RDSR_RDY); }
void at25WriteEnable() { AT25_SELECT(); src_addr[0] = AT25_WREN; sspSend(0, (uint8_t *)src_addr, 1); AT25_DESELECT(); // Delay for at least 250nS (1nS @ 72MHz = ~0.0072 ticks) for (i = 0; i < 100; i++); }
// Set the current cursor position uint8_t VFDSetPosition( uint8_t y, uint8_t x ) { if (VFDStatus != INITIALIZED) return 1; // VFD is not initialized if( x >= VFD_BUFFER_LENGTH || y >= VFD_BUFFER_HEIGHT ) return 1; // Out of bounds uint8_t newPosition = 0b10000000; if( y == 1 ) newPosition |= 0b01000000; newPosition |= x; VFD_STROBE_START; sspSend(VFD_COMMAND_BYTE); sspSend(newPosition); VFD_STROBE_STOP; currentPositionX = x + 1; currentPositionY = y; return 0; }
at25Error_e at25Read (uint16_t address, uint8_t *buffer, uint32_t bufferLength) { if (address >= AT25_MAXADDRESS) { return AT25_ERROR_ADDRERR; } if (bufferLength > 6) { return AT25_ERROR_BUFFEROVERFLOW; } timeout = 0; while ( timeout < SSP_MAX_TIMEOUT ) { // Wait until the device is ready uint8_t status = at25GetRSR() & AT25_RDSR_RDY; if (status == 0) { break; } timeout++; } if ( timeout == SSP_MAX_TIMEOUT ) { return AT25_ERROR_TIMEOUT_WE; } AT25_SELECT(); // Read command (0x03), append A8 if > addr 256 bytes src_addr[0] = address > 0xFF ? AT25_READ | AT25_A8 : AT25_READ; src_addr[1] = (address); sspSend(0, (uint8_t *)src_addr, 2); sspReceive(0, (uint8_t *)&dest_addr[2], bufferLength); AT25_DESELECT(); // Fill response buffer for (i = 0; i < bufferLength; i++) { buffer[i] = dest_addr[i + 2]; } return AT25_ERROR_OK; }
char snd_pkt_no_crc(int size, uint8_t * pkt) { if(size > MAX_PKT) size=MAX_PKT; nrf_write_reg(R_CONFIG, R_CONFIG_PWR_UP| // Power on R_CONFIG_EN_CRC // CRC on, single byte ); CS_LOW(); xmit_spi(C_W_TX_PAYLOAD); sspSend(0,pkt,size); CS_HIGH(); CE_HIGH(); delayms(1); // Send it. (only needs >10ys, i think) CE_LOW(); return nrf_cmd_status(C_NOP); }
at25Error_e at25Write (uint16_t address, uint8_t *buffer, uint32_t bufferLength) { if (address >= AT25_MAXADDRESS) { return AT25_ERROR_ADDRERR; } if (bufferLength > 6) { return AT25_ERROR_BUFFEROVERFLOW; } // Set write enable latch at25WriteEnable(); timeout = 0; while ( timeout < SSP_MAX_TIMEOUT ) { // Wait until the device is write enabled if (at25GetRSR() == AT25_RDSR_WEN) { break; } timeout++; } if ( timeout == SSP_MAX_TIMEOUT ) { return AT25_ERROR_TIMEOUT_WE; } for (i = 0; i < bufferLength; i++) // Init RD and WR buffer { src_addr[i+2] = buffer[i]; // leave two bytes for cmd and offset(8 bits) dest_addr[i] = 0; } AT25_SELECT(); // Write command (0x02), append A8 if addr > 256 bytes src_addr[0] = address > 0xFF ? AT25_WRITE | AT25_A8 : AT25_WRITE; src_addr[1] = (address); sspSend(0, (uint8_t *)src_addr, bufferLength + 2); AT25_DESELECT(); // Wait at least 3ms for (i = 0; i < ((CFG_CPU_CCLK / 1000) * 3); i++); timeout = 0; while ( timeout < SSP_MAX_TIMEOUT ) { // Check status to see if write cycle is done or not AT25_SELECT(); src_addr[0] = AT25_RDSR; sspSend(0, (uint8_t *)src_addr, 1); sspReceive(0, (uint8_t *)dest_addr, 1); AT25_DESELECT(); // Wait until device is ready if ((dest_addr[0] & AT25_RDSR_RDY) == 0x00) { break; } timeout++; } if ( timeout == SSP_MAX_TIMEOUT ) { return AT25_ERROR_TIMEOUT_WFINISH; } for (i = 0; i < 300; i++); // Wait at least 250ns return AT25_ERROR_OK; }
inline void xmit_spi(uint8_t dat) { sspSend(0, (uint8_t*) &dat, 1); }
// Send a command code to the VFD uint8_t VFDSendCommand( eVFDCommand command ) { if ( command == VFD_OFF ){ // Turn off the VFD LPC_GPIO->SET[PORT0] = 1<<4; // Turn off the VFD Power VFDStatus = POWEREDOFF; return 0; } else if ( command == VFD_ON ) { // Turn on the VFD and Initialize it VFDStatus = NOTINITIALIZED; VFD_STROBE_STOP; // Initialize the Strobe line LPC_GPIO->CLR[PORT0] = 1<<4; // Turn on the 40v VFD Power // Initialize the VFD driver if( VFDSendCommand(VFD_INIT) == 0 ){ // Set position to 0,0 and use Euro font set if( VFDSendCommand(VFD_HOME) == 0 ){ VFDStatus = INITIALIZED; return 0; } else { return 1; } } else { return 1; } return 1; // Should never reach this point } else { // Commands requiring VFD SPI Data if (VFDStatus == POWEREDOFF) return 1; // VFD is not powered on if (VFDStatus != INITIALIZED && command != VFD_INIT && command != VFD_INIT2) return 1; VFD_STROBE_START; sspSend(VFD_COMMAND_BYTE); switch( command ){ case VFD_CLEAR: sspSend(0b00000001); // Clear VFD and return to "home" position (0,0) currentPositionX = 0; currentPositionY = 0; break; case VFD_INIT: sspSend(0b00001100); // Turn on display, turn off cursor, turn off blink. VFDStatus = INITIALIZED; currentBrightness = 255; break; case VFD_INIT2: sspSend(0b00001111); // Turn on display, turn on cursor, turn on blink. VFDStatus = INITIALIZED; currentBrightness = 255; break; case VFD_HOME: sspSend(0b00000011); // Returns the cursor to the "home" position (0,0), and set Japanese fontset(last bit) currentPositionX = 0; currentPositionY = 0; break; default: break; } VFD_STROBE_STOP; return 0; } // How the hell did we get here??? return 1; }
void nrf_write_long(const uint8_t cmd, int len, const uint8_t* data){ CS_LOW(); xmit_spi(cmd); sspSend(0,data,len); CS_HIGH(); };