STM32L062,I2C no ACK when clock settings different than default - c

I need to communicate STM32L062 with MC3413 accelerometer, but communication only works when uC clock settings are default (16MHz HSI) and generated by CubeMX. Any change (for example changing source to ~4MHz MSI) causes slave to not give ACK. Im beginner and i've tried many combinations of prescalers etc. and i have no clue what is wrong. Here is my main loop code:
while (1) {
uint8_t config1;
config1 = 0xC0;
status[0]=HAL_I2C_Mem_Write(&hi2c1, 0x98, 0x07, 1, &config1, 1, 100); // 0xC0 konfiguruje akcelerometr w tryb standby
config1 = 0x78;
status[1]=HAL_I2C_Mem_Write(&hi2c1, 0x98, 0x08, 1, &config1, 1, 100); // 0x7A wlacza wykrywanie tapniec we wszystkich osiach i 256Hz sampling
config1 = 0x05;
status[2]=HAL_I2C_Mem_Write(&hi2c1, 0x98, 0x20, 1, &config1, 1, 100); // 0x05 powinno ustawic rozdzielczosc 14bit i zakres 2g
config1 = 0xC1;
status[3]=HAL_I2C_Mem_Write(&hi2c1, 0x98, 0x07, 1, &config1, 1, 100); // 0xC1 konfiguruje akcelerometr w tryb aktywny
status[4]=HAL_I2C_Mem_Read(&hi2c1, 0x98, 0x11, 1, &polozenie_l, 1, 100);
status[5]=HAL_I2C_Mem_Read(&hi2c1, 0x98, 0x12, 1, &polozenie_h, 1, 100);
polozenie = ((polozenie_h << 8) | polozenie_l);
HAL_Delay(100);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
https://i.stack.imgur.com/sgBnY.png
https://i.stack.imgur.com/itOLC.png
https://i.stack.imgur.com/4Y7Kp.png
https://i.stack.imgur.com/5PlLW.png

Related

How to enable 16 bit color in ili9486?

Wanna use 16 bit color, changed register 3A to 0x55 and when I’m trying to fill out my screen with red color 0xf800, nothing happens. When I use 18 bit color 0x3A = 0x66 red color is correct
UPD: Here is my piece of code that I used for 18 bit and how I rewrite it to use with 16 bit
typedef struct {
uint8_t cmd;
uint8_t data[16];
uint8_t databytes;
} lcd_init_cmd_t;
// 18 bit color
DRAM_ATTR static const lcd_init_cmd_t ili_init_cmds_18b[]={
/* SW reset */
{0x01, {0}, 0},
/* Sleep out, also SW reset */
{0x11, {0}, 0},
{0xc0, {0x0D, 0x0D}, 2},
/* 18 bit - 0x66, 16 bit 0x55 */
{0x3A, {0x66}, 1},
/* Power Control 3 (For Normal Mode) */
{0xC2, {0x44}, 1},
/* VCOM Control */
{0xC5, {0,0,0,0}, 4},
/* Positive gamma correction */
{0xE0, {0x0F, 0x1F, 0x1C, 0x0C, 0x0F, 0x08, 0x48, 0X98, 0x37, 0x0A, 0x13, 0x04, 0x11, 0x0D, 0x00}, 15},
/* Negative gamma correction */
{0xE1, {0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75, 0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00}, 15},
/* Display Inversion OFF */
{0x20, {0}, 0},
/* Memory access contorl, MX=MY=0, MV=1, ML=0, BGR=1, MH=0 */
{0x36, {0x28}, 1},
/* Display on */
{0x29, {0}, 0},
};
// 16 bit color
DRAM_ATTR static const lcd_init_cmd_t ili_init_cmds_16b[] = {
{0x01, {0}, 0},
{0x28, {0}, 0},
{0x11, {0}, 0},
{0xC0, {0x0D, 0x0D}, 2},
{0xc1, {0x43, 0x00}, 2},
{0xc2, {0x00}, 1},
{0xC5, {0x00, 0x48}, 2},
{0xB6, {0x00, 0x01, 0x3B}, 3},
{0xE0, {0x0F, 0x24, 0x1C, 0x0A, 0x0F, 0x08, 0x43, 0x88, 0x32, 0x0F, 0x10, 0x06, 0x0F, 0x07, 0x00}, 15},
{0xE1, {0x0F, 0x38, 0x30, 0x09, 0x0F, 0x0F, 0x4E, 0x77, 0x3C, 0x07, 0x10, 0x05, 0x23, 0x1B, 0x00}, 15},
{0x20, {0x00}, 1},
{0x36, {0xE8}, 1},
{0x3A, {0x55}, 1},
{0x2A, {0x00, 0x00, 0x01, 0xDF}, 4},
{0x2B, {0x00, 0x00, 0x01, 0x3F}, 4},
{0x29, {0x00}, 0}
}; // 16-bit color
void lcd_init(){
ili_init_cmds = ili_init_cmds_18b; // <- pseudocode
//Send all the commands
uint8_t len = sizeof(ili_init_cmds)/sizeof(ili_init_cmds[0]);
for (uint8_t i=0; i<len; i++){
lcd_cmd(spi, ili_init_cmds[i].cmd);
lcd_data(spi, ili_init_cmds[i].data, ili_init_cmds[i].databytes & 0x1F);
if (ili_init_cmds[i].cmd == 0x01 || ili_init_cmds[i].cmd == 0x11 || ili_init_cmds[i].cmd == 0x28 || ili_init_cmds[i].cmd == 0x29){
vTaskDelay(100 / portTICK_RATE_MS);
}
}
}
void fill_screen(){
uint16_t color = 0xf800;
uint8_t GRAM_BUFF[1440] = {0}; // width 480 pixels * 3 bytes of color
uint16_t i;
// for 18 bit
for(i=0; i<1440; i+=3){
GRAM_BUFF[i] = RGB_R(color);
GRAM_BUFF[i+1] = RGB_G(color);
GRAM_BUFF[i+2] = RGB_B(color);
}
// for 16 bit
for (i=0; i< 1440; i+=2){
GRAM_BUFF[i] = (uint8_t)(color >> 8);
GRAM_BUFF[i+1] = (uint8_t)(color);
}
for (i=0; i<HEIGHT; i+=3){ // HEIGHT = 320pixels
send_lines(spi, i, GRAM_BUFF);
}

USB Descriptor for multiple Throttle

I would like to have a USB Joystick that is connected to a PIC microcontroller.
It works fine with just one throttle and several buttons and two axis.
How can I add another throttle descriptor ?
Here is the current one
const unsigned int USB_VENDOR_ID = 0x1234;
const unsigned int USB_PRODUCT_ID = 0x0001;
const char USB_SELF_POWER = 0x80; // Self powered 0xC0, 0x80 bus powered
const char USB_MAX_POWER = 50; // Bus power required in units of 2 mA
const char HID_INPUT_REPORT_BYTES = 64;
const char HID_OUTPUT_REPORT_BYTES = 64;
const char EP_IN_INTERVAL = 1;
const char EP_OUT_INTERVAL = 1;
// diske noconst char EP_IN_INTERVAL = 1;
const char USB_INTERRUPT = 0;
const char USB_TRANSFER_TYPE = 0x03; //0x03 Interrupt
const char USB_HID_EP = 1;
const char USB_HID_RPT_SIZE = 78;
/* Device Descriptor */
const struct {
char bLength; // bLength - Descriptor size in bytes (12h)
char bDescriptorType; // bDescriptorType - The constant DEVICE (01h)
unsigned int bcdUSB; // bcdUSB - USB specification release number (BCD)
char bDeviceClass; // bDeviceClass - Class Code
char bDeviceSubClass; // bDeviceSubClass - Subclass code
char bDeviceProtocol; // bDeviceProtocol - Protocol code
char bMaxPacketSize0; // bMaxPacketSize0 - Maximum packet size for endpoint 0
unsigned int idVendor; // idVendor - Vendor ID
unsigned int idProduct; // idProduct - Product ID
unsigned int bcdDevice; // bcdDevice - Device release number (BCD)
char iManufacturer; // iManufacturer - Index of string descriptor for the manufacturer
char iProduct; // iProduct - Index of string descriptor for the product.
char iSerialNumber; // iSerialNumber - Index of string descriptor for the serial number.
char bNumConfigurations; // bNumConfigurations - Number of possible configurations
} device_dsc = {
0x12, // bLength
0x01, // bDescriptorType
0x0200, // bcdUSB
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
8, // bMaxPacketSize0
USB_VENDOR_ID, // idVendor
USB_PRODUCT_ID, // idProduct
0x0020, // bcdDevice
0x01, // iManufacturer
0x02, // iProduct
0x03, // iSerialNumber
0x01 // bNumConfigurations
};
/* Configuration 1 Descriptor */
const char configDescriptor1[]= {
// Configuration Descriptor
0x09, // bLength - Descriptor size in bytes
0x02, // bDescriptorType - The constant CONFIGURATION (02h)
0x29,0x00, // wTotalLength - The number of bytes in the configuration descriptor and all of its subordinate descriptors
1, // bNumInterfaces - Number of interfaces in the configuration
1, // bConfigurationValue - Identifier for Set Configuration and Get Configuration requests
0, // iConfiguration - Index of string descriptor for the configuration
USB_SELF_POWER, // bmAttributes - Self/bus power and remote wakeup settings
USB_MAX_POWER, // bMaxPower - Bus power required in units of 2 mA
// Interface Descriptor
0x09, // bLength - Descriptor size in bytes (09h)
0x04, // bDescriptorType - The constant Interface (04h)
0, // bInterfaceNumber - Number identifying this interface
0, // bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber.
2, // bNumEndpoint - Number of endpoints supported not counting endpoint zero
0x03, // bInterfaceClass - Class code
0, // bInterfaceSubclass - Subclass code
0, // bInterfaceProtocol - Protocol code
0, // iInterface - Interface string index
// HID Class-Specific Descriptor
0x09, // bLength - Descriptor size in bytes.
0x21, // bDescriptorType - This descriptor's type: 21h to indicate the HID class.
0x01,0x01, // bcdHID - HID specification release number (BCD).
0x00, // bCountryCode - Numeric expression identifying the country for localized hardware (BCD) or 00h.
1, // bNumDescriptors - Number of subordinate report and physical descriptors.
0x22, // bDescriptorType - The type of a class-specific descriptor that follows
USB_HID_RPT_SIZE,0x00, // wDescriptorLength - Total length of the descriptor identified above.
// Endpoint Descriptor
0x07, // bLength - Descriptor size in bytes (07h)
0x05, // bDescriptorType - The constant Endpoint (05h)
USB_HID_EP | 0x80, // bEndpointAddress - Endpoint number and direction
USB_TRANSFER_TYPE, // bmAttributes - Transfer type and supplementary information
0x40,0x00, // wMaxPacketSize - Maximum packet size supported
EP_IN_INTERVAL, // bInterval - Service interval or NAK rate
// Endpoint Descriptor
0x07, // bLength - Descriptor size in bytes (07h)
0x05, // bDescriptorType - The constant Endpoint (05h)
USB_HID_EP, // bEndpointAddress - Endpoint number and direction
USB_TRANSFER_TYPE, // bmAttributes - Transfer type and supplementary information
0x40,0x00, // wMaxPacketSize - Maximum packet size supported
EP_OUT_INTERVAL // bInterval - Service interval or NAK rate
};
const struct {
char report[USB_HID_RPT_SIZE];
}
hid_rpt_desc =
{0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x09, 0x04, // USAGE (Joystick)
0xA1, 0x01, // COLLECTION (Application)
0x05, 0x02, // USAGE_PAGE (Simulation Controls)
0x09, 0xBB, // USAGE (Throttle)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x81, 0x02, // INPUT (Data Var Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x01, // USAGE (Pointer)
0xA1, 0x00, // COLLECTION (Physical)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x95, 0x02, // REPORT_COUNT (2)
0x81, 0x02, // INPUT (Data Var Abs)}
0xC0, // END_COLLECTION
0x09, 0x39, // USAGE (Hat switch)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x03, // LOGICAL_MAXIMUM (3)
0x35, 0x00, // PHYSICAL_MINIMUM (0)
0x46, 0x0E, 0x01, // PHYSICAL_MAXIMUM (270)
0x65, 0x14, // UNIT (Eng Rot:Angular Pos)
0x75, 0x04, // REPORT_SIZE (4)
0x95, 0x01, // REPORT_COUNT (1)
0x81, 0x02, // INPUT (Data Var Abs)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x0A, // USAGE_MAXIMUM (Button 10)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x0C, // REPORT_COUNT (12) 2 bits added to switch report count
// so bytes are even. Bytes must be even.
0x55, 0x00, // UNIT_EXPONENT (0)
0x65, 0x00, // UNIT (None)
0x81, 0x02, // INPUT (Data Var Abs)
0xC0 // END_COLLECTION
};
//Language code string descriptor
const struct {
char bLength;
char bDscType;
unsigned int string[1];
} strd1 = {
4,
0x03,
{0x0409}
};
const struct{
//Manufacturer string descriptor
char bLength;
char bDscType;
unsigned int string[4];
}strd2={
10, //sizeof this descriptor string
0x03,
{'N','X','R','T'}
};
//Product string descriptor
const struct{
char bLength;
char bDscType;
unsigned int string[13];
}strd3={
28, //sizeof this descriptor string
0x03,
{'N','X','R','T','-','J','o','y','s','t','i','c','k'}
};
//Array of configuration descriptors
const char* USB_config_dsc_ptr[1];
//Array of string descriptors
const char* USB_string_dsc_ptr[3];
void USB_Init_Desc(){
USB_config_dsc_ptr[0] = &configDescriptor1;
USB_string_dsc_ptr[0] = (const char*)&strd1;
USB_string_dsc_ptr[1] = (const char*)&strd2;
USB_string_dsc_ptr[2] = (const char*)&strd3;
}
This is a multi-throttle descriptor that I've written for the ARDUINO Leonardo that shows multiple throttles on Windows 10. Please be aware though that Windows 10 only accepts 8 axis and 128 buttons per joystick device - so this descriptor will work with X, Y, Rudder, Left Brake, Right Brake, and 3 throttles max (I usually use 2)
I've collected all the USB HID values into #define values to make it more readable. (I'm not including those here). You can find the full library at https://github.com/Raptyr/FlightControl
uint8_t descriptor[150];
int descriptorIndex = 0;
#define DESCRIPTOR(_array,_index,label,value) _array[_index++] = label;_array[_index++] = value;
#define DESCRIPTOR1(_array,_index,label) _array[_index++] = label;
#define DESCRIPTOR3(_array,_index,label,value1,value2) _array[_index++] = label; _array[_index++] = value1;_array[_index++] = value2;
DESCRIPTOR(descriptor, descriptorIndex, USAGE_PAGE, PAGE_GENERIC_DESKTOP)
DESCRIPTOR(descriptor, descriptorIndex, USAGE, GENERIC_JOYSTICK)
DESCRIPTOR(descriptor, descriptorIndex, COLLECTION, COLLECTION_APPLICATION)
DESCRIPTOR(descriptor, descriptorIndex, REPORT_ID, _hidReportID)
if (_buttonCount > 0)
{
uint8_t buttonsInLastByte = _buttonCount % 8;
uint8_t paddingBits = buttonsInLastByte > 0 ? 8 - buttonsInLastByte : 0;
DESCRIPTOR(descriptor,descriptorIndex,USAGE_PAGE,PAGE_BUTTON)
DESCRIPTOR(descriptor,descriptorIndex,USAGE_MIN,01)
DESCRIPTOR(descriptor,descriptorIndex,USAGE_MAX,_buttonCount)
DESCRIPTOR(descriptor,descriptorIndex,LOGICAL_MIN,0)
DESCRIPTOR(descriptor,descriptorIndex,LOGICAL_MAX,1)
DESCRIPTOR(descriptor,descriptorIndex,REPORT_SIZE,1)
DESCRIPTOR(descriptor,descriptorIndex,REPORT_COUNT,_buttonCount)
DESCRIPTOR(descriptor,descriptorIndex,UNIT_EXP,0)
DESCRIPTOR(descriptor,descriptorIndex,UNIT,0)
DESCRIPTOR(descriptor,descriptorIndex,INPUT,IO_DATA | IO_VARIABLE | IO_ABSOLUTE)
if (paddingBits > 0)
{
DESCRIPTOR(descriptor,descriptorIndex,REPORT_SIZE,1)
DESCRIPTOR(descriptor,descriptorIndex,REPORT_COUNT,paddingBits)
DESCRIPTOR(descriptor,descriptorIndex,INPUT,IO_CONSTANT | IO_VARIABLE | IO_ABSOLUTE)
}
}
DESCRIPTOR(descriptor, descriptorIndex, USAGE_PAGE, PAGE_GENERIC_DESKTOP)
DESCRIPTOR(descriptor, descriptorIndex, USAGE, GENERIC_POINTER)
DESCRIPTOR3(descriptor, descriptorIndex, LOGICAL_MIN16, 0x01, 0x80)
DESCRIPTOR3(descriptor, descriptorIndex, LOGICAL_MAX16, 0xFF, 0x7F)
DESCRIPTOR(descriptor, descriptorIndex, REPORT_SIZE, 0x10)
DESCRIPTOR(descriptor, descriptorIndex, REPORT_COUNT, _useBrakes ? 0x04 : 0x02)
DESCRIPTOR(descriptor, descriptorIndex, COLLECTION, COLLECTION_PHYSICAL)
DESCRIPTOR(descriptor, descriptorIndex, USAGE, GENERIC_X)
DESCRIPTOR(descriptor, descriptorIndex, USAGE, GENERIC_Y)
if (_useBrakes)
{
DESCRIPTOR(descriptor, descriptorIndex,USAGE,GENERIC_RX)
DESCRIPTOR(descriptor, descriptorIndex,USAGE,GENERIC_RY)
}
DESCRIPTOR(descriptor, descriptorIndex, INPUT, IO_DATA | IO_VARIABLE | IO_ABSOLUTE)
DESCRIPTOR1(descriptor, descriptorIndex, END_COLLECTION)
if (_useRudder || _throttleCount > 0)
{
uint8_t simulationCount = (_useRudder ? 1 : 0) + _throttleCount;
DESCRIPTOR(descriptor, descriptorIndex, USAGE_PAGE, PAGE_SIMULATION_CONTROLS)
DESCRIPTOR3(descriptor, descriptorIndex, LOGICAL_MIN16, 0x01, 0x80)
DESCRIPTOR3(descriptor, descriptorIndex, LOGICAL_MAX16, 0xFF, 0x7F)
DESCRIPTOR(descriptor, descriptorIndex, REPORT_SIZE, 0x10)
DESCRIPTOR(descriptor, descriptorIndex, REPORT_COUNT, simulationCount)
DESCRIPTOR(descriptor, descriptorIndex, COLLECTION, COLLECTION_PHYSICAL)
if (_useRudder)
{
DESCRIPTOR(descriptor, descriptorIndex, USAGE, SIMULATION_RUDDER)
}
if (_throttleCount > 0)
{
for (uint8_t i = 0; i < _throttleCount; i++)
{
DESCRIPTOR(descriptor, descriptorIndex, USAGE, SIMULATION_THROTTLE)
}
}
DESCRIPTOR(descriptor, descriptorIndex, INPUT, IO_DATA | IO_VARIABLE | IO_ABSOLUTE)
DESCRIPTOR1(descriptor, descriptorIndex, END_COLLECTION)
}
DESCRIPTOR1(descriptor, descriptorIndex, END_COLLECTION)

Error sending APDU commands with Arduino. Change the module context

I try to send APDU commands using Arduino Uno and PN532. PN532 finds the EMV card, but when I try to send the command I get:
Status code indicates error
The response dump shows this result (the first 8 bytes):
0x00 0x00 0xFF 0x03 0xFD 0xD5 0x41 0x27
The command I send(SELECT PPSE):
00 A4 04 00 0E 32 50 41 59 2E 53 59 53 2E 44 44 46 30 31 00
According to the documentation, the code 0x27 (8 byte) means:
This command is not acceptable due to the current context of the PN532 (Initiator vs. Target, unknown target number, Target not in the good state, ...)
How do I change the context to the one I need for sending commands?
My code:
void loop(void) {
uint8_t success;
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0};
uint8_t uidLength;
// EMV card
bool apdusuccess;
uint8_t apdu[255];
uint8_t berBuffer[255];
uint8_t berLength = 255;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, & uidLength);
if (success && uid && (uidLength == 4)) {
uint8_t apdu[] = {0x00, 0xA4, 0x04, 0x00, 0x0e, 0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, 0x00};
apdusuccess = nfc.inDataExchange(apdu, sizeof(apdu), berBuffer, &berLength);
Serial.print("APDU Success: ");
Serial.println(apdusuccess);
}
}
I found a solution and it worked with inListPassiveTarget:
void loop(void) {
uint8_t success;
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0};
uint8_t uidLength;
// EMV card
bool apdusuccess;
uint8_t apdu[255];
uint8_t berBuffer[255];
uint8_t berLength = 255;
success = nfc.inListPassiveTarget();
if (success) {
uint8_t apdu[] = {0x00, 0xA4, 0x04, 0x00, 0x0e, 0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, 0x00};
apdusuccess = nfc.inDataExchange(apdu, sizeof(apdu), berBuffer, &berLength);
Serial.print("APDU Success: ");
Serial.println(apdusuccess);
}
}

