Exemplo n.º 1
0
void set_avr_mapper(uint8_t val) {
	SPI_SS_HIGH();
	FPGA_SS_LOW();
	spiTransferByte(0x30 | (val & 0x0f));
	FPGA_SS_HIGH();
	SPI_SS_LOW();
}
Exemplo n.º 2
0
void set_avr_bank(uint8_t val) {
	SPI_SS_HIGH();
	FPGA_SS_LOW();
	spiTransferByte(0x00); // SET ADDRESS
	spiTransferByte(val * 0x20); // select chip
	spiTransferByte(0x00); // select chip
	spiTransferByte(0x00); // select chip
	FPGA_SS_HIGH();
	SPI_SS_LOW();
}
Exemplo n.º 3
0
byte mmc::readSectors(byte *buffer, uint32_t sector, byte count) {
  byte sec,res,tmp,errorcount;
  uint16_t crc,recvcrc;

  for (sec=0;sec<count;sec++) {
    errorcount = 0;
    while (errorcount < CONFIG_SD_AUTO_RETRIES) {
      res = sendCommand(READ_SINGLE_BLOCK, (sector+sec) << 9, 0);

      if (res != 0) {
        SPI_SS_HIGH();
        disk_state = DISK_ERROR;
        return RES_ERROR;
      }

      // Wait for data token
      if (!sdResponse(0xFE)) {
        SPI_SS_HIGH();
        disk_state = DISK_ERROR;
        return RES_ERROR;
      }

      uint16_t i;

      // Get data
      crc = 0;
      for (i=0; i<512; i++) {
        tmp = spiTransferByte(0xff);
        *(buffer++) = tmp;
      }

      // Check CRC
      recvcrc = (spiTransferByte(0xFF) << 8) + spiTransferByte(0xFF);

      break;
    }
    deselectCard();

    if (errorcount >= CONFIG_SD_AUTO_RETRIES) return RES_ERROR;
  }

  return RES_OK;
}
Exemplo n.º 4
0
void sonar::get_data_spi(byte* left, byte* right)
{
    SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);  // SPI ON

    SPI_SS_LOW();   // CS to low
    IRQ_DELAY;
    *left = spi_read();
    IRQ_DELAY;
    *right = spi_read();
    SPI_SS_HIGH();  // CS to high

    SPCR = 0;  //SPI OFF
}
Exemplo n.º 5
0
/*******************************************************************************
* GPIO_Config: GPIO port initializtion
*******************************************************************************/
void GPIO_Config2(void)
{
  GPIOB_CRH = 0x88883338;  /* GPIOB Bit8-15 状态设置    0x83833333
                |||||||+----- Nib8  Key_n 上拉输入
                ||||||+------ Nib9  Bx0.1 高速输出
                |||||+------- Nib10 C_nSS 高速输出  
                ||||+-------- Nib11 C_SCK 高速输出  
                |||+--------- Nib12 C_SI  高速输出  
                ||+---------- Nib13 C_SO  上拉输入  // 配置成功后改为“体验版”DTr   输入
                |+----------- Nib14 C_RST 高速输出  // 配置成功后改为“体验版”CTr   输入
                +------------ Nib15 Cdone 上拉输入*/ 
  SPI_SS_HIGH();                                    // PWRon = 1
}
Exemplo n.º 6
0
byte mmc::writeSectors(const byte *buffer, uint32_t sector, byte count) {
  byte res,sec,errorcount,status;
  uint16_t crc;

  for (sec=0;sec<count;sec++) {
    errorcount = 0;
    while (errorcount < CONFIG_SD_AUTO_RETRIES) {
      res = sendCommand(WRITE_BLOCK, (sector+sec)<<9, 0);

      if (res != 0) {
        SPI_SS_HIGH();
        disk_state = DISK_ERROR;
        return RES_ERROR;
      }

      // Send data token
      spiTransferByte(0xFE);

      uint16_t i;
      const byte *oldbuffer = buffer;

      // Send data
      crc = 0;
      for (i=0; i<512; i++) {
        spiTransferByte(*(buffer++));
      }

      // Send CRC
      spiTransferByte(crc >> 8);
      spiTransferByte(crc & 0xff);

      // Get and check status feedback
      status = spiTransferByte(0xFF);

      // Retry if neccessary
      if ((status & 0x0F) != 0x05) {
        //	uart_putc('X');
        deselectCard();
        errorcount++;
        buffer = oldbuffer;
        continue;
      }

      // Wait for write finish
      if (!sdWaitWriteFinish()) {
        SPI_SS_HIGH();
        disk_state = DISK_ERROR;
        return RES_ERROR;
      }
      break;
    }
    deselectCard();

    if (errorcount >= CONFIG_SD_AUTO_RETRIES) {
      if (!(status & STATUS_CRC_ERROR))
        disk_state = DISK_ERROR;
      return RES_ERROR;
    }
  }

  return RES_OK;
}
Exemplo n.º 7
0
byte mmc::readSectors(byte *buffer, uint32_t sector, byte count, uint16_t offset1, uint16_t offset2) {
  byte sec,res,tmp,errorcount;
  uint16_t crc,recvcrc;

  // offset1 refers to the place where to start writting on the buffer
  // offset2 keeps track of how much is the offset from where to read
  // offset1 + offset2 = BYTESPERSECTOR in the first call
  // after that, offset1 will become 0 and offset2 will keep size
  // until the end, when we will have to consider adding EOFs
  buffer += offset1;

  for (sec=0;sec<count;sec++) {
    errorcount = 0;
    while (errorcount < CONFIG_SD_AUTO_RETRIES) {
      res = sendCommand(READ_SINGLE_BLOCK, (sector+sec) << 9, 0);

      if (res != 0) {
        SPI_SS_HIGH();
        disk_state = DISK_ERROR;
        return RES_ERROR;
      }

      // Wait for data token
      if (!sdResponse(0xFE)) {
        SPI_SS_HIGH();
        disk_state = DISK_ERROR;
        return RES_ERROR;
      }

      uint16_t i;

      // Get data for the first half of the package
      crc = 0;
      if (offset1 + offset2 == BYTESPERSECTOR) 
      {
        // case when we come in the first time
        // the first half of the buffer is filled up
        // with the data from the other file
        for (i=0; i<offset2; i++) {
          tmp = spiTransferByte(0xff);
          *(buffer++) = tmp;
        }
        // finish reading the block to avoid errors
        for (i=offset2; i<512; i++) {
          tmp = spiTransferByte(0xff);
        }
      }
      else {
        // case when we are starting with an empty buffer, but with offset
        // the first offset2 bytes are there from the last time
        for (i=0; i<offset2; i++) {
          tmp = spiTransferByte(0xff);
        }
        // the second 512-offset2 bytes gotta be stored in the array
        for (i=offset2; i<512; i++) {
          tmp = spiTransferByte(0xff);
          *(buffer++) = tmp;
        }
      }

      // Check CRC
      recvcrc = (spiTransferByte(0xFF) << 8) + spiTransferByte(0xFF);

      break;
    }
    deselectCard();

    if (errorcount >= CONFIG_SD_AUTO_RETRIES) return RES_ERROR;
  }

  return RES_OK;
}
Exemplo n.º 8
0
byte mmc::initialize() {
  byte  i;
  uint16_t counter;
  uint32_t answer;

  disk_state = DISK_ERROR;

  // setup SPI I/O pins
  PORTB |=  _BV(SCK) | _BV(SS) | _BV(MISO); // set SCK+SS hi (no chip select), pullup on MISO
  DDRB  |=  _BV(SCK) | _BV(SS) | _BV(MOSI); // set SCK/MOSI/SS as output
  DDRB  &= ~_BV(MISO);                       // set MISO as input

  // setup SPI interface:
  //   interrupts disabled, SPI enabled, MSB first, master mode,
  //   leading edge rising, sample on leading edge, clock = f/4,
  SPCR = B01010011;

  // Enable SPI double speed mode -> clock = f/8
  //  SPSR = _BV(SPI2X);

  // clear status
  i = SPSR;


  // clear recieve buffer
  i = SPDR;


  SPI_SS_HIGH();

  // Send 80 clks
  for (i=0; i<10; i++) {
    spiTransferByte(0xFF);
  }

  // Reset card
  i = sendCommand(GO_IDLE_STATE, 0, 1);
  if (i != 1) {
    return STA_NOINIT | STA_NODISK;
  }

  counter = 0xffff;
  // According to the spec READ_OCR should work at this point
  // without retries. One of my Sandisk-cards thinks otherwise.
  do {
    // Send CMD58: READ_OCR
    i = sendCommand(READ_OCR, 0, 0);
    if (i > 1) {
      // kills my Sandisk 1G which requires the retries in the first place
      // deselectCard();
    }
  } 
  while (i > 1 && counter-- > 0);

  if (counter > 0) {
    answer = spiTransferLong(0);

    // See if the card likes our supply voltage
    if (!(answer & SD_SUPPLY_VOLTAGE)) {
      // The code isn't set up to completely ignore the card,
      // but at least report it as nonworking
      deselectCard();
      return STA_NOINIT | STA_NODISK;
    }
  }

  // Keep sending CMD1 (SEND_OP_COND) command until zero response
  counter = 0xffff;
  do {
    i = sendCommand(SEND_OP_COND, 1L<<30, 1);
    counter--;
  } 
  while (i != 0 && counter > 0);

  if (counter==0) {
    return STA_NOINIT | STA_NODISK;
  }

  // Send MMC CMD16(SET_BLOCKLEN) to 512 bytes
  i = sendCommand(SET_BLOCKLEN, 512, 1);
  if (i != 0) {
    return STA_NOINIT | STA_NODISK;
  }

  // Thats it!
  disk_state = DISK_OK;
  return RES_OK;
}
Exemplo n.º 9
0
static void deselectCard(void) {
  // Send 8 clock cycles
  SPI_SS_HIGH();
  spiTransferByte(0xff);
}
Exemplo n.º 10
0
/**
  * @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;
}
Exemplo n.º 11
0
/*******************************************************************************
 Set:  硬件控制设备设置
*******************************************************************************/
u32 Set(u8 Object, u32 Value)
{
  switch (Object){  
  case CH_A_OFFSET:  if(Value < 65536){
                       TIM5_ARR = 470; TIM5_CCR1 = 450 - Value; // Value = 0~200
                     } else {
                       TIM5_ARR = Value >>16; TIM5_CCR1 = Value & 0xFFFF;
                     } break;
  case CH_B_OFFSET:  if(Value < 65536){
                       TIM5_ARR = 470; TIM5_CCR2 = 450 - Value; // Value = 0~200
                     } else {
                       TIM5_ARR = Value >>16; TIM5_CCR2 = Value & 0xFFFF;
                     } break;
  case BACKLIGHT:    TIM8_CCR1 = Value;                         // Value = 0~100
                     break;
  case BEEP_VOLUME:  TIM8_CCR2 = 100 - Value/2;                 // Value = 0~50
                     break;
  case BETTERY_DT:   ADC3_CR2 |= (Value & 1)<<22;  // Value = 1/0   ADC3_CR2 |=0x00400000;
                     break;
  case ADC_MODE:     if(Value == SEPARATE) FIFO_MODE_LOW();  
                     else                  FIFO_MODE_HIGH();  break;
  case FIFO_CLR:     if(Value == W_PTR){FIFO_CLRW_HIGH();  FIFO_CLRW_LOW();}
                     if(Value == R_PTR){FIFO_CLRR_HIGH();  FIFO_CLRR_LOW();} break;
  case T_BASE_PSC:   TIM1_PSC  = Value;
                     break;
  case T_BASE_ARR:   if(Value==0)      {TIM1_CCER=0; RCC_CFGR=0x041D8402;} // MCO as SYSCLK  
                     else if(Value==1) {TIM1_CCER=0; RCC_CFGR=0x071D8402;} // MCO as SYSCLK/2 
                     else  {RCC_CFGR=0x001D8402;  TIM1_CCER=0x0003;        // MCO as OC1 
                            TIM1_ARR=Value;  TIM1_CCR1=(Value+1)/2;}
                     break;
  case CH_A_COUPLE:  if(Value == AC ) AC_1();  else DC_1();
                     break;
  case CH_B_COUPLE:  if(Value == AC ) AC_2();  else DC_2();
                     break;
  case CH_A_RANGE:
    switch (Value){  
    case _50MV:  Ax0_ON();  Ax1_ON();  Ax2_HIGH(); Ax5_HIGH();
                 break;
    case _100MV:  Ax0_ON();  Ax1_ON();  Ax2_LOW();  Ax5_HIGH();    
                 break;
    case _200MV: Ax0_ON();  Ax1_ON();  Ax2_HIGH(); Ax5_LOW();
                 break;
    case _500MV: Ax0_ON();  Ax1_ON();  Ax2_LOW();  Ax5_LOW(); 
                 break;
    case _1V: Ax0_OFF(); Ax1_OFF(); Ax2_HIGH(); Ax5_HIGH(); 
                 break;
    case _2V:    Ax0_OFF(); Ax1_OFF(); Ax2_LOW();  Ax5_HIGH();
                 break;
    case _5V:    Ax0_OFF(); Ax1_OFF(); Ax2_HIGH(); Ax5_LOW(); 
                 break;
    case _10V:    Ax0_OFF(); Ax1_OFF(); Ax2_LOW();  Ax5_LOW();  
                 break;
    case CH_B:   Ax0_ON(); Ax1_OFF();  Ax2_LOW();  Ax5_LOW();
                 break;
    }  break;
    
  case CH_B_RANGE:
    switch (Value){  
    case _50MV:  Bx0_ON();  Bx1_ON();  Bx2_HIGH(); Bx5_HIGH();
                 break;
    case _100MV:  Bx0_ON();  Bx1_ON();  Bx2_LOW();  Bx5_HIGH();
                 break;
    case _200MV: Bx0_ON();  Bx1_ON();  Bx2_HIGH();  Bx5_LOW();
                 break;
    case _500MV: Bx0_ON();  Bx1_ON();  Bx2_LOW();  Bx5_LOW();
                 break;
    case _1V: Bx0_OFF(); Bx1_OFF(); Bx2_HIGH(); Bx5_HIGH();
                 break;
    case _2V:    Bx0_OFF(); Bx1_OFF(); Bx2_LOW();  Bx5_HIGH();
                 break;
    case _5V:    Bx0_OFF(); Bx1_OFF(); Bx2_HIGH();  Bx5_LOW();
                 break;
    case _10V:    Bx0_OFF(); Bx1_OFF(); Bx2_LOW(); Bx5_LOW();
                 break;
    case CH_A:   Bx0_ON(); Bx1_OFF();  Bx2_LOW();  Bx5_LOW();
                 break;
    }  break;
    
  case ANALOG_ARR:  GPIOB_CRL = 0x34BBB438;  TIM4_CR1 = 0x0080;  // SQR_OUT = Disnable  
                    GPIOA_CRL   = 0x111011BB;  DAC_CR = 0x0001;  // DAC = Ensable 
                    TIM7_ARR = Value;  TIM7_CR1 = 0x0085;  break;// DAC_CLK = Enable
  case ANALOG_PTR:  DMA2_CMAR4  = Value;
                    break;
  case ANALOG_CNT:  DMA2_CNDTR4 = Value;       // Fout = (Cnt*(ARR+1)/72)KHz
                    break;
  case DIGTAL_PSC:  TIM4_PSC = Value; GPIOA_CRL  |= 0x40000;  // DAC_OUT = Disnable
                    TIM7_CR1 = 0x0084;  DAC_CR = 0;  break;   // DAC = Disnable
  case DIGTAL_ARR:  TIM4_ARR    = Value;
                    break;
  case DIGTAL_CCR:  GPIOB_CRL &= 0xF0FFFFFF;  GPIOB_CRL |= 0x0B000000; // PORT_SQR = Enable
                    TIM4_CCR1 = Value;  TIM4_CR1 = 0x0081; break;      // SQR_OUT = Enable 
  case KEY_IF_RST:  TIM3_SR = 0;               //Clear TIM3 interrupt flag
                    break;
  case STANDBY:     if(Value == 1) { STB_EN();}  else { STB_DN();}   
                    break;
                    
  case FPGA_RST:    GPIOB_CRH &= 0xF0FFFFFF;  GPIOB_CRH |= 0x01000000;        // 设PB14为输出状态
                    SPI_CRST_LOW(); Delayms(1);   // SPI_CRST_LOW 1mS
                    SPI_SS_HIGH(); Delayms(1);     // SPI_SS_HIGH  1mS
                    SPI_SS_LOW(); Delayms(1);      // SPI_SS_LOW   1mS
                    SPI_CRST_HIGH(); Delayms(2);   // SPI_CRST_HIGH 2mS
                    GPIOB_CRH &= 0xF0FFFFFF;  GPIOB_CRH |= 0x08000000; break; // 设PB14为输入状态 

  case TRIGG_MODE:  Set_Param(Object, Value);             
                    break; 
  case V_THRESHOLD: Set_Param(Object, Value);          
                    break;    
  case T_THRESHOLD: Set_Param(Object, Value & 0xFF);            
                    Set_Param(Object +1, Value >> 8);  break;
  case ADC_CTRL:    Set_Param(Object, Value);               
                    break;
  case A_POSITION:  Set_Param(Object, Value);              
                    break; 
  case B_POSITION:  Set_Param(Object, Value);              
                    break; 
  case REG_ADDR:    Set_Param(Object, Value);          
                    break;    
  }
  return 0;
}