Program for controlling MAX32630FTHR using a PC via USB serial interface
Dependencies: OneWire max32630fthr USBDevice
main.cpp@11:d86e072b6eea, 2020-01-31 (annotated)
- Committer:
- destarija
- Date:
- Fri Jan 31 06:10:03 2020 +0000
- Revision:
- 11:d86e072b6eea
- Parent:
- 10:a2ab93f0eb34
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phonemacro | 9:02c5adb483c8 | 1 | /******************************************************************************* |
phonemacro | 9:02c5adb483c8 | 2 | * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved. |
phonemacro | 9:02c5adb483c8 | 3 | * |
phonemacro | 9:02c5adb483c8 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
phonemacro | 9:02c5adb483c8 | 5 | * copy of this software and associated documentation files (the "Software"), |
phonemacro | 9:02c5adb483c8 | 6 | * to deal in the Software without restriction, including without limitation |
phonemacro | 9:02c5adb483c8 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
phonemacro | 9:02c5adb483c8 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
phonemacro | 9:02c5adb483c8 | 9 | * Software is furnished to do so, subject to the following conditions: |
phonemacro | 9:02c5adb483c8 | 10 | * |
phonemacro | 9:02c5adb483c8 | 11 | * The above copyright notice and this permission notice shall be included |
phonemacro | 9:02c5adb483c8 | 12 | * in all copies or substantial portions of the Software. |
phonemacro | 9:02c5adb483c8 | 13 | * |
phonemacro | 9:02c5adb483c8 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
phonemacro | 9:02c5adb483c8 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
phonemacro | 9:02c5adb483c8 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
phonemacro | 9:02c5adb483c8 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
phonemacro | 9:02c5adb483c8 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
phonemacro | 9:02c5adb483c8 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
phonemacro | 9:02c5adb483c8 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
phonemacro | 9:02c5adb483c8 | 21 | * |
phonemacro | 9:02c5adb483c8 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
phonemacro | 9:02c5adb483c8 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
phonemacro | 9:02c5adb483c8 | 24 | * Products, Inc. Branding Policy. |
phonemacro | 9:02c5adb483c8 | 25 | * |
phonemacro | 9:02c5adb483c8 | 26 | * The mere transfer of this software does not imply any licenses |
phonemacro | 9:02c5adb483c8 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
phonemacro | 9:02c5adb483c8 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
phonemacro | 9:02c5adb483c8 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
phonemacro | 9:02c5adb483c8 | 30 | * ownership rights. |
phonemacro | 9:02c5adb483c8 | 31 | ******************************************************************************* |
phonemacro | 9:02c5adb483c8 | 32 | */ |
switches | 0:60a522ae2e35 | 33 | #include "mbed.h" |
switches | 2:57500e991166 | 34 | #include "max32630fthr.h" |
switches | 1:6923b075c8d7 | 35 | #include "USBSerial.h" |
switches | 0:60a522ae2e35 | 36 | |
destarija | 10:a2ab93f0eb34 | 37 | void parse(char *buffer, char *argv[], int c); |
destarija | 10:a2ab93f0eb34 | 38 | int write_reg(char slave_addr, char reg_addr, char *data); |
destarija | 10:a2ab93f0eb34 | 39 | int read_reg(char slave_addr, char reg_addr, char *data); |
switches | 0:60a522ae2e35 | 40 | |
destarija | 10:a2ab93f0eb34 | 41 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
switches | 1:6923b075c8d7 | 42 | |
switches | 3:601c11238ccb | 43 | // Virtual serial port over USB |
h_keyur | 8:d2b660bf5f94 | 44 | USBSerial microUSB; |
switches | 2:57500e991166 | 45 | DigitalOut rLED(LED1); |
switches | 2:57500e991166 | 46 | DigitalOut gLED(LED2); |
switches | 2:57500e991166 | 47 | DigitalOut bLED(LED3); |
destarija | 10:a2ab93f0eb34 | 48 | I2C i2c1(P3_4, P3_5); |
switches | 0:60a522ae2e35 | 49 | |
switches | 0:60a522ae2e35 | 50 | // main() runs in its own thread in the OS |
switches | 0:60a522ae2e35 | 51 | // (note the calls to Thread::wait below for delays) |
destarija | 10:a2ab93f0eb34 | 52 | int main() { |
switches | 0:60a522ae2e35 | 53 | |
destarija | 10:a2ab93f0eb34 | 54 | char buffer[32]; |
destarija | 10:a2ab93f0eb34 | 55 | char *argv[4]; |
destarija | 10:a2ab93f0eb34 | 56 | buffer[31] = '\0'; |
destarija | 10:a2ab93f0eb34 | 57 | char slave_addr, reg_addr, data; |
destarija | 10:a2ab93f0eb34 | 58 | char *ptr; |
destarija | 10:a2ab93f0eb34 | 59 | |
destarija | 10:a2ab93f0eb34 | 60 | i2c1.frequency(0); |
switches | 1:6923b075c8d7 | 61 | while(1) { |
destarija | 10:a2ab93f0eb34 | 62 | microUSB.printf(">> "); |
destarija | 10:a2ab93f0eb34 | 63 | |
destarija | 10:a2ab93f0eb34 | 64 | microUSB.scanf("%31[^\r\n]", buffer); |
destarija | 10:a2ab93f0eb34 | 65 | parse(buffer, argv, 4); |
destarija | 10:a2ab93f0eb34 | 66 | slave_addr = (char) strtol(argv[1], NULL, 16); |
destarija | 10:a2ab93f0eb34 | 67 | reg_addr = (char) strtol(argv[2], NULL, 16); |
destarija | 10:a2ab93f0eb34 | 68 | data = (char) strtol(argv[3], NULL, 16); |
destarija | 10:a2ab93f0eb34 | 69 | |
destarija | 10:a2ab93f0eb34 | 70 | microUSB.printf("command: %s\r\n", argv[0]); |
destarija | 10:a2ab93f0eb34 | 71 | microUSB.printf("slave address: %d\r\n", slave_addr); |
destarija | 10:a2ab93f0eb34 | 72 | microUSB.printf("reg address: %d\r\n", reg_addr); |
destarija | 10:a2ab93f0eb34 | 73 | microUSB.printf("data: %d\r\n", data); |
destarija | 10:a2ab93f0eb34 | 74 | |
destarija | 10:a2ab93f0eb34 | 75 | if (!strcmp(argv[0], "write")) |
destarija | 10:a2ab93f0eb34 | 76 | if (write_reg(slave_addr, reg_addr, &data)==1) |
destarija | 10:a2ab93f0eb34 | 77 | microUSB.printf("ACK received\r\n"); |
destarija | 10:a2ab93f0eb34 | 78 | else |
destarija | 10:a2ab93f0eb34 | 79 | microUSB.printf("NACK received\r\n"); |
destarija | 10:a2ab93f0eb34 | 80 | else if (!strcmp(argv[0], "read")) |
destarija | 10:a2ab93f0eb34 | 81 | if (read_reg(slave_addr, reg_addr, &data)==1) |
destarija | 10:a2ab93f0eb34 | 82 | microUSB.printf("ACK received\r\n"); |
destarija | 10:a2ab93f0eb34 | 83 | else |
destarija | 10:a2ab93f0eb34 | 84 | microUSB.printf("NACK received\r\n"); |
destarija | 10:a2ab93f0eb34 | 85 | else |
destarija | 10:a2ab93f0eb34 | 86 | microUSB.printf("invalid command\r\n"); |
destarija | 10:a2ab93f0eb34 | 87 | |
phonemacro | 9:02c5adb483c8 | 88 | bLED = !bLED; |
switches | 0:60a522ae2e35 | 89 | } |
destarija | 10:a2ab93f0eb34 | 90 | |
destarija | 10:a2ab93f0eb34 | 91 | |
switches | 0:60a522ae2e35 | 92 | } |
switches | 0:60a522ae2e35 | 93 | |
destarija | 10:a2ab93f0eb34 | 94 | int write_reg(char slave_addr, char reg_addr, char *data) { |
destarija | 10:a2ab93f0eb34 | 95 | //i2c1.write((int)slave_addr, ®_addr, 1, true); |
destarija | 10:a2ab93f0eb34 | 96 | //wait_ms(0.01); |
destarija | 10:a2ab93f0eb34 | 97 | //return i2c1.write(*(int*)data); |
destarija | 10:a2ab93f0eb34 | 98 | |
destarija | 10:a2ab93f0eb34 | 99 | i2c1.start(); |
destarija | 10:a2ab93f0eb34 | 100 | //i2c1.write((int)slave_addr); |
destarija | 10:a2ab93f0eb34 | 101 | //i2c1.write((int)reg_addr); |
destarija | 10:a2ab93f0eb34 | 102 | //i2c1.write(*(int*)data); |
destarija | 10:a2ab93f0eb34 | 103 | i2c1.stop(); |
destarija | 10:a2ab93f0eb34 | 104 | } |
destarija | 10:a2ab93f0eb34 | 105 | |
destarija | 10:a2ab93f0eb34 | 106 | int read_reg(char slave_addr, char reg_addr, char *data) { |
destarija | 10:a2ab93f0eb34 | 107 | //i2c1.write((int)slave_addr, ®_addr, 1, true); |
destarija | 10:a2ab93f0eb34 | 108 | //wait_ms(0.01); |
destarija | 10:a2ab93f0eb34 | 109 | //return i2c1.read(*(int*)data); |
destarija | 10:a2ab93f0eb34 | 110 | i2c1.start(); |
destarija | 10:a2ab93f0eb34 | 111 | //i2c1.write((int)slave_addr); |
destarija | 10:a2ab93f0eb34 | 112 | //i2c1.write((int)reg_addr); |
destarija | 10:a2ab93f0eb34 | 113 | //*data = i2c1.read(1); |
destarija | 10:a2ab93f0eb34 | 114 | i2c1.stop(); |
destarija | 10:a2ab93f0eb34 | 115 | } |
destarija | 10:a2ab93f0eb34 | 116 | |
destarija | 10:a2ab93f0eb34 | 117 | void parse(char *buffer, char *argv[], int c) { |
destarija | 10:a2ab93f0eb34 | 118 | char *ptr; |
destarija | 10:a2ab93f0eb34 | 119 | int i; |
destarija | 10:a2ab93f0eb34 | 120 | |
destarija | 10:a2ab93f0eb34 | 121 | i = 0; |
destarija | 10:a2ab93f0eb34 | 122 | ptr = buffer; |
destarija | 10:a2ab93f0eb34 | 123 | |
destarija | 10:a2ab93f0eb34 | 124 | while (i<c && *ptr != '\n' && *ptr != '\0') { |
destarija | 10:a2ab93f0eb34 | 125 | argv[i++] = ptr; |
destarija | 10:a2ab93f0eb34 | 126 | while (*ptr != ' ' && *ptr != '\n' && *ptr != '\0') |
destarija | 10:a2ab93f0eb34 | 127 | ptr++; |
destarija | 10:a2ab93f0eb34 | 128 | *(ptr++) = '\0'; |
destarija | 10:a2ab93f0eb34 | 129 | } |
destarija | 10:a2ab93f0eb34 | 130 | } |