Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | /* * Copyright (c) 2019 Vestas Wind Systems A/S * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr.h> #include <drivers/gpio.h> #include <power/reboot.h> #include <settings/settings.h> #include <canbus/canopen.h> #define LOG_LEVEL CONFIG_CANOPEN_LOG_LEVEL #include <logging/log.h> LOG_MODULE_REGISTER(app); #define CAN_INTERFACE DT_ALIAS_CAN_PRIMARY_LABEL #define CAN_BITRATE (DT_ALIAS_CAN_PRIMARY_BUS_SPEED / 1000) #if !defined(DT_ALIAS_CAN_PRIMARY_LABEL) #error CANopen CAN interface not set #endif #ifdef DT_ALIAS_GREEN_LED_GPIOS_CONTROLLER #define LED_GREEN_PORT DT_ALIAS_GREEN_LED_GPIOS_CONTROLLER #define LED_GREEN_PIN DT_ALIAS_GREEN_LED_GPIOS_PIN #define LED_GREEN_FLAGS DT_ALIAS_GREEN_LED_GPIOS_FLAGS #endif #ifdef DT_ALIAS_RED_LED_GPIOS_CONTROLLER #define LED_RED_PORT DT_ALIAS_RED_LED_GPIOS_CONTROLLER #define LED_RED_PIN DT_ALIAS_RED_LED_GPIOS_PIN #define LED_RED_FLAGS DT_ALIAS_RED_LED_GPIOS_FLAGS #endif #ifdef DT_ALIAS_SW0_GPIOS_CONTROLLER #define BUTTON_PORT DT_ALIAS_SW0_GPIOS_CONTROLLER #define BUTTON_PIN DT_ALIAS_SW0_GPIOS_PIN #define BUTTON_FLAGS DT_ALIAS_SW0_GPIOS_FLAGS static struct gpio_callback button_callback; #endif struct led_indicator { struct device *dev; gpio_pin_t pin; }; static struct led_indicator led_green; static struct led_indicator led_red; static u32_t counter; /** * @brief Callback for setting LED indicator state. * * @param value true if the LED indicator shall be turned on, false otherwise. * @param arg argument that was passed when LEDs were initialized. */ static void led_callback(bool value, void *arg) { struct led_indicator *led = arg; bool drive = value; if (!led || !led->dev) { return; } gpio_pin_set(led->dev, led->pin, drive); } /** * @brief Configure LED indicators pins and callbacks. * * This routine configures the GPIOs for the red and green LEDs (if * available). * * @param nmt CANopenNode NMT object. */ static void config_leds(CO_NMT_t *nmt) { #ifdef LED_GREEN_PORT led_green.dev = device_get_binding(LED_GREEN_PORT); led_green.pin = LED_GREEN_PIN; if (led_green.dev) { gpio_pin_configure(led_green.dev, LED_GREEN_PIN, GPIO_OUTPUT_INACTIVE | LED_GREEN_FLAGS); } #endif /* LED_GREEN_PORT */ #ifdef LED_RED_PORT led_red.dev = device_get_binding(LED_RED_PORT); led_red.pin = LED_RED_PIN; if (led_red.dev) { gpio_pin_configure(led_red.dev, LED_RED_PIN, GPIO_OUTPUT_INACTIVE | LED_RED_FLAGS); } #endif /* LED_RED_PORT */ canopen_leds_init(nmt, led_green.dev ? led_callback : NULL, &led_green, led_red.dev ? led_callback : NULL, &led_red); } /** * @brief Button press counter object dictionary handler function. * * This function is called upon SDO access to the button press counter * object (index 0x2102) in the object dictionary. * * @param odf_arg object dictionary function argument. * * @return SDO abort code. */ static CO_SDO_abortCode_t odf_2102(CO_ODF_arg_t *odf_arg) { u32_t value; value = CO_getUint32(odf_arg->data); if (odf_arg->reading) { return CO_SDO_AB_NONE; } if (odf_arg->subIndex != 0U) { return CO_SDO_AB_NONE; } if (value != 0) { /* Preserve old value */ memcpy(odf_arg->data, odf_arg->ODdataStorage, sizeof(u32_t)); return CO_SDO_AB_DATA_TRANSF; } LOG_INF("Resetting button press counter"); counter = 0; return CO_SDO_AB_NONE; } /** * @brief Button press interrupt callback. * * @param port GPIO device struct. * @param cb GPIO callback struct. * @param pins GPIO pin mask that triggered the interrupt. */ #ifdef BUTTON_PORT static void button_isr_callback(struct device *port, struct gpio_callback *cb, u32_t pins) { counter++; } #endif /** * @brief Configure button GPIO pin and callback. * * This routine configures the GPIO for the button (if available). */ static void config_button(void) { #ifdef BUTTON_PORT struct device *dev; int err; dev = device_get_binding(BUTTON_PORT); if (!dev) { LOG_ERR("failed to get button device"); return; } err = gpio_pin_configure(dev, BUTTON_PIN, GPIO_INPUT | BUTTON_FLAGS); gpio_init_callback(&button_callback, button_isr_callback, BIT(BUTTON_PIN)); err = gpio_add_callback(dev, &button_callback); if (err) { LOG_ERR("failed to add button callback"); return; } err = gpio_pin_interrupt_configure(dev, BUTTON_PIN, GPIO_INT_EDGE_TO_ACTIVE); if (err) { LOG_ERR("failed to enable button callback"); return; } #endif } /** * @brief Main application entry point. * * The main application thread is responsible for initializing the * CANopen stack and doing the non real-time processing. */ void main(void) { CO_NMT_reset_cmd_t reset = CO_RESET_NOT; CO_ReturnError_t err; struct device *can; u16_t timeout; u32_t elapsed; s64_t timestamp; int ret; can = device_get_binding(CAN_INTERFACE); if (!can) { LOG_ERR("CAN interface not found"); return; } ret = settings_subsys_init(); if (ret) { LOG_ERR("failed to initialize settings subsystem (err = %d)", ret); return; } ret = settings_load(); if (ret) { LOG_ERR("failed to load settings (err = %d)", ret); return; } OD_powerOnCounter++; config_button(); while (reset != CO_RESET_APP) { elapsed = 0U; /* milliseconds */ err = CO_init(can, CONFIG_CANOPEN_NODE_ID, CAN_BITRATE); if (err != CO_ERROR_NO) { LOG_ERR("CO_init failed (err = %d)", err); return; } LOG_INF("CANopen stack initialized"); canopen_storage_attach(CO->SDO[0], CO->em); config_leds(CO->NMT); CO_OD_configure(CO->SDO[0], OD_2102_buttonPressCounter, odf_2102, NULL, 0U, 0U); CO_CANsetNormalMode(CO->CANmodule[0]); while (true) { timeout = 1U; /* default timeout in milliseconds */ timestamp = k_uptime_get(); reset = CO_process(CO, (uint16_t)elapsed, &timeout); if (reset != CO_RESET_NOT) { break; } if (timeout > 0) { CO_LOCK_OD(); OD_buttonPressCounter = counter; CO_UNLOCK_OD(); ret = canopen_storage_save( CANOPEN_STORAGE_EEPROM); if (ret) { LOG_ERR("failed to save EEPROM"); } /* * Try to sleep for as long as the * stack requested and calculate the * exact time elapsed. */ k_sleep(K_MSEC(timeout)); elapsed = k_uptime_delta_32(×tamp); } else { /* * Do not sleep, more processing to be * done by the stack. */ elapsed = 0U; } } if (reset == CO_RESET_COMM) { LOG_INF("Resetting communication"); } } LOG_INF("Resetting device"); CO_delete(CAN_INTERFACE); sys_reboot(SYS_REBOOT_COLD); } |