/*!
 * @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;
}
Beispiel #2
1
void Flash_Program(uint32_t Addr, uint8_t *Data, uint16_t Size)
{
	uint8_t cmd[4];
	uint8_t i;

	//Check if Erase/Program in progress
	Flash_WaitBusy();

	// Enable write
	Flash_Command(s_FLASH__CMD_WR_ENABLE);

	s_FLASH__SELECT();

	// Prepare command
	cmd[0] = s_FLASH__CMD_PAGE_PROGRAM;
	cmd[1] = (uint8_t)((Addr>>16) & 0xFF);
	cmd[2] = (uint8_t)((Addr>>8) & 0xFF);
	cmd[3] = (uint8_t)(Addr & 0xFF);

	// Send 'Program Page' instruction and address
	HAL_SPI_Transmit(&g_Flash_SpiHandle, cmd, 4, 1000);

	// Send data
	HAL_SPI_Transmit(&g_Flash_SpiHandle, Data, Size, 1000);

	s_FLASH__DESELECT();
}
Beispiel #3
0
void vfd_write_display_flag(uint8_t addr, uint8_t flag) {
	addr += 0xC0;
	HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1, &addr, 1, 1000);
	HAL_SPI_Transmit(&hspi1, &flag, 1, 1000);
	HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET);
}
Beispiel #4
0
/**
* @brief  Write MPU60x0 register
* @param  addr: register address, data: data to write
* @retval None
*/
u8 INVMPU_WriteBytes(u8 addr, u16 len, const u8* data)
{
    MPU_SPISelect();
    if(HAL_SPI_Transmit(MPU_SPI, &addr, 1, 5) == HAL_OK)
    {
        HAL_SPI_Transmit(MPU_SPI, (u8*)data, len, 5);
    }
	MPU_SPIDeselect();
    return 0;
}
//SPIд¼Ä´æÆ÷
//reg:Ö¸¶¨µÄ¼Ä´æÆ÷µØÖ·
//value:дÈëµÄÖµ
uint8_t MPU9255_Write_Reg(uint8_t reg,uint8_t value)
{
	uint8_t status;
	SPI_MPU9255_CS_L;											  //ʹÄÜSPI´«Êä
	status = HAL_SPI_Transmit(&hspi1, &reg, 1, 0xFFFF);
	status = HAL_SPI_Transmit(&hspi1, &value, 1, 0xFFFF);
	SPI_MPU9255_CS_H;										  	//½ûÖ¹MPU9255
	Delay(0xFFF);
	return(status);													//·µ»Ø״ֵ̬
}
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;
}
Beispiel #7
0
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);
}
Beispiel #8
0
uint16_t DRV8301_readSpi(DRV8301_Handle handle, const DRV8301_RegName_e regName)
{

  // Actuate chipselect
  HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_RESET);
  osDelay(1);

  // Do blocking read
  uint16_t zerobuff = 0;
  uint16_t controlword = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Read, regName, 0);
  uint16_t recbuff = 0xbeef;
  HAL_SPI_Transmit(handle->spiHandle, (uint8_t*)(&controlword), 1, 1000);

  // Datasheet says you don't have to pulse the nCS between transfers, (16 clocks should commit the transfer)
  // but for some reason you actually need to pulse it.
  // Actuate chipselect
  HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_SET);
  osDelay(1);
  // Actuate chipselect
  HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_RESET);
  osDelay(1);

  HAL_SPI_TransmitReceive(handle->spiHandle, (uint8_t*)(&zerobuff), (uint8_t*)(&recbuff), 1, 1000);
  osDelay(1);

  // Actuate chipselect
  HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_SET);
  osDelay(1);

  assert(recbuff != 0xbeef);

  return(recbuff & DRV8301_DATA_MASK);
}  // end of DRV8301_readSpi() function
static sint8 spi_rw(uint8 *tx_buf, uint8 *rx_buf, uint16 u16Sz)
{
	sint8 result = M2M_SUCCESS;

	// Start SPI transfer
    // TODO: Use DMA

    __disable_irq();
    SPI_ASSERT_CS();
    if (tx_buf != 0) {
        if (HAL_SPI_Transmit(&SPI_HANDLE, tx_buf, u16Sz, SPI_TIMEOUT) != HAL_OK) {
            result = M2M_ERR_BUS_FAIL;
        }
	} else {
        tx_buf = rx_buf;
        memset(tx_buf, 0, u16Sz);
        if (HAL_SPI_TransmitReceive(&SPI_HANDLE, tx_buf, rx_buf, u16Sz, SPI_TIMEOUT) != HAL_OK) {
            result = M2M_ERR_BUS_FAIL;
        }

    }
	SPI_DEASSERT_CS();
    __enable_irq();

	return result;
}
Beispiel #10
0
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;
}
Beispiel #11
0
void MAX_Init(){
	// MX_SPI2_Init();
	// __HAL_SPI_ENABLE(&hspi2);

#if 0	
	dataOut[0] = (17<<3) | 2;
	dataOut[1] = 0x10;

	cs_low;
	HAL_SPI_Transmit(&hspi2, dataOut, 2, 1000);
	cs_high;

	Hwreg(rUSBIEN, bmOSCOKIE);
	Hrreg(rUSBIEN);
	Hrreg(18<<3);
#endif
	
	// MAX3421E: Full duplex mode, INTLEVEL=0, POSINT=1 for pos edge interrupt pin
	Hwreg(rPINCTL,(bmFDUPSPI|bmPOSINT)); 
	MAX_Reset();

	// MAX3421E EVB
	Hwreg(rIOPINS1,0x00);		// seven-segs off
	Hwreg(rIOPINS2,0x00);		// and Vbus OFF (in case something already plugged in)
	HAL_Delay(200);
	VBUS_ON;
}
Beispiel #12
0
static void writeReg(SPI_HandleTypeDef *const handle, const uint8_t &adr, const uint8_t val) {
//	HAL_I2C_Mem_Write(handle, 0x82, adr, 1, (uint8_t*)&val, 1, 0x0F);
	uint8_t buff[] = { adr, val };
	BSP_Gpio_SetPin(GPIO_MEMS_CS, false);
	HAL_SPI_Transmit(handle, buff, 2, 0x0F);
	BSP_Gpio_SetPin(GPIO_MEMS_CS, true);
}
Beispiel #13
0
void LCD_WR_DATA8(uint8_t da)   //写8位数据
{
	SPILCD_CS_RESET;  //LCD_CS=0
	SPILCD_RS_SET;				    	   
	HAL_SPI_Transmit(&hspi1, &da, 1 , 100);
	SPILCD_CS_SET;  //LCD_CS=1   			 
}					   
Beispiel #14
0
// 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);
}
HAL_StatusTypeDef HAL_SPI_TransmitNew(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout){
   HAL_StatusTypeDef returnvar;
   HAL_GPIO_WritePin(GPIOD,GPIO_PIN_8,GPIO_PIN_RESET);
	 returnvar = HAL_SPI_Transmit(hspi,pData,Size,Timeout);
	 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_8,GPIO_PIN_SET);
	 return returnvar;

}
Beispiel #16
0
void Flash_Command(uint8_t Cmd)
{
	s_FLASH__SELECT();

	HAL_SPI_Transmit(&g_Flash_SpiHandle, &Cmd, 1, 100);

	s_FLASH__DESELECT();
}
Beispiel #17
0
void glcd_spi_write(uint8_t c)
{

	GLCD_SELECT();
	//todo: add block write with DMA, use SPI_TIMEOUT_VALUE
	HAL_SPI_Transmit(&hspi1, &c, 1, 10);
	GLCD_SELECT();
}
Beispiel #18
0
//写LCD数据
//data:要写入的值
void LCD_WR_DATA(uint16_t data)
{
 	SPILCD_CS_RESET;  //LCD_CS=0
	SPILCD_RS_SET;	
	
	HAL_SPI_Transmit(&hspi1, (uint8_t*)&data, 2, 20);
	
	SPILCD_CS_SET;  //LCD_CS=1		
}
Beispiel #19
0
void comm_out(unsigned char j) //Command Output Serial Interface
{
 HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET);
 HAL_GPIO_WritePin(SPI1_A0_GPIO_Port, SPI1_A0_Pin, GPIO_PIN_RESET);

 HAL_SPI_Transmit(&hspi1, &j, 1, 200);

 HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_SET);
}
Beispiel #20
0
void data_out(unsigned char i) //Data Output Serial Interface
{
 HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET);
 HAL_GPIO_WritePin(SPI1_A0_GPIO_Port, SPI1_A0_Pin, GPIO_PIN_SET);

 HAL_SPI_Transmit(&hspi1, &i, 1, 200);

 HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_SET);
}
Beispiel #21
0
static HAL_StatusTypeDef st7789_write_fb(uint16_t *data, uint16_t size)
{
  if (hspi_lcd == NULL) {
    _Error_Handler(__FILE__, __LINE__);
    return HAL_ERROR;
  }

  return HAL_SPI_Transmit(hspi_lcd, (uint8_t *)data, size, HAL_MAX_DELAY);
}
Beispiel #22
0
void SetAtt(uint8_t data)
{
    uint8_t BufferTX[1];
    BufferTX[0] = data;
  
    HAL_SPI_Transmit(&hspi2, (uint8_t*)BufferTX, 1, 10);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);

}
Beispiel #23
0
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;
}
Beispiel #24
0
/**
* @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;
}
Beispiel #25
0
rc_t cc2500_xfer_tx(bool burst, uint8_t* data, uint8_t len) {
	/* CC2500 send data with burst over SPI if requested, default = true*/
	spi_tx_buffer[0] = (burst ? CC2500_WRITE_SINGLE : CC2500_WRITE_BURST);
	/*length field for UTRUSTA lightning protocol*/
