Update .clang-format and apply
Update .clang-format for LLVM 14.0, available on Ubuntu 22.04. There is still plenty that clang-format sucks at or does wrong, so either add some more blocks to disable it, or just put up with it. Signed-off-by: Tim Crawford <tcrawford@system76.com>
This commit is contained in:
		
				
					committed by
					
						
						Jeremy Soller
					
				
			
			
				
	
			
			
			
						parent
						
							c3267fc4ad
						
					
				
				
					commit
					e032c5f0f2
				
			@@ -9,13 +9,11 @@ void delay_ticks(uint16_t ticks);
 | 
			
		||||
 | 
			
		||||
// 1 us * 9.2 MHz / 12 is 69/90
 | 
			
		||||
// Warning: this will round to the nearest tick
 | 
			
		||||
#define delay_us(X) \
 | 
			
		||||
    delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89UL) / 90UL));
 | 
			
		||||
#define delay_us(X) delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89UL) / 90UL));
 | 
			
		||||
 | 
			
		||||
// 1 ns * 9.2 MHz / 12 is 69/90000
 | 
			
		||||
// Warning: this will round to the nearest tick
 | 
			
		||||
#define delay_ns(X) \
 | 
			
		||||
    delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89999UL) / 90000UL));
 | 
			
		||||
#define delay_ns(X) delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89999UL) / 90000UL));
 | 
			
		||||
 | 
			
		||||
void delay_ms(uint8_t ms);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 | 
			
		||||
#include <arch/gpio.h>
 | 
			
		||||
 | 
			
		||||
