#include "main.h" /* private defines */ #define DISPLAY_COLUMNS 24 /* private macros */ #define HT1632C_CS_ON GPIOA->BRR = (1<<15) #define HT1632C_CS_OFF GPIOA->BSRR = (1<<15) #define HT1632C_WR_LOW GPIOB->BRR = (1<<3) #define HT1632C_WR_HGH GPIOB->BSRR = (1<<3) #define HT1632C_DATA_0 GPIOB->BRR = (1<<5) #define HT1632C_DATA_1 GPIOB->BSRR = (1<<5) /* private variables */ uint8_t display_Buffer[DISPLAY_COLUMNS] = {0}; /* private typedef */ /* private functions */ static void GPIO_Init(void); static void I2C1_Init(void); static void SPI1_Init(void); static void GPIO_SPI_SW(void); static void GPIO_SPI_HW(void); //static void TIM1_Init(void); //static void TIM3_Init(void); //static void TIM14_Init(void); //static void TIM16_Init(void); //static void TIM17_Init(void); //static void USART1_UART_Init(void); static void _display_WriteBits(uint16_t data, uint16_t nbits); static void _delay_c(uint32_t cycle); /* Board perephireal Configuration */ void Board_Init(void) { /* At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f072xb.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Main peripheral clock enable */ RCC->AHBENR |= (RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | RCC_AHBENR_DMAEN); RCC->APB1ENR = (RCC_APB1ENR_PWREN | RCC_APB1ENR_I2C1EN); // | RCC_APB1ENR_TIM14EN| RCC_APB1ENR_TIM3EN); RCC->APB2ENR = (RCC_APB2ENR_SYSCFGEN | RCC_APB2ENR_SPI1EN); // | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_TIM16EN | RCC_APB2ENR_TIM17EN); /* Peripheral interrupt init*/ /* RCC_IRQn interrupt configuration */ NVIC_SetPriority(RCC_IRQn, 0); NVIC_EnableIRQ(RCC_IRQn); /* Initialize all configured peripherals */ HT1632C_CS_OFF; HT1632C_WR_HGH; HT1632C_DATA_1; GPIO_Init(); /* DMA interrupt init */ /* DMA1_Channel2_3_IRQn interrupt configuration */ NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0); NVIC_EnableIRQ(DMA1_Channel2_3_IRQn); I2C1_Init(); SPI1_Init(); /** Star SPI transfer to shift registers */ /* DMA Destination addr: SPI1 data register. */ DMA1_Channel3->CPAR = (uint32_t)&(SPI1->DR); /* Enable SPI transfer */ SPI1->CR1 |= SPI_CR1_SPE; // Flag.SPI_TX_End = 1; // TIM1_Init(); // TIM3_Init(); // TIM14_Init(); // TIM16_Init(); // TIM17_Init(); // USART1_UART_Init(); display_Init(); } /* output 'L', 'G', '5' */ void display_test(void) { // prepare buffer // 1 display_Buffer[0] = 0x01; display_Buffer[1] = 0x40; display_Buffer[2] = 0x7e; display_Buffer[3] = 0x40; display_Buffer[4] = 0x01; // 2 display_Buffer[5] = 0x00; display_Buffer[6] = 0x01; display_Buffer[7] = 0x7c; display_Buffer[8] = 0x41; display_Buffer[9] = 0x00; // : display_Buffer[10] = 0x00; display_Buffer[11] = 0x36; display_Buffer[12] = 0x36; display_Buffer[13] = 0x00; // 3 display_Buffer[14] = 0x3e; display_Buffer[15] = 0x49; display_Buffer[16] = 0x49; display_Buffer[17] = 0x41; display_Buffer[18] = 0x22; // 4 display_Buffer[19] = 0x00; display_Buffer[20] = 0x38; display_Buffer[21] = 0x54; display_Buffer[22] = 0x44; display_Buffer[23] = 0x28; // display_WriteData(display_Buffer, 0x0, DISPLAY_COLUMNS); // tdelay_ms(1000); display_WriteBuffer(); } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void GPIO_Init(void) { /* EXTI Line: falling, pull-up, input */ SYSCFG->EXTICR[3] = 0; /* Enable IT on provided Lines */ EXTI->IMR |= EXTI_IMR_IM12; /* Enable Falling Trigger on provided Lines */ EXTI->FTSR |= EXTI_IMR_IM12; /* EXTI interrupt init*/ NVIC_SetPriority(EXTI4_15_IRQn, 0); NVIC_EnableIRQ(EXTI4_15_IRQn); /* Select output mode (01) PP+PU, High Speed PA8 - Buzzer (AF2 for TIM1_CH1) PA15 - ~CS / SPI_NSS (AF0) * Select output mode (10) AF+PP, High Speed PB3 - ~WR / SPI_SCK (AF0) PB5 - Data / SPI_MOSI (AF0) * Select output mode (10) AF+OD, High Speed PB6 - SCL (AF1) PB7 - SDA (AF1) */ // MODE Output GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8 | GPIO_MODER_MODER15)) \ | (GPIO_MODER_MODER8_0 | GPIO_MODER_MODER15_0); GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER3|GPIO_MODER_MODER5)) \ | (GPIO_MODER_MODER3_1|GPIO_MODER_MODER5_1); // Pull-Up GPIOA->PUPDR = (GPIOA->PUPDR & ~(GPIO_PUPDR_PUPDR8 | GPIO_PUPDR_PUPDR15)) \ | (GPIO_PUPDR_PUPDR8_0 | GPIO_PUPDR_PUPDR15_0); // High Speed GPIOA->OSPEEDR = (GPIO_OSPEEDR_OSPEEDR8|GPIO_OSPEEDR_OSPEEDR15); GPIOB->OSPEEDR = (GPIO_OSPEEDR_OSPEEDR3|GPIO_OSPEEDR_OSPEEDR5 \ |GPIO_OSPEEDR_OSPEEDR6|GPIO_OSPEEDR_OSPEEDR7); // Open Drain GPIOB->OTYPER = (GPIO_OTYPER_OT_6|GPIO_OTYPER_OT_7); // AF1 for PB6 & PB7 GPIOB->AFR[0] = (0x1<<24) | (0x1<<28); /* Select Pull-Up for input pins PA0 - BTN1 / H+ PA1 - BTN2 / M+ PA2 - SW1 / Stop PA3 - SW2 / Sec PA4 - BTN3 / Res PA5 - SW3 / AlarmSet PA6 - SW5 / AlarmOff PA7 - SW4 / Bright PA12 - RTC IRQ / Exti */ GPIOA->PUPDR |= (GPIO_PUPDR_PUPDR0_0|GPIO_PUPDR_PUPDR1_0|GPIO_PUPDR_PUPDR2_0 \ |GPIO_PUPDR_PUPDR3_0|GPIO_PUPDR_PUPDR4_0|GPIO_PUPDR_PUPDR5_0 \ |GPIO_PUPDR_PUPDR6_0|GPIO_PUPDR_PUPDR7_0|GPIO_PUPDR_PUPDR12_0); } /** * @brief Configure SPI pin for software mode */ static void GPIO_SPI_SW(void) { // GPIO pin mode GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5); GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0); } /** * @brief Configure SPI pin for hardware mode */ static void GPIO_SPI_HW(void) { // SPI pin mode GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5); GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1); } /** * @brief I2C1 Initialization Function * @param None * @retval None */ static void I2C1_Init(void) { /* I2C1 DMA Ch2 for I2C1_TX Init */ // DMA_CCR_CIRC DMA1_Channel2->CCR = (DMA_CCR_DIR | DMA_CCR_PL_0 | DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_TEIE); /* I2C1 interrupt Init */ NVIC_SetPriority(I2C1_IRQn, 0); NVIC_EnableIRQ(I2C1_IRQn); /** I2C Initialization: I2C_Fast */ I2C1->CR1 = (I2C_CR1_TXDMAEN | I2C_CR1_RXIE); // | I2C_CR1_ERRIE I2C1->TIMINGR = 0x00701850; // 400 kHz / 48 MHz / 50 ns / 50 ns I2C1->CR2 = I2C_CR2_AUTOEND; I2C1->CR1 |= I2C_CR1_PE; } /** * @brief SPI1 Initialization Function * @param None * @retval None */ static void SPI1_Init(void) { /* SPI1 DMA Init */ /* SPI1_TX Init: Priority high, Memory increment, read from memory, non-circular mode, Enable DMA transfer complete/error interrupts */ DMA1_Channel3->CCR = (DMA_CCR_PL_1 | DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_CIRC | DMA_CCR_TCIE); // | DMA_CCR_TEIE /* SPI1 interrupt Init */ NVIC_SetPriority(SPI1_IRQn, 0); NVIC_EnableIRQ(SPI1_IRQn); /* SPI1 parameter configuration: master mode, data 8 bit, divider = 64, TX DMA */ SPI1->CR1 = (SPI_CR1_BR_2 | SPI_CR1_BR_0 | SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_CPOL | SPI_CR1_CPHA | SPI_CR1_LSBFIRST); SPI1->CR2 = (SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0 | SPI_CR2_TXDMAEN); } /** * @brief Initialization HT1632C */ void display_Init(void) { /* Wait for SPI */ while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); } GPIO_SPI_SW(); HT1632C_CS_ON; // Turn on system oscillator _display_WriteBits(0x802, 0x800); // Turn on LED duty cycle generator _display_WriteBits(0x6, 0x100); HT1632C_CS_OFF; GPIO_SPI_HW(); display_Fill(0x0); } /** * @brief Set HT1632C PWM Value * @param pwm value in 0-15 */ void display_PWM(uint8_t pwm) { // check value if (pwm > 15) { pwm = 15; } // align value pwm <<= 1; /* Wait for SPI */ while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); } GPIO_SPI_SW(); HT1632C_CS_ON; _display_WriteBits((0x940|pwm), 0x800); HT1632C_CS_OFF; GPIO_SPI_HW(); } /** * @brief Set HT1632C Blink state * @param state 0 for off, any other value for on */ void display_BlinkState(dis_en_t state) { /* Wait for SPI */ while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); } GPIO_SPI_SW(); HT1632C_CS_ON; if (state == Disable) { _display_WriteBits(0x810, 0x800); // Blink off } else { _display_WriteBits(0x812, 0x800); // Blink on } HT1632C_CS_OFF; GPIO_SPI_HW(); } /** * @brief Set HT1632C Led state * @param state 0 for off, any other value for on */ void display_LedState(dis_en_t state) { /* Wait for SPI */ while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); } GPIO_SPI_SW(); HT1632C_CS_ON; if (state == Disable) { _display_WriteBits(0x804, 0x800); // Led off } else { _display_WriteBits(0x806, 0x800); // Led on } HT1632C_CS_OFF; GPIO_SPI_HW(); } /** * @brief Primitive delay */ static void _delay_c(uint32_t cycle) { while (cycle) { __NOP(); cycle --; } } /** * @brief Write bit of data to selected HT1632Cs * @param data words to write * @param nbits num of bits (1<<(num-1)) */ void _display_WriteBits(const uint16_t data, uint16_t nbits) { _delay_c(2); do { HT1632C_WR_LOW; if (data & nbits) { HT1632C_DATA_1; } else { HT1632C_DATA_0; } _delay_c(5); HT1632C_WR_HGH; _delay_c(5); } while (nbits >>= 1); } /** * @brief Write Data to HT1632C * @param data pointer to data array * @param addr begin address * @param len bytes to write */ void display_WriteData(const uint8_t * data, uint8_t addr, uint8_t len) { /* check given values */ if (len == 0) { return; } if (addr >= DISPLAY_COLUMNS) { addr = DISPLAY_COLUMNS - 1; } if ((addr + len) > DISPLAY_COLUMNS) { len = DISPLAY_COLUMNS - addr; } #ifdef FRAMEBUFFER_ROTATE /* Copy given data to framebuffer */ int i, a; for (i=0; iSR & SPI_SR_BSY) != 0) { __NOP(); } /* DMA Source addr: Address of the SPI buffer. */ DMA1_Channel3->CMAR = data; /* Set DMA data transfer length (SPI buffer length). */ DMA1_Channel3->CNDTR = len; GPIO_SPI_SW(); HT1632C_CS_ON; _display_WriteBits(a, 0x200); GPIO_SPI_HW(); // start transfer SPI1->CR2 |= (SPI_CR2_TXDMAEN); DMA1_Channel3->CCR |= DMA_CCR_EN; // End of transaction in DMA1_Channel2_3_IRQHandler() #endif /* FRAMEBUFFER_ROTATE */ } /** * @brief Write all buffer to HT1632C */ void display_WriteBuffer(void) { /* Wait for SPI */ while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); } #ifdef FRAMEBUFFER_ROTATE static uint8_t spi_buf[DISPLAY_COLUMNS] = {0}; /* Rotate display buffer */ uint8_t i, j, p = 0x01; for (i=0; i<8; i++) { if ((display_Buffer[0] & p) != 0) { spi_buf[i] |= 0x01; } if ((display_Buffer[1] & p) != 0) { spi_buf[i] |= 0x02; } if ((display_Buffer[2] & p) != 0) { spi_buf[i] |= 0x04; } if ((display_Buffer[3] & p) != 0) { spi_buf[i] |= 0x08; } if ((display_Buffer[4] & p) != 0) { spi_buf[i] |= 0x10; } if ((display_Buffer[5] & p) != 0) { spi_buf[i] |= 0x20; } if ((display_Buffer[6] & p) != 0) { spi_buf[i] |= 0x40; } if ((display_Buffer[7] & p) != 0) { spi_buf[i] |= 0x80; } j = i + 8; if ((display_Buffer[8] & p) != 0) { spi_buf[j] |= 0x01; } if ((display_Buffer[9] & p) != 0) { spi_buf[j] |= 0x02; } if ((display_Buffer[10] & p) != 0) { spi_buf[j] |= 0x04; } if ((display_Buffer[11] & p) != 0) { spi_buf[j] |= 0x08; } if ((display_Buffer[12] & p) != 0) { spi_buf[j] |= 0x10; } if ((display_Buffer[13] & p) != 0) { spi_buf[j] |= 0x20; } if ((display_Buffer[14] & p) != 0) { spi_buf[j] |= 0x40; } if ((display_Buffer[15] & p) != 0) { spi_buf[j] |= 0x80; } j = i + 16; if ((display_Buffer[16] & p) != 0) { spi_buf[j] |= 0x01; } if ((display_Buffer[17] & p) != 0) { spi_buf[j] |= 0x02; } if ((display_Buffer[18] & p) != 0) { spi_buf[j] |= 0x04; } if ((display_Buffer[19] & p) != 0) { spi_buf[j] |= 0x08; } if ((display_Buffer[20] & p) != 0) { spi_buf[j] |= 0x10; } if ((display_Buffer[21] & p) != 0) { spi_buf[j] |= 0x20; } if ((display_Buffer[22] & p) != 0) { spi_buf[j] |= 0x40; } if ((display_Buffer[23] & p) != 0) { spi_buf[j] |= 0x80; } p <<= 1; } /* DMA Source addr: Address of the SPI buffer. */ DMA1_Channel3->CMAR = (uint32_t)&spi_buf[0]; #else /* DMA Source addr: Address of the SPI buffer. */ DMA1_Channel3->CMAR = (uint32_t)&display_Buffer[0]; #endif /* FRAMEBUFFER_ROTATE */ /* Set DMA data transfer length (SPI buffer length). */ DMA1_Channel3->CNDTR = DISPLAY_COLUMNS; GPIO_SPI_SW(); HT1632C_CS_ON; _display_WriteBits(0x280, 0x200); GPIO_SPI_HW(); /* start transfer */ SPI1->CR2 |= SPI_CR2_TXDMAEN; DMA1_Channel3->CCR |= DMA_CCR_EN; /* End of transaction in DMA1_Channel2_3_IRQHandler() */ } /** * @brief Fill display. * @param pattern for filling */ void display_Fill(uint8_t pattern) { int i; for (i=0; iPR & EXTI_IMR_IM12) != 0) { EXTI->PR = EXTI_IMR_IM12; Flag.RTC_IRQ = 1; //ES_PlaceEvent(evNewSecond); } } /** * @brief This function handles DMA1 channel 2 and 3 interrupts. */ void DMA1_Channel2_3_IRQHandler(void) { /* SPI Transmit Channel */ if ((DMA1->ISR & DMA_ISR_TCIF3) != 0) { // clear interrupt flag DMA1->IFCR = DMA_IFCR_CTCIF3; // disable transfer SPI1->CR2 &= ~(SPI_CR2_TXDMAEN); DMA1_Channel3->CCR &= ~(DMA_CCR_EN); // wite for spi transaction while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); } // deselect chip HT1632C_CS_OFF; } /* I2C Transmit Channel */ if ((DMA1->ISR & DMA_ISR_TCIF2) != 0) { // clear interrupt flag DMA1->IFCR = DMA_IFCR_CTCIF2; // Disable DMA channels for I2C TX DMA1_Channel2->CCR &= ~(DMA_CCR_EN); //Flag.I2C_TX_End = 1; } // I2C Transmit Error if ((DMA1->ISR & DMA_ISR_TEIF2) != 0) { DMA1->IFCR = DMA_IFCR_CTEIF2; DMA1_Channel2->CCR &= ~(DMA_CCR_EN); //Flag.I2C_RX_End = 1; //Flag.I2C_RX_Err = 1; } } /** * @brief This function handles I2C1 global interrupt. */ void I2C1_IRQHandler(void) { if ((I2C1->ISR & I2C_ISR_RXNE) != 0) { I2C1->RXDR; } }