From 84c0376a1ad5b07046e7c779b0536add80cd6787 Mon Sep 17 00:00:00 2001 From: Mats van Reenen Date: Wed, 13 May 2020 11:24:21 +0200 Subject: [PATCH] first push --- .ccsproject | 16 ++++ .cproject | 177 ++++++++++++++++++++++++++++++++++++++++++++ .project | 27 +++++++ SPI.c | 65 ++++++++++++++++ SPI.h | 22 ++++++ lnk_msp430g2553.cmd | 146 ++++++++++++++++++++++++++++++++++++ main.c | 39 ++++++++++ motionController.c | 176 +++++++++++++++++++++++++++++++++++++++++++ motionController.h | 14 ++++ motorDriver.c | 124 +++++++++++++++++++++++++++++++ motorDriver.h | 13 ++++ typedefExtention.h | 17 +++++ 12 files changed, 836 insertions(+) create mode 100644 .ccsproject create mode 100644 .cproject create mode 100644 .project create mode 100644 SPI.c create mode 100644 SPI.h create mode 100644 lnk_msp430g2553.cmd create mode 100644 main.c create mode 100644 motionController.c create mode 100644 motionController.h create mode 100644 motorDriver.c create mode 100644 motorDriver.h create mode 100644 typedefExtention.h diff --git a/.ccsproject b/.ccsproject new file mode 100644 index 0000000..521c8e7 --- /dev/null +++ b/.ccsproject @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..fba16b7 --- /dev/null +++ b/.cproject @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..86176af --- /dev/null +++ b/.project @@ -0,0 +1,27 @@ + + + MotionController + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + com.ti.ccstudio.core.ccsNature + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/SPI.c b/SPI.c new file mode 100644 index 0000000..224d7d4 --- /dev/null +++ b/SPI.c @@ -0,0 +1,65 @@ +#include +#include "SPI.h" + +uchar SPI_CS; + +int SPIInit(){ + + UCA0CTL1 = UCSWRST; + + // setup A0 for SPI + UCA0CTL0 = SPI_Mode3 | UCMSB | UCMST | (0x10 << 1) | UCSYNC; + UCA0CTL1 |= UCSSEL_2; + UCA0BR1 = 0; //TODO: set corect frequency + UCA0BR0 = 4; + + + // config port + //set SEL to USCI (0b01); MOSI (pin 1), MISO (pin 2), CLK (pin 4) + P1SEL |= BIT1 | BIT2 | BIT4; + P1SEL2 |= BIT1 | BIT2 | BIT4; + + + UCA0CTL1 &= ~UCSWRST; + + // enable interrupts + IE2 |= UCA0TXIE; + IFG2 &= ~UCA0TXIFG; + + // set initial values for vars + SPI_packetC = 0; + SPI_CS = 0; + + __enable_interrupt(); + + return 0; +} + +void SPISend(){ + P1OUT &= ~SPI_CS; + UCA0TXBUF = SPI_packet[SPI_packetC]; +} + +void SPIInitSend(uchar cs, enum SPI_Mode mode){ + while(SPI_CS != 0) + //TODO: search for a more power effient way for a delay + __delay_cycles (16000); + + UCA0CTL0 |= mode; + UCA0CTL0 &= ~mode ^ 0xC0; + SPI_CS = cs; +} + +#pragma vector = USCIAB0TX_VECTOR +__interrupt void ISR_EUSCI_A0(){ + SPI_packet[SPI_packetC] = UCA0RXIFG; + if(SPI_packetC == 4){ + P1OUT |= SPI_CS; + SPI_packetC = 0; + IFG2 &= ~UCA0TXIFG; + SPI_CS = 0; + }else{ + SPI_packetC++; + UCA0TXBUF = SPI_packet[SPI_packetC]; + } +} diff --git a/SPI.h b/SPI.h new file mode 100644 index 0000000..b9458a4 --- /dev/null +++ b/SPI.h @@ -0,0 +1,22 @@ +/* + * SPI.c + * + * Created on: 4 mei 2020 + * Author: mreenen + */ + +#ifndef SPI_C_ +#define SPI_C_ + +#include "typedefExtention.h" + +enum SPI_Mode {SPI_Mode0=0b00<<6, SPI_Mode1=0b01<<6, SPI_Mode2=0b11<<6, SPI_Mode3=0b10<<6}; + +uchar SPI_packet[5]; +char SPI_packetC; + +int SPIInit(); +void SPISend(); +void SPIInitSend(uchar cs, enum SPI_Mode mode); + +#endif /* SPI_C_ */ diff --git a/lnk_msp430g2553.cmd b/lnk_msp430g2553.cmd new file mode 100644 index 0000000..b907d78 --- /dev/null +++ b/lnk_msp430g2553.cmd @@ -0,0 +1,146 @@ +/* ============================================================================ */ +/* Copyright (c) 2020, Texas Instruments Incorporated */ +/* All rights reserved. */ +/* */ +/* Redistribution and use in source and binary forms, with or without */ +/* modification, are permitted provided that the following conditions */ +/* are met: */ +/* */ +/* * Redistributions of source code must retain the above copyright */ +/* notice, this list of conditions and the following disclaimer. */ +/* */ +/* * Redistributions in binary form must reproduce the above copyright */ +/* notice, this list of conditions and the following disclaimer in the */ +/* documentation and/or other materials provided with the distribution. */ +/* */ +/* * Neither the name of Texas Instruments Incorporated nor the names of */ +/* its contributors may be used to endorse or promote products derived */ +/* from this software without specific prior written permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" */ +/* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, */ +/* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ +/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR */ +/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */ +/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, */ +/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; */ +/* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, */ +/* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR */ +/* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ +/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* ============================================================================ */ + +/******************************************************************************/ +/* lnk_msp430g2553.cmd - LINKER COMMAND FILE FOR LINKING MSP430G2553 PROGRAMS */ +/* */ +/* Usage: lnk430 -o -m lnk.cmd */ +/* cl430 -z -o -m lnk.cmd */ +/* */ +/*----------------------------------------------------------------------------*/ +/* These linker options are for command line linking only. For IDE linking, */ +/* you should set your linker options in Project Properties */ +/* -c LINK USING C CONVENTIONS */ +/* -stack 0x0100 SOFTWARE STACK SIZE */ +/* -heap 0x0100 HEAP AREA SIZE */ +/* */ +/*----------------------------------------------------------------------------*/ +/* Version: 1.209 */ +/*----------------------------------------------------------------------------*/ + +/****************************************************************************/ +/* Specify the system memory map */ +/****************************************************************************/ + +MEMORY +{ + SFR : origin = 0x0000, length = 0x0010 + PERIPHERALS_8BIT : origin = 0x0010, length = 0x00F0 + PERIPHERALS_16BIT : origin = 0x0100, length = 0x0100 + RAM : origin = 0x0200, length = 0x0200 + INFOA : origin = 0x10C0, length = 0x0040 + INFOB : origin = 0x1080, length = 0x0040 + INFOC : origin = 0x1040, length = 0x0040 + INFOD : origin = 0x1000, length = 0x0040 + FLASH : origin = 0xC000, length = 0x3FDE + BSLSIGNATURE : origin = 0xFFDE, length = 0x0002, fill = 0xFFFF + INT00 : origin = 0xFFE0, length = 0x0002 + INT01 : origin = 0xFFE2, length = 0x0002 + INT02 : origin = 0xFFE4, length = 0x0002 + INT03 : origin = 0xFFE6, length = 0x0002 + INT04 : origin = 0xFFE8, length = 0x0002 + INT05 : origin = 0xFFEA, length = 0x0002 + INT06 : origin = 0xFFEC, length = 0x0002 + INT07 : origin = 0xFFEE, length = 0x0002 + INT08 : origin = 0xFFF0, length = 0x0002 + INT09 : origin = 0xFFF2, length = 0x0002 + INT10 : origin = 0xFFF4, length = 0x0002 + INT11 : origin = 0xFFF6, length = 0x0002 + INT12 : origin = 0xFFF8, length = 0x0002 + INT13 : origin = 0xFFFA, length = 0x0002 + INT14 : origin = 0xFFFC, length = 0x0002 + RESET : origin = 0xFFFE, length = 0x0002 +} + +/****************************************************************************/ +/* Specify the sections allocation into memory */ +/****************************************************************************/ + +SECTIONS +{ + .bss : {} > RAM /* Global & static vars */ + .data : {} > RAM /* Global & static vars */ + .TI.noinit : {} > RAM /* For #pragma noinit */ + .sysmem : {} > RAM /* Dynamic memory allocation area */ + .stack : {} > RAM (HIGH) /* Software system stack */ + + .text : {} > FLASH /* Code */ + .cinit : {} > FLASH /* Initialization tables */ + .const : {} > FLASH /* Constant data */ + .bslsignature : {} > BSLSIGNATURE /* BSL Signature */ + .cio : {} > RAM /* C I/O Buffer */ + + .pinit : {} > FLASH /* C++ Constructor tables */ + .binit : {} > FLASH /* Boot-time Initialization tables */ + .init_array : {} > FLASH /* C++ Constructor tables */ + .mspabi.exidx : {} > FLASH /* C++ Constructor tables */ + .mspabi.extab : {} > FLASH /* C++ Constructor tables */ +#ifdef __TI_COMPILER_VERSION__ + #if __TI_COMPILER_VERSION__ >= 15009000 + #ifndef __LARGE_CODE_MODEL__ + .TI.ramfunc : {} load=FLASH, run=RAM, table(BINIT) + #else + .TI.ramfunc : {} load=FLASH | FLASH2, run=RAM, table(BINIT) + #endif + #endif +#endif + + .infoA : {} > INFOA /* MSP430 INFO FLASH Memory segments */ + .infoB : {} > INFOB + .infoC : {} > INFOC + .infoD : {} > INFOD + + /* MSP430 Interrupt vectors */ + TRAPINT : { * ( .int00 ) } > INT00 type = VECT_INIT + .int01 : {} > INT01 + PORT1 : { * ( .int02 ) } > INT02 type = VECT_INIT + PORT2 : { * ( .int03 ) } > INT03 type = VECT_INIT + .int04 : {} > INT04 + ADC10 : { * ( .int05 ) } > INT05 type = VECT_INIT + USCIAB0TX : { * ( .int06 ) } > INT06 type = VECT_INIT + USCIAB0RX : { * ( .int07 ) } > INT07 type = VECT_INIT + TIMER0_A1 : { * ( .int08 ) } > INT08 type = VECT_INIT + TIMER0_A0 : { * ( .int09 ) } > INT09 type = VECT_INIT + WDT : { * ( .int10 ) } > INT10 type = VECT_INIT + COMPARATORA : { * ( .int11 ) } > INT11 type = VECT_INIT + TIMER1_A1 : { * ( .int12 ) } > INT12 type = VECT_INIT + TIMER1_A0 : { * ( .int13 ) } > INT13 type = VECT_INIT + NMI : { * ( .int14 ) } > INT14 type = VECT_INIT + .reset : {} > RESET /* MSP430 Reset vector */ +} + +/****************************************************************************/ +/* Include peripherals memory map */ +/****************************************************************************/ + +-l msp430g2553.cmd + diff --git a/main.c b/main.c new file mode 100644 index 0000000..ca84948 --- /dev/null +++ b/main.c @@ -0,0 +1,39 @@ +#include +#include "typedefExtention.h" +#include "SPI.h" +#include "motorDriver.h" +#include "motionController.h" + +/** + * main.c + */ +int main(void) +{ + int v; + + WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer + + // set cpu op 16MHz + BCSCTL1 = CALBC1_16MHZ; + DCOCTL = CALDCO_16MHZ; + + + SPIInit(); + + // wait some time so the other ic have some time to start + __delay_cycles (1000000); + + MDInit(); // initilize motor driver + MCInit(); // initilize motion controller + + // set snelheid van motor op 50 (ongeveer 1.7 mm/s of 6 meter/uur) + v = 50; + MCSetV(1, v); + + // start een oneinige loop dit elke seconde de senlhied veranderd naar een (pseudo) random waarde + while(1){ + __delay_cycles (15500000); + v = 2047.0 * sin((double) v); + MCSetV(1, v); + } +} diff --git a/motionController.c b/motionController.c new file mode 100644 index 0000000..84c15bc --- /dev/null +++ b/motionController.c @@ -0,0 +1,176 @@ +/* + * motionController.c + * + * TMC429 + * + * Created on: 5 mei 2020 + * Author: mreenen + */ + +#include +#include "typedefExtention.h" +#include "SPI.h" +#include "motionController.h" + +const uchar MC_CS = BIT0; + +// target position 24RW +#define MC_XTar1 0x00 +#define MC_XTar2 0x20 +#define MC_XTar3 0x40 +// actual position 24R (RW if in velocity or hold mode) +#define MC_XAtu1 0x02 +#define MC_XAtu2 0x22 +#define MC_XAtu3 0x42 +// velocity minimum (can be stopt emidetly) 11uRW +#define MC_VMin1 0x04 +#define MC_VMin2 0x24 +#define MC_VMin3 0x44 +// velocity maximum 11uRW +#define MC_VMax1 0x06 +#define MC_VMax2 0x26 +#define MC_VMax3 0x46 +// velocity target 12sRW +#define MC_VTar1 0x08 +#define MC_VTar2 0x28 +#define MC_VTar3 0x48 +// velocity actual 12sR +#define MC_VAtu1 0x0A +#define MC_VAtu2 0x2A +#define MC_VAtu3 0x4A +// acceleration maximum 11uRW +#define MC_AMax1 0x0C +#define MC_AMax2 0x2C +#define MC_AMax3 0x4C +// acceleration actual 12sRW +#define MC_AAtu1 0x0E +#define MC_AAtu2 0x2E +#define MC_AAtu3 0x4E +// acceleration threshold 11uRW +#define MC_AAtu1 0x0E +#define MC_AAtu2 0x2E +#define MC_AAtu3 0x4E +// P value for ramp mode +#define MC_P1 0x12 +#define MC_P2 0x32 +#define MC_P3 0x52 +// ramp_mode, ref conf and lp +#define MC_Rmcl1 0x14 +#define MC_Rmcl2 0x34 +#define MC_Rmcl3 0x54 +#define MC_lp 0x010000 +#define MC_REF_CONF 8 +#define MC_RAMP_MODE 0b00 +#define MC_SOFT_MODE 0b01 +#define MC_VELOCITY_MODE 0b10 +#define MC_HOLD_MODE 0b11 +// deviders +#define MD_Div1 0x18 +#define MD_Div2 0x38 +#define MD_Div3 0x58 +#define MD_PULSE_DIV 12 +#define MD_RAMP_DIV 8 + + +//TODO: make function to calculate and write P + +void MC_write(uchar addr, ulong data) { + char i; + + // wait for SPI bus to be available + SPIInitSend(MC_CS, SPI_Mode3); + + // set write bit to write + addr &= ~0x01; + SPI_packet[1] = addr; + + // save data to SPI packet buffer + for(i=4; i>1; i--){ + SPI_packet[i] = data; + data >>= 8; + } + + SPI_packetC = 1; // set counter to one so it skips byte 0 and sends 4 bytes + SPISend(); +} + +ulong MC_read(unsigned char addr) { + char i; + ulong data; + + // wait for SPI bus to be available + SPIInitSend(MC_CS, SPI_Mode3); + + // set write bit to read + addr |= 0x01; + SPI_packet[1] = addr; + + SPI_packetC = 1; // set counter to one so it skips byte 0 and sends 4 bytes + SPISend(); + + // wait for SPI bus to be available + SPIInitSend(MC_CS, SPI_Mode3); + + // set write bit to read + addr |= 0x01; + SPI_packet[1] = addr; + + SPI_packetC = 1; // set counter to one so it skips byte 0 and sends 4 bytes + SPISend(); + + // read data + for(i=2; i<5; i++){ + data |= (ulong) SPI_packet[i]; + data <<= 8; + } + + return data; +} + +void MCInit(){ + P1DIR |= MC_CS; // set MC_CS (pin 5) as output + P1OUT |= MC_CS; // set MC_CS high + + + // set min and max velocity + MC_write(MC_VMin1, 0); + MC_write(MC_VMin2, 0); + MC_write(MC_VMax1, 2047); + MC_write(MC_VMax2, 2047); + + // set clock dividers + MC_write(MD_Div1, (9 << MD_PULSE_DIV) | (3 << MD_RAMP_DIV)); + MC_write(MD_Div2, (9 << MD_PULSE_DIV) | (3 << MD_RAMP_DIV)); + + // max acceleration + MC_write(MC_AMax1, 1); + MC_write(MC_AMax2, 1); + + // set ramp mode and reference switch configuration + MC_write(MC_Rmcl1, (0b0111 << MC_REF_CONF) | MC_VELOCITY_MODE); + MC_write(MC_Rmcl2, (0b0111 << MC_REF_CONF) | MC_VELOCITY_MODE); + + //DEBUG + MC_read(MC_VMax1); + MC_read(MC_Rmcl2); + MC_read(MC_AMax1); +} + +void MCSetV(uchar m, int v){ + if(v < 0) + v |= 0x0800; // set signed bit + else + v &= ~0x0800; // remove signed bit + v &= 0x0FFF; // contraint to 12 bits + switch (m){ + case 1: + MC_write(MC_VTar1, v); + break; + case 2: + MC_write(MC_VTar2, v); + break; + case 3: + MC_write(MC_VTar3, v); + break; + } +} diff --git a/motionController.h b/motionController.h new file mode 100644 index 0000000..efaeb20 --- /dev/null +++ b/motionController.h @@ -0,0 +1,14 @@ +/* + * modtionController.h + * + * Created on: 5 mei 2020 + * Author: mreenen + */ + +#ifndef MOTIONCONTROLLER_H_ +#define MOTIONCONTROLLER_H_ + +void MCInit(); +void MCSetV(uchar m, int v); + +#endif /* MOTIONCONTROLLER_H_ */ diff --git a/motorDriver.c b/motorDriver.c new file mode 100644 index 0000000..96454eb --- /dev/null +++ b/motorDriver.c @@ -0,0 +1,124 @@ +/* + * motorDriver.c + * + * Created on: 4 mei 2020 + * Author: mreenen + */ + +#include +#include "typedefExtention.h" +#include "SPI.h" +#include "motorDriver.h" + +const uchar MD_CS = BIT3; + +// register 0x00 GCINF +#define MD_GCONF 0X00 +#define MD_GCONF_singgelMotor 0x00000001 +#define MD_GCONF_stepdir1Enable 0x00000002 +#define MD_GCONF_stepdir2Enable 0x00000004 +#define MD_GCONF_motor1Revers 0x00000010 +#define MD_GCONF_motor1Revers 0x00000010 +#define MD_GCONF_lockGCONF 0x00000020 + +// register 0x01 GSTAT +#define MD_GSTAT 0X01 + +// register 0x30, 0x50 IHOLD_IRUN +#define MD_IHIR1 0x30 +#define MD_IHIR2 0x50 +#define MD_IHIR_iHold 0 +#define MD_IHIR_iRun 8 +#define md_IHIR_iHoldDelay 16ul + +// register 0x6C, 7C CHOPCONF +#define MD_CC1 0X6C +#define MD_CC2 0X7C +#define MD_CC_shortProtection 0x40000000 +#define MD_CC_doubbleEdge 0x20000000 +#define MD_CC_16ustapI 0x10000000 +#define MD_CC_mres 24 // 4 bits; 1/(2^x / 2^8) microstappen +#define MD_CC_vsens 0x00020000 +#define MD_CC_tbl 15 // 3 bits? +#define MD_CC_cmh BIT14 +#define MD_CC_rndtf 0x00002000 +#define MD_CC_disfdcc 0x00001000 +#define MD_CC_fd3 BTI11 +#define MD_CC_hend 7 // 4 bits +#define MD_CC_hstrt 4 // 3 bits +#define MD_CC_toff 0 // 4 bits + +// bool md_checkError(){ +// unsigned long stat = md_read(MD_GSTAT); +// if(stat != 0){ +// if(stat == 0x1){ // only a recet has occert +// // restart the motor driver +// md_setup(); +// }else{ // one of the motorst stopt due to short or overheated or a undervoltage in chargepump +// // wait a while for checking again +// delay(1000); +// md_checkError(); +// } +// return true; +// } +// return false; +// } + +void MD_write(uchar addr, ulong data) { + char i; + + // wait for SPI bus to be available + SPIInitSend(MD_CS, SPI_Mode3); + + // set write bit to write + addr |= 0x80; + SPI_packet[0] = addr; + + // save data to packet buffer + for(i=4; i>0; i--){ + SPI_packet[i] = data; + data >>= 8; + } + + SPISend(SPI_Mode3); +} + + +unsigned long MD_read(uchar addr) { + char i; + ulong data; + + // wait for SPI bus to be available + SPIInitSend(MD_CS, SPI_Mode3); + + // set write bit to read + addr &= ~0x80; + SPI_packet[0] = addr; + + SPISend(SPI_Mode3); + + // wait for SPI bus to be available + SPIInitSend(MD_CS, SPI_Mode3); + + SPI_packet[0] = addr; + SPISend(SPI_Mode3); + + // read data + for(i=2; i<5; i++){ + data |= (ulong) SPI_packet[i]; + data <<= 8; + } + + return data; +} + +void MDInit(){ + P1DIR |= MD_CS; // set MD_CS (pin 3) as output + P1OUT |= MD_CS; // set MD_CS high + + MD_write(MD_GCONF, MD_GCONF_stepdir2Enable | MD_GCONF_stepdir1Enable); + MD_write(MD_CC1, 3ul<