diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-08 19:20:49 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-08 19:20:49 +0200 |
commit | feaa43f37c01cf832fed17572d915e5709231893 (patch) | |
tree | 37f63dee764e75ad2dd1485f06c583b5473ff6fc /main/i2c.c | |
parent | e4b241c8ec4f12fe6ac479c2153cc540830e6a5d (diff) |
WIP raspberry pi pico pbdrv
Diffstat (limited to 'main/i2c.c')
-rw-r--r-- | main/i2c.c | 90 |
1 files changed, 34 insertions, 56 deletions
@@ -1,43 +1,14 @@ #include "i2c.h" #include "init.h" +#include "pb-mod.h" + #include <stdio.h> #include <stddef.h> #include <stdint.h> #include <pico/stdlib.h> #include <hardware/i2c.h> -void init_i2c() { - i2c_init(I2C_PORT, 100 * 1000); // currently at 100kHz - - // Initialize I2C pins - sda(16), scl(17) - gpio_set_function(SDA_PIN, GPIO_FUNC_I2C); - gpio_set_function(SCL_PIN, GPIO_FUNC_I2C); - - gpio_pull_up(SDA_PIN); - gpio_pull_up(SCL_PIN); -} - -int read_i2c(uint8_t addr, uint8_t *output, size_t len) { - // false - finished with bus - 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_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[], int size) { - for(int i = 0; i < size; i++){ - array[i] = 0x00; - } -} - uint8_t* scan_bus(uint8_t *array) { int ret; int i = 0; @@ -46,11 +17,7 @@ uint8_t* scan_bus(uint8_t *array) { for(int addr = 0; addr < (1<<7); addr++) { // ignore reserved addresses // These are any addresses of the form 000 0xxx or 111 1xxx - if( reserved_addr(addr) ){ - ret = PICO_ERROR_GENERIC; - }else{ - ret = i2c_read_blocking(I2C_PORT, addr, &rxdata, 1, false); - } + // ret = i2c_read_blocking(I2C_PORT, addr, &rxdata, 1, false); // if acknowledged -> ret == number of bytes sent if(ret > 0){ @@ -67,27 +34,38 @@ void bus_task() { // scan bus for slaves // send updates at regular intervals await_init(); - - int i = 0; - uint8_t found[MAX_SLAVES]; - 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 = 0x01; - // send data to found slave address - write_i2c(found[i], &data, 1); + vTaskDelay(1000 / portTICK_PERIOD_MS); + + // int i = 0; + // uint8_t found[MAX_SLAVES]; + // init_addr_array(found, MAX_SLAVES); + + while (true) { + vTaskDelay(9 / portTICK_PERIOD_MS); + pbdrv_i2c_send(0x69, (uint8_t *) "bbbbbbbb", 9); - data = 0x02; - write_i2c(found[i], &data, 1); - // request update from slave addr at found[i] - //write_i2c(); - } + // i2c_write_blocking(i2c0, 0x69, (uint8_t *) "bbbbbbbb", 9, false); + // pbdrv_i2c_recv(NULL, 0); } + + // while(1) { + // // printf("Bus scan!"); + // scan_bus(found); + + // for(int i = 0; i < MAX_SLAVES; i++){ + // if( found[i] == 0x00 ) + // break; + // + // uint8_t data = 0x01; + // // send data to found slave address + // write_i2c(found[i], &data, 1); + + // data = 0x02; + // write_i2c(found[i], &data, 1); + // // request update from slave addr at found[i] + // //write_i2c(); + // } + // } } + |