|
| 1 | + |
| 2 | +#include <stdint.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include "ch32fun.h" |
| 5 | +#include "pins.h" |
| 6 | +#include "hardware.h" |
| 7 | +#include "buttons.h" |
| 8 | +#include "leds.h" |
| 9 | +#include "globals.h" |
| 10 | +#include "pins.h" |
| 11 | + |
| 12 | +// Pin port/mask arrays/structs (define here) |
| 13 | +const PinDef COL[5] = { |
| 14 | + {GPIOC, 0x01}, {GPIOC, 0x02}, {GPIOC, 0x04}, {GPIOC, 0x08}, {GPIOC, 0x10} |
| 15 | +}; |
| 16 | +const PinDef ROW_RED = { GPIOA, 0x02 }; |
| 17 | +const PinDef ROW_GREEN = { GPIOA, 0x04 }; |
| 18 | +const PinDef BTN[4] = { |
| 19 | + {GPIOD, 0x04}, {GPIOD, 0x08}, {GPIOD, 0x10}, {GPIOD, 0x20} |
| 20 | +}; |
| 21 | + |
| 22 | +// Global variables (define here) |
| 23 | +volatile uint32_t msTicks = 0; |
| 24 | +volatile bool flashState = false; |
| 25 | +volatile bool rapidFlashState = false; |
| 26 | +volatile bool runMode = false; |
| 27 | +volatile uint8_t currentCol = 0; |
| 28 | + |
| 29 | +static const int statusLEDs[] = { PIN_PWR, PIN_INIT, PIN_RDY, PIN_RUN, PIN_IDLE }; |
| 30 | +static const int testCols[] = { PIN_COL_A, PIN_COL_B, PIN_COL_C, PIN_COL_D, PIN_COL_E }; |
| 31 | +static const int inputPins[] = { PIN_INPUT_A, PIN_INPUT_B, PIN_INPUT_C, PIN_INPUT_D }; |
| 32 | + |
| 33 | +// Timer ISR for 1ms tick, button polling, and LED scan |
| 34 | +void TIM1_UP_IRQHandler(void) INTERRUPT_DECORATOR; |
| 35 | +void TIM1_UP_IRQHandler(void) |
| 36 | +{ |
| 37 | + if (TIM1->INTFR & TIM_UIF) { |
| 38 | + TIM1->INTFR &= ~TIM_UIF; |
| 39 | + msTicks++; |
| 40 | + pollButtons(); |
| 41 | + static uint32_t nextFlashAt = 250; |
| 42 | + if (msTicks >= nextFlashAt) { |
| 43 | + nextFlashAt += 250; |
| 44 | + flashState = !flashState; |
| 45 | + } |
| 46 | + static uint32_t nextRapidFlashAt = 125; |
| 47 | + if (msTicks >= nextRapidFlashAt) { |
| 48 | + nextRapidFlashAt += 125; |
| 49 | + rapidFlashState = !rapidFlashState; |
| 50 | + } |
| 51 | + scanStep(); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void setupPins(void) { |
| 56 | + funGpioInitAll(); |
| 57 | + RCC->APB2PCENR |= RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD; |
| 58 | + for (int i = 0; i < 5; i++) { |
| 59 | + funPinMode(statusLEDs[i], GPIO_Speed_10MHz | GPIO_CNF_OUT_PP); |
| 60 | + funDigitalWrite(statusLEDs[i], FUN_HIGH); |
| 61 | + } |
| 62 | + for (int i = 0; i < 5; i++) { |
| 63 | + funPinMode(testCols[i], GPIO_Speed_10MHz | GPIO_CNF_OUT_PP); |
| 64 | + funDigitalWrite(testCols[i], FUN_LOW); |
| 65 | + } |
| 66 | + funPinMode(PIN_ROW_R, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP); |
| 67 | + funPinMode(PIN_ROW_G, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP); |
| 68 | + funDigitalWrite(PIN_ROW_R, FUN_HIGH); |
| 69 | + funDigitalWrite(PIN_ROW_G, FUN_HIGH); |
| 70 | + for (int i = 0; i < 4; i++) { |
| 71 | + funPinMode(inputPins[i], GPIO_CNF_IN_PUPD); |
| 72 | + funDigitalWrite(inputPins[i], FUN_HIGH); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +void setupStatusFlasher(void) { |
| 77 | + RCC->APB2PCENR |= RCC_APB2Periph_TIM1; |
| 78 | + RCC->APB2PRSTR |= RCC_APB2Periph_TIM1; |
| 79 | + RCC->APB2PRSTR &= ~RCC_APB2Periph_TIM1; |
| 80 | + TIM1->PSC = 48 - 1; |
| 81 | + TIM1->ATRLR = 1000 - 1; |
| 82 | + TIM1->SWEVGR |= TIM_UG; |
| 83 | + TIM1->INTFR &= ~TIM_UIF; |
| 84 | + TIM1->DMAINTENR |= TIM_UIE; |
| 85 | + NVIC_EnableIRQ(TIM1_UP_IRQn); |
| 86 | + TIM1->CTLR1 |= TIM_CEN; |
| 87 | +} |
| 88 | + |
| 89 | +void runStatus(bool isRun) { |
| 90 | + runMode = isRun; |
| 91 | + funDigitalWrite(PIN_RUN, FUN_HIGH); |
| 92 | + funDigitalWrite(PIN_IDLE, FUN_HIGH); |
| 93 | +} |
| 94 | + |
| 95 | +void waitTicks(uint32_t ticks) { |
| 96 | + uint32_t start = msTicks; |
| 97 | + while ((uint32_t)(msTicks - start) < ticks) { |
| 98 | + __WFI(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void scanStep(void) { |
| 103 | + static int8_t prev = -1; |
| 104 | + if (activeStates == NULL) { |
| 105 | + gpio_set(ROW_RED.port, ROW_RED.mask); |
| 106 | + gpio_set(ROW_GREEN.port, ROW_GREEN.mask); |
| 107 | + return; |
| 108 | + } |
| 109 | + gpio_set(ROW_RED.port, ROW_RED.mask); |
| 110 | + gpio_set(ROW_GREEN.port, ROW_GREEN.mask); |
| 111 | + if (prev >= 0 && prev < 5) { |
| 112 | + if (COL[prev].port != NULL) { |
| 113 | + gpio_clear(COL[prev].port, COL[prev].mask); |
| 114 | + } |
| 115 | + } |
| 116 | + if (currentCol >= 0 && currentCol < 5) { |
| 117 | + TestCaseState state = activeStates[currentCol]; |
| 118 | + switch (state) { |
| 119 | + case TC_PASS: |
| 120 | + gpio_clear(ROW_GREEN.port, ROW_GREEN.mask); |
| 121 | + break; |
| 122 | + case TC_FAIL: |
| 123 | + gpio_clear(ROW_RED.port, ROW_RED.mask); |
| 124 | + break; |
| 125 | + case TC_IN_PROGRESS: |
| 126 | + if (flashState) |
| 127 | + gpio_clear(ROW_RED.port, ROW_RED.mask); |
| 128 | + else |
| 129 | + gpio_clear(ROW_GREEN.port, ROW_GREEN.mask); |
| 130 | + break; |
| 131 | + case TC_WARNING: |
| 132 | + if (flashState) |
| 133 | + gpio_clear(ROW_RED.port, ROW_RED.mask); |
| 134 | + break; |
| 135 | + case TC_RETRY: |
| 136 | + if (rapidFlashState) |
| 137 | + gpio_clear(ROW_RED.port, ROW_RED.mask); |
| 138 | + break; |
| 139 | + case TC_NO_RESULT: |
| 140 | + default: |
| 141 | + break; |
| 142 | + } |
| 143 | + if (COL[currentCol].port != NULL) { |
| 144 | + gpio_set(COL[currentCol].port, COL[currentCol].mask); |
| 145 | + } |
| 146 | + } |
| 147 | + prev = currentCol; |
| 148 | + currentCol = (currentCol + 1) % 5; |
| 149 | +} |
0 commit comments