diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b3a5613 --- /dev/null +++ b/.github/workflows/build.yml @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b8da3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -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 diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -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 +#include + +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 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..d49b82a --- /dev/null +++ b/platformio.ini @@ -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 diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..8d94e9b --- /dev/null +++ b/src/main.c @@ -0,0 +1,63 @@ +#include +#include +#include +#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; + } + } +} diff --git a/src/swio.c b/src/swio.c new file mode 100644 index 0000000..2a7afdb --- /dev/null +++ b/src/swio.c @@ -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); +} diff --git a/src/swio.h b/src/swio.h new file mode 100644 index 0000000..3a9ab91 --- /dev/null +++ b/src/swio.h @@ -0,0 +1,16 @@ +#ifndef SWIO_H +#define SWIO_H +#include +#include + +// 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 diff --git a/src/uart.c b/src/uart.c new file mode 100644 index 0000000..4bbd394 --- /dev/null +++ b/src/uart.c @@ -0,0 +1,42 @@ +#include +#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; +} diff --git a/src/uart.h b/src/uart.h new file mode 100644 index 0000000..1668615 --- /dev/null +++ b/src/uart.h @@ -0,0 +1,9 @@ +#ifndef UART_H +#define UART_H + +#include +extern FILE *uart; + +void uart_init(); + +#endif diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html