|
| 1 | +/****************************************************************************** |
| 2 | + * @file vio_STM32L496G-DISCO.c |
| 3 | + * @brief Virtual I/O implementation for board STM32L496G-DISCO |
| 4 | + * @version V1.0.0 |
| 5 | + * @date 19. September 2024 |
| 6 | + ******************************************************************************/ |
| 7 | +/* |
| 8 | + * Copyright (c) 2024 Arm Limited (or its affiliates). |
| 9 | + * All rights reserved. |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + * |
| 13 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 14 | + * not use this file except in compliance with the License. |
| 15 | + * You may obtain a copy of the License at |
| 16 | + * |
| 17 | + * www.apache.org/licenses/LICENSE-2.0 |
| 18 | + * |
| 19 | + * Unless required by applicable law or agreed to in writing, software |
| 20 | + * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 21 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | + * See the License for the specific language governing permissions and |
| 23 | + * limitations under the License. |
| 24 | + */ |
| 25 | + |
| 26 | +/*! \page vio_STM32L496G-DISCO Physical I/O Mapping |
| 27 | +
|
| 28 | +The table below lists the physical I/O mapping of this CMSIS-Driver VIO implementation. |
| 29 | +
|
| 30 | +| Virtual I/O | Variable | Board component | Pin |
| 31 | +|:--------------|:---------------|:---------------------|:------ |
| 32 | +| vioBUTTON0 | vioSignalIn.0 | Joystick select (B2) | PC13 |
| 33 | +| vioLED0 | vioSignalOut.0 | LED green (LD2)| PB13 |
| 34 | +*/ |
| 35 | + |
| 36 | +#include "cmsis_vio.h" |
| 37 | + |
| 38 | +#include "RTE_Components.h" // Component selection |
| 39 | +#include CMSIS_device_header |
| 40 | + |
| 41 | +#if !defined CMSIS_VOUT || !defined CMSIS_VIN |
| 42 | +#include "GPIO_STM32.h" |
| 43 | +#endif |
| 44 | + |
| 45 | +// VIO input, output definitions |
| 46 | +#ifndef VIO_VALUE_NUM |
| 47 | +#define VIO_VALUE_NUM 5U // Number of values |
| 48 | +#endif |
| 49 | + |
| 50 | +// VIO input, output variables |
| 51 | +static uint32_t vioSignalIn __USED; // Memory for incoming signal |
| 52 | +static uint32_t vioSignalOut __USED; // Memory for outgoing signal |
| 53 | +static int32_t vioValue[VIO_VALUE_NUM] __USED; // Memory for value used in vioGetValue/vioSetValue |
| 54 | + |
| 55 | +#if !defined CMSIS_VOUT || !defined CMSIS_VIN |
| 56 | + |
| 57 | +// VIO Active State |
| 58 | +#define VIO_ACTIVE_LOW 0U |
| 59 | +#define VIO_ACTIVE_HIGH 1U |
| 60 | + |
| 61 | +typedef struct { |
| 62 | + uint32_t vioSignal; |
| 63 | + uint16_t pin; |
| 64 | + uint8_t pullResistor; |
| 65 | + uint8_t activeState; |
| 66 | +} pinCfg_t; |
| 67 | + |
| 68 | +#if !defined CMSIS_VOUT |
| 69 | +// VOUT Configuration |
| 70 | +static const pinCfg_t outputCfg[] = { |
| 71 | +// signal, pin, pull resistor, active state |
| 72 | + { vioLED0, GPIO_PIN_ID_PORTB(13), ARM_GPIO_PULL_NONE, VIO_ACTIVE_LOW } |
| 73 | +}; |
| 74 | +#endif |
| 75 | + |
| 76 | +#if !defined CMSIS_VIN |
| 77 | +// VIN Configuration |
| 78 | +static const pinCfg_t inputCfg[] = { |
| 79 | +// signal, pin, pull resistor, active state |
| 80 | + { vioBUTTON0, GPIO_PIN_ID_PORTC(13), ARM_GPIO_PULL_DOWN, VIO_ACTIVE_HIGH } |
| 81 | +}; |
| 82 | +#endif |
| 83 | + |
| 84 | +// External GPIO Driver |
| 85 | +extern ARM_DRIVER_GPIO Driver_GPIO0; |
| 86 | +static ARM_DRIVER_GPIO *pGPIODrv = &Driver_GPIO0; |
| 87 | +#endif |
| 88 | + |
| 89 | +// Initialize test input, output. |
| 90 | +void vioInit (void) { |
| 91 | + uint32_t n; |
| 92 | +#if !defined(CMSIS_VOUT) || !defined(CMSIS_VIN) |
| 93 | + ARM_GPIO_Pin_t pin; |
| 94 | +#endif |
| 95 | + |
| 96 | + vioSignalIn = 0U; |
| 97 | + vioSignalOut = 0U; |
| 98 | + |
| 99 | + for (n = 0U; n < VIO_VALUE_NUM; n++) { |
| 100 | + vioValue[n] = 0U; |
| 101 | + } |
| 102 | + |
| 103 | +#if !defined CMSIS_VOUT |
| 104 | + for (n = 0U; n < (sizeof(outputCfg) / sizeof(pinCfg_t)); n++) { |
| 105 | + pin = (ARM_GPIO_Pin_t)outputCfg[n].pin; |
| 106 | + pGPIODrv->Setup(pin, NULL); |
| 107 | + pGPIODrv->SetOutputMode(pin, ARM_GPIO_PUSH_PULL); |
| 108 | + pGPIODrv->SetPullResistor(pin, outputCfg[n].pullResistor); |
| 109 | + pGPIODrv->SetDirection(pin, ARM_GPIO_OUTPUT); |
| 110 | + |
| 111 | + // Set initial pin state to inactive |
| 112 | + if (outputCfg[n].activeState == VIO_ACTIVE_HIGH) { |
| 113 | + pGPIODrv->SetOutput(pin, 0U); |
| 114 | + } else { |
| 115 | + pGPIODrv->SetOutput(pin, 1U); |
| 116 | + } |
| 117 | + } |
| 118 | +#endif |
| 119 | + |
| 120 | +#if !defined CMSIS_VIN |
| 121 | + for (n = 0U; n < (sizeof(inputCfg) / sizeof(pinCfg_t)); n++) { |
| 122 | + pin = (ARM_GPIO_Pin_t)inputCfg[n].pin; |
| 123 | + pGPIODrv->Setup(pin, NULL); |
| 124 | + pGPIODrv->SetPullResistor(pin, inputCfg[n].pullResistor); |
| 125 | + pGPIODrv->SetDirection(pin, ARM_GPIO_INPUT); |
| 126 | + } |
| 127 | +#endif |
| 128 | +} |
| 129 | + |
| 130 | +// Set signal output. |
| 131 | +void vioSetSignal (uint32_t mask, uint32_t signal) { |
| 132 | +#if !defined CMSIS_VOUT |
| 133 | + ARM_GPIO_Pin_t pin; |
| 134 | + uint32_t pinValue, n; |
| 135 | +#endif |
| 136 | + |
| 137 | + vioSignalOut &= ~mask; |
| 138 | + vioSignalOut |= mask & signal; |
| 139 | + |
| 140 | +#if !defined CMSIS_VOUT |
| 141 | + // Output signals to LEDs |
| 142 | + for (n = 0U; n < (sizeof(outputCfg) / sizeof(pinCfg_t)); n++) { |
| 143 | + pin = (ARM_GPIO_Pin_t)outputCfg[n].pin; |
| 144 | + if ((mask & outputCfg[n].vioSignal) != 0U) { |
| 145 | + if ((signal & outputCfg[n].vioSignal) != 0U) { |
| 146 | + pinValue = 1U; |
| 147 | + } else { |
| 148 | + pinValue = 0U; |
| 149 | + } |
| 150 | + if (pinValue == outputCfg[n].activeState) { |
| 151 | + pGPIODrv->SetOutput(pin, 1U); |
| 152 | + } else { |
| 153 | + pGPIODrv->SetOutput(pin, 0U); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +#endif |
| 158 | +} |
| 159 | + |
| 160 | +// Get signal input. |
| 161 | +uint32_t vioGetSignal (uint32_t mask) { |
| 162 | + uint32_t signal; |
| 163 | +#if !defined CMSIS_VIN |
| 164 | + ARM_GPIO_Pin_t pin; |
| 165 | + uint32_t pinValue, n; |
| 166 | +#endif |
| 167 | + |
| 168 | +#if !defined CMSIS_VIN |
| 169 | + // Get input signals from buttons |
| 170 | + for (n = 0U; n < (sizeof(inputCfg) / sizeof(pinCfg_t)); n++) { |
| 171 | + pin = (ARM_GPIO_Pin_t)inputCfg[n].pin; |
| 172 | + if ((mask & inputCfg[n].vioSignal) != 0U) { |
| 173 | + pinValue = pGPIODrv->GetInput(pin); |
| 174 | + if (pinValue == inputCfg[n].activeState) { |
| 175 | + vioSignalIn |= inputCfg[n].vioSignal; |
| 176 | + } else { |
| 177 | + vioSignalIn &= ~inputCfg[n].vioSignal; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +#endif |
| 182 | + |
| 183 | + signal = vioSignalIn & mask; |
| 184 | + |
| 185 | + return signal; |
| 186 | +} |
| 187 | + |
| 188 | +// Set value output. |
| 189 | +// Note: vioAOUT not supported. |
| 190 | +void vioSetValue (uint32_t id, int32_t value) { |
| 191 | + uint32_t index = id; |
| 192 | +#if !defined CMSIS_VOUT |
| 193 | +// Add user variables here: |
| 194 | + |
| 195 | +#endif |
| 196 | + |
| 197 | + if (index >= VIO_VALUE_NUM) { |
| 198 | + return; /* return in case of out-of-range index */ |
| 199 | + } |
| 200 | + |
| 201 | + vioValue[index] = value; |
| 202 | + |
| 203 | +#if !defined CMSIS_VOUT |
| 204 | +// Add user code here: |
| 205 | + |
| 206 | +#endif |
| 207 | +} |
| 208 | + |
| 209 | +// Get value input. |
| 210 | +// Note: vioAIN not supported. |
| 211 | +int32_t vioGetValue (uint32_t id) { |
| 212 | + uint32_t index = id; |
| 213 | + int32_t value; |
| 214 | +#if !defined CMSIS_VIN |
| 215 | +// Add user variables here: |
| 216 | + |
| 217 | +#endif |
| 218 | + |
| 219 | + if (index >= VIO_VALUE_NUM) { |
| 220 | + return 0U; /* return 0 in case of out-of-range index */ |
| 221 | + } |
| 222 | + |
| 223 | +#if !defined CMSIS_VIN |
| 224 | +// Add user code here: |
| 225 | + |
| 226 | +#endif |
| 227 | + |
| 228 | + value = vioValue[index]; |
| 229 | + |
| 230 | + return value; |
| 231 | +} |
0 commit comments