bool gpio_get_dir(struct Gpio * gpio) {
 | 
			
		||||
bool gpio_get_dir(struct Gpio *gpio) {
 | 
			
		||||
    if (*gpio->ddr & gpio->value) {
 | 
			
		||||
        return true;
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -10,7 +10,7 @@ bool gpio_get_dir(struct Gpio * gpio) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void gpio_set_dir(struct Gpio * gpio, bool value) {
 | 
			
		||||
void gpio_set_dir(struct Gpio *gpio, bool value) {
 | 
			
		||||
    if (value) {
 | 
			
		||||
        *gpio->ddr |= gpio->value;
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -18,7 +18,7 @@ void gpio_set_dir(struct Gpio * gpio, bool value) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool gpio_get(struct Gpio * gpio) {
 | 
			
		||||
bool gpio_get(struct Gpio *gpio) {
 | 
			
		||||
    if (*gpio->pin & gpio->value) {
 | 
			
		||||
        return true;
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -26,7 +26,7 @@ bool gpio_get(struct Gpio * gpio) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void gpio_set(struct Gpio * gpio, bool value) {
 | 
			
		||||
void gpio_set(struct Gpio *gpio, bool value) {
 | 
			
		||||
    if (value) {
 | 
			
		||||
        *gpio->port |= gpio->value;
 | 
			
		||||
    } else {
 | 
			
		||||
 
 | 
			
		||||
@@ -8,9 +8,9 @@
 | 
			
		||||
#include <common/i2c.h>
 | 
			
		||||
#include <common/macro.h>
 | 
			
		||||
 | 
			
		||||
#define TIMEOUT (F_CPU/1000)
 | 
			
		||||
#define TIMEOUT (F_CPU / 1000)
 | 
			
		||||
 | 
			
		||||
int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
 | 
			
		||||
int16_t i2c_start(struct I2C *i2c, uint8_t addr, bool read) {
 | 
			
		||||
    uint32_t count;
 | 
			
		||||
 | 
			
		||||
    // reset TWI control register
 | 
			
		||||
@@ -19,11 +19,14 @@ int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
 | 
			
		||||
    TWCR = BIT(TWINT) | BIT(TWSTA) | BIT(TWEN);
 | 
			
		||||
    // wait for end of transmission
 | 
			
		||||
    count = TIMEOUT;
 | 
			
		||||
    while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
 | 
			
		||||
    if (count == 0) return -1;
 | 
			
		||||
    while (!(TWCR & BIT(TWINT)) && count > 0)
 | 
			
		||||
        count -= 1;
 | 
			
		||||
    if (count == 0)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    // check if the start condition was successfully transmitted
 | 
			
		||||
    if((TWSR & 0xF8) != TW_START) return -1;
 | 
			
		||||
    if ((TWSR & 0xF8) != TW_START)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    // load slave addr into data register
 | 
			
		||||
    TWDR = ((addr << 1) | read);
 | 
			
		||||
@@ -31,22 +34,25 @@ int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
 | 
			
		||||
    TWCR = BIT(TWINT) | BIT(TWEN);
 | 
			
		||||
    // wait for end of transmission
 | 
			
		||||
    count = TIMEOUT;
 | 
			
		||||
    while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
 | 
			
		||||
    if (count == 0) return -1;
 | 
			
		||||
    while (!(TWCR & BIT(TWINT)) && count > 0)
 | 
			
		||||
        count -= 1;
 | 
			
		||||
    if (count == 0)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    // check if the device has acknowledged the READ / WRITE mode
 | 
			
		||||
    uint8_t twst = TW_STATUS & 0xF8;
 | 
			
		||||
    if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) return -1;
 | 
			
		||||
    if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK))
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void i2c_stop(struct I2C * i2c) {
 | 
			
		||||
void i2c_stop(struct I2C *i2c) {
 | 
			
		||||
    // transmit STOP condition
 | 
			
		||||
    TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) {
 | 
			
		||||
int16_t i2c_write(struct I2C *i2c, uint8_t *data, uint16_t length) {
 | 
			
		||||
    uint16_t i;
 | 
			
		||||
    for (i = 0; i < length; i++) {
 | 
			
		||||
        // load data into data register
 | 
			
		||||
@@ -55,17 +61,20 @@ int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) {
 | 
			
		||||
        TWCR = BIT(TWINT) | BIT(TWEN);
 | 
			
		||||
        // wait for end of transmission
 | 
			
		||||
        uint32_t count = TIMEOUT;
 | 
			
		||||
        while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
 | 
			
		||||
        while (!(TWCR & BIT(TWINT)) && count > 0)
 | 
			
		||||
            count -= 1;
 | 
			
		||||
        // timed out
 | 
			
		||||
        if (count == 0) return -1;
 | 
			
		||||
        if (count == 0)
 | 
			
		||||
            return -1;
 | 
			
		||||
        // failed to receive ack
 | 
			
		||||
        if((TWSR & 0xF8) != TW_MT_DATA_ACK) return -1;
 | 
			
		||||
        if ((TWSR & 0xF8) != TW_MT_DATA_ACK)
 | 
			
		||||
            return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) {
 | 
			
		||||
int16_t i2c_read(struct I2C *i2c, uint8_t *data, uint16_t length) {
 | 
			
		||||
    uint16_t i;
 | 
			
		||||
    for (i = 0; i < length; i++) {
 | 
			
		||||
        if ((i + 1) < length) {
 | 
			
		||||
@@ -77,8 +86,10 @@ int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) {
 | 
			
		||||
        }
 | 
			
		||||
        // wait for end of transmission
 | 
			
		||||
        uint32_t count = TIMEOUT;
 | 
			
		||||
        while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
 | 
			
		||||
        if (count == 0) return -1;
 | 
			
		||||
        while (!(TWCR & BIT(TWINT)) && count > 0)
 | 
			
		||||
            count -= 1;
 | 
			
		||||
        if (count == 0)
 | 
			
		||||
            return -1;
 | 
			
		||||
        // return received data from TWDR
 | 
			
		||||
        data[i] = TWDR;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -11,11 +11,16 @@
 | 
			
		||||
#include <arch/i2c_slave.h>
 | 
			
		||||
#include <common/macro.h>
 | 
			
		||||
 | 
			
		||||
static void (* volatile i2c_slave_new_cb)() = NULL;
 | 
			
		||||
static void (* volatile i2c_slave_recv_cb)(uint8_t) = NULL;
 | 
			
		||||
static uint8_t (* volatile i2c_slave_send_cb)() = NULL;
 | 
			
		||||
static void (*volatile i2c_slave_new_cb)() = NULL;
 | 
			
		||||
static void (*volatile i2c_slave_recv_cb)(uint8_t) = NULL;
 | 
			
		||||
static uint8_t (*volatile i2c_slave_send_cb)() = NULL;
 | 
			
		||||
 | 
			
		||||
void i2c_slave_init(uint8_t address, void (*new_cb)(), void (*recv_cb)(uint8_t), uint8_t (*send_cb)()){
 | 
			
		||||
void i2c_slave_init(
 | 
			
		||||
    uint8_t address,
 | 
			
		||||
    void (*new_cb)(),
 | 
			
		||||
    void (*recv_cb)(uint8_t),
 | 
			
		||||
    uint8_t (*send_cb)()
 | 
			
		||||
) {
 | 
			
		||||
    // ensure correct behavior by stopping before changing callbacks or address
 | 
			
		||||
    i2c_slave_stop();
 | 
			
		||||
 | 
			
		||||
@@ -35,7 +40,7 @@ void i2c_slave_init(uint8_t address, void (*new_cb)(), void (*recv_cb)(uint8_t),
 | 
			
		||||
    sei();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void i2c_slave_stop(){
 | 
			
		||||
void i2c_slave_stop() {
 | 
			
		||||
    // clear interrupts
 | 
			
		||||
    cli();
 | 
			
		||||
 | 
			
		||||
@@ -54,7 +59,7 @@ void i2c_slave_stop(){
 | 
			
		||||
 | 
			
		||||
ISR(TWI_vect) {
 | 
			
		||||
    uint8_t status = TW_STATUS;
 | 
			
		||||
    switch(status) {
 | 
			
		||||
    switch (status) {
 | 
			
		||||
    case TW_SR_SLA_ACK:
 | 
			
		||||
        // master has started a new transaction, call the new callback
 | 
			
		||||
        if (i2c_slave_new_cb != NULL) {
 | 
			
		||||
@@ -64,7 +69,7 @@ ISR(TWI_vect) {
 | 
			
		||||
        break;
 | 
			
		||||
    case TW_SR_DATA_ACK:
 | 
			
		||||
        // received data from master, call the receive callback
 | 
			
		||||
        if(i2c_slave_send_cb != NULL){
 | 
			
		||||
        if (i2c_slave_send_cb != NULL) {
 | 
			
		||||
            i2c_slave_recv_cb(TWDR);
 | 
			
		||||
        }
 | 
			
		||||
        TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
 | 
			
		||||
@@ -72,7 +77,7 @@ ISR(TWI_vect) {
 | 
			
		||||
    case TW_ST_SLA_ACK:
 | 
			
		||||
    case TW_ST_DATA_ACK:
 | 
			
		||||
        // master is requesting data, call the send callback
 | 
			
		||||
        if(i2c_slave_recv_cb != NULL) {
 | 
			
		||||
        if (i2c_slave_recv_cb != NULL) {
 | 
			
		||||
            TWDR = i2c_slave_send_cb();
 | 
			
		||||
        }
 | 
			
		||||
        TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
 | 
			
		||||
 
 | 
			
		||||
@@ -10,9 +10,9 @@
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
struct Gpio {
 | 
			
		||||
    volatile uint8_t * pin;
 | 
			
		||||
    volatile uint8_t * ddr;
 | 
			
		||||
    volatile uint8_t * port;
 | 
			
		||||
    volatile uint8_t *pin;
 | 
			
		||||
    volatile uint8_t *ddr;
 | 
			
		||||
    volatile uint8_t *port;
 | 
			
		||||
    uint8_t value;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -25,9 +25,9 @@ struct Gpio {
 | 
			
		||||
}
 | 
			
		||||
// clang-format on
 | 
			
		||||
 | 
			
		||||
bool gpio_get(struct Gpio * gpio);
 | 
			
		||||
void gpio_set(struct Gpio * gpio, bool value);
 | 
			
		||||
bool gpio_get_dir(struct Gpio * gpio);
 | 
			
		||||
void gpio_set_dir(struct Gpio * gpio, bool value);
 | 
			
		||||
bool gpio_get(struct Gpio *gpio);
 | 
			
		||||
void gpio_set(struct Gpio *gpio, bool value);
 | 
			
		||||
bool gpio_get_dir(struct Gpio *gpio);
 | 
			
		||||
void gpio_set_dir(struct Gpio *gpio, bool value);
 | 
			
		||||
 | 
			
		||||
#endif // _ARCH_GPIO_H
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,12 @@
 | 
			
		||||
#ifndef _ARCH_I2C_SLAVE_H
 | 
			
		||||
#define _ARCH_I2C_SLAVE_H
 | 
			
		||||
 | 
			
		||||
void i2c_slave_init(uint8_t address, void (*new_cb)(), void (*recv_cb)(uint8_t), uint8_t (*send_cb)());
 | 
			
		||||
void i2c_slave_init(
 | 
			
		||||
    uint8_t address,
 | 
			
		||||
    void (*new_cb)(),
 | 
			
		||||
    void (*recv_cb)(uint8_t),
 | 
			
		||||
    uint8_t (*send_cb)()
 | 
			
		||||
);
 | 
			
		||||
void i2c_slave_stop();
 | 
			
		||||
 | 
			
		||||
#endif // _ARCH_I2C_SLAVE_H
 | 
			
		||||
 
 | 
			
		||||
@@ -6,12 +6,12 @@
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
struct Uart {
 | 
			
		||||
    volatile uint8_t * a;
 | 
			
		||||
    volatile uint8_t * b;
 | 
			
		||||
    volatile uint8_t * c;
 | 
			
		||||
    volatile uint8_t * data;
 | 
			
		||||
    volatile uint8_t * baud_l;
 | 
			
		||||
    volatile uint8_t * baud_h;
 | 
			
		||||
    volatile uint8_t *a;
 | 
			
		||||
    volatile uint8_t *b;
 | 
			
		||||
    volatile uint8_t *c;
 | 
			
		||||
    volatile uint8_t *data;
 | 
			
		||||
    volatile uint8_t *baud_l;
 | 
			
		||||
    volatile uint8_t *baud_h;
 | 
			
		||||
    uint8_t a_read;
 | 
			
		||||
    uint8_t a_write;
 | 
			
		||||
    uint8_t a_init;
 | 
			
		||||
@@ -19,18 +19,18 @@ struct Uart {
 | 
			
		||||
    uint8_t c_init;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void uart_init(struct Uart * uart, uint32_t baud);
 | 
			
		||||
void uart_init(struct Uart *uart, uint32_t baud);
 | 
			
		||||
 | 
			
		||||
int16_t uart_count();
 | 
			
		||||
struct Uart * uart_new(int16_t num);
 | 
			
		||||
struct Uart *uart_new(int16_t num);
 | 
			
		||||
 | 
			
		||||
uint8_t uart_can_read(struct Uart * uart);
 | 
			
		||||
uint8_t uart_can_write(struct Uart * uart);
 | 
			
		||||
uint8_t uart_can_read(struct Uart *uart);
 | 
			
		||||
uint8_t uart_can_write(struct Uart *uart);
 | 
			
		||||
 | 
			
		||||
uint8_t uart_read(struct Uart * uart);
 | 
			
		||||
void uart_write(struct Uart * uart, uint8_t data);
 | 
			
		||||
uint8_t uart_read(struct Uart *uart);
 | 
			
		||||
void uart_write(struct Uart *uart, uint8_t data);
 | 
			
		||||
 | 
			
		||||
extern struct Uart * uart_stdio;
 | 
			
		||||
extern struct Uart *uart_stdio;
 | 
			
		||||
void uart_stdio_init(int16_t num, uint32_t baud);
 | 
			
		||||
 | 
			
		||||
#endif // _ARCH_UART_H
 | 
			
		||||
 
 | 
			
		||||
@@ -43,10 +43,10 @@
 | 
			
		||||
// clang-format on
 | 
			
		||||
 | 
			
		||||
int16_t uart_count() {
 | 
			
		||||
    return sizeof(UARTS)/sizeof(struct Uart);
 | 
			
		||||
    return sizeof(UARTS) / sizeof(struct Uart);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Uart * uart_new(int16_t num) {
 | 
			
		||||
struct Uart *uart_new(int16_t num) {
 | 
			
		||||
    if (num < uart_count()) {
 | 
			
		||||
        return &UARTS[num];
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -54,40 +54,40 @@ struct Uart * uart_new(int16_t num) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void uart_init(struct Uart * uart, uint32_t baud) {
 | 
			
		||||
void uart_init(struct Uart *uart, uint32_t baud) {
 | 
			
		||||
    uint32_t baud_prescale = (F_CPU / (baud * 16UL)) - 1;
 | 
			
		||||
    *(uart->baud_h) = (uint8_t)(baud_prescale>>8);
 | 
			
		||||
    *(uart->baud_h) = (uint8_t)(baud_prescale >> 8);
 | 
			
		||||
    *(uart->baud_l) = (uint8_t)(baud_prescale);
 | 
			
		||||
    *(uart->a) = uart->a_init;
 | 
			
		||||
    *(uart->b) = uart->b_init;
 | 
			
		||||
    *(uart->c) = uart->c_init;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t uart_can_read(struct Uart * uart) {
 | 
			
		||||
uint8_t uart_can_read(struct Uart *uart) {
 | 
			
		||||
    return (*(uart->a)) & uart->a_read;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t uart_read(struct Uart * uart) {
 | 
			
		||||
    while (!uart_can_read(uart)) ;
 | 
			
		||||
uint8_t uart_read(struct Uart *uart) {
 | 
			
		||||
    while (!uart_can_read(uart)) {}
 | 
			
		||||
    return *(uart->data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t uart_can_write(struct Uart * uart) {
 | 
			
		||||
uint8_t uart_can_write(struct Uart *uart) {
 | 
			
		||||
    return (*(uart->a)) & uart->a_write;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void uart_write(struct Uart * uart, uint8_t data) {
 | 
			
		||||
    while (!uart_can_write(uart)) ;
 | 
			
		||||
void uart_write(struct Uart *uart, uint8_t data) {
 | 
			
		||||
    while (!uart_can_write(uart)) {}
 | 
			
		||||
    *(uart->data) = data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Uart * uart_stdio = NULL;
 | 
			
		||||
struct Uart *uart_stdio = NULL;
 | 
			
		||||
 | 
			
		||||
int16_t uart_stdio_get(FILE * stream) {
 | 
			
		||||
int16_t uart_stdio_get(FILE *stream) {
 | 
			
		||||
    return (int16_t)uart_read(uart_stdio);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int16_t uart_stdio_put(char data, FILE * stream) {
 | 
			
		||||
int16_t uart_stdio_put(char data, FILE *stream) {
 | 
			
		||||
    uart_write(uart_stdio, (uint8_t)data);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -95,8 +95,8 @@ int16_t uart_stdio_put(char data, FILE * stream) {
 | 
			
		||||
FILE uart_stdio_file = FDEV_SETUP_STREAM(uart_stdio_put, uart_stdio_get, _FDEV_SETUP_RW);
 | 
			
		||||
 | 
			
		||||
void uart_stdio_init(int16_t num, uint32_t baud) {
 | 
			
		||||
    struct Uart * uart = uart_new(num);
 | 
			
		||||
    if(uart != NULL) {
 | 
			
		||||
    struct Uart *uart = uart_new(num);
 | 
			
		||||
    if (uart != NULL) {
 | 
			
		||||
        uart_init(uart, baud);
 | 
			
		||||
        uart_stdio = uart;
 | 
			
		||||
        stdin = stdout = stderr = &uart_stdio_file;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user