Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark pointers and pointed-to data as const #439

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/arch/avr/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

#include <arch/gpio.h>

bool gpio_get_dir(struct Gpio *gpio) {
bool gpio_get_dir(const struct Gpio *const gpio) {
if (*gpio->ddr & gpio->value) {
return true;
} else {
return false;
}
}

void gpio_set_dir(struct Gpio *gpio, bool value) {
void gpio_set_dir(struct Gpio *const gpio, bool value) {
if (value) {
*gpio->ddr |= gpio->value;
} else {
*gpio->ddr &= ~gpio->value;
}
}

bool gpio_get(struct Gpio *gpio) {
bool gpio_get(const struct Gpio *const gpio) {
if (*gpio->pin & gpio->value) {
return true;
} else {
return false;
}
}

void gpio_set(struct Gpio *gpio, bool value) {
void gpio_set(struct Gpio *const gpio, bool value) {
if (value) {
*gpio->port |= gpio->value;
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/arch/avr/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#define TIMEOUT (F_CPU / 1000)

int16_t i2c_start(struct I2C *i2c, uint8_t addr, bool read) {
int16_t i2c_start(struct I2C *const i2c, uint8_t addr, bool read) {
uint32_t count;

// reset TWI control register
Expand Down Expand Up @@ -47,12 +47,12 @@ int16_t i2c_start(struct I2C *i2c, uint8_t addr, bool read) {
return 0;
}

void i2c_stop(struct I2C *i2c) {
void i2c_stop(struct I2C *const 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 *const i2c, const uint8_t *const data, uint16_t length) {
uint16_t i;
for (i = 0; i < length; i++) {
// load data into data register
Expand All @@ -74,7 +74,7 @@ int16_t i2c_write(struct I2C *i2c, uint8_t *data, uint16_t length) {
return i;
}

int16_t i2c_read(struct I2C *i2c, uint8_t *data, uint16_t length) {
int16_t i2c_read(struct I2C *const i2c, uint8_t *const data, uint16_t length) {
uint16_t i;
for (i = 0; i < length; i++) {
if ((i + 1) < length) {
Expand Down
8 changes: 4 additions & 4 deletions src/arch/avr/include/arch/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(const struct Gpio *const gpio);
void gpio_set(struct Gpio *const gpio, bool value);
bool gpio_get_dir(const struct Gpio *const gpio);
void gpio_set_dir(struct Gpio *const gpio, bool value);

#endif // _ARCH_GPIO_H
10 changes: 5 additions & 5 deletions src/arch/avr/include/arch/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ struct Uart {
uint8_t c_init;
};

void uart_init(struct Uart *uart, uint32_t baud);
void uart_init(struct Uart *const uart, uint32_t baud);

int16_t uart_count(void);
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 *const uart);
uint8_t uart_can_write(struct Uart *const uart);

uint8_t uart_read(struct Uart *uart);
void uart_write(struct Uart *uart, uint8_t data);
uint8_t uart_read(struct Uart *const uart);
void uart_write(struct Uart *const uart, uint8_t data);

extern struct Uart *uart_stdio;
void uart_stdio_init(int16_t num, uint32_t baud);
Expand Down
10 changes: 5 additions & 5 deletions src/arch/avr/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct Uart *uart_new(int16_t num) {
}
}

void uart_init(struct Uart *uart, uint32_t baud) {
void uart_init(struct Uart *const uart, uint32_t baud) {
uint32_t baud_prescale = (F_CPU / (baud * 16UL)) - 1;
*(uart->baud_h) = (uint8_t)(baud_prescale >> 8);
*(uart->baud_l) = (uint8_t)(baud_prescale);
Expand All @@ -63,20 +63,20 @@ void uart_init(struct Uart *uart, uint32_t baud) {
*(uart->c) = uart->c_init;
}

uint8_t uart_can_read(struct Uart *uart) {
uint8_t uart_can_read(struct Uart *const uart) {
return (*(uart->a)) & uart->a_read;
}

uint8_t uart_read(struct Uart *uart) {
uint8_t uart_read(struct Uart *const uart) {
while (!uart_can_read(uart)) {}
return *(uart->data);
}

uint8_t uart_can_write(struct Uart *uart) {
uint8_t uart_can_write(struct Uart *const uart) {
return (*(uart->a)) & uart->a_write;
}

void uart_write(struct Uart *uart, uint8_t data) {
void uart_write(struct Uart *const uart, uint8_t data) {
while (!uart_can_write(uart)) {}
*(uart->data) = data;
}
Expand Down
2 changes: 1 addition & 1 deletion src/board/arduino/mega2560/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <common/i2c.h>

int16_t smbus_read(uint8_t address, uint8_t command, uint16_t *data) {
int16_t smbus_read(uint8_t address, uint8_t command, uint16_t *const data) {
return i2c_get(NULL, address, command, (uint8_t *)data, 2);
}

Expand Down
47 changes: 31 additions & 16 deletions src/board/arduino/mega2560/parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static struct Parallel PORT = {
// clang-format on

// Set port to all high-impedance inputs
void parallel_hiz(struct Parallel *port) {
void parallel_hiz(struct Parallel *const port) {
#define PIN(N, P) \
gpio_set_dir(port->N, false); \
gpio_set(port->N, false);
Expand All @@ -126,15 +126,15 @@ void parallel_hiz(struct Parallel *port) {
}

// Place all data lines in high or low impendance state
void parallel_data_dir(struct Parallel *port, bool dir) {
void parallel_data_dir(struct Parallel *const port, bool dir) {
#define DATA_BIT(B) gpio_set_dir(port->d##B, dir);
DATA_BITS
#undef DATA_BIT
}
#define parallel_data_forward(P) parallel_data_dir(P, true)
#define parallel_data_reverse(P) parallel_data_dir(P, false)

void parallel_data_set_high(struct Parallel *port, uint8_t byte) {
void parallel_data_set_high(struct Parallel *const port, uint8_t byte) {
// By convention all lines are high, so only set the ones needed
#define DATA_BIT(B) \
if (!(byte & (1 << B))) \
Expand All @@ -144,7 +144,7 @@ void parallel_data_set_high(struct Parallel *port, uint8_t byte) {
}

// Set port to initial state required before being able to perform cycles
void parallel_reset(struct Parallel *port, bool host) {
void parallel_reset(struct Parallel *const port, bool host) {
parallel_hiz(port);

// nRESET: output on host, input on peripherals
Expand Down Expand Up @@ -183,7 +183,7 @@ void parallel_reset(struct Parallel *port, bool host) {
}
}

void parallel_state(struct Parallel *port, enum ParallelState state) {
void parallel_state(struct Parallel *const port, enum ParallelState state) {
if (port->state != state) {
switch (state) {
case PARALLEL_STATE_UNKNOWN:
Expand All @@ -202,7 +202,7 @@ void parallel_state(struct Parallel *port, enum ParallelState state) {
}
}

uint8_t parallel_read_data(struct Parallel *port) {
uint8_t parallel_read_data(struct Parallel *const port) {
uint8_t byte = 0;
#define DATA_BIT(B) \
if (gpio_get(port->d##B)) \
Expand All @@ -212,7 +212,7 @@ uint8_t parallel_read_data(struct Parallel *port) {
return byte;
}

void parallel_write_data(struct Parallel *port, uint8_t byte) {
void parallel_write_data(struct Parallel *const port, uint8_t byte) {
// By convention all lines are high, so only set the ones needed

#define DATA_BIT(B) \
Expand All @@ -224,8 +224,8 @@ void parallel_write_data(struct Parallel *port, uint8_t byte) {

//TODO: timeout
int16_t parallel_transaction(
struct Parallel *port,
uint8_t *data,
struct Parallel *const port,
uint8_t *const data,
int16_t length,
bool read,
bool addr
Expand Down Expand Up @@ -299,7 +299,12 @@ int16_t parallel_transaction(

// host write -> peripheral read
// host read -> peripheral write
bool parallel_peripheral_cycle(struct Parallel *port, uint8_t *data, bool *read, bool *addr) {
bool parallel_peripheral_cycle(
struct Parallel *const port,
uint8_t *const data,
bool *const read,
bool *const addr
) {
if (!gpio_get(port->reset_n)) {
// XXX: Give host some time to get ready
_delay_ms(1);
Expand Down Expand Up @@ -351,7 +356,12 @@ static uint8_t ZERO = 0x00;
static uint8_t SPI_ENABLE = 0xFE;
static uint8_t SPI_DATA = 0xFD;

int16_t parallel_ecms_read(struct Parallel *port, uint16_t addr, uint8_t *data, int16_t length) {
int16_t parallel_ecms_read(
struct Parallel *const port,
uint16_t addr,
uint8_t *const data,
int16_t length
) {
int16_t res;

res = parallel_set_address(port, &ADDRESS_ECMSADDR1, 1);
Expand All @@ -378,7 +388,7 @@ int16_t parallel_ecms_read(struct Parallel *port, uint16_t addr, uint8_t *data,
}

// Disable chip
int16_t parallel_spi_reset(struct Parallel *port) {
int16_t parallel_spi_reset(struct Parallel *const port) {
int16_t res;

res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
Expand All @@ -397,7 +407,12 @@ int16_t parallel_spi_reset(struct Parallel *port) {
}

// Enable chip and read or write data
int16_t parallel_spi_transaction(struct Parallel *port, uint8_t *data, int16_t length, bool read) {
int16_t parallel_spi_transaction(
struct Parallel *const port,
uint8_t *const data,
int16_t length,
bool read
) {
int16_t res;

res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
Expand All @@ -420,8 +435,8 @@ int16_t parallel_spi_transaction(struct Parallel *port, uint8_t *data, int16_t l

// "Hardware" accelerated SPI programming, requires ECINDARs to be set
int16_t parallel_spi_program(
struct Parallel *port,
uint8_t *data,
struct Parallel *const port,
const uint8_t *const data,
int16_t length,
bool initialized
) {
Expand Down Expand Up @@ -483,7 +498,7 @@ int16_t parallel_spi_program(
return i;
}

int16_t serial_transaction(uint8_t *data, int16_t length, bool read) {
int16_t serial_transaction(uint8_t *const data, int16_t length, bool read) {
int16_t i;
for (i = 0; i < length; i++) {
if (read) {
Expand Down
Loading
Loading