-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |