.

https://www.mediafire.com/file/sjhxgn70gxshilg/protocol_spi_pwm.png/file

Committer:
voltxd
Date:
Thu Apr 07 12:30:01 2022 +0000
Revision:
111:f11575e7c79b
Parent:
110:a6d1d3525014
Child:
112:478ae92cb106
.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 82:abf1b1785bd7 1 /* mbed Microcontroller Library
mbed_official 102:6979ad8bc0bc 2 * Copyright (c) 2019 ARM Limited
mbed_official 82:abf1b1785bd7 3 * SPDX-License-Identifier: Apache-2.0
mbed_official 82:abf1b1785bd7 4 */
mbed_official 82:abf1b1785bd7 5
Jonathan Austin 0:2757d7abb7d9 6 #include "mbed.h"
mbed_official 100:ec006d6f3cb6 7 #include "platform/mbed_thread.h"
voltxd 109:4ee7ffc8f175 8 #include "protocol.h"
Jonathan Austin 0:2757d7abb7d9 9
voltxd 108:2fd41d299a8c 10 //Define SPI
voltxd 108:2fd41d299a8c 11 #define SPI3_MOSI D11
voltxd 108:2fd41d299a8c 12 #define SPI3_MISO D12
voltxd 108:2fd41d299a8c 13 #define SPI3_SCLK D13
voltxd 108:2fd41d299a8c 14 #define SPI3_CS A3
ajuton 107:0965c72c798e 15
voltxd 108:2fd41d299a8c 16 //Define PWM
voltxd 108:2fd41d299a8c 17 #define PWM_PROP D10
voltxd 108:2fd41d299a8c 18 #define PWM_DIR D9
ajuton 107:0965c72c798e 19
voltxd 111:f11575e7c79b 20 //Declaration PWM outputs
voltxd 108:2fd41d299a8c 21 PwmOut propulsion(PWM_PROP);
voltxd 108:2fd41d299a8c 22 PwmOut direction(PWM_DIR);
ajuton 107:0965c72c798e 23
voltxd 111:f11575e7c79b 24 //Declaration Links
ajuton 107:0965c72c798e 25 static BufferedSerial serial_port(USBTX, USBRX,115200);
voltxd 108:2fd41d299a8c 26 SPISlave device(SPI3_MOSI, SPI3_MISO, SPI3_SCLK, SPI3_CS); // mosi, miso, sclk, ssel
mbed_official 102:6979ad8bc0bc 27
mbed_official 82:abf1b1785bd7 28 int main()
mbed_official 82:abf1b1785bd7 29 {
voltxd 111:f11575e7c79b 30 //Declaration PWM variables
ajuton 107:0965c72c798e 31 uint32_t pulsewidth_direction = 1100;
voltxd 109:4ee7ffc8f175 32 uint32_t pulsewidth_propulsion = 1500;
voltxd 109:4ee7ffc8f175 33
voltxd 111:f11575e7c79b 34 //Test Serial port
voltxd 111:f11575e7c79b 35 serial_port.write("Test\n\r",strlen("Test\n\r"));
voltxd 109:4ee7ffc8f175 36
voltxd 111:f11575e7c79b 37 //Init. propulsion PWM
ajuton 107:0965c72c798e 38 propulsion.period_us(20000);
voltxd 109:4ee7ffc8f175 39 propulsion.pulsewidth_us(pulsewidth_propulsion);
voltxd 109:4ee7ffc8f175 40
voltxd 108:2fd41d299a8c 41 thread_sleep_for(2000);
voltxd 109:4ee7ffc8f175 42
voltxd 111:f11575e7c79b 43 //Init. Direction PWM
ajuton 107:0965c72c798e 44 direction.period_us(20000);
ajuton 107:0965c72c798e 45 direction.pulsewidth_us(pulsewidth_direction);
voltxd 109:4ee7ffc8f175 46
voltxd 111:f11575e7c79b 47 //Init. SPI Link
voltxd 108:2fd41d299a8c 48 device.format(8);
voltxd 108:2fd41d299a8c 49
voltxd 111:f11575e7c79b 50 //Infinite loop
voltxd 111:f11575e7c79b 51 while(1)
voltxd 111:f11575e7c79b 52 {
voltxd 111:f11575e7c79b 53 //If SPI received a char, decode it then reply bullshit.
voltxd 110:a6d1d3525014 54 if(device.receive())
voltxd 109:4ee7ffc8f175 55 {
voltxd 111:f11575e7c79b 56 decodeMessage((char)device.read());
voltxd 111:f11575e7c79b 57 device.reply(0b10101010);
voltxd 109:4ee7ffc8f175 58 }
voltxd 111:f11575e7c79b 59
voltxd 111:f11575e7c79b 60 //If decoding has ended, get and changes PWM values.
voltxd 110:a6d1d3525014 61 if(isDataAvailable())
voltxd 109:4ee7ffc8f175 62 {
voltxd 110:a6d1d3525014 63 getVerifiedPWMValues(&pulsewidth_propulsion, &pulsewidth_direction);
voltxd 109:4ee7ffc8f175 64 propulsion.pulsewidth_us(pulsewidth_propulsion);
voltxd 109:4ee7ffc8f175 65 direction.pulsewidth_us(pulsewidth_direction);
voltxd 111:f11575e7c79b 66 }
Jonathan Austin 0:2757d7abb7d9 67 }
Jonathan Austin 0:2757d7abb7d9 68 }