File tree Expand file tree Collapse file tree 4 files changed +90
-0
lines changed
chipflow_lib/software/drivers Expand file tree Collapse file tree 4 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ /* SPDX-License-Identifier: BSD-2-Clause */
2+ #include "i2c.h"
3+
4+ void i2c_init (volatile i2c_regs_t * i2c , uint32_t divider ) {
5+ i2c -> divider = divider ;
6+ }
7+
8+ void i2c_start (volatile i2c_regs_t * i2c ) {
9+ i2c -> action = (1 <<1 );
10+ while (i2c -> status & 0x1 )
11+ ;
12+ }
13+
14+ int i2c_write (volatile i2c_regs_t * i2c , uint8_t data ) {
15+ i2c -> send_data = data ;
16+ while (i2c -> status & 0x1 )
17+ ;
18+ return (i2c -> status & 0x2 ) != 0 ; // check ACK
19+ }
20+
21+ uint8_t i2c_read (volatile i2c_regs_t * i2c ) {
22+ i2c -> action = (1 <<3 );
23+ while (i2c -> status & 0x1 )
24+ ;
25+ return i2c -> receive_data ;
26+ }
27+
28+ void i2c_stop (volatile i2c_regs_t * i2c ) {
29+ i2c -> action = (1 <<2 );
30+ while (i2c -> status & 0x1 )
31+ ;
32+ }
Original file line number Diff line number Diff line change 1+ /* SPDX-License-Identifier: BSD-2-Clause */
2+ #ifndef I2C_H
3+ #define I2C_H
4+
5+ #include <stdint.h>
6+
7+ typedef struct {
8+ uint32_t divider ;
9+ uint32_t action ;
10+ uint32_t send_data ;
11+ uint32_t receive_data ;
12+ uint32_t status ;
13+ } i2c_regs_t ;
14+
15+ void i2c_init (volatile i2c_regs_t * i2c , uint32_t divider );
16+ void i2c_start (volatile i2c_regs_t * i2c );
17+ int i2c_write (volatile i2c_regs_t * i2c , uint8_t data );
18+ uint8_t i2c_read (volatile i2c_regs_t * i2c );
19+ void i2c_stop (volatile i2c_regs_t * i2c );
20+
21+ #endif
Original file line number Diff line number Diff line change 1+ /* SPDX-License-Identifier: BSD-2-Clause */
2+ #include "spi.h"
3+
4+ void spi_init (volatile spi_regs_t * spi , uint32_t divider ) {
5+ spi -> divider = divider ;
6+ spi -> config = 0x02 ; // CS=0, SCK_EDGE=1, SCK_IDLE=0
7+ }
8+
9+ uint32_t spi_xfer (volatile spi_regs_t * spi , uint32_t data , uint32_t width , bool deselect ) {
10+ spi -> config = ((width - 1 ) << 3 ) | 0x06 ; // CS=1, SCK_EDGE=1, SCK_IDLE=0
11+ spi -> send_data = data << (32U - width );
12+ while (!(spi -> status & 0x1 )) // wait for rx full
13+ ;
14+ if (deselect ) {
15+ spi -> config = ((width - 1 ) << 3 ) | 0x02 ; // CS=0, SCK_EDGE=1, SCK_IDLE=0
16+ }
17+ return spi -> receive_data ;
18+ }
Original file line number Diff line number Diff line change 1+ /* SPDX-License-Identifier: BSD-2-Clause */
2+ #ifndef SPI_H
3+ #define SPI_H
4+
5+ #include <stdint.h>
6+ #include <stdbool.h>
7+
8+ typedef struct {
9+ uint32_t config ;
10+ uint32_t divider ;
11+ uint32_t send_data ;
12+ uint32_t receive_data ;
13+ uint32_t status ;
14+ } spi_regs_t ;
15+
16+ void spi_init (volatile spi_regs_t * spi , uint32_t divider );
17+ uint32_t spi_xfer (volatile spi_regs_t * spi , uint32_t data , uint32_t width , bool deselect );
18+
19+ #endif
You can’t perform that action at this time.
0 commit comments