busvoodoo_onewire.c
Go to the documentation of this file.
/* 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 <http://www.gnu.org/licenses/>.
*
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // standard utilities
#include <string.h> // string utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/timer.h> // 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
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;
static bool busvoodoo_onewire_embedded_pullup = true;
static bool busvoodoo_onewire_power = false;
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<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
}
if (busvoodoo_full) {
const char* pinout_rscan[5] = {"HV", NULL, NULL, NULL, NULL}; // HiZ mode RS/CAN pinout
for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
}
}
busvoodoo_oled_text_pinout(pinout_io, true); // set pinout on display
busvoodoo_oled_update(); // update display to show text and pinout
complete = true; // configuration is complete
}
break;
default: // unknown case
busvoodoo_onewire_setting = BUSVOODOO_ONEWIRE_SETTING_NONE; // restart settings next time
break;
}
return complete;
}
static void busvoodoo_onewire_write(uint8_t value)
{
printf("write: 0x%02x", value);
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we are writing
if (!onewire_master_write_byte(value)) { // send data bytes
printf(" (error)");
}
printf("\n");
}
static void busvoodoo_onewire_read(void)
{
uint8_t data; // buffer to read data
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we are reading
bool error = onewire_master_read_byte(&data); // read byte
printf("read: 0x%02x%s\n", data, error ? "" : " (error)");
}
static void busvoodoo_onewire_exit(void)
{
onewire_master_release(); // release peripheral
busvoodoo_embedded_pullup(false); // disable embedded pull-ups
}
static bool busvoodoo_onewire_action(const char* action, uint32_t repetition, bool perform)
{
uint32_t length = strlen(action); // remember length since it will be used a number of times
if (NULL==action || 0==length) { // there is nothing to do
return true;
}
if (1==length && 'r'==action[0]) { // read data
if (!perform) {
return true;
}
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_onewire_read(); // read from 1-wire
}
} else if (1==length && '['==action[0]) { // start transaction with slave presence detection
if (!perform) {
return true;
}
if (busvoodoo_onewire_power) {
gpio_set_mode(GPIO(ONEWIRE_MASTER_PORT), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(ONEWIRE_MASTER_PIN)); // remove power from data line
}
if (!gpio_get(GPIO(ONEWIRE_MASTER_PORT), GPIO(ONEWIRE_MASTER_PIN))) {
printf("WARNING: data line does not seem to be pulled up\n");
}
printf("start transaction: ");
bool presence = onewire_master_reset(); // send reset pulse and detect slave presence
printf("slave presence %sdetected\n", presence ? "" : "not ");
} else if (1==length && ']'==action[0]) { // stop transaction
if (!perform) {
return true;
}
printf("end transaction%s\n", busvoodoo_onewire_power ? " and provide power on data line" : "");
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)
}
} else if (1==length && 'u'==action[0]) { // sleep us
if (!perform) {
return true;
}
printf("wait for %u us\n", repetition);
sleep_us(repetition); // sleep
} else if (1==length && 'm'==action[0]) { // sleep ms
if (!perform) {
return true;
}
printf("wait for %u ms\n", repetition);
sleep_ms(repetition); // sleep
} else if ('0'==action[0]) { // send digit
if (1==length) { // just send 0
if (!perform) {
return true;
}
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_onewire_write(0); // write to SPI
}
} else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
return busvoodoo_onewire_action(action+1, repetition, perform); // just retry without leading 0
} else if (action[1]>='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<length; i++) { // check string
if (!((action[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; i<repetition; i++) {
busvoodoo_onewire_write(value); // write to SPI
}
} else if ('b'==action[0] && length>1) { // send binary value
for (uint32_t i=1; i<length; i++) { // check string
if (action[i]<'0' || action[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<repetition; i++) {
busvoodoo_onewire_write(value); // write to SPI
}
} else if (action[0]>='1' && action[0]<='9') { // send decimal value
for (uint32_t i=1; i<length; i++) { // check string
if (action[i]<'0' || action[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<repetition; i++) {
busvoodoo_onewire_write(value); // write to SPI
}
} else if (length>=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character
if (!perform) {
return true;
}
for (uint32_t r=0; r<repetition; r++) {
for (uint32_t i=1; i<length-1; i++) { // go through string
busvoodoo_onewire_write(action[i]); // write to SPI
}
}
} else { // malformed action
return false;
}
return true; // all went well
}
// command handlers
static void busvoodoo_onewire_command_actions(void* argument)
{
if (NULL==argument || 0==strlen(argument)) {
printf("available actions (separated by space or ,):\n");
printf("[\tstart transaction: send reset pulse and detect slave presence\n");
printf("]\tend transaction%s\n", busvoodoo_onewire_power ? " and provide power on data line" : "");
printf("0\twrite decimal byte\n");
printf("0x0\twrite hexadecimal byte\n");
printf("0b0\twrite binary byte\n");
printf("\"a\"/'a'\twrite ASCII characters\n");
printf("r\tread byte\n");
printf("u/m\twait 1 us/ms\n");
printf(":n\trepeat action n times\n");
return;
}
// copy argument since it will be modified
char* copy = calloc(strlen(argument)+1, sizeof(char));
if (!copy) {
while (true);
}
strncpy(copy, argument, strlen(argument)+1);
// verify and perform actions
if (!busvoodoo_global_actions(copy, false, &busvoodoo_onewire_action)) { // verify actions
printf("malformed action(s)\n");
} else { // action are OK
busvoodoo_global_actions(argument, true, &busvoodoo_onewire_action); // perform action
}
free(copy); // release memory
}
static void busvoodoo_onewire_rom_search(void* argument)
{
bool alarm = false; // if only ROMs with alarms should be searched
if (argument && 0==strcmp(argument, "alarm")) {
alarm = true;
}
if (busvoodoo_onewire_power) {
gpio_set_mode(GPIO(ONEWIRE_MASTER_PORT), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(ONEWIRE_MASTER_PIN)); // remove power from data line
}
if (!gpio_get(GPIO(ONEWIRE_MASTER_PORT), GPIO(ONEWIRE_MASTER_PIN))) {
printf("WARNING: the data line needs to be pulled up\n");
return;
}
bool presence = onewire_master_reset(); // send reset pulse and detect slave presence
printf("slave presence %sdetected\n", presence ? "" : "not ");
if (presence) { // only search if a slave presence has been detected
uint64_t code = 0; // code found
uint64_t codes = 0; // number of codes found
printf("searching ROM codes%s:\n", alarm ? " with alarm" : "");
bool next; // if another ROM code is detected
do { // search until all has been found
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we are scanning
next = onewire_master_rom_search(&code, alarm); // search for the code
if (0==code && !gpio_get(GPIO(ONEWIRE_MASTER_PORT), GPIO(ONEWIRE_MASTER_PIN))) { // searching for ROM codes does not work when the line is not pulled up
printf("not able to search for ROM codes when the data line is not pulled up\n");
break;
}
if (next) {
presence = onewire_master_reset(); // send reset pulse and detect slave presence for the next slave
}
const char* device = "unknown";
for (uint16_t i = 0; i < LENGTH(onewire_family_codes); i++) {
if ((uint8_t)code == onewire_family_codes[i].code) {
device = onewire_family_codes[i].device;
break;
}
}
uint8_t crc_data[8] = {code, code >> 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
}
}
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,
};
menu_command_t
command menu entry
Definition: menu.h:32
BUSVOODOO_LED_PULSE
#define BUSVOODOO_LED_PULSE
recommended duration in ms for pulsing LEDs to show activity
Definition: busvoodoo_global.h:112
busvoodoo_onewire_action
static bool busvoodoo_onewire_action(const char *action, uint32_t repetition, bool perform)
perform 1-wire action
Definition: busvoodoo_onewire.c:172
busvoodoo_onewire_mode
const struct busvoodoo_mode_t busvoodoo_onewire_mode
1-wire mode interface definition
Definition: busvoodoo_onewire.c:398
busvoodoo_global.h
BusVoodoo global definitions and methods (API)
busvoodoo_onewire_embedded_pullup
static bool busvoodoo_onewire_embedded_pullup
if embedded pull-up resistors are used
Definition: busvoodoo_onewire.c:48
ONEWIRE_MASTER_PORT
#define ONEWIRE_MASTER_PORT
GPIO port.
Definition: onewire_master.h:28
onewire_family_codes
static const struct onewire_family_code_t onewire_family_codes[]
list of possible devices corresponding to the family code sources:
Definition: onewire_master.h:118
busvoodoo_oled_text_pinout
void busvoodoo_oled_text_pinout(const char *pins[10], bool io_connector)
draw pin names on bottom (blue) part in display buffer
Definition: busvoodoo_oled.c:148
busvoodoo_mode_t::name
const char * name
name of the mode (i.e.
Definition: busvoodoo_global.h:116
busvoodoo_led_blue_pulse
void busvoodoo_led_blue_pulse(uint32_t ms)
pulse blue LED for short duration
Definition: busvoodoo_global.c:468
onewire_master_rom_search
bool onewire_master_rom_search(uint64_t *code, bool alarm)
send SEARCH ROM command
Definition: onewire_master.c:426
ONEWIRE_MASTER_PIN
#define ONEWIRE_MASTER_PIN
GPIO pin.
Definition: onewire_master.h:29
busvoodoo_onewire_rom_search
static void busvoodoo_onewire_rom_search(void *argument)
command to perform ROM search
Definition: busvoodoo_onewire.c:327
busvoodoo_oled_update
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
Definition: busvoodoo_oled.c:289
busvoodoo_global_pinout_rscan
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
Definition: busvoodoo_global.c:63
onewire_master_setup
void onewire_master_setup(void)
setup 1-wire peripheral
Definition: onewire_master.c:169
global.h
global definitions and methods (API)
GPIO
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
sleep_us
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:102
busvoodoo_full
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
Definition: busvoodoo_global.c:70
onewire_master.h
library for 1-wire protocol as master
onewire_master_write_byte
bool onewire_master_write_byte(uint8_t data)
write data byte
Definition: onewire_master.c:353
busvoodoo_led_blue_off
void busvoodoo_led_blue_off(void)
switch off blue LED
Definition: busvoodoo_global.c:426
busvoodoo_onewire_setting_t
busvoodoo_onewire_setting_t
mode setup stage
Definition: busvoodoo_onewire.c:41
busvoodoo_onewire_exit
static void busvoodoo_onewire_exit(void)
exit 1-wire mode
Definition: busvoodoo_onewire.c:160
busvoodoo_onewire_setting
static enum busvoodoo_onewire_setting_t busvoodoo_onewire_setting
current mode setup stage
menu_command_t::shortcut
char shortcut
short command code (0 if not available)
Definition: menu.h:33
busvoodoo_global_pinout_io
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
Definition: busvoodoo_global.c:62
busvoodoo_onewire_command_actions
static void busvoodoo_onewire_command_actions(void *argument)
command to perform actions
Definition: busvoodoo_onewire.c:294
user_input_get
char user_input_get(void)
get user input
Definition: global.c:145
busvoodoo_global_string
char busvoodoo_global_string[64]
shared string buffer, i.e.
Definition: busvoodoo_global.c:72
snprintf
size_t snprintf(char *str, size_t size, const char *format,...)
print format string on string or user output
Definition: print.c:458
busvoodoo_global_actions
bool busvoodoo_global_actions(char *actions, bool perform, bool(*action_handler)(const char *action, uint32_t repetition, bool perform))
parse and perform actions
Definition: busvoodoo_global.c:561
sleep_ms
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:117
printf
size_t printf(const char *format,...)
print format string on user output
Definition: print.c:448
busvoodoo_onewire_power
static bool busvoodoo_onewire_power
time (in ms) between slot to provide power
Definition: busvoodoo_onewire.c:50
onewire_master_read_byte
bool onewire_master_read_byte(uint8_t *data)
read data byte
Definition: onewire_master.c:338
print.h
printing utilities to replace the large printf from the standard library (API)
busvoodoo_oled_text_left
void busvoodoo_oled_text_left(char *text)
draw mode text on top (yellow) left side in display buffer
Definition: busvoodoo_oled.c:118
busvoodoo_onewire.h
BusVoodoo 1-wire mode (API)
busvoodoo_embedded_pullup
float busvoodoo_embedded_pullup(bool on)
enable embedded pull-up resistors
Definition: busvoodoo_global.c:373
busvoodoo_onewire_write
static void busvoodoo_onewire_write(uint8_t value)
write to 1-wire
Definition: busvoodoo_onewire.c:138
onewire_family_code_t::device
const char * device
device name(s)
Definition: onewire_master.h:107
busvoodoo_onewire_read
static void busvoodoo_onewire_read(void)
read from 1-wire
Definition: busvoodoo_onewire.c:150
LENGTH
#define LENGTH(x)
get the length of an array
Definition: global.h:26
busvoodoo_onewire_setup
static bool busvoodoo_onewire_setup(char **prefix, const char *line)
setup 1-wire mode
Definition: busvoodoo_onewire.c:57
onewire_master_release
void onewire_master_release(void)
release 1-wire peripheral
Definition: onewire_master.c:206
onewire_master_crc
uint8_t onewire_master_crc(uint8_t *data, uint32_t length)
compute CRC for 1-Wire
Definition: onewire_master.c:318
busvoodoo_oled.h
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
user_input_available
volatile bool user_input_available
flag set when user input is available
Definition: global.c:36
busvoodoo_onewire_commands
static const struct menu_command_t busvoodoo_onewire_commands[]
1-wire menu commands
Definition: busvoodoo_onewire.c:379
busvoodoo_mode_t
BusVoodoo mode interface.
Definition: busvoodoo_global.h:115
onewire_master_reset
bool onewire_master_reset(void)
send reset pulse
Definition: onewire_master.c:227
onewire_family_code_t::code
uint8_t code
ROM ID code.
Definition: onewire_master.h:106