/ Mbed OS L432KC_SPI_Pey_Lal
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #include "mbed.h"
00007 #include "platform/mbed_thread.h"
00008 #include "protocol.h"
00009 
00010 //Define SPI
00011 #define SPI3_MOSI   D11
00012 #define SPI3_MISO   D12
00013 #define SPI3_SCLK   D13
00014 #define SPI3_CS     A3
00015 
00016 //Define PWM
00017 #define PWM_PROP    D9
00018 #define PWM_DIR     D10
00019 
00020 //fontion interrupt SP
00021 void onRxInterrupt();
00022 
00023 //Declaration PWM outputs
00024 PwmOut propulsion(PWM_PROP);
00025 PwmOut direction(PWM_DIR);
00026 
00027 //Declaration Links
00028 static UnbufferedSerial serial_port(USBTX, USBRX,115200);
00029 SPISlave device(SPI3_MOSI, SPI3_MISO, SPI3_SCLK, SPI3_CS); // mosi, miso, sclk, ssel
00030 
00031 int main()
00032 {
00033     //Declaration PWM variables
00034     uint32_t pulsewidth_direction = 1100;
00035     uint32_t pulsewidth_propulsion = 1500;
00036     
00037     //Test Serial port
00038     serial_port.write("Test\n\r",strlen("Test\n\r"));
00039     
00040     //Interrupt SP
00041     serial_port.attach(&onRxInterrupt, SerialBase::RxIrq);
00042     
00043     //Init. propulsion PWM
00044     propulsion.period_us(20000);
00045     propulsion.pulsewidth_us(pulsewidth_propulsion);
00046     
00047     thread_sleep_for(2000);
00048     
00049     //Init. Direction PWM
00050     direction.period_us(20000);
00051     direction.pulsewidth_us(pulsewidth_direction);
00052     
00053     //Init. SPI Link
00054     device.format(8);
00055     
00056     //Infinite loop
00057     while(1) 
00058     {
00059         //If SPI received a char, decode it then reply bullshit.
00060         if(device.receive())
00061         {
00062             char c = device.read();
00063             decodeMessage(c);
00064             serial_port.write(&c, 1);
00065             device.reply(0b10101010);
00066         }
00067         
00068         //If decoding has ended, get and changes PWM values.
00069         if(isDataAvailable())
00070         {
00071             getVerifiedPWMValues(&pulsewidth_propulsion, &pulsewidth_direction);
00072             propulsion.pulsewidth_us(pulsewidth_propulsion);
00073             direction.pulsewidth_us(pulsewidth_direction);
00074         }        
00075         
00076     }
00077 }
00078 
00079 void onRxInterrupt()
00080 {
00081     char c;
00082 
00083 
00084     // Read the data to clear the receive interrupt.
00085     if (serial_port.read(&c, 1)) {
00086         decodeMessage(c);
00087         serial_port.write(&c, 1);
00088     }
00089 
00090 }