static void TASK_SPI_Handler(void *pvParameters) { portTickType xLastExecutionTime; // Initialise the xLastExecutionTime variable on task entry xLastExecutionTime = xTaskGetTickCount(); // fill Tx Buffer with housenumbers { int i; for(i=0; i<TRANSFER_BUFFER_SIZE; ++i) tx_buffer[i] = i; } while( 1 ) { // wait for 100 mS vTaskDelayUntil(&xLastExecutionTime, 100 / portTICK_RATE_MS); // toggle Status LED to as a sign of live MIOS32_BOARD_LED_Set(1, ~MIOS32_BOARD_LED_Get()); // activate CS pin MIOS32_SPI_RC_PinSet(MASTER_SPI, MASTER_CS_PIN, 0); // spi, rc_pin, pin_value // send data non-blocking MIOS32_SPI_TransferBlock(MASTER_SPI, tx_buffer, rx_buffer, TRANSFER_BUFFER_SIZE, SPI_Callback); } }
///////////////////////////////////////////////////////////////////////////// // This task periodically sends a SPI datastream ///////////////////////////////////////////////////////////////////////////// static void SPI_Callback(void) { // called after SPI transfer: // deactivate CS pin MIOS32_SPI_RC_PinSet(MASTER_SPI, MASTER_CS_PIN, 1); // spi, rc_pin, pin_value // change TX values int i; for(i=0; i<TRANSFER_BUFFER_SIZE; ++i) tx_buffer[i] += 0x10; }
s32 SEQ_TERMINAL_TestAoutPin(void *_output_function, u8 pin_number, u8 level) { void (*out)(char *format, ...) = _output_function; s32 status = 0; MUTEX_MIDIOUT_TAKE; switch( pin_number ) { case 0: AOUT_SuspendSet(0); out("Module has been re-initialized and can be used again!\n"); break; case 1: AOUT_SuspendSet(1); out("Setting AOUT:CS pin to ca. %dV - please measure now!\n", level ? 4 : 0); #if !defined(MIOS32_FAMILY_EMULATION) MIOS32_SPI_RC_PinSet(2, 0, level ? 1 : 0); // spi, rc_pin, pin_value #endif break; case 2: AOUT_SuspendSet(1); out("Setting AOUT:SI pin to ca. %dV - please measure now!\n", level ? 4 : 0); #if !defined(MIOS32_FAMILY_EMULATION) MIOS32_SPI2_SET_MOSI(level ? 1 : 0); #endif break; case 3: AOUT_SuspendSet(1); out("Setting AOUT:SC pin to ca. %dV - please measure now!\n", level ? 4 : 0); #if !defined(MIOS32_FAMILY_EMULATION) if( level ) { MIOS32_SPI2_SET_SCLK_1; } else { MIOS32_SPI2_SET_SCLK_0; } #endif break; default: out("ERROR: unsupported pin #%d", pin_number); } MUTEX_MIDIOUT_GIVE; return status; }
///////////////////////////////////////////////////////////////////////////// // This hook is called after startup to initialize the application ///////////////////////////////////////////////////////////////////////////// void APP_Init(void) { // initialize all LEDs MIOS32_BOARD_LED_Init(0xffffffff); // initialize SPI interface // ensure that fast pin drivers are activated MIOS32_SPI_IO_Init(MASTER_SPI, MIOS32_SPI_PIN_DRIVER_STRONG); // init SPI port MIOS32_SPI_TransferModeInit(MASTER_SPI, MIOS32_SPI_MODE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_128); // ensure that chip select line deactivated MIOS32_SPI_RC_PinSet(MASTER_SPI, MASTER_CS_PIN, 1); // spi, rc_pin, pin_value // start task xTaskCreate(TASK_SPI_Handler, (signed portCHAR *)"SPI_Handler", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_SPI_HANDLER, NULL); }
static void TASK_MatrixScan(void *pvParameters) { while( 1 ) { // wait for next timesplice (1 mS) vTaskDelay(1 / portTICK_RATE_MS); // determine timestamp (we need it for delay measurements) mios32_sys_time_t t = MIOS32_SYS_TimeGet(); u32 timestamp = 1000*t.seconds + t.fraction_ms; // loop: // - latch DIN/DOUT values // - shift selection pattern for *next* row to DOUT registers // - read DIN values of previously selected row // since we need to select the first row before the first DIN values are latched, we loop from -1 // to handle the initial state int row; for(row=-1; row<MATRIX_NUM_ROWS; ++row) { if( row >= 0 ) { // not required for initial scan // latch DIN values MIOS32_SPI_RC_PinSet(MIOS32_SRIO_SPI, MIOS32_SRIO_SPI_RC_PIN, 0); // spi, rc_pin, pin_value MIOS32_DELAY_Wait_uS(1); MIOS32_SPI_RC_PinSet(MIOS32_SRIO_SPI, MIOS32_SRIO_SPI_RC_PIN, 1); // spi, rc_pin, pin_value } // determine selection mask for next row (written into DOUT registers while reading DIN registers) u16 select_row_pattern = ~(1 << (row+1)); #if MATRIX_DOUT_HAS_SINK_DRIVERS select_row_pattern ^= 0xffff; // invert selection pattern if sink drivers are connected to DOUT pins #endif // read DIN, write DOUT u8 din0 = MIOS32_SPI_TransferByte(MIOS32_SRIO_SPI, (select_row_pattern >> 8) & 0xff); u8 din1 = MIOS32_SPI_TransferByte(MIOS32_SRIO_SPI, (select_row_pattern >> 0) & 0xff); // latch new DOUT value MIOS32_SPI_RC_PinSet(MIOS32_SRIO_SPI, MIOS32_SRIO_SPI_RC_PIN, 0); // spi, rc_pin, pin_value MIOS32_DELAY_Wait_uS(1); MIOS32_SPI_RC_PinSet(MIOS32_SRIO_SPI, MIOS32_SRIO_SPI_RC_PIN, 1); // spi, rc_pin, pin_value if( row >= 0 ) { // combine read DIN bytes to 16bit value u16 din_pattern = (din1 << 8) | din0; // check if values have been changed via XOR combination with previously scanned value u16 changed = din_pattern ^ din_value[row]; if( changed ) { // store changed value din_value[row] = din_pattern; // notify changed value int column; for(column=0; column<16; ++column) { u16 mask = 1 << column; if( changed & mask ) BUTTON_NotifyToggle(row, column, (din_pattern & mask) ? 1 : 0, timestamp); } } } } } }
///////////////////////////////////////////////////////////////////////////// //! Initializes SPI pins //! \param[in] mode currently only mode 0 supported //! \return < 0 if initialisation failed ///////////////////////////////////////////////////////////////////////////// s32 MIOS32_SPI_Init(u32 mode) { // currently only mode 0 supported if( mode != 0 ) return -1; // unsupported mode DMA_InitTypeDef DMA_InitStructure; DMA_StructInit(&DMA_InitStructure); /////////////////////////////////////////////////////////////////////////// // SPI0 /////////////////////////////////////////////////////////////////////////// #ifndef MIOS32_DONT_USE_SPI0 // disable callback function spi_callback[0] = NULL; // set RC pin(s) to 1 MIOS32_SPI_RC_PinSet(0, 0, 1); // spi, rc_pin, pin_value MIOS32_SPI_RC_PinSet(0, 1, 1); // spi, rc_pin, pin_value // IO configuration MIOS32_SPI_IO_Init(0, MIOS32_SPI_PIN_DRIVER_WEAK_OD); // enable SPI peripheral clock (APB2 == high speed) RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); // enable DMA1 clock RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // DMA Configuration for SPI Rx Event DMA_Cmd(MIOS32_SPI0_DMA_RX_PTR, DISABLE); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&MIOS32_SPI0_PTR->DR; DMA_InitStructure.DMA_MemoryBaseAddr = 0; // will be configured later DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 0; // will be configured later DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(MIOS32_SPI0_DMA_RX_PTR, &DMA_InitStructure); // DMA Configuration for SPI Tx Event // (partly re-using previous DMA setup) DMA_Cmd(MIOS32_SPI0_DMA_TX_PTR, DISABLE); DMA_InitStructure.DMA_MemoryBaseAddr = 0; // will be configured later DMA_InitStructure.DMA_BufferSize = 0; // will be configured later DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_Init(MIOS32_SPI0_DMA_TX_PTR, &DMA_InitStructure); // enable SPI SPI_Cmd(MIOS32_SPI0_PTR, ENABLE); // enable SPI interrupts to DMA SPI_I2S_DMACmd(MIOS32_SPI0_PTR, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE); // Configure DMA interrupt MIOS32_IRQ_Install(MIOS32_SPI0_DMA_IRQ_CHANNEL, MIOS32_IRQ_SPI_DMA_PRIORITY); // initial SPI peripheral configuration MIOS32_SPI_TransferModeInit(0, MIOS32_SPI_MODE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_128); #endif /* MIOS32_DONT_USE_SPI0 */ /////////////////////////////////////////////////////////////////////////// // SPI1 /////////////////////////////////////////////////////////////////////////// #ifndef MIOS32_DONT_USE_SPI1 // disable callback function spi_callback[1] = NULL; // set RC pin(s) to 1 MIOS32_SPI_RC_PinSet(1, 0, 1); // spi, rc_pin, pin_value MIOS32_SPI_RC_PinSet(1, 1, 1); // spi, rc_pin, pin_value // IO configuration MIOS32_SPI_IO_Init(1, MIOS32_SPI_PIN_DRIVER_WEAK_OD); // enable SPI peripheral clock (APB1 == slow speed) RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); // enable DMA1 clock RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // DMA Configuration for SPI Rx Event DMA_Cmd(MIOS32_SPI1_DMA_RX_PTR, DISABLE); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&MIOS32_SPI1_PTR->DR; DMA_InitStructure.DMA_MemoryBaseAddr = 0; // will be configured later DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 0; // will be configured later DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(MIOS32_SPI1_DMA_RX_PTR, &DMA_InitStructure); // DMA Configuration for SPI Tx Event // (partly re-using previous DMA setup) DMA_Cmd(MIOS32_SPI1_DMA_TX_PTR, DISABLE); DMA_InitStructure.DMA_MemoryBaseAddr = 0; // will be configured later DMA_InitStructure.DMA_BufferSize = 0; // will be configured later DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_Init(MIOS32_SPI1_DMA_TX_PTR, &DMA_InitStructure); // enable SPI SPI_Cmd(MIOS32_SPI1_PTR, ENABLE); // enable SPI interrupts to DMA SPI_I2S_DMACmd(MIOS32_SPI1_PTR, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE); // Configure DMA interrupt MIOS32_IRQ_Install(MIOS32_SPI1_DMA_IRQ_CHANNEL, MIOS32_IRQ_SPI_DMA_PRIORITY); // initial SPI peripheral configuration MIOS32_SPI_TransferModeInit(1, MIOS32_SPI_MODE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_128); #endif /* MIOS32_DONT_USE_SPI1 */ /////////////////////////////////////////////////////////////////////////// // SPI2 (software emulated) /////////////////////////////////////////////////////////////////////////// #ifndef MIOS32_DONT_USE_SPI2 // disable callback function spi_callback[2] = NULL; // set RC pin(s) to 1 MIOS32_SPI_RC_PinSet(2, 0, 1); // spi, rc_pin, pin_value MIOS32_SPI_RC_PinSet(2, 1, 1); // spi, rc_pin, pin_value // IO configuration MIOS32_SPI_IO_Init(2, MIOS32_SPI_PIN_DRIVER_WEAK_OD); // initial SPI peripheral configuration MIOS32_SPI_TransferModeInit(2, MIOS32_SPI_MODE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_128); #endif /* MIOS32_DONT_USE_SPI2 */ return 0; // no error }