Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atmega32: Add ATmega32 and ATmega324p support. Implement simpler-api branch #138

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
43 changes: 43 additions & 0 deletions src/chips/ATmega2560.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { adcConfig } from '../peripherals/adc_atmega328p';
import { clockConfig } from '../peripherals/clock';
import { eepromConfig } from '../peripherals/eeprom';
import {
portAConfig,
portEConfig,
portFConfig,
portGConfig,
portHConfig,
portJConfig,
} from '../peripherals/gpio_atmega2560';
import { portBConfig, portCConfig, portDConfig } from '../peripherals/gpio_atmega328p';
import { spiConfig } from '../peripherals/spi';
import { timer0Config, timer1Config, timer2Config } from '../peripherals/timer_atmega328p';
import { twiConfig } from '../peripherals/twi';
import { usart0Config } from '../peripherals/usart_atmega328p';
import { Chip } from './chip';

export const ATmega2560: Chip = {
flashSize: 0x40000,
ramSize: 0x2000,
eepromSize: 0x1000,
registerSpace: 0x100,
defaultFrequency: 16e6,
clock: clockConfig,
eeprom: eepromConfig,
gpio: {
A: portAConfig,
B: portBConfig,
C: portCConfig,
D: portDConfig,
E: portEConfig,
F: portFConfig,
G: portGConfig,
H: portHConfig,
J: portJConfig,
},
timers: [timer0Config, timer1Config, timer2Config],
spi: [spiConfig],
usart: [usart0Config],
twi: [twiConfig],
adc: adcConfig,
};
25 changes: 25 additions & 0 deletions src/chips/ATmega32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { adcConfig } from '../peripherals/adc_atmega32';
import { clockConfig } from '../peripherals/clock';
import { eepromConfig } from '../peripherals/eeprom';
import { portAConfig, portBConfig, portCConfig, portDConfig } from '../peripherals/gpio_atmega32';
import { spiConfig } from '../peripherals/spi';
import { timer0Config, timer1Config, timer2Config } from '../peripherals/timer_atmega32';
import { twiConfig } from '../peripherals/twi';
import { usart0Config } from '../peripherals/usart_atmega32';
import { Chip } from './chip';

export const ATmega32: Chip = {
flashSize: 0x8000,
ramSize: 0x800,
eepromSize: 0x400,
registerSpace: 0x100,
defaultFrequency: 16e6,
clock: clockConfig,
eeprom: eepromConfig,
gpio: { A: portAConfig, B: portBConfig, C: portCConfig, D: portDConfig },
timers: [timer0Config, timer1Config, timer2Config],
spi: [spiConfig],
usart: [usart0Config],
twi: [twiConfig],
adc: adcConfig,
};
25 changes: 25 additions & 0 deletions src/chips/ATmega324p.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { adcConfig } from '../peripherals/adc_atmega324p';
import { clockConfig } from '../peripherals/clock';
import { eepromConfig } from '../peripherals/eeprom_atmega324p';
import { portAConfig, portBConfig, portCConfig, portDConfig } from '../peripherals/gpio_atmega324p';
import { spiConfig } from '../peripherals/spi_atmega324p';
import { timer0Config, timer1Config, timer2Config } from '../peripherals/timer_atmega324p';
import { twiConfig } from '../peripherals/twi_atmega324p';
import { usart0Config } from '../peripherals/usart_atmega324p';
import { Chip } from './chip';

export const ATmega324p: Chip = {
flashSize: 0x8000,
ramSize: 0x800,
eepromSize: 0x400,
registerSpace: 0x100,
defaultFrequency: 20e6,
clock: clockConfig,
eeprom: eepromConfig,
gpio: { A: portAConfig, B: portBConfig, C: portCConfig, D: portDConfig },
timers: [timer0Config, timer1Config, timer2Config],
spi: [spiConfig],
usart: [usart0Config],
twi: [twiConfig],
adc: adcConfig,
};
25 changes: 25 additions & 0 deletions src/chips/ATmega328p.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { adcConfig } from '../peripherals/adc_atmega328p';
import { clockConfig } from '../peripherals/clock';
import { eepromConfig } from '../peripherals/eeprom';
import { portBConfig, portCConfig, portDConfig } from '../peripherals/gpio_atmega328p';
import { spiConfig } from '../peripherals/spi';
import { timer0Config, timer1Config, timer2Config } from '../peripherals/timer_atmega328p';
import { twiConfig } from '../peripherals/twi';
import { usart0Config } from '../peripherals/usart_atmega328p';
import { Chip } from './chip';

export const ATmega328p: Chip = {
flashSize: 0x8000,
ramSize: 0x800,
eepromSize: 0x400,
registerSpace: 0x100,
defaultFrequency: 16e6,
clock: clockConfig,
eeprom: eepromConfig,
gpio: { B: portBConfig, C: portCConfig, D: portDConfig },
timers: [timer0Config, timer1Config, timer2Config],
spi: [spiConfig],
usart: [usart0Config],
twi: [twiConfig],
adc: adcConfig,
};
24 changes: 24 additions & 0 deletions src/chips/chip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ADCConfig } from '../peripherals/adc';
import { AVRClockConfig } from '../peripherals/clock';
import { AVREEPROMConfig } from '../peripherals/eeprom';
import { AVRPortConfig } from '../peripherals/gpio';
import { SPIConfig } from '../peripherals/spi';
import { AVRTimerConfig } from '../peripherals/timer';
import { TWIConfig } from '../peripherals/twi';
import { USARTConfig } from '../peripherals/usart';

