aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.clang-format4
-rw-r--r--src/arch/stm32l4xxx/peripherals/clock.c19
-rw-r--r--src/arch/stm32l4xxx/peripherals/irq.c44
-rw-r--r--src/arch/stm32l4xxx/peripherals/usart.c19
-rw-r--r--src/kern/dma/dma_manager.c84
-rw-r--r--src/kern/gpio/gpio_manager.c156
-rw-r--r--src/kern/gpio/sysled.c4
-rw-r--r--src/kern/init.c16
-rw-r--r--src/kern/lib.c12
-rw-r--r--src/kern/log.c9
-rw-r--r--src/kern/main.c7
-rw-r--r--src/kern/mem.c98
-rw-r--r--src/kern/stdlibrepl.c2
-rw-r--r--src/kern/string.c4
-rw-r--r--test_harness/fake_env.c35
-rw-r--r--test_harness/test_harness.c26
-rw-r--r--tests/test_dma.c60
-rw-r--r--tests/test_gpio.c17
-rw-r--r--tests/test_irq.c7
-rw-r--r--tests/test_lib.c2
-rw-r--r--tests/test_memory.c61
-rw-r--r--tests/test_spi.c3
-rw-r--r--tests/test_usart.c6
23 files changed, 324 insertions, 371 deletions
diff --git a/.clang-format b/.clang-format
index 6c15953..567ff11 100644
--- a/.clang-format
+++ b/.clang-format
@@ -5,3 +5,7 @@ BraceWrapping:
AfterFunction: true
AllowShortFunctionsOnASingleLine: InlineOnly
AlignAfterOpenBracket: AlwaysBreak
+BinPackArguments: false
+BinPackParameters: false
+ExperimentalAutoDetectBinPacking: false
+AllowAllParametersOfDeclarationOnNextLine: true
diff --git a/src/arch/stm32l4xxx/peripherals/clock.c b/src/arch/stm32l4xxx/peripherals/clock.c
index 9051572..32cd00c 100644
--- a/src/arch/stm32l4xxx/peripherals/clock.c
+++ b/src/arch/stm32l4xxx/peripherals/clock.c
@@ -3,14 +3,13 @@
*/
#include "arch/stm32l4xxx/peripherals/clock.h"
-#include "arch/stm32l4xxx/peripherals/flash.h"
-
-#include "kern/init.h"
#include <stdint.h>
-#define TIMEOUT 10000
+#include "arch/stm32l4xxx/peripherals/flash.h"
+#include "kern/init.h"
+#define TIMEOUT 10000
int pll_off()
{
@@ -43,7 +42,8 @@ int pll_on()
}
int configure_pll(
- uint8_t pllp_div_factor, pll_divisor_t pllr, /* System clock divisor. */
+ uint8_t pllp_div_factor,
+ pll_divisor_t pllr, /* System clock divisor. */
pll_divisor_t pllq, /* Divison factor for PLL48M1CLK. */
pllp_divisor_t pllp, /* Divison factor for PLLSAI2CLK. */
uint8_t plln, /* PLL numerator. */
@@ -89,12 +89,15 @@ int set_system_clock_MHz(uint8_t mhz)
pll_off();
configure_pll(
- 0 /* pllp_div_factor */, PLL_DIVISOR_4 /* pllr: VCO / 4 = mhz MHz. */,
- PLL_DIVISOR_4 /* pllq: VCO / 4 = mhz MHz */, PLLP_DIVISOR_7 /* pllp */,
+ 0 /* pllp_div_factor */,
+ PLL_DIVISOR_4 /* pllr: VCO / 4 = mhz MHz. */,
+ PLL_DIVISOR_4 /* pllq: VCO / 4 = mhz MHz */,
+ PLLP_DIVISOR_7 /* pllp */,
/* The following set the frequency of VCO to (mhz*4)MHz: mhz * 1 * 4MHz.
*/
- mhz /* plln | mhz */, PLLM_DIVISOR_1 /* pllm | 01 */,
+ mhz /* plln | mhz */,
+ PLLM_DIVISOR_1 /* pllm | 01 */,
PLL_SRC_MSI /* pll src | 04 Mhz */);
pll_on();
diff --git a/src/arch/stm32l4xxx/peripherals/irq.c b/src/arch/stm32l4xxx/peripherals/irq.c
index 364b9a7..7870a10 100644
--- a/src/arch/stm32l4xxx/peripherals/irq.c
+++ b/src/arch/stm32l4xxx/peripherals/irq.c
@@ -1,16 +1,14 @@
#include "arch/stm32l4xxx/peripherals/irq.h"
-#include "arch/stm32l4xxx/peripherals/gpio.h"
-#include "arch/stm32l4xxx/peripherals/nvic.h"
#include "arch.h"
+#include "arch/stm32l4xxx/peripherals/gpio.h"
+#include "arch/stm32l4xxx/peripherals/nvic.h"
#include "kern/delay.h"
#include "kern/gpio/gpio_manager.h"
#define IRQ_RESERVED(n)
#define IRQ(name, uname_, n) \
- void WEAK name () { \
- unhandled_isr(n); \
- }
+ void WEAK name() { unhandled_isr(n); }
#include "arch/stm32l4xxx/peripherals/isrs.inc"
#undef IRQ_RESERVED
#undef IRQ
@@ -54,28 +52,28 @@ void unhandled_isr(uint8_t number)
gpio_reserved_pin_t pin3 = reserve_gpio_pin(GPIO_PIN_PB3, &opts, &ec);
for (;;) {
- for (int i = 0; i < 20; ++ i) {
- set_gpio_pin_high(pin3);
- delay(1000000);
- set_gpio_pin_low(pin3);
- delay(1000000);
+ for (int i = 0; i < 20; ++i) {
+ set_gpio_pin_high(pin3);
+ delay(1000000);
+ set_gpio_pin_low(pin3);
+ delay(1000000);
}
delay(50000000);
int n = number;
- for (int i = 0; i < 8; ++ i) {
+ for (int i = 0; i < 8; ++i) {
if (n & 1) {
- // LSB is a 1
- set_gpio_pin_high(pin3);
- delay(15000000);
- set_gpio_pin_low(pin3);
- delay(15000000);
+ // LSB is a 1
+ set_gpio_pin_high(pin3);
+ delay(15000000);
+ set_gpio_pin_low(pin3);
+ delay(15000000);
} else {
- // LSB is a 0
- set_gpio_pin_high(pin3);
- delay(1000000);
- set_gpio_pin_low(pin3);
- delay(29000000);
+ // LSB is a 0
+ set_gpio_pin_high(pin3);
+ delay(1000000);
+ set_gpio_pin_low(pin3);
+ delay(29000000);
}
n >>= 1;
@@ -85,12 +83,12 @@ void unhandled_isr(uint8_t number)
void enable_interrupts(interrupt_set_t* interrupts)
{
- for (int i = 0; i < sizeof(NVIC.ise_r) / sizeof(uint32_t); ++ i)
+ for (int i = 0; i < sizeof(NVIC.ise_r) / sizeof(uint32_t); ++i)
NVIC.ise_r[i] = interrupts->irqs[i];
}
void disable_interrupts(interrupt_set_t* interrupts)
{
- for (int i = 0; i < sizeof(NVIC.ise_r) / sizeof(uint32_t); ++ i)
+ for (int i = 0; i < sizeof(NVIC.ise_r) / sizeof(uint32_t); ++i)
NVIC.ice_r[i] = interrupts->irqs[i];
}
diff --git a/src/arch/stm32l4xxx/peripherals/usart.c b/src/arch/stm32l4xxx/peripherals/usart.c
index 7309b48..c682671 100644
--- a/src/arch/stm32l4xxx/peripherals/usart.c
+++ b/src/arch/stm32l4xxx/peripherals/usart.c
@@ -1,7 +1,9 @@
#include "arch/stm32l4xxx/peripherals/usart.h"
+
+#include <stdarg.h>
+
#include "kern/delay.h"
#include "kern/lib.h"
-#include <stdarg.h>
void set_usart1_clock_src(__IO rcc_t* rcc, usart_clk_src_t usart_clk_src)
{
@@ -62,10 +64,11 @@ void usart_transmit_byte_sync(__IO usart_t* usart, uint8_t byte)
;
}
-void usart_transmit_bytes_sync(__IO usart_t* usart, const uint8_t* bytes, uint32_t n)
+void usart_transmit_bytes_sync(
+ __IO usart_t* usart, const uint8_t* bytes, uint32_t n)
{
- while (n --) {
- usart_transmit_byte_sync(usart, *(bytes ++));
+ while (n--) {
+ usart_transmit_byte_sync(usart, *(bytes++));
}
}
@@ -75,13 +78,13 @@ void usart_transmit_str_sync(__IO usart_t* usart, const char* str)
if (*str == '\n') {
usart_transmit_byte_sync(usart, '\r');
}
- usart_transmit_byte_sync(usart, *(str ++));
+ usart_transmit_byte_sync(usart, *(str++));
}
}
void usart_enable_dma(__IO usart_t* usart, usart_enable_t enabled)
{
- switch(enabled) {
+ switch (enabled) {
case USART_ENABLE_DISABLED:
regset(usart->c_r3, usart_dmar, 0);
regset(usart->c_r3, usart_dmat, 0);
@@ -130,12 +133,12 @@ void usart_vprintf(__IO usart_t* usart, const char* fmt, va_list l)
b.str = va_arg(l, char*);
usart_transmit_str_sync(usart, b.str);
}
- ++ fmt;
+ ++fmt;
} else {
if (*fmt == '\n') {
usart_transmit_byte_sync(usart, '\r');
}
- usart_transmit_byte_sync(usart, *(fmt ++));
+ usart_transmit_byte_sync(usart, *(fmt++));
}
}
diff --git a/src/kern/dma/dma_manager.c b/src/kern/dma/dma_manager.c
index 00e9f3d..9ffa795 100644
--- a/src/kern/dma/dma_manager.c
+++ b/src/kern/dma/dma_manager.c
@@ -1,8 +1,8 @@
#include "kern/dma/dma_manager.h"
+
#include "arch/stm32l4xxx/peripherals/dma.h"
-#include "arch/stm32l4xxx/peripherals/usart.h"
#include "arch/stm32l4xxx/peripherals/rcc.h"
-
+#include "arch/stm32l4xxx/peripherals/usart.h"
/* Bitmask of DMA2 channels in use. */
static uint8_t dma_inuse[2];
@@ -29,7 +29,9 @@ static dma_channel_config_t* get_raw_channel_config(dma_channel_t chan)
static uint32_t get_periph_location(dma_peripheral_t operipheral)
{
-#define CASE(p, n) case p: return ptr2reg(n);
+#define CASE(p, n) \
+ case p: \
+ return ptr2reg(n);
switch (operipheral) {
CASE(DMA1_PERIPH_USART1_RX, &USART1.rd_r)
CASE(DMA1_PERIPH_USART1_TX, &USART1.td_r)
@@ -53,10 +55,7 @@ static dma_channel_t allocate_dma_channel(
int chan = peripheral % DMA_N_CHANNELS;
*modesel = peripheral / 7;
- return (dma_channel_t) {
- .dma = dmasel,
- .chan = chan
- };
+ return (dma_channel_t){.dma = dmasel, .chan = chan};
}
/*
@@ -66,11 +65,9 @@ static dma_channel_t allocate_dma_channel(
* Returns 0 if this function was unable to reserve
* the channel.
*/
-static int try_reserve_dma_channel(
- dma_channel_t chan)
+static int try_reserve_dma_channel(dma_channel_t chan)
{
- int in_use = __sync_fetch_and_or(
- &dma_inuse[chan.dma], 1 << chan.chan);
+ int in_use = __sync_fetch_and_or(&dma_inuse[chan.dma], 1 << chan.chan);
return !(in_use & (1 << chan.chan));
}
@@ -108,21 +105,20 @@ void configure_dma_channel(
dma_t* dma = get_raw_dma(chan);
regset(dma->csel_r, 0xF << (4 * chan.chan), selmode);
- dma_channel_config_t* config =
- &dma->channel_config[chan.chan];
+ dma_channel_config_t* config = &dma->channel_config[chan.chan];
uint32_t reg = 0;
- regset(reg, dma_cc_dir, dir);
- regset(reg, dma_cc_tcie, opts->transfer_complete_interrupt_enable);
- regset(reg, dma_cc_htie, opts->half_transfer_interrupt_enable);
- regset(reg, dma_cc_teie, opts->transfer_error_interrupt_enable);
- regset(reg, dma_cc_circ, opts->circular_mode);
- regset(reg, dma_cc_pinc, opts->peripheral_increment);
- regset(reg, dma_cc_minc, opts->memory_increment);
- regset(reg, dma_cc_psize, opts->peripheral_block_size);
- regset(reg, dma_cc_msize, opts->memory_block_size);
- regset(reg, dma_cc_pl, opts->priority);
+ regset(reg, dma_cc_dir, dir);
+ regset(reg, dma_cc_tcie, opts->transfer_complete_interrupt_enable);
+ regset(reg, dma_cc_htie, opts->half_transfer_interrupt_enable);
+ regset(reg, dma_cc_teie, opts->transfer_error_interrupt_enable);
+ regset(reg, dma_cc_circ, opts->circular_mode);
+ regset(reg, dma_cc_pinc, opts->peripheral_increment);
+ regset(reg, dma_cc_minc, opts->memory_increment);
+ regset(reg, dma_cc_psize, opts->peripheral_block_size);
+ regset(reg, dma_cc_msize, opts->memory_block_size);
+ regset(reg, dma_cc_pl, opts->priority);
regset(reg, dma_cc_mem2mem, mem2mem);
config->cc_r = reg;
@@ -132,12 +128,9 @@ void configure_dma_channel(
}
dma_mem2mem_channel_t select_dma_channel_mem2mem(
- int channel,
- dma_opts_t* opts,
- int* error_out)
+ int channel, dma_opts_t* opts, int* error_out)
{
-
-#define WRAP(c) ((dma_mem2mem_channel_t) { .c_ = c })
+#define WRAP(c) ((dma_mem2mem_channel_t){.c_ = c})
// TODO this should probably be in a critical section.
dma_channel_t chan;
if (channel == -1) {
@@ -189,16 +182,13 @@ dma_mem2mem_channel_t select_dma_channel_mem2mem(
}
dma_mem2p_channel_t select_dma_channel_mem2p(
- dma_peripheral_t peripheral,
- dma_opts_t* opts_in,
- int* error_out)
+ dma_peripheral_t peripheral, dma_opts_t* opts_in, int* error_out)
{
-#define WRAP(c) ((dma_mem2p_channel_t) { .c_ = c })
+#define WRAP(c) ((dma_mem2p_channel_t){.c_ = c})
*error_out = 0;
int modesel;
- dma_channel_t ret =
- allocate_dma_channel(peripheral, &modesel);
+ dma_channel_t ret = allocate_dma_channel(peripheral, &modesel);
if (!try_reserve_dma_channel(ret)) {
*error_out = DMA_ERROR_CHANNEL_IN_USE;
@@ -217,23 +207,20 @@ dma_mem2p_channel_t select_dma_channel_mem2p(
if (*error_out) {
return WRAP(DMA_CHAN_ERROR);
}
-
+
*error_out = 0;
return WRAP(ret);
#undef WRAP
}
dma_p2mem_channel_t select_dma_channel_p2mem(
- dma_peripheral_t peripheral,
- dma_opts_t* opts_in,
- int* error_out)
+ dma_peripheral_t peripheral, dma_opts_t* opts_in, int* error_out)
{
-#define WRAP(c) ((dma_p2mem_channel_t) { .c_ = c })
+#define WRAP(c) ((dma_p2mem_channel_t){.c_ = c})
*error_out = 0;
int modesel;
- dma_channel_t ret =
- allocate_dma_channel(peripheral, &modesel);
+ dma_channel_t ret = allocate_dma_channel(peripheral, &modesel);
if (!try_reserve_dma_channel(ret)) {
*error_out = DMA_ERROR_CHANNEL_IN_USE;
@@ -252,13 +239,12 @@ dma_p2mem_channel_t select_dma_channel_p2mem(
if (*error_out) {
return WRAP(DMA_CHAN_ERROR);
}
-
+
*error_out = 0;
return WRAP(ret);
#undef WRAP
}
-
void dma_mem2p_initiate_transfer(
dma_mem2p_channel_t chan, const void* from_loc, uint16_t nblocks)
{
@@ -300,12 +286,12 @@ interrupt_t dma_channel_get_interrupt(dma_channel_t chan)
return IRQ_DMA1_CHANNEL1_IRQ + chan.chan;
} else {
switch (chan.chan) {
- case 5:
- return IRQ_DMA1_CHANNEL6_IRQ;
- case 6:
- return IRQ_DMA1_CHANNEL7_IRQ;
- default:
- return IRQ_DMA2_CHANNEL1_IRQ + chan.chan;
+ case 5:
+ return IRQ_DMA1_CHANNEL6_IRQ;
+ case 6:
+ return IRQ_DMA1_CHANNEL7_IRQ;
+ default:
+ return IRQ_DMA2_CHANNEL1_IRQ + chan.chan;
}
}
}
diff --git a/src/kern/gpio/gpio_manager.c b/src/kern/gpio/gpio_manager.c
index 82dd0ba..749ae79 100644
--- a/src/kern/gpio/gpio_manager.c
+++ b/src/kern/gpio/gpio_manager.c
@@ -17,7 +17,7 @@ struct gpio_afn_and_pin {
*
* This function will use afn_number = -1 as the terminal.
*
- * Note that EVENTOUT is a special case because all pins have an event out
+ * Note that EVENTOUT is a special case because all pins have an event out
* at afn=15 and should be assumed by other logic and thus is not handled
* by this function.
*/
@@ -25,42 +25,38 @@ static void get_ports_and_pins_for_alternate_function(
gpio_alternate_function_t afn, struct gpio_afn_and_pin* out)
{
switch (afn) {
-#define AFN1(fn, ...) \
- static_assert(false, "Unable to parse afn_table at " #fn);
-#define AFN3(fn, ...) \
- static_assert(false, "Unable to parse afn_table at " #fn);
-#define AFN5(fn, ...) \
- static_assert(false, "Unable to parse afn_table at " #fn);
-#define AFN7(fn, ...) \
- static_assert(false, "Unable to parse afn_table at " #fn);
+#define AFN1(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn);
+#define AFN3(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn);
+#define AFN5(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn);
+#define AFN7(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn);
#define AFN2(fn, afn, pin) \
- out[0].afn_number = afn; \
- out[0].gpio_pin = GPIO_PIN_ ## pin
+ out[0].afn_number = afn; \
+ out[0].gpio_pin = GPIO_PIN_##pin
#define AFN4(fn, afn0, pin0, afn1, pin1) \
- AFN2(fn, afn0, pin0); \
- out[1].afn_number = afn1; \
- out[1].gpio_pin = GPIO_PIN_ ## pin1
+ AFN2(fn, afn0, pin0); \
+ out[1].afn_number = afn1; \
+ out[1].gpio_pin = GPIO_PIN_##pin1
#define AFN6(fn, afn0, pin0, afn1, pin1, afn2, pin2) \
- AFN4(fn, afn0, pin0, afn1, pin1); \
- out[2].afn_number = afn2; \
- out[2].gpio_pin = GPIO_PIN_ ## pin2
+ AFN4(fn, afn0, pin0, afn1, pin1); \
+ out[2].afn_number = afn2; \
+ out[2].gpio_pin = GPIO_PIN_##pin2
#define AFN8(fn, afn0, pin0, afn1, pin1, afn2, pin2, afn3, pin3) \
- AFN6(fn, afn0, pin0, afn1, pin1, afn2, pin2); \
- out[2].afn_number = afn3; \
- out[2].gpio_pin = GPIO_PIN_ ## pin3
+ AFN6(fn, afn0, pin0, afn1, pin1, afn2, pin2); \
+ out[2].afn_number = afn3; \
+ out[2].gpio_pin = GPIO_PIN_##pin3
#define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define GET_N(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
-#define AFN(fn, ...) \
- case GPIO_ALTERNATE_FUNCTION_ ## fn: \
- GET_MACRO(__VA_ARGS__, AFN8, AFN7, AFN6, AFN5, AFN4, AFN3, AFN2, AFN1)\
- (fn, __VA_ARGS__); \
- out[GET_N(__VA_ARGS__, 4, 4, 3, 3, 2, 2, 1, 1)] = \
- (struct gpio_afn_and_pin){-1, -1}; \
+#define AFN(fn, ...) \
+ case GPIO_ALTERNATE_FUNCTION_##fn: \
+ GET_MACRO(__VA_ARGS__, AFN8, AFN7, AFN6, AFN5, AFN4, AFN3, AFN2, AFN1) \
+ (fn, __VA_ARGS__); \
+ out[GET_N(__VA_ARGS__, 4, 4, 3, 3, 2, 2, 1, 1)] = \
+ (struct gpio_afn_and_pin){-1, -1}; \
break;
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/afn_table.inc"
@@ -72,11 +68,13 @@ static void get_ports_and_pins_for_alternate_function(
static inline int offset_for_gpio_pin(gpio_pin_t pin)
{
switch (pin) {
-#define PORT(p, pn) \
- case GPIO_PIN_P ## p ## pn: return pn;
+#define PORT(p, pn) \
+ case GPIO_PIN_P##p##pn: \
+ return pn;
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
#undef PORT
- case N_GPIO_PINS: return -1;
+ case N_GPIO_PINS:
+ return -1;
}
/* Should be unreachable. */
@@ -98,16 +96,14 @@ inline bool gpio_pin_in_use(gpio_pin_t pin)
#define H(...)
#define I(...)
#define SELECT_MACRO(PORT) PORT
-#define PORT(port, pin) \
- SELECT_MACRO(port)(GPIO_PIN_P ## port ## pin, pin)
+#define PORT(port, pin) SELECT_MACRO(port)(GPIO_PIN_P##port##pin, pin)
static int gc_port_a()
{
return 0
#undef A
-#define A(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define A(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef A
#define A(...)
}
@@ -116,10 +112,9 @@ static int gc_port_b()
{
return 0
#undef B
-#define B(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define B(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef B
#define B(...)
}
@@ -128,10 +123,9 @@ static int gc_port_c()
{
return 0
#undef C
-#define C(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define C(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef C
#define C(...)
}
@@ -140,10 +134,9 @@ static int gc_port_d()
{
return 0
#undef D
-#define D(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define D(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef D
#define D(...)
}
@@ -152,10 +145,9 @@ static int gc_port_e()
{
return 0
#undef E
-#define E(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define E(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef E
#define E(...)
}
@@ -164,10 +156,9 @@ static int gc_port_f()
{
return 0
#undef F
-#define F(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define F(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef F
#define F(...)
}
@@ -176,10 +167,9 @@ static int gc_port_g()
{
return 0
#undef G
-#define G(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define G(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef G
#define G(...)
}
@@ -188,10 +178,9 @@ static int gc_port_h()
{
return 0
#undef H
-#define H(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define H(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef H
#define H(...)
}
@@ -200,30 +189,32 @@ static int gc_port_i()
{
return 0
#undef I
-#define I(abspin, relpin) \
- | (gpio_pin_in_use(abspin) << (relpin))
+#define I(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin))
#include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc"
- ;
+ ;
#undef I
#define I(...)
}
-
static inline bool gpio_pin_try_reserve(gpio_pin_t pin)
{
- int in_use = __sync_fetch_and_or(
- &pins_inuse[pin / 32], 1 << (pin % 32));
+ int in_use = __sync_fetch_and_or(&pins_inuse[pin / 32], 1 << (pin % 32));
return !(in_use & (1 << (pin % 32)));
}
inline static gpio_port_config_t* get_gpio_port_config(gpio_port_t port)
{
- switch(port) {
- case GPIO_PORT_A: return (gpio_port_config_t*) GPIOA_BASE;
- case GPIO_PORT_B: return (gpio_port_config_t*) GPIOB_BASE;
- case GPIO_PORT_C: return (gpio_port_config_t*) GPIOC_BASE;
- case GPIO_PORT_H: return (gpio_port_config_t*) GPIOH_BASE;
- default: return NULL;
+ switch (port) {
+ case GPIO_PORT_A:
+ return (gpio_port_config_t*)GPIOA_BASE;
+ case GPIO_PORT_B:
+ return (gpio_port_config_t*)GPIOB_BASE;
+ case GPIO_PORT_C:
+ return (gpio_port_config_t*)GPIOC_BASE;
+ case GPIO_PORT_H:
+ return (gpio_port_config_t*)GPIOH_BASE;
+ default:
+ return NULL;
}
}
@@ -239,7 +230,7 @@ gpio_reserved_pin_t reserve_gpio_pin(
*error_out = 0;
if (!gpio_pin_try_reserve(pin)) {
*error_out = GPIO_ERROR_IN_USE;
- return (gpio_reserved_pin_t) { .v_ = -1 };
+ return (gpio_reserved_pin_t){.v_ = -1};
}
gpio_port_t port = get_port_for_pin(pin);
@@ -247,18 +238,18 @@ gpio_reserved_pin_t reserve_gpio_pin(
gpio_port_config_t* port_config = get_gpio_port_config(port);
-
int off = offset_for_gpio_pin(pin);
regset(port_config->mode_r, gpio_mode_n(off), opts->mode);
regset(port_config->pupd_r, gpio_pupd_n(off), opts->pull_dir);
- switch(opts->mode) {
+ switch (opts->mode) {
case GPIO_MODE_INPUT:
break;
case GPIO_MODE_OUTPUT:
- regset(port_config->ospeed_r, gpio_ospeed_n(off), opts->output_opts.speed);
+ regset(
+ port_config->ospeed_r, gpio_ospeed_n(off), opts->output_opts.speed);
regset(port_config->otype_r, gpio_otype_n(off), opts->output_opts.type);
break;
@@ -281,13 +272,11 @@ gpio_reserved_pin_t reserve_gpio_pin(
break;
}
- return (gpio_reserved_pin_t) { .v_ = pin };
+ return (gpio_reserved_pin_t){.v_ = pin};
}
gpio_reserved_pin_t gpio_enable_alternate_function(
- gpio_alternate_function_t fn,
- gpio_pin_t hint,
- int* error_out)
+ gpio_alternate_function_t fn, gpio_pin_t hint, int* error_out)
{
int i = 0;
gpio_pin_opts_t opts;
@@ -295,7 +284,7 @@ gpio_reserved_pin_t gpio_enable_alternate_function(
if (gpio_pin_out_of_range(hint) && hint != -1) {
*error_out = GPIO_ERROR_INVALID_PIN;
- return (gpio_reserved_pin_t) { .v_ = -1 };
+ return (gpio_reserved_pin_t){.v_ = -1};
}
opts.mode = GPIO_MODE_ALTERNATE;
@@ -307,21 +296,20 @@ gpio_reserved_pin_t gpio_enable_alternate_function(
}
afn_and_pin[i].gpio_pin = hint;
} else {
- get_ports_and_pins_for_alternate_function(fn, afn_and_pin);
+ get_ports_and_pins_for_alternate_function(fn, afn_and_pin);
if (hint == -1) {
hint = afn_and_pin[0].gpio_pin;
}
- for(i = 0;
- i < 5
- && afn_and_pin[i].gpio_pin != hint
- && afn_and_pin[i].gpio_pin != -1;
- ++ i);
+ for (i = 0; i < 5 && afn_and_pin[i].gpio_pin != hint &&
+ afn_and_pin[i].gpio_pin != -1;
+ ++i)
+ ;
if (afn_and_pin[i].gpio_pin == -1 || i == 5) {
*error_out = GPIO_ERROR_INVALID_PIN_FOR_ALTERNATE_FUNCTION;
- return (gpio_reserved_pin_t) { .v_ = -1 };
+ return (gpio_reserved_pin_t){.v_ = -1};
}
}
@@ -337,7 +325,7 @@ void release_gpio_pin(gpio_reserved_pin_t rpin)
pins_inuse[pin / 32] &= ~(1 << (pin % 32));
int used;
- switch(port) {
+ switch (port) {
case GPIO_PORT_A:
used = gc_port_a();
break;
diff --git a/src/kern/gpio/sysled.c b/src/kern/gpio/sysled.c
index a728da3..d3e5f30 100644
--- a/src/kern/gpio/sysled.c
+++ b/src/kern/gpio/sysled.c
@@ -5,9 +5,9 @@
gpio_reserved_pin_t get_sysled()
{
if (gpio_pin_in_use(SYSLED)) {
- return (gpio_reserved_pin_t) { .v_ = SYSLED };
+ return (gpio_reserved_pin_t){.v_ = SYSLED};
}
-
+
int ec;
gpio_pin_opts_t opts = DEFAULT_GPIO_OPTS_OUTPUT;
return reserve_gpio_pin(SYSLED, &opts, &ec);
diff --git a/src/kern/init.c b/src/kern/init.c
index 2531ca9..8f2d7e8 100644
--- a/src/kern/init.c
+++ b/src/kern/init.c
@@ -1,9 +1,8 @@
#include "kern/init.h"
#include "arch.h"
-#include "arch/stm32l4xxx/peripherals/system.h"
#include "arch/stm32l4xxx/peripherals/clock.h"
-
+#include "arch/stm32l4xxx/peripherals/system.h"
#include "kern/log.h"
/* Forward-declare the main function. This is implemented in main.c. */
@@ -18,8 +17,8 @@ extern uint32_t DATA_SEGMENT_STOP;
extern uint32_t BSS_START;
extern uint32_t BSS_END;
-extern void(*INIT_ROUTINES_FLASH_START)();
-extern void(*INIT_ROUTINES_FLASH_STOP)();
+extern void (*INIT_ROUTINES_FLASH_START)();
+extern void (*INIT_ROUTINES_FLASH_STOP)();
init0()
{
@@ -66,17 +65,16 @@ init3()
_Noreturn void on_reset()
{
void (**initfn)();
- for(initfn = &INIT_ROUTINES_FLASH_START;
- initfn < &INIT_ROUTINES_FLASH_STOP;
- ++ initfn) {
-
+ for (initfn = &INIT_ROUTINES_FLASH_START; initfn < &INIT_ROUTINES_FLASH_STOP;
+ ++initfn) {
(*initfn)();
}
/* Jump to main. */
main();
- for(;;);
+ for (;;)
+ ;
}
#endif /* ARCH_STM32L4 */
diff --git a/src/kern/lib.c b/src/kern/lib.c
index 88188cc..3214b7c 100644
--- a/src/kern/lib.c
+++ b/src/kern/lib.c
@@ -1,7 +1,6 @@
#include "kern/lib.h"
-#define nybble_to_hex(n) \
- ((n) < 10 ? 0x30 + (n) : ('A' + ((n) - 10)))
+#define nybble_to_hex(n) ((n) < 10 ? 0x30 + (n) : ('A' + ((n)-10)))
void hexify(uint32_t v, char* into)
{
@@ -34,23 +33,22 @@ void decimalify(int v, char* into)
int i;
if (v == 0) {
- *(into ++) = '0';
+ *(into++) = '0';
*into = 0;
return;
} else {
while (v > 0) {
- *(into ++) = 0x30 + (v % 10);
+ *(into++) = 0x30 + (v % 10);
v /= 10;
- ++ c;
+ ++c;
}
}
*into = 0;
into -= c;
- for (i = 0; i < c / 2; ++ i) {
+ for (i = 0; i < c / 2; ++i) {
char tmp = into[i];
into[i] = into[c - i - 1];
into[c - i - 1] = tmp;
}
-
}
diff --git a/src/kern/log.c b/src/kern/log.c
index a217183..ebc2cbe 100644
--- a/src/kern/log.c
+++ b/src/kern/log.c
@@ -1,11 +1,10 @@
-#include "arch/stm32l4xxx/peripherals/usart.h"
-#include "arch/stm32l4xxx/peripherals/clock.h"
-
#include "kern/log.h"
-#include "kern/init.h"
-#include "kern/gpio/gpio_manager.h"
+#include "arch/stm32l4xxx/peripherals/clock.h"
+#include "arch/stm32l4xxx/peripherals/usart.h"
#include "kern/common.h"
+#include "kern/gpio/gpio_manager.h"
+#include "kern/init.h"
void setup_usart2(uint32_t baud_rate);
diff --git a/src/kern/main.c b/src/kern/main.c
index ebb2164..1eedb59 100644
--- a/src/kern/main.c
+++ b/src/kern/main.c
@@ -1,8 +1,7 @@
#include "arch.h"
-#include "kern/log.h"
-
-#include "arch/stm32l4xxx/peripherals/system.h"
#include "arch/stm32l4xxx/peripherals/clock.h"
+#include "arch/stm32l4xxx/peripherals/system.h"
+#include "kern/log.h"
void on_systick() /* Overrides weak-symbol on_systick. */
{
@@ -14,7 +13,7 @@ void on_systick() /* Overrides weak-symbol on_systick. */
/* Main function. This gets executed from the interrupt vector defined above. */
int main()
{
- klogf("Hello, World! Clock Mhz: %d\n", (uint32_t) get_clock_mhz());
+ klogf("Hello, World! Clock Mhz: %d\n", (uint32_t)get_clock_mhz());
/* Set the countdown to start from 10,000,0000. */
SCB.strv_r = 10000000;
diff --git a/src/kern/mem.c b/src/kern/mem.c
index edef8f4..ac90f0d 100644
--- a/src/kern/mem.c
+++ b/src/kern/mem.c
@@ -1,5 +1,6 @@
-#include "arch.h"
#include "kern/mem.h"
+
+#include "arch.h"
#include "kern/common.h"
#ifdef ARCH_STM32L4
@@ -7,12 +8,12 @@
* microcontroller. */
void* memset(void* dest, int c, size_t n)
{
- uint8_t c8 = (uint8_t) c;
- uint8_t* dest8 = (uint8_t*) dest;
+ uint8_t c8 = (uint8_t)c;
+ uint8_t* dest8 = (uint8_t*)dest;
uint8_t* to = dest8 + n;
- while(dest8 < to) {
- *(dest8 ++) = c8;
+ while (dest8 < to) {
+ *(dest8++) = c8;
}
return dest;
@@ -36,12 +37,12 @@ typedef struct KALLOC_NODE {
uint32_t header;
struct {
/* Is this memory block currently in use (hasn't been kfree'd) */
- uint8_t used:1;
+ uint8_t used : 1;
/* Number of words allocated. Does not include the header. */
- uint16_t size:12;
+ uint16_t size : 12;
/* The location of the previous block (in WORDS from offest) */
- kalloc_off_t prev:12;
- uint8_t canary:7;
+ kalloc_off_t prev : 12;
+ uint8_t canary : 7;
} PACKED;
};
@@ -53,7 +54,7 @@ static_assert(offsetof(kalloc_node_t, mem) == 4, "Offset check failed.");
kalloc_node_t* kalloc_start;
#define kalloc_node_out_of_range(node) \
- ((uint8_t*) (node) == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_SIZE)
+ ((uint8_t*)(node) == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_SIZE)
#define kalloc_node_next(cur) \
((kalloc_node_t*)(((uint8_t*)(cur)) + (((cur)->size + 1) * 4)))
@@ -61,28 +62,26 @@ kalloc_node_t* kalloc_start;
#define kalloc_node_prev(cur) kalloc_node_at_off(cur->prev)
#define kalloc_node_at_off(offset) \
- ((kalloc_node_t*)(((uint8_t*) kalloc_start) + (offset) * 4))
+ ((kalloc_node_t*)(((uint8_t*)kalloc_start) + (offset)*4))
#define kalloc_node_get_off(node) \
(((uint32_t)(((uint8_t*)(node)) - ((uint8_t*)(kalloc_start)))) / 4)
-#define get_kalloc_node(mem) \
- ((kalloc_node_t*)(((uint8_t*)mem) - 4))
+#define get_kalloc_node(mem) ((kalloc_node_t*)(((uint8_t*)mem) - 4))
-#define size_for(n) \
- (((n) / 4) + ((n) % 4 != 0))
+#define size_for(n) (((n) / 4) + ((n) % 4 != 0))
void* kalloc(size_t size)
{
if (!kalloc_start) {
- kalloc_start = (kalloc_node_t*) DATA_SEGMENT_STOP_ADDR;
+ kalloc_start = (kalloc_node_t*)DATA_SEGMENT_STOP_ADDR;
memset(kalloc_start, 0, sizeof(kalloc_node_t));
kalloc_start->size = (MAX_HEAP_SIZE / 4) - 1;
kalloc_start->canary = CANARY;
}
size_t realsz = size_for(size); /* Clip the size to the nearest word. */
- kalloc_off_t offset = 0;
+ kalloc_off_t offset = 0;
while (offset < (MAX_HEAP_SIZE / 4)) {
kalloc_node_t* cur = kalloc_node_at_off(offset);
@@ -90,7 +89,7 @@ void* kalloc(size_t size)
cur->used = true;
size_t orig_size = cur->size;
cur->size = realsz;
-
+
if (orig_size > realsz) {
/* This kalloc node needs to split into two blocks. */
kalloc_node_t* next = kalloc_node_next(cur);
@@ -105,7 +104,7 @@ void* kalloc(size_t size)
}
}
- return (void*) cur->mem;
+ return (void*)cur->mem;
}
offset += (sizeof(kalloc_node_t) / 4) + cur->size;
@@ -114,7 +113,6 @@ void* kalloc(size_t size)
return NULL;
}
-
/* Joins this node with the previous and next nodes if they're free. */
static void coalesce(kalloc_node_t* cur)
{
@@ -145,7 +143,7 @@ static void coalesce(kalloc_node_t* cur)
next_used->prev = kalloc_node_get_off(last_freed);
}
- last_freed->size = ((uint8_t*) next_used - (last_freed->mem)) / 4;
+ last_freed->size = ((uint8_t*)next_used - (last_freed->mem)) / 4;
}
#ifdef FOR_TESTING
@@ -159,7 +157,8 @@ void panic(const char* x)
#else
void panic(const char* x)
{
- for(;;);
+ for (;;)
+ ;
}
#endif
@@ -186,7 +185,7 @@ void* debug_kalloc_get_next_ptr(void* ptr)
{
kalloc_node_t* node = ptr - sizeof(kalloc_node_t);
kalloc_node_t* next = kalloc_node_next(node);
-
+
return next->mem;
}
@@ -194,7 +193,7 @@ void* debug_kalloc_get_prev_ptr(void* ptr)
{
kalloc_node_t* node = ptr - sizeof(kalloc_node_t);
kalloc_node_t* prev = kalloc_node_prev(node);
-
+
return prev->mem;
}
@@ -204,8 +203,13 @@ void debug_print_blocks()
kalloc_node_t* cur = kalloc_node_at_off(0);
while (!kalloc_node_out_of_range(cur)) {
- printf("header (%04x) {used=%d, size=%5d, prev=%04x, canary=%02x}\n",
- kalloc_node_get_off(cur), cur->used, cur->size, cur->prev, cur->canary);
+ printf(
+ "header (%04x) {used=%d, size=%5d, prev=%04x, canary=%02x}\n",
+ kalloc_node_get_off(cur),
+ cur->used,
+ cur->size,
+ cur->prev,
+ cur->canary);
cur = kalloc_node_next(cur);
}
}
@@ -220,34 +224,44 @@ int debug_kalloc_assert_consistency(char* error, size_t len)
size_t n_blocks = 1;
size_t n_blocks_back = 1;
- while(1) {
+ while (1) {
if (cur->canary != CANARY) {
- snprintf(error, len, "Node has corrupted canary. %02x vs expected %02x\n",
- cur->canary, CANARY);
+ snprintf(
+ error,
+ len,
+ "Node has corrupted canary. %02x vs expected %02x\n",
+ cur->canary,
+ CANARY);
return 1;
}
total_size += cur->size + 1;
kalloc_node_t* next = kalloc_node_next(cur);
- if ((uint8_t*) next == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_SIZE) {
+ if ((uint8_t*)next == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_SIZE) {
break;
- } else if ((uint8_t*) next > (uint8_t*)DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE){
+ } else if (
+ (uint8_t*)next > (uint8_t*)DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE) {
snprintf(
- error, len, "Next node points is out of bounds. %p vs max of %p\n",
+ error,
+ len,
+ "Next node points is out of bounds. %p vs max of %p\n",
next,
(void*)(DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE));
return 1;
}
cur = next;
- ++ n_blocks;
+ ++n_blocks;
}
if (total_size * 4 != MAX_HEAP_SIZE) {
snprintf(
- error, len, "Total recorded size is inconsistent. %lu vs %lu\n",
- total_size * 4, MAX_HEAP_SIZE);
+ error,
+ len,
+ "Total recorded size is inconsistent. %lu vs %lu\n",
+ total_size * 4,
+ MAX_HEAP_SIZE);
return 1;
}
@@ -257,20 +271,24 @@ int debug_kalloc_assert_consistency(char* error, size_t len)
while (loop_check < 10000) {
kalloc_node_t* prev = kalloc_node_prev(cur);
- ++ n_blocks_back;
+ ++n_blocks_back;
- if (prev == kalloc_start) {
+ if (prev == kalloc_start) {
if (n_blocks != n_blocks_back) {
snprintf(
- error, len, "Different number of blocks found on the way back. Found %lu on the way back vs %lu up.\n",
- n_blocks_back, n_blocks);
+ error,
+ len,
+ "Different number of blocks found on the way back. Found %lu on "
+ "the way back vs %lu up.\n",
+ n_blocks_back,
+ n_blocks);
return 1;
}
return 0;
}
cur = prev;
- ++ loop_check;
+ ++loop_check;
}
snprintf(error, len, "Loop check failed.\n");
diff --git a/src/kern/stdlibrepl.c b/src/kern/stdlibrepl.c
index 2d9d839..98142e2 100644
--- a/src/kern/stdlibrepl.c
+++ b/src/kern/stdlibrepl.c
@@ -8,6 +8,6 @@
size_t strlen(char* ch)
{
size_t ret = 0;
- while(*(ch ++) != 0) ++ ret;
+ while (*(ch++) != 0) ++ret;
return ret;
}
diff --git a/src/kern/string.c b/src/kern/string.c
index 4afa228..ac2d763 100644
--- a/src/kern/string.c
+++ b/src/kern/string.c
@@ -2,8 +2,8 @@
void kstrcpy(char* into, const char* from)
{
- while(*from) {
- *(into ++) = *(from ++);
+ while (*from) {
+ *(into++) = *(from++);
}
*into = 0;
}
diff --git a/test_harness/fake_env.c b/test_harness/fake_env.c
index 6a32c99..7ac0d9b 100644
--- a/test_harness/fake_env.c
+++ b/test_harness/fake_env.c
@@ -1,26 +1,26 @@
#include "fake_env.h"
-#include <stdlib.h>
#include <assert.h>
+#include <stdlib.h>
struct fakeenv_memseg {
const char* name;
void* segment;
};
-#define DEFINE_MEMORY_SEGMENT(seg, start_addr, end_addr) \
- static __attribute((__section__("fakeenv"))) \
- struct fakeenv_memseg fake_##seg = { \
- .name = #seg, \
- .segment = NULL, \
- }; \
- void* load_fake_##seg##__ () \
- { \
- if (fake_##seg .segment == NULL) { \
- fake_##seg .segment = malloc((end_addr) - (start_addr)); \
- assert(fake_##seg .segment != NULL); \
- } \
- return fake_##seg.segment; \
+#define DEFINE_MEMORY_SEGMENT(seg, start_addr, end_addr) \
+ static __attribute( \
+ (__section__("fakeenv"))) struct fakeenv_memseg fake_##seg = { \
+ .name = #seg, \
+ .segment = NULL, \
+ }; \
+ void* load_fake_##seg##__() \
+ { \
+ if (fake_##seg.segment == NULL) { \
+ fake_##seg.segment = malloc((end_addr) - (start_addr)); \
+ assert(fake_##seg.segment != NULL); \
+ } \
+ return fake_##seg.segment; \
}
extern struct fakeenv_memseg __start_fakeenv;
@@ -28,9 +28,8 @@ extern struct fakeenv_memseg __stop_fakeenv;
void wipeout_fake_env()
{
- for (struct fakeenv_memseg* iter = &__start_fakeenv;
- iter < &__stop_fakeenv;
- ++ iter) {
+ for (struct fakeenv_memseg* iter = &__start_fakeenv; iter < &__stop_fakeenv;
+ ++iter) {
free(iter->segment);
iter->segment = NULL;
}
@@ -46,7 +45,7 @@ DEFINE_MEMORY_SEGMENT(ahb1, 0x40020000, 0x40024400)
DEFINE_MEMORY_SEGMENT(ahb2, 0x48000000, 0x50060C00)
/* System Control Block */
-DEFINE_MEMORY_SEGMENT(scb, 0xE000E008, 0xE000EF04)
+DEFINE_MEMORY_SEGMENT(scb, 0xE000E008, 0xE000EF04)
/* Nested Vector Interrupt Controller (NVIC) */
/* Note that this memory space acutally overlaps with the SCB, but
diff --git a/test_harness/test_harness.c b/test_harness/test_harness.c
index bf9249c..fda623f 100644
--- a/test_harness/test_harness.c
+++ b/test_harness/test_harness.c
@@ -1,24 +1,19 @@
#include "test_harness.h"
-#include "fake_env.h"
#include <assert.h>
+#include <setjmp.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
-#include <stdlib.h>
-#include <setjmp.h>
+#include "fake_env.h"
static jmp_buf jmpbuf;
-volatile test_t dummy
- __attribute((__section__("tests")))
- __attribute((__used__)) = {
- "dummy",
- "dummy",
- NULL
-};
+volatile test_t dummy __attribute((__section__("tests")))
+__attribute((__used__)) = {"dummy", "dummy", NULL};
extern unsigned char __data_start;
extern unsigned char _end;
@@ -69,28 +64,27 @@ void test_printp(size_t sz, void* v1, void* v2)
{
fprintf(stderr, "%p == %p\n", v1, v2);
}
-void test_printuc(
- size_t sz, unsigned char v1, unsigned char v2)
+void test_printuc(size_t sz, unsigned char v1, unsigned char v2)
{
- fprintf(stderr, "%02x == %02x\n", (int) v1, (int) v2);
+ fprintf(stderr, "%02x == %02x\n", (int)v1, (int)v2);
}
static int do_fork = 1;
static size_t saved_data_size;
static unsigned char* saved_data = NULL;
-int main(int argc, char** argv) {
+int main(int argc, char** argv)
+{
/* Save all initialized data. */
saved_data_size = &_end - &__data_start;
saved_data = malloc(saved_data_size);
memcpy(saved_data, &__data_start, saved_data_size);
-
if (argc > 1 && strcmp(argv[1], "--nofork") == 0) {
do_fork = 0;
}
- for( ; iter < &__stop_tests; ++ iter) {
+ for (; iter < &__stop_tests; ++iter) {
if (iter->fn_ptr != NULL) {
execute_test(iter);
}
diff --git a/tests/test_dma.c b/tests/test_dma.c
index 50cdb5b..f9b6a26 100644
--- a/tests/test_dma.c
+++ b/tests/test_dma.c
@@ -1,12 +1,12 @@
-#include "test_harness.h"
+#include <memory.h>
+#include <stdio.h>
+#include <stdlib.h>
+
#include "arch/stm32l4xxx/peripherals/dma.h"
#include "arch/stm32l4xxx/peripherals/rcc.h"
#include "arch/stm32l4xxx/peripherals/usart.h"
#include "kern/dma/dma_manager.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <memory.h>
+#include "test_harness.h"
TEST(dma, smoke)
{
@@ -51,9 +51,7 @@ TEST(dma, regset_pl)
ASSERT_EQ(reg, (1 << 12));
- ASSERT_EQ(
- regget(reg, dma_cc_pl),
- DMA_PRIORITY_LEVEL_MEDIUM);
+ ASSERT_EQ(regget(reg, dma_cc_pl), DMA_PRIORITY_LEVEL_MEDIUM);
return 0;
}
@@ -63,20 +61,13 @@ TEST(dma_peri, select_peripheral)
int ec;
dma_mem2p_channel_t chan =
- select_dma_channel_mem2p(
- DMA1_PERIPH_USART2_TX,
- &opts,
- &ec);
+ select_dma_channel_mem2p(DMA1_PERIPH_USART2_TX, &opts, &ec);
ASSERT_EQ(DMA1.channel_config[6].cpa_r, ptr2reg(&USART2.td_r));
- ASSERT_EQ(
- regget(DMA1.channel_config[6].cc_r, dma_cc_dir),
- READ_FROM_MEMORY);
+ ASSERT_EQ(regget(DMA1.channel_config[6].cc_r, dma_cc_dir), READ_FROM_MEMORY);
- ASSERT_EQ(
- regget(DMA1.channel_config[6].cc_r, dma_cc_minc),
- 1);
+ ASSERT_EQ(regget(DMA1.channel_config[6].cc_r, dma_cc_minc), 1);
ASSERT_EQ(
regget(DMA1.channel_config[6].cc_r, dma_cc_pl),
@@ -99,26 +90,17 @@ TEST(dma_peri, unable_to_realloc)
int ec = 0;
dma_mem2p_channel_t chan =
- select_dma_channel_mem2p(
- DMA1_PERIPH_USART2_TX,
- &opts,
- &ec);
+ select_dma_channel_mem2p(DMA1_PERIPH_USART2_TX, &opts, &ec);
ASSERT_EQ(ec, 0);
- select_dma_channel_p2mem(
- DMA1_PERIPH_USART2_TX,
- &opts,
- &ec);
+ select_dma_channel_p2mem(DMA1_PERIPH_USART2_TX, &opts, &ec);
ASSERT_EQ(ec, DMA_ERROR_CHANNEL_IN_USE);
-
+
release_dma_channel(chan.c_);
- chan = select_dma_channel_mem2p(
- DMA1_PERIPH_USART2_TX,
- &opts,
- &ec);
+ chan = select_dma_channel_mem2p(DMA1_PERIPH_USART2_TX, &opts, &ec);
ASSERT_EQ(ec, 0);
@@ -130,16 +112,14 @@ TEST(dma_peri, select_mem2mem)
{
int ec = 0;
dma_opts_t opts = DEFAULT_DMA_OPTS;
- dma_mem2mem_channel_t chan =
- select_dma_channel_mem2mem(-1, &opts, &ec);
+ dma_mem2mem_channel_t chan = select_dma_channel_mem2mem(-1, &opts, &ec);
ASSERT_EQ(ec, 0);
ASSERT_EQ(chan.c_.dma, 1);
ASSERT_EQ(chan.c_.chan, 6);
- dma_mem2mem_channel_t chan2 =
- select_dma_channel_mem2mem(-1, &opts, &ec);
+ dma_mem2mem_channel_t chan2 = select_dma_channel_mem2mem(-1, &opts, &ec);
ASSERT_EQ(ec, 0);
@@ -148,8 +128,7 @@ TEST(dma_peri, select_mem2mem)
release_dma_channel(chan.c_);
- dma_mem2mem_channel_t chan3 =
- select_dma_channel_mem2mem(-1, &opts, &ec);
+ dma_mem2mem_channel_t chan3 = select_dma_channel_mem2mem(-1, &opts, &ec);
ASSERT_EQ(chan3.c_.dma, 1);
ASSERT_EQ(chan3.c_.chan, 6);
@@ -165,9 +144,8 @@ TEST(dma_peri, select_mem2mem_2)
dma_mem2mem_channel_t chans[14];
int ec;
- for (int i = 0; i < 14; ++ i) {
- chans[i] = select_dma_channel_mem2mem(
- -1, &opts, &ec);
+ for (int i = 0; i < 14; ++i) {
+ chans[i] = select_dma_channel_mem2mem(-1, &opts, &ec);
ASSERT_EQ(ec, 0);
}
@@ -175,7 +153,7 @@ TEST(dma_peri, select_mem2mem_2)
select_dma_channel_mem2mem(-1, &opts, &ec);
ASSERT_EQ(ec, DMA_ERROR_CHANNEL_IN_USE);
- for (int i = 0; i < 14; ++ i) {
+ for (int i = 0; i < 14; ++i) {
if (i < 7) {
ASSERT_EQ(chans[i].c_.chan, 6 - i);
ASSERT_EQ(chans[i].c_.dma, 1);
diff --git a/tests/test_gpio.c b/tests/test_gpio.c
index bcb953c..dd9c957 100644
--- a/tests/test_gpio.c
+++ b/tests/test_gpio.c
@@ -1,7 +1,6 @@
-#include "test_harness.h"
-
#include "arch/stm32l4xxx/peripherals/rcc.h"
#include "kern/gpio/gpio_manager.h"
+#include "test_harness.h"
TEST(gpio_manager, smell)
{
@@ -46,7 +45,7 @@ TEST(gpio_manager, alternate)
ASSERT_EQ(ec, 0);
- gpio_port_config_t* gpioa = (gpio_port_config_t*) GPIOA_BASE;
+ gpio_port_config_t* gpioa = (gpio_port_config_t*)GPIOA_BASE;
ASSERT_EQ(regget(gpioa->mode_r, gpio_mode_n(15)), GPIO_MODE_ALTERNATE);
ASSERT_EQ(regget(gpioa->mode_r, gpio_mode_n(2)), GPIO_MODE_ALTERNATE);
@@ -75,8 +74,7 @@ TEST(gpio_manager, bad_pin)
int ec;
/* Pretending to start the USART. */
- gpio_enable_alternate_function(
- GPIO_ALTERNATE_FUNCTION_USART2_RX, 99, &ec);
+ gpio_enable_alternate_function(GPIO_ALTERNATE_FUNCTION_USART2_RX, 99, &ec);
ASSERT_EQ(ec, GPIO_ERROR_INVALID_PIN);
@@ -103,7 +101,6 @@ TEST(gpio_manager, alternate_then_reserve_fail)
TEST(gpio_manager, get_gpio_pin_port_off)
{
-
gpio_port_config_t* cfg;
int off;
get_gpio_pin_port_off(GPIO_PIN_PA5, &cfg, &off);
@@ -127,12 +124,14 @@ TEST(gpio_manager, sets_gpio_settings)
reserve_gpio_pin(GPIO_PIN_PA2, &opts, &ec);
ASSERT_EQ(ec, 0);
- gpio_port_config_t* gpioa = (gpio_port_config_t*) GPIOA_BASE;
+ gpio_port_config_t* gpioa = (gpio_port_config_t*)GPIOA_BASE;
ASSERT_EQ(regget(gpioa->mode_r, gpio_mode_n(2)), GPIO_MODE_OUTPUT);
ASSERT_EQ(regget(gpioa->pupd_r, gpio_pupd_n(2)), GPIO_PULL_DIR_NONE);
- ASSERT_EQ(regget(gpioa->ospeed_r, gpio_ospeed_n(2)), GPIO_OUTPUT_SPEED_VERY_HIGH);
- ASSERT_EQ(regget(gpioa->otype_r, gpio_otype_n(2)), GPIO_OUTPUT_TYPE_PUSH_PULL);
+ ASSERT_EQ(
+ regget(gpioa->ospeed_r, gpio_ospeed_n(2)), GPIO_OUTPUT_SPEED_VERY_HIGH);
+ ASSERT_EQ(
+ regget(gpioa->otype_r, gpio_otype_n(2)), GPIO_OUTPUT_TYPE_PUSH_PULL);
return 0;
}
diff --git a/tests/test_irq.c b/tests/test_irq.c
index 3c4ee9c..6fe20e4 100644
--- a/tests/test_irq.c
+++ b/tests/test_irq.c
@@ -1,11 +1,10 @@
-#include "test_harness.h"
-
#include "arch/stm32l4xxx/peripherals/irq.h"
#include "arch/stm32l4xxx/peripherals/nvic.h"
+#include "test_harness.h"
TEST(irq, nvic)
{
- interrupt_set_t is = { 0 };
+ interrupt_set_t is = {0};
interrupt_set_add(&is, IRQ_USART2);
interrupt_set_add(&is, IRQ_USART3);
@@ -20,7 +19,7 @@ TEST(irq, nvic)
TEST(irq, nvic_edgecase)
{
- interrupt_set_t is = { 0 };
+ interrupt_set_t is = {0};
interrupt_set_add(&is, IRQ_WWDG_IRQ);
interrupt_set_add(&is, IRQ_I2C1_ER);
diff --git a/tests/test_lib.c b/tests/test_lib.c
index 33076c2..e9361c4 100644
--- a/tests/test_lib.c
+++ b/tests/test_lib.c
@@ -1,5 +1,5 @@
-#include "test_harness.h"
#include "kern/lib.h"
+#include "test_harness.h"
TEST(lib, hexify)
{
diff --git a/tests/test_memory.c b/tests/test_memory.c
index 6e523aa..0f4ccab 100644
--- a/tests/test_memory.c
+++ b/tests/test_memory.c
@@ -5,9 +5,9 @@
#include <stdlib.h>
#include "arch.h"
-#include "test_harness.c"
#include "kern/common.h"
#include "kern/mem.h"
+#include "test_harness.c"
struct TEST_STRUCT {
uint32_t array[3];
@@ -23,12 +23,12 @@ typedef struct KALLOC_NODE {
uint32_t header;
struct {
/* Is this memory block currently in use (hasn't been kfree'd) */
- bool used:1;
+ bool used : 1;
/* Number of words allocated. Does not include the header. */
- uint16_t size:12;
+ uint16_t size : 12;
/* The location of the previous block (in WORDS from offest) */
- uint16_t prev:12;
- uint8_t canary:7;
+ uint16_t prev : 12;
+ uint8_t canary : 7;
} PACKED;
};
@@ -43,11 +43,10 @@ static void wipeout_kalloc()
kalloc_start = NULL;
}
-
static struct TEST_STRUCT* new_test_struct()
{
struct TEST_STRUCT* ret = kalloc(sizeof(struct TEST_STRUCT));
-
+
ret->array[0] = 1;
ret->array[1] = 2;
ret->array[2] = 3;
@@ -58,20 +57,18 @@ static struct TEST_STRUCT* new_test_struct()
static struct TEST_STRUCT2* new_test_struct2()
{
struct TEST_STRUCT2* ret = kalloc(sizeof(struct TEST_STRUCT2));
-
- for (int i = 0; i < 10; ++ i) {
+
+ for (int i = 0; i < 10; ++i) {
ret->array[i] = i;
}
return ret;
}
-#define ASSERT_CHAIN(t1, t2) \
- ASSERT_EQ(V(t1) + sizeof(*t1) + 4, V(t2))
+#define ASSERT_CHAIN(t1, t2) ASSERT_EQ(V(t1) + sizeof(*t1) + 4, V(t2))
TEST(memory, kalloc)
{
-
#define V(x) ((void*)(x))
struct TEST_STRUCT* test1 = new_test_struct();
struct TEST_STRUCT2* test2 = new_test_struct2();
@@ -90,14 +87,9 @@ TEST(memory, kalloc)
ASSERT_CHAIN(test3, test4);
ASSERT_CHAIN(test4, test5);
-
char buf[1024];
if (debug_kalloc_assert_consistency(buf, 1024)) {
- fprintf(
- stderr,
- "Consistency check failed. (%s:%d)\n",
- __FILE__,
- __LINE__);
+ fprintf(stderr, "Consistency check failed. (%s:%d)\n", __FILE__, __LINE__);
fprintf(stderr, buf);
ASSERT_TRUE(false);
}
@@ -114,7 +106,7 @@ struct UNEVEN_STRUCT {
struct UNEVEN_STRUCT* new_uneven_struct()
{
struct UNEVEN_STRUCT* ret = kalloc(sizeof(struct UNEVEN_STRUCT));
-
+
ret->arr[0] = 1;
ret->arr[1] = 2;
ret->arr[2] = 3;
@@ -124,8 +116,7 @@ struct UNEVEN_STRUCT* new_uneven_struct()
return ret;
}
-#define size_for(n) \
- (((n) / 4) + ((n) % 4 != 0))
+#define size_for(n) (((n) / 4) + ((n) % 4 != 0))
TEST(memory, uneven_kalloc)
{
@@ -161,7 +152,7 @@ TEST(memory, kalloc_free)
kfree(test1);
kfree(test5);
- ASSERT_EQ((int) kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
+ ASSERT_EQ((int)kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
test1 = new_test_struct();
test2 = new_test_struct2();
@@ -175,7 +166,7 @@ TEST(memory, kalloc_free)
kfree(test4);
kfree(test5);
- ASSERT_EQ((int) kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
+ ASSERT_EQ((int)kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
test1 = new_test_struct();
test2 = new_test_struct2();
@@ -189,7 +180,7 @@ TEST(memory, kalloc_free)
kfree(test2);
kfree(test5);
- ASSERT_EQ((int) kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
+ ASSERT_EQ((int)kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
wipeout_kalloc();
@@ -262,10 +253,10 @@ TEST(memory, consistency_stress)
}
int i;
- void* allocd[NRUNS] = { 0 };
+ void* allocd[NRUNS] = {0};
char buf[1024];
- for (i = 0; i < NRUNS; ++ i) {
+ for (i = 0; i < NRUNS; ++i) {
size_t nalloc = rand() % 20;
allocd[i] = kalloc(nalloc);
@@ -326,23 +317,23 @@ TEST(memory, consistency_stress)
}
}
- for(i = 0; i < NRUNS; ++ i) {
+ for (i = 0; i < NRUNS; ++i) {
if (allocd[i]) {
kfree(allocd[i]);
}
if (debug_kalloc_assert_consistency(buf, 1024)) {
- fprintf(
- stderr,
- "Consistency check failed. (At index=%d, %s:%d)\n",
- i,
- __FILE__,
- __LINE__);
+ fprintf(
+ stderr,
+ "Consistency check failed. (At index=%d, %s:%d)\n",
+ i,
+ __FILE__,
+ __LINE__);
fprintf(stderr, buf);
ASSERT_TRUE(false);
}
}
- ASSERT_EQ((int) kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
+ ASSERT_EQ((int)kalloc_start->size * 4, MAX_HEAP_SIZE - 4);
return 0;
}
@@ -364,7 +355,7 @@ TEST(memory, kalloc_free_alloc)
struct TEST_STRUCT2* test6 = new_test_struct2();
// test_6 should have been allocated in test_4's spot.
- ASSERT_EQ(test6, test4);
+ ASSERT_EQ(test6, test4);
kfree(test2);
struct TEST_STRUCT* test7 = new_test_struct();
diff --git a/tests/test_spi.c b/tests/test_spi.c
index cc25d6e..866f6e8 100644
--- a/tests/test_spi.c
+++ b/tests/test_spi.c
@@ -1,6 +1,5 @@
-#include "test_harness.h"
-
#include "arch/stm32l4xxx/peripherals/spi.h"
+#include "test_harness.h"
TEST(spi, smoke)
{
diff --git a/tests/test_usart.c b/tests/test_usart.c
index b19d687..82b6ce7 100644
--- a/tests/test_usart.c
+++ b/tests/test_usart.c
@@ -1,8 +1,8 @@
-#include "test_harness.h"
-#include "arch/stm32l4xxx/peripherals/usart.h"
-
#include <stdlib.h>
+#include "arch/stm32l4xxx/peripherals/usart.h"
+#include "test_harness.h"
+
TEST(usart, enable_dma)
{
__IO usart_t* usart = &USART1;