Test application for getting the Nucleo F0 30 board to work with Evan's prototype LED board.

Dependencies:   mbed

main.cpp

Committer:
bgrissom
Date:
2014-08-05
Revision:
4:4eeacb39a417
Parent:
3:6f12c437ab88
Child:
5:9a662dec2ddb

File content as of revision 4:4eeacb39a417:

#include "mbed.h" 

#define OK    (0)
#define ERROR (-1)

// Forward Declarations
void pwmout_period_ns(pwmout_t* obj, int us);
int  cmd_S0(uint16_t value);
void cmd_S1(void);

// Globals
bool gSpiMode = false;
SPI* gSpiPtr = NULL;
DigitalOut gbbTRANS(D8); // Global bit bang TRANS (data) line


int main() {
    // NOTE: 24MHz is half the 48MHz clock rate.  The PWM registers
    //       seem to only allow 24MHz at this point, so I'm matching
    //       the SPI bus speed to be the same.
    //
    //       1/24MHz  =>  1/(24*10^6)  =>  41.6*10^-9 second period,
    //       which means 41.6ns period and 20.8ns pulse width at
    //       50% duty cycle (which seems to be right for the SPI clock
    //       line as well as a reasonable choice for the PWM line).

    // BAG ORIG: gbbTRANS = 1; // Start with TRANS high.  It acts like a SPI slave select
                  // that is active-low.
    gbbTRANS = 0;

    // PWMCLK
    pwmout_t outs;
    pwmout_init(&outs, D9);
    pwmout_period_ns(&outs, 2); // 24 MHz (not very clean on the scope)
    // pwmout_period_ns(&outs, 40); // 1.2 MHz on the scope
    pwmout_write(&outs, 0.5f);

    int ret = OK; // Return value
    int i = 0;

    printf("17:10\n");

    while (1) {
        for (i=0; i<16; i++) {
            ret = cmd_S0(0xFFFF);
            // ORIG: ret = cmd_S0(0xFFFF);
            if (ret != OK) {
                printf("ERROR cmd_S0()\n");
                return ERROR;
            }
        }
        cmd_S1();
    }
}



// S0 Command:
//      Needs only SCK and SIN (which are SPI_SCK and SPI_MOSI respectively).
//      This is because TRANS can be 0 for this command according to the datasheet.
int cmd_S0(uint16_t value) {
        // Command S0 and S1 share the same clock line, so we need to be
        // careful which mode we are in.  This avoids re-initializing these
        // pins if we are already in SPI mode.
        // WARNING: Re-initializing every time makes the MOSI line dirty and
        //          is wasteful for the CPU.
        if ( gSpiMode == false &&
             gSpiPtr  == NULL)
        {
            // We are not using MISO, this is a one-way bus
            gSpiPtr = new SPI(SPI_MOSI, NC, SPI_SCK);

            if (gSpiPtr == NULL) {
                printf("ERROR: Could not allocate SPI\n");
                return ERROR;
            }

            // Note: Polarity and phase are both 0 for the TC62D723FNG
            // For a graphical reminder on polarity and phase, visit:
            //     http://www.eetimes.com/document.asp?doc_id=1272534
            gSpiPtr->format(16, 0);
            // gSpiPtr->frequency(1000000);  // 1.5 MHz on the scope
            gSpiPtr->frequency(24000000); // 24 MHz
            gSpiMode = true;
        }
        gbbTRANS = 0; // Like an SPI slave select
        gSpiPtr->write(value);
        gbbTRANS = 1; // Like an SPI slave select
        // LONGTERM OPTIMIZATION: Evan suggests setting it 
        // wait_us(1);
        // gbbTRANS = 0; // Set back low
        return OK;
}



void cmd_S1(void) {
    int i = 0;
    int j = 0;
    
    gbbTRANS = 0; // FIXME
    
    if ( gSpiMode == true &&
         gSpiPtr  != NULL)
    {
        delete gSpiPtr;
        gSpiPtr  = NULL;
        gSpiMode = false;
    }
    
    DigitalOut bbSCK (D13); // bit bang clock

    bbSCK    = 0; // Start off/low
    gbbTRANS = 1; // Set high

    // Loop 6 times = 3 clock cycles
    for (j=0; j<6; j++) { // Always use an even number here!
        // The order of these two lines matter!
        i == 0 ? i = 1 : i = 0;          // Toggle i
        i == 0 ? bbSCK = 0 : bbSCK = 1;  // Set SCK to the same value as i
    }
    gbbTRANS = 0; // Set low
}



// This code is based off:
// mbed/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/pwmout_api.c  pwmout_period_us()
void pwmout_period_ns(pwmout_t* obj, int us) {
    TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm);
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    float dc = pwmout_read(obj);

    TIM_Cmd(tim, DISABLE);

    obj->period = us;

    TIM_TimeBaseStructure.TIM_Period = obj->period - 1;
    // Orig code:  TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
    TIM_TimeBaseStructure.TIM_Prescaler = 0;  // BAG 1 ns tick (?)
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure);

    // Set duty cycle again
    pwmout_write(obj, dc);

    TIM_ARRPreloadConfig(tim, ENABLE);

    TIM_Cmd(tim, ENABLE);
}