Disable all LEDs (#2025)

* [led] add LED_DISABLE option to disable all LED

This is done in most case by setting the GPIO as input with high
impedance.

* [led] remove some old led code for debug in i2c

it was still based on the ST library, not used anymore
This commit is contained in:
Gautier Hattenberger
2017-02-24 18:33:56 +01:00
committed by Michal Podhradsky
parent e0b37d8cd2
commit 6da5030572
9 changed files with 78 additions and 319 deletions
+3 -2
View File
@@ -33,7 +33,7 @@
/*
* hal.h is needed for palXXX functions
*/
#include "hal.h"
#include <hal.h>
#include "mcu_periph/gpio_def.h"
#include BOARD_CONFIG
@@ -46,10 +46,11 @@
#define LED_GPIO(i) _LED_GPIO(LED_ ## i ## _GPIO)
#define LED_GPIO_PIN(i) _LED_GPIO_PIN(LED_ ## i ## _GPIO_PIN)
#define LED_INIT(i) {}
#define LED_INIT(i) palSetPadMode(LED_GPIO(i), LED_GPIO_PIN(i), PAL_MODE_OUTPUT_PUSHPULL)
#define LED_ON(i) palClearPad(LED_GPIO(i), LED_GPIO_PIN(i))
#define LED_OFF(i) palSetPad(LED_GPIO(i), LED_GPIO_PIN(i))
#define LED_TOGGLE(i) palTogglePad(LED_GPIO(i), LED_GPIO_PIN(i))
#define LED_DISABLE(i) palSetPadMode(LED_GPIO(i), LED_GPIO_PIN(i), PAL_MODE_INPUT)
#define LED_PERIODIC() {}
#endif /* LED_HW_H */
+2
View File
@@ -37,11 +37,13 @@ extern uint32_t led_hw_values;
#define LED_ON(i) { led_hw_values |= (1<<i); }
#define LED_OFF(i) { led_hw_values &= ~(1<<i); }
#define LED_TOGGLE(i) { led_hw_values ^= (1<<i); }
#define LED_DISABLE(i) LED_OFF(i)
#else
#define LED_INIT(i) {}
#define LED_ON(i) {}
#define LED_OFF(i) {}
#define LED_TOGGLE(i) {}
#define LED_DISABLE(i) LED_OFF(i)
#endif
#define LED_PERIODIC() {}
+3
View File
@@ -34,4 +34,7 @@
LED_OFF(i) \
}
/* set pin as input */
#define LED_DISABLE(i) LED_DIR(i) &= ~(_BV(LED_PIN(i)))
#endif /* LED_HW_H */
+1
View File
@@ -1,6 +1,7 @@
#include "led_hw.h"
value *leds_closure = 0;
bool led_disable = false;
value register_leds_cb(value cb_name)
{
+6 -4
View File
@@ -7,11 +7,13 @@
#include <caml/callback.h>
extern value *leds_closure;
extern bool led_disable;
#define LED_INIT(i) { }
#define LED_ON(i) { if (leds_closure) callback2(*leds_closure, Val_int(i), Val_int(1)); }
#define LED_OFF(i) { if (leds_closure) callback2(*leds_closure, Val_int(i), Val_int(0)); }
#define LED_TOGGLE(i) { if (leds_closure) callback2(*leds_closure, Val_int(i), Val_int(2)); }
#define LED_INIT(i) { led_disable = false; }
#define LED_ON(i) { if (leds_closure && !led_disable) callback2(*leds_closure, Val_int(i), Val_int(1)); }
#define LED_OFF(i) { if (leds_closure && !led_disable) callback2(*leds_closure, Val_int(i), Val_int(0)); }
#define LED_TOGGLE(i) { if (leds_closure && !led_disable) callback2(*leds_closure, Val_int(i), Val_int(2)); }
#define LED_DISABLE(i) { LED_OFF(i); led_disable = true; }
#define LED_PERIODIC() {}
+2
View File
@@ -55,6 +55,8 @@
#define LED_OFF(i) LED_GPIO_OFF(i)(LED_GPIO(i), LED_GPIO_PIN(i))
#define LED_TOGGLE(i) gpio_toggle(LED_GPIO(i), LED_GPIO_PIN(i))
#define LED_DISABLE(i) gpio_setup_input(LED_GPIO(i), LED_GPIO_PIN(i))
#define LED_PERIODIC() {}
@@ -1,175 +0,0 @@
#warning "LED debug needs porting to libopencm3 (or removal)"
static inline void LED1_ON(void)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_6 , Bit_SET);
}
static inline void LED1_OFF(void)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_6 , !Bit_SET);
}
static inline void LED2_ON(void)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_7 , Bit_SET);
}
static inline void LED2_OFF(void)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_7 , !Bit_SET);
}
static inline void LED_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
LED1_OFF();
LED2_OFF();
}
static inline void LED_ERROR(uint8_t base, uint8_t nr)
{
LED2_ON();
for (int i = 0; i < (base + nr); i++) {
LED1_ON();
LED1_OFF();
}
LED2_OFF();
}
static inline void LED_SHOW_ACTIVE_BITS(I2C_TypeDef *regs)
{
uint16_t CR1 = regs->CR1;
uint16_t SR1 = regs->SR1;
uint16_t SR2 = regs->SR2;
// Note: reading SR1 and then SR2 will clear ADDR bits
LED1_ON();
// 1 Start
if (BIT_X_IS_SET_IN_REG(I2C_SR1_BIT_SB, SR1)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 2 Addr
if (BIT_X_IS_SET_IN_REG(I2C_SR1_BIT_ADDR, SR1)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 3 BTF
if (BIT_X_IS_SET_IN_REG(I2C_SR1_BIT_BTF, SR1)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 4 ERROR
if ((SR1 & I2C_SR1_BITS_ERR) != 0x0000) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// Anything?
if ((SR1 + SR2) != 0x0000) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
LED1_OFF();
LED1_ON();
// 1 Start
if (BIT_X_IS_SET_IN_REG(I2C_CR1_BIT_START, CR1)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 2 Stop
if (BIT_X_IS_SET_IN_REG(I2C_CR1_BIT_STOP, CR1)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 3 Busy
if (BIT_X_IS_SET_IN_REG(I2C_SR2_BIT_BUSY, SR2)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 4 Tra
if (BIT_X_IS_SET_IN_REG(I2C_SR2_BIT_TRA, SR2)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 5 Master
if (BIT_X_IS_SET_IN_REG(I2C_SR2_BIT_MSL, SR2)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
LED1_OFF();
//#define I2C_DEBUG_LED_CONTROL
#ifdef I2C_DEBUG_LED_CONTROL
LED1_ON();
// 1 Anything CR?
if ((CR1) != 0x0000) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 2 PE
if (BIT_X_IS_SET_IN_REG(I2C_CR1_BIT_PE, CR1)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
// 3 SWRESET
if (BIT_X_IS_SET_IN_REG(I2C_CR1_BIT_SWRST, CR1)) {
LED2_ON();
} else {
LED2_OFF();
}
LED2_OFF();
LED1_OFF();
#endif
}
+2 -138
View File
@@ -38,10 +38,6 @@
#include "mcu_periph/gpio.h"
#ifdef I2C_DEBUG_LED
#include "i2c_debug_led.h"
#endif // I2C_DEBUG_LED
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
@@ -172,13 +168,6 @@ static inline void PPRZ_I2C_SEND_STOP(uint32_t i2c)
{
// Man: p722: Stop generation after the current byte transfer or after the current Start condition is sent.
i2c_send_stop(i2c);
#ifdef I2C_DEBUG_LED
LED2_ON();
LED1_ON();
LED1_OFF();
LED2_OFF();
#endif
}
// (RE)START
@@ -191,19 +180,6 @@ static inline void PPRZ_I2C_SEND_START(struct i2c_periph *periph)
periph->idx_buf = 0;
periph->watchdog = 0;
#ifdef I2C_DEBUG_LED
LED_SHOW_ACTIVE_BITS(regs);
LED2_ON();
LED1_ON();
LED1_OFF();
LED1_ON();
LED1_OFF();
LED1_ON();
LED1_OFF();
LED2_OFF();
#endif
// Enable Error IRQ, Event IRQ but disable Buffer IRQ
i2c_enable_interrupt(i2c, I2C_CR2_ITERREN);
i2c_enable_interrupt(i2c, I2C_CR2_ITEVTEN);
@@ -545,64 +521,36 @@ static inline enum STMI2CSubTransactionStatus stmi2c_readmany(uint32_t i2c, stru
static inline void i2c_error(struct i2c_periph *periph)
{
#ifdef I2C_DEBUG_LED
uint8_t err_nr = 0;
#endif
periph->errors->er_irq_cnt++;
if ((I2C_SR1((uint32_t)periph->reg_addr) & I2C_SR1_AF) != 0) { /* Acknowledge failure */
periph->errors->ack_fail_cnt++;
I2C_SR1((uint32_t)periph->reg_addr) &= ~I2C_SR1_AF;
#ifdef I2C_DEBUG_LED
err_nr = 1;
#endif
}
if ((I2C_SR1((uint32_t)periph->reg_addr) & I2C_SR1_BERR) != 0) { /* Misplaced Start or Stop condition */
periph->errors->miss_start_stop_cnt++;
I2C_SR1((uint32_t)periph->reg_addr) &= ~I2C_SR1_BERR;
#ifdef I2C_DEBUG_LED
err_nr = 2;
#endif
}
if ((I2C_SR1((uint32_t)periph->reg_addr) & I2C_SR1_ARLO) != 0) { /* Arbitration lost */
periph->errors->arb_lost_cnt++;
I2C_SR1((uint32_t)periph->reg_addr) &= ~I2C_SR1_ARLO;
#ifdef I2C_DEBUG_LED
err_nr = 3;
#endif
}
if ((I2C_SR1((uint32_t)periph->reg_addr) & I2C_SR1_OVR) != 0) { /* Overrun/Underrun */
periph->errors->over_under_cnt++;
I2C_SR1((uint32_t)periph->reg_addr) &= ~I2C_SR1_OVR;
#ifdef I2C_DEBUG_LED
err_nr = 4;
#endif
}
if ((I2C_SR1((uint32_t)periph->reg_addr) & I2C_SR1_PECERR) != 0) { /* PEC Error in reception */
periph->errors->pec_recep_cnt++;
I2C_SR1((uint32_t)periph->reg_addr) &= ~I2C_SR1_PECERR;
#ifdef I2C_DEBUG_LED
err_nr = 5;
#endif
}
if ((I2C_SR1((uint32_t)periph->reg_addr) & I2C_SR1_TIMEOUT) != 0) { /* Timeout or Tlow error */
periph->errors->timeout_tlow_cnt++;
I2C_SR1((uint32_t)periph->reg_addr) &= ~I2C_SR1_TIMEOUT;
#ifdef I2C_DEBUG_LED
err_nr = 6;
#endif
}
if ((I2C_SR1((uint32_t)periph->reg_addr) & I2C_SR1_SMBALERT) != 0) { /* SMBus alert */
periph->errors->smbus_alert_cnt++;
I2C_SR1((uint32_t)periph->reg_addr) &= ~I2C_SR1_SMBALERT;
#ifdef I2C_DEBUG_LED
err_nr = 7;
#endif
}
#ifdef I2C_DEBUG_LED
LED_ERROR(20, err_nr);
#endif
return;
}
@@ -714,12 +662,6 @@ static inline void i2c_irq(struct i2c_periph *periph)
// Apparently we got an I2C interrupt: EVT BUF or ERR
#ifdef I2C_DEBUG_LED
// Notify ISR is triggered
LED1_ON();
LED1_OFF();
#endif
// Save Some Direct Access to the I2C Registers ...
uint32_t i2c = (uint32_t) periph->reg_addr;
@@ -728,16 +670,6 @@ static inline void i2c_irq(struct i2c_periph *periph)
if (periph->trans_extract_idx == periph->trans_insert_idx) {
// Nothing Left To Do
#ifdef I2C_DEBUG_LED
LED2_ON();
LED1_ON();
LED2_OFF();
LED1_OFF();
// no transaction and also an error?
LED_SHOW_ACTIVE_BITS(regs);
#endif
// If we still get an interrupt but there are no more things to do
// (which can happen if an event was sheduled just before a bus error occurs)
// (or can happen if both error and event interrupts were called together [the 2nd will then get this error])
@@ -770,15 +702,6 @@ static inline void i2c_irq(struct i2c_periph *periph)
// If there was an error:
if ((I2C_SR1(i2c) & I2C_SR1_ERR_MASK) != 0x0000) {
#ifdef I2C_DEBUG_LED
LED1_ON();
LED2_ON();
LED1_OFF();
LED2_OFF();
LED_SHOW_ACTIVE_BITS(regs);
#endif
// Notify everyone about the error ...
// Set result in transaction
@@ -842,16 +765,6 @@ static inline void i2c_irq(struct i2c_periph *periph)
trans->status = I2CTransFailed; // Notify Ready
periph->errors->unexpected_event_cnt++;
// Error
#ifdef I2C_DEBUG_LED
LED2_ON();
LED1_ON();
LED2_OFF();
LED1_OFF();
LED_SHOW_ACTIVE_BITS(regs);
#endif
// Clear Running Events
stmi2c_clear_pending_interrupts(i2c);
}
@@ -891,14 +804,6 @@ static inline void i2c_irq(struct i2c_periph *periph)
if (periph->trans_extract_idx == periph->trans_insert_idx) {
periph->watchdog = -1; // stop watchdog
#ifdef I2C_DEBUG_LED
LED2_ON();
LED1_ON();
LED1_OFF();
LED1_ON();
LED1_OFF();
LED2_OFF();
#endif
}
// if not, start next transaction
else {
@@ -949,11 +854,6 @@ void i2c1_hw_init(void)
/* zeros error counter */
ZEROS_ERR_COUNTER(i2c1_errors);
// Extra
#ifdef I2C_DEBUG_LED
LED_INIT();
#else
/* reset peripheral to default state ( sometimes not achieved on reset :( ) */
//rcc_periph_reset_pulse(RST_I2C1);
@@ -982,7 +882,6 @@ void i2c1_hw_init(void)
i2c_enable_interrupt(I2C1, I2C_CR2_ITERREN);
i2c_setbitrate(&i2c1, I2C1_CLOCK_SPEED);
#endif
}
void i2c1_ev_isr(void)
@@ -1236,21 +1135,6 @@ void i2c_setbitrate(struct i2c_periph *periph, int bitrate)
__enable_irq();
#ifdef I2C_DEBUG_LED
__disable_irq(); // this code is in user space:
LED2_ON();
LED1_ON();
LED2_OFF();
LED1_OFF();
LED2_ON();
LED1_ON();
LED2_OFF();
LED1_OFF();
__enable_irq();
#endif
}
}
@@ -1456,20 +1340,7 @@ bool i2c_submit(struct i2c_periph *periph, struct i2c_transaction *t)
if (periph->status == I2CIdle) {
//if (i2c_idle(periph))
{
#ifdef I2C_DEBUG_LED
#if USE_I2C1
if (periph == &i2c1) {
} else
#endif
#endif
{
#ifdef I2C_DEBUG_LED
LED2_ON();
LED2_OFF();
#endif
PPRZ_I2C_SEND_START(periph);
}
PPRZ_I2C_SEND_START(periph);
}
}
/* else it will be started by the interrupt handler when the previous transactions completes */
@@ -1485,14 +1356,6 @@ bool i2c_idle(struct i2c_periph *periph)
uint32_t i2c = (uint32_t) periph->reg_addr;
#ifdef I2C_DEBUG_LED
#if USE_I2C1
if (periph == &i2c1) {
return true;
}
#endif
#endif
// First we check if the software thinks it is ready
if (periph->status == I2CIdle) {
return !(BIT_X_IS_SET_IN_REG(I2C_SR2_BUSY, I2C_SR2(i2c)));
@@ -1500,3 +1363,4 @@ bool i2c_idle(struct i2c_periph *periph)
return false;
}
}
+59
View File
@@ -33,6 +33,9 @@
#include "led_hw.h"
/** Automatic initialization of actived LED
* Set to OFF at startup
*/
static inline void led_init(void)
{
#if USE_LED_1
@@ -96,14 +99,70 @@ static inline void led_init(void)
#endif /* LED_12 */
}
/** Automatic disabling of activated LED
* Usually by setting control GPIO in high impedance input mode
*/
static inline void led_disable(void)
{
#if USE_LED_1
LED_DISABLE(1);
#endif /* LED_1 */
#if USE_LED_2
LED_DISABLE(2);
#endif /* LED_2 */
#if USE_LED_3
LED_DISABLE(3);
#endif /* LED_3 */
#if USE_LED_4
LED_DISABLE(4);
#endif /* LED_4 */
#if USE_LED_5
LED_DISABLE(5);
#endif /* LED_5 */
#if USE_LED_6
LED_DISABLE(6);
#endif /* LED_6 */
#if USE_LED_7
LED_DISABLE(7);
#endif /* LED_7 */
#if USE_LED_8
LED_DISABLE(8);
#endif /* LED_8 */
#if USE_LED_9
LED_DISABLE(9);
#endif /* LED_9 */
#if USE_LED_10
LED_DISABLE(10);
#endif /* LED_10 */
#ifdef USE_LED_BODY
LED_DISABLE(BODY);
#endif /* LED_BODY */
#if USE_LED_12
LED_DISABLE(12);
#endif /* LED_12 */
}
#define _LED_AVAILABLE(i) USE_LED_ ## i
#define LED_AVAILABLE(i) _LED_AVAILABLE(i)
#else /* USE_LED */
static inline void led_init(void) {}
static inline void led_disable(void) {}
#define LED_ON(i) {}
#define LED_OFF(i) {}
#define LED_TOGGLE(i) {}
#define LED_DISABLE(i) {}
#define LED_PERIODIC() {}
#define LED_AVAILABLE(i) 0
#endif /* USE_LED */