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-07-11
- Revision:
- 2:a57a5501152c
- Parent:
- 1:256d7a2f8391
- Child:
- 3:6f12c437ab88
File content as of revision 2:a57a5501152c:
#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); bool gSpiMode = false; SPI* gSpiPtr = NULL; 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). ///////////////////////////////////////////////// // 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); // pwmout_write(&outs, 0.5f); int ret = OK; // Return value int i = 0; printf("17:32\n"); while (1) { //wait_ms(50); for (i=0; i<16; i++) { ret = cmd_S0(0x0003); if (ret != OK) { printf("ERROR cmd_S0()\n"); return ERROR; } } cmd_S1(); } } // 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); } // 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 MHz //gSpiPtr->frequency(24000000); // 24 MHz gSpiMode = true; } gSpiPtr->write(value); return OK; } void cmd_S1(void) { int i = 0; int j = 0; if ( gSpiMode == true && gSpiPtr != NULL) { delete gSpiPtr; gSpiPtr = NULL; gSpiMode = false; } DigitalOut bbSCK (D13); // bit bang clock DigitalOut bbTRANS(D8); // bit bang TRANS (data) line bbSCK = 0; // Start off/low bbTRANS = 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 } bbTRANS = 0; // Set low }