static uint16_t tempSpiCycle(uint16_t data) { volatile uint16_t value; // Put SPI port into receive mode, this starts SCK running continuously SPI_BiDirectionalLineConfig(SPI1,SPI_Direction_Rx); SPI_I2S_SendData(SPI1,data); // Wait for Rx flag to be set while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); // Stop the clock! Put SPI port into transmit mode SPI_BiDirectionalLineConfig(SPI1,SPI_Direction_Tx); // Read data value=SPI_I2S_ReceiveData(SPI1); // Arbitrary delay because there's no way of telling when the SPI has // finished TIM_Cmd(TIM2,DISABLE); TIM_SetCounter(TIM2,CYCLE_WAIT_TIME); TIM_ClearFlag(TIM2,TIM_FLAG_Update); TIM_Cmd(TIM2,ENABLE); while(!TIM_GetFlagStatus(TIM2,TIM_FLAG_Update)); return value; }
/** * @brief configuration of all spi related things^^ * @param None * @retval None */ void spi_config() { // RCC RCC_PCLK2Config(RCC_HCLK_Div2); /* Enable SPI_MASTER clock and GPIO clock for SPI_MASTER */ RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | SPI_MASTER_CLK, ENABLE); // GPIO GPIO_InitTypeDef GPIO_InitStructure; /* Configure SPI_MASTER pins: NSS, SCK, MOSI*/ GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MOSI; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SS_1 | SPI_MASTER_PIN_SS_2 | SPI_MASTER_PIN_SS_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure); // NVIC /* no interrupts */ SPI_InitTypeDef SPI_InitStructure; // SPI /* SPI_MASTER configuration ------------------------------------------------------*/ SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI_MASTER, &SPI_InitStructure); SPI_BiDirectionalLineConfig(SPI_MASTER, SPI_Direction_Tx); /* Enable SPI_MASTER */ SPI_Cmd(SPI_MASTER, ENABLE); }
/** * @brief configuration of all spi related things^^ * @param 0,1 or 2 * @retval angle */ uint16_t read_spi(uint8_t selected_spi) { /* variables */ uint8_t SPI_MASTER_Buffer_Rx[RxBufferSize]; static uint16_t SPI_SELECT[]={SPI_MASTER_PIN_SS_3, SPI_MASTER_PIN_SS_2, SPI_MASTER_PIN_SS_1}; static uint8_t RxIdx = 0; static uint16_t sensor_angle; static uint16_t sensor_angle_invert; static uint16_t angle_to_send; /* prepare gpio_init structure for mode changes ----------------------------*/ static GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_MOSI; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* CHIP SELECT ON*/ SPI_SS_LOW(selected_spi); Delay(1); /* send data request */ SPI_I2S_SendData(SPI_MASTER, 0xAA); /* wait till data was sent */ while(SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_TXE)==RESET); while(SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_BSY)==SET); /* change from transmit to receive mode */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure); SPI_Cmd(SPI_MASTER, DISABLE); Delay(2); SPI_BiDirectionalLineConfig(SPI_MASTER, SPI_Direction_Rx); Delay(3); /* receive data: 10 bytes*/ RxIdx=0; while(RxIdx<RxBufferSize) { SPI_Cmd(SPI_MASTER, ENABLE); Delay(5); SPI_Cmd(SPI_MASTER, DISABLE); while(SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_RXNE)!=RESET) SPI_MASTER_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI_MASTER); Delay(5); } /* back to transmit mode */ SPI_BiDirectionalLineConfig(SPI_MASTER, SPI_Direction_Tx); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure); SPI_Cmd(SPI_MASTER, ENABLE); Delay(5); /* CHIP SELECT OFF */ SPI_SS_HIGH(selected_spi); /* data interpretation */ sensor_angle = (SPI_MASTER_Buffer_Rx[2] << 8) + SPI_MASTER_Buffer_Rx[3]; sensor_angle_invert = (SPI_MASTER_Buffer_Rx[4] << 8) + SPI_MASTER_Buffer_Rx[5]; if(!(sensor_angle & 1) || sensor_angle == 0xFFFF || (sensor_angle & sensor_angle_invert) || (sensor_angle | sensor_angle_invert) != 0xFFFF) { angle_to_send = 0xabcd; } else { angle_to_send = sensor_angle; } Delay(50); return angle_to_send; }
bool swdTest(void) { // Start = 1 // DP access = 0 // Read access = 1 // DP reg = 00 (IDCODE) // Parity = odd bits -> 1 // Stop = 0 // Park = 1 (not 0!) // First at least fifty ones, then the sequence below to switch from JTAG to SWD and then at least 50 ones again. // The the SWD is reset and ready to use. Note that once this is done the nRF will only go into simulated off mode since // a debugger is connected. // 111...01111001 11100111...111 // The command for reading IDCODE is /// 10100101 // Should read out (and does):0x0BB11477 (according to OpenOCD) //T ACK |------------DATA--------------|P //? 100 111011100010100010001101110100001 tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; #if 0 tx_data[idx++] = 0x9E; tx_data[idx++] = 0xE7; #else tx_data[idx++] = 0x79; tx_data[idx++] = 0xE7; #endif tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFF; tx_data[idx++] = 0xFC; tx_data[idx++] = header; // Rest is 0x00 /* As long as the SPI is enabled in bi-di mode the clock is enabled. So * we write data as normal, but when reading there's no need to output any * data to trigger the clock. Direction needs to be set accordingly. */ while(number_of_txd_bytes < transfer_size) { if (number_of_txd_bytes < idx) { SPI_BiDirectionalLineConfig(SWD_SPI, SPI_Direction_Tx); /*!< Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(SWD_SPI, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SWD_SPI, tx_data[number_of_txd_bytes]); // Make sure that we have sent data in case next loop will be RX, the // mode is switched before we manage to send the data! while (SPI_I2S_GetFlagStatus(SWD_SPI, SPI_I2S_FLAG_BSY) == SET); } else { SPI_BiDirectionalLineConfig(SWD_SPI, SPI_Direction_Rx); while (SPI_I2S_GetFlagStatus(SWD_SPI, SPI_I2S_FLAG_RXNE) == RESET); /*!< Return the byte read from the SPI bus */ rx_data[number_of_txd_bytes] = (uint8_t) SPI_I2S_ReceiveData(SWD_SPI); } number_of_txd_bytes++; } SPI_Cmd(SWD_SPI, DISABLE); return isInit; }