/* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /** BusVoodoo 1-wire mode (code) * @file busvoodoo_onewire.c * @author King Kévin * @date 2018 * @note peripherals used: timer @ref onewire_master_timer */ /* standard libraries */ #include // standard integer types #include // standard utilities #include // string utilities /* STM32 (including CM3) libraries */ #include // real-time control clock library #include // general purpose input output library #include // timer library /* own libraries */ #include "global.h" // board definitions #include "print.h" // printing utilities #include "menu.h" // menu definitions #include "onewire_master.h" // 1-wire methods #include "busvoodoo_global.h" // BusVoodoo definitions #include "busvoodoo_oled.h" // OLED utilities #include "busvoodoo_onewire.h" // own definitions /** mode setup stage */ static enum busvoodoo_onewire_setting_t { BUSVOODOO_ONEWIRE_SETTING_NONE, BUSVOODOO_ONEWIRE_SETTING_PULLUP, BUSVOODOO_ONEWIRE_SETTING_POWER, BUSVOODOO_ONEWIRE_SETTING_DONE, } busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_NONE; /**< current mode setup stage */ /** if embedded pull-up resistors are used */ static bool busvoodoo_onewire_embedded_pullup = true; /** time (in ms) between slot to provide power */ static bool busvoodoo_onewire_power = false; /** setup 1-wire mode * @param[out] prefix terminal prompt prefix * @param[in] line terminal prompt line to configure mode * @return if setup is complete */ static bool busvoodoo_onewire_setup(char** prefix, const char* line) { bool complete = false; // is the setup complete if (NULL==line) { // first call busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_NONE; // re-start configuration } switch (busvoodoo_onewire_setting) { case BUSVOODOO_ONEWIRE_SETTING_NONE: busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_PULLUP; // go to first setting printf("1) use embedded pull-up resistor (2kO)\n"); printf("2) use external pull-up resistor\n"); snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "pull-up mode (1,2) [%c]", busvoodoo_onewire_embedded_pullup ? '1' : '2'); // show pull-up setting *prefix = busvoodoo_global_string; // display next setting break; case BUSVOODOO_ONEWIRE_SETTING_PULLUP: if (NULL==line || 0==strlen(line)) { // use default setting busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_POWER; // go to next setting } else if (1==strlen(line)) { // setting provided uint8_t pullup = atoi(line); // parse setting if (1==pullup || 2==pullup) { // check setting busvoodoo_onewire_embedded_pullup = (1==pullup); // remember setting busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_POWER; // go to next setting } } if (BUSVOODOO_ONEWIRE_SETTING_POWER==busvoodoo_onewire_setting) { printf("1) don't drive 1-wire data line (target uses external or parasitic power)\n"); printf("2) power 1-wire data line at 3.3V when not communicating (not multi-master compatible)\n"); snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "power source (1,2) [%c]", busvoodoo_onewire_power ? '2' : '1'); // show power setting *prefix = busvoodoo_global_string; // display next setting } break; case BUSVOODOO_ONEWIRE_SETTING_POWER: if (NULL==line || 0==strlen(line)) { // use default setting busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_DONE; // go to next setting } else if (1==strlen(line)) { // setting provided uint8_t power = atoi(line); // parse setting if (1==power || 2==power) { // check setting busvoodoo_onewire_power = (1==power); // remember setting busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_DONE; // go to next setting } } if (BUSVOODOO_ONEWIRE_SETTING_DONE==busvoodoo_onewire_setting) { // we have all settings, configure SPI onewire_master_setup(); // setup 1-wire if (busvoodoo_onewire_power) { gpio_set_mode(GPIO(ONEWIRE_MASTER_PORT), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(ONEWIRE_MASTER_PIN)); // provide power (external pull-up resistor is still require for communication) } if (busvoodoo_onewire_embedded_pullup) { if (!busvoodoo_onewire_power) { busvoodoo_embedded_pullup(true); // set embedded pull-ups } printf("use LV to set pull-up voltage\n"); } busvoodoo_led_blue_off(); // disable blue LED because there is no activity busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_NONE; // restart settings next time *prefix = "1-Wire"; // display mode busvoodoo_oled_text_left(*prefix); // set mode title on OLED display const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", NULL, "1WR", NULL, NULL, NULL, NULL}; // 1-wire mode pinout for (uint8_t i=0; i='0' && action[1]<='9') { // send decimal return busvoodoo_onewire_action(action+1, repetition, perform); // just retry without leading 0 } else { // malformed action return false; } } else if ('x'==action[0] && length>1) { // send hexadecimal value for (uint32_t i=1; i='0' && action[i]<='9') || (action[i]>='a' && action[i]<='f') || (action[i]>='A' && action[i]<='F'))) { // check for hexadecimal character return false; // not an hexadecimal string } } if (!perform) { return true; } uint32_t value = strtol(&action[1], NULL, 16); // get hex value for (uint32_t i=0; i1) { // send binary value for (uint32_t i=1; i'1') { // check for binary character return false; // not a binary string } } if (!perform) { return true; } uint32_t value = strtol(&action[1], NULL, 2); // get binary value for (uint32_t i=0; i='1' && action[0]<='9') { // send decimal value for (uint32_t i=1; i'9') { // check for decimal character return false; // not a decimal string } } if (!perform) { return true; } uint32_t value = strtol(&action[0], NULL, 10); // get decimal value for (uint32_t i=0; i=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character if (!perform) { return true; } for (uint32_t r=0; r> 8, code >> 16, code >> 24, code >> 32, code >> 40, code >> 48, code >> 56}; printf("0x%016X: family code=0x%02x (possible device(s): %s), serial number=0x%012X, CRC=0x%02X (%s)\n", code, (uint8_t)code, device, (code >> 8) & 0xffffffffffffUL, (code >> 56) & 0xff, onewire_master_crc(crc_data, LENGTH(crc_data)) ? "ERROR" : "OK"); codes++; // remember we found a code } while (presence && next && !user_input_available); printf("%U ROM code(s)%s found\n", codes, alarm ? " with alarm" : ""); if (user_input_available) { // user interrupted flow user_input_get(); // discard user input } } if (busvoodoo_onewire_power) { gpio_set_mode(GPIO(ONEWIRE_MASTER_PORT), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(ONEWIRE_MASTER_PIN)); // provide power on data line } } /** 1-wire menu commands */ static const struct menu_command_t busvoodoo_onewire_commands[] = { { .shortcut = 'a', .name = "action", .command_description = "perform protocol actions", .argument = MENU_ARGUMENT_STRING, .argument_description = "[actions]", .command_handler = &busvoodoo_onewire_command_actions, }, { .shortcut = 's', .name = "search", .command_description = "perform ROM search", .argument = MENU_ARGUMENT_STRING, .argument_description = "[alarm]", .command_handler = &busvoodoo_onewire_rom_search, }, }; const struct busvoodoo_mode_t busvoodoo_onewire_mode = { .name = "1-wire", .description = "1-Wire", .full_only = false, .setup = &busvoodoo_onewire_setup, .commands = busvoodoo_onewire_commands, .commands_nb = LENGTH(busvoodoo_onewire_commands), .exit = &busvoodoo_onewire_exit, };