//	spi_tx_buffer[1] = len;
	/* SOF for UTRUSTA Protocol */
	memcpy(&spi_tx_buffer[1], data, len);
	HAL_SPI_Transmit(&hspi1, &spi_tx_buffer[0], len + 1, 100);
	return RC_OK;

}
Beispiel #26
0
/**
 * Display graphics buffer on Sharp LCD
 * @param Buffer Pointer to buffer to be displayed
 * @return void No return
 */
void SharpLcd_DisplayBuffer(uint8_t *Buffer)
{
	// Chip select
	s_SHARPLCD__SCS_SET();

	// Send data
	HAL_SPI_Transmit(&g_SharpLcd_SpiHandle, Buffer, 168*20+2, 3000);

	// Chip deselect
	s_SHARPLCD__SCS_RESET();
}
//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, &reg, 1, 0xFFFF);	 	//·¢ËͶÁÃüÁî+¼Ä´æÆ÷ºÅ
 	HAL_SPI_Receive(&hspi1, &reg_val, 1, 0xFFFF);				//¶ÁÈ¡¼Ä´æÆ÷Öµ
	SPI_MPU9255_CS_H;																//½ûÖ¹SPI´«Êä
	Delay(0xFFF);
	return(reg_val);
}
Beispiel #28
0
int  DevSPI::send2( const uint8_t *ds1, int ns1, const uint8_t *ds2, int ns2 )
{
  if( ds1 == nullptr || ns1 < 1 || ds2 == nullptr || ns2 < 1 ) {
    return 0;
  }

  nss_pre_cond();
  last_rc = HAL_SPI_Transmit( spi, (uint8_t*)ds1, ns1, maxWait );
  if( last_rc == HAL_OK ) {
    last_rc = HAL_SPI_Transmit( spi, (uint8_t*)(ds2), ns2, maxWait );
  }
  nss_post_cond();

  if ( last_rc != HAL_OK ) {
    last_err = getErr();
    return 0;
  }

  return ns1 + ns2;
}
Beispiel #29
0
void Hwreg(uint8_t reg, uint8_t val){
	int delay = 50;
	dataOut[0] = reg+0x02;
	dataOut[1] = val;
	// MAX_CS_PORT->BSRR = (uint32_t)MAX_CS_PIN << 16;
	cs_low;
	HAL_SPI_Transmit(&hspi2, dataOut, 2, 1000);
	cs_high;
	// MAX_CS_PORT->BSRR = MAX_CS_PIN;

	while(delay--);
}
Beispiel #30
0
//写寄存器函数
//regval:寄存器值
void LCD_WR_REG(uint16_t regval)
{ 
	uint8_t temp;
	
	SPILCD_CS_RESET;  //LCD_CS=0
  SPILCD_RS_RESET;
	
	temp = regval&0x00FF;
	HAL_SPI_Transmit(&hspi1, &temp, 1 , 100);
	
	SPILCD_CS_SET;  //LCD_CS=1	   		 
}