From 66b0b3fe286967131fad1ca3ffbebf7ed55a1c75 Mon Sep 17 00:00:00 2001 From: Nicholas DePatie <80368116+nwdepatie@users.noreply.github.com> Date: Thu, 28 Mar 2024 20:19:04 -0400 Subject: [PATCH 1/2] Implementing more concise implementation of CAN callbacks (#104) * Implementing more concise implementation of CAN callbacks * Actually getting it building in Cerberus with some mods * Removing unnecessary functions in favor of STM generated callbacks --- middleware/include/c_utils.h | 12 +++++++ platforms/stm32f405/include/can.h | 10 +++--- platforms/stm32f405/src/can.c | 60 ++++--------------------------- 3 files changed, 23 insertions(+), 59 deletions(-) create mode 100644 middleware/include/c_utils.h diff --git a/middleware/include/c_utils.h b/middleware/include/c_utils.h new file mode 100644 index 0000000..1834b6d --- /dev/null +++ b/middleware/include/c_utils.h @@ -0,0 +1,12 @@ +#ifndef C_UTILS +#define C_UTILS + +/* + * Will retrieve the container of a certain pointer given the container type and its pointer + * Trick pulled from Linux Kernel + */ +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + +#endif /* C_UTILS */ \ No newline at end of file diff --git a/platforms/stm32f405/include/can.h b/platforms/stm32f405/include/can.h index d759bf9..75d02c9 100644 --- a/platforms/stm32f405/include/can.h +++ b/platforms/stm32f405/include/can.h @@ -7,17 +7,17 @@ #include "stm32f4xx_hal.h" #include "stm32f4xx_hal_can.h" +#include "c_utils.h" -/* function pointer type for the callback */ -typedef void (*can_callback_t)(CAN_HandleTypeDef *hcan); +/* + * NOTE: For implementing callbacks, generate NVIC for selected CAN bus, then implement in + * `stm32xxxx_it.c`, which STM32CubeMX generates + */ typedef struct{ CAN_HandleTypeDef *hcan; const uint16_t *id_list; uint8_t id_list_len; - - /* desired behavior varies by app - so implement this at app level */ - can_callback_t callback; } can_t; typedef struct{ diff --git a/platforms/stm32f405/src/can.c b/platforms/stm32f405/src/can.c index d41252d..5653daa 100644 --- a/platforms/stm32f405/src/can.c +++ b/platforms/stm32f405/src/can.c @@ -2,50 +2,10 @@ #include #include -/* NOTE: STM32F405 will have MAX of 3 CAN buses */ -#define MAX_CAN_BUS 3 -extern CAN_HandleTypeDef hcan1; - -can_t *can_struct_list[MAX_CAN_BUS] = {NULL, NULL, NULL}; - -static can_callback_t find_callback(CAN_HandleTypeDef *hcan) -{ - for (uint8_t i = 0; i < MAX_CAN_BUS; i++) { - if (hcan == can_struct_list[i]->hcan) - return can_struct_list[i]->callback; - } - return NULL; -} - -/* Add a CAN interfae to be searched for during the event of a callback */ -static uint8_t add_interface(can_t *interface) -{ - for (uint8_t i = 0; i < MAX_CAN_BUS; i++) { - /* Interface already added */ - if (interface->hcan == can_struct_list[i]->hcan) - return -1; - - /* If empty, add interface */ - if (can_struct_list[i]->hcan == NULL) { - can_struct_list[i] = interface; - return 0; - } - } - - /* No open slots, something is wrong */ - return -2; -} - -void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) -{ - /* Handle CAN reception event */ - can_callback_t callback = find_callback(hcan); - - if (callback != NULL) - { - callback(hcan); - } -} +/* + * NOTE: For implementing callbacks, generate NVIC for selected CAN bus, then implement in + * `stm32xxxx_it.c`, which STM32CubeMX generates + */ HAL_StatusTypeDef can_init(can_t *can) { @@ -72,7 +32,7 @@ HAL_StatusTypeDef can_init(can_t *can) sFilterConfig.FilterMaskIdHigh = 0x0000; sFilterConfig.FilterMaskIdLow = 0x0000; sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; - sFilterConfig.FilterActivation = ENABLE; + sFilterConfig.FilterActivation = ENABLE; sFilterConfig.SlaveStartFilterBank = 14; // sFilterConfig.FilterBank = 0; /* Filter bank number (0 to 27 for most STM32 series) */ @@ -95,7 +55,7 @@ HAL_StatusTypeDef can_init(can_t *can) /* set up interrupt & activate CAN */ HAL_CAN_IRQHandler(can->hcan); - + err = HAL_CAN_ActivateNotification(can->hcan, CAN_IT_RX_FIFO0_MSG_PENDING); if (err != HAL_OK) return err; @@ -103,9 +63,6 @@ HAL_StatusTypeDef can_init(can_t *can) if (err != HAL_OK) return err; - /* Override the default callback for CAN_IT_RX_FIFO0_MSG_PENDING */ - err = add_interface(can); - return err; } @@ -128,8 +85,3 @@ HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg) return HAL_OK; } - -void CAN1_RX0_IRQHandler(void) -{ - HAL_CAN_IRQHandler(&hcan1); -} \ No newline at end of file From 3816bcba8e8ec75ed077726532cf147a6b10b930 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Sun, 31 Mar 2024 13:52:39 -0400 Subject: [PATCH 2/2] Automatically build docker image (#107) * initial build_image * Update Dockerfile --- .github/workflows/build_image.yml | 50 +++++++++++++++++++++++++++++++ dev/Dockerfile | 4 +-- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/build_image.yml diff --git a/.github/workflows/build_image.yml b/.github/workflows/build_image.yml new file mode 100644 index 0000000..5c17f4f --- /dev/null +++ b/.github/workflows/build_image.yml @@ -0,0 +1,50 @@ +# +name: Create and publish a Docker image + +# Configures this workflow to run every time a change is pushed to the branch called `release`. +on: + push: + paths: + - 'dev/**' + workflow_dispatch: + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v4 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: ./dev/ + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/dev/Dockerfile b/dev/Dockerfile index c64947d..8d4a478 100644 --- a/dev/Dockerfile +++ b/dev/Dockerfile @@ -6,7 +6,7 @@ RUN apt-get update && \ apt-get -y install tzdata # Download Linux support tools -RUN apt-get install -y \ +RUN apt-get update && apt-get install -y \ build-essential \ wget \ curl \ @@ -32,7 +32,7 @@ COPY ./scripts . RUN echo "source /home/dev/scripts/alias.sh" >> ~/.bashrc -# for usb/ip +# for usb/ip comms and get binary symlinked EXPOSE 3240 RUN ln -sf /usr/lib/linux-tools-*/* /usr/bin/