//Setup hardware
TouchADS7843::TouchADS7843(void)
{
	//Enable clocks
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN); //SPI
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); //SPI
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); //PEN_INT
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); //AFIO (EXTI)
	
	//Setup GPIO
	gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BIT13);	//PEN_INT
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BIT6);	//MISO
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, BIT4); //CS
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, BIT5 | BIT7); //MOSI, SCK
	
	//Set up SPI as mater
	_cs(1);
	spi_enable_software_slave_management(SPI1);
	spi_set_nss_high(SPI1);
	spi_set_baudrate_prescaler(SPI1, 7);
	spi_set_clock_polarity_0(SPI1);
	spi_set_clock_phase_0(SPI1);
	spi_set_dff_8bit(SPI1);
	spi_set_master_mode(SPI1);
	spi_enable(SPI1);
	
	//Enable PEN_INT
	nvic_enable_irq(NVIC_EXTI15_10_IRQ);
	exti_select_source(EXTI13, GPIOC);
	exti_set_trigger(EXTI13, EXTI_TRIGGER_BOTH);
	exti_reset_request(EXTI13);
	exti_enable_request(EXTI13);
	
}
Beispiel #2
0
void spi_setup(void) {
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN);
  /* For spi signal pins */
  rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN);
  /* For spi mode select on the l3gd20 */
  rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN);

  /* Setup GPIOE3 pin for spi mode l3gd20 select. */
  gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3);
  /* Start with spi communication disabled */
  gpio_set(GPIOE, GPIO3);

  /* Setup GPIO pins for AF5 for SPI1 signals. */
  gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO5 | GPIO6 | GPIO7);
  gpio_set_af(GPIOA, GPIO_AF5, GPIO5 | GPIO6 | GPIO7);

  //spi initialization;
  spi_set_master_mode(SPI1);
  spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_64);
  spi_set_clock_polarity_0(SPI1);
  spi_set_clock_phase_0(SPI1);
  spi_set_full_duplex_mode(SPI1);
  spi_set_unidirectional_mode(SPI1); /* bidirectional but in 3-wire */
  spi_set_data_size(SPI1, SPI_CR2_DS_8BIT);
  spi_enable_software_slave_management(SPI1);
  spi_send_msb_first(SPI1);
  spi_set_nss_high(SPI1);
  //spi_enable_ss_output(SPI1);
  spi_fifo_reception_threshold_8bit(SPI1);
  SPI_I2SCFGR(SPI1) &= ~SPI_I2SCFGR_I2SMOD;
  spi_enable(SPI1);
}
Beispiel #3
0
void init_codec() {
  /* enable clock for Aux power and power up the regulators */
  rcc_peripheral_enable_clock(&CODEC_PWR_APB, CODEC_RCC_PWR);
  gpio_set_mode(CODEC_PWR_PORT, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_PUSHPULL, CODEC_PWR);
  gpio_set(CODEC_PWR_PORT, CODEC_PWR);
  
  /* enable SPI1 clock */
  rcc_peripheral_enable_clock(&CODEC_SPI_APB, CODEC_RCC_SPI);
  /* enable clock for the chip select pin */
  rcc_peripheral_enable_clock(&CODEC_IOS_APB, CODEC_RCC_IOS);
  /* enable clock for the RST/DREQ lines */
  rcc_peripheral_enable_clock(&CODEC_IOI_APB, CODEC_RCC_IOI);
  
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);

  /* set the pin modes for the SPI pins */
  gpio_set_mode(CODEC_PORT, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_PUSHPULL, CODEC_CS);
  gpio_set_mode(CODEC_PORT, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, CODEC_MOSI);
  gpio_set_mode(CODEC_PORT, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, CODEC_SCK);
  gpio_set_mode(CODEC_PORT, GPIO_MODE_INPUT,
                GPIO_CNF_INPUT_FLOAT, CODEC_MISO);

  /* set the modes for the reset and busy pins */
  gpio_set_mode(CODEC_RST_PORT, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_PUSHPULL, CODEC_RST);
  gpio_set_mode(CODEC_DREQ_PORT, GPIO_MODE_INPUT,
                GPIO_CNF_INPUT_FLOAT, CODEC_DREQ);

  gpio_clear(CODEC_RST_PORT, CODEC_RST);

  /* configure the SPI port */
  spi_set_unidirectional_mode(CODEC_SPI);
  spi_disable_crc(CODEC_SPI);
  spi_set_dff_8bit(CODEC_SPI);
  spi_set_full_duplex_mode(CODEC_SPI);
  spi_enable_software_slave_management(CODEC_SPI);
  spi_set_nss_high(CODEC_SPI);
  spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_32);
  spi_set_master_mode(CODEC_SPI);
  spi_send_msb_first(CODEC_SPI);
  spi_set_clock_polarity_0(CODEC_SPI);
  spi_set_clock_phase_0(CODEC_SPI);
  spi_disable_ss_output(CODEC_SPI);
  spi_enable(CODEC_SPI);

  /* disable chip select */
  gpio_set(CODEC_PORT, CODEC_CS);

  /* make sure reset is not asserted */
  gpio_set(CODEC_RST_PORT, CODEC_RST);

  return;
}
Beispiel #4
0
static void UseModule(int module)
{
    if (module == last_module_used) return;

    u8 cmd = (module & 0x07) | extra_bits;
    CS_LO();
    spi_xfer(PROTO_SPI.spi, cmd);
    CS_HI();

    // Adjust baud rate
    spi_disable(PROTO_SPI.spi);
    if (module == MODULE_FLASH) {
        spi_set_baudrate_prescaler(PROTO_SPI.spi, SPI_CR1_BR_FPCLK_DIV_4);
    } else if (last_module_used == MODULE_FLASH) {
        spi_set_baudrate_prescaler(PROTO_SPI.spi, SPI_CR1_BR_FPCLK_DIV_16);
    }
    spi_enable(PROTO_SPI.spi);

    last_module_used = module;
}
Beispiel #5
0
void play_file_fast_async(char *filename) {
  //int i, j=0, k; //l, fn;
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  vs1053_SCI_write(0x00, 0xc00);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  vs1053_SCI_write(SCI_VOL, 0x1010);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  iprintf("initial state 0x%04X\n", vs1053_SCI_read(SCI_CLOCKF));
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_256);
  vs1053_SCI_write(SCI_CLOCKF, 0xF800);
  spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_16);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  iprintf("after setup 0x%04X\n", vs1053_SCI_read(SCI_CLOCKF));


