rcc_periph_clock_enable(RCC_GPIO(LED_TM1637_CLK_PORT));// enable clock for GPIO peripheral
gpio_set(GPIO(LED_TM1637_CLK_PORT),GPIO(LED_TM1637_CLK_PIN));// idle high
gpio_set_mode(GPIO(LED_TM1637_CLK_PORT),GPIO_MODE_OUTPUT_10_MHZ,GPIO_CNF_OUTPUT_PUSHPULL,GPIO(LED_TM1637_CLK_PIN));// master start the communication (capacitance is to large for open drain), only switch to input for ack from slave
rcc_periph_clock_enable(RCC_GPIO(LED_TM1637_DIO_PORT));// enable clock for GPIO peripheral
gpio_set(GPIO(LED_TM1637_DIO_PORT),GPIO(LED_TM1637_DIO_PIN));// idle high
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT),GPIO_MODE_OUTPUT_10_MHZ,GPIO_CNF_OUTPUT_PUSHPULL,GPIO(LED_TM1637_DIO_PIN));// master start the communication (capacitance is to large for open drain), only switch to input for ack from slave
// first clock then data high also stands for stop condition
// setup timer to create signal timing (each tick is used for a single GPIO transition)
rcc_periph_clock_enable(RCC_TIM(LED_TM1637_TIMER));// enable clock for timer block
timer_reset(TIM(LED_TM1637_TIMER));// reset timer state
timer_set_mode(TIM(LED_TM1637_TIMER),TIM_CR1_CKD_CK_INT,TIM_CR1_CMS_EDGE,TIM_CR1_DIR_UP);// set timer mode, use undivided timer clock, edge alignment (simple count), and count up
timer_set_prescaler(TIM(LED_TM1637_TIMER),0);// don't prescale to get most precise timing ( 1/(72E6/1/(2**16))=0.91 ms > 0.5 us )
timer_set_period(TIM(LED_TM1637_TIMER),500);// set the clock frequency (emprical value until the signal starts to look bad)
timer_clear_flag(TIM(LED_TM1637_TIMER),TIM_SR_UIF);// clear flag
gpio_set(GPIO(LED_TM1637_CLK_PORT),GPIO(LED_TM1637_CLK_PIN));// put CLK high
led_tm1637_tick();// wait for next tick (no DIO transition when CLK is high)
gpio_clear(GPIO(LED_TM1637_CLK_PORT),GPIO(LED_TM1637_CLK_PIN));// put CLK low
}
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT),GPIO_MODE_INPUT,GPIO_CNF_INPUT_FLOAT,GPIO(LED_TM1637_DIO_PIN));// switch DIO as input to read ACK
led_tm1637_tick();// wait for next tick (when the slave should ACK)
gpio_set(GPIO(LED_TM1637_CLK_PORT),GPIO(LED_TM1637_CLK_PIN));// put CLK high
if(gpio_get(GPIO(LED_TM1637_DIO_PORT),GPIO(LED_TM1637_DIO_PIN))){// no ACK received
to_return=false;// remember there was an error
break;// stop sending bytes
}
led_tm1637_tick();// wait for next tick
gpio_clear(GPIO(LED_TM1637_CLK_PORT),GPIO(LED_TM1637_CLK_PIN));// put CLK low
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT),GPIO_MODE_OUTPUT_2_MHZ,GPIO_CNF_OUTPUT_PUSHPULL,GPIO(LED_TM1637_DIO_PIN));// switch DIO back to output to send next byte
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT),GPIO_MODE_OUTPUT_2_MHZ,GPIO_CNF_OUTPUT_PUSHPULL,GPIO(LED_TM1637_DIO_PIN));// ensure DIO is output (in case no ACK as been received
led_tm1637_tick();// wait for next tick
gpio_set(GPIO(LED_TM1637_CLK_PORT),GPIO(LED_TM1637_CLK_PIN));// put CLK high
led_tm1637_tick();// wait for next tick
gpio_set(GPIO(LED_TM1637_DIO_PORT),GPIO(LED_TM1637_DIO_PIN));// put DIO high
timer_disable_counter(TIM(LED_TM1637_TIMER));// stop timer since it's not used anymore
uint8_tdata[]={0xc0,ascii_7segments[((number/1000)%10)+'0'-''],ascii_7segments[((number/100)%10)+'0'-''],ascii_7segments[((number/10)%10)+'0'-''],ascii_7segments[((number/1)%10)+'0'-'']};// set address C0H and add data
uint8_twrite_data[]={0x40};// command: write data, automatic address adding, normal
uint8_tdata[]={0xc0,ascii_7segments[((hours/10)%10)+'0'-''],ascii_7segments[((hours/1)%10)+'0'-'']|0x80,ascii_7segments[((minutes/10)%10)+'0'-''],ascii_7segments[((minutes/1)%10)+'0'-'']};// set address C0H and add data
uint8_twrite_data[]={0x40};// command: write data, automatic address adding, normal
uint8_tdata[]={0xc0,ascii_7segments[(text[0]&0x7f)-'']|(text[0]&0x80),ascii_7segments[(text[1]&0x7f)-'']|(text[1]&0x80),ascii_7segments[(text[2]&0x7f)-'']|(text[2]&0x80),ascii_7segments[(text[3]&0x7f)-'']|(text[3]&0x80)};// set address C0H and add data