static uint8_t sd_write_block(uint32_t block, uint8_t *buf) { int32_t i; uint8_t res; spi_start(); while (sd_command(WRITE_SINGLE_BLOCK, block) != 0x00); spi_sendrecv(0xfe); for (i = 0; i < 512; i++) spi_sendrecv(buf[i]); spi_sendrecv(0xff); spi_sendrecv(0xff); res = spi_sendrecv(0xff); if ((res & 0x1f) != 0x05) { //res = 0bXXX0AAA1 ; AAA='010' - data accepted spi_stop(); //AAA='101' - CRC error //AAA='110' - write error return res; } while (!spi_sendrecv(0xff)); spi_stop(); spi_sendrecv(0xff); spi_start(); while (!spi_sendrecv(0xff)); spi_stop(); return 0; }
/* This function is meant to contain board-specific initialization code * for, e.g., the I/O pins. The initialization can rely on application- * specific board configuration, found in conf_board.h. */ void v2x_board_init(void) { irq_initialize_vectors(); pmic_init(); sysclk_init(); //configure clock sources for core and USB sleepmgr_init(); // Initialize the sleep manager ioport_init(); //Initializes the IOPORT service pin_init(); //whole chip pin init, modes and initial conditions spi_start(); //start SPI driver PWR_init(); //sets SR to default states - holds power up cpu_irq_enable(); eeprom_init(); //verifies eeprom safe for use menu_init(); //loads menu settings time_init(); //starts the RTC button_init(); //init button stuffs ACL_init(); //configures, but does not start sampling GSM_usart_init(); //starts direct serial channel to the SIM module CAN_uart_start(); //starts direct serial channel to the ELM module canbus_serial_routing(AVR_ROUTING); //cause the serial 3-state buffer to route the serial path from the ELM to the FTDI udc_start(); //start stack and vbus monitoring PWR_hub_start(); //connect the hub to the computer //autostart all systems delay_ms(500); GSM_modem_init(); CAN_elm_init(); ACL_set_sample_on(); PWR_host_start(); }
void updateTimer() { if (OCR0A == 0xff) // if the display is off, don't update anything return; if (!(state & state_IncrementTime) || timer[delay_Second].delay <= 2) { switch (state & ~state_IncrementTime) { case state_ShowTime: digits[1] |= 0x80; digits[2] |= 0x80; break; case state_ShowHour: if (state & state_IncrementTime) digits[0] |= 0x80; break; } } cli(); // interupts are stopped here, so there is no race condition int restart = spi.out == spi.in; for (uint8_t bi = 0; bi < 4; bi++) spi.b[spi.out++] = digits[bi]; if (restart) spi_start(); sei(); }
int main(void) { bl_init(); usart_start(); spi_start(); if (usbd_start()) { led_blink(LED_ACTIVITY, LED_STATE_RAPID); for (;;); } /* HW initialized */ led_on(LED_ACTIVITY); bl_dbg("Bootloader started."); // test_fatfs(); if (usb_connect()) { led_on(LED_USB); bl_dbg("USB connected."); bl_listen(); } else led_off(LED_USB); bl_dbg("No USB."); jump_to_app(APP_LOAD_ADDRESS); return 0; }
// Constructor for use with hardware SPI (specific clock/data pins): WS2801::WS2801(const char* spi_device_arg, uint16_t n) : spi_delay(0), pixels(0) { spi_device = strdup(spi_device_arg); spi_start(); set_length(n); }
void fill(byte color) { int b, i; for (b=0; b < numboards; b++) { if (b) delay(INTER_BOARD_DELAY); spi_start(); for (i = 0; i < 64; i++) spi_put(color); spi_end(); } }
void spi_write (char reg, char value) { //reg=reg<<1; uint32_t regAddress=(reg & 0x7E); regAddress = regAddress << 8; uint32_t data0 = regAddress | value; while(spi0->ucr & SPI_BUSY); spi0->data_in=data0; spi_start(); }
void spi_read(uint8_t addr , uint8_t *data, uint8_t len) { uint8_t i; spi_start(); spi_write_byte(addr); for (i = 0; i < len; i++){ *((data + i)) = spi_read_byte(); } spi_stop(); }
void spi_write(uint8_t addr , uint8_t *data, uint8_t len) { uint8_t i; spi_start(); spi_write_byte((0x80|addr)); for (i = 0; i < len; i++){ spi_write_byte(*(data + i)); } spi_stop(); }
unsigned char spi_read (char reg) { //reg= reg<<1; uint32_t regAddress = (0x80 | (reg & 0x7E)); regAddress = regAddress << 8; while(spi0->ucr & SPI_BUSY); spi0->data_in= regAddress; spi_start(); while(spi0->ucr & SPI_BUSY); return spi0->data_out; }
/** * @fn static int exec_by_interrupt(const SpiDeviceConfigurator_t* config, const void* pTx, int nTx, void* pRx, int nRx ) * @brief SPI execute using polling * @param config a SPI Configuration * @param pTx Tx Buffer pointer * @param nTx number of the send units * @param pRx Rx Buffer pointer * @param nRx number of the receive units */ static int exec_by_polling(const SpiDeviceConfigurator_t* config, const void* pTx, int nTx, void* pRx, int nRx ) { int ret = (nTx > nRx)? nTx : nRx; spi_start(config,pTx,nTx,pRx,nRx); for(;;) { while( !(*pSPI0_STAT & 01)) ; if ( !rcv_snd() ) break; } return ret; }
uint8_t mcp2515_read_id(uint32_t *id) { uint8_t first; uint8_t tmp; first = spi_putc(0xff); tmp = spi_putc(0xff); if (tmp & (1 << IDE)) { spi_start(0xff); *((uint16_t *) id + 1) = (uint16_t) first << 5; *((uint8_t *) id + 1) = spi_wait(); spi_start(0xff); *((uint8_t *) id + 2) |= (tmp >> 3) & 0x1C; *((uint8_t *) id + 2) |= tmp & 0x03; *((uint8_t *) id) = spi_wait(); return TRUE; }
int main(void){ int i; char buf[50] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; spi_setup(); spi_start(); for (i = 0; i < 10; i++) buf[i] = spi_transfer(buf[i]); spi_stop(); return 0; }
int main(void) { USBCS = DISCOE; OUTC = SPI_SEL; OEC = ~FPGA_DONE; OUTC = SPI_SEL | FPGA_PROG_B; FASTXFR = 0; spi_start(); spi_send_read(); /* PROG to high */ PORTACFG = 0x10; /* enable fast write strobe */ FASTXFR = 0x40; /* FBLK */ OUTC = FPGA_PROG_B; spi_start(); _asm /* sleep ? */ MOV R0, #0x20; loop0: MOVX A, @DPTR; DJNZ R0,loop0; _endasm; PORTACFG = 0x0; OUTC = FPGA_PROG_B | FPGA_INIT_B; OEC = (~FPGA_INIT_B) & (~FPGA_DONE); _asm loop1: /* wait for done to high */ MOV DPTR,#_PINSC; MOVX A,@DPTR; JNB ACC.5,loop1; _endasm; OEC = (~FPGA_INIT_B) & (~FPGA_DONE) & (~SPI_SEL); }
/** * @fn static int exec_by_interrupt(const SpiDeviceConfigurator_t* config, const void* pTx, int nTx, void* pRx, int nRx ) * @brief SPI execute using interrupt * @param config a SPI Configuration * @param pTx Tx Buffer pointer * @param nTx number of the send units * @param pRx Rx Buffer pointer * @param nRx number of the receive units */ static int exec_by_interrupt(const SpiDeviceConfigurator_t* config, const void* pTx, int nTx, void* pRx, int nRx ) { int ret = (nTx > nRx)? nTx : nRx; spi_start(config,pTx,nTx,pRx,nRx); ena_int(INHNO_SPI0); if( twai_sem( SPI_COMPLETE_SIG , SPI_TIMEOUT_PER_BYTE * ret) != E_OK ) { sig_sem( SPI_COMPLETE_SIG ); ret = 0; } return ret; }
/* Generic function to send and receive a byte */ uint8_t spi_rw_byte(uint8_t data_out) { uint8_t data_in ; spi_set_sck(8) ; spi_data_len(8) ; SPI->TX_DATAbits.DATA = data_out ; spi_start() ; while(!spi_done()) { /* Wait */ } data_in = SPI->RX_DATAbits.DATA ; spi_int_clear() ; return data_in ; }
static void sd_init(void) { int32_t i; spi_setup(SPI_CS0, 0); for (i = 0; i < 10; i++) spi_sendrecv(0xff); spi_start(); while (sd_command(GO_IDLE_STATE, 0) != 0x01); while (sd_command(SEND_IF_COND, 0x000001AA) != 0x01); while (sd_command(APP_CMD, 0) && sd_command(SD_SEND_OP_COND, 0x40000000)); while (sd_command(READ_OCR, 0) != 0x00); spi_stop(); }
static void sd_read_block(uint32_t block, uint8_t *buf) { int32_t i; spi_start(); while (sd_command(READ_SINGLE_BLOCK, block) != 0x00); while (spi_sendrecv(0xff) != 0xfe); for (i = 0; i < 512; i++) buf[i] = spi_sendrecv(0xff); spi_sendrecv(0xff); spi_sendrecv(0xff); spi_stop(); }
void spi_tx_word(uint8_t addr, uint16_t w) { uint8_t i; spi_start(); spi_write_byte((0x80|addr));//send addr for (i = 0; i< 16; i++){ if (w & 0x8000) { spi_tx_bit(1); } else { spi_tx_bit(0); } w <<= 1; } spi_stop(); }
uint16_t spi_rx_word(uint8_t addr) { uint8_t i; uint16_t w = 0; uint8_t spi_bit; spi_start(); spi_write_byte(addr);// send addr for (i = 0; i< 16; i++){ spi_rx_bit(spi_bit); w <<= 1; if (spi_bit) w |= 1; } spi_stop(); return w; }
void scrolling(void) { snooze(scrolldelay); uint8_t board_offset; for (board_offset = 0; board_offset < (8*numboards); board_offset = board_offset + 8) { if (board_offset) delay(10); // interframe delay, per data sheet spi_start(); uint8_t r; for (r=0; r < 8; r++) { //produce all the columns of one row int o = ho; // start with the global offset char *txt = t; uint8_t w = getCharWidth(*txt); uint8_t b = getCharData(*txt, r, ho); uint8_t c = 0; while (c < (board_offset + 8)) { if (o <= w) { // any bits left? if (c >= board_offset) { spi_put((b & 0x80) ? fgcolor : bgcolor); } c++; // next column o++; // ... using next bit of this one b = (b << 1); // ...teed up right here } else { // advance to next char if (*txt) ++txt; w = getCharWidth(*txt); b = getCharData(*txt, r, 0); o = 0; } } } spi_end(); } // update offset and char for next frame if (++ho > getCharWidth(*t)) { ho = 0; if (*t) ++t; // on to the next char } if (!(*t)) { snooze(0); // cancel the iso-snooze above set_state(dwell); } }
void spi_hw_init_master(const spi_bus_t spi_num) { /* Clear lock variable */ spi_lock[spi_num] = 0; /* Block access to the bus */ spi_acquire_bus(spi_num); /* enable clock gate */ spi_start(spi_num); /* Clear MDIS to enable the module. */ BITBAND_REG(SPI[spi_num]->MCR, SPI_MCR_MDIS_SHIFT) = 0; /* Enable master mode, select chip select signal polarity */ /* XXX: Hard-coded chip select active low */ /* Disable FIFOs, this can be improved in the future */ SPI[spi_num]->MCR = SPI_MCR_MSTR_MASK | SPI_MCR_PCSIS(0x1F) | SPI_MCR_DIS_RXF_MASK | SPI_MCR_DIS_TXF_MASK; /* Enable interrupts for TCF flag */ BITBAND_REG(SPI[spi_num]->RSER, SPI_RSER_TCF_RE_SHIFT) = 1; switch(spi_num) { case SPI_0: NVIC_EnableIRQ(SPI0_IRQn); break; case SPI_1: NVIC_EnableIRQ(SPI1_IRQn); break; case SPI_2: NVIC_EnableIRQ(SPI2_IRQn); break; } /* disable clock gate */ spi_stop(spi_num); /* Allow access to the bus */ spi_release_bus(spi_num); }
void writeData(uint8_t d) { if (!g_HWSPI) { bbData(d); return; } memory_barrier(); gpio_set(PIN_DC); gpio_clear(24); memory_barrier(); pack.command = 0x100 | d; // LoSSI 9-bit Parameter mode spi_start(0); // Start SPI transfer to CS0 destination // Bypass spi_write function here //while (!HW.SPI0.CS.B.TXD); // ensure no reads //HW.SPI0.FIFO = pack.command; spi_write(d); spi_flush(); memory_barrier(); gpio_set(24); memory_barrier(); //printf("Data:%.2X \n",pack.command); }
void writeCommand(uint8_t c) { if (!g_HWSPI) { bbCmd(c); return; } memory_barrier(); gpio_clear(PIN_DC); gpio_clear(24); memory_barrier(); pack.command = 0x000 | c; // LoSSI 9-bit Command mode spi_start(0); // Start SPI transfer to CS0 destination // Bypass spi_write function here //while (!HW.SPI0.CS.B.TXD); // ensure no reads //HW.SPI0.FIFO = pack.command; spi_write(c) ; spi_flush(); memory_barrier(); gpio_set(24); memory_barrier(); printf("Command:%.2X ",pack.command); }
void SPI_hook(bool when) { if(when == SPI_HOOK_BEFORE) { SPIBuf.Data[2] = SPIBuf.Data[1]; SPIBuf.Data[1] = SPIBuf.Data[0]; } else spi_start(&SPIBuf.Data[0], &SPIBuf.Data[2]); }
static void try_flash(void) { int16_t rc; FIL fp; UINT nread; const char *errmsg; static uint8_t buf[512]; SPI3_Dev.cs_pad = SDIO_CS_PAD; SPI3_Dev.cs_pin = SDIO_CS_PNUM; spi_start(&SPI3_Dev, 0); mmc_start(); GPIO_OFF(SDIO_PDOWN); vTaskDelay(pdMS_TO_TICKS(100)); serial_puts(&Serial1, "Bootloader version: " VERSION "\r\n"); rc = mmc_connect(); if (rc == EERR_OK) { serial_puts(&Serial1, "SD connected\r\n"); } else if (rc == EERR_TIMEOUT) { serial_puts(&Serial1, "Timed out waiting for SD\r\n"); return; } else { serial_puts(&Serial1, "Failed to connect to SD\r\n"); return; } serial_puts(&Serial1, "Mounting SD filesystem\r\n"); if (f_mount(0, &MMC_FS) != FR_OK) { serial_puts(&Serial1, "ERROR: Unable to mount filesystem\r\n"); return; } serial_puts(&Serial1, "Opening file " MMC_FIRMWARE_FILENAME "\r\n"); if (f_open(&fp, MMC_FIRMWARE_FILENAME, FA_READ) != FR_OK) { serial_puts(&Serial1, "Error opening file, maybe it does not exist\r\n"); return; } serial_puts(&Serial1, "Comparing file to current flash contents\r\n"); bootloader_start(); while (bootloader_status == BLS_FLASHING) { if (f_read(&fp, buf, sizeof(buf), &nread) != FR_OK) { serial_puts(&Serial1, "Error reading file\r\n"); break; } if (nread == 0) { serial_puts(&Serial1, "Error: premature end of file\r\n"); break; } errmsg = bootloader_feed(buf, nread); if (errmsg != NULL) { serial_puts(&Serial1, "Error flashing firmware: "); serial_puts(&Serial1, errmsg); serial_puts(&Serial1, "\r\n"); break; } } if (bootloader_status == BLS_DONE) { if (bootloader_was_changed()) { serial_puts(&Serial1, "New firmware successfully loaded\r\n"); } else { serial_puts(&Serial1, "Firmware is up-to-date\r\n"); } } else { serial_puts(&Serial1, "ERROR: Reset to try again or load last known good firmware\r\n"); HALT(); } }