//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}
//  vs1053_SCI_write(SCI_CLOCKF, 0xA000);
//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}


  current_track.byte_count = 0;
  current_track_playing = 1;
  // now to set up the external interrupt on DREQ
  // may need to trigger the first service in software too as we probably won't get an edge
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
  
  nvic_enable_irq(NVIC_EXTI3_IRQ);
  nvic_set_priority(NVIC_EXTI3_IRQ, 16);
  
  exti_select_source(EXTI3, CODEC_DREQ_PORT);
  exti_set_trigger(EXTI3, EXTI_TRIGGER_RISING);
  exti_enable_request(EXTI3);
  // generate a software interrupt to get this thing started as there shouldn't be any edges
  sdfat_open_media(filename);
//   iprintf("Generating software interrupt to start playback\r\n");
  EXTI_SWIER |= EXTI3;
//   exti3_isr();
}
Beispiel #6
0
void spi_start(void)
{
    /* Reset flash chip */
    gpio_clear(GPIOD, BL_SPI2_RST);
    wait(10);
    gpio_set(GPIOD, BL_SPI2_RST);
    wait(10);

    gpio_set(GPIOD, BL_SPI2_WP);
    /* No WriteProtect, Set Chip select to 1(no select) */
    gpio_set(GPIOB, BL_SPI2_NSS);

    /* Reset and disable SPI */
    spi_reset(SPI2);

    /* Disable I2S */
    SPI2_I2SCFGR = 0;

    /* CR1 */
    spi_set_clock_phase_0(SPI2);                /* CPHA = 0    */
    spi_set_clock_polarity_0(SPI2);             /* CPOL = 0    */
    spi_send_msb_first(SPI2);                   /* LSB = 0     */
    spi_set_full_duplex_mode(SPI2);             /* RXONLY = 0  */
    spi_set_unidirectional_mode(SPI2);          /* BIDI = 0    */
    spi_enable_software_slave_management(SPI2); /* SSM = 1     */
    spi_set_nss_high(SPI2);                     /* SSI = 1     */
    spi_set_master_mode(SPI2);                  /* MSTR = 1    */
    spi_set_dff_8bit(SPI2);                     /* DFf = 8 bit */
//    spi_enable_crc(SPI2);
    /* XXX: Too fast? Maybe DIV_4 will be better? */
    spi_set_baudrate_prescaler(SPI2, SPI_CR1_BR_FPCLK_DIV_2);

    /* CR2 */
    spi_enable_ss_output(SPI2); /* SSOE = 1 */
    /* Disable regular interrupt flags */
    spi_disable_tx_buffer_empty_interrupt(SPI2);
    spi_disable_rx_buffer_not_empty_interrupt(SPI2);

    spi_disable_error_interrupt(SPI2);

    /* Enabling RX/TX DMA flags */
    spi_enable_tx_dma(SPI2);
    spi_enable_rx_dma(SPI2);

    d_print("REG: %lu:%lu\r\n", SPI_CR1(SPI2), SPI_CR2(SPI2));

    spi_enable(SPI2);
}
static void spi_setup(void)
{
spi_set_master_mode(SPI1);
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_64);
spi_set_clock_polarity_1(SPI1);
spi_set_clock_phase_0(SPI1);
spi_set_bidirectional_transmit_only_mode(SPI1);
//spi_set_unidirectional_mode(SPI1); /* bidirectional but in 3-wire */
spi_set_data_size(SPI1, SPI_CR2_DS_16BIT);
//spi_enable_software_slave_management(SPI1);
spi_send_msb_first(SPI1);
spi_set_nss_low(SPI1);
//spi_enable_ss_output(SPI1);
//spi_fifo_reception_threshold_8bit(SPI1);
//SPI_I2SCFGR(SPI1) &= ~SPI_I2SCFGR_I2SMOD;
spi_enable(SPI1);
}
Beispiel #8
0
static void spi_setup(void)
{
	/* The DOGM128 display is connected to SPI2, so initialise it. */

	rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_SPI2EN);

	spi_set_unidirectional_mode(DOGM128_SPI); /* We want to send only. */
	spi_disable_crc(DOGM128_SPI); /* No CRC for this slave. */
	spi_set_dff_8bit(DOGM128_SPI); /* 8-bit dataword-length */
	spi_set_full_duplex_mode(DOGM128_SPI); /* Not receive-only */
	/* We want to handle the CS signal in software. */
	spi_enable_software_slave_management(DOGM128_SPI);
	spi_set_nss_high(DOGM128_SPI);
	/* PCLOCK/256 as clock. */
	spi_set_baudrate_prescaler(DOGM128_SPI, SPI_CR1_BR_FPCLK_DIV_256);
	/* We want to control everything and generate the clock -> master. */
	spi_set_master_mode(DOGM128_SPI);
	spi_set_clock_polarity_1(DOGM128_SPI); /* SCK idle state high. */
	/* Bit is taken on the second (rising edge) of SCK. */
	spi_set_clock_phase_1(DOGM128_SPI);
	spi_enable_ss_output(DOGM128_SPI);
	spi_enable(DOGM128_SPI);
}
Beispiel #9
0
void mew_spi_flash_init(void) {
    gpio_mode_setup(MEW_FLASH_GPIO_PORT_WP, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_FLASH_GPIO_PIN_WP);
    gpio_set_output_options(MEW_FLASH_GPIO_PORT_WP, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_GPIO_PIN_WP);
    gpio_set(MEW_FLASH_GPIO_PORT_WP, MEW_FLASH_GPIO_PIN_WP);
    
    gpio_mode_setup(MEW_FLASH_GPIO_PORT_HOLD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_FLASH_GPIO_PIN_HOLD);
    gpio_set_output_options(MEW_FLASH_GPIO_PORT_HOLD, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_GPIO_PIN_HOLD);
    gpio_set(MEW_FLASH_GPIO_PORT_HOLD, MEW_FLASH_GPIO_PIN_HOLD);
    
    gpio_mode_setup(MEW_FLASH_GPIO_PORT_CS, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_FLASH_GPIO_PIN_CS);
    gpio_set_output_options(MEW_FLASH_GPIO_PORT_CS, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_GPIO_PIN_CS);
    gpio_set(MEW_FLASH_GPIO_PORT_CS, MEW_FLASH_GPIO_PIN_CS);
    
    gpio_mode_setup(MEW_FLASH_SPI_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, MEW_FLASH_SPI_GPIO_PINS);
    gpio_set_output_options(MEW_FLASH_SPI_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_SPI_GPIO_PINS);
    gpio_set_af(MEW_FLASH_SPI_GPIO_PORT, MEW_FLASH_SPI_GPIO_AF_NUMBER, MEW_FLASH_SPI_GPIO_PINS);
    
    spi_disable(MEW_FLASH_SPI);
    spi_set_master_mode(MEW_FLASH_SPI);
    spi_set_baudrate_prescaler(MEW_FLASH_SPI, SPI_CR1_BR_FPCLK_DIV_2);
    spi_set_clock_polarity_0(MEW_FLASH_SPI);
    spi_set_clock_phase_0(MEW_FLASH_SPI);
    spi_set_unidirectional_mode(MEW_FLASH_SPI);
    spi_enable_software_slave_management(MEW_FLASH_SPI);
    spi_send_msb_first(MEW_FLASH_SPI);
    spi_set_nss_high(MEW_FLASH_SPI);
    SPI_I2SCFGR(MEW_FLASH_SPI) &= ~SPI_I2SCFGR_I2SMOD;
    spi_disable_tx_buffer_empty_interrupt(MEW_FLASH_SPI);
    spi_disable_rx_buffer_not_empty_interrupt(MEW_FLASH_SPI);
    spi_disable_error_interrupt(MEW_FLASH_SPI);
    spi_disable_tx_dma(MEW_FLASH_SPI);
    spi_disable_rx_dma(MEW_FLASH_SPI);
    spi_set_dff_8bit(MEW_FLASH_SPI);
    spi_send_msb_first(MEW_FLASH_SPI);
    spi_enable(MEW_FLASH_SPI);
}
Beispiel #10
0
void play_file_fast(char *filename) {
  int i, j=0, k; //l, fn;
  uint16_t endFillByte;
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  vs1053_SCI_write(0x00, 0xc00);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  vs1053_SCI_write(SCI_VOL, 0x1010);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  iprintf("initial state 0x%04X\n", vs1053_SCI_read(SCI_CLOCKF));
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_256);
  vs1053_SCI_write(SCI_CLOCKF, 0xF800);
  spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_16);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  iprintf("after setup 0x%04X\n", vs1053_SCI_read(SCI_CLOCKF));


