void SPI_Init() { bcm2835_init(); bcm2835_spi_begin(); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, 0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, 0); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); }
int main(int argc, char **argv) { // If you call this, it will not actually access the GPIO // Use for testing // bcm2835_set_debug(1); if (!bcm2835_init()) { return 1; } bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default int i; for( i = 0; i < 100; i++ ) { // Send a byte to the slave and simultaneously read a byte back from the slave // If you tie MISO to MOSI, you should read back what was sent uint8_t data = bcm2835_spi_transfer(i); printf("Wrote: %02X Read from SPI: %02X\n", i, data); } bcm2835_spi_end(); bcm2835_close(); return 0; }
void SpiPlateformImplementation::setCsPolarity( GpioState state) { #ifdef TARGET_RASPBERRY_PI bcm2835_spi_setChipSelectPolarity( _cs, state ); #endif }
/** * * @param dmx_device_info */ static void ws2812_zero(dmx_device_info_t *dmx_device_info, const uint8_t *dmx_data) { int i,j; bcm2835_spi_setClockDivider((uint16_t) ((uint32_t) BCM2835_CORE_CLK_HZ / (uint32_t) 6400000)); bcm2835_spi_chipSelect(dmx_device_info->device_info.chip_select); // Just in case we have a multiplexer bcm2835_spi_setChipSelectPolarity(dmx_device_info->device_info.chip_select, LOW); // Just in case we have a multiplexer // Clear TX and RX fifos BCM2835_PERI_SET_BITS(BCM2835_SPI0->CS, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR); // Set TA = 1 BCM2835_PERI_SET_BITS(BCM2835_SPI0->CS, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA); for (i = 0; i < ((int) dmx_device_info->pixel_count * (int) WS2812_SLOTS_PER_PIXEL); i++) { for (j = 0; j < 8; j++) { // Maybe wait for TXD while (!(BCM2835_SPI0->CS & BCM2835_SPI0_CS_TXD)) ; BCM2835_SPI0->FIFO = (uint32_t) WS2812_LOW_CODE; while ((BCM2835_SPI0->CS & BCM2835_SPI0_CS_RXD)) { (void) BCM2835_SPI0->FIFO; } } } // Wait for DONE to be set while (!(BCM2835_SPI0->CS & BCM2835_SPI0_CS_DONE)) { while ((BCM2835_SPI0->CS & BCM2835_SPI0_CS_RXD)) { (void) BCM2835_SPI0->FIFO; } } // Set TA = 0 BCM2835_PERI_SET_BITS(BCM2835_SPI0->CS, 0, BCM2835_SPI0_CS_TA); }
void Rfid::init_rfid(void) { if(geteuid()!=0 || getenv("FAKEROOTKEY")) { (*_pLinkToConsole)->printOut("You need to be root to properly run this program"); throw "You need to be root to properly run this program"; } if(!bcm2835_init()) { (*_pLinkToConsole)->printOut("Not able to initialize BCM2835"); throw "Not able to initialize BCM2835"; } bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32); // default bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // default /* uid_t uid=500; setuid(uid); */ InitRc522(); }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE1); bcm2835_spi_setClockDivider(2170-200); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4; bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_LEN, BCM2835_SPI0_CS_LEN); //make it 9 bit volatile uint32_t* ltoh = bcm2835_spi0+BCM2835_SPI0_LTOH/4; bcm2835_peri_set_bits(ltoh,0b1111,0); unsigned int i='a'; while(1) { for(char c='a';c<='z';c++) { spi_uart_tx(c); spi_uart_tx(' '); } spi_uart_tx('\n'); // bcm2835_delay(10); // printf(" "); } bcm2835_spi_end(); bcm2835_close(); return 0; }
SpiPlateformImplementation::SpiPlateformImplementation( SpiChipSelect cs): _cs( cs ) { if( s_count++ == 0) { printf("\n SPI INIT"); #ifdef TARGET_RASPBERRY_PI bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_4096); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); #endif /* setBitOrder( SpiMSB ); setDataMode( SpiMode_0); setClockDivider( SPI_CLOCK_DIVIDER_4096); setChipSelect( _cs ); setCsPolarity( low ); */ } }
/** * @ingroup SPI-LCD * * @param device_info */ inline static void lcd_spi_setup(const device_info_t *device_info) { #ifdef __AVR_ARCH__ #else bcm2835_spi_setClockDivider(device_info->internal_clk_div); bcm2835_spi_chipSelect(device_info->chip_select); bcm2835_spi_setChipSelectPolarity(device_info->chip_select, LOW); #endif }
// // Set up a memory regions to access GPIO // void setup_io() { /* open /dev/mem */ if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { printf("can't open /dev/mem \n"); exit(-1); } /* mmap GPIO */ gpio_map = mmap( NULL, //Any adddress in our space will do BLOCK_SIZE, //Map length PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory MAP_SHARED, //Shared with other processes mem_fd, //File to map GPIO_BASE //Offset to GPIO peripheral ); if (gpio_map == MAP_FAILED) { printf("mmap error %d\n", (int)gpio_map);//errno also set! exit(-1); } // Always use volatile pointer! gpio = (volatile unsigned *)gpio_map; #define CLK_LEN 0xA8 gpio_map = mmap( NULL, //Any adddress in our space will do CLK_LEN, //Map length PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory MAP_SHARED, //Shared with other processes mem_fd, //File to map CLOCK_BASE //Offset to GPIO peripheral ); if (gpio_map == MAP_FAILED) { printf("mmap error %d\n", (int)gpio_map);//errno also set! exit(-1); } // Always use volatile pointer! clkReg = (volatile unsigned *)gpio_map; close(mem_fd); //No need to keep mem_fd open after mmap if (!bcm2835_init()) { printf("bcm2835_init error\n"); exit(-1); } #if 0 bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_4); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default #endif } // setup_io
int spi_init() { bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default //bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // The default bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default return 0; }
/** * Constructor of an SPICom instance. Initializes the CS0 settings by default */ SPICom::SPICom(bcm2835SPIClockDivider clockCS0, bcm2835SPIClockDivider clockCS1) { cout << "Initialising SPI object" << endl; this->clockCS0 = clockCS0; this->clockCS1 = clockCS1; this->chipSelect = BCM2835_SPI_CS0; if (!bcm2835_init()) cerr << "Error during initialization of bcm2835 library!!"; if (!bcm2835Initialized) bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_setClockDivider(clockCS0); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW); uint8_t readData = CS0_transfer('H'); //Send RPi heartbeat char 'H' if (readData != 'h') { cout << "Arduino doesnt have 'h' in SPDR .. waiting 3ms" << "\n"; cerr << "instead it has: " << readData << "\n"; usleep(3000); // Wait 3ms if no comm with Arduino readData = CS0_transfer('H'); //Send RPI heartbeat char 'H' if (readData != 'h') { cerr << "Arduino again doesnt have 'h' in SPDR .. Aborting communication" << "\n"; cerr << "instead it has: " << readData << "\n"; throw "SPI initializazion error"; } } readData = CS0_transfer('E'); //Send RPi EOT cout << "Arduino communication initialized\n"; time = micros(); }
/** * @ingroup DEV * * @param dmx_device_info */ static void ws2812(dmx_device_info_t * dmx_device_info, const uint8_t *dmx_data) { int i; uint8_t mask = 0x80; uint16_t dmx_data_index = dmx_device_info->dmx_start_address; bcm2835_spi_setClockDivider((uint16_t) ((uint32_t) BCM2835_CORE_CLK_HZ / (uint32_t) 6400000)); bcm2835_spi_chipSelect(dmx_device_info->device_info.chip_select); // Just in case we have a multiplexer bcm2835_spi_setChipSelectPolarity(dmx_device_info->device_info.chip_select, LOW); // Just in case we have a multiplexer // Clear TX and RX fifos BCM2835_PERI_SET_BITS(BCM2835_SPI0->CS, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR); // Set TA = 1 BCM2835_PERI_SET_BITS(BCM2835_SPI0->CS, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA); for (i = 0; i < ((int)dmx_device_info->pixel_count * (int)WS2812_SLOTS_PER_PIXEL); i++) { if (dmx_data_index > DMX_UNIVERSE_SIZE) { break; } mask = 0x80; while (mask != 0) { // Maybe wait for TXD while (!(BCM2835_SPI0->CS & BCM2835_SPI0_CS_TXD)) ; if (dmx_data[dmx_data_index] & mask) { BCM2835_SPI0->FIFO = (uint32_t) WS2812_HIGH_CODE; } else { BCM2835_SPI0->FIFO = (uint32_t) WS2812_LOW_CODE; } while ((BCM2835_SPI0->CS & BCM2835_SPI0_CS_RXD)) { (void) BCM2835_SPI0->FIFO; } mask >>= 1; } dmx_data_index++; } // Wait for DONE to be set while (!(BCM2835_SPI0->CS & BCM2835_SPI0_CS_DONE)) { while ((BCM2835_SPI0->CS & BCM2835_SPI0_CS_RXD)) { (void) BCM2835_SPI0->FIFO; } } // Set TA = 0 BCM2835_PERI_SET_BITS(BCM2835_SPI0->CS, 0, BCM2835_SPI0_CS_TA); }
// initialization of GPIO and SPI // ---------------------------------------------------------- void TFT_init_board ( void ) { // *************** set the pins to be an output and turn them on bcm2835_gpio_fsel( OE, BCM2835_GPIO_FSEL_OUTP ); bcm2835_gpio_write( OE, HIGH ); bcm2835_gpio_fsel( RAIO_RST, BCM2835_GPIO_FSEL_OUTP ); bcm2835_gpio_write( RAIO_RST, HIGH ); bcm2835_gpio_fsel( RAIO_CS, BCM2835_GPIO_FSEL_OUTP ); bcm2835_gpio_write( RAIO_CS, HIGH ); bcm2835_gpio_fsel( RAIO_RS, BCM2835_GPIO_FSEL_OUTP ); bcm2835_gpio_write( RAIO_RS, HIGH ); bcm2835_gpio_fsel( RAIO_WR, BCM2835_GPIO_FSEL_OUTP ); bcm2835_gpio_write( RAIO_WR, HIGH ); bcm2835_gpio_fsel( RAIO_RD, BCM2835_GPIO_FSEL_OUTP ); bcm2835_gpio_write( RAIO_RD, HIGH ); // *************** now the inputs bcm2835_gpio_fsel( RAIO_WAIT, BCM2835_GPIO_FSEL_INPT ); bcm2835_gpio_set_pud( RAIO_WAIT, BCM2835_GPIO_PUD_UP); bcm2835_gpio_fsel( RAIO_INT, BCM2835_GPIO_FSEL_INPT ); bcm2835_gpio_set_pud( RAIO_INT, BCM2835_GPIO_PUD_UP); // *************** set pins for SPI bcm2835_gpio_fsel(MISO, BCM2835_GPIO_FSEL_ALT0); bcm2835_gpio_fsel(MOSI, BCM2835_GPIO_FSEL_ALT0); bcm2835_gpio_fsel(SCLK, BCM2835_GPIO_FSEL_ALT0); bcm2835_gpio_fsel(SPI_CE1, BCM2835_GPIO_FSEL_ALT0); // set the SPI CS register to the some sensible defaults volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/8; bcm2835_peri_write( paddr, 0 ); // All 0s // clear TX and RX fifos bcm2835_peri_write_nb( paddr, BCM2835_SPI0_CS_CLEAR ); bcm2835_spi_setBitOrder( BCM2835_SPI_BIT_ORDER_MSBFIRST ); bcm2835_spi_setDataMode( BCM2835_SPI_MODE0 ); bcm2835_spi_setClockDivider( BCM2835_SPI_CLOCK_DIVIDER_2 ); bcm2835_spi_chipSelect( BCM2835_SPI_CS1 ); bcm2835_spi_setChipSelectPolarity( BCM2835_SPI_CS1, LOW ); }
/* Name: spi_open Description: Init bcm2835 lib & config SPI . Print to stdout bcm2835 lib version Parameters: Returns: 0 - on success, non-zero - fail */ int spi_open(void) { int r; uint32_t v; r = bcm2835_init(); // return 0 on fail if (r != 1) return -1; bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // MSB bit goes first bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // mode 0 bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_8); // SPI_CLK 31.25MHz - MAX //bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); bcm2835_spi_chipSelect(BCM2835_SPI_CS1); // CS1 - for now, with TP CS0 will be used too // setup both SPI.CSx bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // CS0 - active Low bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW); // CS1 - active Low v = bcm2835_version(); fprintf(stdout, "bcm2835 library version: %u (0x%08x)\n", v,v); return 0; // OK! }
void SPIClass::begin(uint16_t divider, uint8_t bitOrder, uint8_t dataMode) { setClockDivider(divider); setBitOrder(bitOrder); setDataMode(dataMode); //Set CS pins polarity to low bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, 0); bcm2835_spi_begin(); //Initialize a timestamp for millis calculation gettimeofday(&RHStartTime, NULL); }
static PyObject * PyBCM2835_spi_setChipSelectPolarity(PyObject *self, PyObject *args) { uint8_t cs; uint8_t active; if (!PyArg_ParseTuple(args,"ii",&cs,&active)) { return NULL; } bcm2835_spi_setChipSelectPolarity(cs,active); Py_RETURN_NONE; }
/************************************************************************** Function: spi_lcd_init Purpose: Initialize SPI device Returns: Note: Sets defaults *************************************************************************/ int spi_lcd_init(struct spi_lcd_dev *dev) { // wait for 100ms for power up delay - could probably skip this. bcm2835_delay(100); bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); bcm2835_spi_chipSeclect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0,LOW); return 0; }
/*! * Start the SPI communication */ void EcgCapture::spiInit() { bcm2835_gpio_fsel(PIN18, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(PIN18, LOW); delay(100); bcm2835_gpio_write(PIN18, HIGH); delay(100); bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_512); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); bcm2835_gpio_fsel(PIN16, BCM2835_GPIO_FSEL_INPT); }
MPU9250::MPU9250() { if (!bcm2835_init()) { printf("bcm2835_init failed. Are you running as root??\n"); } if (!bcm2835_spi_begin()) { printf("bcm2835_spi_begin failedg. Are you running as root??\n"); } bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16); // The default bcm2835_spi_setChipSelectPolarity(MPU9250_PIN, LOW); // the default bcm2835_spi_chipSelect(MPU9250_PIN); // The default }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; char buffer[4]; buffer[0] = 50; int i,temp; bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); //SCLK rising edge - clock idle state 1 bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); //set clock frequency bcm2835_spi_chipSelect(BCM2835_SPI_CS1); //use chip select 1 bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW); //chip select 0 to activate buffer[0]=buffer[1]=buffer[2]=buffer[3]=0; //bcm2835_spi_transfern(buffer,4); buffer[0] = 0x58; //read the id bcm2835_spi_transfern(buffer,2); printf("id:%02X\n",buffer[1]); while(1) { buffer[0] = 0x50; //read the temp bcm2835_spi_transfern(buffer,3); printf("status %02X %02X\n",buffer[1],buffer[2]); temp = buffer[1]; temp = temp<<8; temp = temp + ( buffer[2] & 0xF8); printf("status %08x\n",temp); temp = temp>>3; temp = temp/16; printf("temp:%d\n",temp); sleep(1); } bcm2835_spi_end(); bcm2835_close(); return 0; }
int main(int argc, char **argv) { if (!bcm2835_init()) { printf("oops, could not init bcm2835\n"); return 1; } else { printf("Initialized"); } bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024); // ~ 4 MHz bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default uint8_t mosi[12] = { 0x60, 0x00 }; uint8_t miso[12] = { 0 }; printf("Starting"); int data[30000]; for (int i = 0; i < 30000; i++) { bcm2835_spi_transfernb(mosi, miso, 2); // // printf("%d\n", miso[1] + ((miso[0] & 3) << 8)); // data[i] = miso[1] + ((miso[0] & 3) << 10); data[i] = miso[1] + miso[0]; } bcm2835_spi_end(); bcm2835_close(); FILE *fp=fopen("output2.csv","wr"); for(int i=0;i<30000;i++) { fprintf(fp,"%d,\n",data[i]); } fclose(fp); return 0; }
uint8_t HW_init(uint32_t spi_speed, uint8_t gpio) { uint16_t sp; sp=(uint16_t)(250000L/spi_speed); if (!bcm2835_init()) { fprintf(stderr, "Can't init bcm2835!\n"); return 1; } if (gpio<28) { bcm2835_gpio_fsel(gpio, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(gpio, LOW); } bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(sp); // The default bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default return 0; }
int ili9340_init(void) { int i; bcm2835_spi_begin(); bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_setClockDivider(8); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); _reset(); _setup(); uint32_t *p = (uint32_t *)buffer; for (i = 0; i < sizeof(buffer) / sizeof(buffer[0] / 2); i++) { *p++ = cur_back << 16 | cur_back; } ili9340_clear(); return 0; }
void wiring_init() { if (!bcm2835_init()) { printf("bcm2835 init failed\n"); exit(errno); } bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); // 4Mhz clock bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default bcm2835_gpio_fsel(WIRING_NRF_PROG_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(WIRING_NRF_RESET_PIN, BCM2835_GPIO_FSEL_OUTP); }
inline void static dio_spi_setup(device_info_t *device_info) { bcm2835_spi_setClockDivider(2500); // 100kHz bcm2835_spi_setChipSelectPolarity(device_info->chip_select, LOW); bcm2835_spi_chipSelect(device_info->chip_select); }
int main(int argc, char **argv) { int16_t ledIndexCounter = 0, scanPasses=0, colourIndex=0; initData(); //Initiate the SPI Data Frame if (!bcm2835_init()) { printf("bcm2835_init failed. Are you running as root??\n"); return 1; } if (!bcm2835_spi_begin()) { printf("bcm2835_spi_begin failedg. Are you running as root??\n"); return 1; } bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default /* BCM2835_SPI_MODE0 CPOL = 0, CPHA = 0 BCM2835_SPI_MODE1 CPOL = 0, CPHA = 1 BCM2835_SPI_MODE2 CPOL = 1, CPHA = 0 BCM2835_SPI_MODE3 CPOL = 1, CPHA = 1 */ bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16); // The default /* BCM2835_SPI_CLOCK_DIVIDER_65536 65536 = 262.144us = 3.814697260kHz BCM2835_SPI_CLOCK_DIVIDER_32768 32768 = 131.072us = 7.629394531kHz BCM2835_SPI_CLOCK_DIVIDER_16384 16384 = 65.536us = 15.25878906kHz BCM2835_SPI_CLOCK_DIVIDER_8192 8192 = 32.768us = 30/51757813kHz BCM2835_SPI_CLOCK_DIVIDER_4096 4096 = 16.384us = 61.03515625kHz BCM2835_SPI_CLOCK_DIVIDER_2048 2048 = 8.192us = 122.0703125kHz BCM2835_SPI_CLOCK_DIVIDER_1024 1024 = 4.096us = 244.140625kHz BCM2835_SPI_CLOCK_DIVIDER_512 512 = 2.048us = 488.28125kHz BCM2835_SPI_CLOCK_DIVIDER_256 256 = 1.024us = 976.5625kHz BCM2835_SPI_CLOCK_DIVIDER_128 128 = 512ns = = 1.953125MHz BCM2835_SPI_CLOCK_DIVIDER_64 64 = 256ns = 3.90625MHz BCM2835_SPI_CLOCK_DIVIDER_32 32 = 128ns = 7.8125MHz BCM2835_SPI_CLOCK_DIVIDER_16 16 = 64ns = 15.625MHz BCM2835_SPI_CLOCK_DIVIDER_8 8 = 32ns = 31.25MHz BCM2835_SPI_CLOCK_DIVIDER_4 4 = 16ns = 62.5MHz BCM2835_SPI_CLOCK_DIVIDER_2 2 = 8ns = 125MHz, fastest you can get */ bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default for(colourIndex=0; colourIndex<rainbowSize*10; colourIndex+=5) { getColour(colourIndex%rainbowSize, tmpColour); //set 1st Pixel ledDataBlock[8]=255; ledDataBlock[9]=tmpColour[2]; ledDataBlock[10]=tmpColour[1]; ledDataBlock[11]=tmpColour[0]; //push the array for(ledIndexCounter=(spiFrameLength-(endFrameLength*bytesPerLED))-bytesPerLED; ledIndexCounter>8; ledIndexCounter-=bytesPerLED) { ledDataBlock[ledIndexCounter] = ledDataBlock[ledIndexCounter-bytesPerLED]; ledDataBlock[ledIndexCounter+1] = ledDataBlock[ledIndexCounter+1-bytesPerLED]; ledDataBlock[ledIndexCounter+2] = ledDataBlock[ledIndexCounter+2-bytesPerLED]; ledDataBlock[ledIndexCounter+3] = ledDataBlock[ledIndexCounter+3-bytesPerLED]; } //send to LEDs bcm2835_spi_writenb(ledDataBlock, spiFrameLength); bcm2835_delay(5); } //clear and render initData(); bcm2835_spi_writenb(ledDataBlock, spiFrameLength); //close the spi bus bcm2835_spi_end(); bcm2835_close(); return 0; }
/** * @brief Disable chip select * */ void EcgCapture::csDisable() { bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, HIGH); }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default softReset(); /* printf("status: %d \n", readStatus()); printf("config: %d \n", readConfig()); printf("mode: %d \n", readMode()); printf("gain: %d \n", gainSetting); */ //changeInput(0); //changeGain(AD7794_GAIN_8); //delay(changeInputDelay); //Time to value is ready long fullData; int readAmount = 10; int delayMilis=20; while(1){ long input0 = readAvgValueFromInput(0, readAmount, delayMilis); printf("Value at input %d: %d / %06X\n", currentInput, input0, input0); long input1 = readAvgValueFromInput(1, readAmount, delayMilis); printf("Value at input %d: %d / %06X\n", currentInput, input1, input1); long input2 = readAvgValueFromInput(2, readAmount, delayMilis); printf("Value at input %d: %d / %06X\n", currentInput, input2, input2); long input3 = readAvgValueFromInput(3, readAmount, delayMilis); printf("Value at input %d: %d / %06X\n", currentInput, input3, input3); long avg = (input0 + input1 + input2 + input3) / 4; printf("avg: %d / %06X\n", avg, avg); } int loopc=0; while(1){ fullData = readSingleValue(); printf("Value at input %d: %d / %06X\n", currentInput, fullData, fullData); printf("status: %d \n", readStatus()); ++loopc; if (loopc>=10){ if (currentInput<3){ changeInput(currentInput+1); } else { changeInput(0); } delay(changeInputDelay); loopc=0; } else { delay(delayMilis); } } bcm2835_spi_end(); return 0; }
int main(int argc, char **argv) { sem_init(&empty, 0, MAX); sem_init(&full, 0, 0); sem_init(&mutex, 0, 1); //mutal exlusion sem_init(&tx_empty, 0, TX_MAX); sem_init(&tx_full, 0, 0); if (!bcm2835_init()){ printf("init done failed \n"); return 1; } bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_40 , BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_40, LOW); bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_LSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); printf("init done \n"); //audio_init(); pthread_t pid, pid2; pthread_create(&pid, NULL, spiReader, NULL); pthread_create(&pid2, NULL, packetreader, NULL); pthread_create(&pid, NULL, spiWriter, NULL); /* create a UDP socket */ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("cannot create socket\n"); return 0; } struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = TIMEOUT_MS; if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(timeout)) < 0) perror("setsockopt failed\n"); /* bind the socket to any valid IP address and a specific port */ memset((char *)&myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(SERVICE_PORT); if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("bind failed"); return 0; } runHermesLite(); bcm2835_spi_end(); bcm2835_close(); }
//void bcm2835_spi_setChipSelectPolarity(uint8_t cs, uint8_t active); /// Call bcm2835_spi_setChipSelectPolarity with 1 parameters /// \par Refer /// \par Modify void ope_spi_setchipselectpolarity(void) { get_byte_code(); get_byte_code(); bcm2835_spi_setChipSelectPolarity( *((uint8_t *)(buff+1)), *((uint8_t *)(buff+2)) ); }