cpu: make use of ARRAY_SIZE macro

This commit is contained in:
Benjamin Valentin 2019-07-18 15:19:55 +02:00
parent d16afe5422
commit b8c4ab5b69
11 changed files with 21 additions and 21 deletions

View File

@ -129,7 +129,7 @@ static const struct _irq_alloc_table_t _irq_alloc_table[] = {
typedef void (*intr_handler_t)(void *arg); typedef void (*intr_handler_t)(void *arg);
#define IRQ_ALLOC_TABLE_SIZE (sizeof(_irq_alloc_table)/sizeof(struct _irq_alloc_table_t)) #define IRQ_ALLOC_TABLE_SIZE ARRAY_SIZE(_irq_alloc_table)
#define ESP_INTR_FLAG_INTRDISABLED (1<<11) #define ESP_INTR_FLAG_INTRDISABLED (1<<11)
/** /**

View File

@ -100,7 +100,7 @@ static const struct _pwm_hw_t _pwm_hw[] =
.mod = PERIPH_PWM0_MODULE, .mod = PERIPH_PWM0_MODULE,
.int_src = ETS_PWM0_INTR_SOURCE, .int_src = ETS_PWM0_INTR_SOURCE,
.signal_group = PWM0_OUT0A_IDX, .signal_group = PWM0_OUT0A_IDX,
.gpio_num = sizeof(pwm0_channels) / sizeof(pwm0_channels[0]), .gpio_num = ARRAY_SIZE(pwm0_channels),
.gpios = pwm0_channels, .gpios = pwm0_channels,
}, },
#endif #endif
@ -110,7 +110,7 @@ static const struct _pwm_hw_t _pwm_hw[] =
.mod = PERIPH_PWM1_MODULE, .mod = PERIPH_PWM1_MODULE,
.int_src = ETS_PWM1_INTR_SOURCE, .int_src = ETS_PWM1_INTR_SOURCE,
.signal_group = PWM1_OUT0A_IDX, .signal_group = PWM1_OUT0A_IDX,
.gpio_num = sizeof(pwm1_channels) / sizeof(pwm1_channels[0]), .gpio_num = ARRAY_SIZE(pwm1_channels),
.gpios = pwm1_channels, .gpios = pwm1_channels,
}, },
#endif #endif

View File

@ -113,7 +113,7 @@ static const struct station_config station_cfg = {
*/ */
static const struct softap_config softap_cfg = { static const struct softap_config softap_cfg = {
.ssid = ESP_WIFI_SSID, .ssid = ESP_WIFI_SSID,
.ssid_len = sizeof(ESP_WIFI_SSID) / sizeof(ESP_WIFI_SSID[0]), .ssid_len = ARRAY_SIZE(ESP_WIFI_SSID),
.ssid_hidden = 1, /* don't make the AP visible */ .ssid_hidden = 1, /* don't make the AP visible */
.password = ESP_WIFI_PASS, .password = ESP_WIFI_PASS,
.authmode = AUTH_WPA2_PSK, .authmode = AUTH_WPA2_PSK,

View File

@ -137,10 +137,10 @@ static void _i2c_clear (_i2c_bus_t* bus);
/* implementation of i2c interface */ /* implementation of i2c interface */
void i2c_init(i2c_t dev) void i2c_init(i2c_t dev)
{ {
if (I2C_NUMOF != sizeof(_i2c_bus)/sizeof(_i2c_bus_t)) { if (I2C_NUMOF != ARRAY_SIZE(_i2c_bus)) {
LOG_INFO("I2C_NUMOF does not match number of I2C_SDA_x/I2C_SCL_x definitions\n"); LOG_INFO("I2C_NUMOF does not match number of I2C_SDA_x/I2C_SCL_x definitions\n");
LOG_INFO("Please check your board configuration in 'board.h'\n"); LOG_INFO("Please check your board configuration in 'board.h'\n");
assert(I2C_NUMOF < sizeof(_i2c_bus)/sizeof(_i2c_bus_t)); assert(I2C_NUMOF < ARRAY_SIZE(_i2c_bus));
return; return;
} }

View File

@ -297,7 +297,7 @@ void timer_print_config(void)
{ {
for (int i = 0; i < HW_TIMER_NUMOF; i++) { for (int i = 0; i < HW_TIMER_NUMOF; i++) {
LOG_INFO("\tTIMER_DEV(%d): %d channel(s)\n", i, LOG_INFO("\tTIMER_DEV(%d): %d channel(s)\n", i,
sizeof(timers[i].channels) / sizeof(struct hw_channel_t)); ARRAY_SIZE(timers[i].channels));
} }
} }
@ -562,7 +562,7 @@ void timer_print_config(void)
{ {
for (int i = 0; i < OS_TIMER_NUMOF; i++) { for (int i = 0; i < OS_TIMER_NUMOF; i++) {
LOG_INFO("\tTIMER_DEV(%d): %d channel(s)\n", i, LOG_INFO("\tTIMER_DEV(%d): %d channel(s)\n", i,
sizeof(timers[i].channels) / sizeof(struct phy_channel_t)); ARRAY_SIZE(timers[i].channels));
} }
} }

View File

@ -195,7 +195,7 @@ int main(int argc, char **argv)
puts("static const uint32_t spi_clk_config[] = {"); puts("static const uint32_t spi_clk_config[] = {");
for (i = 0; i < (sizeof(targets) / sizeof(targets[0])); i++) { for (i = 0; i < ARRAY_SIZE(targets); i++) {
uint8_t tmp, ptmp; uint8_t tmp, ptmp;
long res; long res;
/* bus clock */ /* bus clock */
@ -229,7 +229,7 @@ int main(int argc, char **argv)
} }
printf(" SPI_CTAR_PDT(%i) | SPI_CTAR_DT(%i)\n", (int)ptmp, (int)tmp); printf(" SPI_CTAR_PDT(%i) | SPI_CTAR_DT(%i)\n", (int)ptmp, (int)tmp);
if (i == (sizeof(targets) / sizeof(targets[0])) - 1) { if (i == ARRAY_SIZE(targets) - 1) {
puts(" )"); puts(" )");
} }
else { else {

View File

@ -22,7 +22,7 @@
{ .dev = ADC0, .pin = GPIO_PIN(PORT_C, 1), .chan = 15 }, // PTC1 { .dev = ADC0, .pin = GPIO_PIN(PORT_C, 1), .chan = 15 }, // PTC1
{ .dev = ADC0, .pin = GPIO_PIN(PORT_C, 2), .chan = 4 }, // PTC2 { .dev = ADC0, .pin = GPIO_PIN(PORT_C, 2), .chan = 4 }, // PTC2
}; };
#define ADC_NUMOF (sizeof(adc_config) / sizeof(adc_config[0])) #define ADC_NUMOF ARRAYSIZE(adc_config)
@defgroup cpu_kinetis_cpuid Kinetis CPUID @defgroup cpu_kinetis_cpuid Kinetis CPUID
@ -59,7 +59,7 @@ No configuration is necessary.
.sda_pcr = (PORT_PCR_MUX(2) | PORT_PCR_ODE_MASK), .sda_pcr = (PORT_PCR_MUX(2) | PORT_PCR_ODE_MASK),
}, },
}; };
#define I2C_NUMOF (sizeof(i2c_config) / sizeof(i2c_config[0])) #define I2C_NUMOF ARRAYSIZE(i2c_config)
#define I2C_0_ISR (isr_i2c0) #define I2C_0_ISR (isr_i2c0)
@ -195,7 +195,7 @@ because of the additional overhead of calling gpio_set/clear at every transfer.
.simmask = SIM_SCGC6_SPI1_MASK .simmask = SIM_SCGC6_SPI1_MASK
} }
}; };
#define SPI_NUMOF (sizeof(spi_config) / sizeof(spi_config[0])) #define SPI_NUMOF ARRAYSIZE(spi_config)
@defgroup cpu_kinetis_timer Kinetis Timer @defgroup cpu_kinetis_timer Kinetis Timer
@ -281,6 +281,6 @@ available.
.mode = UART_MODE_8N1, .mode = UART_MODE_8N1,
}, },
}; };
#define UART_NUMOF (sizeof(uart_config) / sizeof(uart_config[0])) #define UART_NUMOF ARRAYSIZE(uart_config)
*/ */

