Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into master3
Browse files Browse the repository at this point in the history
  • Loading branch information
eFiniLan committed Jun 12, 2024
2 parents 6898442 + faa1802 commit e735420
Show file tree
Hide file tree
Showing 24 changed files with 1,249 additions and 2,318 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ repos:
additional_dependencies: ['git+https://github.com/numpy/numpy-stubs', 'types-requests', 'types-atomicwrites',
'types-pycurl']
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
rev: v0.4.7
hooks:
- id: ruff
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The panda firmware is written for its use in conjuction with [openpilot](https:/

These are the [CI regression tests](https://github.com/commaai/panda/actions) we have in place:
* A generic static code analysis is performed by [cppcheck](https://github.com/danmar/cppcheck/).
* In addition, [cppcheck](https://github.com/danmar/cppcheck/) has a specific addon to check for [MISRA C:2012](https://www.misra.org.uk/MISRAHome/MISRAC2012/tabid/196/Default.aspx) violations. See [current coverage](https://github.com/commaai/panda/blob/master/tests/misra/coverage_table).
* In addition, [cppcheck](https://github.com/danmar/cppcheck/) has a specific addon to check for [MISRA C:2012](https://misra.org.uk/) violations. See [current coverage](https://github.com/commaai/panda/blob/master/tests/misra/coverage_table).
* Compiler options are relatively strict: the flags `-Wall -Wextra -Wstrict-prototypes -Werror` are enforced.
* The [safety logic](https://github.com/commaai/panda/tree/master/board/safety) is tested and verified by [unit tests](https://github.com/commaai/panda/tree/master/tests/safety) for each supported car variant.
to ensure that the behavior remains unchanged.
Expand Down Expand Up @@ -78,7 +78,8 @@ For example, to receive CAN messages:
```
And to send one on bus 0:
``` python
>>> panda.can_send(0x1aa, "message", 0)
>>> panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
>>> panda.can_send(0x1aa, b'message', 0)
```
Note that you may have to setup [udev rules](https://github.com/commaai/panda/tree/master/drivers/linux) for Linux, such as
``` bash
Expand Down
4 changes: 2 additions & 2 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ def build_project(project_name, project, extra_flags):
'..',
panda_root,
f"{panda_root}/board/",
f"{panda_root}/board/stm32f4/inc",
f"{panda_root}/board/stm32h7/inc",
]

env = Environment(
Expand Down Expand Up @@ -138,6 +136,7 @@ base_project_f4 = {
"-mhard-float",
"-DSTM32F4",
"-DSTM32F413xx",
"-Iboard/stm32f4/inc",
"-mfpu=fpv4-sp-d16",
"-fsingle-precision-constant",
"-Os",
Expand All @@ -155,6 +154,7 @@ base_project_h7 = {
"-mhard-float",
"-DSTM32H7",
"-DSTM32H725xx",
"-Iboard/stm32h7/inc",
"-mfpu=fpv5-d16",
"-fsingle-precision-constant",
"-Os",
Expand Down
4 changes: 2 additions & 2 deletions board/can_comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void comms_can_write(const uint8_t *data, uint32_t len) {
if (can_write_buffer.ptr != 0U) {
if (can_write_buffer.tail_size <= (len - pos)) {
// we have enough data to complete the buffer
CANPacket_t to_push;
CANPacket_t to_push = {0};
(void)memcpy(&can_write_buffer.data[can_write_buffer.ptr], &data[pos], can_write_buffer.tail_size);
can_write_buffer.ptr += can_write_buffer.tail_size;
pos += can_write_buffer.tail_size;
Expand All @@ -89,7 +89,7 @@ void comms_can_write(const uint8_t *data, uint32_t len) {
while (pos < len) {
uint32_t pckt_len = CANPACKET_HEAD_SIZE + dlc_to_len[(data[pos] >> 4U)];
if ((pos + pckt_len) <= len) {
CANPacket_t to_push;
CANPacket_t to_push = {0};
(void)memcpy(&to_push, &data[pos], pckt_len);
can_send(&to_push, to_push.bus, false);
pos += pckt_len;
Expand Down
6 changes: 3 additions & 3 deletions board/drivers/can_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ void can_set_forwarding(uint8_t from, uint8_t to) {

void ignition_can_hook(CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);
int len = GET_LEN(to_push);

if (bus == 0) {
int addr = GET_ADDR(to_push);
int len = GET_LEN(to_push);

// GM exception
if ((addr == 0x1F1) && (len == 8)) {
// SystemPowerMode (2=Run, 3=Crank Request)
Expand Down
4 changes: 2 additions & 2 deletions board/drivers/registers.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ void register_set(volatile uint32_t *addr, uint32_t val, uint32_t mask){
// Set individual bits. Also add them to the check_mask.
// Do not use this to change bits that get reset by the hardware
void register_set_bits(volatile uint32_t *addr, uint32_t val) {
return register_set(addr, val, val);
register_set(addr, val, val);
}

// Clear individual bits. Also add them to the check_mask.
// Do not use this to clear bits that get set by the hardware
void register_clear_bits(volatile uint32_t *addr, uint32_t val) {
return register_set(addr, (~val), val);
register_set(addr, (~val), val);
}

// To be called periodically
Expand Down
8 changes: 6 additions & 2 deletions board/drivers/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ uint16_t spi_data_len_miso;
uint16_t spi_checksum_error_count = 0;
bool spi_can_tx_ready = false;

const char version_text[] = "VERSION";
const unsigned char version_text[] = "VERSION";

#define SPI_HEADER_SIZE 7U

Expand Down Expand Up @@ -152,7 +152,7 @@ void spi_rx_done(void) {
if (checksum_valid) {
if (spi_endpoint == 0U) {
if (spi_data_len_mosi >= sizeof(ControlPacket_t)) {
ControlPacket_t ctrl;
ControlPacket_t ctrl = {0};
(void)memcpy(&ctrl, &spi_buf_rx[SPI_HEADER_SIZE], sizeof(ControlPacket_t));
response_len = comms_control_handler(&ctrl, &spi_buf_tx[3]);
response_ack = true;
Expand Down Expand Up @@ -182,6 +182,10 @@ void spi_rx_done(void) {
} else {
print("SPI: did expect data for can_write\n");
}
} else if (spi_endpoint == 0xABU) {
// test endpoint, send max response length
response_len = spi_data_len_miso;
response_ack = true;
} else {
print("SPI: unexpected endpoint"); puth(spi_endpoint); print("\n");
}
Expand Down
7 changes: 6 additions & 1 deletion board/early_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
extern void *g_pfnVectors;
extern uint32_t enter_bootloader_mode;

typedef void (*bootloader_fcn)(void);
typedef bootloader_fcn *bootloader_fcn_ptr;

void jump_to_bootloader(void) {
// do enter bootloader
enter_bootloader_mode = 0;
void (*bootloader)(void) = (void (*)(void)) (*((uint32_t *)BOOTLOADER_ADDRESS));

bootloader_fcn_ptr bootloader_ptr = (bootloader_fcn_ptr)BOOTLOADER_ADDRESS;
bootloader_fcn bootloader = *bootloader_ptr;

// jump to bootloader
enable_interrupts();
Expand Down
3 changes: 3 additions & 0 deletions board/jungle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def set_ignition(self, enabled):
def set_can_silent(self, silent):
self._handle.controlWrite(PandaJungle.REQUEST_OUT, 0xf5, int(silent), 0, b'')

def set_generated_can(self, enabled):
self._handle.controlWrite(PandaJungle.REQUEST_OUT, 0xa4, int(enabled), 0, b'')

# ******************* serial *******************

def debug_read(self):
Expand Down
34 changes: 33 additions & 1 deletion board/jungle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ uint32_t loop_counter = 0U;
uint16_t button_press_cnt = 0U;
void tick_handler(void) {
if (TICK_TIMER->SR != 0) {
if (generated_can_traffic) {
for (int i = 0; i < 3; i++) {
if (can_health[i].transmit_error_cnt >= 128) {
(void)llcan_init(CANIF_FROM_CAN_NUM(i));
}
}
}

// tick drivers at 8Hz
usb_tick();

Expand Down Expand Up @@ -193,8 +201,32 @@ int main(void) {
#endif

// LED should keep on blinking all the time
uint64_t cnt = 0;
uint32_t cnt = 0;
for (cnt=0;;cnt++) {
if (generated_can_traffic) {
// fill up all the queues
can_ring *qs[] = {&can_tx1_q, &can_tx2_q, &can_tx3_q};
for (int j = 0; j < 3; j++) {
for (uint16_t n = 0U; n < can_slots_empty(qs[j]); n++) {
uint16_t i = cnt % 100U;
CANPacket_t to_send;
to_send.returned = 0U;
to_send.rejected = 0U;
to_send.extended = 0U;
to_send.addr = 0x200U + i;
to_send.bus = i % 3U;
to_send.data_len_code = i % 8U;
(void)memcpy(to_send.data, "\xff\xff\xff\xff\xff\xff\xff\xff", dlc_to_len[to_send.data_len_code]);
can_set_checksum(&to_send);

can_send(&to_send, to_send.bus, true);
}
}

delay(1000);
continue;
}

// useful for debugging, fade breaks = panda is overloaded
for (uint32_t fade = 0U; fade < MAX_LED_FADE; fade += 1U) {
current_board->set_led(LED_RED, true);
Expand Down
8 changes: 7 additions & 1 deletion board/jungle/main_comms.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern int _app_start[0xc000]; // Only first 3 sectors of size 0x4000 are used

bool generated_can_traffic = false;

int get_jungle_health_pkt(void *dat) {
COMPILE_TIME_ASSERT(sizeof(struct jungle_health_t) <= USBPACKET_MAX_SIZE);
struct jungle_health_t * health = (struct jungle_health_t*)dat;
Expand Down Expand Up @@ -58,10 +60,14 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
case 0xa2:
current_board->set_ignition((req->param1 == 1U));
break;
// **** 0xa0: Set panda power per channel by bitmask.
// **** 0xa3: Set panda power per channel by bitmask.
case 0xa3:
current_board->set_panda_individual_power(req->param1, (req->param2 > 0U));
break;
// **** 0xa4: Enable generated CAN traffic.
case 0xa4:
generated_can_traffic = (req->param1 > 0U);
break;
// **** 0xa8: get microsecond timer
case 0xa8:
time = microsecond_timer_get();
Expand Down
3 changes: 3 additions & 0 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ void set_safety_mode(uint16_t mode, uint16_t param) {
heartbeat_counter = 0U;
heartbeat_lost = false;
if (current_board->has_obd) {
// Clear any pending messages in the can core (i.e. sending while comma power is unplugged)
// TODO: rewrite using hardware queues rather than fifo to cancel specific messages
llcan_clear_send(CANIF_FROM_CAN_NUM(1));
if (param == 0U) {
current_board->set_can_mode(CAN_MODE_OBD_CAN2);
} else {
Expand Down
2 changes: 1 addition & 1 deletion board/provision.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define PROVISION_CHUNK_LEN 0x20

const char unprovisioned_text[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
const unsigned char unprovisioned_text[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";

void get_provision_chunk(uint8_t *resp) {
(void)memcpy(resp, (uint8_t *)PROVISION_CHUNK_ADDRESS, PROVISION_CHUNK_LEN);
Expand Down
3 changes: 1 addition & 2 deletions board/safety/safety_mazda.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ static void mazda_rx_hook(const CANPacket_t *to_push) {

static bool mazda_tx_hook(const CANPacket_t *to_send) {
bool tx = true;
int addr = GET_ADDR(to_send);
int bus = GET_BUS(to_send);

// Check if msg is sent on the main BUS
if (bus == MAZDA_MAIN) {
int addr = GET_ADDR(to_send);

// steer cmd checks
if (addr == MAZDA_LKAS) {
Expand Down
2 changes: 1 addition & 1 deletion board/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define GET_BIT(msg, b) ((bool)!!(((msg)->data[((b) / 8U)] >> ((b) % 8U)) & 0x1U))
#define GET_BYTE(msg, b) ((msg)->data[(b)])
#define GET_FLAG(value, mask) (((__typeof__(mask))(value) & (mask)) == (mask))
#define GET_FLAG(value, mask) (((__typeof__(mask))(value) & (mask)) == (mask)) // cppcheck-suppress misra-c2012-1.2; allow __typeof__

#define BUILD_SAFETY_CFG(rx, tx) ((safety_config){(rx), (sizeof((rx)) / sizeof((rx)[0])), \
(tx), (sizeof((tx)) / sizeof((tx)[0]))})
Expand Down
Loading

0 comments on commit e735420

Please sign in to comment.