export interface Chip {
flashSize: number;
ramSize: number;
eepromSize: number;
registerSpace: number;
defaultFrequency: number;
clock: AVRClockConfig;
eeprom?: AVREEPROMConfig;
gpio: { [key: string]: AVRPortConfig };
timers: AVRTimerConfig[];
spi: SPIConfig[];
usart: USARTConfig[];
twi: TWIConfig[];
adc: ADCConfig;
}
50 changes: 50 additions & 0 deletions src/create-avr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Chip } from './chips/chip';
import { CPU } from './cpu/cpu';
import { AVRClock } from './peripherals/clock';
import { AVREEPROM, EEPROMBackend, EEPROMMemoryBackend } from './peripherals/eeprom';
import { AVRIOPort } from './peripherals/gpio';
import { AVRSPI } from './peripherals/spi';
import { AVRTimer } from './peripherals/timer';
import { AVRTWI } from './peripherals/twi';
import { AVRUSART } from './peripherals/usart';

export interface CreateAVROptions {
eepromBackend?: EEPROMBackend;
program?: Uint16Array;
clockSpeedHz?: number;
}

export interface AVR {
cpu: CPU;
timers: AVRTimer[];
clock: AVRClock;
eeprom?: AVREEPROM;
spi: AVRSPI[];
usart: AVRUSART[];
twi: AVRTWI[];
gpio: {
[key: string]: AVRIOPort;
};
}

export function createAVR(config: Chip, options: CreateAVROptions = {}): AVR {
const frequency = options.clockSpeedHz ?? config.defaultFrequency;
const cpu = new CPU(options.program ?? new Uint16Array(config.flashSize / 2), config.ramSize);
const timers = config.timers.map((timerConfig) => new AVRTimer(cpu, timerConfig));
const clock = new AVRClock(cpu, frequency, config.clock);
const eeprom =
config.eeprom &&
new AVREEPROM(
cpu,
options.eepromBackend ?? new EEPROMMemoryBackend(config.eepromSize),
config.eeprom
);
const spi = config.spi.map((spiConfig) => new AVRSPI(cpu, spiConfig, frequency));
const usart = config.usart.map((usartConfig) => new AVRUSART(cpu, usartConfig, frequency));
const twi = config.twi.map((twiConfig) => new AVRTWI(cpu, twiConfig, frequency));
const gpio: { [key: string]: AVRIOPort } = {};
for (const port of Object.keys(config.gpio)) {
gpio[port] = new AVRIOPort(cpu, config.gpio[port]);
}
return { cpu, timers, clock, eeprom, spi, usart, twi, gpio };
}
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,38 @@
*
* Copyright (C) 2019, 2020, Uri Shaked
*/

export { CPU } from './cpu/cpu';
export type { CPUMemoryHook, CPUMemoryHooks } from './cpu/cpu';
export { avrInstruction } from './cpu/instruction';
export { avrInterrupt } from './cpu/interrupt';
export {
adcConfig,
ADCMuxInputType,
ADCReference,
atmega328Channels,
AVRADC,
} from './peripherals/adc';
export { ADCMuxInputType, ADCReference, AVRADC } from './peripherals/adc';
export { adcConfig, atmega328Channels } from './peripherals/adc_atmega328p';
export type { ADCConfig, ADCMuxConfiguration, ADCMuxInput } from './peripherals/adc';
export { AVRClock, clockConfig } from './peripherals/clock';
export type { AVRClockConfig } from './peripherals/clock';
export { AVREEPROM, eepromConfig, EEPROMMemoryBackend } from './peripherals/eeprom';
export type { AVREEPROMConfig, EEPROMBackend } from './peripherals/eeprom';
export { AVRIOPort, PinState } from './peripherals/gpio';
export {
AVRIOPort,
INT0,
INT1,
PCINT0,
PCINT1,
PCINT2,
PinState,
portAConfig,
portBConfig,
portCConfig,
portDConfig,
} from './peripherals/gpio_atmega328p';
export {
portAConfig,
portEConfig,
portFConfig,
portGConfig,
portHConfig,
portJConfig,
portKConfig,
portLConfig,
} from './peripherals/gpio';
} from './peripherals/gpio_atmega2560';
export type {
AVRExternalInterrupt,
AVRPinChangeInterrupt,
Expand All @@ -48,10 +43,17 @@ export type {
} from './peripherals/gpio';
export { AVRSPI, spiConfig } from './peripherals/spi';
export type { SPIConfig, SPITransferCallback } from './peripherals/spi';
export { AVRTimer, timer0Config, timer1Config, timer2Config } from './peripherals/timer';
export { AVRTimer } from './peripherals/timer';
export { timer0Config, timer1Config, timer2Config } from './peripherals/timer_atmega328p';
export type { AVRTimerConfig } from './peripherals/timer';
export * from './peripherals/twi';
export { AVRUSART, usart0Config } from './peripherals/usart';
export { AVRUSART } from './peripherals/usart';
export { usart0Config } from './peripherals/usart_atmega328p';
export { AVRUSI } from './peripherals/usi';
export { AVRWatchdog, watchdogConfig } from './peripherals/watchdog';
export type { WatchdogConfig } from './peripherals/watchdog';
export { ATmega324p } from './chips/ATmega324p';
export { ATmega32 } from './chips/ATmega32';
export { ATmega328p } from './chips/ATmega328p';
export { ATmega2560 } from './chips/ATmega2560';
export { createAVR } from './create-avr';
3 changes: 2 additions & 1 deletion src/peripherals/adc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CPU } from '../cpu/cpu';
import { asmProgram, TestProgramRunner } from '../utils/test-utils';
import { AVRADC, adcConfig, ADCMuxInputType } from './adc';
import { AVRADC, ADCMuxInputType } from './adc';
import { adcConfig } from './adc_atmega328p';

const R16 = 16;
const R17 = 17;
Expand Down
Loading