#include #include #include #include #include #include #define PCA9555_ADDR (0x40) #define PCA9555_REG_IN_PORT0 (0x0) #define PCA9555_REG_IN_PORT1 (0x1) #define PCA9555_REG_OUT_PORT0 (0x2) #define PCA9555_REG_OUT_PORT1 (0x3) #define PCA9555_REG_POLARITY0 (0x4) #define PCA9555_REG_POLARITY1 (0x5) #define PCA9555_REG_CONFIG0 (0x6) #define PCA9555_REG_CONFIG1 (0x7) static uint8_t dmx_universe[129]; static void setup_pins(void) { uint32_t tmp; RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; tmp = GPIOA->MODER; /* Reset all used port pins */ tmp &= MODER_DELETE(0) & MODER_DELETE(1) & MODER_DELETE(2) & MODER_DELETE(3) & \ MODER_DELETE(4) & MODER_DELETE(5) & MODER_DELETE(6) & MODER_DELETE(7) & \ MODER_DELETE(8) & MODER_DELETE(9) & MODER_DELETE(10); /* Analog ports for POTIs */ tmp |= ANALOG(0) | ANALOG(1) | ANALOG(4); /* Setup USART1 RX/TX (AF1) */ tmp |= ALTFUNC(2) | ALTFUNC(3); SETAF(GPIOA, 2, 1); SETAF(GPIOA, 3, 1); /* LED enables for Potentiometers */ tmp |= OUTPUT(5) | OUTPUT(6) | OUTPUT(7); GPIOA->ODR &= ~((1 << 5) | (1 << 6) | (1 << 7)); /* Setup I2C pins (AF4) */ tmp |= ALTFUNC(9) | ALTFUNC(10); SETAF(GPIOA, 9, 4); SETAF(GPIOA, 10, 4); /* Set I2C Pins to open drain. Others are push pull */ GPIOA->OTYPER = OTYP_OPENDRAIN(9) | OTYP_OPENDRAIN(10); /* Write changes to GPIOA mode register */ GPIOA->MODER = tmp; /* PB1 Input with pullup */ GPIOB->MODER &= MODER_DELETE(1); GPIOB->PUPDR = PULLUP(1); } static void zero_dmx_universe(void) { int i; for (i = 0; i < sizeof(dmx_universe); i++) { dmx_universe[i] = 0; } } int main(void) { uint8_t i2c_command[2]; uint8_t port; uint32_t odr; setup_pins(); i2c_init(); dmx_init(dmx_universe, sizeof(dmx_universe), GPIOA, 2u, 2300u, 10u, 5u); /* Setup Systick for 1ms ticks */ SysTick_Config(48000000UL/1000); GPIOA->ODR |= (1<<5) | (1<<7); /* Setup PCA */ i2c_command[0] = PCA9555_REG_CONFIG1; i2c_command[1] = 0x00; i2c_write(PCA9555_ADDR, i2c_command, 2u); i2c_command[0] = PCA9555_REG_CONFIG0; i2c_command[1] = 0xFF; i2c_write(PCA9555_ADDR, i2c_command, 2u); dmx_stream_start(); dmx_universe[0] = 0x55; dmx_universe[1] = 0x50; /* Blink the LEDs */ while (1) { systick_wait_ms(3); i2c_read(PCA9555_ADDR, PCA9555_REG_IN_PORT0, &port, 1u); i2c_command[0] = PCA9555_REG_OUT_PORT1; i2c_command[1] = ~port; odr = GPIOA->ODR; odr &= ~((1<<5) | (1<<6) | (1<<7)); if (port & (1<<0)) { zero_dmx_universe(); for (int i = 0; i < 32; i++) { dmx_universe[i*4] = 0xFF; } } if (port & (1<<1)) { zero_dmx_universe(); for (int i = 0; i < 32; i++) { dmx_universe[i*4+1] = 0xFF; } } if (port & (1<<2)) { zero_dmx_universe(); for (int i = 0; i < 32; i++) { dmx_universe[i*4+2] = 0xFF; } } GPIOA->ODR = odr; i2c_write(PCA9555_ADDR, i2c_command, 2u); } }