LibOpenCM3 1-Wire over UART DMA STM32F1

I just began to understand the embedded world (after Arduino, RPi, etc) with the STM32F103 and FreeRTOS + Libopencm3. My first challenge is to interface a DS18B20, temperature sensor with my microprocessor. The 1-Wire bus is quite easy to understand but not native supported so I followed your advised and go for 1-wire over UART with DMA.
DS18B20 has DATA on USART2TX (+4k7 pullup + diode) and USART2RX, VCC to 5V and GND.
Initialization of 1-Wire :
static void ow_init(void)
{
// One-Wire
// Already done : rcc_periph_clock_enable(RCC_GPIOA);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
usart_set_baudrate(USART2, 115200);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_STOPBITS_1);
usart_set_mode(USART2, USART_MODE_TX);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
usart_enable(USART2);
rcc_periph_clock_enable(RCC_DMA1);
}
1-Wire Reset :
uint8_t ow_reset(void)
{
usart_disable_rx_dma(USART2);
usart_disable_tx_dma(USART2);
usart_set_baudrate(USART2, 9600);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_STOPBITS_1);
usart_set_mode(USART2, USART_MODE_TX);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
usart_send(USART2, 0xf0);
while(usart_get_flag(USART2, USART_SR_TC));
uint8_t ow_presence;
ow_presence = usart_recv(USART2);
usart_set_baudrate(USART2, 115200);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_STOPBITS_1);
usart_set_mode(USART2, USART_MODE_TX_RX);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
if(ow_presence != 0xf0)
{
return 1;
}
return 0;
}
Getting the scratchpad with :
void ow_convert_to_scratchpad(void)
{
const uint8_t convert_T[] = {0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, // 0xCC
0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00}; // 0x44
dma_channel_reset(DMA1, DMA_CHANNEL7);
dma_set_peripheral_address(DMA1, DMA_CHANNEL7, (uint32_t)&USART2_DR);
dma_set_memory_address(DMA1, DMA_CHANNEL7, (uint32_t) convert_T);
dma_set_number_of_data(DMA1, DMA_CHANNEL7, sizeof(convert_T));
dma_disable_peripheral_increment_mode(DMA1, DMA_CHANNEL7);
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL7);
dma_set_peripheral_size(DMA1, DMA_CHANNEL7, DMA_CCR_PSIZE_8BIT);
dma_set_memory_size(DMA1, DMA_CHANNEL7, DMA_CCR_MSIZE_8BIT);
dma_set_priority(DMA1, DMA_CHANNEL7, DMA_CCR_PL_LOW);
dma_enable_channel(DMA1, DMA_CHANNEL7);
usart_enable_tx_dma(USART2);
}
uint16_t ow_get_scratchpad(void)
{
const uint8_t read_scratch[] = {0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, // 0xCC
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, // 0xBE
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t buf[8];
dma_channel_reset(DMA1, DMA_CHANNEL6);
dma_set_peripheral_address(DMA1, DMA_CHANNEL6, (uint32_t)&USART2_DR);
dma_set_memory_address(DMA1, DMA_CHANNEL6, (uint32_t) buf);
dma_set_read_from_peripheral(DMA1, DMA_CHANNEL6);
dma_set_number_of_data(DMA1, DMA_CHANNEL6, sizeof(read_scratch));
dma_disable_peripheral_increment_mode(DMA1, DMA_CHANNEL6);
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL6);
dma_set_peripheral_size(DMA1, DMA_CHANNEL6, DMA_CCR_PSIZE_8BIT);
dma_set_memory_size(DMA1, DMA_CHANNEL6, DMA_CCR_MSIZE_8BIT);
dma_set_priority(DMA1, DMA_CHANNEL6, DMA_CCR_PL_LOW);
dma_channel_reset(DMA1, DMA_CHANNEL7);
dma_set_peripheral_address(DMA1, DMA_CHANNEL7, (uint32_t)&USART2_DR);
dma_set_memory_address(DMA1, DMA_CHANNEL7, (uint32_t) read_scratch);
dma_set_number_of_data(DMA1, DMA_CHANNEL7, sizeof(read_scratch));
dma_set_read_from_peripheral(DMA1, DMA_CHANNEL6);
dma_disable_peripheral_increment_mode(DMA1, DMA_CHANNEL7);
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL7);
dma_set_peripheral_size(DMA1, DMA_CHANNEL7, DMA_CCR_PSIZE_8BIT);
dma_set_memory_size(DMA1, DMA_CHANNEL7, DMA_CCR_MSIZE_8BIT);
dma_set_priority(DMA1, DMA_CHANNEL7, DMA_CCR_PL_LOW);
dma_enable_channel(DMA1, DMA_CHANNEL6);
dma_enable_channel(DMA1, DMA_CHANNEL7);
usart_enable_tx_dma(USART2);
usart_enable_rx_dma(USART2);
while(dma_get_interrupt_flag(DMA1, DMA_CHANNEL6, DMA_TCIF));
uint16_t tt = 0;
for(int i=0;i<32; i++)
{
uart1_printf("Bit : %d \n\r", buf[i]);
if(buf[i] == 0xff)
{
tt = (tt >> 1) | 0x8000;
}
else
{
tt = tt >> 1;
}
}
return tt;
}
static void demo_task(void *args)
{
(void)args;
for (;;) {
uart1_printf("Hello\n\r");
uint8_t p = ow_reset();
uart1_printf("presence = %d\n\r", p);
ow_convert_to_scratchpad();
for(int i=0; i<5000000; i++)
{
__asm__("nop");
}
ow_reset();
uint16_t t = ow_get_scratchpad();
uart1_printf("t = %d \n\r", t);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
And finally the task that tries to reach the DS18B20
static void demo_task(void *args)
{
(void)args;
for (;;) {
ow_reset();
ow_convert_to_scratchpad();
vTaskDelay(pdMS_TO_TICKS(500));
ow_reset();
uint16_t t = ow_get_scratchpad();
uart1_printf("t = %d \n\r", t);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
I receive some bits as 0xCC, 0xBE, 0xFF but no more answer.
Ok, so
rcc_periph_clock_enable(RCC_USART2);
was missing so USART2 can't do its job. I can send and receive data now.
The reset function works except this line (it wait indefinitely) :
while(usart_get_flag(USART2, USART_SR_TC));
I don't understand why this flag is not true when the transmission is complete... But I have 0x00 on RX line so I think the sensor is responding (I hope...)
My function ow_convert_to_scratchpad with DMA looks like being blocking. I don"t know why...
I just tried (for fun...) to replace the whole DMA by hardcoding the sent of 0xCC, 0x44, 0xCC, 0xBE and the read but no answer (0x00) from the sensors.
I am kind of late to the party, but... Have you tried something a bit less convoluted first? (Simpler to check I mean)
Like asking a single DS18B20 its address?
Reset bus and check presence.
Send search ROM cmd (write byte 0xF0)
loop 64 times reading address bits from LSB to MSB {
read bit i
read bit i complement
check they are 1 and 0 or the other way around
send bit i back to device so it sends you back the next bit
}
At the end you have the 8 byte of the device address. 1 byte family 0x28 48 bit address 1 byte CRC8 to check the whole thing is correct.

Manual HMACSHA1 calculation differs from openssl results

I am trying to implement HMACSHA1 by Hand, to check if our serviceprovider or we calculate a wrong HMAC for a Support-Ticket (a SOAP call, hence the xml content).
The problem is, I stick to NIST Specification for HMACs and get a different result by Hand and when using openssl HMAC functions.
The following code prints:
B92674DCBA96F2DA93F7043071B931F5F2583FBD
4303E965D88D288C9AC594CE6C5E6AFF27D40B2D
while the result by openssl is the same we get in our application - so I assume, on the basis that openssl is so commonly used, that my result is wrong - but where is my error?
This is the spec I refer to:
http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.198-1.pdf
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <string.h>
#define byte unsigned char
#define IPAD 0x36
#define OPAD 0x5c
#define DIGESTSIZE 20
int main () {
byte *derivedKey = (byte[DIGESTSIZE]) {0x42,0xA9,0x78,0x90,0xFC,0xE5,0x16,0x8E,0x58,0x12,0x2F,0xF1,0xBA,0x32,0x5F,0x09,0x88,0x94,0x02,0x91};
byte *content = "<ds:SignedInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><ds:CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"><ec:InclusiveNamespaces xmlns:ec=\"http://www.w3.org/2001/10/xml-exc-c14n#\" PrefixList=\"soap\"></ec:InclusiveNamespaces></ds:CanonicalizationMethod><ds:SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\"></ds:SignatureMethod><ds:Reference URI=\"#TS-B183A13FEB0189143115136776276601\"><ds:Transforms><ds:Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"><ec:InclusiveNamespaces xmlns:ec=\"http://www.w3.org/2001/10/xml-exc-c14n#\" PrefixList=\"wsse soap\"></ec:InclusiveNamespaces></ds:Transform></ds:Transforms><ds:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"></ds:DigestMethod><ds:DigestValue>rXJdGuDqoRrUJxuGiA1eyAozifk=</ds:DigestValue></ds:Reference></ds:SignedInfo>";
byte *oPadKey = malloc(DIGESTSIZE);
byte *iPadKey = malloc(DIGESTSIZE);
int i;
for(i=0;i<DIGESTSIZE;i++){
iPadKey[i]=derivedKey[i]^IPAD;
oPadKey[i]=derivedKey[i]^OPAD;
}
byte *rOpInput= malloc(strlen(content)+DIGESTSIZE);
//concat iPad and content
memcpy(rOpInput, iPadKey, DIGESTSIZE);
memcpy(rOpInput+DIGESTSIZE,content,strlen(content));
//SHA1 (iPad||content) gives the rightmost 20 bytes of the final SHA1 Input
byte *rOp=malloc(DIGESTSIZE); // H(iPad||content)
SHA1(rOpInput,strlen(content)+DIGESTSIZE,rOp);
free(rOpInput);
byte *finalInput = malloc(2*DIGESTSIZE); //oPad||H(iPad||content)
//concat oPad and H(ipad||content)
memcpy(finalInput, oPadKey,DIGESTSIZE);
memcpy(finalInput+DIGESTSIZE,rOp,DIGESTSIZE);
free(rOp);
free(oPadKey);
free(iPadKey);
//SHA1(oPad||H(iPad||content))
byte *hmac = malloc(DIGESTSIZE);
SHA1(finalInput,40,hmac);
free(finalInput);
//print calculated HMAC as HEX
for(i=0;i<DIGESTSIZE;i++){
printf("%02X", (hmac[i] & 0xFF ));
}
printf("\n");
//verify with openssl HMAC
byte *result = HMAC(EVP_sha1(), derivedKey, DIGESTSIZE, content, strlen(content), NULL, NULL);
for(i=0;i<DIGESTSIZE;i++){
printf("%02X", (result[i] & 0xFF ));
}
printf("\n");
return 0;
}
The bug is simple. The ipad and opad need to be (input) block size in length, not the length of the digest output. I.e. they must be 512 bits (64 bytes) for SHA-1, not 20.
I.e.
#define INPUT_BLOCK_SIZE 64
byte derivedKey[64] = {0x42,0xA9,0x78,0x90,0xFC,0xE5,0x16,0x8E,0x58,0x12,
0x2F,0xF1,0xBA,0x32,0x5F,0x09,0x88,0x94,0x02,0x91};
// null-padded on the right
and then change the DIGESTSIZE to INPUT_BLOCK_SIZE for wherever the length of ipad, opad is needed.
Result:
4303E965D88D288C9AC594CE6C5E6AFF27D40B2D
From Wikipedia
K' is another secret key, derived from the original key K (by padding K to the right with extra zeroes to the input block size of the hash function, or by hashing K if it is longer than that block size)
(emphasis mine).
P.S. It would be better to use the SHA1_Init/Update/Final, as then lots of copying would be skipped. You can also avoid allocating memory for the ipad and opad separately by first calculating ipad and after it has been used, then xorring in place by 0x6a to get the opad.
Please refer this. You can easily calculate the hash
https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA1.pdf
#define IPAD 0x36
#define OPAD 0x5c
#define DIGESTSIZE 20
#define INPUT_BLOCK_SIZE 64
#define byte unsigned char
static byte mykey[64];
int main () {
int i;
byte mykey[INPUT_BLOCK_SIZE] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F };
byte content[] = {0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x6D, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x20, 0x66, 0x6F, 0x72, 0x20, 0x6B, 0x65, 0x79, 0x6C, 0x65, 0x6E, 0x3D, 0x62, 0x6C, 0x6F, 0x63, 0x6B, 0x6C, 0x65, 0x6E };
byte k0_ipad_text[sizeof(content) + INPUT_BLOCK_SIZE];
byte hmac[DIGESTSIZE];
byte k0_opad_hash[INPUT_BLOCK_SIZE + DIGESTSIZE];
/*
* K0 is mykey as size is 0x40 = 64 bytes
*/
byte *oPadKey = malloc(INPUT_BLOCK_SIZE);
byte *iPadKey = malloc(INPUT_BLOCK_SIZE);
memset(oPadKey, 0, INPUT_BLOCK_SIZE);
memset(iPadKey, 0, INPUT_BLOCK_SIZE);
for(i=0;i<INPUT_BLOCK_SIZE;i++){
iPadKey[i]=mykey[i]^IPAD;
oPadKey[i]=mykey[i]^OPAD;
}
printf("\n iPad key = ");
for(i=0;i<INPUT_BLOCK_SIZE;i++){
printf("%02x ", iPadKey[i]);
}
printf("\n oPad key = ");
for(i=0;i<INPUT_BLOCK_SIZE;i++){
printf("%02x ", oPadKey[i]);
}
//Key^ipad)||text)
memcpy(k0_ipad_text, iPadKey, INPUT_BLOCK_SIZE);
memcpy(k0_ipad_text + INPUT_BLOCK_SIZE, content, sizeof(content));
SHA1(k0_ipad_text, INPUT_BLOCK_SIZE + sizeof(content), hmac);
#if 0
printf("\n hmac = ");
for(i=0;i<DIGESTSIZE;i++){
printf("%02x ", hmac[i]);
}
#endif
memcpy(k0_opad_hash, oPadKey, INPUT_BLOCK_SIZE);
memcpy(k0_opad_hash + INPUT_BLOCK_SIZE, hmac, DIGESTSIZE);
SHA1(k0_opad_hash, INPUT_BLOCK_SIZE + DIGESTSIZE, hmac);
printf("\n hmac \n");
for(i=0;i<DIGESTSIZE;i++){
printf("%02x", hmac[i]);
}
printf("\n");
//verify with openssl HMAC
byte *result = HMAC(EVP_sha1(), mykey, INPUT_BLOCK_SIZE, content, sizeof(content), NULL, NULL);
for(i=0;i<DIGESTSIZE;i++){
printf("%02X", (result[i] & 0xFF ));
}
printf("\n");
return 0;
}

Resources