/*! * @brief Generic burst read * * @param [out] dev_id I2C address, SPI chip select or user desired identifier * * @return Zero if successful, otherwise an error code */ bstdr_ret_t bstdr_burst_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint32_t len){ uint8_t spi_address_cmd = 0x80; HAL_StatusTypeDef hal_res; /**< Burst read code comes here */ if (dev_id == 0x1A) //ACC sensor { //SPI chip select HAL_GPIO_WritePin(GPIOA, ACC_CS_Pin, GPIO_PIN_RESET); //SPI transcation begin spi_address_cmd |= reg_addr; hal_res = HAL_SPI_Transmit(&hspi3, &spi_address_cmd, 1, 500); hal_res = HAL_SPI_Receive(&hspi3, reg_data, len, 500); //SPI transcation end //SPI chip deselecet HAL_GPIO_WritePin(GPIOA, ACC_CS_Pin, GPIO_PIN_SET); } else if (dev_id == 0x1B) //Gyro sensor { //SPI chip select HAL_GPIO_WritePin(GPIOA, GYRO_CS_Pin, GPIO_PIN_RESET); //SPI transcation begin spi_address_cmd |= reg_addr; hal_res = HAL_SPI_Transmit(&hspi3, &spi_address_cmd, 1, 500); hal_res = HAL_SPI_Receive(&hspi3, reg_data, len, 500); //SPI transcation end //SPI chip deselecet HAL_GPIO_WritePin(GPIOA, GYRO_CS_Pin, GPIO_PIN_SET); } else { return BSTDR_E_CON_ERROR; } return BSTDR_OK; }
void ds3234_get_reg(uint8_t addr, uint8_t* val) { addr &= 0x7F; HAL_GPIO_WritePin(DS3234_CS_GPIO_Port, DS3234_CS_Pin, GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi2, &addr, 1, 1000); HAL_SPI_Receive(&hspi2, val, 1, 1000); HAL_GPIO_WritePin(DS3234_CS_GPIO_Port, DS3234_CS_Pin, GPIO_PIN_SET); }
// I/O void send_SPI(uint8_t * inBuf, uint8_t inLength, uint8_t * outBuf, uint8_t outLength) { HAL_GPIO_WritePin(COMP_CS_P, COMP_CS, GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi1, outBuf, outLength, 1000); HAL_SPI_Receive(&hspi1, inBuf, inLength, 1000); HAL_GPIO_WritePin(COMP_CS_P, COMP_CS, GPIO_PIN_SET); }
static int spi_test(void) { uint8_t res = 0; uint8_t data = 0x23; printf("SPI test\n"); while(1) { spi_delay(1000000); res = 0; // sync spi_sync(); printf(">> synchronized\n"); // data from master if (HAL_SPI_Receive(&SpiHandle, &res, 1, SPI_TIMEOUT_MAX) != HAL_OK) { printf("HAL_SPI_Receive error 1\n"); return -1; } if (res != 0x57) { printf("HAL_SPI_Receive recieved not 0x57 but %x\n", res); } // request data if (HAL_SPI_Transmit(&SpiHandle, &data, 1, SPI_TIMEOUT_MAX) != HAL_OK) { printf("HAL SPI Transmit error\n"); return -1; } } return 0; }
bool spiTransfer(SPI_TypeDef *instance, uint8_t *out, const uint8_t *in, int len) { SPIDevice device = spiDeviceByInstance(instance); HAL_StatusTypeDef status; #define SPI_DEFAULT_TIMEOUT 10 if(!out) // Tx only { status = HAL_SPI_Transmit(&spiHardwareMap[device].hspi, (uint8_t *)in, len, SPI_DEFAULT_TIMEOUT); } else if(!in) // Rx only { status = HAL_SPI_Receive(&spiHardwareMap[device].hspi, out, len, SPI_DEFAULT_TIMEOUT); } else // Tx and Rx { status = HAL_SPI_TransmitReceive(&spiHardwareMap[device].hspi, (uint8_t *)in, out, len, SPI_DEFAULT_TIMEOUT); } if( status != HAL_OK) spiTimeoutUserCallback(instance); return true; }
lepton_buffer* lepton_transfer(void) { HAL_StatusTypeDef status; lepton_buffer *buf = get_next_lepton_buffer(); vospi_packet *packet = (vospi_packet*)&buf->lines[0]; // DEBUG_PRINTF("Transfer starting: %p@%p\r\n", buf, packet); do { if ((status = HAL_SPI_Receive(&hspi2, (uint8_t*)packet, FRAME_TOTAL_LENGTH, 200)) != HAL_OK) { DEBUG_PRINTF("Error setting up SPI receive to buf: %p@%p: %d\r\n", buf, packet, status); buf->status = LEPTON_STATUS_RESYNC; return buf; } } while ((buf->lines[0].header[0] & 0x0f00) == 0x0f00); status = HAL_SPI_Receive_DMA(&hspi2, (uint8_t*)(packet + 1), FRAME_TOTAL_LENGTH * (IMAGE_NUM_LINES + TELEMETRY_NUM_LINES - 1)); if (status) { DEBUG_PRINTF("Error setting up SPI DMA receive: %d\r\n", status); buf->status = LEPTON_STATUS_RESYNC; return buf; } buf->status = LEPTON_STATUS_TRANSFERRING; return buf; }
void Flash_WaitBusy(void) { uint8_t cmd, data; s_FLASH__SELECT(); cmd = s_FLASH__CMD_RD_STATUS_REG1; HAL_SPI_Transmit(&g_Flash_SpiHandle, &cmd, 1, 1000); HAL_SPI_Receive(&g_Flash_SpiHandle, &data, 1, 1000); while(data & s_FLASH__STATUS_REG1_BUSY) { HAL_SPI_Receive(&g_Flash_SpiHandle, &data, 1, 1000); } s_FLASH__DESELECT(); }
/** * @brief This function handles EXTI line1 interrupt. */ void EXTI1_IRQHandler(void) { /* USER CODE BEGIN EXTI1_IRQn 0 */ // HAL_SPI_Receive_DMA(&hspi1, uint8_t * pData, AFE_BYTES_PER_CONV); uint8_t dummy[3] = {0,0,0}, data_ch1[3] = {0,0,0}, data_ch2[3] = {0,0,0}; int32_t data_tmp_ch1, data_tmp_ch2; static int32_t data_log_ch1[1000], data_log_ch2[1000]; static int32_t *ptr_1 = data_log_ch1; static int32_t *ptr_2 = data_log_ch2; //Read 3 ADS1291 status bytes HAL_GPIO_WritePin(GPIOA,AFE_CS_Pin,GPIO_PIN_RESET); //Enable CS HAL_SPI_Receive(&hspi1, dummy, 3, 100); HAL_SPI_Receive(&hspi1, data_ch1, 3, 100); HAL_SPI_Receive(&hspi1, data_ch2, 3, 100); HAL_GPIO_WritePin(GPIOA,AFE_CS_Pin,GPIO_PIN_SET); //Disable CS //Store signal data into ecg signal buffer data_tmp_ch1 = (((int32_t) ((int8_t) data_ch1[0])) << 16) | (((int32_t) data_ch1[1]) << 8) | ((int32_t) data_ch1[2]); data_tmp_ch2 = (((int32_t) ((int8_t) data_ch2[0])) << 16) | (((int32_t) data_ch2[1]) << 8) | ((int32_t) data_ch2[2]); *(ptr_1++) = data_tmp_ch1; *(ptr_2++) = data_tmp_ch2; if (ptr_1 == &(data_log_ch1[999])) { ptr_1 = data_log_ch1; } if (ptr_2 == &(data_log_ch2[999])) { ptr_2 = data_log_ch2; } /* USER CODE END EXTI1_IRQn 0 */ HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1); /* USER CODE BEGIN EXTI1_IRQn 1 */ /* USER CODE END EXTI1_IRQn 1 */ }
/** * @brief This function handles SPI1 global interrupt. */ void SPI1_IRQHandler(void) { __HAL_SPI_DISABLE_IT(&hspi1, SPI_IT_RXNE); HAL_SPI_Receive(&hspi1, &SPI_rx_buf[0], 3, 100); __HAL_SPI_ENABLE_IT(&hspi1, SPI_IT_RXNE); }
void SpiOpen(gcSpiHandleRx pfRxHandler) { DEBUG_printf("SpiOpen\n"); /* initialize SPI state */ sSpiInformation.ulSpiState = eSPI_STATE_POWERUP; sSpiInformation.SPIRxHandler = pfRxHandler; sSpiInformation.usTxPacketLength = 0; sSpiInformation.pTxPacket = NULL; sSpiInformation.pRxPacket = (unsigned char *)spi_buffer; sSpiInformation.usRxPacketLength = 0; spi_buffer[CC3000_RX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; wlan_tx_buffer[CC3000_TX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; /* SPI configuration */ SPI_HANDLE->Init.Mode = SPI_MODE_MASTER; SPI_HANDLE->Init.Direction = SPI_DIRECTION_2LINES; SPI_HANDLE->Init.DataSize = SPI_DATASIZE_8BIT; SPI_HANDLE->Init.CLKPolarity = SPI_POLARITY_LOW; SPI_HANDLE->Init.CLKPhase = SPI_PHASE_2EDGE; SPI_HANDLE->Init.NSS = SPI_NSS_SOFT; SPI_HANDLE->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; SPI_HANDLE->Init.FirstBit = SPI_FIRSTBIT_MSB; SPI_HANDLE->Init.TIMode = SPI_TIMODE_DISABLED; SPI_HANDLE->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; SPI_HANDLE->Init.CRCPolynomial = 7; spi_init(SPI_HANDLE); // configure wlan CS and EN pins GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Alternate = 0; GPIO_InitStructure.Pin = PIN_CS->pin_mask; HAL_GPIO_Init(PIN_CS->gpio, &GPIO_InitStructure); GPIO_InitStructure.Pin = PIN_EN->pin_mask; HAL_GPIO_Init(PIN_EN->gpio, &GPIO_InitStructure); HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_SET); HAL_GPIO_WritePin(PIN_EN->gpio, PIN_EN->pin_mask, GPIO_PIN_RESET); /* do a dummy read, this ensures SCLK is low before actual communications start, it might be required */ CS_LOW(); uint8_t buf[1]; HAL_SPI_Receive(SPI_HANDLE, buf, sizeof(buf), SPI_TIMEOUT); CS_HIGH(); // register EXTI extint_register((mp_obj_t)PIN_IRQ, GPIO_MODE_IT_FALLING, GPIO_PULLUP, (mp_obj_t)&irq_callback_obj, true, NULL); extint_enable(PIN_IRQ->pin); DEBUG_printf("SpiOpen finished; IRQ.pin=%d IRQ_LINE=%d\n", PIN_IRQ->pin, PIN_IRQ->pin); }
uint8_t SPI_Read(uint8_t address) { uint8_t data, tmp; /* Send address with read command */ tmp = address | 0x80; HAL_SPI_Transmit(&hspi5, &tmp, 1, 100); /* Read data */ HAL_SPI_Receive(&hspi5, &data, 1, 100); /* Return data */ return data; }
/** * @brief Read MPU60x0 register * @param addr: register address * @retval single register data */ u8 INVMPU_ReadBytes(u8 addr, u16 len, u8* data) { MPU_SPISelect(); addr |= 0x80; if(HAL_SPI_Transmit(MPU_SPI, &addr, 1, 5) == HAL_OK) { HAL_SPI_Receive(MPU_SPI, data, len, 5); } MPU_SPIDeselect(); return 0; }
//SPI¶ÁÈ¡Ö¸¶¨¼Ä´æÆ÷ //reg:Ö¸¶¨¼Ä´æÆ÷µÄµØÖ· uint8_t MPU9255_Read_Reg(uint8_t reg) { uint8_t reg_val; SPI_MPU9255_CS_L; reg = reg|0x80; HAL_SPI_Transmit(&hspi1, ®, 1, 0xFFFF); //·¢ËͶÁÃüÁî+¼Ä´æÆ÷ºÅ HAL_SPI_Receive(&hspi1, ®_val, 1, 0xFFFF); //¶ÁÈ¡¼Ä´æÆ÷Öµ SPI_MPU9255_CS_H; //½ûÖ¹SPI´«Êä Delay(0xFFF); return(reg_val); }
Spi::Status Spi::receive(uint8_t* data, uint16_t n, ReceiveCallback cb, void* args) { if (!data) return ILLEGAL; if (n < 1) return OK; // nothing to do if (cb) { // Use async spi transfer _rxCallback = cb; _rxArgs = args; return (Spi::Status) HAL_SPI_Receive_IT(&_spi, data, n); } else { // Use blocking spi transfer return (Spi::Status) HAL_SPI_Receive(&_spi, data, n, DEFAULT_TIMEOUT); } }
/** * @brief Reads 4 bytes from device. * @param ReadSize: Number of bytes to read (max 4 bytes) * @retval Value read on the SPI */ static uint32_t SPIx_Read(uint8_t ReadSize) { HAL_StatusTypeDef status = HAL_OK; uint32_t readvalue; status = HAL_SPI_Receive(&SpiHandle, (uint8_t*) &readvalue, ReadSize, SpixTimeout); /* Check the communication status */ if(status != HAL_OK) { /* Re-Initialize the BUS */ SPIx_Error(); } return readvalue; }
int DevSPI::recv( uint8_t *dd, int nd ) { if( dd == nullptr || nd < 1 ) { return 0; } nss_pre_cond(); last_rc = HAL_SPI_Receive( spi, (uint8_t*)(dd), nd, maxWait ); nss_post_cond(); if ( last_rc != HAL_OK ) { last_err = getErr(); return 0; } return nd; }
//SPI¶ÁMPU9255Êý¾Ý uint8_t MPU9255_ReadValue(uint8_t status) { uint8_t data=ACCEL_XOUT_H|0x80; SPI_MPU9255_CS_L; //ʹÄÜSPI´«Êä HAL_SPI_Transmit(&hspi1, &data, 1, 0xFFFF); HAL_SPI_Receive(&hspi1, MPU9255_DataBuffer, 14, 0xFFFF); //¹²¶ÁÈ¡14×Ö½ÚÊý¾Ý //init status if(status == 0) { MPU9255_ACC_OFFSET.X += ((int16_t)(MPU9255_DataBuffer[0]<<8)) | (MPU9255_DataBuffer)[1]; MPU9255_ACC_OFFSET.Y += ((int16_t)(MPU9255_DataBuffer[2]<<8)) | (MPU9255_DataBuffer)[3]; MPU9255_ACC_OFFSET.Z += ((int16_t)(MPU9255_DataBuffer[4]<<8)) | (MPU9255_DataBuffer)[5]; MPU9255_GYRO_OFFSET.X += ((int16_t)(MPU9255_DataBuffer[8]<<8)) | (MPU9255_DataBuffer)[9]; MPU9255_GYRO_OFFSET.Y += ((int16_t)(MPU9255_DataBuffer[10]<<8)) | (MPU9255_DataBuffer)[11]; MPU9255_GYRO_OFFSET.Z += ((int16_t)(MPU9255_DataBuffer[12]<<8)) | (MPU9255_DataBuffer)[13]; } //measure status else if(status == 1) { // //¼ÓËÙ¶È¼Æ MPU9255_ACC_LAST.X = (((int16_t)(MPU9255_DataBuffer[0]<<8)) | (MPU9255_DataBuffer[1])) - (int16_t)MPU9255_ACC_OFFSET.X; MPU9255_ACC_LAST.Y = (((int16_t)(MPU9255_DataBuffer[2]<<8)) | (MPU9255_DataBuffer[3])) - (int16_t)MPU9255_ACC_OFFSET.Y; MPU9255_ACC_LAST.Z = (((int16_t)(MPU9255_DataBuffer[4]<<8)) | (MPU9255_DataBuffer[5])) - (int16_t)MPU9255_ACC_OFFSET.Z; //ÎÂ¶È MPU9255_TEMP_LAST = ((int16_t)(MPU9255_DataBuffer[6]<<8)) | (MPU9255_DataBuffer)[7]; //ÍÓÂÝÒÇ MPU9255_GYRO_LAST.X = (((int16_t)(MPU9255_DataBuffer[8]<<8)) | (MPU9255_DataBuffer[9])) - MPU9255_GYRO_OFFSET.X; MPU9255_GYRO_LAST.Y = (((int16_t)(MPU9255_DataBuffer[10]<<8)) | (MPU9255_DataBuffer[11])) - MPU9255_GYRO_OFFSET.Y; MPU9255_GYRO_LAST.Z = (((int16_t)(MPU9255_DataBuffer[12]<<8)) | (MPU9255_DataBuffer[13])) - MPU9255_GYRO_OFFSET.Z; MPU9255_DataBuffer[0] = MPU9255_ACC_LAST.X >> 8; MPU9255_DataBuffer[1] = MPU9255_ACC_LAST.X; MPU9255_DataBuffer[2] = MPU9255_ACC_LAST.Y >> 8; MPU9255_DataBuffer[3] = MPU9255_ACC_LAST.Y; MPU9255_DataBuffer[4] = MPU9255_ACC_LAST.Z >> 8; MPU9255_DataBuffer[5] = MPU9255_ACC_LAST.Z; MPU9255_DataBuffer[8] = MPU9255_GYRO_LAST.X >> 8; MPU9255_DataBuffer[9] = MPU9255_GYRO_LAST.X; MPU9255_DataBuffer[10] = MPU9255_GYRO_LAST.Y >> 8; MPU9255_DataBuffer[11] = MPU9255_GYRO_LAST.Y; MPU9255_DataBuffer[12] = MPU9255_GYRO_LAST.Z >> 8; MPU9255_DataBuffer[13] = MPU9255_GYRO_LAST.Z; }
// send 1 bytes more ? int DevSPI::duplex( const uint8_t *ds, uint8_t *dd, int nd ) { if( ds == nullptr || dd == nullptr || nd == 0 ) { return 0; } nss_pre_cond(); last_rc = HAL_SPI_TransmitReceive( spi, (uint8_t*)ds, dd, nd, maxWait ); if( last_rc == HAL_OK ) { last_rc = HAL_SPI_Receive( spi, (uint8_t*)(dd), nd, maxWait ); } nss_post_cond(); if ( last_rc != HAL_OK ) { last_err = getErr(); return 0; } return nd; }
/* StartDefaultTask function */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN 5 */ /* Infinite loop */ for(;;) { // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // osDelay(1000); // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, servo1); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, servo2); HAL_SPI_Receive(&hspi1, &(message.bytes.byte1), sizeof(uint32_t), 1000); if(message.all != 0){ if((message.bytes.byte1 != 0) && (message.bytes.byte1 != 255) && (message.bytes.byte1 == message.bytes.byte2)){ servo1 = (uint16_t) message.bytes.byte1 * 20; message.bytes.byte1 = 0; message.bytes.byte2 = 0; } if((message.bytes.byte3 != 0) && (message.bytes.byte3 != 255) && (message.bytes.byte3 == message.bytes.byte4)){ servo2 = (uint16_t) message.bytes.byte3 * 20; message.bytes.byte3 = 0; message.bytes.byte4 = 0; } } // if(message.tasks.servo1 != 0){ // servo1 = message.tasks.servo1; // } // if(message.tasks.servo2 != 0){ // servo2 = message.tasks.servo2; // } //osDelay(1); } /* USER CODE END 5 */ }
void Flash_Read(uint32_t Addr, uint8_t *Data, uint16_t Size) { uint8_t cmd[4]; uint8_t i; //Check if Erase/Program in progress Flash_WaitBusy(); s_FLASH__SELECT(); // Prepare command cmd[0] = s_FLASH__CMD_RD_DATA; cmd[1] = (uint8_t)((Addr>>16) & 0xFF); cmd[2] = (uint8_t)((Addr>>8) & 0xFF); cmd[3] = (uint8_t)(Addr & 0xFF); // Send 'Read Data' instruction and address HAL_SPI_Transmit(&g_Flash_SpiHandle, cmd, 4, 1000); // Read Data HAL_SPI_Receive(&g_Flash_SpiHandle, Data, Size, 1000); s_FLASH__DESELECT(); }
int spi_master_transfer(hacs_spi_t bus, uint8_t *wbuf, size_t wsize, uint8_t *rbuf, size_t rsize) { HAL_SPI_Transmit(&spi_handles[bus], wbuf, wsize, SPI_OP_TIMEOUT_MS); HAL_SPI_Receive(&spi_handles[bus], rbuf, rsize, SPI_OP_TIMEOUT_MS); return 0; }
int spi_master_read(hacs_spi_t bus, uint8_t *rbuf, size_t rsize) { return HAL_SPI_Receive(&spi_handles[bus], rbuf, rsize, SPI_OP_TIMEOUT_MS); }
void AD7190_SPI_Read(uint8_t* data,uint8_t bytesNumber) { HAL_SPI_Receive (&hspi3,data,bytesNumber,1); }
uint8_t ReadAtt(void) { uint8_t BufferRX[1]; HAL_SPI_Receive(&hspi2, (uint8_t*)BufferRX, 1, 10); return BufferRX[0] >> 2; }
static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message) { HAL_StatusTypeDef state; rt_size_t message_length, already_send_length; rt_uint16_t send_length; rt_uint8_t *recv_buf; const rt_uint8_t *send_buf; RT_ASSERT(device != RT_NULL); RT_ASSERT(device->bus != RT_NULL); RT_ASSERT(device->bus->parent.user_data != RT_NULL); RT_ASSERT(message != RT_NULL); struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus); SPI_HandleTypeDef *spi_handle = &spi_drv->handle; struct stm32_hw_spi_cs *cs = device->parent.user_data; if (message->cs_take) { HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_RESET); } LOG_D("%s transfer prepare and start", spi_drv->config->bus_name); LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d", spi_drv->config->bus_name, (uint32_t)message->send_buf, (uint32_t)message->recv_buf, message->length); message_length = message->length; recv_buf = message->recv_buf; send_buf = message->send_buf; while (message_length) { /* the HAL library use uint16 to save the data length */ if (message_length > 65535) { send_length = 65535; message_length = message_length - 65535; } else { send_length = message_length; message_length = 0; } /* calculate the start address */ already_send_length = message->length - send_length - message_length; send_buf = (rt_uint8_t *)message->send_buf + already_send_length; recv_buf = (rt_uint8_t *)message->recv_buf + already_send_length; /* start once data exchange in DMA mode */ if (message->send_buf && message->recv_buf) { if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)) { state = HAL_SPI_TransmitReceive_DMA(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length); } else { state = HAL_SPI_TransmitReceive(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length, 1000); } } else if (message->send_buf) { if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) { state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length); } else { state = HAL_SPI_Transmit(spi_handle, (uint8_t *)send_buf, send_length, 1000); } } else { memset((uint8_t *)recv_buf, 0xff, send_length); if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG) { state = HAL_SPI_Receive_DMA(spi_handle, (uint8_t *)recv_buf, send_length); } else { state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, 1000); } } if (state != HAL_OK) { LOG_I("spi transfer error : %d", state); message->length = 0; spi_handle->State = HAL_SPI_STATE_READY; } else { LOG_D("%s transfer done", spi_drv->config->bus_name); } /* For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY); } if (message->cs_release) { HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET); } return message->length; }
int main(void) { /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_ADC_Init(); MX_SPI1_Init(); MX_USB_DEVICE_Init(); MX_TIM14_Init(); HAL_FLASH_Unlock(); EE_Init(); EE_ReadVariable(VirtAddVarTab[0], &x_low_th); EE_ReadVariable(VirtAddVarTab[1], &x_high_th); EE_ReadVariable(VirtAddVarTab[2], &y_low_th); EE_ReadVariable(VirtAddVarTab[3], &y_high_th); HAL_FLASH_Lock(); HAL_ADC_Start_DMA(&hadc, (uint32_t*)axis, 5); while (1) { HAL_GPIO_WritePin(SPI1_nCS_GPIO_Port, SPI1_nCS_Pin, GPIO_PIN_SET); HAL_TIM_Base_Start_IT(&htim14); while (!u100ticks) /* do nothing for 100 us */; HAL_TIM_Base_Stop_IT(&htim14); u100ticks = 0; HAL_StatusTypeDef status = HAL_SPI_Receive(&hspi1, rx_buffer, sizeof(rx_buffer), 3000); switch(status) { case HAL_OK: report.buttons[0] = 0x00; // report.buttons[1] = 0x00; report.buttons[2] = 0x00; if ((rx_buffer[0] & 0xff) != 0xff) // if all bits of rx_buffer[0] is 1 assume shifter is disconnected { if (rx_buffer[0] & 4) report.buttons[0] |= 1; else report.buttons[0] &= ~1; if (rx_buffer[0] & 1) report.buttons[0] |= (1 << 1); else report.buttons[0] &= ~(1 << 1); if (rx_buffer[0] & 2) report.buttons[0] |= (1 << 2); else report.buttons[0] &= ~(1 << 2); if (rx_buffer[0] & 8) report.buttons[0] |= (1 << 3); else report.buttons[0] &= ~(1 << 3); if (rx_buffer[1] & 1) report.buttons[2] |= 1; else report.buttons[2] &= ~1; if (rx_buffer[1] & 2) report.buttons[2] |= (1 << 1); else report.buttons[2] &= ~(1 << 1); if (rx_buffer[1] & 4) report.buttons[2] |= (1 << 2); else report.buttons[2] &= ~(1 << 2); if (rx_buffer[1] & 8) report.buttons[2] |= (1 << 3); else report.buttons[2] &= ~(1 << 3); if (rx_buffer[1] & 32) report.buttons[0] |= (1 << 4); else report.buttons[0] &= ~(1 << 4); if (rx_buffer[1] & 128) report.buttons[0] |= (1 << 5); else report.buttons[0] &= ~(1 << 5); if (rx_buffer[1] & 64) report.buttons[0] |= (1 << 6); else report.buttons[0] &= ~(1 << 6); if (rx_buffer[1] & 16) report.buttons[0] |= (1 << 7); else report.buttons[0] &= ~(1 << 7); } break; case HAL_TIMEOUT: case HAL_BUSY: case HAL_ERROR: Error_Handler(); default: report.buttons[0] = 0xff; break; } HAL_GPIO_WritePin(SPI1_nCS_GPIO_Port, SPI1_nCS_Pin, GPIO_PIN_RESET); report.id = 0x01; x_axis = ((X_AXIS << 2) + x_axis * 96) / 100; y_axis = ((Y_AXIS << 2) + y_axis * 96) / 100; if (rx_buffer[0] & 16) { if (y_axis < y_low_th) // stick towards player { report.buttons[1] = 128; report.buttons[2] &= ~(1 << 4); } else if (y_axis > y_high_th) // stick opposite to player { report.buttons[1] = 0; // neutral report.buttons[2] |= (1 << 4); } else { report.buttons[1] = 0; // neutral report.buttons[2] &= ~(1 << 4); } } else { report.buttons[1] &= ~(1 << 7); report.buttons[2] &= ~(1 << 4); if (y_axis < y_low_th) // stick towards player { if (x_axis < x_low_th) { if (!report.buttons[1]) report.buttons[1] = 2; // 2nd gear } else if (!report.buttons[1]) { report.buttons[1] = (x_axis > x_high_th) ? ((rx_buffer[0] & 64) ? 64 : 32) : 8; } } else { if (y_axis > y_high_th) // stick opposite to player { if (x_axis < x_low_th) { if (!report.buttons[1]) report.buttons[1] = 1; // 1st gear } else if (!report.buttons[1]) { report.buttons[1] = (x_axis > x_high_th) ? 16 : 4; } } else { report.buttons[1] = 0; // neutral } } } report.axis[0] = x_axis; report.axis[1] = y_axis; if (report2send == 2) { HAL_FLASH_Unlock(); EE_WriteVariable(VirtAddVarTab[0], x_low_th); EE_WriteVariable(VirtAddVarTab[1], x_high_th); EE_WriteVariable(VirtAddVarTab[2], y_low_th); EE_WriteVariable(VirtAddVarTab[3], y_high_th); HAL_FLASH_Lock(); report2send = 0; } if (hUsbDeviceFS.pClassData && ((USBD_HID_HandleTypeDef *)hUsbDeviceFS.pClassData)->state == HID_IDLE) { USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&report, sizeof(report)); } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { uint16_t addrcmd = 0; uint16_t comlength = 0; uint8_t pAddrcmd[CMD_LENGTH] = {0x00}; uint16_t ackbyte = 0x0000; /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure LED5 and LED6 */ BSP_LED_Init(LED5); BSP_LED_Init(LED6); /*##-1- Configure the SPI peripheral #######################################*/ /* Set the SPI parameters */ SpiHandle.Instance = SPIx; SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; SpiHandle.Init.CLKPhase = SPI_PHASE_2EDGE; SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; SpiHandle.Init.CRCPolynomial = 7; SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; SpiHandle.Init.NSS = SPI_NSS_SOFT; SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; SpiHandle.Init.Mode = SPI_MODE_SLAVE; if(HAL_SPI_Init(&SpiHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* enter while loop too keep treating new request from Master */ while(1) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Receive command from Master */ if(HAL_SPI_Receive(&SpiHandle, pAddrcmd, CMD_LENGTH, SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Compute command and required data length */ addrcmd = (uint16_t) ((pAddrcmd[0] << 8) | pAddrcmd[1]); comlength = (uint16_t) ((pAddrcmd[2] << 8) | pAddrcmd[3]); /* Check if received command correct */ if(((addrcmd == ADDRCMD_MASTER_READ) || (addrcmd == ADDRCMD_MASTER_WRITE)) && (comlength > 0)) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send acknowledge to Master */ ackbyte = SPI_ACK_BYTES; if(HAL_SPI_Transmit(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Check if Master requiring data read or write */ if(addrcmd == ADDRCMD_MASTER_READ) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send data to Master */ if(HAL_SPI_Transmit(&SpiHandle, aTxSlaveBuffer, DATA_LENGTH, SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Synchronization between Master and Slave */ Slave_Synchro(); /* Receive acknowledgement from Master */ ackbyte = 0; if(HAL_SPI_Receive(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Check acknowledgement */ if(ackbyte != SPI_ACK_BYTES) { Error_Handler(); } } else if(addrcmd == ADDRCMD_MASTER_WRITE) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Receive data from Master */ if(HAL_SPI_Receive(&SpiHandle, aRxBuffer, DATA_LENGTH, SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send acknowledgement to Master */ ackbyte = SPI_ACK_BYTES; if(HAL_SPI_Transmit(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* In case, Master has sent data, compare received buffer with one expected */ if(Buffercmp((uint8_t*)aTxMasterBuffer, (uint8_t*)aRxBuffer, DATA_LENGTH)) { /* Transfer error in transmission process */ Error_Handler(); } else { /* Toggle LED6 on: Reception is correct */ BSP_LED_Toggle(LED6); } } } else { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send acknowledgement to Master */ ackbyte = SPI_NACK_BYTES; if(HAL_SPI_Transmit(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } Error_Handler(); } /* Flush Rx buffer for next transmission */ Flush_Buffer(aRxBuffer, DATA_LENGTH); } }
/** * @brief * Read inf by SPI3 * @{ */ void spi3_recive(uint8_t *pData_rx , uint16_t Size){ HAL_SPI_Receive(&spi3_init_handle , pData_rx , Size , 100 ); }
STATIC void wiz_spi_read(uint8_t *buf, uint32_t len) { HAL_StatusTypeDef status = HAL_SPI_Receive(wiznet5k_obj.spi->spi, buf, len, 5000); (void)status; }