Conmanx360
Member
Hi! This is my first time posting. I have begun to learn C in hopes of using a Teensy 2.0++ to write to an EEPROM that I will use in my car's ECU. I have been trying to find other code that I can adapt to my scenario, and while I seem to be able to get the basics accomplished, I am stuck on how exactly to transmit the data in hex form over the serial port.
The EEPROM I am interfacing with is a 512kbit EEPROM, datasheet here: http://static.moates.net/zips/27SF512.pdf
Here is the code that I have so far.
The biggest part is setting up addresses, inputting the data, pulsing the write enable pin, and then getting to the next set in the data, which may be able to be accomplished with a simple ++ that brings a value up until the max value of the array is reached.
Where I am stuck at the main program loop. There may be pieces of that code that can be removed, but, I believe it should work. My main issue is, how do I send the data over the serial port? My initial idea was to have it stored into an array byte by byte, but I'm not sure if the Teensy's ram could hold it all at once. Also, I am not sure how to get a hexadecimal value through with usb_serial_getchar. I hope I have given enough data, and any help would be appreciated. I am new to all of this, and just began learning C about a month ago. Thanks!
The EEPROM I am interfacing with is a 512kbit EEPROM, datasheet here: http://static.moates.net/zips/27SF512.pdf
Here is the code that I have so far.
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include "delay_x.h"
#include "usb_serial.h"
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
// Define data ports
#define DATA1_PORT PORTD
#define DATA1_PIN PIND
#define DATA1_DDR DDRD
// Define address line ports
#define ADDR1_PORT PORTF
#define ADDR1_PIN PINF
#define ADDR1_DDR DDRF
#define ADDR2_PORT PORTA
#define ADDR2_PIN PINA
#define ADDR2_DDR DDRA
// Define control port and pins
#define CONT_PORT PORTE
#define CONT_DDR DDRE
#define CONT_PIN PINE
#define CONT_CE 0 // Chip Enable
void put_address(uint8_t address2, uint8_t address1) {
//CONT_PORT |= (1<<CONT_CE); //HIGH
ADDR2_PORT = address2;
ADDR1_PORT = address1;
//CONT_PORT &= ~(1<<CONT_CE); //LOW
}
void put_data(uint8_t data1) {
DATA1_PORT = data1;
_delay_us(20);
CONT_PORT &= ~(1<<CONT_WE); //LOW
CONT_PORT |= (1<<CONT_WE); //HIGH
}
uint8_t ADDR1, ADDR2;
void addr_increment(uint8_t lock_address)
{
if (ADDR1 < 0xFF)
ADDR1++;
else {
ADDR1 = 0;
if (ADDR2 < 0xFF)
ADDR2++;
else {
ADDR2 = 0;
}
}
if (lock_address == 0)
return;
//CONT_PORT |= (1<<CONT_CE); //HIGH
ADDR2_PORT = ADDR2;
ADDR1_PORT = ADDR1;
//CONT_PORT &= ~(1<<CONT_CE); //LOW
}
void bootloader()
{
cli();
// disable watchdog, if enabled
// disable all peripherals
UDCON = 1;
USBCON = (1<<FRZCLK); // disable USB
UCSR1B = 0;
_delay_ms(50);
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
__asm volatile("jmp 0x1FC00");
}
void initports()
{
DATA1_DDR = 0xFF; // set for output
ADDR1_DDR = ADDR2_DDR = 0xFF; //address ports are always output
ADDR1_PORT = ADDR2_PORT = 0;
CONT_DDR = 0xFF; //all control ports are always output
CONT_DDR &= ~(1<<CONT_RYBY); //except RY/BY# (input)
CONT_PORT |= (1<<CONT_RYBY); //enable pull up
CONT_PORT &= ~((1<<CONT_TRI) | (1<<CONT_CE)); //LOW
CONT_PORT |= ((1<<CONT_WE) | (1<<CONT_OE) | (1<<CONT_RESET)); //HIGH
// ADDR1_DDR = ADDR2_DDR = ADDR3_DDR = DATA1_DDR = DATA2_DDR = CONT_DDR = 0; //all ports are always input
// ADDR1_PORT = ADDR2_PORT = ADDR3_PORT = DATA1_PORT = DATA2_PORT = CONT_PORT = 0; //disable pull ups for all ports
// CONT_DDR |= (1<<CONT_TRI);
// CONT_PORT &= ~(1<<CONT_TRI); //LOW
// CONT_PORT |= (1<<CONT_TRI); //HIGH
}
int main(void)
{
int16_t in_data;
uint16_t addr, i;
uint32_t bss;
uint8_t vaddr1, vaddr2;
uint8_t state, cycle, tx_data, tx_wr, buf_ix, do_increment, do_verify, offset_2nddie;
uint8_t buf_read[4096];
// set for 8 MHz clock
CPU_PRESCALE(1);
releaseports();
usb_init();
while (!usb_configured());
initports();
_delay_ms(1000);
ADDR1 = ADDR2 = 0
Where I am stuck at the main program loop. There may be pieces of that code that can be removed, but, I believe it should work. My main issue is, how do I send the data over the serial port? My initial idea was to have it stored into an array byte by byte, but I'm not sure if the Teensy's ram could hold it all at once. Also, I am not sure how to get a hexadecimal value through with usb_serial_getchar. I hope I have given enough data, and any help would be appreciated. I am new to all of this, and just began learning C about a month ago. Thanks!