diff --git a/.github/.gitignore b/.github/.gitignore deleted file mode 100644 index 8b13789..0000000 --- a/.github/.gitignore +++ /dev/null @@ -1 +0,0 @@ - diff --git a/.github/actions/get_changed_files/action.yml b/.github/actions/get_changed_files/action.yml new file mode 100644 index 0000000..ecf73ac --- /dev/null +++ b/.github/actions/get_changed_files/action.yml @@ -0,0 +1,13 @@ +name: "Get changed files list" +description: "Setup Node with caching for dependencies" +runs: + using: "composite" + steps: + - name: Get changed files list + id: get_changed_files + run: | + echo "Changed files:" + git diff --name-only ${{ github.event.pull_request.base.sha }} > changed_files.txt + cat changed_files.txt + echo "The list is saved to changed_files.txt" + shell: bash \ No newline at end of file diff --git a/.github/workflows/Lab_01_CI.yml b/.github/workflows/Lab_01_CI.yml deleted file mode 100644 index 985573b..0000000 --- a/.github/workflows/Lab_01_CI.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Cheking of compile Arduino sketch for AVR/AtMega and ESP8266 - -on: - push: - branches: [ main, master ] - pull_request: - branches: [ main, master ] - -jobs: - test-matrix: - strategy: - matrix: - arduino-platform: - - "arduino:avr" - - "esp8266:esp8266" - include: - - arduino-platform: "arduino:avr" - fqbn: "arduino:avr:mega" - - arduino-platform: "esp8266:esp8266" - fqbn: "esp8266:esp8266:generic" - - runs-on: ubuntu-22.04 - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Arduino CLI - uses: arduino/setup-arduino-cli@v2 - - - name: Install platform - run: | - arduino-cli core update-index - arduino-cli core install ${{ matrix.arduino-platform }} - - - name: Compile Sketch - run: arduino-cli compile --fqbn ${{ matrix.fqbn }} ./mc_labs/mc_lab_01/*.ino diff --git a/.github/workflows/lab-validation.yml b/.github/workflows/lab-validation.yml new file mode 100644 index 0000000..07a90ae --- /dev/null +++ b/.github/workflows/lab-validation.yml @@ -0,0 +1,120 @@ +name: Cheking of compile Arduino sketch for AVR/AtMega + +on: + pull_request: + branches: [main, master] + +env: + platform: "arduino:avr" + fqbn_master: "arduino:avr:mega" + COMMIT_COUNT: $(( ${{ github.event_name == 'pull_request' && github.event.pull_request.commits || 0 }} + 1 )) + +jobs: + handle_bad_branch_name: + runs-on: ubuntu-22.04 + if: (contains(github.head_ref, 'mc_lab_1') || contains(github.head_ref, 'mc_lab_2') || contains(github.head_ref, 'mc_lab_3') || contains(github.head_ref, 'mc_lab_4') || contains(github.head_ref, 'mc_lab_5') || contains(github.head_ref, 'mc_lab_6') || contains(github.head_ref, 'mc_lab_7')) == false + steps: + - name: Fail the build + run: | + echo "The branch name is not correct. It should contain 'mc_lab_' prefix" + exit 1 + build_labs_1_to_4: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_1') || contains(github.head_ref, 'mc_lab_2') || contains(github.head_ref, 'mc_lab_3') || contains(github.head_ref, 'mc_lab_4') + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: Get changed files list + uses: ./.github/actions/get_changed_files + + - name: Set up Arduino CLI + uses: arduino/setup-arduino-cli@v2 + + - name: Install platform + run: | + arduino-cli core update-index + arduino-cli core install ${{ env.platform }} + + - name: Compile Sketch + run: arduino-cli compile --fqbn ${{ env.fqbn_master }} $(grep -E '\.ino$' changed_files.txt | xargs) + build_lab_5: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_5') + env: + fqbn_slave: "arduino:avr:nano" + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: Get changed files list + uses: ./.github/actions/get_changed_files + + - name: Get master folder + run: | + cat changed_files.txt | xargs dirname | grep 'master' | grep -m 1 -vE '/(.*master.*|.*slave.*)/' > master_project.txt + echo "Master project:" + cat master_project.txt + + - name: Get slave folders + run: | + cat changed_files.txt | xargs dirname | grep 'slave' | grep -vE '/(.*master.*|.*slave.*)/' > slave_projects.txt + echo "Slave projects:" + cat slave_projects.txt + + - name: Check if there is at least one master and one slave project + run: | + if [ ! -s master_project.txt ] || [ ! -s slave_projects.txt ]; then + echo "There is no master or slave project" + exit 1 + fi + + - name: Set up Arduino CLI + uses: arduino/setup-arduino-cli@v2 + + - name: Install platform + run: | + arduino-cli core update-index + arduino-cli core install ${{ env.platform }} + + - name: Compile master + run: while read master_folder; do arduino-cli compile --fqbn ${{ env.fqbn_master }} $master_folder/*.ino; done < master_project.txt + + - name: Compile slaves + run: while read slave_folder; do arduino-cli compile --fqbn ${{ env.fqbn_slave }} $slave_folder/*.ino; done < slave_projects.txt + build_lab_6: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_6') + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: It just passes + run: echo "It just passes. It's too complex" + build_lab_7: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_7') + env: + register-bindings: "m2560def.inc" + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: Get changed files list + uses: ./.github/actions/get_changed_files + + - name: Setup AVRA Assembler + run: sudo apt-get install avra + - name: Preprocess sketch - append register bindings to the top of the file + run: printf ".include \"{{ env.register-bindings }}\"\n\n" | cat - $(grep -m 1 -E '\.(asm|S)$' changed_files.txt | xargs) > pipeline_main_assembly_source_file.asm + + - name: Compile Sketch + run: avra pipeline_main_assembly_source_file.asm diff --git a/mc_labs/mc_lab_05/lab5-ptu/lab5.pdsprj b/mc_labs/mc_lab_05/lab5-ptu/lab5.pdsprj new file mode 100644 index 0000000..ddd6e57 Binary files /dev/null and b/mc_labs/mc_lab_05/lab5-ptu/lab5.pdsprj differ diff --git a/mc_labs/mc_lab_05/lab5-ptu/sketch_master/sketch_master.ino b/mc_labs/mc_lab_05/lab5-ptu/sketch_master/sketch_master.ino new file mode 100644 index 0000000..c0560ed --- /dev/null +++ b/mc_labs/mc_lab_05/lab5-ptu/sketch_master/sketch_master.ino @@ -0,0 +1,56 @@ +bool isAddress = true; +bool isCommand = false; +byte command; + +void setWriteModeRS485() { + byte port = PORTD; + PORTD |= 1 << PD1; + delay(1); +} + +ISR(USART1_TX_vect) +{ + PORTD &= ~(1 << PD1); +} + +void setup() { + delay(200); + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, HIGH); + + DDRD |= 1 << PD1; + PORTD &= ~(1 << PD1); + + Serial.begin(9600); + Serial1.begin(9600, SERIAL_8N1); + UCSR1B |= (1 << UCSZ12) | (1 << TXCIE1); +} + +void loop() { + if (Serial.available()) { + byte inByte = Serial.read(); + if (isAddress) { + setWriteModeRS485(); + UCSR1B |= 1 << TXB81; + Serial1.write(inByte); + isAddress = false; + isCommand = true; + } else if (isCommand) { + command = inByte; + isCommand = false; + setWriteModeRS485(); + UCSR1B &= ~(1 << TXB81); + Serial1.write(inByte); + if (command == 0xB1) isAddress = true; + } else { + isAddress = true; + setWriteModeRS485(); + UCSR1B &= ~(1 << TXB81); + Serial1.write(inByte); + } + } + if (Serial1.available()) { + byte inByte1 = Serial1.read(); + Serial.write(inByte1); + } +} diff --git a/mc_labs/mc_lab_05/lab5-ptu/sketch_slave1/sketch_slave1.ino b/mc_labs/mc_lab_05/lab5-ptu/sketch_slave1/sketch_slave1.ino new file mode 100644 index 0000000..d898304 --- /dev/null +++ b/mc_labs/mc_lab_05/lab5-ptu/sketch_slave1/sketch_slave1.ino @@ -0,0 +1,90 @@ +#define B 1 +#define U 2 +#define C 3 +#define H 4 +#define A 5 +#define K 6 +#define R 7 +#define O 8 +#define M 9 +#define N 10 +#define T 11 +#define S 12 +#define V 13 +#define Y 14 +#define _ 15 + +const byte SLAVE_ADDRESS = 55; +const byte DATA_ARRAY_LENGTH = 24; + +byte data_array[DATA_ARRAY_LENGTH] = { + B, U, C, H, A, K, _, + R, O, M, A, N, _, + T, A, R, A, S, O, V, Y, C, H, + _ +}; + +void setWriteModeRS485() { + PORTD |= 1 << PD2; + delay(1); +} + +ISR(USART_TX_vect) { + PORTD &= ~(1 << PD2); +} + +int writeDataToMaster() { + unsigned short checkSumCRC = get_crc8(data_array, DATA_ARRAY_LENGTH - 1); + data_array[DATA_ARRAY_LENGTH - 1] = checkSumCRC; + + for(int k = 0; k < 5; k++){ + for (int i = 0; i < DATA_ARRAY_LENGTH; i++) { + byte byteToSend = data_array[i]; + if (k == 1 && i == DATA_ARRAY_LENGTH - 2) { + byteToSend ^= (1 << 0); + } else if (k == 4 && i == 3) { + byteToSend ^= (1 << 0) | (1 << 2) | (1 << 5); + } + Serial.write(byteToSend); + } + } +} + +void setup() { + delay(200); + DDRD = 0b00000111; + PORTD = 0b11111000; + Serial.begin(9600, SERIAL_8N1); + UCSR0B |= (1 << UCSZ02) | (1 << TXCIE0); + UCSR0A |= (1 << MPCM0); + delay(1); +} + +void loop() { + if (Serial.available()) { + byte inByte = Serial.read(); + if (SLAVE_ADDRESS == inByte) { + UCSR0A &= ~(1 << MPCM0); + setWriteModeRS485(); + writeDataToMaster(); + delay(100); + } + } +} + +uint8_t get_crc8(const uint8_t *data, size_t array_length) { + const uint8_t poly = 0x7; + uint8_t crc = 0x00; + + for (size_t i = 0; i < array_length; i++) { + crc ^= data[i]; + for (int j = 0; j < 8; j++) { + if (crc & 0x80) { + crc = (crc << 1) ^ poly; + } else { + crc <<= 1; + } + } + } + return crc ^ 0x00; +} diff --git a/mc_labs/mc_lab_05/lab5-ptu/sketch_slave2/sketch_slave2.ino b/mc_labs/mc_lab_05/lab5-ptu/sketch_slave2/sketch_slave2.ino new file mode 100644 index 0000000..6266b8e --- /dev/null +++ b/mc_labs/mc_lab_05/lab5-ptu/sketch_slave2/sketch_slave2.ino @@ -0,0 +1,77 @@ +#define ZERO 16 +#define ONE 17 +#define TWO 18 +#define THREE 19 +#define SIX 20 +#define DOT 21 +#define _ 15 + +const byte SLAVE_ADDRESS = 65; +const byte DATA_ARRAY_LENGTH = 11; +byte data_array[DATA_ARRAY_LENGTH] = { TWO, SIX, DOT, ONE, ONE, DOT, TWO, ZERO, ZERO, THREE, _ }; + +void setWriteModeRS485() { + PORTD |= 1 << PD2; + delay(1); +} + +ISR(USART_TX_vect) +{ + PORTD &= ~(1 << PD2); +} + +void writeDataToMaster() { + unsigned short checkSumCRC = get_crc8(data_array, DATA_ARRAY_LENGTH - 1); + data_array[DATA_ARRAY_LENGTH - 1] = checkSumCRC; + + for(int k = 0; k < 5; k++) { + for (int i = 0; i < DATA_ARRAY_LENGTH; i++) { + byte byteToSend = data_array[i]; + if (k == 2 && i == 0) { + byteToSend ^= (1 << 3); + } else if (k == 3 && i == 1) { + byteToSend ^= ((1 << 3) | (1 << 7)) ; + } + Serial.write(byteToSend); + } + } +} + +void setup() { + delay(200); + DDRD = 0b00000111; + PORTD = 0b11111000; + Serial.begin(9600, SERIAL_8N1); + UCSR0B |= (1 << UCSZ02) | (1 << TXCIE0); + UCSR0A |= (1 << MPCM0); + delay(1); +} + +void loop() { + if (Serial.available()) { + byte inByte = Serial.read(); + if (SLAVE_ADDRESS == inByte) { + UCSR0A &= ~(1 << MPCM0); + setWriteModeRS485(); + writeDataToMaster(); + delay(100); + } + } +} + +uint8_t get_crc8(const uint8_t *data, size_t array_length) { + const uint8_t poly = 0x7; + uint8_t crc = 0x00; + + for (size_t i = 0; i < array_length; i++) { + crc ^= data[i]; + for (int j = 0; j < 8; j++) { + if (crc & 0x80) { + crc = (crc << 1) ^ poly; + } else { + crc <<= 1; + } + } + } + return crc ^ 0x00; +}