UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
main.cpp@10:db2be22bc2f9, 2019-05-18 (annotated)
- Committer:
- Inscape_ao
- Date:
- Sat May 18 03:28:31 2019 +0000
- Revision:
- 10:db2be22bc2f9
- Parent:
- 9:c81d0df866f5
- Child:
- 12:a45a9c65dc03
add small Device Driver for ADXL372
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Inscape_ao | 0:c347f602596d | 1 | /** --- Includes --- */ |
Inscape_ao | 0:c347f602596d | 2 | #include "mbed.h" |
Inscape_ao | 2:a694440145e9 | 3 | #include "SDFileSystem.h" |
Inscape_ao | 2:a694440145e9 | 4 | #include "TimeManager.h" |
Inscape_ao | 0:c347f602596d | 5 | #include "UartReceiver.h" |
Inscape_ao | 0:c347f602596d | 6 | #include "CommandParser.h" |
Inscape_ao | 5:a37e3a15444b | 7 | #include "SDDataStore.h" |
Inscape_ao | 0:c347f602596d | 8 | #include "global.h" |
Inscape_ao | 0:c347f602596d | 9 | #include "string.h" |
Inscape_ao | 7:9ab8809f9693 | 10 | #include "DeviceRepeater.h" |
Inscape_ao | 7:9ab8809f9693 | 11 | #include "DummyDevice.h" |
Inscape_ao | 0:c347f602596d | 12 | |
Inscape_ao | 0:c347f602596d | 13 | /** --- Global Variables --- */ |
Inscape_ao | 0:c347f602596d | 14 | Serial pc(SERIAL_TX, SERIAL_RX); |
Inscape_ao | 0:c347f602596d | 15 | DigitalOut myled(LED1); |
Inscape_ao | 1:71c9c97c9f3d | 16 | |
Inscape_ao | 10:db2be22bc2f9 | 17 | static SPI myspi(SPI_MOSI, SPI_MISO, SPI_SCK); /* D13, D12, D11 */ |
Inscape_ao | 10:db2be22bc2f9 | 18 | static DigitalOut mycs(SPI_CS); /* D10 */ |
Inscape_ao | 10:db2be22bc2f9 | 19 | static DigitalIn int1(D7); /* D7/D6 */ |
Inscape_ao | 10:db2be22bc2f9 | 20 | static DigitalIn int2(D5); /* D5/D4 */ |
Inscape_ao | 10:db2be22bc2f9 | 21 | |
Inscape_ao | 10:db2be22bc2f9 | 22 | #define SPI_RD (1) |
Inscape_ao | 10:db2be22bc2f9 | 23 | #define SPI_WR (0) |
Inscape_ao | 10:db2be22bc2f9 | 24 | #define SPI_EMPTY_SEND (0x00) |
Inscape_ao | 10:db2be22bc2f9 | 25 | |
Inscape_ao | 10:db2be22bc2f9 | 26 | static void test() |
Inscape_ao | 10:db2be22bc2f9 | 27 | { |
Inscape_ao | 10:db2be22bc2f9 | 28 | #if 0 |
Inscape_ao | 10:db2be22bc2f9 | 29 | int devSpiId; |
Inscape_ao | 10:db2be22bc2f9 | 30 | int devid_ad; |
Inscape_ao | 10:db2be22bc2f9 | 31 | int devid_mst; |
Inscape_ao | 10:db2be22bc2f9 | 32 | int devid_part; |
Inscape_ao | 10:db2be22bc2f9 | 33 | int devid_rev; |
Inscape_ao | 10:db2be22bc2f9 | 34 | |
Inscape_ao | 10:db2be22bc2f9 | 35 | pc.printf("#debug start\n"); |
Inscape_ao | 10:db2be22bc2f9 | 36 | |
Inscape_ao | 10:db2be22bc2f9 | 37 | /** Init SPI for ADXL372 |
Inscape_ao | 10:db2be22bc2f9 | 38 | * CPHA = CPOL = 0, 1MHz(max.10MHz)) |
Inscape_ao | 10:db2be22bc2f9 | 39 | * CS = inactive |
Inscape_ao | 10:db2be22bc2f9 | 40 | */ |
Inscape_ao | 10:db2be22bc2f9 | 41 | myspi.format(8,0); |
Inscape_ao | 10:db2be22bc2f9 | 42 | myspi.frequency(1000000); |
Inscape_ao | 10:db2be22bc2f9 | 43 | mycs = 1; |
Inscape_ao | 10:db2be22bc2f9 | 44 | |
Inscape_ao | 10:db2be22bc2f9 | 45 | pc.printf("#init spi\n"); |
Inscape_ao | 10:db2be22bc2f9 | 46 | |
Inscape_ao | 10:db2be22bc2f9 | 47 | /* Get DeviceID from SPI */ |
Inscape_ao | 10:db2be22bc2f9 | 48 | mycs = 0; |
Inscape_ao | 10:db2be22bc2f9 | 49 | pc.printf("#spi CS=ACT\n"); |
Inscape_ao | 10:db2be22bc2f9 | 50 | |
Inscape_ao | 10:db2be22bc2f9 | 51 | myspi.write(0x00 << 1 | SPI_RD); |
Inscape_ao | 10:db2be22bc2f9 | 52 | pc.printf("#spi REG Write\n"); |
Inscape_ao | 10:db2be22bc2f9 | 53 | |
Inscape_ao | 10:db2be22bc2f9 | 54 | devid_ad = myspi.write(SPI_EMPTY_SEND); |
Inscape_ao | 10:db2be22bc2f9 | 55 | pc.printf("#spi REG Read\n"); |
Inscape_ao | 10:db2be22bc2f9 | 56 | |
Inscape_ao | 10:db2be22bc2f9 | 57 | mycs = 1; |
Inscape_ao | 10:db2be22bc2f9 | 58 | pc.printf("#spi CS=DAC\n"); |
Inscape_ao | 10:db2be22bc2f9 | 59 | |
Inscape_ao | 10:db2be22bc2f9 | 60 | mycs = 0; |
Inscape_ao | 10:db2be22bc2f9 | 61 | myspi.write(0x01 << 1 | SPI_RD); |
Inscape_ao | 10:db2be22bc2f9 | 62 | devid_mst = myspi.write(SPI_EMPTY_SEND); |
Inscape_ao | 10:db2be22bc2f9 | 63 | mycs = 1; |
Inscape_ao | 10:db2be22bc2f9 | 64 | |
Inscape_ao | 10:db2be22bc2f9 | 65 | mycs = 0; |
Inscape_ao | 10:db2be22bc2f9 | 66 | myspi.write(0x02 << 1 | SPI_RD); |
Inscape_ao | 10:db2be22bc2f9 | 67 | devid_part = myspi.write(SPI_EMPTY_SEND); |
Inscape_ao | 10:db2be22bc2f9 | 68 | mycs = 1; |
Inscape_ao | 10:db2be22bc2f9 | 69 | |
Inscape_ao | 10:db2be22bc2f9 | 70 | mycs = 0; |
Inscape_ao | 10:db2be22bc2f9 | 71 | myspi.write(0x03 << 1 | SPI_RD); |
Inscape_ao | 10:db2be22bc2f9 | 72 | devid_rev = myspi.write(SPI_EMPTY_SEND); |
Inscape_ao | 10:db2be22bc2f9 | 73 | mycs = 1; |
Inscape_ao | 10:db2be22bc2f9 | 74 | |
Inscape_ao | 10:db2be22bc2f9 | 75 | devSpiId = (devid_ad << 0) |
Inscape_ao | 10:db2be22bc2f9 | 76 | | (devid_mst << 8) |
Inscape_ao | 10:db2be22bc2f9 | 77 | | (devid_part << 16) |
Inscape_ao | 10:db2be22bc2f9 | 78 | | (devid_rev << 24); |
Inscape_ao | 10:db2be22bc2f9 | 79 | |
Inscape_ao | 10:db2be22bc2f9 | 80 | pc.printf("#fini spi=0x%x\n", devSpiId); |
Inscape_ao | 10:db2be22bc2f9 | 81 | #endif |
Inscape_ao | 10:db2be22bc2f9 | 82 | } |
Inscape_ao | 10:db2be22bc2f9 | 83 | |
Inscape_ao | 0:c347f602596d | 84 | /** --- main --- */ |
Inscape_ao | 0:c347f602596d | 85 | int main() |
Inscape_ao | 0:c347f602596d | 86 | { |
Inscape_ao | 10:db2be22bc2f9 | 87 | int deviceId = 0; |
Inscape_ao | 10:db2be22bc2f9 | 88 | |
Inscape_ao | 0:c347f602596d | 89 | /** UART Initalizer */ |
Inscape_ao | 0:c347f602596d | 90 | /* setup UART 115200, 8bit, Parity=None, stopbit:1bit */ |
Inscape_ao | 0:c347f602596d | 91 | /* https://os.mbed.com/users/okini3939/notebook/Serial_jp/ */ |
Inscape_ao | 0:c347f602596d | 92 | pc.baud(115200); |
Inscape_ao | 0:c347f602596d | 93 | pc.format(8, Serial::None, 1); |
Inscape_ao | 0:c347f602596d | 94 | |
Inscape_ao | 10:db2be22bc2f9 | 95 | test(); |
Inscape_ao | 10:db2be22bc2f9 | 96 | |
Inscape_ao | 2:a694440145e9 | 97 | /* new timer manager */ |
Inscape_ao | 2:a694440145e9 | 98 | pTM = new TimeManager(); |
Inscape_ao | 5:a37e3a15444b | 99 | /* Generate SDDataStore */ |
Inscape_ao | 5:a37e3a15444b | 100 | pSds = new SDDataStore(pTM); |
Inscape_ao | 0:c347f602596d | 101 | /* Receive Buffer Control */ |
Inscape_ao | 0:c347f602596d | 102 | pUR = new UartReceiver(&pc); |
Inscape_ao | 9:c81d0df866f5 | 103 | |
Inscape_ao | 9:c81d0df866f5 | 104 | /* create Device */ |
Inscape_ao | 10:db2be22bc2f9 | 105 | pDevRept = new DeviceRepeater(deviceId, &DummyDev); |
Inscape_ao | 9:c81d0df866f5 | 106 | |
Inscape_ao | 0:c347f602596d | 107 | /* Generate Command parser as DeviceID = 0, ParsingRule = rules */ |
Inscape_ao | 10:db2be22bc2f9 | 108 | pCP = new CommandParser(pUR, deviceId, rules, getNumOfRules); |
Inscape_ao | 0:c347f602596d | 109 | pCP->run(); |
Inscape_ao | 6:81a3d517fd56 | 110 | |
Inscape_ao | 10:db2be22bc2f9 | 111 | pc.printf(":%d ACK 0004 0000\n", deviceId); |
Inscape_ao | 4:bec3f80dc49c | 112 | |
Inscape_ao | 0:c347f602596d | 113 | while(1) { |
Inscape_ao | 10:db2be22bc2f9 | 114 | test(); |
Inscape_ao | 10:db2be22bc2f9 | 115 | wait(5); |
Inscape_ao | 0:c347f602596d | 116 | } |
Inscape_ao | 0:c347f602596d | 117 | } |