reimplement protocol using big banging (now works)
This commit is contained in:
parent
6fa9c0096e
commit
810a84cd10
150
lib/led_tm1637.c
150
lib/led_tm1637.c
|
@ -16,8 +16,9 @@
|
|||
* @file led_tm1637.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: I2C @ref led_tm1637_i2c
|
||||
* @warning no interrupts or sleep modes are used
|
||||
* @note peripherals used: GPIO @ref led_tm1637_gpio, timer @ref led_tm1637_timer
|
||||
* @note the protocol is very similar to I2C but incompatible for the following reasons: the capacitance is too large for open-drain type output with weak pull-up resistors (push-pull needs to be used, preventing to get ACKs since no indication of the ACK timing is provided); the devices doesn't use addresses; the STM32 I2C will switch to receiver mode when the first sent byte (the I2C address) has last bit set to 1 (such as for address commands with B7=1 where B7 is transmitted last), preventing to send further bytes (the data byte after the address)
|
||||
* @warning all calls are blocking
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
|
@ -28,83 +29,118 @@
|
|||
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/i2c.h> // I2C library
|
||||
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
||||
#include <libopencm3/stm32/timer.h> // timer library
|
||||
|
||||
#include "global.h" // global utilities
|
||||
#include "led_tm1637.h" // TM1637 header and definitions
|
||||
|
||||
/** @defgroup led_tm1637_i2c I2C peripheral used to communication with TM1637 IC
|
||||
/** @defgroup led_tm1637_gpio GPIO used to communication with TM1637 IC
|
||||
* @{
|
||||
*/
|
||||
/** I2C peripheral */
|
||||
#define LED_TM1637_I2C 1 /**< I2C peripheral */
|
||||
#define LED_TM1637_CLK_PORT B /**< port for CLK signal */
|
||||
#define LED_TM1637_CLK_PIN 6 /**< pin for CLK signal */
|
||||
#define LED_TM1637_DIO_PORT B /**< port for DIO signal */
|
||||
#define LED_TM1637_DIO_PIN 7 /**< pin for DIO signal */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup led_tm1637_timer timer used to communication with TM1637 IC
|
||||
* @{
|
||||
*/
|
||||
#define LED_TM1637_TIMER 3 /**< timer to create signal */
|
||||
/** @} */
|
||||
|
||||
/** display brightness */
|
||||
static enum led_tm1637_brightness_t display_brightness = LED_TM1637_11DIV16;
|
||||
/** if display is on */
|
||||
static bool display_on = false;
|
||||
|
||||
//#define RTC_DS1307_I2C_RCC RCC_I2C1 /**< I2C peripheral clock */
|
||||
//#define RTC_DS1307_I2C_PORT_RCC RCC_GPIOB /**< I2C I/O peripheral clock */
|
||||
//#define RTC_DS1307_I2C_PORT GPIOB /**< I2C I/O peripheral port */
|
||||
//#define RTC_DS1307_I2C_PIN_SDA GPIO_I2C1_SDA /**< I2C peripheral data pin (PB7) */
|
||||
//#define RTC_DS1307_I2C_PIN_SCL GPIO_I2C1_SCL /**< I2C peripheral clock pin (PB6) */
|
||||
//#define RTC_DS1307_I2C_ADDR 0x68 /**< DS1307 I2C address (fixed to 0b1101000) */
|
||||
/** @} */
|
||||
|
||||
void led_tm1637_setup(void)
|
||||
{
|
||||
// configure GPIO for I2C peripheral (this cause a small low pulse on the lines, but I didn't figure out why)
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
|
||||
rcc_periph_clock_enable(RCC_I2C_PORT(LED_TM1637_I2C)); // all I2C pin are on port B
|
||||
gpio_set_mode(I2C_SCL_PORT(LED_TM1637_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, I2C_SCL_PIN(LED_TM1637_I2C)); // clock line will be an output since we will be master
|
||||
gpio_set_mode(I2C_SDA_PORT(LED_TM1637_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, I2C_SDA_PIN(LED_TM1637_I2C)); // data line will be an output since we will be master
|
||||
// configure GPIO for CLK and DIO signals
|
||||
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
|
||||
|
||||
// configure I2C peripheral
|
||||
rcc_periph_clock_enable(RCC_I2C(LED_TM1637_I2C)); // enable clock for I2C peripheral
|
||||
i2c_reset(I2C(LED_TM1637_I2C)); // reset configuration
|
||||
i2c_peripheral_disable(I2C(LED_TM1637_I2C)); // I2C needs to be disable to be configured
|
||||
i2c_set_clock_frequency(I2C(LED_TM1637_I2C), rcc_apb1_frequency/1000000); // configure the peripheral clock to the APB1 freq (where it is connected to)
|
||||
i2c_set_fast_mode(I2C(LED_TM1637_I2C));
|
||||
i2c_set_ccr(I2C(LED_TM1637_I2C), rcc_apb1_frequency/(400000*2)); // set Thigh/Tlow to generate frequency of 400 kHz
|
||||
i2c_set_trise(I2C(LED_TM1637_I2C), (300/(1000/(rcc_apb1_frequency/1000000)))+1); // max rise time for 300 kHz is 300 ns
|
||||
i2c_peripheral_enable(I2C(LED_TM1637_I2C)); // enable I2C after configuration completed
|
||||
// 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
|
||||
//timer_update_on_overflow(TIM(LED_TM1637_TIMER)); // only use counter overflow as UEV source (use overflow as start time or timeout)
|
||||
timer_enable_irq(TIM(LED_TM1637_TIMER), TIM_DIER_UIE); // enable update interrupt for timer to wake up (no ISR used)
|
||||
}
|
||||
|
||||
/** write data on I2C bus
|
||||
/** wait until clock tick (timer overflow) occurred
|
||||
*/
|
||||
static void led_tm1637_tick(void)
|
||||
{
|
||||
while (!timer_get_flag(TIM(LED_TM1637_TIMER), TIM_SR_UIF)); // wait until counter overflow update event happens
|
||||
timer_clear_flag(TIM(LED_TM1637_TIMER), TIM_SR_UIF); // clear event flag
|
||||
}
|
||||
|
||||
/** write data on bus
|
||||
* @param[in] data bytes to write
|
||||
* @param[in] length number of bytes to write
|
||||
* @return if write succeeded
|
||||
* @note includes start and stop conditions
|
||||
*/
|
||||
static bool led_tm1637_write(const uint8_t* data, size_t length)
|
||||
static bool led_tm1637_write(uint8_t* data, size_t length)
|
||||
{
|
||||
bool to_return = false; // return if write succeeded
|
||||
bool to_return = true; // return if write succeeded
|
||||
if (data==NULL || length==0) { // verify there it data to be read
|
||||
return false;
|
||||
}
|
||||
i2c_send_start(I2C(LED_TM1637_I2C)); // send start condition to start transaction
|
||||
while (!(I2C_SR1(I2C(LED_TM1637_I2C)) & I2C_SR1_SB)); // wait until start condition is transmitted
|
||||
if (!(I2C_SR2(I2C(LED_TM1637_I2C)) & I2C_SR2_MSL)) { // verify if in master mode
|
||||
goto error;
|
||||
|
||||
// enable timer for signal generation
|
||||
timer_set_counter(TIM(LED_TM1637_TIMER), 0); // reset timer counter
|
||||
timer_enable_counter(TIM(LED_TM1637_TIMER)); // enable timer to generate timing
|
||||
led_tm1637_tick(); // wait to enforce minimum time since last write
|
||||
|
||||
// send start condition (DIO then CLK low)
|
||||
gpio_clear(GPIO(LED_TM1637_DIO_PORT), GPIO(LED_TM1637_DIO_PIN)); // put DIO low
|
||||
led_tm1637_tick(); // wait for next tick
|
||||
gpio_clear(GPIO(LED_TM1637_CLK_PORT), GPIO(LED_TM1637_CLK_PIN)); // put CLK low
|
||||
|
||||
// send data bytes (MSb first)
|
||||
for (size_t i=0; i<length; i++) { // send all bytes
|
||||
for (uint8_t b=0; b<8; b++) { // send all bits
|
||||
if (data[i]&0x1) { // send a 1
|
||||
gpio_set(GPIO(LED_TM1637_DIO_PORT), GPIO(LED_TM1637_DIO_PIN)); // put DIO high
|
||||
} else {
|
||||
gpio_clear(GPIO(LED_TM1637_DIO_PORT), GPIO(LED_TM1637_DIO_PIN)); // put DIO low
|
||||
}
|
||||
data[i] >>= 1; // shift data
|
||||
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 (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
|
||||
}
|
||||
// in I2C the first byte is the slave address and has special handling
|
||||
i2c_send_data(I2C(LED_TM1637_I2C), data[0]); // send first byte (don't use the i2c_send_7bit_address since we don't really care about the 7-bit address or if it's a read or write command)
|
||||
while (!(I2C_SR1(I2C(LED_TM1637_I2C)) & I2C_SR1_ADDR)); // wait until address is transmitted
|
||||
if (length>1 && !((I2C_SR2(I2C(LED_TM1637_I2C)) & I2C_SR2_TRA))) { // we can only send further byte if the first byte is an I2C write command and put us in transmit mode)
|
||||
goto error; // we are not it transmit mode and can't send further bytes
|
||||
}
|
||||
for (size_t i=1; i<length; i++) {
|
||||
i2c_send_data(I2C(LED_TM1637_I2C), data[i]); // send data
|
||||
while (!(I2C_SR1(I2C(LED_TM1637_I2C)) & I2C_SR1_TxE)); // wait until byte has been transmitted and ACKed
|
||||
}
|
||||
to_return = true;
|
||||
error:
|
||||
if (I2C_SR2(I2C(LED_TM1637_I2C)) & I2C_SR2_BUSY) { // release bus if busy
|
||||
i2c_send_stop(I2C(LED_TM1637_I2C)); // send stop to release bus
|
||||
}
|
||||
while (I2C_SR2(I2C(LED_TM1637_I2C)) & I2C_SR2_MSL); // wait until bus released (non master mode)
|
||||
|
||||
// send stop condition
|
||||
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
|
||||
|
||||
return to_return;
|
||||
}
|
||||
|
||||
|
@ -121,7 +157,7 @@ bool led_tm1637_on(void)
|
|||
|
||||
bool led_tm1637_off(void)
|
||||
{
|
||||
const uint8_t data[] = { 0x80+display_brightness }; // command to turn display off (use set brightness)
|
||||
uint8_t data[] = { 0x80+display_brightness }; // command to turn display off (use set brightness)
|
||||
bool to_return = false; // result to return
|
||||
if (led_tm1637_write(data,LENGTH(data))) { // send command
|
||||
display_on = false; // remember display is off
|
||||
|
@ -146,12 +182,10 @@ bool led_tm1637_number(uint16_t number)
|
|||
{
|
||||
(void) number;
|
||||
bool to_return = false; // result to return
|
||||
const uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
|
||||
uint8_t data_0[] = { 0xc0, 0x00, 0xff }; // set address C0H and add data
|
||||
uint8_t data_2[] = { 0xc2, 0x00, 0xff };
|
||||
uint8_t data_4[] = { 0xc4, 0x00, 0xff };
|
||||
uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
|
||||
uint8_t data[] = { 0xc0, 0xff, 0x00, 0x00, 0xff }; // set address C0H and add data
|
||||
|
||||
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data_0,LENGTH(data_0)) && led_tm1637_write(data_2,LENGTH(data_2)) && led_tm1637_write(data_4,LENGTH(data_4))) { // send commands
|
||||
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data,LENGTH(data))) { // send commands
|
||||
to_return = true;
|
||||
}
|
||||
return to_return;
|
||||
|
|
|
@ -16,7 +16,8 @@
|
|||
* @file led_tm1637.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: I2C @ref led_tm1637_i2c
|
||||
* @note peripherals used: GPIO @ref led_tm1637_gpio, timer @ref led_tm1637_timer
|
||||
* @warning all calls are blocking
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
|
Loading…
Reference in New Issue