//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}
//  vs1053_SCI_write(SCI_CLOCKF, 0xA000);
//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}

  sdfat_open_media(filename);

  while(!media_file.near_end) {
    for(k=0;k<4;k++) {
      gpio_set(CODEC_PORT, CODEC_CS);
      for(j=0;j<16;j++) {
        while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
        for(i=0;i<32;i++) {
          spi_msg(media_file.buffer[media_file.active_buffer][512 * k + 32*j + i]);
        }
      }
      gpio_clear(CODEC_PORT, CODEC_CS);
    }
    sdfat_read_media();
  }
  // the current buffer is not full, and is the last
  for(k=0;k<4;k++) {
    gpio_set(CODEC_PORT, CODEC_CS);
    for(j=0;j<16;j++) {
      while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
      for(i=0;i<32;i++) {
        if((512 * k + 32 * j + i) > media_file.file_end) {
          i = 32;
          j = 16;
          k = 4;    /* bust us out of all the nested loops */
        } else {
          spi_msg(media_file.buffer[media_file.active_buffer][512 * k + 32 * j + i]);
        }
      }
    }
    gpio_clear(CODEC_PORT, CODEC_CS);
  }
  /* now need to clean up the fifos and stop the player */
  gpio_clear(CODEC_PORT, CODEC_CS);
  /* fetch the value of endFillByte */
  vs1053_SCI_write(SCI_WRAMADDR, PARAM_END_FILL_BYTE);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  endFillByte = vs1053_SCI_read(SCI_WRAM) & 0xFF;

  gpio_set(CODEC_PORT, CODEC_CS);
  for(i=0;i<65;i++) {
    while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
    for(j=0;j<32;j++) {
      spi_msg(endFillByte);
    }
  }
  gpio_clear(CODEC_PORT, CODEC_CS);
  i = vs1053_SCI_read(SCI_MODE);
  i |= SM_CANCEL;
  vs1053_SCI_write(SCI_MODE, i);
  gpio_set(CODEC_PORT, CODEC_CS);
  j = 0;
  while(j < 2048) {
    while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
    for(i=0;i<32;i++) {
      spi_msg(endFillByte);
    }
    j += 32;
    if(!(vs1053_SCI_read(SCI_MODE) & SM_CANCEL)) {
      break;
    }
  }
  gpio_clear(CODEC_PORT, CODEC_CS);
  if(j >= 2048) {
    /* need to do a software reset */
    vs1053_SCI_write(SCI_MODE, SM_RESET);
  }
  return;
}
Beispiel #11
0
void play_file(char *filename) {
  int i, j=0,fn;
  FILE *fr;
//   int s = 1;
  /*

  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {;}
  vs1053_SCI_write(0x00, 0xc00);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {;}
  vs1053_SCI_write(SCI_VOL, 0x3030);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {;}
  iprintf("initial state 0x%04X\n", vs1053_SCI_read(SCI_CLOCKF));*/
//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}
//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}


//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}
//  vs1053_SCI_write(SCI_CLOCKF, 0xA000);
//  while(!gpio_get(CODEC_CTRL, CODEC_DREQ)) {;}

  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  vs1053_SCI_write(0x00, 0xc00);
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  vs1053_SCI_write(SCI_VOL, 0x3030);
  
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  iprintf("SCI_STATUS = 0x%04X\r\n", vs1053_SCI_read(SCI_STATUS));
  
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  iprintf("initial state 0x%04X\r\n", vs1053_SCI_read(SCI_CLOCKF));
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_256);
  vs1053_SCI_write(SCI_CLOCKF, 0xF800);
  spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_16);
  
  for(i=0;i<1000;i++) {__asm__("nop\n\t");}
  while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
  iprintf("after setup 0x%04X\r\n", vs1053_SCI_read(SCI_CLOCKF));
  
  fr = fopen(filename, "r");
  fn = fileno(fr);
