aboutsummaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorThomasintAnker <thomasintanker1@gmail.com>2024-05-24 11:21:41 +0200
committerThomasintAnker <thomasintanker1@gmail.com>2024-05-24 11:21:41 +0200
commit4499d3cdfa2210a3c8be5e93f45fd7286e0d646d (patch)
tree6c8de3536a0c03f29198e905a4d7f4a8d2538e2a /main
parent5e7481e9170c1334d74a57e84640e89a40cf74c3 (diff)
pani
Diffstat (limited to 'main')
-rw-r--r--main/i2c.c33
-rw-r--r--main/i2c.h2
-rw-r--r--main/main.c4
3 files changed, 25 insertions, 14 deletions
diff --git a/main/i2c.c b/main/i2c.c
index cbf6938..c1f11ea 100644
--- a/main/i2c.c
+++ b/main/i2c.c
@@ -1,4 +1,5 @@
#include "i2c.h"
+#include "init.h"
#include <stdio.h>
#include <stddef.h>
@@ -7,8 +8,9 @@
#include <hardware/i2c.h>
void init_i2c() {
- stdio_init_all();
- i2c_init(i2c_default, 100 * 1000); // currently at 100kHz
+ await_init();
+
+ i2c_init(I2C_PORT, 100 * 1000); // currently at 100kHz
// Initialize I2C pins - sda(16), scl(17)
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
@@ -20,25 +22,25 @@ void init_i2c() {
int read_i2c(uint8_t addr, uint8_t *output, size_t len) {
// false - finished with bus
- return i2c_read_blocking (i2c_default, addr, output, len, false);
+ return i2c_read_blocking (I2C_PORT, addr, output, len, false);
}
int write_i2c(uint8_t addr, uint8_t *input, size_t len) {
// true to keep master control of bus
- return i2c_write_blocking (i2c_default, addr, input, len, true);
+ return i2c_write_blocking (I2C_PORT, addr, input, len, true);
}
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
-void init_addr_array(uint8_t array[]) {
- for(int i = 0; i < MAX_SLAVES; i++){
+void init_addr_array(uint8_t array[], int size) {
+ for(int i = 0; i < size; i++){
array[i] = 0x00;
}
}
-uint8_t* scan_bus(uint8_t* array) {
+uint8_t* scan_bus(uint8_t *array) {
int ret;
int i = 0;
uint8_t rxdata;
@@ -49,11 +51,12 @@ uint8_t* scan_bus(uint8_t* array) {
if( reserved_addr(addr) ){
ret = PICO_ERROR_GENERIC;
}else{
- ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);
+ ret = i2c_read_blocking(I2C_PORT, addr, &rxdata, 1, false);
}
// if acknowledged -> ret == number of bytes sent
if(ret > 0){
+ printf("found i2c slave on addr: %d", addr);
array[i] = addr;
i++;
}
@@ -65,27 +68,33 @@ uint8_t* scan_bus(uint8_t* array) {
void bus_task() {
// scan bus for slaves
// send updates at regular intervals
+ await_init();
+ printf("Bus task!");
+ return;
+
int i = 0;
uint8_t found[MAX_SLAVES];
- init_addr_array(found);
+ init_addr_array(found, MAX_SLAVES);
while(1) {
+ printf("Bus scan!");
scan_bus(found);
for(int i = 0; i < MAX_SLAVES; i++){
if( found[i] == 0x00 )
break;
- uint8_t data = 1;
+ uint8_t data = 0x01;
// send data to found slave address
+ printf("printing data 1");
write_i2c(found[i], &data, 1);
- data = 0;
+ printf("printing data 0");
+ data = 0x02;
write_i2c(found[i], &data, 1);
// request update from slave addr at found[i]
//write_i2c();
}
}
-
}
diff --git a/main/i2c.h b/main/i2c.h
index 405ae1f..5ad5cfb 100644
--- a/main/i2c.h
+++ b/main/i2c.h
@@ -3,9 +3,11 @@
#include <stddef.h>
#include <stdint.h>
+#include <hardware/i2c.h>
#define SDA_PIN 16
#define SCL_PIN 17
+#define I2C_PORT i2c0
#define MAX_SLAVES 10
/**
diff --git a/main/main.c b/main/main.c
index b38030f..22287b3 100644
--- a/main/main.c
+++ b/main/main.c
@@ -24,8 +24,8 @@ int main() {
init();
xTaskCreate((TaskFunction_t) blink_task, "blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
- xTaskCreate((TaskFunction_t) serve_task, "serve", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
- xTaskCreate((TaskFunction_t) bus_task, "bus", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
+ //xTaskCreate((TaskFunction_t) serve_task, "serve", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
+ //xTaskCreate((TaskFunction_t) bus_task, "bus", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
vTaskStartScheduler();
}