Skip to content

Commit 3c3aba3

Browse files
authored
Misra 10.4: Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category (#240)
* Almost done with 10.4, a couple of non-obvious violations remaining
1 parent f2a3a17 commit 3c3aba3

File tree

11 files changed

+97
-87
lines changed

11 files changed

+97
-87
lines changed

board/config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
__typeof__ (b) _b = (b); \
3535
(_a > _b) ? _a : _b; })
3636

37-
#define MAX_RESP_LEN 0x40
37+
#define MAX_RESP_LEN 0x40U
3838

3939
#endif
4040

board/drivers/can.h

+15-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ typedef struct {
1212
#define CAN_BUS_RET_FLAG 0x80U
1313
#define CAN_BUS_NUM_MASK 0x7FU
1414

15-
#define BUS_MAX 4
15+
#define BUS_MAX 4U
1616

1717
extern int can_live, pending_can_live;
1818

@@ -63,8 +63,11 @@ bool can_pop(can_ring *q, CAN_FIFOMailBox_TypeDef *elem) {
6363
enter_critical_section();
6464
if (q->w_ptr != q->r_ptr) {
6565
*elem = q->elems[q->r_ptr];
66-
if ((q->r_ptr + 1) == q->fifo_size) q->r_ptr = 0;
67-
else q->r_ptr += 1;
66+
if ((q->r_ptr + 1U) == q->fifo_size) {
67+
q->r_ptr = 0;
68+
} else {
69+
q->r_ptr += 1U;
70+
}
6871
ret = 1;
6972
}
7073
exit_critical_section();
@@ -77,8 +80,11 @@ bool can_push(can_ring *q, CAN_FIFOMailBox_TypeDef *elem) {
7780
uint32_t next_w_ptr;
7881

7982
enter_critical_section();
80-
if ((q->w_ptr + 1) == q->fifo_size) next_w_ptr = 0;
81-
else next_w_ptr = q->w_ptr + 1;
83+
if ((q->w_ptr + 1U) == q->fifo_size) {
84+
next_w_ptr = 0;
85+
} else {
86+
next_w_ptr = q->w_ptr + 1U;
87+
}
8288
if (next_w_ptr != q->r_ptr) {
8389
q->elems[q->w_ptr] = *elem;
8490
q->w_ptr = next_w_ptr;
@@ -130,15 +136,15 @@ void can_set_speed(uint8_t can_number) {
130136
CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
131137
uint8_t bus_number = BUS_NUM_FROM_CAN_NUM(can_number);
132138

133-
if (!llcan_set_speed(CAN, can_speed[bus_number], can_loopback, can_silent & (1U << can_number))) {
139+
if (!llcan_set_speed(CAN, can_speed[bus_number], can_loopback, (unsigned int)(can_silent) & (1U << can_number))) {
134140
puts("CAN init FAILED!!!!!\n");
135141
puth(can_number); puts(" ");
136142
puth(BUS_NUM_FROM_CAN_NUM(can_number)); puts("\n");
137143
}
138144
}
139145

140146
void can_init(uint8_t can_number) {
141-
if (can_number != 0xff) {
147+
if (can_number != 0xffU) {
142148
CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
143149
set_can_enable(CAN, 1);
144150
can_set_speed(can_number);
@@ -229,7 +235,7 @@ void can_sce(CAN_TypeDef *CAN) {
229235
// ***************************** CAN *****************************
230236

231237
void process_can(uint8_t can_number) {
232-
if (can_number != 0xff) {
238+
if (can_number != 0xffU) {
233239

234240
enter_critical_section();
235241

@@ -343,7 +349,7 @@ void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number) {
343349
// add CAN packet to send queue
344350
// bus number isn't passed through
345351
to_push->RDTR &= 0xF;
346-
if ((bus_number == 3) && (can_num_lookup[3] == 0xFF)) {
352+
if ((bus_number == 3U) && (can_num_lookup[3] == 0xFFU)) {
347353
// TODO: why uint8 bro? only int8?
348354
bitbang_gmlan(to_push);
349355
} else {

board/drivers/gmlan_alt.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int append_crc(char *in, int in_len) {
4444
unsigned int crc = 0;
4545
for (int i = 0; i < in_len; i++) {
4646
crc <<= 1;
47-
if ((in[i] ^ ((crc >> 15) & 1U)) != 0) {
47+
if ((in[i] ^ ((crc >> 15) & 1U)) != 0U) {
4848
crc = crc ^ 0x4599U;
4949
}
5050
crc &= 0x7fffU;
@@ -95,7 +95,7 @@ int get_bit_message(char *out, CAN_FIFOMailBox_TypeDef *to_bang) {
9595
// extended identifier
9696
len = append_int(pkt, len, to_bang->RIR >> 21, 11); // Identifier
9797
len = append_int(pkt, len, 3, 2); // SRR+IDE
98-
len = append_int(pkt, len, (to_bang->RIR >> 3) & ((1U << 18) - 1), 18); // Identifier
98+
len = append_int(pkt, len, (to_bang->RIR >> 3) & ((1U << 18) - 1U), 18); // Identifier
9999
len = append_int(pkt, len, 0, 3); // RTR+r1+r0
100100
} else {
101101
// standard identifier

board/drivers/llcan.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// this is needed for 1 mbps support
2-
#define CAN_QUANTA 8
2+
#define CAN_QUANTA 8U
33
#define CAN_SEQ1 6 // roundf(quanta * 0.875f) - 1;
44
#define CAN_SEQ2 1 // roundf(quanta * 0.125f);
55

6-
#define CAN_PCLK 24000
6+
#define CAN_PCLK 24000U
77
// 333 = 33.3 kbps
88
// 5000 = 500 kbps
9-
#define can_speed_to_prescaler(x) (CAN_PCLK / CAN_QUANTA * 10 / (x))
9+
#define can_speed_to_prescaler(x) (CAN_PCLK / CAN_QUANTA * 10U / (x))
1010

1111
void puts(const char *a);
1212

@@ -18,7 +18,7 @@ bool llcan_set_speed(CAN_TypeDef *CAN, uint32_t speed, bool loopback, bool silen
1818
// set time quanta from defines
1919
CAN->BTR = (CAN_BTR_TS1_0 * (CAN_SEQ1-1)) |
2020
(CAN_BTR_TS2_0 * (CAN_SEQ2-1)) |
21-
(can_speed_to_prescaler(speed) - 1);
21+
(can_speed_to_prescaler(speed) - 1U);
2222

2323
// silent loopback mode for debugging
2424
if (loopback) {
@@ -51,7 +51,7 @@ void llcan_init(CAN_TypeDef *CAN) {
5151
CAN->sFilterRegister[0].FR2 = 0;
5252
CAN->sFilterRegister[14].FR1 = 0;
5353
CAN->sFilterRegister[14].FR2 = 0;
54-
CAN->FA1R |= 1 | (1U << 14);
54+
CAN->FA1R |= 1U | (1U << 14);
5555

5656
CAN->FMR &= ~(CAN_FMR_FINIT);
5757

board/drivers/spi.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ void DMA2_Stream3_IRQHandler(void) {
113113
}
114114

115115
void EXTI4_IRQHandler(void) {
116-
volatile int pr = EXTI->PR & (1U << 4);
116+
volatile unsigned int pr = EXTI->PR & (1U << 4);
117117
#ifdef DEBUG_SPI
118118
puts("exti4\n");
119119
#endif
120120
// SPI CS falling
121-
if ((pr & (1U << 4)) != 0) {
121+
if ((pr & (1U << 4)) != 0U) {
122122
spi_total_count = 0;
123123
spi_rx_dma(spi_buf, 0x14);
124124
}

board/drivers/uart.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// IRQs: USART1, USART2, USART3, UART5
22

3-
#define FIFO_SIZE 0x400
3+
#define FIFO_SIZE 0x400U
44
typedef struct uart_ring {
55
volatile uint16_t w_ptr_tx;
66
volatile uint16_t r_ptr_tx;
@@ -81,7 +81,7 @@ void uart_ring_process(uart_ring *q) {
8181
if (q->w_ptr_tx != q->r_ptr_tx) {
8282
if ((sr & USART_SR_TXE) != 0) {
8383
q->uart->DR = q->elems_tx[q->r_ptr_tx];
84-
q->r_ptr_tx = (q->r_ptr_tx + 1) % FIFO_SIZE;
84+
q->r_ptr_tx = (q->r_ptr_tx + 1U) % FIFO_SIZE;
8585
}
8686
// there could be more to send
8787
q->uart->CR1 |= USART_CR1_TXEIE;
@@ -93,7 +93,7 @@ void uart_ring_process(uart_ring *q) {
9393
if ((sr & USART_SR_RXNE) || (sr & USART_SR_ORE)) {
9494
uint8_t c = q->uart->DR; // TODO: can drop packets
9595
if (q != &esp_ring) {
96-
uint16_t next_w_ptr = (q->w_ptr_rx + 1) % FIFO_SIZE;
96+
uint16_t next_w_ptr = (q->w_ptr_rx + 1U) % FIFO_SIZE;
9797
if (next_w_ptr != q->r_ptr_rx) {
9898
q->elems_rx[q->w_ptr_rx] = c;
9999
q->w_ptr_rx = next_w_ptr;
@@ -124,7 +124,7 @@ bool getc(uart_ring *q, char *elem) {
124124
enter_critical_section();
125125
if (q->w_ptr_rx != q->r_ptr_rx) {
126126
if (elem != NULL) *elem = q->elems_rx[q->r_ptr_rx];
127-
q->r_ptr_rx = (q->r_ptr_rx + 1) % FIFO_SIZE;
127+
q->r_ptr_rx = (q->r_ptr_rx + 1U) % FIFO_SIZE;
128128
ret = true;
129129
}
130130
exit_critical_section();
@@ -137,7 +137,7 @@ bool injectc(uart_ring *q, char elem) {
137137
uint16_t next_w_ptr;
138138

139139
enter_critical_section();
140-
next_w_ptr = (q->w_ptr_rx + 1) % FIFO_SIZE;
140+
next_w_ptr = (q->w_ptr_rx + 1U) % FIFO_SIZE;
141141
if (next_w_ptr != q->r_ptr_rx) {
142142
q->elems_rx[q->w_ptr_rx] = elem;
143143
q->w_ptr_rx = next_w_ptr;
@@ -153,7 +153,7 @@ bool putc(uart_ring *q, char elem) {
153153
uint16_t next_w_ptr;
154154

155155
enter_critical_section();
156-
next_w_ptr = (q->w_ptr_tx + 1) % FIFO_SIZE;
156+
next_w_ptr = (q->w_ptr_tx + 1U) % FIFO_SIZE;
157157
if (next_w_ptr != q->r_ptr_tx) {
158158
q->elems_tx[q->w_ptr_tx] = elem;
159159
q->w_ptr_tx = next_w_ptr;
@@ -195,10 +195,10 @@ void clear_uart_buff(uart_ring *q) {
195195

196196
// ***************************** start UART code *****************************
197197

198-
#define __DIV(_PCLK_, _BAUD_) (((_PCLK_) * 25) / (4 * (_BAUD_)))
199-
#define __DIVMANT(_PCLK_, _BAUD_) (__DIV((_PCLK_), (_BAUD_)) / 100)
200-
#define __DIVFRAQ(_PCLK_, _BAUD_) ((((__DIV((_PCLK_), (_BAUD_)) - (__DIVMANT((_PCLK_), (_BAUD_)) * 100)) * 16) + 50) / 100)
201-
#define __USART_BRR(_PCLK_, _BAUD_) ((__DIVMANT((_PCLK_), (_BAUD_)) << 4) | (__DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0F))
198+
#define __DIV(_PCLK_, _BAUD_) (((_PCLK_) * 25U) / (4U * (_BAUD_)))
199+
#define __DIVMANT(_PCLK_, _BAUD_) (__DIV((_PCLK_), (_BAUD_)) / 100U)
200+
#define __DIVFRAQ(_PCLK_, _BAUD_) ((((__DIV((_PCLK_), (_BAUD_)) - (__DIVMANT((_PCLK_), (_BAUD_)) * 100U)) * 16U) + 50U) / 100U)
201+
#define __USART_BRR(_PCLK_, _BAUD_) ((__DIVMANT((_PCLK_), (_BAUD_)) << 4) | (__DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0FU))
202202

203203
void uart_set_baud(USART_TypeDef *u, unsigned int baud) {
204204
if (u == USART1) {
@@ -226,7 +226,7 @@ void uart_dma_drain(void) {
226226
unsigned int i;
227227
for (i = 0; i < (USART1_DMA_LEN - DMA2_Stream5->NDTR); i++) {
228228
char c = usart1_dma[i];
229-
uint16_t next_w_ptr = (q->w_ptr_rx + 1) % FIFO_SIZE;
229+
uint16_t next_w_ptr = (q->w_ptr_rx + 1U) % FIFO_SIZE;
230230
if (next_w_ptr != q->r_ptr_rx) {
231231
q->elems_rx[q->w_ptr_rx] = c;
232232
q->w_ptr_rx = next_w_ptr;
@@ -322,11 +322,11 @@ void putui(uint32_t i) {
322322
str[idx] = '\0';
323323
idx--;
324324
do {
325-
str[idx] = (i_copy % 10) + 0x30;
325+
str[idx] = (i_copy % 10U) + 0x30U;
326326
idx--;
327327
i_copy /= 10;
328-
} while (i_copy != 0);
329-
puts(str + idx + 1);
328+
} while (i_copy != 0U);
329+
puts(str + idx + 1U);
330330
}
331331

332332
void puth(unsigned int i) {

board/drivers/usb.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ int current_int0_alt_setting = 0;
394394
// packet read and write
395395

396396
void *USB_ReadPacket(void *dest, uint16_t len) {
397-
uint32_t i=0;
398-
uint32_t count32b = (len + 3) / 4;
397+
uint32_t i = 0;
398+
uint32_t count32b = (len + 3U) / 4U;
399399

400400
for ( i = 0; i < count32b; i++) {
401401
// packed?
@@ -411,9 +411,9 @@ void USB_WritePacket(const uint8_t *src, uint16_t len, uint32_t ep) {
411411
hexdump(src, len);
412412
#endif
413413

414-
uint8_t numpacket = (len+(MAX_RESP_LEN-1))/MAX_RESP_LEN;
414+
uint8_t numpacket = (len + (MAX_RESP_LEN - 1U)) / MAX_RESP_LEN;
415415
uint32_t count32b = 0, i = 0;
416-
count32b = (len + 3) / 4;
416+
count32b = (len + 3U) / 4U;
417417

418418
// bullshit
419419
USBx_INEP(ep)->DIEPTSIZ = ((numpacket << 19) & USB_OTG_DIEPTSIZ_PKTCNT) |
@@ -983,12 +983,12 @@ void usb_irqhandler(void) {
983983
puts(" IN PACKET QUEUE\n");
984984
#endif
985985

986-
if ((ep0_txlen != 0) && ((USBx_INEP(0)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= 0x40)) {
986+
if ((ep0_txlen != 0U) && ((USBx_INEP(0)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= 0x40U)) {
987987
uint16_t len = MIN(ep0_txlen, 0x40);
988988
USB_WritePacket(ep0_txdata, len, 0);
989989
ep0_txdata += len;
990990
ep0_txlen -= len;
991-
if (ep0_txlen == 0) {
991+
if (ep0_txlen == 0U) {
992992
ep0_txdata = NULL;
993993
USBx_DEVICE->DIEPEMPMSK &= ~1;
994994
USBx_OUTEP(0)->DOEPCTL |= USB_OTG_DOEPCTL_CNAK;

board/gpio.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,13 @@ void early(void) {
410410
// if wrong chip, reboot
411411
volatile unsigned int id = DBGMCU->IDCODE;
412412
#ifdef STM32F4
413-
if ((id&0xFFF) != 0x463) enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
413+
if ((id & 0xFFFU) != 0x463U) {
414+
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
415+
}
414416
#else
415-
if ((id&0xFFF) != 0x411) enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
417+
if ((id & 0xFFFU) != 0x411U) {
418+
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
419+
}
416420
#endif
417421

418422
// setup interrupt table

0 commit comments

Comments
 (0)