aboutsummaryrefslogtreecommitdiff
path: root/main/pbdrv.c
blob: 0624897cdfae09ed198d22b7237b6f66abf37416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "pb.h"

#include "pb.h"
#include "pb-types.h"
#include "pb-mod.h"
#include "pb-send.h"

#include <hardware/i2c.h>
#include <hardware/gpio.h>
#include <pico/i2c_slave.h>
#include <pico/stdio.h>

#include <FreeRTOS.h>
#include <stdio.h>
#include <timers.h>

#define PB_I2C i2c0
#define BUF_SIZE 256
#define MSGS_MAX 4

typedef struct {
	uint8_t data[BUF_SIZE];
	size_t size;
} i2c_msg_buf_t;

static i2c_msg_buf_t msgs[MSGS_MAX];
static size_t msg_index = 0;

static void async_pb_i2c_recv(void * _msg, uint32_t _) {
	i2c_msg_buf_t * msg = _msg;
	pb_i2c_recv(msg->data, msg->size);
}

static void msg_complete(i2c_msg_buf_t * msg) {
	// defer pb_i2c_recv call to FreeRTOS scheduler as pb_i2c_recv takes
	// too long to return from an ISR
	xTimerPendFunctionCallFromISR(async_pb_i2c_recv, msg, 0, NULL);

	// prepare next message for use
	msg_index = (msg_index + 1) % MSGS_MAX;
	msgs[msg_index].size = 0;
}

// This function is called from the I2C ISR
static void recv_event(i2c_inst_t *i2c, i2c_slave_event_t event) {
	i2c_msg_buf_t * msg = &msgs[msg_index];

	switch (event) {
		case I2C_SLAVE_RECEIVE: {
			if (msg->size == BUF_SIZE) return;
			msg->data[msg->size++] = i2c_read_byte_raw(PB_I2C);
			break;
		}
		case I2C_SLAVE_FINISH: {
			msg_complete(msg);
			break;
		}
		default: break;
	}
}

void pb_setup() {
	i2c_init(PB_I2C, PB_CLOCK_SPEED_HZ);
	i2c_slave_init(PB_I2C, PB_MOD_ADDR, &recv_event);
}

__weak void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) {
	i2c_set_slave_mode(PB_I2C, false, PB_MOD_ADDR);

	// false to write stop condition to i2c bus
	i2c_write_timeout_us(PB_I2C, addr, buf, sz, false, PB_TIMEOUT_US);

	i2c_set_slave_mode(PB_I2C, true, PB_MOD_ADDR);
}

void bus_scan() {
	i2c_set_slave_mode(PB_I2C, false, PB_MOD_ADDR);

	pb_buf_t buf = pb_send_magic_req();

	// check for all 7-bit addresses
	uint16_t addr_max = 1 << 7;
	for (uint16_t addr = 0x00; addr < addr_max; addr++) {
		i2c_write_timeout_us(PB_I2C, addr, (uint8_t *) buf.data, buf.size, false, PB_TIMEOUT_US);
	}

	pb_buf_free(&buf);

	i2c_set_slave_mode(PB_I2C, true, PB_MOD_ADDR);
}

void pb_mod_blocking_delay_ms(unsigned long ms) {
	vTaskDelay(ms / portTICK_PERIOD_MS);
}