/* * main.c — binary-to-BCD demo for the MCU module (Rev C, 16U) target * STM32F303VET6 (U200): reads one bit from each left connector L1..L4 and shows * the 4-bit number on the LED matrix in binary and as two BCD 7-segment digits. * * For the cloud-web code playground, FIRMWARE project type (put it at * src/main.c). Same constraints as mcu_board_test.c: no HAL, no CMSIS, no C * library (-ffreestanding -fno-builtin -nostdlib), FPU left off, so registers * are touched directly and memset/memcpy are provided here. * * Wiring (Rev C, see fw-mcu-g0b1/docs/eco/REVX_TO_REVC_MCU_HANDOFF.md, T01): * * Connector S+ (pad 3) S- (pad 4) series R * L1 PC10 (78) PC11 (79) direct * L2 PA7 (32) PE10 (41) R5 / R37 1k * L3 PC5 (34) PC4 (33) R32 / R33 1k * L4 PF9 (10) PF10 (11) direct * * The bit is read on S+ (internal pull-down: an unplugged connector reads 0). * Set BIT_ON_S_MINUS to 1 to read S- instead. The console prints both. * L1 is the LSB (2^0), L4 the MSB (2^3); swap with L1_IS_MSB. * * IS31FL3741A (U400) I2C3 SCL PA8 (67) SDA PC9 (66) AF3, SDB PE0 (97), addr 0x30 * OBD console USART1 TX PB6 (92) AF7, 115200 8N1 * ("Connect MCU OBD - Serial" port) * RGB LED1 (D200) R PB0 G PB4 B PB5, active low * USER2 (S202) PE7, active low: cycle matrix brightness * * Matrix (39 x 9, L1 at the upper left): * * b3 b2 b1 b0 = tens units * 3x5 digits 5x9 7-segment digits (BCD) * bar under each 1 * * Console: one line per change, e.g. "L4..L1 = 1011 -> 11 BCD 0001 0001". * * Clock: stays on the 8 MHz HSI (nothing here needs the PLL). */ #include <stdint.h> /* ---------------------------------------------------------------- config -- */ #define BIT_ON_S_MINUS 0 /* 0 = read S+ (pad 3), 1 = read S- (pad 4) */ #define L1_IS_MSB 0 /* 0 = L1 is 2^0, 1 = L1 is 2^3 */ static const uint8_t k_gcc_steps[] = { 0x10, 0x30, 0x60 }; #define V_ON 0xFFu /* lit segment / bit */ #define V_DIM 0x0Cu /* unlit segment, leading zero, '0' bit bar */ /* ------------------------------------------------------------- registers -- */ #define REG(addr) (*(volatile uint32_t *)(addr)) #define RCC_BASE 0x40021000u #define RCC_AHBENR REG(RCC_BASE + 0x14) #define RCC_APB2ENR REG(RCC_BASE + 0x18) #define RCC_APB1ENR REG(RCC_BASE + 0x1C) #define RCC_CFGR3 REG(RCC_BASE + 0x30) #define GPIOA 0x48000000u #define GPIOB 0x48000400u #define GPIOC 0x48000800u #define GPIOE 0x48001000u #define GPIOF 0x48001400u #define GPIO_MODER(p) REG((p) + 0x00) #define GPIO_OTYPER(p) REG((p) + 0x04) #define GPIO_OSPEEDR(p) REG((p) + 0x08) #define GPIO_PUPDR(p) REG((p) + 0x0C) #define GPIO_IDR(p) REG((p) + 0x10) #define GPIO_BSRR(p) REG((p) + 0x18) #define GPIO_AFR(p, n) REG((p) + 0x20 + 4u * ((n) >> 3)) #define I2C3 0x40007800u #define I2C_CR1(i) REG((i) + 0x00) #define I2C_CR2(i) REG((i) + 0x04) #define I2C_TIMINGR(i) REG((i) + 0x10) #define I2C_ISR(i) REG((i) + 0x18) #define I2C_ICR(i) REG((i) + 0x1C) #define I2C_RXDR(i) REG((i) + 0x24) #define I2C_TXDR(i) REG((i) + 0x28) #define I2C_ISR_TXIS (1u << 1) #define I2C_ISR_RXNE (1u << 2) #define I2C_ISR_NACKF (1u << 4) #define I2C_ISR_STOPF (1u << 5) #define I2C_ISR_TC (1u << 6) #define I2C_ISR_BUSY (1u << 15) #define I2C_CR2_RD_WRN (1u << 10) #define I2C_CR2_START (1u << 13) #define I2C_CR2_STOP (1u << 14) #define I2C_CR2_AUTOEND (1u << 25) #define USART1 0x40013800u #define USART_CR1 REG(USART1 + 0x00) #define USART_BRR REG(USART1 + 0x0C) #define USART_ISR REG(USART1 + 0x1C) #define USART_TDR REG(USART1 + 0x28) #define SYST_CSR REG(0xE000E010u) #define SYST_RVR REG(0xE000E014u) #define SYST_CVR REG(0xE000E018u) #define HSI_HZ 8000000u /* ------------------------------------------------------- freestanding libc -- */ __attribute__((optimize("no-tree-loop-distribute-patterns"))) void *memset(void *d, int v, __SIZE_TYPE__ n) { uint8_t *p = d; while (n--) *p++ = (uint8_t)v; return d; } __attribute__((optimize("no-tree-loop-distribute-patterns"))) void *memcpy(void *d, const void *s, __SIZE_TYPE__ n) { uint8_t *p = d; const uint8_t *q = s; while (n--) *p++ = *q++; return d; } /* ------------------------------------------------------------------ time -- */ static volatile uint32_t s_ms; void SysTick_Handler(void) { s_ms++; } static uint32_t millis(void) { return s_ms; } static void delay_ms(uint32_t ms) { uint32_t t0 = s_ms; while ((s_ms - t0) < ms) { } } /* ------------------------------------------------------------------ GPIO -- */ #define MODE_IN 0u #define MODE_OUT 1u #define MODE_AF 2u #define PULL_NONE 0u #define PULL_UP 1u #define PULL_DOWN 2u static void pin_cfg(uint32_t port, uint32_t pin, uint32_t mode, int open_drain, uint32_t af, uint32_t pull) { GPIO_MODER(port) = (GPIO_MODER(port) & ~(3u << (2 * pin))) | (mode << (2 * pin)); GPIO_OTYPER(port) = (GPIO_OTYPER(port) & ~(1u << pin)) | ((open_drain ? 1u : 0u) << pin); GPIO_OSPEEDR(port) |= 3u << (2 * pin); GPIO_PUPDR(port) = (GPIO_PUPDR(port) & ~(3u << (2 * pin))) | (pull << (2 * pin)); GPIO_AFR(port, pin) = (GPIO_AFR(port, pin) & ~(0xFu << (4 * (pin & 7)))) | (af << (4 * (pin & 7))); } static void pin_write(uint32_t port, uint32_t pin, int high) { GPIO_BSRR(port) = high ? (1u << pin) : (1u << (pin + 16)); } static int pin_read(uint32_t port, uint32_t pin) { return (GPIO_IDR(port) >> pin) & 1u; } static void led1(int r, int g, int b) /* common anode: low = on */ { pin_write(GPIOB, 0, !r); pin_write(GPIOB, 4, !g); pin_write(GPIOB, 5, !b); } /* Left connectors, L1..L4: S+ then S-. */ typedef struct { uint32_t port; uint8_t pin; } gpio_t; static const gpio_t k_l_splus[4] = { {GPIOC, 10}, {GPIOA, 7}, {GPIOC, 5}, {GPIOF, 9} }; static const gpio_t k_l_sminus[4] = { {GPIOC, 11}, {GPIOE, 10}, {GPIOC, 4}, {GPIOF, 10} }; static void board_init(void) { SYST_RVR = HSI_HZ / 1000u - 1u; SYST_CVR = 0; SYST_CSR = 7u; /* processor clock, IRQ, enable */ RCC_AHBENR |= (1u << 17) | (1u << 18) | (1u << 19) | (1u << 21) | (1u << 22); /* GPIOA/B/C/E/F */ RCC_APB1ENR |= 1u << 30; /* I2C3 */ RCC_APB2ENR |= 1u << 14; /* USART1 */ RCC_CFGR3 = (RCC_CFGR3 & ~((1u << 6) | 3u)) | 3u; /* I2C3/USART1 on HSI */ led1(0, 0, 0); pin_cfg(GPIOB, 0, MODE_OUT, 0, 0, PULL_NONE); pin_cfg(GPIOB, 4, MODE_OUT, 0, 0, PULL_NONE); pin_cfg(GPIOB, 5, MODE_OUT, 0, 0, PULL_NONE); pin_write(GPIOE, 0, 0); pin_cfg(GPIOE, 0, MODE_OUT, 0, 0, PULL_NONE); /* matrix SDB, held in shutdown */ pin_cfg(GPIOE, 7, MODE_IN, 0, 0, PULL_NONE); /* USER2, external pull-up */ /* Connector inputs: pulled down so an open pad reads 0. */ for (int i = 0; i < 4; i++) { pin_cfg(k_l_splus[i].port, k_l_splus[i].pin, MODE_IN, 0, 0, PULL_DOWN); pin_cfg(k_l_sminus[i].port, k_l_sminus[i].pin, MODE_IN, 0, 0, PULL_DOWN); } /* I2C3: PA8 SCL, PC9 SDA (AF3), board pull-ups. */ pin_cfg(GPIOA, 8, MODE_AF, 1, 3, PULL_NONE); pin_cfg(GPIOC, 9, MODE_AF, 1, 3, PULL_NONE); pin_cfg(GPIOB, 6, MODE_AF, 0, 7, PULL_NONE); /* USART1 TX */ USART_CR1 = 0; USART_BRR = (HSI_HZ + 115200u / 2u) / 115200u; USART_CR1 = (1u << 3) | 1u; /* TE, UE */ } /* raw pad levels, bit i = Li (L1 = bit 0) */ static uint8_t read_pads(const gpio_t pads[4]) { uint8_t v = 0; for (int i = 0; i < 4; i++) if (pin_read(pads[i].port, pads[i].pin)) v |= (uint8_t)(1u << i); return v; } static uint8_t to_value(uint8_t pads) { #if L1_IS_MSB return (uint8_t)(((pads & 1u) << 3) | ((pads & 2u) << 1) | ((pads & 4u) >> 1) | ((pads & 8u) >> 3)); #else return pads; #endif } /* --------------------------------------------------------------- console -- */ static void putch(char c) { while (!(USART_ISR & (1u << 7))) { } /* TXE */ USART_TDR = (uint8_t)c; } static void puts_(const char *s) { while (*s) { if (*s == '\n') putch('\r'); putch(*s++); } } static void put_bits4(uint8_t v) /* MSB first */ { for (int b = 3; b >= 0; b--) putch((v >> b) & 1u ? '1' : '0'); } static void put_hex8(uint8_t v) { static const char h[] = "0123456789ABCDEF"; putch(h[v >> 4]); putch(h[v & 15]); } /* ------------------------------------------------------------------- I2C -- */ #define I2C_TIMING 0x00310309u /* 400 kHz from 8 MHz HSI */ #define I2C_TMO_MS 20u static void i2c_init(uint32_t i2c) { I2C_CR1(i2c) = 0; I2C_TIMINGR(i2c) = I2C_TIMING; I2C_CR1(i2c) = 1u; } static int i2c_wait(uint32_t i2c, uint32_t mask) /* 0 ok, -1 NACK, -2 timeout */ { uint32_t t0 = millis(); for (;;) { uint32_t isr = I2C_ISR(i2c); if (isr & I2C_ISR_NACKF) return -1; if (isr & mask) return 0; if ((millis() - t0) > I2C_TMO_MS) return -2; } } static int i2c_finish(uint32_t i2c, int err) { if (err == -1 && !(I2C_CR2(i2c) & I2C_CR2_AUTOEND)) I2C_CR2(i2c) |= I2C_CR2_STOP; if (err != -2 && i2c_wait(i2c, I2C_ISR_STOPF) == -2) err = -2; I2C_ICR(i2c) = I2C_ISR_STOPF | I2C_ISR_NACKF; if (err == -2) { I2C_CR1(i2c) = 0; (void)I2C_CR1(i2c); (void)I2C_CR1(i2c); I2C_CR1(i2c) = 1u; } return err; } static int i2c_write(uint32_t i2c, uint8_t addr, uint8_t reg, const uint8_t *data, uint32_t n) { if (I2C_ISR(i2c) & I2C_ISR_BUSY) i2c_finish(i2c, -2); I2C_CR2(i2c) = ((uint32_t)addr << 1) | ((n + 1u) << 16) | I2C_CR2_AUTOEND | I2C_CR2_START; int err = i2c_wait(i2c, I2C_ISR_TXIS); if (!err) I2C_TXDR(i2c) = reg; for (uint32_t k = 0; !err && k < n; k++) { err = i2c_wait(i2c, I2C_ISR_TXIS); if (!err) I2C_TXDR(i2c) = data[k]; } return i2c_finish(i2c, err); } static int i2c_read(uint32_t i2c, uint8_t addr, uint8_t reg, uint8_t *data, uint32_t n) { if (I2C_ISR(i2c) & I2C_ISR_BUSY) i2c_finish(i2c, -2); I2C_CR2(i2c) = ((uint32_t)addr << 1) | (1u << 16) | I2C_CR2_START; int err = i2c_wait(i2c, I2C_ISR_TXIS); if (!err) { I2C_TXDR(i2c) = reg; err = i2c_wait(i2c, I2C_ISR_TC); } if (!err) { I2C_CR2(i2c) = ((uint32_t)addr << 1) | I2C_CR2_RD_WRN | (n << 16) | I2C_CR2_AUTOEND | I2C_CR2_START; for (uint32_t k = 0; !err && k < n; k++) { err = i2c_wait(i2c, I2C_ISR_RXNE); if (!err) data[k] = (uint8_t)I2C_RXDR(i2c); } } return i2c_finish(i2c, err); } static int i2c_wr1(uint32_t i2c, uint8_t addr, uint8_t reg, uint8_t v) { return i2c_write(i2c, addr, reg, &v, 1); } /* ------------------------------------------------------------ IS31FL3741A -- */ #define IS31_ADDR 0x30u #define MATRIX_COLS 39 #define MATRIX_ROWS 9 static uint8_t s_pwm[MATRIX_ROWS][MATRIX_COLS]; /* [row][col], row 0 = top */ static uint8_t s_page0[180], s_page1[171]; static uint8_t s_gcc_idx = 1; static int is31_page(uint8_t page) { int err = i2c_wr1(I2C3, IS31_ADDR, 0xFE, 0xC5); return err ? err : i2c_wr1(I2C3, IS31_ADDR, 0xFD, page); } static int is31_fill(uint8_t page, uint8_t v, uint32_t len) { for (uint32_t k = 0; k < len; k++) s_page0[k] = v; int err = is31_page(page); return err ? err : i2c_write(I2C3, IS31_ADDR, 0x00, s_page0, len); } static int is31_init(void) { pin_write(GPIOE, 0, 1); /* leave hardware shutdown */ delay_ms(2); if (is31_page(4)) return -1; i2c_wr1(I2C3, IS31_ADDR, 0x3F, 0xAE); /* reset */ delay_ms(5); if (is31_page(4)) return -1; i2c_wr1(I2C3, IS31_ADDR, 0x00, 0x01); /* normal operation, SW1..SW9 */ i2c_wr1(I2C3, IS31_ADDR, 0x01, k_gcc_steps[s_gcc_idx]); if (is31_fill(2, 0xFF, 180) || is31_fill(3, 0xFF, 171)) return -1; /* scaling */ if (is31_fill(0, 0x00, 180) || is31_fill(1, 0x00, 171)) return -1; /* PWM */ return 0; } static void is31_set_gcc(uint8_t v) { if (!is31_page(4)) i2c_wr1(I2C3, IS31_ADDR, 0x01, v); } static int matrix_flush(void) { for (int r = 0; r < MATRIX_ROWS; r++) { int sw = MATRIX_ROWS - 1 - r; /* row 0 = SW9 */ for (int c = 0; c < MATRIX_COLS; c++) { uint8_t v = s_pwm[r][c]; if (c < 30) { if (sw < 6) s_page0[sw * 30 + c] = v; else s_page1[(sw - 6) * 30 + c] = v; } else { s_page1[0x5A + sw * 9 + (c - 30)] = v; } } } int err = is31_page(0); if (!err) err = i2c_write(I2C3, IS31_ADDR, 0, s_page0, sizeof s_page0); if (!err) err = is31_page(1); if (!err) err = i2c_write(I2C3, IS31_ADDR, 0, s_page1, sizeof s_page1); return err; } static void matrix_clear(void) { for (int r = 0; r < MATRIX_ROWS; r++) for (int c = 0; c < MATRIX_COLS; c++) s_pwm[r][c] = 0; } static void px(int c, int r, uint8_t v) { if (c < 0 || r < 0 || c >= MATRIX_COLS || r >= MATRIX_ROWS) return; if (v > s_pwm[r][c]) s_pwm[r][c] = v; } /* --------------------------------------------------------------- drawing -- */ /* 3x5 font for the binary digits: one byte per row, bit 2 = left column. */ static const uint8_t k_bit_glyph[2][5] = { {7,5,5,5,7}, {2,6,2,2,7} }; static void draw_glyph3(int x, int y, const uint8_t g[5], uint8_t v) { for (int r = 0; r < 5; r++) for (int c = 0; c < 3; c++) if (g[r] & (4 >> c)) px(x + c, y + r, v); } /* 7-segment masks, bit 0..6 = a..g */ static const uint8_t k_seg[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F }; /* 5 wide x 9 tall 7-segment digit, top-left at (x, 0). Unlit segments are drawn * at `off` so the digit reads as a real 7-segment display. */ static void draw_seg5x9(int x, uint8_t mask, uint8_t on, uint8_t off) { for (int s = 0; s < 7; s++) { uint8_t v = (mask >> s) & 1u ? on : off; if (!v) continue; switch (s) { case 0: for (int c = 1; c <= 3; c++) px(x + c, 0, v); break; /* a */ case 1: for (int r = 1; r <= 3; r++) px(x + 4, r, v); break; /* b */ case 2: for (int r = 5; r <= 7; r++) px(x + 4, r, v); break; /* c */ case 3: for (int c = 1; c <= 3; c++) px(x + c, 8, v); break; /* d */ case 4: for (int r = 5; r <= 7; r++) px(x, r, v); break; /* e */ case 5: for (int r = 1; r <= 3; r++) px(x, r, v); break; /* f */ case 6: for (int c = 1; c <= 3; c++) px(x + c, 4, v); break; /* g */ } } } /* cols 0..14 b3 b2 b1 b0 (3x5 at rows 1..5, bar at row 7 under each 1) * cols 18..20 '=' * cols 27..31 tens, 34..38 units (BCD, 7-segment) */ static void draw_value(uint8_t value) { for (int i = 0; i < 4; i++) { int bit = (value >> (3 - i)) & 1; int x = i * 4; draw_glyph3(x, 1, k_bit_glyph[bit], bit ? V_ON : V_DIM * 3u); for (int c = 0; c < 3; c++) px(x + c, 7, bit ? V_ON : V_DIM); } for (int c = 18; c <= 20; c++) { px(c, 3, V_ON); px(c, 5, V_ON); } uint8_t tens = value / 10u, units = value % 10u; draw_seg5x9(27, k_seg[tens], tens ? V_ON : V_DIM * 3u, V_DIM); /* leading 0 dimmed */ draw_seg5x9(34, k_seg[units], V_ON, V_DIM); } /* --------------------------------------------------------------------- main -- */ int main(void) { board_init(); i2c_init(I2C3); delay_ms(20); puts_("\n=== MCU module (F303 target): binary -> BCD on L1..L4 ===\n"); puts_(BIT_ON_S_MINUS ? "reading S- (pad 4)" : "reading S+ (pad 3)"); puts_(L1_IS_MSB ? ", L1 = MSB\n" : ", L1 = LSB\n"); int mat_ok = is31_init() == 0; if (mat_ok) { uint8_t id = 0; i2c_read(I2C3, IS31_ADDR, 0xFC, &id, 1); puts_("IS31FL3741A: ok, ID=0x"); put_hex8(id); puts_("\n"); } else { puts_("IS31FL3741A: no ACK at 0x30 (check PE0/SDB, PA8/PC9, 5V_Module)\n"); } led1(!mat_ok, mat_ok, 0); uint8_t prev_value = 0xFF, prev_raw = 0xFF, prev_btn = 0; for (;;) { uint8_t splus = read_pads(k_l_splus); uint8_t sminus = read_pads(k_l_sminus); uint8_t value = to_value(BIT_ON_S_MINUS ? sminus : splus); uint8_t raw = (uint8_t)((sminus << 4) | splus); uint8_t btn = !pin_read(GPIOE, 7); if (btn && !prev_btn && mat_ok) { s_gcc_idx = (uint8_t)((s_gcc_idx + 1u) % sizeof k_gcc_steps); is31_set_gcc(k_gcc_steps[s_gcc_idx]); puts_("brightness GCC=0x"); put_hex8(k_gcc_steps[s_gcc_idx]); puts_("\n"); } prev_btn = btn; if (raw != prev_raw) { prev_raw = raw; puts_("L4..L1 = "); put_bits4(value); puts_(" -> "); if (value < 10) putch(' '); if (value >= 10) putch('1'); putch((char)('0' + value % 10u)); puts_(" BCD "); put_bits4(value / 10u); putch(' '); put_bits4(value % 10u); puts_(" (pads S+="); put_bits4(splus); puts_(" S-="); put_bits4(sminus); puts_(")\n"); } if (mat_ok && value != prev_value) { prev_value = value; matrix_clear(); draw_value(value); if (matrix_flush()) { led1(1, 0, 0); prev_value = 0xFF; } /* retry next pass */ } delay_ms(10); } }