View File

@ -129,7 +129,7 @@ static uint8_t i2c_find_divider(unsigned freq, unsigned speed)
{ {
unsigned diff = UINT_MAX; unsigned diff = UINT_MAX;
/* Use maximum divider if nothing matches */ /* Use maximum divider if nothing matches */
uint8_t F = sizeof(i2c_dividers) / sizeof(i2c_dividers[0]) - 1; uint8_t F = ARRAY_SIZE(i2c_dividers) - 1;
/* We avoid using the MULT field to simplify the driver and avoid having to /* We avoid using the MULT field to simplify the driver and avoid having to
* work around hardware errata on some Kinetis parts * work around hardware errata on some Kinetis parts
* *
@ -151,7 +151,7 @@ static uint8_t i2c_find_divider(unsigned freq, unsigned speed)
* the I2Cx_F [MULT] field to the original value after the repeated start * the I2Cx_F [MULT] field to the original value after the repeated start
* has occurred * has occurred
*/ */
for (unsigned k = 0; k < sizeof(i2c_dividers) / sizeof(i2c_dividers[0]); ++k) { for (unsigned k = 0; k < ARRAY_SIZE(i2c_dividers); ++k) {
/* Test dividers until we find one that gives a good match */ /* Test dividers until we find one that gives a good match */
unsigned lim = (speed * i2c_dividers[k]); unsigned lim = (speed * i2c_dividers[k]);
if (lim >= freq) { if (lim >= freq) {

View File

@ -158,7 +158,7 @@ uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
uint8_t pwm_channels(pwm_t dev) uint8_t pwm_channels(pwm_t dev)
{ {
return sizeof(pwm_config[dev].chan) / sizeof(pwm_config[dev].chan[0]); return ARRAY_SIZE(pwm_config[dev].chan);
} }
void pwm_set(pwm_t dev, uint8_t channel, uint16_t value) void pwm_set(pwm_t dev, uint8_t channel, uint16_t value)

View File

@ -72,7 +72,7 @@ static int real_clk(int bus, int br)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int tnum = sizeof(targets) / sizeof(targets[0]); int tnum = ARRAY_SIZE(targets);
int apb[2]; int apb[2];
if (argc != 3) { if (argc != 3) {
@ -90,7 +90,7 @@ int main(int argc, char **argv)
printf("static const uint8_t spi_divtable[2][%i] = {\n", tnum); printf("static const uint8_t spi_divtable[2][%i] = {\n", tnum);
for (int bus = 0; bus < (sizeof(apb) / sizeof(apb[0])); bus ++) { for (int bus = 0; bus < ARRAY_SIZE(apb); bus ++) {
printf(" { /* for APB%i @ %iHz */\n", (bus + 1), apb[bus]); printf(" { /* for APB%i @ %iHz */\n", (bus + 1), apb[bus]);
for (int t = 0; t < tnum; t++) { for (int t = 0; t < tnum; t++) {
int br = find_best(apb[bus], targets[t]); int br = find_best(apb[bus], targets[t]);

View File

@ -586,7 +586,7 @@ static void shared_isr(uint8_t *streams, size_t nb)
void DMA_SHARED_ISR_0(void) void DMA_SHARED_ISR_0(void)
{ {
uint8_t streams[] = DMA_SHARED_ISR_0_STREAMS; uint8_t streams[] = DMA_SHARED_ISR_0_STREAMS;
shared_isr(streams, sizeof(streams) / sizeof(streams[0])); shared_isr(streams, ARRAY_SIZE(streams));
} }
#endif #endif
@ -594,6 +594,6 @@ void DMA_SHARED_ISR_0(void)
void DMA_SHARED_ISR_1(void) void DMA_SHARED_ISR_1(void)
{ {
uint8_t streams[] = DMA_SHARED_ISR_1_STREAMS; uint8_t streams[] = DMA_SHARED_ISR_1_STREAMS;
shared_isr(streams, sizeof(streams) / sizeof(streams[0])); shared_isr(streams, ARRAY_SIZE(streams));
} }
#endif #endif