-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37e2cba
commit 285be0b
Showing
11 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: PlatformIO CI | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.cache/pip | ||
~/.platformio/.cache | ||
key: ${{ runner.os }}-pio | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.9' | ||
- name: Install PlatformIO Core | ||
run: pip install --upgrade platformio | ||
|
||
- name: Build PlatformIO Project | ||
run: pio run | ||
|
||
- name: "Upload Firmware" | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: Uno Firmware (Hex) | ||
path: .pio/build/uno/firmware.hex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.pio | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
This directory is intended for project header files. | ||
|
||
A header file is a file containing C declarations and macro definitions | ||
to be shared between several project source files. You request the use of a | ||
header file in your project source file (C, C++, etc) located in `src` folder | ||
by including it, with the C preprocessing directive `#include'. | ||
|
||
```src/main.c | ||
|
||
#include "header.h" | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
``` | ||
|
||
Including a header file produces the same results as copying the header file | ||
into each source file that needs it. Such copying would be time-consuming | ||
and error-prone. With a header file, the related declarations appear | ||
in only one place. If they need to be changed, they can be changed in one | ||
place, and programs that include the header file will automatically use the | ||
new version when next recompiled. The header file eliminates the labor of | ||
finding and changing all the copies as well as the risk that a failure to | ||
find one copy will result in inconsistencies within a program. | ||
|
||
In C, the usual convention is to give header files names that end with `.h'. | ||
It is most portable to use only letters, digits, dashes, and underscores in | ||
header file names, and at most one dot. | ||
|
||
Read more about using header files in official GCC documentation: | ||
|
||
* Include Syntax | ||
* Include Operation | ||
* Once-Only Headers | ||
* Computed Includes | ||
|
||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
This directory is intended for project specific (private) libraries. | ||
PlatformIO will compile them to static libraries and link into executable file. | ||
|
||
The source code of each library should be placed in a an own separate directory | ||
("lib/your_library_name/[here are source files]"). | ||
|
||
For example, see a structure of the following two libraries `Foo` and `Bar`: | ||
|
||
|--lib | ||
| | | ||
| |--Bar | ||
| | |--docs | ||
| | |--examples | ||
| | |--src | ||
| | |- Bar.c | ||
| | |- Bar.h | ||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html | ||
| | | ||
| |--Foo | ||
| | |- Foo.c | ||
| | |- Foo.h | ||
| | | ||
| |- README --> THIS FILE | ||
| | ||
|- platformio.ini | ||
|--src | ||
|- main.c | ||
|
||
and a contents of `src/main.c`: | ||
``` | ||
#include <Foo.h> | ||
#include <Bar.h> | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
|
||
``` | ||
|
||
PlatformIO Library Dependency Finder will find automatically dependent | ||
libraries scanning project source files. | ||
|
||
More information about PlatformIO Library Dependency Finder | ||
- https://docs.platformio.org/page/librarymanager/ldf.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
; PlatformIO Project Configuration File | ||
; | ||
; Build options: build flags, source filter | ||
; Upload options: custom upload port, speed and extra flags | ||
; Library options: dependencies, extra library storages | ||
; Advanced options: extra scripting | ||
; | ||
; Please visit documentation for the other options and examples | ||
; https://docs.platformio.org/page/projectconf.html | ||
|
||
[env:uno] | ||
platform = atmelavr | ||
board = uno |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <stdio.h> | ||
#include <avr/io.h> | ||
#include <stdint.h> | ||
#include "uart.h" | ||
#include "swio.h" | ||
|
||
#define TARGET_NRESET_PORT PORTB | ||
#define TARGET_NRESET_DDR DDRB | ||
#define TARGET_NRESET_BIT 1 | ||
|
||
void target_reset(int x) { | ||
if (x) | ||
TARGET_NRESET_PORT &= ~_BV(TARGET_NRESET_BIT); | ||
else | ||
TARGET_NRESET_PORT |= _BV(TARGET_NRESET_BIT); | ||
} | ||
|
||
#define PROTOCOL_START '!' | ||
#define PROTOCOL_ACK '+' | ||
#define PROTOCOL_TEST '?' | ||
#define PROTOCOL_RESET 'a' | ||
#define PROTOCOL_NORES 'A' | ||
#define PROTOCOL_WRITE_REG 'w' | ||
#define PROTOCOL_READ_REG 'r' | ||
|
||
int main() { | ||
uint8_t reg; | ||
uint32_t val; | ||
|
||
// Make the target reset pin an output. | ||
TARGET_NRESET_DDR |= _BV(TARGET_NRESET_BIT); | ||
|
||
uart_init(); | ||
swio_init(); | ||
|
||
fputc(PROTOCOL_START, uart); | ||
while (1) { | ||
switch (fgetc(uart)) { | ||
case PROTOCOL_TEST: | ||
fputc(PROTOCOL_ACK, uart); | ||
break; | ||
case PROTOCOL_RESET: | ||
target_reset(1); | ||
fputc(PROTOCOL_ACK, uart); | ||
break; | ||
case PROTOCOL_NORES: | ||
target_reset(0); | ||
fputc(PROTOCOL_ACK, uart); | ||
break; | ||
case PROTOCOL_WRITE_REG: | ||
fread(®, sizeof(uint8_t), 1, uart); | ||
fread(&val, sizeof(uint32_t), 1, uart); | ||
swio_write_reg(reg, val); | ||
fputc(PROTOCOL_ACK, uart); | ||
break; | ||
case PROTOCOL_READ_REG: | ||
fread(®, sizeof(uint8_t), 1, uart); | ||
val = swio_read_reg(reg); | ||
fwrite(&val, sizeof(uint32_t), 1, uart); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include "swio.h" | ||
|
||
// Timings. | ||
// T = 1 / 8 MHz | ||
// 0 = T-4T low, T-16T high | ||
// 1 = 6T-64T low, T-16T high | ||
|
||
// Note: bitwise ops take up 2 clock cycles (1T). | ||
// nop are 1 cycle (0.5T). | ||
|
||
static inline void swio_send_one() { | ||
/* 0.0T */ SWIO_DDR |= _BV(SWIO_BIT); | ||
/* 1.0T */ SWIO_PORT &= ~_BV(SWIO_BIT); | ||
/* 2.0T */ asm("nop"); asm("nop"); | ||
/* 3.0T */ SWIO_PORT |= _BV(SWIO_BIT); | ||
/* 4.0T */ SWIO_DDR &= ~SWIO_BIT; | ||
} | ||
|
||
static inline void swio_send_zero() { | ||
/* 0.0T */ SWIO_DDR |= _BV(SWIO_BIT); | ||
/* 1.0T */ SWIO_PORT &= ~_BV(SWIO_BIT); | ||
/* 2.0T */ asm("nop"); asm("nop"); | ||
/* 3.0T */ asm("nop"); asm("nop"); | ||
/* 4.0T */ asm("nop"); asm("nop"); | ||
/* 5.0T */ asm("nop"); asm("nop"); | ||
/* 6.0T */ asm("nop"); asm("nop"); | ||
/* 7.0T */ asm("nop"); asm("nop"); | ||
/* 8.0T */ SWIO_PORT |= _BV(SWIO_BIT); | ||
/* 9.0T */ SWIO_DDR &= ~SWIO_BIT; | ||
} | ||
|
||
static inline char swio_recv_bit() { | ||
char x; | ||
/* 0.0T */ SWIO_DDR |= _BV(SWIO_BIT); | ||
/* 1.0T */ SWIO_PORT &= ~_BV(SWIO_BIT); | ||
/* 2.0T */ SWIO_PORT |= _BV(SWIO_BIT); // Precharge when the line is floating. | ||
/* 3.0T */ SWIO_DDR &= ~_BV(SWIO_BIT); | ||
/* 4.0T */ asm("nop"); asm("nop"); | ||
/* 5.0T */ asm("nop"); asm("nop"); | ||
/* 6.0T */ x = SWIO_PIN; | ||
|
||
// Wait for the line to come back up if it's down. | ||
while (!(SWIO_PIN & _BV(SWIO_BIT))) | ||
; | ||
|
||
return x & _BV(SWIO_BIT); | ||
} | ||
|
||
// Write a register. | ||
void swio_write_reg(uint8_t addr, uint32_t val) { | ||
char i; | ||
|
||
// Start bit. | ||
swio_send_one(); | ||
|
||
// Send the address. | ||
for (i = 0; i < 7; i++) { | ||
if (addr & 0x40) | ||
swio_send_one(); | ||
else | ||
swio_send_zero(); | ||
|
||
addr <<= 1; | ||
} | ||
|
||
// Start bit. | ||
swio_send_one(); | ||
|
||
// Send the word. | ||
for (i = 0; i < 32; i++) { | ||
if (val & 0x80000000) | ||
swio_send_one(); | ||
else | ||
swio_send_zero(); | ||
|
||
val <<= 1; | ||
} | ||
|
||
// Stop bit. | ||
_delay_us(10); | ||
} | ||
|
||
// Read a register. | ||
uint32_t swio_read_reg(uint8_t addr) { | ||
char i; | ||
uint32_t x = 0; | ||
|
||
// Start bit. | ||
swio_send_one(); | ||
|
||
// Send the address. | ||
for (i = 0; i < 7; i++) { | ||
if (addr & 0x40) | ||
swio_send_one(); | ||
else | ||
swio_send_zero(); | ||
|
||
addr <<= 1; | ||
} | ||
|
||
// Start bit. | ||
swio_send_zero(); | ||
|
||
// Receive the word. | ||
for (i = 0; i < 32; i++) { | ||
x <<= 1; | ||
|
||
if (swio_recv_bit()) | ||
x |= 1; | ||
} | ||
|
||
// Stop bit. | ||
_delay_us(10); | ||
|
||
return x; | ||
} | ||
|
||
void swio_init() { | ||
SWIO_PORT |= _BV(SWIO_BIT); | ||
SWIO_DDR |= _BV(SWIO_BIT); | ||
_delay_ms(5); | ||
SWIO_PORT &= ~_BV(SWIO_BIT); | ||
_delay_ms(20); | ||
SWIO_PORT |= _BV(SWIO_BIT); | ||
SWIO_DDR &= ~_BV(SWIO_BIT); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef SWIO_H | ||
#define SWIO_H | ||
#include <avr/io.h> | ||
#include <util/delay.h> | ||
|
||
// SWIO on PB0 | ||
#define SWIO_DDR DDRB | ||
#define SWIO_PORT PORTB | ||
#define SWIO_PIN PINB | ||
#define SWIO_BIT 0 | ||
|
||
void swio_init(); | ||
void swio_write_reg(uint8_t addr, uint32_t val); | ||
uint32_t swio_read_reg(uint8_t addr); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <avr/io.h> | ||
#include "uart.h" | ||
|
||
int _uart_putchar(char c, FILE *unused); | ||
int _uart_getchar(FILE *unused); | ||
FILE uart_file = FDEV_SETUP_STREAM(_uart_putchar, _uart_getchar, _FDEV_SETUP_READ | _FDEV_SETUP_WRITE); | ||
FILE *uart = &uart_file; | ||
|
||
void uart_init() { | ||
// Enable 2X clock. | ||
UCSR0A |= _BV(U2X0); | ||
|
||
// Set the baud rate divider. | ||
// 16M / 8 / (16 + 1) = 117647 bps (2% error) | ||
UBRR0L = 16; | ||
|
||
// Enable TX. | ||
UCSR0B |= _BV(TXEN0) | _BV(RXEN0); | ||
} | ||
|
||
int _uart_putchar(char c, FILE *unused) { | ||
(void)unused; | ||
|
||
// Wait for the UART to become ready. | ||
while (!(UCSR0A & _BV(UDRE0))) | ||
; | ||
|
||
// Send. | ||
UDR0 = c; | ||
|
||
return 0; | ||
} | ||
|
||
int _uart_getchar(FILE *unused) { | ||
(void)*unused; | ||
|
||
// Wait for a character. | ||
while (!(UCSR0A & _BV(RXC0))) | ||
; | ||
|
||
return UDR0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef UART_H | ||
#define UART_H | ||
|
||
#include <stdio.h> | ||
extern FILE *uart; | ||
|
||
void uart_init(); | ||
|
||
#endif |
Oops, something went wrong.