naoki tanabe / a4960
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers a4960.cpp Source File

a4960.cpp

00001 #include "a4960.h"
00002 
00003 a4960::a4960() : spi(PB_15,PB_14,PB_13), _cs_pin(PB_1), _blink_pin(LED1), _PWM_pin(PA_11)
00004 {
00005     PWM_freq = 20000.0;
00006     PWM_duty = 0.5;
00007     motor_started = true;
00008 
00009     _config[0] = 0b0000000000000000; // 50 us comm blank time, 2.4 us blank time, 200 ns deadtime
00010     _config[1] = 0b0010000000000000; // Vri = 100% Vref, Vdsth = 800mV
00011     _config[2] =  0b0100000000000000; // 35.6 us current control time
00012     _config[3] = 0b0110000000000000; // current limited, 50% current for hold, 18ms hold time
00013     _config[4] =   0b1000000000000000; // 0.8ms min comm time, 24 ms start comm time
00014     _config[5] =  0b1010000000000000; // 16.875deg phase adv, 100% ramp current, 0.4ms ramp rate
00015     _config[6] =  0b1100000000000000; // fault detection all on
00016     _config[7] = 0b1110000000000100;
00017     // auto BEMF hyst, 3.2us zx det window, no stop on fail, DIAG pin = fault, restart on loss of sync, brake off, forward, coast
00018     
00019     SPI_init();
00020 }
00021 
00022 void a4960::SPI_init()
00023 {
00024     // initialize pins
00025     _cs_pin = 1;
00026     _blink_pin = 1;
00027     _PWM_pin.period(1/PWM_freq);
00028     _PWM_pin.write(PWM_duty);
00029     spi.format(16,3);
00030     spi.frequency(1000000);
00031     // initialize SPI connection
00032     //SPI.begin(P0_8, P0_9, P0_11);//SCK, MOSI, MOSI
00033     // set registers to default config
00034     for (int i = 0; i < 8; i++) {
00035         write_to_a4960(_config[i]);
00036     }
00037     // turn off pin to indicate initialization complete
00038     _blink_pin = 0;
00039 }
00040 void a4960::write_to_a4960(uint16_t msg)
00041 {
00042     // split 16-bit message to a4960 into two 8-bit messages
00043     uint8_t ms_half;
00044     uint8_t ls_half;
00045 
00046     // ---- 1st message (bits 15 - 8)
00047     // 3 bit address
00048     // 12 bit message
00049     // shift message to get rid of 8 LSB on the end and OR
00050     // the write bit and the
00051     // 4 remaining bits into the first-half msg
00052     ms_half = 1 << 4 | (uint8_t)(msg>>8);
00053     // ---- 2nd message (bits 7 - 0)
00054     ls_half = (uint8_t)(msg);
00055 
00056     // pull to active low
00057     _cs_pin = 0;
00058     wait_us(200);
00059 
00060     // transfer the to messages halves, MSB first
00061     printf("test %d :%d\n\r",spi.write(ms_half<<8 | ls_half),(ms_half<<8 | ls_half));
00062     printf("test2 :%d",spi.write(0));
00063 
00064     // wait and pull CS line back to inactive high
00065     wait_us(200);
00066     _cs_pin = 1;
00067 }
00068 
00069 void a4960::write_run(void)
00070 {
00071     write_to_a4960(_config[7] | 1);
00072 }
00073 
00074 void a4960::write_brake(void)
00075 {
00076     write_to_a4960(_config[7]);
00077 }
00078 
00079 int a4960::read()
00080 {   _cs_pin = 0;
00081     uint8_t h;
00082     wait_us(200);
00083     h = spi.write(0b1110000000000000);
00084     //h = spi.write(0);
00085     wait_us(200);
00086     _cs_pin = 1;
00087     return h;
00088 }