char mcp2515_read_status() { char output; spi_select(); spi_transmit(MCP_READ); spi_transmit(MCP_CANSTAT); output = spi_receive(); spi_deselect(); return output; }
void mcp2515_write(char addr, char data) { spi_select(); spi_transmit(MCP_WRITE); spi_transmit(addr); spi_transmit(data); spi_deselect(); }
void flash_erase_sector(int sector){ flash_unprotect(sector); spi_select(); spi_out_byte(FLASH_ERASE_SECTOR); flash_send_address((sector * PAGE_SIZE) + 1); spi_deselect(); flash_wait_complete(); flash_protect(sector); }
void mcp2515_bit_modify(char addr, char mask_byte, char data) { spi_select(); spi_transmit(MCP_BITMOD); spi_transmit(addr); spi_transmit(mask_byte); spi_transmit(data); spi_deselect(); }
void eeprom_93c06_enable_write(int en) { unsigned char trash; unsigned char op = (en ? SPI_OP_93C06_EWEN_MASK : SPI_OP_93C06_EWDS_MASK); spi_select(); eeprom_93c06_start_bit(); spi_transmit(op, &trash); spi_deselect(); }
void flash_write_page(uint8_t* buf, int page){ flash_unprotect(); spi_select(); spi_out_byte(FLASH_PROG_PAGE); flash_send_address(page * PAGE_SIZE); int i; for(i = 0; i < PAGE_SIZE; i++){ spi_out_byte(buf[i]); } spi_deselect(); flash_wait_complete(); }
void ms5611_prom_read (void) { for( int i = 0; i < 6; i++ ){ delay_ms(1); spi_select(); spi_write(0xA2 + (i*2) ); coeff[i] = ((uint16_t)spi_read())<<8; coeff[i]+= ((uint16_t)spi_read()); spi_deselect(); delay_ms(1); //printf("C%d:\t%u\n", i, coeff[i]); } }
void flash_init(){ spi_init(); spi_select(); spi_out_byte(FLASH_READ_ID); flash_id = spi_in_byte() << 16; flash_id |= spi_in_byte() << 8; flash_id |= spi_in_byte(); flash_size = 1 << (flash_id & 0xFF); printf("JEDEC ID: %06X, size: %u bytes\n", (unsigned int)flash_id, flash_size); spi_deselect(); }
char mcp2515_read (char addr){ char result; spi_select(); spi_transmit(MCP_READ); spi_transmit(addr); result = spi_receive(); spi_deselect(); return result; }
void eeprom_93c06_write_short(unsigned char addr, unsigned short data) { unsigned char trash; spi_select(); eeprom_93c06_start_bit(); spi_transmit(SPI_OP_93C06_WRITE_MASK | SPI_93C06_ADDR_MASK(addr), &trash); spi_transmit(SPI_93C06_WRITE_DATA_BYTE1(data), &trash); spi_transmit(SPI_93C06_WRITE_DATA_BYTE2(data), &trash); spi_deselect(); /* Operation takes 2ms to be guaranteed. */ msleep(2); }
/**************************************************************************//** * @brief Clear the display. * * @detail This function clears the display. * * @return EMSTATUS code of the operation. *****************************************************************************/ static EMSTATUS DisplayClear ( void ) { uint16_t cmd; spi_select(lcd_spi_handle); /* SCS setup time: min 6us */ PAL_TimerMicroSecondsDelay(6); /* Send command */ cmd = LS013B7DH03_CMD_ALL_CLEAR | lcdPolarity; spi_exchange_bytes(lcd_spi_handle, (uint8_t*)&cmd, NULL, 2); /* SCS hold time: min 2us */ PAL_TimerMicroSecondsDelay(2); spi_deselect(lcd_spi_handle); return DISPLAY_EMSTATUS_OK; }
void eeprom_93c06_read_short(unsigned char addr, unsigned short *data) { unsigned char r1, r2, r3; unsigned char trash; spi_select(); eeprom_93c06_start_bit(); spi_transmit(SPI_OP_93C06_READ_MASK | SPI_93C06_ADDR_MASK(addr), &trash); spi_transmit(SPI_DONT_CARE, &r1); spi_transmit(SPI_DONT_CARE, &r2); spi_transmit(SPI_DONT_CARE, &r3); spi_deselect(); *data = SPI_93C06_READ_DATA_ASSEMBLE(r1, r2, r3); }
static void _test_spi_transfer(struct spi_slave *spi, const char *prompt) { uint8_t buf[4] = {0xaa, 0x55, 0xf0, 0x0f}; debug_printf("\n"); debug_printf(STR_TAB "Now testing transfer: %s\n", prompt); do { debug_printf(STR_TAB "Press enter to start transfer..."); uart_getc(); uart_putc('\n'); spi_setup(spi); spi_select(spi); spi_transfer(spi, buf, NULL, sizeof(buf)); spi_deselect(spi); pinteract("%s occurred?", prompt); } while(1); }
void spi_stop(void) { if ( !(spi_state & spi_rx_started) )//If spi is not running return; if (COMMON_POLLING_ENABLED){ //stop polling sending and sync line swap NVIC_DisableIRQ(TIM2_IRQn); } //disable transmission interrupts while (SPI2->SR & (SPI_SR_BSY) ) { // Wait until send and receive end ( according 28.3.8 Disabling the SPI) } SPI2->CR1 &= ~SPI_CR1_SPE;// disable SPI. Enter Halt mode NVIC_DisableIRQ(SPI2_IRQn); spi_deselect(); //call interface_finalize() to send received data if any interface_finalize(); //at this point interface is stopped and can be reconfigured spi_state &= ~spi_rx_started; }
void flash_unprotect(){ spi_select(); spi_out_byte(FLASH_WRITE_ENABLE); spi_deselect(); }
void flash_protect(){ spi_select(); spi_out_byte(FLASH_WRITE_DISABLE); spi_deselect(); }
void SPI2_IRQHandler(void) { volatile unsigned int IIR; IIR = SPI2->SR; if ( (IIR & SPI_SR_BSY) )// read interrupt return; if ( (IIR & SPI_SR_RXNE) ) {// read interrupt uint16_t byte = SPI2->DR; intf_add_byte(byte); if (!poll_started){ interface_finalize();//Call to send collected data } } if (IIR & SPI_SR_TXE) {//if transmit buffer is empty char buf_el = 0; if(spi_ch_to_send == spi_ch0){ if (!SPI_TX0_isempty()) {//If there is something to send poll_started = 1; spi_select_cs(spi_ch0);//Select 0 channel in which we will send&recieve data buf_el = SPI_TX0_pop();//Take TX buffer element SPI2->DR = buf_el;//Write into SPI bus }else{//If last packet byte has been sent in previous call then last answer byte has been recieved in this call. //Or there was nothing to send to this channel spi_ch_to_send = spi_ch1;//Process next channel buffer buffer0_is_busy = 0; spi_deselect();//Deselect from previous transfer } } if(spi_ch_to_send == spi_ch1){ if (!SPI_TX1_isempty()) {//If there is something to send poll_started = 1; spi_select_cs(spi_ch1);//Select 1 channel in which we will send&recieve data buf_el = SPI_TX1_pop();//Take TX buffer element SPI2->DR = buf_el;//Write into SPI bus }else{//If last packet byte has been sent in previous call then last answer byte has been recieved in this call //Or there was nothing to send to this channel spi_ch_to_send = spi_ch2;//Process next channel buffer buffer1_is_busy = 0; spi_deselect();//Deselect from previous transfer } } if(spi_ch_to_send == spi_ch2){ if (!SPI_TX2_isempty()) {//If there is something to send poll_started = 1; spi_select_cs(spi_ch2);//Select 2 channel in which we will send&recieve data buf_el = SPI_TX2_pop();//Take TX buffer element SPI2->DR = buf_el;//Write into SPI bus }else{//If last packet byte has been sent in previous call then last answer byte has been recieved in this call //Or there was nothing to send to this channel spi_ch_to_send = spi_ch0;//Process next channel buffer buffer2_is_busy = 0; spi_deselect();//Deselect from previous transfer } } if (!buffer0_is_busy && !buffer1_is_busy && !buffer2_is_busy){//If all channels buffers are empty SPI2->CR2 &= ~SPI_CR2_TXEIE;// disable TXEmpty interrupt if nothing to send poll_started = 0;//poll ended. All packets are sent interface_finalize();//Call to send collected data } } }
/**************************************************************************//** * @brief Move and show the contents of a pixel matrix buffer onto the display. * * @param[in] device Display device pointer. * @param[in] pixelMatrix Pointer to the pixel matrix buffer to draw. * @param[in] startColumn Start column on the display where to start * drawing the pixel matrix. Must be 0 if addressing * mode is DISPLAY_ADDRESSING_BY_ROWS_ONLY. * @param[in] width Width in pixel columns of the pixel matrix to draw * Must be max width of display if the addresssing * mode is DISPLAY_ADDRESSING_BY_ROWS_ONLY. * @param[in] startRow Start row on the display where to start drawing * the pizel matrix. * @param[in] height Height in pixel rows/lines of the pixel matrix. * * @return EMSTATUS code of the operation. *****************************************************************************/ static EMSTATUS PixelMatrixDraw( DISPLAY_Device_t* device, DISPLAY_PixelMatrix_t pixelMatrix, unsigned int startColumn, unsigned int width, #ifdef EMWIN_WORKAROUND unsigned int userStride, #endif unsigned int startRow, unsigned int height ) { unsigned int i; uint16_t* p = (uint16_t *)pixelMatrix; uint16_t cmd; #ifdef EMWIN_WORKAROUND int strideGap = (userStride-width-(LS013B7DH03_CONTROL_BYTES*8)) / 8 / sizeof(uint16_t); if ((userStride-width) % sizeof(uint16_t)) return DISPLAY_EMSTATUS_INVALID_PARAMETER; #else (void) width; /* Suppress compiler warning: unused parameter. */ #endif (void) startColumn; /* Suppress compiler warning: unused parameter. */ (void) device; /* Suppress compiler warning: unused parameter. */ /* Need to adjust start row by one because LS013B7DH03 starts counting lines from 1, while the DISPLAY interface starts from 0. */ startRow++; #ifdef USE_CONTROL_BYTES /* Setup line addressing in control words. */ pixelMatrixSetup(pixelMatrix, startRow, height #ifdef EMWIN_WORKAROUND , userStride #endif ); #endif /* Assert SCS */ spi_select(lcd_spi_handle); //PAL_GpioPinOutSet( LCD_PORT_SCS, LCD_PIN_SCS ); /* SCS setup time: min 6us */ PAL_TimerMicroSecondsDelay(6); /* Send update command and first line address */ cmd = LS013B7DH03_CMD_UPDATE | (startRow << 8); spi_exchange_bytes(lcd_spi_handle, (uint8_t*) &cmd, NULL, 2 ); /* Get start address to draw from */ for ( i=0; i<height; i++ ) { /* Send pixels for this line */ spi_exchange_bytes(lcd_spi_handle, (uint8_t*) p, NULL, LS013B7DH03_WIDTH/8 + LS013B7DH03_CONTROL_BYTES); p+=(LS013B7DH03_WIDTH/8 + LS013B7DH03_CONTROL_BYTES) / sizeof(uint16_t); #ifndef USE_CONTROL_BYTES if (i==height-1) { cmd = 0xffff; } else { cmd = 0xff | ((startRow+i+1) << 8); } spi_exchange_bytes(lcd_spi_handle, (uint8_t*) &cmd, NULL, 2 ); #endif #ifdef EMWIN_WORKAROUND p += strideGap; #endif } /* SCS hold time: min 2us */ PAL_TimerMicroSecondsDelay(2); /* De-assert SCS */ spi_deselect(lcd_spi_handle); return DISPLAY_EMSTATUS_OK; }
void test_spi(void) { struct spi_slave spi; uint8_t txbuf[1024], rxbuf[1024]; unsigned int i; ptest(); /* Enable GPIO12 / PIO1.0 for CS */ LPC_IOCON->R_PIO1_0 = 0x1; LPC_GPIO1->DIR |= (1<<0); /* Setup SPI slave */ spi.master = &SPI0; spi.cs_pin = 12; spi.cs_active_high = false; spi.speed = 1000000; spi.mode = 0; _test_spi_transfer(&spi, "SPI mode=0, speed=1e6, cs active low"); spi.mode = 1; _test_spi_transfer(&spi, "SPI mode=1, speed=1e6, cs active low"); spi.mode = 2; _test_spi_transfer(&spi, "SPI mode=2, speed=1e6, cs active low"); spi.mode = 3; _test_spi_transfer(&spi, "SPI mode=3, speed=1e6, cs active low"); spi.mode = 0; spi.cs_active_high = true; _test_spi_transfer(&spi, "SPI mode=0, speed=1e6, cs active high"); spi.mode = 0; spi.cs_active_high = false; spi.speed = 6000000; _test_spi_transfer(&spi, "SPI mode=0, speed=6e6, cs active low"); spi.speed = 12000000; _test_spi_transfer(&spi, "SPI mode=0, speed=12e6, cs active low"); spi.speed = 24000000; _test_spi_transfer(&spi, "SPI mode=0, speed=24e6, cs active low"); debug_printf("\n"); debug_printf(STR_TAB "Generating pseudorandom txbuf:\n"); srand(0xdeadbeef); for (i = 0; i < sizeof(txbuf); i++) { txbuf[i] = (uint8_t)rand(); rxbuf[i] = 0xff; debug_printf("%02x ", txbuf[i]); } debug_printf("\n"); spi.speed = 1000000; debug_printf(STR_TAB "Please tie MISO and MOSI for loopback test and press enter..."); uart_getc(); uart_putc('\n'); spi_setup(&spi); spi_select(&spi); spi_transfer(&spi, txbuf, rxbuf, sizeof(txbuf)); spi_deselect(&spi); if (memcmp(txbuf, rxbuf, sizeof(txbuf)) == 0) pokay("SPI mode=0, speed=1e6 loopback success!"); else { pfail("SPI mode=0, speed=1e6 loopback failed! Got back:"); for (i = 0; i < sizeof(rxbuf); i++) debug_printf("%02x ", rxbuf[i]); debug_printf("\n"); } }