//   iprintf("File No. %d\r\n", fn - FIRST_DISC_FILENO);
  
//   iprintf("==================================\r\n");
//   int k;
//   for(k=0;k<sizeof(FileS);k++) {
//     iprintf("%02X ", ((char *)(&file_num[fn- FIRST_DISC_FILENO]))[k]);
//     if((k+1) % 8 == 0) iprintf("\r\n");
//   }
  
//  l = read(fn, dat, 512);
  while(1) {
    gpio_set(CODEC_PORT, CODEC_CS);
    for(j=0;j<16;j++) {
      while(!gpio_get(CODEC_DREQ_PORT, CODEC_DREQ)) {__asm__("nop\n\t");}
      for(i=0;i<32;i+=2) {
        spi_msg(file_num[fn-FIRST_DISC_FILENO].buffer[32*j+i]);
        spi_msg(file_num[fn-FIRST_DISC_FILENO].buffer[32*j+i+1]);
      }
    }
    gpio_clear(CODEC_PORT, CODEC_CS);
//     iprintf("getting sector %d\r\n", s++);
    sdfat_next_sector(fn-FIRST_DISC_FILENO);
  }

}
int devspi_create(const struct spi_config *conf)
{
    struct dev_spi *spi = NULL;

    if (!conf)
        return -EINVAL;
    if (conf->base == 0)
        return -EINVAL;

    if ((conf->idx < 0) || (conf->idx > MAX_SPIS))
        return -EINVAL;

    spi = kalloc(sizeof(struct dev_spi));
    if (!spi)
        return -ENOMEM;

    /* Claim pins for SCK/MOSI/MISO */
    gpio_create(&mod_spi, &conf->pio_sck);
    gpio_create(&mod_spi, &conf->pio_mosi);
    gpio_create(&mod_spi, &conf->pio_miso);

    /* Erase spi content */
    memset(spi, 0, sizeof(struct dev_spi));

    /* Enable clocks */
    rcc_periph_clock_enable(conf->rcc);
    rcc_periph_clock_enable(conf->dma_rcc);

    /* Startup routine */
    //spi_disable(conf->base);

    /**********************************/
	/* reset SPI1 */
	spi_reset(conf->base);
	/* init SPI1 master */
	spi_init_master(conf->base,
					SPI_CR1_BAUDRATE_FPCLK_DIV_64,
					SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE,
					SPI_CR1_CPHA_CLK_TRANSITION_1,
					SPI_CR1_DFF_8BIT,
					SPI_CR1_MSBFIRST);
	/* enable SPI1 first */
	spi_enable(conf->base);
    /**********************************/

#if 0
    spi_set_master_mode(conf->base);
    spi_set_baudrate_prescaler(conf->base, SPI_CR1_BR_FPCLK_DIV_256); /* TODO: Calculate prescaler from baudrate */
    if(conf->polarity == 0) 
        spi_set_clock_polarity_0(conf->base);
    else                    
        spi_set_clock_polarity_1(conf->base);
    if(conf->phase == 0) spi_set_clock_phase_0(conf->base);
    else
        spi_set_clock_phase_1(conf->base);
    if(conf->rx_only == 0)      
        spi_set_full_duplex_mode(conf->base);
    else
        spi_set_receive_only_mode(conf->base);
    if(conf->bidir_mode == 0)      
        spi_set_unidirectional_mode(conf->base);
    else
        spi_set_bidirectional_mode(conf->base);
    if(conf->dff_16) 
        spi_set_dff_16bit(conf->base);
    else
        spi_set_dff_8bit(conf->base);
    if(conf->enable_software_slave_management) 
        spi_enable_software_slave_management(conf->base);
    else
        spi_disable_software_slave_management(conf->base);
    if(conf->send_msb_first) 
        spi_send_msb_first(conf->base);
    else
        spi_send_lsb_first(conf->base);
    spi_set_nss_high(conf->base);
#endif

    /* Set up device struct */
    spi->base = conf->base;
    spi->irq = conf->irq;
    //spi->tx_dma_config = &conf->tx_dma;
    //spi->rx_dma_config = &conf->rx_dma;
    spi->mutex = mutex_init();

    /* Store address in the DEV_SPI array. */
    DEV_SPI[conf->idx] = spi;

    /* Enable interrupts */
    //nvic_set_priority(conf->irq, 1);
    //nvic_enable_irq(conf->irq);
    return 0;
}