Skip to content

Commit

Permalink
Proof of Concept mcp3008 reading
Browse files Browse the repository at this point in the history
  • Loading branch information
jcxldn committed Oct 1, 2023
1 parent c58a123 commit 54e6147
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// Source control information embedded at build-time
#include "git.h"

#include "mcp3008/mcp3008.h"

extern void *__BUILD_INCLUDES_FLASHLOADER;

#ifdef INCLUDES_FLASHLOADER
Expand Down Expand Up @@ -78,6 +80,24 @@ int main(void)
pvRegisterFlashloaderTask();
#endif

printf("HELLO, world!");

spi_inst_t *spi = spi0;

// TODO: fix schematic pinout (HW_SPI0)
spi_pinout_t pinout = {
.sck = 2,
.csn = 5,
.rx = 4,
.tx = 3,
};

spi_dual_inst inst = mcp3008_init_hardware(spi, 3600000, &pinout);
// true/false has no effect
uint16_t res = mcp3008_internal_do_adc(&inst, 1, false);

printf("\n%x", res);

// TinyUSB demos call this after creating tasks.
vTaskStartScheduler();

Expand Down
3 changes: 2 additions & 1 deletion src/core/main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ target_sources(${MAIN} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/tinyusb-config/usb_descriptors.c
${CMAKE_CURRENT_LIST_DIR}/tasks/bulk.c
${CMAKE_CURRENT_LIST_DIR}/tasks/mcu_temperature.c
${CMAKE_CURRENT_LIST_DIR}/mcp3008/mcp3008.c
${CMAKE_CURRENT_LIST_DIR}/config.h
)

Expand All @@ -51,7 +52,7 @@ target_sources(${MAIN} PUBLIC
# pico_malloc, pico_mem_ops - optimized versions of standard libraries.
# memory management: FreeRTOS-Kernel-Heap# required for pvPortMalloc
# tinyusb_device tinyusb_board (https://github.com/raspberrypi/pico-examples/blob/master/usb/device/dev_hid_composite/CMakeLists.txt)
target_link_libraries(${MAIN} pico_stdlib pico_malloc pico_mem_ops hardware_adc pico_unique_id FreeRTOS-Kernel FreeRTOS-Kernel-Heap4 tinyusb_device tinyusb_board cmake_git_version_tracking)
target_link_libraries(${MAIN} hardware_spi pico_stdlib pico_malloc pico_mem_ops hardware_adc pico_unique_id FreeRTOS-Kernel FreeRTOS-Kernel-Heap4 tinyusb_device tinyusb_board cmake_git_version_tracking)

# stdio only on UART (UART0 by default, pins 1 and 2)
pico_enable_stdio_usb(${MAIN} 0)
Expand Down
81 changes: 81 additions & 0 deletions src/core/mcp3008/mcp3008.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "mcp3008.h"

// gpio_set_function
#include "pico/stdlib.h"

spi_dual_inst mcp3008_init_hardware(spi_inst_t *spi, uint baudrate, spi_pinout_t *pinout)
{
spi_dual_inst inst = {
.spi_hw = spi,
//.baudrate = baudrate,
.pinout = pinout};

// Init SPIx at specified baudrate
inst.baudrate = spi_init(inst.spi_hw, baudrate);

// Assign SPI functions to pins
gpio_set_function(inst.pinout->rx, GPIO_FUNC_SPI);
gpio_set_function(inst.pinout->sck, GPIO_FUNC_SPI);
gpio_set_function(inst.pinout->tx, GPIO_FUNC_SPI);
// gpio_set_function(5, GPIO_FUNC_SPI);

gpio_init(5);
gpio_set_dir(5, GPIO_OUT);
gpio_put(5, 1);

// Create an input buffer and initialise it to zero.
for (uint8_t i = 0; i < BUF_LEN; i++)
{
inst.input_buffer[i] = 0;
}

return inst;
}

// -- Internal function to handle sending data --
#define OUTPUT_BUFFER_LEN 3
uint16_t mcp3008_internal_do_adc(spi_dual_inst *inst, uint8_t channel, bool differential)
{
// Create a buffer to sent to the device initialised to zero
uint8_t output_buffer[OUTPUT_BUFFER_LEN];
for (uint8_t i = 0; i < OUTPUT_BUFFER_LEN; i++)
{
output_buffer[i] = 0;
}

// Byte 1, leading zeros ending with a start bit
output_buffer[0] = 0x01;

// Byte 2
// Bit 1, differential signal sleection
// Bits 2-4 channel select
// Rest of buffer ignored
output_buffer[1] = ((differential ? 0 : 1) >> 7 | channel << 4);

// temp
uint8_t input_buffer[BUF_LEN];
for (uint8_t i = 0; i < BUF_LEN; i++)
{
input_buffer[i] = 0;
}

// Send the SPI command

gpio_put(5, 0);
spi_write_read_blocking(spi0, output_buffer, input_buffer, BUF_LEN);
gpio_put(5, 1);

printf("RECIEVED DATA");

uint16_t data = (((uint16_t)(input_buffer[1] & 0x03)) << 8) | input_buffer[2];

return data;

// Response
// 13 bits garbage
// 1 bit null (0)
// data, ordered bits 9 to 0
// mask 0x07 last 3 bits
// mask 0x03 last 2 bits
// return (((uint16_t)(input_buffer[1] & 0x03)) << 8) | input_buffer[2];
}
22 changes: 22 additions & 0 deletions src/core/mcp3008/mcp3008.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef MCP3008_H_
#define MCP3008_H_

#include "hardware/spi.h"

#include "pinout.h"

// TODO: can definitely get away with a smaller size
#define BUF_LEN 3

typedef struct
{
spi_inst_t *spi_hw;
uint baudrate;
spi_pinout_t *pinout;
uint8_t input_buffer[BUF_LEN];
} spi_dual_inst;

spi_dual_inst mcp3008_init_hardware(spi_inst_t *spi, uint baudrate, spi_pinout_t *pinout);
uint16_t mcp3008_internal_do_adc(spi_dual_inst *inst, uint8_t channel, bool differential);

#endif /* MCP3008_H_ */
7 changes: 7 additions & 0 deletions src/core/mcp3008/pinout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
typedef struct TU_ATTR_PACKED
{
int rx;
int sck;
int tx;
int csn;
} spi_pinout_t;

0 comments on commit 54e6147

Please sign in to comment.