Switch to ftdi on PC.

This commit is contained in:
Marco 2023-02-22 18:26:41 +01:00
parent 27b2f0bd87
commit 00b2b28024
9 changed files with 37 additions and 178 deletions

View File

@ -3,16 +3,20 @@
{
"name": "Linux",
"includePath": [
"/usr/avr/include",
"${workspaceFolder}/**"
"${workspaceFolder}/src/include/**",
"${default}",
"/usr/include/libftdi1/**"
],
"defines": [
"__AVR_ATmega32U4__"
],
"compilerPath": "/usr/bin/avr-gcc",
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-x64"
"intelliSenseMode": "linux-gcc-x64",
"browse": {
"path": [
"/usr/include/libftdi1/**"
]
}
}
],
"version": 4

10
.vscode/settings.json vendored
View File

@ -1,10 +1,6 @@
{
"files.associations": {
"io.h": "c",
"sfr_defs.h": "c",
"string.h": "c",
"random": "c",
"math.h": "c",
"bridge.h": "c"
"C_Cpp.errorSquiggles": "Enabled",
"files.associations": {
"pwm.h": "c"
}
}

View File

@ -1,10 +1,9 @@
#include <avr/io.h>
#include <util/delay.h>
#include "bridge.h"
#include "statemachine.h"
#include "pins.h"
bdg_sm_state_t bridge_state;
sm_trans_t bridge_sm_transitions[] =

View File

@ -1,5 +1,8 @@
#include <inttypes.h>
const uint8_t PORTB = 1;
void togglePin(volatile uint8_t *port, unsigned int pin);
void unsetPin(volatile uint8_t *port, unsigned int pin);
void setPin(volatile uint8_t *port, unsigned int pin);

View File

@ -1,20 +0,0 @@
#include <stdint.h>
#define NOTIFY_ARRAY_SIZE 10
typedef struct
{
void (*callOnInterrupt[NOTIFY_ARRAY_SIZE])();
int index;
} timer_notify_t;
void tmr_initT1(void);
void tmr_initT0(void);
void tmr_subToT1Overflow(void (*)());
void tmr_enableT1(void);
void tmr_disableT1(void);
void tmr_setT1CompareMatch(uint16_t cm);
uint16_t tmr_getT1Top(void);

View File

@ -1,30 +1,32 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ftdi.h>
#include <unistd.h>
#include "uart.h"
#include "timer.h"
#include "commands.h"
#include "pins.h"
#include "pwm.h"
#include "ftdi_utils.h"
int main()
{
sei();
/* With this MCU frequency (16 MHz), 25 is a baudrate of 38.4k baud */
uart_init(25);
if ((ftdi = ftdi_new()) == 0)
{
fprintf(stderr, "ftdi_new failed\n");
return EXIT_FAILURE;
}
pwm_init();
tmr_initT1();
tmr_enableT1();
/* Arduino Leonardo has a LED that we turn off here */
DDRC |= (1 << DDC7);
unsetPin(&PORTC, PIN7);
printf("Up and running.\n\r");
while (1) {
while (1)
{
cmd_worker();
pwm_worker();
}

View File

@ -2,10 +2,14 @@
#include <stdio.h>
#include "pins.h"
#include "ftdi_utils.h"
void setPin(volatile uint8_t *port, unsigned int pin)
{
*port |= (1 << pin);
if (fftdi_write_data(ftdi, buf, 1)< 0)
{
fprintf(stderr, "write failed for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(ftdi));
}
}
void unsetPin(volatile uint8_t *port, unsigned int pin)

View File

@ -1,26 +0,0 @@
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "pwm.h"
#include "timer.h"
int interrupt_received = 0;
uint32_t interrupt_counter = 0;
void pwm_init(void)
{
tmr_subToT1Overflow(pwm_interrupt);
}
void pwm_worker()
{
if (!interrupt_received)
return;
}
void pwm_interrupt(void)
{
}

View File

@ -1,103 +0,0 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.h>
#include <stdio.h>
#include "timer.h"
timer_notify_t timer1 =
{
.index = 0,
.callOnInterrupt = {0}
};
void tmr_initT1(void)
{
/*
* We are using Timer 1 Compare Match Unit B.
* OC1B is on Pin PB6. Let's make it an output.
*/
DDRB |= (1 << DDB6);
/* Enable Fast PWM with TOP in OCR1A */
TCCR1A |= (1 << WGM11) | (1 << WGM10);
TCCR1B |= (1 << WGM13) | (1 << WGM12);
/* Set OC1B on compare match, clear OC1B at TOP */
TCCR1A |= (1 << COM1B1);
/* Enable Counter1 Compare Match B Interrupt */
TIMSK1 |= (1 << OCIE1B);
/* Enable Counter1 Overflow Interrupt */
TIMSK1 |= (1 << TOIE1);
/* TOP value */
OCR1A = 40000;
/* Compare Match with OCR1B */
OCR1B = 20000;
}
void tmr_enableT1(void)
{
/* Clock prescaler of 8 */
TCCR1B |= (1 << CS11);
}
void tmr_disableT1(void)
{
TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12));
}
void tmr_setT1CompareMatch(uint16_t cm)
{
OCR1B = cm;
}
uint16_t tmr_getT1Top(void)
{
return OCR1A;
}
void tmr_initT0(void)
{
/* Initialize counter 0 */
TCNT0 = 0;
/* Enable Counter0 Compare Match A Interrupt */
TIMSK0 |= (1 << OCIE0A);
/* Select clock. Prescaler of 1024 */
TCCR0B |= (1 << CS02) | (1 << CS00);
/* Use CTC Mode */
TCCR0A |= (1 << WGM01);
/*
* OCR0A contains TOP value for counter:
*/
OCR0A = 255;
}
ISR(TIMER1_OVF_vect)
{
for(int i = 0; i < timer1.index; i++)
timer1.callOnInterrupt[i]();
}
ISR(TIMER1_COMPB_vect)
{
PORTD ^= (1 << PD7);
}
void tmr_subToT1Overflow(void (*call)())
{
if (timer1.index < NOTIFY_ARRAY_SIZE) {
timer1.callOnInterrupt[timer1.index] = call;
timer1.index++;
}
}