|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2024, |
| 5 | + * Romain Reignier <romain@reignier.sh> |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +#include "board.h" |
| 28 | +#include "config.h" |
| 29 | +#include "device.h" |
| 30 | +#include "gpio.h" |
| 31 | +#include "usbd_gs_can.h" |
| 32 | + |
| 33 | +/* |
| 34 | + * LED_RX PA0 |
| 35 | + * LED_TX PA1 |
| 36 | + * LED_READY PA2 |
| 37 | + * USB_P PA12 |
| 38 | + * USB_N PA11 |
| 39 | + * CAN1_RX PB8 |
| 40 | + * CAN1_TX PB9 |
| 41 | + */ |
| 42 | + |
| 43 | +#define LEDRX_GPIO_Port GPIOA |
| 44 | +#define LEDRX_Pin GPIO_PIN_0 |
| 45 | +#define LEDRX_Mode GPIO_MODE_OUTPUT_PP |
| 46 | +#define LEDRX_Active_High 1 |
| 47 | + |
| 48 | +#define LEDTX_GPIO_Port GPIOA |
| 49 | +#define LEDTX_Pin GPIO_PIN_1 |
| 50 | +#define LEDTX_Mode GPIO_MODE_OUTPUT_PP |
| 51 | +#define LEDTX_Active_High 1 |
| 52 | + |
| 53 | +#define LEDREADY_GPIO_Port GPIOA |
| 54 | +#define LEDREADY_Pin GPIO_PIN_2 |
| 55 | +#define LEDREADY_Mode GPIO_MODE_OUTPUT_PP |
| 56 | +#define LEDREADY_Active_High 1 |
| 57 | + |
| 58 | +static void candlelightfd_setup(USBD_GS_CAN_HandleTypeDef *hcan) |
| 59 | +{ |
| 60 | + GPIO_InitTypeDef GPIO_InitStruct = { }; |
| 61 | + |
| 62 | + UNUSED(hcan); |
| 63 | + |
| 64 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 65 | + __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 66 | + |
| 67 | + /* LEDs */ |
| 68 | + HAL_GPIO_WritePin(LEDRX_GPIO_Port, LEDRX_Pin, GPIO_INIT_STATE(LEDRX_Active_High)); |
| 69 | + GPIO_InitStruct.Pin = LEDRX_Pin; |
| 70 | + GPIO_InitStruct.Mode = LEDRX_Mode; |
| 71 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 72 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 73 | + HAL_GPIO_Init(LEDRX_GPIO_Port, &GPIO_InitStruct); |
| 74 | + |
| 75 | + HAL_GPIO_WritePin(LEDTX_GPIO_Port, LEDTX_Pin, GPIO_INIT_STATE(LEDTX_Active_High)); |
| 76 | + GPIO_InitStruct.Pin = LEDTX_Pin; |
| 77 | + GPIO_InitStruct.Mode = LEDTX_Mode; |
| 78 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 79 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 80 | + HAL_GPIO_Init(LEDTX_GPIO_Port, &GPIO_InitStruct); |
| 81 | + |
| 82 | + /* Switch ON the LED READY on start, not driven by the firmware */ |
| 83 | + HAL_GPIO_WritePin(LEDREADY_GPIO_Port, LEDREADY_Pin, GPIO_INIT_STATE(LEDREADY_Active_High)); |
| 84 | + GPIO_InitStruct.Pin = LEDREADY_Pin; |
| 85 | + GPIO_InitStruct.Mode = LEDREADY_Mode; |
| 86 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 87 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 88 | + HAL_GPIO_Init(LEDREADY_GPIO_Port, &GPIO_InitStruct); |
| 89 | + |
| 90 | + /* FDCAN */ |
| 91 | + RCC_PeriphCLKInitTypeDef PeriphClkInit = { |
| 92 | + .PeriphClockSelection = RCC_PERIPHCLK_FDCAN, |
| 93 | + .FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL, |
| 94 | + }; |
| 95 | + |
| 96 | + HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); |
| 97 | + __HAL_RCC_FDCAN_CLK_ENABLE(); |
| 98 | + |
| 99 | + /* FDCAN1_RX, FDCAN1_TX */ |
| 100 | + GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; |
| 101 | + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 102 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 103 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 104 | + GPIO_InitStruct.Alternate = GPIO_AF3_FDCAN1; |
| 105 | + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 106 | +} |
| 107 | + |
| 108 | +const struct board_config config = { |
| 109 | + .setup = candlelightfd_setup, |
| 110 | + .channel[0] = { |
| 111 | + .interface = FDCAN1, |
| 112 | + .leds = { |
| 113 | + [LED_RX] = { |
| 114 | + .port = LEDRX_GPIO_Port, |
| 115 | + .pin = LEDRX_Pin, |
| 116 | + .active_high = LEDRX_Active_High, |
| 117 | + }, |
| 118 | + [LED_TX] = { |
| 119 | + .port = LEDTX_GPIO_Port, |
| 120 | + .pin = LEDTX_Pin, |
| 121 | + .active_high = LEDTX_Active_High, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | +}; |
0 commit comments